* trans-stmt.c (gfc_trans_simple_do): New function.
[official-gcc.git] / gcc / cp / parser.c
blob79342dbf576a17d69517a7966e9ef97ed0e0cbea
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
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.
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.
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. */
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"
40 /* The lexer. */
42 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
43 and c-lex.c) and the C++ parser. */
45 /* A C++ token. */
47 typedef struct cp_token GTY (())
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;
66 /* The cp_lexer structure represents the C++ lexer. It is responsible
67 for managing the token stream from the preprocessor and supplying
68 it to the parser. Tokens are never added to the cp_lexer after
69 it is created. */
71 typedef struct cp_lexer GTY (())
73 /* The memory allocated for the buffer. Never NULL. */
74 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
75 /* A pointer just past the end of the memory allocated for the buffer. */
76 cp_token * GTY ((skip)) buffer_end;
77 /* A pointer just past the last available token. The tokens
78 in this lexer are [buffer, last_token). */
79 cp_token * GTY ((skip)) last_token;
81 /* The next available token. If NEXT_TOKEN is NULL, then there are
82 no more available tokens. */
83 cp_token * GTY ((skip)) next_token;
85 /* A stack indicating positions at which cp_lexer_save_tokens was
86 called. The top entry is the most recent position at which we
87 began saving tokens. The entries are differences in token
88 position between BUFFER and the first saved token.
89 If the stack is non-empty, we are saving tokens. */
90 varray_type saved_tokens;
92 /* True if we should output debugging information. */
93 bool debugging_p;
95 /* The next lexer in a linked list of lexers. */
96 struct cp_lexer *next;
97 } cp_lexer;
99 /* cp_token_cache is a range of tokens. There is no need to represent
100 allocate heap memory for it, since tokens are never removed from the
101 lexer's array. There is also no need for the GC to walk through
102 a cp_token_cache, since everything in here is referenced through
103 a lexer. */
105 typedef struct cp_token_cache GTY(())
107 /* The beginning of the token range. */
108 cp_token * GTY((skip)) first;
110 /* Points immediately after the last token in the range. */
111 cp_token * GTY ((skip)) last;
112 } cp_token_cache;
114 /* Prototypes. */
116 static cp_lexer *cp_lexer_new_main
117 (void);
118 static cp_lexer *cp_lexer_new_from_tokens
119 (cp_token_cache *tokens);
120 static void cp_lexer_destroy
121 (cp_lexer *);
122 static int cp_lexer_saving_tokens
123 (const cp_lexer *);
124 static cp_token *cp_lexer_next_token
125 (cp_lexer *, cp_token *);
126 static cp_token *cp_lexer_prev_token
127 (cp_lexer *, cp_token *);
128 static ptrdiff_t cp_lexer_token_difference
129 (cp_lexer *, cp_token *, cp_token *);
130 static void cp_lexer_grow_buffer
131 (cp_lexer *);
132 static void cp_lexer_get_preprocessor_token
133 (cp_lexer *, cp_token *);
134 static inline cp_token *cp_lexer_peek_token
135 (cp_lexer *);
136 static cp_token *cp_lexer_peek_nth_token
137 (cp_lexer *, size_t);
138 static inline bool cp_lexer_next_token_is
139 (cp_lexer *, enum cpp_ttype);
140 static bool cp_lexer_next_token_is_not
141 (cp_lexer *, enum cpp_ttype);
142 static bool cp_lexer_next_token_is_keyword
143 (cp_lexer *, enum rid);
144 static cp_token *cp_lexer_consume_token
145 (cp_lexer *);
146 static void cp_lexer_purge_token
147 (cp_lexer *);
148 static void cp_lexer_purge_tokens_after
149 (cp_lexer *, cp_token *);
150 static void cp_lexer_handle_pragma
151 (cp_lexer *);
152 static void cp_lexer_save_tokens
153 (cp_lexer *);
154 static void cp_lexer_commit_tokens
155 (cp_lexer *);
156 static void cp_lexer_rollback_tokens
157 (cp_lexer *);
158 #ifdef ENABLE_CHECKING
159 static void cp_lexer_print_token
160 (FILE *, cp_token *);
161 static inline bool cp_lexer_debugging_p
162 (cp_lexer *);
163 static void cp_lexer_start_debugging
164 (cp_lexer *) ATTRIBUTE_UNUSED;
165 static void cp_lexer_stop_debugging
166 (cp_lexer *) ATTRIBUTE_UNUSED;
167 static void cp_lexer_peek_token_emit_debug_info
168 (cp_lexer *, cp_token *);
169 #else
170 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
171 about passing NULL to functions that require non-NULL arguments
172 (fputs, fprintf). It will never be used, so all we need is a value
173 of the right type that's guaranteed not to be NULL. */
174 #define cp_lexer_debug_stream stdout
175 #define cp_lexer_print_token(str, tok) (void) 0
176 #define cp_lexer_debugging_p(lexer) 0
177 #define cp_lexer_peek_token_emit_debug_info(lexer, tok) (void) 0
178 #endif /* ENABLE_CHECKING */
180 static cp_token_cache *cp_token_cache_new
181 (cp_token *, cp_token *);
183 /* Manifest constants. */
185 #define CP_LEXER_BUFFER_SIZE 10000
186 #define CP_SAVED_TOKENS_SIZE 5
188 /* A token type for keywords, as opposed to ordinary identifiers. */
189 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
191 /* A token type for template-ids. If a template-id is processed while
192 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
193 the value of the CPP_TEMPLATE_ID is whatever was returned by
194 cp_parser_template_id. */
195 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
197 /* A token type for nested-name-specifiers. If a
198 nested-name-specifier is processed while parsing tentatively, it is
199 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
200 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
201 cp_parser_nested_name_specifier_opt. */
202 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
204 /* A token type for tokens that are not tokens at all; these are used
205 to represent slots in the array where there used to be a token
206 that has now been deleted. */
207 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
209 /* The number of token types, including C++-specific ones. */
210 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
212 /* Variables. */
214 #ifdef ENABLE_CHECKING
215 /* The stream to which debugging output should be written. */
216 static FILE *cp_lexer_debug_stream;
217 #endif /* ENABLE_CHECKING */
219 /* Create a new main C++ lexer, the lexer that gets tokens from the
220 preprocessor. */
222 static cp_lexer *
223 cp_lexer_new_main (void)
225 cp_lexer *lexer;
226 cp_token first_token;
228 /* Tell cpplib we want CPP_PRAGMA tokens. */
229 cpp_get_options (parse_in)->defer_pragmas = true;
231 /* Tell c_lex not to merge string constants. */
232 c_lex_return_raw_strings = true;
234 /* It's possible that lexing the first token will load a PCH file,
235 which is a GC collection point. So we have to grab the first
236 token before allocating any memory. */
237 cp_lexer_get_preprocessor_token (NULL, &first_token);
238 c_common_no_more_pch ();
240 /* Allocate the memory. */
241 lexer = GGC_CNEW (cp_lexer);
243 /* Create the buffer. */
244 lexer->buffer = ggc_calloc (CP_LEXER_BUFFER_SIZE, sizeof (cp_token));
245 lexer->buffer_end = lexer->buffer + CP_LEXER_BUFFER_SIZE;
247 /* There is one token in the buffer. */
248 lexer->last_token = lexer->buffer + 1;
249 lexer->next_token = lexer->buffer;
250 *lexer->next_token = first_token;
252 /* Create the SAVED_TOKENS stack. */
253 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
255 #ifdef ENABLE_CHECKING
256 /* Initially we are not debugging. */
257 lexer->debugging_p = false;
258 #endif /* ENABLE_CHECKING */
260 /* Get the rest of the tokens from the preprocessor. */
261 while (lexer->last_token[-1].type != CPP_EOF)
263 if (lexer->last_token == lexer->buffer_end)
264 cp_lexer_grow_buffer (lexer);
265 cp_lexer_get_preprocessor_token (lexer, lexer->last_token++);
268 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
269 direct calls to c_lex. Those callers all expect c_lex to do
270 string constant concatenation. */
271 c_lex_return_raw_strings = false;
273 gcc_assert (lexer->next_token->type != CPP_PURGED);
274 return lexer;
277 /* Create a new lexer whose token stream is primed with the tokens in
278 CACHE. When these tokens are exhausted, no new tokens will be read. */
280 static cp_lexer *
281 cp_lexer_new_from_tokens (cp_token_cache *cache)
283 cp_token *first = cache->first;
284 cp_token *last = cache->last;
285 cp_lexer *lexer = GGC_CNEW (cp_lexer);
286 cp_token *eof;
288 /* Allocate a new buffer. The reason we do this is to make sure
289 there's a CPP_EOF token at the end. An alternative would be to
290 modify cp_lexer_peek_token so that it checks for end-of-buffer
291 and returns a CPP_EOF when appropriate. */
293 lexer->buffer = GGC_NEWVEC (cp_token, (last - first) + 1);
294 memcpy (lexer->buffer, first, sizeof (cp_token) * (last - first));
295 lexer->next_token = lexer->buffer;
296 lexer->buffer_end = lexer->last_token = lexer->buffer + (last - first);
298 eof = lexer->buffer + (last - first);
299 eof->type = CPP_EOF;
300 eof->location = UNKNOWN_LOCATION;
301 eof->value = NULL_TREE;
302 eof->keyword = RID_MAX;
304 /* Create the SAVED_TOKENS stack. */
305 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
307 #ifdef ENABLE_CHECKING
308 /* Initially we are not debugging. */
309 lexer->debugging_p = false;
310 #endif
312 gcc_assert (lexer->next_token->type != CPP_PURGED);
313 return lexer;
316 /* Frees all resources associated with LEXER. */
318 static void
319 cp_lexer_destroy (cp_lexer *lexer)
321 ggc_free (lexer->buffer);
322 ggc_free (lexer);
325 /* Returns nonzero if debugging information should be output. */
327 #ifdef ENABLE_CHECKING
329 static inline bool
330 cp_lexer_debugging_p (cp_lexer *lexer)
332 return lexer->debugging_p;
335 #endif /* ENABLE_CHECKING */
337 /* TOKEN points into the circular token buffer. Return a pointer to
338 the next token in the buffer. */
340 static inline cp_token *
341 cp_lexer_next_token (cp_lexer* lexer ATTRIBUTE_UNUSED, cp_token* token)
343 token++;
344 return token;
347 /* TOKEN points into the circular token buffer. Return a pointer to
348 the previous token in the buffer. */
350 static inline cp_token *
351 cp_lexer_prev_token (cp_lexer* lexer ATTRIBUTE_UNUSED, cp_token* token)
353 return token - 1;
356 /* nonzero if we are presently saving tokens. */
358 static int
359 cp_lexer_saving_tokens (const cp_lexer* lexer)
361 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
364 /* Return a pointer to the token that is N tokens beyond TOKEN in the
365 buffer. */
367 static inline cp_token *
368 cp_lexer_advance_token (cp_lexer *lexer ATTRIBUTE_UNUSED,
369 cp_token *token, ptrdiff_t n)
371 return token + n;
374 /* Returns the number of times that START would have to be incremented
375 to reach FINISH. If START and FINISH are the same, returns zero. */
377 static inline ptrdiff_t
378 cp_lexer_token_difference (cp_lexer* lexer ATTRIBUTE_UNUSED,
379 cp_token* start, cp_token* finish)
381 return finish - start;
384 /* If the buffer is full, make it bigger. */
385 static void
386 cp_lexer_grow_buffer (cp_lexer* lexer)
388 cp_token *old_buffer;
389 cp_token *new_buffer;
390 ptrdiff_t buffer_length;
392 /* This function should only be called when buffer is full. */
393 gcc_assert (lexer->last_token == lexer->buffer_end);
395 /* Remember the current buffer pointer. It will become invalid,
396 but we will need to do pointer arithmetic involving this
397 value. */
398 old_buffer = lexer->buffer;
399 /* Compute the current buffer size. */
400 buffer_length = lexer->buffer_end - lexer->buffer;
401 /* Allocate a buffer twice as big. */
402 new_buffer = ggc_realloc (lexer->buffer,
403 2 * buffer_length * sizeof (cp_token));
405 /* Recompute buffer positions. */
406 lexer->buffer = new_buffer;
407 lexer->buffer_end = new_buffer + 2 * buffer_length;
408 lexer->last_token = new_buffer + (lexer->last_token - old_buffer);
409 lexer->next_token = new_buffer + (lexer->next_token - old_buffer);
411 /* Clear the rest of the buffer. We never look at this storage,
412 but the garbage collector may. */
413 memset (lexer->last_token, 0,
414 (lexer->buffer_end - lexer->last_token) * sizeof(cp_token));
417 /* Store the next token from the preprocessor in *TOKEN. */
419 static void
420 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
421 cp_token *token)
423 static int is_extern_c = 0;
424 bool done;
426 done = false;
427 /* Keep going until we get a token we like. */
428 while (!done)
430 /* Get a new token from the preprocessor. */
431 token->type = c_lex_with_flags (&token->value, &token->flags);
432 /* Issue messages about tokens we cannot process. */
433 switch (token->type)
435 case CPP_ATSIGN:
436 case CPP_HASH:
437 case CPP_PASTE:
438 error ("invalid token");
439 break;
441 default:
442 /* This is a good token, so we exit the loop. */
443 done = true;
444 break;
447 /* Now we've got our token. */
448 token->location = input_location;
449 token->in_system_header = in_system_header;
451 /* On some systems, some header files are surrounded by an
452 implicit extern "C" block. Set a flag in the token if it
453 comes from such a header. */
454 is_extern_c += pending_lang_change;
455 pending_lang_change = 0;
456 token->implicit_extern_c = is_extern_c > 0;
458 /* Check to see if this token is a keyword. */
459 if (token->type == CPP_NAME
460 && C_IS_RESERVED_WORD (token->value))
462 /* Mark this token as a keyword. */
463 token->type = CPP_KEYWORD;
464 /* Record which keyword. */
465 token->keyword = C_RID_CODE (token->value);
466 /* Update the value. Some keywords are mapped to particular
467 entities, rather than simply having the value of the
468 corresponding IDENTIFIER_NODE. For example, `__const' is
469 mapped to `const'. */
470 token->value = ridpointers[token->keyword];
472 else
473 token->keyword = RID_MAX;
476 /* Update the globals input_location and in_system_header from TOKEN. */
477 static inline void
478 cp_lexer_set_source_position_from_token (cp_token *token)
480 if (token->type != CPP_EOF)
482 input_location = token->location;
483 in_system_header = token->in_system_header;
487 /* Return a pointer to the next token in the token stream, but do not
488 consume it. */
490 static inline cp_token *
491 cp_lexer_peek_token (cp_lexer *lexer)
493 if (cp_lexer_debugging_p (lexer))
494 cp_lexer_peek_token_emit_debug_info (lexer, lexer->next_token);
495 return lexer->next_token;
498 #ifdef ENABLE_CHECKING
499 /* Emit debug output for cp_lexer_peek_token. Split out into a
500 separate function so that cp_lexer_peek_token can be small and
501 inlinable. */
503 static void
504 cp_lexer_peek_token_emit_debug_info (cp_lexer *lexer ATTRIBUTE_UNUSED,
505 cp_token *token ATTRIBUTE_UNUSED)
507 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
508 cp_lexer_print_token (cp_lexer_debug_stream, token);
509 putc ('\n', cp_lexer_debug_stream);
511 #endif
513 /* Return true if the next token has the indicated TYPE. */
515 static inline bool
516 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
518 return cp_lexer_peek_token (lexer)->type == type;
521 /* Return true if the next token does not have the indicated TYPE. */
523 static inline bool
524 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
526 return !cp_lexer_next_token_is (lexer, type);
529 /* Return true if the next token is the indicated KEYWORD. */
531 static inline bool
532 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
534 cp_token *token;
536 /* Peek at the next token. */
537 token = cp_lexer_peek_token (lexer);
538 /* Check to see if it is the indicated keyword. */
539 return token->keyword == keyword;
542 /* Return a pointer to the Nth token in the token stream. If N is 1,
543 then this is precisely equivalent to cp_lexer_peek_token (except
544 that it is not inline). One would like to disallow that case, but
545 there is one case (cp_parser_nth_token_starts_template_id) where
546 the caller passes a variable for N and it might be 1. */
548 static cp_token *
549 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
551 cp_token *token;
553 /* N is 1-based, not zero-based. */
554 gcc_assert (n > 0);
556 if (cp_lexer_debugging_p (lexer))
557 fprintf (cp_lexer_debug_stream,
558 "cp_lexer: peeking ahead %ld at token: ", (long)n);
560 --n;
561 token = lexer->next_token;
562 while (n != 0)
564 ++token;
565 if (token->type != CPP_PURGED)
566 --n;
569 if (cp_lexer_debugging_p (lexer))
571 cp_lexer_print_token (cp_lexer_debug_stream, token);
572 putc ('\n', cp_lexer_debug_stream);
575 return token;
578 /* Return the next token, and advance the lexer's next_token pointer
579 to point to the next non-purged token. */
581 static cp_token *
582 cp_lexer_consume_token (cp_lexer* lexer)
584 cp_token *token = lexer->next_token;
587 ++lexer->next_token;
588 while (lexer->next_token->type == CPP_PURGED);
590 cp_lexer_set_source_position_from_token (token);
592 /* Provide debugging output. */
593 if (cp_lexer_debugging_p (lexer))
595 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
596 cp_lexer_print_token (cp_lexer_debug_stream, token);
597 putc ('\n', cp_lexer_debug_stream);
600 return token;
603 /* Permanently remove the next token from the token stream, and
604 advance the next_token pointer to refer to the next non-purged
605 token. */
607 static void
608 cp_lexer_purge_token (cp_lexer *lexer)
610 cp_token *tok = lexer->next_token;
611 tok->type = CPP_PURGED;
612 tok->location = UNKNOWN_LOCATION;
613 tok->value = NULL_TREE;
614 tok->keyword = RID_MAX;
617 ++lexer->next_token;
618 while (lexer->next_token->type == CPP_PURGED);
621 /* Permanently remove all tokens after TOK, up to, but not
622 including, the token that will be returned next by
623 cp_lexer_peek_token. */
625 static void
626 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
628 cp_token *peek;
630 peek = cp_lexer_peek_token (lexer);
631 gcc_assert (tok < peek);
633 for ( tok += 1; tok != peek; tok += 1)
635 tok->type = CPP_PURGED;
636 tok->location = UNKNOWN_LOCATION;
637 tok->value = NULL_TREE;
638 tok->keyword = RID_MAX;
642 /* Consume and handle a pragma token. */
643 static void
644 cp_lexer_handle_pragma (cp_lexer *lexer)
646 cpp_string s;
647 cp_token *token = cp_lexer_consume_token (lexer);
648 gcc_assert (token->type == CPP_PRAGMA);
649 gcc_assert (token->value);
651 s.len = TREE_STRING_LENGTH (token->value);
652 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
654 cpp_handle_deferred_pragma (parse_in, &s);
656 /* Clearing token->value here means that we will get an ICE if we
657 try to process this #pragma again (which should be impossible). */
658 token->value = NULL;
661 /* Begin saving tokens. All tokens consumed after this point will be
662 preserved. */
664 static void
665 cp_lexer_save_tokens (cp_lexer* lexer)
667 /* Provide debugging output. */
668 if (cp_lexer_debugging_p (lexer))
669 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
671 VARRAY_PUSH_INT (lexer->saved_tokens,
672 cp_lexer_token_difference (lexer,
673 lexer->buffer,
674 lexer->next_token));
677 /* Commit to the portion of the token stream most recently saved. */
679 static void
680 cp_lexer_commit_tokens (cp_lexer* lexer)
682 /* Provide debugging output. */
683 if (cp_lexer_debugging_p (lexer))
684 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
686 VARRAY_POP (lexer->saved_tokens);
689 /* Return all tokens saved since the last call to cp_lexer_save_tokens
690 to the token stream. Stop saving tokens. */
692 static void
693 cp_lexer_rollback_tokens (cp_lexer* lexer)
695 size_t delta;
697 /* Provide debugging output. */
698 if (cp_lexer_debugging_p (lexer))
699 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
701 /* Find the token that was the NEXT_TOKEN when we started saving
702 tokens. */
703 delta = VARRAY_TOP_INT(lexer->saved_tokens);
704 /* Make it the next token again now. */
705 lexer->next_token = cp_lexer_advance_token (lexer, lexer->buffer, delta);
707 /* Stop saving tokens. */
708 VARRAY_POP (lexer->saved_tokens);
711 /* Print a representation of the TOKEN on the STREAM. */
713 #ifdef ENABLE_CHECKING
715 static void
716 cp_lexer_print_token (FILE * stream, cp_token *token)
718 /* We don't use cpp_type2name here because the parser defines
719 a few tokens of its own. */
720 static const char *const token_names[] = {
721 /* cpplib-defined token types */
722 #define OP(e, s) #e,
723 #define TK(e, s) #e,
724 TTYPE_TABLE
725 #undef OP
726 #undef TK
727 /* C++ parser token types - see "Manifest constants", above. */
728 "KEYWORD",
729 "TEMPLATE_ID",
730 "NESTED_NAME_SPECIFIER",
731 "PURGED"
734 /* If we have a name for the token, print it out. Otherwise, we
735 simply give the numeric code. */
736 gcc_assert (token->type < ARRAY_SIZE(token_names));
737 fputs (token_names[token->type], stream);
739 /* For some tokens, print the associated data. */
740 switch (token->type)
742 case CPP_KEYWORD:
743 /* Some keywords have a value that is not an IDENTIFIER_NODE.
744 For example, `struct' is mapped to an INTEGER_CST. */
745 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
746 break;
747 /* else fall through */
748 case CPP_NAME:
749 fputs (IDENTIFIER_POINTER (token->value), stream);
750 break;
752 case CPP_STRING:
753 case CPP_WSTRING:
754 case CPP_PRAGMA:
755 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
756 break;
758 default:
759 break;
763 /* Start emitting debugging information. */
765 static void
766 cp_lexer_start_debugging (cp_lexer* lexer)
768 ++lexer->debugging_p;
771 /* Stop emitting debugging information. */
773 static void
774 cp_lexer_stop_debugging (cp_lexer* lexer)
776 --lexer->debugging_p;
779 #endif /* ENABLE_CHECKING */
781 /* Create a new cp_token_cache, representing a range of tokens. */
783 static cp_token_cache *
784 cp_token_cache_new (cp_token *first, cp_token *last)
786 cp_token_cache *cache = GGC_NEW (cp_token_cache);
787 cache->first = first;
788 cache->last = last;
789 return cache;
793 /* Decl-specifiers. */
795 static void clear_decl_specs
796 (cp_decl_specifier_seq *);
798 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
800 static void
801 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
803 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
806 /* Declarators. */
808 /* Nothing other than the parser should be creating declarators;
809 declarators are a semi-syntactic representation of C++ entities.
810 Other parts of the front end that need to create entities (like
811 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
813 static cp_declarator *make_id_declarator
814 (tree);
815 static cp_declarator *make_call_declarator
816 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
817 static cp_declarator *make_array_declarator
818 (cp_declarator *, tree);
819 static cp_declarator *make_pointer_declarator
820 (cp_cv_quals, cp_declarator *);
821 static cp_declarator *make_reference_declarator
822 (cp_cv_quals, cp_declarator *);
823 static cp_parameter_declarator *make_parameter_declarator
824 (cp_decl_specifier_seq *, cp_declarator *, tree);
825 static cp_declarator *make_ptrmem_declarator
826 (cp_cv_quals, tree, cp_declarator *);
828 cp_declarator *cp_error_declarator;
830 /* The obstack on which declarators and related data structures are
831 allocated. */
832 static struct obstack declarator_obstack;
834 /* Alloc BYTES from the declarator memory pool. */
836 static inline void *
837 alloc_declarator (size_t bytes)
839 return obstack_alloc (&declarator_obstack, bytes);
842 /* Allocate a declarator of the indicated KIND. Clear fields that are
843 common to all declarators. */
845 static cp_declarator *
846 make_declarator (cp_declarator_kind kind)
848 cp_declarator *declarator;
850 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
851 declarator->kind = kind;
852 declarator->attributes = NULL_TREE;
853 declarator->declarator = NULL;
855 return declarator;
858 /* Make a declarator for a generalized identifier. */
860 cp_declarator *
861 make_id_declarator (tree id)
863 cp_declarator *declarator;
865 declarator = make_declarator (cdk_id);
866 declarator->u.id.name = id;
867 declarator->u.id.sfk = sfk_none;
869 return declarator;
872 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
873 of modifiers such as const or volatile to apply to the pointer
874 type, represented as identifiers. */
876 cp_declarator *
877 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
879 cp_declarator *declarator;
881 declarator = make_declarator (cdk_pointer);
882 declarator->declarator = target;
883 declarator->u.pointer.qualifiers = cv_qualifiers;
884 declarator->u.pointer.class_type = NULL_TREE;
886 return declarator;
889 /* Like make_pointer_declarator -- but for references. */
891 cp_declarator *
892 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
894 cp_declarator *declarator;
896 declarator = make_declarator (cdk_reference);
897 declarator->declarator = target;
898 declarator->u.pointer.qualifiers = cv_qualifiers;
899 declarator->u.pointer.class_type = NULL_TREE;
901 return declarator;
904 /* Like make_pointer_declarator -- but for a pointer to a non-static
905 member of CLASS_TYPE. */
907 cp_declarator *
908 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
909 cp_declarator *pointee)
911 cp_declarator *declarator;
913 declarator = make_declarator (cdk_ptrmem);
914 declarator->declarator = pointee;
915 declarator->u.pointer.qualifiers = cv_qualifiers;
916 declarator->u.pointer.class_type = class_type;
918 return declarator;
921 /* Make a declarator for the function given by TARGET, with the
922 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
923 "const"-qualified member function. The EXCEPTION_SPECIFICATION
924 indicates what exceptions can be thrown. */
926 cp_declarator *
927 make_call_declarator (cp_declarator *target,
928 cp_parameter_declarator *parms,
929 cp_cv_quals cv_qualifiers,
930 tree exception_specification)
932 cp_declarator *declarator;
934 declarator = make_declarator (cdk_function);
935 declarator->declarator = target;
936 declarator->u.function.parameters = parms;
937 declarator->u.function.qualifiers = cv_qualifiers;
938 declarator->u.function.exception_specification = exception_specification;
940 return declarator;
943 /* Make a declarator for an array of BOUNDS elements, each of which is
944 defined by ELEMENT. */
946 cp_declarator *
947 make_array_declarator (cp_declarator *element, tree bounds)
949 cp_declarator *declarator;
951 declarator = make_declarator (cdk_array);
952 declarator->declarator = element;
953 declarator->u.array.bounds = bounds;
955 return declarator;
958 cp_parameter_declarator *no_parameters;
960 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
961 DECLARATOR and DEFAULT_ARGUMENT. */
963 cp_parameter_declarator *
964 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
965 cp_declarator *declarator,
966 tree default_argument)
968 cp_parameter_declarator *parameter;
970 parameter = ((cp_parameter_declarator *)
971 alloc_declarator (sizeof (cp_parameter_declarator)));
972 parameter->next = NULL;
973 if (decl_specifiers)
974 parameter->decl_specifiers = *decl_specifiers;
975 else
976 clear_decl_specs (&parameter->decl_specifiers);
977 parameter->declarator = declarator;
978 parameter->default_argument = default_argument;
979 parameter->ellipsis_p = false;
981 return parameter;
984 /* The parser. */
986 /* Overview
987 --------
989 A cp_parser parses the token stream as specified by the C++
990 grammar. Its job is purely parsing, not semantic analysis. For
991 example, the parser breaks the token stream into declarators,
992 expressions, statements, and other similar syntactic constructs.
993 It does not check that the types of the expressions on either side
994 of an assignment-statement are compatible, or that a function is
995 not declared with a parameter of type `void'.
997 The parser invokes routines elsewhere in the compiler to perform
998 semantic analysis and to build up the abstract syntax tree for the
999 code processed.
1001 The parser (and the template instantiation code, which is, in a
1002 way, a close relative of parsing) are the only parts of the
1003 compiler that should be calling push_scope and pop_scope, or
1004 related functions. The parser (and template instantiation code)
1005 keeps track of what scope is presently active; everything else
1006 should simply honor that. (The code that generates static
1007 initializers may also need to set the scope, in order to check
1008 access control correctly when emitting the initializers.)
1010 Methodology
1011 -----------
1013 The parser is of the standard recursive-descent variety. Upcoming
1014 tokens in the token stream are examined in order to determine which
1015 production to use when parsing a non-terminal. Some C++ constructs
1016 require arbitrary look ahead to disambiguate. For example, it is
1017 impossible, in the general case, to tell whether a statement is an
1018 expression or declaration without scanning the entire statement.
1019 Therefore, the parser is capable of "parsing tentatively." When the
1020 parser is not sure what construct comes next, it enters this mode.
1021 Then, while we attempt to parse the construct, the parser queues up
1022 error messages, rather than issuing them immediately, and saves the
1023 tokens it consumes. If the construct is parsed successfully, the
1024 parser "commits", i.e., it issues any queued error messages and
1025 the tokens that were being preserved are permanently discarded.
1026 If, however, the construct is not parsed successfully, the parser
1027 rolls back its state completely so that it can resume parsing using
1028 a different alternative.
1030 Future Improvements
1031 -------------------
1033 The performance of the parser could probably be improved substantially.
1034 We could often eliminate the need to parse tentatively by looking ahead
1035 a little bit. In some places, this approach might not entirely eliminate
1036 the need to parse tentatively, but it might still speed up the average
1037 case. */
1039 /* Flags that are passed to some parsing functions. These values can
1040 be bitwise-ored together. */
1042 typedef enum cp_parser_flags
1044 /* No flags. */
1045 CP_PARSER_FLAGS_NONE = 0x0,
1046 /* The construct is optional. If it is not present, then no error
1047 should be issued. */
1048 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1049 /* When parsing a type-specifier, do not allow user-defined types. */
1050 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1051 } cp_parser_flags;
1053 /* The different kinds of declarators we want to parse. */
1055 typedef enum cp_parser_declarator_kind
1057 /* We want an abstract declarator. */
1058 CP_PARSER_DECLARATOR_ABSTRACT,
1059 /* We want a named declarator. */
1060 CP_PARSER_DECLARATOR_NAMED,
1061 /* We don't mind, but the name must be an unqualified-id. */
1062 CP_PARSER_DECLARATOR_EITHER
1063 } cp_parser_declarator_kind;
1065 /* The precedence values used to parse binary expressions. The minimum value
1066 of PREC must be 1, because zero is reserved to quickly discriminate
1067 binary operators from other tokens. */
1069 enum cp_parser_prec
1071 PREC_NOT_OPERATOR,
1072 PREC_LOGICAL_OR_EXPRESSION,
1073 PREC_LOGICAL_AND_EXPRESSION,
1074 PREC_INCLUSIVE_OR_EXPRESSION,
1075 PREC_EXCLUSIVE_OR_EXPRESSION,
1076 PREC_AND_EXPRESSION,
1077 PREC_RELATIONAL_EXPRESSION,
1078 PREC_EQUALITY_EXPRESSION,
1079 PREC_SHIFT_EXPRESSION,
1080 PREC_ADDITIVE_EXPRESSION,
1081 PREC_MULTIPLICATIVE_EXPRESSION,
1082 PREC_PM_EXPRESSION,
1083 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1086 /* A mapping from a token type to a corresponding tree node type, with a
1087 precedence value. */
1089 typedef struct cp_parser_binary_operations_map_node
1091 /* The token type. */
1092 enum cpp_ttype token_type;
1093 /* The corresponding tree code. */
1094 enum tree_code tree_type;
1095 /* The precedence of this operator. */
1096 enum cp_parser_prec prec;
1097 } cp_parser_binary_operations_map_node;
1099 /* The status of a tentative parse. */
1101 typedef enum cp_parser_status_kind
1103 /* No errors have occurred. */
1104 CP_PARSER_STATUS_KIND_NO_ERROR,
1105 /* An error has occurred. */
1106 CP_PARSER_STATUS_KIND_ERROR,
1107 /* We are committed to this tentative parse, whether or not an error
1108 has occurred. */
1109 CP_PARSER_STATUS_KIND_COMMITTED
1110 } cp_parser_status_kind;
1112 typedef struct cp_parser_expression_stack_entry
1114 tree lhs;
1115 enum tree_code tree_type;
1116 int prec;
1117 } cp_parser_expression_stack_entry;
1119 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1120 entries because precedence levels on the stack are monotonically
1121 increasing. */
1122 typedef struct cp_parser_expression_stack_entry
1123 cp_parser_expression_stack[NUM_PREC_VALUES];
1125 /* Context that is saved and restored when parsing tentatively. */
1126 typedef struct cp_parser_context GTY (())
1128 /* If this is a tentative parsing context, the status of the
1129 tentative parse. */
1130 enum cp_parser_status_kind status;
1131 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1132 that are looked up in this context must be looked up both in the
1133 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1134 the context of the containing expression. */
1135 tree object_type;
1137 /* The next parsing context in the stack. */
1138 struct cp_parser_context *next;
1139 } cp_parser_context;
1141 /* Prototypes. */
1143 /* Constructors and destructors. */
1145 static cp_parser_context *cp_parser_context_new
1146 (cp_parser_context *);
1148 /* Class variables. */
1150 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1152 /* The operator-precedence table used by cp_parser_binary_expression.
1153 Transformed into an associative array (binops_by_token) by
1154 cp_parser_new. */
1156 static const cp_parser_binary_operations_map_node binops[] = {
1157 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1158 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1160 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1161 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1162 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1164 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1165 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1167 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1168 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1170 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1171 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1172 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1173 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1174 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1175 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1177 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1178 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1180 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1182 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1184 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1186 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1188 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1191 /* The same as binops, but initialized by cp_parser_new so that
1192 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1193 for speed. */
1194 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1196 /* Constructors and destructors. */
1198 /* Construct a new context. The context below this one on the stack
1199 is given by NEXT. */
1201 static cp_parser_context *
1202 cp_parser_context_new (cp_parser_context* next)
1204 cp_parser_context *context;
1206 /* Allocate the storage. */
1207 if (cp_parser_context_free_list != NULL)
1209 /* Pull the first entry from the free list. */
1210 context = cp_parser_context_free_list;
1211 cp_parser_context_free_list = context->next;
1212 memset (context, 0, sizeof (*context));
1214 else
1215 context = GGC_CNEW (cp_parser_context);
1217 /* No errors have occurred yet in this context. */
1218 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1219 /* If this is not the bottomost context, copy information that we
1220 need from the previous context. */
1221 if (next)
1223 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1224 expression, then we are parsing one in this context, too. */
1225 context->object_type = next->object_type;
1226 /* Thread the stack. */
1227 context->next = next;
1230 return context;
1233 /* The cp_parser structure represents the C++ parser. */
1235 typedef struct cp_parser GTY(())
1237 /* The lexer from which we are obtaining tokens. */
1238 cp_lexer *lexer;
1240 /* The scope in which names should be looked up. If NULL_TREE, then
1241 we look up names in the scope that is currently open in the
1242 source program. If non-NULL, this is either a TYPE or
1243 NAMESPACE_DECL for the scope in which we should look.
1245 This value is not cleared automatically after a name is looked
1246 up, so we must be careful to clear it before starting a new look
1247 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1248 will look up `Z' in the scope of `X', rather than the current
1249 scope.) Unfortunately, it is difficult to tell when name lookup
1250 is complete, because we sometimes peek at a token, look it up,
1251 and then decide not to consume it. */
1252 tree scope;
1254 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1255 last lookup took place. OBJECT_SCOPE is used if an expression
1256 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1257 respectively. QUALIFYING_SCOPE is used for an expression of the
1258 form "X::Y"; it refers to X. */
1259 tree object_scope;
1260 tree qualifying_scope;
1262 /* A stack of parsing contexts. All but the bottom entry on the
1263 stack will be tentative contexts.
1265 We parse tentatively in order to determine which construct is in
1266 use in some situations. For example, in order to determine
1267 whether a statement is an expression-statement or a
1268 declaration-statement we parse it tentatively as a
1269 declaration-statement. If that fails, we then reparse the same
1270 token stream as an expression-statement. */
1271 cp_parser_context *context;
1273 /* True if we are parsing GNU C++. If this flag is not set, then
1274 GNU extensions are not recognized. */
1275 bool allow_gnu_extensions_p;
1277 /* TRUE if the `>' token should be interpreted as the greater-than
1278 operator. FALSE if it is the end of a template-id or
1279 template-parameter-list. */
1280 bool greater_than_is_operator_p;
1282 /* TRUE if default arguments are allowed within a parameter list
1283 that starts at this point. FALSE if only a gnu extension makes
1284 them permissible. */
1285 bool default_arg_ok_p;
1287 /* TRUE if we are parsing an integral constant-expression. See
1288 [expr.const] for a precise definition. */
1289 bool integral_constant_expression_p;
1291 /* TRUE if we are parsing an integral constant-expression -- but a
1292 non-constant expression should be permitted as well. This flag
1293 is used when parsing an array bound so that GNU variable-length
1294 arrays are tolerated. */
1295 bool allow_non_integral_constant_expression_p;
1297 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1298 been seen that makes the expression non-constant. */
1299 bool non_integral_constant_expression_p;
1301 /* TRUE if local variable names and `this' are forbidden in the
1302 current context. */
1303 bool local_variables_forbidden_p;
1305 /* TRUE if the declaration we are parsing is part of a
1306 linkage-specification of the form `extern string-literal
1307 declaration'. */
1308 bool in_unbraced_linkage_specification_p;
1310 /* TRUE if we are presently parsing a declarator, after the
1311 direct-declarator. */
1312 bool in_declarator_p;
1314 /* TRUE if we are presently parsing a template-argument-list. */
1315 bool in_template_argument_list_p;
1317 /* TRUE if we are presently parsing the body of an
1318 iteration-statement. */
1319 bool in_iteration_statement_p;
1321 /* TRUE if we are presently parsing the body of a switch
1322 statement. */
1323 bool in_switch_statement_p;
1325 /* TRUE if we are parsing a type-id in an expression context. In
1326 such a situation, both "type (expr)" and "type (type)" are valid
1327 alternatives. */
1328 bool in_type_id_in_expr_p;
1330 /* TRUE if we are currently in a header file where declarations are
1331 implicitly extern "C". */
1332 bool implicit_extern_c;
1334 /* TRUE if strings in expressions should be translated to the execution
1335 character set. */
1336 bool translate_strings_p;
1338 /* If non-NULL, then we are parsing a construct where new type
1339 definitions are not permitted. The string stored here will be
1340 issued as an error message if a type is defined. */
1341 const char *type_definition_forbidden_message;
1343 /* A list of lists. The outer list is a stack, used for member
1344 functions of local classes. At each level there are two sub-list,
1345 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1346 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1347 TREE_VALUE's. The functions are chained in reverse declaration
1348 order.
1350 The TREE_PURPOSE sublist contains those functions with default
1351 arguments that need post processing, and the TREE_VALUE sublist
1352 contains those functions with definitions that need post
1353 processing.
1355 These lists can only be processed once the outermost class being
1356 defined is complete. */
1357 tree unparsed_functions_queues;
1359 /* The number of classes whose definitions are currently in
1360 progress. */
1361 unsigned num_classes_being_defined;
1363 /* The number of template parameter lists that apply directly to the
1364 current declaration. */
1365 unsigned num_template_parameter_lists;
1366 } cp_parser;
1368 /* The type of a function that parses some kind of expression. */
1369 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1371 /* Prototypes. */
1373 /* Constructors and destructors. */
1375 static cp_parser *cp_parser_new
1376 (void);
1378 /* Routines to parse various constructs.
1380 Those that return `tree' will return the error_mark_node (rather
1381 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1382 Sometimes, they will return an ordinary node if error-recovery was
1383 attempted, even though a parse error occurred. So, to check
1384 whether or not a parse error occurred, you should always use
1385 cp_parser_error_occurred. If the construct is optional (indicated
1386 either by an `_opt' in the name of the function that does the
1387 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1388 the construct is not present. */
1390 /* Lexical conventions [gram.lex] */
1392 static tree cp_parser_identifier
1393 (cp_parser *);
1394 static tree cp_parser_string_literal
1395 (cp_parser *, bool, bool);
1397 /* Basic concepts [gram.basic] */
1399 static bool cp_parser_translation_unit
1400 (cp_parser *);
1402 /* Expressions [gram.expr] */
1404 static tree cp_parser_primary_expression
1405 (cp_parser *, cp_id_kind *, tree *);
1406 static tree cp_parser_id_expression
1407 (cp_parser *, bool, bool, bool *, bool);
1408 static tree cp_parser_unqualified_id
1409 (cp_parser *, bool, bool, bool);
1410 static tree cp_parser_nested_name_specifier_opt
1411 (cp_parser *, bool, bool, bool, bool);
1412 static tree cp_parser_nested_name_specifier
1413 (cp_parser *, bool, bool, bool, bool);
1414 static tree cp_parser_class_or_namespace_name
1415 (cp_parser *, bool, bool, bool, bool, bool);
1416 static tree cp_parser_postfix_expression
1417 (cp_parser *, bool);
1418 static tree cp_parser_postfix_open_square_expression
1419 (cp_parser *, tree, bool);
1420 static tree cp_parser_postfix_dot_deref_expression
1421 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1422 static tree cp_parser_parenthesized_expression_list
1423 (cp_parser *, bool, bool *);
1424 static void cp_parser_pseudo_destructor_name
1425 (cp_parser *, tree *, tree *);
1426 static tree cp_parser_unary_expression
1427 (cp_parser *, bool);
1428 static enum tree_code cp_parser_unary_operator
1429 (cp_token *);
1430 static tree cp_parser_new_expression
1431 (cp_parser *);
1432 static tree cp_parser_new_placement
1433 (cp_parser *);
1434 static tree cp_parser_new_type_id
1435 (cp_parser *, tree *);
1436 static cp_declarator *cp_parser_new_declarator_opt
1437 (cp_parser *);
1438 static cp_declarator *cp_parser_direct_new_declarator
1439 (cp_parser *);
1440 static tree cp_parser_new_initializer
1441 (cp_parser *);
1442 static tree cp_parser_delete_expression
1443 (cp_parser *);
1444 static tree cp_parser_cast_expression
1445 (cp_parser *, bool);
1446 static tree cp_parser_binary_expression
1447 (cp_parser *);
1448 static tree cp_parser_question_colon_clause
1449 (cp_parser *, tree);
1450 static tree cp_parser_assignment_expression
1451 (cp_parser *);
1452 static enum tree_code cp_parser_assignment_operator_opt
1453 (cp_parser *);
1454 static tree cp_parser_expression
1455 (cp_parser *);
1456 static tree cp_parser_constant_expression
1457 (cp_parser *, bool, bool *);
1458 static tree cp_parser_builtin_offsetof
1459 (cp_parser *);
1461 /* Statements [gram.stmt.stmt] */
1463 static void cp_parser_statement
1464 (cp_parser *, tree);
1465 static tree cp_parser_labeled_statement
1466 (cp_parser *, tree);
1467 static tree cp_parser_expression_statement
1468 (cp_parser *, tree);
1469 static tree cp_parser_compound_statement
1470 (cp_parser *, tree, bool);
1471 static void cp_parser_statement_seq_opt
1472 (cp_parser *, tree);
1473 static tree cp_parser_selection_statement
1474 (cp_parser *);
1475 static tree cp_parser_condition
1476 (cp_parser *);
1477 static tree cp_parser_iteration_statement
1478 (cp_parser *);
1479 static void cp_parser_for_init_statement
1480 (cp_parser *);
1481 static tree cp_parser_jump_statement
1482 (cp_parser *);
1483 static void cp_parser_declaration_statement
1484 (cp_parser *);
1486 static tree cp_parser_implicitly_scoped_statement
1487 (cp_parser *);
1488 static void cp_parser_already_scoped_statement
1489 (cp_parser *);
1491 /* Declarations [gram.dcl.dcl] */
1493 static void cp_parser_declaration_seq_opt
1494 (cp_parser *);
1495 static void cp_parser_declaration
1496 (cp_parser *);
1497 static void cp_parser_block_declaration
1498 (cp_parser *, bool);
1499 static void cp_parser_simple_declaration
1500 (cp_parser *, bool);
1501 static void cp_parser_decl_specifier_seq
1502 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1503 static tree cp_parser_storage_class_specifier_opt
1504 (cp_parser *);
1505 static tree cp_parser_function_specifier_opt
1506 (cp_parser *, cp_decl_specifier_seq *);
1507 static tree cp_parser_type_specifier
1508 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1509 int *, bool *);
1510 static tree cp_parser_simple_type_specifier
1511 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1512 static tree cp_parser_type_name
1513 (cp_parser *);
1514 static tree cp_parser_elaborated_type_specifier
1515 (cp_parser *, bool, bool);
1516 static tree cp_parser_enum_specifier
1517 (cp_parser *);
1518 static void cp_parser_enumerator_list
1519 (cp_parser *, tree);
1520 static void cp_parser_enumerator_definition
1521 (cp_parser *, tree);
1522 static tree cp_parser_namespace_name
1523 (cp_parser *);
1524 static void cp_parser_namespace_definition
1525 (cp_parser *);
1526 static void cp_parser_namespace_body
1527 (cp_parser *);
1528 static tree cp_parser_qualified_namespace_specifier
1529 (cp_parser *);
1530 static void cp_parser_namespace_alias_definition
1531 (cp_parser *);
1532 static void cp_parser_using_declaration
1533 (cp_parser *);
1534 static void cp_parser_using_directive
1535 (cp_parser *);
1536 static void cp_parser_asm_definition
1537 (cp_parser *);
1538 static void cp_parser_linkage_specification
1539 (cp_parser *);
1541 /* Declarators [gram.dcl.decl] */
1543 static tree cp_parser_init_declarator
1544 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1545 static cp_declarator *cp_parser_declarator
1546 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1547 static cp_declarator *cp_parser_direct_declarator
1548 (cp_parser *, cp_parser_declarator_kind, int *);
1549 static enum tree_code cp_parser_ptr_operator
1550 (cp_parser *, tree *, cp_cv_quals *);
1551 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1552 (cp_parser *);
1553 static tree cp_parser_declarator_id
1554 (cp_parser *);
1555 static tree cp_parser_type_id
1556 (cp_parser *);
1557 static void cp_parser_type_specifier_seq
1558 (cp_parser *, cp_decl_specifier_seq *);
1559 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1560 (cp_parser *);
1561 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1562 (cp_parser *, bool *);
1563 static cp_parameter_declarator *cp_parser_parameter_declaration
1564 (cp_parser *, bool, bool *);
1565 static void cp_parser_function_body
1566 (cp_parser *);
1567 static tree cp_parser_initializer
1568 (cp_parser *, bool *, bool *);
1569 static tree cp_parser_initializer_clause
1570 (cp_parser *, bool *);
1571 static tree cp_parser_initializer_list
1572 (cp_parser *, bool *);
1574 static bool cp_parser_ctor_initializer_opt_and_function_body
1575 (cp_parser *);
1577 /* Classes [gram.class] */
1579 static tree cp_parser_class_name
1580 (cp_parser *, bool, bool, bool, bool, bool, bool);
1581 static tree cp_parser_class_specifier
1582 (cp_parser *);
1583 static tree cp_parser_class_head
1584 (cp_parser *, bool *, tree *);
1585 static enum tag_types cp_parser_class_key
1586 (cp_parser *);
1587 static void cp_parser_member_specification_opt
1588 (cp_parser *);
1589 static void cp_parser_member_declaration
1590 (cp_parser *);
1591 static tree cp_parser_pure_specifier
1592 (cp_parser *);
1593 static tree cp_parser_constant_initializer
1594 (cp_parser *);
1596 /* Derived classes [gram.class.derived] */
1598 static tree cp_parser_base_clause
1599 (cp_parser *);
1600 static tree cp_parser_base_specifier
1601 (cp_parser *);
1603 /* Special member functions [gram.special] */
1605 static tree cp_parser_conversion_function_id
1606 (cp_parser *);
1607 static tree cp_parser_conversion_type_id
1608 (cp_parser *);
1609 static cp_declarator *cp_parser_conversion_declarator_opt
1610 (cp_parser *);
1611 static bool cp_parser_ctor_initializer_opt
1612 (cp_parser *);
1613 static void cp_parser_mem_initializer_list
1614 (cp_parser *);
1615 static tree cp_parser_mem_initializer
1616 (cp_parser *);
1617 static tree cp_parser_mem_initializer_id
1618 (cp_parser *);
1620 /* Overloading [gram.over] */
1622 static tree cp_parser_operator_function_id
1623 (cp_parser *);
1624 static tree cp_parser_operator
1625 (cp_parser *);
1627 /* Templates [gram.temp] */
1629 static void cp_parser_template_declaration
1630 (cp_parser *, bool);
1631 static tree cp_parser_template_parameter_list
1632 (cp_parser *);
1633 static tree cp_parser_template_parameter
1634 (cp_parser *, bool *);
1635 static tree cp_parser_type_parameter
1636 (cp_parser *);
1637 static tree cp_parser_template_id
1638 (cp_parser *, bool, bool, bool);
1639 static tree cp_parser_template_name
1640 (cp_parser *, bool, bool, bool, bool *);
1641 static tree cp_parser_template_argument_list
1642 (cp_parser *);
1643 static tree cp_parser_template_argument
1644 (cp_parser *);
1645 static void cp_parser_explicit_instantiation
1646 (cp_parser *);
1647 static void cp_parser_explicit_specialization
1648 (cp_parser *);
1650 /* Exception handling [gram.exception] */
1652 static tree cp_parser_try_block
1653 (cp_parser *);
1654 static bool cp_parser_function_try_block
1655 (cp_parser *);
1656 static void cp_parser_handler_seq
1657 (cp_parser *);
1658 static void cp_parser_handler
1659 (cp_parser *);
1660 static tree cp_parser_exception_declaration
1661 (cp_parser *);
1662 static tree cp_parser_throw_expression
1663 (cp_parser *);
1664 static tree cp_parser_exception_specification_opt
1665 (cp_parser *);
1666 static tree cp_parser_type_id_list
1667 (cp_parser *);
1669 /* GNU Extensions */
1671 static tree cp_parser_asm_specification_opt
1672 (cp_parser *);
1673 static tree cp_parser_asm_operand_list
1674 (cp_parser *);
1675 static tree cp_parser_asm_clobber_list
1676 (cp_parser *);
1677 static tree cp_parser_attributes_opt
1678 (cp_parser *);
1679 static tree cp_parser_attribute_list
1680 (cp_parser *);
1681 static bool cp_parser_extension_opt
1682 (cp_parser *, int *);
1683 static void cp_parser_label_declaration
1684 (cp_parser *);
1686 /* Utility Routines */
1688 static tree cp_parser_lookup_name
1689 (cp_parser *, tree, bool, bool, bool, bool, bool *);
1690 static tree cp_parser_lookup_name_simple
1691 (cp_parser *, tree);
1692 static tree cp_parser_maybe_treat_template_as_class
1693 (tree, bool);
1694 static bool cp_parser_check_declarator_template_parameters
1695 (cp_parser *, cp_declarator *);
1696 static bool cp_parser_check_template_parameters
1697 (cp_parser *, unsigned);
1698 static tree cp_parser_simple_cast_expression
1699 (cp_parser *);
1700 static tree cp_parser_global_scope_opt
1701 (cp_parser *, bool);
1702 static bool cp_parser_constructor_declarator_p
1703 (cp_parser *, bool);
1704 static tree cp_parser_function_definition_from_specifiers_and_declarator
1705 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1706 static tree cp_parser_function_definition_after_declarator
1707 (cp_parser *, bool);
1708 static void cp_parser_template_declaration_after_export
1709 (cp_parser *, bool);
1710 static tree cp_parser_single_declaration
1711 (cp_parser *, bool, bool *);
1712 static tree cp_parser_functional_cast
1713 (cp_parser *, tree);
1714 static tree cp_parser_save_member_function_body
1715 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1716 static tree cp_parser_enclosed_template_argument_list
1717 (cp_parser *);
1718 static void cp_parser_save_default_args
1719 (cp_parser *, tree);
1720 static void cp_parser_late_parsing_for_member
1721 (cp_parser *, tree);
1722 static void cp_parser_late_parsing_default_args
1723 (cp_parser *, tree);
1724 static tree cp_parser_sizeof_operand
1725 (cp_parser *, enum rid);
1726 static bool cp_parser_declares_only_class_p
1727 (cp_parser *);
1728 static void cp_parser_set_storage_class
1729 (cp_decl_specifier_seq *, cp_storage_class);
1730 static void cp_parser_set_decl_spec_type
1731 (cp_decl_specifier_seq *, tree, bool);
1732 static bool cp_parser_friend_p
1733 (const cp_decl_specifier_seq *);
1734 static cp_token *cp_parser_require
1735 (cp_parser *, enum cpp_ttype, const char *);
1736 static cp_token *cp_parser_require_keyword
1737 (cp_parser *, enum rid, const char *);
1738 static bool cp_parser_token_starts_function_definition_p
1739 (cp_token *);
1740 static bool cp_parser_next_token_starts_class_definition_p
1741 (cp_parser *);
1742 static bool cp_parser_next_token_ends_template_argument_p
1743 (cp_parser *);
1744 static bool cp_parser_nth_token_starts_template_argument_list_p
1745 (cp_parser *, size_t);
1746 static enum tag_types cp_parser_token_is_class_key
1747 (cp_token *);
1748 static void cp_parser_check_class_key
1749 (enum tag_types, tree type);
1750 static void cp_parser_check_access_in_redeclaration
1751 (tree type);
1752 static bool cp_parser_optional_template_keyword
1753 (cp_parser *);
1754 static void cp_parser_pre_parsed_nested_name_specifier
1755 (cp_parser *);
1756 static void cp_parser_cache_group
1757 (cp_parser *, enum cpp_ttype, unsigned);
1758 static void cp_parser_parse_tentatively
1759 (cp_parser *);
1760 static void cp_parser_commit_to_tentative_parse
1761 (cp_parser *);
1762 static void cp_parser_abort_tentative_parse
1763 (cp_parser *);
1764 static bool cp_parser_parse_definitely
1765 (cp_parser *);
1766 static inline bool cp_parser_parsing_tentatively
1767 (cp_parser *);
1768 static bool cp_parser_committed_to_tentative_parse
1769 (cp_parser *);
1770 static void cp_parser_error
1771 (cp_parser *, const char *);
1772 static void cp_parser_name_lookup_error
1773 (cp_parser *, tree, tree, const char *);
1774 static bool cp_parser_simulate_error
1775 (cp_parser *);
1776 static void cp_parser_check_type_definition
1777 (cp_parser *);
1778 static void cp_parser_check_for_definition_in_return_type
1779 (cp_declarator *, int);
1780 static void cp_parser_check_for_invalid_template_id
1781 (cp_parser *, tree);
1782 static bool cp_parser_non_integral_constant_expression
1783 (cp_parser *, const char *);
1784 static void cp_parser_diagnose_invalid_type_name
1785 (cp_parser *, tree, tree);
1786 static bool cp_parser_parse_and_diagnose_invalid_type_name
1787 (cp_parser *);
1788 static int cp_parser_skip_to_closing_parenthesis
1789 (cp_parser *, bool, bool, bool);
1790 static void cp_parser_skip_to_end_of_statement
1791 (cp_parser *);
1792 static void cp_parser_consume_semicolon_at_end_of_statement
1793 (cp_parser *);
1794 static void cp_parser_skip_to_end_of_block_or_statement
1795 (cp_parser *);
1796 static void cp_parser_skip_to_closing_brace
1797 (cp_parser *);
1798 static void cp_parser_skip_until_found
1799 (cp_parser *, enum cpp_ttype, const char *);
1800 static bool cp_parser_error_occurred
1801 (cp_parser *);
1802 static bool cp_parser_allow_gnu_extensions_p
1803 (cp_parser *);
1804 static bool cp_parser_is_string_literal
1805 (cp_token *);
1806 static bool cp_parser_is_keyword
1807 (cp_token *, enum rid);
1808 static tree cp_parser_make_typename_type
1809 (cp_parser *, tree, tree);
1811 /* Returns nonzero if we are parsing tentatively. */
1813 static inline bool
1814 cp_parser_parsing_tentatively (cp_parser* parser)
1816 return parser->context->next != NULL;
1819 /* Returns nonzero if TOKEN is a string literal. */
1821 static bool
1822 cp_parser_is_string_literal (cp_token* token)
1824 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1827 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1829 static bool
1830 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1832 return token->keyword == keyword;
1835 /* If not parsing tentatively, issue a diagnostic of the form
1836 FILE:LINE: MESSAGE before TOKEN
1837 where TOKEN is the next token in the input stream. MESSAGE
1838 (specified by the caller) is usually of the form "expected
1839 OTHER-TOKEN". */
1841 static void
1842 cp_parser_error (cp_parser* parser, const char* message)
1844 if (!cp_parser_simulate_error (parser))
1846 cp_token *token = cp_lexer_peek_token (parser->lexer);
1847 /* This diagnostic makes more sense if it is tagged to the line
1848 of the token we just peeked at. */
1849 cp_lexer_set_source_position_from_token (token);
1850 c_parse_error (message,
1851 /* Because c_parser_error does not understand
1852 CPP_KEYWORD, keywords are treated like
1853 identifiers. */
1854 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1855 token->value);
1859 /* Issue an error about name-lookup failing. NAME is the
1860 IDENTIFIER_NODE DECL is the result of
1861 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1862 the thing that we hoped to find. */
1864 static void
1865 cp_parser_name_lookup_error (cp_parser* parser,
1866 tree name,
1867 tree decl,
1868 const char* desired)
1870 /* If name lookup completely failed, tell the user that NAME was not
1871 declared. */
1872 if (decl == error_mark_node)
1874 if (parser->scope && parser->scope != global_namespace)
1875 error ("%<%D::%D%> has not been declared",
1876 parser->scope, name);
1877 else if (parser->scope == global_namespace)
1878 error ("%<::%D%> has not been declared", name);
1879 else if (parser->object_scope
1880 && !CLASS_TYPE_P (parser->object_scope))
1881 error ("request for member %qD in non-class type %qT",
1882 name, parser->object_scope);
1883 else if (parser->object_scope)
1884 error ("%<%T::%D%> has not been declared",
1885 parser->object_scope, name);
1886 else
1887 error ("`%D' has not been declared", name);
1889 else if (parser->scope && parser->scope != global_namespace)
1890 error ("%<%D::%D%> %s", parser->scope, name, desired);
1891 else if (parser->scope == global_namespace)
1892 error ("%<::%D%> %s", name, desired);
1893 else
1894 error ("%qD %s", name, desired);
1897 /* If we are parsing tentatively, remember that an error has occurred
1898 during this tentative parse. Returns true if the error was
1899 simulated; false if a message should be issued by the caller. */
1901 static bool
1902 cp_parser_simulate_error (cp_parser* parser)
1904 if (cp_parser_parsing_tentatively (parser)
1905 && !cp_parser_committed_to_tentative_parse (parser))
1907 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1908 return true;
1910 return false;
1913 /* This function is called when a type is defined. If type
1914 definitions are forbidden at this point, an error message is
1915 issued. */
1917 static void
1918 cp_parser_check_type_definition (cp_parser* parser)
1920 /* If types are forbidden here, issue a message. */
1921 if (parser->type_definition_forbidden_message)
1922 /* Use `%s' to print the string in case there are any escape
1923 characters in the message. */
1924 error ("%s", parser->type_definition_forbidden_message);
1927 /* This function is called when a declaration is parsed. If
1928 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1929 indicates that a type was defined in the decl-specifiers for DECL,
1930 then an error is issued. */
1932 static void
1933 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1934 int declares_class_or_enum)
1936 /* [dcl.fct] forbids type definitions in return types.
1937 Unfortunately, it's not easy to know whether or not we are
1938 processing a return type until after the fact. */
1939 while (declarator
1940 && (declarator->kind == cdk_pointer
1941 || declarator->kind == cdk_reference
1942 || declarator->kind == cdk_ptrmem))
1943 declarator = declarator->declarator;
1944 if (declarator
1945 && declarator->kind == cdk_function
1946 && declares_class_or_enum & 2)
1947 error ("new types may not be defined in a return type");
1950 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1951 "<" in any valid C++ program. If the next token is indeed "<",
1952 issue a message warning the user about what appears to be an
1953 invalid attempt to form a template-id. */
1955 static void
1956 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1957 tree type)
1959 ptrdiff_t start;
1960 cp_token *token;
1962 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1964 if (TYPE_P (type))
1965 error ("%qT is not a template", type);
1966 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1967 error ("%qE is not a template", type);
1968 else
1969 error ("invalid template-id");
1970 /* Remember the location of the invalid "<". */
1971 if (cp_parser_parsing_tentatively (parser)
1972 && !cp_parser_committed_to_tentative_parse (parser))
1974 token = cp_lexer_peek_token (parser->lexer);
1975 token = cp_lexer_prev_token (parser->lexer, token);
1976 start = cp_lexer_token_difference (parser->lexer,
1977 parser->lexer->buffer,
1978 token);
1980 else
1981 start = -1;
1982 /* Consume the "<". */
1983 cp_lexer_consume_token (parser->lexer);
1984 /* Parse the template arguments. */
1985 cp_parser_enclosed_template_argument_list (parser);
1986 /* Permanently remove the invalid template arguments so that
1987 this error message is not issued again. */
1988 if (start >= 0)
1990 token = cp_lexer_advance_token (parser->lexer,
1991 parser->lexer->buffer,
1992 start);
1993 cp_lexer_purge_tokens_after (parser->lexer, token);
1998 /* If parsing an integral constant-expression, issue an error message
1999 about the fact that THING appeared and return true. Otherwise,
2000 return false, marking the current expression as non-constant. */
2002 static bool
2003 cp_parser_non_integral_constant_expression (cp_parser *parser,
2004 const char *thing)
2006 if (parser->integral_constant_expression_p)
2008 if (!parser->allow_non_integral_constant_expression_p)
2010 error ("%s cannot appear in a constant-expression", thing);
2011 return true;
2013 parser->non_integral_constant_expression_p = true;
2015 return false;
2018 /* Emit a diagnostic for an invalid type name. Consider also if it is
2019 qualified or not and the result of a lookup, to provide a better
2020 message. */
2022 static void
2023 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2025 tree decl, old_scope;
2026 /* Try to lookup the identifier. */
2027 old_scope = parser->scope;
2028 parser->scope = scope;
2029 decl = cp_parser_lookup_name_simple (parser, id);
2030 parser->scope = old_scope;
2031 /* If the lookup found a template-name, it means that the user forgot
2032 to specify an argument list. Emit an useful error message. */
2033 if (TREE_CODE (decl) == TEMPLATE_DECL)
2034 error ("invalid use of template-name %qE without an argument list",
2035 decl);
2036 else if (!parser->scope)
2038 /* Issue an error message. */
2039 error ("%qE does not name a type", id);
2040 /* If we're in a template class, it's possible that the user was
2041 referring to a type from a base class. For example:
2043 template <typename T> struct A { typedef T X; };
2044 template <typename T> struct B : public A<T> { X x; };
2046 The user should have said "typename A<T>::X". */
2047 if (processing_template_decl && current_class_type)
2049 tree b;
2051 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2053 b = TREE_CHAIN (b))
2055 tree base_type = BINFO_TYPE (b);
2056 if (CLASS_TYPE_P (base_type)
2057 && dependent_type_p (base_type))
2059 tree field;
2060 /* Go from a particular instantiation of the
2061 template (which will have an empty TYPE_FIELDs),
2062 to the main version. */
2063 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2064 for (field = TYPE_FIELDS (base_type);
2065 field;
2066 field = TREE_CHAIN (field))
2067 if (TREE_CODE (field) == TYPE_DECL
2068 && DECL_NAME (field) == id)
2070 inform ("(perhaps `typename %T::%E' was intended)",
2071 BINFO_TYPE (b), id);
2072 break;
2074 if (field)
2075 break;
2080 /* Here we diagnose qualified-ids where the scope is actually correct,
2081 but the identifier does not resolve to a valid type name. */
2082 else
2084 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2085 error ("%qE in namespace %qE does not name a type",
2086 id, parser->scope);
2087 else if (TYPE_P (parser->scope))
2088 error ("q%E in class %qT does not name a type", id, parser->scope);
2089 else
2090 gcc_unreachable ();
2094 /* Check for a common situation where a type-name should be present,
2095 but is not, and issue a sensible error message. Returns true if an
2096 invalid type-name was detected.
2098 The situation handled by this function are variable declarations of the
2099 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2100 Usually, `ID' should name a type, but if we got here it means that it
2101 does not. We try to emit the best possible error message depending on
2102 how exactly the id-expression looks like.
2105 static bool
2106 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2108 tree id;
2110 cp_parser_parse_tentatively (parser);
2111 id = cp_parser_id_expression (parser,
2112 /*template_keyword_p=*/false,
2113 /*check_dependency_p=*/true,
2114 /*template_p=*/NULL,
2115 /*declarator_p=*/true);
2116 /* After the id-expression, there should be a plain identifier,
2117 otherwise this is not a simple variable declaration. Also, if
2118 the scope is dependent, we cannot do much. */
2119 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2120 || (parser->scope && TYPE_P (parser->scope)
2121 && dependent_type_p (parser->scope)))
2123 cp_parser_abort_tentative_parse (parser);
2124 return false;
2126 if (!cp_parser_parse_definitely (parser)
2127 || TREE_CODE (id) != IDENTIFIER_NODE)
2128 return false;
2130 /* Emit a diagnostic for the invalid type. */
2131 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2132 /* Skip to the end of the declaration; there's no point in
2133 trying to process it. */
2134 cp_parser_skip_to_end_of_block_or_statement (parser);
2135 return true;
2138 /* Consume tokens up to, and including, the next non-nested closing `)'.
2139 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2140 are doing error recovery. Returns -1 if OR_COMMA is true and we
2141 found an unnested comma. */
2143 static int
2144 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2145 bool recovering,
2146 bool or_comma,
2147 bool consume_paren)
2149 unsigned paren_depth = 0;
2150 unsigned brace_depth = 0;
2151 int result;
2153 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2154 && !cp_parser_committed_to_tentative_parse (parser))
2155 return 0;
2157 while (true)
2159 cp_token *token;
2161 /* If we've run out of tokens, then there is no closing `)'. */
2162 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2164 result = 0;
2165 break;
2168 token = cp_lexer_peek_token (parser->lexer);
2170 /* This matches the processing in skip_to_end_of_statement. */
2171 if (token->type == CPP_SEMICOLON && !brace_depth)
2173 result = 0;
2174 break;
2176 if (token->type == CPP_OPEN_BRACE)
2177 ++brace_depth;
2178 if (token->type == CPP_CLOSE_BRACE)
2180 if (!brace_depth--)
2182 result = 0;
2183 break;
2186 if (recovering && or_comma && token->type == CPP_COMMA
2187 && !brace_depth && !paren_depth)
2189 result = -1;
2190 break;
2193 if (!brace_depth)
2195 /* If it is an `(', we have entered another level of nesting. */
2196 if (token->type == CPP_OPEN_PAREN)
2197 ++paren_depth;
2198 /* If it is a `)', then we might be done. */
2199 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2201 if (consume_paren)
2202 cp_lexer_consume_token (parser->lexer);
2204 result = 1;
2205 break;
2210 /* Consume the token. */
2211 cp_lexer_consume_token (parser->lexer);
2214 return result;
2217 /* Consume tokens until we reach the end of the current statement.
2218 Normally, that will be just before consuming a `;'. However, if a
2219 non-nested `}' comes first, then we stop before consuming that. */
2221 static void
2222 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2224 unsigned nesting_depth = 0;
2226 while (true)
2228 cp_token *token;
2230 /* Peek at the next token. */
2231 token = cp_lexer_peek_token (parser->lexer);
2232 /* If we've run out of tokens, stop. */
2233 if (token->type == CPP_EOF)
2234 break;
2235 /* If the next token is a `;', we have reached the end of the
2236 statement. */
2237 if (token->type == CPP_SEMICOLON && !nesting_depth)
2238 break;
2239 /* If the next token is a non-nested `}', then we have reached
2240 the end of the current block. */
2241 if (token->type == CPP_CLOSE_BRACE)
2243 /* If this is a non-nested `}', stop before consuming it.
2244 That way, when confronted with something like:
2246 { 3 + }
2248 we stop before consuming the closing `}', even though we
2249 have not yet reached a `;'. */
2250 if (nesting_depth == 0)
2251 break;
2252 /* If it is the closing `}' for a block that we have
2253 scanned, stop -- but only after consuming the token.
2254 That way given:
2256 void f g () { ... }
2257 typedef int I;
2259 we will stop after the body of the erroneously declared
2260 function, but before consuming the following `typedef'
2261 declaration. */
2262 if (--nesting_depth == 0)
2264 cp_lexer_consume_token (parser->lexer);
2265 break;
2268 /* If it the next token is a `{', then we are entering a new
2269 block. Consume the entire block. */
2270 else if (token->type == CPP_OPEN_BRACE)
2271 ++nesting_depth;
2272 /* Consume the token. */
2273 cp_lexer_consume_token (parser->lexer);
2277 /* This function is called at the end of a statement or declaration.
2278 If the next token is a semicolon, it is consumed; otherwise, error
2279 recovery is attempted. */
2281 static void
2282 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2284 /* Look for the trailing `;'. */
2285 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2287 /* If there is additional (erroneous) input, skip to the end of
2288 the statement. */
2289 cp_parser_skip_to_end_of_statement (parser);
2290 /* If the next token is now a `;', consume it. */
2291 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2292 cp_lexer_consume_token (parser->lexer);
2296 /* Skip tokens until we have consumed an entire block, or until we
2297 have consumed a non-nested `;'. */
2299 static void
2300 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2302 unsigned nesting_depth = 0;
2304 while (true)
2306 cp_token *token;
2308 /* Peek at the next token. */
2309 token = cp_lexer_peek_token (parser->lexer);
2310 /* If we've run out of tokens, stop. */
2311 if (token->type == CPP_EOF)
2312 break;
2313 /* If the next token is a `;', we have reached the end of the
2314 statement. */
2315 if (token->type == CPP_SEMICOLON && !nesting_depth)
2317 /* Consume the `;'. */
2318 cp_lexer_consume_token (parser->lexer);
2319 break;
2321 /* Consume the token. */
2322 token = cp_lexer_consume_token (parser->lexer);
2323 /* If the next token is a non-nested `}', then we have reached
2324 the end of the current block. */
2325 if (token->type == CPP_CLOSE_BRACE
2326 && (nesting_depth == 0 || --nesting_depth == 0))
2327 break;
2328 /* If it the next token is a `{', then we are entering a new
2329 block. Consume the entire block. */
2330 if (token->type == CPP_OPEN_BRACE)
2331 ++nesting_depth;
2335 /* Skip tokens until a non-nested closing curly brace is the next
2336 token. */
2338 static void
2339 cp_parser_skip_to_closing_brace (cp_parser *parser)
2341 unsigned nesting_depth = 0;
2343 while (true)
2345 cp_token *token;
2347 /* Peek at the next token. */
2348 token = cp_lexer_peek_token (parser->lexer);
2349 /* If we've run out of tokens, stop. */
2350 if (token->type == CPP_EOF)
2351 break;
2352 /* If the next token is a non-nested `}', then we have reached
2353 the end of the current block. */
2354 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2355 break;
2356 /* If it the next token is a `{', then we are entering a new
2357 block. Consume the entire block. */
2358 else if (token->type == CPP_OPEN_BRACE)
2359 ++nesting_depth;
2360 /* Consume the token. */
2361 cp_lexer_consume_token (parser->lexer);
2365 /* This is a simple wrapper around make_typename_type. When the id is
2366 an unresolved identifier node, we can provide a superior diagnostic
2367 using cp_parser_diagnose_invalid_type_name. */
2369 static tree
2370 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2372 tree result;
2373 if (TREE_CODE (id) == IDENTIFIER_NODE)
2375 result = make_typename_type (scope, id, /*complain=*/0);
2376 if (result == error_mark_node)
2377 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2378 return result;
2380 return make_typename_type (scope, id, tf_error);
2384 /* Create a new C++ parser. */
2386 static cp_parser *
2387 cp_parser_new (void)
2389 cp_parser *parser;
2390 cp_lexer *lexer;
2391 unsigned i;
2393 /* cp_lexer_new_main is called before calling ggc_alloc because
2394 cp_lexer_new_main might load a PCH file. */
2395 lexer = cp_lexer_new_main ();
2397 /* Initialize the binops_by_token so that we can get the tree
2398 directly from the token. */
2399 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2400 binops_by_token[binops[i].token_type] = binops[i];
2402 parser = GGC_CNEW (cp_parser);
2403 parser->lexer = lexer;
2404 parser->context = cp_parser_context_new (NULL);
2406 /* For now, we always accept GNU extensions. */
2407 parser->allow_gnu_extensions_p = 1;
2409 /* The `>' token is a greater-than operator, not the end of a
2410 template-id. */
2411 parser->greater_than_is_operator_p = true;
2413 parser->default_arg_ok_p = true;
2415 /* We are not parsing a constant-expression. */
2416 parser->integral_constant_expression_p = false;
2417 parser->allow_non_integral_constant_expression_p = false;
2418 parser->non_integral_constant_expression_p = false;
2420 /* Local variable names are not forbidden. */
2421 parser->local_variables_forbidden_p = false;
2423 /* We are not processing an `extern "C"' declaration. */
2424 parser->in_unbraced_linkage_specification_p = false;
2426 /* We are not processing a declarator. */
2427 parser->in_declarator_p = false;
2429 /* We are not processing a template-argument-list. */
2430 parser->in_template_argument_list_p = false;
2432 /* We are not in an iteration statement. */
2433 parser->in_iteration_statement_p = false;
2435 /* We are not in a switch statement. */
2436 parser->in_switch_statement_p = false;
2438 /* We are not parsing a type-id inside an expression. */
2439 parser->in_type_id_in_expr_p = false;
2441 /* Declarations aren't implicitly extern "C". */
2442 parser->implicit_extern_c = false;
2444 /* String literals should be translated to the execution character set. */
2445 parser->translate_strings_p = true;
2447 /* The unparsed function queue is empty. */
2448 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2450 /* There are no classes being defined. */
2451 parser->num_classes_being_defined = 0;
2453 /* No template parameters apply. */
2454 parser->num_template_parameter_lists = 0;
2456 return parser;
2459 /* Create a cp_lexer structure which will emit the tokens in CACHE
2460 and push it onto the parser's lexer stack. This is used for delayed
2461 parsing of in-class method bodies and default arguments, and should
2462 not be confused with tentative parsing. */
2463 static void
2464 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2466 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2467 lexer->next = parser->lexer;
2468 parser->lexer = lexer;
2470 /* Move the current source position to that of the first token in the
2471 new lexer. */
2472 cp_lexer_set_source_position_from_token (lexer->next_token);
2475 /* Pop the top lexer off the parser stack. This is never used for the
2476 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2477 static void
2478 cp_parser_pop_lexer (cp_parser *parser)
2480 cp_lexer *lexer = parser->lexer;
2481 parser->lexer = lexer->next;
2482 cp_lexer_destroy (lexer);
2484 /* Put the current source position back where it was before this
2485 lexer was pushed. */
2486 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2489 /* Lexical conventions [gram.lex] */
2491 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2492 identifier. */
2494 static tree
2495 cp_parser_identifier (cp_parser* parser)
2497 cp_token *token;
2499 /* Look for the identifier. */
2500 token = cp_parser_require (parser, CPP_NAME, "identifier");
2501 /* Return the value. */
2502 return token ? token->value : error_mark_node;
2505 /* Parse a sequence of adjacent string constants. Returns a
2506 TREE_STRING representing the combined, nul-terminated string
2507 constant. If TRANSLATE is true, translate the string to the
2508 execution character set. If WIDE_OK is true, a wide string is
2509 invalid here.
2511 C++98 [lex.string] says that if a narrow string literal token is
2512 adjacent to a wide string literal token, the behavior is undefined.
2513 However, C99 6.4.5p4 says that this results in a wide string literal.
2514 We follow C99 here, for consistency with the C front end.
2516 This code is largely lifted from lex_string() in c-lex.c.
2518 FUTURE: ObjC++ will need to handle @-strings here. */
2519 static tree
2520 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2522 tree value;
2523 bool wide = false;
2524 size_t count;
2525 struct obstack str_ob;
2526 cpp_string str, istr, *strs;
2527 cp_token *tok;
2529 tok = cp_lexer_peek_token (parser->lexer);
2530 if (!cp_parser_is_string_literal (tok))
2532 cp_parser_error (parser, "expected string-literal");
2533 return error_mark_node;
2536 /* Try to avoid the overhead of creating and destroying an obstack
2537 for the common case of just one string. */
2538 if (!cp_parser_is_string_literal
2539 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2541 cp_lexer_consume_token (parser->lexer);
2543 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2544 str.len = TREE_STRING_LENGTH (tok->value);
2545 count = 1;
2546 if (tok->type == CPP_WSTRING)
2547 wide = true;
2549 strs = &str;
2551 else
2553 gcc_obstack_init (&str_ob);
2554 count = 0;
2558 cp_lexer_consume_token (parser->lexer);
2559 count++;
2560 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2561 str.len = TREE_STRING_LENGTH (tok->value);
2562 if (tok->type == CPP_WSTRING)
2563 wide = true;
2565 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2567 tok = cp_lexer_peek_token (parser->lexer);
2569 while (cp_parser_is_string_literal (tok));
2571 strs = (cpp_string *) obstack_finish (&str_ob);
2574 if (wide && !wide_ok)
2576 cp_parser_error (parser, "a wide string is invalid in this context");
2577 wide = false;
2580 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2581 (parse_in, strs, count, &istr, wide))
2583 value = build_string (istr.len, (char *)istr.text);
2584 free ((void *)istr.text);
2586 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2587 value = fix_string_type (value);
2589 else
2590 /* cpp_interpret_string has issued an error. */
2591 value = error_mark_node;
2593 if (count > 1)
2594 obstack_free (&str_ob, 0);
2596 return value;
2600 /* Basic concepts [gram.basic] */
2602 /* Parse a translation-unit.
2604 translation-unit:
2605 declaration-seq [opt]
2607 Returns TRUE if all went well. */
2609 static bool
2610 cp_parser_translation_unit (cp_parser* parser)
2612 /* The address of the first non-permanent object on the declarator
2613 obstack. */
2614 static void *declarator_obstack_base;
2616 bool success;
2618 /* Create the declarator obstack, if necessary. */
2619 if (!cp_error_declarator)
2621 gcc_obstack_init (&declarator_obstack);
2622 /* Create the error declarator. */
2623 cp_error_declarator = make_declarator (cdk_error);
2624 /* Create the empty parameter list. */
2625 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2626 /* Remember where the base of the declarator obstack lies. */
2627 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2630 while (true)
2632 cp_parser_declaration_seq_opt (parser);
2634 /* If there are no tokens left then all went well. */
2635 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2637 /* Consume the EOF token. */
2638 cp_parser_require (parser, CPP_EOF, "end-of-file");
2640 /* Get rid of the token array; we don't need it any more. */
2641 cp_lexer_destroy (parser->lexer);
2642 parser->lexer = NULL;
2644 /* This file might have been a context that's implicitly extern
2645 "C". If so, pop the lang context. (Only relevant for PCH.) */
2646 if (parser->implicit_extern_c)
2648 pop_lang_context ();
2649 parser->implicit_extern_c = false;
2652 /* Finish up. */
2653 finish_translation_unit ();
2655 success = true;
2656 break;
2658 else
2660 cp_parser_error (parser, "expected declaration");
2661 success = false;
2662 break;
2666 /* Make sure the declarator obstack was fully cleaned up. */
2667 gcc_assert (obstack_next_free (&declarator_obstack)
2668 == declarator_obstack_base);
2670 /* All went well. */
2671 return success;
2674 /* Expressions [gram.expr] */
2676 /* Parse a primary-expression.
2678 primary-expression:
2679 literal
2680 this
2681 ( expression )
2682 id-expression
2684 GNU Extensions:
2686 primary-expression:
2687 ( compound-statement )
2688 __builtin_va_arg ( assignment-expression , type-id )
2690 literal:
2691 __null
2693 Returns a representation of the expression.
2695 *IDK indicates what kind of id-expression (if any) was present.
2697 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2698 used as the operand of a pointer-to-member. In that case,
2699 *QUALIFYING_CLASS gives the class that is used as the qualifying
2700 class in the pointer-to-member. */
2702 static tree
2703 cp_parser_primary_expression (cp_parser *parser,
2704 cp_id_kind *idk,
2705 tree *qualifying_class)
2707 cp_token *token;
2709 /* Assume the primary expression is not an id-expression. */
2710 *idk = CP_ID_KIND_NONE;
2711 /* And that it cannot be used as pointer-to-member. */
2712 *qualifying_class = NULL_TREE;
2714 /* Peek at the next token. */
2715 token = cp_lexer_peek_token (parser->lexer);
2716 switch (token->type)
2718 /* literal:
2719 integer-literal
2720 character-literal
2721 floating-literal
2722 string-literal
2723 boolean-literal */
2724 case CPP_CHAR:
2725 case CPP_WCHAR:
2726 case CPP_NUMBER:
2727 token = cp_lexer_consume_token (parser->lexer);
2728 return token->value;
2730 case CPP_STRING:
2731 case CPP_WSTRING:
2732 /* ??? Should wide strings be allowed when parser->translate_strings_p
2733 is false (i.e. in attributes)? If not, we can kill the third
2734 argument to cp_parser_string_literal. */
2735 return cp_parser_string_literal (parser,
2736 parser->translate_strings_p,
2737 true);
2739 case CPP_OPEN_PAREN:
2741 tree expr;
2742 bool saved_greater_than_is_operator_p;
2744 /* Consume the `('. */
2745 cp_lexer_consume_token (parser->lexer);
2746 /* Within a parenthesized expression, a `>' token is always
2747 the greater-than operator. */
2748 saved_greater_than_is_operator_p
2749 = parser->greater_than_is_operator_p;
2750 parser->greater_than_is_operator_p = true;
2751 /* If we see `( { ' then we are looking at the beginning of
2752 a GNU statement-expression. */
2753 if (cp_parser_allow_gnu_extensions_p (parser)
2754 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2756 /* Statement-expressions are not allowed by the standard. */
2757 if (pedantic)
2758 pedwarn ("ISO C++ forbids braced-groups within expressions");
2760 /* And they're not allowed outside of a function-body; you
2761 cannot, for example, write:
2763 int i = ({ int j = 3; j + 1; });
2765 at class or namespace scope. */
2766 if (!at_function_scope_p ())
2767 error ("statement-expressions are allowed only inside functions");
2768 /* Start the statement-expression. */
2769 expr = begin_stmt_expr ();
2770 /* Parse the compound-statement. */
2771 cp_parser_compound_statement (parser, expr, false);
2772 /* Finish up. */
2773 expr = finish_stmt_expr (expr, false);
2775 else
2777 /* Parse the parenthesized expression. */
2778 expr = cp_parser_expression (parser);
2779 /* Let the front end know that this expression was
2780 enclosed in parentheses. This matters in case, for
2781 example, the expression is of the form `A::B', since
2782 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2783 not. */
2784 finish_parenthesized_expr (expr);
2786 /* The `>' token might be the end of a template-id or
2787 template-parameter-list now. */
2788 parser->greater_than_is_operator_p
2789 = saved_greater_than_is_operator_p;
2790 /* Consume the `)'. */
2791 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2792 cp_parser_skip_to_end_of_statement (parser);
2794 return expr;
2797 case CPP_KEYWORD:
2798 switch (token->keyword)
2800 /* These two are the boolean literals. */
2801 case RID_TRUE:
2802 cp_lexer_consume_token (parser->lexer);
2803 return boolean_true_node;
2804 case RID_FALSE:
2805 cp_lexer_consume_token (parser->lexer);
2806 return boolean_false_node;
2808 /* The `__null' literal. */
2809 case RID_NULL:
2810 cp_lexer_consume_token (parser->lexer);
2811 return null_node;
2813 /* Recognize the `this' keyword. */
2814 case RID_THIS:
2815 cp_lexer_consume_token (parser->lexer);
2816 if (parser->local_variables_forbidden_p)
2818 error ("%<this%> may not be used in this context");
2819 return error_mark_node;
2821 /* Pointers cannot appear in constant-expressions. */
2822 if (cp_parser_non_integral_constant_expression (parser,
2823 "`this'"))
2824 return error_mark_node;
2825 return finish_this_expr ();
2827 /* The `operator' keyword can be the beginning of an
2828 id-expression. */
2829 case RID_OPERATOR:
2830 goto id_expression;
2832 case RID_FUNCTION_NAME:
2833 case RID_PRETTY_FUNCTION_NAME:
2834 case RID_C99_FUNCTION_NAME:
2835 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2836 __func__ are the names of variables -- but they are
2837 treated specially. Therefore, they are handled here,
2838 rather than relying on the generic id-expression logic
2839 below. Grammatically, these names are id-expressions.
2841 Consume the token. */
2842 token = cp_lexer_consume_token (parser->lexer);
2843 /* Look up the name. */
2844 return finish_fname (token->value);
2846 case RID_VA_ARG:
2848 tree expression;
2849 tree type;
2851 /* The `__builtin_va_arg' construct is used to handle
2852 `va_arg'. Consume the `__builtin_va_arg' token. */
2853 cp_lexer_consume_token (parser->lexer);
2854 /* Look for the opening `('. */
2855 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2856 /* Now, parse the assignment-expression. */
2857 expression = cp_parser_assignment_expression (parser);
2858 /* Look for the `,'. */
2859 cp_parser_require (parser, CPP_COMMA, "`,'");
2860 /* Parse the type-id. */
2861 type = cp_parser_type_id (parser);
2862 /* Look for the closing `)'. */
2863 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2864 /* Using `va_arg' in a constant-expression is not
2865 allowed. */
2866 if (cp_parser_non_integral_constant_expression (parser,
2867 "`va_arg'"))
2868 return error_mark_node;
2869 return build_x_va_arg (expression, type);
2872 case RID_OFFSETOF:
2873 return cp_parser_builtin_offsetof (parser);
2875 default:
2876 cp_parser_error (parser, "expected primary-expression");
2877 return error_mark_node;
2880 /* An id-expression can start with either an identifier, a
2881 `::' as the beginning of a qualified-id, or the "operator"
2882 keyword. */
2883 case CPP_NAME:
2884 case CPP_SCOPE:
2885 case CPP_TEMPLATE_ID:
2886 case CPP_NESTED_NAME_SPECIFIER:
2888 tree id_expression;
2889 tree decl;
2890 const char *error_msg;
2892 id_expression:
2893 /* Parse the id-expression. */
2894 id_expression
2895 = cp_parser_id_expression (parser,
2896 /*template_keyword_p=*/false,
2897 /*check_dependency_p=*/true,
2898 /*template_p=*/NULL,
2899 /*declarator_p=*/false);
2900 if (id_expression == error_mark_node)
2901 return error_mark_node;
2902 /* If we have a template-id, then no further lookup is
2903 required. If the template-id was for a template-class, we
2904 will sometimes have a TYPE_DECL at this point. */
2905 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2906 || TREE_CODE (id_expression) == TYPE_DECL)
2907 decl = id_expression;
2908 /* Look up the name. */
2909 else
2911 bool ambiguous_p;
2913 decl = cp_parser_lookup_name (parser, id_expression,
2914 /*is_type=*/false,
2915 /*is_template=*/false,
2916 /*is_namespace=*/false,
2917 /*check_dependency=*/true,
2918 &ambiguous_p);
2919 /* If the lookup was ambiguous, an error will already have
2920 been issued. */
2921 if (ambiguous_p)
2922 return error_mark_node;
2923 /* If name lookup gives us a SCOPE_REF, then the
2924 qualifying scope was dependent. Just propagate the
2925 name. */
2926 if (TREE_CODE (decl) == SCOPE_REF)
2928 if (TYPE_P (TREE_OPERAND (decl, 0)))
2929 *qualifying_class = TREE_OPERAND (decl, 0);
2930 return decl;
2932 /* Check to see if DECL is a local variable in a context
2933 where that is forbidden. */
2934 if (parser->local_variables_forbidden_p
2935 && local_variable_p (decl))
2937 /* It might be that we only found DECL because we are
2938 trying to be generous with pre-ISO scoping rules.
2939 For example, consider:
2941 int i;
2942 void g() {
2943 for (int i = 0; i < 10; ++i) {}
2944 extern void f(int j = i);
2947 Here, name look up will originally find the out
2948 of scope `i'. We need to issue a warning message,
2949 but then use the global `i'. */
2950 decl = check_for_out_of_scope_variable (decl);
2951 if (local_variable_p (decl))
2953 error ("local variable %qD may not appear in this context",
2954 decl);
2955 return error_mark_node;
2960 decl = finish_id_expression (id_expression, decl, parser->scope,
2961 idk, qualifying_class,
2962 parser->integral_constant_expression_p,
2963 parser->allow_non_integral_constant_expression_p,
2964 &parser->non_integral_constant_expression_p,
2965 &error_msg);
2966 if (error_msg)
2967 cp_parser_error (parser, error_msg);
2968 return decl;
2971 /* Anything else is an error. */
2972 default:
2973 cp_parser_error (parser, "expected primary-expression");
2974 return error_mark_node;
2978 /* Parse an id-expression.
2980 id-expression:
2981 unqualified-id
2982 qualified-id
2984 qualified-id:
2985 :: [opt] nested-name-specifier template [opt] unqualified-id
2986 :: identifier
2987 :: operator-function-id
2988 :: template-id
2990 Return a representation of the unqualified portion of the
2991 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2992 a `::' or nested-name-specifier.
2994 Often, if the id-expression was a qualified-id, the caller will
2995 want to make a SCOPE_REF to represent the qualified-id. This
2996 function does not do this in order to avoid wastefully creating
2997 SCOPE_REFs when they are not required.
2999 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3000 `template' keyword.
3002 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3003 uninstantiated templates.
3005 If *TEMPLATE_P is non-NULL, it is set to true iff the
3006 `template' keyword is used to explicitly indicate that the entity
3007 named is a template.
3009 If DECLARATOR_P is true, the id-expression is appearing as part of
3010 a declarator, rather than as part of an expression. */
3012 static tree
3013 cp_parser_id_expression (cp_parser *parser,
3014 bool template_keyword_p,
3015 bool check_dependency_p,
3016 bool *template_p,
3017 bool declarator_p)
3019 bool global_scope_p;
3020 bool nested_name_specifier_p;
3022 /* Assume the `template' keyword was not used. */
3023 if (template_p)
3024 *template_p = false;
3026 /* Look for the optional `::' operator. */
3027 global_scope_p
3028 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3029 != NULL_TREE);
3030 /* Look for the optional nested-name-specifier. */
3031 nested_name_specifier_p
3032 = (cp_parser_nested_name_specifier_opt (parser,
3033 /*typename_keyword_p=*/false,
3034 check_dependency_p,
3035 /*type_p=*/false,
3036 declarator_p)
3037 != NULL_TREE);
3038 /* If there is a nested-name-specifier, then we are looking at
3039 the first qualified-id production. */
3040 if (nested_name_specifier_p)
3042 tree saved_scope;
3043 tree saved_object_scope;
3044 tree saved_qualifying_scope;
3045 tree unqualified_id;
3046 bool is_template;
3048 /* See if the next token is the `template' keyword. */
3049 if (!template_p)
3050 template_p = &is_template;
3051 *template_p = cp_parser_optional_template_keyword (parser);
3052 /* Name lookup we do during the processing of the
3053 unqualified-id might obliterate SCOPE. */
3054 saved_scope = parser->scope;
3055 saved_object_scope = parser->object_scope;
3056 saved_qualifying_scope = parser->qualifying_scope;
3057 /* Process the final unqualified-id. */
3058 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3059 check_dependency_p,
3060 declarator_p);
3061 /* Restore the SAVED_SCOPE for our caller. */
3062 parser->scope = saved_scope;
3063 parser->object_scope = saved_object_scope;
3064 parser->qualifying_scope = saved_qualifying_scope;
3066 return unqualified_id;
3068 /* Otherwise, if we are in global scope, then we are looking at one
3069 of the other qualified-id productions. */
3070 else if (global_scope_p)
3072 cp_token *token;
3073 tree id;
3075 /* Peek at the next token. */
3076 token = cp_lexer_peek_token (parser->lexer);
3078 /* If it's an identifier, and the next token is not a "<", then
3079 we can avoid the template-id case. This is an optimization
3080 for this common case. */
3081 if (token->type == CPP_NAME
3082 && !cp_parser_nth_token_starts_template_argument_list_p
3083 (parser, 2))
3084 return cp_parser_identifier (parser);
3086 cp_parser_parse_tentatively (parser);
3087 /* Try a template-id. */
3088 id = cp_parser_template_id (parser,
3089 /*template_keyword_p=*/false,
3090 /*check_dependency_p=*/true,
3091 declarator_p);
3092 /* If that worked, we're done. */
3093 if (cp_parser_parse_definitely (parser))
3094 return id;
3096 /* Peek at the next token. (Changes in the token buffer may
3097 have invalidated the pointer obtained above.) */
3098 token = cp_lexer_peek_token (parser->lexer);
3100 switch (token->type)
3102 case CPP_NAME:
3103 return cp_parser_identifier (parser);
3105 case CPP_KEYWORD:
3106 if (token->keyword == RID_OPERATOR)
3107 return cp_parser_operator_function_id (parser);
3108 /* Fall through. */
3110 default:
3111 cp_parser_error (parser, "expected id-expression");
3112 return error_mark_node;
3115 else
3116 return cp_parser_unqualified_id (parser, template_keyword_p,
3117 /*check_dependency_p=*/true,
3118 declarator_p);
3121 /* Parse an unqualified-id.
3123 unqualified-id:
3124 identifier
3125 operator-function-id
3126 conversion-function-id
3127 ~ class-name
3128 template-id
3130 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3131 keyword, in a construct like `A::template ...'.
3133 Returns a representation of unqualified-id. For the `identifier'
3134 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3135 production a BIT_NOT_EXPR is returned; the operand of the
3136 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3137 other productions, see the documentation accompanying the
3138 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3139 names are looked up in uninstantiated templates. If DECLARATOR_P
3140 is true, the unqualified-id is appearing as part of a declarator,
3141 rather than as part of an expression. */
3143 static tree
3144 cp_parser_unqualified_id (cp_parser* parser,
3145 bool template_keyword_p,
3146 bool check_dependency_p,
3147 bool declarator_p)
3149 cp_token *token;
3151 /* Peek at the next token. */
3152 token = cp_lexer_peek_token (parser->lexer);
3154 switch (token->type)
3156 case CPP_NAME:
3158 tree id;
3160 /* We don't know yet whether or not this will be a
3161 template-id. */
3162 cp_parser_parse_tentatively (parser);
3163 /* Try a template-id. */
3164 id = cp_parser_template_id (parser, template_keyword_p,
3165 check_dependency_p,
3166 declarator_p);
3167 /* If it worked, we're done. */
3168 if (cp_parser_parse_definitely (parser))
3169 return id;
3170 /* Otherwise, it's an ordinary identifier. */
3171 return cp_parser_identifier (parser);
3174 case CPP_TEMPLATE_ID:
3175 return cp_parser_template_id (parser, template_keyword_p,
3176 check_dependency_p,
3177 declarator_p);
3179 case CPP_COMPL:
3181 tree type_decl;
3182 tree qualifying_scope;
3183 tree object_scope;
3184 tree scope;
3186 /* Consume the `~' token. */
3187 cp_lexer_consume_token (parser->lexer);
3188 /* Parse the class-name. The standard, as written, seems to
3189 say that:
3191 template <typename T> struct S { ~S (); };
3192 template <typename T> S<T>::~S() {}
3194 is invalid, since `~' must be followed by a class-name, but
3195 `S<T>' is dependent, and so not known to be a class.
3196 That's not right; we need to look in uninstantiated
3197 templates. A further complication arises from:
3199 template <typename T> void f(T t) {
3200 t.T::~T();
3203 Here, it is not possible to look up `T' in the scope of `T'
3204 itself. We must look in both the current scope, and the
3205 scope of the containing complete expression.
3207 Yet another issue is:
3209 struct S {
3210 int S;
3211 ~S();
3214 S::~S() {}
3216 The standard does not seem to say that the `S' in `~S'
3217 should refer to the type `S' and not the data member
3218 `S::S'. */
3220 /* DR 244 says that we look up the name after the "~" in the
3221 same scope as we looked up the qualifying name. That idea
3222 isn't fully worked out; it's more complicated than that. */
3223 scope = parser->scope;
3224 object_scope = parser->object_scope;
3225 qualifying_scope = parser->qualifying_scope;
3227 /* If the name is of the form "X::~X" it's OK. */
3228 if (scope && TYPE_P (scope)
3229 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3230 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3231 == CPP_OPEN_PAREN)
3232 && (cp_lexer_peek_token (parser->lexer)->value
3233 == TYPE_IDENTIFIER (scope)))
3235 cp_lexer_consume_token (parser->lexer);
3236 return build_nt (BIT_NOT_EXPR, scope);
3239 /* If there was an explicit qualification (S::~T), first look
3240 in the scope given by the qualification (i.e., S). */
3241 if (scope)
3243 cp_parser_parse_tentatively (parser);
3244 type_decl = cp_parser_class_name (parser,
3245 /*typename_keyword_p=*/false,
3246 /*template_keyword_p=*/false,
3247 /*type_p=*/false,
3248 /*check_dependency=*/false,
3249 /*class_head_p=*/false,
3250 declarator_p);
3251 if (cp_parser_parse_definitely (parser))
3252 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3254 /* In "N::S::~S", look in "N" as well. */
3255 if (scope && qualifying_scope)
3257 cp_parser_parse_tentatively (parser);
3258 parser->scope = qualifying_scope;
3259 parser->object_scope = NULL_TREE;
3260 parser->qualifying_scope = NULL_TREE;
3261 type_decl
3262 = cp_parser_class_name (parser,
3263 /*typename_keyword_p=*/false,
3264 /*template_keyword_p=*/false,
3265 /*type_p=*/false,
3266 /*check_dependency=*/false,
3267 /*class_head_p=*/false,
3268 declarator_p);
3269 if (cp_parser_parse_definitely (parser))
3270 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3272 /* In "p->S::~T", look in the scope given by "*p" as well. */
3273 else if (object_scope)
3275 cp_parser_parse_tentatively (parser);
3276 parser->scope = object_scope;
3277 parser->object_scope = NULL_TREE;
3278 parser->qualifying_scope = NULL_TREE;
3279 type_decl
3280 = cp_parser_class_name (parser,
3281 /*typename_keyword_p=*/false,
3282 /*template_keyword_p=*/false,
3283 /*type_p=*/false,
3284 /*check_dependency=*/false,
3285 /*class_head_p=*/false,
3286 declarator_p);
3287 if (cp_parser_parse_definitely (parser))
3288 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3290 /* Look in the surrounding context. */
3291 parser->scope = NULL_TREE;
3292 parser->object_scope = NULL_TREE;
3293 parser->qualifying_scope = NULL_TREE;
3294 type_decl
3295 = cp_parser_class_name (parser,
3296 /*typename_keyword_p=*/false,
3297 /*template_keyword_p=*/false,
3298 /*type_p=*/false,
3299 /*check_dependency=*/false,
3300 /*class_head_p=*/false,
3301 declarator_p);
3302 /* If an error occurred, assume that the name of the
3303 destructor is the same as the name of the qualifying
3304 class. That allows us to keep parsing after running
3305 into ill-formed destructor names. */
3306 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3307 return build_nt (BIT_NOT_EXPR, scope);
3308 else if (type_decl == error_mark_node)
3309 return error_mark_node;
3311 /* [class.dtor]
3313 A typedef-name that names a class shall not be used as the
3314 identifier in the declarator for a destructor declaration. */
3315 if (declarator_p
3316 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3317 && !DECL_SELF_REFERENCE_P (type_decl))
3318 error ("typedef-name %qD used as destructor declarator",
3319 type_decl);
3321 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3324 case CPP_KEYWORD:
3325 if (token->keyword == RID_OPERATOR)
3327 tree id;
3329 /* This could be a template-id, so we try that first. */
3330 cp_parser_parse_tentatively (parser);
3331 /* Try a template-id. */
3332 id = cp_parser_template_id (parser, template_keyword_p,
3333 /*check_dependency_p=*/true,
3334 declarator_p);
3335 /* If that worked, we're done. */
3336 if (cp_parser_parse_definitely (parser))
3337 return id;
3338 /* We still don't know whether we're looking at an
3339 operator-function-id or a conversion-function-id. */
3340 cp_parser_parse_tentatively (parser);
3341 /* Try an operator-function-id. */
3342 id = cp_parser_operator_function_id (parser);
3343 /* If that didn't work, try a conversion-function-id. */
3344 if (!cp_parser_parse_definitely (parser))
3345 id = cp_parser_conversion_function_id (parser);
3347 return id;
3349 /* Fall through. */
3351 default:
3352 cp_parser_error (parser, "expected unqualified-id");
3353 return error_mark_node;
3357 /* Parse an (optional) nested-name-specifier.
3359 nested-name-specifier:
3360 class-or-namespace-name :: nested-name-specifier [opt]
3361 class-or-namespace-name :: template nested-name-specifier [opt]
3363 PARSER->SCOPE should be set appropriately before this function is
3364 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3365 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3366 in name lookups.
3368 Sets PARSER->SCOPE to the class (TYPE) or namespace
3369 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3370 it unchanged if there is no nested-name-specifier. Returns the new
3371 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3373 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3374 part of a declaration and/or decl-specifier. */
3376 static tree
3377 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3378 bool typename_keyword_p,
3379 bool check_dependency_p,
3380 bool type_p,
3381 bool is_declaration)
3383 bool success = false;
3384 tree access_check = NULL_TREE;
3385 ptrdiff_t start;
3386 cp_token* token;
3388 /* If the next token corresponds to a nested name specifier, there
3389 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3390 false, it may have been true before, in which case something
3391 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3392 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3393 CHECK_DEPENDENCY_P is false, we have to fall through into the
3394 main loop. */
3395 if (check_dependency_p
3396 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3398 cp_parser_pre_parsed_nested_name_specifier (parser);
3399 return parser->scope;
3402 /* Remember where the nested-name-specifier starts. */
3403 if (cp_parser_parsing_tentatively (parser)
3404 && !cp_parser_committed_to_tentative_parse (parser))
3406 token = cp_lexer_peek_token (parser->lexer);
3407 start = cp_lexer_token_difference (parser->lexer,
3408 parser->lexer->buffer,
3409 token);
3411 else
3412 start = -1;
3414 push_deferring_access_checks (dk_deferred);
3416 while (true)
3418 tree new_scope;
3419 tree old_scope;
3420 tree saved_qualifying_scope;
3421 bool template_keyword_p;
3423 /* Spot cases that cannot be the beginning of a
3424 nested-name-specifier. */
3425 token = cp_lexer_peek_token (parser->lexer);
3427 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3428 the already parsed nested-name-specifier. */
3429 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3431 /* Grab the nested-name-specifier and continue the loop. */
3432 cp_parser_pre_parsed_nested_name_specifier (parser);
3433 success = true;
3434 continue;
3437 /* Spot cases that cannot be the beginning of a
3438 nested-name-specifier. On the second and subsequent times
3439 through the loop, we look for the `template' keyword. */
3440 if (success && token->keyword == RID_TEMPLATE)
3442 /* A template-id can start a nested-name-specifier. */
3443 else if (token->type == CPP_TEMPLATE_ID)
3445 else
3447 /* If the next token is not an identifier, then it is
3448 definitely not a class-or-namespace-name. */
3449 if (token->type != CPP_NAME)
3450 break;
3451 /* If the following token is neither a `<' (to begin a
3452 template-id), nor a `::', then we are not looking at a
3453 nested-name-specifier. */
3454 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3455 if (token->type != CPP_SCOPE
3456 && !cp_parser_nth_token_starts_template_argument_list_p
3457 (parser, 2))
3458 break;
3461 /* The nested-name-specifier is optional, so we parse
3462 tentatively. */
3463 cp_parser_parse_tentatively (parser);
3465 /* Look for the optional `template' keyword, if this isn't the
3466 first time through the loop. */
3467 if (success)
3468 template_keyword_p = cp_parser_optional_template_keyword (parser);
3469 else
3470 template_keyword_p = false;
3472 /* Save the old scope since the name lookup we are about to do
3473 might destroy it. */
3474 old_scope = parser->scope;
3475 saved_qualifying_scope = parser->qualifying_scope;
3476 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3477 look up names in "X<T>::I" in order to determine that "Y" is
3478 a template. So, if we have a typename at this point, we make
3479 an effort to look through it. */
3480 if (is_declaration
3481 && !typename_keyword_p
3482 && parser->scope
3483 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3484 parser->scope = resolve_typename_type (parser->scope,
3485 /*only_current_p=*/false);
3486 /* Parse the qualifying entity. */
3487 new_scope
3488 = cp_parser_class_or_namespace_name (parser,
3489 typename_keyword_p,
3490 template_keyword_p,
3491 check_dependency_p,
3492 type_p,
3493 is_declaration);
3494 /* Look for the `::' token. */
3495 cp_parser_require (parser, CPP_SCOPE, "`::'");
3497 /* If we found what we wanted, we keep going; otherwise, we're
3498 done. */
3499 if (!cp_parser_parse_definitely (parser))
3501 bool error_p = false;
3503 /* Restore the OLD_SCOPE since it was valid before the
3504 failed attempt at finding the last
3505 class-or-namespace-name. */
3506 parser->scope = old_scope;
3507 parser->qualifying_scope = saved_qualifying_scope;
3508 /* If the next token is an identifier, and the one after
3509 that is a `::', then any valid interpretation would have
3510 found a class-or-namespace-name. */
3511 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3512 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3513 == CPP_SCOPE)
3514 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3515 != CPP_COMPL))
3517 token = cp_lexer_consume_token (parser->lexer);
3518 if (!error_p)
3520 tree decl;
3522 decl = cp_parser_lookup_name_simple (parser, token->value);
3523 if (TREE_CODE (decl) == TEMPLATE_DECL)
3524 error ("%qD used without template parameters", decl);
3525 else
3526 cp_parser_name_lookup_error
3527 (parser, token->value, decl,
3528 "is not a class or namespace");
3529 parser->scope = NULL_TREE;
3530 error_p = true;
3531 /* Treat this as a successful nested-name-specifier
3532 due to:
3534 [basic.lookup.qual]
3536 If the name found is not a class-name (clause
3537 _class_) or namespace-name (_namespace.def_), the
3538 program is ill-formed. */
3539 success = true;
3541 cp_lexer_consume_token (parser->lexer);
3543 break;
3546 /* We've found one valid nested-name-specifier. */
3547 success = true;
3548 /* Make sure we look in the right scope the next time through
3549 the loop. */
3550 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3551 ? TREE_TYPE (new_scope)
3552 : new_scope);
3553 /* If it is a class scope, try to complete it; we are about to
3554 be looking up names inside the class. */
3555 if (TYPE_P (parser->scope)
3556 /* Since checking types for dependency can be expensive,
3557 avoid doing it if the type is already complete. */
3558 && !COMPLETE_TYPE_P (parser->scope)
3559 /* Do not try to complete dependent types. */
3560 && !dependent_type_p (parser->scope))
3561 complete_type (parser->scope);
3564 /* Retrieve any deferred checks. Do not pop this access checks yet
3565 so the memory will not be reclaimed during token replacing below. */
3566 access_check = get_deferred_access_checks ();
3568 /* If parsing tentatively, replace the sequence of tokens that makes
3569 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3570 token. That way, should we re-parse the token stream, we will
3571 not have to repeat the effort required to do the parse, nor will
3572 we issue duplicate error messages. */
3573 if (success && start >= 0)
3575 /* Find the token that corresponds to the start of the
3576 template-id. */
3577 token = cp_lexer_advance_token (parser->lexer,
3578 parser->lexer->buffer,
3579 start);
3581 /* Reset the contents of the START token. */
3582 token->type = CPP_NESTED_NAME_SPECIFIER;
3583 token->value = build_tree_list (access_check, parser->scope);
3584 TREE_TYPE (token->value) = parser->qualifying_scope;
3585 token->keyword = RID_MAX;
3586 /* Purge all subsequent tokens. */
3587 cp_lexer_purge_tokens_after (parser->lexer, token);
3590 pop_deferring_access_checks ();
3591 return success ? parser->scope : NULL_TREE;
3594 /* Parse a nested-name-specifier. See
3595 cp_parser_nested_name_specifier_opt for details. This function
3596 behaves identically, except that it will an issue an error if no
3597 nested-name-specifier is present, and it will return
3598 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3599 is present. */
3601 static tree
3602 cp_parser_nested_name_specifier (cp_parser *parser,
3603 bool typename_keyword_p,
3604 bool check_dependency_p,
3605 bool type_p,
3606 bool is_declaration)
3608 tree scope;
3610 /* Look for the nested-name-specifier. */
3611 scope = cp_parser_nested_name_specifier_opt (parser,
3612 typename_keyword_p,
3613 check_dependency_p,
3614 type_p,
3615 is_declaration);
3616 /* If it was not present, issue an error message. */
3617 if (!scope)
3619 cp_parser_error (parser, "expected nested-name-specifier");
3620 parser->scope = NULL_TREE;
3621 return error_mark_node;
3624 return scope;
3627 /* Parse a class-or-namespace-name.
3629 class-or-namespace-name:
3630 class-name
3631 namespace-name
3633 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3634 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3635 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3636 TYPE_P is TRUE iff the next name should be taken as a class-name,
3637 even the same name is declared to be another entity in the same
3638 scope.
3640 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3641 specified by the class-or-namespace-name. If neither is found the
3642 ERROR_MARK_NODE is returned. */
3644 static tree
3645 cp_parser_class_or_namespace_name (cp_parser *parser,
3646 bool typename_keyword_p,
3647 bool template_keyword_p,
3648 bool check_dependency_p,
3649 bool type_p,
3650 bool is_declaration)
3652 tree saved_scope;
3653 tree saved_qualifying_scope;
3654 tree saved_object_scope;
3655 tree scope;
3656 bool only_class_p;
3658 /* Before we try to parse the class-name, we must save away the
3659 current PARSER->SCOPE since cp_parser_class_name will destroy
3660 it. */
3661 saved_scope = parser->scope;
3662 saved_qualifying_scope = parser->qualifying_scope;
3663 saved_object_scope = parser->object_scope;
3664 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3665 there is no need to look for a namespace-name. */
3666 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3667 if (!only_class_p)
3668 cp_parser_parse_tentatively (parser);
3669 scope = cp_parser_class_name (parser,
3670 typename_keyword_p,
3671 template_keyword_p,
3672 type_p,
3673 check_dependency_p,
3674 /*class_head_p=*/false,
3675 is_declaration);
3676 /* If that didn't work, try for a namespace-name. */
3677 if (!only_class_p && !cp_parser_parse_definitely (parser))
3679 /* Restore the saved scope. */
3680 parser->scope = saved_scope;
3681 parser->qualifying_scope = saved_qualifying_scope;
3682 parser->object_scope = saved_object_scope;
3683 /* If we are not looking at an identifier followed by the scope
3684 resolution operator, then this is not part of a
3685 nested-name-specifier. (Note that this function is only used
3686 to parse the components of a nested-name-specifier.) */
3687 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3688 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3689 return error_mark_node;
3690 scope = cp_parser_namespace_name (parser);
3693 return scope;
3696 /* Parse a postfix-expression.
3698 postfix-expression:
3699 primary-expression
3700 postfix-expression [ expression ]
3701 postfix-expression ( expression-list [opt] )
3702 simple-type-specifier ( expression-list [opt] )
3703 typename :: [opt] nested-name-specifier identifier
3704 ( expression-list [opt] )
3705 typename :: [opt] nested-name-specifier template [opt] template-id
3706 ( expression-list [opt] )
3707 postfix-expression . template [opt] id-expression
3708 postfix-expression -> template [opt] id-expression
3709 postfix-expression . pseudo-destructor-name
3710 postfix-expression -> pseudo-destructor-name
3711 postfix-expression ++
3712 postfix-expression --
3713 dynamic_cast < type-id > ( expression )
3714 static_cast < type-id > ( expression )
3715 reinterpret_cast < type-id > ( expression )
3716 const_cast < type-id > ( expression )
3717 typeid ( expression )
3718 typeid ( type-id )
3720 GNU Extension:
3722 postfix-expression:
3723 ( type-id ) { initializer-list , [opt] }
3725 This extension is a GNU version of the C99 compound-literal
3726 construct. (The C99 grammar uses `type-name' instead of `type-id',
3727 but they are essentially the same concept.)
3729 If ADDRESS_P is true, the postfix expression is the operand of the
3730 `&' operator.
3732 Returns a representation of the expression. */
3734 static tree
3735 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3737 cp_token *token;
3738 enum rid keyword;
3739 cp_id_kind idk = CP_ID_KIND_NONE;
3740 tree postfix_expression = NULL_TREE;
3741 /* Non-NULL only if the current postfix-expression can be used to
3742 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3743 class used to qualify the member. */
3744 tree qualifying_class = NULL_TREE;
3746 /* Peek at the next token. */
3747 token = cp_lexer_peek_token (parser->lexer);
3748 /* Some of the productions are determined by keywords. */
3749 keyword = token->keyword;
3750 switch (keyword)
3752 case RID_DYNCAST:
3753 case RID_STATCAST:
3754 case RID_REINTCAST:
3755 case RID_CONSTCAST:
3757 tree type;
3758 tree expression;
3759 const char *saved_message;
3761 /* All of these can be handled in the same way from the point
3762 of view of parsing. Begin by consuming the token
3763 identifying the cast. */
3764 cp_lexer_consume_token (parser->lexer);
3766 /* New types cannot be defined in the cast. */
3767 saved_message = parser->type_definition_forbidden_message;
3768 parser->type_definition_forbidden_message
3769 = "types may not be defined in casts";
3771 /* Look for the opening `<'. */
3772 cp_parser_require (parser, CPP_LESS, "`<'");
3773 /* Parse the type to which we are casting. */
3774 type = cp_parser_type_id (parser);
3775 /* Look for the closing `>'. */
3776 cp_parser_require (parser, CPP_GREATER, "`>'");
3777 /* Restore the old message. */
3778 parser->type_definition_forbidden_message = saved_message;
3780 /* And the expression which is being cast. */
3781 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3782 expression = cp_parser_expression (parser);
3783 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3785 /* Only type conversions to integral or enumeration types
3786 can be used in constant-expressions. */
3787 if (parser->integral_constant_expression_p
3788 && !dependent_type_p (type)
3789 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3790 && (cp_parser_non_integral_constant_expression
3791 (parser,
3792 "a cast to a type other than an integral or "
3793 "enumeration type")))
3794 return error_mark_node;
3796 switch (keyword)
3798 case RID_DYNCAST:
3799 postfix_expression
3800 = build_dynamic_cast (type, expression);
3801 break;
3802 case RID_STATCAST:
3803 postfix_expression
3804 = build_static_cast (type, expression);
3805 break;
3806 case RID_REINTCAST:
3807 postfix_expression
3808 = build_reinterpret_cast (type, expression);
3809 break;
3810 case RID_CONSTCAST:
3811 postfix_expression
3812 = build_const_cast (type, expression);
3813 break;
3814 default:
3815 gcc_unreachable ();
3818 break;
3820 case RID_TYPEID:
3822 tree type;
3823 const char *saved_message;
3824 bool saved_in_type_id_in_expr_p;
3826 /* Consume the `typeid' token. */
3827 cp_lexer_consume_token (parser->lexer);
3828 /* Look for the `(' token. */
3829 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3830 /* Types cannot be defined in a `typeid' expression. */
3831 saved_message = parser->type_definition_forbidden_message;
3832 parser->type_definition_forbidden_message
3833 = "types may not be defined in a `typeid\' expression";
3834 /* We can't be sure yet whether we're looking at a type-id or an
3835 expression. */
3836 cp_parser_parse_tentatively (parser);
3837 /* Try a type-id first. */
3838 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3839 parser->in_type_id_in_expr_p = true;
3840 type = cp_parser_type_id (parser);
3841 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3842 /* Look for the `)' token. Otherwise, we can't be sure that
3843 we're not looking at an expression: consider `typeid (int
3844 (3))', for example. */
3845 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3846 /* If all went well, simply lookup the type-id. */
3847 if (cp_parser_parse_definitely (parser))
3848 postfix_expression = get_typeid (type);
3849 /* Otherwise, fall back to the expression variant. */
3850 else
3852 tree expression;
3854 /* Look for an expression. */
3855 expression = cp_parser_expression (parser);
3856 /* Compute its typeid. */
3857 postfix_expression = build_typeid (expression);
3858 /* Look for the `)' token. */
3859 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3861 /* `typeid' may not appear in an integral constant expression. */
3862 if (cp_parser_non_integral_constant_expression(parser,
3863 "`typeid' operator"))
3864 return error_mark_node;
3865 /* Restore the saved message. */
3866 parser->type_definition_forbidden_message = saved_message;
3868 break;
3870 case RID_TYPENAME:
3872 bool template_p = false;
3873 tree id;
3874 tree type;
3876 /* Consume the `typename' token. */
3877 cp_lexer_consume_token (parser->lexer);
3878 /* Look for the optional `::' operator. */
3879 cp_parser_global_scope_opt (parser,
3880 /*current_scope_valid_p=*/false);
3881 /* Look for the nested-name-specifier. */
3882 cp_parser_nested_name_specifier (parser,
3883 /*typename_keyword_p=*/true,
3884 /*check_dependency_p=*/true,
3885 /*type_p=*/true,
3886 /*is_declaration=*/true);
3887 /* Look for the optional `template' keyword. */
3888 template_p = cp_parser_optional_template_keyword (parser);
3889 /* We don't know whether we're looking at a template-id or an
3890 identifier. */
3891 cp_parser_parse_tentatively (parser);
3892 /* Try a template-id. */
3893 id = cp_parser_template_id (parser, template_p,
3894 /*check_dependency_p=*/true,
3895 /*is_declaration=*/true);
3896 /* If that didn't work, try an identifier. */
3897 if (!cp_parser_parse_definitely (parser))
3898 id = cp_parser_identifier (parser);
3899 /* If we look up a template-id in a non-dependent qualifying
3900 scope, there's no need to create a dependent type. */
3901 if (TREE_CODE (id) == TYPE_DECL
3902 && !dependent_type_p (parser->scope))
3903 type = TREE_TYPE (id);
3904 /* Create a TYPENAME_TYPE to represent the type to which the
3905 functional cast is being performed. */
3906 else
3907 type = make_typename_type (parser->scope, id,
3908 /*complain=*/1);
3910 postfix_expression = cp_parser_functional_cast (parser, type);
3912 break;
3914 default:
3916 tree type;
3918 /* If the next thing is a simple-type-specifier, we may be
3919 looking at a functional cast. We could also be looking at
3920 an id-expression. So, we try the functional cast, and if
3921 that doesn't work we fall back to the primary-expression. */
3922 cp_parser_parse_tentatively (parser);
3923 /* Look for the simple-type-specifier. */
3924 type = cp_parser_simple_type_specifier (parser,
3925 /*decl_specs=*/NULL,
3926 CP_PARSER_FLAGS_NONE);
3927 /* Parse the cast itself. */
3928 if (!cp_parser_error_occurred (parser))
3929 postfix_expression
3930 = cp_parser_functional_cast (parser, type);
3931 /* If that worked, we're done. */
3932 if (cp_parser_parse_definitely (parser))
3933 break;
3935 /* If the functional-cast didn't work out, try a
3936 compound-literal. */
3937 if (cp_parser_allow_gnu_extensions_p (parser)
3938 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3940 tree initializer_list = NULL_TREE;
3941 bool saved_in_type_id_in_expr_p;
3943 cp_parser_parse_tentatively (parser);
3944 /* Consume the `('. */
3945 cp_lexer_consume_token (parser->lexer);
3946 /* Parse the type. */
3947 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3948 parser->in_type_id_in_expr_p = true;
3949 type = cp_parser_type_id (parser);
3950 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3951 /* Look for the `)'. */
3952 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3953 /* Look for the `{'. */
3954 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3955 /* If things aren't going well, there's no need to
3956 keep going. */
3957 if (!cp_parser_error_occurred (parser))
3959 bool non_constant_p;
3960 /* Parse the initializer-list. */
3961 initializer_list
3962 = cp_parser_initializer_list (parser, &non_constant_p);
3963 /* Allow a trailing `,'. */
3964 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3965 cp_lexer_consume_token (parser->lexer);
3966 /* Look for the final `}'. */
3967 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3969 /* If that worked, we're definitely looking at a
3970 compound-literal expression. */
3971 if (cp_parser_parse_definitely (parser))
3973 /* Warn the user that a compound literal is not
3974 allowed in standard C++. */
3975 if (pedantic)
3976 pedwarn ("ISO C++ forbids compound-literals");
3977 /* Form the representation of the compound-literal. */
3978 postfix_expression
3979 = finish_compound_literal (type, initializer_list);
3980 break;
3984 /* It must be a primary-expression. */
3985 postfix_expression = cp_parser_primary_expression (parser,
3986 &idk,
3987 &qualifying_class);
3989 break;
3992 /* If we were avoiding committing to the processing of a
3993 qualified-id until we knew whether or not we had a
3994 pointer-to-member, we now know. */
3995 if (qualifying_class)
3997 bool done;
3999 /* Peek at the next token. */
4000 token = cp_lexer_peek_token (parser->lexer);
4001 done = (token->type != CPP_OPEN_SQUARE
4002 && token->type != CPP_OPEN_PAREN
4003 && token->type != CPP_DOT
4004 && token->type != CPP_DEREF
4005 && token->type != CPP_PLUS_PLUS
4006 && token->type != CPP_MINUS_MINUS);
4008 postfix_expression = finish_qualified_id_expr (qualifying_class,
4009 postfix_expression,
4010 done,
4011 address_p);
4012 if (done)
4013 return postfix_expression;
4016 /* Keep looping until the postfix-expression is complete. */
4017 while (true)
4019 if (idk == CP_ID_KIND_UNQUALIFIED
4020 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4021 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4022 /* It is not a Koenig lookup function call. */
4023 postfix_expression
4024 = unqualified_name_lookup_error (postfix_expression);
4026 /* Peek at the next token. */
4027 token = cp_lexer_peek_token (parser->lexer);
4029 switch (token->type)
4031 case CPP_OPEN_SQUARE:
4032 postfix_expression
4033 = cp_parser_postfix_open_square_expression (parser,
4034 postfix_expression,
4035 false);
4036 idk = CP_ID_KIND_NONE;
4037 break;
4039 case CPP_OPEN_PAREN:
4040 /* postfix-expression ( expression-list [opt] ) */
4042 bool koenig_p;
4043 tree args = (cp_parser_parenthesized_expression_list
4044 (parser, false, /*non_constant_p=*/NULL));
4046 if (args == error_mark_node)
4048 postfix_expression = error_mark_node;
4049 break;
4052 /* Function calls are not permitted in
4053 constant-expressions. */
4054 if (cp_parser_non_integral_constant_expression (parser,
4055 "a function call"))
4057 postfix_expression = error_mark_node;
4058 break;
4061 koenig_p = false;
4062 if (idk == CP_ID_KIND_UNQUALIFIED)
4064 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4066 if (args)
4068 koenig_p = true;
4069 postfix_expression
4070 = perform_koenig_lookup (postfix_expression, args);
4072 else
4073 postfix_expression
4074 = unqualified_fn_lookup_error (postfix_expression);
4076 /* We do not perform argument-dependent lookup if
4077 normal lookup finds a non-function, in accordance
4078 with the expected resolution of DR 218. */
4079 else if (args && is_overloaded_fn (postfix_expression))
4081 tree fn = get_first_fn (postfix_expression);
4083 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4084 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4086 /* Only do argument dependent lookup if regular
4087 lookup does not find a set of member functions.
4088 [basic.lookup.koenig]/2a */
4089 if (!DECL_FUNCTION_MEMBER_P (fn))
4091 koenig_p = true;
4092 postfix_expression
4093 = perform_koenig_lookup (postfix_expression, args);
4098 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4100 tree instance = TREE_OPERAND (postfix_expression, 0);
4101 tree fn = TREE_OPERAND (postfix_expression, 1);
4103 if (processing_template_decl
4104 && (type_dependent_expression_p (instance)
4105 || (!BASELINK_P (fn)
4106 && TREE_CODE (fn) != FIELD_DECL)
4107 || type_dependent_expression_p (fn)
4108 || any_type_dependent_arguments_p (args)))
4110 postfix_expression
4111 = build_min_nt (CALL_EXPR, postfix_expression,
4112 args, NULL_TREE);
4113 break;
4116 if (BASELINK_P (fn))
4117 postfix_expression
4118 = (build_new_method_call
4119 (instance, fn, args, NULL_TREE,
4120 (idk == CP_ID_KIND_QUALIFIED
4121 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4122 else
4123 postfix_expression
4124 = finish_call_expr (postfix_expression, args,
4125 /*disallow_virtual=*/false,
4126 /*koenig_p=*/false);
4128 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4129 || TREE_CODE (postfix_expression) == MEMBER_REF
4130 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4131 postfix_expression = (build_offset_ref_call_from_tree
4132 (postfix_expression, args));
4133 else if (idk == CP_ID_KIND_QUALIFIED)
4134 /* A call to a static class member, or a namespace-scope
4135 function. */
4136 postfix_expression
4137 = finish_call_expr (postfix_expression, args,
4138 /*disallow_virtual=*/true,
4139 koenig_p);
4140 else
4141 /* All other function calls. */
4142 postfix_expression
4143 = finish_call_expr (postfix_expression, args,
4144 /*disallow_virtual=*/false,
4145 koenig_p);
4147 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4148 idk = CP_ID_KIND_NONE;
4150 break;
4152 case CPP_DOT:
4153 case CPP_DEREF:
4154 /* postfix-expression . template [opt] id-expression
4155 postfix-expression . pseudo-destructor-name
4156 postfix-expression -> template [opt] id-expression
4157 postfix-expression -> pseudo-destructor-name */
4159 /* Consume the `.' or `->' operator. */
4160 cp_lexer_consume_token (parser->lexer);
4162 postfix_expression
4163 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4164 postfix_expression,
4165 false, &idk);
4166 break;
4168 case CPP_PLUS_PLUS:
4169 /* postfix-expression ++ */
4170 /* Consume the `++' token. */
4171 cp_lexer_consume_token (parser->lexer);
4172 /* Generate a representation for the complete expression. */
4173 postfix_expression
4174 = finish_increment_expr (postfix_expression,
4175 POSTINCREMENT_EXPR);
4176 /* Increments may not appear in constant-expressions. */
4177 if (cp_parser_non_integral_constant_expression (parser,
4178 "an increment"))
4179 postfix_expression = error_mark_node;
4180 idk = CP_ID_KIND_NONE;
4181 break;
4183 case CPP_MINUS_MINUS:
4184 /* postfix-expression -- */
4185 /* Consume the `--' token. */
4186 cp_lexer_consume_token (parser->lexer);
4187 /* Generate a representation for the complete expression. */
4188 postfix_expression
4189 = finish_increment_expr (postfix_expression,
4190 POSTDECREMENT_EXPR);
4191 /* Decrements may not appear in constant-expressions. */
4192 if (cp_parser_non_integral_constant_expression (parser,
4193 "a decrement"))
4194 postfix_expression = error_mark_node;
4195 idk = CP_ID_KIND_NONE;
4196 break;
4198 default:
4199 return postfix_expression;
4203 /* We should never get here. */
4204 gcc_unreachable ();
4205 return error_mark_node;
4208 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4209 by cp_parser_builtin_offsetof. We're looking for
4211 postfix-expression [ expression ]
4213 FOR_OFFSETOF is set if we're being called in that context, which
4214 changes how we deal with integer constant expressions. */
4216 static tree
4217 cp_parser_postfix_open_square_expression (cp_parser *parser,
4218 tree postfix_expression,
4219 bool for_offsetof)
4221 tree index;
4223 /* Consume the `[' token. */
4224 cp_lexer_consume_token (parser->lexer);
4226 /* Parse the index expression. */
4227 /* ??? For offsetof, there is a question of what to allow here. If
4228 offsetof is not being used in an integral constant expression context,
4229 then we *could* get the right answer by computing the value at runtime.
4230 If we are in an integral constant expression context, then we might
4231 could accept any constant expression; hard to say without analysis.
4232 Rather than open the barn door too wide right away, allow only integer
4233 constant expressions here. */
4234 if (for_offsetof)
4235 index = cp_parser_constant_expression (parser, false, NULL);
4236 else
4237 index = cp_parser_expression (parser);
4239 /* Look for the closing `]'. */
4240 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4242 /* Build the ARRAY_REF. */
4243 postfix_expression = grok_array_decl (postfix_expression, index);
4245 /* When not doing offsetof, array references are not permitted in
4246 constant-expressions. */
4247 if (!for_offsetof
4248 && (cp_parser_non_integral_constant_expression
4249 (parser, "an array reference")))
4250 postfix_expression = error_mark_node;
4252 return postfix_expression;
4255 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4256 by cp_parser_builtin_offsetof. We're looking for
4258 postfix-expression . template [opt] id-expression
4259 postfix-expression . pseudo-destructor-name
4260 postfix-expression -> template [opt] id-expression
4261 postfix-expression -> pseudo-destructor-name
4263 FOR_OFFSETOF is set if we're being called in that context. That sorta
4264 limits what of the above we'll actually accept, but nevermind.
4265 TOKEN_TYPE is the "." or "->" token, which will already have been
4266 removed from the stream. */
4268 static tree
4269 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4270 enum cpp_ttype token_type,
4271 tree postfix_expression,
4272 bool for_offsetof, cp_id_kind *idk)
4274 tree name;
4275 bool dependent_p;
4276 bool template_p;
4277 tree scope = NULL_TREE;
4279 /* If this is a `->' operator, dereference the pointer. */
4280 if (token_type == CPP_DEREF)
4281 postfix_expression = build_x_arrow (postfix_expression);
4282 /* Check to see whether or not the expression is type-dependent. */
4283 dependent_p = type_dependent_expression_p (postfix_expression);
4284 /* The identifier following the `->' or `.' is not qualified. */
4285 parser->scope = NULL_TREE;
4286 parser->qualifying_scope = NULL_TREE;
4287 parser->object_scope = NULL_TREE;
4288 *idk = CP_ID_KIND_NONE;
4289 /* Enter the scope corresponding to the type of the object
4290 given by the POSTFIX_EXPRESSION. */
4291 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4293 scope = TREE_TYPE (postfix_expression);
4294 /* According to the standard, no expression should ever have
4295 reference type. Unfortunately, we do not currently match
4296 the standard in this respect in that our internal representation
4297 of an expression may have reference type even when the standard
4298 says it does not. Therefore, we have to manually obtain the
4299 underlying type here. */
4300 scope = non_reference (scope);
4301 /* The type of the POSTFIX_EXPRESSION must be complete. */
4302 scope = complete_type_or_else (scope, NULL_TREE);
4303 /* Let the name lookup machinery know that we are processing a
4304 class member access expression. */
4305 parser->context->object_type = scope;
4306 /* If something went wrong, we want to be able to discern that case,
4307 as opposed to the case where there was no SCOPE due to the type
4308 of expression being dependent. */
4309 if (!scope)
4310 scope = error_mark_node;
4311 /* If the SCOPE was erroneous, make the various semantic analysis
4312 functions exit quickly -- and without issuing additional error
4313 messages. */
4314 if (scope == error_mark_node)
4315 postfix_expression = error_mark_node;
4318 /* If the SCOPE is not a scalar type, we are looking at an
4319 ordinary class member access expression, rather than a
4320 pseudo-destructor-name. */
4321 if (!scope || !SCALAR_TYPE_P (scope))
4323 template_p = cp_parser_optional_template_keyword (parser);
4324 /* Parse the id-expression. */
4325 name = cp_parser_id_expression (parser, template_p,
4326 /*check_dependency_p=*/true,
4327 /*template_p=*/NULL,
4328 /*declarator_p=*/false);
4329 /* In general, build a SCOPE_REF if the member name is qualified.
4330 However, if the name was not dependent and has already been
4331 resolved; there is no need to build the SCOPE_REF. For example;
4333 struct X { void f(); };
4334 template <typename T> void f(T* t) { t->X::f(); }
4336 Even though "t" is dependent, "X::f" is not and has been resolved
4337 to a BASELINK; there is no need to include scope information. */
4339 /* But we do need to remember that there was an explicit scope for
4340 virtual function calls. */
4341 if (parser->scope)
4342 *idk = CP_ID_KIND_QUALIFIED;
4344 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4346 name = build_nt (SCOPE_REF, parser->scope, name);
4347 parser->scope = NULL_TREE;
4348 parser->qualifying_scope = NULL_TREE;
4349 parser->object_scope = NULL_TREE;
4351 if (scope && name && BASELINK_P (name))
4352 adjust_result_of_qualified_name_lookup
4353 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4354 postfix_expression
4355 = finish_class_member_access_expr (postfix_expression, name);
4357 /* Otherwise, try the pseudo-destructor-name production. */
4358 else
4360 tree s = NULL_TREE;
4361 tree type;
4363 /* Parse the pseudo-destructor-name. */
4364 cp_parser_pseudo_destructor_name (parser, &s, &type);
4365 /* Form the call. */
4366 postfix_expression
4367 = finish_pseudo_destructor_expr (postfix_expression,
4368 s, TREE_TYPE (type));
4371 /* We no longer need to look up names in the scope of the object on
4372 the left-hand side of the `.' or `->' operator. */
4373 parser->context->object_type = NULL_TREE;
4375 /* Outside of offsetof, these operators may not appear in
4376 constant-expressions. */
4377 if (!for_offsetof
4378 && (cp_parser_non_integral_constant_expression
4379 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4380 postfix_expression = error_mark_node;
4382 return postfix_expression;
4385 /* Parse a parenthesized expression-list.
4387 expression-list:
4388 assignment-expression
4389 expression-list, assignment-expression
4391 attribute-list:
4392 expression-list
4393 identifier
4394 identifier, expression-list
4396 Returns a TREE_LIST. The TREE_VALUE of each node is a
4397 representation of an assignment-expression. Note that a TREE_LIST
4398 is returned even if there is only a single expression in the list.
4399 error_mark_node is returned if the ( and or ) are
4400 missing. NULL_TREE is returned on no expressions. The parentheses
4401 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4402 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4403 indicates whether or not all of the expressions in the list were
4404 constant. */
4406 static tree
4407 cp_parser_parenthesized_expression_list (cp_parser* parser,
4408 bool is_attribute_list,
4409 bool *non_constant_p)
4411 tree expression_list = NULL_TREE;
4412 tree identifier = NULL_TREE;
4414 /* Assume all the expressions will be constant. */
4415 if (non_constant_p)
4416 *non_constant_p = false;
4418 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4419 return error_mark_node;
4421 /* Consume expressions until there are no more. */
4422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4423 while (true)
4425 tree expr;
4427 /* At the beginning of attribute lists, check to see if the
4428 next token is an identifier. */
4429 if (is_attribute_list
4430 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4432 cp_token *token;
4434 /* Consume the identifier. */
4435 token = cp_lexer_consume_token (parser->lexer);
4436 /* Save the identifier. */
4437 identifier = token->value;
4439 else
4441 /* Parse the next assignment-expression. */
4442 if (non_constant_p)
4444 bool expr_non_constant_p;
4445 expr = (cp_parser_constant_expression
4446 (parser, /*allow_non_constant_p=*/true,
4447 &expr_non_constant_p));
4448 if (expr_non_constant_p)
4449 *non_constant_p = true;
4451 else
4452 expr = cp_parser_assignment_expression (parser);
4454 /* Add it to the list. We add error_mark_node
4455 expressions to the list, so that we can still tell if
4456 the correct form for a parenthesized expression-list
4457 is found. That gives better errors. */
4458 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4460 if (expr == error_mark_node)
4461 goto skip_comma;
4464 /* After the first item, attribute lists look the same as
4465 expression lists. */
4466 is_attribute_list = false;
4468 get_comma:;
4469 /* If the next token isn't a `,', then we are done. */
4470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4471 break;
4473 /* Otherwise, consume the `,' and keep going. */
4474 cp_lexer_consume_token (parser->lexer);
4477 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4479 int ending;
4481 skip_comma:;
4482 /* We try and resync to an unnested comma, as that will give the
4483 user better diagnostics. */
4484 ending = cp_parser_skip_to_closing_parenthesis (parser,
4485 /*recovering=*/true,
4486 /*or_comma=*/true,
4487 /*consume_paren=*/true);
4488 if (ending < 0)
4489 goto get_comma;
4490 if (!ending)
4491 return error_mark_node;
4494 /* We built up the list in reverse order so we must reverse it now. */
4495 expression_list = nreverse (expression_list);
4496 if (identifier)
4497 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4499 return expression_list;
4502 /* Parse a pseudo-destructor-name.
4504 pseudo-destructor-name:
4505 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4506 :: [opt] nested-name-specifier template template-id :: ~ type-name
4507 :: [opt] nested-name-specifier [opt] ~ type-name
4509 If either of the first two productions is used, sets *SCOPE to the
4510 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4511 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4512 or ERROR_MARK_NODE if the parse fails. */
4514 static void
4515 cp_parser_pseudo_destructor_name (cp_parser* parser,
4516 tree* scope,
4517 tree* type)
4519 bool nested_name_specifier_p;
4521 /* Assume that things will not work out. */
4522 *type = error_mark_node;
4524 /* Look for the optional `::' operator. */
4525 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4526 /* Look for the optional nested-name-specifier. */
4527 nested_name_specifier_p
4528 = (cp_parser_nested_name_specifier_opt (parser,
4529 /*typename_keyword_p=*/false,
4530 /*check_dependency_p=*/true,
4531 /*type_p=*/false,
4532 /*is_declaration=*/true)
4533 != NULL_TREE);
4534 /* Now, if we saw a nested-name-specifier, we might be doing the
4535 second production. */
4536 if (nested_name_specifier_p
4537 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4539 /* Consume the `template' keyword. */
4540 cp_lexer_consume_token (parser->lexer);
4541 /* Parse the template-id. */
4542 cp_parser_template_id (parser,
4543 /*template_keyword_p=*/true,
4544 /*check_dependency_p=*/false,
4545 /*is_declaration=*/true);
4546 /* Look for the `::' token. */
4547 cp_parser_require (parser, CPP_SCOPE, "`::'");
4549 /* If the next token is not a `~', then there might be some
4550 additional qualification. */
4551 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4553 /* Look for the type-name. */
4554 *scope = TREE_TYPE (cp_parser_type_name (parser));
4556 if (*scope == error_mark_node)
4557 return;
4559 /* If we don't have ::~, then something has gone wrong. Since
4560 the only caller of this function is looking for something
4561 after `.' or `->' after a scalar type, most likely the
4562 program is trying to get a member of a non-aggregate
4563 type. */
4564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4565 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4567 cp_parser_error (parser, "request for member of non-aggregate type");
4568 return;
4571 /* Look for the `::' token. */
4572 cp_parser_require (parser, CPP_SCOPE, "`::'");
4574 else
4575 *scope = NULL_TREE;
4577 /* Look for the `~'. */
4578 cp_parser_require (parser, CPP_COMPL, "`~'");
4579 /* Look for the type-name again. We are not responsible for
4580 checking that it matches the first type-name. */
4581 *type = cp_parser_type_name (parser);
4584 /* Parse a unary-expression.
4586 unary-expression:
4587 postfix-expression
4588 ++ cast-expression
4589 -- cast-expression
4590 unary-operator cast-expression
4591 sizeof unary-expression
4592 sizeof ( type-id )
4593 new-expression
4594 delete-expression
4596 GNU Extensions:
4598 unary-expression:
4599 __extension__ cast-expression
4600 __alignof__ unary-expression
4601 __alignof__ ( type-id )
4602 __real__ cast-expression
4603 __imag__ cast-expression
4604 && identifier
4606 ADDRESS_P is true iff the unary-expression is appearing as the
4607 operand of the `&' operator.
4609 Returns a representation of the expression. */
4611 static tree
4612 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4614 cp_token *token;
4615 enum tree_code unary_operator;
4617 /* Peek at the next token. */
4618 token = cp_lexer_peek_token (parser->lexer);
4619 /* Some keywords give away the kind of expression. */
4620 if (token->type == CPP_KEYWORD)
4622 enum rid keyword = token->keyword;
4624 switch (keyword)
4626 case RID_ALIGNOF:
4627 case RID_SIZEOF:
4629 tree operand;
4630 enum tree_code op;
4632 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4633 /* Consume the token. */
4634 cp_lexer_consume_token (parser->lexer);
4635 /* Parse the operand. */
4636 operand = cp_parser_sizeof_operand (parser, keyword);
4638 if (TYPE_P (operand))
4639 return cxx_sizeof_or_alignof_type (operand, op, true);
4640 else
4641 return cxx_sizeof_or_alignof_expr (operand, op);
4644 case RID_NEW:
4645 return cp_parser_new_expression (parser);
4647 case RID_DELETE:
4648 return cp_parser_delete_expression (parser);
4650 case RID_EXTENSION:
4652 /* The saved value of the PEDANTIC flag. */
4653 int saved_pedantic;
4654 tree expr;
4656 /* Save away the PEDANTIC flag. */
4657 cp_parser_extension_opt (parser, &saved_pedantic);
4658 /* Parse the cast-expression. */
4659 expr = cp_parser_simple_cast_expression (parser);
4660 /* Restore the PEDANTIC flag. */
4661 pedantic = saved_pedantic;
4663 return expr;
4666 case RID_REALPART:
4667 case RID_IMAGPART:
4669 tree expression;
4671 /* Consume the `__real__' or `__imag__' token. */
4672 cp_lexer_consume_token (parser->lexer);
4673 /* Parse the cast-expression. */
4674 expression = cp_parser_simple_cast_expression (parser);
4675 /* Create the complete representation. */
4676 return build_x_unary_op ((keyword == RID_REALPART
4677 ? REALPART_EXPR : IMAGPART_EXPR),
4678 expression);
4680 break;
4682 default:
4683 break;
4687 /* Look for the `:: new' and `:: delete', which also signal the
4688 beginning of a new-expression, or delete-expression,
4689 respectively. If the next token is `::', then it might be one of
4690 these. */
4691 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4693 enum rid keyword;
4695 /* See if the token after the `::' is one of the keywords in
4696 which we're interested. */
4697 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4698 /* If it's `new', we have a new-expression. */
4699 if (keyword == RID_NEW)
4700 return cp_parser_new_expression (parser);
4701 /* Similarly, for `delete'. */
4702 else if (keyword == RID_DELETE)
4703 return cp_parser_delete_expression (parser);
4706 /* Look for a unary operator. */
4707 unary_operator = cp_parser_unary_operator (token);
4708 /* The `++' and `--' operators can be handled similarly, even though
4709 they are not technically unary-operators in the grammar. */
4710 if (unary_operator == ERROR_MARK)
4712 if (token->type == CPP_PLUS_PLUS)
4713 unary_operator = PREINCREMENT_EXPR;
4714 else if (token->type == CPP_MINUS_MINUS)
4715 unary_operator = PREDECREMENT_EXPR;
4716 /* Handle the GNU address-of-label extension. */
4717 else if (cp_parser_allow_gnu_extensions_p (parser)
4718 && token->type == CPP_AND_AND)
4720 tree identifier;
4722 /* Consume the '&&' token. */
4723 cp_lexer_consume_token (parser->lexer);
4724 /* Look for the identifier. */
4725 identifier = cp_parser_identifier (parser);
4726 /* Create an expression representing the address. */
4727 return finish_label_address_expr (identifier);
4730 if (unary_operator != ERROR_MARK)
4732 tree cast_expression;
4733 tree expression = error_mark_node;
4734 const char *non_constant_p = NULL;
4736 /* Consume the operator token. */
4737 token = cp_lexer_consume_token (parser->lexer);
4738 /* Parse the cast-expression. */
4739 cast_expression
4740 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4741 /* Now, build an appropriate representation. */
4742 switch (unary_operator)
4744 case INDIRECT_REF:
4745 non_constant_p = "`*'";
4746 expression = build_x_indirect_ref (cast_expression, "unary *");
4747 break;
4749 case ADDR_EXPR:
4750 non_constant_p = "`&'";
4751 /* Fall through. */
4752 case BIT_NOT_EXPR:
4753 expression = build_x_unary_op (unary_operator, cast_expression);
4754 break;
4756 case PREINCREMENT_EXPR:
4757 case PREDECREMENT_EXPR:
4758 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4759 ? "`++'" : "`--'");
4760 /* Fall through. */
4761 case CONVERT_EXPR:
4762 case NEGATE_EXPR:
4763 case TRUTH_NOT_EXPR:
4764 expression = finish_unary_op_expr (unary_operator, cast_expression);
4765 break;
4767 default:
4768 gcc_unreachable ();
4771 if (non_constant_p
4772 && cp_parser_non_integral_constant_expression (parser,
4773 non_constant_p))
4774 expression = error_mark_node;
4776 return expression;
4779 return cp_parser_postfix_expression (parser, address_p);
4782 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4783 unary-operator, the corresponding tree code is returned. */
4785 static enum tree_code
4786 cp_parser_unary_operator (cp_token* token)
4788 switch (token->type)
4790 case CPP_MULT:
4791 return INDIRECT_REF;
4793 case CPP_AND:
4794 return ADDR_EXPR;
4796 case CPP_PLUS:
4797 return CONVERT_EXPR;
4799 case CPP_MINUS:
4800 return NEGATE_EXPR;
4802 case CPP_NOT:
4803 return TRUTH_NOT_EXPR;
4805 case CPP_COMPL:
4806 return BIT_NOT_EXPR;
4808 default:
4809 return ERROR_MARK;
4813 /* Parse a new-expression.
4815 new-expression:
4816 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4817 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4819 Returns a representation of the expression. */
4821 static tree
4822 cp_parser_new_expression (cp_parser* parser)
4824 bool global_scope_p;
4825 tree placement;
4826 tree type;
4827 tree initializer;
4828 tree nelts;
4830 /* Look for the optional `::' operator. */
4831 global_scope_p
4832 = (cp_parser_global_scope_opt (parser,
4833 /*current_scope_valid_p=*/false)
4834 != NULL_TREE);
4835 /* Look for the `new' operator. */
4836 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4837 /* There's no easy way to tell a new-placement from the
4838 `( type-id )' construct. */
4839 cp_parser_parse_tentatively (parser);
4840 /* Look for a new-placement. */
4841 placement = cp_parser_new_placement (parser);
4842 /* If that didn't work out, there's no new-placement. */
4843 if (!cp_parser_parse_definitely (parser))
4844 placement = NULL_TREE;
4846 /* If the next token is a `(', then we have a parenthesized
4847 type-id. */
4848 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4850 /* Consume the `('. */
4851 cp_lexer_consume_token (parser->lexer);
4852 /* Parse the type-id. */
4853 type = cp_parser_type_id (parser);
4854 /* Look for the closing `)'. */
4855 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4856 /* There should not be a direct-new-declarator in this production,
4857 but GCC used to allowed this, so we check and emit a sensible error
4858 message for this case. */
4859 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4861 error ("array bound forbidden after parenthesized type-id");
4862 inform ("try removing the parentheses around the type-id");
4863 cp_parser_direct_new_declarator (parser);
4865 nelts = integer_one_node;
4867 /* Otherwise, there must be a new-type-id. */
4868 else
4869 type = cp_parser_new_type_id (parser, &nelts);
4871 /* If the next token is a `(', then we have a new-initializer. */
4872 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4873 initializer = cp_parser_new_initializer (parser);
4874 else
4875 initializer = NULL_TREE;
4877 /* A new-expression may not appear in an integral constant
4878 expression. */
4879 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4880 return error_mark_node;
4882 /* Create a representation of the new-expression. */
4883 return build_new (placement, type, nelts, initializer, global_scope_p);
4886 /* Parse a new-placement.
4888 new-placement:
4889 ( expression-list )
4891 Returns the same representation as for an expression-list. */
4893 static tree
4894 cp_parser_new_placement (cp_parser* parser)
4896 tree expression_list;
4898 /* Parse the expression-list. */
4899 expression_list = (cp_parser_parenthesized_expression_list
4900 (parser, false, /*non_constant_p=*/NULL));
4902 return expression_list;
4905 /* Parse a new-type-id.
4907 new-type-id:
4908 type-specifier-seq new-declarator [opt]
4910 Returns the TYPE allocated. If the new-type-id indicates an array
4911 type, *NELTS is set to the number of elements in the last array
4912 bound; the TYPE will not include the last array bound. */
4914 static tree
4915 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4917 cp_decl_specifier_seq type_specifier_seq;
4918 cp_declarator *new_declarator;
4919 cp_declarator *declarator;
4920 cp_declarator *outer_declarator;
4921 const char *saved_message;
4922 tree type;
4924 /* The type-specifier sequence must not contain type definitions.
4925 (It cannot contain declarations of new types either, but if they
4926 are not definitions we will catch that because they are not
4927 complete.) */
4928 saved_message = parser->type_definition_forbidden_message;
4929 parser->type_definition_forbidden_message
4930 = "types may not be defined in a new-type-id";
4931 /* Parse the type-specifier-seq. */
4932 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4933 /* Restore the old message. */
4934 parser->type_definition_forbidden_message = saved_message;
4935 /* Parse the new-declarator. */
4936 new_declarator = cp_parser_new_declarator_opt (parser);
4938 /* Determine the number of elements in the last array dimension, if
4939 any. */
4940 *nelts = NULL_TREE;
4941 /* Skip down to the last array dimension. */
4942 declarator = new_declarator;
4943 outer_declarator = NULL;
4944 while (declarator && (declarator->kind == cdk_pointer
4945 || declarator->kind == cdk_ptrmem))
4947 outer_declarator = declarator;
4948 declarator = declarator->declarator;
4950 while (declarator
4951 && declarator->kind == cdk_array
4952 && declarator->declarator
4953 && declarator->declarator->kind == cdk_array)
4955 outer_declarator = declarator;
4956 declarator = declarator->declarator;
4959 if (declarator && declarator->kind == cdk_array)
4961 *nelts = declarator->u.array.bounds;
4962 if (*nelts == error_mark_node)
4963 *nelts = integer_one_node;
4964 else if (!processing_template_decl)
4966 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4967 false))
4968 pedwarn ("size in array new must have integral type");
4969 *nelts = save_expr (cp_convert (sizetype, *nelts));
4970 if (*nelts == integer_zero_node)
4971 warning ("zero size array reserves no space");
4973 if (outer_declarator)
4974 outer_declarator->declarator = declarator->declarator;
4975 else
4976 new_declarator = NULL;
4979 type = groktypename (&type_specifier_seq, new_declarator);
4980 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4982 *nelts = array_type_nelts_top (type);
4983 type = TREE_TYPE (type);
4985 return type;
4988 /* Parse an (optional) new-declarator.
4990 new-declarator:
4991 ptr-operator new-declarator [opt]
4992 direct-new-declarator
4994 Returns the declarator. */
4996 static cp_declarator *
4997 cp_parser_new_declarator_opt (cp_parser* parser)
4999 enum tree_code code;
5000 tree type;
5001 cp_cv_quals cv_quals;
5003 /* We don't know if there's a ptr-operator next, or not. */
5004 cp_parser_parse_tentatively (parser);
5005 /* Look for a ptr-operator. */
5006 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5007 /* If that worked, look for more new-declarators. */
5008 if (cp_parser_parse_definitely (parser))
5010 cp_declarator *declarator;
5012 /* Parse another optional declarator. */
5013 declarator = cp_parser_new_declarator_opt (parser);
5015 /* Create the representation of the declarator. */
5016 if (type)
5017 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5018 else if (code == INDIRECT_REF)
5019 declarator = make_pointer_declarator (cv_quals, declarator);
5020 else
5021 declarator = make_reference_declarator (cv_quals, declarator);
5023 return declarator;
5026 /* If the next token is a `[', there is a direct-new-declarator. */
5027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5028 return cp_parser_direct_new_declarator (parser);
5030 return NULL;
5033 /* Parse a direct-new-declarator.
5035 direct-new-declarator:
5036 [ expression ]
5037 direct-new-declarator [constant-expression]
5041 static cp_declarator *
5042 cp_parser_direct_new_declarator (cp_parser* parser)
5044 cp_declarator *declarator = NULL;
5046 while (true)
5048 tree expression;
5050 /* Look for the opening `['. */
5051 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5052 /* The first expression is not required to be constant. */
5053 if (!declarator)
5055 expression = cp_parser_expression (parser);
5056 /* The standard requires that the expression have integral
5057 type. DR 74 adds enumeration types. We believe that the
5058 real intent is that these expressions be handled like the
5059 expression in a `switch' condition, which also allows
5060 classes with a single conversion to integral or
5061 enumeration type. */
5062 if (!processing_template_decl)
5064 expression
5065 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5066 expression,
5067 /*complain=*/true);
5068 if (!expression)
5070 error ("expression in new-declarator must have integral "
5071 "or enumeration type");
5072 expression = error_mark_node;
5076 /* But all the other expressions must be. */
5077 else
5078 expression
5079 = cp_parser_constant_expression (parser,
5080 /*allow_non_constant=*/false,
5081 NULL);
5082 /* Look for the closing `]'. */
5083 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5085 /* Add this bound to the declarator. */
5086 declarator = make_array_declarator (declarator, expression);
5088 /* If the next token is not a `[', then there are no more
5089 bounds. */
5090 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5091 break;
5094 return declarator;
5097 /* Parse a new-initializer.
5099 new-initializer:
5100 ( expression-list [opt] )
5102 Returns a representation of the expression-list. If there is no
5103 expression-list, VOID_ZERO_NODE is returned. */
5105 static tree
5106 cp_parser_new_initializer (cp_parser* parser)
5108 tree expression_list;
5110 expression_list = (cp_parser_parenthesized_expression_list
5111 (parser, false, /*non_constant_p=*/NULL));
5112 if (!expression_list)
5113 expression_list = void_zero_node;
5115 return expression_list;
5118 /* Parse a delete-expression.
5120 delete-expression:
5121 :: [opt] delete cast-expression
5122 :: [opt] delete [ ] cast-expression
5124 Returns a representation of the expression. */
5126 static tree
5127 cp_parser_delete_expression (cp_parser* parser)
5129 bool global_scope_p;
5130 bool array_p;
5131 tree expression;
5133 /* Look for the optional `::' operator. */
5134 global_scope_p
5135 = (cp_parser_global_scope_opt (parser,
5136 /*current_scope_valid_p=*/false)
5137 != NULL_TREE);
5138 /* Look for the `delete' keyword. */
5139 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5140 /* See if the array syntax is in use. */
5141 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5143 /* Consume the `[' token. */
5144 cp_lexer_consume_token (parser->lexer);
5145 /* Look for the `]' token. */
5146 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5147 /* Remember that this is the `[]' construct. */
5148 array_p = true;
5150 else
5151 array_p = false;
5153 /* Parse the cast-expression. */
5154 expression = cp_parser_simple_cast_expression (parser);
5156 /* A delete-expression may not appear in an integral constant
5157 expression. */
5158 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5159 return error_mark_node;
5161 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5164 /* Parse a cast-expression.
5166 cast-expression:
5167 unary-expression
5168 ( type-id ) cast-expression
5170 Returns a representation of the expression. */
5172 static tree
5173 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5175 /* If it's a `(', then we might be looking at a cast. */
5176 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5178 tree type = NULL_TREE;
5179 tree expr = NULL_TREE;
5180 bool compound_literal_p;
5181 const char *saved_message;
5183 /* There's no way to know yet whether or not this is a cast.
5184 For example, `(int (3))' is a unary-expression, while `(int)
5185 3' is a cast. So, we resort to parsing tentatively. */
5186 cp_parser_parse_tentatively (parser);
5187 /* Types may not be defined in a cast. */
5188 saved_message = parser->type_definition_forbidden_message;
5189 parser->type_definition_forbidden_message
5190 = "types may not be defined in casts";
5191 /* Consume the `('. */
5192 cp_lexer_consume_token (parser->lexer);
5193 /* A very tricky bit is that `(struct S) { 3 }' is a
5194 compound-literal (which we permit in C++ as an extension).
5195 But, that construct is not a cast-expression -- it is a
5196 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5197 is legal; if the compound-literal were a cast-expression,
5198 you'd need an extra set of parentheses.) But, if we parse
5199 the type-id, and it happens to be a class-specifier, then we
5200 will commit to the parse at that point, because we cannot
5201 undo the action that is done when creating a new class. So,
5202 then we cannot back up and do a postfix-expression.
5204 Therefore, we scan ahead to the closing `)', and check to see
5205 if the token after the `)' is a `{'. If so, we are not
5206 looking at a cast-expression.
5208 Save tokens so that we can put them back. */
5209 cp_lexer_save_tokens (parser->lexer);
5210 /* Skip tokens until the next token is a closing parenthesis.
5211 If we find the closing `)', and the next token is a `{', then
5212 we are looking at a compound-literal. */
5213 compound_literal_p
5214 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5215 /*consume_paren=*/true)
5216 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5217 /* Roll back the tokens we skipped. */
5218 cp_lexer_rollback_tokens (parser->lexer);
5219 /* If we were looking at a compound-literal, simulate an error
5220 so that the call to cp_parser_parse_definitely below will
5221 fail. */
5222 if (compound_literal_p)
5223 cp_parser_simulate_error (parser);
5224 else
5226 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5227 parser->in_type_id_in_expr_p = true;
5228 /* Look for the type-id. */
5229 type = cp_parser_type_id (parser);
5230 /* Look for the closing `)'. */
5231 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5232 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5235 /* Restore the saved message. */
5236 parser->type_definition_forbidden_message = saved_message;
5238 /* If ok so far, parse the dependent expression. We cannot be
5239 sure it is a cast. Consider `(T ())'. It is a parenthesized
5240 ctor of T, but looks like a cast to function returning T
5241 without a dependent expression. */
5242 if (!cp_parser_error_occurred (parser))
5243 expr = cp_parser_simple_cast_expression (parser);
5245 if (cp_parser_parse_definitely (parser))
5247 /* Warn about old-style casts, if so requested. */
5248 if (warn_old_style_cast
5249 && !in_system_header
5250 && !VOID_TYPE_P (type)
5251 && current_lang_name != lang_name_c)
5252 warning ("use of old-style cast");
5254 /* Only type conversions to integral or enumeration types
5255 can be used in constant-expressions. */
5256 if (parser->integral_constant_expression_p
5257 && !dependent_type_p (type)
5258 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5259 && (cp_parser_non_integral_constant_expression
5260 (parser,
5261 "a cast to a type other than an integral or "
5262 "enumeration type")))
5263 return error_mark_node;
5265 /* Perform the cast. */
5266 expr = build_c_cast (type, expr);
5267 return expr;
5271 /* If we get here, then it's not a cast, so it must be a
5272 unary-expression. */
5273 return cp_parser_unary_expression (parser, address_p);
5276 /* Parse a binary expression of the general form:
5278 pm-expression:
5279 cast-expression
5280 pm-expression .* cast-expression
5281 pm-expression ->* cast-expression
5283 multiplicative-expression:
5284 pm-expression
5285 multiplicative-expression * pm-expression
5286 multiplicative-expression / pm-expression
5287 multiplicative-expression % pm-expression
5289 additive-expression:
5290 multiplicative-expression
5291 additive-expression + multiplicative-expression
5292 additive-expression - multiplicative-expression
5294 shift-expression:
5295 additive-expression
5296 shift-expression << additive-expression
5297 shift-expression >> additive-expression
5299 relational-expression:
5300 shift-expression
5301 relational-expression < shift-expression
5302 relational-expression > shift-expression
5303 relational-expression <= shift-expression
5304 relational-expression >= shift-expression
5306 GNU Extension:
5308 relational-expression:
5309 relational-expression <? shift-expression
5310 relational-expression >? shift-expression
5312 equality-expression:
5313 relational-expression
5314 equality-expression == relational-expression
5315 equality-expression != relational-expression
5317 and-expression:
5318 equality-expression
5319 and-expression & equality-expression
5321 exclusive-or-expression:
5322 and-expression
5323 exclusive-or-expression ^ and-expression
5325 inclusive-or-expression:
5326 exclusive-or-expression
5327 inclusive-or-expression | exclusive-or-expression
5329 logical-and-expression:
5330 inclusive-or-expression
5331 logical-and-expression && inclusive-or-expression
5333 logical-or-expression:
5334 logical-and-expression
5335 logical-or-expression || logical-and-expression
5337 All these are implemented with a single function like:
5339 binary-expression:
5340 simple-cast-expression
5341 binary-expression <token> binary-expression
5343 The binops_by_token map is used to get the tree codes for each <token> type.
5344 binary-expressions are associated according to a precedence table. */
5346 #define TOKEN_PRECEDENCE(token) \
5347 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5348 ? PREC_NOT_OPERATOR \
5349 : binops_by_token[token->type].prec)
5351 static tree
5352 cp_parser_binary_expression (cp_parser* parser)
5354 cp_parser_expression_stack stack;
5355 cp_parser_expression_stack_entry *sp = &stack[0];
5356 tree lhs, rhs;
5357 cp_token *token;
5358 enum tree_code tree_type;
5359 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5360 bool overloaded_p;
5362 /* Parse the first expression. */
5363 lhs = cp_parser_simple_cast_expression (parser);
5365 for (;;)
5367 /* Get an operator token. */
5368 token = cp_lexer_peek_token (parser->lexer);
5369 new_prec = TOKEN_PRECEDENCE (token);
5371 /* Popping an entry off the stack means we completed a subexpression:
5372 - either we found a token which is not an operator (`>' where it is not
5373 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5374 will happen repeatedly;
5375 - or, we found an operator which has lower priority. This is the case
5376 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5377 parsing `3 * 4'. */
5378 if (new_prec <= prec)
5380 if (sp == stack)
5381 break;
5382 else
5383 goto pop;
5386 get_rhs:
5387 tree_type = binops_by_token[token->type].tree_type;
5389 /* We used the operator token. */
5390 cp_lexer_consume_token (parser->lexer);
5392 /* Extract another operand. It may be the RHS of this expression
5393 or the LHS of a new, higher priority expression. */
5394 rhs = cp_parser_simple_cast_expression (parser);
5396 /* Get another operator token. Look up its precedence to avoid
5397 building a useless (immediately popped) stack entry for common
5398 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5399 token = cp_lexer_peek_token (parser->lexer);
5400 lookahead_prec = TOKEN_PRECEDENCE (token);
5401 if (lookahead_prec > new_prec)
5403 /* ... and prepare to parse the RHS of the new, higher priority
5404 expression. Since precedence levels on the stack are
5405 monotonically increasing, we do not have to care about
5406 stack overflows. */
5407 sp->prec = prec;
5408 sp->tree_type = tree_type;
5409 sp->lhs = lhs;
5410 sp++;
5411 lhs = rhs;
5412 prec = new_prec;
5413 new_prec = lookahead_prec;
5414 goto get_rhs;
5416 pop:
5417 /* If the stack is not empty, we have parsed into LHS the right side
5418 (`4' in the example above) of an expression we had suspended.
5419 We can use the information on the stack to recover the LHS (`3')
5420 from the stack together with the tree code (`MULT_EXPR'), and
5421 the precedence of the higher level subexpression
5422 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5423 which will be used to actually build the additive expression. */
5424 --sp;
5425 prec = sp->prec;
5426 tree_type = sp->tree_type;
5427 rhs = lhs;
5428 lhs = sp->lhs;
5431 overloaded_p = false;
5432 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5434 /* If the binary operator required the use of an overloaded operator,
5435 then this expression cannot be an integral constant-expression.
5436 An overloaded operator can be used even if both operands are
5437 otherwise permissible in an integral constant-expression if at
5438 least one of the operands is of enumeration type. */
5440 if (overloaded_p
5441 && (cp_parser_non_integral_constant_expression
5442 (parser, "calls to overloaded operators")))
5443 return error_mark_node;
5446 return lhs;
5450 /* Parse the `? expression : assignment-expression' part of a
5451 conditional-expression. The LOGICAL_OR_EXPR is the
5452 logical-or-expression that started the conditional-expression.
5453 Returns a representation of the entire conditional-expression.
5455 This routine is used by cp_parser_assignment_expression.
5457 ? expression : assignment-expression
5459 GNU Extensions:
5461 ? : assignment-expression */
5463 static tree
5464 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5466 tree expr;
5467 tree assignment_expr;
5469 /* Consume the `?' token. */
5470 cp_lexer_consume_token (parser->lexer);
5471 if (cp_parser_allow_gnu_extensions_p (parser)
5472 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5473 /* Implicit true clause. */
5474 expr = NULL_TREE;
5475 else
5476 /* Parse the expression. */
5477 expr = cp_parser_expression (parser);
5479 /* The next token should be a `:'. */
5480 cp_parser_require (parser, CPP_COLON, "`:'");
5481 /* Parse the assignment-expression. */
5482 assignment_expr = cp_parser_assignment_expression (parser);
5484 /* Build the conditional-expression. */
5485 return build_x_conditional_expr (logical_or_expr,
5486 expr,
5487 assignment_expr);
5490 /* Parse an assignment-expression.
5492 assignment-expression:
5493 conditional-expression
5494 logical-or-expression assignment-operator assignment_expression
5495 throw-expression
5497 Returns a representation for the expression. */
5499 static tree
5500 cp_parser_assignment_expression (cp_parser* parser)
5502 tree expr;
5504 /* If the next token is the `throw' keyword, then we're looking at
5505 a throw-expression. */
5506 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5507 expr = cp_parser_throw_expression (parser);
5508 /* Otherwise, it must be that we are looking at a
5509 logical-or-expression. */
5510 else
5512 /* Parse the binary expressions (logical-or-expression). */
5513 expr = cp_parser_binary_expression (parser);
5514 /* If the next token is a `?' then we're actually looking at a
5515 conditional-expression. */
5516 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5517 return cp_parser_question_colon_clause (parser, expr);
5518 else
5520 enum tree_code assignment_operator;
5522 /* If it's an assignment-operator, we're using the second
5523 production. */
5524 assignment_operator
5525 = cp_parser_assignment_operator_opt (parser);
5526 if (assignment_operator != ERROR_MARK)
5528 tree rhs;
5530 /* Parse the right-hand side of the assignment. */
5531 rhs = cp_parser_assignment_expression (parser);
5532 /* An assignment may not appear in a
5533 constant-expression. */
5534 if (cp_parser_non_integral_constant_expression (parser,
5535 "an assignment"))
5536 return error_mark_node;
5537 /* Build the assignment expression. */
5538 expr = build_x_modify_expr (expr,
5539 assignment_operator,
5540 rhs);
5545 return expr;
5548 /* Parse an (optional) assignment-operator.
5550 assignment-operator: one of
5551 = *= /= %= += -= >>= <<= &= ^= |=
5553 GNU Extension:
5555 assignment-operator: one of
5556 <?= >?=
5558 If the next token is an assignment operator, the corresponding tree
5559 code is returned, and the token is consumed. For example, for
5560 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5561 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5562 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5563 operator, ERROR_MARK is returned. */
5565 static enum tree_code
5566 cp_parser_assignment_operator_opt (cp_parser* parser)
5568 enum tree_code op;
5569 cp_token *token;
5571 /* Peek at the next toen. */
5572 token = cp_lexer_peek_token (parser->lexer);
5574 switch (token->type)
5576 case CPP_EQ:
5577 op = NOP_EXPR;
5578 break;
5580 case CPP_MULT_EQ:
5581 op = MULT_EXPR;
5582 break;
5584 case CPP_DIV_EQ:
5585 op = TRUNC_DIV_EXPR;
5586 break;
5588 case CPP_MOD_EQ:
5589 op = TRUNC_MOD_EXPR;
5590 break;
5592 case CPP_PLUS_EQ:
5593 op = PLUS_EXPR;
5594 break;
5596 case CPP_MINUS_EQ:
5597 op = MINUS_EXPR;
5598 break;
5600 case CPP_RSHIFT_EQ:
5601 op = RSHIFT_EXPR;
5602 break;
5604 case CPP_LSHIFT_EQ:
5605 op = LSHIFT_EXPR;
5606 break;
5608 case CPP_AND_EQ:
5609 op = BIT_AND_EXPR;
5610 break;
5612 case CPP_XOR_EQ:
5613 op = BIT_XOR_EXPR;
5614 break;
5616 case CPP_OR_EQ:
5617 op = BIT_IOR_EXPR;
5618 break;
5620 case CPP_MIN_EQ:
5621 op = MIN_EXPR;
5622 break;
5624 case CPP_MAX_EQ:
5625 op = MAX_EXPR;
5626 break;
5628 default:
5629 /* Nothing else is an assignment operator. */
5630 op = ERROR_MARK;
5633 /* If it was an assignment operator, consume it. */
5634 if (op != ERROR_MARK)
5635 cp_lexer_consume_token (parser->lexer);
5637 return op;
5640 /* Parse an expression.
5642 expression:
5643 assignment-expression
5644 expression , assignment-expression
5646 Returns a representation of the expression. */
5648 static tree
5649 cp_parser_expression (cp_parser* parser)
5651 tree expression = NULL_TREE;
5653 while (true)
5655 tree assignment_expression;
5657 /* Parse the next assignment-expression. */
5658 assignment_expression
5659 = cp_parser_assignment_expression (parser);
5660 /* If this is the first assignment-expression, we can just
5661 save it away. */
5662 if (!expression)
5663 expression = assignment_expression;
5664 else
5665 expression = build_x_compound_expr (expression,
5666 assignment_expression);
5667 /* If the next token is not a comma, then we are done with the
5668 expression. */
5669 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5670 break;
5671 /* Consume the `,'. */
5672 cp_lexer_consume_token (parser->lexer);
5673 /* A comma operator cannot appear in a constant-expression. */
5674 if (cp_parser_non_integral_constant_expression (parser,
5675 "a comma operator"))
5676 expression = error_mark_node;
5679 return expression;
5682 /* Parse a constant-expression.
5684 constant-expression:
5685 conditional-expression
5687 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5688 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5689 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5690 is false, NON_CONSTANT_P should be NULL. */
5692 static tree
5693 cp_parser_constant_expression (cp_parser* parser,
5694 bool allow_non_constant_p,
5695 bool *non_constant_p)
5697 bool saved_integral_constant_expression_p;
5698 bool saved_allow_non_integral_constant_expression_p;
5699 bool saved_non_integral_constant_expression_p;
5700 tree expression;
5702 /* It might seem that we could simply parse the
5703 conditional-expression, and then check to see if it were
5704 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5705 one that the compiler can figure out is constant, possibly after
5706 doing some simplifications or optimizations. The standard has a
5707 precise definition of constant-expression, and we must honor
5708 that, even though it is somewhat more restrictive.
5710 For example:
5712 int i[(2, 3)];
5714 is not a legal declaration, because `(2, 3)' is not a
5715 constant-expression. The `,' operator is forbidden in a
5716 constant-expression. However, GCC's constant-folding machinery
5717 will fold this operation to an INTEGER_CST for `3'. */
5719 /* Save the old settings. */
5720 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5721 saved_allow_non_integral_constant_expression_p
5722 = parser->allow_non_integral_constant_expression_p;
5723 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5724 /* We are now parsing a constant-expression. */
5725 parser->integral_constant_expression_p = true;
5726 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5727 parser->non_integral_constant_expression_p = false;
5728 /* Although the grammar says "conditional-expression", we parse an
5729 "assignment-expression", which also permits "throw-expression"
5730 and the use of assignment operators. In the case that
5731 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5732 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5733 actually essential that we look for an assignment-expression.
5734 For example, cp_parser_initializer_clauses uses this function to
5735 determine whether a particular assignment-expression is in fact
5736 constant. */
5737 expression = cp_parser_assignment_expression (parser);
5738 /* Restore the old settings. */
5739 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5740 parser->allow_non_integral_constant_expression_p
5741 = saved_allow_non_integral_constant_expression_p;
5742 if (allow_non_constant_p)
5743 *non_constant_p = parser->non_integral_constant_expression_p;
5744 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5746 return expression;
5749 /* Parse __builtin_offsetof.
5751 offsetof-expression:
5752 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5754 offsetof-member-designator:
5755 id-expression
5756 | offsetof-member-designator "." id-expression
5757 | offsetof-member-designator "[" expression "]"
5760 static tree
5761 cp_parser_builtin_offsetof (cp_parser *parser)
5763 int save_ice_p, save_non_ice_p;
5764 tree type, expr;
5765 cp_id_kind dummy;
5767 /* We're about to accept non-integral-constant things, but will
5768 definitely yield an integral constant expression. Save and
5769 restore these values around our local parsing. */
5770 save_ice_p = parser->integral_constant_expression_p;
5771 save_non_ice_p = parser->non_integral_constant_expression_p;
5773 /* Consume the "__builtin_offsetof" token. */
5774 cp_lexer_consume_token (parser->lexer);
5775 /* Consume the opening `('. */
5776 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5777 /* Parse the type-id. */
5778 type = cp_parser_type_id (parser);
5779 /* Look for the `,'. */
5780 cp_parser_require (parser, CPP_COMMA, "`,'");
5782 /* Build the (type *)null that begins the traditional offsetof macro. */
5783 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5785 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5786 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5787 true, &dummy);
5788 while (true)
5790 cp_token *token = cp_lexer_peek_token (parser->lexer);
5791 switch (token->type)
5793 case CPP_OPEN_SQUARE:
5794 /* offsetof-member-designator "[" expression "]" */
5795 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5796 break;
5798 case CPP_DOT:
5799 /* offsetof-member-designator "." identifier */
5800 cp_lexer_consume_token (parser->lexer);
5801 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5802 true, &dummy);
5803 break;
5805 case CPP_CLOSE_PAREN:
5806 /* Consume the ")" token. */
5807 cp_lexer_consume_token (parser->lexer);
5808 goto success;
5810 default:
5811 /* Error. We know the following require will fail, but
5812 that gives the proper error message. */
5813 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5814 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5815 expr = error_mark_node;
5816 goto failure;
5820 success:
5821 /* If we're processing a template, we can't finish the semantics yet.
5822 Otherwise we can fold the entire expression now. */
5823 if (processing_template_decl)
5824 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5825 else
5826 expr = fold_offsetof (expr);
5828 failure:
5829 parser->integral_constant_expression_p = save_ice_p;
5830 parser->non_integral_constant_expression_p = save_non_ice_p;
5832 return expr;
5835 /* Statements [gram.stmt.stmt] */
5837 /* Parse a statement.
5839 statement:
5840 labeled-statement
5841 expression-statement
5842 compound-statement
5843 selection-statement
5844 iteration-statement
5845 jump-statement
5846 declaration-statement
5847 try-block */
5849 static void
5850 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5852 tree statement;
5853 cp_token *token;
5854 location_t statement_location;
5856 /* There is no statement yet. */
5857 statement = NULL_TREE;
5858 /* Peek at the next token. */
5859 token = cp_lexer_peek_token (parser->lexer);
5860 /* Remember the location of the first token in the statement. */
5861 statement_location = token->location;
5862 /* If this is a keyword, then that will often determine what kind of
5863 statement we have. */
5864 if (token->type == CPP_KEYWORD)
5866 enum rid keyword = token->keyword;
5868 switch (keyword)
5870 case RID_CASE:
5871 case RID_DEFAULT:
5872 statement = cp_parser_labeled_statement (parser,
5873 in_statement_expr);
5874 break;
5876 case RID_IF:
5877 case RID_SWITCH:
5878 statement = cp_parser_selection_statement (parser);
5879 break;
5881 case RID_WHILE:
5882 case RID_DO:
5883 case RID_FOR:
5884 statement = cp_parser_iteration_statement (parser);
5885 break;
5887 case RID_BREAK:
5888 case RID_CONTINUE:
5889 case RID_RETURN:
5890 case RID_GOTO:
5891 statement = cp_parser_jump_statement (parser);
5892 break;
5894 case RID_TRY:
5895 statement = cp_parser_try_block (parser);
5896 break;
5898 default:
5899 /* It might be a keyword like `int' that can start a
5900 declaration-statement. */
5901 break;
5904 else if (token->type == CPP_NAME)
5906 /* If the next token is a `:', then we are looking at a
5907 labeled-statement. */
5908 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5909 if (token->type == CPP_COLON)
5910 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5912 /* Anything that starts with a `{' must be a compound-statement. */
5913 else if (token->type == CPP_OPEN_BRACE)
5914 statement = cp_parser_compound_statement (parser, NULL, false);
5915 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5916 a statement all its own. */
5917 else if (token->type == CPP_PRAGMA)
5919 cp_lexer_handle_pragma (parser->lexer);
5920 return;
5923 /* Everything else must be a declaration-statement or an
5924 expression-statement. Try for the declaration-statement
5925 first, unless we are looking at a `;', in which case we know that
5926 we have an expression-statement. */
5927 if (!statement)
5929 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5931 cp_parser_parse_tentatively (parser);
5932 /* Try to parse the declaration-statement. */
5933 cp_parser_declaration_statement (parser);
5934 /* If that worked, we're done. */
5935 if (cp_parser_parse_definitely (parser))
5936 return;
5938 /* Look for an expression-statement instead. */
5939 statement = cp_parser_expression_statement (parser, in_statement_expr);
5942 /* Set the line number for the statement. */
5943 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5944 SET_EXPR_LOCATION (statement, statement_location);
5947 /* Parse a labeled-statement.
5949 labeled-statement:
5950 identifier : statement
5951 case constant-expression : statement
5952 default : statement
5954 GNU Extension:
5956 labeled-statement:
5957 case constant-expression ... constant-expression : statement
5959 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5960 For an ordinary label, returns a LABEL_EXPR. */
5962 static tree
5963 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5965 cp_token *token;
5966 tree statement = error_mark_node;
5968 /* The next token should be an identifier. */
5969 token = cp_lexer_peek_token (parser->lexer);
5970 if (token->type != CPP_NAME
5971 && token->type != CPP_KEYWORD)
5973 cp_parser_error (parser, "expected labeled-statement");
5974 return error_mark_node;
5977 switch (token->keyword)
5979 case RID_CASE:
5981 tree expr, expr_hi;
5982 cp_token *ellipsis;
5984 /* Consume the `case' token. */
5985 cp_lexer_consume_token (parser->lexer);
5986 /* Parse the constant-expression. */
5987 expr = cp_parser_constant_expression (parser,
5988 /*allow_non_constant_p=*/false,
5989 NULL);
5991 ellipsis = cp_lexer_peek_token (parser->lexer);
5992 if (ellipsis->type == CPP_ELLIPSIS)
5994 /* Consume the `...' token. */
5995 cp_lexer_consume_token (parser->lexer);
5996 expr_hi =
5997 cp_parser_constant_expression (parser,
5998 /*allow_non_constant_p=*/false,
5999 NULL);
6000 /* We don't need to emit warnings here, as the common code
6001 will do this for us. */
6003 else
6004 expr_hi = NULL_TREE;
6006 if (!parser->in_switch_statement_p)
6007 error ("case label %qE not within a switch statement", expr);
6008 else
6009 statement = finish_case_label (expr, expr_hi);
6011 break;
6013 case RID_DEFAULT:
6014 /* Consume the `default' token. */
6015 cp_lexer_consume_token (parser->lexer);
6016 if (!parser->in_switch_statement_p)
6017 error ("case label not within a switch statement");
6018 else
6019 statement = finish_case_label (NULL_TREE, NULL_TREE);
6020 break;
6022 default:
6023 /* Anything else must be an ordinary label. */
6024 statement = finish_label_stmt (cp_parser_identifier (parser));
6025 break;
6028 /* Require the `:' token. */
6029 cp_parser_require (parser, CPP_COLON, "`:'");
6030 /* Parse the labeled statement. */
6031 cp_parser_statement (parser, in_statement_expr);
6033 /* Return the label, in the case of a `case' or `default' label. */
6034 return statement;
6037 /* Parse an expression-statement.
6039 expression-statement:
6040 expression [opt] ;
6042 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6043 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6044 indicates whether this expression-statement is part of an
6045 expression statement. */
6047 static tree
6048 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6050 tree statement = NULL_TREE;
6052 /* If the next token is a ';', then there is no expression
6053 statement. */
6054 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6055 statement = cp_parser_expression (parser);
6057 /* Consume the final `;'. */
6058 cp_parser_consume_semicolon_at_end_of_statement (parser);
6060 if (in_statement_expr
6061 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6063 /* This is the final expression statement of a statement
6064 expression. */
6065 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6067 else if (statement)
6068 statement = finish_expr_stmt (statement);
6069 else
6070 finish_stmt ();
6072 return statement;
6075 /* Parse a compound-statement.
6077 compound-statement:
6078 { statement-seq [opt] }
6080 Returns a tree representing the statement. */
6082 static tree
6083 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6084 bool in_try)
6086 tree compound_stmt;
6088 /* Consume the `{'. */
6089 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6090 return error_mark_node;
6091 /* Begin the compound-statement. */
6092 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6093 /* Parse an (optional) statement-seq. */
6094 cp_parser_statement_seq_opt (parser, in_statement_expr);
6095 /* Finish the compound-statement. */
6096 finish_compound_stmt (compound_stmt);
6097 /* Consume the `}'. */
6098 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6100 return compound_stmt;
6103 /* Parse an (optional) statement-seq.
6105 statement-seq:
6106 statement
6107 statement-seq [opt] statement */
6109 static void
6110 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6112 /* Scan statements until there aren't any more. */
6113 while (true)
6115 /* If we're looking at a `}', then we've run out of statements. */
6116 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6117 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6118 break;
6120 /* Parse the statement. */
6121 cp_parser_statement (parser, in_statement_expr);
6125 /* Parse a selection-statement.
6127 selection-statement:
6128 if ( condition ) statement
6129 if ( condition ) statement else statement
6130 switch ( condition ) statement
6132 Returns the new IF_STMT or SWITCH_STMT. */
6134 static tree
6135 cp_parser_selection_statement (cp_parser* parser)
6137 cp_token *token;
6138 enum rid keyword;
6140 /* Peek at the next token. */
6141 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6143 /* See what kind of keyword it is. */
6144 keyword = token->keyword;
6145 switch (keyword)
6147 case RID_IF:
6148 case RID_SWITCH:
6150 tree statement;
6151 tree condition;
6153 /* Look for the `('. */
6154 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6156 cp_parser_skip_to_end_of_statement (parser);
6157 return error_mark_node;
6160 /* Begin the selection-statement. */
6161 if (keyword == RID_IF)
6162 statement = begin_if_stmt ();
6163 else
6164 statement = begin_switch_stmt ();
6166 /* Parse the condition. */
6167 condition = cp_parser_condition (parser);
6168 /* Look for the `)'. */
6169 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6170 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6171 /*consume_paren=*/true);
6173 if (keyword == RID_IF)
6175 /* Add the condition. */
6176 finish_if_stmt_cond (condition, statement);
6178 /* Parse the then-clause. */
6179 cp_parser_implicitly_scoped_statement (parser);
6180 finish_then_clause (statement);
6182 /* If the next token is `else', parse the else-clause. */
6183 if (cp_lexer_next_token_is_keyword (parser->lexer,
6184 RID_ELSE))
6186 /* Consume the `else' keyword. */
6187 cp_lexer_consume_token (parser->lexer);
6188 begin_else_clause (statement);
6189 /* Parse the else-clause. */
6190 cp_parser_implicitly_scoped_statement (parser);
6191 finish_else_clause (statement);
6194 /* Now we're all done with the if-statement. */
6195 finish_if_stmt (statement);
6197 else
6199 bool in_switch_statement_p;
6201 /* Add the condition. */
6202 finish_switch_cond (condition, statement);
6204 /* Parse the body of the switch-statement. */
6205 in_switch_statement_p = parser->in_switch_statement_p;
6206 parser->in_switch_statement_p = true;
6207 cp_parser_implicitly_scoped_statement (parser);
6208 parser->in_switch_statement_p = in_switch_statement_p;
6210 /* Now we're all done with the switch-statement. */
6211 finish_switch_stmt (statement);
6214 return statement;
6216 break;
6218 default:
6219 cp_parser_error (parser, "expected selection-statement");
6220 return error_mark_node;
6224 /* Parse a condition.
6226 condition:
6227 expression
6228 type-specifier-seq declarator = assignment-expression
6230 GNU Extension:
6232 condition:
6233 type-specifier-seq declarator asm-specification [opt]
6234 attributes [opt] = assignment-expression
6236 Returns the expression that should be tested. */
6238 static tree
6239 cp_parser_condition (cp_parser* parser)
6241 cp_decl_specifier_seq type_specifiers;
6242 const char *saved_message;
6244 /* Try the declaration first. */
6245 cp_parser_parse_tentatively (parser);
6246 /* New types are not allowed in the type-specifier-seq for a
6247 condition. */
6248 saved_message = parser->type_definition_forbidden_message;
6249 parser->type_definition_forbidden_message
6250 = "types may not be defined in conditions";
6251 /* Parse the type-specifier-seq. */
6252 cp_parser_type_specifier_seq (parser, &type_specifiers);
6253 /* Restore the saved message. */
6254 parser->type_definition_forbidden_message = saved_message;
6255 /* If all is well, we might be looking at a declaration. */
6256 if (!cp_parser_error_occurred (parser))
6258 tree decl;
6259 tree asm_specification;
6260 tree attributes;
6261 cp_declarator *declarator;
6262 tree initializer = NULL_TREE;
6264 /* Parse the declarator. */
6265 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6266 /*ctor_dtor_or_conv_p=*/NULL,
6267 /*parenthesized_p=*/NULL);
6268 /* Parse the attributes. */
6269 attributes = cp_parser_attributes_opt (parser);
6270 /* Parse the asm-specification. */
6271 asm_specification = cp_parser_asm_specification_opt (parser);
6272 /* If the next token is not an `=', then we might still be
6273 looking at an expression. For example:
6275 if (A(a).x)
6277 looks like a decl-specifier-seq and a declarator -- but then
6278 there is no `=', so this is an expression. */
6279 cp_parser_require (parser, CPP_EQ, "`='");
6280 /* If we did see an `=', then we are looking at a declaration
6281 for sure. */
6282 if (cp_parser_parse_definitely (parser))
6284 bool pop_p;
6286 /* Create the declaration. */
6287 decl = start_decl (declarator, &type_specifiers,
6288 /*initialized_p=*/true,
6289 attributes, /*prefix_attributes=*/NULL_TREE,
6290 &pop_p);
6291 /* Parse the assignment-expression. */
6292 initializer = cp_parser_assignment_expression (parser);
6294 /* Process the initializer. */
6295 cp_finish_decl (decl,
6296 initializer,
6297 asm_specification,
6298 LOOKUP_ONLYCONVERTING);
6300 if (pop_p)
6301 pop_scope (DECL_CONTEXT (decl));
6303 return convert_from_reference (decl);
6306 /* If we didn't even get past the declarator successfully, we are
6307 definitely not looking at a declaration. */
6308 else
6309 cp_parser_abort_tentative_parse (parser);
6311 /* Otherwise, we are looking at an expression. */
6312 return cp_parser_expression (parser);
6315 /* Parse an iteration-statement.
6317 iteration-statement:
6318 while ( condition ) statement
6319 do statement while ( expression ) ;
6320 for ( for-init-statement condition [opt] ; expression [opt] )
6321 statement
6323 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6325 static tree
6326 cp_parser_iteration_statement (cp_parser* parser)
6328 cp_token *token;
6329 enum rid keyword;
6330 tree statement;
6331 bool in_iteration_statement_p;
6334 /* Peek at the next token. */
6335 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6336 if (!token)
6337 return error_mark_node;
6339 /* Remember whether or not we are already within an iteration
6340 statement. */
6341 in_iteration_statement_p = parser->in_iteration_statement_p;
6343 /* See what kind of keyword it is. */
6344 keyword = token->keyword;
6345 switch (keyword)
6347 case RID_WHILE:
6349 tree condition;
6351 /* Begin the while-statement. */
6352 statement = begin_while_stmt ();
6353 /* Look for the `('. */
6354 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6355 /* Parse the condition. */
6356 condition = cp_parser_condition (parser);
6357 finish_while_stmt_cond (condition, statement);
6358 /* Look for the `)'. */
6359 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6360 /* Parse the dependent statement. */
6361 parser->in_iteration_statement_p = true;
6362 cp_parser_already_scoped_statement (parser);
6363 parser->in_iteration_statement_p = in_iteration_statement_p;
6364 /* We're done with the while-statement. */
6365 finish_while_stmt (statement);
6367 break;
6369 case RID_DO:
6371 tree expression;
6373 /* Begin the do-statement. */
6374 statement = begin_do_stmt ();
6375 /* Parse the body of the do-statement. */
6376 parser->in_iteration_statement_p = true;
6377 cp_parser_implicitly_scoped_statement (parser);
6378 parser->in_iteration_statement_p = in_iteration_statement_p;
6379 finish_do_body (statement);
6380 /* Look for the `while' keyword. */
6381 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6382 /* Look for the `('. */
6383 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6384 /* Parse the expression. */
6385 expression = cp_parser_expression (parser);
6386 /* We're done with the do-statement. */
6387 finish_do_stmt (expression, statement);
6388 /* Look for the `)'. */
6389 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6390 /* Look for the `;'. */
6391 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6393 break;
6395 case RID_FOR:
6397 tree condition = NULL_TREE;
6398 tree expression = NULL_TREE;
6400 /* Begin the for-statement. */
6401 statement = begin_for_stmt ();
6402 /* Look for the `('. */
6403 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6404 /* Parse the initialization. */
6405 cp_parser_for_init_statement (parser);
6406 finish_for_init_stmt (statement);
6408 /* If there's a condition, process it. */
6409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6410 condition = cp_parser_condition (parser);
6411 finish_for_cond (condition, statement);
6412 /* Look for the `;'. */
6413 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6415 /* If there's an expression, process it. */
6416 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6417 expression = cp_parser_expression (parser);
6418 finish_for_expr (expression, statement);
6419 /* Look for the `)'. */
6420 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6422 /* Parse the body of the for-statement. */
6423 parser->in_iteration_statement_p = true;
6424 cp_parser_already_scoped_statement (parser);
6425 parser->in_iteration_statement_p = in_iteration_statement_p;
6427 /* We're done with the for-statement. */
6428 finish_for_stmt (statement);
6430 break;
6432 default:
6433 cp_parser_error (parser, "expected iteration-statement");
6434 statement = error_mark_node;
6435 break;
6438 return statement;
6441 /* Parse a for-init-statement.
6443 for-init-statement:
6444 expression-statement
6445 simple-declaration */
6447 static void
6448 cp_parser_for_init_statement (cp_parser* parser)
6450 /* If the next token is a `;', then we have an empty
6451 expression-statement. Grammatically, this is also a
6452 simple-declaration, but an invalid one, because it does not
6453 declare anything. Therefore, if we did not handle this case
6454 specially, we would issue an error message about an invalid
6455 declaration. */
6456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6458 /* We're going to speculatively look for a declaration, falling back
6459 to an expression, if necessary. */
6460 cp_parser_parse_tentatively (parser);
6461 /* Parse the declaration. */
6462 cp_parser_simple_declaration (parser,
6463 /*function_definition_allowed_p=*/false);
6464 /* If the tentative parse failed, then we shall need to look for an
6465 expression-statement. */
6466 if (cp_parser_parse_definitely (parser))
6467 return;
6470 cp_parser_expression_statement (parser, false);
6473 /* Parse a jump-statement.
6475 jump-statement:
6476 break ;
6477 continue ;
6478 return expression [opt] ;
6479 goto identifier ;
6481 GNU extension:
6483 jump-statement:
6484 goto * expression ;
6486 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6488 static tree
6489 cp_parser_jump_statement (cp_parser* parser)
6491 tree statement = error_mark_node;
6492 cp_token *token;
6493 enum rid keyword;
6495 /* Peek at the next token. */
6496 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6497 if (!token)
6498 return error_mark_node;
6500 /* See what kind of keyword it is. */
6501 keyword = token->keyword;
6502 switch (keyword)
6504 case RID_BREAK:
6505 if (!parser->in_switch_statement_p
6506 && !parser->in_iteration_statement_p)
6508 error ("break statement not within loop or switch");
6509 statement = error_mark_node;
6511 else
6512 statement = finish_break_stmt ();
6513 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6514 break;
6516 case RID_CONTINUE:
6517 if (!parser->in_iteration_statement_p)
6519 error ("continue statement not within a loop");
6520 statement = error_mark_node;
6522 else
6523 statement = finish_continue_stmt ();
6524 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6525 break;
6527 case RID_RETURN:
6529 tree expr;
6531 /* If the next token is a `;', then there is no
6532 expression. */
6533 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6534 expr = cp_parser_expression (parser);
6535 else
6536 expr = NULL_TREE;
6537 /* Build the return-statement. */
6538 statement = finish_return_stmt (expr);
6539 /* Look for the final `;'. */
6540 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6542 break;
6544 case RID_GOTO:
6545 /* Create the goto-statement. */
6546 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6548 /* Issue a warning about this use of a GNU extension. */
6549 if (pedantic)
6550 pedwarn ("ISO C++ forbids computed gotos");
6551 /* Consume the '*' token. */
6552 cp_lexer_consume_token (parser->lexer);
6553 /* Parse the dependent expression. */
6554 finish_goto_stmt (cp_parser_expression (parser));
6556 else
6557 finish_goto_stmt (cp_parser_identifier (parser));
6558 /* Look for the final `;'. */
6559 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6560 break;
6562 default:
6563 cp_parser_error (parser, "expected jump-statement");
6564 break;
6567 return statement;
6570 /* Parse a declaration-statement.
6572 declaration-statement:
6573 block-declaration */
6575 static void
6576 cp_parser_declaration_statement (cp_parser* parser)
6578 void *p;
6580 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6581 p = obstack_alloc (&declarator_obstack, 0);
6583 /* Parse the block-declaration. */
6584 cp_parser_block_declaration (parser, /*statement_p=*/true);
6586 /* Free any declarators allocated. */
6587 obstack_free (&declarator_obstack, p);
6589 /* Finish off the statement. */
6590 finish_stmt ();
6593 /* Some dependent statements (like `if (cond) statement'), are
6594 implicitly in their own scope. In other words, if the statement is
6595 a single statement (as opposed to a compound-statement), it is
6596 none-the-less treated as if it were enclosed in braces. Any
6597 declarations appearing in the dependent statement are out of scope
6598 after control passes that point. This function parses a statement,
6599 but ensures that is in its own scope, even if it is not a
6600 compound-statement.
6602 Returns the new statement. */
6604 static tree
6605 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6607 tree statement;
6609 /* If the token is not a `{', then we must take special action. */
6610 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6612 /* Create a compound-statement. */
6613 statement = begin_compound_stmt (0);
6614 /* Parse the dependent-statement. */
6615 cp_parser_statement (parser, false);
6616 /* Finish the dummy compound-statement. */
6617 finish_compound_stmt (statement);
6619 /* Otherwise, we simply parse the statement directly. */
6620 else
6621 statement = cp_parser_compound_statement (parser, NULL, false);
6623 /* Return the statement. */
6624 return statement;
6627 /* For some dependent statements (like `while (cond) statement'), we
6628 have already created a scope. Therefore, even if the dependent
6629 statement is a compound-statement, we do not want to create another
6630 scope. */
6632 static void
6633 cp_parser_already_scoped_statement (cp_parser* parser)
6635 /* If the token is a `{', then we must take special action. */
6636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6637 cp_parser_statement (parser, false);
6638 else
6640 /* Avoid calling cp_parser_compound_statement, so that we
6641 don't create a new scope. Do everything else by hand. */
6642 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6643 cp_parser_statement_seq_opt (parser, false);
6644 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6648 /* Declarations [gram.dcl.dcl] */
6650 /* Parse an optional declaration-sequence.
6652 declaration-seq:
6653 declaration
6654 declaration-seq declaration */
6656 static void
6657 cp_parser_declaration_seq_opt (cp_parser* parser)
6659 while (true)
6661 cp_token *token;
6663 token = cp_lexer_peek_token (parser->lexer);
6665 if (token->type == CPP_CLOSE_BRACE
6666 || token->type == CPP_EOF)
6667 break;
6669 if (token->type == CPP_SEMICOLON)
6671 /* A declaration consisting of a single semicolon is
6672 invalid. Allow it unless we're being pedantic. */
6673 cp_lexer_consume_token (parser->lexer);
6674 if (pedantic && !in_system_header)
6675 pedwarn ("extra %<;%>");
6676 continue;
6679 /* If we're entering or exiting a region that's implicitly
6680 extern "C", modify the lang context appropriately. */
6681 if (!parser->implicit_extern_c && token->implicit_extern_c)
6683 push_lang_context (lang_name_c);
6684 parser->implicit_extern_c = true;
6686 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6688 pop_lang_context ();
6689 parser->implicit_extern_c = false;
6692 if (token->type == CPP_PRAGMA)
6694 /* A top-level declaration can consist solely of a #pragma.
6695 A nested declaration cannot, so this is done here and not
6696 in cp_parser_declaration. (A #pragma at block scope is
6697 handled in cp_parser_statement.) */
6698 cp_lexer_handle_pragma (parser->lexer);
6699 continue;
6702 /* Parse the declaration itself. */
6703 cp_parser_declaration (parser);
6707 /* Parse a declaration.
6709 declaration:
6710 block-declaration
6711 function-definition
6712 template-declaration
6713 explicit-instantiation
6714 explicit-specialization
6715 linkage-specification
6716 namespace-definition
6718 GNU extension:
6720 declaration:
6721 __extension__ declaration */
6723 static void
6724 cp_parser_declaration (cp_parser* parser)
6726 cp_token token1;
6727 cp_token token2;
6728 int saved_pedantic;
6729 void *p;
6731 /* Check for the `__extension__' keyword. */
6732 if (cp_parser_extension_opt (parser, &saved_pedantic))
6734 /* Parse the qualified declaration. */
6735 cp_parser_declaration (parser);
6736 /* Restore the PEDANTIC flag. */
6737 pedantic = saved_pedantic;
6739 return;
6742 /* Try to figure out what kind of declaration is present. */
6743 token1 = *cp_lexer_peek_token (parser->lexer);
6745 if (token1.type != CPP_EOF)
6746 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6748 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6749 p = obstack_alloc (&declarator_obstack, 0);
6751 /* If the next token is `extern' and the following token is a string
6752 literal, then we have a linkage specification. */
6753 if (token1.keyword == RID_EXTERN
6754 && cp_parser_is_string_literal (&token2))
6755 cp_parser_linkage_specification (parser);
6756 /* If the next token is `template', then we have either a template
6757 declaration, an explicit instantiation, or an explicit
6758 specialization. */
6759 else if (token1.keyword == RID_TEMPLATE)
6761 /* `template <>' indicates a template specialization. */
6762 if (token2.type == CPP_LESS
6763 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6764 cp_parser_explicit_specialization (parser);
6765 /* `template <' indicates a template declaration. */
6766 else if (token2.type == CPP_LESS)
6767 cp_parser_template_declaration (parser, /*member_p=*/false);
6768 /* Anything else must be an explicit instantiation. */
6769 else
6770 cp_parser_explicit_instantiation (parser);
6772 /* If the next token is `export', then we have a template
6773 declaration. */
6774 else if (token1.keyword == RID_EXPORT)
6775 cp_parser_template_declaration (parser, /*member_p=*/false);
6776 /* If the next token is `extern', 'static' or 'inline' and the one
6777 after that is `template', we have a GNU extended explicit
6778 instantiation directive. */
6779 else if (cp_parser_allow_gnu_extensions_p (parser)
6780 && (token1.keyword == RID_EXTERN
6781 || token1.keyword == RID_STATIC
6782 || token1.keyword == RID_INLINE)
6783 && token2.keyword == RID_TEMPLATE)
6784 cp_parser_explicit_instantiation (parser);
6785 /* If the next token is `namespace', check for a named or unnamed
6786 namespace definition. */
6787 else if (token1.keyword == RID_NAMESPACE
6788 && (/* A named namespace definition. */
6789 (token2.type == CPP_NAME
6790 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6791 == CPP_OPEN_BRACE))
6792 /* An unnamed namespace definition. */
6793 || token2.type == CPP_OPEN_BRACE))
6794 cp_parser_namespace_definition (parser);
6795 /* We must have either a block declaration or a function
6796 definition. */
6797 else
6798 /* Try to parse a block-declaration, or a function-definition. */
6799 cp_parser_block_declaration (parser, /*statement_p=*/false);
6801 /* Free any declarators allocated. */
6802 obstack_free (&declarator_obstack, p);
6805 /* Parse a block-declaration.
6807 block-declaration:
6808 simple-declaration
6809 asm-definition
6810 namespace-alias-definition
6811 using-declaration
6812 using-directive
6814 GNU Extension:
6816 block-declaration:
6817 __extension__ block-declaration
6818 label-declaration
6820 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6821 part of a declaration-statement. */
6823 static void
6824 cp_parser_block_declaration (cp_parser *parser,
6825 bool statement_p)
6827 cp_token *token1;
6828 int saved_pedantic;
6830 /* Check for the `__extension__' keyword. */
6831 if (cp_parser_extension_opt (parser, &saved_pedantic))
6833 /* Parse the qualified declaration. */
6834 cp_parser_block_declaration (parser, statement_p);
6835 /* Restore the PEDANTIC flag. */
6836 pedantic = saved_pedantic;
6838 return;
6841 /* Peek at the next token to figure out which kind of declaration is
6842 present. */
6843 token1 = cp_lexer_peek_token (parser->lexer);
6845 /* If the next keyword is `asm', we have an asm-definition. */
6846 if (token1->keyword == RID_ASM)
6848 if (statement_p)
6849 cp_parser_commit_to_tentative_parse (parser);
6850 cp_parser_asm_definition (parser);
6852 /* If the next keyword is `namespace', we have a
6853 namespace-alias-definition. */
6854 else if (token1->keyword == RID_NAMESPACE)
6855 cp_parser_namespace_alias_definition (parser);
6856 /* If the next keyword is `using', we have either a
6857 using-declaration or a using-directive. */
6858 else if (token1->keyword == RID_USING)
6860 cp_token *token2;
6862 if (statement_p)
6863 cp_parser_commit_to_tentative_parse (parser);
6864 /* If the token after `using' is `namespace', then we have a
6865 using-directive. */
6866 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6867 if (token2->keyword == RID_NAMESPACE)
6868 cp_parser_using_directive (parser);
6869 /* Otherwise, it's a using-declaration. */
6870 else
6871 cp_parser_using_declaration (parser);
6873 /* If the next keyword is `__label__' we have a label declaration. */
6874 else if (token1->keyword == RID_LABEL)
6876 if (statement_p)
6877 cp_parser_commit_to_tentative_parse (parser);
6878 cp_parser_label_declaration (parser);
6880 /* Anything else must be a simple-declaration. */
6881 else
6882 cp_parser_simple_declaration (parser, !statement_p);
6885 /* Parse a simple-declaration.
6887 simple-declaration:
6888 decl-specifier-seq [opt] init-declarator-list [opt] ;
6890 init-declarator-list:
6891 init-declarator
6892 init-declarator-list , init-declarator
6894 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6895 function-definition as a simple-declaration. */
6897 static void
6898 cp_parser_simple_declaration (cp_parser* parser,
6899 bool function_definition_allowed_p)
6901 cp_decl_specifier_seq decl_specifiers;
6902 int declares_class_or_enum;
6903 bool saw_declarator;
6905 /* Defer access checks until we know what is being declared; the
6906 checks for names appearing in the decl-specifier-seq should be
6907 done as if we were in the scope of the thing being declared. */
6908 push_deferring_access_checks (dk_deferred);
6910 /* Parse the decl-specifier-seq. We have to keep track of whether
6911 or not the decl-specifier-seq declares a named class or
6912 enumeration type, since that is the only case in which the
6913 init-declarator-list is allowed to be empty.
6915 [dcl.dcl]
6917 In a simple-declaration, the optional init-declarator-list can be
6918 omitted only when declaring a class or enumeration, that is when
6919 the decl-specifier-seq contains either a class-specifier, an
6920 elaborated-type-specifier, or an enum-specifier. */
6921 cp_parser_decl_specifier_seq (parser,
6922 CP_PARSER_FLAGS_OPTIONAL,
6923 &decl_specifiers,
6924 &declares_class_or_enum);
6925 /* We no longer need to defer access checks. */
6926 stop_deferring_access_checks ();
6928 /* In a block scope, a valid declaration must always have a
6929 decl-specifier-seq. By not trying to parse declarators, we can
6930 resolve the declaration/expression ambiguity more quickly. */
6931 if (!function_definition_allowed_p
6932 && !decl_specifiers.any_specifiers_p)
6934 cp_parser_error (parser, "expected declaration");
6935 goto done;
6938 /* If the next two tokens are both identifiers, the code is
6939 erroneous. The usual cause of this situation is code like:
6941 T t;
6943 where "T" should name a type -- but does not. */
6944 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6946 /* If parsing tentatively, we should commit; we really are
6947 looking at a declaration. */
6948 cp_parser_commit_to_tentative_parse (parser);
6949 /* Give up. */
6950 goto done;
6953 /* If we have seen at least one decl-specifier, and the next token
6954 is not a parenthesis, then we must be looking at a declaration.
6955 (After "int (" we might be looking at a functional cast.) */
6956 if (decl_specifiers.any_specifiers_p
6957 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6958 cp_parser_commit_to_tentative_parse (parser);
6960 /* Keep going until we hit the `;' at the end of the simple
6961 declaration. */
6962 saw_declarator = false;
6963 while (cp_lexer_next_token_is_not (parser->lexer,
6964 CPP_SEMICOLON))
6966 cp_token *token;
6967 bool function_definition_p;
6968 tree decl;
6970 saw_declarator = true;
6971 /* Parse the init-declarator. */
6972 decl = cp_parser_init_declarator (parser, &decl_specifiers,
6973 function_definition_allowed_p,
6974 /*member_p=*/false,
6975 declares_class_or_enum,
6976 &function_definition_p);
6977 /* If an error occurred while parsing tentatively, exit quickly.
6978 (That usually happens when in the body of a function; each
6979 statement is treated as a declaration-statement until proven
6980 otherwise.) */
6981 if (cp_parser_error_occurred (parser))
6982 goto done;
6983 /* Handle function definitions specially. */
6984 if (function_definition_p)
6986 /* If the next token is a `,', then we are probably
6987 processing something like:
6989 void f() {}, *p;
6991 which is erroneous. */
6992 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6993 error ("mixing declarations and function-definitions is forbidden");
6994 /* Otherwise, we're done with the list of declarators. */
6995 else
6997 pop_deferring_access_checks ();
6998 return;
7001 /* The next token should be either a `,' or a `;'. */
7002 token = cp_lexer_peek_token (parser->lexer);
7003 /* If it's a `,', there are more declarators to come. */
7004 if (token->type == CPP_COMMA)
7005 cp_lexer_consume_token (parser->lexer);
7006 /* If it's a `;', we are done. */
7007 else if (token->type == CPP_SEMICOLON)
7008 break;
7009 /* Anything else is an error. */
7010 else
7012 /* If we have already issued an error message we don't need
7013 to issue another one. */
7014 if (decl != error_mark_node
7015 || (cp_parser_parsing_tentatively (parser)
7016 && !cp_parser_committed_to_tentative_parse (parser)))
7017 cp_parser_error (parser, "expected %<,%> or %<;%>");
7018 /* Skip tokens until we reach the end of the statement. */
7019 cp_parser_skip_to_end_of_statement (parser);
7020 /* If the next token is now a `;', consume it. */
7021 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7022 cp_lexer_consume_token (parser->lexer);
7023 goto done;
7025 /* After the first time around, a function-definition is not
7026 allowed -- even if it was OK at first. For example:
7028 int i, f() {}
7030 is not valid. */
7031 function_definition_allowed_p = false;
7034 /* Issue an error message if no declarators are present, and the
7035 decl-specifier-seq does not itself declare a class or
7036 enumeration. */
7037 if (!saw_declarator)
7039 if (cp_parser_declares_only_class_p (parser))
7040 shadow_tag (&decl_specifiers);
7041 /* Perform any deferred access checks. */
7042 perform_deferred_access_checks ();
7045 /* Consume the `;'. */
7046 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7048 done:
7049 pop_deferring_access_checks ();
7052 /* Parse a decl-specifier-seq.
7054 decl-specifier-seq:
7055 decl-specifier-seq [opt] decl-specifier
7057 decl-specifier:
7058 storage-class-specifier
7059 type-specifier
7060 function-specifier
7061 friend
7062 typedef
7064 GNU Extension:
7066 decl-specifier:
7067 attributes
7069 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7071 The parser flags FLAGS is used to control type-specifier parsing.
7073 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7074 flags:
7076 1: one of the decl-specifiers is an elaborated-type-specifier
7077 (i.e., a type declaration)
7078 2: one of the decl-specifiers is an enum-specifier or a
7079 class-specifier (i.e., a type definition)
7083 static void
7084 cp_parser_decl_specifier_seq (cp_parser* parser,
7085 cp_parser_flags flags,
7086 cp_decl_specifier_seq *decl_specs,
7087 int* declares_class_or_enum)
7089 bool constructor_possible_p = !parser->in_declarator_p;
7091 /* Clear DECL_SPECS. */
7092 clear_decl_specs (decl_specs);
7094 /* Assume no class or enumeration type is declared. */
7095 *declares_class_or_enum = 0;
7097 /* Keep reading specifiers until there are no more to read. */
7098 while (true)
7100 bool constructor_p;
7101 bool found_decl_spec;
7102 cp_token *token;
7104 /* Peek at the next token. */
7105 token = cp_lexer_peek_token (parser->lexer);
7106 /* Handle attributes. */
7107 if (token->keyword == RID_ATTRIBUTE)
7109 /* Parse the attributes. */
7110 decl_specs->attributes
7111 = chainon (decl_specs->attributes,
7112 cp_parser_attributes_opt (parser));
7113 continue;
7115 /* Assume we will find a decl-specifier keyword. */
7116 found_decl_spec = true;
7117 /* If the next token is an appropriate keyword, we can simply
7118 add it to the list. */
7119 switch (token->keyword)
7121 /* decl-specifier:
7122 friend */
7123 case RID_FRIEND:
7124 if (decl_specs->specs[(int) ds_friend]++)
7125 error ("duplicate %<friend%>");
7126 /* Consume the token. */
7127 cp_lexer_consume_token (parser->lexer);
7128 break;
7130 /* function-specifier:
7131 inline
7132 virtual
7133 explicit */
7134 case RID_INLINE:
7135 case RID_VIRTUAL:
7136 case RID_EXPLICIT:
7137 cp_parser_function_specifier_opt (parser, decl_specs);
7138 break;
7140 /* decl-specifier:
7141 typedef */
7142 case RID_TYPEDEF:
7143 ++decl_specs->specs[(int) ds_typedef];
7144 /* Consume the token. */
7145 cp_lexer_consume_token (parser->lexer);
7146 /* A constructor declarator cannot appear in a typedef. */
7147 constructor_possible_p = false;
7148 /* The "typedef" keyword can only occur in a declaration; we
7149 may as well commit at this point. */
7150 cp_parser_commit_to_tentative_parse (parser);
7151 break;
7153 /* storage-class-specifier:
7154 auto
7155 register
7156 static
7157 extern
7158 mutable
7160 GNU Extension:
7161 thread */
7162 case RID_AUTO:
7163 /* Consume the token. */
7164 cp_lexer_consume_token (parser->lexer);
7165 cp_parser_set_storage_class (decl_specs, sc_auto);
7166 break;
7167 case RID_REGISTER:
7168 /* Consume the token. */
7169 cp_lexer_consume_token (parser->lexer);
7170 cp_parser_set_storage_class (decl_specs, sc_register);
7171 break;
7172 case RID_STATIC:
7173 /* Consume the token. */
7174 cp_lexer_consume_token (parser->lexer);
7175 if (decl_specs->specs[(int) ds_thread])
7177 error ("%<__thread%> before %<static%>");
7178 decl_specs->specs[(int) ds_thread] = 0;
7180 cp_parser_set_storage_class (decl_specs, sc_static);
7181 break;
7182 case RID_EXTERN:
7183 /* Consume the token. */
7184 cp_lexer_consume_token (parser->lexer);
7185 if (decl_specs->specs[(int) ds_thread])
7187 error ("%<__thread%> before %<extern%>");
7188 decl_specs->specs[(int) ds_thread] = 0;
7190 cp_parser_set_storage_class (decl_specs, sc_extern);
7191 break;
7192 case RID_MUTABLE:
7193 /* Consume the token. */
7194 cp_lexer_consume_token (parser->lexer);
7195 cp_parser_set_storage_class (decl_specs, sc_mutable);
7196 break;
7197 case RID_THREAD:
7198 /* Consume the token. */
7199 cp_lexer_consume_token (parser->lexer);
7200 ++decl_specs->specs[(int) ds_thread];
7201 break;
7203 default:
7204 /* We did not yet find a decl-specifier yet. */
7205 found_decl_spec = false;
7206 break;
7209 /* Constructors are a special case. The `S' in `S()' is not a
7210 decl-specifier; it is the beginning of the declarator. */
7211 constructor_p
7212 = (!found_decl_spec
7213 && constructor_possible_p
7214 && (cp_parser_constructor_declarator_p
7215 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7217 /* If we don't have a DECL_SPEC yet, then we must be looking at
7218 a type-specifier. */
7219 if (!found_decl_spec && !constructor_p)
7221 int decl_spec_declares_class_or_enum;
7222 bool is_cv_qualifier;
7223 tree type_spec;
7225 type_spec
7226 = cp_parser_type_specifier (parser, flags,
7227 decl_specs,
7228 /*is_declaration=*/true,
7229 &decl_spec_declares_class_or_enum,
7230 &is_cv_qualifier);
7232 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7234 /* If this type-specifier referenced a user-defined type
7235 (a typedef, class-name, etc.), then we can't allow any
7236 more such type-specifiers henceforth.
7238 [dcl.spec]
7240 The longest sequence of decl-specifiers that could
7241 possibly be a type name is taken as the
7242 decl-specifier-seq of a declaration. The sequence shall
7243 be self-consistent as described below.
7245 [dcl.type]
7247 As a general rule, at most one type-specifier is allowed
7248 in the complete decl-specifier-seq of a declaration. The
7249 only exceptions are the following:
7251 -- const or volatile can be combined with any other
7252 type-specifier.
7254 -- signed or unsigned can be combined with char, long,
7255 short, or int.
7257 -- ..
7259 Example:
7261 typedef char* Pc;
7262 void g (const int Pc);
7264 Here, Pc is *not* part of the decl-specifier seq; it's
7265 the declarator. Therefore, once we see a type-specifier
7266 (other than a cv-qualifier), we forbid any additional
7267 user-defined types. We *do* still allow things like `int
7268 int' to be considered a decl-specifier-seq, and issue the
7269 error message later. */
7270 if (type_spec && !is_cv_qualifier)
7271 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7272 /* A constructor declarator cannot follow a type-specifier. */
7273 if (type_spec)
7275 constructor_possible_p = false;
7276 found_decl_spec = true;
7280 /* If we still do not have a DECL_SPEC, then there are no more
7281 decl-specifiers. */
7282 if (!found_decl_spec)
7283 break;
7285 decl_specs->any_specifiers_p = true;
7286 /* After we see one decl-specifier, further decl-specifiers are
7287 always optional. */
7288 flags |= CP_PARSER_FLAGS_OPTIONAL;
7291 /* Don't allow a friend specifier with a class definition. */
7292 if (decl_specs->specs[(int) ds_friend] != 0
7293 && (*declares_class_or_enum & 2))
7294 error ("class definition may not be declared a friend");
7297 /* Parse an (optional) storage-class-specifier.
7299 storage-class-specifier:
7300 auto
7301 register
7302 static
7303 extern
7304 mutable
7306 GNU Extension:
7308 storage-class-specifier:
7309 thread
7311 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7313 static tree
7314 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7316 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7318 case RID_AUTO:
7319 case RID_REGISTER:
7320 case RID_STATIC:
7321 case RID_EXTERN:
7322 case RID_MUTABLE:
7323 case RID_THREAD:
7324 /* Consume the token. */
7325 return cp_lexer_consume_token (parser->lexer)->value;
7327 default:
7328 return NULL_TREE;
7332 /* Parse an (optional) function-specifier.
7334 function-specifier:
7335 inline
7336 virtual
7337 explicit
7339 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7340 Updates DECL_SPECS, if it is non-NULL. */
7342 static tree
7343 cp_parser_function_specifier_opt (cp_parser* parser,
7344 cp_decl_specifier_seq *decl_specs)
7346 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7348 case RID_INLINE:
7349 if (decl_specs)
7350 ++decl_specs->specs[(int) ds_inline];
7351 break;
7353 case RID_VIRTUAL:
7354 if (decl_specs)
7355 ++decl_specs->specs[(int) ds_virtual];
7356 break;
7358 case RID_EXPLICIT:
7359 if (decl_specs)
7360 ++decl_specs->specs[(int) ds_explicit];
7361 break;
7363 default:
7364 return NULL_TREE;
7367 /* Consume the token. */
7368 return cp_lexer_consume_token (parser->lexer)->value;
7371 /* Parse a linkage-specification.
7373 linkage-specification:
7374 extern string-literal { declaration-seq [opt] }
7375 extern string-literal declaration */
7377 static void
7378 cp_parser_linkage_specification (cp_parser* parser)
7380 tree linkage;
7382 /* Look for the `extern' keyword. */
7383 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7385 /* Look for the string-literal. */
7386 linkage = cp_parser_string_literal (parser, false, false);
7388 /* Transform the literal into an identifier. If the literal is a
7389 wide-character string, or contains embedded NULs, then we can't
7390 handle it as the user wants. */
7391 if (strlen (TREE_STRING_POINTER (linkage))
7392 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7394 cp_parser_error (parser, "invalid linkage-specification");
7395 /* Assume C++ linkage. */
7396 linkage = lang_name_cplusplus;
7398 else
7399 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7401 /* We're now using the new linkage. */
7402 push_lang_context (linkage);
7404 /* If the next token is a `{', then we're using the first
7405 production. */
7406 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7408 /* Consume the `{' token. */
7409 cp_lexer_consume_token (parser->lexer);
7410 /* Parse the declarations. */
7411 cp_parser_declaration_seq_opt (parser);
7412 /* Look for the closing `}'. */
7413 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7415 /* Otherwise, there's just one declaration. */
7416 else
7418 bool saved_in_unbraced_linkage_specification_p;
7420 saved_in_unbraced_linkage_specification_p
7421 = parser->in_unbraced_linkage_specification_p;
7422 parser->in_unbraced_linkage_specification_p = true;
7423 have_extern_spec = true;
7424 cp_parser_declaration (parser);
7425 have_extern_spec = false;
7426 parser->in_unbraced_linkage_specification_p
7427 = saved_in_unbraced_linkage_specification_p;
7430 /* We're done with the linkage-specification. */
7431 pop_lang_context ();
7434 /* Special member functions [gram.special] */
7436 /* Parse a conversion-function-id.
7438 conversion-function-id:
7439 operator conversion-type-id
7441 Returns an IDENTIFIER_NODE representing the operator. */
7443 static tree
7444 cp_parser_conversion_function_id (cp_parser* parser)
7446 tree type;
7447 tree saved_scope;
7448 tree saved_qualifying_scope;
7449 tree saved_object_scope;
7450 bool pop_p = false;
7452 /* Look for the `operator' token. */
7453 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7454 return error_mark_node;
7455 /* When we parse the conversion-type-id, the current scope will be
7456 reset. However, we need that information in able to look up the
7457 conversion function later, so we save it here. */
7458 saved_scope = parser->scope;
7459 saved_qualifying_scope = parser->qualifying_scope;
7460 saved_object_scope = parser->object_scope;
7461 /* We must enter the scope of the class so that the names of
7462 entities declared within the class are available in the
7463 conversion-type-id. For example, consider:
7465 struct S {
7466 typedef int I;
7467 operator I();
7470 S::operator I() { ... }
7472 In order to see that `I' is a type-name in the definition, we
7473 must be in the scope of `S'. */
7474 if (saved_scope)
7475 pop_p = push_scope (saved_scope);
7476 /* Parse the conversion-type-id. */
7477 type = cp_parser_conversion_type_id (parser);
7478 /* Leave the scope of the class, if any. */
7479 if (pop_p)
7480 pop_scope (saved_scope);
7481 /* Restore the saved scope. */
7482 parser->scope = saved_scope;
7483 parser->qualifying_scope = saved_qualifying_scope;
7484 parser->object_scope = saved_object_scope;
7485 /* If the TYPE is invalid, indicate failure. */
7486 if (type == error_mark_node)
7487 return error_mark_node;
7488 return mangle_conv_op_name_for_type (type);
7491 /* Parse a conversion-type-id:
7493 conversion-type-id:
7494 type-specifier-seq conversion-declarator [opt]
7496 Returns the TYPE specified. */
7498 static tree
7499 cp_parser_conversion_type_id (cp_parser* parser)
7501 tree attributes;
7502 cp_decl_specifier_seq type_specifiers;
7503 cp_declarator *declarator;
7504 tree type_specified;
7506 /* Parse the attributes. */
7507 attributes = cp_parser_attributes_opt (parser);
7508 /* Parse the type-specifiers. */
7509 cp_parser_type_specifier_seq (parser, &type_specifiers);
7510 /* If that didn't work, stop. */
7511 if (type_specifiers.type == error_mark_node)
7512 return error_mark_node;
7513 /* Parse the conversion-declarator. */
7514 declarator = cp_parser_conversion_declarator_opt (parser);
7516 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7517 /*initialized=*/0, &attributes);
7518 if (attributes)
7519 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7520 return type_specified;
7523 /* Parse an (optional) conversion-declarator.
7525 conversion-declarator:
7526 ptr-operator conversion-declarator [opt]
7530 static cp_declarator *
7531 cp_parser_conversion_declarator_opt (cp_parser* parser)
7533 enum tree_code code;
7534 tree class_type;
7535 cp_cv_quals cv_quals;
7537 /* We don't know if there's a ptr-operator next, or not. */
7538 cp_parser_parse_tentatively (parser);
7539 /* Try the ptr-operator. */
7540 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7541 /* If it worked, look for more conversion-declarators. */
7542 if (cp_parser_parse_definitely (parser))
7544 cp_declarator *declarator;
7546 /* Parse another optional declarator. */
7547 declarator = cp_parser_conversion_declarator_opt (parser);
7549 /* Create the representation of the declarator. */
7550 if (class_type)
7551 declarator = make_ptrmem_declarator (cv_quals, class_type,
7552 declarator);
7553 else if (code == INDIRECT_REF)
7554 declarator = make_pointer_declarator (cv_quals, declarator);
7555 else
7556 declarator = make_reference_declarator (cv_quals, declarator);
7558 return declarator;
7561 return NULL;
7564 /* Parse an (optional) ctor-initializer.
7566 ctor-initializer:
7567 : mem-initializer-list
7569 Returns TRUE iff the ctor-initializer was actually present. */
7571 static bool
7572 cp_parser_ctor_initializer_opt (cp_parser* parser)
7574 /* If the next token is not a `:', then there is no
7575 ctor-initializer. */
7576 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7578 /* Do default initialization of any bases and members. */
7579 if (DECL_CONSTRUCTOR_P (current_function_decl))
7580 finish_mem_initializers (NULL_TREE);
7582 return false;
7585 /* Consume the `:' token. */
7586 cp_lexer_consume_token (parser->lexer);
7587 /* And the mem-initializer-list. */
7588 cp_parser_mem_initializer_list (parser);
7590 return true;
7593 /* Parse a mem-initializer-list.
7595 mem-initializer-list:
7596 mem-initializer
7597 mem-initializer , mem-initializer-list */
7599 static void
7600 cp_parser_mem_initializer_list (cp_parser* parser)
7602 tree mem_initializer_list = NULL_TREE;
7604 /* Let the semantic analysis code know that we are starting the
7605 mem-initializer-list. */
7606 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7607 error ("only constructors take base initializers");
7609 /* Loop through the list. */
7610 while (true)
7612 tree mem_initializer;
7614 /* Parse the mem-initializer. */
7615 mem_initializer = cp_parser_mem_initializer (parser);
7616 /* Add it to the list, unless it was erroneous. */
7617 if (mem_initializer)
7619 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7620 mem_initializer_list = mem_initializer;
7622 /* If the next token is not a `,', we're done. */
7623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7624 break;
7625 /* Consume the `,' token. */
7626 cp_lexer_consume_token (parser->lexer);
7629 /* Perform semantic analysis. */
7630 if (DECL_CONSTRUCTOR_P (current_function_decl))
7631 finish_mem_initializers (mem_initializer_list);
7634 /* Parse a mem-initializer.
7636 mem-initializer:
7637 mem-initializer-id ( expression-list [opt] )
7639 GNU extension:
7641 mem-initializer:
7642 ( expression-list [opt] )
7644 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7645 class) or FIELD_DECL (for a non-static data member) to initialize;
7646 the TREE_VALUE is the expression-list. */
7648 static tree
7649 cp_parser_mem_initializer (cp_parser* parser)
7651 tree mem_initializer_id;
7652 tree expression_list;
7653 tree member;
7655 /* Find out what is being initialized. */
7656 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7658 pedwarn ("anachronistic old-style base class initializer");
7659 mem_initializer_id = NULL_TREE;
7661 else
7662 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7663 member = expand_member_init (mem_initializer_id);
7664 if (member && !DECL_P (member))
7665 in_base_initializer = 1;
7667 expression_list
7668 = cp_parser_parenthesized_expression_list (parser, false,
7669 /*non_constant_p=*/NULL);
7670 if (!expression_list)
7671 expression_list = void_type_node;
7673 in_base_initializer = 0;
7675 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7678 /* Parse a mem-initializer-id.
7680 mem-initializer-id:
7681 :: [opt] nested-name-specifier [opt] class-name
7682 identifier
7684 Returns a TYPE indicating the class to be initializer for the first
7685 production. Returns an IDENTIFIER_NODE indicating the data member
7686 to be initialized for the second production. */
7688 static tree
7689 cp_parser_mem_initializer_id (cp_parser* parser)
7691 bool global_scope_p;
7692 bool nested_name_specifier_p;
7693 bool template_p = false;
7694 tree id;
7696 /* `typename' is not allowed in this context ([temp.res]). */
7697 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7699 error ("keyword %<typename%> not allowed in this context (a qualified "
7700 "member initializer is implicitly a type)");
7701 cp_lexer_consume_token (parser->lexer);
7703 /* Look for the optional `::' operator. */
7704 global_scope_p
7705 = (cp_parser_global_scope_opt (parser,
7706 /*current_scope_valid_p=*/false)
7707 != NULL_TREE);
7708 /* Look for the optional nested-name-specifier. The simplest way to
7709 implement:
7711 [temp.res]
7713 The keyword `typename' is not permitted in a base-specifier or
7714 mem-initializer; in these contexts a qualified name that
7715 depends on a template-parameter is implicitly assumed to be a
7716 type name.
7718 is to assume that we have seen the `typename' keyword at this
7719 point. */
7720 nested_name_specifier_p
7721 = (cp_parser_nested_name_specifier_opt (parser,
7722 /*typename_keyword_p=*/true,
7723 /*check_dependency_p=*/true,
7724 /*type_p=*/true,
7725 /*is_declaration=*/true)
7726 != NULL_TREE);
7727 if (nested_name_specifier_p)
7728 template_p = cp_parser_optional_template_keyword (parser);
7729 /* If there is a `::' operator or a nested-name-specifier, then we
7730 are definitely looking for a class-name. */
7731 if (global_scope_p || nested_name_specifier_p)
7732 return cp_parser_class_name (parser,
7733 /*typename_keyword_p=*/true,
7734 /*template_keyword_p=*/template_p,
7735 /*type_p=*/false,
7736 /*check_dependency_p=*/true,
7737 /*class_head_p=*/false,
7738 /*is_declaration=*/true);
7739 /* Otherwise, we could also be looking for an ordinary identifier. */
7740 cp_parser_parse_tentatively (parser);
7741 /* Try a class-name. */
7742 id = cp_parser_class_name (parser,
7743 /*typename_keyword_p=*/true,
7744 /*template_keyword_p=*/false,
7745 /*type_p=*/false,
7746 /*check_dependency_p=*/true,
7747 /*class_head_p=*/false,
7748 /*is_declaration=*/true);
7749 /* If we found one, we're done. */
7750 if (cp_parser_parse_definitely (parser))
7751 return id;
7752 /* Otherwise, look for an ordinary identifier. */
7753 return cp_parser_identifier (parser);
7756 /* Overloading [gram.over] */
7758 /* Parse an operator-function-id.
7760 operator-function-id:
7761 operator operator
7763 Returns an IDENTIFIER_NODE for the operator which is a
7764 human-readable spelling of the identifier, e.g., `operator +'. */
7766 static tree
7767 cp_parser_operator_function_id (cp_parser* parser)
7769 /* Look for the `operator' keyword. */
7770 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7771 return error_mark_node;
7772 /* And then the name of the operator itself. */
7773 return cp_parser_operator (parser);
7776 /* Parse an operator.
7778 operator:
7779 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7780 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7781 || ++ -- , ->* -> () []
7783 GNU Extensions:
7785 operator:
7786 <? >? <?= >?=
7788 Returns an IDENTIFIER_NODE for the operator which is a
7789 human-readable spelling of the identifier, e.g., `operator +'. */
7791 static tree
7792 cp_parser_operator (cp_parser* parser)
7794 tree id = NULL_TREE;
7795 cp_token *token;
7797 /* Peek at the next token. */
7798 token = cp_lexer_peek_token (parser->lexer);
7799 /* Figure out which operator we have. */
7800 switch (token->type)
7802 case CPP_KEYWORD:
7804 enum tree_code op;
7806 /* The keyword should be either `new' or `delete'. */
7807 if (token->keyword == RID_NEW)
7808 op = NEW_EXPR;
7809 else if (token->keyword == RID_DELETE)
7810 op = DELETE_EXPR;
7811 else
7812 break;
7814 /* Consume the `new' or `delete' token. */
7815 cp_lexer_consume_token (parser->lexer);
7817 /* Peek at the next token. */
7818 token = cp_lexer_peek_token (parser->lexer);
7819 /* If it's a `[' token then this is the array variant of the
7820 operator. */
7821 if (token->type == CPP_OPEN_SQUARE)
7823 /* Consume the `[' token. */
7824 cp_lexer_consume_token (parser->lexer);
7825 /* Look for the `]' token. */
7826 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7827 id = ansi_opname (op == NEW_EXPR
7828 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7830 /* Otherwise, we have the non-array variant. */
7831 else
7832 id = ansi_opname (op);
7834 return id;
7837 case CPP_PLUS:
7838 id = ansi_opname (PLUS_EXPR);
7839 break;
7841 case CPP_MINUS:
7842 id = ansi_opname (MINUS_EXPR);
7843 break;
7845 case CPP_MULT:
7846 id = ansi_opname (MULT_EXPR);
7847 break;
7849 case CPP_DIV:
7850 id = ansi_opname (TRUNC_DIV_EXPR);
7851 break;
7853 case CPP_MOD:
7854 id = ansi_opname (TRUNC_MOD_EXPR);
7855 break;
7857 case CPP_XOR:
7858 id = ansi_opname (BIT_XOR_EXPR);
7859 break;
7861 case CPP_AND:
7862 id = ansi_opname (BIT_AND_EXPR);
7863 break;
7865 case CPP_OR:
7866 id = ansi_opname (BIT_IOR_EXPR);
7867 break;
7869 case CPP_COMPL:
7870 id = ansi_opname (BIT_NOT_EXPR);
7871 break;
7873 case CPP_NOT:
7874 id = ansi_opname (TRUTH_NOT_EXPR);
7875 break;
7877 case CPP_EQ:
7878 id = ansi_assopname (NOP_EXPR);
7879 break;
7881 case CPP_LESS:
7882 id = ansi_opname (LT_EXPR);
7883 break;
7885 case CPP_GREATER:
7886 id = ansi_opname (GT_EXPR);
7887 break;
7889 case CPP_PLUS_EQ:
7890 id = ansi_assopname (PLUS_EXPR);
7891 break;
7893 case CPP_MINUS_EQ:
7894 id = ansi_assopname (MINUS_EXPR);
7895 break;
7897 case CPP_MULT_EQ:
7898 id = ansi_assopname (MULT_EXPR);
7899 break;
7901 case CPP_DIV_EQ:
7902 id = ansi_assopname (TRUNC_DIV_EXPR);
7903 break;
7905 case CPP_MOD_EQ:
7906 id = ansi_assopname (TRUNC_MOD_EXPR);
7907 break;
7909 case CPP_XOR_EQ:
7910 id = ansi_assopname (BIT_XOR_EXPR);
7911 break;
7913 case CPP_AND_EQ:
7914 id = ansi_assopname (BIT_AND_EXPR);
7915 break;
7917 case CPP_OR_EQ:
7918 id = ansi_assopname (BIT_IOR_EXPR);
7919 break;
7921 case CPP_LSHIFT:
7922 id = ansi_opname (LSHIFT_EXPR);
7923 break;
7925 case CPP_RSHIFT:
7926 id = ansi_opname (RSHIFT_EXPR);
7927 break;
7929 case CPP_LSHIFT_EQ:
7930 id = ansi_assopname (LSHIFT_EXPR);
7931 break;
7933 case CPP_RSHIFT_EQ:
7934 id = ansi_assopname (RSHIFT_EXPR);
7935 break;
7937 case CPP_EQ_EQ:
7938 id = ansi_opname (EQ_EXPR);
7939 break;
7941 case CPP_NOT_EQ:
7942 id = ansi_opname (NE_EXPR);
7943 break;
7945 case CPP_LESS_EQ:
7946 id = ansi_opname (LE_EXPR);
7947 break;
7949 case CPP_GREATER_EQ:
7950 id = ansi_opname (GE_EXPR);
7951 break;
7953 case CPP_AND_AND:
7954 id = ansi_opname (TRUTH_ANDIF_EXPR);
7955 break;
7957 case CPP_OR_OR:
7958 id = ansi_opname (TRUTH_ORIF_EXPR);
7959 break;
7961 case CPP_PLUS_PLUS:
7962 id = ansi_opname (POSTINCREMENT_EXPR);
7963 break;
7965 case CPP_MINUS_MINUS:
7966 id = ansi_opname (PREDECREMENT_EXPR);
7967 break;
7969 case CPP_COMMA:
7970 id = ansi_opname (COMPOUND_EXPR);
7971 break;
7973 case CPP_DEREF_STAR:
7974 id = ansi_opname (MEMBER_REF);
7975 break;
7977 case CPP_DEREF:
7978 id = ansi_opname (COMPONENT_REF);
7979 break;
7981 case CPP_OPEN_PAREN:
7982 /* Consume the `('. */
7983 cp_lexer_consume_token (parser->lexer);
7984 /* Look for the matching `)'. */
7985 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7986 return ansi_opname (CALL_EXPR);
7988 case CPP_OPEN_SQUARE:
7989 /* Consume the `['. */
7990 cp_lexer_consume_token (parser->lexer);
7991 /* Look for the matching `]'. */
7992 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7993 return ansi_opname (ARRAY_REF);
7995 /* Extensions. */
7996 case CPP_MIN:
7997 id = ansi_opname (MIN_EXPR);
7998 break;
8000 case CPP_MAX:
8001 id = ansi_opname (MAX_EXPR);
8002 break;
8004 case CPP_MIN_EQ:
8005 id = ansi_assopname (MIN_EXPR);
8006 break;
8008 case CPP_MAX_EQ:
8009 id = ansi_assopname (MAX_EXPR);
8010 break;
8012 default:
8013 /* Anything else is an error. */
8014 break;
8017 /* If we have selected an identifier, we need to consume the
8018 operator token. */
8019 if (id)
8020 cp_lexer_consume_token (parser->lexer);
8021 /* Otherwise, no valid operator name was present. */
8022 else
8024 cp_parser_error (parser, "expected operator");
8025 id = error_mark_node;
8028 return id;
8031 /* Parse a template-declaration.
8033 template-declaration:
8034 export [opt] template < template-parameter-list > declaration
8036 If MEMBER_P is TRUE, this template-declaration occurs within a
8037 class-specifier.
8039 The grammar rule given by the standard isn't correct. What
8040 is really meant is:
8042 template-declaration:
8043 export [opt] template-parameter-list-seq
8044 decl-specifier-seq [opt] init-declarator [opt] ;
8045 export [opt] template-parameter-list-seq
8046 function-definition
8048 template-parameter-list-seq:
8049 template-parameter-list-seq [opt]
8050 template < template-parameter-list > */
8052 static void
8053 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8055 /* Check for `export'. */
8056 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8058 /* Consume the `export' token. */
8059 cp_lexer_consume_token (parser->lexer);
8060 /* Warn that we do not support `export'. */
8061 warning ("keyword %<export%> not implemented, and will be ignored");
8064 cp_parser_template_declaration_after_export (parser, member_p);
8067 /* Parse a template-parameter-list.
8069 template-parameter-list:
8070 template-parameter
8071 template-parameter-list , template-parameter
8073 Returns a TREE_LIST. Each node represents a template parameter.
8074 The nodes are connected via their TREE_CHAINs. */
8076 static tree
8077 cp_parser_template_parameter_list (cp_parser* parser)
8079 tree parameter_list = NULL_TREE;
8081 while (true)
8083 tree parameter;
8084 cp_token *token;
8085 bool is_non_type;
8087 /* Parse the template-parameter. */
8088 parameter = cp_parser_template_parameter (parser, &is_non_type);
8089 /* Add it to the list. */
8090 parameter_list = process_template_parm (parameter_list,
8091 parameter,
8092 is_non_type);
8093 /* Peek at the next token. */
8094 token = cp_lexer_peek_token (parser->lexer);
8095 /* If it's not a `,', we're done. */
8096 if (token->type != CPP_COMMA)
8097 break;
8098 /* Otherwise, consume the `,' token. */
8099 cp_lexer_consume_token (parser->lexer);
8102 return parameter_list;
8105 /* Parse a template-parameter.
8107 template-parameter:
8108 type-parameter
8109 parameter-declaration
8111 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
8112 TREE_PURPOSE is the default value, if any. *IS_NON_TYPE is set to
8113 true iff this parameter is a non-type parameter. */
8115 static tree
8116 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8118 cp_token *token;
8119 cp_parameter_declarator *parameter_declarator;
8121 /* Assume it is a type parameter or a template parameter. */
8122 *is_non_type = false;
8123 /* Peek at the next token. */
8124 token = cp_lexer_peek_token (parser->lexer);
8125 /* If it is `class' or `template', we have a type-parameter. */
8126 if (token->keyword == RID_TEMPLATE)
8127 return cp_parser_type_parameter (parser);
8128 /* If it is `class' or `typename' we do not know yet whether it is a
8129 type parameter or a non-type parameter. Consider:
8131 template <typename T, typename T::X X> ...
8135 template <class C, class D*> ...
8137 Here, the first parameter is a type parameter, and the second is
8138 a non-type parameter. We can tell by looking at the token after
8139 the identifier -- if it is a `,', `=', or `>' then we have a type
8140 parameter. */
8141 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8143 /* Peek at the token after `class' or `typename'. */
8144 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8145 /* If it's an identifier, skip it. */
8146 if (token->type == CPP_NAME)
8147 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8148 /* Now, see if the token looks like the end of a template
8149 parameter. */
8150 if (token->type == CPP_COMMA
8151 || token->type == CPP_EQ
8152 || token->type == CPP_GREATER)
8153 return cp_parser_type_parameter (parser);
8156 /* Otherwise, it is a non-type parameter.
8158 [temp.param]
8160 When parsing a default template-argument for a non-type
8161 template-parameter, the first non-nested `>' is taken as the end
8162 of the template parameter-list rather than a greater-than
8163 operator. */
8164 *is_non_type = true;
8165 parameter_declarator
8166 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8167 /*parenthesized_p=*/NULL);
8168 return (build_tree_list
8169 (parameter_declarator->default_argument,
8170 grokdeclarator (parameter_declarator->declarator,
8171 &parameter_declarator->decl_specifiers,
8172 PARM, /*initialized=*/0,
8173 /*attrlist=*/NULL)));
8176 /* Parse a type-parameter.
8178 type-parameter:
8179 class identifier [opt]
8180 class identifier [opt] = type-id
8181 typename identifier [opt]
8182 typename identifier [opt] = type-id
8183 template < template-parameter-list > class identifier [opt]
8184 template < template-parameter-list > class identifier [opt]
8185 = id-expression
8187 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8188 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8189 the declaration of the parameter. */
8191 static tree
8192 cp_parser_type_parameter (cp_parser* parser)
8194 cp_token *token;
8195 tree parameter;
8197 /* Look for a keyword to tell us what kind of parameter this is. */
8198 token = cp_parser_require (parser, CPP_KEYWORD,
8199 "`class', `typename', or `template'");
8200 if (!token)
8201 return error_mark_node;
8203 switch (token->keyword)
8205 case RID_CLASS:
8206 case RID_TYPENAME:
8208 tree identifier;
8209 tree default_argument;
8211 /* If the next token is an identifier, then it names the
8212 parameter. */
8213 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8214 identifier = cp_parser_identifier (parser);
8215 else
8216 identifier = NULL_TREE;
8218 /* Create the parameter. */
8219 parameter = finish_template_type_parm (class_type_node, identifier);
8221 /* If the next token is an `=', we have a default argument. */
8222 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8224 /* Consume the `=' token. */
8225 cp_lexer_consume_token (parser->lexer);
8226 /* Parse the default-argument. */
8227 default_argument = cp_parser_type_id (parser);
8229 else
8230 default_argument = NULL_TREE;
8232 /* Create the combined representation of the parameter and the
8233 default argument. */
8234 parameter = build_tree_list (default_argument, parameter);
8236 break;
8238 case RID_TEMPLATE:
8240 tree parameter_list;
8241 tree identifier;
8242 tree default_argument;
8244 /* Look for the `<'. */
8245 cp_parser_require (parser, CPP_LESS, "`<'");
8246 /* Parse the template-parameter-list. */
8247 begin_template_parm_list ();
8248 parameter_list
8249 = cp_parser_template_parameter_list (parser);
8250 parameter_list = end_template_parm_list (parameter_list);
8251 /* Look for the `>'. */
8252 cp_parser_require (parser, CPP_GREATER, "`>'");
8253 /* Look for the `class' keyword. */
8254 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8255 /* If the next token is an `=', then there is a
8256 default-argument. If the next token is a `>', we are at
8257 the end of the parameter-list. If the next token is a `,',
8258 then we are at the end of this parameter. */
8259 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8260 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8261 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8262 identifier = cp_parser_identifier (parser);
8263 else
8264 identifier = NULL_TREE;
8265 /* Create the template parameter. */
8266 parameter = finish_template_template_parm (class_type_node,
8267 identifier);
8269 /* If the next token is an `=', then there is a
8270 default-argument. */
8271 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8273 bool is_template;
8275 /* Consume the `='. */
8276 cp_lexer_consume_token (parser->lexer);
8277 /* Parse the id-expression. */
8278 default_argument
8279 = cp_parser_id_expression (parser,
8280 /*template_keyword_p=*/false,
8281 /*check_dependency_p=*/true,
8282 /*template_p=*/&is_template,
8283 /*declarator_p=*/false);
8284 if (TREE_CODE (default_argument) == TYPE_DECL)
8285 /* If the id-expression was a template-id that refers to
8286 a template-class, we already have the declaration here,
8287 so no further lookup is needed. */
8289 else
8290 /* Look up the name. */
8291 default_argument
8292 = cp_parser_lookup_name (parser, default_argument,
8293 /*is_type=*/false,
8294 /*is_template=*/is_template,
8295 /*is_namespace=*/false,
8296 /*check_dependency=*/true,
8297 /*ambiguous_p=*/NULL);
8298 /* See if the default argument is valid. */
8299 default_argument
8300 = check_template_template_default_arg (default_argument);
8302 else
8303 default_argument = NULL_TREE;
8305 /* Create the combined representation of the parameter and the
8306 default argument. */
8307 parameter = build_tree_list (default_argument, parameter);
8309 break;
8311 default:
8312 /* Anything else is an error. */
8313 cp_parser_error (parser,
8314 "expected %<class%>, %<typename%>, or %<template%>");
8315 parameter = error_mark_node;
8318 return parameter;
8321 /* Parse a template-id.
8323 template-id:
8324 template-name < template-argument-list [opt] >
8326 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8327 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8328 returned. Otherwise, if the template-name names a function, or set
8329 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8330 names a class, returns a TYPE_DECL for the specialization.
8332 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8333 uninstantiated templates. */
8335 static tree
8336 cp_parser_template_id (cp_parser *parser,
8337 bool template_keyword_p,
8338 bool check_dependency_p,
8339 bool is_declaration)
8341 tree template;
8342 tree arguments;
8343 tree template_id;
8344 ptrdiff_t start_of_id;
8345 tree access_check = NULL_TREE;
8346 cp_token *next_token, *next_token_2;
8347 bool is_identifier;
8349 /* If the next token corresponds to a template-id, there is no need
8350 to reparse it. */
8351 next_token = cp_lexer_peek_token (parser->lexer);
8352 if (next_token->type == CPP_TEMPLATE_ID)
8354 tree value;
8355 tree check;
8357 /* Get the stored value. */
8358 value = cp_lexer_consume_token (parser->lexer)->value;
8359 /* Perform any access checks that were deferred. */
8360 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8361 perform_or_defer_access_check (TREE_PURPOSE (check),
8362 TREE_VALUE (check));
8363 /* Return the stored value. */
8364 return TREE_VALUE (value);
8367 /* Avoid performing name lookup if there is no possibility of
8368 finding a template-id. */
8369 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8370 || (next_token->type == CPP_NAME
8371 && !cp_parser_nth_token_starts_template_argument_list_p
8372 (parser, 2)))
8374 cp_parser_error (parser, "expected template-id");
8375 return error_mark_node;
8378 /* Remember where the template-id starts. */
8379 if (cp_parser_parsing_tentatively (parser)
8380 && !cp_parser_committed_to_tentative_parse (parser))
8382 next_token = cp_lexer_peek_token (parser->lexer);
8383 start_of_id = cp_lexer_token_difference (parser->lexer,
8384 parser->lexer->buffer,
8385 next_token);
8387 else
8388 start_of_id = -1;
8390 push_deferring_access_checks (dk_deferred);
8392 /* Parse the template-name. */
8393 is_identifier = false;
8394 template = cp_parser_template_name (parser, template_keyword_p,
8395 check_dependency_p,
8396 is_declaration,
8397 &is_identifier);
8398 if (template == error_mark_node || is_identifier)
8400 pop_deferring_access_checks ();
8401 return template;
8404 /* If we find the sequence `[:' after a template-name, it's probably
8405 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8406 parse correctly the argument list. */
8407 next_token = cp_lexer_peek_token (parser->lexer);
8408 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8409 if (next_token->type == CPP_OPEN_SQUARE
8410 && next_token->flags & DIGRAPH
8411 && next_token_2->type == CPP_COLON
8412 && !(next_token_2->flags & PREV_WHITE))
8414 cp_parser_parse_tentatively (parser);
8415 /* Change `:' into `::'. */
8416 next_token_2->type = CPP_SCOPE;
8417 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8418 CPP_LESS. */
8419 cp_lexer_consume_token (parser->lexer);
8420 /* Parse the arguments. */
8421 arguments = cp_parser_enclosed_template_argument_list (parser);
8422 if (!cp_parser_parse_definitely (parser))
8424 /* If we couldn't parse an argument list, then we revert our changes
8425 and return simply an error. Maybe this is not a template-id
8426 after all. */
8427 next_token_2->type = CPP_COLON;
8428 cp_parser_error (parser, "expected %<<%>");
8429 pop_deferring_access_checks ();
8430 return error_mark_node;
8432 /* Otherwise, emit an error about the invalid digraph, but continue
8433 parsing because we got our argument list. */
8434 pedwarn ("%<<::%> cannot begin a template-argument list");
8435 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8436 "between %<<%> and %<::%>");
8437 if (!flag_permissive)
8439 static bool hint;
8440 if (!hint)
8442 inform ("(if you use -fpermissive G++ will accept your code)");
8443 hint = true;
8447 else
8449 /* Look for the `<' that starts the template-argument-list. */
8450 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8452 pop_deferring_access_checks ();
8453 return error_mark_node;
8455 /* Parse the arguments. */
8456 arguments = cp_parser_enclosed_template_argument_list (parser);
8459 /* Build a representation of the specialization. */
8460 if (TREE_CODE (template) == IDENTIFIER_NODE)
8461 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8462 else if (DECL_CLASS_TEMPLATE_P (template)
8463 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8464 template_id
8465 = finish_template_type (template, arguments,
8466 cp_lexer_next_token_is (parser->lexer,
8467 CPP_SCOPE));
8468 else
8470 /* If it's not a class-template or a template-template, it should be
8471 a function-template. */
8472 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8473 || TREE_CODE (template) == OVERLOAD
8474 || BASELINK_P (template)));
8476 template_id = lookup_template_function (template, arguments);
8479 /* Retrieve any deferred checks. Do not pop this access checks yet
8480 so the memory will not be reclaimed during token replacing below. */
8481 access_check = get_deferred_access_checks ();
8483 /* If parsing tentatively, replace the sequence of tokens that makes
8484 up the template-id with a CPP_TEMPLATE_ID token. That way,
8485 should we re-parse the token stream, we will not have to repeat
8486 the effort required to do the parse, nor will we issue duplicate
8487 error messages about problems during instantiation of the
8488 template. */
8489 if (start_of_id >= 0)
8491 cp_token *token;
8493 /* Find the token that corresponds to the start of the
8494 template-id. */
8495 token = cp_lexer_advance_token (parser->lexer,
8496 parser->lexer->buffer,
8497 start_of_id);
8499 /* Reset the contents of the START_OF_ID token. */
8500 token->type = CPP_TEMPLATE_ID;
8501 token->value = build_tree_list (access_check, template_id);
8502 token->keyword = RID_MAX;
8503 /* Purge all subsequent tokens. */
8504 cp_lexer_purge_tokens_after (parser->lexer, token);
8507 pop_deferring_access_checks ();
8508 return template_id;
8511 /* Parse a template-name.
8513 template-name:
8514 identifier
8516 The standard should actually say:
8518 template-name:
8519 identifier
8520 operator-function-id
8522 A defect report has been filed about this issue.
8524 A conversion-function-id cannot be a template name because they cannot
8525 be part of a template-id. In fact, looking at this code:
8527 a.operator K<int>()
8529 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8530 It is impossible to call a templated conversion-function-id with an
8531 explicit argument list, since the only allowed template parameter is
8532 the type to which it is converting.
8534 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8535 `template' keyword, in a construction like:
8537 T::template f<3>()
8539 In that case `f' is taken to be a template-name, even though there
8540 is no way of knowing for sure.
8542 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8543 name refers to a set of overloaded functions, at least one of which
8544 is a template, or an IDENTIFIER_NODE with the name of the template,
8545 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8546 names are looked up inside uninstantiated templates. */
8548 static tree
8549 cp_parser_template_name (cp_parser* parser,
8550 bool template_keyword_p,
8551 bool check_dependency_p,
8552 bool is_declaration,
8553 bool *is_identifier)
8555 tree identifier;
8556 tree decl;
8557 tree fns;
8559 /* If the next token is `operator', then we have either an
8560 operator-function-id or a conversion-function-id. */
8561 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8563 /* We don't know whether we're looking at an
8564 operator-function-id or a conversion-function-id. */
8565 cp_parser_parse_tentatively (parser);
8566 /* Try an operator-function-id. */
8567 identifier = cp_parser_operator_function_id (parser);
8568 /* If that didn't work, try a conversion-function-id. */
8569 if (!cp_parser_parse_definitely (parser))
8571 cp_parser_error (parser, "expected template-name");
8572 return error_mark_node;
8575 /* Look for the identifier. */
8576 else
8577 identifier = cp_parser_identifier (parser);
8579 /* If we didn't find an identifier, we don't have a template-id. */
8580 if (identifier == error_mark_node)
8581 return error_mark_node;
8583 /* If the name immediately followed the `template' keyword, then it
8584 is a template-name. However, if the next token is not `<', then
8585 we do not treat it as a template-name, since it is not being used
8586 as part of a template-id. This enables us to handle constructs
8587 like:
8589 template <typename T> struct S { S(); };
8590 template <typename T> S<T>::S();
8592 correctly. We would treat `S' as a template -- if it were `S<T>'
8593 -- but we do not if there is no `<'. */
8595 if (processing_template_decl
8596 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8598 /* In a declaration, in a dependent context, we pretend that the
8599 "template" keyword was present in order to improve error
8600 recovery. For example, given:
8602 template <typename T> void f(T::X<int>);
8604 we want to treat "X<int>" as a template-id. */
8605 if (is_declaration
8606 && !template_keyword_p
8607 && parser->scope && TYPE_P (parser->scope)
8608 && check_dependency_p
8609 && dependent_type_p (parser->scope)
8610 /* Do not do this for dtors (or ctors), since they never
8611 need the template keyword before their name. */
8612 && !constructor_name_p (identifier, parser->scope))
8614 ptrdiff_t start;
8615 cp_token* token;
8616 /* Explain what went wrong. */
8617 error ("non-template %qD used as template", identifier);
8618 inform ("use %<%T::template %D%> to indicate that it is a template",
8619 parser->scope, identifier);
8620 /* If parsing tentatively, find the location of the "<"
8621 token. */
8622 if (cp_parser_parsing_tentatively (parser)
8623 && !cp_parser_committed_to_tentative_parse (parser))
8625 cp_parser_simulate_error (parser);
8626 token = cp_lexer_peek_token (parser->lexer);
8627 token = cp_lexer_prev_token (parser->lexer, token);
8628 start = cp_lexer_token_difference (parser->lexer,
8629 parser->lexer->buffer,
8630 token);
8632 else
8633 start = -1;
8634 /* Parse the template arguments so that we can issue error
8635 messages about them. */
8636 cp_lexer_consume_token (parser->lexer);
8637 cp_parser_enclosed_template_argument_list (parser);
8638 /* Skip tokens until we find a good place from which to
8639 continue parsing. */
8640 cp_parser_skip_to_closing_parenthesis (parser,
8641 /*recovering=*/true,
8642 /*or_comma=*/true,
8643 /*consume_paren=*/false);
8644 /* If parsing tentatively, permanently remove the
8645 template argument list. That will prevent duplicate
8646 error messages from being issued about the missing
8647 "template" keyword. */
8648 if (start >= 0)
8650 token = cp_lexer_advance_token (parser->lexer,
8651 parser->lexer->buffer,
8652 start);
8653 cp_lexer_purge_tokens_after (parser->lexer, token);
8655 if (is_identifier)
8656 *is_identifier = true;
8657 return identifier;
8660 /* If the "template" keyword is present, then there is generally
8661 no point in doing name-lookup, so we just return IDENTIFIER.
8662 But, if the qualifying scope is non-dependent then we can
8663 (and must) do name-lookup normally. */
8664 if (template_keyword_p
8665 && (!parser->scope
8666 || (TYPE_P (parser->scope)
8667 && dependent_type_p (parser->scope))))
8668 return identifier;
8671 /* Look up the name. */
8672 decl = cp_parser_lookup_name (parser, identifier,
8673 /*is_type=*/false,
8674 /*is_template=*/false,
8675 /*is_namespace=*/false,
8676 check_dependency_p,
8677 /*ambiguous_p=*/NULL);
8678 decl = maybe_get_template_decl_from_type_decl (decl);
8680 /* If DECL is a template, then the name was a template-name. */
8681 if (TREE_CODE (decl) == TEMPLATE_DECL)
8683 else
8685 /* The standard does not explicitly indicate whether a name that
8686 names a set of overloaded declarations, some of which are
8687 templates, is a template-name. However, such a name should
8688 be a template-name; otherwise, there is no way to form a
8689 template-id for the overloaded templates. */
8690 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8691 if (TREE_CODE (fns) == OVERLOAD)
8693 tree fn;
8695 for (fn = fns; fn; fn = OVL_NEXT (fn))
8696 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8697 break;
8699 else
8701 /* Otherwise, the name does not name a template. */
8702 cp_parser_error (parser, "expected template-name");
8703 return error_mark_node;
8707 /* If DECL is dependent, and refers to a function, then just return
8708 its name; we will look it up again during template instantiation. */
8709 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8711 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8712 if (TYPE_P (scope) && dependent_type_p (scope))
8713 return identifier;
8716 return decl;
8719 /* Parse a template-argument-list.
8721 template-argument-list:
8722 template-argument
8723 template-argument-list , template-argument
8725 Returns a TREE_VEC containing the arguments. */
8727 static tree
8728 cp_parser_template_argument_list (cp_parser* parser)
8730 tree fixed_args[10];
8731 unsigned n_args = 0;
8732 unsigned alloced = 10;
8733 tree *arg_ary = fixed_args;
8734 tree vec;
8735 bool saved_in_template_argument_list_p;
8737 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8738 parser->in_template_argument_list_p = true;
8741 tree argument;
8743 if (n_args)
8744 /* Consume the comma. */
8745 cp_lexer_consume_token (parser->lexer);
8747 /* Parse the template-argument. */
8748 argument = cp_parser_template_argument (parser);
8749 if (n_args == alloced)
8751 alloced *= 2;
8753 if (arg_ary == fixed_args)
8755 arg_ary = xmalloc (sizeof (tree) * alloced);
8756 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8758 else
8759 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8761 arg_ary[n_args++] = argument;
8763 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8765 vec = make_tree_vec (n_args);
8767 while (n_args--)
8768 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8770 if (arg_ary != fixed_args)
8771 free (arg_ary);
8772 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8773 return vec;
8776 /* Parse a template-argument.
8778 template-argument:
8779 assignment-expression
8780 type-id
8781 id-expression
8783 The representation is that of an assignment-expression, type-id, or
8784 id-expression -- except that the qualified id-expression is
8785 evaluated, so that the value returned is either a DECL or an
8786 OVERLOAD.
8788 Although the standard says "assignment-expression", it forbids
8789 throw-expressions or assignments in the template argument.
8790 Therefore, we use "conditional-expression" instead. */
8792 static tree
8793 cp_parser_template_argument (cp_parser* parser)
8795 tree argument;
8796 bool template_p;
8797 bool address_p;
8798 bool maybe_type_id = false;
8799 cp_token *token;
8800 cp_id_kind idk;
8801 tree qualifying_class;
8803 /* There's really no way to know what we're looking at, so we just
8804 try each alternative in order.
8806 [temp.arg]
8808 In a template-argument, an ambiguity between a type-id and an
8809 expression is resolved to a type-id, regardless of the form of
8810 the corresponding template-parameter.
8812 Therefore, we try a type-id first. */
8813 cp_parser_parse_tentatively (parser);
8814 argument = cp_parser_type_id (parser);
8815 /* If there was no error parsing the type-id but the next token is a '>>',
8816 we probably found a typo for '> >'. But there are type-id which are
8817 also valid expressions. For instance:
8819 struct X { int operator >> (int); };
8820 template <int V> struct Foo {};
8821 Foo<X () >> 5> r;
8823 Here 'X()' is a valid type-id of a function type, but the user just
8824 wanted to write the expression "X() >> 5". Thus, we remember that we
8825 found a valid type-id, but we still try to parse the argument as an
8826 expression to see what happens. */
8827 if (!cp_parser_error_occurred (parser)
8828 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8830 maybe_type_id = true;
8831 cp_parser_abort_tentative_parse (parser);
8833 else
8835 /* If the next token isn't a `,' or a `>', then this argument wasn't
8836 really finished. This means that the argument is not a valid
8837 type-id. */
8838 if (!cp_parser_next_token_ends_template_argument_p (parser))
8839 cp_parser_error (parser, "expected template-argument");
8840 /* If that worked, we're done. */
8841 if (cp_parser_parse_definitely (parser))
8842 return argument;
8844 /* We're still not sure what the argument will be. */
8845 cp_parser_parse_tentatively (parser);
8846 /* Try a template. */
8847 argument = cp_parser_id_expression (parser,
8848 /*template_keyword_p=*/false,
8849 /*check_dependency_p=*/true,
8850 &template_p,
8851 /*declarator_p=*/false);
8852 /* If the next token isn't a `,' or a `>', then this argument wasn't
8853 really finished. */
8854 if (!cp_parser_next_token_ends_template_argument_p (parser))
8855 cp_parser_error (parser, "expected template-argument");
8856 if (!cp_parser_error_occurred (parser))
8858 /* Figure out what is being referred to. If the id-expression
8859 was for a class template specialization, then we will have a
8860 TYPE_DECL at this point. There is no need to do name lookup
8861 at this point in that case. */
8862 if (TREE_CODE (argument) != TYPE_DECL)
8863 argument = cp_parser_lookup_name (parser, argument,
8864 /*is_type=*/false,
8865 /*is_template=*/template_p,
8866 /*is_namespace=*/false,
8867 /*check_dependency=*/true,
8868 /*ambiguous_p=*/NULL);
8869 if (TREE_CODE (argument) != TEMPLATE_DECL
8870 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8871 cp_parser_error (parser, "expected template-name");
8873 if (cp_parser_parse_definitely (parser))
8874 return argument;
8875 /* It must be a non-type argument. There permitted cases are given
8876 in [temp.arg.nontype]:
8878 -- an integral constant-expression of integral or enumeration
8879 type; or
8881 -- the name of a non-type template-parameter; or
8883 -- the name of an object or function with external linkage...
8885 -- the address of an object or function with external linkage...
8887 -- a pointer to member... */
8888 /* Look for a non-type template parameter. */
8889 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8891 cp_parser_parse_tentatively (parser);
8892 argument = cp_parser_primary_expression (parser,
8893 &idk,
8894 &qualifying_class);
8895 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8896 || !cp_parser_next_token_ends_template_argument_p (parser))
8897 cp_parser_simulate_error (parser);
8898 if (cp_parser_parse_definitely (parser))
8899 return argument;
8901 /* If the next token is "&", the argument must be the address of an
8902 object or function with external linkage. */
8903 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8904 if (address_p)
8905 cp_lexer_consume_token (parser->lexer);
8906 /* See if we might have an id-expression. */
8907 token = cp_lexer_peek_token (parser->lexer);
8908 if (token->type == CPP_NAME
8909 || token->keyword == RID_OPERATOR
8910 || token->type == CPP_SCOPE
8911 || token->type == CPP_TEMPLATE_ID
8912 || token->type == CPP_NESTED_NAME_SPECIFIER)
8914 cp_parser_parse_tentatively (parser);
8915 argument = cp_parser_primary_expression (parser,
8916 &idk,
8917 &qualifying_class);
8918 if (cp_parser_error_occurred (parser)
8919 || !cp_parser_next_token_ends_template_argument_p (parser))
8920 cp_parser_abort_tentative_parse (parser);
8921 else
8923 if (qualifying_class)
8924 argument = finish_qualified_id_expr (qualifying_class,
8925 argument,
8926 /*done=*/true,
8927 address_p);
8928 if (TREE_CODE (argument) == VAR_DECL)
8930 /* A variable without external linkage might still be a
8931 valid constant-expression, so no error is issued here
8932 if the external-linkage check fails. */
8933 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8934 cp_parser_simulate_error (parser);
8936 else if (is_overloaded_fn (argument))
8937 /* All overloaded functions are allowed; if the external
8938 linkage test does not pass, an error will be issued
8939 later. */
8941 else if (address_p
8942 && (TREE_CODE (argument) == OFFSET_REF
8943 || TREE_CODE (argument) == SCOPE_REF))
8944 /* A pointer-to-member. */
8946 else
8947 cp_parser_simulate_error (parser);
8949 if (cp_parser_parse_definitely (parser))
8951 if (address_p)
8952 argument = build_x_unary_op (ADDR_EXPR, argument);
8953 return argument;
8957 /* If the argument started with "&", there are no other valid
8958 alternatives at this point. */
8959 if (address_p)
8961 cp_parser_error (parser, "invalid non-type template argument");
8962 return error_mark_node;
8964 /* If the argument wasn't successfully parsed as a type-id followed
8965 by '>>', the argument can only be a constant expression now.
8966 Otherwise, we try parsing the constant-expression tentatively,
8967 because the argument could really be a type-id. */
8968 if (maybe_type_id)
8969 cp_parser_parse_tentatively (parser);
8970 argument = cp_parser_constant_expression (parser,
8971 /*allow_non_constant_p=*/false,
8972 /*non_constant_p=*/NULL);
8973 argument = fold_non_dependent_expr (argument);
8974 if (!maybe_type_id)
8975 return argument;
8976 if (!cp_parser_next_token_ends_template_argument_p (parser))
8977 cp_parser_error (parser, "expected template-argument");
8978 if (cp_parser_parse_definitely (parser))
8979 return argument;
8980 /* We did our best to parse the argument as a non type-id, but that
8981 was the only alternative that matched (albeit with a '>' after
8982 it). We can assume it's just a typo from the user, and a
8983 diagnostic will then be issued. */
8984 return cp_parser_type_id (parser);
8987 /* Parse an explicit-instantiation.
8989 explicit-instantiation:
8990 template declaration
8992 Although the standard says `declaration', what it really means is:
8994 explicit-instantiation:
8995 template decl-specifier-seq [opt] declarator [opt] ;
8997 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8998 supposed to be allowed. A defect report has been filed about this
8999 issue.
9001 GNU Extension:
9003 explicit-instantiation:
9004 storage-class-specifier template
9005 decl-specifier-seq [opt] declarator [opt] ;
9006 function-specifier template
9007 decl-specifier-seq [opt] declarator [opt] ; */
9009 static void
9010 cp_parser_explicit_instantiation (cp_parser* parser)
9012 int declares_class_or_enum;
9013 cp_decl_specifier_seq decl_specifiers;
9014 tree extension_specifier = NULL_TREE;
9016 /* Look for an (optional) storage-class-specifier or
9017 function-specifier. */
9018 if (cp_parser_allow_gnu_extensions_p (parser))
9020 extension_specifier
9021 = cp_parser_storage_class_specifier_opt (parser);
9022 if (!extension_specifier)
9023 extension_specifier
9024 = cp_parser_function_specifier_opt (parser,
9025 /*decl_specs=*/NULL);
9028 /* Look for the `template' keyword. */
9029 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9030 /* Let the front end know that we are processing an explicit
9031 instantiation. */
9032 begin_explicit_instantiation ();
9033 /* [temp.explicit] says that we are supposed to ignore access
9034 control while processing explicit instantiation directives. */
9035 push_deferring_access_checks (dk_no_check);
9036 /* Parse a decl-specifier-seq. */
9037 cp_parser_decl_specifier_seq (parser,
9038 CP_PARSER_FLAGS_OPTIONAL,
9039 &decl_specifiers,
9040 &declares_class_or_enum);
9041 /* If there was exactly one decl-specifier, and it declared a class,
9042 and there's no declarator, then we have an explicit type
9043 instantiation. */
9044 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9046 tree type;
9048 type = check_tag_decl (&decl_specifiers);
9049 /* Turn access control back on for names used during
9050 template instantiation. */
9051 pop_deferring_access_checks ();
9052 if (type)
9053 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9055 else
9057 cp_declarator *declarator;
9058 tree decl;
9060 /* Parse the declarator. */
9061 declarator
9062 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9063 /*ctor_dtor_or_conv_p=*/NULL,
9064 /*parenthesized_p=*/NULL);
9065 cp_parser_check_for_definition_in_return_type (declarator,
9066 declares_class_or_enum);
9067 if (declarator != cp_error_declarator)
9069 decl = grokdeclarator (declarator, &decl_specifiers,
9070 NORMAL, 0, NULL);
9071 /* Turn access control back on for names used during
9072 template instantiation. */
9073 pop_deferring_access_checks ();
9074 /* Do the explicit instantiation. */
9075 do_decl_instantiation (decl, extension_specifier);
9077 else
9079 pop_deferring_access_checks ();
9080 /* Skip the body of the explicit instantiation. */
9081 cp_parser_skip_to_end_of_statement (parser);
9084 /* We're done with the instantiation. */
9085 end_explicit_instantiation ();
9087 cp_parser_consume_semicolon_at_end_of_statement (parser);
9090 /* Parse an explicit-specialization.
9092 explicit-specialization:
9093 template < > declaration
9095 Although the standard says `declaration', what it really means is:
9097 explicit-specialization:
9098 template <> decl-specifier [opt] init-declarator [opt] ;
9099 template <> function-definition
9100 template <> explicit-specialization
9101 template <> template-declaration */
9103 static void
9104 cp_parser_explicit_specialization (cp_parser* parser)
9106 /* Look for the `template' keyword. */
9107 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9108 /* Look for the `<'. */
9109 cp_parser_require (parser, CPP_LESS, "`<'");
9110 /* Look for the `>'. */
9111 cp_parser_require (parser, CPP_GREATER, "`>'");
9112 /* We have processed another parameter list. */
9113 ++parser->num_template_parameter_lists;
9114 /* Let the front end know that we are beginning a specialization. */
9115 begin_specialization ();
9117 /* If the next keyword is `template', we need to figure out whether
9118 or not we're looking a template-declaration. */
9119 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9121 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9122 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9123 cp_parser_template_declaration_after_export (parser,
9124 /*member_p=*/false);
9125 else
9126 cp_parser_explicit_specialization (parser);
9128 else
9129 /* Parse the dependent declaration. */
9130 cp_parser_single_declaration (parser,
9131 /*member_p=*/false,
9132 /*friend_p=*/NULL);
9134 /* We're done with the specialization. */
9135 end_specialization ();
9136 /* We're done with this parameter list. */
9137 --parser->num_template_parameter_lists;
9140 /* Parse a type-specifier.
9142 type-specifier:
9143 simple-type-specifier
9144 class-specifier
9145 enum-specifier
9146 elaborated-type-specifier
9147 cv-qualifier
9149 GNU Extension:
9151 type-specifier:
9152 __complex__
9154 Returns a representation of the type-specifier. For a
9155 class-specifier, enum-specifier, or elaborated-type-specifier, a
9156 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9158 The parser flags FLAGS is used to control type-specifier parsing.
9160 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9161 in a decl-specifier-seq.
9163 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9164 class-specifier, enum-specifier, or elaborated-type-specifier, then
9165 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9166 if a type is declared; 2 if it is defined. Otherwise, it is set to
9167 zero.
9169 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9170 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9171 is set to FALSE. */
9173 static tree
9174 cp_parser_type_specifier (cp_parser* parser,
9175 cp_parser_flags flags,
9176 cp_decl_specifier_seq *decl_specs,
9177 bool is_declaration,
9178 int* declares_class_or_enum,
9179 bool* is_cv_qualifier)
9181 tree type_spec = NULL_TREE;
9182 cp_token *token;
9183 enum rid keyword;
9184 cp_decl_spec ds = ds_last;
9186 /* Assume this type-specifier does not declare a new type. */
9187 if (declares_class_or_enum)
9188 *declares_class_or_enum = 0;
9189 /* And that it does not specify a cv-qualifier. */
9190 if (is_cv_qualifier)
9191 *is_cv_qualifier = false;
9192 /* Peek at the next token. */
9193 token = cp_lexer_peek_token (parser->lexer);
9195 /* If we're looking at a keyword, we can use that to guide the
9196 production we choose. */
9197 keyword = token->keyword;
9198 switch (keyword)
9200 case RID_ENUM:
9201 /* 'enum' [identifier] '{' introduces an enum-specifier;
9202 'enum' <anything else> introduces an elaborated-type-specifier. */
9203 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9204 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9205 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9206 == CPP_OPEN_BRACE))
9208 type_spec = cp_parser_enum_specifier (parser);
9209 if (declares_class_or_enum)
9210 *declares_class_or_enum = 2;
9211 if (decl_specs)
9212 cp_parser_set_decl_spec_type (decl_specs,
9213 type_spec,
9214 /*user_defined_p=*/true);
9215 return type_spec;
9217 else
9218 goto elaborated_type_specifier;
9220 /* Any of these indicate either a class-specifier, or an
9221 elaborated-type-specifier. */
9222 case RID_CLASS:
9223 case RID_STRUCT:
9224 case RID_UNION:
9225 /* Parse tentatively so that we can back up if we don't find a
9226 class-specifier. */
9227 cp_parser_parse_tentatively (parser);
9228 /* Look for the class-specifier. */
9229 type_spec = cp_parser_class_specifier (parser);
9230 /* If that worked, we're done. */
9231 if (cp_parser_parse_definitely (parser))
9233 if (declares_class_or_enum)
9234 *declares_class_or_enum = 2;
9235 if (decl_specs)
9236 cp_parser_set_decl_spec_type (decl_specs,
9237 type_spec,
9238 /*user_defined_p=*/true);
9239 return type_spec;
9242 /* Fall through. */
9243 elaborated_type_specifier:
9244 /* We're declaring (not defining) a class or enum. */
9245 if (declares_class_or_enum)
9246 *declares_class_or_enum = 1;
9248 /* Fall through. */
9249 case RID_TYPENAME:
9250 /* Look for an elaborated-type-specifier. */
9251 type_spec
9252 = (cp_parser_elaborated_type_specifier
9253 (parser,
9254 decl_specs && decl_specs->specs[(int) ds_friend],
9255 is_declaration));
9256 if (decl_specs)
9257 cp_parser_set_decl_spec_type (decl_specs,
9258 type_spec,
9259 /*user_defined_p=*/true);
9260 return type_spec;
9262 case RID_CONST:
9263 ds = ds_const;
9264 if (is_cv_qualifier)
9265 *is_cv_qualifier = true;
9266 break;
9268 case RID_VOLATILE:
9269 ds = ds_volatile;
9270 if (is_cv_qualifier)
9271 *is_cv_qualifier = true;
9272 break;
9274 case RID_RESTRICT:
9275 ds = ds_restrict;
9276 if (is_cv_qualifier)
9277 *is_cv_qualifier = true;
9278 break;
9280 case RID_COMPLEX:
9281 /* The `__complex__' keyword is a GNU extension. */
9282 ds = ds_complex;
9283 break;
9285 default:
9286 break;
9289 /* Handle simple keywords. */
9290 if (ds != ds_last)
9292 if (decl_specs)
9294 ++decl_specs->specs[(int)ds];
9295 decl_specs->any_specifiers_p = true;
9297 return cp_lexer_consume_token (parser->lexer)->value;
9300 /* If we do not already have a type-specifier, assume we are looking
9301 at a simple-type-specifier. */
9302 type_spec = cp_parser_simple_type_specifier (parser,
9303 decl_specs,
9304 flags);
9306 /* If we didn't find a type-specifier, and a type-specifier was not
9307 optional in this context, issue an error message. */
9308 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9310 cp_parser_error (parser, "expected type specifier");
9311 return error_mark_node;
9314 return type_spec;
9317 /* Parse a simple-type-specifier.
9319 simple-type-specifier:
9320 :: [opt] nested-name-specifier [opt] type-name
9321 :: [opt] nested-name-specifier template template-id
9322 char
9323 wchar_t
9324 bool
9325 short
9327 long
9328 signed
9329 unsigned
9330 float
9331 double
9332 void
9334 GNU Extension:
9336 simple-type-specifier:
9337 __typeof__ unary-expression
9338 __typeof__ ( type-id )
9340 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9341 appropriately updated. */
9343 static tree
9344 cp_parser_simple_type_specifier (cp_parser* parser,
9345 cp_decl_specifier_seq *decl_specs,
9346 cp_parser_flags flags)
9348 tree type = NULL_TREE;
9349 cp_token *token;
9351 /* Peek at the next token. */
9352 token = cp_lexer_peek_token (parser->lexer);
9354 /* If we're looking at a keyword, things are easy. */
9355 switch (token->keyword)
9357 case RID_CHAR:
9358 if (decl_specs)
9359 decl_specs->explicit_char_p = true;
9360 type = char_type_node;
9361 break;
9362 case RID_WCHAR:
9363 type = wchar_type_node;
9364 break;
9365 case RID_BOOL:
9366 type = boolean_type_node;
9367 break;
9368 case RID_SHORT:
9369 if (decl_specs)
9370 ++decl_specs->specs[(int) ds_short];
9371 type = short_integer_type_node;
9372 break;
9373 case RID_INT:
9374 if (decl_specs)
9375 decl_specs->explicit_int_p = true;
9376 type = integer_type_node;
9377 break;
9378 case RID_LONG:
9379 if (decl_specs)
9380 ++decl_specs->specs[(int) ds_long];
9381 type = long_integer_type_node;
9382 break;
9383 case RID_SIGNED:
9384 if (decl_specs)
9385 ++decl_specs->specs[(int) ds_signed];
9386 type = integer_type_node;
9387 break;
9388 case RID_UNSIGNED:
9389 if (decl_specs)
9390 ++decl_specs->specs[(int) ds_unsigned];
9391 type = unsigned_type_node;
9392 break;
9393 case RID_FLOAT:
9394 type = float_type_node;
9395 break;
9396 case RID_DOUBLE:
9397 type = double_type_node;
9398 break;
9399 case RID_VOID:
9400 type = void_type_node;
9401 break;
9403 case RID_TYPEOF:
9404 /* Consume the `typeof' token. */
9405 cp_lexer_consume_token (parser->lexer);
9406 /* Parse the operand to `typeof'. */
9407 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9408 /* If it is not already a TYPE, take its type. */
9409 if (!TYPE_P (type))
9410 type = finish_typeof (type);
9412 if (decl_specs)
9413 cp_parser_set_decl_spec_type (decl_specs, type,
9414 /*user_defined_p=*/true);
9416 return type;
9418 default:
9419 break;
9422 /* If the type-specifier was for a built-in type, we're done. */
9423 if (type)
9425 tree id;
9427 /* Record the type. */
9428 if (decl_specs
9429 && (token->keyword != RID_SIGNED
9430 && token->keyword != RID_UNSIGNED
9431 && token->keyword != RID_SHORT
9432 && token->keyword != RID_LONG))
9433 cp_parser_set_decl_spec_type (decl_specs,
9434 type,
9435 /*user_defined=*/false);
9436 if (decl_specs)
9437 decl_specs->any_specifiers_p = true;
9439 /* Consume the token. */
9440 id = cp_lexer_consume_token (parser->lexer)->value;
9442 /* There is no valid C++ program where a non-template type is
9443 followed by a "<". That usually indicates that the user thought
9444 that the type was a template. */
9445 cp_parser_check_for_invalid_template_id (parser, type);
9447 return TYPE_NAME (type);
9450 /* The type-specifier must be a user-defined type. */
9451 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9453 bool qualified_p;
9454 bool global_p;
9456 /* Don't gobble tokens or issue error messages if this is an
9457 optional type-specifier. */
9458 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9459 cp_parser_parse_tentatively (parser);
9461 /* Look for the optional `::' operator. */
9462 global_p
9463 = (cp_parser_global_scope_opt (parser,
9464 /*current_scope_valid_p=*/false)
9465 != NULL_TREE);
9466 /* Look for the nested-name specifier. */
9467 qualified_p
9468 = (cp_parser_nested_name_specifier_opt (parser,
9469 /*typename_keyword_p=*/false,
9470 /*check_dependency_p=*/true,
9471 /*type_p=*/false,
9472 /*is_declaration=*/false)
9473 != NULL_TREE);
9474 /* If we have seen a nested-name-specifier, and the next token
9475 is `template', then we are using the template-id production. */
9476 if (parser->scope
9477 && cp_parser_optional_template_keyword (parser))
9479 /* Look for the template-id. */
9480 type = cp_parser_template_id (parser,
9481 /*template_keyword_p=*/true,
9482 /*check_dependency_p=*/true,
9483 /*is_declaration=*/false);
9484 /* If the template-id did not name a type, we are out of
9485 luck. */
9486 if (TREE_CODE (type) != TYPE_DECL)
9488 cp_parser_error (parser, "expected template-id for type");
9489 type = NULL_TREE;
9492 /* Otherwise, look for a type-name. */
9493 else
9494 type = cp_parser_type_name (parser);
9495 /* Keep track of all name-lookups performed in class scopes. */
9496 if (type
9497 && !global_p
9498 && !qualified_p
9499 && TREE_CODE (type) == TYPE_DECL
9500 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9501 maybe_note_name_used_in_class (DECL_NAME (type), type);
9502 /* If it didn't work out, we don't have a TYPE. */
9503 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9504 && !cp_parser_parse_definitely (parser))
9505 type = NULL_TREE;
9506 if (type && decl_specs)
9507 cp_parser_set_decl_spec_type (decl_specs, type,
9508 /*user_defined=*/true);
9511 /* If we didn't get a type-name, issue an error message. */
9512 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9514 cp_parser_error (parser, "expected type-name");
9515 return error_mark_node;
9518 /* There is no valid C++ program where a non-template type is
9519 followed by a "<". That usually indicates that the user thought
9520 that the type was a template. */
9521 if (type && type != error_mark_node)
9522 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9524 return type;
9527 /* Parse a type-name.
9529 type-name:
9530 class-name
9531 enum-name
9532 typedef-name
9534 enum-name:
9535 identifier
9537 typedef-name:
9538 identifier
9540 Returns a TYPE_DECL for the the type. */
9542 static tree
9543 cp_parser_type_name (cp_parser* parser)
9545 tree type_decl;
9546 tree identifier;
9548 /* We can't know yet whether it is a class-name or not. */
9549 cp_parser_parse_tentatively (parser);
9550 /* Try a class-name. */
9551 type_decl = cp_parser_class_name (parser,
9552 /*typename_keyword_p=*/false,
9553 /*template_keyword_p=*/false,
9554 /*type_p=*/false,
9555 /*check_dependency_p=*/true,
9556 /*class_head_p=*/false,
9557 /*is_declaration=*/false);
9558 /* If it's not a class-name, keep looking. */
9559 if (!cp_parser_parse_definitely (parser))
9561 /* It must be a typedef-name or an enum-name. */
9562 identifier = cp_parser_identifier (parser);
9563 if (identifier == error_mark_node)
9564 return error_mark_node;
9566 /* Look up the type-name. */
9567 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9568 /* Issue an error if we did not find a type-name. */
9569 if (TREE_CODE (type_decl) != TYPE_DECL)
9571 if (!cp_parser_simulate_error (parser))
9572 cp_parser_name_lookup_error (parser, identifier, type_decl,
9573 "is not a type");
9574 type_decl = error_mark_node;
9576 /* Remember that the name was used in the definition of the
9577 current class so that we can check later to see if the
9578 meaning would have been different after the class was
9579 entirely defined. */
9580 else if (type_decl != error_mark_node
9581 && !parser->scope)
9582 maybe_note_name_used_in_class (identifier, type_decl);
9585 return type_decl;
9589 /* Parse an elaborated-type-specifier. Note that the grammar given
9590 here incorporates the resolution to DR68.
9592 elaborated-type-specifier:
9593 class-key :: [opt] nested-name-specifier [opt] identifier
9594 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9595 enum :: [opt] nested-name-specifier [opt] identifier
9596 typename :: [opt] nested-name-specifier identifier
9597 typename :: [opt] nested-name-specifier template [opt]
9598 template-id
9600 GNU extension:
9602 elaborated-type-specifier:
9603 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9604 class-key attributes :: [opt] nested-name-specifier [opt]
9605 template [opt] template-id
9606 enum attributes :: [opt] nested-name-specifier [opt] identifier
9608 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9609 declared `friend'. If IS_DECLARATION is TRUE, then this
9610 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9611 something is being declared.
9613 Returns the TYPE specified. */
9615 static tree
9616 cp_parser_elaborated_type_specifier (cp_parser* parser,
9617 bool is_friend,
9618 bool is_declaration)
9620 enum tag_types tag_type;
9621 tree identifier;
9622 tree type = NULL_TREE;
9623 tree attributes = NULL_TREE;
9625 /* See if we're looking at the `enum' keyword. */
9626 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9628 /* Consume the `enum' token. */
9629 cp_lexer_consume_token (parser->lexer);
9630 /* Remember that it's an enumeration type. */
9631 tag_type = enum_type;
9632 /* Parse the attributes. */
9633 attributes = cp_parser_attributes_opt (parser);
9635 /* Or, it might be `typename'. */
9636 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9637 RID_TYPENAME))
9639 /* Consume the `typename' token. */
9640 cp_lexer_consume_token (parser->lexer);
9641 /* Remember that it's a `typename' type. */
9642 tag_type = typename_type;
9643 /* The `typename' keyword is only allowed in templates. */
9644 if (!processing_template_decl)
9645 pedwarn ("using %<typename%> outside of template");
9647 /* Otherwise it must be a class-key. */
9648 else
9650 tag_type = cp_parser_class_key (parser);
9651 if (tag_type == none_type)
9652 return error_mark_node;
9653 /* Parse the attributes. */
9654 attributes = cp_parser_attributes_opt (parser);
9657 /* Look for the `::' operator. */
9658 cp_parser_global_scope_opt (parser,
9659 /*current_scope_valid_p=*/false);
9660 /* Look for the nested-name-specifier. */
9661 if (tag_type == typename_type)
9663 if (cp_parser_nested_name_specifier (parser,
9664 /*typename_keyword_p=*/true,
9665 /*check_dependency_p=*/true,
9666 /*type_p=*/true,
9667 is_declaration)
9668 == error_mark_node)
9669 return error_mark_node;
9671 else
9672 /* Even though `typename' is not present, the proposed resolution
9673 to Core Issue 180 says that in `class A<T>::B', `B' should be
9674 considered a type-name, even if `A<T>' is dependent. */
9675 cp_parser_nested_name_specifier_opt (parser,
9676 /*typename_keyword_p=*/true,
9677 /*check_dependency_p=*/true,
9678 /*type_p=*/true,
9679 is_declaration);
9680 /* For everything but enumeration types, consider a template-id. */
9681 if (tag_type != enum_type)
9683 bool template_p = false;
9684 tree decl;
9686 /* Allow the `template' keyword. */
9687 template_p = cp_parser_optional_template_keyword (parser);
9688 /* If we didn't see `template', we don't know if there's a
9689 template-id or not. */
9690 if (!template_p)
9691 cp_parser_parse_tentatively (parser);
9692 /* Parse the template-id. */
9693 decl = cp_parser_template_id (parser, template_p,
9694 /*check_dependency_p=*/true,
9695 is_declaration);
9696 /* If we didn't find a template-id, look for an ordinary
9697 identifier. */
9698 if (!template_p && !cp_parser_parse_definitely (parser))
9700 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9701 in effect, then we must assume that, upon instantiation, the
9702 template will correspond to a class. */
9703 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9704 && tag_type == typename_type)
9705 type = make_typename_type (parser->scope, decl,
9706 /*complain=*/1);
9707 else
9708 type = TREE_TYPE (decl);
9711 /* For an enumeration type, consider only a plain identifier. */
9712 if (!type)
9714 identifier = cp_parser_identifier (parser);
9716 if (identifier == error_mark_node)
9718 parser->scope = NULL_TREE;
9719 return error_mark_node;
9722 /* For a `typename', we needn't call xref_tag. */
9723 if (tag_type == typename_type)
9724 return cp_parser_make_typename_type (parser, parser->scope,
9725 identifier);
9726 /* Look up a qualified name in the usual way. */
9727 if (parser->scope)
9729 tree decl;
9731 /* In an elaborated-type-specifier, names are assumed to name
9732 types, so we set IS_TYPE to TRUE when calling
9733 cp_parser_lookup_name. */
9734 decl = cp_parser_lookup_name (parser, identifier,
9735 /*is_type=*/true,
9736 /*is_template=*/false,
9737 /*is_namespace=*/false,
9738 /*check_dependency=*/true,
9739 /*ambiguous_p=*/NULL);
9741 /* If we are parsing friend declaration, DECL may be a
9742 TEMPLATE_DECL tree node here. However, we need to check
9743 whether this TEMPLATE_DECL results in valid code. Consider
9744 the following example:
9746 namespace N {
9747 template <class T> class C {};
9749 class X {
9750 template <class T> friend class N::C; // #1, valid code
9752 template <class T> class Y {
9753 friend class N::C; // #2, invalid code
9756 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9757 name lookup of `N::C'. We see that friend declaration must
9758 be template for the code to be valid. Note that
9759 processing_template_decl does not work here since it is
9760 always 1 for the above two cases. */
9762 decl = (cp_parser_maybe_treat_template_as_class
9763 (decl, /*tag_name_p=*/is_friend
9764 && parser->num_template_parameter_lists));
9766 if (TREE_CODE (decl) != TYPE_DECL)
9768 error ("expected type-name");
9769 return error_mark_node;
9772 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9773 check_elaborated_type_specifier
9774 (tag_type, decl,
9775 (parser->num_template_parameter_lists
9776 || DECL_SELF_REFERENCE_P (decl)));
9778 type = TREE_TYPE (decl);
9780 else
9782 /* An elaborated-type-specifier sometimes introduces a new type and
9783 sometimes names an existing type. Normally, the rule is that it
9784 introduces a new type only if there is not an existing type of
9785 the same name already in scope. For example, given:
9787 struct S {};
9788 void f() { struct S s; }
9790 the `struct S' in the body of `f' is the same `struct S' as in
9791 the global scope; the existing definition is used. However, if
9792 there were no global declaration, this would introduce a new
9793 local class named `S'.
9795 An exception to this rule applies to the following code:
9797 namespace N { struct S; }
9799 Here, the elaborated-type-specifier names a new type
9800 unconditionally; even if there is already an `S' in the
9801 containing scope this declaration names a new type.
9802 This exception only applies if the elaborated-type-specifier
9803 forms the complete declaration:
9805 [class.name]
9807 A declaration consisting solely of `class-key identifier ;' is
9808 either a redeclaration of the name in the current scope or a
9809 forward declaration of the identifier as a class name. It
9810 introduces the name into the current scope.
9812 We are in this situation precisely when the next token is a `;'.
9814 An exception to the exception is that a `friend' declaration does
9815 *not* name a new type; i.e., given:
9817 struct S { friend struct T; };
9819 `T' is not a new type in the scope of `S'.
9821 Also, `new struct S' or `sizeof (struct S)' never results in the
9822 definition of a new type; a new type can only be declared in a
9823 declaration context. */
9825 /* Warn about attributes. They are ignored. */
9826 if (attributes)
9827 warning ("type attributes are honored only at type definition");
9829 type = xref_tag (tag_type, identifier,
9830 (is_friend
9831 || !is_declaration
9832 || cp_lexer_next_token_is_not (parser->lexer,
9833 CPP_SEMICOLON)),
9834 parser->num_template_parameter_lists);
9837 if (tag_type != enum_type)
9838 cp_parser_check_class_key (tag_type, type);
9840 /* A "<" cannot follow an elaborated type specifier. If that
9841 happens, the user was probably trying to form a template-id. */
9842 cp_parser_check_for_invalid_template_id (parser, type);
9844 return type;
9847 /* Parse an enum-specifier.
9849 enum-specifier:
9850 enum identifier [opt] { enumerator-list [opt] }
9852 Returns an ENUM_TYPE representing the enumeration. */
9854 static tree
9855 cp_parser_enum_specifier (cp_parser* parser)
9857 tree identifier;
9858 tree type;
9860 /* Caller guarantees that the current token is 'enum', an identifier
9861 possibly follows, and the token after that is an opening brace.
9862 If we don't have an identifier, fabricate an anonymous name for
9863 the enumeration being defined. */
9864 cp_lexer_consume_token (parser->lexer);
9866 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9867 identifier = cp_parser_identifier (parser);
9868 else
9869 identifier = make_anon_name ();
9871 /* Issue an error message if type-definitions are forbidden here. */
9872 cp_parser_check_type_definition (parser);
9874 /* Create the new type. We do this before consuming the opening brace
9875 so the enum will be recorded as being on the line of its tag (or the
9876 'enum' keyword, if there is no tag). */
9877 type = start_enum (identifier);
9879 /* Consume the opening brace. */
9880 cp_lexer_consume_token (parser->lexer);
9882 /* If the next token is not '}', then there are some enumerators. */
9883 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9884 cp_parser_enumerator_list (parser, type);
9886 /* Consume the final '}'. */
9887 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9889 /* Finish up the enumeration. */
9890 finish_enum (type);
9892 return type;
9895 /* Parse an enumerator-list. The enumerators all have the indicated
9896 TYPE.
9898 enumerator-list:
9899 enumerator-definition
9900 enumerator-list , enumerator-definition */
9902 static void
9903 cp_parser_enumerator_list (cp_parser* parser, tree type)
9905 while (true)
9907 /* Parse an enumerator-definition. */
9908 cp_parser_enumerator_definition (parser, type);
9910 /* If the next token is not a ',', we've reached the end of
9911 the list. */
9912 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9913 break;
9914 /* Otherwise, consume the `,' and keep going. */
9915 cp_lexer_consume_token (parser->lexer);
9916 /* If the next token is a `}', there is a trailing comma. */
9917 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9919 if (pedantic && !in_system_header)
9920 pedwarn ("comma at end of enumerator list");
9921 break;
9926 /* Parse an enumerator-definition. The enumerator has the indicated
9927 TYPE.
9929 enumerator-definition:
9930 enumerator
9931 enumerator = constant-expression
9933 enumerator:
9934 identifier */
9936 static void
9937 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9939 tree identifier;
9940 tree value;
9942 /* Look for the identifier. */
9943 identifier = cp_parser_identifier (parser);
9944 if (identifier == error_mark_node)
9945 return;
9947 /* If the next token is an '=', then there is an explicit value. */
9948 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9950 /* Consume the `=' token. */
9951 cp_lexer_consume_token (parser->lexer);
9952 /* Parse the value. */
9953 value = cp_parser_constant_expression (parser,
9954 /*allow_non_constant_p=*/false,
9955 NULL);
9957 else
9958 value = NULL_TREE;
9960 /* Create the enumerator. */
9961 build_enumerator (identifier, value, type);
9964 /* Parse a namespace-name.
9966 namespace-name:
9967 original-namespace-name
9968 namespace-alias
9970 Returns the NAMESPACE_DECL for the namespace. */
9972 static tree
9973 cp_parser_namespace_name (cp_parser* parser)
9975 tree identifier;
9976 tree namespace_decl;
9978 /* Get the name of the namespace. */
9979 identifier = cp_parser_identifier (parser);
9980 if (identifier == error_mark_node)
9981 return error_mark_node;
9983 /* Look up the identifier in the currently active scope. Look only
9984 for namespaces, due to:
9986 [basic.lookup.udir]
9988 When looking up a namespace-name in a using-directive or alias
9989 definition, only namespace names are considered.
9991 And:
9993 [basic.lookup.qual]
9995 During the lookup of a name preceding the :: scope resolution
9996 operator, object, function, and enumerator names are ignored.
9998 (Note that cp_parser_class_or_namespace_name only calls this
9999 function if the token after the name is the scope resolution
10000 operator.) */
10001 namespace_decl = cp_parser_lookup_name (parser, identifier,
10002 /*is_type=*/false,
10003 /*is_template=*/false,
10004 /*is_namespace=*/true,
10005 /*check_dependency=*/true,
10006 /*ambiguous_p=*/NULL);
10007 /* If it's not a namespace, issue an error. */
10008 if (namespace_decl == error_mark_node
10009 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10011 cp_parser_error (parser, "expected namespace-name");
10012 namespace_decl = error_mark_node;
10015 return namespace_decl;
10018 /* Parse a namespace-definition.
10020 namespace-definition:
10021 named-namespace-definition
10022 unnamed-namespace-definition
10024 named-namespace-definition:
10025 original-namespace-definition
10026 extension-namespace-definition
10028 original-namespace-definition:
10029 namespace identifier { namespace-body }
10031 extension-namespace-definition:
10032 namespace original-namespace-name { namespace-body }
10034 unnamed-namespace-definition:
10035 namespace { namespace-body } */
10037 static void
10038 cp_parser_namespace_definition (cp_parser* parser)
10040 tree identifier;
10042 /* Look for the `namespace' keyword. */
10043 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10045 /* Get the name of the namespace. We do not attempt to distinguish
10046 between an original-namespace-definition and an
10047 extension-namespace-definition at this point. The semantic
10048 analysis routines are responsible for that. */
10049 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10050 identifier = cp_parser_identifier (parser);
10051 else
10052 identifier = NULL_TREE;
10054 /* Look for the `{' to start the namespace. */
10055 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10056 /* Start the namespace. */
10057 push_namespace (identifier);
10058 /* Parse the body of the namespace. */
10059 cp_parser_namespace_body (parser);
10060 /* Finish the namespace. */
10061 pop_namespace ();
10062 /* Look for the final `}'. */
10063 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10066 /* Parse a namespace-body.
10068 namespace-body:
10069 declaration-seq [opt] */
10071 static void
10072 cp_parser_namespace_body (cp_parser* parser)
10074 cp_parser_declaration_seq_opt (parser);
10077 /* Parse a namespace-alias-definition.
10079 namespace-alias-definition:
10080 namespace identifier = qualified-namespace-specifier ; */
10082 static void
10083 cp_parser_namespace_alias_definition (cp_parser* parser)
10085 tree identifier;
10086 tree namespace_specifier;
10088 /* Look for the `namespace' keyword. */
10089 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10090 /* Look for the identifier. */
10091 identifier = cp_parser_identifier (parser);
10092 if (identifier == error_mark_node)
10093 return;
10094 /* Look for the `=' token. */
10095 cp_parser_require (parser, CPP_EQ, "`='");
10096 /* Look for the qualified-namespace-specifier. */
10097 namespace_specifier
10098 = cp_parser_qualified_namespace_specifier (parser);
10099 /* Look for the `;' token. */
10100 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10102 /* Register the alias in the symbol table. */
10103 do_namespace_alias (identifier, namespace_specifier);
10106 /* Parse a qualified-namespace-specifier.
10108 qualified-namespace-specifier:
10109 :: [opt] nested-name-specifier [opt] namespace-name
10111 Returns a NAMESPACE_DECL corresponding to the specified
10112 namespace. */
10114 static tree
10115 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10117 /* Look for the optional `::'. */
10118 cp_parser_global_scope_opt (parser,
10119 /*current_scope_valid_p=*/false);
10121 /* Look for the optional nested-name-specifier. */
10122 cp_parser_nested_name_specifier_opt (parser,
10123 /*typename_keyword_p=*/false,
10124 /*check_dependency_p=*/true,
10125 /*type_p=*/false,
10126 /*is_declaration=*/true);
10128 return cp_parser_namespace_name (parser);
10131 /* Parse a using-declaration.
10133 using-declaration:
10134 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10135 using :: unqualified-id ; */
10137 static void
10138 cp_parser_using_declaration (cp_parser* parser)
10140 cp_token *token;
10141 bool typename_p = false;
10142 bool global_scope_p;
10143 tree decl;
10144 tree identifier;
10145 tree scope;
10146 tree qscope;
10148 /* Look for the `using' keyword. */
10149 cp_parser_require_keyword (parser, RID_USING, "`using'");
10151 /* Peek at the next token. */
10152 token = cp_lexer_peek_token (parser->lexer);
10153 /* See if it's `typename'. */
10154 if (token->keyword == RID_TYPENAME)
10156 /* Remember that we've seen it. */
10157 typename_p = true;
10158 /* Consume the `typename' token. */
10159 cp_lexer_consume_token (parser->lexer);
10162 /* Look for the optional global scope qualification. */
10163 global_scope_p
10164 = (cp_parser_global_scope_opt (parser,
10165 /*current_scope_valid_p=*/false)
10166 != NULL_TREE);
10168 /* If we saw `typename', or didn't see `::', then there must be a
10169 nested-name-specifier present. */
10170 if (typename_p || !global_scope_p)
10171 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10172 /*check_dependency_p=*/true,
10173 /*type_p=*/false,
10174 /*is_declaration=*/true);
10175 /* Otherwise, we could be in either of the two productions. In that
10176 case, treat the nested-name-specifier as optional. */
10177 else
10178 qscope = cp_parser_nested_name_specifier_opt (parser,
10179 /*typename_keyword_p=*/false,
10180 /*check_dependency_p=*/true,
10181 /*type_p=*/false,
10182 /*is_declaration=*/true);
10183 if (!qscope)
10184 qscope = global_namespace;
10186 /* Parse the unqualified-id. */
10187 identifier = cp_parser_unqualified_id (parser,
10188 /*template_keyword_p=*/false,
10189 /*check_dependency_p=*/true,
10190 /*declarator_p=*/true);
10192 /* The function we call to handle a using-declaration is different
10193 depending on what scope we are in. */
10194 if (identifier == error_mark_node)
10196 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10197 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10198 /* [namespace.udecl]
10200 A using declaration shall not name a template-id. */
10201 error ("a template-id may not appear in a using-declaration");
10202 else
10204 scope = current_scope ();
10205 if (scope && TYPE_P (scope))
10207 /* Create the USING_DECL. */
10208 decl = do_class_using_decl (build_nt (SCOPE_REF,
10209 parser->scope,
10210 identifier));
10211 /* Add it to the list of members in this class. */
10212 finish_member_declaration (decl);
10214 else
10216 decl = cp_parser_lookup_name_simple (parser, identifier);
10217 if (decl == error_mark_node)
10218 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10219 else if (scope)
10220 do_local_using_decl (decl, qscope, identifier);
10221 else
10222 do_toplevel_using_decl (decl, qscope, identifier);
10226 /* Look for the final `;'. */
10227 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10230 /* Parse a using-directive.
10232 using-directive:
10233 using namespace :: [opt] nested-name-specifier [opt]
10234 namespace-name ; */
10236 static void
10237 cp_parser_using_directive (cp_parser* parser)
10239 tree namespace_decl;
10240 tree attribs;
10242 /* Look for the `using' keyword. */
10243 cp_parser_require_keyword (parser, RID_USING, "`using'");
10244 /* And the `namespace' keyword. */
10245 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10246 /* Look for the optional `::' operator. */
10247 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10248 /* And the optional nested-name-specifier. */
10249 cp_parser_nested_name_specifier_opt (parser,
10250 /*typename_keyword_p=*/false,
10251 /*check_dependency_p=*/true,
10252 /*type_p=*/false,
10253 /*is_declaration=*/true);
10254 /* Get the namespace being used. */
10255 namespace_decl = cp_parser_namespace_name (parser);
10256 /* And any specified attributes. */
10257 attribs = cp_parser_attributes_opt (parser);
10258 /* Update the symbol table. */
10259 parse_using_directive (namespace_decl, attribs);
10260 /* Look for the final `;'. */
10261 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10264 /* Parse an asm-definition.
10266 asm-definition:
10267 asm ( string-literal ) ;
10269 GNU Extension:
10271 asm-definition:
10272 asm volatile [opt] ( string-literal ) ;
10273 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10274 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10275 : asm-operand-list [opt] ) ;
10276 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10277 : asm-operand-list [opt]
10278 : asm-operand-list [opt] ) ; */
10280 static void
10281 cp_parser_asm_definition (cp_parser* parser)
10283 tree string;
10284 tree outputs = NULL_TREE;
10285 tree inputs = NULL_TREE;
10286 tree clobbers = NULL_TREE;
10287 tree asm_stmt;
10288 bool volatile_p = false;
10289 bool extended_p = false;
10291 /* Look for the `asm' keyword. */
10292 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10293 /* See if the next token is `volatile'. */
10294 if (cp_parser_allow_gnu_extensions_p (parser)
10295 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10297 /* Remember that we saw the `volatile' keyword. */
10298 volatile_p = true;
10299 /* Consume the token. */
10300 cp_lexer_consume_token (parser->lexer);
10302 /* Look for the opening `('. */
10303 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10304 return;
10305 /* Look for the string. */
10306 string = cp_parser_string_literal (parser, false, false);
10307 if (string == error_mark_node)
10309 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10310 /*consume_paren=*/true);
10311 return;
10314 /* If we're allowing GNU extensions, check for the extended assembly
10315 syntax. Unfortunately, the `:' tokens need not be separated by
10316 a space in C, and so, for compatibility, we tolerate that here
10317 too. Doing that means that we have to treat the `::' operator as
10318 two `:' tokens. */
10319 if (cp_parser_allow_gnu_extensions_p (parser)
10320 && at_function_scope_p ()
10321 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10322 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10324 bool inputs_p = false;
10325 bool clobbers_p = false;
10327 /* The extended syntax was used. */
10328 extended_p = true;
10330 /* Look for outputs. */
10331 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10333 /* Consume the `:'. */
10334 cp_lexer_consume_token (parser->lexer);
10335 /* Parse the output-operands. */
10336 if (cp_lexer_next_token_is_not (parser->lexer,
10337 CPP_COLON)
10338 && cp_lexer_next_token_is_not (parser->lexer,
10339 CPP_SCOPE)
10340 && cp_lexer_next_token_is_not (parser->lexer,
10341 CPP_CLOSE_PAREN))
10342 outputs = cp_parser_asm_operand_list (parser);
10344 /* If the next token is `::', there are no outputs, and the
10345 next token is the beginning of the inputs. */
10346 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10347 /* The inputs are coming next. */
10348 inputs_p = true;
10350 /* Look for inputs. */
10351 if (inputs_p
10352 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10354 /* Consume the `:' or `::'. */
10355 cp_lexer_consume_token (parser->lexer);
10356 /* Parse the output-operands. */
10357 if (cp_lexer_next_token_is_not (parser->lexer,
10358 CPP_COLON)
10359 && cp_lexer_next_token_is_not (parser->lexer,
10360 CPP_CLOSE_PAREN))
10361 inputs = cp_parser_asm_operand_list (parser);
10363 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10364 /* The clobbers are coming next. */
10365 clobbers_p = true;
10367 /* Look for clobbers. */
10368 if (clobbers_p
10369 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10371 /* Consume the `:' or `::'. */
10372 cp_lexer_consume_token (parser->lexer);
10373 /* Parse the clobbers. */
10374 if (cp_lexer_next_token_is_not (parser->lexer,
10375 CPP_CLOSE_PAREN))
10376 clobbers = cp_parser_asm_clobber_list (parser);
10379 /* Look for the closing `)'. */
10380 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10381 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10382 /*consume_paren=*/true);
10383 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10385 /* Create the ASM_EXPR. */
10386 if (at_function_scope_p ())
10388 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10389 inputs, clobbers);
10390 /* If the extended syntax was not used, mark the ASM_EXPR. */
10391 if (!extended_p)
10392 ASM_INPUT_P (asm_stmt) = 1;
10394 else
10395 assemble_asm (string);
10398 /* Declarators [gram.dcl.decl] */
10400 /* Parse an init-declarator.
10402 init-declarator:
10403 declarator initializer [opt]
10405 GNU Extension:
10407 init-declarator:
10408 declarator asm-specification [opt] attributes [opt] initializer [opt]
10410 function-definition:
10411 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10412 function-body
10413 decl-specifier-seq [opt] declarator function-try-block
10415 GNU Extension:
10417 function-definition:
10418 __extension__ function-definition
10420 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10421 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10422 then this declarator appears in a class scope. The new DECL created
10423 by this declarator is returned.
10425 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10426 for a function-definition here as well. If the declarator is a
10427 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10428 be TRUE upon return. By that point, the function-definition will
10429 have been completely parsed.
10431 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10432 is FALSE. */
10434 static tree
10435 cp_parser_init_declarator (cp_parser* parser,
10436 cp_decl_specifier_seq *decl_specifiers,
10437 bool function_definition_allowed_p,
10438 bool member_p,
10439 int declares_class_or_enum,
10440 bool* function_definition_p)
10442 cp_token *token;
10443 cp_declarator *declarator;
10444 tree prefix_attributes;
10445 tree attributes;
10446 tree asm_specification;
10447 tree initializer;
10448 tree decl = NULL_TREE;
10449 tree scope;
10450 bool is_initialized;
10451 bool is_parenthesized_init;
10452 bool is_non_constant_init;
10453 int ctor_dtor_or_conv_p;
10454 bool friend_p;
10455 bool pop_p = false;
10457 /* Gather the attributes that were provided with the
10458 decl-specifiers. */
10459 prefix_attributes = decl_specifiers->attributes;
10461 /* Assume that this is not the declarator for a function
10462 definition. */
10463 if (function_definition_p)
10464 *function_definition_p = false;
10466 /* Defer access checks while parsing the declarator; we cannot know
10467 what names are accessible until we know what is being
10468 declared. */
10469 resume_deferring_access_checks ();
10471 /* Parse the declarator. */
10472 declarator
10473 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10474 &ctor_dtor_or_conv_p,
10475 /*parenthesized_p=*/NULL);
10476 /* Gather up the deferred checks. */
10477 stop_deferring_access_checks ();
10479 /* If the DECLARATOR was erroneous, there's no need to go
10480 further. */
10481 if (declarator == cp_error_declarator)
10482 return error_mark_node;
10484 cp_parser_check_for_definition_in_return_type (declarator,
10485 declares_class_or_enum);
10487 /* Figure out what scope the entity declared by the DECLARATOR is
10488 located in. `grokdeclarator' sometimes changes the scope, so
10489 we compute it now. */
10490 scope = get_scope_of_declarator (declarator);
10492 /* If we're allowing GNU extensions, look for an asm-specification
10493 and attributes. */
10494 if (cp_parser_allow_gnu_extensions_p (parser))
10496 /* Look for an asm-specification. */
10497 asm_specification = cp_parser_asm_specification_opt (parser);
10498 /* And attributes. */
10499 attributes = cp_parser_attributes_opt (parser);
10501 else
10503 asm_specification = NULL_TREE;
10504 attributes = NULL_TREE;
10507 /* Peek at the next token. */
10508 token = cp_lexer_peek_token (parser->lexer);
10509 /* Check to see if the token indicates the start of a
10510 function-definition. */
10511 if (cp_parser_token_starts_function_definition_p (token))
10513 if (!function_definition_allowed_p)
10515 /* If a function-definition should not appear here, issue an
10516 error message. */
10517 cp_parser_error (parser,
10518 "a function-definition is not allowed here");
10519 return error_mark_node;
10521 else
10523 /* Neither attributes nor an asm-specification are allowed
10524 on a function-definition. */
10525 if (asm_specification)
10526 error ("an asm-specification is not allowed on a function-definition");
10527 if (attributes)
10528 error ("attributes are not allowed on a function-definition");
10529 /* This is a function-definition. */
10530 *function_definition_p = true;
10532 /* Parse the function definition. */
10533 if (member_p)
10534 decl = cp_parser_save_member_function_body (parser,
10535 decl_specifiers,
10536 declarator,
10537 prefix_attributes);
10538 else
10539 decl
10540 = (cp_parser_function_definition_from_specifiers_and_declarator
10541 (parser, decl_specifiers, prefix_attributes, declarator));
10543 return decl;
10547 /* [dcl.dcl]
10549 Only in function declarations for constructors, destructors, and
10550 type conversions can the decl-specifier-seq be omitted.
10552 We explicitly postpone this check past the point where we handle
10553 function-definitions because we tolerate function-definitions
10554 that are missing their return types in some modes. */
10555 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10557 cp_parser_error (parser,
10558 "expected constructor, destructor, or type conversion");
10559 return error_mark_node;
10562 /* An `=' or an `(' indicates an initializer. */
10563 is_initialized = (token->type == CPP_EQ
10564 || token->type == CPP_OPEN_PAREN);
10565 /* If the init-declarator isn't initialized and isn't followed by a
10566 `,' or `;', it's not a valid init-declarator. */
10567 if (!is_initialized
10568 && token->type != CPP_COMMA
10569 && token->type != CPP_SEMICOLON)
10571 cp_parser_error (parser, "expected initializer");
10572 return error_mark_node;
10575 /* Because start_decl has side-effects, we should only call it if we
10576 know we're going ahead. By this point, we know that we cannot
10577 possibly be looking at any other construct. */
10578 cp_parser_commit_to_tentative_parse (parser);
10580 /* If the decl specifiers were bad, issue an error now that we're
10581 sure this was intended to be a declarator. Then continue
10582 declaring the variable(s), as int, to try to cut down on further
10583 errors. */
10584 if (decl_specifiers->any_specifiers_p
10585 && decl_specifiers->type == error_mark_node)
10587 cp_parser_error (parser, "invalid type in declaration");
10588 decl_specifiers->type = integer_type_node;
10591 /* Check to see whether or not this declaration is a friend. */
10592 friend_p = cp_parser_friend_p (decl_specifiers);
10594 /* Check that the number of template-parameter-lists is OK. */
10595 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10596 return error_mark_node;
10598 /* Enter the newly declared entry in the symbol table. If we're
10599 processing a declaration in a class-specifier, we wait until
10600 after processing the initializer. */
10601 if (!member_p)
10603 if (parser->in_unbraced_linkage_specification_p)
10605 decl_specifiers->storage_class = sc_extern;
10606 have_extern_spec = false;
10608 decl = start_decl (declarator, decl_specifiers,
10609 is_initialized, attributes, prefix_attributes,
10610 &pop_p);
10612 else if (scope)
10613 /* Enter the SCOPE. That way unqualified names appearing in the
10614 initializer will be looked up in SCOPE. */
10615 pop_p = push_scope (scope);
10617 /* Perform deferred access control checks, now that we know in which
10618 SCOPE the declared entity resides. */
10619 if (!member_p && decl)
10621 tree saved_current_function_decl = NULL_TREE;
10623 /* If the entity being declared is a function, pretend that we
10624 are in its scope. If it is a `friend', it may have access to
10625 things that would not otherwise be accessible. */
10626 if (TREE_CODE (decl) == FUNCTION_DECL)
10628 saved_current_function_decl = current_function_decl;
10629 current_function_decl = decl;
10632 /* Perform the access control checks for the declarator and the
10633 the decl-specifiers. */
10634 perform_deferred_access_checks ();
10636 /* Restore the saved value. */
10637 if (TREE_CODE (decl) == FUNCTION_DECL)
10638 current_function_decl = saved_current_function_decl;
10641 /* Parse the initializer. */
10642 if (is_initialized)
10643 initializer = cp_parser_initializer (parser,
10644 &is_parenthesized_init,
10645 &is_non_constant_init);
10646 else
10648 initializer = NULL_TREE;
10649 is_parenthesized_init = false;
10650 is_non_constant_init = true;
10653 /* The old parser allows attributes to appear after a parenthesized
10654 initializer. Mark Mitchell proposed removing this functionality
10655 on the GCC mailing lists on 2002-08-13. This parser accepts the
10656 attributes -- but ignores them. */
10657 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10658 if (cp_parser_attributes_opt (parser))
10659 warning ("attributes after parenthesized initializer ignored");
10661 /* For an in-class declaration, use `grokfield' to create the
10662 declaration. */
10663 if (member_p)
10665 if (pop_p)
10666 pop_scope (scope);
10667 decl = grokfield (declarator, decl_specifiers,
10668 initializer, /*asmspec=*/NULL_TREE,
10669 /*attributes=*/NULL_TREE);
10670 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10671 cp_parser_save_default_args (parser, decl);
10674 /* Finish processing the declaration. But, skip friend
10675 declarations. */
10676 if (!friend_p && decl && decl != error_mark_node)
10678 cp_finish_decl (decl,
10679 initializer,
10680 asm_specification,
10681 /* If the initializer is in parentheses, then this is
10682 a direct-initialization, which means that an
10683 `explicit' constructor is OK. Otherwise, an
10684 `explicit' constructor cannot be used. */
10685 ((is_parenthesized_init || !is_initialized)
10686 ? 0 : LOOKUP_ONLYCONVERTING));
10687 if (pop_p)
10688 pop_scope (DECL_CONTEXT (decl));
10691 /* Remember whether or not variables were initialized by
10692 constant-expressions. */
10693 if (decl && TREE_CODE (decl) == VAR_DECL
10694 && is_initialized && !is_non_constant_init)
10695 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10697 return decl;
10700 /* Parse a declarator.
10702 declarator:
10703 direct-declarator
10704 ptr-operator declarator
10706 abstract-declarator:
10707 ptr-operator abstract-declarator [opt]
10708 direct-abstract-declarator
10710 GNU Extensions:
10712 declarator:
10713 attributes [opt] direct-declarator
10714 attributes [opt] ptr-operator declarator
10716 abstract-declarator:
10717 attributes [opt] ptr-operator abstract-declarator [opt]
10718 attributes [opt] direct-abstract-declarator
10720 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10721 detect constructor, destructor or conversion operators. It is set
10722 to -1 if the declarator is a name, and +1 if it is a
10723 function. Otherwise it is set to zero. Usually you just want to
10724 test for >0, but internally the negative value is used.
10726 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10727 a decl-specifier-seq unless it declares a constructor, destructor,
10728 or conversion. It might seem that we could check this condition in
10729 semantic analysis, rather than parsing, but that makes it difficult
10730 to handle something like `f()'. We want to notice that there are
10731 no decl-specifiers, and therefore realize that this is an
10732 expression, not a declaration.)
10734 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10735 the declarator is a direct-declarator of the form "(...)". */
10737 static cp_declarator *
10738 cp_parser_declarator (cp_parser* parser,
10739 cp_parser_declarator_kind dcl_kind,
10740 int* ctor_dtor_or_conv_p,
10741 bool* parenthesized_p)
10743 cp_token *token;
10744 cp_declarator *declarator;
10745 enum tree_code code;
10746 cp_cv_quals cv_quals;
10747 tree class_type;
10748 tree attributes = NULL_TREE;
10750 /* Assume this is not a constructor, destructor, or type-conversion
10751 operator. */
10752 if (ctor_dtor_or_conv_p)
10753 *ctor_dtor_or_conv_p = 0;
10755 if (cp_parser_allow_gnu_extensions_p (parser))
10756 attributes = cp_parser_attributes_opt (parser);
10758 /* Peek at the next token. */
10759 token = cp_lexer_peek_token (parser->lexer);
10761 /* Check for the ptr-operator production. */
10762 cp_parser_parse_tentatively (parser);
10763 /* Parse the ptr-operator. */
10764 code = cp_parser_ptr_operator (parser,
10765 &class_type,
10766 &cv_quals);
10767 /* If that worked, then we have a ptr-operator. */
10768 if (cp_parser_parse_definitely (parser))
10770 /* If a ptr-operator was found, then this declarator was not
10771 parenthesized. */
10772 if (parenthesized_p)
10773 *parenthesized_p = true;
10774 /* The dependent declarator is optional if we are parsing an
10775 abstract-declarator. */
10776 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10777 cp_parser_parse_tentatively (parser);
10779 /* Parse the dependent declarator. */
10780 declarator = cp_parser_declarator (parser, dcl_kind,
10781 /*ctor_dtor_or_conv_p=*/NULL,
10782 /*parenthesized_p=*/NULL);
10784 /* If we are parsing an abstract-declarator, we must handle the
10785 case where the dependent declarator is absent. */
10786 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10787 && !cp_parser_parse_definitely (parser))
10788 declarator = NULL;
10790 /* Build the representation of the ptr-operator. */
10791 if (class_type)
10792 declarator = make_ptrmem_declarator (cv_quals,
10793 class_type,
10794 declarator);
10795 else if (code == INDIRECT_REF)
10796 declarator = make_pointer_declarator (cv_quals, declarator);
10797 else
10798 declarator = make_reference_declarator (cv_quals, declarator);
10800 /* Everything else is a direct-declarator. */
10801 else
10803 if (parenthesized_p)
10804 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10805 CPP_OPEN_PAREN);
10806 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10807 ctor_dtor_or_conv_p);
10810 if (attributes && declarator != cp_error_declarator)
10811 declarator->attributes = attributes;
10813 return declarator;
10816 /* Parse a direct-declarator or direct-abstract-declarator.
10818 direct-declarator:
10819 declarator-id
10820 direct-declarator ( parameter-declaration-clause )
10821 cv-qualifier-seq [opt]
10822 exception-specification [opt]
10823 direct-declarator [ constant-expression [opt] ]
10824 ( declarator )
10826 direct-abstract-declarator:
10827 direct-abstract-declarator [opt]
10828 ( parameter-declaration-clause )
10829 cv-qualifier-seq [opt]
10830 exception-specification [opt]
10831 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10832 ( abstract-declarator )
10834 Returns a representation of the declarator. DCL_KIND is
10835 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10836 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10837 we are parsing a direct-declarator. It is
10838 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10839 of ambiguity we prefer an abstract declarator, as per
10840 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10841 cp_parser_declarator. */
10843 static cp_declarator *
10844 cp_parser_direct_declarator (cp_parser* parser,
10845 cp_parser_declarator_kind dcl_kind,
10846 int* ctor_dtor_or_conv_p)
10848 cp_token *token;
10849 cp_declarator *declarator = NULL;
10850 tree scope = NULL_TREE;
10851 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10852 bool saved_in_declarator_p = parser->in_declarator_p;
10853 bool first = true;
10854 bool pop_p = false;
10856 while (true)
10858 /* Peek at the next token. */
10859 token = cp_lexer_peek_token (parser->lexer);
10860 if (token->type == CPP_OPEN_PAREN)
10862 /* This is either a parameter-declaration-clause, or a
10863 parenthesized declarator. When we know we are parsing a
10864 named declarator, it must be a parenthesized declarator
10865 if FIRST is true. For instance, `(int)' is a
10866 parameter-declaration-clause, with an omitted
10867 direct-abstract-declarator. But `((*))', is a
10868 parenthesized abstract declarator. Finally, when T is a
10869 template parameter `(T)' is a
10870 parameter-declaration-clause, and not a parenthesized
10871 named declarator.
10873 We first try and parse a parameter-declaration-clause,
10874 and then try a nested declarator (if FIRST is true).
10876 It is not an error for it not to be a
10877 parameter-declaration-clause, even when FIRST is
10878 false. Consider,
10880 int i (int);
10881 int i (3);
10883 The first is the declaration of a function while the
10884 second is a the definition of a variable, including its
10885 initializer.
10887 Having seen only the parenthesis, we cannot know which of
10888 these two alternatives should be selected. Even more
10889 complex are examples like:
10891 int i (int (a));
10892 int i (int (3));
10894 The former is a function-declaration; the latter is a
10895 variable initialization.
10897 Thus again, we try a parameter-declaration-clause, and if
10898 that fails, we back out and return. */
10900 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10902 cp_parameter_declarator *params;
10903 unsigned saved_num_template_parameter_lists;
10905 cp_parser_parse_tentatively (parser);
10907 /* Consume the `('. */
10908 cp_lexer_consume_token (parser->lexer);
10909 if (first)
10911 /* If this is going to be an abstract declarator, we're
10912 in a declarator and we can't have default args. */
10913 parser->default_arg_ok_p = false;
10914 parser->in_declarator_p = true;
10917 /* Inside the function parameter list, surrounding
10918 template-parameter-lists do not apply. */
10919 saved_num_template_parameter_lists
10920 = parser->num_template_parameter_lists;
10921 parser->num_template_parameter_lists = 0;
10923 /* Parse the parameter-declaration-clause. */
10924 params = cp_parser_parameter_declaration_clause (parser);
10926 parser->num_template_parameter_lists
10927 = saved_num_template_parameter_lists;
10929 /* If all went well, parse the cv-qualifier-seq and the
10930 exception-specification. */
10931 if (cp_parser_parse_definitely (parser))
10933 cp_cv_quals cv_quals;
10934 tree exception_specification;
10936 if (ctor_dtor_or_conv_p)
10937 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10938 first = false;
10939 /* Consume the `)'. */
10940 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10942 /* Parse the cv-qualifier-seq. */
10943 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10944 /* And the exception-specification. */
10945 exception_specification
10946 = cp_parser_exception_specification_opt (parser);
10948 /* Create the function-declarator. */
10949 declarator = make_call_declarator (declarator,
10950 params,
10951 cv_quals,
10952 exception_specification);
10953 /* Any subsequent parameter lists are to do with
10954 return type, so are not those of the declared
10955 function. */
10956 parser->default_arg_ok_p = false;
10958 /* Repeat the main loop. */
10959 continue;
10963 /* If this is the first, we can try a parenthesized
10964 declarator. */
10965 if (first)
10967 bool saved_in_type_id_in_expr_p;
10969 parser->default_arg_ok_p = saved_default_arg_ok_p;
10970 parser->in_declarator_p = saved_in_declarator_p;
10972 /* Consume the `('. */
10973 cp_lexer_consume_token (parser->lexer);
10974 /* Parse the nested declarator. */
10975 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10976 parser->in_type_id_in_expr_p = true;
10977 declarator
10978 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10979 /*parenthesized_p=*/NULL);
10980 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10981 first = false;
10982 /* Expect a `)'. */
10983 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10984 declarator = cp_error_declarator;
10985 if (declarator == cp_error_declarator)
10986 break;
10988 goto handle_declarator;
10990 /* Otherwise, we must be done. */
10991 else
10992 break;
10994 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10995 && token->type == CPP_OPEN_SQUARE)
10997 /* Parse an array-declarator. */
10998 tree bounds;
11000 if (ctor_dtor_or_conv_p)
11001 *ctor_dtor_or_conv_p = 0;
11003 first = false;
11004 parser->default_arg_ok_p = false;
11005 parser->in_declarator_p = true;
11006 /* Consume the `['. */
11007 cp_lexer_consume_token (parser->lexer);
11008 /* Peek at the next token. */
11009 token = cp_lexer_peek_token (parser->lexer);
11010 /* If the next token is `]', then there is no
11011 constant-expression. */
11012 if (token->type != CPP_CLOSE_SQUARE)
11014 bool non_constant_p;
11016 bounds
11017 = cp_parser_constant_expression (parser,
11018 /*allow_non_constant=*/true,
11019 &non_constant_p);
11020 if (!non_constant_p)
11021 bounds = fold_non_dependent_expr (bounds);
11023 else
11024 bounds = NULL_TREE;
11025 /* Look for the closing `]'. */
11026 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11028 declarator = cp_error_declarator;
11029 break;
11032 declarator = make_array_declarator (declarator, bounds);
11034 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11036 tree id;
11038 /* Parse a declarator-id */
11039 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11040 cp_parser_parse_tentatively (parser);
11041 id = cp_parser_declarator_id (parser);
11042 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11044 if (!cp_parser_parse_definitely (parser))
11045 id = error_mark_node;
11046 else if (TREE_CODE (id) != IDENTIFIER_NODE)
11048 cp_parser_error (parser, "expected unqualified-id");
11049 id = error_mark_node;
11053 if (id == error_mark_node)
11055 declarator = cp_error_declarator;
11056 break;
11059 if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11061 tree scope = TREE_OPERAND (id, 0);
11063 /* In the declaration of a member of a template class
11064 outside of the class itself, the SCOPE will sometimes
11065 be a TYPENAME_TYPE. For example, given:
11067 template <typename T>
11068 int S<T>::R::i = 3;
11070 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11071 this context, we must resolve S<T>::R to an ordinary
11072 type, rather than a typename type.
11074 The reason we normally avoid resolving TYPENAME_TYPEs
11075 is that a specialization of `S' might render
11076 `S<T>::R' not a type. However, if `S' is
11077 specialized, then this `i' will not be used, so there
11078 is no harm in resolving the types here. */
11079 if (TREE_CODE (scope) == TYPENAME_TYPE)
11081 tree type;
11083 /* Resolve the TYPENAME_TYPE. */
11084 type = resolve_typename_type (scope,
11085 /*only_current_p=*/false);
11086 /* If that failed, the declarator is invalid. */
11087 if (type == error_mark_node)
11088 error ("%<%T::%D%> is not a type",
11089 TYPE_CONTEXT (scope),
11090 TYPE_IDENTIFIER (scope));
11091 /* Build a new DECLARATOR. */
11092 id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11096 declarator = make_id_declarator (id);
11097 if (id)
11099 tree class_type;
11100 tree unqualified_name;
11102 if (TREE_CODE (id) == SCOPE_REF
11103 && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11105 class_type = TREE_OPERAND (id, 0);
11106 unqualified_name = TREE_OPERAND (id, 1);
11108 else
11110 class_type = current_class_type;
11111 unqualified_name = id;
11114 if (class_type)
11116 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11117 declarator->u.id.sfk = sfk_destructor;
11118 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11119 declarator->u.id.sfk = sfk_conversion;
11120 else if (constructor_name_p (unqualified_name,
11121 class_type)
11122 || (TREE_CODE (unqualified_name) == TYPE_DECL
11123 && same_type_p (TREE_TYPE (unqualified_name),
11124 class_type)))
11125 declarator->u.id.sfk = sfk_constructor;
11127 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11128 *ctor_dtor_or_conv_p = -1;
11129 if (TREE_CODE (id) == SCOPE_REF
11130 && TREE_CODE (unqualified_name) == TYPE_DECL
11131 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11133 error ("invalid use of constructor as a template");
11134 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11135 "the constructor in a qualified name",
11136 class_type,
11137 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11138 class_type, class_type);
11143 handle_declarator:;
11144 scope = get_scope_of_declarator (declarator);
11145 if (scope)
11146 /* Any names that appear after the declarator-id for a
11147 member are looked up in the containing scope. */
11148 pop_p = push_scope (scope);
11149 parser->in_declarator_p = true;
11150 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11151 || (declarator && declarator->kind == cdk_id))
11152 /* Default args are only allowed on function
11153 declarations. */
11154 parser->default_arg_ok_p = saved_default_arg_ok_p;
11155 else
11156 parser->default_arg_ok_p = false;
11158 first = false;
11160 /* We're done. */
11161 else
11162 break;
11165 /* For an abstract declarator, we might wind up with nothing at this
11166 point. That's an error; the declarator is not optional. */
11167 if (!declarator)
11168 cp_parser_error (parser, "expected declarator");
11170 /* If we entered a scope, we must exit it now. */
11171 if (pop_p)
11172 pop_scope (scope);
11174 parser->default_arg_ok_p = saved_default_arg_ok_p;
11175 parser->in_declarator_p = saved_in_declarator_p;
11177 return declarator;
11180 /* Parse a ptr-operator.
11182 ptr-operator:
11183 * cv-qualifier-seq [opt]
11185 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11187 GNU Extension:
11189 ptr-operator:
11190 & cv-qualifier-seq [opt]
11192 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11193 Returns ADDR_EXPR if a reference was used. In the case of a
11194 pointer-to-member, *TYPE is filled in with the TYPE containing the
11195 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11196 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11197 ERROR_MARK if an error occurred. */
11199 static enum tree_code
11200 cp_parser_ptr_operator (cp_parser* parser,
11201 tree* type,
11202 cp_cv_quals *cv_quals)
11204 enum tree_code code = ERROR_MARK;
11205 cp_token *token;
11207 /* Assume that it's not a pointer-to-member. */
11208 *type = NULL_TREE;
11209 /* And that there are no cv-qualifiers. */
11210 *cv_quals = TYPE_UNQUALIFIED;
11212 /* Peek at the next token. */
11213 token = cp_lexer_peek_token (parser->lexer);
11214 /* If it's a `*' or `&' we have a pointer or reference. */
11215 if (token->type == CPP_MULT || token->type == CPP_AND)
11217 /* Remember which ptr-operator we were processing. */
11218 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11220 /* Consume the `*' or `&'. */
11221 cp_lexer_consume_token (parser->lexer);
11223 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11224 `&', if we are allowing GNU extensions. (The only qualifier
11225 that can legally appear after `&' is `restrict', but that is
11226 enforced during semantic analysis. */
11227 if (code == INDIRECT_REF
11228 || cp_parser_allow_gnu_extensions_p (parser))
11229 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11231 else
11233 /* Try the pointer-to-member case. */
11234 cp_parser_parse_tentatively (parser);
11235 /* Look for the optional `::' operator. */
11236 cp_parser_global_scope_opt (parser,
11237 /*current_scope_valid_p=*/false);
11238 /* Look for the nested-name specifier. */
11239 cp_parser_nested_name_specifier (parser,
11240 /*typename_keyword_p=*/false,
11241 /*check_dependency_p=*/true,
11242 /*type_p=*/false,
11243 /*is_declaration=*/false);
11244 /* If we found it, and the next token is a `*', then we are
11245 indeed looking at a pointer-to-member operator. */
11246 if (!cp_parser_error_occurred (parser)
11247 && cp_parser_require (parser, CPP_MULT, "`*'"))
11249 /* The type of which the member is a member is given by the
11250 current SCOPE. */
11251 *type = parser->scope;
11252 /* The next name will not be qualified. */
11253 parser->scope = NULL_TREE;
11254 parser->qualifying_scope = NULL_TREE;
11255 parser->object_scope = NULL_TREE;
11256 /* Indicate that the `*' operator was used. */
11257 code = INDIRECT_REF;
11258 /* Look for the optional cv-qualifier-seq. */
11259 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11261 /* If that didn't work we don't have a ptr-operator. */
11262 if (!cp_parser_parse_definitely (parser))
11263 cp_parser_error (parser, "expected ptr-operator");
11266 return code;
11269 /* Parse an (optional) cv-qualifier-seq.
11271 cv-qualifier-seq:
11272 cv-qualifier cv-qualifier-seq [opt]
11274 cv-qualifier:
11275 const
11276 volatile
11278 GNU Extension:
11280 cv-qualifier:
11281 __restrict__
11283 Returns a bitmask representing the cv-qualifiers. */
11285 static cp_cv_quals
11286 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11288 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11290 while (true)
11292 cp_token *token;
11293 cp_cv_quals cv_qualifier;
11295 /* Peek at the next token. */
11296 token = cp_lexer_peek_token (parser->lexer);
11297 /* See if it's a cv-qualifier. */
11298 switch (token->keyword)
11300 case RID_CONST:
11301 cv_qualifier = TYPE_QUAL_CONST;
11302 break;
11304 case RID_VOLATILE:
11305 cv_qualifier = TYPE_QUAL_VOLATILE;
11306 break;
11308 case RID_RESTRICT:
11309 cv_qualifier = TYPE_QUAL_RESTRICT;
11310 break;
11312 default:
11313 cv_qualifier = TYPE_UNQUALIFIED;
11314 break;
11317 if (!cv_qualifier)
11318 break;
11320 if (cv_quals & cv_qualifier)
11322 error ("duplicate cv-qualifier");
11323 cp_lexer_purge_token (parser->lexer);
11325 else
11327 cp_lexer_consume_token (parser->lexer);
11328 cv_quals |= cv_qualifier;
11332 return cv_quals;
11335 /* Parse a declarator-id.
11337 declarator-id:
11338 id-expression
11339 :: [opt] nested-name-specifier [opt] type-name
11341 In the `id-expression' case, the value returned is as for
11342 cp_parser_id_expression if the id-expression was an unqualified-id.
11343 If the id-expression was a qualified-id, then a SCOPE_REF is
11344 returned. The first operand is the scope (either a NAMESPACE_DECL
11345 or TREE_TYPE), but the second is still just a representation of an
11346 unqualified-id. */
11348 static tree
11349 cp_parser_declarator_id (cp_parser* parser)
11351 tree id_expression;
11353 /* The expression must be an id-expression. Assume that qualified
11354 names are the names of types so that:
11356 template <class T>
11357 int S<T>::R::i = 3;
11359 will work; we must treat `S<T>::R' as the name of a type.
11360 Similarly, assume that qualified names are templates, where
11361 required, so that:
11363 template <class T>
11364 int S<T>::R<T>::i = 3;
11366 will work, too. */
11367 id_expression = cp_parser_id_expression (parser,
11368 /*template_keyword_p=*/false,
11369 /*check_dependency_p=*/false,
11370 /*template_p=*/NULL,
11371 /*declarator_p=*/true);
11372 /* If the name was qualified, create a SCOPE_REF to represent
11373 that. */
11374 if (parser->scope)
11376 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11377 parser->scope = NULL_TREE;
11380 return id_expression;
11383 /* Parse a type-id.
11385 type-id:
11386 type-specifier-seq abstract-declarator [opt]
11388 Returns the TYPE specified. */
11390 static tree
11391 cp_parser_type_id (cp_parser* parser)
11393 cp_decl_specifier_seq type_specifier_seq;
11394 cp_declarator *abstract_declarator;
11396 /* Parse the type-specifier-seq. */
11397 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11398 if (type_specifier_seq.type == error_mark_node)
11399 return error_mark_node;
11401 /* There might or might not be an abstract declarator. */
11402 cp_parser_parse_tentatively (parser);
11403 /* Look for the declarator. */
11404 abstract_declarator
11405 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11406 /*parenthesized_p=*/NULL);
11407 /* Check to see if there really was a declarator. */
11408 if (!cp_parser_parse_definitely (parser))
11409 abstract_declarator = NULL;
11411 return groktypename (&type_specifier_seq, abstract_declarator);
11414 /* Parse a type-specifier-seq.
11416 type-specifier-seq:
11417 type-specifier type-specifier-seq [opt]
11419 GNU extension:
11421 type-specifier-seq:
11422 attributes type-specifier-seq [opt]
11424 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11426 static void
11427 cp_parser_type_specifier_seq (cp_parser* parser,
11428 cp_decl_specifier_seq *type_specifier_seq)
11430 bool seen_type_specifier = false;
11432 /* Clear the TYPE_SPECIFIER_SEQ. */
11433 clear_decl_specs (type_specifier_seq);
11435 /* Parse the type-specifiers and attributes. */
11436 while (true)
11438 tree type_specifier;
11440 /* Check for attributes first. */
11441 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11443 type_specifier_seq->attributes =
11444 chainon (type_specifier_seq->attributes,
11445 cp_parser_attributes_opt (parser));
11446 continue;
11449 /* Look for the type-specifier. */
11450 type_specifier = cp_parser_type_specifier (parser,
11451 CP_PARSER_FLAGS_OPTIONAL,
11452 type_specifier_seq,
11453 /*is_declaration=*/false,
11454 NULL,
11455 NULL);
11456 /* If the first type-specifier could not be found, this is not a
11457 type-specifier-seq at all. */
11458 if (!seen_type_specifier && !type_specifier)
11460 cp_parser_error (parser, "expected type-specifier");
11461 type_specifier_seq->type = error_mark_node;
11462 return;
11464 /* If subsequent type-specifiers could not be found, the
11465 type-specifier-seq is complete. */
11466 else if (seen_type_specifier && !type_specifier)
11467 break;
11469 seen_type_specifier = true;
11472 return;
11475 /* Parse a parameter-declaration-clause.
11477 parameter-declaration-clause:
11478 parameter-declaration-list [opt] ... [opt]
11479 parameter-declaration-list , ...
11481 Returns a representation for the parameter declarations. A return
11482 value of NULL indicates a parameter-declaration-clause consisting
11483 only of an ellipsis. */
11485 static cp_parameter_declarator *
11486 cp_parser_parameter_declaration_clause (cp_parser* parser)
11488 cp_parameter_declarator *parameters;
11489 cp_token *token;
11490 bool ellipsis_p;
11491 bool is_error;
11493 /* Peek at the next token. */
11494 token = cp_lexer_peek_token (parser->lexer);
11495 /* Check for trivial parameter-declaration-clauses. */
11496 if (token->type == CPP_ELLIPSIS)
11498 /* Consume the `...' token. */
11499 cp_lexer_consume_token (parser->lexer);
11500 return NULL;
11502 else if (token->type == CPP_CLOSE_PAREN)
11503 /* There are no parameters. */
11505 #ifndef NO_IMPLICIT_EXTERN_C
11506 if (in_system_header && current_class_type == NULL
11507 && current_lang_name == lang_name_c)
11508 return NULL;
11509 else
11510 #endif
11511 return no_parameters;
11513 /* Check for `(void)', too, which is a special case. */
11514 else if (token->keyword == RID_VOID
11515 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11516 == CPP_CLOSE_PAREN))
11518 /* Consume the `void' token. */
11519 cp_lexer_consume_token (parser->lexer);
11520 /* There are no parameters. */
11521 return no_parameters;
11524 /* Parse the parameter-declaration-list. */
11525 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11526 /* If a parse error occurred while parsing the
11527 parameter-declaration-list, then the entire
11528 parameter-declaration-clause is erroneous. */
11529 if (is_error)
11530 return NULL;
11532 /* Peek at the next token. */
11533 token = cp_lexer_peek_token (parser->lexer);
11534 /* If it's a `,', the clause should terminate with an ellipsis. */
11535 if (token->type == CPP_COMMA)
11537 /* Consume the `,'. */
11538 cp_lexer_consume_token (parser->lexer);
11539 /* Expect an ellipsis. */
11540 ellipsis_p
11541 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11543 /* It might also be `...' if the optional trailing `,' was
11544 omitted. */
11545 else if (token->type == CPP_ELLIPSIS)
11547 /* Consume the `...' token. */
11548 cp_lexer_consume_token (parser->lexer);
11549 /* And remember that we saw it. */
11550 ellipsis_p = true;
11552 else
11553 ellipsis_p = false;
11555 /* Finish the parameter list. */
11556 if (parameters && ellipsis_p)
11557 parameters->ellipsis_p = true;
11559 return parameters;
11562 /* Parse a parameter-declaration-list.
11564 parameter-declaration-list:
11565 parameter-declaration
11566 parameter-declaration-list , parameter-declaration
11568 Returns a representation of the parameter-declaration-list, as for
11569 cp_parser_parameter_declaration_clause. However, the
11570 `void_list_node' is never appended to the list. Upon return,
11571 *IS_ERROR will be true iff an error occurred. */
11573 static cp_parameter_declarator *
11574 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11576 cp_parameter_declarator *parameters = NULL;
11577 cp_parameter_declarator **tail = &parameters;
11579 /* Assume all will go well. */
11580 *is_error = false;
11582 /* Look for more parameters. */
11583 while (true)
11585 cp_parameter_declarator *parameter;
11586 bool parenthesized_p;
11587 /* Parse the parameter. */
11588 parameter
11589 = cp_parser_parameter_declaration (parser,
11590 /*template_parm_p=*/false,
11591 &parenthesized_p);
11593 /* If a parse error occurred parsing the parameter declaration,
11594 then the entire parameter-declaration-list is erroneous. */
11595 if (!parameter)
11597 *is_error = true;
11598 parameters = NULL;
11599 break;
11601 /* Add the new parameter to the list. */
11602 *tail = parameter;
11603 tail = &parameter->next;
11605 /* Peek at the next token. */
11606 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11607 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11608 /* The parameter-declaration-list is complete. */
11609 break;
11610 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11612 cp_token *token;
11614 /* Peek at the next token. */
11615 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11616 /* If it's an ellipsis, then the list is complete. */
11617 if (token->type == CPP_ELLIPSIS)
11618 break;
11619 /* Otherwise, there must be more parameters. Consume the
11620 `,'. */
11621 cp_lexer_consume_token (parser->lexer);
11622 /* When parsing something like:
11624 int i(float f, double d)
11626 we can tell after seeing the declaration for "f" that we
11627 are not looking at an initialization of a variable "i",
11628 but rather at the declaration of a function "i".
11630 Due to the fact that the parsing of template arguments
11631 (as specified to a template-id) requires backtracking we
11632 cannot use this technique when inside a template argument
11633 list. */
11634 if (!parser->in_template_argument_list_p
11635 && !parser->in_type_id_in_expr_p
11636 && cp_parser_parsing_tentatively (parser)
11637 && !cp_parser_committed_to_tentative_parse (parser)
11638 /* However, a parameter-declaration of the form
11639 "foat(f)" (which is a valid declaration of a
11640 parameter "f") can also be interpreted as an
11641 expression (the conversion of "f" to "float"). */
11642 && !parenthesized_p)
11643 cp_parser_commit_to_tentative_parse (parser);
11645 else
11647 cp_parser_error (parser, "expected %<,%> or %<...%>");
11648 if (!cp_parser_parsing_tentatively (parser)
11649 || cp_parser_committed_to_tentative_parse (parser))
11650 cp_parser_skip_to_closing_parenthesis (parser,
11651 /*recovering=*/true,
11652 /*or_comma=*/false,
11653 /*consume_paren=*/false);
11654 break;
11658 return parameters;
11661 /* Parse a parameter declaration.
11663 parameter-declaration:
11664 decl-specifier-seq declarator
11665 decl-specifier-seq declarator = assignment-expression
11666 decl-specifier-seq abstract-declarator [opt]
11667 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11669 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11670 declares a template parameter. (In that case, a non-nested `>'
11671 token encountered during the parsing of the assignment-expression
11672 is not interpreted as a greater-than operator.)
11674 Returns a representation of the parameter, or NULL if an error
11675 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11676 true iff the declarator is of the form "(p)". */
11678 static cp_parameter_declarator *
11679 cp_parser_parameter_declaration (cp_parser *parser,
11680 bool template_parm_p,
11681 bool *parenthesized_p)
11683 int declares_class_or_enum;
11684 bool greater_than_is_operator_p;
11685 cp_decl_specifier_seq decl_specifiers;
11686 cp_declarator *declarator;
11687 tree default_argument;
11688 cp_token *token;
11689 const char *saved_message;
11691 /* In a template parameter, `>' is not an operator.
11693 [temp.param]
11695 When parsing a default template-argument for a non-type
11696 template-parameter, the first non-nested `>' is taken as the end
11697 of the template parameter-list rather than a greater-than
11698 operator. */
11699 greater_than_is_operator_p = !template_parm_p;
11701 /* Type definitions may not appear in parameter types. */
11702 saved_message = parser->type_definition_forbidden_message;
11703 parser->type_definition_forbidden_message
11704 = "types may not be defined in parameter types";
11706 /* Parse the declaration-specifiers. */
11707 cp_parser_decl_specifier_seq (parser,
11708 CP_PARSER_FLAGS_NONE,
11709 &decl_specifiers,
11710 &declares_class_or_enum);
11711 /* If an error occurred, there's no reason to attempt to parse the
11712 rest of the declaration. */
11713 if (cp_parser_error_occurred (parser))
11715 parser->type_definition_forbidden_message = saved_message;
11716 return NULL;
11719 /* Peek at the next token. */
11720 token = cp_lexer_peek_token (parser->lexer);
11721 /* If the next token is a `)', `,', `=', `>', or `...', then there
11722 is no declarator. */
11723 if (token->type == CPP_CLOSE_PAREN
11724 || token->type == CPP_COMMA
11725 || token->type == CPP_EQ
11726 || token->type == CPP_ELLIPSIS
11727 || token->type == CPP_GREATER)
11729 declarator = NULL;
11730 if (parenthesized_p)
11731 *parenthesized_p = false;
11733 /* Otherwise, there should be a declarator. */
11734 else
11736 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11737 parser->default_arg_ok_p = false;
11739 /* After seeing a decl-specifier-seq, if the next token is not a
11740 "(", there is no possibility that the code is a valid
11741 expression. Therefore, if parsing tentatively, we commit at
11742 this point. */
11743 if (!parser->in_template_argument_list_p
11744 /* In an expression context, having seen:
11746 (int((char ...
11748 we cannot be sure whether we are looking at a
11749 function-type (taking a "char" as a parameter) or a cast
11750 of some object of type "char" to "int". */
11751 && !parser->in_type_id_in_expr_p
11752 && cp_parser_parsing_tentatively (parser)
11753 && !cp_parser_committed_to_tentative_parse (parser)
11754 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11755 cp_parser_commit_to_tentative_parse (parser);
11756 /* Parse the declarator. */
11757 declarator = cp_parser_declarator (parser,
11758 CP_PARSER_DECLARATOR_EITHER,
11759 /*ctor_dtor_or_conv_p=*/NULL,
11760 parenthesized_p);
11761 parser->default_arg_ok_p = saved_default_arg_ok_p;
11762 /* After the declarator, allow more attributes. */
11763 decl_specifiers.attributes
11764 = chainon (decl_specifiers.attributes,
11765 cp_parser_attributes_opt (parser));
11768 /* The restriction on defining new types applies only to the type
11769 of the parameter, not to the default argument. */
11770 parser->type_definition_forbidden_message = saved_message;
11772 /* If the next token is `=', then process a default argument. */
11773 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11775 bool saved_greater_than_is_operator_p;
11776 /* Consume the `='. */
11777 cp_lexer_consume_token (parser->lexer);
11779 /* If we are defining a class, then the tokens that make up the
11780 default argument must be saved and processed later. */
11781 if (!template_parm_p && at_class_scope_p ()
11782 && TYPE_BEING_DEFINED (current_class_type))
11784 unsigned depth = 0;
11785 cp_token *first_token;
11786 cp_token *token;
11788 /* Add tokens until we have processed the entire default
11789 argument. We add the range [first_token, token). */
11790 first_token = cp_lexer_peek_token (parser->lexer);
11791 while (true)
11793 bool done = false;
11795 /* Peek at the next token. */
11796 token = cp_lexer_peek_token (parser->lexer);
11797 /* What we do depends on what token we have. */
11798 switch (token->type)
11800 /* In valid code, a default argument must be
11801 immediately followed by a `,' `)', or `...'. */
11802 case CPP_COMMA:
11803 case CPP_CLOSE_PAREN:
11804 case CPP_ELLIPSIS:
11805 /* If we run into a non-nested `;', `}', or `]',
11806 then the code is invalid -- but the default
11807 argument is certainly over. */
11808 case CPP_SEMICOLON:
11809 case CPP_CLOSE_BRACE:
11810 case CPP_CLOSE_SQUARE:
11811 if (depth == 0)
11812 done = true;
11813 /* Update DEPTH, if necessary. */
11814 else if (token->type == CPP_CLOSE_PAREN
11815 || token->type == CPP_CLOSE_BRACE
11816 || token->type == CPP_CLOSE_SQUARE)
11817 --depth;
11818 break;
11820 case CPP_OPEN_PAREN:
11821 case CPP_OPEN_SQUARE:
11822 case CPP_OPEN_BRACE:
11823 ++depth;
11824 break;
11826 case CPP_GREATER:
11827 /* If we see a non-nested `>', and `>' is not an
11828 operator, then it marks the end of the default
11829 argument. */
11830 if (!depth && !greater_than_is_operator_p)
11831 done = true;
11832 break;
11834 /* If we run out of tokens, issue an error message. */
11835 case CPP_EOF:
11836 error ("file ends in default argument");
11837 done = true;
11838 break;
11840 case CPP_NAME:
11841 case CPP_SCOPE:
11842 /* In these cases, we should look for template-ids.
11843 For example, if the default argument is
11844 `X<int, double>()', we need to do name lookup to
11845 figure out whether or not `X' is a template; if
11846 so, the `,' does not end the default argument.
11848 That is not yet done. */
11849 break;
11851 default:
11852 break;
11855 /* If we've reached the end, stop. */
11856 if (done)
11857 break;
11859 /* Add the token to the token block. */
11860 token = cp_lexer_consume_token (parser->lexer);
11863 /* Create a DEFAULT_ARG to represented the unparsed default
11864 argument. */
11865 default_argument = make_node (DEFAULT_ARG);
11866 DEFARG_TOKENS (default_argument)
11867 = cp_token_cache_new (first_token, token);
11869 /* Outside of a class definition, we can just parse the
11870 assignment-expression. */
11871 else
11873 bool saved_local_variables_forbidden_p;
11875 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11876 set correctly. */
11877 saved_greater_than_is_operator_p
11878 = parser->greater_than_is_operator_p;
11879 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11880 /* Local variable names (and the `this' keyword) may not
11881 appear in a default argument. */
11882 saved_local_variables_forbidden_p
11883 = parser->local_variables_forbidden_p;
11884 parser->local_variables_forbidden_p = true;
11885 /* Parse the assignment-expression. */
11886 default_argument = cp_parser_assignment_expression (parser);
11887 /* Restore saved state. */
11888 parser->greater_than_is_operator_p
11889 = saved_greater_than_is_operator_p;
11890 parser->local_variables_forbidden_p
11891 = saved_local_variables_forbidden_p;
11893 if (!parser->default_arg_ok_p)
11895 if (!flag_pedantic_errors)
11896 warning ("deprecated use of default argument for parameter of non-function");
11897 else
11899 error ("default arguments are only permitted for function parameters");
11900 default_argument = NULL_TREE;
11904 else
11905 default_argument = NULL_TREE;
11907 return make_parameter_declarator (&decl_specifiers,
11908 declarator,
11909 default_argument);
11912 /* Parse a function-body.
11914 function-body:
11915 compound_statement */
11917 static void
11918 cp_parser_function_body (cp_parser *parser)
11920 cp_parser_compound_statement (parser, NULL, false);
11923 /* Parse a ctor-initializer-opt followed by a function-body. Return
11924 true if a ctor-initializer was present. */
11926 static bool
11927 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11929 tree body;
11930 bool ctor_initializer_p;
11932 /* Begin the function body. */
11933 body = begin_function_body ();
11934 /* Parse the optional ctor-initializer. */
11935 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11936 /* Parse the function-body. */
11937 cp_parser_function_body (parser);
11938 /* Finish the function body. */
11939 finish_function_body (body);
11941 return ctor_initializer_p;
11944 /* Parse an initializer.
11946 initializer:
11947 = initializer-clause
11948 ( expression-list )
11950 Returns a expression representing the initializer. If no
11951 initializer is present, NULL_TREE is returned.
11953 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11954 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11955 set to FALSE if there is no initializer present. If there is an
11956 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11957 is set to true; otherwise it is set to false. */
11959 static tree
11960 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11961 bool* non_constant_p)
11963 cp_token *token;
11964 tree init;
11966 /* Peek at the next token. */
11967 token = cp_lexer_peek_token (parser->lexer);
11969 /* Let our caller know whether or not this initializer was
11970 parenthesized. */
11971 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11972 /* Assume that the initializer is constant. */
11973 *non_constant_p = false;
11975 if (token->type == CPP_EQ)
11977 /* Consume the `='. */
11978 cp_lexer_consume_token (parser->lexer);
11979 /* Parse the initializer-clause. */
11980 init = cp_parser_initializer_clause (parser, non_constant_p);
11982 else if (token->type == CPP_OPEN_PAREN)
11983 init = cp_parser_parenthesized_expression_list (parser, false,
11984 non_constant_p);
11985 else
11987 /* Anything else is an error. */
11988 cp_parser_error (parser, "expected initializer");
11989 init = error_mark_node;
11992 return init;
11995 /* Parse an initializer-clause.
11997 initializer-clause:
11998 assignment-expression
11999 { initializer-list , [opt] }
12002 Returns an expression representing the initializer.
12004 If the `assignment-expression' production is used the value
12005 returned is simply a representation for the expression.
12007 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12008 the elements of the initializer-list (or NULL_TREE, if the last
12009 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12010 NULL_TREE. There is no way to detect whether or not the optional
12011 trailing `,' was provided. NON_CONSTANT_P is as for
12012 cp_parser_initializer. */
12014 static tree
12015 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12017 tree initializer;
12019 /* If it is not a `{', then we are looking at an
12020 assignment-expression. */
12021 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12023 initializer
12024 = cp_parser_constant_expression (parser,
12025 /*allow_non_constant_p=*/true,
12026 non_constant_p);
12027 if (!*non_constant_p)
12028 initializer = fold_non_dependent_expr (initializer);
12030 else
12032 /* Consume the `{' token. */
12033 cp_lexer_consume_token (parser->lexer);
12034 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12035 initializer = make_node (CONSTRUCTOR);
12036 /* If it's not a `}', then there is a non-trivial initializer. */
12037 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12039 /* Parse the initializer list. */
12040 CONSTRUCTOR_ELTS (initializer)
12041 = cp_parser_initializer_list (parser, non_constant_p);
12042 /* A trailing `,' token is allowed. */
12043 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12044 cp_lexer_consume_token (parser->lexer);
12046 /* Now, there should be a trailing `}'. */
12047 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12050 return initializer;
12053 /* Parse an initializer-list.
12055 initializer-list:
12056 initializer-clause
12057 initializer-list , initializer-clause
12059 GNU Extension:
12061 initializer-list:
12062 identifier : initializer-clause
12063 initializer-list, identifier : initializer-clause
12065 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12066 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12067 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12068 as for cp_parser_initializer. */
12070 static tree
12071 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12073 tree initializers = NULL_TREE;
12075 /* Assume all of the expressions are constant. */
12076 *non_constant_p = false;
12078 /* Parse the rest of the list. */
12079 while (true)
12081 cp_token *token;
12082 tree identifier;
12083 tree initializer;
12084 bool clause_non_constant_p;
12086 /* If the next token is an identifier and the following one is a
12087 colon, we are looking at the GNU designated-initializer
12088 syntax. */
12089 if (cp_parser_allow_gnu_extensions_p (parser)
12090 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12091 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12093 /* Consume the identifier. */
12094 identifier = cp_lexer_consume_token (parser->lexer)->value;
12095 /* Consume the `:'. */
12096 cp_lexer_consume_token (parser->lexer);
12098 else
12099 identifier = NULL_TREE;
12101 /* Parse the initializer. */
12102 initializer = cp_parser_initializer_clause (parser,
12103 &clause_non_constant_p);
12104 /* If any clause is non-constant, so is the entire initializer. */
12105 if (clause_non_constant_p)
12106 *non_constant_p = true;
12107 /* Add it to the list. */
12108 initializers = tree_cons (identifier, initializer, initializers);
12110 /* If the next token is not a comma, we have reached the end of
12111 the list. */
12112 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12113 break;
12115 /* Peek at the next token. */
12116 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12117 /* If the next token is a `}', then we're still done. An
12118 initializer-clause can have a trailing `,' after the
12119 initializer-list and before the closing `}'. */
12120 if (token->type == CPP_CLOSE_BRACE)
12121 break;
12123 /* Consume the `,' token. */
12124 cp_lexer_consume_token (parser->lexer);
12127 /* The initializers were built up in reverse order, so we need to
12128 reverse them now. */
12129 return nreverse (initializers);
12132 /* Classes [gram.class] */
12134 /* Parse a class-name.
12136 class-name:
12137 identifier
12138 template-id
12140 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12141 to indicate that names looked up in dependent types should be
12142 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12143 keyword has been used to indicate that the name that appears next
12144 is a template. TYPE_P is true iff the next name should be treated
12145 as class-name, even if it is declared to be some other kind of name
12146 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12147 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
12148 being defined in a class-head.
12150 Returns the TYPE_DECL representing the class. */
12152 static tree
12153 cp_parser_class_name (cp_parser *parser,
12154 bool typename_keyword_p,
12155 bool template_keyword_p,
12156 bool type_p,
12157 bool check_dependency_p,
12158 bool class_head_p,
12159 bool is_declaration)
12161 tree decl;
12162 tree scope;
12163 bool typename_p;
12164 cp_token *token;
12166 /* All class-names start with an identifier. */
12167 token = cp_lexer_peek_token (parser->lexer);
12168 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12170 cp_parser_error (parser, "expected class-name");
12171 return error_mark_node;
12174 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12175 to a template-id, so we save it here. */
12176 scope = parser->scope;
12177 if (scope == error_mark_node)
12178 return error_mark_node;
12180 /* Any name names a type if we're following the `typename' keyword
12181 in a qualified name where the enclosing scope is type-dependent. */
12182 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12183 && dependent_type_p (scope));
12184 /* Handle the common case (an identifier, but not a template-id)
12185 efficiently. */
12186 if (token->type == CPP_NAME
12187 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12189 tree identifier;
12191 /* Look for the identifier. */
12192 identifier = cp_parser_identifier (parser);
12193 /* If the next token isn't an identifier, we are certainly not
12194 looking at a class-name. */
12195 if (identifier == error_mark_node)
12196 decl = error_mark_node;
12197 /* If we know this is a type-name, there's no need to look it
12198 up. */
12199 else if (typename_p)
12200 decl = identifier;
12201 else
12203 /* If the next token is a `::', then the name must be a type
12204 name.
12206 [basic.lookup.qual]
12208 During the lookup for a name preceding the :: scope
12209 resolution operator, object, function, and enumerator
12210 names are ignored. */
12211 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12212 type_p = true;
12213 /* Look up the name. */
12214 decl = cp_parser_lookup_name (parser, identifier,
12215 type_p,
12216 /*is_template=*/false,
12217 /*is_namespace=*/false,
12218 check_dependency_p,
12219 /*ambiguous_p=*/NULL);
12222 else
12224 /* Try a template-id. */
12225 decl = cp_parser_template_id (parser, template_keyword_p,
12226 check_dependency_p,
12227 is_declaration);
12228 if (decl == error_mark_node)
12229 return error_mark_node;
12232 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12234 /* If this is a typename, create a TYPENAME_TYPE. */
12235 if (typename_p && decl != error_mark_node)
12237 decl = make_typename_type (scope, decl, /*complain=*/1);
12238 if (decl != error_mark_node)
12239 decl = TYPE_NAME (decl);
12242 /* Check to see that it is really the name of a class. */
12243 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12244 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12245 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12246 /* Situations like this:
12248 template <typename T> struct A {
12249 typename T::template X<int>::I i;
12252 are problematic. Is `T::template X<int>' a class-name? The
12253 standard does not seem to be definitive, but there is no other
12254 valid interpretation of the following `::'. Therefore, those
12255 names are considered class-names. */
12256 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12257 else if (decl == error_mark_node
12258 || TREE_CODE (decl) != TYPE_DECL
12259 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12261 cp_parser_error (parser, "expected class-name");
12262 return error_mark_node;
12265 return decl;
12268 /* Parse a class-specifier.
12270 class-specifier:
12271 class-head { member-specification [opt] }
12273 Returns the TREE_TYPE representing the class. */
12275 static tree
12276 cp_parser_class_specifier (cp_parser* parser)
12278 cp_token *token;
12279 tree type;
12280 tree attributes = NULL_TREE;
12281 int has_trailing_semicolon;
12282 bool nested_name_specifier_p;
12283 unsigned saved_num_template_parameter_lists;
12284 bool pop_p = false;
12285 tree scope = NULL_TREE;
12287 push_deferring_access_checks (dk_no_deferred);
12289 /* Parse the class-head. */
12290 type = cp_parser_class_head (parser,
12291 &nested_name_specifier_p,
12292 &attributes);
12293 /* If the class-head was a semantic disaster, skip the entire body
12294 of the class. */
12295 if (!type)
12297 cp_parser_skip_to_end_of_block_or_statement (parser);
12298 pop_deferring_access_checks ();
12299 return error_mark_node;
12302 /* Look for the `{'. */
12303 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12305 pop_deferring_access_checks ();
12306 return error_mark_node;
12309 /* Issue an error message if type-definitions are forbidden here. */
12310 cp_parser_check_type_definition (parser);
12311 /* Remember that we are defining one more class. */
12312 ++parser->num_classes_being_defined;
12313 /* Inside the class, surrounding template-parameter-lists do not
12314 apply. */
12315 saved_num_template_parameter_lists
12316 = parser->num_template_parameter_lists;
12317 parser->num_template_parameter_lists = 0;
12319 /* Start the class. */
12320 if (nested_name_specifier_p)
12322 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12323 pop_p = push_scope (scope);
12325 type = begin_class_definition (type);
12327 if (type == error_mark_node)
12328 /* If the type is erroneous, skip the entire body of the class. */
12329 cp_parser_skip_to_closing_brace (parser);
12330 else
12331 /* Parse the member-specification. */
12332 cp_parser_member_specification_opt (parser);
12334 /* Look for the trailing `}'. */
12335 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12336 /* We get better error messages by noticing a common problem: a
12337 missing trailing `;'. */
12338 token = cp_lexer_peek_token (parser->lexer);
12339 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12340 /* Look for trailing attributes to apply to this class. */
12341 if (cp_parser_allow_gnu_extensions_p (parser))
12343 tree sub_attr = cp_parser_attributes_opt (parser);
12344 attributes = chainon (attributes, sub_attr);
12346 if (type != error_mark_node)
12347 type = finish_struct (type, attributes);
12348 if (pop_p)
12349 pop_scope (scope);
12350 /* If this class is not itself within the scope of another class,
12351 then we need to parse the bodies of all of the queued function
12352 definitions. Note that the queued functions defined in a class
12353 are not always processed immediately following the
12354 class-specifier for that class. Consider:
12356 struct A {
12357 struct B { void f() { sizeof (A); } };
12360 If `f' were processed before the processing of `A' were
12361 completed, there would be no way to compute the size of `A'.
12362 Note that the nesting we are interested in here is lexical --
12363 not the semantic nesting given by TYPE_CONTEXT. In particular,
12364 for:
12366 struct A { struct B; };
12367 struct A::B { void f() { } };
12369 there is no need to delay the parsing of `A::B::f'. */
12370 if (--parser->num_classes_being_defined == 0)
12372 tree queue_entry;
12373 tree fn;
12374 tree class_type;
12375 bool pop_p;
12377 /* In a first pass, parse default arguments to the functions.
12378 Then, in a second pass, parse the bodies of the functions.
12379 This two-phased approach handles cases like:
12381 struct S {
12382 void f() { g(); }
12383 void g(int i = 3);
12387 class_type = NULL_TREE;
12388 pop_p = false;
12389 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12390 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12391 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12392 TREE_PURPOSE (parser->unparsed_functions_queues)
12393 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12395 fn = TREE_VALUE (queue_entry);
12396 /* If there are default arguments that have not yet been processed,
12397 take care of them now. */
12398 if (class_type != TREE_PURPOSE (queue_entry))
12400 if (pop_p)
12401 pop_scope (class_type);
12402 class_type = TREE_PURPOSE (queue_entry);
12403 pop_p = push_scope (class_type);
12405 /* Make sure that any template parameters are in scope. */
12406 maybe_begin_member_template_processing (fn);
12407 /* Parse the default argument expressions. */
12408 cp_parser_late_parsing_default_args (parser, fn);
12409 /* Remove any template parameters from the symbol table. */
12410 maybe_end_member_template_processing ();
12412 if (pop_p)
12413 pop_scope (class_type);
12414 /* Now parse the body of the functions. */
12415 for (TREE_VALUE (parser->unparsed_functions_queues)
12416 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12417 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12418 TREE_VALUE (parser->unparsed_functions_queues)
12419 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12421 /* Figure out which function we need to process. */
12422 fn = TREE_VALUE (queue_entry);
12424 /* A hack to prevent garbage collection. */
12425 function_depth++;
12427 /* Parse the function. */
12428 cp_parser_late_parsing_for_member (parser, fn);
12429 function_depth--;
12433 /* Put back any saved access checks. */
12434 pop_deferring_access_checks ();
12436 /* Restore the count of active template-parameter-lists. */
12437 parser->num_template_parameter_lists
12438 = saved_num_template_parameter_lists;
12440 return type;
12443 /* Parse a class-head.
12445 class-head:
12446 class-key identifier [opt] base-clause [opt]
12447 class-key nested-name-specifier identifier base-clause [opt]
12448 class-key nested-name-specifier [opt] template-id
12449 base-clause [opt]
12451 GNU Extensions:
12452 class-key attributes identifier [opt] base-clause [opt]
12453 class-key attributes nested-name-specifier identifier base-clause [opt]
12454 class-key attributes nested-name-specifier [opt] template-id
12455 base-clause [opt]
12457 Returns the TYPE of the indicated class. Sets
12458 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12459 involving a nested-name-specifier was used, and FALSE otherwise.
12461 Returns NULL_TREE if the class-head is syntactically valid, but
12462 semantically invalid in a way that means we should skip the entire
12463 body of the class. */
12465 static tree
12466 cp_parser_class_head (cp_parser* parser,
12467 bool* nested_name_specifier_p,
12468 tree *attributes_p)
12470 tree nested_name_specifier;
12471 enum tag_types class_key;
12472 tree id = NULL_TREE;
12473 tree type = NULL_TREE;
12474 tree attributes;
12475 bool template_id_p = false;
12476 bool qualified_p = false;
12477 bool invalid_nested_name_p = false;
12478 bool invalid_explicit_specialization_p = false;
12479 bool pop_p = false;
12480 unsigned num_templates;
12481 tree bases;
12483 /* Assume no nested-name-specifier will be present. */
12484 *nested_name_specifier_p = false;
12485 /* Assume no template parameter lists will be used in defining the
12486 type. */
12487 num_templates = 0;
12489 /* Look for the class-key. */
12490 class_key = cp_parser_class_key (parser);
12491 if (class_key == none_type)
12492 return error_mark_node;
12494 /* Parse the attributes. */
12495 attributes = cp_parser_attributes_opt (parser);
12497 /* If the next token is `::', that is invalid -- but sometimes
12498 people do try to write:
12500 struct ::S {};
12502 Handle this gracefully by accepting the extra qualifier, and then
12503 issuing an error about it later if this really is a
12504 class-head. If it turns out just to be an elaborated type
12505 specifier, remain silent. */
12506 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12507 qualified_p = true;
12509 push_deferring_access_checks (dk_no_check);
12511 /* Determine the name of the class. Begin by looking for an
12512 optional nested-name-specifier. */
12513 nested_name_specifier
12514 = cp_parser_nested_name_specifier_opt (parser,
12515 /*typename_keyword_p=*/false,
12516 /*check_dependency_p=*/false,
12517 /*type_p=*/false,
12518 /*is_declaration=*/false);
12519 /* If there was a nested-name-specifier, then there *must* be an
12520 identifier. */
12521 if (nested_name_specifier)
12523 /* Although the grammar says `identifier', it really means
12524 `class-name' or `template-name'. You are only allowed to
12525 define a class that has already been declared with this
12526 syntax.
12528 The proposed resolution for Core Issue 180 says that whever
12529 you see `class T::X' you should treat `X' as a type-name.
12531 It is OK to define an inaccessible class; for example:
12533 class A { class B; };
12534 class A::B {};
12536 We do not know if we will see a class-name, or a
12537 template-name. We look for a class-name first, in case the
12538 class-name is a template-id; if we looked for the
12539 template-name first we would stop after the template-name. */
12540 cp_parser_parse_tentatively (parser);
12541 type = cp_parser_class_name (parser,
12542 /*typename_keyword_p=*/false,
12543 /*template_keyword_p=*/false,
12544 /*type_p=*/true,
12545 /*check_dependency_p=*/false,
12546 /*class_head_p=*/true,
12547 /*is_declaration=*/false);
12548 /* If that didn't work, ignore the nested-name-specifier. */
12549 if (!cp_parser_parse_definitely (parser))
12551 invalid_nested_name_p = true;
12552 id = cp_parser_identifier (parser);
12553 if (id == error_mark_node)
12554 id = NULL_TREE;
12556 /* If we could not find a corresponding TYPE, treat this
12557 declaration like an unqualified declaration. */
12558 if (type == error_mark_node)
12559 nested_name_specifier = NULL_TREE;
12560 /* Otherwise, count the number of templates used in TYPE and its
12561 containing scopes. */
12562 else
12564 tree scope;
12566 for (scope = TREE_TYPE (type);
12567 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12568 scope = (TYPE_P (scope)
12569 ? TYPE_CONTEXT (scope)
12570 : DECL_CONTEXT (scope)))
12571 if (TYPE_P (scope)
12572 && CLASS_TYPE_P (scope)
12573 && CLASSTYPE_TEMPLATE_INFO (scope)
12574 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12575 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12576 ++num_templates;
12579 /* Otherwise, the identifier is optional. */
12580 else
12582 /* We don't know whether what comes next is a template-id,
12583 an identifier, or nothing at all. */
12584 cp_parser_parse_tentatively (parser);
12585 /* Check for a template-id. */
12586 id = cp_parser_template_id (parser,
12587 /*template_keyword_p=*/false,
12588 /*check_dependency_p=*/true,
12589 /*is_declaration=*/true);
12590 /* If that didn't work, it could still be an identifier. */
12591 if (!cp_parser_parse_definitely (parser))
12593 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12594 id = cp_parser_identifier (parser);
12595 else
12596 id = NULL_TREE;
12598 else
12600 template_id_p = true;
12601 ++num_templates;
12605 pop_deferring_access_checks ();
12607 if (id)
12608 cp_parser_check_for_invalid_template_id (parser, id);
12610 /* If it's not a `:' or a `{' then we can't really be looking at a
12611 class-head, since a class-head only appears as part of a
12612 class-specifier. We have to detect this situation before calling
12613 xref_tag, since that has irreversible side-effects. */
12614 if (!cp_parser_next_token_starts_class_definition_p (parser))
12616 cp_parser_error (parser, "expected %<{%> or %<:%>");
12617 return error_mark_node;
12620 /* At this point, we're going ahead with the class-specifier, even
12621 if some other problem occurs. */
12622 cp_parser_commit_to_tentative_parse (parser);
12623 /* Issue the error about the overly-qualified name now. */
12624 if (qualified_p)
12625 cp_parser_error (parser,
12626 "global qualification of class name is invalid");
12627 else if (invalid_nested_name_p)
12628 cp_parser_error (parser,
12629 "qualified name does not name a class");
12630 else if (nested_name_specifier)
12632 tree scope;
12633 /* Figure out in what scope the declaration is being placed. */
12634 scope = current_scope ();
12635 if (!scope)
12636 scope = current_namespace;
12637 /* If that scope does not contain the scope in which the
12638 class was originally declared, the program is invalid. */
12639 if (scope && !is_ancestor (scope, nested_name_specifier))
12641 error ("declaration of %qD in %qD which does not enclose %qD",
12642 type, scope, nested_name_specifier);
12643 type = NULL_TREE;
12644 goto done;
12646 /* [dcl.meaning]
12648 A declarator-id shall not be qualified exception of the
12649 definition of a ... nested class outside of its class
12650 ... [or] a the definition or explicit instantiation of a
12651 class member of a namespace outside of its namespace. */
12652 if (scope == nested_name_specifier)
12654 pedwarn ("extra qualification ignored");
12655 nested_name_specifier = NULL_TREE;
12656 num_templates = 0;
12659 /* An explicit-specialization must be preceded by "template <>". If
12660 it is not, try to recover gracefully. */
12661 if (at_namespace_scope_p ()
12662 && parser->num_template_parameter_lists == 0
12663 && template_id_p)
12665 error ("an explicit specialization must be preceded by %<template <>%>");
12666 invalid_explicit_specialization_p = true;
12667 /* Take the same action that would have been taken by
12668 cp_parser_explicit_specialization. */
12669 ++parser->num_template_parameter_lists;
12670 begin_specialization ();
12672 /* There must be no "return" statements between this point and the
12673 end of this function; set "type "to the correct return value and
12674 use "goto done;" to return. */
12675 /* Make sure that the right number of template parameters were
12676 present. */
12677 if (!cp_parser_check_template_parameters (parser, num_templates))
12679 /* If something went wrong, there is no point in even trying to
12680 process the class-definition. */
12681 type = NULL_TREE;
12682 goto done;
12685 /* Look up the type. */
12686 if (template_id_p)
12688 type = TREE_TYPE (id);
12689 maybe_process_partial_specialization (type);
12691 else if (!nested_name_specifier)
12693 /* If the class was unnamed, create a dummy name. */
12694 if (!id)
12695 id = make_anon_name ();
12696 type = xref_tag (class_key, id, /*globalize=*/false,
12697 parser->num_template_parameter_lists);
12699 else
12701 tree class_type;
12702 bool pop_p = false;
12704 /* Given:
12706 template <typename T> struct S { struct T };
12707 template <typename T> struct S<T>::T { };
12709 we will get a TYPENAME_TYPE when processing the definition of
12710 `S::T'. We need to resolve it to the actual type before we
12711 try to define it. */
12712 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12714 class_type = resolve_typename_type (TREE_TYPE (type),
12715 /*only_current_p=*/false);
12716 if (class_type != error_mark_node)
12717 type = TYPE_NAME (class_type);
12718 else
12720 cp_parser_error (parser, "could not resolve typename type");
12721 type = error_mark_node;
12725 maybe_process_partial_specialization (TREE_TYPE (type));
12726 class_type = current_class_type;
12727 /* Enter the scope indicated by the nested-name-specifier. */
12728 if (nested_name_specifier)
12729 pop_p = push_scope (nested_name_specifier);
12730 /* Get the canonical version of this type. */
12731 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12732 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12733 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12734 type = push_template_decl (type);
12735 type = TREE_TYPE (type);
12736 if (nested_name_specifier)
12738 *nested_name_specifier_p = true;
12739 if (pop_p)
12740 pop_scope (nested_name_specifier);
12743 /* Indicate whether this class was declared as a `class' or as a
12744 `struct'. */
12745 if (TREE_CODE (type) == RECORD_TYPE)
12746 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12747 cp_parser_check_class_key (class_key, type);
12749 /* Enter the scope containing the class; the names of base classes
12750 should be looked up in that context. For example, given:
12752 struct A { struct B {}; struct C; };
12753 struct A::C : B {};
12755 is valid. */
12756 if (nested_name_specifier)
12757 pop_p = push_scope (nested_name_specifier);
12759 bases = NULL_TREE;
12761 /* Get the list of base-classes, if there is one. */
12762 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12763 bases = cp_parser_base_clause (parser);
12765 /* Process the base classes. */
12766 xref_basetypes (type, bases);
12768 /* Leave the scope given by the nested-name-specifier. We will
12769 enter the class scope itself while processing the members. */
12770 if (pop_p)
12771 pop_scope (nested_name_specifier);
12773 done:
12774 if (invalid_explicit_specialization_p)
12776 end_specialization ();
12777 --parser->num_template_parameter_lists;
12779 *attributes_p = attributes;
12780 return type;
12783 /* Parse a class-key.
12785 class-key:
12786 class
12787 struct
12788 union
12790 Returns the kind of class-key specified, or none_type to indicate
12791 error. */
12793 static enum tag_types
12794 cp_parser_class_key (cp_parser* parser)
12796 cp_token *token;
12797 enum tag_types tag_type;
12799 /* Look for the class-key. */
12800 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12801 if (!token)
12802 return none_type;
12804 /* Check to see if the TOKEN is a class-key. */
12805 tag_type = cp_parser_token_is_class_key (token);
12806 if (!tag_type)
12807 cp_parser_error (parser, "expected class-key");
12808 return tag_type;
12811 /* Parse an (optional) member-specification.
12813 member-specification:
12814 member-declaration member-specification [opt]
12815 access-specifier : member-specification [opt] */
12817 static void
12818 cp_parser_member_specification_opt (cp_parser* parser)
12820 while (true)
12822 cp_token *token;
12823 enum rid keyword;
12825 /* Peek at the next token. */
12826 token = cp_lexer_peek_token (parser->lexer);
12827 /* If it's a `}', or EOF then we've seen all the members. */
12828 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12829 break;
12831 /* See if this token is a keyword. */
12832 keyword = token->keyword;
12833 switch (keyword)
12835 case RID_PUBLIC:
12836 case RID_PROTECTED:
12837 case RID_PRIVATE:
12838 /* Consume the access-specifier. */
12839 cp_lexer_consume_token (parser->lexer);
12840 /* Remember which access-specifier is active. */
12841 current_access_specifier = token->value;
12842 /* Look for the `:'. */
12843 cp_parser_require (parser, CPP_COLON, "`:'");
12844 break;
12846 default:
12847 /* Otherwise, the next construction must be a
12848 member-declaration. */
12849 cp_parser_member_declaration (parser);
12854 /* Parse a member-declaration.
12856 member-declaration:
12857 decl-specifier-seq [opt] member-declarator-list [opt] ;
12858 function-definition ; [opt]
12859 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12860 using-declaration
12861 template-declaration
12863 member-declarator-list:
12864 member-declarator
12865 member-declarator-list , member-declarator
12867 member-declarator:
12868 declarator pure-specifier [opt]
12869 declarator constant-initializer [opt]
12870 identifier [opt] : constant-expression
12872 GNU Extensions:
12874 member-declaration:
12875 __extension__ member-declaration
12877 member-declarator:
12878 declarator attributes [opt] pure-specifier [opt]
12879 declarator attributes [opt] constant-initializer [opt]
12880 identifier [opt] attributes [opt] : constant-expression */
12882 static void
12883 cp_parser_member_declaration (cp_parser* parser)
12885 cp_decl_specifier_seq decl_specifiers;
12886 tree prefix_attributes;
12887 tree decl;
12888 int declares_class_or_enum;
12889 bool friend_p;
12890 cp_token *token;
12891 int saved_pedantic;
12893 /* Check for the `__extension__' keyword. */
12894 if (cp_parser_extension_opt (parser, &saved_pedantic))
12896 /* Recurse. */
12897 cp_parser_member_declaration (parser);
12898 /* Restore the old value of the PEDANTIC flag. */
12899 pedantic = saved_pedantic;
12901 return;
12904 /* Check for a template-declaration. */
12905 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12907 /* Parse the template-declaration. */
12908 cp_parser_template_declaration (parser, /*member_p=*/true);
12910 return;
12913 /* Check for a using-declaration. */
12914 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12916 /* Parse the using-declaration. */
12917 cp_parser_using_declaration (parser);
12919 return;
12922 /* Parse the decl-specifier-seq. */
12923 cp_parser_decl_specifier_seq (parser,
12924 CP_PARSER_FLAGS_OPTIONAL,
12925 &decl_specifiers,
12926 &declares_class_or_enum);
12927 prefix_attributes = decl_specifiers.attributes;
12928 decl_specifiers.attributes = NULL_TREE;
12929 /* Check for an invalid type-name. */
12930 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12931 return;
12932 /* If there is no declarator, then the decl-specifier-seq should
12933 specify a type. */
12934 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12936 /* If there was no decl-specifier-seq, and the next token is a
12937 `;', then we have something like:
12939 struct S { ; };
12941 [class.mem]
12943 Each member-declaration shall declare at least one member
12944 name of the class. */
12945 if (!decl_specifiers.any_specifiers_p)
12947 cp_token *token = cp_lexer_peek_token (parser->lexer);
12948 if (pedantic && !token->in_system_header)
12949 pedwarn ("%Hextra %<;%>", &token->location);
12951 else
12953 tree type;
12955 /* See if this declaration is a friend. */
12956 friend_p = cp_parser_friend_p (&decl_specifiers);
12957 /* If there were decl-specifiers, check to see if there was
12958 a class-declaration. */
12959 type = check_tag_decl (&decl_specifiers);
12960 /* Nested classes have already been added to the class, but
12961 a `friend' needs to be explicitly registered. */
12962 if (friend_p)
12964 /* If the `friend' keyword was present, the friend must
12965 be introduced with a class-key. */
12966 if (!declares_class_or_enum)
12967 error ("a class-key must be used when declaring a friend");
12968 /* In this case:
12970 template <typename T> struct A {
12971 friend struct A<T>::B;
12974 A<T>::B will be represented by a TYPENAME_TYPE, and
12975 therefore not recognized by check_tag_decl. */
12976 if (!type
12977 && decl_specifiers.type
12978 && TYPE_P (decl_specifiers.type))
12979 type = decl_specifiers.type;
12980 if (!type || !TYPE_P (type))
12981 error ("friend declaration does not name a class or "
12982 "function");
12983 else
12984 make_friend_class (current_class_type, type,
12985 /*complain=*/true);
12987 /* If there is no TYPE, an error message will already have
12988 been issued. */
12989 else if (!type || type == error_mark_node)
12991 /* An anonymous aggregate has to be handled specially; such
12992 a declaration really declares a data member (with a
12993 particular type), as opposed to a nested class. */
12994 else if (ANON_AGGR_TYPE_P (type))
12996 /* Remove constructors and such from TYPE, now that we
12997 know it is an anonymous aggregate. */
12998 fixup_anonymous_aggr (type);
12999 /* And make the corresponding data member. */
13000 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13001 /* Add it to the class. */
13002 finish_member_declaration (decl);
13004 else
13005 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13008 else
13010 /* See if these declarations will be friends. */
13011 friend_p = cp_parser_friend_p (&decl_specifiers);
13013 /* Keep going until we hit the `;' at the end of the
13014 declaration. */
13015 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13017 tree attributes = NULL_TREE;
13018 tree first_attribute;
13020 /* Peek at the next token. */
13021 token = cp_lexer_peek_token (parser->lexer);
13023 /* Check for a bitfield declaration. */
13024 if (token->type == CPP_COLON
13025 || (token->type == CPP_NAME
13026 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13027 == CPP_COLON))
13029 tree identifier;
13030 tree width;
13032 /* Get the name of the bitfield. Note that we cannot just
13033 check TOKEN here because it may have been invalidated by
13034 the call to cp_lexer_peek_nth_token above. */
13035 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13036 identifier = cp_parser_identifier (parser);
13037 else
13038 identifier = NULL_TREE;
13040 /* Consume the `:' token. */
13041 cp_lexer_consume_token (parser->lexer);
13042 /* Get the width of the bitfield. */
13043 width
13044 = cp_parser_constant_expression (parser,
13045 /*allow_non_constant=*/false,
13046 NULL);
13048 /* Look for attributes that apply to the bitfield. */
13049 attributes = cp_parser_attributes_opt (parser);
13050 /* Remember which attributes are prefix attributes and
13051 which are not. */
13052 first_attribute = attributes;
13053 /* Combine the attributes. */
13054 attributes = chainon (prefix_attributes, attributes);
13056 /* Create the bitfield declaration. */
13057 decl = grokbitfield (identifier
13058 ? make_id_declarator (identifier)
13059 : NULL,
13060 &decl_specifiers,
13061 width);
13062 /* Apply the attributes. */
13063 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13065 else
13067 cp_declarator *declarator;
13068 tree initializer;
13069 tree asm_specification;
13070 int ctor_dtor_or_conv_p;
13072 /* Parse the declarator. */
13073 declarator
13074 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13075 &ctor_dtor_or_conv_p,
13076 /*parenthesized_p=*/NULL);
13078 /* If something went wrong parsing the declarator, make sure
13079 that we at least consume some tokens. */
13080 if (declarator == cp_error_declarator)
13082 /* Skip to the end of the statement. */
13083 cp_parser_skip_to_end_of_statement (parser);
13084 /* If the next token is not a semicolon, that is
13085 probably because we just skipped over the body of
13086 a function. So, we consume a semicolon if
13087 present, but do not issue an error message if it
13088 is not present. */
13089 if (cp_lexer_next_token_is (parser->lexer,
13090 CPP_SEMICOLON))
13091 cp_lexer_consume_token (parser->lexer);
13092 return;
13095 cp_parser_check_for_definition_in_return_type
13096 (declarator, declares_class_or_enum);
13098 /* Look for an asm-specification. */
13099 asm_specification = cp_parser_asm_specification_opt (parser);
13100 /* Look for attributes that apply to the declaration. */
13101 attributes = cp_parser_attributes_opt (parser);
13102 /* Remember which attributes are prefix attributes and
13103 which are not. */
13104 first_attribute = attributes;
13105 /* Combine the attributes. */
13106 attributes = chainon (prefix_attributes, attributes);
13108 /* If it's an `=', then we have a constant-initializer or a
13109 pure-specifier. It is not correct to parse the
13110 initializer before registering the member declaration
13111 since the member declaration should be in scope while
13112 its initializer is processed. However, the rest of the
13113 front end does not yet provide an interface that allows
13114 us to handle this correctly. */
13115 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13117 /* In [class.mem]:
13119 A pure-specifier shall be used only in the declaration of
13120 a virtual function.
13122 A member-declarator can contain a constant-initializer
13123 only if it declares a static member of integral or
13124 enumeration type.
13126 Therefore, if the DECLARATOR is for a function, we look
13127 for a pure-specifier; otherwise, we look for a
13128 constant-initializer. When we call `grokfield', it will
13129 perform more stringent semantics checks. */
13130 if (declarator->kind == cdk_function)
13131 initializer = cp_parser_pure_specifier (parser);
13132 else
13133 /* Parse the initializer. */
13134 initializer = cp_parser_constant_initializer (parser);
13136 /* Otherwise, there is no initializer. */
13137 else
13138 initializer = NULL_TREE;
13140 /* See if we are probably looking at a function
13141 definition. We are certainly not looking at at a
13142 member-declarator. Calling `grokfield' has
13143 side-effects, so we must not do it unless we are sure
13144 that we are looking at a member-declarator. */
13145 if (cp_parser_token_starts_function_definition_p
13146 (cp_lexer_peek_token (parser->lexer)))
13148 /* The grammar does not allow a pure-specifier to be
13149 used when a member function is defined. (It is
13150 possible that this fact is an oversight in the
13151 standard, since a pure function may be defined
13152 outside of the class-specifier. */
13153 if (initializer)
13154 error ("pure-specifier on function-definition");
13155 decl = cp_parser_save_member_function_body (parser,
13156 &decl_specifiers,
13157 declarator,
13158 attributes);
13159 /* If the member was not a friend, declare it here. */
13160 if (!friend_p)
13161 finish_member_declaration (decl);
13162 /* Peek at the next token. */
13163 token = cp_lexer_peek_token (parser->lexer);
13164 /* If the next token is a semicolon, consume it. */
13165 if (token->type == CPP_SEMICOLON)
13166 cp_lexer_consume_token (parser->lexer);
13167 return;
13169 else
13171 /* Create the declaration. */
13172 decl = grokfield (declarator, &decl_specifiers,
13173 initializer, asm_specification,
13174 attributes);
13175 /* Any initialization must have been from a
13176 constant-expression. */
13177 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13178 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13182 /* Reset PREFIX_ATTRIBUTES. */
13183 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13184 attributes = TREE_CHAIN (attributes);
13185 if (attributes)
13186 TREE_CHAIN (attributes) = NULL_TREE;
13188 /* If there is any qualification still in effect, clear it
13189 now; we will be starting fresh with the next declarator. */
13190 parser->scope = NULL_TREE;
13191 parser->qualifying_scope = NULL_TREE;
13192 parser->object_scope = NULL_TREE;
13193 /* If it's a `,', then there are more declarators. */
13194 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13195 cp_lexer_consume_token (parser->lexer);
13196 /* If the next token isn't a `;', then we have a parse error. */
13197 else if (cp_lexer_next_token_is_not (parser->lexer,
13198 CPP_SEMICOLON))
13200 cp_parser_error (parser, "expected %<;%>");
13201 /* Skip tokens until we find a `;'. */
13202 cp_parser_skip_to_end_of_statement (parser);
13204 break;
13207 if (decl)
13209 /* Add DECL to the list of members. */
13210 if (!friend_p)
13211 finish_member_declaration (decl);
13213 if (TREE_CODE (decl) == FUNCTION_DECL)
13214 cp_parser_save_default_args (parser, decl);
13219 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13222 /* Parse a pure-specifier.
13224 pure-specifier:
13227 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13228 Otherwise, ERROR_MARK_NODE is returned. */
13230 static tree
13231 cp_parser_pure_specifier (cp_parser* parser)
13233 cp_token *token;
13235 /* Look for the `=' token. */
13236 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13237 return error_mark_node;
13238 /* Look for the `0' token. */
13239 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13240 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13241 to get information from the lexer about how the number was
13242 spelled in order to fix this problem. */
13243 if (!token || !integer_zerop (token->value))
13244 return error_mark_node;
13246 return integer_zero_node;
13249 /* Parse a constant-initializer.
13251 constant-initializer:
13252 = constant-expression
13254 Returns a representation of the constant-expression. */
13256 static tree
13257 cp_parser_constant_initializer (cp_parser* parser)
13259 /* Look for the `=' token. */
13260 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13261 return error_mark_node;
13263 /* It is invalid to write:
13265 struct S { static const int i = { 7 }; };
13268 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13270 cp_parser_error (parser,
13271 "a brace-enclosed initializer is not allowed here");
13272 /* Consume the opening brace. */
13273 cp_lexer_consume_token (parser->lexer);
13274 /* Skip the initializer. */
13275 cp_parser_skip_to_closing_brace (parser);
13276 /* Look for the trailing `}'. */
13277 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13279 return error_mark_node;
13282 return cp_parser_constant_expression (parser,
13283 /*allow_non_constant=*/false,
13284 NULL);
13287 /* Derived classes [gram.class.derived] */
13289 /* Parse a base-clause.
13291 base-clause:
13292 : base-specifier-list
13294 base-specifier-list:
13295 base-specifier
13296 base-specifier-list , base-specifier
13298 Returns a TREE_LIST representing the base-classes, in the order in
13299 which they were declared. The representation of each node is as
13300 described by cp_parser_base_specifier.
13302 In the case that no bases are specified, this function will return
13303 NULL_TREE, not ERROR_MARK_NODE. */
13305 static tree
13306 cp_parser_base_clause (cp_parser* parser)
13308 tree bases = NULL_TREE;
13310 /* Look for the `:' that begins the list. */
13311 cp_parser_require (parser, CPP_COLON, "`:'");
13313 /* Scan the base-specifier-list. */
13314 while (true)
13316 cp_token *token;
13317 tree base;
13319 /* Look for the base-specifier. */
13320 base = cp_parser_base_specifier (parser);
13321 /* Add BASE to the front of the list. */
13322 if (base != error_mark_node)
13324 TREE_CHAIN (base) = bases;
13325 bases = base;
13327 /* Peek at the next token. */
13328 token = cp_lexer_peek_token (parser->lexer);
13329 /* If it's not a comma, then the list is complete. */
13330 if (token->type != CPP_COMMA)
13331 break;
13332 /* Consume the `,'. */
13333 cp_lexer_consume_token (parser->lexer);
13336 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13337 base class had a qualified name. However, the next name that
13338 appears is certainly not qualified. */
13339 parser->scope = NULL_TREE;
13340 parser->qualifying_scope = NULL_TREE;
13341 parser->object_scope = NULL_TREE;
13343 return nreverse (bases);
13346 /* Parse a base-specifier.
13348 base-specifier:
13349 :: [opt] nested-name-specifier [opt] class-name
13350 virtual access-specifier [opt] :: [opt] nested-name-specifier
13351 [opt] class-name
13352 access-specifier virtual [opt] :: [opt] nested-name-specifier
13353 [opt] class-name
13355 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13356 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13357 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13358 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13360 static tree
13361 cp_parser_base_specifier (cp_parser* parser)
13363 cp_token *token;
13364 bool done = false;
13365 bool virtual_p = false;
13366 bool duplicate_virtual_error_issued_p = false;
13367 bool duplicate_access_error_issued_p = false;
13368 bool class_scope_p, template_p;
13369 tree access = access_default_node;
13370 tree type;
13372 /* Process the optional `virtual' and `access-specifier'. */
13373 while (!done)
13375 /* Peek at the next token. */
13376 token = cp_lexer_peek_token (parser->lexer);
13377 /* Process `virtual'. */
13378 switch (token->keyword)
13380 case RID_VIRTUAL:
13381 /* If `virtual' appears more than once, issue an error. */
13382 if (virtual_p && !duplicate_virtual_error_issued_p)
13384 cp_parser_error (parser,
13385 "%<virtual%> specified more than once in base-specified");
13386 duplicate_virtual_error_issued_p = true;
13389 virtual_p = true;
13391 /* Consume the `virtual' token. */
13392 cp_lexer_consume_token (parser->lexer);
13394 break;
13396 case RID_PUBLIC:
13397 case RID_PROTECTED:
13398 case RID_PRIVATE:
13399 /* If more than one access specifier appears, issue an
13400 error. */
13401 if (access != access_default_node
13402 && !duplicate_access_error_issued_p)
13404 cp_parser_error (parser,
13405 "more than one access specifier in base-specified");
13406 duplicate_access_error_issued_p = true;
13409 access = ridpointers[(int) token->keyword];
13411 /* Consume the access-specifier. */
13412 cp_lexer_consume_token (parser->lexer);
13414 break;
13416 default:
13417 done = true;
13418 break;
13421 /* It is not uncommon to see programs mechanically, erroneously, use
13422 the 'typename' keyword to denote (dependent) qualified types
13423 as base classes. */
13424 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13426 if (!processing_template_decl)
13427 error ("keyword %<typename%> not allowed outside of templates");
13428 else
13429 error ("keyword %<typename%> not allowed in this context "
13430 "(the base class is implicitly a type)");
13431 cp_lexer_consume_token (parser->lexer);
13434 /* Look for the optional `::' operator. */
13435 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13436 /* Look for the nested-name-specifier. The simplest way to
13437 implement:
13439 [temp.res]
13441 The keyword `typename' is not permitted in a base-specifier or
13442 mem-initializer; in these contexts a qualified name that
13443 depends on a template-parameter is implicitly assumed to be a
13444 type name.
13446 is to pretend that we have seen the `typename' keyword at this
13447 point. */
13448 cp_parser_nested_name_specifier_opt (parser,
13449 /*typename_keyword_p=*/true,
13450 /*check_dependency_p=*/true,
13451 /*type_p=*/true,
13452 /*is_declaration=*/true);
13453 /* If the base class is given by a qualified name, assume that names
13454 we see are type names or templates, as appropriate. */
13455 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13456 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13458 /* Finally, look for the class-name. */
13459 type = cp_parser_class_name (parser,
13460 class_scope_p,
13461 template_p,
13462 /*type_p=*/true,
13463 /*check_dependency_p=*/true,
13464 /*class_head_p=*/false,
13465 /*is_declaration=*/true);
13467 if (type == error_mark_node)
13468 return error_mark_node;
13470 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13473 /* Exception handling [gram.exception] */
13475 /* Parse an (optional) exception-specification.
13477 exception-specification:
13478 throw ( type-id-list [opt] )
13480 Returns a TREE_LIST representing the exception-specification. The
13481 TREE_VALUE of each node is a type. */
13483 static tree
13484 cp_parser_exception_specification_opt (cp_parser* parser)
13486 cp_token *token;
13487 tree type_id_list;
13489 /* Peek at the next token. */
13490 token = cp_lexer_peek_token (parser->lexer);
13491 /* If it's not `throw', then there's no exception-specification. */
13492 if (!cp_parser_is_keyword (token, RID_THROW))
13493 return NULL_TREE;
13495 /* Consume the `throw'. */
13496 cp_lexer_consume_token (parser->lexer);
13498 /* Look for the `('. */
13499 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13501 /* Peek at the next token. */
13502 token = cp_lexer_peek_token (parser->lexer);
13503 /* If it's not a `)', then there is a type-id-list. */
13504 if (token->type != CPP_CLOSE_PAREN)
13506 const char *saved_message;
13508 /* Types may not be defined in an exception-specification. */
13509 saved_message = parser->type_definition_forbidden_message;
13510 parser->type_definition_forbidden_message
13511 = "types may not be defined in an exception-specification";
13512 /* Parse the type-id-list. */
13513 type_id_list = cp_parser_type_id_list (parser);
13514 /* Restore the saved message. */
13515 parser->type_definition_forbidden_message = saved_message;
13517 else
13518 type_id_list = empty_except_spec;
13520 /* Look for the `)'. */
13521 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13523 return type_id_list;
13526 /* Parse an (optional) type-id-list.
13528 type-id-list:
13529 type-id
13530 type-id-list , type-id
13532 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13533 in the order that the types were presented. */
13535 static tree
13536 cp_parser_type_id_list (cp_parser* parser)
13538 tree types = NULL_TREE;
13540 while (true)
13542 cp_token *token;
13543 tree type;
13545 /* Get the next type-id. */
13546 type = cp_parser_type_id (parser);
13547 /* Add it to the list. */
13548 types = add_exception_specifier (types, type, /*complain=*/1);
13549 /* Peek at the next token. */
13550 token = cp_lexer_peek_token (parser->lexer);
13551 /* If it is not a `,', we are done. */
13552 if (token->type != CPP_COMMA)
13553 break;
13554 /* Consume the `,'. */
13555 cp_lexer_consume_token (parser->lexer);
13558 return nreverse (types);
13561 /* Parse a try-block.
13563 try-block:
13564 try compound-statement handler-seq */
13566 static tree
13567 cp_parser_try_block (cp_parser* parser)
13569 tree try_block;
13571 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13572 try_block = begin_try_block ();
13573 cp_parser_compound_statement (parser, NULL, true);
13574 finish_try_block (try_block);
13575 cp_parser_handler_seq (parser);
13576 finish_handler_sequence (try_block);
13578 return try_block;
13581 /* Parse a function-try-block.
13583 function-try-block:
13584 try ctor-initializer [opt] function-body handler-seq */
13586 static bool
13587 cp_parser_function_try_block (cp_parser* parser)
13589 tree try_block;
13590 bool ctor_initializer_p;
13592 /* Look for the `try' keyword. */
13593 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13594 return false;
13595 /* Let the rest of the front-end know where we are. */
13596 try_block = begin_function_try_block ();
13597 /* Parse the function-body. */
13598 ctor_initializer_p
13599 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13600 /* We're done with the `try' part. */
13601 finish_function_try_block (try_block);
13602 /* Parse the handlers. */
13603 cp_parser_handler_seq (parser);
13604 /* We're done with the handlers. */
13605 finish_function_handler_sequence (try_block);
13607 return ctor_initializer_p;
13610 /* Parse a handler-seq.
13612 handler-seq:
13613 handler handler-seq [opt] */
13615 static void
13616 cp_parser_handler_seq (cp_parser* parser)
13618 while (true)
13620 cp_token *token;
13622 /* Parse the handler. */
13623 cp_parser_handler (parser);
13624 /* Peek at the next token. */
13625 token = cp_lexer_peek_token (parser->lexer);
13626 /* If it's not `catch' then there are no more handlers. */
13627 if (!cp_parser_is_keyword (token, RID_CATCH))
13628 break;
13632 /* Parse a handler.
13634 handler:
13635 catch ( exception-declaration ) compound-statement */
13637 static void
13638 cp_parser_handler (cp_parser* parser)
13640 tree handler;
13641 tree declaration;
13643 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13644 handler = begin_handler ();
13645 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13646 declaration = cp_parser_exception_declaration (parser);
13647 finish_handler_parms (declaration, handler);
13648 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13649 cp_parser_compound_statement (parser, NULL, false);
13650 finish_handler (handler);
13653 /* Parse an exception-declaration.
13655 exception-declaration:
13656 type-specifier-seq declarator
13657 type-specifier-seq abstract-declarator
13658 type-specifier-seq
13661 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13662 ellipsis variant is used. */
13664 static tree
13665 cp_parser_exception_declaration (cp_parser* parser)
13667 tree decl;
13668 cp_decl_specifier_seq type_specifiers;
13669 cp_declarator *declarator;
13670 const char *saved_message;
13672 /* If it's an ellipsis, it's easy to handle. */
13673 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13675 /* Consume the `...' token. */
13676 cp_lexer_consume_token (parser->lexer);
13677 return NULL_TREE;
13680 /* Types may not be defined in exception-declarations. */
13681 saved_message = parser->type_definition_forbidden_message;
13682 parser->type_definition_forbidden_message
13683 = "types may not be defined in exception-declarations";
13685 /* Parse the type-specifier-seq. */
13686 cp_parser_type_specifier_seq (parser, &type_specifiers);
13687 /* If it's a `)', then there is no declarator. */
13688 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13689 declarator = NULL;
13690 else
13691 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13692 /*ctor_dtor_or_conv_p=*/NULL,
13693 /*parenthesized_p=*/NULL);
13695 /* Restore the saved message. */
13696 parser->type_definition_forbidden_message = saved_message;
13698 if (type_specifiers.any_specifiers_p)
13700 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13701 if (decl == NULL_TREE)
13702 error ("invalid catch parameter");
13704 else
13705 decl = NULL_TREE;
13707 return decl;
13710 /* Parse a throw-expression.
13712 throw-expression:
13713 throw assignment-expression [opt]
13715 Returns a THROW_EXPR representing the throw-expression. */
13717 static tree
13718 cp_parser_throw_expression (cp_parser* parser)
13720 tree expression;
13721 cp_token* token;
13723 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13724 token = cp_lexer_peek_token (parser->lexer);
13725 /* Figure out whether or not there is an assignment-expression
13726 following the "throw" keyword. */
13727 if (token->type == CPP_COMMA
13728 || token->type == CPP_SEMICOLON
13729 || token->type == CPP_CLOSE_PAREN
13730 || token->type == CPP_CLOSE_SQUARE
13731 || token->type == CPP_CLOSE_BRACE
13732 || token->type == CPP_COLON)
13733 expression = NULL_TREE;
13734 else
13735 expression = cp_parser_assignment_expression (parser);
13737 return build_throw (expression);
13740 /* GNU Extensions */
13742 /* Parse an (optional) asm-specification.
13744 asm-specification:
13745 asm ( string-literal )
13747 If the asm-specification is present, returns a STRING_CST
13748 corresponding to the string-literal. Otherwise, returns
13749 NULL_TREE. */
13751 static tree
13752 cp_parser_asm_specification_opt (cp_parser* parser)
13754 cp_token *token;
13755 tree asm_specification;
13757 /* Peek at the next token. */
13758 token = cp_lexer_peek_token (parser->lexer);
13759 /* If the next token isn't the `asm' keyword, then there's no
13760 asm-specification. */
13761 if (!cp_parser_is_keyword (token, RID_ASM))
13762 return NULL_TREE;
13764 /* Consume the `asm' token. */
13765 cp_lexer_consume_token (parser->lexer);
13766 /* Look for the `('. */
13767 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13769 /* Look for the string-literal. */
13770 asm_specification = cp_parser_string_literal (parser, false, false);
13772 /* Look for the `)'. */
13773 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13775 return asm_specification;
13778 /* Parse an asm-operand-list.
13780 asm-operand-list:
13781 asm-operand
13782 asm-operand-list , asm-operand
13784 asm-operand:
13785 string-literal ( expression )
13786 [ string-literal ] string-literal ( expression )
13788 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13789 each node is the expression. The TREE_PURPOSE is itself a
13790 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13791 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13792 is a STRING_CST for the string literal before the parenthesis. */
13794 static tree
13795 cp_parser_asm_operand_list (cp_parser* parser)
13797 tree asm_operands = NULL_TREE;
13799 while (true)
13801 tree string_literal;
13802 tree expression;
13803 tree name;
13805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13807 /* Consume the `[' token. */
13808 cp_lexer_consume_token (parser->lexer);
13809 /* Read the operand name. */
13810 name = cp_parser_identifier (parser);
13811 if (name != error_mark_node)
13812 name = build_string (IDENTIFIER_LENGTH (name),
13813 IDENTIFIER_POINTER (name));
13814 /* Look for the closing `]'. */
13815 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13817 else
13818 name = NULL_TREE;
13819 /* Look for the string-literal. */
13820 string_literal = cp_parser_string_literal (parser, false, false);
13822 /* Look for the `('. */
13823 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13824 /* Parse the expression. */
13825 expression = cp_parser_expression (parser);
13826 /* Look for the `)'. */
13827 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13829 /* Add this operand to the list. */
13830 asm_operands = tree_cons (build_tree_list (name, string_literal),
13831 expression,
13832 asm_operands);
13833 /* If the next token is not a `,', there are no more
13834 operands. */
13835 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13836 break;
13837 /* Consume the `,'. */
13838 cp_lexer_consume_token (parser->lexer);
13841 return nreverse (asm_operands);
13844 /* Parse an asm-clobber-list.
13846 asm-clobber-list:
13847 string-literal
13848 asm-clobber-list , string-literal
13850 Returns a TREE_LIST, indicating the clobbers in the order that they
13851 appeared. The TREE_VALUE of each node is a STRING_CST. */
13853 static tree
13854 cp_parser_asm_clobber_list (cp_parser* parser)
13856 tree clobbers = NULL_TREE;
13858 while (true)
13860 tree string_literal;
13862 /* Look for the string literal. */
13863 string_literal = cp_parser_string_literal (parser, false, false);
13864 /* Add it to the list. */
13865 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13866 /* If the next token is not a `,', then the list is
13867 complete. */
13868 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13869 break;
13870 /* Consume the `,' token. */
13871 cp_lexer_consume_token (parser->lexer);
13874 return clobbers;
13877 /* Parse an (optional) series of attributes.
13879 attributes:
13880 attributes attribute
13882 attribute:
13883 __attribute__ (( attribute-list [opt] ))
13885 The return value is as for cp_parser_attribute_list. */
13887 static tree
13888 cp_parser_attributes_opt (cp_parser* parser)
13890 tree attributes = NULL_TREE;
13892 while (true)
13894 cp_token *token;
13895 tree attribute_list;
13897 /* Peek at the next token. */
13898 token = cp_lexer_peek_token (parser->lexer);
13899 /* If it's not `__attribute__', then we're done. */
13900 if (token->keyword != RID_ATTRIBUTE)
13901 break;
13903 /* Consume the `__attribute__' keyword. */
13904 cp_lexer_consume_token (parser->lexer);
13905 /* Look for the two `(' tokens. */
13906 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13907 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13909 /* Peek at the next token. */
13910 token = cp_lexer_peek_token (parser->lexer);
13911 if (token->type != CPP_CLOSE_PAREN)
13912 /* Parse the attribute-list. */
13913 attribute_list = cp_parser_attribute_list (parser);
13914 else
13915 /* If the next token is a `)', then there is no attribute
13916 list. */
13917 attribute_list = NULL;
13919 /* Look for the two `)' tokens. */
13920 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13921 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13923 /* Add these new attributes to the list. */
13924 attributes = chainon (attributes, attribute_list);
13927 return attributes;
13930 /* Parse an attribute-list.
13932 attribute-list:
13933 attribute
13934 attribute-list , attribute
13936 attribute:
13937 identifier
13938 identifier ( identifier )
13939 identifier ( identifier , expression-list )
13940 identifier ( expression-list )
13942 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13943 TREE_PURPOSE of each node is the identifier indicating which
13944 attribute is in use. The TREE_VALUE represents the arguments, if
13945 any. */
13947 static tree
13948 cp_parser_attribute_list (cp_parser* parser)
13950 tree attribute_list = NULL_TREE;
13951 bool save_translate_strings_p = parser->translate_strings_p;
13953 parser->translate_strings_p = false;
13954 while (true)
13956 cp_token *token;
13957 tree identifier;
13958 tree attribute;
13960 /* Look for the identifier. We also allow keywords here; for
13961 example `__attribute__ ((const))' is legal. */
13962 token = cp_lexer_peek_token (parser->lexer);
13963 if (token->type != CPP_NAME
13964 && token->type != CPP_KEYWORD)
13965 return error_mark_node;
13966 /* Consume the token. */
13967 token = cp_lexer_consume_token (parser->lexer);
13969 /* Save away the identifier that indicates which attribute this is. */
13970 identifier = token->value;
13971 attribute = build_tree_list (identifier, NULL_TREE);
13973 /* Peek at the next token. */
13974 token = cp_lexer_peek_token (parser->lexer);
13975 /* If it's an `(', then parse the attribute arguments. */
13976 if (token->type == CPP_OPEN_PAREN)
13978 tree arguments;
13980 arguments = (cp_parser_parenthesized_expression_list
13981 (parser, true, /*non_constant_p=*/NULL));
13982 /* Save the identifier and arguments away. */
13983 TREE_VALUE (attribute) = arguments;
13986 /* Add this attribute to the list. */
13987 TREE_CHAIN (attribute) = attribute_list;
13988 attribute_list = attribute;
13990 /* Now, look for more attributes. */
13991 token = cp_lexer_peek_token (parser->lexer);
13992 /* If the next token isn't a `,', we're done. */
13993 if (token->type != CPP_COMMA)
13994 break;
13996 /* Consume the comma and keep going. */
13997 cp_lexer_consume_token (parser->lexer);
13999 parser->translate_strings_p = save_translate_strings_p;
14001 /* We built up the list in reverse order. */
14002 return nreverse (attribute_list);
14005 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14006 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14007 current value of the PEDANTIC flag, regardless of whether or not
14008 the `__extension__' keyword is present. The caller is responsible
14009 for restoring the value of the PEDANTIC flag. */
14011 static bool
14012 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14014 /* Save the old value of the PEDANTIC flag. */
14015 *saved_pedantic = pedantic;
14017 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14019 /* Consume the `__extension__' token. */
14020 cp_lexer_consume_token (parser->lexer);
14021 /* We're not being pedantic while the `__extension__' keyword is
14022 in effect. */
14023 pedantic = 0;
14025 return true;
14028 return false;
14031 /* Parse a label declaration.
14033 label-declaration:
14034 __label__ label-declarator-seq ;
14036 label-declarator-seq:
14037 identifier , label-declarator-seq
14038 identifier */
14040 static void
14041 cp_parser_label_declaration (cp_parser* parser)
14043 /* Look for the `__label__' keyword. */
14044 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14046 while (true)
14048 tree identifier;
14050 /* Look for an identifier. */
14051 identifier = cp_parser_identifier (parser);
14052 /* Declare it as a lobel. */
14053 finish_label_decl (identifier);
14054 /* If the next token is a `;', stop. */
14055 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14056 break;
14057 /* Look for the `,' separating the label declarations. */
14058 cp_parser_require (parser, CPP_COMMA, "`,'");
14061 /* Look for the final `;'. */
14062 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14065 /* Support Functions */
14067 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14068 NAME should have one of the representations used for an
14069 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14070 is returned. If PARSER->SCOPE is a dependent type, then a
14071 SCOPE_REF is returned.
14073 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14074 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14075 was formed. Abstractly, such entities should not be passed to this
14076 function, because they do not need to be looked up, but it is
14077 simpler to check for this special case here, rather than at the
14078 call-sites.
14080 In cases not explicitly covered above, this function returns a
14081 DECL, OVERLOAD, or baselink representing the result of the lookup.
14082 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14083 is returned.
14085 If IS_TYPE is TRUE, bindings that do not refer to types are
14086 ignored.
14088 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14089 ignored.
14091 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14092 are ignored.
14094 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14095 types.
14097 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14098 results in an ambiguity, and false otherwise. */
14100 static tree
14101 cp_parser_lookup_name (cp_parser *parser, tree name,
14102 bool is_type, bool is_template, bool is_namespace,
14103 bool check_dependency,
14104 bool *ambiguous_p)
14106 tree decl;
14107 tree object_type = parser->context->object_type;
14109 /* Assume that the lookup will be unambiguous. */
14110 if (ambiguous_p)
14111 *ambiguous_p = false;
14113 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14114 no longer valid. Note that if we are parsing tentatively, and
14115 the parse fails, OBJECT_TYPE will be automatically restored. */
14116 parser->context->object_type = NULL_TREE;
14118 if (name == error_mark_node)
14119 return error_mark_node;
14121 /* A template-id has already been resolved; there is no lookup to
14122 do. */
14123 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14124 return name;
14125 if (BASELINK_P (name))
14127 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14128 == TEMPLATE_ID_EXPR);
14129 return name;
14132 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14133 it should already have been checked to make sure that the name
14134 used matches the type being destroyed. */
14135 if (TREE_CODE (name) == BIT_NOT_EXPR)
14137 tree type;
14139 /* Figure out to which type this destructor applies. */
14140 if (parser->scope)
14141 type = parser->scope;
14142 else if (object_type)
14143 type = object_type;
14144 else
14145 type = current_class_type;
14146 /* If that's not a class type, there is no destructor. */
14147 if (!type || !CLASS_TYPE_P (type))
14148 return error_mark_node;
14149 if (!CLASSTYPE_DESTRUCTORS (type))
14150 return error_mark_node;
14151 /* If it was a class type, return the destructor. */
14152 return CLASSTYPE_DESTRUCTORS (type);
14155 /* By this point, the NAME should be an ordinary identifier. If
14156 the id-expression was a qualified name, the qualifying scope is
14157 stored in PARSER->SCOPE at this point. */
14158 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14160 /* Perform the lookup. */
14161 if (parser->scope)
14163 bool dependent_p;
14165 if (parser->scope == error_mark_node)
14166 return error_mark_node;
14168 /* If the SCOPE is dependent, the lookup must be deferred until
14169 the template is instantiated -- unless we are explicitly
14170 looking up names in uninstantiated templates. Even then, we
14171 cannot look up the name if the scope is not a class type; it
14172 might, for example, be a template type parameter. */
14173 dependent_p = (TYPE_P (parser->scope)
14174 && !(parser->in_declarator_p
14175 && currently_open_class (parser->scope))
14176 && dependent_type_p (parser->scope));
14177 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14178 && dependent_p)
14180 if (is_type)
14181 /* The resolution to Core Issue 180 says that `struct A::B'
14182 should be considered a type-name, even if `A' is
14183 dependent. */
14184 decl = TYPE_NAME (make_typename_type (parser->scope,
14185 name,
14186 /*complain=*/1));
14187 else if (is_template)
14188 decl = make_unbound_class_template (parser->scope,
14189 name,
14190 /*complain=*/1);
14191 else
14192 decl = build_nt (SCOPE_REF, parser->scope, name);
14194 else
14196 bool pop_p = false;
14198 /* If PARSER->SCOPE is a dependent type, then it must be a
14199 class type, and we must not be checking dependencies;
14200 otherwise, we would have processed this lookup above. So
14201 that PARSER->SCOPE is not considered a dependent base by
14202 lookup_member, we must enter the scope here. */
14203 if (dependent_p)
14204 pop_p = push_scope (parser->scope);
14205 /* If the PARSER->SCOPE is a a template specialization, it
14206 may be instantiated during name lookup. In that case,
14207 errors may be issued. Even if we rollback the current
14208 tentative parse, those errors are valid. */
14209 decl = lookup_qualified_name (parser->scope, name, is_type,
14210 /*complain=*/true);
14211 if (pop_p)
14212 pop_scope (parser->scope);
14214 parser->qualifying_scope = parser->scope;
14215 parser->object_scope = NULL_TREE;
14217 else if (object_type)
14219 tree object_decl = NULL_TREE;
14220 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14221 OBJECT_TYPE is not a class. */
14222 if (CLASS_TYPE_P (object_type))
14223 /* If the OBJECT_TYPE is a template specialization, it may
14224 be instantiated during name lookup. In that case, errors
14225 may be issued. Even if we rollback the current tentative
14226 parse, those errors are valid. */
14227 object_decl = lookup_member (object_type,
14228 name,
14229 /*protect=*/0, is_type);
14230 /* Look it up in the enclosing context, too. */
14231 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14232 /*block_p=*/true, is_namespace,
14233 /*flags=*/0);
14234 parser->object_scope = object_type;
14235 parser->qualifying_scope = NULL_TREE;
14236 if (object_decl)
14237 decl = object_decl;
14239 else
14241 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14242 /*block_p=*/true, is_namespace,
14243 /*flags=*/0);
14244 parser->qualifying_scope = NULL_TREE;
14245 parser->object_scope = NULL_TREE;
14248 /* If the lookup failed, let our caller know. */
14249 if (!decl
14250 || decl == error_mark_node
14251 || (TREE_CODE (decl) == FUNCTION_DECL
14252 && DECL_ANTICIPATED (decl)))
14253 return error_mark_node;
14255 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14256 if (TREE_CODE (decl) == TREE_LIST)
14258 if (ambiguous_p)
14259 *ambiguous_p = true;
14260 /* The error message we have to print is too complicated for
14261 cp_parser_error, so we incorporate its actions directly. */
14262 if (!cp_parser_simulate_error (parser))
14264 error ("reference to %qD is ambiguous", name);
14265 print_candidates (decl);
14267 return error_mark_node;
14270 gcc_assert (DECL_P (decl)
14271 || TREE_CODE (decl) == OVERLOAD
14272 || TREE_CODE (decl) == SCOPE_REF
14273 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14274 || BASELINK_P (decl));
14276 /* If we have resolved the name of a member declaration, check to
14277 see if the declaration is accessible. When the name resolves to
14278 set of overloaded functions, accessibility is checked when
14279 overload resolution is done.
14281 During an explicit instantiation, access is not checked at all,
14282 as per [temp.explicit]. */
14283 if (DECL_P (decl))
14284 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14286 return decl;
14289 /* Like cp_parser_lookup_name, but for use in the typical case where
14290 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14291 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14293 static tree
14294 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14296 return cp_parser_lookup_name (parser, name,
14297 /*is_type=*/false,
14298 /*is_template=*/false,
14299 /*is_namespace=*/false,
14300 /*check_dependency=*/true,
14301 /*ambiguous_p=*/NULL);
14304 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14305 the current context, return the TYPE_DECL. If TAG_NAME_P is
14306 true, the DECL indicates the class being defined in a class-head,
14307 or declared in an elaborated-type-specifier.
14309 Otherwise, return DECL. */
14311 static tree
14312 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14314 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14315 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14317 struct A {
14318 template <typename T> struct B;
14321 template <typename T> struct A::B {};
14323 Similarly, in a elaborated-type-specifier:
14325 namespace N { struct X{}; }
14327 struct A {
14328 template <typename T> friend struct N::X;
14331 However, if the DECL refers to a class type, and we are in
14332 the scope of the class, then the name lookup automatically
14333 finds the TYPE_DECL created by build_self_reference rather
14334 than a TEMPLATE_DECL. For example, in:
14336 template <class T> struct S {
14337 S s;
14340 there is no need to handle such case. */
14342 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14343 return DECL_TEMPLATE_RESULT (decl);
14345 return decl;
14348 /* If too many, or too few, template-parameter lists apply to the
14349 declarator, issue an error message. Returns TRUE if all went well,
14350 and FALSE otherwise. */
14352 static bool
14353 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14354 cp_declarator *declarator)
14356 unsigned num_templates;
14358 /* We haven't seen any classes that involve template parameters yet. */
14359 num_templates = 0;
14361 switch (declarator->kind)
14363 case cdk_id:
14364 if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14366 tree scope;
14367 tree member;
14369 scope = TREE_OPERAND (declarator->u.id.name, 0);
14370 member = TREE_OPERAND (declarator->u.id.name, 1);
14372 while (scope && CLASS_TYPE_P (scope))
14374 /* You're supposed to have one `template <...>'
14375 for every template class, but you don't need one
14376 for a full specialization. For example:
14378 template <class T> struct S{};
14379 template <> struct S<int> { void f(); };
14380 void S<int>::f () {}
14382 is correct; there shouldn't be a `template <>' for
14383 the definition of `S<int>::f'. */
14384 if (CLASSTYPE_TEMPLATE_INFO (scope)
14385 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14386 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14387 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14388 ++num_templates;
14390 scope = TYPE_CONTEXT (scope);
14394 /* If the DECLARATOR has the form `X<y>' then it uses one
14395 additional level of template parameters. */
14396 if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14397 ++num_templates;
14399 return cp_parser_check_template_parameters (parser,
14400 num_templates);
14402 case cdk_function:
14403 case cdk_array:
14404 case cdk_pointer:
14405 case cdk_reference:
14406 case cdk_ptrmem:
14407 return (cp_parser_check_declarator_template_parameters
14408 (parser, declarator->declarator));
14410 case cdk_error:
14411 return true;
14413 default:
14414 gcc_unreachable ();
14416 return false;
14419 /* NUM_TEMPLATES were used in the current declaration. If that is
14420 invalid, return FALSE and issue an error messages. Otherwise,
14421 return TRUE. */
14423 static bool
14424 cp_parser_check_template_parameters (cp_parser* parser,
14425 unsigned num_templates)
14427 /* If there are more template classes than parameter lists, we have
14428 something like:
14430 template <class T> void S<T>::R<T>::f (); */
14431 if (parser->num_template_parameter_lists < num_templates)
14433 error ("too few template-parameter-lists");
14434 return false;
14436 /* If there are the same number of template classes and parameter
14437 lists, that's OK. */
14438 if (parser->num_template_parameter_lists == num_templates)
14439 return true;
14440 /* If there are more, but only one more, then we are referring to a
14441 member template. That's OK too. */
14442 if (parser->num_template_parameter_lists == num_templates + 1)
14443 return true;
14444 /* Otherwise, there are too many template parameter lists. We have
14445 something like:
14447 template <class T> template <class U> void S::f(); */
14448 error ("too many template-parameter-lists");
14449 return false;
14452 /* Parse an optional `::' token indicating that the following name is
14453 from the global namespace. If so, PARSER->SCOPE is set to the
14454 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14455 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14456 Returns the new value of PARSER->SCOPE, if the `::' token is
14457 present, and NULL_TREE otherwise. */
14459 static tree
14460 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14462 cp_token *token;
14464 /* Peek at the next token. */
14465 token = cp_lexer_peek_token (parser->lexer);
14466 /* If we're looking at a `::' token then we're starting from the
14467 global namespace, not our current location. */
14468 if (token->type == CPP_SCOPE)
14470 /* Consume the `::' token. */
14471 cp_lexer_consume_token (parser->lexer);
14472 /* Set the SCOPE so that we know where to start the lookup. */
14473 parser->scope = global_namespace;
14474 parser->qualifying_scope = global_namespace;
14475 parser->object_scope = NULL_TREE;
14477 return parser->scope;
14479 else if (!current_scope_valid_p)
14481 parser->scope = NULL_TREE;
14482 parser->qualifying_scope = NULL_TREE;
14483 parser->object_scope = NULL_TREE;
14486 return NULL_TREE;
14489 /* Returns TRUE if the upcoming token sequence is the start of a
14490 constructor declarator. If FRIEND_P is true, the declarator is
14491 preceded by the `friend' specifier. */
14493 static bool
14494 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14496 bool constructor_p;
14497 tree type_decl = NULL_TREE;
14498 bool nested_name_p;
14499 cp_token *next_token;
14501 /* The common case is that this is not a constructor declarator, so
14502 try to avoid doing lots of work if at all possible. It's not
14503 valid declare a constructor at function scope. */
14504 if (at_function_scope_p ())
14505 return false;
14506 /* And only certain tokens can begin a constructor declarator. */
14507 next_token = cp_lexer_peek_token (parser->lexer);
14508 if (next_token->type != CPP_NAME
14509 && next_token->type != CPP_SCOPE
14510 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14511 && next_token->type != CPP_TEMPLATE_ID)
14512 return false;
14514 /* Parse tentatively; we are going to roll back all of the tokens
14515 consumed here. */
14516 cp_parser_parse_tentatively (parser);
14517 /* Assume that we are looking at a constructor declarator. */
14518 constructor_p = true;
14520 /* Look for the optional `::' operator. */
14521 cp_parser_global_scope_opt (parser,
14522 /*current_scope_valid_p=*/false);
14523 /* Look for the nested-name-specifier. */
14524 nested_name_p
14525 = (cp_parser_nested_name_specifier_opt (parser,
14526 /*typename_keyword_p=*/false,
14527 /*check_dependency_p=*/false,
14528 /*type_p=*/false,
14529 /*is_declaration=*/false)
14530 != NULL_TREE);
14531 /* Outside of a class-specifier, there must be a
14532 nested-name-specifier. */
14533 if (!nested_name_p &&
14534 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14535 || friend_p))
14536 constructor_p = false;
14537 /* If we still think that this might be a constructor-declarator,
14538 look for a class-name. */
14539 if (constructor_p)
14541 /* If we have:
14543 template <typename T> struct S { S(); };
14544 template <typename T> S<T>::S ();
14546 we must recognize that the nested `S' names a class.
14547 Similarly, for:
14549 template <typename T> S<T>::S<T> ();
14551 we must recognize that the nested `S' names a template. */
14552 type_decl = cp_parser_class_name (parser,
14553 /*typename_keyword_p=*/false,
14554 /*template_keyword_p=*/false,
14555 /*type_p=*/false,
14556 /*check_dependency_p=*/false,
14557 /*class_head_p=*/false,
14558 /*is_declaration=*/false);
14559 /* If there was no class-name, then this is not a constructor. */
14560 constructor_p = !cp_parser_error_occurred (parser);
14563 /* If we're still considering a constructor, we have to see a `(',
14564 to begin the parameter-declaration-clause, followed by either a
14565 `)', an `...', or a decl-specifier. We need to check for a
14566 type-specifier to avoid being fooled into thinking that:
14568 S::S (f) (int);
14570 is a constructor. (It is actually a function named `f' that
14571 takes one parameter (of type `int') and returns a value of type
14572 `S::S'. */
14573 if (constructor_p
14574 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14576 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14577 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14578 /* A parameter declaration begins with a decl-specifier,
14579 which is either the "attribute" keyword, a storage class
14580 specifier, or (usually) a type-specifier. */
14581 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14582 && !cp_parser_storage_class_specifier_opt (parser))
14584 tree type;
14585 bool pop_p = false;
14586 unsigned saved_num_template_parameter_lists;
14588 /* Names appearing in the type-specifier should be looked up
14589 in the scope of the class. */
14590 if (current_class_type)
14591 type = NULL_TREE;
14592 else
14594 type = TREE_TYPE (type_decl);
14595 if (TREE_CODE (type) == TYPENAME_TYPE)
14597 type = resolve_typename_type (type,
14598 /*only_current_p=*/false);
14599 if (type == error_mark_node)
14601 cp_parser_abort_tentative_parse (parser);
14602 return false;
14605 pop_p = push_scope (type);
14608 /* Inside the constructor parameter list, surrounding
14609 template-parameter-lists do not apply. */
14610 saved_num_template_parameter_lists
14611 = parser->num_template_parameter_lists;
14612 parser->num_template_parameter_lists = 0;
14614 /* Look for the type-specifier. */
14615 cp_parser_type_specifier (parser,
14616 CP_PARSER_FLAGS_NONE,
14617 /*decl_specs=*/NULL,
14618 /*is_declarator=*/true,
14619 /*declares_class_or_enum=*/NULL,
14620 /*is_cv_qualifier=*/NULL);
14622 parser->num_template_parameter_lists
14623 = saved_num_template_parameter_lists;
14625 /* Leave the scope of the class. */
14626 if (pop_p)
14627 pop_scope (type);
14629 constructor_p = !cp_parser_error_occurred (parser);
14632 else
14633 constructor_p = false;
14634 /* We did not really want to consume any tokens. */
14635 cp_parser_abort_tentative_parse (parser);
14637 return constructor_p;
14640 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14641 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14642 they must be performed once we are in the scope of the function.
14644 Returns the function defined. */
14646 static tree
14647 cp_parser_function_definition_from_specifiers_and_declarator
14648 (cp_parser* parser,
14649 cp_decl_specifier_seq *decl_specifiers,
14650 tree attributes,
14651 const cp_declarator *declarator)
14653 tree fn;
14654 bool success_p;
14656 /* Begin the function-definition. */
14657 success_p = start_function (decl_specifiers, declarator, attributes);
14659 /* The things we're about to see are not directly qualified by any
14660 template headers we've seen thus far. */
14661 reset_specialization ();
14663 /* If there were names looked up in the decl-specifier-seq that we
14664 did not check, check them now. We must wait until we are in the
14665 scope of the function to perform the checks, since the function
14666 might be a friend. */
14667 perform_deferred_access_checks ();
14669 if (!success_p)
14671 /* Skip the entire function. */
14672 error ("invalid function declaration");
14673 cp_parser_skip_to_end_of_block_or_statement (parser);
14674 fn = error_mark_node;
14676 else
14677 fn = cp_parser_function_definition_after_declarator (parser,
14678 /*inline_p=*/false);
14680 return fn;
14683 /* Parse the part of a function-definition that follows the
14684 declarator. INLINE_P is TRUE iff this function is an inline
14685 function defined with a class-specifier.
14687 Returns the function defined. */
14689 static tree
14690 cp_parser_function_definition_after_declarator (cp_parser* parser,
14691 bool inline_p)
14693 tree fn;
14694 bool ctor_initializer_p = false;
14695 bool saved_in_unbraced_linkage_specification_p;
14696 unsigned saved_num_template_parameter_lists;
14698 /* If the next token is `return', then the code may be trying to
14699 make use of the "named return value" extension that G++ used to
14700 support. */
14701 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14703 /* Consume the `return' keyword. */
14704 cp_lexer_consume_token (parser->lexer);
14705 /* Look for the identifier that indicates what value is to be
14706 returned. */
14707 cp_parser_identifier (parser);
14708 /* Issue an error message. */
14709 error ("named return values are no longer supported");
14710 /* Skip tokens until we reach the start of the function body. */
14711 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14712 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14713 cp_lexer_consume_token (parser->lexer);
14715 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14716 anything declared inside `f'. */
14717 saved_in_unbraced_linkage_specification_p
14718 = parser->in_unbraced_linkage_specification_p;
14719 parser->in_unbraced_linkage_specification_p = false;
14720 /* Inside the function, surrounding template-parameter-lists do not
14721 apply. */
14722 saved_num_template_parameter_lists
14723 = parser->num_template_parameter_lists;
14724 parser->num_template_parameter_lists = 0;
14725 /* If the next token is `try', then we are looking at a
14726 function-try-block. */
14727 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14728 ctor_initializer_p = cp_parser_function_try_block (parser);
14729 /* A function-try-block includes the function-body, so we only do
14730 this next part if we're not processing a function-try-block. */
14731 else
14732 ctor_initializer_p
14733 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14735 /* Finish the function. */
14736 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14737 (inline_p ? 2 : 0));
14738 /* Generate code for it, if necessary. */
14739 expand_or_defer_fn (fn);
14740 /* Restore the saved values. */
14741 parser->in_unbraced_linkage_specification_p
14742 = saved_in_unbraced_linkage_specification_p;
14743 parser->num_template_parameter_lists
14744 = saved_num_template_parameter_lists;
14746 return fn;
14749 /* Parse a template-declaration, assuming that the `export' (and
14750 `extern') keywords, if present, has already been scanned. MEMBER_P
14751 is as for cp_parser_template_declaration. */
14753 static void
14754 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14756 tree decl = NULL_TREE;
14757 tree parameter_list;
14758 bool friend_p = false;
14760 /* Look for the `template' keyword. */
14761 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14762 return;
14764 /* And the `<'. */
14765 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14766 return;
14768 /* If the next token is `>', then we have an invalid
14769 specialization. Rather than complain about an invalid template
14770 parameter, issue an error message here. */
14771 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14773 cp_parser_error (parser, "invalid explicit specialization");
14774 begin_specialization ();
14775 parameter_list = NULL_TREE;
14777 else
14779 /* Parse the template parameters. */
14780 begin_template_parm_list ();
14781 parameter_list = cp_parser_template_parameter_list (parser);
14782 parameter_list = end_template_parm_list (parameter_list);
14785 /* Look for the `>'. */
14786 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14787 /* We just processed one more parameter list. */
14788 ++parser->num_template_parameter_lists;
14789 /* If the next token is `template', there are more template
14790 parameters. */
14791 if (cp_lexer_next_token_is_keyword (parser->lexer,
14792 RID_TEMPLATE))
14793 cp_parser_template_declaration_after_export (parser, member_p);
14794 else
14796 /* There are no access checks when parsing a template, as we do not
14797 know if a specialization will be a friend. */
14798 push_deferring_access_checks (dk_no_check);
14800 decl = cp_parser_single_declaration (parser,
14801 member_p,
14802 &friend_p);
14804 pop_deferring_access_checks ();
14806 /* If this is a member template declaration, let the front
14807 end know. */
14808 if (member_p && !friend_p && decl)
14810 if (TREE_CODE (decl) == TYPE_DECL)
14811 cp_parser_check_access_in_redeclaration (decl);
14813 decl = finish_member_template_decl (decl);
14815 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14816 make_friend_class (current_class_type, TREE_TYPE (decl),
14817 /*complain=*/true);
14819 /* We are done with the current parameter list. */
14820 --parser->num_template_parameter_lists;
14822 /* Finish up. */
14823 finish_template_decl (parameter_list);
14825 /* Register member declarations. */
14826 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14827 finish_member_declaration (decl);
14829 /* If DECL is a function template, we must return to parse it later.
14830 (Even though there is no definition, there might be default
14831 arguments that need handling.) */
14832 if (member_p && decl
14833 && (TREE_CODE (decl) == FUNCTION_DECL
14834 || DECL_FUNCTION_TEMPLATE_P (decl)))
14835 TREE_VALUE (parser->unparsed_functions_queues)
14836 = tree_cons (NULL_TREE, decl,
14837 TREE_VALUE (parser->unparsed_functions_queues));
14840 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14841 `function-definition' sequence. MEMBER_P is true, this declaration
14842 appears in a class scope.
14844 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14845 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14847 static tree
14848 cp_parser_single_declaration (cp_parser* parser,
14849 bool member_p,
14850 bool* friend_p)
14852 int declares_class_or_enum;
14853 tree decl = NULL_TREE;
14854 cp_decl_specifier_seq decl_specifiers;
14855 bool function_definition_p = false;
14857 /* Defer access checks until we know what is being declared. */
14858 push_deferring_access_checks (dk_deferred);
14860 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14861 alternative. */
14862 cp_parser_decl_specifier_seq (parser,
14863 CP_PARSER_FLAGS_OPTIONAL,
14864 &decl_specifiers,
14865 &declares_class_or_enum);
14866 if (friend_p)
14867 *friend_p = cp_parser_friend_p (&decl_specifiers);
14868 /* Gather up the access checks that occurred the
14869 decl-specifier-seq. */
14870 stop_deferring_access_checks ();
14872 /* Check for the declaration of a template class. */
14873 if (declares_class_or_enum)
14875 if (cp_parser_declares_only_class_p (parser))
14877 decl = shadow_tag (&decl_specifiers);
14878 if (decl && decl != error_mark_node)
14879 decl = TYPE_NAME (decl);
14880 else
14881 decl = error_mark_node;
14884 else
14885 decl = NULL_TREE;
14886 /* If it's not a template class, try for a template function. If
14887 the next token is a `;', then this declaration does not declare
14888 anything. But, if there were errors in the decl-specifiers, then
14889 the error might well have come from an attempted class-specifier.
14890 In that case, there's no need to warn about a missing declarator. */
14891 if (!decl
14892 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14893 || decl_specifiers.type != error_mark_node))
14894 decl = cp_parser_init_declarator (parser,
14895 &decl_specifiers,
14896 /*function_definition_allowed_p=*/true,
14897 member_p,
14898 declares_class_or_enum,
14899 &function_definition_p);
14901 pop_deferring_access_checks ();
14903 /* Clear any current qualification; whatever comes next is the start
14904 of something new. */
14905 parser->scope = NULL_TREE;
14906 parser->qualifying_scope = NULL_TREE;
14907 parser->object_scope = NULL_TREE;
14908 /* Look for a trailing `;' after the declaration. */
14909 if (!function_definition_p
14910 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14911 cp_parser_skip_to_end_of_block_or_statement (parser);
14913 return decl;
14916 /* Parse a cast-expression that is not the operand of a unary "&". */
14918 static tree
14919 cp_parser_simple_cast_expression (cp_parser *parser)
14921 return cp_parser_cast_expression (parser, /*address_p=*/false);
14924 /* Parse a functional cast to TYPE. Returns an expression
14925 representing the cast. */
14927 static tree
14928 cp_parser_functional_cast (cp_parser* parser, tree type)
14930 tree expression_list;
14931 tree cast;
14933 expression_list
14934 = cp_parser_parenthesized_expression_list (parser, false,
14935 /*non_constant_p=*/NULL);
14937 cast = build_functional_cast (type, expression_list);
14938 /* [expr.const]/1: In an integral constant expression "only type
14939 conversions to integral or enumeration type can be used". */
14940 if (cast != error_mark_node && !type_dependent_expression_p (type)
14941 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14943 if (cp_parser_non_integral_constant_expression
14944 (parser, "a call to a constructor"))
14945 return error_mark_node;
14947 return cast;
14950 /* Save the tokens that make up the body of a member function defined
14951 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14952 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14953 specifiers applied to the declaration. Returns the FUNCTION_DECL
14954 for the member function. */
14956 static tree
14957 cp_parser_save_member_function_body (cp_parser* parser,
14958 cp_decl_specifier_seq *decl_specifiers,
14959 cp_declarator *declarator,
14960 tree attributes)
14962 cp_token *first;
14963 cp_token *last;
14964 tree fn;
14966 /* Create the function-declaration. */
14967 fn = start_method (decl_specifiers, declarator, attributes);
14968 /* If something went badly wrong, bail out now. */
14969 if (fn == error_mark_node)
14971 /* If there's a function-body, skip it. */
14972 if (cp_parser_token_starts_function_definition_p
14973 (cp_lexer_peek_token (parser->lexer)))
14974 cp_parser_skip_to_end_of_block_or_statement (parser);
14975 return error_mark_node;
14978 /* Remember it, if there default args to post process. */
14979 cp_parser_save_default_args (parser, fn);
14981 /* Save away the tokens that make up the body of the
14982 function. */
14983 first = parser->lexer->next_token;
14984 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14985 /* Handle function try blocks. */
14986 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14987 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14988 last = parser->lexer->next_token;
14990 /* Save away the inline definition; we will process it when the
14991 class is complete. */
14992 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
14993 DECL_PENDING_INLINE_P (fn) = 1;
14995 /* We need to know that this was defined in the class, so that
14996 friend templates are handled correctly. */
14997 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14999 /* We're done with the inline definition. */
15000 finish_method (fn);
15002 /* Add FN to the queue of functions to be parsed later. */
15003 TREE_VALUE (parser->unparsed_functions_queues)
15004 = tree_cons (NULL_TREE, fn,
15005 TREE_VALUE (parser->unparsed_functions_queues));
15007 return fn;
15010 /* Parse a template-argument-list, as well as the trailing ">" (but
15011 not the opening ">"). See cp_parser_template_argument_list for the
15012 return value. */
15014 static tree
15015 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15017 tree arguments;
15018 tree saved_scope;
15019 tree saved_qualifying_scope;
15020 tree saved_object_scope;
15021 bool saved_greater_than_is_operator_p;
15023 /* [temp.names]
15025 When parsing a template-id, the first non-nested `>' is taken as
15026 the end of the template-argument-list rather than a greater-than
15027 operator. */
15028 saved_greater_than_is_operator_p
15029 = parser->greater_than_is_operator_p;
15030 parser->greater_than_is_operator_p = false;
15031 /* Parsing the argument list may modify SCOPE, so we save it
15032 here. */
15033 saved_scope = parser->scope;
15034 saved_qualifying_scope = parser->qualifying_scope;
15035 saved_object_scope = parser->object_scope;
15036 /* Parse the template-argument-list itself. */
15037 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15038 arguments = NULL_TREE;
15039 else
15040 arguments = cp_parser_template_argument_list (parser);
15041 /* Look for the `>' that ends the template-argument-list. If we find
15042 a '>>' instead, it's probably just a typo. */
15043 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15045 if (!saved_greater_than_is_operator_p)
15047 /* If we're in a nested template argument list, the '>>' has
15048 to be a typo for '> >'. We emit the error message, but we
15049 continue parsing and we push a '>' as next token, so that
15050 the argument list will be parsed correctly. Note that the
15051 global source location is still on the token before the
15052 '>>', so we need to say explicitly where we want it. */
15053 cp_token *token = cp_lexer_peek_token (parser->lexer);
15054 error ("%H%<>>%> should be %<> >%> "
15055 "within a nested template argument list",
15056 &token->location);
15058 /* ??? Proper recovery should terminate two levels of
15059 template argument list here. */
15060 token->type = CPP_GREATER;
15062 else
15064 /* If this is not a nested template argument list, the '>>'
15065 is a typo for '>'. Emit an error message and continue.
15066 Same deal about the token location, but here we can get it
15067 right by consuming the '>>' before issuing the diagnostic. */
15068 cp_lexer_consume_token (parser->lexer);
15069 error ("spurious %<>>%>, use %<>%> to terminate "
15070 "a template argument list");
15073 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15074 error ("missing %<>%> to terminate the template argument list");
15075 else
15076 /* It's what we want, a '>'; consume it. */
15077 cp_lexer_consume_token (parser->lexer);
15078 /* The `>' token might be a greater-than operator again now. */
15079 parser->greater_than_is_operator_p
15080 = saved_greater_than_is_operator_p;
15081 /* Restore the SAVED_SCOPE. */
15082 parser->scope = saved_scope;
15083 parser->qualifying_scope = saved_qualifying_scope;
15084 parser->object_scope = saved_object_scope;
15086 return arguments;
15089 /* MEMBER_FUNCTION is a member function, or a friend. If default
15090 arguments, or the body of the function have not yet been parsed,
15091 parse them now. */
15093 static void
15094 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15096 /* If this member is a template, get the underlying
15097 FUNCTION_DECL. */
15098 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15099 member_function = DECL_TEMPLATE_RESULT (member_function);
15101 /* There should not be any class definitions in progress at this
15102 point; the bodies of members are only parsed outside of all class
15103 definitions. */
15104 gcc_assert (parser->num_classes_being_defined == 0);
15105 /* While we're parsing the member functions we might encounter more
15106 classes. We want to handle them right away, but we don't want
15107 them getting mixed up with functions that are currently in the
15108 queue. */
15109 parser->unparsed_functions_queues
15110 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15112 /* Make sure that any template parameters are in scope. */
15113 maybe_begin_member_template_processing (member_function);
15115 /* If the body of the function has not yet been parsed, parse it
15116 now. */
15117 if (DECL_PENDING_INLINE_P (member_function))
15119 tree function_scope;
15120 cp_token_cache *tokens;
15122 /* The function is no longer pending; we are processing it. */
15123 tokens = DECL_PENDING_INLINE_INFO (member_function);
15124 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15125 DECL_PENDING_INLINE_P (member_function) = 0;
15126 /* If this was an inline function in a local class, enter the scope
15127 of the containing function. */
15128 function_scope = decl_function_context (member_function);
15129 if (function_scope)
15130 push_function_context_to (function_scope);
15132 /* Push the body of the function onto the lexer stack. */
15133 cp_parser_push_lexer_for_tokens (parser, tokens);
15135 /* Let the front end know that we going to be defining this
15136 function. */
15137 start_preparsed_function (member_function, NULL_TREE,
15138 SF_PRE_PARSED | SF_INCLASS_INLINE);
15140 /* Now, parse the body of the function. */
15141 cp_parser_function_definition_after_declarator (parser,
15142 /*inline_p=*/true);
15144 /* Leave the scope of the containing function. */
15145 if (function_scope)
15146 pop_function_context_from (function_scope);
15147 cp_parser_pop_lexer (parser);
15150 /* Remove any template parameters from the symbol table. */
15151 maybe_end_member_template_processing ();
15153 /* Restore the queue. */
15154 parser->unparsed_functions_queues
15155 = TREE_CHAIN (parser->unparsed_functions_queues);
15158 /* If DECL contains any default args, remember it on the unparsed
15159 functions queue. */
15161 static void
15162 cp_parser_save_default_args (cp_parser* parser, tree decl)
15164 tree probe;
15166 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15167 probe;
15168 probe = TREE_CHAIN (probe))
15169 if (TREE_PURPOSE (probe))
15171 TREE_PURPOSE (parser->unparsed_functions_queues)
15172 = tree_cons (current_class_type, decl,
15173 TREE_PURPOSE (parser->unparsed_functions_queues));
15174 break;
15176 return;
15179 /* FN is a FUNCTION_DECL which may contains a parameter with an
15180 unparsed DEFAULT_ARG. Parse the default args now. This function
15181 assumes that the current scope is the scope in which the default
15182 argument should be processed. */
15184 static void
15185 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15187 bool saved_local_variables_forbidden_p;
15188 tree parm;
15190 /* While we're parsing the default args, we might (due to the
15191 statement expression extension) encounter more classes. We want
15192 to handle them right away, but we don't want them getting mixed
15193 up with default args that are currently in the queue. */
15194 parser->unparsed_functions_queues
15195 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15197 /* Local variable names (and the `this' keyword) may not appear
15198 in a default argument. */
15199 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15200 parser->local_variables_forbidden_p = true;
15202 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15203 parm;
15204 parm = TREE_CHAIN (parm))
15206 cp_token_cache *tokens;
15208 if (!TREE_PURPOSE (parm)
15209 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15210 continue;
15212 /* Push the saved tokens for the default argument onto the parser's
15213 lexer stack. */
15214 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15215 cp_parser_push_lexer_for_tokens (parser, tokens);
15217 /* Parse the assignment-expression. */
15218 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
15220 /* If the token stream has not been completely used up, then
15221 there was extra junk after the end of the default
15222 argument. */
15223 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15224 cp_parser_error (parser, "expected %<,%>");
15226 /* Revert to the main lexer. */
15227 cp_parser_pop_lexer (parser);
15230 /* Restore the state of local_variables_forbidden_p. */
15231 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15233 /* Restore the queue. */
15234 parser->unparsed_functions_queues
15235 = TREE_CHAIN (parser->unparsed_functions_queues);
15238 /* Parse the operand of `sizeof' (or a similar operator). Returns
15239 either a TYPE or an expression, depending on the form of the
15240 input. The KEYWORD indicates which kind of expression we have
15241 encountered. */
15243 static tree
15244 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15246 static const char *format;
15247 tree expr = NULL_TREE;
15248 const char *saved_message;
15249 bool saved_integral_constant_expression_p;
15251 /* Initialize FORMAT the first time we get here. */
15252 if (!format)
15253 format = "types may not be defined in `%s' expressions";
15255 /* Types cannot be defined in a `sizeof' expression. Save away the
15256 old message. */
15257 saved_message = parser->type_definition_forbidden_message;
15258 /* And create the new one. */
15259 parser->type_definition_forbidden_message
15260 = xmalloc (strlen (format)
15261 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15262 + 1 /* `\0' */);
15263 sprintf ((char *) parser->type_definition_forbidden_message,
15264 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15266 /* The restrictions on constant-expressions do not apply inside
15267 sizeof expressions. */
15268 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15269 parser->integral_constant_expression_p = false;
15271 /* Do not actually evaluate the expression. */
15272 ++skip_evaluation;
15273 /* If it's a `(', then we might be looking at the type-id
15274 construction. */
15275 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15277 tree type;
15278 bool saved_in_type_id_in_expr_p;
15280 /* We can't be sure yet whether we're looking at a type-id or an
15281 expression. */
15282 cp_parser_parse_tentatively (parser);
15283 /* Consume the `('. */
15284 cp_lexer_consume_token (parser->lexer);
15285 /* Parse the type-id. */
15286 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15287 parser->in_type_id_in_expr_p = true;
15288 type = cp_parser_type_id (parser);
15289 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15290 /* Now, look for the trailing `)'. */
15291 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15292 /* If all went well, then we're done. */
15293 if (cp_parser_parse_definitely (parser))
15295 cp_decl_specifier_seq decl_specs;
15297 /* Build a trivial decl-specifier-seq. */
15298 clear_decl_specs (&decl_specs);
15299 decl_specs.type = type;
15301 /* Call grokdeclarator to figure out what type this is. */
15302 expr = grokdeclarator (NULL,
15303 &decl_specs,
15304 TYPENAME,
15305 /*initialized=*/0,
15306 /*attrlist=*/NULL);
15310 /* If the type-id production did not work out, then we must be
15311 looking at the unary-expression production. */
15312 if (!expr)
15313 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15314 /* Go back to evaluating expressions. */
15315 --skip_evaluation;
15317 /* Free the message we created. */
15318 free ((char *) parser->type_definition_forbidden_message);
15319 /* And restore the old one. */
15320 parser->type_definition_forbidden_message = saved_message;
15321 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15323 return expr;
15326 /* If the current declaration has no declarator, return true. */
15328 static bool
15329 cp_parser_declares_only_class_p (cp_parser *parser)
15331 /* If the next token is a `;' or a `,' then there is no
15332 declarator. */
15333 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15334 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15337 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15339 static void
15340 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15341 cp_storage_class storage_class)
15343 if (decl_specs->storage_class != sc_none)
15344 decl_specs->multiple_storage_classes_p = true;
15345 else
15346 decl_specs->storage_class = storage_class;
15349 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15350 is true, the type is a user-defined type; otherwise it is a
15351 built-in type specified by a keyword. */
15353 static void
15354 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15355 tree type_spec,
15356 bool user_defined_p)
15358 decl_specs->any_specifiers_p = true;
15360 /* If the user tries to redeclare a built-in type (with, for example,
15361 in "typedef int wchar_t;") we remember that this is what
15362 happened. In system headers, we ignore these declarations so
15363 that G++ can work with system headers that are not C++-safe. */
15364 if (decl_specs->specs[(int) ds_typedef]
15365 && !user_defined_p
15366 && (decl_specs->type
15367 || decl_specs->specs[(int) ds_long]
15368 || decl_specs->specs[(int) ds_short]
15369 || decl_specs->specs[(int) ds_unsigned]
15370 || decl_specs->specs[(int) ds_signed]))
15372 decl_specs->redefined_builtin_type = type_spec;
15373 if (!decl_specs->type)
15375 decl_specs->type = type_spec;
15376 decl_specs->user_defined_type_p = false;
15379 else if (decl_specs->type)
15380 decl_specs->multiple_types_p = true;
15381 else
15383 decl_specs->type = type_spec;
15384 decl_specs->user_defined_type_p = user_defined_p;
15385 decl_specs->redefined_builtin_type = NULL_TREE;
15389 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15390 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15392 static bool
15393 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15395 return decl_specifiers->specs[(int) ds_friend] != 0;
15398 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15399 issue an error message indicating that TOKEN_DESC was expected.
15401 Returns the token consumed, if the token had the appropriate type.
15402 Otherwise, returns NULL. */
15404 static cp_token *
15405 cp_parser_require (cp_parser* parser,
15406 enum cpp_ttype type,
15407 const char* token_desc)
15409 if (cp_lexer_next_token_is (parser->lexer, type))
15410 return cp_lexer_consume_token (parser->lexer);
15411 else
15413 /* Output the MESSAGE -- unless we're parsing tentatively. */
15414 if (!cp_parser_simulate_error (parser))
15416 char *message = concat ("expected ", token_desc, NULL);
15417 cp_parser_error (parser, message);
15418 free (message);
15420 return NULL;
15424 /* Like cp_parser_require, except that tokens will be skipped until
15425 the desired token is found. An error message is still produced if
15426 the next token is not as expected. */
15428 static void
15429 cp_parser_skip_until_found (cp_parser* parser,
15430 enum cpp_ttype type,
15431 const char* token_desc)
15433 cp_token *token;
15434 unsigned nesting_depth = 0;
15436 if (cp_parser_require (parser, type, token_desc))
15437 return;
15439 /* Skip tokens until the desired token is found. */
15440 while (true)
15442 /* Peek at the next token. */
15443 token = cp_lexer_peek_token (parser->lexer);
15444 /* If we've reached the token we want, consume it and
15445 stop. */
15446 if (token->type == type && !nesting_depth)
15448 cp_lexer_consume_token (parser->lexer);
15449 return;
15451 /* If we've run out of tokens, stop. */
15452 if (token->type == CPP_EOF)
15453 return;
15454 if (token->type == CPP_OPEN_BRACE
15455 || token->type == CPP_OPEN_PAREN
15456 || token->type == CPP_OPEN_SQUARE)
15457 ++nesting_depth;
15458 else if (token->type == CPP_CLOSE_BRACE
15459 || token->type == CPP_CLOSE_PAREN
15460 || token->type == CPP_CLOSE_SQUARE)
15462 if (nesting_depth-- == 0)
15463 return;
15465 /* Consume this token. */
15466 cp_lexer_consume_token (parser->lexer);
15470 /* If the next token is the indicated keyword, consume it. Otherwise,
15471 issue an error message indicating that TOKEN_DESC was expected.
15473 Returns the token consumed, if the token had the appropriate type.
15474 Otherwise, returns NULL. */
15476 static cp_token *
15477 cp_parser_require_keyword (cp_parser* parser,
15478 enum rid keyword,
15479 const char* token_desc)
15481 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15483 if (token && token->keyword != keyword)
15485 dyn_string_t error_msg;
15487 /* Format the error message. */
15488 error_msg = dyn_string_new (0);
15489 dyn_string_append_cstr (error_msg, "expected ");
15490 dyn_string_append_cstr (error_msg, token_desc);
15491 cp_parser_error (parser, error_msg->s);
15492 dyn_string_delete (error_msg);
15493 return NULL;
15496 return token;
15499 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15500 function-definition. */
15502 static bool
15503 cp_parser_token_starts_function_definition_p (cp_token* token)
15505 return (/* An ordinary function-body begins with an `{'. */
15506 token->type == CPP_OPEN_BRACE
15507 /* A ctor-initializer begins with a `:'. */
15508 || token->type == CPP_COLON
15509 /* A function-try-block begins with `try'. */
15510 || token->keyword == RID_TRY
15511 /* The named return value extension begins with `return'. */
15512 || token->keyword == RID_RETURN);
15515 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15516 definition. */
15518 static bool
15519 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15521 cp_token *token;
15523 token = cp_lexer_peek_token (parser->lexer);
15524 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15527 /* Returns TRUE iff the next token is the "," or ">" ending a
15528 template-argument. ">>" is also accepted (after the full
15529 argument was parsed) because it's probably a typo for "> >",
15530 and there is a specific diagnostic for this. */
15532 static bool
15533 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15535 cp_token *token;
15537 token = cp_lexer_peek_token (parser->lexer);
15538 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15539 || token->type == CPP_RSHIFT);
15542 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15543 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15545 static bool
15546 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15547 size_t n)
15549 cp_token *token;
15551 token = cp_lexer_peek_nth_token (parser->lexer, n);
15552 if (token->type == CPP_LESS)
15553 return true;
15554 /* Check for the sequence `<::' in the original code. It would be lexed as
15555 `[:', where `[' is a digraph, and there is no whitespace before
15556 `:'. */
15557 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15559 cp_token *token2;
15560 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15561 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15562 return true;
15564 return false;
15567 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15568 or none_type otherwise. */
15570 static enum tag_types
15571 cp_parser_token_is_class_key (cp_token* token)
15573 switch (token->keyword)
15575 case RID_CLASS:
15576 return class_type;
15577 case RID_STRUCT:
15578 return record_type;
15579 case RID_UNION:
15580 return union_type;
15582 default:
15583 return none_type;
15587 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15589 static void
15590 cp_parser_check_class_key (enum tag_types class_key, tree type)
15592 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15593 pedwarn ("%qs tag used in naming %q#T",
15594 class_key == union_type ? "union"
15595 : class_key == record_type ? "struct" : "class",
15596 type);
15599 /* Issue an error message if DECL is redeclared with different
15600 access than its original declaration [class.access.spec/3].
15601 This applies to nested classes and nested class templates.
15602 [class.mem/1]. */
15604 static void
15605 cp_parser_check_access_in_redeclaration (tree decl)
15607 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15608 return;
15610 if ((TREE_PRIVATE (decl)
15611 != (current_access_specifier == access_private_node))
15612 || (TREE_PROTECTED (decl)
15613 != (current_access_specifier == access_protected_node)))
15614 error ("%qD redeclared with different access", decl);
15617 /* Look for the `template' keyword, as a syntactic disambiguator.
15618 Return TRUE iff it is present, in which case it will be
15619 consumed. */
15621 static bool
15622 cp_parser_optional_template_keyword (cp_parser *parser)
15624 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15626 /* The `template' keyword can only be used within templates;
15627 outside templates the parser can always figure out what is a
15628 template and what is not. */
15629 if (!processing_template_decl)
15631 error ("%<template%> (as a disambiguator) is only allowed "
15632 "within templates");
15633 /* If this part of the token stream is rescanned, the same
15634 error message would be generated. So, we purge the token
15635 from the stream. */
15636 cp_lexer_purge_token (parser->lexer);
15637 return false;
15639 else
15641 /* Consume the `template' keyword. */
15642 cp_lexer_consume_token (parser->lexer);
15643 return true;
15647 return false;
15650 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15651 set PARSER->SCOPE, and perform other related actions. */
15653 static void
15654 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15656 tree value;
15657 tree check;
15659 /* Get the stored value. */
15660 value = cp_lexer_consume_token (parser->lexer)->value;
15661 /* Perform any access checks that were deferred. */
15662 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15663 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15664 /* Set the scope from the stored value. */
15665 parser->scope = TREE_VALUE (value);
15666 parser->qualifying_scope = TREE_TYPE (value);
15667 parser->object_scope = NULL_TREE;
15670 /* Consume tokens up through a non-nested END token. */
15672 static void
15673 cp_parser_cache_group (cp_parser *parser,
15674 enum cpp_ttype end,
15675 unsigned depth)
15677 while (true)
15679 cp_token *token;
15681 /* Abort a parenthesized expression if we encounter a brace. */
15682 if ((end == CPP_CLOSE_PAREN || depth == 0)
15683 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15684 return;
15685 /* If we've reached the end of the file, stop. */
15686 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15687 return;
15688 /* Consume the next token. */
15689 token = cp_lexer_consume_token (parser->lexer);
15690 /* See if it starts a new group. */
15691 if (token->type == CPP_OPEN_BRACE)
15693 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15694 if (depth == 0)
15695 return;
15697 else if (token->type == CPP_OPEN_PAREN)
15698 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15699 else if (token->type == end)
15700 return;
15704 /* Begin parsing tentatively. We always save tokens while parsing
15705 tentatively so that if the tentative parsing fails we can restore the
15706 tokens. */
15708 static void
15709 cp_parser_parse_tentatively (cp_parser* parser)
15711 /* Enter a new parsing context. */
15712 parser->context = cp_parser_context_new (parser->context);
15713 /* Begin saving tokens. */
15714 cp_lexer_save_tokens (parser->lexer);
15715 /* In order to avoid repetitive access control error messages,
15716 access checks are queued up until we are no longer parsing
15717 tentatively. */
15718 push_deferring_access_checks (dk_deferred);
15721 /* Commit to the currently active tentative parse. */
15723 static void
15724 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15726 cp_parser_context *context;
15727 cp_lexer *lexer;
15729 /* Mark all of the levels as committed. */
15730 lexer = parser->lexer;
15731 for (context = parser->context; context->next; context = context->next)
15733 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15734 break;
15735 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15736 while (!cp_lexer_saving_tokens (lexer))
15737 lexer = lexer->next;
15738 cp_lexer_commit_tokens (lexer);
15742 /* Abort the currently active tentative parse. All consumed tokens
15743 will be rolled back, and no diagnostics will be issued. */
15745 static void
15746 cp_parser_abort_tentative_parse (cp_parser* parser)
15748 cp_parser_simulate_error (parser);
15749 /* Now, pretend that we want to see if the construct was
15750 successfully parsed. */
15751 cp_parser_parse_definitely (parser);
15754 /* Stop parsing tentatively. If a parse error has occurred, restore the
15755 token stream. Otherwise, commit to the tokens we have consumed.
15756 Returns true if no error occurred; false otherwise. */
15758 static bool
15759 cp_parser_parse_definitely (cp_parser* parser)
15761 bool error_occurred;
15762 cp_parser_context *context;
15764 /* Remember whether or not an error occurred, since we are about to
15765 destroy that information. */
15766 error_occurred = cp_parser_error_occurred (parser);
15767 /* Remove the topmost context from the stack. */
15768 context = parser->context;
15769 parser->context = context->next;
15770 /* If no parse errors occurred, commit to the tentative parse. */
15771 if (!error_occurred)
15773 /* Commit to the tokens read tentatively, unless that was
15774 already done. */
15775 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15776 cp_lexer_commit_tokens (parser->lexer);
15778 pop_to_parent_deferring_access_checks ();
15780 /* Otherwise, if errors occurred, roll back our state so that things
15781 are just as they were before we began the tentative parse. */
15782 else
15784 cp_lexer_rollback_tokens (parser->lexer);
15785 pop_deferring_access_checks ();
15787 /* Add the context to the front of the free list. */
15788 context->next = cp_parser_context_free_list;
15789 cp_parser_context_free_list = context;
15791 return !error_occurred;
15794 /* Returns true if we are parsing tentatively -- but have decided that
15795 we will stick with this tentative parse, even if errors occur. */
15797 static bool
15798 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15800 return (cp_parser_parsing_tentatively (parser)
15801 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15804 /* Returns nonzero iff an error has occurred during the most recent
15805 tentative parse. */
15807 static bool
15808 cp_parser_error_occurred (cp_parser* parser)
15810 return (cp_parser_parsing_tentatively (parser)
15811 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15814 /* Returns nonzero if GNU extensions are allowed. */
15816 static bool
15817 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15819 return parser->allow_gnu_extensions_p;
15823 /* The parser. */
15825 static GTY (()) cp_parser *the_parser;
15827 /* External interface. */
15829 /* Parse one entire translation unit. */
15831 void
15832 c_parse_file (void)
15834 bool error_occurred;
15835 static bool already_called = false;
15837 if (already_called)
15839 sorry ("inter-module optimizations not implemented for C++");
15840 return;
15842 already_called = true;
15844 the_parser = cp_parser_new ();
15845 push_deferring_access_checks (flag_access_control
15846 ? dk_no_deferred : dk_no_check);
15847 error_occurred = cp_parser_translation_unit (the_parser);
15848 the_parser = NULL;
15851 /* This variable must be provided by every front end. */
15853 int yydebug;
15855 #include "gt-cp-parser.h"