]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
* parser.c: Fix comment typos.
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38
39 \f
40 /* The lexer. */
41
42 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
43 and c-lex.c) and the C++ parser. */
44
45 /* A C++ token. */
46
47 typedef struct cp_token GTY (())
48 {
49 /* The kind of token. */
50 ENUM_BITFIELD (cpp_ttype) type : 8;
51 /* If this token is a keyword, this value indicates which keyword.
52 Otherwise, this value is RID_MAX. */
53 ENUM_BITFIELD (rid) keyword : 8;
54 /* Token flags. */
55 unsigned char flags;
56 /* True if this token is from a system header. */
57 BOOL_BITFIELD in_system_header : 1;
58 /* True if this token is from a context where it is implicitly extern "C" */
59 BOOL_BITFIELD implicit_extern_c : 1;
60 /* The value associated with this token, if any. */
61 tree value;
62 /* The location at which this token was found. */
63 location_t location;
64 } cp_token;
65
66 /* We use a stack of token pointer for saving token sets. */
67 typedef struct cp_token *cp_token_position;
68 DEF_VEC_MALLOC_P (cp_token_position);
69
70 static const cp_token eof_token =
71 {
72 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
73 #if USE_MAPPED_LOCATION
74 0
75 #else
76 {0, 0}
77 #endif
78 };
79
80 /* The cp_lexer structure represents the C++ lexer. It is responsible
81 for managing the token stream from the preprocessor and supplying
82 it to the parser. Tokens are never added to the cp_lexer after
83 it is created. */
84
85 typedef struct cp_lexer GTY (())
86 {
87 /* The memory allocated for the buffer. NULL if this lexer does not
88 own the token buffer. */
89 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
90 /* If the lexer owns the buffer, this is the number of tokens in the
91 buffer. */
92 size_t buffer_length;
93
94 /* A pointer just past the last available token. The tokens
95 in this lexer are [buffer, last_token). */
96 cp_token_position GTY ((skip)) last_token;
97
98 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
99 no more available tokens. */
100 cp_token_position GTY ((skip)) next_token;
101
102 /* A stack indicating positions at which cp_lexer_save_tokens was
103 called. The top entry is the most recent position at which we
104 began saving tokens. If the stack is non-empty, we are saving
105 tokens. */
106 VEC (cp_token_position) *GTY ((skip)) saved_tokens;
107
108 /* True if we should output debugging information. */
109 bool debugging_p;
110
111 /* The next lexer in a linked list of lexers. */
112 struct cp_lexer *next;
113 } cp_lexer;
114
115 /* cp_token_cache is a range of tokens. There is no need to represent
116 allocate heap memory for it, since tokens are never removed from the
117 lexer's array. There is also no need for the GC to walk through
118 a cp_token_cache, since everything in here is referenced through
119 a lexer. */
120
121 typedef struct cp_token_cache GTY(())
122 {
123 /* The beginning of the token range. */
124 cp_token * GTY((skip)) first;
125
126 /* Points immediately after the last token in the range. */
127 cp_token * GTY ((skip)) last;
128 } cp_token_cache;
129
130 /* Prototypes. */
131
132 static cp_lexer *cp_lexer_new_main
133 (void);
134 static cp_lexer *cp_lexer_new_from_tokens
135 (cp_token_cache *tokens);
136 static void cp_lexer_destroy
137 (cp_lexer *);
138 static int cp_lexer_saving_tokens
139 (const cp_lexer *);
140 static cp_token_position cp_lexer_token_position
141 (cp_lexer *, bool);
142 static cp_token *cp_lexer_token_at
143 (cp_lexer *, cp_token_position);
144 static void cp_lexer_get_preprocessor_token
145 (cp_lexer *, cp_token *);
146 static inline cp_token *cp_lexer_peek_token
147 (cp_lexer *);
148 static cp_token *cp_lexer_peek_nth_token
149 (cp_lexer *, size_t);
150 static inline bool cp_lexer_next_token_is
151 (cp_lexer *, enum cpp_ttype);
152 static bool cp_lexer_next_token_is_not
153 (cp_lexer *, enum cpp_ttype);
154 static bool cp_lexer_next_token_is_keyword
155 (cp_lexer *, enum rid);
156 static cp_token *cp_lexer_consume_token
157 (cp_lexer *);
158 static void cp_lexer_purge_token
159 (cp_lexer *);
160 static void cp_lexer_purge_tokens_after
161 (cp_lexer *, cp_token_position);
162 static void cp_lexer_handle_pragma
163 (cp_lexer *);
164 static void cp_lexer_save_tokens
165 (cp_lexer *);
166 static void cp_lexer_commit_tokens
167 (cp_lexer *);
168 static void cp_lexer_rollback_tokens
169 (cp_lexer *);
170 #ifdef ENABLE_CHECKING
171 static void cp_lexer_print_token
172 (FILE *, cp_token *);
173 static inline bool cp_lexer_debugging_p
174 (cp_lexer *);
175 static void cp_lexer_start_debugging
176 (cp_lexer *) ATTRIBUTE_UNUSED;
177 static void cp_lexer_stop_debugging
178 (cp_lexer *) ATTRIBUTE_UNUSED;
179 #else
180 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
181 about passing NULL to functions that require non-NULL arguments
182 (fputs, fprintf). It will never be used, so all we need is a value
183 of the right type that's guaranteed not to be NULL. */
184 #define cp_lexer_debug_stream stdout
185 #define cp_lexer_print_token(str, tok) (void) 0
186 #define cp_lexer_debugging_p(lexer) 0
187 #endif /* ENABLE_CHECKING */
188
189 static cp_token_cache *cp_token_cache_new
190 (cp_token *, cp_token *);
191
192 /* Manifest constants. */
193 #define CP_LEXER_BUFFER_SIZE 10000
194 #define CP_SAVED_TOKEN_STACK 5
195
196 /* A token type for keywords, as opposed to ordinary identifiers. */
197 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
198
199 /* A token type for template-ids. If a template-id is processed while
200 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
201 the value of the CPP_TEMPLATE_ID is whatever was returned by
202 cp_parser_template_id. */
203 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
204
205 /* A token type for nested-name-specifiers. If a
206 nested-name-specifier is processed while parsing tentatively, it is
207 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
208 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
209 cp_parser_nested_name_specifier_opt. */
210 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
211
212 /* A token type for tokens that are not tokens at all; these are used
213 to represent slots in the array where there used to be a token
214 that has now been deleted. */
215 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
216
217 /* The number of token types, including C++-specific ones. */
218 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
219
220 /* Variables. */
221
222 #ifdef ENABLE_CHECKING
223 /* The stream to which debugging output should be written. */
224 static FILE *cp_lexer_debug_stream;
225 #endif /* ENABLE_CHECKING */
226
227 /* Create a new main C++ lexer, the lexer that gets tokens from the
228 preprocessor. */
229
230 static cp_lexer *
231 cp_lexer_new_main (void)
232 {
233 cp_token first_token;
234 cp_lexer *lexer;
235 cp_token *pos;
236 size_t alloc;
237 size_t space;
238 cp_token *buffer;
239
240 /* Tell cpplib we want CPP_PRAGMA tokens. */
241 cpp_get_options (parse_in)->defer_pragmas = true;
242
243 /* Tell c_lex not to merge string constants. */
244 c_lex_return_raw_strings = true;
245
246 /* It's possible that lexing the first token will load a PCH file,
247 which is a GC collection point. So we have to grab the first
248 token before allocating any memory. */
249 cp_lexer_get_preprocessor_token (NULL, &first_token);
250 c_common_no_more_pch ();
251
252 /* Allocate the memory. */
253 lexer = GGC_CNEW (cp_lexer);
254
255 #ifdef ENABLE_CHECKING
256 /* Initially we are not debugging. */
257 lexer->debugging_p = false;
258 #endif /* ENABLE_CHECKING */
259 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
260
261 /* Create the buffer. */
262 alloc = CP_LEXER_BUFFER_SIZE;
263 buffer = ggc_alloc (alloc * sizeof (cp_token));
264
265 /* Put the first token in the buffer. */
266 space = alloc;
267 pos = buffer;
268 *pos = first_token;
269
270 /* Get the remaining tokens from the preprocessor. */
271 while (pos->type != CPP_EOF)
272 {
273 pos++;
274 if (!--space)
275 {
276 space = alloc;
277 alloc *= 2;
278 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
279 pos = buffer + space;
280 }
281 cp_lexer_get_preprocessor_token (lexer, pos);
282 }
283 lexer->buffer = buffer;
284 lexer->buffer_length = alloc - space;
285 lexer->last_token = pos;
286 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
287
288 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
289 direct calls to c_lex. Those callers all expect c_lex to do
290 string constant concatenation. */
291 c_lex_return_raw_strings = false;
292
293 gcc_assert (lexer->next_token->type != CPP_PURGED);
294 return lexer;
295 }
296
297 /* Create a new lexer whose token stream is primed with the tokens in
298 CACHE. When these tokens are exhausted, no new tokens will be read. */
299
300 static cp_lexer *
301 cp_lexer_new_from_tokens (cp_token_cache *cache)
302 {
303 cp_token *first = cache->first;
304 cp_token *last = cache->last;
305 cp_lexer *lexer = GGC_CNEW (cp_lexer);
306
307 /* We do not own the buffer. */
308 lexer->buffer = NULL;
309 lexer->buffer_length = 0;
310 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
311 lexer->last_token = last;
312
313 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
314
315 #ifdef ENABLE_CHECKING
316 /* Initially we are not debugging. */
317 lexer->debugging_p = false;
318 #endif
319
320 gcc_assert (lexer->next_token->type != CPP_PURGED);
321 return lexer;
322 }
323
324 /* Frees all resources associated with LEXER. */
325
326 static void
327 cp_lexer_destroy (cp_lexer *lexer)
328 {
329 if (lexer->buffer)
330 ggc_free (lexer->buffer);
331 VEC_free (cp_token_position, lexer->saved_tokens);
332 ggc_free (lexer);
333 }
334
335 /* Returns nonzero if debugging information should be output. */
336
337 #ifdef ENABLE_CHECKING
338
339 static inline bool
340 cp_lexer_debugging_p (cp_lexer *lexer)
341 {
342 return lexer->debugging_p;
343 }
344
345 #endif /* ENABLE_CHECKING */
346
347 static inline cp_token_position
348 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
349 {
350 gcc_assert (!previous_p || lexer->next_token != &eof_token);
351
352 return lexer->next_token - previous_p;
353 }
354
355 static inline cp_token *
356 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
357 {
358 return pos;
359 }
360
361 /* nonzero if we are presently saving tokens. */
362
363 static inline int
364 cp_lexer_saving_tokens (const cp_lexer* lexer)
365 {
366 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
367 }
368
369 /* Store the next token from the preprocessor in *TOKEN. Return true
370 if we reach EOF. */
371
372 static void
373 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
374 cp_token *token)
375 {
376 static int is_extern_c = 0;
377
378 /* Get a new token from the preprocessor. */
379 token->type = c_lex_with_flags (&token->value, &token->flags);
380 token->location = input_location;
381 token->in_system_header = in_system_header;
382
383 /* On some systems, some header files are surrounded by an
384 implicit extern "C" block. Set a flag in the token if it
385 comes from such a header. */
386 is_extern_c += pending_lang_change;
387 pending_lang_change = 0;
388 token->implicit_extern_c = is_extern_c > 0;
389
390 /* Check to see if this token is a keyword. */
391 if (token->type == CPP_NAME
392 && C_IS_RESERVED_WORD (token->value))
393 {
394 /* Mark this token as a keyword. */
395 token->type = CPP_KEYWORD;
396 /* Record which keyword. */
397 token->keyword = C_RID_CODE (token->value);
398 /* Update the value. Some keywords are mapped to particular
399 entities, rather than simply having the value of the
400 corresponding IDENTIFIER_NODE. For example, `__const' is
401 mapped to `const'. */
402 token->value = ridpointers[token->keyword];
403 }
404 else
405 token->keyword = RID_MAX;
406 }
407
408 /* Update the globals input_location and in_system_header from TOKEN. */
409 static inline void
410 cp_lexer_set_source_position_from_token (cp_token *token)
411 {
412 if (token->type != CPP_EOF)
413 {
414 input_location = token->location;
415 in_system_header = token->in_system_header;
416 }
417 }
418
419 /* Return a pointer to the next token in the token stream, but do not
420 consume it. */
421
422 static inline cp_token *
423 cp_lexer_peek_token (cp_lexer *lexer)
424 {
425 if (cp_lexer_debugging_p (lexer))
426 {
427 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
428 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
429 putc ('\n', cp_lexer_debug_stream);
430 }
431 return lexer->next_token;
432 }
433
434 /* Return true if the next token has the indicated TYPE. */
435
436 static inline bool
437 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
438 {
439 return cp_lexer_peek_token (lexer)->type == type;
440 }
441
442 /* Return true if the next token does not have the indicated TYPE. */
443
444 static inline bool
445 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
446 {
447 return !cp_lexer_next_token_is (lexer, type);
448 }
449
450 /* Return true if the next token is the indicated KEYWORD. */
451
452 static inline bool
453 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
454 {
455 cp_token *token;
456
457 /* Peek at the next token. */
458 token = cp_lexer_peek_token (lexer);
459 /* Check to see if it is the indicated keyword. */
460 return token->keyword == keyword;
461 }
462
463 /* Return a pointer to the Nth token in the token stream. If N is 1,
464 then this is precisely equivalent to cp_lexer_peek_token (except
465 that it is not inline). One would like to disallow that case, but
466 there is one case (cp_parser_nth_token_starts_template_id) where
467 the caller passes a variable for N and it might be 1. */
468
469 static cp_token *
470 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
471 {
472 cp_token *token;
473
474 /* N is 1-based, not zero-based. */
475 gcc_assert (n > 0 && lexer->next_token != &eof_token);
476
477 if (cp_lexer_debugging_p (lexer))
478 fprintf (cp_lexer_debug_stream,
479 "cp_lexer: peeking ahead %ld at token: ", (long)n);
480
481 --n;
482 token = lexer->next_token;
483 while (n != 0)
484 {
485 ++token;
486 if (token == lexer->last_token)
487 {
488 token = (cp_token *)&eof_token;
489 break;
490 }
491
492 if (token->type != CPP_PURGED)
493 --n;
494 }
495
496 if (cp_lexer_debugging_p (lexer))
497 {
498 cp_lexer_print_token (cp_lexer_debug_stream, token);
499 putc ('\n', cp_lexer_debug_stream);
500 }
501
502 return token;
503 }
504
505 /* Return the next token, and advance the lexer's next_token pointer
506 to point to the next non-purged token. */
507
508 static cp_token *
509 cp_lexer_consume_token (cp_lexer* lexer)
510 {
511 cp_token *token = lexer->next_token;
512
513 gcc_assert (token != &eof_token);
514
515 do
516 {
517 lexer->next_token++;
518 if (lexer->next_token == lexer->last_token)
519 {
520 lexer->next_token = (cp_token *)&eof_token;
521 break;
522 }
523
524 }
525 while (lexer->next_token->type == CPP_PURGED);
526
527 cp_lexer_set_source_position_from_token (token);
528
529 /* Provide debugging output. */
530 if (cp_lexer_debugging_p (lexer))
531 {
532 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
533 cp_lexer_print_token (cp_lexer_debug_stream, token);
534 putc ('\n', cp_lexer_debug_stream);
535 }
536
537 return token;
538 }
539
540 /* Permanently remove the next token from the token stream, and
541 advance the next_token pointer to refer to the next non-purged
542 token. */
543
544 static void
545 cp_lexer_purge_token (cp_lexer *lexer)
546 {
547 cp_token *tok = lexer->next_token;
548
549 gcc_assert (tok != &eof_token);
550 tok->type = CPP_PURGED;
551 tok->location = UNKNOWN_LOCATION;
552 tok->value = NULL_TREE;
553 tok->keyword = RID_MAX;
554
555 do
556 {
557 tok++;
558 if (tok == lexer->last_token)
559 {
560 tok = (cp_token *)&eof_token;
561 break;
562 }
563 }
564 while (tok->type == CPP_PURGED);
565 lexer->next_token = tok;
566 }
567
568 /* Permanently remove all tokens after TOK, up to, but not
569 including, the token that will be returned next by
570 cp_lexer_peek_token. */
571
572 static void
573 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
574 {
575 cp_token *peek = lexer->next_token;
576
577 if (peek == &eof_token)
578 peek = lexer->last_token;
579
580 gcc_assert (tok < peek);
581
582 for ( tok += 1; tok != peek; tok += 1)
583 {
584 tok->type = CPP_PURGED;
585 tok->location = UNKNOWN_LOCATION;
586 tok->value = NULL_TREE;
587 tok->keyword = RID_MAX;
588 }
589 }
590
591 /* Consume and handle a pragma token. */
592 static void
593 cp_lexer_handle_pragma (cp_lexer *lexer)
594 {
595 cpp_string s;
596 cp_token *token = cp_lexer_consume_token (lexer);
597 gcc_assert (token->type == CPP_PRAGMA);
598 gcc_assert (token->value);
599
600 s.len = TREE_STRING_LENGTH (token->value);
601 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
602
603 cpp_handle_deferred_pragma (parse_in, &s);
604
605 /* Clearing token->value here means that we will get an ICE if we
606 try to process this #pragma again (which should be impossible). */
607 token->value = NULL;
608 }
609
610 /* Begin saving tokens. All tokens consumed after this point will be
611 preserved. */
612
613 static void
614 cp_lexer_save_tokens (cp_lexer* lexer)
615 {
616 /* Provide debugging output. */
617 if (cp_lexer_debugging_p (lexer))
618 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
619
620 VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
621 }
622
623 /* Commit to the portion of the token stream most recently saved. */
624
625 static void
626 cp_lexer_commit_tokens (cp_lexer* lexer)
627 {
628 /* Provide debugging output. */
629 if (cp_lexer_debugging_p (lexer))
630 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
631
632 VEC_pop (cp_token_position, lexer->saved_tokens);
633 }
634
635 /* Return all tokens saved since the last call to cp_lexer_save_tokens
636 to the token stream. Stop saving tokens. */
637
638 static void
639 cp_lexer_rollback_tokens (cp_lexer* lexer)
640 {
641 /* Provide debugging output. */
642 if (cp_lexer_debugging_p (lexer))
643 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
644
645 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
646 }
647
648 /* Print a representation of the TOKEN on the STREAM. */
649
650 #ifdef ENABLE_CHECKING
651
652 static void
653 cp_lexer_print_token (FILE * stream, cp_token *token)
654 {
655 /* We don't use cpp_type2name here because the parser defines
656 a few tokens of its own. */
657 static const char *const token_names[] = {
658 /* cpplib-defined token types */
659 #define OP(e, s) #e,
660 #define TK(e, s) #e,
661 TTYPE_TABLE
662 #undef OP
663 #undef TK
664 /* C++ parser token types - see "Manifest constants", above. */
665 "KEYWORD",
666 "TEMPLATE_ID",
667 "NESTED_NAME_SPECIFIER",
668 "PURGED"
669 };
670
671 /* If we have a name for the token, print it out. Otherwise, we
672 simply give the numeric code. */
673 gcc_assert (token->type < ARRAY_SIZE(token_names));
674 fputs (token_names[token->type], stream);
675
676 /* For some tokens, print the associated data. */
677 switch (token->type)
678 {
679 case CPP_KEYWORD:
680 /* Some keywords have a value that is not an IDENTIFIER_NODE.
681 For example, `struct' is mapped to an INTEGER_CST. */
682 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
683 break;
684 /* else fall through */
685 case CPP_NAME:
686 fputs (IDENTIFIER_POINTER (token->value), stream);
687 break;
688
689 case CPP_STRING:
690 case CPP_WSTRING:
691 case CPP_PRAGMA:
692 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
693 break;
694
695 default:
696 break;
697 }
698 }
699
700 /* Start emitting debugging information. */
701
702 static void
703 cp_lexer_start_debugging (cp_lexer* lexer)
704 {
705 ++lexer->debugging_p;
706 }
707
708 /* Stop emitting debugging information. */
709
710 static void
711 cp_lexer_stop_debugging (cp_lexer* lexer)
712 {
713 --lexer->debugging_p;
714 }
715
716 #endif /* ENABLE_CHECKING */
717
718 /* Create a new cp_token_cache, representing a range of tokens. */
719
720 static cp_token_cache *
721 cp_token_cache_new (cp_token *first, cp_token *last)
722 {
723 cp_token_cache *cache = GGC_NEW (cp_token_cache);
724 cache->first = first;
725 cache->last = last;
726 return cache;
727 }
728
729 \f
730 /* Decl-specifiers. */
731
732 static void clear_decl_specs
733 (cp_decl_specifier_seq *);
734
735 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
736
737 static void
738 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
739 {
740 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
741 }
742
743 /* Declarators. */
744
745 /* Nothing other than the parser should be creating declarators;
746 declarators are a semi-syntactic representation of C++ entities.
747 Other parts of the front end that need to create entities (like
748 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
749
750 static cp_declarator *make_id_declarator
751 (tree);
752 static cp_declarator *make_call_declarator
753 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
754 static cp_declarator *make_array_declarator
755 (cp_declarator *, tree);
756 static cp_declarator *make_pointer_declarator
757 (cp_cv_quals, cp_declarator *);
758 static cp_declarator *make_reference_declarator
759 (cp_cv_quals, cp_declarator *);
760 static cp_parameter_declarator *make_parameter_declarator
761 (cp_decl_specifier_seq *, cp_declarator *, tree);
762 static cp_declarator *make_ptrmem_declarator
763 (cp_cv_quals, tree, cp_declarator *);
764
765 cp_declarator *cp_error_declarator;
766
767 /* The obstack on which declarators and related data structures are
768 allocated. */
769 static struct obstack declarator_obstack;
770
771 /* Alloc BYTES from the declarator memory pool. */
772
773 static inline void *
774 alloc_declarator (size_t bytes)
775 {
776 return obstack_alloc (&declarator_obstack, bytes);
777 }
778
779 /* Allocate a declarator of the indicated KIND. Clear fields that are
780 common to all declarators. */
781
782 static cp_declarator *
783 make_declarator (cp_declarator_kind kind)
784 {
785 cp_declarator *declarator;
786
787 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
788 declarator->kind = kind;
789 declarator->attributes = NULL_TREE;
790 declarator->declarator = NULL;
791
792 return declarator;
793 }
794
795 /* Make a declarator for a generalized identifier. */
796
797 cp_declarator *
798 make_id_declarator (tree id)
799 {
800 cp_declarator *declarator;
801
802 declarator = make_declarator (cdk_id);
803 declarator->u.id.name = id;
804 declarator->u.id.sfk = sfk_none;
805
806 return declarator;
807 }
808
809 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
810 of modifiers such as const or volatile to apply to the pointer
811 type, represented as identifiers. */
812
813 cp_declarator *
814 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
815 {
816 cp_declarator *declarator;
817
818 declarator = make_declarator (cdk_pointer);
819 declarator->declarator = target;
820 declarator->u.pointer.qualifiers = cv_qualifiers;
821 declarator->u.pointer.class_type = NULL_TREE;
822
823 return declarator;
824 }
825
826 /* Like make_pointer_declarator -- but for references. */
827
828 cp_declarator *
829 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
830 {
831 cp_declarator *declarator;
832
833 declarator = make_declarator (cdk_reference);
834 declarator->declarator = target;
835 declarator->u.pointer.qualifiers = cv_qualifiers;
836 declarator->u.pointer.class_type = NULL_TREE;
837
838 return declarator;
839 }
840
841 /* Like make_pointer_declarator -- but for a pointer to a non-static
842 member of CLASS_TYPE. */
843
844 cp_declarator *
845 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
846 cp_declarator *pointee)
847 {
848 cp_declarator *declarator;
849
850 declarator = make_declarator (cdk_ptrmem);
851 declarator->declarator = pointee;
852 declarator->u.pointer.qualifiers = cv_qualifiers;
853 declarator->u.pointer.class_type = class_type;
854
855 return declarator;
856 }
857
858 /* Make a declarator for the function given by TARGET, with the
859 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
860 "const"-qualified member function. The EXCEPTION_SPECIFICATION
861 indicates what exceptions can be thrown. */
862
863 cp_declarator *
864 make_call_declarator (cp_declarator *target,
865 cp_parameter_declarator *parms,
866 cp_cv_quals cv_qualifiers,
867 tree exception_specification)
868 {
869 cp_declarator *declarator;
870
871 declarator = make_declarator (cdk_function);
872 declarator->declarator = target;
873 declarator->u.function.parameters = parms;
874 declarator->u.function.qualifiers = cv_qualifiers;
875 declarator->u.function.exception_specification = exception_specification;
876
877 return declarator;
878 }
879
880 /* Make a declarator for an array of BOUNDS elements, each of which is
881 defined by ELEMENT. */
882
883 cp_declarator *
884 make_array_declarator (cp_declarator *element, tree bounds)
885 {
886 cp_declarator *declarator;
887
888 declarator = make_declarator (cdk_array);
889 declarator->declarator = element;
890 declarator->u.array.bounds = bounds;
891
892 return declarator;
893 }
894
895 cp_parameter_declarator *no_parameters;
896
897 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
898 DECLARATOR and DEFAULT_ARGUMENT. */
899
900 cp_parameter_declarator *
901 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
902 cp_declarator *declarator,
903 tree default_argument)
904 {
905 cp_parameter_declarator *parameter;
906
907 parameter = ((cp_parameter_declarator *)
908 alloc_declarator (sizeof (cp_parameter_declarator)));
909 parameter->next = NULL;
910 if (decl_specifiers)
911 parameter->decl_specifiers = *decl_specifiers;
912 else
913 clear_decl_specs (&parameter->decl_specifiers);
914 parameter->declarator = declarator;
915 parameter->default_argument = default_argument;
916 parameter->ellipsis_p = false;
917
918 return parameter;
919 }
920
921 /* The parser. */
922
923 /* Overview
924 --------
925
926 A cp_parser parses the token stream as specified by the C++
927 grammar. Its job is purely parsing, not semantic analysis. For
928 example, the parser breaks the token stream into declarators,
929 expressions, statements, and other similar syntactic constructs.
930 It does not check that the types of the expressions on either side
931 of an assignment-statement are compatible, or that a function is
932 not declared with a parameter of type `void'.
933
934 The parser invokes routines elsewhere in the compiler to perform
935 semantic analysis and to build up the abstract syntax tree for the
936 code processed.
937
938 The parser (and the template instantiation code, which is, in a
939 way, a close relative of parsing) are the only parts of the
940 compiler that should be calling push_scope and pop_scope, or
941 related functions. The parser (and template instantiation code)
942 keeps track of what scope is presently active; everything else
943 should simply honor that. (The code that generates static
944 initializers may also need to set the scope, in order to check
945 access control correctly when emitting the initializers.)
946
947 Methodology
948 -----------
949
950 The parser is of the standard recursive-descent variety. Upcoming
951 tokens in the token stream are examined in order to determine which
952 production to use when parsing a non-terminal. Some C++ constructs
953 require arbitrary look ahead to disambiguate. For example, it is
954 impossible, in the general case, to tell whether a statement is an
955 expression or declaration without scanning the entire statement.
956 Therefore, the parser is capable of "parsing tentatively." When the
957 parser is not sure what construct comes next, it enters this mode.
958 Then, while we attempt to parse the construct, the parser queues up
959 error messages, rather than issuing them immediately, and saves the
960 tokens it consumes. If the construct is parsed successfully, the
961 parser "commits", i.e., it issues any queued error messages and
962 the tokens that were being preserved are permanently discarded.
963 If, however, the construct is not parsed successfully, the parser
964 rolls back its state completely so that it can resume parsing using
965 a different alternative.
966
967 Future Improvements
968 -------------------
969
970 The performance of the parser could probably be improved substantially.
971 We could often eliminate the need to parse tentatively by looking ahead
972 a little bit. In some places, this approach might not entirely eliminate
973 the need to parse tentatively, but it might still speed up the average
974 case. */
975
976 /* Flags that are passed to some parsing functions. These values can
977 be bitwise-ored together. */
978
979 typedef enum cp_parser_flags
980 {
981 /* No flags. */
982 CP_PARSER_FLAGS_NONE = 0x0,
983 /* The construct is optional. If it is not present, then no error
984 should be issued. */
985 CP_PARSER_FLAGS_OPTIONAL = 0x1,
986 /* When parsing a type-specifier, do not allow user-defined types. */
987 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
988 } cp_parser_flags;
989
990 /* The different kinds of declarators we want to parse. */
991
992 typedef enum cp_parser_declarator_kind
993 {
994 /* We want an abstract declarator. */
995 CP_PARSER_DECLARATOR_ABSTRACT,
996 /* We want a named declarator. */
997 CP_PARSER_DECLARATOR_NAMED,
998 /* We don't mind, but the name must be an unqualified-id. */
999 CP_PARSER_DECLARATOR_EITHER
1000 } cp_parser_declarator_kind;
1001
1002 /* The precedence values used to parse binary expressions. The minimum value
1003 of PREC must be 1, because zero is reserved to quickly discriminate
1004 binary operators from other tokens. */
1005
1006 enum cp_parser_prec
1007 {
1008 PREC_NOT_OPERATOR,
1009 PREC_LOGICAL_OR_EXPRESSION,
1010 PREC_LOGICAL_AND_EXPRESSION,
1011 PREC_INCLUSIVE_OR_EXPRESSION,
1012 PREC_EXCLUSIVE_OR_EXPRESSION,
1013 PREC_AND_EXPRESSION,
1014 PREC_EQUALITY_EXPRESSION,
1015 PREC_RELATIONAL_EXPRESSION,
1016 PREC_SHIFT_EXPRESSION,
1017 PREC_ADDITIVE_EXPRESSION,
1018 PREC_MULTIPLICATIVE_EXPRESSION,
1019 PREC_PM_EXPRESSION,
1020 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1021 };
1022
1023 /* A mapping from a token type to a corresponding tree node type, with a
1024 precedence value. */
1025
1026 typedef struct cp_parser_binary_operations_map_node
1027 {
1028 /* The token type. */
1029 enum cpp_ttype token_type;
1030 /* The corresponding tree code. */
1031 enum tree_code tree_type;
1032 /* The precedence of this operator. */
1033 enum cp_parser_prec prec;
1034 } cp_parser_binary_operations_map_node;
1035
1036 /* The status of a tentative parse. */
1037
1038 typedef enum cp_parser_status_kind
1039 {
1040 /* No errors have occurred. */
1041 CP_PARSER_STATUS_KIND_NO_ERROR,
1042 /* An error has occurred. */
1043 CP_PARSER_STATUS_KIND_ERROR,
1044 /* We are committed to this tentative parse, whether or not an error
1045 has occurred. */
1046 CP_PARSER_STATUS_KIND_COMMITTED
1047 } cp_parser_status_kind;
1048
1049 typedef struct cp_parser_expression_stack_entry
1050 {
1051 tree lhs;
1052 enum tree_code tree_type;
1053 int prec;
1054 } cp_parser_expression_stack_entry;
1055
1056 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1057 entries because precedence levels on the stack are monotonically
1058 increasing. */
1059 typedef struct cp_parser_expression_stack_entry
1060 cp_parser_expression_stack[NUM_PREC_VALUES];
1061
1062 /* Context that is saved and restored when parsing tentatively. */
1063 typedef struct cp_parser_context GTY (())
1064 {
1065 /* If this is a tentative parsing context, the status of the
1066 tentative parse. */
1067 enum cp_parser_status_kind status;
1068 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1069 that are looked up in this context must be looked up both in the
1070 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1071 the context of the containing expression. */
1072 tree object_type;
1073
1074 /* The next parsing context in the stack. */
1075 struct cp_parser_context *next;
1076 } cp_parser_context;
1077
1078 /* Prototypes. */
1079
1080 /* Constructors and destructors. */
1081
1082 static cp_parser_context *cp_parser_context_new
1083 (cp_parser_context *);
1084
1085 /* Class variables. */
1086
1087 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1088
1089 /* The operator-precedence table used by cp_parser_binary_expression.
1090 Transformed into an associative array (binops_by_token) by
1091 cp_parser_new. */
1092
1093 static const cp_parser_binary_operations_map_node binops[] = {
1094 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1095 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1096
1097 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1098 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1099 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1100
1101 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1102 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1103
1104 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1105 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1106
1107 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1108 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1109 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1110 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1111 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1112 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1113
1114 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1115 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1116
1117 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1118
1119 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1120
1121 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1122
1123 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1124
1125 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1126 };
1127
1128 /* The same as binops, but initialized by cp_parser_new so that
1129 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1130 for speed. */
1131 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1132
1133 /* Constructors and destructors. */
1134
1135 /* Construct a new context. The context below this one on the stack
1136 is given by NEXT. */
1137
1138 static cp_parser_context *
1139 cp_parser_context_new (cp_parser_context* next)
1140 {
1141 cp_parser_context *context;
1142
1143 /* Allocate the storage. */
1144 if (cp_parser_context_free_list != NULL)
1145 {
1146 /* Pull the first entry from the free list. */
1147 context = cp_parser_context_free_list;
1148 cp_parser_context_free_list = context->next;
1149 memset (context, 0, sizeof (*context));
1150 }
1151 else
1152 context = GGC_CNEW (cp_parser_context);
1153
1154 /* No errors have occurred yet in this context. */
1155 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1156 /* If this is not the bottomost context, copy information that we
1157 need from the previous context. */
1158 if (next)
1159 {
1160 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1161 expression, then we are parsing one in this context, too. */
1162 context->object_type = next->object_type;
1163 /* Thread the stack. */
1164 context->next = next;
1165 }
1166
1167 return context;
1168 }
1169
1170 /* The cp_parser structure represents the C++ parser. */
1171
1172 typedef struct cp_parser GTY(())
1173 {
1174 /* The lexer from which we are obtaining tokens. */
1175 cp_lexer *lexer;
1176
1177 /* The scope in which names should be looked up. If NULL_TREE, then
1178 we look up names in the scope that is currently open in the
1179 source program. If non-NULL, this is either a TYPE or
1180 NAMESPACE_DECL for the scope in which we should look.
1181
1182 This value is not cleared automatically after a name is looked
1183 up, so we must be careful to clear it before starting a new look
1184 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1185 will look up `Z' in the scope of `X', rather than the current
1186 scope.) Unfortunately, it is difficult to tell when name lookup
1187 is complete, because we sometimes peek at a token, look it up,
1188 and then decide not to consume it. */
1189 tree scope;
1190
1191 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1192 last lookup took place. OBJECT_SCOPE is used if an expression
1193 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1194 respectively. QUALIFYING_SCOPE is used for an expression of the
1195 form "X::Y"; it refers to X. */
1196 tree object_scope;
1197 tree qualifying_scope;
1198
1199 /* A stack of parsing contexts. All but the bottom entry on the
1200 stack will be tentative contexts.
1201
1202 We parse tentatively in order to determine which construct is in
1203 use in some situations. For example, in order to determine
1204 whether a statement is an expression-statement or a
1205 declaration-statement we parse it tentatively as a
1206 declaration-statement. If that fails, we then reparse the same
1207 token stream as an expression-statement. */
1208 cp_parser_context *context;
1209
1210 /* True if we are parsing GNU C++. If this flag is not set, then
1211 GNU extensions are not recognized. */
1212 bool allow_gnu_extensions_p;
1213
1214 /* TRUE if the `>' token should be interpreted as the greater-than
1215 operator. FALSE if it is the end of a template-id or
1216 template-parameter-list. */
1217 bool greater_than_is_operator_p;
1218
1219 /* TRUE if default arguments are allowed within a parameter list
1220 that starts at this point. FALSE if only a gnu extension makes
1221 them permissible. */
1222 bool default_arg_ok_p;
1223
1224 /* TRUE if we are parsing an integral constant-expression. See
1225 [expr.const] for a precise definition. */
1226 bool integral_constant_expression_p;
1227
1228 /* TRUE if we are parsing an integral constant-expression -- but a
1229 non-constant expression should be permitted as well. This flag
1230 is used when parsing an array bound so that GNU variable-length
1231 arrays are tolerated. */
1232 bool allow_non_integral_constant_expression_p;
1233
1234 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1235 been seen that makes the expression non-constant. */
1236 bool non_integral_constant_expression_p;
1237
1238 /* TRUE if local variable names and `this' are forbidden in the
1239 current context. */
1240 bool local_variables_forbidden_p;
1241
1242 /* TRUE if the declaration we are parsing is part of a
1243 linkage-specification of the form `extern string-literal
1244 declaration'. */
1245 bool in_unbraced_linkage_specification_p;
1246
1247 /* TRUE if we are presently parsing a declarator, after the
1248 direct-declarator. */
1249 bool in_declarator_p;
1250
1251 /* TRUE if we are presently parsing a template-argument-list. */
1252 bool in_template_argument_list_p;
1253
1254 /* TRUE if we are presently parsing the body of an
1255 iteration-statement. */
1256 bool in_iteration_statement_p;
1257
1258 /* TRUE if we are presently parsing the body of a switch
1259 statement. */
1260 bool in_switch_statement_p;
1261
1262 /* TRUE if we are parsing a type-id in an expression context. In
1263 such a situation, both "type (expr)" and "type (type)" are valid
1264 alternatives. */
1265 bool in_type_id_in_expr_p;
1266
1267 /* TRUE if we are currently in a header file where declarations are
1268 implicitly extern "C". */
1269 bool implicit_extern_c;
1270
1271 /* TRUE if strings in expressions should be translated to the execution
1272 character set. */
1273 bool translate_strings_p;
1274
1275 /* If non-NULL, then we are parsing a construct where new type
1276 definitions are not permitted. The string stored here will be
1277 issued as an error message if a type is defined. */
1278 const char *type_definition_forbidden_message;
1279
1280 /* A list of lists. The outer list is a stack, used for member
1281 functions of local classes. At each level there are two sub-list,
1282 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1283 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1284 TREE_VALUE's. The functions are chained in reverse declaration
1285 order.
1286
1287 The TREE_PURPOSE sublist contains those functions with default
1288 arguments that need post processing, and the TREE_VALUE sublist
1289 contains those functions with definitions that need post
1290 processing.
1291
1292 These lists can only be processed once the outermost class being
1293 defined is complete. */
1294 tree unparsed_functions_queues;
1295
1296 /* The number of classes whose definitions are currently in
1297 progress. */
1298 unsigned num_classes_being_defined;
1299
1300 /* The number of template parameter lists that apply directly to the
1301 current declaration. */
1302 unsigned num_template_parameter_lists;
1303 } cp_parser;
1304
1305 /* The type of a function that parses some kind of expression. */
1306 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1307
1308 /* Prototypes. */
1309
1310 /* Constructors and destructors. */
1311
1312 static cp_parser *cp_parser_new
1313 (void);
1314
1315 /* Routines to parse various constructs.
1316
1317 Those that return `tree' will return the error_mark_node (rather
1318 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1319 Sometimes, they will return an ordinary node if error-recovery was
1320 attempted, even though a parse error occurred. So, to check
1321 whether or not a parse error occurred, you should always use
1322 cp_parser_error_occurred. If the construct is optional (indicated
1323 either by an `_opt' in the name of the function that does the
1324 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1325 the construct is not present. */
1326
1327 /* Lexical conventions [gram.lex] */
1328
1329 static tree cp_parser_identifier
1330 (cp_parser *);
1331 static tree cp_parser_string_literal
1332 (cp_parser *, bool, bool);
1333
1334 /* Basic concepts [gram.basic] */
1335
1336 static bool cp_parser_translation_unit
1337 (cp_parser *);
1338
1339 /* Expressions [gram.expr] */
1340
1341 static tree cp_parser_primary_expression
1342 (cp_parser *, cp_id_kind *, tree *);
1343 static tree cp_parser_id_expression
1344 (cp_parser *, bool, bool, bool *, bool);
1345 static tree cp_parser_unqualified_id
1346 (cp_parser *, bool, bool, bool);
1347 static tree cp_parser_nested_name_specifier_opt
1348 (cp_parser *, bool, bool, bool, bool);
1349 static tree cp_parser_nested_name_specifier
1350 (cp_parser *, bool, bool, bool, bool);
1351 static tree cp_parser_class_or_namespace_name
1352 (cp_parser *, bool, bool, bool, bool, bool);
1353 static tree cp_parser_postfix_expression
1354 (cp_parser *, bool);
1355 static tree cp_parser_postfix_open_square_expression
1356 (cp_parser *, tree, bool);
1357 static tree cp_parser_postfix_dot_deref_expression
1358 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1359 static tree cp_parser_parenthesized_expression_list
1360 (cp_parser *, bool, bool *);
1361 static void cp_parser_pseudo_destructor_name
1362 (cp_parser *, tree *, tree *);
1363 static tree cp_parser_unary_expression
1364 (cp_parser *, bool);
1365 static enum tree_code cp_parser_unary_operator
1366 (cp_token *);
1367 static tree cp_parser_new_expression
1368 (cp_parser *);
1369 static tree cp_parser_new_placement
1370 (cp_parser *);
1371 static tree cp_parser_new_type_id
1372 (cp_parser *, tree *);
1373 static cp_declarator *cp_parser_new_declarator_opt
1374 (cp_parser *);
1375 static cp_declarator *cp_parser_direct_new_declarator
1376 (cp_parser *);
1377 static tree cp_parser_new_initializer
1378 (cp_parser *);
1379 static tree cp_parser_delete_expression
1380 (cp_parser *);
1381 static tree cp_parser_cast_expression
1382 (cp_parser *, bool);
1383 static tree cp_parser_binary_expression
1384 (cp_parser *);
1385 static tree cp_parser_question_colon_clause
1386 (cp_parser *, tree);
1387 static tree cp_parser_assignment_expression
1388 (cp_parser *);
1389 static enum tree_code cp_parser_assignment_operator_opt
1390 (cp_parser *);
1391 static tree cp_parser_expression
1392 (cp_parser *);
1393 static tree cp_parser_constant_expression
1394 (cp_parser *, bool, bool *);
1395 static tree cp_parser_builtin_offsetof
1396 (cp_parser *);
1397
1398 /* Statements [gram.stmt.stmt] */
1399
1400 static void cp_parser_statement
1401 (cp_parser *, tree);
1402 static tree cp_parser_labeled_statement
1403 (cp_parser *, tree);
1404 static tree cp_parser_expression_statement
1405 (cp_parser *, tree);
1406 static tree cp_parser_compound_statement
1407 (cp_parser *, tree, bool);
1408 static void cp_parser_statement_seq_opt
1409 (cp_parser *, tree);
1410 static tree cp_parser_selection_statement
1411 (cp_parser *);
1412 static tree cp_parser_condition
1413 (cp_parser *);
1414 static tree cp_parser_iteration_statement
1415 (cp_parser *);
1416 static void cp_parser_for_init_statement
1417 (cp_parser *);
1418 static tree cp_parser_jump_statement
1419 (cp_parser *);
1420 static void cp_parser_declaration_statement
1421 (cp_parser *);
1422
1423 static tree cp_parser_implicitly_scoped_statement
1424 (cp_parser *);
1425 static void cp_parser_already_scoped_statement
1426 (cp_parser *);
1427
1428 /* Declarations [gram.dcl.dcl] */
1429
1430 static void cp_parser_declaration_seq_opt
1431 (cp_parser *);
1432 static void cp_parser_declaration
1433 (cp_parser *);
1434 static void cp_parser_block_declaration
1435 (cp_parser *, bool);
1436 static void cp_parser_simple_declaration
1437 (cp_parser *, bool);
1438 static void cp_parser_decl_specifier_seq
1439 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1440 static tree cp_parser_storage_class_specifier_opt
1441 (cp_parser *);
1442 static tree cp_parser_function_specifier_opt
1443 (cp_parser *, cp_decl_specifier_seq *);
1444 static tree cp_parser_type_specifier
1445 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1446 int *, bool *);
1447 static tree cp_parser_simple_type_specifier
1448 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1449 static tree cp_parser_type_name
1450 (cp_parser *);
1451 static tree cp_parser_elaborated_type_specifier
1452 (cp_parser *, bool, bool);
1453 static tree cp_parser_enum_specifier
1454 (cp_parser *);
1455 static void cp_parser_enumerator_list
1456 (cp_parser *, tree);
1457 static void cp_parser_enumerator_definition
1458 (cp_parser *, tree);
1459 static tree cp_parser_namespace_name
1460 (cp_parser *);
1461 static void cp_parser_namespace_definition
1462 (cp_parser *);
1463 static void cp_parser_namespace_body
1464 (cp_parser *);
1465 static tree cp_parser_qualified_namespace_specifier
1466 (cp_parser *);
1467 static void cp_parser_namespace_alias_definition
1468 (cp_parser *);
1469 static void cp_parser_using_declaration
1470 (cp_parser *);
1471 static void cp_parser_using_directive
1472 (cp_parser *);
1473 static void cp_parser_asm_definition
1474 (cp_parser *);
1475 static void cp_parser_linkage_specification
1476 (cp_parser *);
1477
1478 /* Declarators [gram.dcl.decl] */
1479
1480 static tree cp_parser_init_declarator
1481 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1482 static cp_declarator *cp_parser_declarator
1483 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1484 static cp_declarator *cp_parser_direct_declarator
1485 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1486 static enum tree_code cp_parser_ptr_operator
1487 (cp_parser *, tree *, cp_cv_quals *);
1488 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1489 (cp_parser *);
1490 static tree cp_parser_declarator_id
1491 (cp_parser *);
1492 static tree cp_parser_type_id
1493 (cp_parser *);
1494 static void cp_parser_type_specifier_seq
1495 (cp_parser *, cp_decl_specifier_seq *);
1496 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1497 (cp_parser *);
1498 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1499 (cp_parser *, bool *);
1500 static cp_parameter_declarator *cp_parser_parameter_declaration
1501 (cp_parser *, bool, bool *);
1502 static void cp_parser_function_body
1503 (cp_parser *);
1504 static tree cp_parser_initializer
1505 (cp_parser *, bool *, bool *);
1506 static tree cp_parser_initializer_clause
1507 (cp_parser *, bool *);
1508 static tree cp_parser_initializer_list
1509 (cp_parser *, bool *);
1510
1511 static bool cp_parser_ctor_initializer_opt_and_function_body
1512 (cp_parser *);
1513
1514 /* Classes [gram.class] */
1515
1516 static tree cp_parser_class_name
1517 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1518 static tree cp_parser_class_specifier
1519 (cp_parser *);
1520 static tree cp_parser_class_head
1521 (cp_parser *, bool *, tree *);
1522 static enum tag_types cp_parser_class_key
1523 (cp_parser *);
1524 static void cp_parser_member_specification_opt
1525 (cp_parser *);
1526 static void cp_parser_member_declaration
1527 (cp_parser *);
1528 static tree cp_parser_pure_specifier
1529 (cp_parser *);
1530 static tree cp_parser_constant_initializer
1531 (cp_parser *);
1532
1533 /* Derived classes [gram.class.derived] */
1534
1535 static tree cp_parser_base_clause
1536 (cp_parser *);
1537 static tree cp_parser_base_specifier
1538 (cp_parser *);
1539
1540 /* Special member functions [gram.special] */
1541
1542 static tree cp_parser_conversion_function_id
1543 (cp_parser *);
1544 static tree cp_parser_conversion_type_id
1545 (cp_parser *);
1546 static cp_declarator *cp_parser_conversion_declarator_opt
1547 (cp_parser *);
1548 static bool cp_parser_ctor_initializer_opt
1549 (cp_parser *);
1550 static void cp_parser_mem_initializer_list
1551 (cp_parser *);
1552 static tree cp_parser_mem_initializer
1553 (cp_parser *);
1554 static tree cp_parser_mem_initializer_id
1555 (cp_parser *);
1556
1557 /* Overloading [gram.over] */
1558
1559 static tree cp_parser_operator_function_id
1560 (cp_parser *);
1561 static tree cp_parser_operator
1562 (cp_parser *);
1563
1564 /* Templates [gram.temp] */
1565
1566 static void cp_parser_template_declaration
1567 (cp_parser *, bool);
1568 static tree cp_parser_template_parameter_list
1569 (cp_parser *);
1570 static tree cp_parser_template_parameter
1571 (cp_parser *, bool *);
1572 static tree cp_parser_type_parameter
1573 (cp_parser *);
1574 static tree cp_parser_template_id
1575 (cp_parser *, bool, bool, bool);
1576 static tree cp_parser_template_name
1577 (cp_parser *, bool, bool, bool, bool *);
1578 static tree cp_parser_template_argument_list
1579 (cp_parser *);
1580 static tree cp_parser_template_argument
1581 (cp_parser *);
1582 static void cp_parser_explicit_instantiation
1583 (cp_parser *);
1584 static void cp_parser_explicit_specialization
1585 (cp_parser *);
1586
1587 /* Exception handling [gram.exception] */
1588
1589 static tree cp_parser_try_block
1590 (cp_parser *);
1591 static bool cp_parser_function_try_block
1592 (cp_parser *);
1593 static void cp_parser_handler_seq
1594 (cp_parser *);
1595 static void cp_parser_handler
1596 (cp_parser *);
1597 static tree cp_parser_exception_declaration
1598 (cp_parser *);
1599 static tree cp_parser_throw_expression
1600 (cp_parser *);
1601 static tree cp_parser_exception_specification_opt
1602 (cp_parser *);
1603 static tree cp_parser_type_id_list
1604 (cp_parser *);
1605
1606 /* GNU Extensions */
1607
1608 static tree cp_parser_asm_specification_opt
1609 (cp_parser *);
1610 static tree cp_parser_asm_operand_list
1611 (cp_parser *);
1612 static tree cp_parser_asm_clobber_list
1613 (cp_parser *);
1614 static tree cp_parser_attributes_opt
1615 (cp_parser *);
1616 static tree cp_parser_attribute_list
1617 (cp_parser *);
1618 static bool cp_parser_extension_opt
1619 (cp_parser *, int *);
1620 static void cp_parser_label_declaration
1621 (cp_parser *);
1622
1623 /* Utility Routines */
1624
1625 static tree cp_parser_lookup_name
1626 (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1627 static tree cp_parser_lookup_name_simple
1628 (cp_parser *, tree);
1629 static tree cp_parser_maybe_treat_template_as_class
1630 (tree, bool);
1631 static bool cp_parser_check_declarator_template_parameters
1632 (cp_parser *, cp_declarator *);
1633 static bool cp_parser_check_template_parameters
1634 (cp_parser *, unsigned);
1635 static tree cp_parser_simple_cast_expression
1636 (cp_parser *);
1637 static tree cp_parser_global_scope_opt
1638 (cp_parser *, bool);
1639 static bool cp_parser_constructor_declarator_p
1640 (cp_parser *, bool);
1641 static tree cp_parser_function_definition_from_specifiers_and_declarator
1642 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1643 static tree cp_parser_function_definition_after_declarator
1644 (cp_parser *, bool);
1645 static void cp_parser_template_declaration_after_export
1646 (cp_parser *, bool);
1647 static tree cp_parser_single_declaration
1648 (cp_parser *, bool, bool *);
1649 static tree cp_parser_functional_cast
1650 (cp_parser *, tree);
1651 static tree cp_parser_save_member_function_body
1652 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1653 static tree cp_parser_enclosed_template_argument_list
1654 (cp_parser *);
1655 static void cp_parser_save_default_args
1656 (cp_parser *, tree);
1657 static void cp_parser_late_parsing_for_member
1658 (cp_parser *, tree);
1659 static void cp_parser_late_parsing_default_args
1660 (cp_parser *, tree);
1661 static tree cp_parser_sizeof_operand
1662 (cp_parser *, enum rid);
1663 static bool cp_parser_declares_only_class_p
1664 (cp_parser *);
1665 static void cp_parser_set_storage_class
1666 (cp_decl_specifier_seq *, cp_storage_class);
1667 static void cp_parser_set_decl_spec_type
1668 (cp_decl_specifier_seq *, tree, bool);
1669 static bool cp_parser_friend_p
1670 (const cp_decl_specifier_seq *);
1671 static cp_token *cp_parser_require
1672 (cp_parser *, enum cpp_ttype, const char *);
1673 static cp_token *cp_parser_require_keyword
1674 (cp_parser *, enum rid, const char *);
1675 static bool cp_parser_token_starts_function_definition_p
1676 (cp_token *);
1677 static bool cp_parser_next_token_starts_class_definition_p
1678 (cp_parser *);
1679 static bool cp_parser_next_token_ends_template_argument_p
1680 (cp_parser *);
1681 static bool cp_parser_nth_token_starts_template_argument_list_p
1682 (cp_parser *, size_t);
1683 static enum tag_types cp_parser_token_is_class_key
1684 (cp_token *);
1685 static void cp_parser_check_class_key
1686 (enum tag_types, tree type);
1687 static void cp_parser_check_access_in_redeclaration
1688 (tree type);
1689 static bool cp_parser_optional_template_keyword
1690 (cp_parser *);
1691 static void cp_parser_pre_parsed_nested_name_specifier
1692 (cp_parser *);
1693 static void cp_parser_cache_group
1694 (cp_parser *, enum cpp_ttype, unsigned);
1695 static void cp_parser_parse_tentatively
1696 (cp_parser *);
1697 static void cp_parser_commit_to_tentative_parse
1698 (cp_parser *);
1699 static void cp_parser_abort_tentative_parse
1700 (cp_parser *);
1701 static bool cp_parser_parse_definitely
1702 (cp_parser *);
1703 static inline bool cp_parser_parsing_tentatively
1704 (cp_parser *);
1705 static bool cp_parser_committed_to_tentative_parse
1706 (cp_parser *);
1707 static void cp_parser_error
1708 (cp_parser *, const char *);
1709 static void cp_parser_name_lookup_error
1710 (cp_parser *, tree, tree, const char *);
1711 static bool cp_parser_simulate_error
1712 (cp_parser *);
1713 static void cp_parser_check_type_definition
1714 (cp_parser *);
1715 static void cp_parser_check_for_definition_in_return_type
1716 (cp_declarator *, tree);
1717 static void cp_parser_check_for_invalid_template_id
1718 (cp_parser *, tree);
1719 static bool cp_parser_non_integral_constant_expression
1720 (cp_parser *, const char *);
1721 static void cp_parser_diagnose_invalid_type_name
1722 (cp_parser *, tree, tree);
1723 static bool cp_parser_parse_and_diagnose_invalid_type_name
1724 (cp_parser *);
1725 static int cp_parser_skip_to_closing_parenthesis
1726 (cp_parser *, bool, bool, bool);
1727 static void cp_parser_skip_to_end_of_statement
1728 (cp_parser *);
1729 static void cp_parser_consume_semicolon_at_end_of_statement
1730 (cp_parser *);
1731 static void cp_parser_skip_to_end_of_block_or_statement
1732 (cp_parser *);
1733 static void cp_parser_skip_to_closing_brace
1734 (cp_parser *);
1735 static void cp_parser_skip_until_found
1736 (cp_parser *, enum cpp_ttype, const char *);
1737 static bool cp_parser_error_occurred
1738 (cp_parser *);
1739 static bool cp_parser_allow_gnu_extensions_p
1740 (cp_parser *);
1741 static bool cp_parser_is_string_literal
1742 (cp_token *);
1743 static bool cp_parser_is_keyword
1744 (cp_token *, enum rid);
1745 static tree cp_parser_make_typename_type
1746 (cp_parser *, tree, tree);
1747
1748 /* Returns nonzero if we are parsing tentatively. */
1749
1750 static inline bool
1751 cp_parser_parsing_tentatively (cp_parser* parser)
1752 {
1753 return parser->context->next != NULL;
1754 }
1755
1756 /* Returns nonzero if TOKEN is a string literal. */
1757
1758 static bool
1759 cp_parser_is_string_literal (cp_token* token)
1760 {
1761 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1762 }
1763
1764 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1765
1766 static bool
1767 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1768 {
1769 return token->keyword == keyword;
1770 }
1771
1772 /* If not parsing tentatively, issue a diagnostic of the form
1773 FILE:LINE: MESSAGE before TOKEN
1774 where TOKEN is the next token in the input stream. MESSAGE
1775 (specified by the caller) is usually of the form "expected
1776 OTHER-TOKEN". */
1777
1778 static void
1779 cp_parser_error (cp_parser* parser, const char* message)
1780 {
1781 if (!cp_parser_simulate_error (parser))
1782 {
1783 cp_token *token = cp_lexer_peek_token (parser->lexer);
1784 /* This diagnostic makes more sense if it is tagged to the line
1785 of the token we just peeked at. */
1786 cp_lexer_set_source_position_from_token (token);
1787 c_parse_error (message,
1788 /* Because c_parser_error does not understand
1789 CPP_KEYWORD, keywords are treated like
1790 identifiers. */
1791 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1792 token->value);
1793 }
1794 }
1795
1796 /* Issue an error about name-lookup failing. NAME is the
1797 IDENTIFIER_NODE DECL is the result of
1798 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1799 the thing that we hoped to find. */
1800
1801 static void
1802 cp_parser_name_lookup_error (cp_parser* parser,
1803 tree name,
1804 tree decl,
1805 const char* desired)
1806 {
1807 /* If name lookup completely failed, tell the user that NAME was not
1808 declared. */
1809 if (decl == error_mark_node)
1810 {
1811 if (parser->scope && parser->scope != global_namespace)
1812 error ("%<%D::%D%> has not been declared",
1813 parser->scope, name);
1814 else if (parser->scope == global_namespace)
1815 error ("%<::%D%> has not been declared", name);
1816 else if (parser->object_scope
1817 && !CLASS_TYPE_P (parser->object_scope))
1818 error ("request for member %qD in non-class type %qT",
1819 name, parser->object_scope);
1820 else if (parser->object_scope)
1821 error ("%<%T::%D%> has not been declared",
1822 parser->object_scope, name);
1823 else
1824 error ("%qD has not been declared", name);
1825 }
1826 else if (parser->scope && parser->scope != global_namespace)
1827 error ("%<%D::%D%> %s", parser->scope, name, desired);
1828 else if (parser->scope == global_namespace)
1829 error ("%<::%D%> %s", name, desired);
1830 else
1831 error ("%qD %s", name, desired);
1832 }
1833
1834 /* If we are parsing tentatively, remember that an error has occurred
1835 during this tentative parse. Returns true if the error was
1836 simulated; false if a message should be issued by the caller. */
1837
1838 static bool
1839 cp_parser_simulate_error (cp_parser* parser)
1840 {
1841 if (cp_parser_parsing_tentatively (parser)
1842 && !cp_parser_committed_to_tentative_parse (parser))
1843 {
1844 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1845 return true;
1846 }
1847 return false;
1848 }
1849
1850 /* This function is called when a type is defined. If type
1851 definitions are forbidden at this point, an error message is
1852 issued. */
1853
1854 static void
1855 cp_parser_check_type_definition (cp_parser* parser)
1856 {
1857 /* If types are forbidden here, issue a message. */
1858 if (parser->type_definition_forbidden_message)
1859 /* Use `%s' to print the string in case there are any escape
1860 characters in the message. */
1861 error ("%s", parser->type_definition_forbidden_message);
1862 }
1863
1864 /* This function is called when the DECLARATOR is processed. The TYPE
1865 was a type defined in the decl-specifiers. If it is invalid to
1866 define a type in the decl-specifiers for DECLARATOR, an error is
1867 issued. */
1868
1869 static void
1870 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1871 tree type)
1872 {
1873 /* [dcl.fct] forbids type definitions in return types.
1874 Unfortunately, it's not easy to know whether or not we are
1875 processing a return type until after the fact. */
1876 while (declarator
1877 && (declarator->kind == cdk_pointer
1878 || declarator->kind == cdk_reference
1879 || declarator->kind == cdk_ptrmem))
1880 declarator = declarator->declarator;
1881 if (declarator
1882 && declarator->kind == cdk_function)
1883 {
1884 error ("new types may not be defined in a return type");
1885 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1886 type);
1887 }
1888 }
1889
1890 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1891 "<" in any valid C++ program. If the next token is indeed "<",
1892 issue a message warning the user about what appears to be an
1893 invalid attempt to form a template-id. */
1894
1895 static void
1896 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1897 tree type)
1898 {
1899 cp_token_position start = 0;
1900
1901 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1902 {
1903 if (TYPE_P (type))
1904 error ("%qT is not a template", type);
1905 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1906 error ("%qE is not a template", type);
1907 else
1908 error ("invalid template-id");
1909 /* Remember the location of the invalid "<". */
1910 if (cp_parser_parsing_tentatively (parser)
1911 && !cp_parser_committed_to_tentative_parse (parser))
1912 start = cp_lexer_token_position (parser->lexer, true);
1913 /* Consume the "<". */
1914 cp_lexer_consume_token (parser->lexer);
1915 /* Parse the template arguments. */
1916 cp_parser_enclosed_template_argument_list (parser);
1917 /* Permanently remove the invalid template arguments so that
1918 this error message is not issued again. */
1919 if (start)
1920 cp_lexer_purge_tokens_after (parser->lexer, start);
1921 }
1922 }
1923
1924 /* If parsing an integral constant-expression, issue an error message
1925 about the fact that THING appeared and return true. Otherwise,
1926 return false, marking the current expression as non-constant. */
1927
1928 static bool
1929 cp_parser_non_integral_constant_expression (cp_parser *parser,
1930 const char *thing)
1931 {
1932 if (parser->integral_constant_expression_p)
1933 {
1934 if (!parser->allow_non_integral_constant_expression_p)
1935 {
1936 error ("%s cannot appear in a constant-expression", thing);
1937 return true;
1938 }
1939 parser->non_integral_constant_expression_p = true;
1940 }
1941 return false;
1942 }
1943
1944 /* Emit a diagnostic for an invalid type name. Consider also if it is
1945 qualified or not and the result of a lookup, to provide a better
1946 message. */
1947
1948 static void
1949 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1950 {
1951 tree decl, old_scope;
1952 /* Try to lookup the identifier. */
1953 old_scope = parser->scope;
1954 parser->scope = scope;
1955 decl = cp_parser_lookup_name_simple (parser, id);
1956 parser->scope = old_scope;
1957 /* If the lookup found a template-name, it means that the user forgot
1958 to specify an argument list. Emit an useful error message. */
1959 if (TREE_CODE (decl) == TEMPLATE_DECL)
1960 error ("invalid use of template-name %qE without an argument list",
1961 decl);
1962 else if (!parser->scope)
1963 {
1964 /* Issue an error message. */
1965 error ("%qE does not name a type", id);
1966 /* If we're in a template class, it's possible that the user was
1967 referring to a type from a base class. For example:
1968
1969 template <typename T> struct A { typedef T X; };
1970 template <typename T> struct B : public A<T> { X x; };
1971
1972 The user should have said "typename A<T>::X". */
1973 if (processing_template_decl && current_class_type)
1974 {
1975 tree b;
1976
1977 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1978 b;
1979 b = TREE_CHAIN (b))
1980 {
1981 tree base_type = BINFO_TYPE (b);
1982 if (CLASS_TYPE_P (base_type)
1983 && dependent_type_p (base_type))
1984 {
1985 tree field;
1986 /* Go from a particular instantiation of the
1987 template (which will have an empty TYPE_FIELDs),
1988 to the main version. */
1989 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1990 for (field = TYPE_FIELDS (base_type);
1991 field;
1992 field = TREE_CHAIN (field))
1993 if (TREE_CODE (field) == TYPE_DECL
1994 && DECL_NAME (field) == id)
1995 {
1996 inform ("(perhaps %<typename %T::%E%> was intended)",
1997 BINFO_TYPE (b), id);
1998 break;
1999 }
2000 if (field)
2001 break;
2002 }
2003 }
2004 }
2005 }
2006 /* Here we diagnose qualified-ids where the scope is actually correct,
2007 but the identifier does not resolve to a valid type name. */
2008 else
2009 {
2010 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2011 error ("%qE in namespace %qE does not name a type",
2012 id, parser->scope);
2013 else if (TYPE_P (parser->scope))
2014 error ("%qE in class %qT does not name a type", id, parser->scope);
2015 else
2016 gcc_unreachable ();
2017 }
2018 }
2019
2020 /* Check for a common situation where a type-name should be present,
2021 but is not, and issue a sensible error message. Returns true if an
2022 invalid type-name was detected.
2023
2024 The situation handled by this function are variable declarations of the
2025 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2026 Usually, `ID' should name a type, but if we got here it means that it
2027 does not. We try to emit the best possible error message depending on
2028 how exactly the id-expression looks like.
2029 */
2030
2031 static bool
2032 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2033 {
2034 tree id;
2035
2036 cp_parser_parse_tentatively (parser);
2037 id = cp_parser_id_expression (parser,
2038 /*template_keyword_p=*/false,
2039 /*check_dependency_p=*/true,
2040 /*template_p=*/NULL,
2041 /*declarator_p=*/true);
2042 /* After the id-expression, there should be a plain identifier,
2043 otherwise this is not a simple variable declaration. Also, if
2044 the scope is dependent, we cannot do much. */
2045 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2046 || (parser->scope && TYPE_P (parser->scope)
2047 && dependent_type_p (parser->scope)))
2048 {
2049 cp_parser_abort_tentative_parse (parser);
2050 return false;
2051 }
2052 if (!cp_parser_parse_definitely (parser)
2053 || TREE_CODE (id) != IDENTIFIER_NODE)
2054 return false;
2055
2056 /* Emit a diagnostic for the invalid type. */
2057 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2058 /* Skip to the end of the declaration; there's no point in
2059 trying to process it. */
2060 cp_parser_skip_to_end_of_block_or_statement (parser);
2061 return true;
2062 }
2063
2064 /* Consume tokens up to, and including, the next non-nested closing `)'.
2065 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2066 are doing error recovery. Returns -1 if OR_COMMA is true and we
2067 found an unnested comma. */
2068
2069 static int
2070 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2071 bool recovering,
2072 bool or_comma,
2073 bool consume_paren)
2074 {
2075 unsigned paren_depth = 0;
2076 unsigned brace_depth = 0;
2077 int result;
2078
2079 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2080 && !cp_parser_committed_to_tentative_parse (parser))
2081 return 0;
2082
2083 while (true)
2084 {
2085 cp_token *token;
2086
2087 /* If we've run out of tokens, then there is no closing `)'. */
2088 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2089 {
2090 result = 0;
2091 break;
2092 }
2093
2094 token = cp_lexer_peek_token (parser->lexer);
2095
2096 /* This matches the processing in skip_to_end_of_statement. */
2097 if (token->type == CPP_SEMICOLON && !brace_depth)
2098 {
2099 result = 0;
2100 break;
2101 }
2102 if (token->type == CPP_OPEN_BRACE)
2103 ++brace_depth;
2104 if (token->type == CPP_CLOSE_BRACE)
2105 {
2106 if (!brace_depth--)
2107 {
2108 result = 0;
2109 break;
2110 }
2111 }
2112 if (recovering && or_comma && token->type == CPP_COMMA
2113 && !brace_depth && !paren_depth)
2114 {
2115 result = -1;
2116 break;
2117 }
2118
2119 if (!brace_depth)
2120 {
2121 /* If it is an `(', we have entered another level of nesting. */
2122 if (token->type == CPP_OPEN_PAREN)
2123 ++paren_depth;
2124 /* If it is a `)', then we might be done. */
2125 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2126 {
2127 if (consume_paren)
2128 cp_lexer_consume_token (parser->lexer);
2129 {
2130 result = 1;
2131 break;
2132 }
2133 }
2134 }
2135
2136 /* Consume the token. */
2137 cp_lexer_consume_token (parser->lexer);
2138 }
2139
2140 return result;
2141 }
2142
2143 /* Consume tokens until we reach the end of the current statement.
2144 Normally, that will be just before consuming a `;'. However, if a
2145 non-nested `}' comes first, then we stop before consuming that. */
2146
2147 static void
2148 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2149 {
2150 unsigned nesting_depth = 0;
2151
2152 while (true)
2153 {
2154 cp_token *token;
2155
2156 /* Peek at the next token. */
2157 token = cp_lexer_peek_token (parser->lexer);
2158 /* If we've run out of tokens, stop. */
2159 if (token->type == CPP_EOF)
2160 break;
2161 /* If the next token is a `;', we have reached the end of the
2162 statement. */
2163 if (token->type == CPP_SEMICOLON && !nesting_depth)
2164 break;
2165 /* If the next token is a non-nested `}', then we have reached
2166 the end of the current block. */
2167 if (token->type == CPP_CLOSE_BRACE)
2168 {
2169 /* If this is a non-nested `}', stop before consuming it.
2170 That way, when confronted with something like:
2171
2172 { 3 + }
2173
2174 we stop before consuming the closing `}', even though we
2175 have not yet reached a `;'. */
2176 if (nesting_depth == 0)
2177 break;
2178 /* If it is the closing `}' for a block that we have
2179 scanned, stop -- but only after consuming the token.
2180 That way given:
2181
2182 void f g () { ... }
2183 typedef int I;
2184
2185 we will stop after the body of the erroneously declared
2186 function, but before consuming the following `typedef'
2187 declaration. */
2188 if (--nesting_depth == 0)
2189 {
2190 cp_lexer_consume_token (parser->lexer);
2191 break;
2192 }
2193 }
2194 /* If it the next token is a `{', then we are entering a new
2195 block. Consume the entire block. */
2196 else if (token->type == CPP_OPEN_BRACE)
2197 ++nesting_depth;
2198 /* Consume the token. */
2199 cp_lexer_consume_token (parser->lexer);
2200 }
2201 }
2202
2203 /* This function is called at the end of a statement or declaration.
2204 If the next token is a semicolon, it is consumed; otherwise, error
2205 recovery is attempted. */
2206
2207 static void
2208 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2209 {
2210 /* Look for the trailing `;'. */
2211 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2212 {
2213 /* If there is additional (erroneous) input, skip to the end of
2214 the statement. */
2215 cp_parser_skip_to_end_of_statement (parser);
2216 /* If the next token is now a `;', consume it. */
2217 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2218 cp_lexer_consume_token (parser->lexer);
2219 }
2220 }
2221
2222 /* Skip tokens until we have consumed an entire block, or until we
2223 have consumed a non-nested `;'. */
2224
2225 static void
2226 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2227 {
2228 unsigned nesting_depth = 0;
2229
2230 while (true)
2231 {
2232 cp_token *token;
2233
2234 /* Peek at the next token. */
2235 token = cp_lexer_peek_token (parser->lexer);
2236 /* If we've run out of tokens, stop. */
2237 if (token->type == CPP_EOF)
2238 break;
2239 /* If the next token is a `;', we have reached the end of the
2240 statement. */
2241 if (token->type == CPP_SEMICOLON && !nesting_depth)
2242 {
2243 /* Consume the `;'. */
2244 cp_lexer_consume_token (parser->lexer);
2245 break;
2246 }
2247 /* Consume the token. */
2248 token = cp_lexer_consume_token (parser->lexer);
2249 /* If the next token is a non-nested `}', then we have reached
2250 the end of the current block. */
2251 if (token->type == CPP_CLOSE_BRACE
2252 && (nesting_depth == 0 || --nesting_depth == 0))
2253 break;
2254 /* If it the next token is a `{', then we are entering a new
2255 block. Consume the entire block. */
2256 if (token->type == CPP_OPEN_BRACE)
2257 ++nesting_depth;
2258 }
2259 }
2260
2261 /* Skip tokens until a non-nested closing curly brace is the next
2262 token. */
2263
2264 static void
2265 cp_parser_skip_to_closing_brace (cp_parser *parser)
2266 {
2267 unsigned nesting_depth = 0;
2268
2269 while (true)
2270 {
2271 cp_token *token;
2272
2273 /* Peek at the next token. */
2274 token = cp_lexer_peek_token (parser->lexer);
2275 /* If we've run out of tokens, stop. */
2276 if (token->type == CPP_EOF)
2277 break;
2278 /* If the next token is a non-nested `}', then we have reached
2279 the end of the current block. */
2280 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2281 break;
2282 /* If it the next token is a `{', then we are entering a new
2283 block. Consume the entire block. */
2284 else if (token->type == CPP_OPEN_BRACE)
2285 ++nesting_depth;
2286 /* Consume the token. */
2287 cp_lexer_consume_token (parser->lexer);
2288 }
2289 }
2290
2291 /* This is a simple wrapper around make_typename_type. When the id is
2292 an unresolved identifier node, we can provide a superior diagnostic
2293 using cp_parser_diagnose_invalid_type_name. */
2294
2295 static tree
2296 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2297 {
2298 tree result;
2299 if (TREE_CODE (id) == IDENTIFIER_NODE)
2300 {
2301 result = make_typename_type (scope, id, typename_type,
2302 /*complain=*/0);
2303 if (result == error_mark_node)
2304 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2305 return result;
2306 }
2307 return make_typename_type (scope, id, typename_type, tf_error);
2308 }
2309
2310
2311 /* Create a new C++ parser. */
2312
2313 static cp_parser *
2314 cp_parser_new (void)
2315 {
2316 cp_parser *parser;
2317 cp_lexer *lexer;
2318 unsigned i;
2319
2320 /* cp_lexer_new_main is called before calling ggc_alloc because
2321 cp_lexer_new_main might load a PCH file. */
2322 lexer = cp_lexer_new_main ();
2323
2324 /* Initialize the binops_by_token so that we can get the tree
2325 directly from the token. */
2326 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2327 binops_by_token[binops[i].token_type] = binops[i];
2328
2329 parser = GGC_CNEW (cp_parser);
2330 parser->lexer = lexer;
2331 parser->context = cp_parser_context_new (NULL);
2332
2333 /* For now, we always accept GNU extensions. */
2334 parser->allow_gnu_extensions_p = 1;
2335
2336 /* The `>' token is a greater-than operator, not the end of a
2337 template-id. */
2338 parser->greater_than_is_operator_p = true;
2339
2340 parser->default_arg_ok_p = true;
2341
2342 /* We are not parsing a constant-expression. */
2343 parser->integral_constant_expression_p = false;
2344 parser->allow_non_integral_constant_expression_p = false;
2345 parser->non_integral_constant_expression_p = false;
2346
2347 /* Local variable names are not forbidden. */
2348 parser->local_variables_forbidden_p = false;
2349
2350 /* We are not processing an `extern "C"' declaration. */
2351 parser->in_unbraced_linkage_specification_p = false;
2352
2353 /* We are not processing a declarator. */
2354 parser->in_declarator_p = false;
2355
2356 /* We are not processing a template-argument-list. */
2357 parser->in_template_argument_list_p = false;
2358
2359 /* We are not in an iteration statement. */
2360 parser->in_iteration_statement_p = false;
2361
2362 /* We are not in a switch statement. */
2363 parser->in_switch_statement_p = false;
2364
2365 /* We are not parsing a type-id inside an expression. */
2366 parser->in_type_id_in_expr_p = false;
2367
2368 /* Declarations aren't implicitly extern "C". */
2369 parser->implicit_extern_c = false;
2370
2371 /* String literals should be translated to the execution character set. */
2372 parser->translate_strings_p = true;
2373
2374 /* The unparsed function queue is empty. */
2375 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2376
2377 /* There are no classes being defined. */
2378 parser->num_classes_being_defined = 0;
2379
2380 /* No template parameters apply. */
2381 parser->num_template_parameter_lists = 0;
2382
2383 return parser;
2384 }
2385
2386 /* Create a cp_lexer structure which will emit the tokens in CACHE
2387 and push it onto the parser's lexer stack. This is used for delayed
2388 parsing of in-class method bodies and default arguments, and should
2389 not be confused with tentative parsing. */
2390 static void
2391 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2392 {
2393 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2394 lexer->next = parser->lexer;
2395 parser->lexer = lexer;
2396
2397 /* Move the current source position to that of the first token in the
2398 new lexer. */
2399 cp_lexer_set_source_position_from_token (lexer->next_token);
2400 }
2401
2402 /* Pop the top lexer off the parser stack. This is never used for the
2403 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2404 static void
2405 cp_parser_pop_lexer (cp_parser *parser)
2406 {
2407 cp_lexer *lexer = parser->lexer;
2408 parser->lexer = lexer->next;
2409 cp_lexer_destroy (lexer);
2410
2411 /* Put the current source position back where it was before this
2412 lexer was pushed. */
2413 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2414 }
2415
2416 /* Lexical conventions [gram.lex] */
2417
2418 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2419 identifier. */
2420
2421 static tree
2422 cp_parser_identifier (cp_parser* parser)
2423 {
2424 cp_token *token;
2425
2426 /* Look for the identifier. */
2427 token = cp_parser_require (parser, CPP_NAME, "identifier");
2428 /* Return the value. */
2429 return token ? token->value : error_mark_node;
2430 }
2431
2432 /* Parse a sequence of adjacent string constants. Returns a
2433 TREE_STRING representing the combined, nul-terminated string
2434 constant. If TRANSLATE is true, translate the string to the
2435 execution character set. If WIDE_OK is true, a wide string is
2436 invalid here.
2437
2438 C++98 [lex.string] says that if a narrow string literal token is
2439 adjacent to a wide string literal token, the behavior is undefined.
2440 However, C99 6.4.5p4 says that this results in a wide string literal.
2441 We follow C99 here, for consistency with the C front end.
2442
2443 This code is largely lifted from lex_string() in c-lex.c.
2444
2445 FUTURE: ObjC++ will need to handle @-strings here. */
2446 static tree
2447 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2448 {
2449 tree value;
2450 bool wide = false;
2451 size_t count;
2452 struct obstack str_ob;
2453 cpp_string str, istr, *strs;
2454 cp_token *tok;
2455
2456 tok = cp_lexer_peek_token (parser->lexer);
2457 if (!cp_parser_is_string_literal (tok))
2458 {
2459 cp_parser_error (parser, "expected string-literal");
2460 return error_mark_node;
2461 }
2462
2463 /* Try to avoid the overhead of creating and destroying an obstack
2464 for the common case of just one string. */
2465 if (!cp_parser_is_string_literal
2466 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2467 {
2468 cp_lexer_consume_token (parser->lexer);
2469
2470 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2471 str.len = TREE_STRING_LENGTH (tok->value);
2472 count = 1;
2473 if (tok->type == CPP_WSTRING)
2474 wide = true;
2475
2476 strs = &str;
2477 }
2478 else
2479 {
2480 gcc_obstack_init (&str_ob);
2481 count = 0;
2482
2483 do
2484 {
2485 cp_lexer_consume_token (parser->lexer);
2486 count++;
2487 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2488 str.len = TREE_STRING_LENGTH (tok->value);
2489 if (tok->type == CPP_WSTRING)
2490 wide = true;
2491
2492 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2493
2494 tok = cp_lexer_peek_token (parser->lexer);
2495 }
2496 while (cp_parser_is_string_literal (tok));
2497
2498 strs = (cpp_string *) obstack_finish (&str_ob);
2499 }
2500
2501 if (wide && !wide_ok)
2502 {
2503 cp_parser_error (parser, "a wide string is invalid in this context");
2504 wide = false;
2505 }
2506
2507 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2508 (parse_in, strs, count, &istr, wide))
2509 {
2510 value = build_string (istr.len, (char *)istr.text);
2511 free ((void *)istr.text);
2512
2513 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2514 value = fix_string_type (value);
2515 }
2516 else
2517 /* cpp_interpret_string has issued an error. */
2518 value = error_mark_node;
2519
2520 if (count > 1)
2521 obstack_free (&str_ob, 0);
2522
2523 return value;
2524 }
2525
2526
2527 /* Basic concepts [gram.basic] */
2528
2529 /* Parse a translation-unit.
2530
2531 translation-unit:
2532 declaration-seq [opt]
2533
2534 Returns TRUE if all went well. */
2535
2536 static bool
2537 cp_parser_translation_unit (cp_parser* parser)
2538 {
2539 /* The address of the first non-permanent object on the declarator
2540 obstack. */
2541 static void *declarator_obstack_base;
2542
2543 bool success;
2544
2545 /* Create the declarator obstack, if necessary. */
2546 if (!cp_error_declarator)
2547 {
2548 gcc_obstack_init (&declarator_obstack);
2549 /* Create the error declarator. */
2550 cp_error_declarator = make_declarator (cdk_error);
2551 /* Create the empty parameter list. */
2552 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2553 /* Remember where the base of the declarator obstack lies. */
2554 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2555 }
2556
2557 while (true)
2558 {
2559 cp_parser_declaration_seq_opt (parser);
2560
2561 /* If there are no tokens left then all went well. */
2562 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2563 {
2564 /* Get rid of the token array; we don't need it any more. */
2565 cp_lexer_destroy (parser->lexer);
2566 parser->lexer = NULL;
2567
2568 /* This file might have been a context that's implicitly extern
2569 "C". If so, pop the lang context. (Only relevant for PCH.) */
2570 if (parser->implicit_extern_c)
2571 {
2572 pop_lang_context ();
2573 parser->implicit_extern_c = false;
2574 }
2575
2576 /* Finish up. */
2577 finish_translation_unit ();
2578
2579 success = true;
2580 break;
2581 }
2582 else
2583 {
2584 cp_parser_error (parser, "expected declaration");
2585 success = false;
2586 break;
2587 }
2588 }
2589
2590 /* Make sure the declarator obstack was fully cleaned up. */
2591 gcc_assert (obstack_next_free (&declarator_obstack)
2592 == declarator_obstack_base);
2593
2594 /* All went well. */
2595 return success;
2596 }
2597
2598 /* Expressions [gram.expr] */
2599
2600 /* Parse a primary-expression.
2601
2602 primary-expression:
2603 literal
2604 this
2605 ( expression )
2606 id-expression
2607
2608 GNU Extensions:
2609
2610 primary-expression:
2611 ( compound-statement )
2612 __builtin_va_arg ( assignment-expression , type-id )
2613
2614 literal:
2615 __null
2616
2617 Returns a representation of the expression.
2618
2619 *IDK indicates what kind of id-expression (if any) was present.
2620
2621 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2622 used as the operand of a pointer-to-member. In that case,
2623 *QUALIFYING_CLASS gives the class that is used as the qualifying
2624 class in the pointer-to-member. */
2625
2626 static tree
2627 cp_parser_primary_expression (cp_parser *parser,
2628 cp_id_kind *idk,
2629 tree *qualifying_class)
2630 {
2631 cp_token *token;
2632
2633 /* Assume the primary expression is not an id-expression. */
2634 *idk = CP_ID_KIND_NONE;
2635 /* And that it cannot be used as pointer-to-member. */
2636 *qualifying_class = NULL_TREE;
2637
2638 /* Peek at the next token. */
2639 token = cp_lexer_peek_token (parser->lexer);
2640 switch (token->type)
2641 {
2642 /* literal:
2643 integer-literal
2644 character-literal
2645 floating-literal
2646 string-literal
2647 boolean-literal */
2648 case CPP_CHAR:
2649 case CPP_WCHAR:
2650 case CPP_NUMBER:
2651 token = cp_lexer_consume_token (parser->lexer);
2652 return token->value;
2653
2654 case CPP_STRING:
2655 case CPP_WSTRING:
2656 /* ??? Should wide strings be allowed when parser->translate_strings_p
2657 is false (i.e. in attributes)? If not, we can kill the third
2658 argument to cp_parser_string_literal. */
2659 return cp_parser_string_literal (parser,
2660 parser->translate_strings_p,
2661 true);
2662
2663 case CPP_OPEN_PAREN:
2664 {
2665 tree expr;
2666 bool saved_greater_than_is_operator_p;
2667
2668 /* Consume the `('. */
2669 cp_lexer_consume_token (parser->lexer);
2670 /* Within a parenthesized expression, a `>' token is always
2671 the greater-than operator. */
2672 saved_greater_than_is_operator_p
2673 = parser->greater_than_is_operator_p;
2674 parser->greater_than_is_operator_p = true;
2675 /* If we see `( { ' then we are looking at the beginning of
2676 a GNU statement-expression. */
2677 if (cp_parser_allow_gnu_extensions_p (parser)
2678 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2679 {
2680 /* Statement-expressions are not allowed by the standard. */
2681 if (pedantic)
2682 pedwarn ("ISO C++ forbids braced-groups within expressions");
2683
2684 /* And they're not allowed outside of a function-body; you
2685 cannot, for example, write:
2686
2687 int i = ({ int j = 3; j + 1; });
2688
2689 at class or namespace scope. */
2690 if (!at_function_scope_p ())
2691 error ("statement-expressions are allowed only inside functions");
2692 /* Start the statement-expression. */
2693 expr = begin_stmt_expr ();
2694 /* Parse the compound-statement. */
2695 cp_parser_compound_statement (parser, expr, false);
2696 /* Finish up. */
2697 expr = finish_stmt_expr (expr, false);
2698 }
2699 else
2700 {
2701 /* Parse the parenthesized expression. */
2702 expr = cp_parser_expression (parser);
2703 /* Let the front end know that this expression was
2704 enclosed in parentheses. This matters in case, for
2705 example, the expression is of the form `A::B', since
2706 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2707 not. */
2708 finish_parenthesized_expr (expr);
2709 }
2710 /* The `>' token might be the end of a template-id or
2711 template-parameter-list now. */
2712 parser->greater_than_is_operator_p
2713 = saved_greater_than_is_operator_p;
2714 /* Consume the `)'. */
2715 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2716 cp_parser_skip_to_end_of_statement (parser);
2717
2718 return expr;
2719 }
2720
2721 case CPP_KEYWORD:
2722 switch (token->keyword)
2723 {
2724 /* These two are the boolean literals. */
2725 case RID_TRUE:
2726 cp_lexer_consume_token (parser->lexer);
2727 return boolean_true_node;
2728 case RID_FALSE:
2729 cp_lexer_consume_token (parser->lexer);
2730 return boolean_false_node;
2731
2732 /* The `__null' literal. */
2733 case RID_NULL:
2734 cp_lexer_consume_token (parser->lexer);
2735 return null_node;
2736
2737 /* Recognize the `this' keyword. */
2738 case RID_THIS:
2739 cp_lexer_consume_token (parser->lexer);
2740 if (parser->local_variables_forbidden_p)
2741 {
2742 error ("%<this%> may not be used in this context");
2743 return error_mark_node;
2744 }
2745 /* Pointers cannot appear in constant-expressions. */
2746 if (cp_parser_non_integral_constant_expression (parser,
2747 "`this'"))
2748 return error_mark_node;
2749 return finish_this_expr ();
2750
2751 /* The `operator' keyword can be the beginning of an
2752 id-expression. */
2753 case RID_OPERATOR:
2754 goto id_expression;
2755
2756 case RID_FUNCTION_NAME:
2757 case RID_PRETTY_FUNCTION_NAME:
2758 case RID_C99_FUNCTION_NAME:
2759 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2760 __func__ are the names of variables -- but they are
2761 treated specially. Therefore, they are handled here,
2762 rather than relying on the generic id-expression logic
2763 below. Grammatically, these names are id-expressions.
2764
2765 Consume the token. */
2766 token = cp_lexer_consume_token (parser->lexer);
2767 /* Look up the name. */
2768 return finish_fname (token->value);
2769
2770 case RID_VA_ARG:
2771 {
2772 tree expression;
2773 tree type;
2774
2775 /* The `__builtin_va_arg' construct is used to handle
2776 `va_arg'. Consume the `__builtin_va_arg' token. */
2777 cp_lexer_consume_token (parser->lexer);
2778 /* Look for the opening `('. */
2779 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2780 /* Now, parse the assignment-expression. */
2781 expression = cp_parser_assignment_expression (parser);
2782 /* Look for the `,'. */
2783 cp_parser_require (parser, CPP_COMMA, "`,'");
2784 /* Parse the type-id. */
2785 type = cp_parser_type_id (parser);
2786 /* Look for the closing `)'. */
2787 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2788 /* Using `va_arg' in a constant-expression is not
2789 allowed. */
2790 if (cp_parser_non_integral_constant_expression (parser,
2791 "`va_arg'"))
2792 return error_mark_node;
2793 return build_x_va_arg (expression, type);
2794 }
2795
2796 case RID_OFFSETOF:
2797 return cp_parser_builtin_offsetof (parser);
2798
2799 default:
2800 cp_parser_error (parser, "expected primary-expression");
2801 return error_mark_node;
2802 }
2803
2804 /* An id-expression can start with either an identifier, a
2805 `::' as the beginning of a qualified-id, or the "operator"
2806 keyword. */
2807 case CPP_NAME:
2808 case CPP_SCOPE:
2809 case CPP_TEMPLATE_ID:
2810 case CPP_NESTED_NAME_SPECIFIER:
2811 {
2812 tree id_expression;
2813 tree decl;
2814 const char *error_msg;
2815
2816 id_expression:
2817 /* Parse the id-expression. */
2818 id_expression
2819 = cp_parser_id_expression (parser,
2820 /*template_keyword_p=*/false,
2821 /*check_dependency_p=*/true,
2822 /*template_p=*/NULL,
2823 /*declarator_p=*/false);
2824 if (id_expression == error_mark_node)
2825 return error_mark_node;
2826 /* If we have a template-id, then no further lookup is
2827 required. If the template-id was for a template-class, we
2828 will sometimes have a TYPE_DECL at this point. */
2829 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2830 || TREE_CODE (id_expression) == TYPE_DECL)
2831 decl = id_expression;
2832 /* Look up the name. */
2833 else
2834 {
2835 bool ambiguous_p;
2836
2837 decl = cp_parser_lookup_name (parser, id_expression,
2838 none_type,
2839 /*is_template=*/false,
2840 /*is_namespace=*/false,
2841 /*check_dependency=*/true,
2842 &ambiguous_p);
2843 /* If the lookup was ambiguous, an error will already have
2844 been issued. */
2845 if (ambiguous_p)
2846 return error_mark_node;
2847 /* If name lookup gives us a SCOPE_REF, then the
2848 qualifying scope was dependent. Just propagate the
2849 name. */
2850 if (TREE_CODE (decl) == SCOPE_REF)
2851 {
2852 if (TYPE_P (TREE_OPERAND (decl, 0)))
2853 *qualifying_class = TREE_OPERAND (decl, 0);
2854 return decl;
2855 }
2856 /* Check to see if DECL is a local variable in a context
2857 where that is forbidden. */
2858 if (parser->local_variables_forbidden_p
2859 && local_variable_p (decl))
2860 {
2861 /* It might be that we only found DECL because we are
2862 trying to be generous with pre-ISO scoping rules.
2863 For example, consider:
2864
2865 int i;
2866 void g() {
2867 for (int i = 0; i < 10; ++i) {}
2868 extern void f(int j = i);
2869 }
2870
2871 Here, name look up will originally find the out
2872 of scope `i'. We need to issue a warning message,
2873 but then use the global `i'. */
2874 decl = check_for_out_of_scope_variable (decl);
2875 if (local_variable_p (decl))
2876 {
2877 error ("local variable %qD may not appear in this context",
2878 decl);
2879 return error_mark_node;
2880 }
2881 }
2882 }
2883
2884 decl = finish_id_expression (id_expression, decl, parser->scope,
2885 idk, qualifying_class,
2886 parser->integral_constant_expression_p,
2887 parser->allow_non_integral_constant_expression_p,
2888 &parser->non_integral_constant_expression_p,
2889 &error_msg);
2890 if (error_msg)
2891 cp_parser_error (parser, error_msg);
2892 return decl;
2893 }
2894
2895 /* Anything else is an error. */
2896 default:
2897 cp_parser_error (parser, "expected primary-expression");
2898 return error_mark_node;
2899 }
2900 }
2901
2902 /* Parse an id-expression.
2903
2904 id-expression:
2905 unqualified-id
2906 qualified-id
2907
2908 qualified-id:
2909 :: [opt] nested-name-specifier template [opt] unqualified-id
2910 :: identifier
2911 :: operator-function-id
2912 :: template-id
2913
2914 Return a representation of the unqualified portion of the
2915 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2916 a `::' or nested-name-specifier.
2917
2918 Often, if the id-expression was a qualified-id, the caller will
2919 want to make a SCOPE_REF to represent the qualified-id. This
2920 function does not do this in order to avoid wastefully creating
2921 SCOPE_REFs when they are not required.
2922
2923 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2924 `template' keyword.
2925
2926 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2927 uninstantiated templates.
2928
2929 If *TEMPLATE_P is non-NULL, it is set to true iff the
2930 `template' keyword is used to explicitly indicate that the entity
2931 named is a template.
2932
2933 If DECLARATOR_P is true, the id-expression is appearing as part of
2934 a declarator, rather than as part of an expression. */
2935
2936 static tree
2937 cp_parser_id_expression (cp_parser *parser,
2938 bool template_keyword_p,
2939 bool check_dependency_p,
2940 bool *template_p,
2941 bool declarator_p)
2942 {
2943 bool global_scope_p;
2944 bool nested_name_specifier_p;
2945
2946 /* Assume the `template' keyword was not used. */
2947 if (template_p)
2948 *template_p = false;
2949
2950 /* Look for the optional `::' operator. */
2951 global_scope_p
2952 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2953 != NULL_TREE);
2954 /* Look for the optional nested-name-specifier. */
2955 nested_name_specifier_p
2956 = (cp_parser_nested_name_specifier_opt (parser,
2957 /*typename_keyword_p=*/false,
2958 check_dependency_p,
2959 /*type_p=*/false,
2960 declarator_p)
2961 != NULL_TREE);
2962 /* If there is a nested-name-specifier, then we are looking at
2963 the first qualified-id production. */
2964 if (nested_name_specifier_p)
2965 {
2966 tree saved_scope;
2967 tree saved_object_scope;
2968 tree saved_qualifying_scope;
2969 tree unqualified_id;
2970 bool is_template;
2971
2972 /* See if the next token is the `template' keyword. */
2973 if (!template_p)
2974 template_p = &is_template;
2975 *template_p = cp_parser_optional_template_keyword (parser);
2976 /* Name lookup we do during the processing of the
2977 unqualified-id might obliterate SCOPE. */
2978 saved_scope = parser->scope;
2979 saved_object_scope = parser->object_scope;
2980 saved_qualifying_scope = parser->qualifying_scope;
2981 /* Process the final unqualified-id. */
2982 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2983 check_dependency_p,
2984 declarator_p);
2985 /* Restore the SAVED_SCOPE for our caller. */
2986 parser->scope = saved_scope;
2987 parser->object_scope = saved_object_scope;
2988 parser->qualifying_scope = saved_qualifying_scope;
2989
2990 return unqualified_id;
2991 }
2992 /* Otherwise, if we are in global scope, then we are looking at one
2993 of the other qualified-id productions. */
2994 else if (global_scope_p)
2995 {
2996 cp_token *token;
2997 tree id;
2998
2999 /* Peek at the next token. */
3000 token = cp_lexer_peek_token (parser->lexer);
3001
3002 /* If it's an identifier, and the next token is not a "<", then
3003 we can avoid the template-id case. This is an optimization
3004 for this common case. */
3005 if (token->type == CPP_NAME
3006 && !cp_parser_nth_token_starts_template_argument_list_p
3007 (parser, 2))
3008 return cp_parser_identifier (parser);
3009
3010 cp_parser_parse_tentatively (parser);
3011 /* Try a template-id. */
3012 id = cp_parser_template_id (parser,
3013 /*template_keyword_p=*/false,
3014 /*check_dependency_p=*/true,
3015 declarator_p);
3016 /* If that worked, we're done. */
3017 if (cp_parser_parse_definitely (parser))
3018 return id;
3019
3020 /* Peek at the next token. (Changes in the token buffer may
3021 have invalidated the pointer obtained above.) */
3022 token = cp_lexer_peek_token (parser->lexer);
3023
3024 switch (token->type)
3025 {
3026 case CPP_NAME:
3027 return cp_parser_identifier (parser);
3028
3029 case CPP_KEYWORD:
3030 if (token->keyword == RID_OPERATOR)
3031 return cp_parser_operator_function_id (parser);
3032 /* Fall through. */
3033
3034 default:
3035 cp_parser_error (parser, "expected id-expression");
3036 return error_mark_node;
3037 }
3038 }
3039 else
3040 return cp_parser_unqualified_id (parser, template_keyword_p,
3041 /*check_dependency_p=*/true,
3042 declarator_p);
3043 }
3044
3045 /* Parse an unqualified-id.
3046
3047 unqualified-id:
3048 identifier
3049 operator-function-id
3050 conversion-function-id
3051 ~ class-name
3052 template-id
3053
3054 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3055 keyword, in a construct like `A::template ...'.
3056
3057 Returns a representation of unqualified-id. For the `identifier'
3058 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3059 production a BIT_NOT_EXPR is returned; the operand of the
3060 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3061 other productions, see the documentation accompanying the
3062 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3063 names are looked up in uninstantiated templates. If DECLARATOR_P
3064 is true, the unqualified-id is appearing as part of a declarator,
3065 rather than as part of an expression. */
3066
3067 static tree
3068 cp_parser_unqualified_id (cp_parser* parser,
3069 bool template_keyword_p,
3070 bool check_dependency_p,
3071 bool declarator_p)
3072 {
3073 cp_token *token;
3074
3075 /* Peek at the next token. */
3076 token = cp_lexer_peek_token (parser->lexer);
3077
3078 switch (token->type)
3079 {
3080 case CPP_NAME:
3081 {
3082 tree id;
3083
3084 /* We don't know yet whether or not this will be a
3085 template-id. */
3086 cp_parser_parse_tentatively (parser);
3087 /* Try a template-id. */
3088 id = cp_parser_template_id (parser, template_keyword_p,
3089 check_dependency_p,
3090 declarator_p);
3091 /* If it worked, we're done. */
3092 if (cp_parser_parse_definitely (parser))
3093 return id;
3094 /* Otherwise, it's an ordinary identifier. */
3095 return cp_parser_identifier (parser);
3096 }
3097
3098 case CPP_TEMPLATE_ID:
3099 return cp_parser_template_id (parser, template_keyword_p,
3100 check_dependency_p,
3101 declarator_p);
3102
3103 case CPP_COMPL:
3104 {
3105 tree type_decl;
3106 tree qualifying_scope;
3107 tree object_scope;
3108 tree scope;
3109
3110 /* Consume the `~' token. */
3111 cp_lexer_consume_token (parser->lexer);
3112 /* Parse the class-name. The standard, as written, seems to
3113 say that:
3114
3115 template <typename T> struct S { ~S (); };
3116 template <typename T> S<T>::~S() {}
3117
3118 is invalid, since `~' must be followed by a class-name, but
3119 `S<T>' is dependent, and so not known to be a class.
3120 That's not right; we need to look in uninstantiated
3121 templates. A further complication arises from:
3122
3123 template <typename T> void f(T t) {
3124 t.T::~T();
3125 }
3126
3127 Here, it is not possible to look up `T' in the scope of `T'
3128 itself. We must look in both the current scope, and the
3129 scope of the containing complete expression.
3130
3131 Yet another issue is:
3132
3133 struct S {
3134 int S;
3135 ~S();
3136 };
3137
3138 S::~S() {}
3139
3140 The standard does not seem to say that the `S' in `~S'
3141 should refer to the type `S' and not the data member
3142 `S::S'. */
3143
3144 /* DR 244 says that we look up the name after the "~" in the
3145 same scope as we looked up the qualifying name. That idea
3146 isn't fully worked out; it's more complicated than that. */
3147 scope = parser->scope;
3148 object_scope = parser->object_scope;
3149 qualifying_scope = parser->qualifying_scope;
3150
3151 /* If the name is of the form "X::~X" it's OK. */
3152 if (scope && TYPE_P (scope)
3153 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3154 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3155 == CPP_OPEN_PAREN)
3156 && (cp_lexer_peek_token (parser->lexer)->value
3157 == TYPE_IDENTIFIER (scope)))
3158 {
3159 cp_lexer_consume_token (parser->lexer);
3160 return build_nt (BIT_NOT_EXPR, scope);
3161 }
3162
3163 /* If there was an explicit qualification (S::~T), first look
3164 in the scope given by the qualification (i.e., S). */
3165 if (scope)
3166 {
3167 cp_parser_parse_tentatively (parser);
3168 type_decl = cp_parser_class_name (parser,
3169 /*typename_keyword_p=*/false,
3170 /*template_keyword_p=*/false,
3171 none_type,
3172 /*check_dependency=*/false,
3173 /*class_head_p=*/false,
3174 declarator_p);
3175 if (cp_parser_parse_definitely (parser))
3176 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3177 }
3178 /* In "N::S::~S", look in "N" as well. */
3179 if (scope && qualifying_scope)
3180 {
3181 cp_parser_parse_tentatively (parser);
3182 parser->scope = qualifying_scope;
3183 parser->object_scope = NULL_TREE;
3184 parser->qualifying_scope = NULL_TREE;
3185 type_decl
3186 = cp_parser_class_name (parser,
3187 /*typename_keyword_p=*/false,
3188 /*template_keyword_p=*/false,
3189 none_type,
3190 /*check_dependency=*/false,
3191 /*class_head_p=*/false,
3192 declarator_p);
3193 if (cp_parser_parse_definitely (parser))
3194 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3195 }
3196 /* In "p->S::~T", look in the scope given by "*p" as well. */
3197 else if (object_scope)
3198 {
3199 cp_parser_parse_tentatively (parser);
3200 parser->scope = object_scope;
3201 parser->object_scope = NULL_TREE;
3202 parser->qualifying_scope = NULL_TREE;
3203 type_decl
3204 = cp_parser_class_name (parser,
3205 /*typename_keyword_p=*/false,
3206 /*template_keyword_p=*/false,
3207 none_type,
3208 /*check_dependency=*/false,
3209 /*class_head_p=*/false,
3210 declarator_p);
3211 if (cp_parser_parse_definitely (parser))
3212 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3213 }
3214 /* Look in the surrounding context. */
3215 parser->scope = NULL_TREE;
3216 parser->object_scope = NULL_TREE;
3217 parser->qualifying_scope = NULL_TREE;
3218 type_decl
3219 = cp_parser_class_name (parser,
3220 /*typename_keyword_p=*/false,
3221 /*template_keyword_p=*/false,
3222 none_type,
3223 /*check_dependency=*/false,
3224 /*class_head_p=*/false,
3225 declarator_p);
3226 /* If an error occurred, assume that the name of the
3227 destructor is the same as the name of the qualifying
3228 class. That allows us to keep parsing after running
3229 into ill-formed destructor names. */
3230 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3231 return build_nt (BIT_NOT_EXPR, scope);
3232 else if (type_decl == error_mark_node)
3233 return error_mark_node;
3234
3235 /* [class.dtor]
3236
3237 A typedef-name that names a class shall not be used as the
3238 identifier in the declarator for a destructor declaration. */
3239 if (declarator_p
3240 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3241 && !DECL_SELF_REFERENCE_P (type_decl))
3242 error ("typedef-name %qD used as destructor declarator",
3243 type_decl);
3244
3245 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3246 }
3247
3248 case CPP_KEYWORD:
3249 if (token->keyword == RID_OPERATOR)
3250 {
3251 tree id;
3252
3253 /* This could be a template-id, so we try that first. */
3254 cp_parser_parse_tentatively (parser);
3255 /* Try a template-id. */
3256 id = cp_parser_template_id (parser, template_keyword_p,
3257 /*check_dependency_p=*/true,
3258 declarator_p);
3259 /* If that worked, we're done. */
3260 if (cp_parser_parse_definitely (parser))
3261 return id;
3262 /* We still don't know whether we're looking at an
3263 operator-function-id or a conversion-function-id. */
3264 cp_parser_parse_tentatively (parser);
3265 /* Try an operator-function-id. */
3266 id = cp_parser_operator_function_id (parser);
3267 /* If that didn't work, try a conversion-function-id. */
3268 if (!cp_parser_parse_definitely (parser))
3269 id = cp_parser_conversion_function_id (parser);
3270
3271 return id;
3272 }
3273 /* Fall through. */
3274
3275 default:
3276 cp_parser_error (parser, "expected unqualified-id");
3277 return error_mark_node;
3278 }
3279 }
3280
3281 /* Parse an (optional) nested-name-specifier.
3282
3283 nested-name-specifier:
3284 class-or-namespace-name :: nested-name-specifier [opt]
3285 class-or-namespace-name :: template nested-name-specifier [opt]
3286
3287 PARSER->SCOPE should be set appropriately before this function is
3288 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3289 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3290 in name lookups.
3291
3292 Sets PARSER->SCOPE to the class (TYPE) or namespace
3293 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3294 it unchanged if there is no nested-name-specifier. Returns the new
3295 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3296
3297 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3298 part of a declaration and/or decl-specifier. */
3299
3300 static tree
3301 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3302 bool typename_keyword_p,
3303 bool check_dependency_p,
3304 bool type_p,
3305 bool is_declaration)
3306 {
3307 bool success = false;
3308 tree access_check = NULL_TREE;
3309 cp_token_position start = 0;
3310 cp_token *token;
3311
3312 /* If the next token corresponds to a nested name specifier, there
3313 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3314 false, it may have been true before, in which case something
3315 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3316 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3317 CHECK_DEPENDENCY_P is false, we have to fall through into the
3318 main loop. */
3319 if (check_dependency_p
3320 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3321 {
3322 cp_parser_pre_parsed_nested_name_specifier (parser);
3323 return parser->scope;
3324 }
3325
3326 /* Remember where the nested-name-specifier starts. */
3327 if (cp_parser_parsing_tentatively (parser)
3328 && !cp_parser_committed_to_tentative_parse (parser))
3329 start = cp_lexer_token_position (parser->lexer, false);
3330
3331 push_deferring_access_checks (dk_deferred);
3332
3333 while (true)
3334 {
3335 tree new_scope;
3336 tree old_scope;
3337 tree saved_qualifying_scope;
3338 bool template_keyword_p;
3339
3340 /* Spot cases that cannot be the beginning of a
3341 nested-name-specifier. */
3342 token = cp_lexer_peek_token (parser->lexer);
3343
3344 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3345 the already parsed nested-name-specifier. */
3346 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3347 {
3348 /* Grab the nested-name-specifier and continue the loop. */
3349 cp_parser_pre_parsed_nested_name_specifier (parser);
3350 success = true;
3351 continue;
3352 }
3353
3354 /* Spot cases that cannot be the beginning of a
3355 nested-name-specifier. On the second and subsequent times
3356 through the loop, we look for the `template' keyword. */
3357 if (success && token->keyword == RID_TEMPLATE)
3358 ;
3359 /* A template-id can start a nested-name-specifier. */
3360 else if (token->type == CPP_TEMPLATE_ID)
3361 ;
3362 else
3363 {
3364 /* If the next token is not an identifier, then it is
3365 definitely not a class-or-namespace-name. */
3366 if (token->type != CPP_NAME)
3367 break;
3368 /* If the following token is neither a `<' (to begin a
3369 template-id), nor a `::', then we are not looking at a
3370 nested-name-specifier. */
3371 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3372 if (token->type != CPP_SCOPE
3373 && !cp_parser_nth_token_starts_template_argument_list_p
3374 (parser, 2))
3375 break;
3376 }
3377
3378 /* The nested-name-specifier is optional, so we parse
3379 tentatively. */
3380 cp_parser_parse_tentatively (parser);
3381
3382 /* Look for the optional `template' keyword, if this isn't the
3383 first time through the loop. */
3384 if (success)
3385 template_keyword_p = cp_parser_optional_template_keyword (parser);
3386 else
3387 template_keyword_p = false;
3388
3389 /* Save the old scope since the name lookup we are about to do
3390 might destroy it. */
3391 old_scope = parser->scope;
3392 saved_qualifying_scope = parser->qualifying_scope;
3393 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3394 look up names in "X<T>::I" in order to determine that "Y" is
3395 a template. So, if we have a typename at this point, we make
3396 an effort to look through it. */
3397 if (is_declaration
3398 && !typename_keyword_p
3399 && parser->scope
3400 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3401 parser->scope = resolve_typename_type (parser->scope,
3402 /*only_current_p=*/false);
3403 /* Parse the qualifying entity. */
3404 new_scope
3405 = cp_parser_class_or_namespace_name (parser,
3406 typename_keyword_p,
3407 template_keyword_p,
3408 check_dependency_p,
3409 type_p,
3410 is_declaration);
3411 /* Look for the `::' token. */
3412 cp_parser_require (parser, CPP_SCOPE, "`::'");
3413
3414 /* If we found what we wanted, we keep going; otherwise, we're
3415 done. */
3416 if (!cp_parser_parse_definitely (parser))
3417 {
3418 bool error_p = false;
3419
3420 /* Restore the OLD_SCOPE since it was valid before the
3421 failed attempt at finding the last
3422 class-or-namespace-name. */
3423 parser->scope = old_scope;
3424 parser->qualifying_scope = saved_qualifying_scope;
3425 /* If the next token is an identifier, and the one after
3426 that is a `::', then any valid interpretation would have
3427 found a class-or-namespace-name. */
3428 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3429 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3430 == CPP_SCOPE)
3431 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3432 != CPP_COMPL))
3433 {
3434 token = cp_lexer_consume_token (parser->lexer);
3435 if (!error_p)
3436 {
3437 tree decl;
3438
3439 decl = cp_parser_lookup_name_simple (parser, token->value);
3440 if (TREE_CODE (decl) == TEMPLATE_DECL)
3441 error ("%qD used without template parameters", decl);
3442 else
3443 cp_parser_name_lookup_error
3444 (parser, token->value, decl,
3445 "is not a class or namespace");
3446 parser->scope = NULL_TREE;
3447 error_p = true;
3448 /* Treat this as a successful nested-name-specifier
3449 due to:
3450
3451 [basic.lookup.qual]
3452
3453 If the name found is not a class-name (clause
3454 _class_) or namespace-name (_namespace.def_), the
3455 program is ill-formed. */
3456 success = true;
3457 }
3458 cp_lexer_consume_token (parser->lexer);
3459 }
3460 break;
3461 }
3462
3463 /* We've found one valid nested-name-specifier. */
3464 success = true;
3465 /* Make sure we look in the right scope the next time through
3466 the loop. */
3467 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3468 ? TREE_TYPE (new_scope)
3469 : new_scope);
3470 /* If it is a class scope, try to complete it; we are about to
3471 be looking up names inside the class. */
3472 if (TYPE_P (parser->scope)
3473 /* Since checking types for dependency can be expensive,
3474 avoid doing it if the type is already complete. */
3475 && !COMPLETE_TYPE_P (parser->scope)
3476 /* Do not try to complete dependent types. */
3477 && !dependent_type_p (parser->scope))
3478 complete_type (parser->scope);
3479 }
3480
3481 /* Retrieve any deferred checks. Do not pop this access checks yet
3482 so the memory will not be reclaimed during token replacing below. */
3483 access_check = get_deferred_access_checks ();
3484
3485 /* If parsing tentatively, replace the sequence of tokens that makes
3486 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3487 token. That way, should we re-parse the token stream, we will
3488 not have to repeat the effort required to do the parse, nor will
3489 we issue duplicate error messages. */
3490 if (success && start)
3491 {
3492 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3493
3494 /* Reset the contents of the START token. */
3495 token->type = CPP_NESTED_NAME_SPECIFIER;
3496 token->value = build_tree_list (access_check, parser->scope);
3497 TREE_TYPE (token->value) = parser->qualifying_scope;
3498 token->keyword = RID_MAX;
3499
3500 /* Purge all subsequent tokens. */
3501 cp_lexer_purge_tokens_after (parser->lexer, start);
3502 }
3503
3504 pop_deferring_access_checks ();
3505 return success ? parser->scope : NULL_TREE;
3506 }
3507
3508 /* Parse a nested-name-specifier. See
3509 cp_parser_nested_name_specifier_opt for details. This function
3510 behaves identically, except that it will an issue an error if no
3511 nested-name-specifier is present, and it will return
3512 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3513 is present. */
3514
3515 static tree
3516 cp_parser_nested_name_specifier (cp_parser *parser,
3517 bool typename_keyword_p,
3518 bool check_dependency_p,
3519 bool type_p,
3520 bool is_declaration)
3521 {
3522 tree scope;
3523
3524 /* Look for the nested-name-specifier. */
3525 scope = cp_parser_nested_name_specifier_opt (parser,
3526 typename_keyword_p,
3527 check_dependency_p,
3528 type_p,
3529 is_declaration);
3530 /* If it was not present, issue an error message. */
3531 if (!scope)
3532 {
3533 cp_parser_error (parser, "expected nested-name-specifier");
3534 parser->scope = NULL_TREE;
3535 return error_mark_node;
3536 }
3537
3538 return scope;
3539 }
3540
3541 /* Parse a class-or-namespace-name.
3542
3543 class-or-namespace-name:
3544 class-name
3545 namespace-name
3546
3547 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3548 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3549 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3550 TYPE_P is TRUE iff the next name should be taken as a class-name,
3551 even the same name is declared to be another entity in the same
3552 scope.
3553
3554 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3555 specified by the class-or-namespace-name. If neither is found the
3556 ERROR_MARK_NODE is returned. */
3557
3558 static tree
3559 cp_parser_class_or_namespace_name (cp_parser *parser,
3560 bool typename_keyword_p,
3561 bool template_keyword_p,
3562 bool check_dependency_p,
3563 bool type_p,
3564 bool is_declaration)
3565 {
3566 tree saved_scope;
3567 tree saved_qualifying_scope;
3568 tree saved_object_scope;
3569 tree scope;
3570 bool only_class_p;
3571
3572 /* Before we try to parse the class-name, we must save away the
3573 current PARSER->SCOPE since cp_parser_class_name will destroy
3574 it. */
3575 saved_scope = parser->scope;
3576 saved_qualifying_scope = parser->qualifying_scope;
3577 saved_object_scope = parser->object_scope;
3578 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3579 there is no need to look for a namespace-name. */
3580 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3581 if (!only_class_p)
3582 cp_parser_parse_tentatively (parser);
3583 scope = cp_parser_class_name (parser,
3584 typename_keyword_p,
3585 template_keyword_p,
3586 type_p ? class_type : none_type,
3587 check_dependency_p,
3588 /*class_head_p=*/false,
3589 is_declaration);
3590 /* If that didn't work, try for a namespace-name. */
3591 if (!only_class_p && !cp_parser_parse_definitely (parser))
3592 {
3593 /* Restore the saved scope. */
3594 parser->scope = saved_scope;
3595 parser->qualifying_scope = saved_qualifying_scope;
3596 parser->object_scope = saved_object_scope;
3597 /* If we are not looking at an identifier followed by the scope
3598 resolution operator, then this is not part of a
3599 nested-name-specifier. (Note that this function is only used
3600 to parse the components of a nested-name-specifier.) */
3601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3602 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3603 return error_mark_node;
3604 scope = cp_parser_namespace_name (parser);
3605 }
3606
3607 return scope;
3608 }
3609
3610 /* Parse a postfix-expression.
3611
3612 postfix-expression:
3613 primary-expression
3614 postfix-expression [ expression ]
3615 postfix-expression ( expression-list [opt] )
3616 simple-type-specifier ( expression-list [opt] )
3617 typename :: [opt] nested-name-specifier identifier
3618 ( expression-list [opt] )
3619 typename :: [opt] nested-name-specifier template [opt] template-id
3620 ( expression-list [opt] )
3621 postfix-expression . template [opt] id-expression
3622 postfix-expression -> template [opt] id-expression
3623 postfix-expression . pseudo-destructor-name
3624 postfix-expression -> pseudo-destructor-name
3625 postfix-expression ++
3626 postfix-expression --
3627 dynamic_cast < type-id > ( expression )
3628 static_cast < type-id > ( expression )
3629 reinterpret_cast < type-id > ( expression )
3630 const_cast < type-id > ( expression )
3631 typeid ( expression )
3632 typeid ( type-id )
3633
3634 GNU Extension:
3635
3636 postfix-expression:
3637 ( type-id ) { initializer-list , [opt] }
3638
3639 This extension is a GNU version of the C99 compound-literal
3640 construct. (The C99 grammar uses `type-name' instead of `type-id',
3641 but they are essentially the same concept.)
3642
3643 If ADDRESS_P is true, the postfix expression is the operand of the
3644 `&' operator.
3645
3646 Returns a representation of the expression. */
3647
3648 static tree
3649 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3650 {
3651 cp_token *token;
3652 enum rid keyword;
3653 cp_id_kind idk = CP_ID_KIND_NONE;
3654 tree postfix_expression = NULL_TREE;
3655 /* Non-NULL only if the current postfix-expression can be used to
3656 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3657 class used to qualify the member. */
3658 tree qualifying_class = NULL_TREE;
3659
3660 /* Peek at the next token. */
3661 token = cp_lexer_peek_token (parser->lexer);
3662 /* Some of the productions are determined by keywords. */
3663 keyword = token->keyword;
3664 switch (keyword)
3665 {
3666 case RID_DYNCAST:
3667 case RID_STATCAST:
3668 case RID_REINTCAST:
3669 case RID_CONSTCAST:
3670 {
3671 tree type;
3672 tree expression;
3673 const char *saved_message;
3674
3675 /* All of these can be handled in the same way from the point
3676 of view of parsing. Begin by consuming the token
3677 identifying the cast. */
3678 cp_lexer_consume_token (parser->lexer);
3679
3680 /* New types cannot be defined in the cast. */
3681 saved_message = parser->type_definition_forbidden_message;
3682 parser->type_definition_forbidden_message
3683 = "types may not be defined in casts";
3684
3685 /* Look for the opening `<'. */
3686 cp_parser_require (parser, CPP_LESS, "`<'");
3687 /* Parse the type to which we are casting. */
3688 type = cp_parser_type_id (parser);
3689 /* Look for the closing `>'. */
3690 cp_parser_require (parser, CPP_GREATER, "`>'");
3691 /* Restore the old message. */
3692 parser->type_definition_forbidden_message = saved_message;
3693
3694 /* And the expression which is being cast. */
3695 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3696 expression = cp_parser_expression (parser);
3697 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3698
3699 /* Only type conversions to integral or enumeration types
3700 can be used in constant-expressions. */
3701 if (parser->integral_constant_expression_p
3702 && !dependent_type_p (type)
3703 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3704 && (cp_parser_non_integral_constant_expression
3705 (parser,
3706 "a cast to a type other than an integral or "
3707 "enumeration type")))
3708 return error_mark_node;
3709
3710 switch (keyword)
3711 {
3712 case RID_DYNCAST:
3713 postfix_expression
3714 = build_dynamic_cast (type, expression);
3715 break;
3716 case RID_STATCAST:
3717 postfix_expression
3718 = build_static_cast (type, expression);
3719 break;
3720 case RID_REINTCAST:
3721 postfix_expression
3722 = build_reinterpret_cast (type, expression);
3723 break;
3724 case RID_CONSTCAST:
3725 postfix_expression
3726 = build_const_cast (type, expression);
3727 break;
3728 default:
3729 gcc_unreachable ();
3730 }
3731 }
3732 break;
3733
3734 case RID_TYPEID:
3735 {
3736 tree type;
3737 const char *saved_message;
3738 bool saved_in_type_id_in_expr_p;
3739
3740 /* Consume the `typeid' token. */
3741 cp_lexer_consume_token (parser->lexer);
3742 /* Look for the `(' token. */
3743 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3744 /* Types cannot be defined in a `typeid' expression. */
3745 saved_message = parser->type_definition_forbidden_message;
3746 parser->type_definition_forbidden_message
3747 = "types may not be defined in a `typeid\' expression";
3748 /* We can't be sure yet whether we're looking at a type-id or an
3749 expression. */
3750 cp_parser_parse_tentatively (parser);
3751 /* Try a type-id first. */
3752 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3753 parser->in_type_id_in_expr_p = true;
3754 type = cp_parser_type_id (parser);
3755 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3756 /* Look for the `)' token. Otherwise, we can't be sure that
3757 we're not looking at an expression: consider `typeid (int
3758 (3))', for example. */
3759 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3760 /* If all went well, simply lookup the type-id. */
3761 if (cp_parser_parse_definitely (parser))
3762 postfix_expression = get_typeid (type);
3763 /* Otherwise, fall back to the expression variant. */
3764 else
3765 {
3766 tree expression;
3767
3768 /* Look for an expression. */
3769 expression = cp_parser_expression (parser);
3770 /* Compute its typeid. */
3771 postfix_expression = build_typeid (expression);
3772 /* Look for the `)' token. */
3773 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3774 }
3775 /* `typeid' may not appear in an integral constant expression. */
3776 if (cp_parser_non_integral_constant_expression(parser,
3777 "`typeid' operator"))
3778 return error_mark_node;
3779 /* Restore the saved message. */
3780 parser->type_definition_forbidden_message = saved_message;
3781 }
3782 break;
3783
3784 case RID_TYPENAME:
3785 {
3786 bool template_p = false;
3787 tree id;
3788 tree type;
3789
3790 /* Consume the `typename' token. */
3791 cp_lexer_consume_token (parser->lexer);
3792 /* Look for the optional `::' operator. */
3793 cp_parser_global_scope_opt (parser,
3794 /*current_scope_valid_p=*/false);
3795 /* Look for the nested-name-specifier. */
3796 cp_parser_nested_name_specifier (parser,
3797 /*typename_keyword_p=*/true,
3798 /*check_dependency_p=*/true,
3799 /*type_p=*/true,
3800 /*is_declaration=*/true);
3801 /* Look for the optional `template' keyword. */
3802 template_p = cp_parser_optional_template_keyword (parser);
3803 /* We don't know whether we're looking at a template-id or an
3804 identifier. */
3805 cp_parser_parse_tentatively (parser);
3806 /* Try a template-id. */
3807 id = cp_parser_template_id (parser, template_p,
3808 /*check_dependency_p=*/true,
3809 /*is_declaration=*/true);
3810 /* If that didn't work, try an identifier. */
3811 if (!cp_parser_parse_definitely (parser))
3812 id = cp_parser_identifier (parser);
3813 /* If we look up a template-id in a non-dependent qualifying
3814 scope, there's no need to create a dependent type. */
3815 if (TREE_CODE (id) == TYPE_DECL
3816 && !dependent_type_p (parser->scope))
3817 type = TREE_TYPE (id);
3818 /* Create a TYPENAME_TYPE to represent the type to which the
3819 functional cast is being performed. */
3820 else
3821 type = make_typename_type (parser->scope, id,
3822 typename_type,
3823 /*complain=*/1);
3824
3825 postfix_expression = cp_parser_functional_cast (parser, type);
3826 }
3827 break;
3828
3829 default:
3830 {
3831 tree type;
3832
3833 /* If the next thing is a simple-type-specifier, we may be
3834 looking at a functional cast. We could also be looking at
3835 an id-expression. So, we try the functional cast, and if
3836 that doesn't work we fall back to the primary-expression. */
3837 cp_parser_parse_tentatively (parser);
3838 /* Look for the simple-type-specifier. */
3839 type = cp_parser_simple_type_specifier (parser,
3840 /*decl_specs=*/NULL,
3841 CP_PARSER_FLAGS_NONE);
3842 /* Parse the cast itself. */
3843 if (!cp_parser_error_occurred (parser))
3844 postfix_expression
3845 = cp_parser_functional_cast (parser, type);
3846 /* If that worked, we're done. */
3847 if (cp_parser_parse_definitely (parser))
3848 break;
3849
3850 /* If the functional-cast didn't work out, try a
3851 compound-literal. */
3852 if (cp_parser_allow_gnu_extensions_p (parser)
3853 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3854 {
3855 tree initializer_list = NULL_TREE;
3856 bool saved_in_type_id_in_expr_p;
3857
3858 cp_parser_parse_tentatively (parser);
3859 /* Consume the `('. */
3860 cp_lexer_consume_token (parser->lexer);
3861 /* Parse the type. */
3862 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3863 parser->in_type_id_in_expr_p = true;
3864 type = cp_parser_type_id (parser);
3865 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3866 /* Look for the `)'. */
3867 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3868 /* Look for the `{'. */
3869 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3870 /* If things aren't going well, there's no need to
3871 keep going. */
3872 if (!cp_parser_error_occurred (parser))
3873 {
3874 bool non_constant_p;
3875 /* Parse the initializer-list. */
3876 initializer_list
3877 = cp_parser_initializer_list (parser, &non_constant_p);
3878 /* Allow a trailing `,'. */
3879 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3880 cp_lexer_consume_token (parser->lexer);
3881 /* Look for the final `}'. */
3882 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3883 }
3884 /* If that worked, we're definitely looking at a
3885 compound-literal expression. */
3886 if (cp_parser_parse_definitely (parser))
3887 {
3888 /* Warn the user that a compound literal is not
3889 allowed in standard C++. */
3890 if (pedantic)
3891 pedwarn ("ISO C++ forbids compound-literals");
3892 /* Form the representation of the compound-literal. */
3893 postfix_expression
3894 = finish_compound_literal (type, initializer_list);
3895 break;
3896 }
3897 }
3898
3899 /* It must be a primary-expression. */
3900 postfix_expression = cp_parser_primary_expression (parser,
3901 &idk,
3902 &qualifying_class);
3903 }
3904 break;
3905 }
3906
3907 /* If we were avoiding committing to the processing of a
3908 qualified-id until we knew whether or not we had a
3909 pointer-to-member, we now know. */
3910 if (qualifying_class)
3911 {
3912 bool done;
3913
3914 /* Peek at the next token. */
3915 token = cp_lexer_peek_token (parser->lexer);
3916 done = (token->type != CPP_OPEN_SQUARE
3917 && token->type != CPP_OPEN_PAREN
3918 && token->type != CPP_DOT
3919 && token->type != CPP_DEREF
3920 && token->type != CPP_PLUS_PLUS
3921 && token->type != CPP_MINUS_MINUS);
3922
3923 postfix_expression = finish_qualified_id_expr (qualifying_class,
3924 postfix_expression,
3925 done,
3926 address_p);
3927 if (done)
3928 return postfix_expression;
3929 }
3930
3931 /* Keep looping until the postfix-expression is complete. */
3932 while (true)
3933 {
3934 if (idk == CP_ID_KIND_UNQUALIFIED
3935 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3936 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3937 /* It is not a Koenig lookup function call. */
3938 postfix_expression
3939 = unqualified_name_lookup_error (postfix_expression);
3940
3941 /* Peek at the next token. */
3942 token = cp_lexer_peek_token (parser->lexer);
3943
3944 switch (token->type)
3945 {
3946 case CPP_OPEN_SQUARE:
3947 postfix_expression
3948 = cp_parser_postfix_open_square_expression (parser,
3949 postfix_expression,
3950 false);
3951 idk = CP_ID_KIND_NONE;
3952 break;
3953
3954 case CPP_OPEN_PAREN:
3955 /* postfix-expression ( expression-list [opt] ) */
3956 {
3957 bool koenig_p;
3958 tree args = (cp_parser_parenthesized_expression_list
3959 (parser, false, /*non_constant_p=*/NULL));
3960
3961 if (args == error_mark_node)
3962 {
3963 postfix_expression = error_mark_node;
3964 break;
3965 }
3966
3967 /* Function calls are not permitted in
3968 constant-expressions. */
3969 if (cp_parser_non_integral_constant_expression (parser,
3970 "a function call"))
3971 {
3972 postfix_expression = error_mark_node;
3973 break;
3974 }
3975
3976 koenig_p = false;
3977 if (idk == CP_ID_KIND_UNQUALIFIED)
3978 {
3979 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3980 {
3981 if (args)
3982 {
3983 koenig_p = true;
3984 postfix_expression
3985 = perform_koenig_lookup (postfix_expression, args);
3986 }
3987 else
3988 postfix_expression
3989 = unqualified_fn_lookup_error (postfix_expression);
3990 }
3991 /* We do not perform argument-dependent lookup if
3992 normal lookup finds a non-function, in accordance
3993 with the expected resolution of DR 218. */
3994 else if (args && is_overloaded_fn (postfix_expression))
3995 {
3996 tree fn = get_first_fn (postfix_expression);
3997
3998 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3999 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4000
4001 /* Only do argument dependent lookup if regular
4002 lookup does not find a set of member functions.
4003 [basic.lookup.koenig]/2a */
4004 if (!DECL_FUNCTION_MEMBER_P (fn))
4005 {
4006 koenig_p = true;
4007 postfix_expression
4008 = perform_koenig_lookup (postfix_expression, args);
4009 }
4010 }
4011 }
4012
4013 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4014 {
4015 tree instance = TREE_OPERAND (postfix_expression, 0);
4016 tree fn = TREE_OPERAND (postfix_expression, 1);
4017
4018 if (processing_template_decl
4019 && (type_dependent_expression_p (instance)
4020 || (!BASELINK_P (fn)
4021 && TREE_CODE (fn) != FIELD_DECL)
4022 || type_dependent_expression_p (fn)
4023 || any_type_dependent_arguments_p (args)))
4024 {
4025 postfix_expression
4026 = build_min_nt (CALL_EXPR, postfix_expression,
4027 args, NULL_TREE);
4028 break;
4029 }
4030
4031 if (BASELINK_P (fn))
4032 postfix_expression
4033 = (build_new_method_call
4034 (instance, fn, args, NULL_TREE,
4035 (idk == CP_ID_KIND_QUALIFIED
4036 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4037 else
4038 postfix_expression
4039 = finish_call_expr (postfix_expression, args,
4040 /*disallow_virtual=*/false,
4041 /*koenig_p=*/false);
4042 }
4043 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4044 || TREE_CODE (postfix_expression) == MEMBER_REF
4045 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4046 postfix_expression = (build_offset_ref_call_from_tree
4047 (postfix_expression, args));
4048 else if (idk == CP_ID_KIND_QUALIFIED)
4049 /* A call to a static class member, or a namespace-scope
4050 function. */
4051 postfix_expression
4052 = finish_call_expr (postfix_expression, args,
4053 /*disallow_virtual=*/true,
4054 koenig_p);
4055 else
4056 /* All other function calls. */
4057 postfix_expression
4058 = finish_call_expr (postfix_expression, args,
4059 /*disallow_virtual=*/false,
4060 koenig_p);
4061
4062 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4063 idk = CP_ID_KIND_NONE;
4064 }
4065 break;
4066
4067 case CPP_DOT:
4068 case CPP_DEREF:
4069 /* postfix-expression . template [opt] id-expression
4070 postfix-expression . pseudo-destructor-name
4071 postfix-expression -> template [opt] id-expression
4072 postfix-expression -> pseudo-destructor-name */
4073
4074 /* Consume the `.' or `->' operator. */
4075 cp_lexer_consume_token (parser->lexer);
4076
4077 postfix_expression
4078 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4079 postfix_expression,
4080 false, &idk);
4081 break;
4082
4083 case CPP_PLUS_PLUS:
4084 /* postfix-expression ++ */
4085 /* Consume the `++' token. */
4086 cp_lexer_consume_token (parser->lexer);
4087 /* Generate a representation for the complete expression. */
4088 postfix_expression
4089 = finish_increment_expr (postfix_expression,
4090 POSTINCREMENT_EXPR);
4091 /* Increments may not appear in constant-expressions. */
4092 if (cp_parser_non_integral_constant_expression (parser,
4093 "an increment"))
4094 postfix_expression = error_mark_node;
4095 idk = CP_ID_KIND_NONE;
4096 break;
4097
4098 case CPP_MINUS_MINUS:
4099 /* postfix-expression -- */
4100 /* Consume the `--' token. */
4101 cp_lexer_consume_token (parser->lexer);
4102 /* Generate a representation for the complete expression. */
4103 postfix_expression
4104 = finish_increment_expr (postfix_expression,
4105 POSTDECREMENT_EXPR);
4106 /* Decrements may not appear in constant-expressions. */
4107 if (cp_parser_non_integral_constant_expression (parser,
4108 "a decrement"))
4109 postfix_expression = error_mark_node;
4110 idk = CP_ID_KIND_NONE;
4111 break;
4112
4113 default:
4114 return postfix_expression;
4115 }
4116 }
4117
4118 /* We should never get here. */
4119 gcc_unreachable ();
4120 return error_mark_node;
4121 }
4122
4123 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4124 by cp_parser_builtin_offsetof. We're looking for
4125
4126 postfix-expression [ expression ]
4127
4128 FOR_OFFSETOF is set if we're being called in that context, which
4129 changes how we deal with integer constant expressions. */
4130
4131 static tree
4132 cp_parser_postfix_open_square_expression (cp_parser *parser,
4133 tree postfix_expression,
4134 bool for_offsetof)
4135 {
4136 tree index;
4137
4138 /* Consume the `[' token. */
4139 cp_lexer_consume_token (parser->lexer);
4140
4141 /* Parse the index expression. */
4142 /* ??? For offsetof, there is a question of what to allow here. If
4143 offsetof is not being used in an integral constant expression context,
4144 then we *could* get the right answer by computing the value at runtime.
4145 If we are in an integral constant expression context, then we might
4146 could accept any constant expression; hard to say without analysis.
4147 Rather than open the barn door too wide right away, allow only integer
4148 constant expressions here. */
4149 if (for_offsetof)
4150 index = cp_parser_constant_expression (parser, false, NULL);
4151 else
4152 index = cp_parser_expression (parser);
4153
4154 /* Look for the closing `]'. */
4155 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4156
4157 /* Build the ARRAY_REF. */
4158 postfix_expression = grok_array_decl (postfix_expression, index);
4159
4160 /* When not doing offsetof, array references are not permitted in
4161 constant-expressions. */
4162 if (!for_offsetof
4163 && (cp_parser_non_integral_constant_expression
4164 (parser, "an array reference")))
4165 postfix_expression = error_mark_node;
4166
4167 return postfix_expression;
4168 }
4169
4170 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4171 by cp_parser_builtin_offsetof. We're looking for
4172
4173 postfix-expression . template [opt] id-expression
4174 postfix-expression . pseudo-destructor-name
4175 postfix-expression -> template [opt] id-expression
4176 postfix-expression -> pseudo-destructor-name
4177
4178 FOR_OFFSETOF is set if we're being called in that context. That sorta
4179 limits what of the above we'll actually accept, but nevermind.
4180 TOKEN_TYPE is the "." or "->" token, which will already have been
4181 removed from the stream. */
4182
4183 static tree
4184 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4185 enum cpp_ttype token_type,
4186 tree postfix_expression,
4187 bool for_offsetof, cp_id_kind *idk)
4188 {
4189 tree name;
4190 bool dependent_p;
4191 bool template_p;
4192 bool pseudo_destructor_p;
4193 tree scope = NULL_TREE;
4194
4195 /* If this is a `->' operator, dereference the pointer. */
4196 if (token_type == CPP_DEREF)
4197 postfix_expression = build_x_arrow (postfix_expression);
4198 /* Check to see whether or not the expression is type-dependent. */
4199 dependent_p = type_dependent_expression_p (postfix_expression);
4200 /* The identifier following the `->' or `.' is not qualified. */
4201 parser->scope = NULL_TREE;
4202 parser->qualifying_scope = NULL_TREE;
4203 parser->object_scope = NULL_TREE;
4204 *idk = CP_ID_KIND_NONE;
4205 /* Enter the scope corresponding to the type of the object
4206 given by the POSTFIX_EXPRESSION. */
4207 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4208 {
4209 scope = TREE_TYPE (postfix_expression);
4210 /* According to the standard, no expression should ever have
4211 reference type. Unfortunately, we do not currently match
4212 the standard in this respect in that our internal representation
4213 of an expression may have reference type even when the standard
4214 says it does not. Therefore, we have to manually obtain the
4215 underlying type here. */
4216 scope = non_reference (scope);
4217 /* The type of the POSTFIX_EXPRESSION must be complete. */
4218 scope = complete_type_or_else (scope, NULL_TREE);
4219 /* Let the name lookup machinery know that we are processing a
4220 class member access expression. */
4221 parser->context->object_type = scope;
4222 /* If something went wrong, we want to be able to discern that case,
4223 as opposed to the case where there was no SCOPE due to the type
4224 of expression being dependent. */
4225 if (!scope)
4226 scope = error_mark_node;
4227 /* If the SCOPE was erroneous, make the various semantic analysis
4228 functions exit quickly -- and without issuing additional error
4229 messages. */
4230 if (scope == error_mark_node)
4231 postfix_expression = error_mark_node;
4232 }
4233
4234 /* Assume this expression is not a pseudo-destructor access. */
4235 pseudo_destructor_p = false;
4236
4237 /* If the SCOPE is a scalar type, then, if this is a valid program,
4238 we must be looking at a pseudo-destructor-name. */
4239 if (scope && SCALAR_TYPE_P (scope))
4240 {
4241 tree s;
4242 tree type;
4243
4244 cp_parser_parse_tentatively (parser);
4245 /* Parse the pseudo-destructor-name. */
4246 s = NULL_TREE;
4247 cp_parser_pseudo_destructor_name (parser, &s, &type);
4248 if (cp_parser_parse_definitely (parser))
4249 {
4250 pseudo_destructor_p = true;
4251 postfix_expression
4252 = finish_pseudo_destructor_expr (postfix_expression,
4253 s, TREE_TYPE (type));
4254 }
4255 }
4256
4257 if (!pseudo_destructor_p)
4258 {
4259 /* If the SCOPE is not a scalar type, we are looking at an
4260 ordinary class member access expression, rather than a
4261 pseudo-destructor-name. */
4262 template_p = cp_parser_optional_template_keyword (parser);
4263 /* Parse the id-expression. */
4264 name = cp_parser_id_expression (parser, template_p,
4265 /*check_dependency_p=*/true,
4266 /*template_p=*/NULL,
4267 /*declarator_p=*/false);
4268 /* In general, build a SCOPE_REF if the member name is qualified.
4269 However, if the name was not dependent and has already been
4270 resolved; there is no need to build the SCOPE_REF. For example;
4271
4272 struct X { void f(); };
4273 template <typename T> void f(T* t) { t->X::f(); }
4274
4275 Even though "t" is dependent, "X::f" is not and has been resolved
4276 to a BASELINK; there is no need to include scope information. */
4277
4278 /* But we do need to remember that there was an explicit scope for
4279 virtual function calls. */
4280 if (parser->scope)
4281 *idk = CP_ID_KIND_QUALIFIED;
4282
4283 /* If the name is a template-id that names a type, we will get a
4284 TYPE_DECL here. That is invalid code. */
4285 if (TREE_CODE (name) == TYPE_DECL)
4286 {
4287 error ("invalid use of %qD", name);
4288 postfix_expression = error_mark_node;
4289 }
4290 else
4291 {
4292 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4293 {
4294 name = build_nt (SCOPE_REF, parser->scope, name);
4295 parser->scope = NULL_TREE;
4296 parser->qualifying_scope = NULL_TREE;
4297 parser->object_scope = NULL_TREE;
4298 }
4299 if (scope && name && BASELINK_P (name))
4300 adjust_result_of_qualified_name_lookup
4301 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4302 postfix_expression
4303 = finish_class_member_access_expr (postfix_expression, name);
4304 }
4305 }
4306
4307 /* We no longer need to look up names in the scope of the object on
4308 the left-hand side of the `.' or `->' operator. */
4309 parser->context->object_type = NULL_TREE;
4310
4311 /* Outside of offsetof, these operators may not appear in
4312 constant-expressions. */
4313 if (!for_offsetof
4314 && (cp_parser_non_integral_constant_expression
4315 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4316 postfix_expression = error_mark_node;
4317
4318 return postfix_expression;
4319 }
4320
4321 /* Parse a parenthesized expression-list.
4322
4323 expression-list:
4324 assignment-expression
4325 expression-list, assignment-expression
4326
4327 attribute-list:
4328 expression-list
4329 identifier
4330 identifier, expression-list
4331
4332 Returns a TREE_LIST. The TREE_VALUE of each node is a
4333 representation of an assignment-expression. Note that a TREE_LIST
4334 is returned even if there is only a single expression in the list.
4335 error_mark_node is returned if the ( and or ) are
4336 missing. NULL_TREE is returned on no expressions. The parentheses
4337 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4338 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4339 indicates whether or not all of the expressions in the list were
4340 constant. */
4341
4342 static tree
4343 cp_parser_parenthesized_expression_list (cp_parser* parser,
4344 bool is_attribute_list,
4345 bool *non_constant_p)
4346 {
4347 tree expression_list = NULL_TREE;
4348 bool fold_expr_p = is_attribute_list;
4349 tree identifier = NULL_TREE;
4350
4351 /* Assume all the expressions will be constant. */
4352 if (non_constant_p)
4353 *non_constant_p = false;
4354
4355 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4356 return error_mark_node;
4357
4358 /* Consume expressions until there are no more. */
4359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4360 while (true)
4361 {
4362 tree expr;
4363
4364 /* At the beginning of attribute lists, check to see if the
4365 next token is an identifier. */
4366 if (is_attribute_list
4367 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4368 {
4369 cp_token *token;
4370
4371 /* Consume the identifier. */
4372 token = cp_lexer_consume_token (parser->lexer);
4373 /* Save the identifier. */
4374 identifier = token->value;
4375 }
4376 else
4377 {
4378 /* Parse the next assignment-expression. */
4379 if (non_constant_p)
4380 {
4381 bool expr_non_constant_p;
4382 expr = (cp_parser_constant_expression
4383 (parser, /*allow_non_constant_p=*/true,
4384 &expr_non_constant_p));
4385 if (expr_non_constant_p)
4386 *non_constant_p = true;
4387 }
4388 else
4389 expr = cp_parser_assignment_expression (parser);
4390
4391 if (fold_expr_p)
4392 expr = fold_non_dependent_expr (expr);
4393
4394 /* Add it to the list. We add error_mark_node
4395 expressions to the list, so that we can still tell if
4396 the correct form for a parenthesized expression-list
4397 is found. That gives better errors. */
4398 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4399
4400 if (expr == error_mark_node)
4401 goto skip_comma;
4402 }
4403
4404 /* After the first item, attribute lists look the same as
4405 expression lists. */
4406 is_attribute_list = false;
4407
4408 get_comma:;
4409 /* If the next token isn't a `,', then we are done. */
4410 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4411 break;
4412
4413 /* Otherwise, consume the `,' and keep going. */
4414 cp_lexer_consume_token (parser->lexer);
4415 }
4416
4417 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4418 {
4419 int ending;
4420
4421 skip_comma:;
4422 /* We try and resync to an unnested comma, as that will give the
4423 user better diagnostics. */
4424 ending = cp_parser_skip_to_closing_parenthesis (parser,
4425 /*recovering=*/true,
4426 /*or_comma=*/true,
4427 /*consume_paren=*/true);
4428 if (ending < 0)
4429 goto get_comma;
4430 if (!ending)
4431 return error_mark_node;
4432 }
4433
4434 /* We built up the list in reverse order so we must reverse it now. */
4435 expression_list = nreverse (expression_list);
4436 if (identifier)
4437 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4438
4439 return expression_list;
4440 }
4441
4442 /* Parse a pseudo-destructor-name.
4443
4444 pseudo-destructor-name:
4445 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4446 :: [opt] nested-name-specifier template template-id :: ~ type-name
4447 :: [opt] nested-name-specifier [opt] ~ type-name
4448
4449 If either of the first two productions is used, sets *SCOPE to the
4450 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4451 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4452 or ERROR_MARK_NODE if the parse fails. */
4453
4454 static void
4455 cp_parser_pseudo_destructor_name (cp_parser* parser,
4456 tree* scope,
4457 tree* type)
4458 {
4459 bool nested_name_specifier_p;
4460
4461 /* Assume that things will not work out. */
4462 *type = error_mark_node;
4463
4464 /* Look for the optional `::' operator. */
4465 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4466 /* Look for the optional nested-name-specifier. */
4467 nested_name_specifier_p
4468 = (cp_parser_nested_name_specifier_opt (parser,
4469 /*typename_keyword_p=*/false,
4470 /*check_dependency_p=*/true,
4471 /*type_p=*/false,
4472 /*is_declaration=*/true)
4473 != NULL_TREE);
4474 /* Now, if we saw a nested-name-specifier, we might be doing the
4475 second production. */
4476 if (nested_name_specifier_p
4477 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4478 {
4479 /* Consume the `template' keyword. */
4480 cp_lexer_consume_token (parser->lexer);
4481 /* Parse the template-id. */
4482 cp_parser_template_id (parser,
4483 /*template_keyword_p=*/true,
4484 /*check_dependency_p=*/false,
4485 /*is_declaration=*/true);
4486 /* Look for the `::' token. */
4487 cp_parser_require (parser, CPP_SCOPE, "`::'");
4488 }
4489 /* If the next token is not a `~', then there might be some
4490 additional qualification. */
4491 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4492 {
4493 /* Look for the type-name. */
4494 *scope = TREE_TYPE (cp_parser_type_name (parser));
4495
4496 if (*scope == error_mark_node)
4497 return;
4498
4499 /* If we don't have ::~, then something has gone wrong. Since
4500 the only caller of this function is looking for something
4501 after `.' or `->' after a scalar type, most likely the
4502 program is trying to get a member of a non-aggregate
4503 type. */
4504 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4505 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4506 {
4507 cp_parser_error (parser, "request for member of non-aggregate type");
4508 return;
4509 }
4510
4511 /* Look for the `::' token. */
4512 cp_parser_require (parser, CPP_SCOPE, "`::'");
4513 }
4514 else
4515 *scope = NULL_TREE;
4516
4517 /* Look for the `~'. */
4518 cp_parser_require (parser, CPP_COMPL, "`~'");
4519 /* Look for the type-name again. We are not responsible for
4520 checking that it matches the first type-name. */
4521 *type = cp_parser_type_name (parser);
4522 }
4523
4524 /* Parse a unary-expression.
4525
4526 unary-expression:
4527 postfix-expression
4528 ++ cast-expression
4529 -- cast-expression
4530 unary-operator cast-expression
4531 sizeof unary-expression
4532 sizeof ( type-id )
4533 new-expression
4534 delete-expression
4535
4536 GNU Extensions:
4537
4538 unary-expression:
4539 __extension__ cast-expression
4540 __alignof__ unary-expression
4541 __alignof__ ( type-id )
4542 __real__ cast-expression
4543 __imag__ cast-expression
4544 && identifier
4545
4546 ADDRESS_P is true iff the unary-expression is appearing as the
4547 operand of the `&' operator.
4548
4549 Returns a representation of the expression. */
4550
4551 static tree
4552 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4553 {
4554 cp_token *token;
4555 enum tree_code unary_operator;
4556
4557 /* Peek at the next token. */
4558 token = cp_lexer_peek_token (parser->lexer);
4559 /* Some keywords give away the kind of expression. */
4560 if (token->type == CPP_KEYWORD)
4561 {
4562 enum rid keyword = token->keyword;
4563
4564 switch (keyword)
4565 {
4566 case RID_ALIGNOF:
4567 case RID_SIZEOF:
4568 {
4569 tree operand;
4570 enum tree_code op;
4571
4572 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4573 /* Consume the token. */
4574 cp_lexer_consume_token (parser->lexer);
4575 /* Parse the operand. */
4576 operand = cp_parser_sizeof_operand (parser, keyword);
4577
4578 if (TYPE_P (operand))
4579 return cxx_sizeof_or_alignof_type (operand, op, true);
4580 else
4581 return cxx_sizeof_or_alignof_expr (operand, op);
4582 }
4583
4584 case RID_NEW:
4585 return cp_parser_new_expression (parser);
4586
4587 case RID_DELETE:
4588 return cp_parser_delete_expression (parser);
4589
4590 case RID_EXTENSION:
4591 {
4592 /* The saved value of the PEDANTIC flag. */
4593 int saved_pedantic;
4594 tree expr;
4595
4596 /* Save away the PEDANTIC flag. */
4597 cp_parser_extension_opt (parser, &saved_pedantic);
4598 /* Parse the cast-expression. */
4599 expr = cp_parser_simple_cast_expression (parser);
4600 /* Restore the PEDANTIC flag. */
4601 pedantic = saved_pedantic;
4602
4603 return expr;
4604 }
4605
4606 case RID_REALPART:
4607 case RID_IMAGPART:
4608 {
4609 tree expression;
4610
4611 /* Consume the `__real__' or `__imag__' token. */
4612 cp_lexer_consume_token (parser->lexer);
4613 /* Parse the cast-expression. */
4614 expression = cp_parser_simple_cast_expression (parser);
4615 /* Create the complete representation. */
4616 return build_x_unary_op ((keyword == RID_REALPART
4617 ? REALPART_EXPR : IMAGPART_EXPR),
4618 expression);
4619 }
4620 break;
4621
4622 default:
4623 break;
4624 }
4625 }
4626
4627 /* Look for the `:: new' and `:: delete', which also signal the
4628 beginning of a new-expression, or delete-expression,
4629 respectively. If the next token is `::', then it might be one of
4630 these. */
4631 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4632 {
4633 enum rid keyword;
4634
4635 /* See if the token after the `::' is one of the keywords in
4636 which we're interested. */
4637 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4638 /* If it's `new', we have a new-expression. */
4639 if (keyword == RID_NEW)
4640 return cp_parser_new_expression (parser);
4641 /* Similarly, for `delete'. */
4642 else if (keyword == RID_DELETE)
4643 return cp_parser_delete_expression (parser);
4644 }
4645
4646 /* Look for a unary operator. */
4647 unary_operator = cp_parser_unary_operator (token);
4648 /* The `++' and `--' operators can be handled similarly, even though
4649 they are not technically unary-operators in the grammar. */
4650 if (unary_operator == ERROR_MARK)
4651 {
4652 if (token->type == CPP_PLUS_PLUS)
4653 unary_operator = PREINCREMENT_EXPR;
4654 else if (token->type == CPP_MINUS_MINUS)
4655 unary_operator = PREDECREMENT_EXPR;
4656 /* Handle the GNU address-of-label extension. */
4657 else if (cp_parser_allow_gnu_extensions_p (parser)
4658 && token->type == CPP_AND_AND)
4659 {
4660 tree identifier;
4661
4662 /* Consume the '&&' token. */
4663 cp_lexer_consume_token (parser->lexer);
4664 /* Look for the identifier. */
4665 identifier = cp_parser_identifier (parser);
4666 /* Create an expression representing the address. */
4667 return finish_label_address_expr (identifier);
4668 }
4669 }
4670 if (unary_operator != ERROR_MARK)
4671 {
4672 tree cast_expression;
4673 tree expression = error_mark_node;
4674 const char *non_constant_p = NULL;
4675
4676 /* Consume the operator token. */
4677 token = cp_lexer_consume_token (parser->lexer);
4678 /* Parse the cast-expression. */
4679 cast_expression
4680 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4681 /* Now, build an appropriate representation. */
4682 switch (unary_operator)
4683 {
4684 case INDIRECT_REF:
4685 non_constant_p = "`*'";
4686 expression = build_x_indirect_ref (cast_expression, "unary *");
4687 break;
4688
4689 case ADDR_EXPR:
4690 non_constant_p = "`&'";
4691 /* Fall through. */
4692 case BIT_NOT_EXPR:
4693 expression = build_x_unary_op (unary_operator, cast_expression);
4694 break;
4695
4696 case PREINCREMENT_EXPR:
4697 case PREDECREMENT_EXPR:
4698 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4699 ? "`++'" : "`--'");
4700 /* Fall through. */
4701 case CONVERT_EXPR:
4702 case NEGATE_EXPR:
4703 case TRUTH_NOT_EXPR:
4704 expression = finish_unary_op_expr (unary_operator, cast_expression);
4705 break;
4706
4707 default:
4708 gcc_unreachable ();
4709 }
4710
4711 if (non_constant_p
4712 && cp_parser_non_integral_constant_expression (parser,
4713 non_constant_p))
4714 expression = error_mark_node;
4715
4716 return expression;
4717 }
4718
4719 return cp_parser_postfix_expression (parser, address_p);
4720 }
4721
4722 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4723 unary-operator, the corresponding tree code is returned. */
4724
4725 static enum tree_code
4726 cp_parser_unary_operator (cp_token* token)
4727 {
4728 switch (token->type)
4729 {
4730 case CPP_MULT:
4731 return INDIRECT_REF;
4732
4733 case CPP_AND:
4734 return ADDR_EXPR;
4735
4736 case CPP_PLUS:
4737 return CONVERT_EXPR;
4738
4739 case CPP_MINUS:
4740 return NEGATE_EXPR;
4741
4742 case CPP_NOT:
4743 return TRUTH_NOT_EXPR;
4744
4745 case CPP_COMPL:
4746 return BIT_NOT_EXPR;
4747
4748 default:
4749 return ERROR_MARK;
4750 }
4751 }
4752
4753 /* Parse a new-expression.
4754
4755 new-expression:
4756 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4757 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4758
4759 Returns a representation of the expression. */
4760
4761 static tree
4762 cp_parser_new_expression (cp_parser* parser)
4763 {
4764 bool global_scope_p;
4765 tree placement;
4766 tree type;
4767 tree initializer;
4768 tree nelts;
4769
4770 /* Look for the optional `::' operator. */
4771 global_scope_p
4772 = (cp_parser_global_scope_opt (parser,
4773 /*current_scope_valid_p=*/false)
4774 != NULL_TREE);
4775 /* Look for the `new' operator. */
4776 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4777 /* There's no easy way to tell a new-placement from the
4778 `( type-id )' construct. */
4779 cp_parser_parse_tentatively (parser);
4780 /* Look for a new-placement. */
4781 placement = cp_parser_new_placement (parser);
4782 /* If that didn't work out, there's no new-placement. */
4783 if (!cp_parser_parse_definitely (parser))
4784 placement = NULL_TREE;
4785
4786 /* If the next token is a `(', then we have a parenthesized
4787 type-id. */
4788 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4789 {
4790 /* Consume the `('. */
4791 cp_lexer_consume_token (parser->lexer);
4792 /* Parse the type-id. */
4793 type = cp_parser_type_id (parser);
4794 /* Look for the closing `)'. */
4795 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4796 /* There should not be a direct-new-declarator in this production,
4797 but GCC used to allowed this, so we check and emit a sensible error
4798 message for this case. */
4799 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4800 {
4801 error ("array bound forbidden after parenthesized type-id");
4802 inform ("try removing the parentheses around the type-id");
4803 cp_parser_direct_new_declarator (parser);
4804 }
4805 nelts = NULL_TREE;
4806 }
4807 /* Otherwise, there must be a new-type-id. */
4808 else
4809 type = cp_parser_new_type_id (parser, &nelts);
4810
4811 /* If the next token is a `(', then we have a new-initializer. */
4812 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4813 initializer = cp_parser_new_initializer (parser);
4814 else
4815 initializer = NULL_TREE;
4816
4817 /* A new-expression may not appear in an integral constant
4818 expression. */
4819 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4820 return error_mark_node;
4821
4822 /* Create a representation of the new-expression. */
4823 return build_new (placement, type, nelts, initializer, global_scope_p);
4824 }
4825
4826 /* Parse a new-placement.
4827
4828 new-placement:
4829 ( expression-list )
4830
4831 Returns the same representation as for an expression-list. */
4832
4833 static tree
4834 cp_parser_new_placement (cp_parser* parser)
4835 {
4836 tree expression_list;
4837
4838 /* Parse the expression-list. */
4839 expression_list = (cp_parser_parenthesized_expression_list
4840 (parser, false, /*non_constant_p=*/NULL));
4841
4842 return expression_list;
4843 }
4844
4845 /* Parse a new-type-id.
4846
4847 new-type-id:
4848 type-specifier-seq new-declarator [opt]
4849
4850 Returns the TYPE allocated. If the new-type-id indicates an array
4851 type, *NELTS is set to the number of elements in the last array
4852 bound; the TYPE will not include the last array bound. */
4853
4854 static tree
4855 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4856 {
4857 cp_decl_specifier_seq type_specifier_seq;
4858 cp_declarator *new_declarator;
4859 cp_declarator *declarator;
4860 cp_declarator *outer_declarator;
4861 const char *saved_message;
4862 tree type;
4863
4864 /* The type-specifier sequence must not contain type definitions.
4865 (It cannot contain declarations of new types either, but if they
4866 are not definitions we will catch that because they are not
4867 complete.) */
4868 saved_message = parser->type_definition_forbidden_message;
4869 parser->type_definition_forbidden_message
4870 = "types may not be defined in a new-type-id";
4871 /* Parse the type-specifier-seq. */
4872 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4873 /* Restore the old message. */
4874 parser->type_definition_forbidden_message = saved_message;
4875 /* Parse the new-declarator. */
4876 new_declarator = cp_parser_new_declarator_opt (parser);
4877
4878 /* Determine the number of elements in the last array dimension, if
4879 any. */
4880 *nelts = NULL_TREE;
4881 /* Skip down to the last array dimension. */
4882 declarator = new_declarator;
4883 outer_declarator = NULL;
4884 while (declarator && (declarator->kind == cdk_pointer
4885 || declarator->kind == cdk_ptrmem))
4886 {
4887 outer_declarator = declarator;
4888 declarator = declarator->declarator;
4889 }
4890 while (declarator
4891 && declarator->kind == cdk_array
4892 && declarator->declarator
4893 && declarator->declarator->kind == cdk_array)
4894 {
4895 outer_declarator = declarator;
4896 declarator = declarator->declarator;
4897 }
4898
4899 if (declarator && declarator->kind == cdk_array)
4900 {
4901 *nelts = declarator->u.array.bounds;
4902 if (*nelts == error_mark_node)
4903 *nelts = integer_one_node;
4904 else if (!processing_template_decl)
4905 {
4906 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4907 false))
4908 pedwarn ("size in array new must have integral type");
4909 *nelts = save_expr (cp_convert (sizetype, *nelts));
4910 if (*nelts == integer_zero_node)
4911 warning ("zero size array reserves no space");
4912 }
4913 if (outer_declarator)
4914 outer_declarator->declarator = declarator->declarator;
4915 else
4916 new_declarator = NULL;
4917 }
4918
4919 type = groktypename (&type_specifier_seq, new_declarator);
4920 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4921 {
4922 *nelts = array_type_nelts_top (type);
4923 type = TREE_TYPE (type);
4924 }
4925 return type;
4926 }
4927
4928 /* Parse an (optional) new-declarator.
4929
4930 new-declarator:
4931 ptr-operator new-declarator [opt]
4932 direct-new-declarator
4933
4934 Returns the declarator. */
4935
4936 static cp_declarator *
4937 cp_parser_new_declarator_opt (cp_parser* parser)
4938 {
4939 enum tree_code code;
4940 tree type;
4941 cp_cv_quals cv_quals;
4942
4943 /* We don't know if there's a ptr-operator next, or not. */
4944 cp_parser_parse_tentatively (parser);
4945 /* Look for a ptr-operator. */
4946 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4947 /* If that worked, look for more new-declarators. */
4948 if (cp_parser_parse_definitely (parser))
4949 {
4950 cp_declarator *declarator;
4951
4952 /* Parse another optional declarator. */
4953 declarator = cp_parser_new_declarator_opt (parser);
4954
4955 /* Create the representation of the declarator. */
4956 if (type)
4957 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4958 else if (code == INDIRECT_REF)
4959 declarator = make_pointer_declarator (cv_quals, declarator);
4960 else
4961 declarator = make_reference_declarator (cv_quals, declarator);
4962
4963 return declarator;
4964 }
4965
4966 /* If the next token is a `[', there is a direct-new-declarator. */
4967 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4968 return cp_parser_direct_new_declarator (parser);
4969
4970 return NULL;
4971 }
4972
4973 /* Parse a direct-new-declarator.
4974
4975 direct-new-declarator:
4976 [ expression ]
4977 direct-new-declarator [constant-expression]
4978
4979 */
4980
4981 static cp_declarator *
4982 cp_parser_direct_new_declarator (cp_parser* parser)
4983 {
4984 cp_declarator *declarator = NULL;
4985
4986 while (true)
4987 {
4988 tree expression;
4989
4990 /* Look for the opening `['. */
4991 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4992 /* The first expression is not required to be constant. */
4993 if (!declarator)
4994 {
4995 expression = cp_parser_expression (parser);
4996 /* The standard requires that the expression have integral
4997 type. DR 74 adds enumeration types. We believe that the
4998 real intent is that these expressions be handled like the
4999 expression in a `switch' condition, which also allows
5000 classes with a single conversion to integral or
5001 enumeration type. */
5002 if (!processing_template_decl)
5003 {
5004 expression
5005 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5006 expression,
5007 /*complain=*/true);
5008 if (!expression)
5009 {
5010 error ("expression in new-declarator must have integral "
5011 "or enumeration type");
5012 expression = error_mark_node;
5013 }
5014 }
5015 }
5016 /* But all the other expressions must be. */
5017 else
5018 expression
5019 = cp_parser_constant_expression (parser,
5020 /*allow_non_constant=*/false,
5021 NULL);
5022 /* Look for the closing `]'. */
5023 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5024
5025 /* Add this bound to the declarator. */
5026 declarator = make_array_declarator (declarator, expression);
5027
5028 /* If the next token is not a `[', then there are no more
5029 bounds. */
5030 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5031 break;
5032 }
5033
5034 return declarator;
5035 }
5036
5037 /* Parse a new-initializer.
5038
5039 new-initializer:
5040 ( expression-list [opt] )
5041
5042 Returns a representation of the expression-list. If there is no
5043 expression-list, VOID_ZERO_NODE is returned. */
5044
5045 static tree
5046 cp_parser_new_initializer (cp_parser* parser)
5047 {
5048 tree expression_list;
5049
5050 expression_list = (cp_parser_parenthesized_expression_list
5051 (parser, false, /*non_constant_p=*/NULL));
5052 if (!expression_list)
5053 expression_list = void_zero_node;
5054
5055 return expression_list;
5056 }
5057
5058 /* Parse a delete-expression.
5059
5060 delete-expression:
5061 :: [opt] delete cast-expression
5062 :: [opt] delete [ ] cast-expression
5063
5064 Returns a representation of the expression. */
5065
5066 static tree
5067 cp_parser_delete_expression (cp_parser* parser)
5068 {
5069 bool global_scope_p;
5070 bool array_p;
5071 tree expression;
5072
5073 /* Look for the optional `::' operator. */
5074 global_scope_p
5075 = (cp_parser_global_scope_opt (parser,
5076 /*current_scope_valid_p=*/false)
5077 != NULL_TREE);
5078 /* Look for the `delete' keyword. */
5079 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5080 /* See if the array syntax is in use. */
5081 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5082 {
5083 /* Consume the `[' token. */
5084 cp_lexer_consume_token (parser->lexer);
5085 /* Look for the `]' token. */
5086 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5087 /* Remember that this is the `[]' construct. */
5088 array_p = true;
5089 }
5090 else
5091 array_p = false;
5092
5093 /* Parse the cast-expression. */
5094 expression = cp_parser_simple_cast_expression (parser);
5095
5096 /* A delete-expression may not appear in an integral constant
5097 expression. */
5098 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5099 return error_mark_node;
5100
5101 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5102 }
5103
5104 /* Parse a cast-expression.
5105
5106 cast-expression:
5107 unary-expression
5108 ( type-id ) cast-expression
5109
5110 Returns a representation of the expression. */
5111
5112 static tree
5113 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5114 {
5115 /* If it's a `(', then we might be looking at a cast. */
5116 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5117 {
5118 tree type = NULL_TREE;
5119 tree expr = NULL_TREE;
5120 bool compound_literal_p;
5121 const char *saved_message;
5122
5123 /* There's no way to know yet whether or not this is a cast.
5124 For example, `(int (3))' is a unary-expression, while `(int)
5125 3' is a cast. So, we resort to parsing tentatively. */
5126 cp_parser_parse_tentatively (parser);
5127 /* Types may not be defined in a cast. */
5128 saved_message = parser->type_definition_forbidden_message;
5129 parser->type_definition_forbidden_message
5130 = "types may not be defined in casts";
5131 /* Consume the `('. */
5132 cp_lexer_consume_token (parser->lexer);
5133 /* A very tricky bit is that `(struct S) { 3 }' is a
5134 compound-literal (which we permit in C++ as an extension).
5135 But, that construct is not a cast-expression -- it is a
5136 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5137 is legal; if the compound-literal were a cast-expression,
5138 you'd need an extra set of parentheses.) But, if we parse
5139 the type-id, and it happens to be a class-specifier, then we
5140 will commit to the parse at that point, because we cannot
5141 undo the action that is done when creating a new class. So,
5142 then we cannot back up and do a postfix-expression.
5143
5144 Therefore, we scan ahead to the closing `)', and check to see
5145 if the token after the `)' is a `{'. If so, we are not
5146 looking at a cast-expression.
5147
5148 Save tokens so that we can put them back. */
5149 cp_lexer_save_tokens (parser->lexer);
5150 /* Skip tokens until the next token is a closing parenthesis.
5151 If we find the closing `)', and the next token is a `{', then
5152 we are looking at a compound-literal. */
5153 compound_literal_p
5154 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5155 /*consume_paren=*/true)
5156 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5157 /* Roll back the tokens we skipped. */
5158 cp_lexer_rollback_tokens (parser->lexer);
5159 /* If we were looking at a compound-literal, simulate an error
5160 so that the call to cp_parser_parse_definitely below will
5161 fail. */
5162 if (compound_literal_p)
5163 cp_parser_simulate_error (parser);
5164 else
5165 {
5166 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5167 parser->in_type_id_in_expr_p = true;
5168 /* Look for the type-id. */
5169 type = cp_parser_type_id (parser);
5170 /* Look for the closing `)'. */
5171 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5172 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5173 }
5174
5175 /* Restore the saved message. */
5176 parser->type_definition_forbidden_message = saved_message;
5177
5178 /* If ok so far, parse the dependent expression. We cannot be
5179 sure it is a cast. Consider `(T ())'. It is a parenthesized
5180 ctor of T, but looks like a cast to function returning T
5181 without a dependent expression. */
5182 if (!cp_parser_error_occurred (parser))
5183 expr = cp_parser_simple_cast_expression (parser);
5184
5185 if (cp_parser_parse_definitely (parser))
5186 {
5187 /* Warn about old-style casts, if so requested. */
5188 if (warn_old_style_cast
5189 && !in_system_header
5190 && !VOID_TYPE_P (type)
5191 && current_lang_name != lang_name_c)
5192 warning ("use of old-style cast");
5193
5194 /* Only type conversions to integral or enumeration types
5195 can be used in constant-expressions. */
5196 if (parser->integral_constant_expression_p
5197 && !dependent_type_p (type)
5198 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5199 && (cp_parser_non_integral_constant_expression
5200 (parser,
5201 "a cast to a type other than an integral or "
5202 "enumeration type")))
5203 return error_mark_node;
5204
5205 /* Perform the cast. */
5206 expr = build_c_cast (type, expr);
5207 return expr;
5208 }
5209 }
5210
5211 /* If we get here, then it's not a cast, so it must be a
5212 unary-expression. */
5213 return cp_parser_unary_expression (parser, address_p);
5214 }
5215
5216 /* Parse a binary expression of the general form:
5217
5218 pm-expression:
5219 cast-expression
5220 pm-expression .* cast-expression
5221 pm-expression ->* cast-expression
5222
5223 multiplicative-expression:
5224 pm-expression
5225 multiplicative-expression * pm-expression
5226 multiplicative-expression / pm-expression
5227 multiplicative-expression % pm-expression
5228
5229 additive-expression:
5230 multiplicative-expression
5231 additive-expression + multiplicative-expression
5232 additive-expression - multiplicative-expression
5233
5234 shift-expression:
5235 additive-expression
5236 shift-expression << additive-expression
5237 shift-expression >> additive-expression
5238
5239 relational-expression:
5240 shift-expression
5241 relational-expression < shift-expression
5242 relational-expression > shift-expression
5243 relational-expression <= shift-expression
5244 relational-expression >= shift-expression
5245
5246 GNU Extension:
5247
5248 relational-expression:
5249 relational-expression <? shift-expression
5250 relational-expression >? shift-expression
5251
5252 equality-expression:
5253 relational-expression
5254 equality-expression == relational-expression
5255 equality-expression != relational-expression
5256
5257 and-expression:
5258 equality-expression
5259 and-expression & equality-expression
5260
5261 exclusive-or-expression:
5262 and-expression
5263 exclusive-or-expression ^ and-expression
5264
5265 inclusive-or-expression:
5266 exclusive-or-expression
5267 inclusive-or-expression | exclusive-or-expression
5268
5269 logical-and-expression:
5270 inclusive-or-expression
5271 logical-and-expression && inclusive-or-expression
5272
5273 logical-or-expression:
5274 logical-and-expression
5275 logical-or-expression || logical-and-expression
5276
5277 All these are implemented with a single function like:
5278
5279 binary-expression:
5280 simple-cast-expression
5281 binary-expression <token> binary-expression
5282
5283 The binops_by_token map is used to get the tree codes for each <token> type.
5284 binary-expressions are associated according to a precedence table. */
5285
5286 #define TOKEN_PRECEDENCE(token) \
5287 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5288 ? PREC_NOT_OPERATOR \
5289 : binops_by_token[token->type].prec)
5290
5291 static tree
5292 cp_parser_binary_expression (cp_parser* parser)
5293 {
5294 cp_parser_expression_stack stack;
5295 cp_parser_expression_stack_entry *sp = &stack[0];
5296 tree lhs, rhs;
5297 cp_token *token;
5298 enum tree_code tree_type;
5299 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5300 bool overloaded_p;
5301
5302 /* Parse the first expression. */
5303 lhs = cp_parser_simple_cast_expression (parser);
5304
5305 for (;;)
5306 {
5307 /* Get an operator token. */
5308 token = cp_lexer_peek_token (parser->lexer);
5309 new_prec = TOKEN_PRECEDENCE (token);
5310
5311 /* Popping an entry off the stack means we completed a subexpression:
5312 - either we found a token which is not an operator (`>' where it is not
5313 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5314 will happen repeatedly;
5315 - or, we found an operator which has lower priority. This is the case
5316 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5317 parsing `3 * 4'. */
5318 if (new_prec <= prec)
5319 {
5320 if (sp == stack)
5321 break;
5322 else
5323 goto pop;
5324 }
5325
5326 get_rhs:
5327 tree_type = binops_by_token[token->type].tree_type;
5328
5329 /* We used the operator token. */
5330 cp_lexer_consume_token (parser->lexer);
5331
5332 /* Extract another operand. It may be the RHS of this expression
5333 or the LHS of a new, higher priority expression. */
5334 rhs = cp_parser_simple_cast_expression (parser);
5335
5336 /* Get another operator token. Look up its precedence to avoid
5337 building a useless (immediately popped) stack entry for common
5338 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5339 token = cp_lexer_peek_token (parser->lexer);
5340 lookahead_prec = TOKEN_PRECEDENCE (token);
5341 if (lookahead_prec > new_prec)
5342 {
5343 /* ... and prepare to parse the RHS of the new, higher priority
5344 expression. Since precedence levels on the stack are
5345 monotonically increasing, we do not have to care about
5346 stack overflows. */
5347 sp->prec = prec;
5348 sp->tree_type = tree_type;
5349 sp->lhs = lhs;
5350 sp++;
5351 lhs = rhs;
5352 prec = new_prec;
5353 new_prec = lookahead_prec;
5354 goto get_rhs;
5355
5356 pop:
5357 /* If the stack is not empty, we have parsed into LHS the right side
5358 (`4' in the example above) of an expression we had suspended.
5359 We can use the information on the stack to recover the LHS (`3')
5360 from the stack together with the tree code (`MULT_EXPR'), and
5361 the precedence of the higher level subexpression
5362 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5363 which will be used to actually build the additive expression. */
5364 --sp;
5365 prec = sp->prec;
5366 tree_type = sp->tree_type;
5367 rhs = lhs;
5368 lhs = sp->lhs;
5369 }
5370
5371 overloaded_p = false;
5372 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5373
5374 /* If the binary operator required the use of an overloaded operator,
5375 then this expression cannot be an integral constant-expression.
5376 An overloaded operator can be used even if both operands are
5377 otherwise permissible in an integral constant-expression if at
5378 least one of the operands is of enumeration type. */
5379
5380 if (overloaded_p
5381 && (cp_parser_non_integral_constant_expression
5382 (parser, "calls to overloaded operators")))
5383 return error_mark_node;
5384 }
5385
5386 return lhs;
5387 }
5388
5389
5390 /* Parse the `? expression : assignment-expression' part of a
5391 conditional-expression. The LOGICAL_OR_EXPR is the
5392 logical-or-expression that started the conditional-expression.
5393 Returns a representation of the entire conditional-expression.
5394
5395 This routine is used by cp_parser_assignment_expression.
5396
5397 ? expression : assignment-expression
5398
5399 GNU Extensions:
5400
5401 ? : assignment-expression */
5402
5403 static tree
5404 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5405 {
5406 tree expr;
5407 tree assignment_expr;
5408
5409 /* Consume the `?' token. */
5410 cp_lexer_consume_token (parser->lexer);
5411 if (cp_parser_allow_gnu_extensions_p (parser)
5412 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5413 /* Implicit true clause. */
5414 expr = NULL_TREE;
5415 else
5416 /* Parse the expression. */
5417 expr = cp_parser_expression (parser);
5418
5419 /* The next token should be a `:'. */
5420 cp_parser_require (parser, CPP_COLON, "`:'");
5421 /* Parse the assignment-expression. */
5422 assignment_expr = cp_parser_assignment_expression (parser);
5423
5424 /* Build the conditional-expression. */
5425 return build_x_conditional_expr (logical_or_expr,
5426 expr,
5427 assignment_expr);
5428 }
5429
5430 /* Parse an assignment-expression.
5431
5432 assignment-expression:
5433 conditional-expression
5434 logical-or-expression assignment-operator assignment_expression
5435 throw-expression
5436
5437 Returns a representation for the expression. */
5438
5439 static tree
5440 cp_parser_assignment_expression (cp_parser* parser)
5441 {
5442 tree expr;
5443
5444 /* If the next token is the `throw' keyword, then we're looking at
5445 a throw-expression. */
5446 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5447 expr = cp_parser_throw_expression (parser);
5448 /* Otherwise, it must be that we are looking at a
5449 logical-or-expression. */
5450 else
5451 {
5452 /* Parse the binary expressions (logical-or-expression). */
5453 expr = cp_parser_binary_expression (parser);
5454 /* If the next token is a `?' then we're actually looking at a
5455 conditional-expression. */
5456 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5457 return cp_parser_question_colon_clause (parser, expr);
5458 else
5459 {
5460 enum tree_code assignment_operator;
5461
5462 /* If it's an assignment-operator, we're using the second
5463 production. */
5464 assignment_operator
5465 = cp_parser_assignment_operator_opt (parser);
5466 if (assignment_operator != ERROR_MARK)
5467 {
5468 tree rhs;
5469
5470 /* Parse the right-hand side of the assignment. */
5471 rhs = cp_parser_assignment_expression (parser);
5472 /* An assignment may not appear in a
5473 constant-expression. */
5474 if (cp_parser_non_integral_constant_expression (parser,
5475 "an assignment"))
5476 return error_mark_node;
5477 /* Build the assignment expression. */
5478 expr = build_x_modify_expr (expr,
5479 assignment_operator,
5480 rhs);
5481 }
5482 }
5483 }
5484
5485 return expr;
5486 }
5487
5488 /* Parse an (optional) assignment-operator.
5489
5490 assignment-operator: one of
5491 = *= /= %= += -= >>= <<= &= ^= |=
5492
5493 GNU Extension:
5494
5495 assignment-operator: one of
5496 <?= >?=
5497
5498 If the next token is an assignment operator, the corresponding tree
5499 code is returned, and the token is consumed. For example, for
5500 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5501 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5502 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5503 operator, ERROR_MARK is returned. */
5504
5505 static enum tree_code
5506 cp_parser_assignment_operator_opt (cp_parser* parser)
5507 {
5508 enum tree_code op;
5509 cp_token *token;
5510
5511 /* Peek at the next toen. */
5512 token = cp_lexer_peek_token (parser->lexer);
5513
5514 switch (token->type)
5515 {
5516 case CPP_EQ:
5517 op = NOP_EXPR;
5518 break;
5519
5520 case CPP_MULT_EQ:
5521 op = MULT_EXPR;
5522 break;
5523
5524 case CPP_DIV_EQ:
5525 op = TRUNC_DIV_EXPR;
5526 break;
5527
5528 case CPP_MOD_EQ:
5529 op = TRUNC_MOD_EXPR;
5530 break;
5531
5532 case CPP_PLUS_EQ:
5533 op = PLUS_EXPR;
5534 break;
5535
5536 case CPP_MINUS_EQ:
5537 op = MINUS_EXPR;
5538 break;
5539
5540 case CPP_RSHIFT_EQ:
5541 op = RSHIFT_EXPR;
5542 break;
5543
5544 case CPP_LSHIFT_EQ:
5545 op = LSHIFT_EXPR;
5546 break;
5547
5548 case CPP_AND_EQ:
5549 op = BIT_AND_EXPR;
5550 break;
5551
5552 case CPP_XOR_EQ:
5553 op = BIT_XOR_EXPR;
5554 break;
5555
5556 case CPP_OR_EQ:
5557 op = BIT_IOR_EXPR;
5558 break;
5559
5560 case CPP_MIN_EQ:
5561 op = MIN_EXPR;
5562 break;
5563
5564 case CPP_MAX_EQ:
5565 op = MAX_EXPR;
5566 break;
5567
5568 default:
5569 /* Nothing else is an assignment operator. */
5570 op = ERROR_MARK;
5571 }
5572
5573 /* If it was an assignment operator, consume it. */
5574 if (op != ERROR_MARK)
5575 cp_lexer_consume_token (parser->lexer);
5576
5577 return op;
5578 }
5579
5580 /* Parse an expression.
5581
5582 expression:
5583 assignment-expression
5584 expression , assignment-expression
5585
5586 Returns a representation of the expression. */
5587
5588 static tree
5589 cp_parser_expression (cp_parser* parser)
5590 {
5591 tree expression = NULL_TREE;
5592
5593 while (true)
5594 {
5595 tree assignment_expression;
5596
5597 /* Parse the next assignment-expression. */
5598 assignment_expression
5599 = cp_parser_assignment_expression (parser);
5600 /* If this is the first assignment-expression, we can just
5601 save it away. */
5602 if (!expression)
5603 expression = assignment_expression;
5604 else
5605 expression = build_x_compound_expr (expression,
5606 assignment_expression);
5607 /* If the next token is not a comma, then we are done with the
5608 expression. */
5609 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5610 break;
5611 /* Consume the `,'. */
5612 cp_lexer_consume_token (parser->lexer);
5613 /* A comma operator cannot appear in a constant-expression. */
5614 if (cp_parser_non_integral_constant_expression (parser,
5615 "a comma operator"))
5616 expression = error_mark_node;
5617 }
5618
5619 return expression;
5620 }
5621
5622 /* Parse a constant-expression.
5623
5624 constant-expression:
5625 conditional-expression
5626
5627 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5628 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5629 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5630 is false, NON_CONSTANT_P should be NULL. */
5631
5632 static tree
5633 cp_parser_constant_expression (cp_parser* parser,
5634 bool allow_non_constant_p,
5635 bool *non_constant_p)
5636 {
5637 bool saved_integral_constant_expression_p;
5638 bool saved_allow_non_integral_constant_expression_p;
5639 bool saved_non_integral_constant_expression_p;
5640 tree expression;
5641
5642 /* It might seem that we could simply parse the
5643 conditional-expression, and then check to see if it were
5644 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5645 one that the compiler can figure out is constant, possibly after
5646 doing some simplifications or optimizations. The standard has a
5647 precise definition of constant-expression, and we must honor
5648 that, even though it is somewhat more restrictive.
5649
5650 For example:
5651
5652 int i[(2, 3)];
5653
5654 is not a legal declaration, because `(2, 3)' is not a
5655 constant-expression. The `,' operator is forbidden in a
5656 constant-expression. However, GCC's constant-folding machinery
5657 will fold this operation to an INTEGER_CST for `3'. */
5658
5659 /* Save the old settings. */
5660 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5661 saved_allow_non_integral_constant_expression_p
5662 = parser->allow_non_integral_constant_expression_p;
5663 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5664 /* We are now parsing a constant-expression. */
5665 parser->integral_constant_expression_p = true;
5666 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5667 parser->non_integral_constant_expression_p = false;
5668 /* Although the grammar says "conditional-expression", we parse an
5669 "assignment-expression", which also permits "throw-expression"
5670 and the use of assignment operators. In the case that
5671 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5672 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5673 actually essential that we look for an assignment-expression.
5674 For example, cp_parser_initializer_clauses uses this function to
5675 determine whether a particular assignment-expression is in fact
5676 constant. */
5677 expression = cp_parser_assignment_expression (parser);
5678 /* Restore the old settings. */
5679 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5680 parser->allow_non_integral_constant_expression_p
5681 = saved_allow_non_integral_constant_expression_p;
5682 if (allow_non_constant_p)
5683 *non_constant_p = parser->non_integral_constant_expression_p;
5684 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5685
5686 return expression;
5687 }
5688
5689 /* Parse __builtin_offsetof.
5690
5691 offsetof-expression:
5692 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5693
5694 offsetof-member-designator:
5695 id-expression
5696 | offsetof-member-designator "." id-expression
5697 | offsetof-member-designator "[" expression "]"
5698 */
5699
5700 static tree
5701 cp_parser_builtin_offsetof (cp_parser *parser)
5702 {
5703 int save_ice_p, save_non_ice_p;
5704 tree type, expr;
5705 cp_id_kind dummy;
5706
5707 /* We're about to accept non-integral-constant things, but will
5708 definitely yield an integral constant expression. Save and
5709 restore these values around our local parsing. */
5710 save_ice_p = parser->integral_constant_expression_p;
5711 save_non_ice_p = parser->non_integral_constant_expression_p;
5712
5713 /* Consume the "__builtin_offsetof" token. */
5714 cp_lexer_consume_token (parser->lexer);
5715 /* Consume the opening `('. */
5716 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5717 /* Parse the type-id. */
5718 type = cp_parser_type_id (parser);
5719 /* Look for the `,'. */
5720 cp_parser_require (parser, CPP_COMMA, "`,'");
5721
5722 /* Build the (type *)null that begins the traditional offsetof macro. */
5723 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5724
5725 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5726 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5727 true, &dummy);
5728 while (true)
5729 {
5730 cp_token *token = cp_lexer_peek_token (parser->lexer);
5731 switch (token->type)
5732 {
5733 case CPP_OPEN_SQUARE:
5734 /* offsetof-member-designator "[" expression "]" */
5735 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5736 break;
5737
5738 case CPP_DOT:
5739 /* offsetof-member-designator "." identifier */
5740 cp_lexer_consume_token (parser->lexer);
5741 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5742 true, &dummy);
5743 break;
5744
5745 case CPP_CLOSE_PAREN:
5746 /* Consume the ")" token. */
5747 cp_lexer_consume_token (parser->lexer);
5748 goto success;
5749
5750 default:
5751 /* Error. We know the following require will fail, but
5752 that gives the proper error message. */
5753 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5754 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5755 expr = error_mark_node;
5756 goto failure;
5757 }
5758 }
5759
5760 success:
5761 /* If we're processing a template, we can't finish the semantics yet.
5762 Otherwise we can fold the entire expression now. */
5763 if (processing_template_decl)
5764 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5765 else
5766 expr = fold_offsetof (expr);
5767
5768 failure:
5769 parser->integral_constant_expression_p = save_ice_p;
5770 parser->non_integral_constant_expression_p = save_non_ice_p;
5771
5772 return expr;
5773 }
5774
5775 /* Statements [gram.stmt.stmt] */
5776
5777 /* Parse a statement.
5778
5779 statement:
5780 labeled-statement
5781 expression-statement
5782 compound-statement
5783 selection-statement
5784 iteration-statement
5785 jump-statement
5786 declaration-statement
5787 try-block */
5788
5789 static void
5790 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5791 {
5792 tree statement;
5793 cp_token *token;
5794 location_t statement_location;
5795
5796 /* There is no statement yet. */
5797 statement = NULL_TREE;
5798 /* Peek at the next token. */
5799 token = cp_lexer_peek_token (parser->lexer);
5800 /* Remember the location of the first token in the statement. */
5801 statement_location = token->location;
5802 /* If this is a keyword, then that will often determine what kind of
5803 statement we have. */
5804 if (token->type == CPP_KEYWORD)
5805 {
5806 enum rid keyword = token->keyword;
5807
5808 switch (keyword)
5809 {
5810 case RID_CASE:
5811 case RID_DEFAULT:
5812 statement = cp_parser_labeled_statement (parser,
5813 in_statement_expr);
5814 break;
5815
5816 case RID_IF:
5817 case RID_SWITCH:
5818 statement = cp_parser_selection_statement (parser);
5819 break;
5820
5821 case RID_WHILE:
5822 case RID_DO:
5823 case RID_FOR:
5824 statement = cp_parser_iteration_statement (parser);
5825 break;
5826
5827 case RID_BREAK:
5828 case RID_CONTINUE:
5829 case RID_RETURN:
5830 case RID_GOTO:
5831 statement = cp_parser_jump_statement (parser);
5832 break;
5833
5834 case RID_TRY:
5835 statement = cp_parser_try_block (parser);
5836 break;
5837
5838 default:
5839 /* It might be a keyword like `int' that can start a
5840 declaration-statement. */
5841 break;
5842 }
5843 }
5844 else if (token->type == CPP_NAME)
5845 {
5846 /* If the next token is a `:', then we are looking at a
5847 labeled-statement. */
5848 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5849 if (token->type == CPP_COLON)
5850 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5851 }
5852 /* Anything that starts with a `{' must be a compound-statement. */
5853 else if (token->type == CPP_OPEN_BRACE)
5854 statement = cp_parser_compound_statement (parser, NULL, false);
5855 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5856 a statement all its own. */
5857 else if (token->type == CPP_PRAGMA)
5858 {
5859 cp_lexer_handle_pragma (parser->lexer);
5860 return;
5861 }
5862
5863 /* Everything else must be a declaration-statement or an
5864 expression-statement. Try for the declaration-statement
5865 first, unless we are looking at a `;', in which case we know that
5866 we have an expression-statement. */
5867 if (!statement)
5868 {
5869 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5870 {
5871 cp_parser_parse_tentatively (parser);
5872 /* Try to parse the declaration-statement. */
5873 cp_parser_declaration_statement (parser);
5874 /* If that worked, we're done. */
5875 if (cp_parser_parse_definitely (parser))
5876 return;
5877 }
5878 /* Look for an expression-statement instead. */
5879 statement = cp_parser_expression_statement (parser, in_statement_expr);
5880 }
5881
5882 /* Set the line number for the statement. */
5883 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5884 SET_EXPR_LOCATION (statement, statement_location);
5885 }
5886
5887 /* Parse a labeled-statement.
5888
5889 labeled-statement:
5890 identifier : statement
5891 case constant-expression : statement
5892 default : statement
5893
5894 GNU Extension:
5895
5896 labeled-statement:
5897 case constant-expression ... constant-expression : statement
5898
5899 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5900 For an ordinary label, returns a LABEL_EXPR. */
5901
5902 static tree
5903 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5904 {
5905 cp_token *token;
5906 tree statement = error_mark_node;
5907
5908 /* The next token should be an identifier. */
5909 token = cp_lexer_peek_token (parser->lexer);
5910 if (token->type != CPP_NAME
5911 && token->type != CPP_KEYWORD)
5912 {
5913 cp_parser_error (parser, "expected labeled-statement");
5914 return error_mark_node;
5915 }
5916
5917 switch (token->keyword)
5918 {
5919 case RID_CASE:
5920 {
5921 tree expr, expr_hi;
5922 cp_token *ellipsis;
5923
5924 /* Consume the `case' token. */
5925 cp_lexer_consume_token (parser->lexer);
5926 /* Parse the constant-expression. */
5927 expr = cp_parser_constant_expression (parser,
5928 /*allow_non_constant_p=*/false,
5929 NULL);
5930
5931 ellipsis = cp_lexer_peek_token (parser->lexer);
5932 if (ellipsis->type == CPP_ELLIPSIS)
5933 {
5934 /* Consume the `...' token. */
5935 cp_lexer_consume_token (parser->lexer);
5936 expr_hi =
5937 cp_parser_constant_expression (parser,
5938 /*allow_non_constant_p=*/false,
5939 NULL);
5940 /* We don't need to emit warnings here, as the common code
5941 will do this for us. */
5942 }
5943 else
5944 expr_hi = NULL_TREE;
5945
5946 if (!parser->in_switch_statement_p)
5947 error ("case label %qE not within a switch statement", expr);
5948 else
5949 statement = finish_case_label (expr, expr_hi);
5950 }
5951 break;
5952
5953 case RID_DEFAULT:
5954 /* Consume the `default' token. */
5955 cp_lexer_consume_token (parser->lexer);
5956 if (!parser->in_switch_statement_p)
5957 error ("case label not within a switch statement");
5958 else
5959 statement = finish_case_label (NULL_TREE, NULL_TREE);
5960 break;
5961
5962 default:
5963 /* Anything else must be an ordinary label. */
5964 statement = finish_label_stmt (cp_parser_identifier (parser));
5965 break;
5966 }
5967
5968 /* Require the `:' token. */
5969 cp_parser_require (parser, CPP_COLON, "`:'");
5970 /* Parse the labeled statement. */
5971 cp_parser_statement (parser, in_statement_expr);
5972
5973 /* Return the label, in the case of a `case' or `default' label. */
5974 return statement;
5975 }
5976
5977 /* Parse an expression-statement.
5978
5979 expression-statement:
5980 expression [opt] ;
5981
5982 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5983 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5984 indicates whether this expression-statement is part of an
5985 expression statement. */
5986
5987 static tree
5988 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
5989 {
5990 tree statement = NULL_TREE;
5991
5992 /* If the next token is a ';', then there is no expression
5993 statement. */
5994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5995 statement = cp_parser_expression (parser);
5996
5997 /* Consume the final `;'. */
5998 cp_parser_consume_semicolon_at_end_of_statement (parser);
5999
6000 if (in_statement_expr
6001 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6002 {
6003 /* This is the final expression statement of a statement
6004 expression. */
6005 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6006 }
6007 else if (statement)
6008 statement = finish_expr_stmt (statement);
6009 else
6010 finish_stmt ();
6011
6012 return statement;
6013 }
6014
6015 /* Parse a compound-statement.
6016
6017 compound-statement:
6018 { statement-seq [opt] }
6019
6020 Returns a tree representing the statement. */
6021
6022 static tree
6023 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6024 bool in_try)
6025 {
6026 tree compound_stmt;
6027
6028 /* Consume the `{'. */
6029 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6030 return error_mark_node;
6031 /* Begin the compound-statement. */
6032 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6033 /* Parse an (optional) statement-seq. */
6034 cp_parser_statement_seq_opt (parser, in_statement_expr);
6035 /* Finish the compound-statement. */
6036 finish_compound_stmt (compound_stmt);
6037 /* Consume the `}'. */
6038 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6039
6040 return compound_stmt;
6041 }
6042
6043 /* Parse an (optional) statement-seq.
6044
6045 statement-seq:
6046 statement
6047 statement-seq [opt] statement */
6048
6049 static void
6050 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6051 {
6052 /* Scan statements until there aren't any more. */
6053 while (true)
6054 {
6055 /* If we're looking at a `}', then we've run out of statements. */
6056 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6057 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6058 break;
6059
6060 /* Parse the statement. */
6061 cp_parser_statement (parser, in_statement_expr);
6062 }
6063 }
6064
6065 /* Parse a selection-statement.
6066
6067 selection-statement:
6068 if ( condition ) statement
6069 if ( condition ) statement else statement
6070 switch ( condition ) statement
6071
6072 Returns the new IF_STMT or SWITCH_STMT. */
6073
6074 static tree
6075 cp_parser_selection_statement (cp_parser* parser)
6076 {
6077 cp_token *token;
6078 enum rid keyword;
6079
6080 /* Peek at the next token. */
6081 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6082
6083 /* See what kind of keyword it is. */
6084 keyword = token->keyword;
6085 switch (keyword)
6086 {
6087 case RID_IF:
6088 case RID_SWITCH:
6089 {
6090 tree statement;
6091 tree condition;
6092
6093 /* Look for the `('. */
6094 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6095 {
6096 cp_parser_skip_to_end_of_statement (parser);
6097 return error_mark_node;
6098 }
6099
6100 /* Begin the selection-statement. */
6101 if (keyword == RID_IF)
6102 statement = begin_if_stmt ();
6103 else
6104 statement = begin_switch_stmt ();
6105
6106 /* Parse the condition. */
6107 condition = cp_parser_condition (parser);
6108 /* Look for the `)'. */
6109 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6110 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6111 /*consume_paren=*/true);
6112
6113 if (keyword == RID_IF)
6114 {
6115 /* Add the condition. */
6116 finish_if_stmt_cond (condition, statement);
6117
6118 /* Parse the then-clause. */
6119 cp_parser_implicitly_scoped_statement (parser);
6120 finish_then_clause (statement);
6121
6122 /* If the next token is `else', parse the else-clause. */
6123 if (cp_lexer_next_token_is_keyword (parser->lexer,
6124 RID_ELSE))
6125 {
6126 /* Consume the `else' keyword. */
6127 cp_lexer_consume_token (parser->lexer);
6128 begin_else_clause (statement);
6129 /* Parse the else-clause. */
6130 cp_parser_implicitly_scoped_statement (parser);
6131 finish_else_clause (statement);
6132 }
6133
6134 /* Now we're all done with the if-statement. */
6135 finish_if_stmt (statement);
6136 }
6137 else
6138 {
6139 bool in_switch_statement_p;
6140
6141 /* Add the condition. */
6142 finish_switch_cond (condition, statement);
6143
6144 /* Parse the body of the switch-statement. */
6145 in_switch_statement_p = parser->in_switch_statement_p;
6146 parser->in_switch_statement_p = true;
6147 cp_parser_implicitly_scoped_statement (parser);
6148 parser->in_switch_statement_p = in_switch_statement_p;
6149
6150 /* Now we're all done with the switch-statement. */
6151 finish_switch_stmt (statement);
6152 }
6153
6154 return statement;
6155 }
6156 break;
6157
6158 default:
6159 cp_parser_error (parser, "expected selection-statement");
6160 return error_mark_node;
6161 }
6162 }
6163
6164 /* Parse a condition.
6165
6166 condition:
6167 expression
6168 type-specifier-seq declarator = assignment-expression
6169
6170 GNU Extension:
6171
6172 condition:
6173 type-specifier-seq declarator asm-specification [opt]
6174 attributes [opt] = assignment-expression
6175
6176 Returns the expression that should be tested. */
6177
6178 static tree
6179 cp_parser_condition (cp_parser* parser)
6180 {
6181 cp_decl_specifier_seq type_specifiers;
6182 const char *saved_message;
6183
6184 /* Try the declaration first. */
6185 cp_parser_parse_tentatively (parser);
6186 /* New types are not allowed in the type-specifier-seq for a
6187 condition. */
6188 saved_message = parser->type_definition_forbidden_message;
6189 parser->type_definition_forbidden_message
6190 = "types may not be defined in conditions";
6191 /* Parse the type-specifier-seq. */
6192 cp_parser_type_specifier_seq (parser, &type_specifiers);
6193 /* Restore the saved message. */
6194 parser->type_definition_forbidden_message = saved_message;
6195 /* If all is well, we might be looking at a declaration. */
6196 if (!cp_parser_error_occurred (parser))
6197 {
6198 tree decl;
6199 tree asm_specification;
6200 tree attributes;
6201 cp_declarator *declarator;
6202 tree initializer = NULL_TREE;
6203
6204 /* Parse the declarator. */
6205 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6206 /*ctor_dtor_or_conv_p=*/NULL,
6207 /*parenthesized_p=*/NULL,
6208 /*member_p=*/false);
6209 /* Parse the attributes. */
6210 attributes = cp_parser_attributes_opt (parser);
6211 /* Parse the asm-specification. */
6212 asm_specification = cp_parser_asm_specification_opt (parser);
6213 /* If the next token is not an `=', then we might still be
6214 looking at an expression. For example:
6215
6216 if (A(a).x)
6217
6218 looks like a decl-specifier-seq and a declarator -- but then
6219 there is no `=', so this is an expression. */
6220 cp_parser_require (parser, CPP_EQ, "`='");
6221 /* If we did see an `=', then we are looking at a declaration
6222 for sure. */
6223 if (cp_parser_parse_definitely (parser))
6224 {
6225 bool pop_p;
6226
6227 /* Create the declaration. */
6228 decl = start_decl (declarator, &type_specifiers,
6229 /*initialized_p=*/true,
6230 attributes, /*prefix_attributes=*/NULL_TREE,
6231 &pop_p);
6232 /* Parse the assignment-expression. */
6233 initializer = cp_parser_assignment_expression (parser);
6234
6235 /* Process the initializer. */
6236 cp_finish_decl (decl,
6237 initializer,
6238 asm_specification,
6239 LOOKUP_ONLYCONVERTING);
6240
6241 if (pop_p)
6242 pop_scope (DECL_CONTEXT (decl));
6243
6244 return convert_from_reference (decl);
6245 }
6246 }
6247 /* If we didn't even get past the declarator successfully, we are
6248 definitely not looking at a declaration. */
6249 else
6250 cp_parser_abort_tentative_parse (parser);
6251
6252 /* Otherwise, we are looking at an expression. */
6253 return cp_parser_expression (parser);
6254 }
6255
6256 /* Parse an iteration-statement.
6257
6258 iteration-statement:
6259 while ( condition ) statement
6260 do statement while ( expression ) ;
6261 for ( for-init-statement condition [opt] ; expression [opt] )
6262 statement
6263
6264 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6265
6266 static tree
6267 cp_parser_iteration_statement (cp_parser* parser)
6268 {
6269 cp_token *token;
6270 enum rid keyword;
6271 tree statement;
6272 bool in_iteration_statement_p;
6273
6274
6275 /* Peek at the next token. */
6276 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6277 if (!token)
6278 return error_mark_node;
6279
6280 /* Remember whether or not we are already within an iteration
6281 statement. */
6282 in_iteration_statement_p = parser->in_iteration_statement_p;
6283
6284 /* See what kind of keyword it is. */
6285 keyword = token->keyword;
6286 switch (keyword)
6287 {
6288 case RID_WHILE:
6289 {
6290 tree condition;
6291
6292 /* Begin the while-statement. */
6293 statement = begin_while_stmt ();
6294 /* Look for the `('. */
6295 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6296 /* Parse the condition. */
6297 condition = cp_parser_condition (parser);
6298 finish_while_stmt_cond (condition, statement);
6299 /* Look for the `)'. */
6300 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6301 /* Parse the dependent statement. */
6302 parser->in_iteration_statement_p = true;
6303 cp_parser_already_scoped_statement (parser);
6304 parser->in_iteration_statement_p = in_iteration_statement_p;
6305 /* We're done with the while-statement. */
6306 finish_while_stmt (statement);
6307 }
6308 break;
6309
6310 case RID_DO:
6311 {
6312 tree expression;
6313
6314 /* Begin the do-statement. */
6315 statement = begin_do_stmt ();
6316 /* Parse the body of the do-statement. */
6317 parser->in_iteration_statement_p = true;
6318 cp_parser_implicitly_scoped_statement (parser);
6319 parser->in_iteration_statement_p = in_iteration_statement_p;
6320 finish_do_body (statement);
6321 /* Look for the `while' keyword. */
6322 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6323 /* Look for the `('. */
6324 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6325 /* Parse the expression. */
6326 expression = cp_parser_expression (parser);
6327 /* We're done with the do-statement. */
6328 finish_do_stmt (expression, statement);
6329 /* Look for the `)'. */
6330 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6331 /* Look for the `;'. */
6332 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6333 }
6334 break;
6335
6336 case RID_FOR:
6337 {
6338 tree condition = NULL_TREE;
6339 tree expression = NULL_TREE;
6340
6341 /* Begin the for-statement. */
6342 statement = begin_for_stmt ();
6343 /* Look for the `('. */
6344 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6345 /* Parse the initialization. */
6346 cp_parser_for_init_statement (parser);
6347 finish_for_init_stmt (statement);
6348
6349 /* If there's a condition, process it. */
6350 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6351 condition = cp_parser_condition (parser);
6352 finish_for_cond (condition, statement);
6353 /* Look for the `;'. */
6354 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6355
6356 /* If there's an expression, process it. */
6357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6358 expression = cp_parser_expression (parser);
6359 finish_for_expr (expression, statement);
6360 /* Look for the `)'. */
6361 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6362
6363 /* Parse the body of the for-statement. */
6364 parser->in_iteration_statement_p = true;
6365 cp_parser_already_scoped_statement (parser);
6366 parser->in_iteration_statement_p = in_iteration_statement_p;
6367
6368 /* We're done with the for-statement. */
6369 finish_for_stmt (statement);
6370 }
6371 break;
6372
6373 default:
6374 cp_parser_error (parser, "expected iteration-statement");
6375 statement = error_mark_node;
6376 break;
6377 }
6378
6379 return statement;
6380 }
6381
6382 /* Parse a for-init-statement.
6383
6384 for-init-statement:
6385 expression-statement
6386 simple-declaration */
6387
6388 static void
6389 cp_parser_for_init_statement (cp_parser* parser)
6390 {
6391 /* If the next token is a `;', then we have an empty
6392 expression-statement. Grammatically, this is also a
6393 simple-declaration, but an invalid one, because it does not
6394 declare anything. Therefore, if we did not handle this case
6395 specially, we would issue an error message about an invalid
6396 declaration. */
6397 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6398 {
6399 /* We're going to speculatively look for a declaration, falling back
6400 to an expression, if necessary. */
6401 cp_parser_parse_tentatively (parser);
6402 /* Parse the declaration. */
6403 cp_parser_simple_declaration (parser,
6404 /*function_definition_allowed_p=*/false);
6405 /* If the tentative parse failed, then we shall need to look for an
6406 expression-statement. */
6407 if (cp_parser_parse_definitely (parser))
6408 return;
6409 }
6410
6411 cp_parser_expression_statement (parser, false);
6412 }
6413
6414 /* Parse a jump-statement.
6415
6416 jump-statement:
6417 break ;
6418 continue ;
6419 return expression [opt] ;
6420 goto identifier ;
6421
6422 GNU extension:
6423
6424 jump-statement:
6425 goto * expression ;
6426
6427 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6428
6429 static tree
6430 cp_parser_jump_statement (cp_parser* parser)
6431 {
6432 tree statement = error_mark_node;
6433 cp_token *token;
6434 enum rid keyword;
6435
6436 /* Peek at the next token. */
6437 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6438 if (!token)
6439 return error_mark_node;
6440
6441 /* See what kind of keyword it is. */
6442 keyword = token->keyword;
6443 switch (keyword)
6444 {
6445 case RID_BREAK:
6446 if (!parser->in_switch_statement_p
6447 && !parser->in_iteration_statement_p)
6448 {
6449 error ("break statement not within loop or switch");
6450 statement = error_mark_node;
6451 }
6452 else
6453 statement = finish_break_stmt ();
6454 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6455 break;
6456
6457 case RID_CONTINUE:
6458 if (!parser->in_iteration_statement_p)
6459 {
6460 error ("continue statement not within a loop");
6461 statement = error_mark_node;
6462 }
6463 else
6464 statement = finish_continue_stmt ();
6465 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6466 break;
6467
6468 case RID_RETURN:
6469 {
6470 tree expr;
6471
6472 /* If the next token is a `;', then there is no
6473 expression. */
6474 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6475 expr = cp_parser_expression (parser);
6476 else
6477 expr = NULL_TREE;
6478 /* Build the return-statement. */
6479 statement = finish_return_stmt (expr);
6480 /* Look for the final `;'. */
6481 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6482 }
6483 break;
6484
6485 case RID_GOTO:
6486 /* Create the goto-statement. */
6487 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6488 {
6489 /* Issue a warning about this use of a GNU extension. */
6490 if (pedantic)
6491 pedwarn ("ISO C++ forbids computed gotos");
6492 /* Consume the '*' token. */
6493 cp_lexer_consume_token (parser->lexer);
6494 /* Parse the dependent expression. */
6495 finish_goto_stmt (cp_parser_expression (parser));
6496 }
6497 else
6498 finish_goto_stmt (cp_parser_identifier (parser));
6499 /* Look for the final `;'. */
6500 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6501 break;
6502
6503 default:
6504 cp_parser_error (parser, "expected jump-statement");
6505 break;
6506 }
6507
6508 return statement;
6509 }
6510
6511 /* Parse a declaration-statement.
6512
6513 declaration-statement:
6514 block-declaration */
6515
6516 static void
6517 cp_parser_declaration_statement (cp_parser* parser)
6518 {
6519 void *p;
6520
6521 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6522 p = obstack_alloc (&declarator_obstack, 0);
6523
6524 /* Parse the block-declaration. */
6525 cp_parser_block_declaration (parser, /*statement_p=*/true);
6526
6527 /* Free any declarators allocated. */
6528 obstack_free (&declarator_obstack, p);
6529
6530 /* Finish off the statement. */
6531 finish_stmt ();
6532 }
6533
6534 /* Some dependent statements (like `if (cond) statement'), are
6535 implicitly in their own scope. In other words, if the statement is
6536 a single statement (as opposed to a compound-statement), it is
6537 none-the-less treated as if it were enclosed in braces. Any
6538 declarations appearing in the dependent statement are out of scope
6539 after control passes that point. This function parses a statement,
6540 but ensures that is in its own scope, even if it is not a
6541 compound-statement.
6542
6543 Returns the new statement. */
6544
6545 static tree
6546 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6547 {
6548 tree statement;
6549
6550 /* If the token is not a `{', then we must take special action. */
6551 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6552 {
6553 /* Create a compound-statement. */
6554 statement = begin_compound_stmt (0);
6555 /* Parse the dependent-statement. */
6556 cp_parser_statement (parser, false);
6557 /* Finish the dummy compound-statement. */
6558 finish_compound_stmt (statement);
6559 }
6560 /* Otherwise, we simply parse the statement directly. */
6561 else
6562 statement = cp_parser_compound_statement (parser, NULL, false);
6563
6564 /* Return the statement. */
6565 return statement;
6566 }
6567
6568 /* For some dependent statements (like `while (cond) statement'), we
6569 have already created a scope. Therefore, even if the dependent
6570 statement is a compound-statement, we do not want to create another
6571 scope. */
6572
6573 static void
6574 cp_parser_already_scoped_statement (cp_parser* parser)
6575 {
6576 /* If the token is a `{', then we must take special action. */
6577 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6578 cp_parser_statement (parser, false);
6579 else
6580 {
6581 /* Avoid calling cp_parser_compound_statement, so that we
6582 don't create a new scope. Do everything else by hand. */
6583 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6584 cp_parser_statement_seq_opt (parser, false);
6585 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6586 }
6587 }
6588
6589 /* Declarations [gram.dcl.dcl] */
6590
6591 /* Parse an optional declaration-sequence.
6592
6593 declaration-seq:
6594 declaration
6595 declaration-seq declaration */
6596
6597 static void
6598 cp_parser_declaration_seq_opt (cp_parser* parser)
6599 {
6600 while (true)
6601 {
6602 cp_token *token;
6603
6604 token = cp_lexer_peek_token (parser->lexer);
6605
6606 if (token->type == CPP_CLOSE_BRACE
6607 || token->type == CPP_EOF)
6608 break;
6609
6610 if (token->type == CPP_SEMICOLON)
6611 {
6612 /* A declaration consisting of a single semicolon is
6613 invalid. Allow it unless we're being pedantic. */
6614 cp_lexer_consume_token (parser->lexer);
6615 if (pedantic && !in_system_header)
6616 pedwarn ("extra %<;%>");
6617 continue;
6618 }
6619
6620 /* If we're entering or exiting a region that's implicitly
6621 extern "C", modify the lang context appropriately. */
6622 if (!parser->implicit_extern_c && token->implicit_extern_c)
6623 {
6624 push_lang_context (lang_name_c);
6625 parser->implicit_extern_c = true;
6626 }
6627 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6628 {
6629 pop_lang_context ();
6630 parser->implicit_extern_c = false;
6631 }
6632
6633 if (token->type == CPP_PRAGMA)
6634 {
6635 /* A top-level declaration can consist solely of a #pragma.
6636 A nested declaration cannot, so this is done here and not
6637 in cp_parser_declaration. (A #pragma at block scope is
6638 handled in cp_parser_statement.) */
6639 cp_lexer_handle_pragma (parser->lexer);
6640 continue;
6641 }
6642
6643 /* Parse the declaration itself. */
6644 cp_parser_declaration (parser);
6645 }
6646 }
6647
6648 /* Parse a declaration.
6649
6650 declaration:
6651 block-declaration
6652 function-definition
6653 template-declaration
6654 explicit-instantiation
6655 explicit-specialization
6656 linkage-specification
6657 namespace-definition
6658
6659 GNU extension:
6660
6661 declaration:
6662 __extension__ declaration */
6663
6664 static void
6665 cp_parser_declaration (cp_parser* parser)
6666 {
6667 cp_token token1;
6668 cp_token token2;
6669 int saved_pedantic;
6670 void *p;
6671
6672 /* Check for the `__extension__' keyword. */
6673 if (cp_parser_extension_opt (parser, &saved_pedantic))
6674 {
6675 /* Parse the qualified declaration. */
6676 cp_parser_declaration (parser);
6677 /* Restore the PEDANTIC flag. */
6678 pedantic = saved_pedantic;
6679
6680 return;
6681 }
6682
6683 /* Try to figure out what kind of declaration is present. */
6684 token1 = *cp_lexer_peek_token (parser->lexer);
6685
6686 if (token1.type != CPP_EOF)
6687 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6688
6689 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6690 p = obstack_alloc (&declarator_obstack, 0);
6691
6692 /* If the next token is `extern' and the following token is a string
6693 literal, then we have a linkage specification. */
6694 if (token1.keyword == RID_EXTERN
6695 && cp_parser_is_string_literal (&token2))
6696 cp_parser_linkage_specification (parser);
6697 /* If the next token is `template', then we have either a template
6698 declaration, an explicit instantiation, or an explicit
6699 specialization. */
6700 else if (token1.keyword == RID_TEMPLATE)
6701 {
6702 /* `template <>' indicates a template specialization. */
6703 if (token2.type == CPP_LESS
6704 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6705 cp_parser_explicit_specialization (parser);
6706 /* `template <' indicates a template declaration. */
6707 else if (token2.type == CPP_LESS)
6708 cp_parser_template_declaration (parser, /*member_p=*/false);
6709 /* Anything else must be an explicit instantiation. */
6710 else
6711 cp_parser_explicit_instantiation (parser);
6712 }
6713 /* If the next token is `export', then we have a template
6714 declaration. */
6715 else if (token1.keyword == RID_EXPORT)
6716 cp_parser_template_declaration (parser, /*member_p=*/false);
6717 /* If the next token is `extern', 'static' or 'inline' and the one
6718 after that is `template', we have a GNU extended explicit
6719 instantiation directive. */
6720 else if (cp_parser_allow_gnu_extensions_p (parser)
6721 && (token1.keyword == RID_EXTERN
6722 || token1.keyword == RID_STATIC
6723 || token1.keyword == RID_INLINE)
6724 && token2.keyword == RID_TEMPLATE)
6725 cp_parser_explicit_instantiation (parser);
6726 /* If the next token is `namespace', check for a named or unnamed
6727 namespace definition. */
6728 else if (token1.keyword == RID_NAMESPACE
6729 && (/* A named namespace definition. */
6730 (token2.type == CPP_NAME
6731 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6732 == CPP_OPEN_BRACE))
6733 /* An unnamed namespace definition. */
6734 || token2.type == CPP_OPEN_BRACE))
6735 cp_parser_namespace_definition (parser);
6736 /* We must have either a block declaration or a function
6737 definition. */
6738 else
6739 /* Try to parse a block-declaration, or a function-definition. */
6740 cp_parser_block_declaration (parser, /*statement_p=*/false);
6741
6742 /* Free any declarators allocated. */
6743 obstack_free (&declarator_obstack, p);
6744 }
6745
6746 /* Parse a block-declaration.
6747
6748 block-declaration:
6749 simple-declaration
6750 asm-definition
6751 namespace-alias-definition
6752 using-declaration
6753 using-directive
6754
6755 GNU Extension:
6756
6757 block-declaration:
6758 __extension__ block-declaration
6759 label-declaration
6760
6761 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6762 part of a declaration-statement. */
6763
6764 static void
6765 cp_parser_block_declaration (cp_parser *parser,
6766 bool statement_p)
6767 {
6768 cp_token *token1;
6769 int saved_pedantic;
6770
6771 /* Check for the `__extension__' keyword. */
6772 if (cp_parser_extension_opt (parser, &saved_pedantic))
6773 {
6774 /* Parse the qualified declaration. */
6775 cp_parser_block_declaration (parser, statement_p);
6776 /* Restore the PEDANTIC flag. */
6777 pedantic = saved_pedantic;
6778
6779 return;
6780 }
6781
6782 /* Peek at the next token to figure out which kind of declaration is
6783 present. */
6784 token1 = cp_lexer_peek_token (parser->lexer);
6785
6786 /* If the next keyword is `asm', we have an asm-definition. */
6787 if (token1->keyword == RID_ASM)
6788 {
6789 if (statement_p)
6790 cp_parser_commit_to_tentative_parse (parser);
6791 cp_parser_asm_definition (parser);
6792 }
6793 /* If the next keyword is `namespace', we have a
6794 namespace-alias-definition. */
6795 else if (token1->keyword == RID_NAMESPACE)
6796 cp_parser_namespace_alias_definition (parser);
6797 /* If the next keyword is `using', we have either a
6798 using-declaration or a using-directive. */
6799 else if (token1->keyword == RID_USING)
6800 {
6801 cp_token *token2;
6802
6803 if (statement_p)
6804 cp_parser_commit_to_tentative_parse (parser);
6805 /* If the token after `using' is `namespace', then we have a
6806 using-directive. */
6807 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6808 if (token2->keyword == RID_NAMESPACE)
6809 cp_parser_using_directive (parser);
6810 /* Otherwise, it's a using-declaration. */
6811 else
6812 cp_parser_using_declaration (parser);
6813 }
6814 /* If the next keyword is `__label__' we have a label declaration. */
6815 else if (token1->keyword == RID_LABEL)
6816 {
6817 if (statement_p)
6818 cp_parser_commit_to_tentative_parse (parser);
6819 cp_parser_label_declaration (parser);
6820 }
6821 /* Anything else must be a simple-declaration. */
6822 else
6823 cp_parser_simple_declaration (parser, !statement_p);
6824 }
6825
6826 /* Parse a simple-declaration.
6827
6828 simple-declaration:
6829 decl-specifier-seq [opt] init-declarator-list [opt] ;
6830
6831 init-declarator-list:
6832 init-declarator
6833 init-declarator-list , init-declarator
6834
6835 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6836 function-definition as a simple-declaration. */
6837
6838 static void
6839 cp_parser_simple_declaration (cp_parser* parser,
6840 bool function_definition_allowed_p)
6841 {
6842 cp_decl_specifier_seq decl_specifiers;
6843 int declares_class_or_enum;
6844 bool saw_declarator;
6845
6846 /* Defer access checks until we know what is being declared; the
6847 checks for names appearing in the decl-specifier-seq should be
6848 done as if we were in the scope of the thing being declared. */
6849 push_deferring_access_checks (dk_deferred);
6850
6851 /* Parse the decl-specifier-seq. We have to keep track of whether
6852 or not the decl-specifier-seq declares a named class or
6853 enumeration type, since that is the only case in which the
6854 init-declarator-list is allowed to be empty.
6855
6856 [dcl.dcl]
6857
6858 In a simple-declaration, the optional init-declarator-list can be
6859 omitted only when declaring a class or enumeration, that is when
6860 the decl-specifier-seq contains either a class-specifier, an
6861 elaborated-type-specifier, or an enum-specifier. */
6862 cp_parser_decl_specifier_seq (parser,
6863 CP_PARSER_FLAGS_OPTIONAL,
6864 &decl_specifiers,
6865 &declares_class_or_enum);
6866 /* We no longer need to defer access checks. */
6867 stop_deferring_access_checks ();
6868
6869 /* In a block scope, a valid declaration must always have a
6870 decl-specifier-seq. By not trying to parse declarators, we can
6871 resolve the declaration/expression ambiguity more quickly. */
6872 if (!function_definition_allowed_p
6873 && !decl_specifiers.any_specifiers_p)
6874 {
6875 cp_parser_error (parser, "expected declaration");
6876 goto done;
6877 }
6878
6879 /* If the next two tokens are both identifiers, the code is
6880 erroneous. The usual cause of this situation is code like:
6881
6882 T t;
6883
6884 where "T" should name a type -- but does not. */
6885 if (!decl_specifiers.type
6886 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
6887 {
6888 /* If parsing tentatively, we should commit; we really are
6889 looking at a declaration. */
6890 cp_parser_commit_to_tentative_parse (parser);
6891 /* Give up. */
6892 goto done;
6893 }
6894
6895 /* If we have seen at least one decl-specifier, and the next token
6896 is not a parenthesis, then we must be looking at a declaration.
6897 (After "int (" we might be looking at a functional cast.) */
6898 if (decl_specifiers.any_specifiers_p
6899 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6900 cp_parser_commit_to_tentative_parse (parser);
6901
6902 /* Keep going until we hit the `;' at the end of the simple
6903 declaration. */
6904 saw_declarator = false;
6905 while (cp_lexer_next_token_is_not (parser->lexer,
6906 CPP_SEMICOLON))
6907 {
6908 cp_token *token;
6909 bool function_definition_p;
6910 tree decl;
6911
6912 saw_declarator = true;
6913 /* Parse the init-declarator. */
6914 decl = cp_parser_init_declarator (parser, &decl_specifiers,
6915 function_definition_allowed_p,
6916 /*member_p=*/false,
6917 declares_class_or_enum,
6918 &function_definition_p);
6919 /* If an error occurred while parsing tentatively, exit quickly.
6920 (That usually happens when in the body of a function; each
6921 statement is treated as a declaration-statement until proven
6922 otherwise.) */
6923 if (cp_parser_error_occurred (parser))
6924 goto done;
6925 /* Handle function definitions specially. */
6926 if (function_definition_p)
6927 {
6928 /* If the next token is a `,', then we are probably
6929 processing something like:
6930
6931 void f() {}, *p;
6932
6933 which is erroneous. */
6934 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6935 error ("mixing declarations and function-definitions is forbidden");
6936 /* Otherwise, we're done with the list of declarators. */
6937 else
6938 {
6939 pop_deferring_access_checks ();
6940 return;
6941 }
6942 }
6943 /* The next token should be either a `,' or a `;'. */
6944 token = cp_lexer_peek_token (parser->lexer);
6945 /* If it's a `,', there are more declarators to come. */
6946 if (token->type == CPP_COMMA)
6947 cp_lexer_consume_token (parser->lexer);
6948 /* If it's a `;', we are done. */
6949 else if (token->type == CPP_SEMICOLON)
6950 break;
6951 /* Anything else is an error. */
6952 else
6953 {
6954 /* If we have already issued an error message we don't need
6955 to issue another one. */
6956 if (decl != error_mark_node
6957 || (cp_parser_parsing_tentatively (parser)
6958 && !cp_parser_committed_to_tentative_parse (parser)))
6959 cp_parser_error (parser, "expected %<,%> or %<;%>");
6960 /* Skip tokens until we reach the end of the statement. */
6961 cp_parser_skip_to_end_of_statement (parser);
6962 /* If the next token is now a `;', consume it. */
6963 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6964 cp_lexer_consume_token (parser->lexer);
6965 goto done;
6966 }
6967 /* After the first time around, a function-definition is not
6968 allowed -- even if it was OK at first. For example:
6969
6970 int i, f() {}
6971
6972 is not valid. */
6973 function_definition_allowed_p = false;
6974 }
6975
6976 /* Issue an error message if no declarators are present, and the
6977 decl-specifier-seq does not itself declare a class or
6978 enumeration. */
6979 if (!saw_declarator)
6980 {
6981 if (cp_parser_declares_only_class_p (parser))
6982 shadow_tag (&decl_specifiers);
6983 /* Perform any deferred access checks. */
6984 perform_deferred_access_checks ();
6985 }
6986
6987 /* Consume the `;'. */
6988 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6989
6990 done:
6991 pop_deferring_access_checks ();
6992 }
6993
6994 /* Parse a decl-specifier-seq.
6995
6996 decl-specifier-seq:
6997 decl-specifier-seq [opt] decl-specifier
6998
6999 decl-specifier:
7000 storage-class-specifier
7001 type-specifier
7002 function-specifier
7003 friend
7004 typedef
7005
7006 GNU Extension:
7007
7008 decl-specifier:
7009 attributes
7010
7011 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7012
7013 The parser flags FLAGS is used to control type-specifier parsing.
7014
7015 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7016 flags:
7017
7018 1: one of the decl-specifiers is an elaborated-type-specifier
7019 (i.e., a type declaration)
7020 2: one of the decl-specifiers is an enum-specifier or a
7021 class-specifier (i.e., a type definition)
7022
7023 */
7024
7025 static void
7026 cp_parser_decl_specifier_seq (cp_parser* parser,
7027 cp_parser_flags flags,
7028 cp_decl_specifier_seq *decl_specs,
7029 int* declares_class_or_enum)
7030 {
7031 bool constructor_possible_p = !parser->in_declarator_p;
7032
7033 /* Clear DECL_SPECS. */
7034 clear_decl_specs (decl_specs);
7035
7036 /* Assume no class or enumeration type is declared. */
7037 *declares_class_or_enum = 0;
7038
7039 /* Keep reading specifiers until there are no more to read. */
7040 while (true)
7041 {
7042 bool constructor_p;
7043 bool found_decl_spec;
7044 cp_token *token;
7045
7046 /* Peek at the next token. */
7047 token = cp_lexer_peek_token (parser->lexer);
7048 /* Handle attributes. */
7049 if (token->keyword == RID_ATTRIBUTE)
7050 {
7051 /* Parse the attributes. */
7052 decl_specs->attributes
7053 = chainon (decl_specs->attributes,
7054 cp_parser_attributes_opt (parser));
7055 continue;
7056 }
7057 /* Assume we will find a decl-specifier keyword. */
7058 found_decl_spec = true;
7059 /* If the next token is an appropriate keyword, we can simply
7060 add it to the list. */
7061 switch (token->keyword)
7062 {
7063 /* decl-specifier:
7064 friend */
7065 case RID_FRIEND:
7066 if (decl_specs->specs[(int) ds_friend]++)
7067 error ("duplicate %<friend%>");
7068 /* Consume the token. */
7069 cp_lexer_consume_token (parser->lexer);
7070 break;
7071
7072 /* function-specifier:
7073 inline
7074 virtual
7075 explicit */
7076 case RID_INLINE:
7077 case RID_VIRTUAL:
7078 case RID_EXPLICIT:
7079 cp_parser_function_specifier_opt (parser, decl_specs);
7080 break;
7081
7082 /* decl-specifier:
7083 typedef */
7084 case RID_TYPEDEF:
7085 ++decl_specs->specs[(int) ds_typedef];
7086 /* Consume the token. */
7087 cp_lexer_consume_token (parser->lexer);
7088 /* A constructor declarator cannot appear in a typedef. */
7089 constructor_possible_p = false;
7090 /* The "typedef" keyword can only occur in a declaration; we
7091 may as well commit at this point. */
7092 cp_parser_commit_to_tentative_parse (parser);
7093 break;
7094
7095 /* storage-class-specifier:
7096 auto
7097 register
7098 static
7099 extern
7100 mutable
7101
7102 GNU Extension:
7103 thread */
7104 case RID_AUTO:
7105 /* Consume the token. */
7106 cp_lexer_consume_token (parser->lexer);
7107 cp_parser_set_storage_class (decl_specs, sc_auto);
7108 break;
7109 case RID_REGISTER:
7110 /* Consume the token. */
7111 cp_lexer_consume_token (parser->lexer);
7112 cp_parser_set_storage_class (decl_specs, sc_register);
7113 break;
7114 case RID_STATIC:
7115 /* Consume the token. */
7116 cp_lexer_consume_token (parser->lexer);
7117 if (decl_specs->specs[(int) ds_thread])
7118 {
7119 error ("%<__thread%> before %<static%>");
7120 decl_specs->specs[(int) ds_thread] = 0;
7121 }
7122 cp_parser_set_storage_class (decl_specs, sc_static);
7123 break;
7124 case RID_EXTERN:
7125 /* Consume the token. */
7126 cp_lexer_consume_token (parser->lexer);
7127 if (decl_specs->specs[(int) ds_thread])
7128 {
7129 error ("%<__thread%> before %<extern%>");
7130 decl_specs->specs[(int) ds_thread] = 0;
7131 }
7132 cp_parser_set_storage_class (decl_specs, sc_extern);
7133 break;
7134 case RID_MUTABLE:
7135 /* Consume the token. */
7136 cp_lexer_consume_token (parser->lexer);
7137 cp_parser_set_storage_class (decl_specs, sc_mutable);
7138 break;
7139 case RID_THREAD:
7140 /* Consume the token. */
7141 cp_lexer_consume_token (parser->lexer);
7142 ++decl_specs->specs[(int) ds_thread];
7143 break;
7144
7145 default:
7146 /* We did not yet find a decl-specifier yet. */
7147 found_decl_spec = false;
7148 break;
7149 }
7150
7151 /* Constructors are a special case. The `S' in `S()' is not a
7152 decl-specifier; it is the beginning of the declarator. */
7153 constructor_p
7154 = (!found_decl_spec
7155 && constructor_possible_p
7156 && (cp_parser_constructor_declarator_p
7157 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7158
7159 /* If we don't have a DECL_SPEC yet, then we must be looking at
7160 a type-specifier. */
7161 if (!found_decl_spec && !constructor_p)
7162 {
7163 int decl_spec_declares_class_or_enum;
7164 bool is_cv_qualifier;
7165 tree type_spec;
7166
7167 type_spec
7168 = cp_parser_type_specifier (parser, flags,
7169 decl_specs,
7170 /*is_declaration=*/true,
7171 &decl_spec_declares_class_or_enum,
7172 &is_cv_qualifier);
7173
7174 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7175
7176 /* If this type-specifier referenced a user-defined type
7177 (a typedef, class-name, etc.), then we can't allow any
7178 more such type-specifiers henceforth.
7179
7180 [dcl.spec]
7181
7182 The longest sequence of decl-specifiers that could
7183 possibly be a type name is taken as the
7184 decl-specifier-seq of a declaration. The sequence shall
7185 be self-consistent as described below.
7186
7187 [dcl.type]
7188
7189 As a general rule, at most one type-specifier is allowed
7190 in the complete decl-specifier-seq of a declaration. The
7191 only exceptions are the following:
7192
7193 -- const or volatile can be combined with any other
7194 type-specifier.
7195
7196 -- signed or unsigned can be combined with char, long,
7197 short, or int.
7198
7199 -- ..
7200
7201 Example:
7202
7203 typedef char* Pc;
7204 void g (const int Pc);
7205
7206 Here, Pc is *not* part of the decl-specifier seq; it's
7207 the declarator. Therefore, once we see a type-specifier
7208 (other than a cv-qualifier), we forbid any additional
7209 user-defined types. We *do* still allow things like `int
7210 int' to be considered a decl-specifier-seq, and issue the
7211 error message later. */
7212 if (type_spec && !is_cv_qualifier)
7213 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7214 /* A constructor declarator cannot follow a type-specifier. */
7215 if (type_spec)
7216 {
7217 constructor_possible_p = false;
7218 found_decl_spec = true;
7219 }
7220 }
7221
7222 /* If we still do not have a DECL_SPEC, then there are no more
7223 decl-specifiers. */
7224 if (!found_decl_spec)
7225 break;
7226
7227 decl_specs->any_specifiers_p = true;
7228 /* After we see one decl-specifier, further decl-specifiers are
7229 always optional. */
7230 flags |= CP_PARSER_FLAGS_OPTIONAL;
7231 }
7232
7233 /* Don't allow a friend specifier with a class definition. */
7234 if (decl_specs->specs[(int) ds_friend] != 0
7235 && (*declares_class_or_enum & 2))
7236 error ("class definition may not be declared a friend");
7237 }
7238
7239 /* Parse an (optional) storage-class-specifier.
7240
7241 storage-class-specifier:
7242 auto
7243 register
7244 static
7245 extern
7246 mutable
7247
7248 GNU Extension:
7249
7250 storage-class-specifier:
7251 thread
7252
7253 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7254
7255 static tree
7256 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7257 {
7258 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7259 {
7260 case RID_AUTO:
7261 case RID_REGISTER:
7262 case RID_STATIC:
7263 case RID_EXTERN:
7264 case RID_MUTABLE:
7265 case RID_THREAD:
7266 /* Consume the token. */
7267 return cp_lexer_consume_token (parser->lexer)->value;
7268
7269 default:
7270 return NULL_TREE;
7271 }
7272 }
7273
7274 /* Parse an (optional) function-specifier.
7275
7276 function-specifier:
7277 inline
7278 virtual
7279 explicit
7280
7281 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7282 Updates DECL_SPECS, if it is non-NULL. */
7283
7284 static tree
7285 cp_parser_function_specifier_opt (cp_parser* parser,
7286 cp_decl_specifier_seq *decl_specs)
7287 {
7288 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7289 {
7290 case RID_INLINE:
7291 if (decl_specs)
7292 ++decl_specs->specs[(int) ds_inline];
7293 break;
7294
7295 case RID_VIRTUAL:
7296 if (decl_specs)
7297 ++decl_specs->specs[(int) ds_virtual];
7298 break;
7299
7300 case RID_EXPLICIT:
7301 if (decl_specs)
7302 ++decl_specs->specs[(int) ds_explicit];
7303 break;
7304
7305 default:
7306 return NULL_TREE;
7307 }
7308
7309 /* Consume the token. */
7310 return cp_lexer_consume_token (parser->lexer)->value;
7311 }
7312
7313 /* Parse a linkage-specification.
7314
7315 linkage-specification:
7316 extern string-literal { declaration-seq [opt] }
7317 extern string-literal declaration */
7318
7319 static void
7320 cp_parser_linkage_specification (cp_parser* parser)
7321 {
7322 tree linkage;
7323
7324 /* Look for the `extern' keyword. */
7325 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7326
7327 /* Look for the string-literal. */
7328 linkage = cp_parser_string_literal (parser, false, false);
7329
7330 /* Transform the literal into an identifier. If the literal is a
7331 wide-character string, or contains embedded NULs, then we can't
7332 handle it as the user wants. */
7333 if (strlen (TREE_STRING_POINTER (linkage))
7334 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7335 {
7336 cp_parser_error (parser, "invalid linkage-specification");
7337 /* Assume C++ linkage. */
7338 linkage = lang_name_cplusplus;
7339 }
7340 else
7341 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7342
7343 /* We're now using the new linkage. */
7344 push_lang_context (linkage);
7345
7346 /* If the next token is a `{', then we're using the first
7347 production. */
7348 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7349 {
7350 /* Consume the `{' token. */
7351 cp_lexer_consume_token (parser->lexer);
7352 /* Parse the declarations. */
7353 cp_parser_declaration_seq_opt (parser);
7354 /* Look for the closing `}'. */
7355 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7356 }
7357 /* Otherwise, there's just one declaration. */
7358 else
7359 {
7360 bool saved_in_unbraced_linkage_specification_p;
7361
7362 saved_in_unbraced_linkage_specification_p
7363 = parser->in_unbraced_linkage_specification_p;
7364 parser->in_unbraced_linkage_specification_p = true;
7365 have_extern_spec = true;
7366 cp_parser_declaration (parser);
7367 have_extern_spec = false;
7368 parser->in_unbraced_linkage_specification_p
7369 = saved_in_unbraced_linkage_specification_p;
7370 }
7371
7372 /* We're done with the linkage-specification. */
7373 pop_lang_context ();
7374 }
7375
7376 /* Special member functions [gram.special] */
7377
7378 /* Parse a conversion-function-id.
7379
7380 conversion-function-id:
7381 operator conversion-type-id
7382
7383 Returns an IDENTIFIER_NODE representing the operator. */
7384
7385 static tree
7386 cp_parser_conversion_function_id (cp_parser* parser)
7387 {
7388 tree type;
7389 tree saved_scope;
7390 tree saved_qualifying_scope;
7391 tree saved_object_scope;
7392 bool pop_p = false;
7393
7394 /* Look for the `operator' token. */
7395 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7396 return error_mark_node;
7397 /* When we parse the conversion-type-id, the current scope will be
7398 reset. However, we need that information in able to look up the
7399 conversion function later, so we save it here. */
7400 saved_scope = parser->scope;
7401 saved_qualifying_scope = parser->qualifying_scope;
7402 saved_object_scope = parser->object_scope;
7403 /* We must enter the scope of the class so that the names of
7404 entities declared within the class are available in the
7405 conversion-type-id. For example, consider:
7406
7407 struct S {
7408 typedef int I;
7409 operator I();
7410 };
7411
7412 S::operator I() { ... }
7413
7414 In order to see that `I' is a type-name in the definition, we
7415 must be in the scope of `S'. */
7416 if (saved_scope)
7417 pop_p = push_scope (saved_scope);
7418 /* Parse the conversion-type-id. */
7419 type = cp_parser_conversion_type_id (parser);
7420 /* Leave the scope of the class, if any. */
7421 if (pop_p)
7422 pop_scope (saved_scope);
7423 /* Restore the saved scope. */
7424 parser->scope = saved_scope;
7425 parser->qualifying_scope = saved_qualifying_scope;
7426 parser->object_scope = saved_object_scope;
7427 /* If the TYPE is invalid, indicate failure. */
7428 if (type == error_mark_node)
7429 return error_mark_node;
7430 return mangle_conv_op_name_for_type (type);
7431 }
7432
7433 /* Parse a conversion-type-id:
7434
7435 conversion-type-id:
7436 type-specifier-seq conversion-declarator [opt]
7437
7438 Returns the TYPE specified. */
7439
7440 static tree
7441 cp_parser_conversion_type_id (cp_parser* parser)
7442 {
7443 tree attributes;
7444 cp_decl_specifier_seq type_specifiers;
7445 cp_declarator *declarator;
7446 tree type_specified;
7447
7448 /* Parse the attributes. */
7449 attributes = cp_parser_attributes_opt (parser);
7450 /* Parse the type-specifiers. */
7451 cp_parser_type_specifier_seq (parser, &type_specifiers);
7452 /* If that didn't work, stop. */
7453 if (type_specifiers.type == error_mark_node)
7454 return error_mark_node;
7455 /* Parse the conversion-declarator. */
7456 declarator = cp_parser_conversion_declarator_opt (parser);
7457
7458 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7459 /*initialized=*/0, &attributes);
7460 if (attributes)
7461 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7462 return type_specified;
7463 }
7464
7465 /* Parse an (optional) conversion-declarator.
7466
7467 conversion-declarator:
7468 ptr-operator conversion-declarator [opt]
7469
7470 */
7471
7472 static cp_declarator *
7473 cp_parser_conversion_declarator_opt (cp_parser* parser)
7474 {
7475 enum tree_code code;
7476 tree class_type;
7477 cp_cv_quals cv_quals;
7478
7479 /* We don't know if there's a ptr-operator next, or not. */
7480 cp_parser_parse_tentatively (parser);
7481 /* Try the ptr-operator. */
7482 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7483 /* If it worked, look for more conversion-declarators. */
7484 if (cp_parser_parse_definitely (parser))
7485 {
7486 cp_declarator *declarator;
7487
7488 /* Parse another optional declarator. */
7489 declarator = cp_parser_conversion_declarator_opt (parser);
7490
7491 /* Create the representation of the declarator. */
7492 if (class_type)
7493 declarator = make_ptrmem_declarator (cv_quals, class_type,
7494 declarator);
7495 else if (code == INDIRECT_REF)
7496 declarator = make_pointer_declarator (cv_quals, declarator);
7497 else
7498 declarator = make_reference_declarator (cv_quals, declarator);
7499
7500 return declarator;
7501 }
7502
7503 return NULL;
7504 }
7505
7506 /* Parse an (optional) ctor-initializer.
7507
7508 ctor-initializer:
7509 : mem-initializer-list
7510
7511 Returns TRUE iff the ctor-initializer was actually present. */
7512
7513 static bool
7514 cp_parser_ctor_initializer_opt (cp_parser* parser)
7515 {
7516 /* If the next token is not a `:', then there is no
7517 ctor-initializer. */
7518 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7519 {
7520 /* Do default initialization of any bases and members. */
7521 if (DECL_CONSTRUCTOR_P (current_function_decl))
7522 finish_mem_initializers (NULL_TREE);
7523
7524 return false;
7525 }
7526
7527 /* Consume the `:' token. */
7528 cp_lexer_consume_token (parser->lexer);
7529 /* And the mem-initializer-list. */
7530 cp_parser_mem_initializer_list (parser);
7531
7532 return true;
7533 }
7534
7535 /* Parse a mem-initializer-list.
7536
7537 mem-initializer-list:
7538 mem-initializer
7539 mem-initializer , mem-initializer-list */
7540
7541 static void
7542 cp_parser_mem_initializer_list (cp_parser* parser)
7543 {
7544 tree mem_initializer_list = NULL_TREE;
7545
7546 /* Let the semantic analysis code know that we are starting the
7547 mem-initializer-list. */
7548 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7549 error ("only constructors take base initializers");
7550
7551 /* Loop through the list. */
7552 while (true)
7553 {
7554 tree mem_initializer;
7555
7556 /* Parse the mem-initializer. */
7557 mem_initializer = cp_parser_mem_initializer (parser);
7558 /* Add it to the list, unless it was erroneous. */
7559 if (mem_initializer)
7560 {
7561 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7562 mem_initializer_list = mem_initializer;
7563 }
7564 /* If the next token is not a `,', we're done. */
7565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7566 break;
7567 /* Consume the `,' token. */
7568 cp_lexer_consume_token (parser->lexer);
7569 }
7570
7571 /* Perform semantic analysis. */
7572 if (DECL_CONSTRUCTOR_P (current_function_decl))
7573 finish_mem_initializers (mem_initializer_list);
7574 }
7575
7576 /* Parse a mem-initializer.
7577
7578 mem-initializer:
7579 mem-initializer-id ( expression-list [opt] )
7580
7581 GNU extension:
7582
7583 mem-initializer:
7584 ( expression-list [opt] )
7585
7586 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7587 class) or FIELD_DECL (for a non-static data member) to initialize;
7588 the TREE_VALUE is the expression-list. */
7589
7590 static tree
7591 cp_parser_mem_initializer (cp_parser* parser)
7592 {
7593 tree mem_initializer_id;
7594 tree expression_list;
7595 tree member;
7596
7597 /* Find out what is being initialized. */
7598 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7599 {
7600 pedwarn ("anachronistic old-style base class initializer");
7601 mem_initializer_id = NULL_TREE;
7602 }
7603 else
7604 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7605 member = expand_member_init (mem_initializer_id);
7606 if (member && !DECL_P (member))
7607 in_base_initializer = 1;
7608
7609 expression_list
7610 = cp_parser_parenthesized_expression_list (parser, false,
7611 /*non_constant_p=*/NULL);
7612 if (!expression_list)
7613 expression_list = void_type_node;
7614
7615 in_base_initializer = 0;
7616
7617 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7618 }
7619
7620 /* Parse a mem-initializer-id.
7621
7622 mem-initializer-id:
7623 :: [opt] nested-name-specifier [opt] class-name
7624 identifier
7625
7626 Returns a TYPE indicating the class to be initializer for the first
7627 production. Returns an IDENTIFIER_NODE indicating the data member
7628 to be initialized for the second production. */
7629
7630 static tree
7631 cp_parser_mem_initializer_id (cp_parser* parser)
7632 {
7633 bool global_scope_p;
7634 bool nested_name_specifier_p;
7635 bool template_p = false;
7636 tree id;
7637
7638 /* `typename' is not allowed in this context ([temp.res]). */
7639 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7640 {
7641 error ("keyword %<typename%> not allowed in this context (a qualified "
7642 "member initializer is implicitly a type)");
7643 cp_lexer_consume_token (parser->lexer);
7644 }
7645 /* Look for the optional `::' operator. */
7646 global_scope_p
7647 = (cp_parser_global_scope_opt (parser,
7648 /*current_scope_valid_p=*/false)
7649 != NULL_TREE);
7650 /* Look for the optional nested-name-specifier. The simplest way to
7651 implement:
7652
7653 [temp.res]
7654
7655 The keyword `typename' is not permitted in a base-specifier or
7656 mem-initializer; in these contexts a qualified name that
7657 depends on a template-parameter is implicitly assumed to be a
7658 type name.
7659
7660 is to assume that we have seen the `typename' keyword at this
7661 point. */
7662 nested_name_specifier_p
7663 = (cp_parser_nested_name_specifier_opt (parser,
7664 /*typename_keyword_p=*/true,
7665 /*check_dependency_p=*/true,
7666 /*type_p=*/true,
7667 /*is_declaration=*/true)
7668 != NULL_TREE);
7669 if (nested_name_specifier_p)
7670 template_p = cp_parser_optional_template_keyword (parser);
7671 /* If there is a `::' operator or a nested-name-specifier, then we
7672 are definitely looking for a class-name. */
7673 if (global_scope_p || nested_name_specifier_p)
7674 return cp_parser_class_name (parser,
7675 /*typename_keyword_p=*/true,
7676 /*template_keyword_p=*/template_p,
7677 none_type,
7678 /*check_dependency_p=*/true,
7679 /*class_head_p=*/false,
7680 /*is_declaration=*/true);
7681 /* Otherwise, we could also be looking for an ordinary identifier. */
7682 cp_parser_parse_tentatively (parser);
7683 /* Try a class-name. */
7684 id = cp_parser_class_name (parser,
7685 /*typename_keyword_p=*/true,
7686 /*template_keyword_p=*/false,
7687 none_type,
7688 /*check_dependency_p=*/true,
7689 /*class_head_p=*/false,
7690 /*is_declaration=*/true);
7691 /* If we found one, we're done. */
7692 if (cp_parser_parse_definitely (parser))
7693 return id;
7694 /* Otherwise, look for an ordinary identifier. */
7695 return cp_parser_identifier (parser);
7696 }
7697
7698 /* Overloading [gram.over] */
7699
7700 /* Parse an operator-function-id.
7701
7702 operator-function-id:
7703 operator operator
7704
7705 Returns an IDENTIFIER_NODE for the operator which is a
7706 human-readable spelling of the identifier, e.g., `operator +'. */
7707
7708 static tree
7709 cp_parser_operator_function_id (cp_parser* parser)
7710 {
7711 /* Look for the `operator' keyword. */
7712 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7713 return error_mark_node;
7714 /* And then the name of the operator itself. */
7715 return cp_parser_operator (parser);
7716 }
7717
7718 /* Parse an operator.
7719
7720 operator:
7721 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7722 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7723 || ++ -- , ->* -> () []
7724
7725 GNU Extensions:
7726
7727 operator:
7728 <? >? <?= >?=
7729
7730 Returns an IDENTIFIER_NODE for the operator which is a
7731 human-readable spelling of the identifier, e.g., `operator +'. */
7732
7733 static tree
7734 cp_parser_operator (cp_parser* parser)
7735 {
7736 tree id = NULL_TREE;
7737 cp_token *token;
7738
7739 /* Peek at the next token. */
7740 token = cp_lexer_peek_token (parser->lexer);
7741 /* Figure out which operator we have. */
7742 switch (token->type)
7743 {
7744 case CPP_KEYWORD:
7745 {
7746 enum tree_code op;
7747
7748 /* The keyword should be either `new' or `delete'. */
7749 if (token->keyword == RID_NEW)
7750 op = NEW_EXPR;
7751 else if (token->keyword == RID_DELETE)
7752 op = DELETE_EXPR;
7753 else
7754 break;
7755
7756 /* Consume the `new' or `delete' token. */
7757 cp_lexer_consume_token (parser->lexer);
7758
7759 /* Peek at the next token. */
7760 token = cp_lexer_peek_token (parser->lexer);
7761 /* If it's a `[' token then this is the array variant of the
7762 operator. */
7763 if (token->type == CPP_OPEN_SQUARE)
7764 {
7765 /* Consume the `[' token. */
7766 cp_lexer_consume_token (parser->lexer);
7767 /* Look for the `]' token. */
7768 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7769 id = ansi_opname (op == NEW_EXPR
7770 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7771 }
7772 /* Otherwise, we have the non-array variant. */
7773 else
7774 id = ansi_opname (op);
7775
7776 return id;
7777 }
7778
7779 case CPP_PLUS:
7780 id = ansi_opname (PLUS_EXPR);
7781 break;
7782
7783 case CPP_MINUS:
7784 id = ansi_opname (MINUS_EXPR);
7785 break;
7786
7787 case CPP_MULT:
7788 id = ansi_opname (MULT_EXPR);
7789 break;
7790
7791 case CPP_DIV:
7792 id = ansi_opname (TRUNC_DIV_EXPR);
7793 break;
7794
7795 case CPP_MOD:
7796 id = ansi_opname (TRUNC_MOD_EXPR);
7797 break;
7798
7799 case CPP_XOR:
7800 id = ansi_opname (BIT_XOR_EXPR);
7801 break;
7802
7803 case CPP_AND:
7804 id = ansi_opname (BIT_AND_EXPR);
7805 break;
7806
7807 case CPP_OR:
7808 id = ansi_opname (BIT_IOR_EXPR);
7809 break;
7810
7811 case CPP_COMPL:
7812 id = ansi_opname (BIT_NOT_EXPR);
7813 break;
7814
7815 case CPP_NOT:
7816 id = ansi_opname (TRUTH_NOT_EXPR);
7817 break;
7818
7819 case CPP_EQ:
7820 id = ansi_assopname (NOP_EXPR);
7821 break;
7822
7823 case CPP_LESS:
7824 id = ansi_opname (LT_EXPR);
7825 break;
7826
7827 case CPP_GREATER:
7828 id = ansi_opname (GT_EXPR);
7829 break;
7830
7831 case CPP_PLUS_EQ:
7832 id = ansi_assopname (PLUS_EXPR);
7833 break;
7834
7835 case CPP_MINUS_EQ:
7836 id = ansi_assopname (MINUS_EXPR);
7837 break;
7838
7839 case CPP_MULT_EQ:
7840 id = ansi_assopname (MULT_EXPR);
7841 break;
7842
7843 case CPP_DIV_EQ:
7844 id = ansi_assopname (TRUNC_DIV_EXPR);
7845 break;
7846
7847 case CPP_MOD_EQ:
7848 id = ansi_assopname (TRUNC_MOD_EXPR);
7849 break;
7850
7851 case CPP_XOR_EQ:
7852 id = ansi_assopname (BIT_XOR_EXPR);
7853 break;
7854
7855 case CPP_AND_EQ:
7856 id = ansi_assopname (BIT_AND_EXPR);
7857 break;
7858
7859 case CPP_OR_EQ:
7860 id = ansi_assopname (BIT_IOR_EXPR);
7861 break;
7862
7863 case CPP_LSHIFT:
7864 id = ansi_opname (LSHIFT_EXPR);
7865 break;
7866
7867 case CPP_RSHIFT:
7868 id = ansi_opname (RSHIFT_EXPR);
7869 break;
7870
7871 case CPP_LSHIFT_EQ:
7872 id = ansi_assopname (LSHIFT_EXPR);
7873 break;
7874
7875 case CPP_RSHIFT_EQ:
7876 id = ansi_assopname (RSHIFT_EXPR);
7877 break;
7878
7879 case CPP_EQ_EQ:
7880 id = ansi_opname (EQ_EXPR);
7881 break;
7882
7883 case CPP_NOT_EQ:
7884 id = ansi_opname (NE_EXPR);
7885 break;
7886
7887 case CPP_LESS_EQ:
7888 id = ansi_opname (LE_EXPR);
7889 break;
7890
7891 case CPP_GREATER_EQ:
7892 id = ansi_opname (GE_EXPR);
7893 break;
7894
7895 case CPP_AND_AND:
7896 id = ansi_opname (TRUTH_ANDIF_EXPR);
7897 break;
7898
7899 case CPP_OR_OR:
7900 id = ansi_opname (TRUTH_ORIF_EXPR);
7901 break;
7902
7903 case CPP_PLUS_PLUS:
7904 id = ansi_opname (POSTINCREMENT_EXPR);
7905 break;
7906
7907 case CPP_MINUS_MINUS:
7908 id = ansi_opname (PREDECREMENT_EXPR);
7909 break;
7910
7911 case CPP_COMMA:
7912 id = ansi_opname (COMPOUND_EXPR);
7913 break;
7914
7915 case CPP_DEREF_STAR:
7916 id = ansi_opname (MEMBER_REF);
7917 break;
7918
7919 case CPP_DEREF:
7920 id = ansi_opname (COMPONENT_REF);
7921 break;
7922
7923 case CPP_OPEN_PAREN:
7924 /* Consume the `('. */
7925 cp_lexer_consume_token (parser->lexer);
7926 /* Look for the matching `)'. */
7927 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7928 return ansi_opname (CALL_EXPR);
7929
7930 case CPP_OPEN_SQUARE:
7931 /* Consume the `['. */
7932 cp_lexer_consume_token (parser->lexer);
7933 /* Look for the matching `]'. */
7934 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7935 return ansi_opname (ARRAY_REF);
7936
7937 /* Extensions. */
7938 case CPP_MIN:
7939 id = ansi_opname (MIN_EXPR);
7940 break;
7941
7942 case CPP_MAX:
7943 id = ansi_opname (MAX_EXPR);
7944 break;
7945
7946 case CPP_MIN_EQ:
7947 id = ansi_assopname (MIN_EXPR);
7948 break;
7949
7950 case CPP_MAX_EQ:
7951 id = ansi_assopname (MAX_EXPR);
7952 break;
7953
7954 default:
7955 /* Anything else is an error. */
7956 break;
7957 }
7958
7959 /* If we have selected an identifier, we need to consume the
7960 operator token. */
7961 if (id)
7962 cp_lexer_consume_token (parser->lexer);
7963 /* Otherwise, no valid operator name was present. */
7964 else
7965 {
7966 cp_parser_error (parser, "expected operator");
7967 id = error_mark_node;
7968 }
7969
7970 return id;
7971 }
7972
7973 /* Parse a template-declaration.
7974
7975 template-declaration:
7976 export [opt] template < template-parameter-list > declaration
7977
7978 If MEMBER_P is TRUE, this template-declaration occurs within a
7979 class-specifier.
7980
7981 The grammar rule given by the standard isn't correct. What
7982 is really meant is:
7983
7984 template-declaration:
7985 export [opt] template-parameter-list-seq
7986 decl-specifier-seq [opt] init-declarator [opt] ;
7987 export [opt] template-parameter-list-seq
7988 function-definition
7989
7990 template-parameter-list-seq:
7991 template-parameter-list-seq [opt]
7992 template < template-parameter-list > */
7993
7994 static void
7995 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7996 {
7997 /* Check for `export'. */
7998 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7999 {
8000 /* Consume the `export' token. */
8001 cp_lexer_consume_token (parser->lexer);
8002 /* Warn that we do not support `export'. */
8003 warning ("keyword %<export%> not implemented, and will be ignored");
8004 }
8005
8006 cp_parser_template_declaration_after_export (parser, member_p);
8007 }
8008
8009 /* Parse a template-parameter-list.
8010
8011 template-parameter-list:
8012 template-parameter
8013 template-parameter-list , template-parameter
8014
8015 Returns a TREE_LIST. Each node represents a template parameter.
8016 The nodes are connected via their TREE_CHAINs. */
8017
8018 static tree
8019 cp_parser_template_parameter_list (cp_parser* parser)
8020 {
8021 tree parameter_list = NULL_TREE;
8022
8023 while (true)
8024 {
8025 tree parameter;
8026 cp_token *token;
8027 bool is_non_type;
8028
8029 /* Parse the template-parameter. */
8030 parameter = cp_parser_template_parameter (parser, &is_non_type);
8031 /* Add it to the list. */
8032 parameter_list = process_template_parm (parameter_list,
8033 parameter,
8034 is_non_type);
8035 /* Peek at the next token. */
8036 token = cp_lexer_peek_token (parser->lexer);
8037 /* If it's not a `,', we're done. */
8038 if (token->type != CPP_COMMA)
8039 break;
8040 /* Otherwise, consume the `,' token. */
8041 cp_lexer_consume_token (parser->lexer);
8042 }
8043
8044 return parameter_list;
8045 }
8046
8047 /* Parse a template-parameter.
8048
8049 template-parameter:
8050 type-parameter
8051 parameter-declaration
8052
8053 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
8054 TREE_PURPOSE is the default value, if any. *IS_NON_TYPE is set to
8055 true iff this parameter is a non-type parameter. */
8056
8057 static tree
8058 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8059 {
8060 cp_token *token;
8061 cp_parameter_declarator *parameter_declarator;
8062
8063 /* Assume it is a type parameter or a template parameter. */
8064 *is_non_type = false;
8065 /* Peek at the next token. */
8066 token = cp_lexer_peek_token (parser->lexer);
8067 /* If it is `class' or `template', we have a type-parameter. */
8068 if (token->keyword == RID_TEMPLATE)
8069 return cp_parser_type_parameter (parser);
8070 /* If it is `class' or `typename' we do not know yet whether it is a
8071 type parameter or a non-type parameter. Consider:
8072
8073 template <typename T, typename T::X X> ...
8074
8075 or:
8076
8077 template <class C, class D*> ...
8078
8079 Here, the first parameter is a type parameter, and the second is
8080 a non-type parameter. We can tell by looking at the token after
8081 the identifier -- if it is a `,', `=', or `>' then we have a type
8082 parameter. */
8083 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8084 {
8085 /* Peek at the token after `class' or `typename'. */
8086 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8087 /* If it's an identifier, skip it. */
8088 if (token->type == CPP_NAME)
8089 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8090 /* Now, see if the token looks like the end of a template
8091 parameter. */
8092 if (token->type == CPP_COMMA
8093 || token->type == CPP_EQ
8094 || token->type == CPP_GREATER)
8095 return cp_parser_type_parameter (parser);
8096 }
8097
8098 /* Otherwise, it is a non-type parameter.
8099
8100 [temp.param]
8101
8102 When parsing a default template-argument for a non-type
8103 template-parameter, the first non-nested `>' is taken as the end
8104 of the template parameter-list rather than a greater-than
8105 operator. */
8106 *is_non_type = true;
8107 parameter_declarator
8108 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8109 /*parenthesized_p=*/NULL);
8110 return (build_tree_list
8111 (parameter_declarator->default_argument,
8112 grokdeclarator (parameter_declarator->declarator,
8113 &parameter_declarator->decl_specifiers,
8114 PARM, /*initialized=*/0,
8115 /*attrlist=*/NULL)));
8116 }
8117
8118 /* Parse a type-parameter.
8119
8120 type-parameter:
8121 class identifier [opt]
8122 class identifier [opt] = type-id
8123 typename identifier [opt]
8124 typename identifier [opt] = type-id
8125 template < template-parameter-list > class identifier [opt]
8126 template < template-parameter-list > class identifier [opt]
8127 = id-expression
8128
8129 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8130 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8131 the declaration of the parameter. */
8132
8133 static tree
8134 cp_parser_type_parameter (cp_parser* parser)
8135 {
8136 cp_token *token;
8137 tree parameter;
8138
8139 /* Look for a keyword to tell us what kind of parameter this is. */
8140 token = cp_parser_require (parser, CPP_KEYWORD,
8141 "`class', `typename', or `template'");
8142 if (!token)
8143 return error_mark_node;
8144
8145 switch (token->keyword)
8146 {
8147 case RID_CLASS:
8148 case RID_TYPENAME:
8149 {
8150 tree identifier;
8151 tree default_argument;
8152
8153 /* If the next token is an identifier, then it names the
8154 parameter. */
8155 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8156 identifier = cp_parser_identifier (parser);
8157 else
8158 identifier = NULL_TREE;
8159
8160 /* Create the parameter. */
8161 parameter = finish_template_type_parm (class_type_node, identifier);
8162
8163 /* If the next token is an `=', we have a default argument. */
8164 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8165 {
8166 /* Consume the `=' token. */
8167 cp_lexer_consume_token (parser->lexer);
8168 /* Parse the default-argument. */
8169 default_argument = cp_parser_type_id (parser);
8170 }
8171 else
8172 default_argument = NULL_TREE;
8173
8174 /* Create the combined representation of the parameter and the
8175 default argument. */
8176 parameter = build_tree_list (default_argument, parameter);
8177 }
8178 break;
8179
8180 case RID_TEMPLATE:
8181 {
8182 tree parameter_list;
8183 tree identifier;
8184 tree default_argument;
8185
8186 /* Look for the `<'. */
8187 cp_parser_require (parser, CPP_LESS, "`<'");
8188 /* Parse the template-parameter-list. */
8189 begin_template_parm_list ();
8190 parameter_list
8191 = cp_parser_template_parameter_list (parser);
8192 parameter_list = end_template_parm_list (parameter_list);
8193 /* Look for the `>'. */
8194 cp_parser_require (parser, CPP_GREATER, "`>'");
8195 /* Look for the `class' keyword. */
8196 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8197 /* If the next token is an `=', then there is a
8198 default-argument. If the next token is a `>', we are at
8199 the end of the parameter-list. If the next token is a `,',
8200 then we are at the end of this parameter. */
8201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8202 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8203 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8204 {
8205 identifier = cp_parser_identifier (parser);
8206 /* Treat invalid names as if the parameter were nameless. */
8207 if (identifier == error_mark_node)
8208 identifier = NULL_TREE;
8209 }
8210 else
8211 identifier = NULL_TREE;
8212
8213 /* Create the template parameter. */
8214 parameter = finish_template_template_parm (class_type_node,
8215 identifier);
8216
8217 /* If the next token is an `=', then there is a
8218 default-argument. */
8219 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8220 {
8221 bool is_template;
8222
8223 /* Consume the `='. */
8224 cp_lexer_consume_token (parser->lexer);
8225 /* Parse the id-expression. */
8226 default_argument
8227 = cp_parser_id_expression (parser,
8228 /*template_keyword_p=*/false,
8229 /*check_dependency_p=*/true,
8230 /*template_p=*/&is_template,
8231 /*declarator_p=*/false);
8232 if (TREE_CODE (default_argument) == TYPE_DECL)
8233 /* If the id-expression was a template-id that refers to
8234 a template-class, we already have the declaration here,
8235 so no further lookup is needed. */
8236 ;
8237 else
8238 /* Look up the name. */
8239 default_argument
8240 = cp_parser_lookup_name (parser, default_argument,
8241 none_type,
8242 /*is_template=*/is_template,
8243 /*is_namespace=*/false,
8244 /*check_dependency=*/true,
8245 /*ambiguous_p=*/NULL);
8246 /* See if the default argument is valid. */
8247 default_argument
8248 = check_template_template_default_arg (default_argument);
8249 }
8250 else
8251 default_argument = NULL_TREE;
8252
8253 /* Create the combined representation of the parameter and the
8254 default argument. */
8255 parameter = build_tree_list (default_argument, parameter);
8256 }
8257 break;
8258
8259 default:
8260 gcc_unreachable ();
8261 break;
8262 }
8263
8264 return parameter;
8265 }
8266
8267 /* Parse a template-id.
8268
8269 template-id:
8270 template-name < template-argument-list [opt] >
8271
8272 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8273 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8274 returned. Otherwise, if the template-name names a function, or set
8275 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8276 names a class, returns a TYPE_DECL for the specialization.
8277
8278 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8279 uninstantiated templates. */
8280
8281 static tree
8282 cp_parser_template_id (cp_parser *parser,
8283 bool template_keyword_p,
8284 bool check_dependency_p,
8285 bool is_declaration)
8286 {
8287 tree template;
8288 tree arguments;
8289 tree template_id;
8290 cp_token_position start_of_id = 0;
8291 tree access_check = NULL_TREE;
8292 cp_token *next_token, *next_token_2;
8293 bool is_identifier;
8294
8295 /* If the next token corresponds to a template-id, there is no need
8296 to reparse it. */
8297 next_token = cp_lexer_peek_token (parser->lexer);
8298 if (next_token->type == CPP_TEMPLATE_ID)
8299 {
8300 tree value;
8301 tree check;
8302
8303 /* Get the stored value. */
8304 value = cp_lexer_consume_token (parser->lexer)->value;
8305 /* Perform any access checks that were deferred. */
8306 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8307 perform_or_defer_access_check (TREE_PURPOSE (check),
8308 TREE_VALUE (check));
8309 /* Return the stored value. */
8310 return TREE_VALUE (value);
8311 }
8312
8313 /* Avoid performing name lookup if there is no possibility of
8314 finding a template-id. */
8315 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8316 || (next_token->type == CPP_NAME
8317 && !cp_parser_nth_token_starts_template_argument_list_p
8318 (parser, 2)))
8319 {
8320 cp_parser_error (parser, "expected template-id");
8321 return error_mark_node;
8322 }
8323
8324 /* Remember where the template-id starts. */
8325 if (cp_parser_parsing_tentatively (parser)
8326 && !cp_parser_committed_to_tentative_parse (parser))
8327 start_of_id = cp_lexer_token_position (parser->lexer, false);
8328
8329 push_deferring_access_checks (dk_deferred);
8330
8331 /* Parse the template-name. */
8332 is_identifier = false;
8333 template = cp_parser_template_name (parser, template_keyword_p,
8334 check_dependency_p,
8335 is_declaration,
8336 &is_identifier);
8337 if (template == error_mark_node || is_identifier)
8338 {
8339 pop_deferring_access_checks ();
8340 return template;
8341 }
8342
8343 /* If we find the sequence `[:' after a template-name, it's probably
8344 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8345 parse correctly the argument list. */
8346 next_token = cp_lexer_peek_token (parser->lexer);
8347 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8348 if (next_token->type == CPP_OPEN_SQUARE
8349 && next_token->flags & DIGRAPH
8350 && next_token_2->type == CPP_COLON
8351 && !(next_token_2->flags & PREV_WHITE))
8352 {
8353 cp_parser_parse_tentatively (parser);
8354 /* Change `:' into `::'. */
8355 next_token_2->type = CPP_SCOPE;
8356 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8357 CPP_LESS. */
8358 cp_lexer_consume_token (parser->lexer);
8359 /* Parse the arguments. */
8360 arguments = cp_parser_enclosed_template_argument_list (parser);
8361 if (!cp_parser_parse_definitely (parser))
8362 {
8363 /* If we couldn't parse an argument list, then we revert our changes
8364 and return simply an error. Maybe this is not a template-id
8365 after all. */
8366 next_token_2->type = CPP_COLON;
8367 cp_parser_error (parser, "expected %<<%>");
8368 pop_deferring_access_checks ();
8369 return error_mark_node;
8370 }
8371 /* Otherwise, emit an error about the invalid digraph, but continue
8372 parsing because we got our argument list. */
8373 pedwarn ("%<<::%> cannot begin a template-argument list");
8374 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8375 "between %<<%> and %<::%>");
8376 if (!flag_permissive)
8377 {
8378 static bool hint;
8379 if (!hint)
8380 {
8381 inform ("(if you use -fpermissive G++ will accept your code)");
8382 hint = true;
8383 }
8384 }
8385 }
8386 else
8387 {
8388 /* Look for the `<' that starts the template-argument-list. */
8389 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8390 {
8391 pop_deferring_access_checks ();
8392 return error_mark_node;
8393 }
8394 /* Parse the arguments. */
8395 arguments = cp_parser_enclosed_template_argument_list (parser);
8396 }
8397
8398 /* Build a representation of the specialization. */
8399 if (TREE_CODE (template) == IDENTIFIER_NODE)
8400 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8401 else if (DECL_CLASS_TEMPLATE_P (template)
8402 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8403 template_id
8404 = finish_template_type (template, arguments,
8405 cp_lexer_next_token_is (parser->lexer,
8406 CPP_SCOPE));
8407 else
8408 {
8409 /* If it's not a class-template or a template-template, it should be
8410 a function-template. */
8411 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8412 || TREE_CODE (template) == OVERLOAD
8413 || BASELINK_P (template)));
8414
8415 template_id = lookup_template_function (template, arguments);
8416 }
8417
8418 /* Retrieve any deferred checks. Do not pop this access checks yet
8419 so the memory will not be reclaimed during token replacing below. */
8420 access_check = get_deferred_access_checks ();
8421
8422 /* If parsing tentatively, replace the sequence of tokens that makes
8423 up the template-id with a CPP_TEMPLATE_ID token. That way,
8424 should we re-parse the token stream, we will not have to repeat
8425 the effort required to do the parse, nor will we issue duplicate
8426 error messages about problems during instantiation of the
8427 template. */
8428 if (start_of_id)
8429 {
8430 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8431
8432 /* Reset the contents of the START_OF_ID token. */
8433 token->type = CPP_TEMPLATE_ID;
8434 token->value = build_tree_list (access_check, template_id);
8435 token->keyword = RID_MAX;
8436
8437 /* Purge all subsequent tokens. */
8438 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8439 }
8440
8441 pop_deferring_access_checks ();
8442 return template_id;
8443 }
8444
8445 /* Parse a template-name.
8446
8447 template-name:
8448 identifier
8449
8450 The standard should actually say:
8451
8452 template-name:
8453 identifier
8454 operator-function-id
8455
8456 A defect report has been filed about this issue.
8457
8458 A conversion-function-id cannot be a template name because they cannot
8459 be part of a template-id. In fact, looking at this code:
8460
8461 a.operator K<int>()
8462
8463 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8464 It is impossible to call a templated conversion-function-id with an
8465 explicit argument list, since the only allowed template parameter is
8466 the type to which it is converting.
8467
8468 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8469 `template' keyword, in a construction like:
8470
8471 T::template f<3>()
8472
8473 In that case `f' is taken to be a template-name, even though there
8474 is no way of knowing for sure.
8475
8476 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8477 name refers to a set of overloaded functions, at least one of which
8478 is a template, or an IDENTIFIER_NODE with the name of the template,
8479 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8480 names are looked up inside uninstantiated templates. */
8481
8482 static tree
8483 cp_parser_template_name (cp_parser* parser,
8484 bool template_keyword_p,
8485 bool check_dependency_p,
8486 bool is_declaration,
8487 bool *is_identifier)
8488 {
8489 tree identifier;
8490 tree decl;
8491 tree fns;
8492
8493 /* If the next token is `operator', then we have either an
8494 operator-function-id or a conversion-function-id. */
8495 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8496 {
8497 /* We don't know whether we're looking at an
8498 operator-function-id or a conversion-function-id. */
8499 cp_parser_parse_tentatively (parser);
8500 /* Try an operator-function-id. */
8501 identifier = cp_parser_operator_function_id (parser);
8502 /* If that didn't work, try a conversion-function-id. */
8503 if (!cp_parser_parse_definitely (parser))
8504 {
8505 cp_parser_error (parser, "expected template-name");
8506 return error_mark_node;
8507 }
8508 }
8509 /* Look for the identifier. */
8510 else
8511 identifier = cp_parser_identifier (parser);
8512
8513 /* If we didn't find an identifier, we don't have a template-id. */
8514 if (identifier == error_mark_node)
8515 return error_mark_node;
8516
8517 /* If the name immediately followed the `template' keyword, then it
8518 is a template-name. However, if the next token is not `<', then
8519 we do not treat it as a template-name, since it is not being used
8520 as part of a template-id. This enables us to handle constructs
8521 like:
8522
8523 template <typename T> struct S { S(); };
8524 template <typename T> S<T>::S();
8525
8526 correctly. We would treat `S' as a template -- if it were `S<T>'
8527 -- but we do not if there is no `<'. */
8528
8529 if (processing_template_decl
8530 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8531 {
8532 /* In a declaration, in a dependent context, we pretend that the
8533 "template" keyword was present in order to improve error
8534 recovery. For example, given:
8535
8536 template <typename T> void f(T::X<int>);
8537
8538 we want to treat "X<int>" as a template-id. */
8539 if (is_declaration
8540 && !template_keyword_p
8541 && parser->scope && TYPE_P (parser->scope)
8542 && check_dependency_p
8543 && dependent_type_p (parser->scope)
8544 /* Do not do this for dtors (or ctors), since they never
8545 need the template keyword before their name. */
8546 && !constructor_name_p (identifier, parser->scope))
8547 {
8548 cp_token_position start = 0;
8549
8550 /* Explain what went wrong. */
8551 error ("non-template %qD used as template", identifier);
8552 inform ("use %<%T::template %D%> to indicate that it is a template",
8553 parser->scope, identifier);
8554 /* If parsing tentatively, find the location of the "<"
8555 token. */
8556 if (cp_parser_parsing_tentatively (parser)
8557 && !cp_parser_committed_to_tentative_parse (parser))
8558 {
8559 cp_parser_simulate_error (parser);
8560 start = cp_lexer_token_position (parser->lexer, true);
8561 }
8562 /* Parse the template arguments so that we can issue error
8563 messages about them. */
8564 cp_lexer_consume_token (parser->lexer);
8565 cp_parser_enclosed_template_argument_list (parser);
8566 /* Skip tokens until we find a good place from which to
8567 continue parsing. */
8568 cp_parser_skip_to_closing_parenthesis (parser,
8569 /*recovering=*/true,
8570 /*or_comma=*/true,
8571 /*consume_paren=*/false);
8572 /* If parsing tentatively, permanently remove the
8573 template argument list. That will prevent duplicate
8574 error messages from being issued about the missing
8575 "template" keyword. */
8576 if (start)
8577 cp_lexer_purge_tokens_after (parser->lexer, start);
8578 if (is_identifier)
8579 *is_identifier = true;
8580 return identifier;
8581 }
8582
8583 /* If the "template" keyword is present, then there is generally
8584 no point in doing name-lookup, so we just return IDENTIFIER.
8585 But, if the qualifying scope is non-dependent then we can
8586 (and must) do name-lookup normally. */
8587 if (template_keyword_p
8588 && (!parser->scope
8589 || (TYPE_P (parser->scope)
8590 && dependent_type_p (parser->scope))))
8591 return identifier;
8592 }
8593
8594 /* Look up the name. */
8595 decl = cp_parser_lookup_name (parser, identifier,
8596 none_type,
8597 /*is_template=*/false,
8598 /*is_namespace=*/false,
8599 check_dependency_p,
8600 /*ambiguous_p=*/NULL);
8601 decl = maybe_get_template_decl_from_type_decl (decl);
8602
8603 /* If DECL is a template, then the name was a template-name. */
8604 if (TREE_CODE (decl) == TEMPLATE_DECL)
8605 ;
8606 else
8607 {
8608 /* The standard does not explicitly indicate whether a name that
8609 names a set of overloaded declarations, some of which are
8610 templates, is a template-name. However, such a name should
8611 be a template-name; otherwise, there is no way to form a
8612 template-id for the overloaded templates. */
8613 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8614 if (TREE_CODE (fns) == OVERLOAD)
8615 {
8616 tree fn;
8617
8618 for (fn = fns; fn; fn = OVL_NEXT (fn))
8619 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8620 break;
8621 }
8622 else
8623 {
8624 /* Otherwise, the name does not name a template. */
8625 cp_parser_error (parser, "expected template-name");
8626 return error_mark_node;
8627 }
8628 }
8629
8630 /* If DECL is dependent, and refers to a function, then just return
8631 its name; we will look it up again during template instantiation. */
8632 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8633 {
8634 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8635 if (TYPE_P (scope) && dependent_type_p (scope))
8636 return identifier;
8637 }
8638
8639 return decl;
8640 }
8641
8642 /* Parse a template-argument-list.
8643
8644 template-argument-list:
8645 template-argument
8646 template-argument-list , template-argument
8647
8648 Returns a TREE_VEC containing the arguments. */
8649
8650 static tree
8651 cp_parser_template_argument_list (cp_parser* parser)
8652 {
8653 tree fixed_args[10];
8654 unsigned n_args = 0;
8655 unsigned alloced = 10;
8656 tree *arg_ary = fixed_args;
8657 tree vec;
8658 bool saved_in_template_argument_list_p;
8659
8660 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8661 parser->in_template_argument_list_p = true;
8662 do
8663 {
8664 tree argument;
8665
8666 if (n_args)
8667 /* Consume the comma. */
8668 cp_lexer_consume_token (parser->lexer);
8669
8670 /* Parse the template-argument. */
8671 argument = cp_parser_template_argument (parser);
8672 if (n_args == alloced)
8673 {
8674 alloced *= 2;
8675
8676 if (arg_ary == fixed_args)
8677 {
8678 arg_ary = xmalloc (sizeof (tree) * alloced);
8679 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8680 }
8681 else
8682 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8683 }
8684 arg_ary[n_args++] = argument;
8685 }
8686 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8687
8688 vec = make_tree_vec (n_args);
8689
8690 while (n_args--)
8691 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8692
8693 if (arg_ary != fixed_args)
8694 free (arg_ary);
8695 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8696 return vec;
8697 }
8698
8699 /* Parse a template-argument.
8700
8701 template-argument:
8702 assignment-expression
8703 type-id
8704 id-expression
8705
8706 The representation is that of an assignment-expression, type-id, or
8707 id-expression -- except that the qualified id-expression is
8708 evaluated, so that the value returned is either a DECL or an
8709 OVERLOAD.
8710
8711 Although the standard says "assignment-expression", it forbids
8712 throw-expressions or assignments in the template argument.
8713 Therefore, we use "conditional-expression" instead. */
8714
8715 static tree
8716 cp_parser_template_argument (cp_parser* parser)
8717 {
8718 tree argument;
8719 bool template_p;
8720 bool address_p;
8721 bool maybe_type_id = false;
8722 cp_token *token;
8723 cp_id_kind idk;
8724 tree qualifying_class;
8725
8726 /* There's really no way to know what we're looking at, so we just
8727 try each alternative in order.
8728
8729 [temp.arg]
8730
8731 In a template-argument, an ambiguity between a type-id and an
8732 expression is resolved to a type-id, regardless of the form of
8733 the corresponding template-parameter.
8734
8735 Therefore, we try a type-id first. */
8736 cp_parser_parse_tentatively (parser);
8737 argument = cp_parser_type_id (parser);
8738 /* If there was no error parsing the type-id but the next token is a '>>',
8739 we probably found a typo for '> >'. But there are type-id which are
8740 also valid expressions. For instance:
8741
8742 struct X { int operator >> (int); };
8743 template <int V> struct Foo {};
8744 Foo<X () >> 5> r;
8745
8746 Here 'X()' is a valid type-id of a function type, but the user just
8747 wanted to write the expression "X() >> 5". Thus, we remember that we
8748 found a valid type-id, but we still try to parse the argument as an
8749 expression to see what happens. */
8750 if (!cp_parser_error_occurred (parser)
8751 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8752 {
8753 maybe_type_id = true;
8754 cp_parser_abort_tentative_parse (parser);
8755 }
8756 else
8757 {
8758 /* If the next token isn't a `,' or a `>', then this argument wasn't
8759 really finished. This means that the argument is not a valid
8760 type-id. */
8761 if (!cp_parser_next_token_ends_template_argument_p (parser))
8762 cp_parser_error (parser, "expected template-argument");
8763 /* If that worked, we're done. */
8764 if (cp_parser_parse_definitely (parser))
8765 return argument;
8766 }
8767 /* We're still not sure what the argument will be. */
8768 cp_parser_parse_tentatively (parser);
8769 /* Try a template. */
8770 argument = cp_parser_id_expression (parser,
8771 /*template_keyword_p=*/false,
8772 /*check_dependency_p=*/true,
8773 &template_p,
8774 /*declarator_p=*/false);
8775 /* If the next token isn't a `,' or a `>', then this argument wasn't
8776 really finished. */
8777 if (!cp_parser_next_token_ends_template_argument_p (parser))
8778 cp_parser_error (parser, "expected template-argument");
8779 if (!cp_parser_error_occurred (parser))
8780 {
8781 /* Figure out what is being referred to. If the id-expression
8782 was for a class template specialization, then we will have a
8783 TYPE_DECL at this point. There is no need to do name lookup
8784 at this point in that case. */
8785 if (TREE_CODE (argument) != TYPE_DECL)
8786 argument = cp_parser_lookup_name (parser, argument,
8787 none_type,
8788 /*is_template=*/template_p,
8789 /*is_namespace=*/false,
8790 /*check_dependency=*/true,
8791 /*ambiguous_p=*/NULL);
8792 if (TREE_CODE (argument) != TEMPLATE_DECL
8793 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8794 cp_parser_error (parser, "expected template-name");
8795 }
8796 if (cp_parser_parse_definitely (parser))
8797 return argument;
8798 /* It must be a non-type argument. There permitted cases are given
8799 in [temp.arg.nontype]:
8800
8801 -- an integral constant-expression of integral or enumeration
8802 type; or
8803
8804 -- the name of a non-type template-parameter; or
8805
8806 -- the name of an object or function with external linkage...
8807
8808 -- the address of an object or function with external linkage...
8809
8810 -- a pointer to member... */
8811 /* Look for a non-type template parameter. */
8812 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8813 {
8814 cp_parser_parse_tentatively (parser);
8815 argument = cp_parser_primary_expression (parser,
8816 &idk,
8817 &qualifying_class);
8818 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8819 || !cp_parser_next_token_ends_template_argument_p (parser))
8820 cp_parser_simulate_error (parser);
8821 if (cp_parser_parse_definitely (parser))
8822 return argument;
8823 }
8824 /* If the next token is "&", the argument must be the address of an
8825 object or function with external linkage. */
8826 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8827 if (address_p)
8828 cp_lexer_consume_token (parser->lexer);
8829 /* See if we might have an id-expression. */
8830 token = cp_lexer_peek_token (parser->lexer);
8831 if (token->type == CPP_NAME
8832 || token->keyword == RID_OPERATOR
8833 || token->type == CPP_SCOPE
8834 || token->type == CPP_TEMPLATE_ID
8835 || token->type == CPP_NESTED_NAME_SPECIFIER)
8836 {
8837 cp_parser_parse_tentatively (parser);
8838 argument = cp_parser_primary_expression (parser,
8839 &idk,
8840 &qualifying_class);
8841 if (cp_parser_error_occurred (parser)
8842 || !cp_parser_next_token_ends_template_argument_p (parser))
8843 cp_parser_abort_tentative_parse (parser);
8844 else
8845 {
8846 if (qualifying_class)
8847 argument = finish_qualified_id_expr (qualifying_class,
8848 argument,
8849 /*done=*/true,
8850 address_p);
8851 if (TREE_CODE (argument) == VAR_DECL)
8852 {
8853 /* A variable without external linkage might still be a
8854 valid constant-expression, so no error is issued here
8855 if the external-linkage check fails. */
8856 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8857 cp_parser_simulate_error (parser);
8858 }
8859 else if (is_overloaded_fn (argument))
8860 /* All overloaded functions are allowed; if the external
8861 linkage test does not pass, an error will be issued
8862 later. */
8863 ;
8864 else if (address_p
8865 && (TREE_CODE (argument) == OFFSET_REF
8866 || TREE_CODE (argument) == SCOPE_REF))
8867 /* A pointer-to-member. */
8868 ;
8869 else
8870 cp_parser_simulate_error (parser);
8871
8872 if (cp_parser_parse_definitely (parser))
8873 {
8874 if (address_p)
8875 argument = build_x_unary_op (ADDR_EXPR, argument);
8876 return argument;
8877 }
8878 }
8879 }
8880 /* If the argument started with "&", there are no other valid
8881 alternatives at this point. */
8882 if (address_p)
8883 {
8884 cp_parser_error (parser, "invalid non-type template argument");
8885 return error_mark_node;
8886 }
8887 /* If the argument wasn't successfully parsed as a type-id followed
8888 by '>>', the argument can only be a constant expression now.
8889 Otherwise, we try parsing the constant-expression tentatively,
8890 because the argument could really be a type-id. */
8891 if (maybe_type_id)
8892 cp_parser_parse_tentatively (parser);
8893 argument = cp_parser_constant_expression (parser,
8894 /*allow_non_constant_p=*/false,
8895 /*non_constant_p=*/NULL);
8896 argument = fold_non_dependent_expr (argument);
8897 if (!maybe_type_id)
8898 return argument;
8899 if (!cp_parser_next_token_ends_template_argument_p (parser))
8900 cp_parser_error (parser, "expected template-argument");
8901 if (cp_parser_parse_definitely (parser))
8902 return argument;
8903 /* We did our best to parse the argument as a non type-id, but that
8904 was the only alternative that matched (albeit with a '>' after
8905 it). We can assume it's just a typo from the user, and a
8906 diagnostic will then be issued. */
8907 return cp_parser_type_id (parser);
8908 }
8909
8910 /* Parse an explicit-instantiation.
8911
8912 explicit-instantiation:
8913 template declaration
8914
8915 Although the standard says `declaration', what it really means is:
8916
8917 explicit-instantiation:
8918 template decl-specifier-seq [opt] declarator [opt] ;
8919
8920 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8921 supposed to be allowed. A defect report has been filed about this
8922 issue.
8923
8924 GNU Extension:
8925
8926 explicit-instantiation:
8927 storage-class-specifier template
8928 decl-specifier-seq [opt] declarator [opt] ;
8929 function-specifier template
8930 decl-specifier-seq [opt] declarator [opt] ; */
8931
8932 static void
8933 cp_parser_explicit_instantiation (cp_parser* parser)
8934 {
8935 int declares_class_or_enum;
8936 cp_decl_specifier_seq decl_specifiers;
8937 tree extension_specifier = NULL_TREE;
8938
8939 /* Look for an (optional) storage-class-specifier or
8940 function-specifier. */
8941 if (cp_parser_allow_gnu_extensions_p (parser))
8942 {
8943 extension_specifier
8944 = cp_parser_storage_class_specifier_opt (parser);
8945 if (!extension_specifier)
8946 extension_specifier
8947 = cp_parser_function_specifier_opt (parser,
8948 /*decl_specs=*/NULL);
8949 }
8950
8951 /* Look for the `template' keyword. */
8952 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8953 /* Let the front end know that we are processing an explicit
8954 instantiation. */
8955 begin_explicit_instantiation ();
8956 /* [temp.explicit] says that we are supposed to ignore access
8957 control while processing explicit instantiation directives. */
8958 push_deferring_access_checks (dk_no_check);
8959 /* Parse a decl-specifier-seq. */
8960 cp_parser_decl_specifier_seq (parser,
8961 CP_PARSER_FLAGS_OPTIONAL,
8962 &decl_specifiers,
8963 &declares_class_or_enum);
8964 /* If there was exactly one decl-specifier, and it declared a class,
8965 and there's no declarator, then we have an explicit type
8966 instantiation. */
8967 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8968 {
8969 tree type;
8970
8971 type = check_tag_decl (&decl_specifiers);
8972 /* Turn access control back on for names used during
8973 template instantiation. */
8974 pop_deferring_access_checks ();
8975 if (type)
8976 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8977 }
8978 else
8979 {
8980 cp_declarator *declarator;
8981 tree decl;
8982
8983 /* Parse the declarator. */
8984 declarator
8985 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8986 /*ctor_dtor_or_conv_p=*/NULL,
8987 /*parenthesized_p=*/NULL,
8988 /*member_p=*/false);
8989 if (declares_class_or_enum & 2)
8990 cp_parser_check_for_definition_in_return_type (declarator,
8991 decl_specifiers.type);
8992 if (declarator != cp_error_declarator)
8993 {
8994 decl = grokdeclarator (declarator, &decl_specifiers,
8995 NORMAL, 0, NULL);
8996 /* Turn access control back on for names used during
8997 template instantiation. */
8998 pop_deferring_access_checks ();
8999 /* Do the explicit instantiation. */
9000 do_decl_instantiation (decl, extension_specifier);
9001 }
9002 else
9003 {
9004 pop_deferring_access_checks ();
9005 /* Skip the body of the explicit instantiation. */
9006 cp_parser_skip_to_end_of_statement (parser);
9007 }
9008 }
9009 /* We're done with the instantiation. */
9010 end_explicit_instantiation ();
9011
9012 cp_parser_consume_semicolon_at_end_of_statement (parser);
9013 }
9014
9015 /* Parse an explicit-specialization.
9016
9017 explicit-specialization:
9018 template < > declaration
9019
9020 Although the standard says `declaration', what it really means is:
9021
9022 explicit-specialization:
9023 template <> decl-specifier [opt] init-declarator [opt] ;
9024 template <> function-definition
9025 template <> explicit-specialization
9026 template <> template-declaration */
9027
9028 static void
9029 cp_parser_explicit_specialization (cp_parser* parser)
9030 {
9031 /* Look for the `template' keyword. */
9032 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9033 /* Look for the `<'. */
9034 cp_parser_require (parser, CPP_LESS, "`<'");
9035 /* Look for the `>'. */
9036 cp_parser_require (parser, CPP_GREATER, "`>'");
9037 /* We have processed another parameter list. */
9038 ++parser->num_template_parameter_lists;
9039 /* Let the front end know that we are beginning a specialization. */
9040 begin_specialization ();
9041
9042 /* If the next keyword is `template', we need to figure out whether
9043 or not we're looking a template-declaration. */
9044 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9045 {
9046 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9047 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9048 cp_parser_template_declaration_after_export (parser,
9049 /*member_p=*/false);
9050 else
9051 cp_parser_explicit_specialization (parser);
9052 }
9053 else
9054 /* Parse the dependent declaration. */
9055 cp_parser_single_declaration (parser,
9056 /*member_p=*/false,
9057 /*friend_p=*/NULL);
9058
9059 /* We're done with the specialization. */
9060 end_specialization ();
9061 /* We're done with this parameter list. */
9062 --parser->num_template_parameter_lists;
9063 }
9064
9065 /* Parse a type-specifier.
9066
9067 type-specifier:
9068 simple-type-specifier
9069 class-specifier
9070 enum-specifier
9071 elaborated-type-specifier
9072 cv-qualifier
9073
9074 GNU Extension:
9075
9076 type-specifier:
9077 __complex__
9078
9079 Returns a representation of the type-specifier. For a
9080 class-specifier, enum-specifier, or elaborated-type-specifier, a
9081 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9082
9083 The parser flags FLAGS is used to control type-specifier parsing.
9084
9085 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9086 in a decl-specifier-seq.
9087
9088 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9089 class-specifier, enum-specifier, or elaborated-type-specifier, then
9090 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9091 if a type is declared; 2 if it is defined. Otherwise, it is set to
9092 zero.
9093
9094 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9095 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9096 is set to FALSE. */
9097
9098 static tree
9099 cp_parser_type_specifier (cp_parser* parser,
9100 cp_parser_flags flags,
9101 cp_decl_specifier_seq *decl_specs,
9102 bool is_declaration,
9103 int* declares_class_or_enum,
9104 bool* is_cv_qualifier)
9105 {
9106 tree type_spec = NULL_TREE;
9107 cp_token *token;
9108 enum rid keyword;
9109 cp_decl_spec ds = ds_last;
9110
9111 /* Assume this type-specifier does not declare a new type. */
9112 if (declares_class_or_enum)
9113 *declares_class_or_enum = 0;
9114 /* And that it does not specify a cv-qualifier. */
9115 if (is_cv_qualifier)
9116 *is_cv_qualifier = false;
9117 /* Peek at the next token. */
9118 token = cp_lexer_peek_token (parser->lexer);
9119
9120 /* If we're looking at a keyword, we can use that to guide the
9121 production we choose. */
9122 keyword = token->keyword;
9123 switch (keyword)
9124 {
9125 case RID_ENUM:
9126 /* 'enum' [identifier] '{' introduces an enum-specifier;
9127 'enum' <anything else> introduces an elaborated-type-specifier. */
9128 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9129 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9130 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9131 == CPP_OPEN_BRACE))
9132 {
9133 type_spec = cp_parser_enum_specifier (parser);
9134 if (declares_class_or_enum)
9135 *declares_class_or_enum = 2;
9136 if (decl_specs)
9137 cp_parser_set_decl_spec_type (decl_specs,
9138 type_spec,
9139 /*user_defined_p=*/true);
9140 return type_spec;
9141 }
9142 else
9143 goto elaborated_type_specifier;
9144
9145 /* Any of these indicate either a class-specifier, or an
9146 elaborated-type-specifier. */
9147 case RID_CLASS:
9148 case RID_STRUCT:
9149 case RID_UNION:
9150 /* Parse tentatively so that we can back up if we don't find a
9151 class-specifier. */
9152 cp_parser_parse_tentatively (parser);
9153 /* Look for the class-specifier. */
9154 type_spec = cp_parser_class_specifier (parser);
9155 /* If that worked, we're done. */
9156 if (cp_parser_parse_definitely (parser))
9157 {
9158 if (declares_class_or_enum)
9159 *declares_class_or_enum = 2;
9160 if (decl_specs)
9161 cp_parser_set_decl_spec_type (decl_specs,
9162 type_spec,
9163 /*user_defined_p=*/true);
9164 return type_spec;
9165 }
9166
9167 /* Fall through. */
9168 elaborated_type_specifier:
9169 /* We're declaring (not defining) a class or enum. */
9170 if (declares_class_or_enum)
9171 *declares_class_or_enum = 1;
9172
9173 /* Fall through. */
9174 case RID_TYPENAME:
9175 /* Look for an elaborated-type-specifier. */
9176 type_spec
9177 = (cp_parser_elaborated_type_specifier
9178 (parser,
9179 decl_specs && decl_specs->specs[(int) ds_friend],
9180 is_declaration));
9181 if (decl_specs)
9182 cp_parser_set_decl_spec_type (decl_specs,
9183 type_spec,
9184 /*user_defined_p=*/true);
9185 return type_spec;
9186
9187 case RID_CONST:
9188 ds = ds_const;
9189 if (is_cv_qualifier)
9190 *is_cv_qualifier = true;
9191 break;
9192
9193 case RID_VOLATILE:
9194 ds = ds_volatile;
9195 if (is_cv_qualifier)
9196 *is_cv_qualifier = true;
9197 break;
9198
9199 case RID_RESTRICT:
9200 ds = ds_restrict;
9201 if (is_cv_qualifier)
9202 *is_cv_qualifier = true;
9203 break;
9204
9205 case RID_COMPLEX:
9206 /* The `__complex__' keyword is a GNU extension. */
9207 ds = ds_complex;
9208 break;
9209
9210 default:
9211 break;
9212 }
9213
9214 /* Handle simple keywords. */
9215 if (ds != ds_last)
9216 {
9217 if (decl_specs)
9218 {
9219 ++decl_specs->specs[(int)ds];
9220 decl_specs->any_specifiers_p = true;
9221 }
9222 return cp_lexer_consume_token (parser->lexer)->value;
9223 }
9224
9225 /* If we do not already have a type-specifier, assume we are looking
9226 at a simple-type-specifier. */
9227 type_spec = cp_parser_simple_type_specifier (parser,
9228 decl_specs,
9229 flags);
9230
9231 /* If we didn't find a type-specifier, and a type-specifier was not
9232 optional in this context, issue an error message. */
9233 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9234 {
9235 cp_parser_error (parser, "expected type specifier");
9236 return error_mark_node;
9237 }
9238
9239 return type_spec;
9240 }
9241
9242 /* Parse a simple-type-specifier.
9243
9244 simple-type-specifier:
9245 :: [opt] nested-name-specifier [opt] type-name
9246 :: [opt] nested-name-specifier template template-id
9247 char
9248 wchar_t
9249 bool
9250 short
9251 int
9252 long
9253 signed
9254 unsigned
9255 float
9256 double
9257 void
9258
9259 GNU Extension:
9260
9261 simple-type-specifier:
9262 __typeof__ unary-expression
9263 __typeof__ ( type-id )
9264
9265 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9266 appropriately updated. */
9267
9268 static tree
9269 cp_parser_simple_type_specifier (cp_parser* parser,
9270 cp_decl_specifier_seq *decl_specs,
9271 cp_parser_flags flags)
9272 {
9273 tree type = NULL_TREE;
9274 cp_token *token;
9275
9276 /* Peek at the next token. */
9277 token = cp_lexer_peek_token (parser->lexer);
9278
9279 /* If we're looking at a keyword, things are easy. */
9280 switch (token->keyword)
9281 {
9282 case RID_CHAR:
9283 if (decl_specs)
9284 decl_specs->explicit_char_p = true;
9285 type = char_type_node;
9286 break;
9287 case RID_WCHAR:
9288 type = wchar_type_node;
9289 break;
9290 case RID_BOOL:
9291 type = boolean_type_node;
9292 break;
9293 case RID_SHORT:
9294 if (decl_specs)
9295 ++decl_specs->specs[(int) ds_short];
9296 type = short_integer_type_node;
9297 break;
9298 case RID_INT:
9299 if (decl_specs)
9300 decl_specs->explicit_int_p = true;
9301 type = integer_type_node;
9302 break;
9303 case RID_LONG:
9304 if (decl_specs)
9305 ++decl_specs->specs[(int) ds_long];
9306 type = long_integer_type_node;
9307 break;
9308 case RID_SIGNED:
9309 if (decl_specs)
9310 ++decl_specs->specs[(int) ds_signed];
9311 type = integer_type_node;
9312 break;
9313 case RID_UNSIGNED:
9314 if (decl_specs)
9315 ++decl_specs->specs[(int) ds_unsigned];
9316 type = unsigned_type_node;
9317 break;
9318 case RID_FLOAT:
9319 type = float_type_node;
9320 break;
9321 case RID_DOUBLE:
9322 type = double_type_node;
9323 break;
9324 case RID_VOID:
9325 type = void_type_node;
9326 break;
9327
9328 case RID_TYPEOF:
9329 /* Consume the `typeof' token. */
9330 cp_lexer_consume_token (parser->lexer);
9331 /* Parse the operand to `typeof'. */
9332 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9333 /* If it is not already a TYPE, take its type. */
9334 if (!TYPE_P (type))
9335 type = finish_typeof (type);
9336
9337 if (decl_specs)
9338 cp_parser_set_decl_spec_type (decl_specs, type,
9339 /*user_defined_p=*/true);
9340
9341 return type;
9342
9343 default:
9344 break;
9345 }
9346
9347 /* If the type-specifier was for a built-in type, we're done. */
9348 if (type)
9349 {
9350 tree id;
9351
9352 /* Record the type. */
9353 if (decl_specs
9354 && (token->keyword != RID_SIGNED
9355 && token->keyword != RID_UNSIGNED
9356 && token->keyword != RID_SHORT
9357 && token->keyword != RID_LONG))
9358 cp_parser_set_decl_spec_type (decl_specs,
9359 type,
9360 /*user_defined=*/false);
9361 if (decl_specs)
9362 decl_specs->any_specifiers_p = true;
9363
9364 /* Consume the token. */
9365 id = cp_lexer_consume_token (parser->lexer)->value;
9366
9367 /* There is no valid C++ program where a non-template type is
9368 followed by a "<". That usually indicates that the user thought
9369 that the type was a template. */
9370 cp_parser_check_for_invalid_template_id (parser, type);
9371
9372 return TYPE_NAME (type);
9373 }
9374
9375 /* The type-specifier must be a user-defined type. */
9376 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9377 {
9378 bool qualified_p;
9379 bool global_p;
9380
9381 /* Don't gobble tokens or issue error messages if this is an
9382 optional type-specifier. */
9383 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9384 cp_parser_parse_tentatively (parser);
9385
9386 /* Look for the optional `::' operator. */
9387 global_p
9388 = (cp_parser_global_scope_opt (parser,
9389 /*current_scope_valid_p=*/false)
9390 != NULL_TREE);
9391 /* Look for the nested-name specifier. */
9392 qualified_p
9393 = (cp_parser_nested_name_specifier_opt (parser,
9394 /*typename_keyword_p=*/false,
9395 /*check_dependency_p=*/true,
9396 /*type_p=*/false,
9397 /*is_declaration=*/false)
9398 != NULL_TREE);
9399 /* If we have seen a nested-name-specifier, and the next token
9400 is `template', then we are using the template-id production. */
9401 if (parser->scope
9402 && cp_parser_optional_template_keyword (parser))
9403 {
9404 /* Look for the template-id. */
9405 type = cp_parser_template_id (parser,
9406 /*template_keyword_p=*/true,
9407 /*check_dependency_p=*/true,
9408 /*is_declaration=*/false);
9409 /* If the template-id did not name a type, we are out of
9410 luck. */
9411 if (TREE_CODE (type) != TYPE_DECL)
9412 {
9413 cp_parser_error (parser, "expected template-id for type");
9414 type = NULL_TREE;
9415 }
9416 }
9417 /* Otherwise, look for a type-name. */
9418 else
9419 type = cp_parser_type_name (parser);
9420 /* Keep track of all name-lookups performed in class scopes. */
9421 if (type
9422 && !global_p
9423 && !qualified_p
9424 && TREE_CODE (type) == TYPE_DECL
9425 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9426 maybe_note_name_used_in_class (DECL_NAME (type), type);
9427 /* If it didn't work out, we don't have a TYPE. */
9428 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9429 && !cp_parser_parse_definitely (parser))
9430 type = NULL_TREE;
9431 if (type && decl_specs)
9432 cp_parser_set_decl_spec_type (decl_specs, type,
9433 /*user_defined=*/true);
9434 }
9435
9436 /* If we didn't get a type-name, issue an error message. */
9437 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9438 {
9439 cp_parser_error (parser, "expected type-name");
9440 return error_mark_node;
9441 }
9442
9443 /* There is no valid C++ program where a non-template type is
9444 followed by a "<". That usually indicates that the user thought
9445 that the type was a template. */
9446 if (type && type != error_mark_node)
9447 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9448
9449 return type;
9450 }
9451
9452 /* Parse a type-name.
9453
9454 type-name:
9455 class-name
9456 enum-name
9457 typedef-name
9458
9459 enum-name:
9460 identifier
9461
9462 typedef-name:
9463 identifier
9464
9465 Returns a TYPE_DECL for the the type. */
9466
9467 static tree
9468 cp_parser_type_name (cp_parser* parser)
9469 {
9470 tree type_decl;
9471 tree identifier;
9472
9473 /* We can't know yet whether it is a class-name or not. */
9474 cp_parser_parse_tentatively (parser);
9475 /* Try a class-name. */
9476 type_decl = cp_parser_class_name (parser,
9477 /*typename_keyword_p=*/false,
9478 /*template_keyword_p=*/false,
9479 none_type,
9480 /*check_dependency_p=*/true,
9481 /*class_head_p=*/false,
9482 /*is_declaration=*/false);
9483 /* If it's not a class-name, keep looking. */
9484 if (!cp_parser_parse_definitely (parser))
9485 {
9486 /* It must be a typedef-name or an enum-name. */
9487 identifier = cp_parser_identifier (parser);
9488 if (identifier == error_mark_node)
9489 return error_mark_node;
9490
9491 /* Look up the type-name. */
9492 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9493 /* Issue an error if we did not find a type-name. */
9494 if (TREE_CODE (type_decl) != TYPE_DECL)
9495 {
9496 if (!cp_parser_simulate_error (parser))
9497 cp_parser_name_lookup_error (parser, identifier, type_decl,
9498 "is not a type");
9499 type_decl = error_mark_node;
9500 }
9501 /* Remember that the name was used in the definition of the
9502 current class so that we can check later to see if the
9503 meaning would have been different after the class was
9504 entirely defined. */
9505 else if (type_decl != error_mark_node
9506 && !parser->scope)
9507 maybe_note_name_used_in_class (identifier, type_decl);
9508 }
9509
9510 return type_decl;
9511 }
9512
9513
9514 /* Parse an elaborated-type-specifier. Note that the grammar given
9515 here incorporates the resolution to DR68.
9516
9517 elaborated-type-specifier:
9518 class-key :: [opt] nested-name-specifier [opt] identifier
9519 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9520 enum :: [opt] nested-name-specifier [opt] identifier
9521 typename :: [opt] nested-name-specifier identifier
9522 typename :: [opt] nested-name-specifier template [opt]
9523 template-id
9524
9525 GNU extension:
9526
9527 elaborated-type-specifier:
9528 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9529 class-key attributes :: [opt] nested-name-specifier [opt]
9530 template [opt] template-id
9531 enum attributes :: [opt] nested-name-specifier [opt] identifier
9532
9533 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9534 declared `friend'. If IS_DECLARATION is TRUE, then this
9535 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9536 something is being declared.
9537
9538 Returns the TYPE specified. */
9539
9540 static tree
9541 cp_parser_elaborated_type_specifier (cp_parser* parser,
9542 bool is_friend,
9543 bool is_declaration)
9544 {
9545 enum tag_types tag_type;
9546 tree identifier;
9547 tree type = NULL_TREE;
9548 tree attributes = NULL_TREE;
9549
9550 /* See if we're looking at the `enum' keyword. */
9551 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9552 {
9553 /* Consume the `enum' token. */
9554 cp_lexer_consume_token (parser->lexer);
9555 /* Remember that it's an enumeration type. */
9556 tag_type = enum_type;
9557 /* Parse the attributes. */
9558 attributes = cp_parser_attributes_opt (parser);
9559 }
9560 /* Or, it might be `typename'. */
9561 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9562 RID_TYPENAME))
9563 {
9564 /* Consume the `typename' token. */
9565 cp_lexer_consume_token (parser->lexer);
9566 /* Remember that it's a `typename' type. */
9567 tag_type = typename_type;
9568 /* The `typename' keyword is only allowed in templates. */
9569 if (!processing_template_decl)
9570 pedwarn ("using %<typename%> outside of template");
9571 }
9572 /* Otherwise it must be a class-key. */
9573 else
9574 {
9575 tag_type = cp_parser_class_key (parser);
9576 if (tag_type == none_type)
9577 return error_mark_node;
9578 /* Parse the attributes. */
9579 attributes = cp_parser_attributes_opt (parser);
9580 }
9581
9582 /* Look for the `::' operator. */
9583 cp_parser_global_scope_opt (parser,
9584 /*current_scope_valid_p=*/false);
9585 /* Look for the nested-name-specifier. */
9586 if (tag_type == typename_type)
9587 {
9588 if (cp_parser_nested_name_specifier (parser,
9589 /*typename_keyword_p=*/true,
9590 /*check_dependency_p=*/true,
9591 /*type_p=*/true,
9592 is_declaration)
9593 == error_mark_node)
9594 return error_mark_node;
9595 }
9596 else
9597 /* Even though `typename' is not present, the proposed resolution
9598 to Core Issue 180 says that in `class A<T>::B', `B' should be
9599 considered a type-name, even if `A<T>' is dependent. */
9600 cp_parser_nested_name_specifier_opt (parser,
9601 /*typename_keyword_p=*/true,
9602 /*check_dependency_p=*/true,
9603 /*type_p=*/true,
9604 is_declaration);
9605 /* For everything but enumeration types, consider a template-id. */
9606 if (tag_type != enum_type)
9607 {
9608 bool template_p = false;
9609 tree decl;
9610
9611 /* Allow the `template' keyword. */
9612 template_p = cp_parser_optional_template_keyword (parser);
9613 /* If we didn't see `template', we don't know if there's a
9614 template-id or not. */
9615 if (!template_p)
9616 cp_parser_parse_tentatively (parser);
9617 /* Parse the template-id. */
9618 decl = cp_parser_template_id (parser, template_p,
9619 /*check_dependency_p=*/true,
9620 is_declaration);
9621 /* If we didn't find a template-id, look for an ordinary
9622 identifier. */
9623 if (!template_p && !cp_parser_parse_definitely (parser))
9624 ;
9625 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9626 in effect, then we must assume that, upon instantiation, the
9627 template will correspond to a class. */
9628 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9629 && tag_type == typename_type)
9630 type = make_typename_type (parser->scope, decl,
9631 typename_type,
9632 /*complain=*/1);
9633 else
9634 type = TREE_TYPE (decl);
9635 }
9636
9637 /* For an enumeration type, consider only a plain identifier. */
9638 if (!type)
9639 {
9640 identifier = cp_parser_identifier (parser);
9641
9642 if (identifier == error_mark_node)
9643 {
9644 parser->scope = NULL_TREE;
9645 return error_mark_node;
9646 }
9647
9648 /* For a `typename', we needn't call xref_tag. */
9649 if (tag_type == typename_type)
9650 return cp_parser_make_typename_type (parser, parser->scope,
9651 identifier);
9652 /* Look up a qualified name in the usual way. */
9653 if (parser->scope)
9654 {
9655 tree decl;
9656
9657 /* In an elaborated-type-specifier, names are assumed to name
9658 types, so we set IS_TYPE to TRUE when calling
9659 cp_parser_lookup_name. */
9660 decl = cp_parser_lookup_name (parser, identifier,
9661 tag_type,
9662 /*is_template=*/false,
9663 /*is_namespace=*/false,
9664 /*check_dependency=*/true,
9665 /*ambiguous_p=*/NULL);
9666
9667 /* If we are parsing friend declaration, DECL may be a
9668 TEMPLATE_DECL tree node here. However, we need to check
9669 whether this TEMPLATE_DECL results in valid code. Consider
9670 the following example:
9671
9672 namespace N {
9673 template <class T> class C {};
9674 }
9675 class X {
9676 template <class T> friend class N::C; // #1, valid code
9677 };
9678 template <class T> class Y {
9679 friend class N::C; // #2, invalid code
9680 };
9681
9682 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9683 name lookup of `N::C'. We see that friend declaration must
9684 be template for the code to be valid. Note that
9685 processing_template_decl does not work here since it is
9686 always 1 for the above two cases. */
9687
9688 decl = (cp_parser_maybe_treat_template_as_class
9689 (decl, /*tag_name_p=*/is_friend
9690 && parser->num_template_parameter_lists));
9691
9692 if (TREE_CODE (decl) != TYPE_DECL)
9693 {
9694 error ("expected type-name");
9695 return error_mark_node;
9696 }
9697
9698 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9699 check_elaborated_type_specifier
9700 (tag_type, decl,
9701 (parser->num_template_parameter_lists
9702 || DECL_SELF_REFERENCE_P (decl)));
9703
9704 type = TREE_TYPE (decl);
9705 }
9706 else
9707 {
9708 /* An elaborated-type-specifier sometimes introduces a new type and
9709 sometimes names an existing type. Normally, the rule is that it
9710 introduces a new type only if there is not an existing type of
9711 the same name already in scope. For example, given:
9712
9713 struct S {};
9714 void f() { struct S s; }
9715
9716 the `struct S' in the body of `f' is the same `struct S' as in
9717 the global scope; the existing definition is used. However, if
9718 there were no global declaration, this would introduce a new
9719 local class named `S'.
9720
9721 An exception to this rule applies to the following code:
9722
9723 namespace N { struct S; }
9724
9725 Here, the elaborated-type-specifier names a new type
9726 unconditionally; even if there is already an `S' in the
9727 containing scope this declaration names a new type.
9728 This exception only applies if the elaborated-type-specifier
9729 forms the complete declaration:
9730
9731 [class.name]
9732
9733 A declaration consisting solely of `class-key identifier ;' is
9734 either a redeclaration of the name in the current scope or a
9735 forward declaration of the identifier as a class name. It
9736 introduces the name into the current scope.
9737
9738 We are in this situation precisely when the next token is a `;'.
9739
9740 An exception to the exception is that a `friend' declaration does
9741 *not* name a new type; i.e., given:
9742
9743 struct S { friend struct T; };
9744
9745 `T' is not a new type in the scope of `S'.
9746
9747 Also, `new struct S' or `sizeof (struct S)' never results in the
9748 definition of a new type; a new type can only be declared in a
9749 declaration context. */
9750
9751 tag_scope ts;
9752 if (is_friend)
9753 /* Friends have special name lookup rules. */
9754 ts = ts_within_enclosing_non_class;
9755 else if (is_declaration
9756 && cp_lexer_next_token_is (parser->lexer,
9757 CPP_SEMICOLON))
9758 /* This is a `class-key identifier ;' */
9759 ts = ts_current;
9760 else
9761 ts = ts_global;
9762
9763 /* Warn about attributes. They are ignored. */
9764 if (attributes)
9765 warning ("type attributes are honored only at type definition");
9766
9767 type = xref_tag (tag_type, identifier, ts,
9768 parser->num_template_parameter_lists);
9769 }
9770 }
9771 if (tag_type != enum_type)
9772 cp_parser_check_class_key (tag_type, type);
9773
9774 /* A "<" cannot follow an elaborated type specifier. If that
9775 happens, the user was probably trying to form a template-id. */
9776 cp_parser_check_for_invalid_template_id (parser, type);
9777
9778 return type;
9779 }
9780
9781 /* Parse an enum-specifier.
9782
9783 enum-specifier:
9784 enum identifier [opt] { enumerator-list [opt] }
9785
9786 GNU Extensions:
9787 enum identifier [opt] { enumerator-list [opt] } attributes
9788
9789 Returns an ENUM_TYPE representing the enumeration. */
9790
9791 static tree
9792 cp_parser_enum_specifier (cp_parser* parser)
9793 {
9794 tree identifier;
9795 tree type;
9796
9797 /* Caller guarantees that the current token is 'enum', an identifier
9798 possibly follows, and the token after that is an opening brace.
9799 If we don't have an identifier, fabricate an anonymous name for
9800 the enumeration being defined. */
9801 cp_lexer_consume_token (parser->lexer);
9802
9803 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9804 identifier = cp_parser_identifier (parser);
9805 else
9806 identifier = make_anon_name ();
9807
9808 /* Issue an error message if type-definitions are forbidden here. */
9809 cp_parser_check_type_definition (parser);
9810
9811 /* Create the new type. We do this before consuming the opening brace
9812 so the enum will be recorded as being on the line of its tag (or the
9813 'enum' keyword, if there is no tag). */
9814 type = start_enum (identifier);
9815
9816 /* Consume the opening brace. */
9817 cp_lexer_consume_token (parser->lexer);
9818
9819 /* If the next token is not '}', then there are some enumerators. */
9820 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9821 cp_parser_enumerator_list (parser, type);
9822
9823 /* Consume the final '}'. */
9824 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9825
9826 /* Look for trailing attributes to apply to this enumeration, and
9827 apply them if appropriate. */
9828 if (cp_parser_allow_gnu_extensions_p (parser))
9829 {
9830 tree trailing_attr = cp_parser_attributes_opt (parser);
9831 cplus_decl_attributes (&type,
9832 trailing_attr,
9833 (int) ATTR_FLAG_TYPE_IN_PLACE);
9834 }
9835
9836 /* Finish up the enumeration. */
9837 finish_enum (type);
9838
9839 return type;
9840 }
9841
9842 /* Parse an enumerator-list. The enumerators all have the indicated
9843 TYPE.
9844
9845 enumerator-list:
9846 enumerator-definition
9847 enumerator-list , enumerator-definition */
9848
9849 static void
9850 cp_parser_enumerator_list (cp_parser* parser, tree type)
9851 {
9852 while (true)
9853 {
9854 /* Parse an enumerator-definition. */
9855 cp_parser_enumerator_definition (parser, type);
9856
9857 /* If the next token is not a ',', we've reached the end of
9858 the list. */
9859 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9860 break;
9861 /* Otherwise, consume the `,' and keep going. */
9862 cp_lexer_consume_token (parser->lexer);
9863 /* If the next token is a `}', there is a trailing comma. */
9864 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9865 {
9866 if (pedantic && !in_system_header)
9867 pedwarn ("comma at end of enumerator list");
9868 break;
9869 }
9870 }
9871 }
9872
9873 /* Parse an enumerator-definition. The enumerator has the indicated
9874 TYPE.
9875
9876 enumerator-definition:
9877 enumerator
9878 enumerator = constant-expression
9879
9880 enumerator:
9881 identifier */
9882
9883 static void
9884 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9885 {
9886 tree identifier;
9887 tree value;
9888
9889 /* Look for the identifier. */
9890 identifier = cp_parser_identifier (parser);
9891 if (identifier == error_mark_node)
9892 return;
9893
9894 /* If the next token is an '=', then there is an explicit value. */
9895 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9896 {
9897 /* Consume the `=' token. */
9898 cp_lexer_consume_token (parser->lexer);
9899 /* Parse the value. */
9900 value = cp_parser_constant_expression (parser,
9901 /*allow_non_constant_p=*/false,
9902 NULL);
9903 }
9904 else
9905 value = NULL_TREE;
9906
9907 /* Create the enumerator. */
9908 build_enumerator (identifier, value, type);
9909 }
9910
9911 /* Parse a namespace-name.
9912
9913 namespace-name:
9914 original-namespace-name
9915 namespace-alias
9916
9917 Returns the NAMESPACE_DECL for the namespace. */
9918
9919 static tree
9920 cp_parser_namespace_name (cp_parser* parser)
9921 {
9922 tree identifier;
9923 tree namespace_decl;
9924
9925 /* Get the name of the namespace. */
9926 identifier = cp_parser_identifier (parser);
9927 if (identifier == error_mark_node)
9928 return error_mark_node;
9929
9930 /* Look up the identifier in the currently active scope. Look only
9931 for namespaces, due to:
9932
9933 [basic.lookup.udir]
9934
9935 When looking up a namespace-name in a using-directive or alias
9936 definition, only namespace names are considered.
9937
9938 And:
9939
9940 [basic.lookup.qual]
9941
9942 During the lookup of a name preceding the :: scope resolution
9943 operator, object, function, and enumerator names are ignored.
9944
9945 (Note that cp_parser_class_or_namespace_name only calls this
9946 function if the token after the name is the scope resolution
9947 operator.) */
9948 namespace_decl = cp_parser_lookup_name (parser, identifier,
9949 none_type,
9950 /*is_template=*/false,
9951 /*is_namespace=*/true,
9952 /*check_dependency=*/true,
9953 /*ambiguous_p=*/NULL);
9954 /* If it's not a namespace, issue an error. */
9955 if (namespace_decl == error_mark_node
9956 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9957 {
9958 cp_parser_error (parser, "expected namespace-name");
9959 namespace_decl = error_mark_node;
9960 }
9961
9962 return namespace_decl;
9963 }
9964
9965 /* Parse a namespace-definition.
9966
9967 namespace-definition:
9968 named-namespace-definition
9969 unnamed-namespace-definition
9970
9971 named-namespace-definition:
9972 original-namespace-definition
9973 extension-namespace-definition
9974
9975 original-namespace-definition:
9976 namespace identifier { namespace-body }
9977
9978 extension-namespace-definition:
9979 namespace original-namespace-name { namespace-body }
9980
9981 unnamed-namespace-definition:
9982 namespace { namespace-body } */
9983
9984 static void
9985 cp_parser_namespace_definition (cp_parser* parser)
9986 {
9987 tree identifier;
9988
9989 /* Look for the `namespace' keyword. */
9990 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9991
9992 /* Get the name of the namespace. We do not attempt to distinguish
9993 between an original-namespace-definition and an
9994 extension-namespace-definition at this point. The semantic
9995 analysis routines are responsible for that. */
9996 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9997 identifier = cp_parser_identifier (parser);
9998 else
9999 identifier = NULL_TREE;
10000
10001 /* Look for the `{' to start the namespace. */
10002 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10003 /* Start the namespace. */
10004 push_namespace (identifier);
10005 /* Parse the body of the namespace. */
10006 cp_parser_namespace_body (parser);
10007 /* Finish the namespace. */
10008 pop_namespace ();
10009 /* Look for the final `}'. */
10010 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10011 }
10012
10013 /* Parse a namespace-body.
10014
10015 namespace-body:
10016 declaration-seq [opt] */
10017
10018 static void
10019 cp_parser_namespace_body (cp_parser* parser)
10020 {
10021 cp_parser_declaration_seq_opt (parser);
10022 }
10023
10024 /* Parse a namespace-alias-definition.
10025
10026 namespace-alias-definition:
10027 namespace identifier = qualified-namespace-specifier ; */
10028
10029 static void
10030 cp_parser_namespace_alias_definition (cp_parser* parser)
10031 {
10032 tree identifier;
10033 tree namespace_specifier;
10034
10035 /* Look for the `namespace' keyword. */
10036 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10037 /* Look for the identifier. */
10038 identifier = cp_parser_identifier (parser);
10039 if (identifier == error_mark_node)
10040 return;
10041 /* Look for the `=' token. */
10042 cp_parser_require (parser, CPP_EQ, "`='");
10043 /* Look for the qualified-namespace-specifier. */
10044 namespace_specifier
10045 = cp_parser_qualified_namespace_specifier (parser);
10046 /* Look for the `;' token. */
10047 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10048
10049 /* Register the alias in the symbol table. */
10050 do_namespace_alias (identifier, namespace_specifier);
10051 }
10052
10053 /* Parse a qualified-namespace-specifier.
10054
10055 qualified-namespace-specifier:
10056 :: [opt] nested-name-specifier [opt] namespace-name
10057
10058 Returns a NAMESPACE_DECL corresponding to the specified
10059 namespace. */
10060
10061 static tree
10062 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10063 {
10064 /* Look for the optional `::'. */
10065 cp_parser_global_scope_opt (parser,
10066 /*current_scope_valid_p=*/false);
10067
10068 /* Look for the optional nested-name-specifier. */
10069 cp_parser_nested_name_specifier_opt (parser,
10070 /*typename_keyword_p=*/false,
10071 /*check_dependency_p=*/true,
10072 /*type_p=*/false,
10073 /*is_declaration=*/true);
10074
10075 return cp_parser_namespace_name (parser);
10076 }
10077
10078 /* Parse a using-declaration.
10079
10080 using-declaration:
10081 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10082 using :: unqualified-id ; */
10083
10084 static void
10085 cp_parser_using_declaration (cp_parser* parser)
10086 {
10087 cp_token *token;
10088 bool typename_p = false;
10089 bool global_scope_p;
10090 tree decl;
10091 tree identifier;
10092 tree qscope;
10093
10094 /* Look for the `using' keyword. */
10095 cp_parser_require_keyword (parser, RID_USING, "`using'");
10096
10097 /* Peek at the next token. */
10098 token = cp_lexer_peek_token (parser->lexer);
10099 /* See if it's `typename'. */
10100 if (token->keyword == RID_TYPENAME)
10101 {
10102 /* Remember that we've seen it. */
10103 typename_p = true;
10104 /* Consume the `typename' token. */
10105 cp_lexer_consume_token (parser->lexer);
10106 }
10107
10108 /* Look for the optional global scope qualification. */
10109 global_scope_p
10110 = (cp_parser_global_scope_opt (parser,
10111 /*current_scope_valid_p=*/false)
10112 != NULL_TREE);
10113
10114 /* If we saw `typename', or didn't see `::', then there must be a
10115 nested-name-specifier present. */
10116 if (typename_p || !global_scope_p)
10117 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10118 /*check_dependency_p=*/true,
10119 /*type_p=*/false,
10120 /*is_declaration=*/true);
10121 /* Otherwise, we could be in either of the two productions. In that
10122 case, treat the nested-name-specifier as optional. */
10123 else
10124 qscope = cp_parser_nested_name_specifier_opt (parser,
10125 /*typename_keyword_p=*/false,
10126 /*check_dependency_p=*/true,
10127 /*type_p=*/false,
10128 /*is_declaration=*/true);
10129 if (!qscope)
10130 qscope = global_namespace;
10131
10132 /* Parse the unqualified-id. */
10133 identifier = cp_parser_unqualified_id (parser,
10134 /*template_keyword_p=*/false,
10135 /*check_dependency_p=*/true,
10136 /*declarator_p=*/true);
10137
10138 /* The function we call to handle a using-declaration is different
10139 depending on what scope we are in. */
10140 if (identifier == error_mark_node)
10141 ;
10142 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10143 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10144 /* [namespace.udecl]
10145
10146 A using declaration shall not name a template-id. */
10147 error ("a template-id may not appear in a using-declaration");
10148 else
10149 {
10150 if (at_class_scope_p ())
10151 {
10152 /* Create the USING_DECL. */
10153 decl = do_class_using_decl (build_nt (SCOPE_REF,
10154 parser->scope,
10155 identifier));
10156 /* Add it to the list of members in this class. */
10157 finish_member_declaration (decl);
10158 }
10159 else
10160 {
10161 decl = cp_parser_lookup_name_simple (parser, identifier);
10162 if (decl == error_mark_node)
10163 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10164 else if (!at_namespace_scope_p ())
10165 do_local_using_decl (decl, qscope, identifier);
10166 else
10167 do_toplevel_using_decl (decl, qscope, identifier);
10168 }
10169 }
10170
10171 /* Look for the final `;'. */
10172 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10173 }
10174
10175 /* Parse a using-directive.
10176
10177 using-directive:
10178 using namespace :: [opt] nested-name-specifier [opt]
10179 namespace-name ; */
10180
10181 static void
10182 cp_parser_using_directive (cp_parser* parser)
10183 {
10184 tree namespace_decl;
10185 tree attribs;
10186
10187 /* Look for the `using' keyword. */
10188 cp_parser_require_keyword (parser, RID_USING, "`using'");
10189 /* And the `namespace' keyword. */
10190 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10191 /* Look for the optional `::' operator. */
10192 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10193 /* And the optional nested-name-specifier. */
10194 cp_parser_nested_name_specifier_opt (parser,
10195 /*typename_keyword_p=*/false,
10196 /*check_dependency_p=*/true,
10197 /*type_p=*/false,
10198 /*is_declaration=*/true);
10199 /* Get the namespace being used. */
10200 namespace_decl = cp_parser_namespace_name (parser);
10201 /* And any specified attributes. */
10202 attribs = cp_parser_attributes_opt (parser);
10203 /* Update the symbol table. */
10204 parse_using_directive (namespace_decl, attribs);
10205 /* Look for the final `;'. */
10206 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10207 }
10208
10209 /* Parse an asm-definition.
10210
10211 asm-definition:
10212 asm ( string-literal ) ;
10213
10214 GNU Extension:
10215
10216 asm-definition:
10217 asm volatile [opt] ( string-literal ) ;
10218 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10219 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10220 : asm-operand-list [opt] ) ;
10221 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10222 : asm-operand-list [opt]
10223 : asm-operand-list [opt] ) ; */
10224
10225 static void
10226 cp_parser_asm_definition (cp_parser* parser)
10227 {
10228 tree string;
10229 tree outputs = NULL_TREE;
10230 tree inputs = NULL_TREE;
10231 tree clobbers = NULL_TREE;
10232 tree asm_stmt;
10233 bool volatile_p = false;
10234 bool extended_p = false;
10235
10236 /* Look for the `asm' keyword. */
10237 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10238 /* See if the next token is `volatile'. */
10239 if (cp_parser_allow_gnu_extensions_p (parser)
10240 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10241 {
10242 /* Remember that we saw the `volatile' keyword. */
10243 volatile_p = true;
10244 /* Consume the token. */
10245 cp_lexer_consume_token (parser->lexer);
10246 }
10247 /* Look for the opening `('. */
10248 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10249 return;
10250 /* Look for the string. */
10251 string = cp_parser_string_literal (parser, false, false);
10252 if (string == error_mark_node)
10253 {
10254 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10255 /*consume_paren=*/true);
10256 return;
10257 }
10258
10259 /* If we're allowing GNU extensions, check for the extended assembly
10260 syntax. Unfortunately, the `:' tokens need not be separated by
10261 a space in C, and so, for compatibility, we tolerate that here
10262 too. Doing that means that we have to treat the `::' operator as
10263 two `:' tokens. */
10264 if (cp_parser_allow_gnu_extensions_p (parser)
10265 && at_function_scope_p ()
10266 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10267 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10268 {
10269 bool inputs_p = false;
10270 bool clobbers_p = false;
10271
10272 /* The extended syntax was used. */
10273 extended_p = true;
10274
10275 /* Look for outputs. */
10276 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10277 {
10278 /* Consume the `:'. */
10279 cp_lexer_consume_token (parser->lexer);
10280 /* Parse the output-operands. */
10281 if (cp_lexer_next_token_is_not (parser->lexer,
10282 CPP_COLON)
10283 && cp_lexer_next_token_is_not (parser->lexer,
10284 CPP_SCOPE)
10285 && cp_lexer_next_token_is_not (parser->lexer,
10286 CPP_CLOSE_PAREN))
10287 outputs = cp_parser_asm_operand_list (parser);
10288 }
10289 /* If the next token is `::', there are no outputs, and the
10290 next token is the beginning of the inputs. */
10291 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10292 /* The inputs are coming next. */
10293 inputs_p = true;
10294
10295 /* Look for inputs. */
10296 if (inputs_p
10297 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10298 {
10299 /* Consume the `:' or `::'. */
10300 cp_lexer_consume_token (parser->lexer);
10301 /* Parse the output-operands. */
10302 if (cp_lexer_next_token_is_not (parser->lexer,
10303 CPP_COLON)
10304 && cp_lexer_next_token_is_not (parser->lexer,
10305 CPP_CLOSE_PAREN))
10306 inputs = cp_parser_asm_operand_list (parser);
10307 }
10308 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10309 /* The clobbers are coming next. */
10310 clobbers_p = true;
10311
10312 /* Look for clobbers. */
10313 if (clobbers_p
10314 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10315 {
10316 /* Consume the `:' or `::'. */
10317 cp_lexer_consume_token (parser->lexer);
10318 /* Parse the clobbers. */
10319 if (cp_lexer_next_token_is_not (parser->lexer,
10320 CPP_CLOSE_PAREN))
10321 clobbers = cp_parser_asm_clobber_list (parser);
10322 }
10323 }
10324 /* Look for the closing `)'. */
10325 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10326 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10327 /*consume_paren=*/true);
10328 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10329
10330 /* Create the ASM_EXPR. */
10331 if (at_function_scope_p ())
10332 {
10333 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10334 inputs, clobbers);
10335 /* If the extended syntax was not used, mark the ASM_EXPR. */
10336 if (!extended_p)
10337 {
10338 tree temp = asm_stmt;
10339 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10340 temp = TREE_OPERAND (temp, 0);
10341
10342 ASM_INPUT_P (temp) = 1;
10343 }
10344 }
10345 else
10346 assemble_asm (string);
10347 }
10348
10349 /* Declarators [gram.dcl.decl] */
10350
10351 /* Parse an init-declarator.
10352
10353 init-declarator:
10354 declarator initializer [opt]
10355
10356 GNU Extension:
10357
10358 init-declarator:
10359 declarator asm-specification [opt] attributes [opt] initializer [opt]
10360
10361 function-definition:
10362 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10363 function-body
10364 decl-specifier-seq [opt] declarator function-try-block
10365
10366 GNU Extension:
10367
10368 function-definition:
10369 __extension__ function-definition
10370
10371 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10372 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10373 then this declarator appears in a class scope. The new DECL created
10374 by this declarator is returned.
10375
10376 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10377 for a function-definition here as well. If the declarator is a
10378 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10379 be TRUE upon return. By that point, the function-definition will
10380 have been completely parsed.
10381
10382 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10383 is FALSE. */
10384
10385 static tree
10386 cp_parser_init_declarator (cp_parser* parser,
10387 cp_decl_specifier_seq *decl_specifiers,
10388 bool function_definition_allowed_p,
10389 bool member_p,
10390 int declares_class_or_enum,
10391 bool* function_definition_p)
10392 {
10393 cp_token *token;
10394 cp_declarator *declarator;
10395 tree prefix_attributes;
10396 tree attributes;
10397 tree asm_specification;
10398 tree initializer;
10399 tree decl = NULL_TREE;
10400 tree scope;
10401 bool is_initialized;
10402 bool is_parenthesized_init;
10403 bool is_non_constant_init;
10404 int ctor_dtor_or_conv_p;
10405 bool friend_p;
10406 bool pop_p = false;
10407
10408 /* Gather the attributes that were provided with the
10409 decl-specifiers. */
10410 prefix_attributes = decl_specifiers->attributes;
10411
10412 /* Assume that this is not the declarator for a function
10413 definition. */
10414 if (function_definition_p)
10415 *function_definition_p = false;
10416
10417 /* Defer access checks while parsing the declarator; we cannot know
10418 what names are accessible until we know what is being
10419 declared. */
10420 resume_deferring_access_checks ();
10421
10422 /* Parse the declarator. */
10423 declarator
10424 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10425 &ctor_dtor_or_conv_p,
10426 /*parenthesized_p=*/NULL,
10427 /*member_p=*/false);
10428 /* Gather up the deferred checks. */
10429 stop_deferring_access_checks ();
10430
10431 /* If the DECLARATOR was erroneous, there's no need to go
10432 further. */
10433 if (declarator == cp_error_declarator)
10434 return error_mark_node;
10435
10436 if (declares_class_or_enum & 2)
10437 cp_parser_check_for_definition_in_return_type (declarator,
10438 decl_specifiers->type);
10439
10440 /* Figure out what scope the entity declared by the DECLARATOR is
10441 located in. `grokdeclarator' sometimes changes the scope, so
10442 we compute it now. */
10443 scope = get_scope_of_declarator (declarator);
10444
10445 /* If we're allowing GNU extensions, look for an asm-specification
10446 and attributes. */
10447 if (cp_parser_allow_gnu_extensions_p (parser))
10448 {
10449 /* Look for an asm-specification. */
10450 asm_specification = cp_parser_asm_specification_opt (parser);
10451 /* And attributes. */
10452 attributes = cp_parser_attributes_opt (parser);
10453 }
10454 else
10455 {
10456 asm_specification = NULL_TREE;
10457 attributes = NULL_TREE;
10458 }
10459
10460 /* Peek at the next token. */
10461 token = cp_lexer_peek_token (parser->lexer);
10462 /* Check to see if the token indicates the start of a
10463 function-definition. */
10464 if (cp_parser_token_starts_function_definition_p (token))
10465 {
10466 if (!function_definition_allowed_p)
10467 {
10468 /* If a function-definition should not appear here, issue an
10469 error message. */
10470 cp_parser_error (parser,
10471 "a function-definition is not allowed here");
10472 return error_mark_node;
10473 }
10474 else
10475 {
10476 /* Neither attributes nor an asm-specification are allowed
10477 on a function-definition. */
10478 if (asm_specification)
10479 error ("an asm-specification is not allowed on a function-definition");
10480 if (attributes)
10481 error ("attributes are not allowed on a function-definition");
10482 /* This is a function-definition. */
10483 *function_definition_p = true;
10484
10485 /* Parse the function definition. */
10486 if (member_p)
10487 decl = cp_parser_save_member_function_body (parser,
10488 decl_specifiers,
10489 declarator,
10490 prefix_attributes);
10491 else
10492 decl
10493 = (cp_parser_function_definition_from_specifiers_and_declarator
10494 (parser, decl_specifiers, prefix_attributes, declarator));
10495
10496 return decl;
10497 }
10498 }
10499
10500 /* [dcl.dcl]
10501
10502 Only in function declarations for constructors, destructors, and
10503 type conversions can the decl-specifier-seq be omitted.
10504
10505 We explicitly postpone this check past the point where we handle
10506 function-definitions because we tolerate function-definitions
10507 that are missing their return types in some modes. */
10508 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10509 {
10510 cp_parser_error (parser,
10511 "expected constructor, destructor, or type conversion");
10512 return error_mark_node;
10513 }
10514
10515 /* An `=' or an `(' indicates an initializer. */
10516 is_initialized = (token->type == CPP_EQ
10517 || token->type == CPP_OPEN_PAREN);
10518 /* If the init-declarator isn't initialized and isn't followed by a
10519 `,' or `;', it's not a valid init-declarator. */
10520 if (!is_initialized
10521 && token->type != CPP_COMMA
10522 && token->type != CPP_SEMICOLON)
10523 {
10524 cp_parser_error (parser, "expected initializer");
10525 return error_mark_node;
10526 }
10527
10528 /* Because start_decl has side-effects, we should only call it if we
10529 know we're going ahead. By this point, we know that we cannot
10530 possibly be looking at any other construct. */
10531 cp_parser_commit_to_tentative_parse (parser);
10532
10533 /* If the decl specifiers were bad, issue an error now that we're
10534 sure this was intended to be a declarator. Then continue
10535 declaring the variable(s), as int, to try to cut down on further
10536 errors. */
10537 if (decl_specifiers->any_specifiers_p
10538 && decl_specifiers->type == error_mark_node)
10539 {
10540 cp_parser_error (parser, "invalid type in declaration");
10541 decl_specifiers->type = integer_type_node;
10542 }
10543
10544 /* Check to see whether or not this declaration is a friend. */
10545 friend_p = cp_parser_friend_p (decl_specifiers);
10546
10547 /* Check that the number of template-parameter-lists is OK. */
10548 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10549 return error_mark_node;
10550
10551 /* Enter the newly declared entry in the symbol table. If we're
10552 processing a declaration in a class-specifier, we wait until
10553 after processing the initializer. */
10554 if (!member_p)
10555 {
10556 if (parser->in_unbraced_linkage_specification_p)
10557 {
10558 decl_specifiers->storage_class = sc_extern;
10559 have_extern_spec = false;
10560 }
10561 decl = start_decl (declarator, decl_specifiers,
10562 is_initialized, attributes, prefix_attributes,
10563 &pop_p);
10564 }
10565 else if (scope)
10566 /* Enter the SCOPE. That way unqualified names appearing in the
10567 initializer will be looked up in SCOPE. */
10568 pop_p = push_scope (scope);
10569
10570 /* Perform deferred access control checks, now that we know in which
10571 SCOPE the declared entity resides. */
10572 if (!member_p && decl)
10573 {
10574 tree saved_current_function_decl = NULL_TREE;
10575
10576 /* If the entity being declared is a function, pretend that we
10577 are in its scope. If it is a `friend', it may have access to
10578 things that would not otherwise be accessible. */
10579 if (TREE_CODE (decl) == FUNCTION_DECL)
10580 {
10581 saved_current_function_decl = current_function_decl;
10582 current_function_decl = decl;
10583 }
10584
10585 /* Perform the access control checks for the declarator and the
10586 the decl-specifiers. */
10587 perform_deferred_access_checks ();
10588
10589 /* Restore the saved value. */
10590 if (TREE_CODE (decl) == FUNCTION_DECL)
10591 current_function_decl = saved_current_function_decl;
10592 }
10593
10594 /* Parse the initializer. */
10595 if (is_initialized)
10596 initializer = cp_parser_initializer (parser,
10597 &is_parenthesized_init,
10598 &is_non_constant_init);
10599 else
10600 {
10601 initializer = NULL_TREE;
10602 is_parenthesized_init = false;
10603 is_non_constant_init = true;
10604 }
10605
10606 /* The old parser allows attributes to appear after a parenthesized
10607 initializer. Mark Mitchell proposed removing this functionality
10608 on the GCC mailing lists on 2002-08-13. This parser accepts the
10609 attributes -- but ignores them. */
10610 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10611 if (cp_parser_attributes_opt (parser))
10612 warning ("attributes after parenthesized initializer ignored");
10613
10614 /* For an in-class declaration, use `grokfield' to create the
10615 declaration. */
10616 if (member_p)
10617 {
10618 if (pop_p)
10619 {
10620 pop_scope (scope);
10621 pop_p = false;
10622 }
10623 decl = grokfield (declarator, decl_specifiers,
10624 initializer, /*asmspec=*/NULL_TREE,
10625 /*attributes=*/NULL_TREE);
10626 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10627 cp_parser_save_default_args (parser, decl);
10628 }
10629
10630 /* Finish processing the declaration. But, skip friend
10631 declarations. */
10632 if (!friend_p && decl && decl != error_mark_node)
10633 {
10634 cp_finish_decl (decl,
10635 initializer,
10636 asm_specification,
10637 /* If the initializer is in parentheses, then this is
10638 a direct-initialization, which means that an
10639 `explicit' constructor is OK. Otherwise, an
10640 `explicit' constructor cannot be used. */
10641 ((is_parenthesized_init || !is_initialized)
10642 ? 0 : LOOKUP_ONLYCONVERTING));
10643 if (pop_p)
10644 pop_scope (DECL_CONTEXT (decl));
10645 }
10646
10647 /* Remember whether or not variables were initialized by
10648 constant-expressions. */
10649 if (decl && TREE_CODE (decl) == VAR_DECL
10650 && is_initialized && !is_non_constant_init)
10651 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10652
10653 return decl;
10654 }
10655
10656 /* Parse a declarator.
10657
10658 declarator:
10659 direct-declarator
10660 ptr-operator declarator
10661
10662 abstract-declarator:
10663 ptr-operator abstract-declarator [opt]
10664 direct-abstract-declarator
10665
10666 GNU Extensions:
10667
10668 declarator:
10669 attributes [opt] direct-declarator
10670 attributes [opt] ptr-operator declarator
10671
10672 abstract-declarator:
10673 attributes [opt] ptr-operator abstract-declarator [opt]
10674 attributes [opt] direct-abstract-declarator
10675
10676 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10677 detect constructor, destructor or conversion operators. It is set
10678 to -1 if the declarator is a name, and +1 if it is a
10679 function. Otherwise it is set to zero. Usually you just want to
10680 test for >0, but internally the negative value is used.
10681
10682 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10683 a decl-specifier-seq unless it declares a constructor, destructor,
10684 or conversion. It might seem that we could check this condition in
10685 semantic analysis, rather than parsing, but that makes it difficult
10686 to handle something like `f()'. We want to notice that there are
10687 no decl-specifiers, and therefore realize that this is an
10688 expression, not a declaration.)
10689
10690 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10691 the declarator is a direct-declarator of the form "(...)".
10692
10693 MEMBER_P is true iff this declarator is a member-declarator. */
10694
10695 static cp_declarator *
10696 cp_parser_declarator (cp_parser* parser,
10697 cp_parser_declarator_kind dcl_kind,
10698 int* ctor_dtor_or_conv_p,
10699 bool* parenthesized_p,
10700 bool member_p)
10701 {
10702 cp_token *token;
10703 cp_declarator *declarator;
10704 enum tree_code code;
10705 cp_cv_quals cv_quals;
10706 tree class_type;
10707 tree attributes = NULL_TREE;
10708
10709 /* Assume this is not a constructor, destructor, or type-conversion
10710 operator. */
10711 if (ctor_dtor_or_conv_p)
10712 *ctor_dtor_or_conv_p = 0;
10713
10714 if (cp_parser_allow_gnu_extensions_p (parser))
10715 attributes = cp_parser_attributes_opt (parser);
10716
10717 /* Peek at the next token. */
10718 token = cp_lexer_peek_token (parser->lexer);
10719
10720 /* Check for the ptr-operator production. */
10721 cp_parser_parse_tentatively (parser);
10722 /* Parse the ptr-operator. */
10723 code = cp_parser_ptr_operator (parser,
10724 &class_type,
10725 &cv_quals);
10726 /* If that worked, then we have a ptr-operator. */
10727 if (cp_parser_parse_definitely (parser))
10728 {
10729 /* If a ptr-operator was found, then this declarator was not
10730 parenthesized. */
10731 if (parenthesized_p)
10732 *parenthesized_p = true;
10733 /* The dependent declarator is optional if we are parsing an
10734 abstract-declarator. */
10735 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10736 cp_parser_parse_tentatively (parser);
10737
10738 /* Parse the dependent declarator. */
10739 declarator = cp_parser_declarator (parser, dcl_kind,
10740 /*ctor_dtor_or_conv_p=*/NULL,
10741 /*parenthesized_p=*/NULL,
10742 /*member_p=*/false);
10743
10744 /* If we are parsing an abstract-declarator, we must handle the
10745 case where the dependent declarator is absent. */
10746 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10747 && !cp_parser_parse_definitely (parser))
10748 declarator = NULL;
10749
10750 /* Build the representation of the ptr-operator. */
10751 if (class_type)
10752 declarator = make_ptrmem_declarator (cv_quals,
10753 class_type,
10754 declarator);
10755 else if (code == INDIRECT_REF)
10756 declarator = make_pointer_declarator (cv_quals, declarator);
10757 else
10758 declarator = make_reference_declarator (cv_quals, declarator);
10759 }
10760 /* Everything else is a direct-declarator. */
10761 else
10762 {
10763 if (parenthesized_p)
10764 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10765 CPP_OPEN_PAREN);
10766 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10767 ctor_dtor_or_conv_p,
10768 member_p);
10769 }
10770
10771 if (attributes && declarator != cp_error_declarator)
10772 declarator->attributes = attributes;
10773
10774 return declarator;
10775 }
10776
10777 /* Parse a direct-declarator or direct-abstract-declarator.
10778
10779 direct-declarator:
10780 declarator-id
10781 direct-declarator ( parameter-declaration-clause )
10782 cv-qualifier-seq [opt]
10783 exception-specification [opt]
10784 direct-declarator [ constant-expression [opt] ]
10785 ( declarator )
10786
10787 direct-abstract-declarator:
10788 direct-abstract-declarator [opt]
10789 ( parameter-declaration-clause )
10790 cv-qualifier-seq [opt]
10791 exception-specification [opt]
10792 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10793 ( abstract-declarator )
10794
10795 Returns a representation of the declarator. DCL_KIND is
10796 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10797 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10798 we are parsing a direct-declarator. It is
10799 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10800 of ambiguity we prefer an abstract declarator, as per
10801 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10802 cp_parser_declarator. */
10803
10804 static cp_declarator *
10805 cp_parser_direct_declarator (cp_parser* parser,
10806 cp_parser_declarator_kind dcl_kind,
10807 int* ctor_dtor_or_conv_p,
10808 bool member_p)
10809 {
10810 cp_token *token;
10811 cp_declarator *declarator = NULL;
10812 tree scope = NULL_TREE;
10813 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10814 bool saved_in_declarator_p = parser->in_declarator_p;
10815 bool first = true;
10816 bool pop_p = false;
10817
10818 while (true)
10819 {
10820 /* Peek at the next token. */
10821 token = cp_lexer_peek_token (parser->lexer);
10822 if (token->type == CPP_OPEN_PAREN)
10823 {
10824 /* This is either a parameter-declaration-clause, or a
10825 parenthesized declarator. When we know we are parsing a
10826 named declarator, it must be a parenthesized declarator
10827 if FIRST is true. For instance, `(int)' is a
10828 parameter-declaration-clause, with an omitted
10829 direct-abstract-declarator. But `((*))', is a
10830 parenthesized abstract declarator. Finally, when T is a
10831 template parameter `(T)' is a
10832 parameter-declaration-clause, and not a parenthesized
10833 named declarator.
10834
10835 We first try and parse a parameter-declaration-clause,
10836 and then try a nested declarator (if FIRST is true).
10837
10838 It is not an error for it not to be a
10839 parameter-declaration-clause, even when FIRST is
10840 false. Consider,
10841
10842 int i (int);
10843 int i (3);
10844
10845 The first is the declaration of a function while the
10846 second is a the definition of a variable, including its
10847 initializer.
10848
10849 Having seen only the parenthesis, we cannot know which of
10850 these two alternatives should be selected. Even more
10851 complex are examples like:
10852
10853 int i (int (a));
10854 int i (int (3));
10855
10856 The former is a function-declaration; the latter is a
10857 variable initialization.
10858
10859 Thus again, we try a parameter-declaration-clause, and if
10860 that fails, we back out and return. */
10861
10862 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10863 {
10864 cp_parameter_declarator *params;
10865 unsigned saved_num_template_parameter_lists;
10866
10867 /* In a member-declarator, the only valid interpretation
10868 of a parenthesis is the start of a
10869 parameter-declaration-clause. (It is invalid to
10870 initialize a static data member with a parenthesized
10871 initializer; only the "=" form of initialization is
10872 permitted.) */
10873 if (!member_p)
10874 cp_parser_parse_tentatively (parser);
10875
10876 /* Consume the `('. */
10877 cp_lexer_consume_token (parser->lexer);
10878 if (first)
10879 {
10880 /* If this is going to be an abstract declarator, we're
10881 in a declarator and we can't have default args. */
10882 parser->default_arg_ok_p = false;
10883 parser->in_declarator_p = true;
10884 }
10885
10886 /* Inside the function parameter list, surrounding
10887 template-parameter-lists do not apply. */
10888 saved_num_template_parameter_lists
10889 = parser->num_template_parameter_lists;
10890 parser->num_template_parameter_lists = 0;
10891
10892 /* Parse the parameter-declaration-clause. */
10893 params = cp_parser_parameter_declaration_clause (parser);
10894
10895 parser->num_template_parameter_lists
10896 = saved_num_template_parameter_lists;
10897
10898 /* If all went well, parse the cv-qualifier-seq and the
10899 exception-specification. */
10900 if (member_p || cp_parser_parse_definitely (parser))
10901 {
10902 cp_cv_quals cv_quals;
10903 tree exception_specification;
10904
10905 if (ctor_dtor_or_conv_p)
10906 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10907 first = false;
10908 /* Consume the `)'. */
10909 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10910
10911 /* Parse the cv-qualifier-seq. */
10912 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10913 /* And the exception-specification. */
10914 exception_specification
10915 = cp_parser_exception_specification_opt (parser);
10916
10917 /* Create the function-declarator. */
10918 declarator = make_call_declarator (declarator,
10919 params,
10920 cv_quals,
10921 exception_specification);
10922 /* Any subsequent parameter lists are to do with
10923 return type, so are not those of the declared
10924 function. */
10925 parser->default_arg_ok_p = false;
10926
10927 /* Repeat the main loop. */
10928 continue;
10929 }
10930 }
10931
10932 /* If this is the first, we can try a parenthesized
10933 declarator. */
10934 if (first)
10935 {
10936 bool saved_in_type_id_in_expr_p;
10937
10938 parser->default_arg_ok_p = saved_default_arg_ok_p;
10939 parser->in_declarator_p = saved_in_declarator_p;
10940
10941 /* Consume the `('. */
10942 cp_lexer_consume_token (parser->lexer);
10943 /* Parse the nested declarator. */
10944 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10945 parser->in_type_id_in_expr_p = true;
10946 declarator
10947 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10948 /*parenthesized_p=*/NULL,
10949 member_p);
10950 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10951 first = false;
10952 /* Expect a `)'. */
10953 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10954 declarator = cp_error_declarator;
10955 if (declarator == cp_error_declarator)
10956 break;
10957
10958 goto handle_declarator;
10959 }
10960 /* Otherwise, we must be done. */
10961 else
10962 break;
10963 }
10964 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10965 && token->type == CPP_OPEN_SQUARE)
10966 {
10967 /* Parse an array-declarator. */
10968 tree bounds;
10969
10970 if (ctor_dtor_or_conv_p)
10971 *ctor_dtor_or_conv_p = 0;
10972
10973 first = false;
10974 parser->default_arg_ok_p = false;
10975 parser->in_declarator_p = true;
10976 /* Consume the `['. */
10977 cp_lexer_consume_token (parser->lexer);
10978 /* Peek at the next token. */
10979 token = cp_lexer_peek_token (parser->lexer);
10980 /* If the next token is `]', then there is no
10981 constant-expression. */
10982 if (token->type != CPP_CLOSE_SQUARE)
10983 {
10984 bool non_constant_p;
10985
10986 bounds
10987 = cp_parser_constant_expression (parser,
10988 /*allow_non_constant=*/true,
10989 &non_constant_p);
10990 if (!non_constant_p)
10991 bounds = fold_non_dependent_expr (bounds);
10992 else if (!at_function_scope_p ())
10993 {
10994 error ("array bound is not an integer constant");
10995 bounds = error_mark_node;
10996 }
10997 }
10998 else
10999 bounds = NULL_TREE;
11000 /* Look for the closing `]'. */
11001 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11002 {
11003 declarator = cp_error_declarator;
11004 break;
11005 }
11006
11007 declarator = make_array_declarator (declarator, bounds);
11008 }
11009 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11010 {
11011 tree id;
11012
11013 /* Parse a declarator-id */
11014 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11015 cp_parser_parse_tentatively (parser);
11016 id = cp_parser_declarator_id (parser);
11017 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11018 {
11019 if (!cp_parser_parse_definitely (parser))
11020 id = error_mark_node;
11021 else if (TREE_CODE (id) != IDENTIFIER_NODE)
11022 {
11023 cp_parser_error (parser, "expected unqualified-id");
11024 id = error_mark_node;
11025 }
11026 }
11027
11028 if (id == error_mark_node)
11029 {
11030 declarator = cp_error_declarator;
11031 break;
11032 }
11033
11034 if (TREE_CODE (id) == SCOPE_REF && at_namespace_scope_p ())
11035 {
11036 tree scope = TREE_OPERAND (id, 0);
11037
11038 /* In the declaration of a member of a template class
11039 outside of the class itself, the SCOPE will sometimes
11040 be a TYPENAME_TYPE. For example, given:
11041
11042 template <typename T>
11043 int S<T>::R::i = 3;
11044
11045 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11046 this context, we must resolve S<T>::R to an ordinary
11047 type, rather than a typename type.
11048
11049 The reason we normally avoid resolving TYPENAME_TYPEs
11050 is that a specialization of `S' might render
11051 `S<T>::R' not a type. However, if `S' is
11052 specialized, then this `i' will not be used, so there
11053 is no harm in resolving the types here. */
11054 if (TREE_CODE (scope) == TYPENAME_TYPE)
11055 {
11056 tree type;
11057
11058 /* Resolve the TYPENAME_TYPE. */
11059 type = resolve_typename_type (scope,
11060 /*only_current_p=*/false);
11061 /* If that failed, the declarator is invalid. */
11062 if (type == error_mark_node)
11063 error ("%<%T::%D%> is not a type",
11064 TYPE_CONTEXT (scope),
11065 TYPE_IDENTIFIER (scope));
11066 /* Build a new DECLARATOR. */
11067 id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11068 }
11069 }
11070
11071 declarator = make_id_declarator (id);
11072 if (id)
11073 {
11074 tree class_type;
11075 tree unqualified_name;
11076
11077 if (TREE_CODE (id) == SCOPE_REF
11078 && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11079 {
11080 class_type = TREE_OPERAND (id, 0);
11081 unqualified_name = TREE_OPERAND (id, 1);
11082 }
11083 else
11084 {
11085 class_type = current_class_type;
11086 unqualified_name = id;
11087 }
11088
11089 if (class_type)
11090 {
11091 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11092 declarator->u.id.sfk = sfk_destructor;
11093 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11094 declarator->u.id.sfk = sfk_conversion;
11095 else if (constructor_name_p (unqualified_name,
11096 class_type)
11097 || (TREE_CODE (unqualified_name) == TYPE_DECL
11098 && same_type_p (TREE_TYPE (unqualified_name),
11099 class_type)))
11100 declarator->u.id.sfk = sfk_constructor;
11101
11102 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11103 *ctor_dtor_or_conv_p = -1;
11104 if (TREE_CODE (id) == SCOPE_REF
11105 && TREE_CODE (unqualified_name) == TYPE_DECL
11106 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11107 {
11108 error ("invalid use of constructor as a template");
11109 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11110 "the constructor in a qualified name",
11111 class_type,
11112 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11113 class_type, class_type);
11114 }
11115 }
11116 }
11117
11118 handle_declarator:;
11119 scope = get_scope_of_declarator (declarator);
11120 if (scope)
11121 /* Any names that appear after the declarator-id for a
11122 member are looked up in the containing scope. */
11123 pop_p = push_scope (scope);
11124 parser->in_declarator_p = true;
11125 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11126 || (declarator && declarator->kind == cdk_id))
11127 /* Default args are only allowed on function
11128 declarations. */
11129 parser->default_arg_ok_p = saved_default_arg_ok_p;
11130 else
11131 parser->default_arg_ok_p = false;
11132
11133 first = false;
11134 }
11135 /* We're done. */
11136 else
11137 break;
11138 }
11139
11140 /* For an abstract declarator, we might wind up with nothing at this
11141 point. That's an error; the declarator is not optional. */
11142 if (!declarator)
11143 cp_parser_error (parser, "expected declarator");
11144
11145 /* If we entered a scope, we must exit it now. */
11146 if (pop_p)
11147 pop_scope (scope);
11148
11149 parser->default_arg_ok_p = saved_default_arg_ok_p;
11150 parser->in_declarator_p = saved_in_declarator_p;
11151
11152 return declarator;
11153 }
11154
11155 /* Parse a ptr-operator.
11156
11157 ptr-operator:
11158 * cv-qualifier-seq [opt]
11159 &
11160 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11161
11162 GNU Extension:
11163
11164 ptr-operator:
11165 & cv-qualifier-seq [opt]
11166
11167 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11168 Returns ADDR_EXPR if a reference was used. In the case of a
11169 pointer-to-member, *TYPE is filled in with the TYPE containing the
11170 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11171 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11172 ERROR_MARK if an error occurred. */
11173
11174 static enum tree_code
11175 cp_parser_ptr_operator (cp_parser* parser,
11176 tree* type,
11177 cp_cv_quals *cv_quals)
11178 {
11179 enum tree_code code = ERROR_MARK;
11180 cp_token *token;
11181
11182 /* Assume that it's not a pointer-to-member. */
11183 *type = NULL_TREE;
11184 /* And that there are no cv-qualifiers. */
11185 *cv_quals = TYPE_UNQUALIFIED;
11186
11187 /* Peek at the next token. */
11188 token = cp_lexer_peek_token (parser->lexer);
11189 /* If it's a `*' or `&' we have a pointer or reference. */
11190 if (token->type == CPP_MULT || token->type == CPP_AND)
11191 {
11192 /* Remember which ptr-operator we were processing. */
11193 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11194
11195 /* Consume the `*' or `&'. */
11196 cp_lexer_consume_token (parser->lexer);
11197
11198 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11199 `&', if we are allowing GNU extensions. (The only qualifier
11200 that can legally appear after `&' is `restrict', but that is
11201 enforced during semantic analysis. */
11202 if (code == INDIRECT_REF
11203 || cp_parser_allow_gnu_extensions_p (parser))
11204 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11205 }
11206 else
11207 {
11208 /* Try the pointer-to-member case. */
11209 cp_parser_parse_tentatively (parser);
11210 /* Look for the optional `::' operator. */
11211 cp_parser_global_scope_opt (parser,
11212 /*current_scope_valid_p=*/false);
11213 /* Look for the nested-name specifier. */
11214 cp_parser_nested_name_specifier (parser,
11215 /*typename_keyword_p=*/false,
11216 /*check_dependency_p=*/true,
11217 /*type_p=*/false,
11218 /*is_declaration=*/false);
11219 /* If we found it, and the next token is a `*', then we are
11220 indeed looking at a pointer-to-member operator. */
11221 if (!cp_parser_error_occurred (parser)
11222 && cp_parser_require (parser, CPP_MULT, "`*'"))
11223 {
11224 /* The type of which the member is a member is given by the
11225 current SCOPE. */
11226 *type = parser->scope;
11227 /* The next name will not be qualified. */
11228 parser->scope = NULL_TREE;
11229 parser->qualifying_scope = NULL_TREE;
11230 parser->object_scope = NULL_TREE;
11231 /* Indicate that the `*' operator was used. */
11232 code = INDIRECT_REF;
11233 /* Look for the optional cv-qualifier-seq. */
11234 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11235 }
11236 /* If that didn't work we don't have a ptr-operator. */
11237 if (!cp_parser_parse_definitely (parser))
11238 cp_parser_error (parser, "expected ptr-operator");
11239 }
11240
11241 return code;
11242 }
11243
11244 /* Parse an (optional) cv-qualifier-seq.
11245
11246 cv-qualifier-seq:
11247 cv-qualifier cv-qualifier-seq [opt]
11248
11249 cv-qualifier:
11250 const
11251 volatile
11252
11253 GNU Extension:
11254
11255 cv-qualifier:
11256 __restrict__
11257
11258 Returns a bitmask representing the cv-qualifiers. */
11259
11260 static cp_cv_quals
11261 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11262 {
11263 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11264
11265 while (true)
11266 {
11267 cp_token *token;
11268 cp_cv_quals cv_qualifier;
11269
11270 /* Peek at the next token. */
11271 token = cp_lexer_peek_token (parser->lexer);
11272 /* See if it's a cv-qualifier. */
11273 switch (token->keyword)
11274 {
11275 case RID_CONST:
11276 cv_qualifier = TYPE_QUAL_CONST;
11277 break;
11278
11279 case RID_VOLATILE:
11280 cv_qualifier = TYPE_QUAL_VOLATILE;
11281 break;
11282
11283 case RID_RESTRICT:
11284 cv_qualifier = TYPE_QUAL_RESTRICT;
11285 break;
11286
11287 default:
11288 cv_qualifier = TYPE_UNQUALIFIED;
11289 break;
11290 }
11291
11292 if (!cv_qualifier)
11293 break;
11294
11295 if (cv_quals & cv_qualifier)
11296 {
11297 error ("duplicate cv-qualifier");
11298 cp_lexer_purge_token (parser->lexer);
11299 }
11300 else
11301 {
11302 cp_lexer_consume_token (parser->lexer);
11303 cv_quals |= cv_qualifier;
11304 }
11305 }
11306
11307 return cv_quals;
11308 }
11309
11310 /* Parse a declarator-id.
11311
11312 declarator-id:
11313 id-expression
11314 :: [opt] nested-name-specifier [opt] type-name
11315
11316 In the `id-expression' case, the value returned is as for
11317 cp_parser_id_expression if the id-expression was an unqualified-id.
11318 If the id-expression was a qualified-id, then a SCOPE_REF is
11319 returned. The first operand is the scope (either a NAMESPACE_DECL
11320 or TREE_TYPE), but the second is still just a representation of an
11321 unqualified-id. */
11322
11323 static tree
11324 cp_parser_declarator_id (cp_parser* parser)
11325 {
11326 tree id_expression;
11327
11328 /* The expression must be an id-expression. Assume that qualified
11329 names are the names of types so that:
11330
11331 template <class T>
11332 int S<T>::R::i = 3;
11333
11334 will work; we must treat `S<T>::R' as the name of a type.
11335 Similarly, assume that qualified names are templates, where
11336 required, so that:
11337
11338 template <class T>
11339 int S<T>::R<T>::i = 3;
11340
11341 will work, too. */
11342 id_expression = cp_parser_id_expression (parser,
11343 /*template_keyword_p=*/false,
11344 /*check_dependency_p=*/false,
11345 /*template_p=*/NULL,
11346 /*declarator_p=*/true);
11347 /* If the name was qualified, create a SCOPE_REF to represent
11348 that. */
11349 if (parser->scope)
11350 {
11351 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11352 parser->scope = NULL_TREE;
11353 }
11354
11355 return id_expression;
11356 }
11357
11358 /* Parse a type-id.
11359
11360 type-id:
11361 type-specifier-seq abstract-declarator [opt]
11362
11363 Returns the TYPE specified. */
11364
11365 static tree
11366 cp_parser_type_id (cp_parser* parser)
11367 {
11368 cp_decl_specifier_seq type_specifier_seq;
11369 cp_declarator *abstract_declarator;
11370
11371 /* Parse the type-specifier-seq. */
11372 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11373 if (type_specifier_seq.type == error_mark_node)
11374 return error_mark_node;
11375
11376 /* There might or might not be an abstract declarator. */
11377 cp_parser_parse_tentatively (parser);
11378 /* Look for the declarator. */
11379 abstract_declarator
11380 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11381 /*parenthesized_p=*/NULL,
11382 /*member_p=*/false);
11383 /* Check to see if there really was a declarator. */
11384 if (!cp_parser_parse_definitely (parser))
11385 abstract_declarator = NULL;
11386
11387 return groktypename (&type_specifier_seq, abstract_declarator);
11388 }
11389
11390 /* Parse a type-specifier-seq.
11391
11392 type-specifier-seq:
11393 type-specifier type-specifier-seq [opt]
11394
11395 GNU extension:
11396
11397 type-specifier-seq:
11398 attributes type-specifier-seq [opt]
11399
11400 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11401
11402 static void
11403 cp_parser_type_specifier_seq (cp_parser* parser,
11404 cp_decl_specifier_seq *type_specifier_seq)
11405 {
11406 bool seen_type_specifier = false;
11407
11408 /* Clear the TYPE_SPECIFIER_SEQ. */
11409 clear_decl_specs (type_specifier_seq);
11410
11411 /* Parse the type-specifiers and attributes. */
11412 while (true)
11413 {
11414 tree type_specifier;
11415
11416 /* Check for attributes first. */
11417 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11418 {
11419 type_specifier_seq->attributes =
11420 chainon (type_specifier_seq->attributes,
11421 cp_parser_attributes_opt (parser));
11422 continue;
11423 }
11424
11425 /* Look for the type-specifier. */
11426 type_specifier = cp_parser_type_specifier (parser,
11427 CP_PARSER_FLAGS_OPTIONAL,
11428 type_specifier_seq,
11429 /*is_declaration=*/false,
11430 NULL,
11431 NULL);
11432 /* If the first type-specifier could not be found, this is not a
11433 type-specifier-seq at all. */
11434 if (!seen_type_specifier && !type_specifier)
11435 {
11436 cp_parser_error (parser, "expected type-specifier");
11437 type_specifier_seq->type = error_mark_node;
11438 return;
11439 }
11440 /* If subsequent type-specifiers could not be found, the
11441 type-specifier-seq is complete. */
11442 else if (seen_type_specifier && !type_specifier)
11443 break;
11444
11445 seen_type_specifier = true;
11446 }
11447
11448 return;
11449 }
11450
11451 /* Parse a parameter-declaration-clause.
11452
11453 parameter-declaration-clause:
11454 parameter-declaration-list [opt] ... [opt]
11455 parameter-declaration-list , ...
11456
11457 Returns a representation for the parameter declarations. A return
11458 value of NULL indicates a parameter-declaration-clause consisting
11459 only of an ellipsis. */
11460
11461 static cp_parameter_declarator *
11462 cp_parser_parameter_declaration_clause (cp_parser* parser)
11463 {
11464 cp_parameter_declarator *parameters;
11465 cp_token *token;
11466 bool ellipsis_p;
11467 bool is_error;
11468
11469 /* Peek at the next token. */
11470 token = cp_lexer_peek_token (parser->lexer);
11471 /* Check for trivial parameter-declaration-clauses. */
11472 if (token->type == CPP_ELLIPSIS)
11473 {
11474 /* Consume the `...' token. */
11475 cp_lexer_consume_token (parser->lexer);
11476 return NULL;
11477 }
11478 else if (token->type == CPP_CLOSE_PAREN)
11479 /* There are no parameters. */
11480 {
11481 #ifndef NO_IMPLICIT_EXTERN_C
11482 if (in_system_header && current_class_type == NULL
11483 && current_lang_name == lang_name_c)
11484 return NULL;
11485 else
11486 #endif
11487 return no_parameters;
11488 }
11489 /* Check for `(void)', too, which is a special case. */
11490 else if (token->keyword == RID_VOID
11491 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11492 == CPP_CLOSE_PAREN))
11493 {
11494 /* Consume the `void' token. */
11495 cp_lexer_consume_token (parser->lexer);
11496 /* There are no parameters. */
11497 return no_parameters;
11498 }
11499
11500 /* Parse the parameter-declaration-list. */
11501 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11502 /* If a parse error occurred while parsing the
11503 parameter-declaration-list, then the entire
11504 parameter-declaration-clause is erroneous. */
11505 if (is_error)
11506 return NULL;
11507
11508 /* Peek at the next token. */
11509 token = cp_lexer_peek_token (parser->lexer);
11510 /* If it's a `,', the clause should terminate with an ellipsis. */
11511 if (token->type == CPP_COMMA)
11512 {
11513 /* Consume the `,'. */
11514 cp_lexer_consume_token (parser->lexer);
11515 /* Expect an ellipsis. */
11516 ellipsis_p
11517 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11518 }
11519 /* It might also be `...' if the optional trailing `,' was
11520 omitted. */
11521 else if (token->type == CPP_ELLIPSIS)
11522 {
11523 /* Consume the `...' token. */
11524 cp_lexer_consume_token (parser->lexer);
11525 /* And remember that we saw it. */
11526 ellipsis_p = true;
11527 }
11528 else
11529 ellipsis_p = false;
11530
11531 /* Finish the parameter list. */
11532 if (parameters && ellipsis_p)
11533 parameters->ellipsis_p = true;
11534
11535 return parameters;
11536 }
11537
11538 /* Parse a parameter-declaration-list.
11539
11540 parameter-declaration-list:
11541 parameter-declaration
11542 parameter-declaration-list , parameter-declaration
11543
11544 Returns a representation of the parameter-declaration-list, as for
11545 cp_parser_parameter_declaration_clause. However, the
11546 `void_list_node' is never appended to the list. Upon return,
11547 *IS_ERROR will be true iff an error occurred. */
11548
11549 static cp_parameter_declarator *
11550 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11551 {
11552 cp_parameter_declarator *parameters = NULL;
11553 cp_parameter_declarator **tail = &parameters;
11554
11555 /* Assume all will go well. */
11556 *is_error = false;
11557
11558 /* Look for more parameters. */
11559 while (true)
11560 {
11561 cp_parameter_declarator *parameter;
11562 bool parenthesized_p;
11563 /* Parse the parameter. */
11564 parameter
11565 = cp_parser_parameter_declaration (parser,
11566 /*template_parm_p=*/false,
11567 &parenthesized_p);
11568
11569 /* If a parse error occurred parsing the parameter declaration,
11570 then the entire parameter-declaration-list is erroneous. */
11571 if (!parameter)
11572 {
11573 *is_error = true;
11574 parameters = NULL;
11575 break;
11576 }
11577 /* Add the new parameter to the list. */
11578 *tail = parameter;
11579 tail = &parameter->next;
11580
11581 /* Peek at the next token. */
11582 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11583 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11584 /* The parameter-declaration-list is complete. */
11585 break;
11586 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11587 {
11588 cp_token *token;
11589
11590 /* Peek at the next token. */
11591 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11592 /* If it's an ellipsis, then the list is complete. */
11593 if (token->type == CPP_ELLIPSIS)
11594 break;
11595 /* Otherwise, there must be more parameters. Consume the
11596 `,'. */
11597 cp_lexer_consume_token (parser->lexer);
11598 /* When parsing something like:
11599
11600 int i(float f, double d)
11601
11602 we can tell after seeing the declaration for "f" that we
11603 are not looking at an initialization of a variable "i",
11604 but rather at the declaration of a function "i".
11605
11606 Due to the fact that the parsing of template arguments
11607 (as specified to a template-id) requires backtracking we
11608 cannot use this technique when inside a template argument
11609 list. */
11610 if (!parser->in_template_argument_list_p
11611 && !parser->in_type_id_in_expr_p
11612 && cp_parser_parsing_tentatively (parser)
11613 && !cp_parser_committed_to_tentative_parse (parser)
11614 /* However, a parameter-declaration of the form
11615 "foat(f)" (which is a valid declaration of a
11616 parameter "f") can also be interpreted as an
11617 expression (the conversion of "f" to "float"). */
11618 && !parenthesized_p)
11619 cp_parser_commit_to_tentative_parse (parser);
11620 }
11621 else
11622 {
11623 cp_parser_error (parser, "expected %<,%> or %<...%>");
11624 if (!cp_parser_parsing_tentatively (parser)
11625 || cp_parser_committed_to_tentative_parse (parser))
11626 cp_parser_skip_to_closing_parenthesis (parser,
11627 /*recovering=*/true,
11628 /*or_comma=*/false,
11629 /*consume_paren=*/false);
11630 break;
11631 }
11632 }
11633
11634 return parameters;
11635 }
11636
11637 /* Parse a parameter declaration.
11638
11639 parameter-declaration:
11640 decl-specifier-seq declarator
11641 decl-specifier-seq declarator = assignment-expression
11642 decl-specifier-seq abstract-declarator [opt]
11643 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11644
11645 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11646 declares a template parameter. (In that case, a non-nested `>'
11647 token encountered during the parsing of the assignment-expression
11648 is not interpreted as a greater-than operator.)
11649
11650 Returns a representation of the parameter, or NULL if an error
11651 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11652 true iff the declarator is of the form "(p)". */
11653
11654 static cp_parameter_declarator *
11655 cp_parser_parameter_declaration (cp_parser *parser,
11656 bool template_parm_p,
11657 bool *parenthesized_p)
11658 {
11659 int declares_class_or_enum;
11660 bool greater_than_is_operator_p;
11661 cp_decl_specifier_seq decl_specifiers;
11662 cp_declarator *declarator;
11663 tree default_argument;
11664 cp_token *token;
11665 const char *saved_message;
11666
11667 /* In a template parameter, `>' is not an operator.
11668
11669 [temp.param]
11670
11671 When parsing a default template-argument for a non-type
11672 template-parameter, the first non-nested `>' is taken as the end
11673 of the template parameter-list rather than a greater-than
11674 operator. */
11675 greater_than_is_operator_p = !template_parm_p;
11676
11677 /* Type definitions may not appear in parameter types. */
11678 saved_message = parser->type_definition_forbidden_message;
11679 parser->type_definition_forbidden_message
11680 = "types may not be defined in parameter types";
11681
11682 /* Parse the declaration-specifiers. */
11683 cp_parser_decl_specifier_seq (parser,
11684 CP_PARSER_FLAGS_NONE,
11685 &decl_specifiers,
11686 &declares_class_or_enum);
11687 /* If an error occurred, there's no reason to attempt to parse the
11688 rest of the declaration. */
11689 if (cp_parser_error_occurred (parser))
11690 {
11691 parser->type_definition_forbidden_message = saved_message;
11692 return NULL;
11693 }
11694
11695 /* Peek at the next token. */
11696 token = cp_lexer_peek_token (parser->lexer);
11697 /* If the next token is a `)', `,', `=', `>', or `...', then there
11698 is no declarator. */
11699 if (token->type == CPP_CLOSE_PAREN
11700 || token->type == CPP_COMMA
11701 || token->type == CPP_EQ
11702 || token->type == CPP_ELLIPSIS
11703 || token->type == CPP_GREATER)
11704 {
11705 declarator = NULL;
11706 if (parenthesized_p)
11707 *parenthesized_p = false;
11708 }
11709 /* Otherwise, there should be a declarator. */
11710 else
11711 {
11712 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11713 parser->default_arg_ok_p = false;
11714
11715 /* After seeing a decl-specifier-seq, if the next token is not a
11716 "(", there is no possibility that the code is a valid
11717 expression. Therefore, if parsing tentatively, we commit at
11718 this point. */
11719 if (!parser->in_template_argument_list_p
11720 /* In an expression context, having seen:
11721
11722 (int((char ...
11723
11724 we cannot be sure whether we are looking at a
11725 function-type (taking a "char" as a parameter) or a cast
11726 of some object of type "char" to "int". */
11727 && !parser->in_type_id_in_expr_p
11728 && cp_parser_parsing_tentatively (parser)
11729 && !cp_parser_committed_to_tentative_parse (parser)
11730 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11731 cp_parser_commit_to_tentative_parse (parser);
11732 /* Parse the declarator. */
11733 declarator = cp_parser_declarator (parser,
11734 CP_PARSER_DECLARATOR_EITHER,
11735 /*ctor_dtor_or_conv_p=*/NULL,
11736 parenthesized_p,
11737 /*member_p=*/false);
11738 parser->default_arg_ok_p = saved_default_arg_ok_p;
11739 /* After the declarator, allow more attributes. */
11740 decl_specifiers.attributes
11741 = chainon (decl_specifiers.attributes,
11742 cp_parser_attributes_opt (parser));
11743 }
11744
11745 /* The restriction on defining new types applies only to the type
11746 of the parameter, not to the default argument. */
11747 parser->type_definition_forbidden_message = saved_message;
11748
11749 /* If the next token is `=', then process a default argument. */
11750 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11751 {
11752 bool saved_greater_than_is_operator_p;
11753 /* Consume the `='. */
11754 cp_lexer_consume_token (parser->lexer);
11755
11756 /* If we are defining a class, then the tokens that make up the
11757 default argument must be saved and processed later. */
11758 if (!template_parm_p && at_class_scope_p ()
11759 && TYPE_BEING_DEFINED (current_class_type))
11760 {
11761 unsigned depth = 0;
11762 cp_token *first_token;
11763 cp_token *token;
11764
11765 /* Add tokens until we have processed the entire default
11766 argument. We add the range [first_token, token). */
11767 first_token = cp_lexer_peek_token (parser->lexer);
11768 while (true)
11769 {
11770 bool done = false;
11771
11772 /* Peek at the next token. */
11773 token = cp_lexer_peek_token (parser->lexer);
11774 /* What we do depends on what token we have. */
11775 switch (token->type)
11776 {
11777 /* In valid code, a default argument must be
11778 immediately followed by a `,' `)', or `...'. */
11779 case CPP_COMMA:
11780 case CPP_CLOSE_PAREN:
11781 case CPP_ELLIPSIS:
11782 /* If we run into a non-nested `;', `}', or `]',
11783 then the code is invalid -- but the default
11784 argument is certainly over. */
11785 case CPP_SEMICOLON:
11786 case CPP_CLOSE_BRACE:
11787 case CPP_CLOSE_SQUARE:
11788 if (depth == 0)
11789 done = true;
11790 /* Update DEPTH, if necessary. */
11791 else if (token->type == CPP_CLOSE_PAREN
11792 || token->type == CPP_CLOSE_BRACE
11793 || token->type == CPP_CLOSE_SQUARE)
11794 --depth;
11795 break;
11796
11797 case CPP_OPEN_PAREN:
11798 case CPP_OPEN_SQUARE:
11799 case CPP_OPEN_BRACE:
11800 ++depth;
11801 break;
11802
11803 case CPP_GREATER:
11804 /* If we see a non-nested `>', and `>' is not an
11805 operator, then it marks the end of the default
11806 argument. */
11807 if (!depth && !greater_than_is_operator_p)
11808 done = true;
11809 break;
11810
11811 /* If we run out of tokens, issue an error message. */
11812 case CPP_EOF:
11813 error ("file ends in default argument");
11814 done = true;
11815 break;
11816
11817 case CPP_NAME:
11818 case CPP_SCOPE:
11819 /* In these cases, we should look for template-ids.
11820 For example, if the default argument is
11821 `X<int, double>()', we need to do name lookup to
11822 figure out whether or not `X' is a template; if
11823 so, the `,' does not end the default argument.
11824
11825 That is not yet done. */
11826 break;
11827
11828 default:
11829 break;
11830 }
11831
11832 /* If we've reached the end, stop. */
11833 if (done)
11834 break;
11835
11836 /* Add the token to the token block. */
11837 token = cp_lexer_consume_token (parser->lexer);
11838 }
11839
11840 /* Create a DEFAULT_ARG to represented the unparsed default
11841 argument. */
11842 default_argument = make_node (DEFAULT_ARG);
11843 DEFARG_TOKENS (default_argument)
11844 = cp_token_cache_new (first_token, token);
11845 }
11846 /* Outside of a class definition, we can just parse the
11847 assignment-expression. */
11848 else
11849 {
11850 bool saved_local_variables_forbidden_p;
11851
11852 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11853 set correctly. */
11854 saved_greater_than_is_operator_p
11855 = parser->greater_than_is_operator_p;
11856 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11857 /* Local variable names (and the `this' keyword) may not
11858 appear in a default argument. */
11859 saved_local_variables_forbidden_p
11860 = parser->local_variables_forbidden_p;
11861 parser->local_variables_forbidden_p = true;
11862 /* Parse the assignment-expression. */
11863 default_argument = cp_parser_assignment_expression (parser);
11864 /* Restore saved state. */
11865 parser->greater_than_is_operator_p
11866 = saved_greater_than_is_operator_p;
11867 parser->local_variables_forbidden_p
11868 = saved_local_variables_forbidden_p;
11869 }
11870 if (!parser->default_arg_ok_p)
11871 {
11872 if (!flag_pedantic_errors)
11873 warning ("deprecated use of default argument for parameter of non-function");
11874 else
11875 {
11876 error ("default arguments are only permitted for function parameters");
11877 default_argument = NULL_TREE;
11878 }
11879 }
11880 }
11881 else
11882 default_argument = NULL_TREE;
11883
11884 return make_parameter_declarator (&decl_specifiers,
11885 declarator,
11886 default_argument);
11887 }
11888
11889 /* Parse a function-body.
11890
11891 function-body:
11892 compound_statement */
11893
11894 static void
11895 cp_parser_function_body (cp_parser *parser)
11896 {
11897 cp_parser_compound_statement (parser, NULL, false);
11898 }
11899
11900 /* Parse a ctor-initializer-opt followed by a function-body. Return
11901 true if a ctor-initializer was present. */
11902
11903 static bool
11904 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11905 {
11906 tree body;
11907 bool ctor_initializer_p;
11908
11909 /* Begin the function body. */
11910 body = begin_function_body ();
11911 /* Parse the optional ctor-initializer. */
11912 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11913 /* Parse the function-body. */
11914 cp_parser_function_body (parser);
11915 /* Finish the function body. */
11916 finish_function_body (body);
11917
11918 return ctor_initializer_p;
11919 }
11920
11921 /* Parse an initializer.
11922
11923 initializer:
11924 = initializer-clause
11925 ( expression-list )
11926
11927 Returns a expression representing the initializer. If no
11928 initializer is present, NULL_TREE is returned.
11929
11930 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11931 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11932 set to FALSE if there is no initializer present. If there is an
11933 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11934 is set to true; otherwise it is set to false. */
11935
11936 static tree
11937 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11938 bool* non_constant_p)
11939 {
11940 cp_token *token;
11941 tree init;
11942
11943 /* Peek at the next token. */
11944 token = cp_lexer_peek_token (parser->lexer);
11945
11946 /* Let our caller know whether or not this initializer was
11947 parenthesized. */
11948 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11949 /* Assume that the initializer is constant. */
11950 *non_constant_p = false;
11951
11952 if (token->type == CPP_EQ)
11953 {
11954 /* Consume the `='. */
11955 cp_lexer_consume_token (parser->lexer);
11956 /* Parse the initializer-clause. */
11957 init = cp_parser_initializer_clause (parser, non_constant_p);
11958 }
11959 else if (token->type == CPP_OPEN_PAREN)
11960 init = cp_parser_parenthesized_expression_list (parser, false,
11961 non_constant_p);
11962 else
11963 {
11964 /* Anything else is an error. */
11965 cp_parser_error (parser, "expected initializer");
11966 init = error_mark_node;
11967 }
11968
11969 return init;
11970 }
11971
11972 /* Parse an initializer-clause.
11973
11974 initializer-clause:
11975 assignment-expression
11976 { initializer-list , [opt] }
11977 { }
11978
11979 Returns an expression representing the initializer.
11980
11981 If the `assignment-expression' production is used the value
11982 returned is simply a representation for the expression.
11983
11984 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11985 the elements of the initializer-list (or NULL_TREE, if the last
11986 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11987 NULL_TREE. There is no way to detect whether or not the optional
11988 trailing `,' was provided. NON_CONSTANT_P is as for
11989 cp_parser_initializer. */
11990
11991 static tree
11992 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11993 {
11994 tree initializer;
11995
11996 /* If it is not a `{', then we are looking at an
11997 assignment-expression. */
11998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11999 {
12000 initializer
12001 = cp_parser_constant_expression (parser,
12002 /*allow_non_constant_p=*/true,
12003 non_constant_p);
12004 if (!*non_constant_p)
12005 initializer = fold_non_dependent_expr (initializer);
12006 }
12007 else
12008 {
12009 /* Consume the `{' token. */
12010 cp_lexer_consume_token (parser->lexer);
12011 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12012 initializer = make_node (CONSTRUCTOR);
12013 /* If it's not a `}', then there is a non-trivial initializer. */
12014 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12015 {
12016 /* Parse the initializer list. */
12017 CONSTRUCTOR_ELTS (initializer)
12018 = cp_parser_initializer_list (parser, non_constant_p);
12019 /* A trailing `,' token is allowed. */
12020 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12021 cp_lexer_consume_token (parser->lexer);
12022 }
12023 /* Now, there should be a trailing `}'. */
12024 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12025 }
12026
12027 return initializer;
12028 }
12029
12030 /* Parse an initializer-list.
12031
12032 initializer-list:
12033 initializer-clause
12034 initializer-list , initializer-clause
12035
12036 GNU Extension:
12037
12038 initializer-list:
12039 identifier : initializer-clause
12040 initializer-list, identifier : initializer-clause
12041
12042 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12043 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12044 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12045 as for cp_parser_initializer. */
12046
12047 static tree
12048 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12049 {
12050 tree initializers = NULL_TREE;
12051
12052 /* Assume all of the expressions are constant. */
12053 *non_constant_p = false;
12054
12055 /* Parse the rest of the list. */
12056 while (true)
12057 {
12058 cp_token *token;
12059 tree identifier;
12060 tree initializer;
12061 bool clause_non_constant_p;
12062
12063 /* If the next token is an identifier and the following one is a
12064 colon, we are looking at the GNU designated-initializer
12065 syntax. */
12066 if (cp_parser_allow_gnu_extensions_p (parser)
12067 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12068 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12069 {
12070 /* Consume the identifier. */
12071 identifier = cp_lexer_consume_token (parser->lexer)->value;
12072 /* Consume the `:'. */
12073 cp_lexer_consume_token (parser->lexer);
12074 }
12075 else
12076 identifier = NULL_TREE;
12077
12078 /* Parse the initializer. */
12079 initializer = cp_parser_initializer_clause (parser,
12080 &clause_non_constant_p);
12081 /* If any clause is non-constant, so is the entire initializer. */
12082 if (clause_non_constant_p)
12083 *non_constant_p = true;
12084 /* Add it to the list. */
12085 initializers = tree_cons (identifier, initializer, initializers);
12086
12087 /* If the next token is not a comma, we have reached the end of
12088 the list. */
12089 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12090 break;
12091
12092 /* Peek at the next token. */
12093 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12094 /* If the next token is a `}', then we're still done. An
12095 initializer-clause can have a trailing `,' after the
12096 initializer-list and before the closing `}'. */
12097 if (token->type == CPP_CLOSE_BRACE)
12098 break;
12099
12100 /* Consume the `,' token. */
12101 cp_lexer_consume_token (parser->lexer);
12102 }
12103
12104 /* The initializers were built up in reverse order, so we need to
12105 reverse them now. */
12106 return nreverse (initializers);
12107 }
12108
12109 /* Classes [gram.class] */
12110
12111 /* Parse a class-name.
12112
12113 class-name:
12114 identifier
12115 template-id
12116
12117 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12118 to indicate that names looked up in dependent types should be
12119 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12120 keyword has been used to indicate that the name that appears next
12121 is a template. TAG_TYPE indicates the explicit tag given before
12122 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12123 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12124 is the class being defined in a class-head.
12125
12126 Returns the TYPE_DECL representing the class. */
12127
12128 static tree
12129 cp_parser_class_name (cp_parser *parser,
12130 bool typename_keyword_p,
12131 bool template_keyword_p,
12132 enum tag_types tag_type,
12133 bool check_dependency_p,
12134 bool class_head_p,
12135 bool is_declaration)
12136 {
12137 tree decl;
12138 tree scope;
12139 bool typename_p;
12140 cp_token *token;
12141
12142 /* All class-names start with an identifier. */
12143 token = cp_lexer_peek_token (parser->lexer);
12144 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12145 {
12146 cp_parser_error (parser, "expected class-name");
12147 return error_mark_node;
12148 }
12149
12150 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12151 to a template-id, so we save it here. */
12152 scope = parser->scope;
12153 if (scope == error_mark_node)
12154 return error_mark_node;
12155
12156 /* Any name names a type if we're following the `typename' keyword
12157 in a qualified name where the enclosing scope is type-dependent. */
12158 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12159 && dependent_type_p (scope));
12160 /* Handle the common case (an identifier, but not a template-id)
12161 efficiently. */
12162 if (token->type == CPP_NAME
12163 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12164 {
12165 tree identifier;
12166
12167 /* Look for the identifier. */
12168 identifier = cp_parser_identifier (parser);
12169 /* If the next token isn't an identifier, we are certainly not
12170 looking at a class-name. */
12171 if (identifier == error_mark_node)
12172 decl = error_mark_node;
12173 /* If we know this is a type-name, there's no need to look it
12174 up. */
12175 else if (typename_p)
12176 decl = identifier;
12177 else
12178 {
12179 /* If the next token is a `::', then the name must be a type
12180 name.
12181
12182 [basic.lookup.qual]
12183
12184 During the lookup for a name preceding the :: scope
12185 resolution operator, object, function, and enumerator
12186 names are ignored. */
12187 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12188 tag_type = typename_type;
12189 /* Look up the name. */
12190 decl = cp_parser_lookup_name (parser, identifier,
12191 tag_type,
12192 /*is_template=*/false,
12193 /*is_namespace=*/false,
12194 check_dependency_p,
12195 /*ambiguous_p=*/NULL);
12196 }
12197 }
12198 else
12199 {
12200 /* Try a template-id. */
12201 decl = cp_parser_template_id (parser, template_keyword_p,
12202 check_dependency_p,
12203 is_declaration);
12204 if (decl == error_mark_node)
12205 return error_mark_node;
12206 }
12207
12208 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12209
12210 /* If this is a typename, create a TYPENAME_TYPE. */
12211 if (typename_p && decl != error_mark_node)
12212 {
12213 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12214 if (decl != error_mark_node)
12215 decl = TYPE_NAME (decl);
12216 }
12217
12218 /* Check to see that it is really the name of a class. */
12219 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12220 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12221 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12222 /* Situations like this:
12223
12224 template <typename T> struct A {
12225 typename T::template X<int>::I i;
12226 };
12227
12228 are problematic. Is `T::template X<int>' a class-name? The
12229 standard does not seem to be definitive, but there is no other
12230 valid interpretation of the following `::'. Therefore, those
12231 names are considered class-names. */
12232 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12233 else if (decl == error_mark_node
12234 || TREE_CODE (decl) != TYPE_DECL
12235 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12236 {
12237 cp_parser_error (parser, "expected class-name");
12238 return error_mark_node;
12239 }
12240
12241 return decl;
12242 }
12243
12244 /* Parse a class-specifier.
12245
12246 class-specifier:
12247 class-head { member-specification [opt] }
12248
12249 Returns the TREE_TYPE representing the class. */
12250
12251 static tree
12252 cp_parser_class_specifier (cp_parser* parser)
12253 {
12254 cp_token *token;
12255 tree type;
12256 tree attributes = NULL_TREE;
12257 int has_trailing_semicolon;
12258 bool nested_name_specifier_p;
12259 unsigned saved_num_template_parameter_lists;
12260 tree old_scope = NULL_TREE;
12261 tree scope = NULL_TREE;
12262
12263 push_deferring_access_checks (dk_no_deferred);
12264
12265 /* Parse the class-head. */
12266 type = cp_parser_class_head (parser,
12267 &nested_name_specifier_p,
12268 &attributes);
12269 /* If the class-head was a semantic disaster, skip the entire body
12270 of the class. */
12271 if (!type)
12272 {
12273 cp_parser_skip_to_end_of_block_or_statement (parser);
12274 pop_deferring_access_checks ();
12275 return error_mark_node;
12276 }
12277
12278 /* Look for the `{'. */
12279 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12280 {
12281 pop_deferring_access_checks ();
12282 return error_mark_node;
12283 }
12284
12285 /* Issue an error message if type-definitions are forbidden here. */
12286 cp_parser_check_type_definition (parser);
12287 /* Remember that we are defining one more class. */
12288 ++parser->num_classes_being_defined;
12289 /* Inside the class, surrounding template-parameter-lists do not
12290 apply. */
12291 saved_num_template_parameter_lists
12292 = parser->num_template_parameter_lists;
12293 parser->num_template_parameter_lists = 0;
12294
12295 /* Start the class. */
12296 if (nested_name_specifier_p)
12297 {
12298 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12299 old_scope = push_inner_scope (scope);
12300 }
12301 type = begin_class_definition (type);
12302
12303 if (type == error_mark_node)
12304 /* If the type is erroneous, skip the entire body of the class. */
12305 cp_parser_skip_to_closing_brace (parser);
12306 else
12307 /* Parse the member-specification. */
12308 cp_parser_member_specification_opt (parser);
12309
12310 /* Look for the trailing `}'. */
12311 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12312 /* We get better error messages by noticing a common problem: a
12313 missing trailing `;'. */
12314 token = cp_lexer_peek_token (parser->lexer);
12315 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12316 /* Look for trailing attributes to apply to this class. */
12317 if (cp_parser_allow_gnu_extensions_p (parser))
12318 {
12319 tree sub_attr = cp_parser_attributes_opt (parser);
12320 attributes = chainon (attributes, sub_attr);
12321 }
12322 if (type != error_mark_node)
12323 type = finish_struct (type, attributes);
12324 if (nested_name_specifier_p)
12325 pop_inner_scope (old_scope, scope);
12326 /* If this class is not itself within the scope of another class,
12327 then we need to parse the bodies of all of the queued function
12328 definitions. Note that the queued functions defined in a class
12329 are not always processed immediately following the
12330 class-specifier for that class. Consider:
12331
12332 struct A {
12333 struct B { void f() { sizeof (A); } };
12334 };
12335
12336 If `f' were processed before the processing of `A' were
12337 completed, there would be no way to compute the size of `A'.
12338 Note that the nesting we are interested in here is lexical --
12339 not the semantic nesting given by TYPE_CONTEXT. In particular,
12340 for:
12341
12342 struct A { struct B; };
12343 struct A::B { void f() { } };
12344
12345 there is no need to delay the parsing of `A::B::f'. */
12346 if (--parser->num_classes_being_defined == 0)
12347 {
12348 tree queue_entry;
12349 tree fn;
12350 tree class_type;
12351 bool pop_p;
12352
12353 /* In a first pass, parse default arguments to the functions.
12354 Then, in a second pass, parse the bodies of the functions.
12355 This two-phased approach handles cases like:
12356
12357 struct S {
12358 void f() { g(); }
12359 void g(int i = 3);
12360 };
12361
12362 */
12363 class_type = NULL_TREE;
12364 pop_p = false;
12365 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12366 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12367 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12368 TREE_PURPOSE (parser->unparsed_functions_queues)
12369 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12370 {
12371 fn = TREE_VALUE (queue_entry);
12372 /* If there are default arguments that have not yet been processed,
12373 take care of them now. */
12374 if (class_type != TREE_PURPOSE (queue_entry))
12375 {
12376 if (pop_p)
12377 pop_scope (class_type);
12378 class_type = TREE_PURPOSE (queue_entry);
12379 pop_p = push_scope (class_type);
12380 }
12381 /* Make sure that any template parameters are in scope. */
12382 maybe_begin_member_template_processing (fn);
12383 /* Parse the default argument expressions. */
12384 cp_parser_late_parsing_default_args (parser, fn);
12385 /* Remove any template parameters from the symbol table. */
12386 maybe_end_member_template_processing ();
12387 }
12388 if (pop_p)
12389 pop_scope (class_type);
12390 /* Now parse the body of the functions. */
12391 for (TREE_VALUE (parser->unparsed_functions_queues)
12392 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12393 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12394 TREE_VALUE (parser->unparsed_functions_queues)
12395 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12396 {
12397 /* Figure out which function we need to process. */
12398 fn = TREE_VALUE (queue_entry);
12399
12400 /* A hack to prevent garbage collection. */
12401 function_depth++;
12402
12403 /* Parse the function. */
12404 cp_parser_late_parsing_for_member (parser, fn);
12405 function_depth--;
12406 }
12407 }
12408
12409 /* Put back any saved access checks. */
12410 pop_deferring_access_checks ();
12411
12412 /* Restore the count of active template-parameter-lists. */
12413 parser->num_template_parameter_lists
12414 = saved_num_template_parameter_lists;
12415
12416 return type;
12417 }
12418
12419 /* Parse a class-head.
12420
12421 class-head:
12422 class-key identifier [opt] base-clause [opt]
12423 class-key nested-name-specifier identifier base-clause [opt]
12424 class-key nested-name-specifier [opt] template-id
12425 base-clause [opt]
12426
12427 GNU Extensions:
12428 class-key attributes identifier [opt] base-clause [opt]
12429 class-key attributes nested-name-specifier identifier base-clause [opt]
12430 class-key attributes nested-name-specifier [opt] template-id
12431 base-clause [opt]
12432
12433 Returns the TYPE of the indicated class. Sets
12434 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12435 involving a nested-name-specifier was used, and FALSE otherwise.
12436
12437 Returns NULL_TREE if the class-head is syntactically valid, but
12438 semantically invalid in a way that means we should skip the entire
12439 body of the class. */
12440
12441 static tree
12442 cp_parser_class_head (cp_parser* parser,
12443 bool* nested_name_specifier_p,
12444 tree *attributes_p)
12445 {
12446 tree nested_name_specifier;
12447 enum tag_types class_key;
12448 tree id = NULL_TREE;
12449 tree type = NULL_TREE;
12450 tree attributes;
12451 bool template_id_p = false;
12452 bool qualified_p = false;
12453 bool invalid_nested_name_p = false;
12454 bool invalid_explicit_specialization_p = false;
12455 bool pop_p = false;
12456 unsigned num_templates;
12457 tree bases;
12458
12459 /* Assume no nested-name-specifier will be present. */
12460 *nested_name_specifier_p = false;
12461 /* Assume no template parameter lists will be used in defining the
12462 type. */
12463 num_templates = 0;
12464
12465 /* Look for the class-key. */
12466 class_key = cp_parser_class_key (parser);
12467 if (class_key == none_type)
12468 return error_mark_node;
12469
12470 /* Parse the attributes. */
12471 attributes = cp_parser_attributes_opt (parser);
12472
12473 /* If the next token is `::', that is invalid -- but sometimes
12474 people do try to write:
12475
12476 struct ::S {};
12477
12478 Handle this gracefully by accepting the extra qualifier, and then
12479 issuing an error about it later if this really is a
12480 class-head. If it turns out just to be an elaborated type
12481 specifier, remain silent. */
12482 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12483 qualified_p = true;
12484
12485 push_deferring_access_checks (dk_no_check);
12486
12487 /* Determine the name of the class. Begin by looking for an
12488 optional nested-name-specifier. */
12489 nested_name_specifier
12490 = cp_parser_nested_name_specifier_opt (parser,
12491 /*typename_keyword_p=*/false,
12492 /*check_dependency_p=*/false,
12493 /*type_p=*/false,
12494 /*is_declaration=*/false);
12495 /* If there was a nested-name-specifier, then there *must* be an
12496 identifier. */
12497 if (nested_name_specifier)
12498 {
12499 /* Although the grammar says `identifier', it really means
12500 `class-name' or `template-name'. You are only allowed to
12501 define a class that has already been declared with this
12502 syntax.
12503
12504 The proposed resolution for Core Issue 180 says that whever
12505 you see `class T::X' you should treat `X' as a type-name.
12506
12507 It is OK to define an inaccessible class; for example:
12508
12509 class A { class B; };
12510 class A::B {};
12511
12512 We do not know if we will see a class-name, or a
12513 template-name. We look for a class-name first, in case the
12514 class-name is a template-id; if we looked for the
12515 template-name first we would stop after the template-name. */
12516 cp_parser_parse_tentatively (parser);
12517 type = cp_parser_class_name (parser,
12518 /*typename_keyword_p=*/false,
12519 /*template_keyword_p=*/false,
12520 class_type,
12521 /*check_dependency_p=*/false,
12522 /*class_head_p=*/true,
12523 /*is_declaration=*/false);
12524 /* If that didn't work, ignore the nested-name-specifier. */
12525 if (!cp_parser_parse_definitely (parser))
12526 {
12527 invalid_nested_name_p = true;
12528 id = cp_parser_identifier (parser);
12529 if (id == error_mark_node)
12530 id = NULL_TREE;
12531 }
12532 /* If we could not find a corresponding TYPE, treat this
12533 declaration like an unqualified declaration. */
12534 if (type == error_mark_node)
12535 nested_name_specifier = NULL_TREE;
12536 /* Otherwise, count the number of templates used in TYPE and its
12537 containing scopes. */
12538 else
12539 {
12540 tree scope;
12541
12542 for (scope = TREE_TYPE (type);
12543 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12544 scope = (TYPE_P (scope)
12545 ? TYPE_CONTEXT (scope)
12546 : DECL_CONTEXT (scope)))
12547 if (TYPE_P (scope)
12548 && CLASS_TYPE_P (scope)
12549 && CLASSTYPE_TEMPLATE_INFO (scope)
12550 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12551 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12552 ++num_templates;
12553 }
12554 }
12555 /* Otherwise, the identifier is optional. */
12556 else
12557 {
12558 /* We don't know whether what comes next is a template-id,
12559 an identifier, or nothing at all. */
12560 cp_parser_parse_tentatively (parser);
12561 /* Check for a template-id. */
12562 id = cp_parser_template_id (parser,
12563 /*template_keyword_p=*/false,
12564 /*check_dependency_p=*/true,
12565 /*is_declaration=*/true);
12566 /* If that didn't work, it could still be an identifier. */
12567 if (!cp_parser_parse_definitely (parser))
12568 {
12569 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12570 id = cp_parser_identifier (parser);
12571 else
12572 id = NULL_TREE;
12573 }
12574 else
12575 {
12576 template_id_p = true;
12577 ++num_templates;
12578 }
12579 }
12580
12581 pop_deferring_access_checks ();
12582
12583 if (id)
12584 cp_parser_check_for_invalid_template_id (parser, id);
12585
12586 /* If it's not a `:' or a `{' then we can't really be looking at a
12587 class-head, since a class-head only appears as part of a
12588 class-specifier. We have to detect this situation before calling
12589 xref_tag, since that has irreversible side-effects. */
12590 if (!cp_parser_next_token_starts_class_definition_p (parser))
12591 {
12592 cp_parser_error (parser, "expected %<{%> or %<:%>");
12593 return error_mark_node;
12594 }
12595
12596 /* At this point, we're going ahead with the class-specifier, even
12597 if some other problem occurs. */
12598 cp_parser_commit_to_tentative_parse (parser);
12599 /* Issue the error about the overly-qualified name now. */
12600 if (qualified_p)
12601 cp_parser_error (parser,
12602 "global qualification of class name is invalid");
12603 else if (invalid_nested_name_p)
12604 cp_parser_error (parser,
12605 "qualified name does not name a class");
12606 else if (nested_name_specifier)
12607 {
12608 tree scope;
12609 /* Figure out in what scope the declaration is being placed. */
12610 scope = current_scope ();
12611 /* If that scope does not contain the scope in which the
12612 class was originally declared, the program is invalid. */
12613 if (scope && !is_ancestor (scope, nested_name_specifier))
12614 {
12615 error ("declaration of %qD in %qD which does not enclose %qD",
12616 type, scope, nested_name_specifier);
12617 type = NULL_TREE;
12618 goto done;
12619 }
12620 /* [dcl.meaning]
12621
12622 A declarator-id shall not be qualified exception of the
12623 definition of a ... nested class outside of its class
12624 ... [or] a the definition or explicit instantiation of a
12625 class member of a namespace outside of its namespace. */
12626 if (scope == nested_name_specifier)
12627 {
12628 pedwarn ("extra qualification ignored");
12629 nested_name_specifier = NULL_TREE;
12630 num_templates = 0;
12631 }
12632 }
12633 /* An explicit-specialization must be preceded by "template <>". If
12634 it is not, try to recover gracefully. */
12635 if (at_namespace_scope_p ()
12636 && parser->num_template_parameter_lists == 0
12637 && template_id_p)
12638 {
12639 error ("an explicit specialization must be preceded by %<template <>%>");
12640 invalid_explicit_specialization_p = true;
12641 /* Take the same action that would have been taken by
12642 cp_parser_explicit_specialization. */
12643 ++parser->num_template_parameter_lists;
12644 begin_specialization ();
12645 }
12646 /* There must be no "return" statements between this point and the
12647 end of this function; set "type "to the correct return value and
12648 use "goto done;" to return. */
12649 /* Make sure that the right number of template parameters were
12650 present. */
12651 if (!cp_parser_check_template_parameters (parser, num_templates))
12652 {
12653 /* If something went wrong, there is no point in even trying to
12654 process the class-definition. */
12655 type = NULL_TREE;
12656 goto done;
12657 }
12658
12659 /* Look up the type. */
12660 if (template_id_p)
12661 {
12662 type = TREE_TYPE (id);
12663 maybe_process_partial_specialization (type);
12664 }
12665 else if (!nested_name_specifier)
12666 {
12667 /* If the class was unnamed, create a dummy name. */
12668 if (!id)
12669 id = make_anon_name ();
12670 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12671 parser->num_template_parameter_lists);
12672 }
12673 else
12674 {
12675 tree class_type;
12676 bool pop_p = false;
12677
12678 /* Given:
12679
12680 template <typename T> struct S { struct T };
12681 template <typename T> struct S<T>::T { };
12682
12683 we will get a TYPENAME_TYPE when processing the definition of
12684 `S::T'. We need to resolve it to the actual type before we
12685 try to define it. */
12686 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12687 {
12688 class_type = resolve_typename_type (TREE_TYPE (type),
12689 /*only_current_p=*/false);
12690 if (class_type != error_mark_node)
12691 type = TYPE_NAME (class_type);
12692 else
12693 {
12694 cp_parser_error (parser, "could not resolve typename type");
12695 type = error_mark_node;
12696 }
12697 }
12698
12699 maybe_process_partial_specialization (TREE_TYPE (type));
12700 class_type = current_class_type;
12701 /* Enter the scope indicated by the nested-name-specifier. */
12702 if (nested_name_specifier)
12703 pop_p = push_scope (nested_name_specifier);
12704 /* Get the canonical version of this type. */
12705 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12706 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12707 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12708 type = push_template_decl (type);
12709 type = TREE_TYPE (type);
12710 if (nested_name_specifier)
12711 {
12712 *nested_name_specifier_p = true;
12713 if (pop_p)
12714 pop_scope (nested_name_specifier);
12715 }
12716 }
12717 /* Indicate whether this class was declared as a `class' or as a
12718 `struct'. */
12719 if (TREE_CODE (type) == RECORD_TYPE)
12720 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12721 cp_parser_check_class_key (class_key, type);
12722
12723 /* Enter the scope containing the class; the names of base classes
12724 should be looked up in that context. For example, given:
12725
12726 struct A { struct B {}; struct C; };
12727 struct A::C : B {};
12728
12729 is valid. */
12730 if (nested_name_specifier)
12731 pop_p = push_scope (nested_name_specifier);
12732
12733 bases = NULL_TREE;
12734
12735 /* Get the list of base-classes, if there is one. */
12736 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12737 bases = cp_parser_base_clause (parser);
12738
12739 /* Process the base classes. */
12740 xref_basetypes (type, bases);
12741
12742 /* Leave the scope given by the nested-name-specifier. We will
12743 enter the class scope itself while processing the members. */
12744 if (pop_p)
12745 pop_scope (nested_name_specifier);
12746
12747 done:
12748 if (invalid_explicit_specialization_p)
12749 {
12750 end_specialization ();
12751 --parser->num_template_parameter_lists;
12752 }
12753 *attributes_p = attributes;
12754 return type;
12755 }
12756
12757 /* Parse a class-key.
12758
12759 class-key:
12760 class
12761 struct
12762 union
12763
12764 Returns the kind of class-key specified, or none_type to indicate
12765 error. */
12766
12767 static enum tag_types
12768 cp_parser_class_key (cp_parser* parser)
12769 {
12770 cp_token *token;
12771 enum tag_types tag_type;
12772
12773 /* Look for the class-key. */
12774 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12775 if (!token)
12776 return none_type;
12777
12778 /* Check to see if the TOKEN is a class-key. */
12779 tag_type = cp_parser_token_is_class_key (token);
12780 if (!tag_type)
12781 cp_parser_error (parser, "expected class-key");
12782 return tag_type;
12783 }
12784
12785 /* Parse an (optional) member-specification.
12786
12787 member-specification:
12788 member-declaration member-specification [opt]
12789 access-specifier : member-specification [opt] */
12790
12791 static void
12792 cp_parser_member_specification_opt (cp_parser* parser)
12793 {
12794 while (true)
12795 {
12796 cp_token *token;
12797 enum rid keyword;
12798
12799 /* Peek at the next token. */
12800 token = cp_lexer_peek_token (parser->lexer);
12801 /* If it's a `}', or EOF then we've seen all the members. */
12802 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12803 break;
12804
12805 /* See if this token is a keyword. */
12806 keyword = token->keyword;
12807 switch (keyword)
12808 {
12809 case RID_PUBLIC:
12810 case RID_PROTECTED:
12811 case RID_PRIVATE:
12812 /* Consume the access-specifier. */
12813 cp_lexer_consume_token (parser->lexer);
12814 /* Remember which access-specifier is active. */
12815 current_access_specifier = token->value;
12816 /* Look for the `:'. */
12817 cp_parser_require (parser, CPP_COLON, "`:'");
12818 break;
12819
12820 default:
12821 /* Accept #pragmas at class scope. */
12822 if (token->type == CPP_PRAGMA)
12823 {
12824 cp_lexer_handle_pragma (parser->lexer);
12825 break;
12826 }
12827
12828 /* Otherwise, the next construction must be a
12829 member-declaration. */
12830 cp_parser_member_declaration (parser);
12831 }
12832 }
12833 }
12834
12835 /* Parse a member-declaration.
12836
12837 member-declaration:
12838 decl-specifier-seq [opt] member-declarator-list [opt] ;
12839 function-definition ; [opt]
12840 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12841 using-declaration
12842 template-declaration
12843
12844 member-declarator-list:
12845 member-declarator
12846 member-declarator-list , member-declarator
12847
12848 member-declarator:
12849 declarator pure-specifier [opt]
12850 declarator constant-initializer [opt]
12851 identifier [opt] : constant-expression
12852
12853 GNU Extensions:
12854
12855 member-declaration:
12856 __extension__ member-declaration
12857
12858 member-declarator:
12859 declarator attributes [opt] pure-specifier [opt]
12860 declarator attributes [opt] constant-initializer [opt]
12861 identifier [opt] attributes [opt] : constant-expression */
12862
12863 static void
12864 cp_parser_member_declaration (cp_parser* parser)
12865 {
12866 cp_decl_specifier_seq decl_specifiers;
12867 tree prefix_attributes;
12868 tree decl;
12869 int declares_class_or_enum;
12870 bool friend_p;
12871 cp_token *token;
12872 int saved_pedantic;
12873
12874 /* Check for the `__extension__' keyword. */
12875 if (cp_parser_extension_opt (parser, &saved_pedantic))
12876 {
12877 /* Recurse. */
12878 cp_parser_member_declaration (parser);
12879 /* Restore the old value of the PEDANTIC flag. */
12880 pedantic = saved_pedantic;
12881
12882 return;
12883 }
12884
12885 /* Check for a template-declaration. */
12886 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12887 {
12888 /* Parse the template-declaration. */
12889 cp_parser_template_declaration (parser, /*member_p=*/true);
12890
12891 return;
12892 }
12893
12894 /* Check for a using-declaration. */
12895 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12896 {
12897 /* Parse the using-declaration. */
12898 cp_parser_using_declaration (parser);
12899
12900 return;
12901 }
12902
12903 /* Parse the decl-specifier-seq. */
12904 cp_parser_decl_specifier_seq (parser,
12905 CP_PARSER_FLAGS_OPTIONAL,
12906 &decl_specifiers,
12907 &declares_class_or_enum);
12908 prefix_attributes = decl_specifiers.attributes;
12909 decl_specifiers.attributes = NULL_TREE;
12910 /* Check for an invalid type-name. */
12911 if (!decl_specifiers.type
12912 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12913 return;
12914 /* If there is no declarator, then the decl-specifier-seq should
12915 specify a type. */
12916 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12917 {
12918 /* If there was no decl-specifier-seq, and the next token is a
12919 `;', then we have something like:
12920
12921 struct S { ; };
12922
12923 [class.mem]
12924
12925 Each member-declaration shall declare at least one member
12926 name of the class. */
12927 if (!decl_specifiers.any_specifiers_p)
12928 {
12929 cp_token *token = cp_lexer_peek_token (parser->lexer);
12930 if (pedantic && !token->in_system_header)
12931 pedwarn ("%Hextra %<;%>", &token->location);
12932 }
12933 else
12934 {
12935 tree type;
12936
12937 /* See if this declaration is a friend. */
12938 friend_p = cp_parser_friend_p (&decl_specifiers);
12939 /* If there were decl-specifiers, check to see if there was
12940 a class-declaration. */
12941 type = check_tag_decl (&decl_specifiers);
12942 /* Nested classes have already been added to the class, but
12943 a `friend' needs to be explicitly registered. */
12944 if (friend_p)
12945 {
12946 /* If the `friend' keyword was present, the friend must
12947 be introduced with a class-key. */
12948 if (!declares_class_or_enum)
12949 error ("a class-key must be used when declaring a friend");
12950 /* In this case:
12951
12952 template <typename T> struct A {
12953 friend struct A<T>::B;
12954 };
12955
12956 A<T>::B will be represented by a TYPENAME_TYPE, and
12957 therefore not recognized by check_tag_decl. */
12958 if (!type
12959 && decl_specifiers.type
12960 && TYPE_P (decl_specifiers.type))
12961 type = decl_specifiers.type;
12962 if (!type || !TYPE_P (type))
12963 error ("friend declaration does not name a class or "
12964 "function");
12965 else
12966 make_friend_class (current_class_type, type,
12967 /*complain=*/true);
12968 }
12969 /* If there is no TYPE, an error message will already have
12970 been issued. */
12971 else if (!type || type == error_mark_node)
12972 ;
12973 /* An anonymous aggregate has to be handled specially; such
12974 a declaration really declares a data member (with a
12975 particular type), as opposed to a nested class. */
12976 else if (ANON_AGGR_TYPE_P (type))
12977 {
12978 /* Remove constructors and such from TYPE, now that we
12979 know it is an anonymous aggregate. */
12980 fixup_anonymous_aggr (type);
12981 /* And make the corresponding data member. */
12982 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12983 /* Add it to the class. */
12984 finish_member_declaration (decl);
12985 }
12986 else
12987 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12988 }
12989 }
12990 else
12991 {
12992 /* See if these declarations will be friends. */
12993 friend_p = cp_parser_friend_p (&decl_specifiers);
12994
12995 /* Keep going until we hit the `;' at the end of the
12996 declaration. */
12997 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12998 {
12999 tree attributes = NULL_TREE;
13000 tree first_attribute;
13001
13002 /* Peek at the next token. */
13003 token = cp_lexer_peek_token (parser->lexer);
13004
13005 /* Check for a bitfield declaration. */
13006 if (token->type == CPP_COLON
13007 || (token->type == CPP_NAME
13008 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13009 == CPP_COLON))
13010 {
13011 tree identifier;
13012 tree width;
13013
13014 /* Get the name of the bitfield. Note that we cannot just
13015 check TOKEN here because it may have been invalidated by
13016 the call to cp_lexer_peek_nth_token above. */
13017 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13018 identifier = cp_parser_identifier (parser);
13019 else
13020 identifier = NULL_TREE;
13021
13022 /* Consume the `:' token. */
13023 cp_lexer_consume_token (parser->lexer);
13024 /* Get the width of the bitfield. */
13025 width
13026 = cp_parser_constant_expression (parser,
13027 /*allow_non_constant=*/false,
13028 NULL);
13029
13030 /* Look for attributes that apply to the bitfield. */
13031 attributes = cp_parser_attributes_opt (parser);
13032 /* Remember which attributes are prefix attributes and
13033 which are not. */
13034 first_attribute = attributes;
13035 /* Combine the attributes. */
13036 attributes = chainon (prefix_attributes, attributes);
13037
13038 /* Create the bitfield declaration. */
13039 decl = grokbitfield (identifier
13040 ? make_id_declarator (identifier)
13041 : NULL,
13042 &decl_specifiers,
13043 width);
13044 /* Apply the attributes. */
13045 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13046 }
13047 else
13048 {
13049 cp_declarator *declarator;
13050 tree initializer;
13051 tree asm_specification;
13052 int ctor_dtor_or_conv_p;
13053
13054 /* Parse the declarator. */
13055 declarator
13056 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13057 &ctor_dtor_or_conv_p,
13058 /*parenthesized_p=*/NULL,
13059 /*member_p=*/true);
13060
13061 /* If something went wrong parsing the declarator, make sure
13062 that we at least consume some tokens. */
13063 if (declarator == cp_error_declarator)
13064 {
13065 /* Skip to the end of the statement. */
13066 cp_parser_skip_to_end_of_statement (parser);
13067 /* If the next token is not a semicolon, that is
13068 probably because we just skipped over the body of
13069 a function. So, we consume a semicolon if
13070 present, but do not issue an error message if it
13071 is not present. */
13072 if (cp_lexer_next_token_is (parser->lexer,
13073 CPP_SEMICOLON))
13074 cp_lexer_consume_token (parser->lexer);
13075 return;
13076 }
13077
13078 if (declares_class_or_enum & 2)
13079 cp_parser_check_for_definition_in_return_type
13080 (declarator, decl_specifiers.type);
13081
13082 /* Look for an asm-specification. */
13083 asm_specification = cp_parser_asm_specification_opt (parser);
13084 /* Look for attributes that apply to the declaration. */
13085 attributes = cp_parser_attributes_opt (parser);
13086 /* Remember which attributes are prefix attributes and
13087 which are not. */
13088 first_attribute = attributes;
13089 /* Combine the attributes. */
13090 attributes = chainon (prefix_attributes, attributes);
13091
13092 /* If it's an `=', then we have a constant-initializer or a
13093 pure-specifier. It is not correct to parse the
13094 initializer before registering the member declaration
13095 since the member declaration should be in scope while
13096 its initializer is processed. However, the rest of the
13097 front end does not yet provide an interface that allows
13098 us to handle this correctly. */
13099 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13100 {
13101 /* In [class.mem]:
13102
13103 A pure-specifier shall be used only in the declaration of
13104 a virtual function.
13105
13106 A member-declarator can contain a constant-initializer
13107 only if it declares a static member of integral or
13108 enumeration type.
13109
13110 Therefore, if the DECLARATOR is for a function, we look
13111 for a pure-specifier; otherwise, we look for a
13112 constant-initializer. When we call `grokfield', it will
13113 perform more stringent semantics checks. */
13114 if (declarator->kind == cdk_function)
13115 initializer = cp_parser_pure_specifier (parser);
13116 else
13117 /* Parse the initializer. */
13118 initializer = cp_parser_constant_initializer (parser);
13119 }
13120 /* Otherwise, there is no initializer. */
13121 else
13122 initializer = NULL_TREE;
13123
13124 /* See if we are probably looking at a function
13125 definition. We are certainly not looking at at a
13126 member-declarator. Calling `grokfield' has
13127 side-effects, so we must not do it unless we are sure
13128 that we are looking at a member-declarator. */
13129 if (cp_parser_token_starts_function_definition_p
13130 (cp_lexer_peek_token (parser->lexer)))
13131 {
13132 /* The grammar does not allow a pure-specifier to be
13133 used when a member function is defined. (It is
13134 possible that this fact is an oversight in the
13135 standard, since a pure function may be defined
13136 outside of the class-specifier. */
13137 if (initializer)
13138 error ("pure-specifier on function-definition");
13139 decl = cp_parser_save_member_function_body (parser,
13140 &decl_specifiers,
13141 declarator,
13142 attributes);
13143 /* If the member was not a friend, declare it here. */
13144 if (!friend_p)
13145 finish_member_declaration (decl);
13146 /* Peek at the next token. */
13147 token = cp_lexer_peek_token (parser->lexer);
13148 /* If the next token is a semicolon, consume it. */
13149 if (token->type == CPP_SEMICOLON)
13150 cp_lexer_consume_token (parser->lexer);
13151 return;
13152 }
13153 else
13154 {
13155 /* Create the declaration. */
13156 decl = grokfield (declarator, &decl_specifiers,
13157 initializer, asm_specification,
13158 attributes);
13159 /* Any initialization must have been from a
13160 constant-expression. */
13161 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13162 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13163 }
13164 }
13165
13166 /* Reset PREFIX_ATTRIBUTES. */
13167 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13168 attributes = TREE_CHAIN (attributes);
13169 if (attributes)
13170 TREE_CHAIN (attributes) = NULL_TREE;
13171
13172 /* If there is any qualification still in effect, clear it
13173 now; we will be starting fresh with the next declarator. */
13174 parser->scope = NULL_TREE;
13175 parser->qualifying_scope = NULL_TREE;
13176 parser->object_scope = NULL_TREE;
13177 /* If it's a `,', then there are more declarators. */
13178 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13179 cp_lexer_consume_token (parser->lexer);
13180 /* If the next token isn't a `;', then we have a parse error. */
13181 else if (cp_lexer_next_token_is_not (parser->lexer,
13182 CPP_SEMICOLON))
13183 {
13184 cp_parser_error (parser, "expected %<;%>");
13185 /* Skip tokens until we find a `;'. */
13186 cp_parser_skip_to_end_of_statement (parser);
13187
13188 break;
13189 }
13190
13191 if (decl)
13192 {
13193 /* Add DECL to the list of members. */
13194 if (!friend_p)
13195 finish_member_declaration (decl);
13196
13197 if (TREE_CODE (decl) == FUNCTION_DECL)
13198 cp_parser_save_default_args (parser, decl);
13199 }
13200 }
13201 }
13202
13203 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13204 }
13205
13206 /* Parse a pure-specifier.
13207
13208 pure-specifier:
13209 = 0
13210
13211 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13212 Otherwise, ERROR_MARK_NODE is returned. */
13213
13214 static tree
13215 cp_parser_pure_specifier (cp_parser* parser)
13216 {
13217 cp_token *token;
13218
13219 /* Look for the `=' token. */
13220 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13221 return error_mark_node;
13222 /* Look for the `0' token. */
13223 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13224 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13225 to get information from the lexer about how the number was
13226 spelled in order to fix this problem. */
13227 if (!token || !integer_zerop (token->value))
13228 return error_mark_node;
13229
13230 return integer_zero_node;
13231 }
13232
13233 /* Parse a constant-initializer.
13234
13235 constant-initializer:
13236 = constant-expression
13237
13238 Returns a representation of the constant-expression. */
13239
13240 static tree
13241 cp_parser_constant_initializer (cp_parser* parser)
13242 {
13243 /* Look for the `=' token. */
13244 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13245 return error_mark_node;
13246
13247 /* It is invalid to write:
13248
13249 struct S { static const int i = { 7 }; };
13250
13251 */
13252 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13253 {
13254 cp_parser_error (parser,
13255 "a brace-enclosed initializer is not allowed here");
13256 /* Consume the opening brace. */
13257 cp_lexer_consume_token (parser->lexer);
13258 /* Skip the initializer. */
13259 cp_parser_skip_to_closing_brace (parser);
13260 /* Look for the trailing `}'. */
13261 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13262
13263 return error_mark_node;
13264 }
13265
13266 return cp_parser_constant_expression (parser,
13267 /*allow_non_constant=*/false,
13268 NULL);
13269 }
13270
13271 /* Derived classes [gram.class.derived] */
13272
13273 /* Parse a base-clause.
13274
13275 base-clause:
13276 : base-specifier-list
13277
13278 base-specifier-list:
13279 base-specifier
13280 base-specifier-list , base-specifier
13281
13282 Returns a TREE_LIST representing the base-classes, in the order in
13283 which they were declared. The representation of each node is as
13284 described by cp_parser_base_specifier.
13285
13286 In the case that no bases are specified, this function will return
13287 NULL_TREE, not ERROR_MARK_NODE. */
13288
13289 static tree
13290 cp_parser_base_clause (cp_parser* parser)
13291 {
13292 tree bases = NULL_TREE;
13293
13294 /* Look for the `:' that begins the list. */
13295 cp_parser_require (parser, CPP_COLON, "`:'");
13296
13297 /* Scan the base-specifier-list. */
13298 while (true)
13299 {
13300 cp_token *token;
13301 tree base;
13302
13303 /* Look for the base-specifier. */
13304 base = cp_parser_base_specifier (parser);
13305 /* Add BASE to the front of the list. */
13306 if (base != error_mark_node)
13307 {
13308 TREE_CHAIN (base) = bases;
13309 bases = base;
13310 }
13311 /* Peek at the next token. */
13312 token = cp_lexer_peek_token (parser->lexer);
13313 /* If it's not a comma, then the list is complete. */
13314 if (token->type != CPP_COMMA)
13315 break;
13316 /* Consume the `,'. */
13317 cp_lexer_consume_token (parser->lexer);
13318 }
13319
13320 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13321 base class had a qualified name. However, the next name that
13322 appears is certainly not qualified. */
13323 parser->scope = NULL_TREE;
13324 parser->qualifying_scope = NULL_TREE;
13325 parser->object_scope = NULL_TREE;
13326
13327 return nreverse (bases);
13328 }
13329
13330 /* Parse a base-specifier.
13331
13332 base-specifier:
13333 :: [opt] nested-name-specifier [opt] class-name
13334 virtual access-specifier [opt] :: [opt] nested-name-specifier
13335 [opt] class-name
13336 access-specifier virtual [opt] :: [opt] nested-name-specifier
13337 [opt] class-name
13338
13339 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13340 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13341 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13342 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13343
13344 static tree
13345 cp_parser_base_specifier (cp_parser* parser)
13346 {
13347 cp_token *token;
13348 bool done = false;
13349 bool virtual_p = false;
13350 bool duplicate_virtual_error_issued_p = false;
13351 bool duplicate_access_error_issued_p = false;
13352 bool class_scope_p, template_p;
13353 tree access = access_default_node;
13354 tree type;
13355
13356 /* Process the optional `virtual' and `access-specifier'. */
13357 while (!done)
13358 {
13359 /* Peek at the next token. */
13360 token = cp_lexer_peek_token (parser->lexer);
13361 /* Process `virtual'. */
13362 switch (token->keyword)
13363 {
13364 case RID_VIRTUAL:
13365 /* If `virtual' appears more than once, issue an error. */
13366 if (virtual_p && !duplicate_virtual_error_issued_p)
13367 {
13368 cp_parser_error (parser,
13369 "%<virtual%> specified more than once in base-specified");
13370 duplicate_virtual_error_issued_p = true;
13371 }
13372
13373 virtual_p = true;
13374
13375 /* Consume the `virtual' token. */
13376 cp_lexer_consume_token (parser->lexer);
13377
13378 break;
13379
13380 case RID_PUBLIC:
13381 case RID_PROTECTED:
13382 case RID_PRIVATE:
13383 /* If more than one access specifier appears, issue an
13384 error. */
13385 if (access != access_default_node
13386 && !duplicate_access_error_issued_p)
13387 {
13388 cp_parser_error (parser,
13389 "more than one access specifier in base-specified");
13390 duplicate_access_error_issued_p = true;
13391 }
13392
13393 access = ridpointers[(int) token->keyword];
13394
13395 /* Consume the access-specifier. */
13396 cp_lexer_consume_token (parser->lexer);
13397
13398 break;
13399
13400 default:
13401 done = true;
13402 break;
13403 }
13404 }
13405 /* It is not uncommon to see programs mechanically, erroneously, use
13406 the 'typename' keyword to denote (dependent) qualified types
13407 as base classes. */
13408 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13409 {
13410 if (!processing_template_decl)
13411 error ("keyword %<typename%> not allowed outside of templates");
13412 else
13413 error ("keyword %<typename%> not allowed in this context "
13414 "(the base class is implicitly a type)");
13415 cp_lexer_consume_token (parser->lexer);
13416 }
13417
13418 /* Look for the optional `::' operator. */
13419 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13420 /* Look for the nested-name-specifier. The simplest way to
13421 implement:
13422
13423 [temp.res]
13424
13425 The keyword `typename' is not permitted in a base-specifier or
13426 mem-initializer; in these contexts a qualified name that
13427 depends on a template-parameter is implicitly assumed to be a
13428 type name.
13429
13430 is to pretend that we have seen the `typename' keyword at this
13431 point. */
13432 cp_parser_nested_name_specifier_opt (parser,
13433 /*typename_keyword_p=*/true,
13434 /*check_dependency_p=*/true,
13435 typename_type,
13436 /*is_declaration=*/true);
13437 /* If the base class is given by a qualified name, assume that names
13438 we see are type names or templates, as appropriate. */
13439 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13440 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13441
13442 /* Finally, look for the class-name. */
13443 type = cp_parser_class_name (parser,
13444 class_scope_p,
13445 template_p,
13446 typename_type,
13447 /*check_dependency_p=*/true,
13448 /*class_head_p=*/false,
13449 /*is_declaration=*/true);
13450
13451 if (type == error_mark_node)
13452 return error_mark_node;
13453
13454 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13455 }
13456
13457 /* Exception handling [gram.exception] */
13458
13459 /* Parse an (optional) exception-specification.
13460
13461 exception-specification:
13462 throw ( type-id-list [opt] )
13463
13464 Returns a TREE_LIST representing the exception-specification. The
13465 TREE_VALUE of each node is a type. */
13466
13467 static tree
13468 cp_parser_exception_specification_opt (cp_parser* parser)
13469 {
13470 cp_token *token;
13471 tree type_id_list;
13472
13473 /* Peek at the next token. */
13474 token = cp_lexer_peek_token (parser->lexer);
13475 /* If it's not `throw', then there's no exception-specification. */
13476 if (!cp_parser_is_keyword (token, RID_THROW))
13477 return NULL_TREE;
13478
13479 /* Consume the `throw'. */
13480 cp_lexer_consume_token (parser->lexer);
13481
13482 /* Look for the `('. */
13483 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13484
13485 /* Peek at the next token. */
13486 token = cp_lexer_peek_token (parser->lexer);
13487 /* If it's not a `)', then there is a type-id-list. */
13488 if (token->type != CPP_CLOSE_PAREN)
13489 {
13490 const char *saved_message;
13491
13492 /* Types may not be defined in an exception-specification. */
13493 saved_message = parser->type_definition_forbidden_message;
13494 parser->type_definition_forbidden_message
13495 = "types may not be defined in an exception-specification";
13496 /* Parse the type-id-list. */
13497 type_id_list = cp_parser_type_id_list (parser);
13498 /* Restore the saved message. */
13499 parser->type_definition_forbidden_message = saved_message;
13500 }
13501 else
13502 type_id_list = empty_except_spec;
13503
13504 /* Look for the `)'. */
13505 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13506
13507 return type_id_list;
13508 }
13509
13510 /* Parse an (optional) type-id-list.
13511
13512 type-id-list:
13513 type-id
13514 type-id-list , type-id
13515
13516 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13517 in the order that the types were presented. */
13518
13519 static tree
13520 cp_parser_type_id_list (cp_parser* parser)
13521 {
13522 tree types = NULL_TREE;
13523
13524 while (true)
13525 {
13526 cp_token *token;
13527 tree type;
13528
13529 /* Get the next type-id. */
13530 type = cp_parser_type_id (parser);
13531 /* Add it to the list. */
13532 types = add_exception_specifier (types, type, /*complain=*/1);
13533 /* Peek at the next token. */
13534 token = cp_lexer_peek_token (parser->lexer);
13535 /* If it is not a `,', we are done. */
13536 if (token->type != CPP_COMMA)
13537 break;
13538 /* Consume the `,'. */
13539 cp_lexer_consume_token (parser->lexer);
13540 }
13541
13542 return nreverse (types);
13543 }
13544
13545 /* Parse a try-block.
13546
13547 try-block:
13548 try compound-statement handler-seq */
13549
13550 static tree
13551 cp_parser_try_block (cp_parser* parser)
13552 {
13553 tree try_block;
13554
13555 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13556 try_block = begin_try_block ();
13557 cp_parser_compound_statement (parser, NULL, true);
13558 finish_try_block (try_block);
13559 cp_parser_handler_seq (parser);
13560 finish_handler_sequence (try_block);
13561
13562 return try_block;
13563 }
13564
13565 /* Parse a function-try-block.
13566
13567 function-try-block:
13568 try ctor-initializer [opt] function-body handler-seq */
13569
13570 static bool
13571 cp_parser_function_try_block (cp_parser* parser)
13572 {
13573 tree try_block;
13574 bool ctor_initializer_p;
13575
13576 /* Look for the `try' keyword. */
13577 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13578 return false;
13579 /* Let the rest of the front-end know where we are. */
13580 try_block = begin_function_try_block ();
13581 /* Parse the function-body. */
13582 ctor_initializer_p
13583 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13584 /* We're done with the `try' part. */
13585 finish_function_try_block (try_block);
13586 /* Parse the handlers. */
13587 cp_parser_handler_seq (parser);
13588 /* We're done with the handlers. */
13589 finish_function_handler_sequence (try_block);
13590
13591 return ctor_initializer_p;
13592 }
13593
13594 /* Parse a handler-seq.
13595
13596 handler-seq:
13597 handler handler-seq [opt] */
13598
13599 static void
13600 cp_parser_handler_seq (cp_parser* parser)
13601 {
13602 while (true)
13603 {
13604 cp_token *token;
13605
13606 /* Parse the handler. */
13607 cp_parser_handler (parser);
13608 /* Peek at the next token. */
13609 token = cp_lexer_peek_token (parser->lexer);
13610 /* If it's not `catch' then there are no more handlers. */
13611 if (!cp_parser_is_keyword (token, RID_CATCH))
13612 break;
13613 }
13614 }
13615
13616 /* Parse a handler.
13617
13618 handler:
13619 catch ( exception-declaration ) compound-statement */
13620
13621 static void
13622 cp_parser_handler (cp_parser* parser)
13623 {
13624 tree handler;
13625 tree declaration;
13626
13627 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13628 handler = begin_handler ();
13629 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13630 declaration = cp_parser_exception_declaration (parser);
13631 finish_handler_parms (declaration, handler);
13632 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13633 cp_parser_compound_statement (parser, NULL, false);
13634 finish_handler (handler);
13635 }
13636
13637 /* Parse an exception-declaration.
13638
13639 exception-declaration:
13640 type-specifier-seq declarator
13641 type-specifier-seq abstract-declarator
13642 type-specifier-seq
13643 ...
13644
13645 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13646 ellipsis variant is used. */
13647
13648 static tree
13649 cp_parser_exception_declaration (cp_parser* parser)
13650 {
13651 tree decl;
13652 cp_decl_specifier_seq type_specifiers;
13653 cp_declarator *declarator;
13654 const char *saved_message;
13655
13656 /* If it's an ellipsis, it's easy to handle. */
13657 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13658 {
13659 /* Consume the `...' token. */
13660 cp_lexer_consume_token (parser->lexer);
13661 return NULL_TREE;
13662 }
13663
13664 /* Types may not be defined in exception-declarations. */
13665 saved_message = parser->type_definition_forbidden_message;
13666 parser->type_definition_forbidden_message
13667 = "types may not be defined in exception-declarations";
13668
13669 /* Parse the type-specifier-seq. */
13670 cp_parser_type_specifier_seq (parser, &type_specifiers);
13671 /* If it's a `)', then there is no declarator. */
13672 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13673 declarator = NULL;
13674 else
13675 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13676 /*ctor_dtor_or_conv_p=*/NULL,
13677 /*parenthesized_p=*/NULL,
13678 /*member_p=*/false);
13679
13680 /* Restore the saved message. */
13681 parser->type_definition_forbidden_message = saved_message;
13682
13683 if (type_specifiers.any_specifiers_p)
13684 {
13685 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13686 if (decl == NULL_TREE)
13687 error ("invalid catch parameter");
13688 }
13689 else
13690 decl = NULL_TREE;
13691
13692 return decl;
13693 }
13694
13695 /* Parse a throw-expression.
13696
13697 throw-expression:
13698 throw assignment-expression [opt]
13699
13700 Returns a THROW_EXPR representing the throw-expression. */
13701
13702 static tree
13703 cp_parser_throw_expression (cp_parser* parser)
13704 {
13705 tree expression;
13706 cp_token* token;
13707
13708 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13709 token = cp_lexer_peek_token (parser->lexer);
13710 /* Figure out whether or not there is an assignment-expression
13711 following the "throw" keyword. */
13712 if (token->type == CPP_COMMA
13713 || token->type == CPP_SEMICOLON
13714 || token->type == CPP_CLOSE_PAREN
13715 || token->type == CPP_CLOSE_SQUARE
13716 || token->type == CPP_CLOSE_BRACE
13717 || token->type == CPP_COLON)
13718 expression = NULL_TREE;
13719 else
13720 expression = cp_parser_assignment_expression (parser);
13721
13722 return build_throw (expression);
13723 }
13724
13725 /* GNU Extensions */
13726
13727 /* Parse an (optional) asm-specification.
13728
13729 asm-specification:
13730 asm ( string-literal )
13731
13732 If the asm-specification is present, returns a STRING_CST
13733 corresponding to the string-literal. Otherwise, returns
13734 NULL_TREE. */
13735
13736 static tree
13737 cp_parser_asm_specification_opt (cp_parser* parser)
13738 {
13739 cp_token *token;
13740 tree asm_specification;
13741
13742 /* Peek at the next token. */
13743 token = cp_lexer_peek_token (parser->lexer);
13744 /* If the next token isn't the `asm' keyword, then there's no
13745 asm-specification. */
13746 if (!cp_parser_is_keyword (token, RID_ASM))
13747 return NULL_TREE;
13748
13749 /* Consume the `asm' token. */
13750 cp_lexer_consume_token (parser->lexer);
13751 /* Look for the `('. */
13752 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13753
13754 /* Look for the string-literal. */
13755 asm_specification = cp_parser_string_literal (parser, false, false);
13756
13757 /* Look for the `)'. */
13758 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13759
13760 return asm_specification;
13761 }
13762
13763 /* Parse an asm-operand-list.
13764
13765 asm-operand-list:
13766 asm-operand
13767 asm-operand-list , asm-operand
13768
13769 asm-operand:
13770 string-literal ( expression )
13771 [ string-literal ] string-literal ( expression )
13772
13773 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13774 each node is the expression. The TREE_PURPOSE is itself a
13775 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13776 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13777 is a STRING_CST for the string literal before the parenthesis. */
13778
13779 static tree
13780 cp_parser_asm_operand_list (cp_parser* parser)
13781 {
13782 tree asm_operands = NULL_TREE;
13783
13784 while (true)
13785 {
13786 tree string_literal;
13787 tree expression;
13788 tree name;
13789
13790 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13791 {
13792 /* Consume the `[' token. */
13793 cp_lexer_consume_token (parser->lexer);
13794 /* Read the operand name. */
13795 name = cp_parser_identifier (parser);
13796 if (name != error_mark_node)
13797 name = build_string (IDENTIFIER_LENGTH (name),
13798 IDENTIFIER_POINTER (name));
13799 /* Look for the closing `]'. */
13800 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13801 }
13802 else
13803 name = NULL_TREE;
13804 /* Look for the string-literal. */
13805 string_literal = cp_parser_string_literal (parser, false, false);
13806
13807 /* Look for the `('. */
13808 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13809 /* Parse the expression. */
13810 expression = cp_parser_expression (parser);
13811 /* Look for the `)'. */
13812 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13813
13814 /* Add this operand to the list. */
13815 asm_operands = tree_cons (build_tree_list (name, string_literal),
13816 expression,
13817 asm_operands);
13818 /* If the next token is not a `,', there are no more
13819 operands. */
13820 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13821 break;
13822 /* Consume the `,'. */
13823 cp_lexer_consume_token (parser->lexer);
13824 }
13825
13826 return nreverse (asm_operands);
13827 }
13828
13829 /* Parse an asm-clobber-list.
13830
13831 asm-clobber-list:
13832 string-literal
13833 asm-clobber-list , string-literal
13834
13835 Returns a TREE_LIST, indicating the clobbers in the order that they
13836 appeared. The TREE_VALUE of each node is a STRING_CST. */
13837
13838 static tree
13839 cp_parser_asm_clobber_list (cp_parser* parser)
13840 {
13841 tree clobbers = NULL_TREE;
13842
13843 while (true)
13844 {
13845 tree string_literal;
13846
13847 /* Look for the string literal. */
13848 string_literal = cp_parser_string_literal (parser, false, false);
13849 /* Add it to the list. */
13850 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13851 /* If the next token is not a `,', then the list is
13852 complete. */
13853 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13854 break;
13855 /* Consume the `,' token. */
13856 cp_lexer_consume_token (parser->lexer);
13857 }
13858
13859 return clobbers;
13860 }
13861
13862 /* Parse an (optional) series of attributes.
13863
13864 attributes:
13865 attributes attribute
13866
13867 attribute:
13868 __attribute__ (( attribute-list [opt] ))
13869
13870 The return value is as for cp_parser_attribute_list. */
13871
13872 static tree
13873 cp_parser_attributes_opt (cp_parser* parser)
13874 {
13875 tree attributes = NULL_TREE;
13876
13877 while (true)
13878 {
13879 cp_token *token;
13880 tree attribute_list;
13881
13882 /* Peek at the next token. */
13883 token = cp_lexer_peek_token (parser->lexer);
13884 /* If it's not `__attribute__', then we're done. */
13885 if (token->keyword != RID_ATTRIBUTE)
13886 break;
13887
13888 /* Consume the `__attribute__' keyword. */
13889 cp_lexer_consume_token (parser->lexer);
13890 /* Look for the two `(' tokens. */
13891 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13892 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13893
13894 /* Peek at the next token. */
13895 token = cp_lexer_peek_token (parser->lexer);
13896 if (token->type != CPP_CLOSE_PAREN)
13897 /* Parse the attribute-list. */
13898 attribute_list = cp_parser_attribute_list (parser);
13899 else
13900 /* If the next token is a `)', then there is no attribute
13901 list. */
13902 attribute_list = NULL;
13903
13904 /* Look for the two `)' tokens. */
13905 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13906 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13907
13908 /* Add these new attributes to the list. */
13909 attributes = chainon (attributes, attribute_list);
13910 }
13911
13912 return attributes;
13913 }
13914
13915 /* Parse an attribute-list.
13916
13917 attribute-list:
13918 attribute
13919 attribute-list , attribute
13920
13921 attribute:
13922 identifier
13923 identifier ( identifier )
13924 identifier ( identifier , expression-list )
13925 identifier ( expression-list )
13926
13927 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13928 TREE_PURPOSE of each node is the identifier indicating which
13929 attribute is in use. The TREE_VALUE represents the arguments, if
13930 any. */
13931
13932 static tree
13933 cp_parser_attribute_list (cp_parser* parser)
13934 {
13935 tree attribute_list = NULL_TREE;
13936 bool save_translate_strings_p = parser->translate_strings_p;
13937
13938 parser->translate_strings_p = false;
13939 while (true)
13940 {
13941 cp_token *token;
13942 tree identifier;
13943 tree attribute;
13944
13945 /* Look for the identifier. We also allow keywords here; for
13946 example `__attribute__ ((const))' is legal. */
13947 token = cp_lexer_peek_token (parser->lexer);
13948 if (token->type != CPP_NAME
13949 && token->type != CPP_KEYWORD)
13950 return error_mark_node;
13951 /* Consume the token. */
13952 token = cp_lexer_consume_token (parser->lexer);
13953
13954 /* Save away the identifier that indicates which attribute this is. */
13955 identifier = token->value;
13956 attribute = build_tree_list (identifier, NULL_TREE);
13957
13958 /* Peek at the next token. */
13959 token = cp_lexer_peek_token (parser->lexer);
13960 /* If it's an `(', then parse the attribute arguments. */
13961 if (token->type == CPP_OPEN_PAREN)
13962 {
13963 tree arguments;
13964
13965 arguments = (cp_parser_parenthesized_expression_list
13966 (parser, true, /*non_constant_p=*/NULL));
13967 /* Save the identifier and arguments away. */
13968 TREE_VALUE (attribute) = arguments;
13969 }
13970
13971 /* Add this attribute to the list. */
13972 TREE_CHAIN (attribute) = attribute_list;
13973 attribute_list = attribute;
13974
13975 /* Now, look for more attributes. */
13976 token = cp_lexer_peek_token (parser->lexer);
13977 /* If the next token isn't a `,', we're done. */
13978 if (token->type != CPP_COMMA)
13979 break;
13980
13981 /* Consume the comma and keep going. */
13982 cp_lexer_consume_token (parser->lexer);
13983 }
13984 parser->translate_strings_p = save_translate_strings_p;
13985
13986 /* We built up the list in reverse order. */
13987 return nreverse (attribute_list);
13988 }
13989
13990 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13991 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13992 current value of the PEDANTIC flag, regardless of whether or not
13993 the `__extension__' keyword is present. The caller is responsible
13994 for restoring the value of the PEDANTIC flag. */
13995
13996 static bool
13997 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13998 {
13999 /* Save the old value of the PEDANTIC flag. */
14000 *saved_pedantic = pedantic;
14001
14002 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14003 {
14004 /* Consume the `__extension__' token. */
14005 cp_lexer_consume_token (parser->lexer);
14006 /* We're not being pedantic while the `__extension__' keyword is
14007 in effect. */
14008 pedantic = 0;
14009
14010 return true;
14011 }
14012
14013 return false;
14014 }
14015
14016 /* Parse a label declaration.
14017
14018 label-declaration:
14019 __label__ label-declarator-seq ;
14020
14021 label-declarator-seq:
14022 identifier , label-declarator-seq
14023 identifier */
14024
14025 static void
14026 cp_parser_label_declaration (cp_parser* parser)
14027 {
14028 /* Look for the `__label__' keyword. */
14029 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14030
14031 while (true)
14032 {
14033 tree identifier;
14034
14035 /* Look for an identifier. */
14036 identifier = cp_parser_identifier (parser);
14037 /* Declare it as a lobel. */
14038 finish_label_decl (identifier);
14039 /* If the next token is a `;', stop. */
14040 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14041 break;
14042 /* Look for the `,' separating the label declarations. */
14043 cp_parser_require (parser, CPP_COMMA, "`,'");
14044 }
14045
14046 /* Look for the final `;'. */
14047 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14048 }
14049
14050 /* Support Functions */
14051
14052 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14053 NAME should have one of the representations used for an
14054 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14055 is returned. If PARSER->SCOPE is a dependent type, then a
14056 SCOPE_REF is returned.
14057
14058 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14059 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14060 was formed. Abstractly, such entities should not be passed to this
14061 function, because they do not need to be looked up, but it is
14062 simpler to check for this special case here, rather than at the
14063 call-sites.
14064
14065 In cases not explicitly covered above, this function returns a
14066 DECL, OVERLOAD, or baselink representing the result of the lookup.
14067 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14068 is returned.
14069
14070 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14071 (e.g., "struct") that was used. In that case bindings that do not
14072 refer to types are ignored.
14073
14074 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14075 ignored.
14076
14077 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14078 are ignored.
14079
14080 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14081 types.
14082
14083 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14084 results in an ambiguity, and false otherwise. */
14085
14086 static tree
14087 cp_parser_lookup_name (cp_parser *parser, tree name,
14088 enum tag_types tag_type,
14089 bool is_template, bool is_namespace,
14090 bool check_dependency,
14091 bool *ambiguous_p)
14092 {
14093 tree decl;
14094 tree object_type = parser->context->object_type;
14095
14096 /* Assume that the lookup will be unambiguous. */
14097 if (ambiguous_p)
14098 *ambiguous_p = false;
14099
14100 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14101 no longer valid. Note that if we are parsing tentatively, and
14102 the parse fails, OBJECT_TYPE will be automatically restored. */
14103 parser->context->object_type = NULL_TREE;
14104
14105 if (name == error_mark_node)
14106 return error_mark_node;
14107
14108 /* A template-id has already been resolved; there is no lookup to
14109 do. */
14110 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14111 return name;
14112 if (BASELINK_P (name))
14113 {
14114 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14115 == TEMPLATE_ID_EXPR);
14116 return name;
14117 }
14118
14119 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14120 it should already have been checked to make sure that the name
14121 used matches the type being destroyed. */
14122 if (TREE_CODE (name) == BIT_NOT_EXPR)
14123 {
14124 tree type;
14125
14126 /* Figure out to which type this destructor applies. */
14127 if (parser->scope)
14128 type = parser->scope;
14129 else if (object_type)
14130 type = object_type;
14131 else
14132 type = current_class_type;
14133 /* If that's not a class type, there is no destructor. */
14134 if (!type || !CLASS_TYPE_P (type))
14135 return error_mark_node;
14136 if (!CLASSTYPE_DESTRUCTORS (type))
14137 return error_mark_node;
14138 /* If it was a class type, return the destructor. */
14139 return CLASSTYPE_DESTRUCTORS (type);
14140 }
14141
14142 /* By this point, the NAME should be an ordinary identifier. If
14143 the id-expression was a qualified name, the qualifying scope is
14144 stored in PARSER->SCOPE at this point. */
14145 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14146
14147 /* Perform the lookup. */
14148 if (parser->scope)
14149 {
14150 bool dependent_p;
14151
14152 if (parser->scope == error_mark_node)
14153 return error_mark_node;
14154
14155 /* If the SCOPE is dependent, the lookup must be deferred until
14156 the template is instantiated -- unless we are explicitly
14157 looking up names in uninstantiated templates. Even then, we
14158 cannot look up the name if the scope is not a class type; it
14159 might, for example, be a template type parameter. */
14160 dependent_p = (TYPE_P (parser->scope)
14161 && !(parser->in_declarator_p
14162 && currently_open_class (parser->scope))
14163 && dependent_type_p (parser->scope));
14164 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14165 && dependent_p)
14166 {
14167 if (tag_type)
14168 {
14169 tree type;
14170
14171 /* The resolution to Core Issue 180 says that `struct
14172 A::B' should be considered a type-name, even if `A'
14173 is dependent. */
14174 type = make_typename_type (parser->scope, name, tag_type,
14175 /*complain=*/1);
14176 if (tag_type == enum_type)
14177 TYPENAME_IS_ENUM_P (type) = 1;
14178 else if (tag_type != typename_type)
14179 TYPENAME_IS_CLASS_P (type) = 1;
14180 decl = TYPE_NAME (type);
14181 }
14182 else if (is_template)
14183 decl = make_unbound_class_template (parser->scope,
14184 name, NULL_TREE,
14185 /*complain=*/1);
14186 else
14187 decl = build_nt (SCOPE_REF, parser->scope, name);
14188 }
14189 else
14190 {
14191 bool pop_p = false;
14192
14193 /* If PARSER->SCOPE is a dependent type, then it must be a
14194 class type, and we must not be checking dependencies;
14195 otherwise, we would have processed this lookup above. So
14196 that PARSER->SCOPE is not considered a dependent base by
14197 lookup_member, we must enter the scope here. */
14198 if (dependent_p)
14199 pop_p = push_scope (parser->scope);
14200 /* If the PARSER->SCOPE is a a template specialization, it
14201 may be instantiated during name lookup. In that case,
14202 errors may be issued. Even if we rollback the current
14203 tentative parse, those errors are valid. */
14204 decl = lookup_qualified_name (parser->scope, name,
14205 tag_type != none_type,
14206 /*complain=*/true);
14207 if (pop_p)
14208 pop_scope (parser->scope);
14209 }
14210 parser->qualifying_scope = parser->scope;
14211 parser->object_scope = NULL_TREE;
14212 }
14213 else if (object_type)
14214 {
14215 tree object_decl = NULL_TREE;
14216 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14217 OBJECT_TYPE is not a class. */
14218 if (CLASS_TYPE_P (object_type))
14219 /* If the OBJECT_TYPE is a template specialization, it may
14220 be instantiated during name lookup. In that case, errors
14221 may be issued. Even if we rollback the current tentative
14222 parse, those errors are valid. */
14223 object_decl = lookup_member (object_type,
14224 name,
14225 /*protect=*/0,
14226 tag_type != none_type);
14227 /* Look it up in the enclosing context, too. */
14228 decl = lookup_name_real (name, tag_type != none_type,
14229 /*nonclass=*/0,
14230 /*block_p=*/true, is_namespace,
14231 /*flags=*/0);
14232 parser->object_scope = object_type;
14233 parser->qualifying_scope = NULL_TREE;
14234 if (object_decl)
14235 decl = object_decl;
14236 }
14237 else
14238 {
14239 decl = lookup_name_real (name, tag_type != none_type,
14240 /*nonclass=*/0,
14241 /*block_p=*/true, is_namespace,
14242 /*flags=*/0);
14243 parser->qualifying_scope = NULL_TREE;
14244 parser->object_scope = NULL_TREE;
14245 }
14246
14247 /* If the lookup failed, let our caller know. */
14248 if (!decl
14249 || decl == error_mark_node
14250 || (TREE_CODE (decl) == FUNCTION_DECL
14251 && DECL_ANTICIPATED (decl)))
14252 return error_mark_node;
14253
14254 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14255 if (TREE_CODE (decl) == TREE_LIST)
14256 {
14257 if (ambiguous_p)
14258 *ambiguous_p = true;
14259 /* The error message we have to print is too complicated for
14260 cp_parser_error, so we incorporate its actions directly. */
14261 if (!cp_parser_simulate_error (parser))
14262 {
14263 error ("reference to %qD is ambiguous", name);
14264 print_candidates (decl);
14265 }
14266 return error_mark_node;
14267 }
14268
14269 gcc_assert (DECL_P (decl)
14270 || TREE_CODE (decl) == OVERLOAD
14271 || TREE_CODE (decl) == SCOPE_REF
14272 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14273 || BASELINK_P (decl));
14274
14275 /* If we have resolved the name of a member declaration, check to
14276 see if the declaration is accessible. When the name resolves to
14277 set of overloaded functions, accessibility is checked when
14278 overload resolution is done.
14279
14280 During an explicit instantiation, access is not checked at all,
14281 as per [temp.explicit]. */
14282 if (DECL_P (decl))
14283 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14284
14285 return decl;
14286 }
14287
14288 /* Like cp_parser_lookup_name, but for use in the typical case where
14289 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14290 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14291
14292 static tree
14293 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14294 {
14295 return cp_parser_lookup_name (parser, name,
14296 none_type,
14297 /*is_template=*/false,
14298 /*is_namespace=*/false,
14299 /*check_dependency=*/true,
14300 /*ambiguous_p=*/NULL);
14301 }
14302
14303 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14304 the current context, return the TYPE_DECL. If TAG_NAME_P is
14305 true, the DECL indicates the class being defined in a class-head,
14306 or declared in an elaborated-type-specifier.
14307
14308 Otherwise, return DECL. */
14309
14310 static tree
14311 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14312 {
14313 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14314 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14315
14316 struct A {
14317 template <typename T> struct B;
14318 };
14319
14320 template <typename T> struct A::B {};
14321
14322 Similarly, in a elaborated-type-specifier:
14323
14324 namespace N { struct X{}; }
14325
14326 struct A {
14327 template <typename T> friend struct N::X;
14328 };
14329
14330 However, if the DECL refers to a class type, and we are in
14331 the scope of the class, then the name lookup automatically
14332 finds the TYPE_DECL created by build_self_reference rather
14333 than a TEMPLATE_DECL. For example, in:
14334
14335 template <class T> struct S {
14336 S s;
14337 };
14338
14339 there is no need to handle such case. */
14340
14341 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14342 return DECL_TEMPLATE_RESULT (decl);
14343
14344 return decl;
14345 }
14346
14347 /* If too many, or too few, template-parameter lists apply to the
14348 declarator, issue an error message. Returns TRUE if all went well,
14349 and FALSE otherwise. */
14350
14351 static bool
14352 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14353 cp_declarator *declarator)
14354 {
14355 unsigned num_templates;
14356
14357 /* We haven't seen any classes that involve template parameters yet. */
14358 num_templates = 0;
14359
14360 switch (declarator->kind)
14361 {
14362 case cdk_id:
14363 if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14364 {
14365 tree scope;
14366 tree member;
14367
14368 scope = TREE_OPERAND (declarator->u.id.name, 0);
14369 member = TREE_OPERAND (declarator->u.id.name, 1);
14370
14371 while (scope && CLASS_TYPE_P (scope))
14372 {
14373 /* You're supposed to have one `template <...>'
14374 for every template class, but you don't need one
14375 for a full specialization. For example:
14376
14377 template <class T> struct S{};
14378 template <> struct S<int> { void f(); };
14379 void S<int>::f () {}
14380
14381 is correct; there shouldn't be a `template <>' for
14382 the definition of `S<int>::f'. */
14383 if (CLASSTYPE_TEMPLATE_INFO (scope)
14384 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14385 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14386 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14387 ++num_templates;
14388
14389 scope = TYPE_CONTEXT (scope);
14390 }
14391 }
14392
14393 /* If the DECLARATOR has the form `X<y>' then it uses one
14394 additional level of template parameters. */
14395 if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14396 ++num_templates;
14397
14398 return cp_parser_check_template_parameters (parser,
14399 num_templates);
14400
14401 case cdk_function:
14402 case cdk_array:
14403 case cdk_pointer:
14404 case cdk_reference:
14405 case cdk_ptrmem:
14406 return (cp_parser_check_declarator_template_parameters
14407 (parser, declarator->declarator));
14408
14409 case cdk_error:
14410 return true;
14411
14412 default:
14413 gcc_unreachable ();
14414 }
14415 return false;
14416 }
14417
14418 /* NUM_TEMPLATES were used in the current declaration. If that is
14419 invalid, return FALSE and issue an error messages. Otherwise,
14420 return TRUE. */
14421
14422 static bool
14423 cp_parser_check_template_parameters (cp_parser* parser,
14424 unsigned num_templates)
14425 {
14426 /* If there are more template classes than parameter lists, we have
14427 something like:
14428
14429 template <class T> void S<T>::R<T>::f (); */
14430 if (parser->num_template_parameter_lists < num_templates)
14431 {
14432 error ("too few template-parameter-lists");
14433 return false;
14434 }
14435 /* If there are the same number of template classes and parameter
14436 lists, that's OK. */
14437 if (parser->num_template_parameter_lists == num_templates)
14438 return true;
14439 /* If there are more, but only one more, then we are referring to a
14440 member template. That's OK too. */
14441 if (parser->num_template_parameter_lists == num_templates + 1)
14442 return true;
14443 /* Otherwise, there are too many template parameter lists. We have
14444 something like:
14445
14446 template <class T> template <class U> void S::f(); */
14447 error ("too many template-parameter-lists");
14448 return false;
14449 }
14450
14451 /* Parse an optional `::' token indicating that the following name is
14452 from the global namespace. If so, PARSER->SCOPE is set to the
14453 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14454 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14455 Returns the new value of PARSER->SCOPE, if the `::' token is
14456 present, and NULL_TREE otherwise. */
14457
14458 static tree
14459 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14460 {
14461 cp_token *token;
14462
14463 /* Peek at the next token. */
14464 token = cp_lexer_peek_token (parser->lexer);
14465 /* If we're looking at a `::' token then we're starting from the
14466 global namespace, not our current location. */
14467 if (token->type == CPP_SCOPE)
14468 {
14469 /* Consume the `::' token. */
14470 cp_lexer_consume_token (parser->lexer);
14471 /* Set the SCOPE so that we know where to start the lookup. */
14472 parser->scope = global_namespace;
14473 parser->qualifying_scope = global_namespace;
14474 parser->object_scope = NULL_TREE;
14475
14476 return parser->scope;
14477 }
14478 else if (!current_scope_valid_p)
14479 {
14480 parser->scope = NULL_TREE;
14481 parser->qualifying_scope = NULL_TREE;
14482 parser->object_scope = NULL_TREE;
14483 }
14484
14485 return NULL_TREE;
14486 }
14487
14488 /* Returns TRUE if the upcoming token sequence is the start of a
14489 constructor declarator. If FRIEND_P is true, the declarator is
14490 preceded by the `friend' specifier. */
14491
14492 static bool
14493 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14494 {
14495 bool constructor_p;
14496 tree type_decl = NULL_TREE;
14497 bool nested_name_p;
14498 cp_token *next_token;
14499
14500 /* The common case is that this is not a constructor declarator, so
14501 try to avoid doing lots of work if at all possible. It's not
14502 valid declare a constructor at function scope. */
14503 if (at_function_scope_p ())
14504 return false;
14505 /* And only certain tokens can begin a constructor declarator. */
14506 next_token = cp_lexer_peek_token (parser->lexer);
14507 if (next_token->type != CPP_NAME
14508 && next_token->type != CPP_SCOPE
14509 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14510 && next_token->type != CPP_TEMPLATE_ID)
14511 return false;
14512
14513 /* Parse tentatively; we are going to roll back all of the tokens
14514 consumed here. */
14515 cp_parser_parse_tentatively (parser);
14516 /* Assume that we are looking at a constructor declarator. */
14517 constructor_p = true;
14518
14519 /* Look for the optional `::' operator. */
14520 cp_parser_global_scope_opt (parser,
14521 /*current_scope_valid_p=*/false);
14522 /* Look for the nested-name-specifier. */
14523 nested_name_p
14524 = (cp_parser_nested_name_specifier_opt (parser,
14525 /*typename_keyword_p=*/false,
14526 /*check_dependency_p=*/false,
14527 /*type_p=*/false,
14528 /*is_declaration=*/false)
14529 != NULL_TREE);
14530 /* Outside of a class-specifier, there must be a
14531 nested-name-specifier. */
14532 if (!nested_name_p &&
14533 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14534 || friend_p))
14535 constructor_p = false;
14536 /* If we still think that this might be a constructor-declarator,
14537 look for a class-name. */
14538 if (constructor_p)
14539 {
14540 /* If we have:
14541
14542 template <typename T> struct S { S(); };
14543 template <typename T> S<T>::S ();
14544
14545 we must recognize that the nested `S' names a class.
14546 Similarly, for:
14547
14548 template <typename T> S<T>::S<T> ();
14549
14550 we must recognize that the nested `S' names a template. */
14551 type_decl = cp_parser_class_name (parser,
14552 /*typename_keyword_p=*/false,
14553 /*template_keyword_p=*/false,
14554 none_type,
14555 /*check_dependency_p=*/false,
14556 /*class_head_p=*/false,
14557 /*is_declaration=*/false);
14558 /* If there was no class-name, then this is not a constructor. */
14559 constructor_p = !cp_parser_error_occurred (parser);
14560 }
14561
14562 /* If we're still considering a constructor, we have to see a `(',
14563 to begin the parameter-declaration-clause, followed by either a
14564 `)', an `...', or a decl-specifier. We need to check for a
14565 type-specifier to avoid being fooled into thinking that:
14566
14567 S::S (f) (int);
14568
14569 is a constructor. (It is actually a function named `f' that
14570 takes one parameter (of type `int') and returns a value of type
14571 `S::S'. */
14572 if (constructor_p
14573 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14574 {
14575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14576 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14577 /* A parameter declaration begins with a decl-specifier,
14578 which is either the "attribute" keyword, a storage class
14579 specifier, or (usually) a type-specifier. */
14580 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14581 && !cp_parser_storage_class_specifier_opt (parser))
14582 {
14583 tree type;
14584 bool pop_p = false;
14585 unsigned saved_num_template_parameter_lists;
14586
14587 /* Names appearing in the type-specifier should be looked up
14588 in the scope of the class. */
14589 if (current_class_type)
14590 type = NULL_TREE;
14591 else
14592 {
14593 type = TREE_TYPE (type_decl);
14594 if (TREE_CODE (type) == TYPENAME_TYPE)
14595 {
14596 type = resolve_typename_type (type,
14597 /*only_current_p=*/false);
14598 if (type == error_mark_node)
14599 {
14600 cp_parser_abort_tentative_parse (parser);
14601 return false;
14602 }
14603 }
14604 pop_p = push_scope (type);
14605 }
14606
14607 /* Inside the constructor parameter list, surrounding
14608 template-parameter-lists do not apply. */
14609 saved_num_template_parameter_lists
14610 = parser->num_template_parameter_lists;
14611 parser->num_template_parameter_lists = 0;
14612
14613 /* Look for the type-specifier. */
14614 cp_parser_type_specifier (parser,
14615 CP_PARSER_FLAGS_NONE,
14616 /*decl_specs=*/NULL,
14617 /*is_declarator=*/true,
14618 /*declares_class_or_enum=*/NULL,
14619 /*is_cv_qualifier=*/NULL);
14620
14621 parser->num_template_parameter_lists
14622 = saved_num_template_parameter_lists;
14623
14624 /* Leave the scope of the class. */
14625 if (pop_p)
14626 pop_scope (type);
14627
14628 constructor_p = !cp_parser_error_occurred (parser);
14629 }
14630 }
14631 else
14632 constructor_p = false;
14633 /* We did not really want to consume any tokens. */
14634 cp_parser_abort_tentative_parse (parser);
14635
14636 return constructor_p;
14637 }
14638
14639 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14640 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14641 they must be performed once we are in the scope of the function.
14642
14643 Returns the function defined. */
14644
14645 static tree
14646 cp_parser_function_definition_from_specifiers_and_declarator
14647 (cp_parser* parser,
14648 cp_decl_specifier_seq *decl_specifiers,
14649 tree attributes,
14650 const cp_declarator *declarator)
14651 {
14652 tree fn;
14653 bool success_p;
14654
14655 /* Begin the function-definition. */
14656 success_p = start_function (decl_specifiers, declarator, attributes);
14657
14658 /* The things we're about to see are not directly qualified by any
14659 template headers we've seen thus far. */
14660 reset_specialization ();
14661
14662 /* If there were names looked up in the decl-specifier-seq that we
14663 did not check, check them now. We must wait until we are in the
14664 scope of the function to perform the checks, since the function
14665 might be a friend. */
14666 perform_deferred_access_checks ();
14667
14668 if (!success_p)
14669 {
14670 /* Skip the entire function. */
14671 error ("invalid function declaration");
14672 cp_parser_skip_to_end_of_block_or_statement (parser);
14673 fn = error_mark_node;
14674 }
14675 else
14676 fn = cp_parser_function_definition_after_declarator (parser,
14677 /*inline_p=*/false);
14678
14679 return fn;
14680 }
14681
14682 /* Parse the part of a function-definition that follows the
14683 declarator. INLINE_P is TRUE iff this function is an inline
14684 function defined with a class-specifier.
14685
14686 Returns the function defined. */
14687
14688 static tree
14689 cp_parser_function_definition_after_declarator (cp_parser* parser,
14690 bool inline_p)
14691 {
14692 tree fn;
14693 bool ctor_initializer_p = false;
14694 bool saved_in_unbraced_linkage_specification_p;
14695 unsigned saved_num_template_parameter_lists;
14696
14697 /* If the next token is `return', then the code may be trying to
14698 make use of the "named return value" extension that G++ used to
14699 support. */
14700 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14701 {
14702 /* Consume the `return' keyword. */
14703 cp_lexer_consume_token (parser->lexer);
14704 /* Look for the identifier that indicates what value is to be
14705 returned. */
14706 cp_parser_identifier (parser);
14707 /* Issue an error message. */
14708 error ("named return values are no longer supported");
14709 /* Skip tokens until we reach the start of the function body. */
14710 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14711 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14712 cp_lexer_consume_token (parser->lexer);
14713 }
14714 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14715 anything declared inside `f'. */
14716 saved_in_unbraced_linkage_specification_p
14717 = parser->in_unbraced_linkage_specification_p;
14718 parser->in_unbraced_linkage_specification_p = false;
14719 /* Inside the function, surrounding template-parameter-lists do not
14720 apply. */
14721 saved_num_template_parameter_lists
14722 = parser->num_template_parameter_lists;
14723 parser->num_template_parameter_lists = 0;
14724 /* If the next token is `try', then we are looking at a
14725 function-try-block. */
14726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14727 ctor_initializer_p = cp_parser_function_try_block (parser);
14728 /* A function-try-block includes the function-body, so we only do
14729 this next part if we're not processing a function-try-block. */
14730 else
14731 ctor_initializer_p
14732 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14733
14734 /* Finish the function. */
14735 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14736 (inline_p ? 2 : 0));
14737 /* Generate code for it, if necessary. */
14738 expand_or_defer_fn (fn);
14739 /* Restore the saved values. */
14740 parser->in_unbraced_linkage_specification_p
14741 = saved_in_unbraced_linkage_specification_p;
14742 parser->num_template_parameter_lists
14743 = saved_num_template_parameter_lists;
14744
14745 return fn;
14746 }
14747
14748 /* Parse a template-declaration, assuming that the `export' (and
14749 `extern') keywords, if present, has already been scanned. MEMBER_P
14750 is as for cp_parser_template_declaration. */
14751
14752 static void
14753 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14754 {
14755 tree decl = NULL_TREE;
14756 tree parameter_list;
14757 bool friend_p = false;
14758
14759 /* Look for the `template' keyword. */
14760 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14761 return;
14762
14763 /* And the `<'. */
14764 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14765 return;
14766
14767 /* If the next token is `>', then we have an invalid
14768 specialization. Rather than complain about an invalid template
14769 parameter, issue an error message here. */
14770 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14771 {
14772 cp_parser_error (parser, "invalid explicit specialization");
14773 begin_specialization ();
14774 parameter_list = NULL_TREE;
14775 }
14776 else
14777 {
14778 /* Parse the template parameters. */
14779 begin_template_parm_list ();
14780 parameter_list = cp_parser_template_parameter_list (parser);
14781 parameter_list = end_template_parm_list (parameter_list);
14782 }
14783
14784 /* Look for the `>'. */
14785 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14786 /* We just processed one more parameter list. */
14787 ++parser->num_template_parameter_lists;
14788 /* If the next token is `template', there are more template
14789 parameters. */
14790 if (cp_lexer_next_token_is_keyword (parser->lexer,
14791 RID_TEMPLATE))
14792 cp_parser_template_declaration_after_export (parser, member_p);
14793 else
14794 {
14795 /* There are no access checks when parsing a template, as we do not
14796 know if a specialization will be a friend. */
14797 push_deferring_access_checks (dk_no_check);
14798
14799 decl = cp_parser_single_declaration (parser,
14800 member_p,
14801 &friend_p);
14802
14803 pop_deferring_access_checks ();
14804
14805 /* If this is a member template declaration, let the front
14806 end know. */
14807 if (member_p && !friend_p && decl)
14808 {
14809 if (TREE_CODE (decl) == TYPE_DECL)
14810 cp_parser_check_access_in_redeclaration (decl);
14811
14812 decl = finish_member_template_decl (decl);
14813 }
14814 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14815 make_friend_class (current_class_type, TREE_TYPE (decl),
14816 /*complain=*/true);
14817 }
14818 /* We are done with the current parameter list. */
14819 --parser->num_template_parameter_lists;
14820
14821 /* Finish up. */
14822 finish_template_decl (parameter_list);
14823
14824 /* Register member declarations. */
14825 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14826 finish_member_declaration (decl);
14827
14828 /* If DECL is a function template, we must return to parse it later.
14829 (Even though there is no definition, there might be default
14830 arguments that need handling.) */
14831 if (member_p && decl
14832 && (TREE_CODE (decl) == FUNCTION_DECL
14833 || DECL_FUNCTION_TEMPLATE_P (decl)))
14834 TREE_VALUE (parser->unparsed_functions_queues)
14835 = tree_cons (NULL_TREE, decl,
14836 TREE_VALUE (parser->unparsed_functions_queues));
14837 }
14838
14839 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14840 `function-definition' sequence. MEMBER_P is true, this declaration
14841 appears in a class scope.
14842
14843 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14844 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14845
14846 static tree
14847 cp_parser_single_declaration (cp_parser* parser,
14848 bool member_p,
14849 bool* friend_p)
14850 {
14851 int declares_class_or_enum;
14852 tree decl = NULL_TREE;
14853 cp_decl_specifier_seq decl_specifiers;
14854 bool function_definition_p = false;
14855
14856 /* This function is only used when processing a template
14857 declaration. */
14858 gcc_assert (innermost_scope_kind () == sk_template_parms
14859 || innermost_scope_kind () == sk_template_spec);
14860
14861 /* Defer access checks until we know what is being declared. */
14862 push_deferring_access_checks (dk_deferred);
14863
14864 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14865 alternative. */
14866 cp_parser_decl_specifier_seq (parser,
14867 CP_PARSER_FLAGS_OPTIONAL,
14868 &decl_specifiers,
14869 &declares_class_or_enum);
14870 if (friend_p)
14871 *friend_p = cp_parser_friend_p (&decl_specifiers);
14872
14873 /* There are no template typedefs. */
14874 if (decl_specifiers.specs[(int) ds_typedef])
14875 {
14876 error ("template declaration of %qs", "typedef");
14877 decl = error_mark_node;
14878 }
14879
14880 /* Gather up the access checks that occurred the
14881 decl-specifier-seq. */
14882 stop_deferring_access_checks ();
14883
14884 /* Check for the declaration of a template class. */
14885 if (declares_class_or_enum)
14886 {
14887 if (cp_parser_declares_only_class_p (parser))
14888 {
14889 decl = shadow_tag (&decl_specifiers);
14890
14891 /* In this case:
14892
14893 struct C {
14894 friend template <typename T> struct A<T>::B;
14895 };
14896
14897 A<T>::B will be represented by a TYPENAME_TYPE, and
14898 therefore not recognized by shadow_tag. */
14899 if (friend_p && *friend_p
14900 && !decl
14901 && decl_specifiers.type
14902 && TYPE_P (decl_specifiers.type))
14903 decl = decl_specifiers.type;
14904
14905 if (decl && decl != error_mark_node)
14906 decl = TYPE_NAME (decl);
14907 else
14908 decl = error_mark_node;
14909 }
14910 }
14911 /* If it's not a template class, try for a template function. If
14912 the next token is a `;', then this declaration does not declare
14913 anything. But, if there were errors in the decl-specifiers, then
14914 the error might well have come from an attempted class-specifier.
14915 In that case, there's no need to warn about a missing declarator. */
14916 if (!decl
14917 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14918 || decl_specifiers.type != error_mark_node))
14919 decl = cp_parser_init_declarator (parser,
14920 &decl_specifiers,
14921 /*function_definition_allowed_p=*/true,
14922 member_p,
14923 declares_class_or_enum,
14924 &function_definition_p);
14925
14926 pop_deferring_access_checks ();
14927
14928 /* Clear any current qualification; whatever comes next is the start
14929 of something new. */
14930 parser->scope = NULL_TREE;
14931 parser->qualifying_scope = NULL_TREE;
14932 parser->object_scope = NULL_TREE;
14933 /* Look for a trailing `;' after the declaration. */
14934 if (!function_definition_p
14935 && (decl == error_mark_node
14936 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
14937 cp_parser_skip_to_end_of_block_or_statement (parser);
14938
14939 return decl;
14940 }
14941
14942 /* Parse a cast-expression that is not the operand of a unary "&". */
14943
14944 static tree
14945 cp_parser_simple_cast_expression (cp_parser *parser)
14946 {
14947 return cp_parser_cast_expression (parser, /*address_p=*/false);
14948 }
14949
14950 /* Parse a functional cast to TYPE. Returns an expression
14951 representing the cast. */
14952
14953 static tree
14954 cp_parser_functional_cast (cp_parser* parser, tree type)
14955 {
14956 tree expression_list;
14957 tree cast;
14958
14959 expression_list
14960 = cp_parser_parenthesized_expression_list (parser, false,
14961 /*non_constant_p=*/NULL);
14962
14963 cast = build_functional_cast (type, expression_list);
14964 /* [expr.const]/1: In an integral constant expression "only type
14965 conversions to integral or enumeration type can be used". */
14966 if (cast != error_mark_node && !type_dependent_expression_p (type)
14967 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14968 {
14969 if (cp_parser_non_integral_constant_expression
14970 (parser, "a call to a constructor"))
14971 return error_mark_node;
14972 }
14973 return cast;
14974 }
14975
14976 /* Save the tokens that make up the body of a member function defined
14977 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14978 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14979 specifiers applied to the declaration. Returns the FUNCTION_DECL
14980 for the member function. */
14981
14982 static tree
14983 cp_parser_save_member_function_body (cp_parser* parser,
14984 cp_decl_specifier_seq *decl_specifiers,
14985 cp_declarator *declarator,
14986 tree attributes)
14987 {
14988 cp_token *first;
14989 cp_token *last;
14990 tree fn;
14991
14992 /* Create the function-declaration. */
14993 fn = start_method (decl_specifiers, declarator, attributes);
14994 /* If something went badly wrong, bail out now. */
14995 if (fn == error_mark_node)
14996 {
14997 /* If there's a function-body, skip it. */
14998 if (cp_parser_token_starts_function_definition_p
14999 (cp_lexer_peek_token (parser->lexer)))
15000 cp_parser_skip_to_end_of_block_or_statement (parser);
15001 return error_mark_node;
15002 }
15003
15004 /* Remember it, if there default args to post process. */
15005 cp_parser_save_default_args (parser, fn);
15006
15007 /* Save away the tokens that make up the body of the
15008 function. */
15009 first = parser->lexer->next_token;
15010 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15011 /* Handle function try blocks. */
15012 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15013 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15014 last = parser->lexer->next_token;
15015
15016 /* Save away the inline definition; we will process it when the
15017 class is complete. */
15018 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15019 DECL_PENDING_INLINE_P (fn) = 1;
15020
15021 /* We need to know that this was defined in the class, so that
15022 friend templates are handled correctly. */
15023 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15024
15025 /* We're done with the inline definition. */
15026 finish_method (fn);
15027
15028 /* Add FN to the queue of functions to be parsed later. */
15029 TREE_VALUE (parser->unparsed_functions_queues)
15030 = tree_cons (NULL_TREE, fn,
15031 TREE_VALUE (parser->unparsed_functions_queues));
15032
15033 return fn;
15034 }
15035
15036 /* Parse a template-argument-list, as well as the trailing ">" (but
15037 not the opening ">"). See cp_parser_template_argument_list for the
15038 return value. */
15039
15040 static tree
15041 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15042 {
15043 tree arguments;
15044 tree saved_scope;
15045 tree saved_qualifying_scope;
15046 tree saved_object_scope;
15047 bool saved_greater_than_is_operator_p;
15048
15049 /* [temp.names]
15050
15051 When parsing a template-id, the first non-nested `>' is taken as
15052 the end of the template-argument-list rather than a greater-than
15053 operator. */
15054 saved_greater_than_is_operator_p
15055 = parser->greater_than_is_operator_p;
15056 parser->greater_than_is_operator_p = false;
15057 /* Parsing the argument list may modify SCOPE, so we save it
15058 here. */
15059 saved_scope = parser->scope;
15060 saved_qualifying_scope = parser->qualifying_scope;
15061 saved_object_scope = parser->object_scope;
15062 /* Parse the template-argument-list itself. */
15063 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15064 arguments = NULL_TREE;
15065 else
15066 arguments = cp_parser_template_argument_list (parser);
15067 /* Look for the `>' that ends the template-argument-list. If we find
15068 a '>>' instead, it's probably just a typo. */
15069 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15070 {
15071 if (!saved_greater_than_is_operator_p)
15072 {
15073 /* If we're in a nested template argument list, the '>>' has
15074 to be a typo for '> >'. We emit the error message, but we
15075 continue parsing and we push a '>' as next token, so that
15076 the argument list will be parsed correctly. Note that the
15077 global source location is still on the token before the
15078 '>>', so we need to say explicitly where we want it. */
15079 cp_token *token = cp_lexer_peek_token (parser->lexer);
15080 error ("%H%<>>%> should be %<> >%> "
15081 "within a nested template argument list",
15082 &token->location);
15083
15084 /* ??? Proper recovery should terminate two levels of
15085 template argument list here. */
15086 token->type = CPP_GREATER;
15087 }
15088 else
15089 {
15090 /* If this is not a nested template argument list, the '>>'
15091 is a typo for '>'. Emit an error message and continue.
15092 Same deal about the token location, but here we can get it
15093 right by consuming the '>>' before issuing the diagnostic. */
15094 cp_lexer_consume_token (parser->lexer);
15095 error ("spurious %<>>%>, use %<>%> to terminate "
15096 "a template argument list");
15097 }
15098 }
15099 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15100 error ("missing %<>%> to terminate the template argument list");
15101 else
15102 /* It's what we want, a '>'; consume it. */
15103 cp_lexer_consume_token (parser->lexer);
15104 /* The `>' token might be a greater-than operator again now. */
15105 parser->greater_than_is_operator_p
15106 = saved_greater_than_is_operator_p;
15107 /* Restore the SAVED_SCOPE. */
15108 parser->scope = saved_scope;
15109 parser->qualifying_scope = saved_qualifying_scope;
15110 parser->object_scope = saved_object_scope;
15111
15112 return arguments;
15113 }
15114
15115 /* MEMBER_FUNCTION is a member function, or a friend. If default
15116 arguments, or the body of the function have not yet been parsed,
15117 parse them now. */
15118
15119 static void
15120 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15121 {
15122 /* If this member is a template, get the underlying
15123 FUNCTION_DECL. */
15124 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15125 member_function = DECL_TEMPLATE_RESULT (member_function);
15126
15127 /* There should not be any class definitions in progress at this
15128 point; the bodies of members are only parsed outside of all class
15129 definitions. */
15130 gcc_assert (parser->num_classes_being_defined == 0);
15131 /* While we're parsing the member functions we might encounter more
15132 classes. We want to handle them right away, but we don't want
15133 them getting mixed up with functions that are currently in the
15134 queue. */
15135 parser->unparsed_functions_queues
15136 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15137
15138 /* Make sure that any template parameters are in scope. */
15139 maybe_begin_member_template_processing (member_function);
15140
15141 /* If the body of the function has not yet been parsed, parse it
15142 now. */
15143 if (DECL_PENDING_INLINE_P (member_function))
15144 {
15145 tree function_scope;
15146 cp_token_cache *tokens;
15147
15148 /* The function is no longer pending; we are processing it. */
15149 tokens = DECL_PENDING_INLINE_INFO (member_function);
15150 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15151 DECL_PENDING_INLINE_P (member_function) = 0;
15152 /* If this was an inline function in a local class, enter the scope
15153 of the containing function. */
15154 function_scope = decl_function_context (member_function);
15155 if (function_scope)
15156 push_function_context_to (function_scope);
15157
15158 /* Push the body of the function onto the lexer stack. */
15159 cp_parser_push_lexer_for_tokens (parser, tokens);
15160
15161 /* Let the front end know that we going to be defining this
15162 function. */
15163 start_preparsed_function (member_function, NULL_TREE,
15164 SF_PRE_PARSED | SF_INCLASS_INLINE);
15165
15166 /* Now, parse the body of the function. */
15167 cp_parser_function_definition_after_declarator (parser,
15168 /*inline_p=*/true);
15169
15170 /* Leave the scope of the containing function. */
15171 if (function_scope)
15172 pop_function_context_from (function_scope);
15173 cp_parser_pop_lexer (parser);
15174 }
15175
15176 /* Remove any template parameters from the symbol table. */
15177 maybe_end_member_template_processing ();
15178
15179 /* Restore the queue. */
15180 parser->unparsed_functions_queues
15181 = TREE_CHAIN (parser->unparsed_functions_queues);
15182 }
15183
15184 /* If DECL contains any default args, remember it on the unparsed
15185 functions queue. */
15186
15187 static void
15188 cp_parser_save_default_args (cp_parser* parser, tree decl)
15189 {
15190 tree probe;
15191
15192 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15193 probe;
15194 probe = TREE_CHAIN (probe))
15195 if (TREE_PURPOSE (probe))
15196 {
15197 TREE_PURPOSE (parser->unparsed_functions_queues)
15198 = tree_cons (current_class_type, decl,
15199 TREE_PURPOSE (parser->unparsed_functions_queues));
15200 break;
15201 }
15202 return;
15203 }
15204
15205 /* FN is a FUNCTION_DECL which may contains a parameter with an
15206 unparsed DEFAULT_ARG. Parse the default args now. This function
15207 assumes that the current scope is the scope in which the default
15208 argument should be processed. */
15209
15210 static void
15211 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15212 {
15213 bool saved_local_variables_forbidden_p;
15214 tree parm;
15215
15216 /* While we're parsing the default args, we might (due to the
15217 statement expression extension) encounter more classes. We want
15218 to handle them right away, but we don't want them getting mixed
15219 up with default args that are currently in the queue. */
15220 parser->unparsed_functions_queues
15221 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15222
15223 /* Local variable names (and the `this' keyword) may not appear
15224 in a default argument. */
15225 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15226 parser->local_variables_forbidden_p = true;
15227
15228 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15229 parm;
15230 parm = TREE_CHAIN (parm))
15231 {
15232 cp_token_cache *tokens;
15233
15234 if (!TREE_PURPOSE (parm)
15235 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15236 continue;
15237
15238 /* Push the saved tokens for the default argument onto the parser's
15239 lexer stack. */
15240 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15241 cp_parser_push_lexer_for_tokens (parser, tokens);
15242
15243 /* Parse the assignment-expression. */
15244 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
15245
15246 /* If the token stream has not been completely used up, then
15247 there was extra junk after the end of the default
15248 argument. */
15249 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15250 cp_parser_error (parser, "expected %<,%>");
15251
15252 /* Revert to the main lexer. */
15253 cp_parser_pop_lexer (parser);
15254 }
15255
15256 /* Restore the state of local_variables_forbidden_p. */
15257 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15258
15259 /* Restore the queue. */
15260 parser->unparsed_functions_queues
15261 = TREE_CHAIN (parser->unparsed_functions_queues);
15262 }
15263
15264 /* Parse the operand of `sizeof' (or a similar operator). Returns
15265 either a TYPE or an expression, depending on the form of the
15266 input. The KEYWORD indicates which kind of expression we have
15267 encountered. */
15268
15269 static tree
15270 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15271 {
15272 static const char *format;
15273 tree expr = NULL_TREE;
15274 const char *saved_message;
15275 bool saved_integral_constant_expression_p;
15276
15277 /* Initialize FORMAT the first time we get here. */
15278 if (!format)
15279 format = "types may not be defined in '%s' expressions";
15280
15281 /* Types cannot be defined in a `sizeof' expression. Save away the
15282 old message. */
15283 saved_message = parser->type_definition_forbidden_message;
15284 /* And create the new one. */
15285 parser->type_definition_forbidden_message
15286 = xmalloc (strlen (format)
15287 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15288 + 1 /* `\0' */);
15289 sprintf ((char *) parser->type_definition_forbidden_message,
15290 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15291
15292 /* The restrictions on constant-expressions do not apply inside
15293 sizeof expressions. */
15294 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15295 parser->integral_constant_expression_p = false;
15296
15297 /* Do not actually evaluate the expression. */
15298 ++skip_evaluation;
15299 /* If it's a `(', then we might be looking at the type-id
15300 construction. */
15301 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15302 {
15303 tree type;
15304 bool saved_in_type_id_in_expr_p;
15305
15306 /* We can't be sure yet whether we're looking at a type-id or an
15307 expression. */
15308 cp_parser_parse_tentatively (parser);
15309 /* Consume the `('. */
15310 cp_lexer_consume_token (parser->lexer);
15311 /* Parse the type-id. */
15312 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15313 parser->in_type_id_in_expr_p = true;
15314 type = cp_parser_type_id (parser);
15315 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15316 /* Now, look for the trailing `)'. */
15317 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15318 /* If all went well, then we're done. */
15319 if (cp_parser_parse_definitely (parser))
15320 {
15321 cp_decl_specifier_seq decl_specs;
15322
15323 /* Build a trivial decl-specifier-seq. */
15324 clear_decl_specs (&decl_specs);
15325 decl_specs.type = type;
15326
15327 /* Call grokdeclarator to figure out what type this is. */
15328 expr = grokdeclarator (NULL,
15329 &decl_specs,
15330 TYPENAME,
15331 /*initialized=*/0,
15332 /*attrlist=*/NULL);
15333 }
15334 }
15335
15336 /* If the type-id production did not work out, then we must be
15337 looking at the unary-expression production. */
15338 if (!expr)
15339 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15340 /* Go back to evaluating expressions. */
15341 --skip_evaluation;
15342
15343 /* Free the message we created. */
15344 free ((char *) parser->type_definition_forbidden_message);
15345 /* And restore the old one. */
15346 parser->type_definition_forbidden_message = saved_message;
15347 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15348
15349 return expr;
15350 }
15351
15352 /* If the current declaration has no declarator, return true. */
15353
15354 static bool
15355 cp_parser_declares_only_class_p (cp_parser *parser)
15356 {
15357 /* If the next token is a `;' or a `,' then there is no
15358 declarator. */
15359 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15360 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15361 }
15362
15363 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15364
15365 static void
15366 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15367 cp_storage_class storage_class)
15368 {
15369 if (decl_specs->storage_class != sc_none)
15370 decl_specs->multiple_storage_classes_p = true;
15371 else
15372 decl_specs->storage_class = storage_class;
15373 }
15374
15375 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15376 is true, the type is a user-defined type; otherwise it is a
15377 built-in type specified by a keyword. */
15378
15379 static void
15380 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15381 tree type_spec,
15382 bool user_defined_p)
15383 {
15384 decl_specs->any_specifiers_p = true;
15385
15386 /* If the user tries to redeclare bool or wchar_t (with, for
15387 example, in "typedef int wchar_t;") we remember that this is what
15388 happened. In system headers, we ignore these declarations so
15389 that G++ can work with system headers that are not C++-safe. */
15390 if (decl_specs->specs[(int) ds_typedef]
15391 && !user_defined_p
15392 && (type_spec == boolean_type_node
15393 || type_spec == wchar_type_node)
15394 && (decl_specs->type
15395 || decl_specs->specs[(int) ds_long]
15396 || decl_specs->specs[(int) ds_short]
15397 || decl_specs->specs[(int) ds_unsigned]
15398 || decl_specs->specs[(int) ds_signed]))
15399 {
15400 decl_specs->redefined_builtin_type = type_spec;
15401 if (!decl_specs->type)
15402 {
15403 decl_specs->type = type_spec;
15404 decl_specs->user_defined_type_p = false;
15405 }
15406 }
15407 else if (decl_specs->type)
15408 decl_specs->multiple_types_p = true;
15409 else
15410 {
15411 decl_specs->type = type_spec;
15412 decl_specs->user_defined_type_p = user_defined_p;
15413 decl_specs->redefined_builtin_type = NULL_TREE;
15414 }
15415 }
15416
15417 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15418 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15419
15420 static bool
15421 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15422 {
15423 return decl_specifiers->specs[(int) ds_friend] != 0;
15424 }
15425
15426 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15427 issue an error message indicating that TOKEN_DESC was expected.
15428
15429 Returns the token consumed, if the token had the appropriate type.
15430 Otherwise, returns NULL. */
15431
15432 static cp_token *
15433 cp_parser_require (cp_parser* parser,
15434 enum cpp_ttype type,
15435 const char* token_desc)
15436 {
15437 if (cp_lexer_next_token_is (parser->lexer, type))
15438 return cp_lexer_consume_token (parser->lexer);
15439 else
15440 {
15441 /* Output the MESSAGE -- unless we're parsing tentatively. */
15442 if (!cp_parser_simulate_error (parser))
15443 {
15444 char *message = concat ("expected ", token_desc, NULL);
15445 cp_parser_error (parser, message);
15446 free (message);
15447 }
15448 return NULL;
15449 }
15450 }
15451
15452 /* Like cp_parser_require, except that tokens will be skipped until
15453 the desired token is found. An error message is still produced if
15454 the next token is not as expected. */
15455
15456 static void
15457 cp_parser_skip_until_found (cp_parser* parser,
15458 enum cpp_ttype type,
15459 const char* token_desc)
15460 {
15461 cp_token *token;
15462 unsigned nesting_depth = 0;
15463
15464 if (cp_parser_require (parser, type, token_desc))
15465 return;
15466
15467 /* Skip tokens until the desired token is found. */
15468 while (true)
15469 {
15470 /* Peek at the next token. */
15471 token = cp_lexer_peek_token (parser->lexer);
15472 /* If we've reached the token we want, consume it and
15473 stop. */
15474 if (token->type == type && !nesting_depth)
15475 {
15476 cp_lexer_consume_token (parser->lexer);
15477 return;
15478 }
15479 /* If we've run out of tokens, stop. */
15480 if (token->type == CPP_EOF)
15481 return;
15482 if (token->type == CPP_OPEN_BRACE
15483 || token->type == CPP_OPEN_PAREN
15484 || token->type == CPP_OPEN_SQUARE)
15485 ++nesting_depth;
15486 else if (token->type == CPP_CLOSE_BRACE
15487 || token->type == CPP_CLOSE_PAREN
15488 || token->type == CPP_CLOSE_SQUARE)
15489 {
15490 if (nesting_depth-- == 0)
15491 return;
15492 }
15493 /* Consume this token. */
15494 cp_lexer_consume_token (parser->lexer);
15495 }
15496 }
15497
15498 /* If the next token is the indicated keyword, consume it. Otherwise,
15499 issue an error message indicating that TOKEN_DESC was expected.
15500
15501 Returns the token consumed, if the token had the appropriate type.
15502 Otherwise, returns NULL. */
15503
15504 static cp_token *
15505 cp_parser_require_keyword (cp_parser* parser,
15506 enum rid keyword,
15507 const char* token_desc)
15508 {
15509 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15510
15511 if (token && token->keyword != keyword)
15512 {
15513 dyn_string_t error_msg;
15514
15515 /* Format the error message. */
15516 error_msg = dyn_string_new (0);
15517 dyn_string_append_cstr (error_msg, "expected ");
15518 dyn_string_append_cstr (error_msg, token_desc);
15519 cp_parser_error (parser, error_msg->s);
15520 dyn_string_delete (error_msg);
15521 return NULL;
15522 }
15523
15524 return token;
15525 }
15526
15527 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15528 function-definition. */
15529
15530 static bool
15531 cp_parser_token_starts_function_definition_p (cp_token* token)
15532 {
15533 return (/* An ordinary function-body begins with an `{'. */
15534 token->type == CPP_OPEN_BRACE
15535 /* A ctor-initializer begins with a `:'. */
15536 || token->type == CPP_COLON
15537 /* A function-try-block begins with `try'. */
15538 || token->keyword == RID_TRY
15539 /* The named return value extension begins with `return'. */
15540 || token->keyword == RID_RETURN);
15541 }
15542
15543 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15544 definition. */
15545
15546 static bool
15547 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15548 {
15549 cp_token *token;
15550
15551 token = cp_lexer_peek_token (parser->lexer);
15552 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15553 }
15554
15555 /* Returns TRUE iff the next token is the "," or ">" ending a
15556 template-argument. */
15557
15558 static bool
15559 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15560 {
15561 cp_token *token;
15562
15563 token = cp_lexer_peek_token (parser->lexer);
15564 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15565 }
15566
15567 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15568 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15569
15570 static bool
15571 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15572 size_t n)
15573 {
15574 cp_token *token;
15575
15576 token = cp_lexer_peek_nth_token (parser->lexer, n);
15577 if (token->type == CPP_LESS)
15578 return true;
15579 /* Check for the sequence `<::' in the original code. It would be lexed as
15580 `[:', where `[' is a digraph, and there is no whitespace before
15581 `:'. */
15582 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15583 {
15584 cp_token *token2;
15585 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15586 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15587 return true;
15588 }
15589 return false;
15590 }
15591
15592 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15593 or none_type otherwise. */
15594
15595 static enum tag_types
15596 cp_parser_token_is_class_key (cp_token* token)
15597 {
15598 switch (token->keyword)
15599 {
15600 case RID_CLASS:
15601 return class_type;
15602 case RID_STRUCT:
15603 return record_type;
15604 case RID_UNION:
15605 return union_type;
15606
15607 default:
15608 return none_type;
15609 }
15610 }
15611
15612 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15613
15614 static void
15615 cp_parser_check_class_key (enum tag_types class_key, tree type)
15616 {
15617 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15618 pedwarn ("%qs tag used in naming %q#T",
15619 class_key == union_type ? "union"
15620 : class_key == record_type ? "struct" : "class",
15621 type);
15622 }
15623
15624 /* Issue an error message if DECL is redeclared with different
15625 access than its original declaration [class.access.spec/3].
15626 This applies to nested classes and nested class templates.
15627 [class.mem/1]. */
15628
15629 static void
15630 cp_parser_check_access_in_redeclaration (tree decl)
15631 {
15632 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15633 return;
15634
15635 if ((TREE_PRIVATE (decl)
15636 != (current_access_specifier == access_private_node))
15637 || (TREE_PROTECTED (decl)
15638 != (current_access_specifier == access_protected_node)))
15639 error ("%qD redeclared with different access", decl);
15640 }
15641
15642 /* Look for the `template' keyword, as a syntactic disambiguator.
15643 Return TRUE iff it is present, in which case it will be
15644 consumed. */
15645
15646 static bool
15647 cp_parser_optional_template_keyword (cp_parser *parser)
15648 {
15649 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15650 {
15651 /* The `template' keyword can only be used within templates;
15652 outside templates the parser can always figure out what is a
15653 template and what is not. */
15654 if (!processing_template_decl)
15655 {
15656 error ("%<template%> (as a disambiguator) is only allowed "
15657 "within templates");
15658 /* If this part of the token stream is rescanned, the same
15659 error message would be generated. So, we purge the token
15660 from the stream. */
15661 cp_lexer_purge_token (parser->lexer);
15662 return false;
15663 }
15664 else
15665 {
15666 /* Consume the `template' keyword. */
15667 cp_lexer_consume_token (parser->lexer);
15668 return true;
15669 }
15670 }
15671
15672 return false;
15673 }
15674
15675 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15676 set PARSER->SCOPE, and perform other related actions. */
15677
15678 static void
15679 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15680 {
15681 tree value;
15682 tree check;
15683
15684 /* Get the stored value. */
15685 value = cp_lexer_consume_token (parser->lexer)->value;
15686 /* Perform any access checks that were deferred. */
15687 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15688 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15689 /* Set the scope from the stored value. */
15690 parser->scope = TREE_VALUE (value);
15691 parser->qualifying_scope = TREE_TYPE (value);
15692 parser->object_scope = NULL_TREE;
15693 }
15694
15695 /* Consume tokens up through a non-nested END token. */
15696
15697 static void
15698 cp_parser_cache_group (cp_parser *parser,
15699 enum cpp_ttype end,
15700 unsigned depth)
15701 {
15702 while (true)
15703 {
15704 cp_token *token;
15705
15706 /* Abort a parenthesized expression if we encounter a brace. */
15707 if ((end == CPP_CLOSE_PAREN || depth == 0)
15708 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15709 return;
15710 /* If we've reached the end of the file, stop. */
15711 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15712 return;
15713 /* Consume the next token. */
15714 token = cp_lexer_consume_token (parser->lexer);
15715 /* See if it starts a new group. */
15716 if (token->type == CPP_OPEN_BRACE)
15717 {
15718 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15719 if (depth == 0)
15720 return;
15721 }
15722 else if (token->type == CPP_OPEN_PAREN)
15723 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15724 else if (token->type == end)
15725 return;
15726 }
15727 }
15728
15729 /* Begin parsing tentatively. We always save tokens while parsing
15730 tentatively so that if the tentative parsing fails we can restore the
15731 tokens. */
15732
15733 static void
15734 cp_parser_parse_tentatively (cp_parser* parser)
15735 {
15736 /* Enter a new parsing context. */
15737 parser->context = cp_parser_context_new (parser->context);
15738 /* Begin saving tokens. */
15739 cp_lexer_save_tokens (parser->lexer);
15740 /* In order to avoid repetitive access control error messages,
15741 access checks are queued up until we are no longer parsing
15742 tentatively. */
15743 push_deferring_access_checks (dk_deferred);
15744 }
15745
15746 /* Commit to the currently active tentative parse. */
15747
15748 static void
15749 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15750 {
15751 cp_parser_context *context;
15752 cp_lexer *lexer;
15753
15754 /* Mark all of the levels as committed. */
15755 lexer = parser->lexer;
15756 for (context = parser->context; context->next; context = context->next)
15757 {
15758 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15759 break;
15760 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15761 while (!cp_lexer_saving_tokens (lexer))
15762 lexer = lexer->next;
15763 cp_lexer_commit_tokens (lexer);
15764 }
15765 }
15766
15767 /* Abort the currently active tentative parse. All consumed tokens
15768 will be rolled back, and no diagnostics will be issued. */
15769
15770 static void
15771 cp_parser_abort_tentative_parse (cp_parser* parser)
15772 {
15773 cp_parser_simulate_error (parser);
15774 /* Now, pretend that we want to see if the construct was
15775 successfully parsed. */
15776 cp_parser_parse_definitely (parser);
15777 }
15778
15779 /* Stop parsing tentatively. If a parse error has occurred, restore the
15780 token stream. Otherwise, commit to the tokens we have consumed.
15781 Returns true if no error occurred; false otherwise. */
15782
15783 static bool
15784 cp_parser_parse_definitely (cp_parser* parser)
15785 {
15786 bool error_occurred;
15787 cp_parser_context *context;
15788
15789 /* Remember whether or not an error occurred, since we are about to
15790 destroy that information. */
15791 error_occurred = cp_parser_error_occurred (parser);
15792 /* Remove the topmost context from the stack. */
15793 context = parser->context;
15794 parser->context = context->next;
15795 /* If no parse errors occurred, commit to the tentative parse. */
15796 if (!error_occurred)
15797 {
15798 /* Commit to the tokens read tentatively, unless that was
15799 already done. */
15800 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15801 cp_lexer_commit_tokens (parser->lexer);
15802
15803 pop_to_parent_deferring_access_checks ();
15804 }
15805 /* Otherwise, if errors occurred, roll back our state so that things
15806 are just as they were before we began the tentative parse. */
15807 else
15808 {
15809 cp_lexer_rollback_tokens (parser->lexer);
15810 pop_deferring_access_checks ();
15811 }
15812 /* Add the context to the front of the free list. */
15813 context->next = cp_parser_context_free_list;
15814 cp_parser_context_free_list = context;
15815
15816 return !error_occurred;
15817 }
15818
15819 /* Returns true if we are parsing tentatively -- but have decided that
15820 we will stick with this tentative parse, even if errors occur. */
15821
15822 static bool
15823 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15824 {
15825 return (cp_parser_parsing_tentatively (parser)
15826 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15827 }
15828
15829 /* Returns nonzero iff an error has occurred during the most recent
15830 tentative parse. */
15831
15832 static bool
15833 cp_parser_error_occurred (cp_parser* parser)
15834 {
15835 return (cp_parser_parsing_tentatively (parser)
15836 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15837 }
15838
15839 /* Returns nonzero if GNU extensions are allowed. */
15840
15841 static bool
15842 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15843 {
15844 return parser->allow_gnu_extensions_p;
15845 }
15846
15847 \f
15848 /* The parser. */
15849
15850 static GTY (()) cp_parser *the_parser;
15851
15852 /* External interface. */
15853
15854 /* Parse one entire translation unit. */
15855
15856 void
15857 c_parse_file (void)
15858 {
15859 bool error_occurred;
15860 static bool already_called = false;
15861
15862 if (already_called)
15863 {
15864 sorry ("inter-module optimizations not implemented for C++");
15865 return;
15866 }
15867 already_called = true;
15868
15869 the_parser = cp_parser_new ();
15870 push_deferring_access_checks (flag_access_control
15871 ? dk_no_deferred : dk_no_check);
15872 error_occurred = cp_parser_translation_unit (the_parser);
15873 the_parser = NULL;
15874 }
15875
15876 /* This variable must be provided by every front end. */
15877
15878 int yydebug;
15879
15880 #include "gt-cp-parser.h"