* parser.c: Fix a comment typo.
[official-gcc.git] / gcc / cp / parser.c
blob543c9fa0dc33e07046de1e0e0eacd34ae194a501
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 /* The value associated with this token, if any. */
59 tree value;
60 /* The location at which this token was found. */
61 location_t location;
62 } cp_token;
64 /* The cp_lexer structure represents the C++ lexer. It is responsible
65 for managing the token stream from the preprocessor and supplying
66 it to the parser. Tokens are never added to the cp_lexer after
67 it is created. */
69 typedef struct cp_lexer GTY (())
71 /* The memory allocated for the buffer. Never NULL. */
72 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
73 /* A pointer just past the end of the memory allocated for the buffer. */
74 cp_token * GTY ((skip)) buffer_end;
75 /* A pointer just past the last available token. The tokens
76 in this lexer are [buffer, last_token). */
77 cp_token * GTY ((skip)) last_token;
79 /* The next available token. If NEXT_TOKEN is NULL, then there are
80 no more available tokens. */
81 cp_token * GTY ((skip)) next_token;
83 /* A stack indicating positions at which cp_lexer_save_tokens was
84 called. The top entry is the most recent position at which we
85 began saving tokens. The entries are differences in token
86 position between BUFFER and the first saved token.
87 If the stack is non-empty, we are saving tokens. */
88 varray_type saved_tokens;
90 /* True if we should output debugging information. */
91 bool debugging_p;
93 /* The next lexer in a linked list of lexers. */
94 struct cp_lexer *next;
95 } cp_lexer;
97 /* cp_token_cache is a range of tokens. There is no need to represent
98 allocate heap memory for it, since tokens are never removed from the
99 lexer's array. There is also no need for the GC to walk through
100 a cp_token_cache, since everything in here is referenced through
101 a lexer. */
103 typedef struct cp_token_cache GTY(())
105 /* The beginning of the token range. */
106 cp_token * GTY((skip)) first;
108 /* Points immediately after the last token in the range. */
109 cp_token * GTY ((skip)) last;
110 } cp_token_cache;
112 /* Prototypes. */
114 static cp_lexer *cp_lexer_new_main
115 (void);
116 static cp_lexer *cp_lexer_new_from_token_array
117 (cp_token *, cp_token *);
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 void cp_lexer_peek_token_emit_debug_info
137 (cp_lexer *, cp_token *);
138 static void cp_lexer_skip_purged_tokens
139 (cp_lexer *);
140 static cp_token *cp_lexer_peek_nth_token
141 (cp_lexer *, size_t);
142 static inline bool cp_lexer_next_token_is
143 (cp_lexer *, enum cpp_ttype);
144 static bool cp_lexer_next_token_is_not
145 (cp_lexer *, enum cpp_ttype);
146 static bool cp_lexer_next_token_is_keyword
147 (cp_lexer *, enum rid);
148 static cp_token *cp_lexer_consume_token
149 (cp_lexer *);
150 static void cp_lexer_purge_token
151 (cp_lexer *);
152 static void cp_lexer_purge_tokens_after
153 (cp_lexer *, cp_token *);
154 static void cp_lexer_handle_pragma
155 (cp_lexer *);
156 static void cp_lexer_save_tokens
157 (cp_lexer *);
158 static void cp_lexer_commit_tokens
159 (cp_lexer *);
160 static void cp_lexer_rollback_tokens
161 (cp_lexer *);
162 static inline void cp_lexer_set_source_position_from_token
163 (cp_lexer *, const cp_token *);
164 #ifdef ENABLE_CHECKING
165 static void cp_lexer_print_token
166 (FILE *, cp_token *);
167 static inline bool cp_lexer_debugging_p
168 (cp_lexer *);
169 static void cp_lexer_start_debugging
170 (cp_lexer *) ATTRIBUTE_UNUSED;
171 static void cp_lexer_stop_debugging
172 (cp_lexer *) ATTRIBUTE_UNUSED;
173 #else
174 #define cp_lexer_debug_stream NULL
175 #define cp_lexer_print_token(str, tok)
176 #define cp_lexer_debugging_p(lexer) 0
177 #endif /* ENABLE_CHECKING */
179 static cp_token_cache *cp_token_cache_new
180 (cp_token *, cp_token *);
182 /* Manifest constants. */
184 #define CP_LEXER_BUFFER_SIZE 10000
185 #define CP_SAVED_TOKENS_SIZE 5
187 /* A token type for keywords, as opposed to ordinary identifiers. */
188 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
190 /* A token type for template-ids. If a template-id is processed while
191 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
192 the value of the CPP_TEMPLATE_ID is whatever was returned by
193 cp_parser_template_id. */
194 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
196 /* A token type for nested-name-specifiers. If a
197 nested-name-specifier is processed while parsing tentatively, it is
198 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
199 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
200 cp_parser_nested_name_specifier_opt. */
201 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
203 /* A token type for tokens that are not tokens at all; these are used
204 to represent slots in the array where there used to be a token
205 that has now been deleted. */
206 #define CPP_PURGED (CPP_NESTED_NAME_SPECIFIER + 1)
208 /* Variables. */
210 #ifdef ENABLE_CHECKING
211 /* The stream to which debugging output should be written. */
212 static FILE *cp_lexer_debug_stream;
213 #endif /* ENABLE_CHECKING */
215 /* Create a new main C++ lexer, the lexer that gets tokens from the
216 preprocessor. */
218 static cp_lexer *
219 cp_lexer_new_main (void)
221 cp_lexer *lexer;
222 cp_token first_token;
224 /* Tell cpplib we want CPP_PRAGMA tokens. */
225 cpp_get_options (parse_in)->defer_pragmas = true;
227 /* Tell c_lex not to merge string constants. */
228 c_lex_return_raw_strings = true;
230 /* It's possible that lexing the first token will load a PCH file,
231 which is a GC collection point. So we have to grab the first
232 token before allocating any memory. */
233 cp_lexer_get_preprocessor_token (NULL, &first_token);
234 c_common_no_more_pch ();
236 /* Allocate the memory. */
237 lexer = GGC_CNEW (cp_lexer);
239 /* Create the buffer. */
240 lexer->buffer = ggc_calloc (CP_LEXER_BUFFER_SIZE, sizeof (cp_token));
241 lexer->buffer_end = lexer->buffer + CP_LEXER_BUFFER_SIZE;
243 /* There is one token in the buffer. */
244 lexer->last_token = lexer->buffer + 1;
245 lexer->next_token = lexer->buffer;
246 *lexer->next_token = first_token;
248 /* Create the SAVED_TOKENS stack. */
249 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
251 #ifdef ENABLE_CHECKING
252 /* Initially we are not debugging. */
253 lexer->debugging_p = false;
254 #endif /* ENABLE_CHECKING */
256 /* Get the rest of the tokens from the preprocessor. */
257 while (lexer->last_token[-1].type != CPP_EOF)
259 if (lexer->last_token == lexer->buffer_end)
260 cp_lexer_grow_buffer (lexer);
261 cp_lexer_get_preprocessor_token (lexer, lexer->last_token++);
264 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
265 direct calls to c_lex. Those callers all expect c_lex to do
266 string constant concatenation. */
267 c_lex_return_raw_strings = false;
269 return lexer;
272 /* Create a new lexer whose token stream is primed with the tokens in
273 the range [FIRST, LAST). When these tokens are exhausted, no new
274 tokens will be read. */
276 static cp_lexer *
277 cp_lexer_new_from_token_array (cp_token *first, cp_token *last)
279 cp_lexer *lexer = GGC_CNEW (cp_lexer);
280 cp_token *eof;
282 /* Allocate a new buffer. The reason we do this is to make sure
283 there's a CPP_EOF token at the end. An alternative would be to
284 modify cp_lexer_peek_token so that it checks for end-of-buffer
285 and returns a CPP_EOF when appropriate. */
287 lexer->buffer = GGC_NEWVEC (cp_token, (last - first) + 1);
288 memcpy (lexer->buffer, first, sizeof (cp_token) * (last - first));
289 lexer->next_token = lexer->buffer;
290 lexer->buffer_end = lexer->last_token = lexer->buffer + (last - first);
292 eof = lexer->buffer + (last - first);
293 eof->type = CPP_EOF;
294 eof->location = UNKNOWN_LOCATION;
295 eof->value = NULL_TREE;
296 eof->keyword = RID_MAX;
298 /* Create the SAVED_TOKENS stack. */
299 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
301 #ifdef ENABLE_CHECKING
302 /* Initially we are not debugging. */
303 lexer->debugging_p = false;
304 #endif
305 return lexer;
308 /* Create a new lexer whose token stream is primed with the tokens in
309 CACHE. When these tokens are exhausted, no new tokens will be read. */
311 static cp_lexer *
312 cp_lexer_new_from_tokens (cp_token_cache *cache)
314 return cp_lexer_new_from_token_array (cache->first, cache->last);
317 /* Frees all resources associated with LEXER. */
319 static void
320 cp_lexer_destroy (cp_lexer *lexer)
322 ggc_free (lexer->buffer);
323 ggc_free (lexer);
326 /* Returns nonzero if debugging information should be output. */
328 #ifdef ENABLE_CHECKING
330 static inline bool
331 cp_lexer_debugging_p (cp_lexer *lexer)
333 return lexer->debugging_p;
336 #endif /* ENABLE_CHECKING */
338 /* Set the current source position from the information stored in
339 TOKEN. */
341 static inline void
342 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
343 const cp_token *token)
345 /* Ideally, the source position information would not be a global
346 variable, but it is. */
348 /* Update the line number and system header flag. */
349 if (token->type != CPP_EOF)
351 input_location = token->location;
352 in_system_header = token->in_system_header;
356 /* TOKEN points into the circular token buffer. Return a pointer to
357 the next token in the buffer. */
359 static inline cp_token *
360 cp_lexer_next_token (cp_lexer* lexer ATTRIBUTE_UNUSED, cp_token* token)
362 token++;
363 return token;
366 /* TOKEN points into the circular token buffer. Return a pointer to
367 the previous token in the buffer. */
369 static inline cp_token *
370 cp_lexer_prev_token (cp_lexer* lexer ATTRIBUTE_UNUSED, cp_token* token)
372 return token - 1;
375 /* nonzero if we are presently saving tokens. */
377 static int
378 cp_lexer_saving_tokens (const cp_lexer* lexer)
380 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
383 /* Return a pointer to the token that is N tokens beyond TOKEN in the
384 buffer. */
386 static inline cp_token *
387 cp_lexer_advance_token (cp_lexer *lexer ATTRIBUTE_UNUSED,
388 cp_token *token, ptrdiff_t n)
390 return token + n;
393 /* Returns the number of times that START would have to be incremented
394 to reach FINISH. If START and FINISH are the same, returns zero. */
396 static inline ptrdiff_t
397 cp_lexer_token_difference (cp_lexer* lexer ATTRIBUTE_UNUSED,
398 cp_token* start, cp_token* finish)
400 return finish - start;
403 /* If the buffer is full, make it bigger. */
404 static void
405 cp_lexer_grow_buffer (cp_lexer* lexer)
407 cp_token *old_buffer;
408 cp_token *new_buffer;
409 ptrdiff_t buffer_length;
411 /* This function should only be called when buffer is full. */
412 gcc_assert (lexer->last_token == lexer->buffer_end);
414 /* Remember the current buffer pointer. It will become invalid,
415 but we will need to do pointer arithmetic involving this
416 value. */
417 old_buffer = lexer->buffer;
418 /* Compute the current buffer size. */
419 buffer_length = lexer->buffer_end - lexer->buffer;
420 /* Allocate a buffer twice as big. */
421 new_buffer = ggc_realloc (lexer->buffer,
422 2 * buffer_length * sizeof (cp_token));
424 /* Recompute buffer positions. */
425 lexer->buffer = new_buffer;
426 lexer->buffer_end = new_buffer + 2 * buffer_length;
427 lexer->last_token = new_buffer + (lexer->last_token - old_buffer);
428 lexer->next_token = new_buffer + (lexer->next_token - old_buffer);
430 /* Clear the rest of the buffer. We never look at this storage,
431 but the garbage collector may. */
432 memset (lexer->last_token, 0,
433 (lexer->buffer_end - lexer->last_token) * sizeof(cp_token));
436 /* Store the next token from the preprocessor in *TOKEN. */
438 static void
439 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
440 cp_token *token)
442 bool done;
444 done = false;
445 /* Keep going until we get a token we like. */
446 while (!done)
448 /* Get a new token from the preprocessor. */
449 token->type = c_lex_with_flags (&token->value, &token->flags);
450 /* Issue messages about tokens we cannot process. */
451 switch (token->type)
453 case CPP_ATSIGN:
454 case CPP_HASH:
455 case CPP_PASTE:
456 error ("invalid token");
457 break;
459 default:
460 /* This is a good token, so we exit the loop. */
461 done = true;
462 break;
465 /* Now we've got our token. */
466 token->location = input_location;
467 token->in_system_header = in_system_header;
469 /* Check to see if this token is a keyword. */
470 if (token->type == CPP_NAME
471 && C_IS_RESERVED_WORD (token->value))
473 /* Mark this token as a keyword. */
474 token->type = CPP_KEYWORD;
475 /* Record which keyword. */
476 token->keyword = C_RID_CODE (token->value);
477 /* Update the value. Some keywords are mapped to particular
478 entities, rather than simply having the value of the
479 corresponding IDENTIFIER_NODE. For example, `__const' is
480 mapped to `const'. */
481 token->value = ridpointers[token->keyword];
483 else
484 token->keyword = RID_MAX;
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 cp_token *token;
495 /* Skip over purged tokens if necessary. */
496 if (lexer->next_token->type == CPP_PURGED)
497 cp_lexer_skip_purged_tokens (lexer);
499 token = lexer->next_token;
501 /* Provide debugging output. */
502 if (cp_lexer_debugging_p (lexer))
503 cp_lexer_peek_token_emit_debug_info (lexer, token);
505 cp_lexer_set_source_position_from_token (lexer, token);
506 return token;
509 /* Emit debug output for cp_lexer_peek_token. Split out into a
510 separate function so that cp_lexer_peek_token can be small and
511 inlinable. */
513 static void
514 cp_lexer_peek_token_emit_debug_info (cp_lexer *lexer ATTRIBUTE_UNUSED,
515 cp_token *token ATTRIBUTE_UNUSED)
517 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
518 cp_lexer_print_token (cp_lexer_debug_stream, token);
519 fprintf (cp_lexer_debug_stream, "\n");
522 /* Skip all tokens whose type is CPP_PURGED. */
524 static void cp_lexer_skip_purged_tokens (cp_lexer *lexer)
526 while (lexer->next_token->type == CPP_PURGED)
527 ++lexer->next_token;
530 /* Return true if the next token has the indicated TYPE. */
532 static bool
533 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
535 cp_token *token;
537 /* Peek at the next token. */
538 token = cp_lexer_peek_token (lexer);
539 /* Check to see if it has the indicated TYPE. */
540 return token->type == type;
543 /* Return true if the next token does not have the indicated TYPE. */
545 static bool
546 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
548 return !cp_lexer_next_token_is (lexer, type);
551 /* Return true if the next token is the indicated KEYWORD. */
553 static bool
554 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
556 cp_token *token;
558 /* Peek at the next token. */
559 token = cp_lexer_peek_token (lexer);
560 /* Check to see if it is the indicated keyword. */
561 return token->keyword == keyword;
564 /* Return a pointer to the Nth token in the token stream. If N is 1,
565 then this is precisely equivalent to cp_lexer_peek_token. */
567 static cp_token *
568 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
570 cp_token *token;
572 /* N is 1-based, not zero-based. */
573 gcc_assert (n > 0);
575 --n;
576 token = lexer->next_token;
577 while (n != 0)
579 ++token;
580 if (token->type != CPP_PURGED)
581 --n;
584 return token;
587 /* Consume the next token. The pointer returned is valid only until
588 another token is read. Callers should preserve copy the token
589 explicitly if they will need its value for a longer period of
590 time. */
592 static cp_token *
593 cp_lexer_consume_token (cp_lexer* lexer)
595 cp_token *token;
597 /* Skip over purged tokens if necessary. */
598 if (lexer->next_token->type == CPP_PURGED)
599 cp_lexer_skip_purged_tokens (lexer);
601 token = lexer->next_token++;
603 /* Provide debugging output. */
604 if (cp_lexer_debugging_p (lexer))
606 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
607 cp_lexer_print_token (cp_lexer_debug_stream, token);
608 fprintf (cp_lexer_debug_stream, "\n");
611 return token;
614 /* Permanently remove the next token from the token stream. There
615 must be a valid next token already; this token never reads
616 additional tokens from the preprocessor. */
618 static void
619 cp_lexer_purge_token (cp_lexer *lexer)
621 cp_token *tok = lexer->next_token;
622 tok->type = CPP_PURGED;
623 tok->location = UNKNOWN_LOCATION;
624 tok->value = NULL_TREE;
625 tok->keyword = RID_MAX;
628 /* Permanently remove all tokens after TOK, up to, but not
629 including, the token that will be returned next by
630 cp_lexer_peek_token. */
632 static void
633 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
635 cp_token *peek;
637 peek = cp_lexer_peek_token (lexer);
638 gcc_assert (tok < peek);
640 for ( tok += 1; tok != peek; tok += 1)
642 tok->type = CPP_PURGED;
643 tok->location = UNKNOWN_LOCATION;
644 tok->value = NULL_TREE;
645 tok->keyword = RID_MAX;
649 /* Consume and handle a pragma token. */
650 static void
651 cp_lexer_handle_pragma (cp_lexer *lexer)
653 cpp_string s;
654 cp_token *token = cp_lexer_consume_token (lexer);
655 gcc_assert (token->type == CPP_PRAGMA);
656 gcc_assert (token->value);
658 s.len = TREE_STRING_LENGTH (token->value);
659 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
661 cp_lexer_set_source_position_from_token (lexer, token);
662 cpp_handle_deferred_pragma (parse_in, &s);
664 /* Clearing token->value here means that we will get an ICE if we
665 try to process this #pragma again (which should be impossible). */
666 token->value = NULL;
669 /* Begin saving tokens. All tokens consumed after this point will be
670 preserved. */
672 static void
673 cp_lexer_save_tokens (cp_lexer* lexer)
675 /* Provide debugging output. */
676 if (cp_lexer_debugging_p (lexer))
677 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
679 VARRAY_PUSH_INT (lexer->saved_tokens,
680 cp_lexer_token_difference (lexer,
681 lexer->buffer,
682 lexer->next_token));
685 /* Commit to the portion of the token stream most recently saved. */
687 static void
688 cp_lexer_commit_tokens (cp_lexer* lexer)
690 /* Provide debugging output. */
691 if (cp_lexer_debugging_p (lexer))
692 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
694 VARRAY_POP (lexer->saved_tokens);
697 /* Return all tokens saved since the last call to cp_lexer_save_tokens
698 to the token stream. Stop saving tokens. */
700 static void
701 cp_lexer_rollback_tokens (cp_lexer* lexer)
703 size_t delta;
705 /* Provide debugging output. */
706 if (cp_lexer_debugging_p (lexer))
707 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
709 /* Find the token that was the NEXT_TOKEN when we started saving
710 tokens. */
711 delta = VARRAY_TOP_INT(lexer->saved_tokens);
712 /* Make it the next token again now. */
713 lexer->next_token = cp_lexer_advance_token (lexer, lexer->buffer, delta);
715 /* Stop saving tokens. */
716 VARRAY_POP (lexer->saved_tokens);
719 /* Print a representation of the TOKEN on the STREAM. */
721 #ifdef ENABLE_CHECKING
723 static void
724 cp_lexer_print_token (FILE * stream, cp_token *token)
726 /* We don't use cpp_type2name here because the parser defines
727 a few tokens of its own. */
728 static const char *const token_names[] = {
729 /* cpplib-defined token types */
730 #define OP(e, s) #e,
731 #define TK(e, s) #e,
732 TTYPE_TABLE
733 #undef OP
734 #undef TK
735 /* C++ parser token types - see "Manifest constants", above. */
736 "KEYWORD",
737 "TEMPLATE_ID",
738 "NESTED_NAME_SPECIFIER",
739 "PURGED"
742 /* If we have a name for the token, print it out. Otherwise, we
743 simply give the numeric code. */
744 gcc_assert (token->type < ARRAY_SIZE(token_names));
745 fputs (token_names[token->type], stream);
747 /* For some tokens, print the associated data. */
748 switch (token->type)
750 case CPP_KEYWORD:
751 /* Some keywords have a value that is not an IDENTIFIER_NODE.
752 For example, `struct' is mapped to an INTEGER_CST. */
753 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
754 break;
755 /* else fall through */
756 case CPP_NAME:
757 fputs (IDENTIFIER_POINTER (token->value), stream);
758 break;
760 case CPP_STRING:
761 case CPP_WSTRING:
762 case CPP_PRAGMA:
763 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
764 break;
766 default:
767 break;
771 /* Start emitting debugging information. */
773 static void
774 cp_lexer_start_debugging (cp_lexer* lexer)
776 ++lexer->debugging_p;
779 /* Stop emitting debugging information. */
781 static void
782 cp_lexer_stop_debugging (cp_lexer* lexer)
784 --lexer->debugging_p;
787 #endif /* ENABLE_CHECKING */
789 /* Create a new cp_token_cache, representing a range of tokens. */
791 static cp_token_cache *
792 cp_token_cache_new (cp_token *first, cp_token *last)
794 cp_token_cache *cache = GGC_NEW (cp_token_cache);
795 cache->first = first;
796 cache->last = last;
797 return cache;
801 /* Decl-specifiers. */
803 static void clear_decl_specs
804 (cp_decl_specifier_seq *);
806 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
808 static void
809 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
811 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
814 /* Declarators. */
816 /* Nothing other than the parser should be creating declarators;
817 declarators are a semi-syntactic representation of C++ entities.
818 Other parts of the front end that need to create entities (like
819 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
821 static cp_declarator *make_id_declarator
822 (tree);
823 static cp_declarator *make_call_declarator
824 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
825 static cp_declarator *make_array_declarator
826 (cp_declarator *, tree);
827 static cp_declarator *make_pointer_declarator
828 (cp_cv_quals, cp_declarator *);
829 static cp_declarator *make_reference_declarator
830 (cp_cv_quals, cp_declarator *);
831 static cp_parameter_declarator *make_parameter_declarator
832 (cp_decl_specifier_seq *, cp_declarator *, tree);
833 static cp_declarator *make_ptrmem_declarator
834 (cp_cv_quals, tree, cp_declarator *);
836 cp_declarator *cp_error_declarator;
838 /* The obstack on which declarators and related data structures are
839 allocated. */
840 static struct obstack declarator_obstack;
842 /* Alloc BYTES from the declarator memory pool. */
844 static inline void *
845 alloc_declarator (size_t bytes)
847 return obstack_alloc (&declarator_obstack, bytes);
850 /* Allocate a declarator of the indicated KIND. Clear fields that are
851 common to all declarators. */
853 static cp_declarator *
854 make_declarator (cp_declarator_kind kind)
856 cp_declarator *declarator;
858 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
859 declarator->kind = kind;
860 declarator->attributes = NULL_TREE;
861 declarator->declarator = NULL;
863 return declarator;
866 /* Make a declarator for a generalized identifier. */
868 cp_declarator *
869 make_id_declarator (tree id)
871 cp_declarator *declarator;
873 declarator = make_declarator (cdk_id);
874 declarator->u.id.name = id;
875 declarator->u.id.sfk = sfk_none;
877 return declarator;
880 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
881 of modifiers such as const or volatile to apply to the pointer
882 type, represented as identifiers. */
884 cp_declarator *
885 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
887 cp_declarator *declarator;
889 declarator = make_declarator (cdk_pointer);
890 declarator->declarator = target;
891 declarator->u.pointer.qualifiers = cv_qualifiers;
892 declarator->u.pointer.class_type = NULL_TREE;
894 return declarator;
897 /* Like make_pointer_declarator -- but for references. */
899 cp_declarator *
900 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
902 cp_declarator *declarator;
904 declarator = make_declarator (cdk_reference);
905 declarator->declarator = target;
906 declarator->u.pointer.qualifiers = cv_qualifiers;
907 declarator->u.pointer.class_type = NULL_TREE;
909 return declarator;
912 /* Like make_pointer_declarator -- but for a pointer to a non-static
913 member of CLASS_TYPE. */
915 cp_declarator *
916 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
917 cp_declarator *pointee)
919 cp_declarator *declarator;
921 declarator = make_declarator (cdk_ptrmem);
922 declarator->declarator = pointee;
923 declarator->u.pointer.qualifiers = cv_qualifiers;
924 declarator->u.pointer.class_type = class_type;
926 return declarator;
929 /* Make a declarator for the function given by TARGET, with the
930 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
931 "const"-qualified member function. The EXCEPTION_SPECIFICATION
932 indicates what exceptions can be thrown. */
934 cp_declarator *
935 make_call_declarator (cp_declarator *target,
936 cp_parameter_declarator *parms,
937 cp_cv_quals cv_qualifiers,
938 tree exception_specification)
940 cp_declarator *declarator;
942 declarator = make_declarator (cdk_function);
943 declarator->declarator = target;
944 declarator->u.function.parameters = parms;
945 declarator->u.function.qualifiers = cv_qualifiers;
946 declarator->u.function.exception_specification = exception_specification;
948 return declarator;
951 /* Make a declarator for an array of BOUNDS elements, each of which is
952 defined by ELEMENT. */
954 cp_declarator *
955 make_array_declarator (cp_declarator *element, tree bounds)
957 cp_declarator *declarator;
959 declarator = make_declarator (cdk_array);
960 declarator->declarator = element;
961 declarator->u.array.bounds = bounds;
963 return declarator;
966 cp_parameter_declarator *no_parameters;
968 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
969 DECLARATOR and DEFAULT_ARGUMENT. */
971 cp_parameter_declarator *
972 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
973 cp_declarator *declarator,
974 tree default_argument)
976 cp_parameter_declarator *parameter;
978 parameter = ((cp_parameter_declarator *)
979 alloc_declarator (sizeof (cp_parameter_declarator)));
980 parameter->next = NULL;
981 if (decl_specifiers)
982 parameter->decl_specifiers = *decl_specifiers;
983 else
984 clear_decl_specs (&parameter->decl_specifiers);
985 parameter->declarator = declarator;
986 parameter->default_argument = default_argument;
987 parameter->ellipsis_p = false;
989 return parameter;
992 /* The parser. */
994 /* Overview
995 --------
997 A cp_parser parses the token stream as specified by the C++
998 grammar. Its job is purely parsing, not semantic analysis. For
999 example, the parser breaks the token stream into declarators,
1000 expressions, statements, and other similar syntactic constructs.
1001 It does not check that the types of the expressions on either side
1002 of an assignment-statement are compatible, or that a function is
1003 not declared with a parameter of type `void'.
1005 The parser invokes routines elsewhere in the compiler to perform
1006 semantic analysis and to build up the abstract syntax tree for the
1007 code processed.
1009 The parser (and the template instantiation code, which is, in a
1010 way, a close relative of parsing) are the only parts of the
1011 compiler that should be calling push_scope and pop_scope, or
1012 related functions. The parser (and template instantiation code)
1013 keeps track of what scope is presently active; everything else
1014 should simply honor that. (The code that generates static
1015 initializers may also need to set the scope, in order to check
1016 access control correctly when emitting the initializers.)
1018 Methodology
1019 -----------
1021 The parser is of the standard recursive-descent variety. Upcoming
1022 tokens in the token stream are examined in order to determine which
1023 production to use when parsing a non-terminal. Some C++ constructs
1024 require arbitrary look ahead to disambiguate. For example, it is
1025 impossible, in the general case, to tell whether a statement is an
1026 expression or declaration without scanning the entire statement.
1027 Therefore, the parser is capable of "parsing tentatively." When the
1028 parser is not sure what construct comes next, it enters this mode.
1029 Then, while we attempt to parse the construct, the parser queues up
1030 error messages, rather than issuing them immediately, and saves the
1031 tokens it consumes. If the construct is parsed successfully, the
1032 parser "commits", i.e., it issues any queued error messages and
1033 the tokens that were being preserved are permanently discarded.
1034 If, however, the construct is not parsed successfully, the parser
1035 rolls back its state completely so that it can resume parsing using
1036 a different alternative.
1038 Future Improvements
1039 -------------------
1041 The performance of the parser could probably be improved
1042 substantially. Some possible improvements include:
1044 - The expression parser recurses through the various levels of
1045 precedence as specified in the grammar, rather than using an
1046 operator-precedence technique. Therefore, parsing a simple
1047 identifier requires multiple recursive calls.
1049 - We could often eliminate the need to parse tentatively by
1050 looking ahead a little bit. In some places, this approach
1051 might not entirely eliminate the need to parse tentatively, but
1052 it might still speed up the average case. */
1054 /* Flags that are passed to some parsing functions. These values can
1055 be bitwise-ored together. */
1057 typedef enum cp_parser_flags
1059 /* No flags. */
1060 CP_PARSER_FLAGS_NONE = 0x0,
1061 /* The construct is optional. If it is not present, then no error
1062 should be issued. */
1063 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1064 /* When parsing a type-specifier, do not allow user-defined types. */
1065 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1066 } cp_parser_flags;
1068 /* The different kinds of declarators we want to parse. */
1070 typedef enum cp_parser_declarator_kind
1072 /* We want an abstract declarator. */
1073 CP_PARSER_DECLARATOR_ABSTRACT,
1074 /* We want a named declarator. */
1075 CP_PARSER_DECLARATOR_NAMED,
1076 /* We don't mind, but the name must be an unqualified-id. */
1077 CP_PARSER_DECLARATOR_EITHER
1078 } cp_parser_declarator_kind;
1080 /* A mapping from a token type to a corresponding tree node type. */
1082 typedef struct cp_parser_token_tree_map_node
1084 /* The token type. */
1085 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1086 /* The corresponding tree code. */
1087 ENUM_BITFIELD (tree_code) tree_type : 8;
1088 } cp_parser_token_tree_map_node;
1090 /* A complete map consists of several ordinary entries, followed by a
1091 terminator. The terminating entry has a token_type of CPP_EOF. */
1093 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1095 /* The status of a tentative parse. */
1097 typedef enum cp_parser_status_kind
1099 /* No errors have occurred. */
1100 CP_PARSER_STATUS_KIND_NO_ERROR,
1101 /* An error has occurred. */
1102 CP_PARSER_STATUS_KIND_ERROR,
1103 /* We are committed to this tentative parse, whether or not an error
1104 has occurred. */
1105 CP_PARSER_STATUS_KIND_COMMITTED
1106 } cp_parser_status_kind;
1108 /* Context that is saved and restored when parsing tentatively. */
1110 typedef struct cp_parser_context GTY (())
1112 /* If this is a tentative parsing context, the status of the
1113 tentative parse. */
1114 enum cp_parser_status_kind status;
1115 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1116 that are looked up in this context must be looked up both in the
1117 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1118 the context of the containing expression. */
1119 tree object_type;
1120 /* The next parsing context in the stack. */
1121 struct cp_parser_context *next;
1122 } cp_parser_context;
1124 /* Prototypes. */
1126 /* Constructors and destructors. */
1128 static cp_parser_context *cp_parser_context_new
1129 (cp_parser_context *);
1131 /* Class variables. */
1133 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1135 /* Constructors and destructors. */
1137 /* Construct a new context. The context below this one on the stack
1138 is given by NEXT. */
1140 static cp_parser_context *
1141 cp_parser_context_new (cp_parser_context* next)
1143 cp_parser_context *context;
1145 /* Allocate the storage. */
1146 if (cp_parser_context_free_list != NULL)
1148 /* Pull the first entry from the free list. */
1149 context = cp_parser_context_free_list;
1150 cp_parser_context_free_list = context->next;
1151 memset (context, 0, sizeof (*context));
1153 else
1154 context = GGC_CNEW (cp_parser_context);
1155 /* No errors have occurred yet in this context. */
1156 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1157 /* If this is not the bottomost context, copy information that we
1158 need from the previous context. */
1159 if (next)
1161 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1162 expression, then we are parsing one in this context, too. */
1163 context->object_type = next->object_type;
1164 /* Thread the stack. */
1165 context->next = next;
1168 return context;
1171 /* The cp_parser structure represents the C++ parser. */
1173 typedef struct cp_parser GTY(())
1175 /* The lexer from which we are obtaining tokens. */
1176 cp_lexer *lexer;
1178 /* The scope in which names should be looked up. If NULL_TREE, then
1179 we look up names in the scope that is currently open in the
1180 source program. If non-NULL, this is either a TYPE or
1181 NAMESPACE_DECL for the scope in which we should look.
1183 This value is not cleared automatically after a name is looked
1184 up, so we must be careful to clear it before starting a new look
1185 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1186 will look up `Z' in the scope of `X', rather than the current
1187 scope.) Unfortunately, it is difficult to tell when name lookup
1188 is complete, because we sometimes peek at a token, look it up,
1189 and then decide not to consume it. */
1190 tree scope;
1192 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1193 last lookup took place. OBJECT_SCOPE is used if an expression
1194 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1195 respectively. QUALIFYING_SCOPE is used for an expression of the
1196 form "X::Y"; it refers to X. */
1197 tree object_scope;
1198 tree qualifying_scope;
1200 /* A stack of parsing contexts. All but the bottom entry on the
1201 stack will be tentative contexts.
1203 We parse tentatively in order to determine which construct is in
1204 use in some situations. For example, in order to determine
1205 whether a statement is an expression-statement or a
1206 declaration-statement we parse it tentatively as a
1207 declaration-statement. If that fails, we then reparse the same
1208 token stream as an expression-statement. */
1209 cp_parser_context *context;
1211 /* True if we are parsing GNU C++. If this flag is not set, then
1212 GNU extensions are not recognized. */
1213 bool allow_gnu_extensions_p;
1215 /* TRUE if the `>' token should be interpreted as the greater-than
1216 operator. FALSE if it is the end of a template-id or
1217 template-parameter-list. */
1218 bool greater_than_is_operator_p;
1220 /* TRUE if default arguments are allowed within a parameter list
1221 that starts at this point. FALSE if only a gnu extension makes
1222 them permissible. */
1223 bool default_arg_ok_p;
1225 /* TRUE if we are parsing an integral constant-expression. See
1226 [expr.const] for a precise definition. */
1227 bool integral_constant_expression_p;
1229 /* TRUE if we are parsing an integral constant-expression -- but a
1230 non-constant expression should be permitted as well. This flag
1231 is used when parsing an array bound so that GNU variable-length
1232 arrays are tolerated. */
1233 bool allow_non_integral_constant_expression_p;
1235 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1236 been seen that makes the expression non-constant. */
1237 bool non_integral_constant_expression_p;
1239 /* TRUE if local variable names and `this' are forbidden in the
1240 current context. */
1241 bool local_variables_forbidden_p;
1243 /* TRUE if the declaration we are parsing is part of a
1244 linkage-specification of the form `extern string-literal
1245 declaration'. */
1246 bool in_unbraced_linkage_specification_p;
1248 /* TRUE if we are presently parsing a declarator, after the
1249 direct-declarator. */
1250 bool in_declarator_p;
1252 /* TRUE if we are presently parsing a template-argument-list. */
1253 bool in_template_argument_list_p;
1255 /* TRUE if we are presently parsing the body of an
1256 iteration-statement. */
1257 bool in_iteration_statement_p;
1259 /* TRUE if we are presently parsing the body of a switch
1260 statement. */
1261 bool in_switch_statement_p;
1263 /* TRUE if we are parsing a type-id in an expression context. In
1264 such a situation, both "type (expr)" and "type (type)" are valid
1265 alternatives. */
1266 bool in_type_id_in_expr_p;
1268 /* TRUE if strings in expressions should be translated to the execution
1269 character set. */
1270 bool translate_strings_p;
1272 /* If non-NULL, then we are parsing a construct where new type
1273 definitions are not permitted. The string stored here will be
1274 issued as an error message if a type is defined. */
1275 const char *type_definition_forbidden_message;
1277 /* A list of lists. The outer list is a stack, used for member
1278 functions of local classes. At each level there are two sub-list,
1279 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1280 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1281 TREE_VALUE's. The functions are chained in reverse declaration
1282 order.
1284 The TREE_PURPOSE sublist contains those functions with default
1285 arguments that need post processing, and the TREE_VALUE sublist
1286 contains those functions with definitions that need post
1287 processing.
1289 These lists can only be processed once the outermost class being
1290 defined is complete. */
1291 tree unparsed_functions_queues;
1293 /* The number of classes whose definitions are currently in
1294 progress. */
1295 unsigned num_classes_being_defined;
1297 /* The number of template parameter lists that apply directly to the
1298 current declaration. */
1299 unsigned num_template_parameter_lists;
1300 } cp_parser;
1302 /* The type of a function that parses some kind of expression. */
1303 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1305 /* Prototypes. */
1307 /* Constructors and destructors. */
1309 static cp_parser *cp_parser_new
1310 (void);
1312 /* Routines to parse various constructs.
1314 Those that return `tree' will return the error_mark_node (rather
1315 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1316 Sometimes, they will return an ordinary node if error-recovery was
1317 attempted, even though a parse error occurred. So, to check
1318 whether or not a parse error occurred, you should always use
1319 cp_parser_error_occurred. If the construct is optional (indicated
1320 either by an `_opt' in the name of the function that does the
1321 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1322 the construct is not present. */
1324 /* Lexical conventions [gram.lex] */
1326 static tree cp_parser_identifier
1327 (cp_parser *);
1328 static tree cp_parser_string_literal
1329 (cp_parser *, bool, bool);
1331 /* Basic concepts [gram.basic] */
1333 static bool cp_parser_translation_unit
1334 (cp_parser *);
1336 /* Expressions [gram.expr] */
1338 static tree cp_parser_primary_expression
1339 (cp_parser *, cp_id_kind *, tree *);
1340 static tree cp_parser_id_expression
1341 (cp_parser *, bool, bool, bool *, bool);
1342 static tree cp_parser_unqualified_id
1343 (cp_parser *, bool, bool, bool);
1344 static tree cp_parser_nested_name_specifier_opt
1345 (cp_parser *, bool, bool, bool, bool);
1346 static tree cp_parser_nested_name_specifier
1347 (cp_parser *, bool, bool, bool, bool);
1348 static tree cp_parser_class_or_namespace_name
1349 (cp_parser *, bool, bool, bool, bool, bool);
1350 static tree cp_parser_postfix_expression
1351 (cp_parser *, bool);
1352 static tree cp_parser_postfix_open_square_expression
1353 (cp_parser *, tree, bool);
1354 static tree cp_parser_postfix_dot_deref_expression
1355 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1356 static tree cp_parser_parenthesized_expression_list
1357 (cp_parser *, bool, bool *);
1358 static void cp_parser_pseudo_destructor_name
1359 (cp_parser *, tree *, tree *);
1360 static tree cp_parser_unary_expression
1361 (cp_parser *, bool);
1362 static enum tree_code cp_parser_unary_operator
1363 (cp_token *);
1364 static tree cp_parser_new_expression
1365 (cp_parser *);
1366 static tree cp_parser_new_placement
1367 (cp_parser *);
1368 static tree cp_parser_new_type_id
1369 (cp_parser *, tree *);
1370 static cp_declarator *cp_parser_new_declarator_opt
1371 (cp_parser *);
1372 static cp_declarator *cp_parser_direct_new_declarator
1373 (cp_parser *);
1374 static tree cp_parser_new_initializer
1375 (cp_parser *);
1376 static tree cp_parser_delete_expression
1377 (cp_parser *);
1378 static tree cp_parser_cast_expression
1379 (cp_parser *, bool);
1380 static tree cp_parser_pm_expression
1381 (cp_parser *);
1382 static tree cp_parser_multiplicative_expression
1383 (cp_parser *);
1384 static tree cp_parser_additive_expression
1385 (cp_parser *);
1386 static tree cp_parser_shift_expression
1387 (cp_parser *);
1388 static tree cp_parser_relational_expression
1389 (cp_parser *);
1390 static tree cp_parser_equality_expression
1391 (cp_parser *);
1392 static tree cp_parser_and_expression
1393 (cp_parser *);
1394 static tree cp_parser_exclusive_or_expression
1395 (cp_parser *);
1396 static tree cp_parser_inclusive_or_expression
1397 (cp_parser *);
1398 static tree cp_parser_logical_and_expression
1399 (cp_parser *);
1400 static tree cp_parser_logical_or_expression
1401 (cp_parser *);
1402 static tree cp_parser_question_colon_clause
1403 (cp_parser *, tree);
1404 static tree cp_parser_assignment_expression
1405 (cp_parser *);
1406 static enum tree_code cp_parser_assignment_operator_opt
1407 (cp_parser *);
1408 static tree cp_parser_expression
1409 (cp_parser *);
1410 static tree cp_parser_constant_expression
1411 (cp_parser *, bool, bool *);
1412 static tree cp_parser_builtin_offsetof
1413 (cp_parser *);
1415 /* Statements [gram.stmt.stmt] */
1417 static void cp_parser_statement
1418 (cp_parser *, tree);
1419 static tree cp_parser_labeled_statement
1420 (cp_parser *, tree);
1421 static tree cp_parser_expression_statement
1422 (cp_parser *, tree);
1423 static tree cp_parser_compound_statement
1424 (cp_parser *, tree, bool);
1425 static void cp_parser_statement_seq_opt
1426 (cp_parser *, tree);
1427 static tree cp_parser_selection_statement
1428 (cp_parser *);
1429 static tree cp_parser_condition
1430 (cp_parser *);
1431 static tree cp_parser_iteration_statement
1432 (cp_parser *);
1433 static void cp_parser_for_init_statement
1434 (cp_parser *);
1435 static tree cp_parser_jump_statement
1436 (cp_parser *);
1437 static void cp_parser_declaration_statement
1438 (cp_parser *);
1440 static tree cp_parser_implicitly_scoped_statement
1441 (cp_parser *);
1442 static void cp_parser_already_scoped_statement
1443 (cp_parser *);
1445 /* Declarations [gram.dcl.dcl] */
1447 static void cp_parser_declaration_seq_opt
1448 (cp_parser *);
1449 static void cp_parser_declaration
1450 (cp_parser *);
1451 static void cp_parser_block_declaration
1452 (cp_parser *, bool);
1453 static void cp_parser_simple_declaration
1454 (cp_parser *, bool);
1455 static void cp_parser_decl_specifier_seq
1456 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1457 static tree cp_parser_storage_class_specifier_opt
1458 (cp_parser *);
1459 static tree cp_parser_function_specifier_opt
1460 (cp_parser *, cp_decl_specifier_seq *);
1461 static tree cp_parser_type_specifier
1462 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1463 int *, bool *);
1464 static tree cp_parser_simple_type_specifier
1465 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1466 static tree cp_parser_type_name
1467 (cp_parser *);
1468 static tree cp_parser_elaborated_type_specifier
1469 (cp_parser *, bool, bool);
1470 static tree cp_parser_enum_specifier
1471 (cp_parser *);
1472 static void cp_parser_enumerator_list
1473 (cp_parser *, tree);
1474 static void cp_parser_enumerator_definition
1475 (cp_parser *, tree);
1476 static tree cp_parser_namespace_name
1477 (cp_parser *);
1478 static void cp_parser_namespace_definition
1479 (cp_parser *);
1480 static void cp_parser_namespace_body
1481 (cp_parser *);
1482 static tree cp_parser_qualified_namespace_specifier
1483 (cp_parser *);
1484 static void cp_parser_namespace_alias_definition
1485 (cp_parser *);
1486 static void cp_parser_using_declaration
1487 (cp_parser *);
1488 static void cp_parser_using_directive
1489 (cp_parser *);
1490 static void cp_parser_asm_definition
1491 (cp_parser *);
1492 static void cp_parser_linkage_specification
1493 (cp_parser *);
1495 /* Declarators [gram.dcl.decl] */
1497 static tree cp_parser_init_declarator
1498 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1499 static cp_declarator *cp_parser_declarator
1500 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1501 static cp_declarator *cp_parser_direct_declarator
1502 (cp_parser *, cp_parser_declarator_kind, int *);
1503 static enum tree_code cp_parser_ptr_operator
1504 (cp_parser *, tree *, cp_cv_quals *);
1505 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1506 (cp_parser *);
1507 static tree cp_parser_declarator_id
1508 (cp_parser *);
1509 static tree cp_parser_type_id
1510 (cp_parser *);
1511 static void cp_parser_type_specifier_seq
1512 (cp_parser *, cp_decl_specifier_seq *);
1513 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1514 (cp_parser *);
1515 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1516 (cp_parser *, bool *);
1517 static cp_parameter_declarator *cp_parser_parameter_declaration
1518 (cp_parser *, bool, bool *);
1519 static void cp_parser_function_body
1520 (cp_parser *);
1521 static tree cp_parser_initializer
1522 (cp_parser *, bool *, bool *);
1523 static tree cp_parser_initializer_clause
1524 (cp_parser *, bool *);
1525 static tree cp_parser_initializer_list
1526 (cp_parser *, bool *);
1528 static bool cp_parser_ctor_initializer_opt_and_function_body
1529 (cp_parser *);
1531 /* Classes [gram.class] */
1533 static tree cp_parser_class_name
1534 (cp_parser *, bool, bool, bool, bool, bool, bool);
1535 static tree cp_parser_class_specifier
1536 (cp_parser *);
1537 static tree cp_parser_class_head
1538 (cp_parser *, bool *, tree *);
1539 static enum tag_types cp_parser_class_key
1540 (cp_parser *);
1541 static void cp_parser_member_specification_opt
1542 (cp_parser *);
1543 static void cp_parser_member_declaration
1544 (cp_parser *);
1545 static tree cp_parser_pure_specifier
1546 (cp_parser *);
1547 static tree cp_parser_constant_initializer
1548 (cp_parser *);
1550 /* Derived classes [gram.class.derived] */
1552 static tree cp_parser_base_clause
1553 (cp_parser *);
1554 static tree cp_parser_base_specifier
1555 (cp_parser *);
1557 /* Special member functions [gram.special] */
1559 static tree cp_parser_conversion_function_id
1560 (cp_parser *);
1561 static tree cp_parser_conversion_type_id
1562 (cp_parser *);
1563 static cp_declarator *cp_parser_conversion_declarator_opt
1564 (cp_parser *);
1565 static bool cp_parser_ctor_initializer_opt
1566 (cp_parser *);
1567 static void cp_parser_mem_initializer_list
1568 (cp_parser *);
1569 static tree cp_parser_mem_initializer
1570 (cp_parser *);
1571 static tree cp_parser_mem_initializer_id
1572 (cp_parser *);
1574 /* Overloading [gram.over] */
1576 static tree cp_parser_operator_function_id
1577 (cp_parser *);
1578 static tree cp_parser_operator
1579 (cp_parser *);
1581 /* Templates [gram.temp] */
1583 static void cp_parser_template_declaration
1584 (cp_parser *, bool);
1585 static tree cp_parser_template_parameter_list
1586 (cp_parser *);
1587 static tree cp_parser_template_parameter
1588 (cp_parser *, bool *);
1589 static tree cp_parser_type_parameter
1590 (cp_parser *);
1591 static tree cp_parser_template_id
1592 (cp_parser *, bool, bool, bool);
1593 static tree cp_parser_template_name
1594 (cp_parser *, bool, bool, bool, bool *);
1595 static tree cp_parser_template_argument_list
1596 (cp_parser *);
1597 static tree cp_parser_template_argument
1598 (cp_parser *);
1599 static void cp_parser_explicit_instantiation
1600 (cp_parser *);
1601 static void cp_parser_explicit_specialization
1602 (cp_parser *);
1604 /* Exception handling [gram.exception] */
1606 static tree cp_parser_try_block
1607 (cp_parser *);
1608 static bool cp_parser_function_try_block
1609 (cp_parser *);
1610 static void cp_parser_handler_seq
1611 (cp_parser *);
1612 static void cp_parser_handler
1613 (cp_parser *);
1614 static tree cp_parser_exception_declaration
1615 (cp_parser *);
1616 static tree cp_parser_throw_expression
1617 (cp_parser *);
1618 static tree cp_parser_exception_specification_opt
1619 (cp_parser *);
1620 static tree cp_parser_type_id_list
1621 (cp_parser *);
1623 /* GNU Extensions */
1625 static tree cp_parser_asm_specification_opt
1626 (cp_parser *);
1627 static tree cp_parser_asm_operand_list
1628 (cp_parser *);
1629 static tree cp_parser_asm_clobber_list
1630 (cp_parser *);
1631 static tree cp_parser_attributes_opt
1632 (cp_parser *);
1633 static tree cp_parser_attribute_list
1634 (cp_parser *);
1635 static bool cp_parser_extension_opt
1636 (cp_parser *, int *);
1637 static void cp_parser_label_declaration
1638 (cp_parser *);
1640 /* Utility Routines */
1642 static tree cp_parser_lookup_name
1643 (cp_parser *, tree, bool, bool, bool, bool, bool *);
1644 static tree cp_parser_lookup_name_simple
1645 (cp_parser *, tree);
1646 static tree cp_parser_maybe_treat_template_as_class
1647 (tree, bool);
1648 static bool cp_parser_check_declarator_template_parameters
1649 (cp_parser *, cp_declarator *);
1650 static bool cp_parser_check_template_parameters
1651 (cp_parser *, unsigned);
1652 static tree cp_parser_simple_cast_expression
1653 (cp_parser *);
1654 static tree cp_parser_binary_expression
1655 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1656 static tree cp_parser_global_scope_opt
1657 (cp_parser *, bool);
1658 static bool cp_parser_constructor_declarator_p
1659 (cp_parser *, bool);
1660 static tree cp_parser_function_definition_from_specifiers_and_declarator
1661 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1662 static tree cp_parser_function_definition_after_declarator
1663 (cp_parser *, bool);
1664 static void cp_parser_template_declaration_after_export
1665 (cp_parser *, bool);
1666 static tree cp_parser_single_declaration
1667 (cp_parser *, bool, bool *);
1668 static tree cp_parser_functional_cast
1669 (cp_parser *, tree);
1670 static tree cp_parser_save_member_function_body
1671 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1672 static tree cp_parser_enclosed_template_argument_list
1673 (cp_parser *);
1674 static void cp_parser_save_default_args
1675 (cp_parser *, tree);
1676 static void cp_parser_late_parsing_for_member
1677 (cp_parser *, tree);
1678 static void cp_parser_late_parsing_default_args
1679 (cp_parser *, tree);
1680 static tree cp_parser_sizeof_operand
1681 (cp_parser *, enum rid);
1682 static bool cp_parser_declares_only_class_p
1683 (cp_parser *);
1684 static void cp_parser_set_storage_class
1685 (cp_decl_specifier_seq *, cp_storage_class);
1686 static void cp_parser_set_decl_spec_type
1687 (cp_decl_specifier_seq *, tree, bool);
1688 static bool cp_parser_friend_p
1689 (const cp_decl_specifier_seq *);
1690 static cp_token *cp_parser_require
1691 (cp_parser *, enum cpp_ttype, const char *);
1692 static cp_token *cp_parser_require_keyword
1693 (cp_parser *, enum rid, const char *);
1694 static bool cp_parser_token_starts_function_definition_p
1695 (cp_token *);
1696 static bool cp_parser_next_token_starts_class_definition_p
1697 (cp_parser *);
1698 static bool cp_parser_next_token_ends_template_argument_p
1699 (cp_parser *);
1700 static bool cp_parser_nth_token_starts_template_argument_list_p
1701 (cp_parser *, size_t);
1702 static enum tag_types cp_parser_token_is_class_key
1703 (cp_token *);
1704 static void cp_parser_check_class_key
1705 (enum tag_types, tree type);
1706 static void cp_parser_check_access_in_redeclaration
1707 (tree type);
1708 static bool cp_parser_optional_template_keyword
1709 (cp_parser *);
1710 static void cp_parser_pre_parsed_nested_name_specifier
1711 (cp_parser *);
1712 static void cp_parser_cache_group
1713 (cp_parser *, enum cpp_ttype, unsigned);
1714 static void cp_parser_parse_tentatively
1715 (cp_parser *);
1716 static void cp_parser_commit_to_tentative_parse
1717 (cp_parser *);
1718 static void cp_parser_abort_tentative_parse
1719 (cp_parser *);
1720 static bool cp_parser_parse_definitely
1721 (cp_parser *);
1722 static inline bool cp_parser_parsing_tentatively
1723 (cp_parser *);
1724 static bool cp_parser_committed_to_tentative_parse
1725 (cp_parser *);
1726 static void cp_parser_error
1727 (cp_parser *, const char *);
1728 static void cp_parser_name_lookup_error
1729 (cp_parser *, tree, tree, const char *);
1730 static bool cp_parser_simulate_error
1731 (cp_parser *);
1732 static void cp_parser_check_type_definition
1733 (cp_parser *);
1734 static void cp_parser_check_for_definition_in_return_type
1735 (cp_declarator *, int);
1736 static void cp_parser_check_for_invalid_template_id
1737 (cp_parser *, tree);
1738 static bool cp_parser_non_integral_constant_expression
1739 (cp_parser *, const char *);
1740 static void cp_parser_diagnose_invalid_type_name
1741 (cp_parser *, tree, tree);
1742 static bool cp_parser_parse_and_diagnose_invalid_type_name
1743 (cp_parser *);
1744 static int cp_parser_skip_to_closing_parenthesis
1745 (cp_parser *, bool, bool, bool);
1746 static void cp_parser_skip_to_end_of_statement
1747 (cp_parser *);
1748 static void cp_parser_consume_semicolon_at_end_of_statement
1749 (cp_parser *);
1750 static void cp_parser_skip_to_end_of_block_or_statement
1751 (cp_parser *);
1752 static void cp_parser_skip_to_closing_brace
1753 (cp_parser *);
1754 static void cp_parser_skip_until_found
1755 (cp_parser *, enum cpp_ttype, const char *);
1756 static bool cp_parser_error_occurred
1757 (cp_parser *);
1758 static bool cp_parser_allow_gnu_extensions_p
1759 (cp_parser *);
1760 static bool cp_parser_is_string_literal
1761 (cp_token *);
1762 static bool cp_parser_is_keyword
1763 (cp_token *, enum rid);
1764 static tree cp_parser_make_typename_type
1765 (cp_parser *, tree, tree);
1767 /* Returns nonzero if we are parsing tentatively. */
1769 static inline bool
1770 cp_parser_parsing_tentatively (cp_parser* parser)
1772 return parser->context->next != NULL;
1775 /* Returns nonzero if TOKEN is a string literal. */
1777 static bool
1778 cp_parser_is_string_literal (cp_token* token)
1780 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1783 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1785 static bool
1786 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1788 return token->keyword == keyword;
1791 /* Issue the indicated error MESSAGE. */
1793 static void
1794 cp_parser_error (cp_parser* parser, const char* message)
1796 /* Output the MESSAGE -- unless we're parsing tentatively. */
1797 if (!cp_parser_simulate_error (parser))
1799 cp_token *token;
1800 token = cp_lexer_peek_token (parser->lexer);
1801 c_parse_error (message,
1802 /* Because c_parser_error does not understand
1803 CPP_KEYWORD, keywords are treated like
1804 identifiers. */
1805 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1806 token->value);
1810 /* Issue an error about name-lookup failing. NAME is the
1811 IDENTIFIER_NODE DECL is the result of
1812 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1813 the thing that we hoped to find. */
1815 static void
1816 cp_parser_name_lookup_error (cp_parser* parser,
1817 tree name,
1818 tree decl,
1819 const char* desired)
1821 /* If name lookup completely failed, tell the user that NAME was not
1822 declared. */
1823 if (decl == error_mark_node)
1825 if (parser->scope && parser->scope != global_namespace)
1826 error ("`%D::%D' has not been declared",
1827 parser->scope, name);
1828 else if (parser->scope == global_namespace)
1829 error ("`::%D' has not been declared", name);
1830 else if (parser->object_scope
1831 && !CLASS_TYPE_P (parser->object_scope))
1832 error ("request for member `%D' in non-class type `%T'",
1833 name, parser->object_scope);
1834 else if (parser->object_scope)
1835 error ("`%T::%D' has not been declared",
1836 parser->object_scope, name);
1837 else
1838 error ("`%D' has not been declared", name);
1840 else if (parser->scope && parser->scope != global_namespace)
1841 error ("`%D::%D' %s", parser->scope, name, desired);
1842 else if (parser->scope == global_namespace)
1843 error ("`::%D' %s", name, desired);
1844 else
1845 error ("`%D' %s", name, desired);
1848 /* If we are parsing tentatively, remember that an error has occurred
1849 during this tentative parse. Returns true if the error was
1850 simulated; false if a message should be issued by the caller. */
1852 static bool
1853 cp_parser_simulate_error (cp_parser* parser)
1855 if (cp_parser_parsing_tentatively (parser)
1856 && !cp_parser_committed_to_tentative_parse (parser))
1858 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1859 return true;
1861 return false;
1864 /* This function is called when a type is defined. If type
1865 definitions are forbidden at this point, an error message is
1866 issued. */
1868 static void
1869 cp_parser_check_type_definition (cp_parser* parser)
1871 /* If types are forbidden here, issue a message. */
1872 if (parser->type_definition_forbidden_message)
1873 /* Use `%s' to print the string in case there are any escape
1874 characters in the message. */
1875 error ("%s", parser->type_definition_forbidden_message);
1878 /* This function is called when a declaration is parsed. If
1879 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1880 indicates that a type was defined in the decl-specifiers for DECL,
1881 then an error is issued. */
1883 static void
1884 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1885 int declares_class_or_enum)
1887 /* [dcl.fct] forbids type definitions in return types.
1888 Unfortunately, it's not easy to know whether or not we are
1889 processing a return type until after the fact. */
1890 while (declarator
1891 && (declarator->kind == cdk_pointer
1892 || declarator->kind == cdk_reference
1893 || declarator->kind == cdk_ptrmem))
1894 declarator = declarator->declarator;
1895 if (declarator
1896 && declarator->kind == cdk_function
1897 && declares_class_or_enum & 2)
1898 error ("new types may not be defined in a return type");
1901 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1902 "<" in any valid C++ program. If the next token is indeed "<",
1903 issue a message warning the user about what appears to be an
1904 invalid attempt to form a template-id. */
1906 static void
1907 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1908 tree type)
1910 ptrdiff_t start;
1911 cp_token *token;
1913 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1915 if (TYPE_P (type))
1916 error ("`%T' is not a template", type);
1917 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1918 error ("`%E' is not a template", type);
1919 else
1920 error ("invalid template-id");
1921 /* Remember the location of the invalid "<". */
1922 if (cp_parser_parsing_tentatively (parser)
1923 && !cp_parser_committed_to_tentative_parse (parser))
1925 token = cp_lexer_peek_token (parser->lexer);
1926 token = cp_lexer_prev_token (parser->lexer, token);
1927 start = cp_lexer_token_difference (parser->lexer,
1928 parser->lexer->buffer,
1929 token);
1931 else
1932 start = -1;
1933 /* Consume the "<". */
1934 cp_lexer_consume_token (parser->lexer);
1935 /* Parse the template arguments. */
1936 cp_parser_enclosed_template_argument_list (parser);
1937 /* Permanently remove the invalid template arguments so that
1938 this error message is not issued again. */
1939 if (start >= 0)
1941 token = cp_lexer_advance_token (parser->lexer,
1942 parser->lexer->buffer,
1943 start);
1944 cp_lexer_purge_tokens_after (parser->lexer, token);
1949 /* If parsing an integral constant-expression, issue an error message
1950 about the fact that THING appeared and return true. Otherwise,
1951 return false, marking the current expression as non-constant. */
1953 static bool
1954 cp_parser_non_integral_constant_expression (cp_parser *parser,
1955 const char *thing)
1957 if (parser->integral_constant_expression_p)
1959 if (!parser->allow_non_integral_constant_expression_p)
1961 error ("%s cannot appear in a constant-expression", thing);
1962 return true;
1964 parser->non_integral_constant_expression_p = true;
1966 return false;
1969 /* Emit a diagnostic for an invalid type name. Consider also if it is
1970 qualified or not and the result of a lookup, to provide a better
1971 message. */
1973 static void
1974 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1976 tree decl, old_scope;
1977 /* Try to lookup the identifier. */
1978 old_scope = parser->scope;
1979 parser->scope = scope;
1980 decl = cp_parser_lookup_name_simple (parser, id);
1981 parser->scope = old_scope;
1982 /* If the lookup found a template-name, it means that the user forgot
1983 to specify an argument list. Emit an useful error message. */
1984 if (TREE_CODE (decl) == TEMPLATE_DECL)
1985 error ("invalid use of template-name `%E' without an argument list",
1986 decl);
1987 else if (!parser->scope)
1989 /* Issue an error message. */
1990 error ("`%E' does not name a type", id);
1991 /* If we're in a template class, it's possible that the user was
1992 referring to a type from a base class. For example:
1994 template <typename T> struct A { typedef T X; };
1995 template <typename T> struct B : public A<T> { X x; };
1997 The user should have said "typename A<T>::X". */
1998 if (processing_template_decl && current_class_type)
2000 tree b;
2002 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2004 b = TREE_CHAIN (b))
2006 tree base_type = BINFO_TYPE (b);
2007 if (CLASS_TYPE_P (base_type)
2008 && dependent_type_p (base_type))
2010 tree field;
2011 /* Go from a particular instantiation of the
2012 template (which will have an empty TYPE_FIELDs),
2013 to the main version. */
2014 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2015 for (field = TYPE_FIELDS (base_type);
2016 field;
2017 field = TREE_CHAIN (field))
2018 if (TREE_CODE (field) == TYPE_DECL
2019 && DECL_NAME (field) == id)
2021 inform ("(perhaps `typename %T::%E' was intended)",
2022 BINFO_TYPE (b), id);
2023 break;
2025 if (field)
2026 break;
2031 /* Here we diagnose qualified-ids where the scope is actually correct,
2032 but the identifier does not resolve to a valid type name. */
2033 else
2035 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2036 error ("`%E' in namespace `%E' does not name a type",
2037 id, parser->scope);
2038 else if (TYPE_P (parser->scope))
2039 error ("`%E' in class `%T' does not name a type",
2040 id, parser->scope);
2041 else
2042 gcc_unreachable ();
2046 /* Check for a common situation where a type-name should be present,
2047 but is not, and issue a sensible error message. Returns true if an
2048 invalid type-name was detected.
2050 The situation handled by this function are variable declarations of the
2051 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2052 Usually, `ID' should name a type, but if we got here it means that it
2053 does not. We try to emit the best possible error message depending on
2054 how exactly the id-expression looks like.
2057 static bool
2058 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2060 tree id;
2062 cp_parser_parse_tentatively (parser);
2063 id = cp_parser_id_expression (parser,
2064 /*template_keyword_p=*/false,
2065 /*check_dependency_p=*/true,
2066 /*template_p=*/NULL,
2067 /*declarator_p=*/true);
2068 /* After the id-expression, there should be a plain identifier,
2069 otherwise this is not a simple variable declaration. Also, if
2070 the scope is dependent, we cannot do much. */
2071 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2072 || (parser->scope && TYPE_P (parser->scope)
2073 && dependent_type_p (parser->scope)))
2075 cp_parser_abort_tentative_parse (parser);
2076 return false;
2078 if (!cp_parser_parse_definitely (parser)
2079 || TREE_CODE (id) != IDENTIFIER_NODE)
2080 return false;
2082 /* Emit a diagnostic for the invalid type. */
2083 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2084 /* Skip to the end of the declaration; there's no point in
2085 trying to process it. */
2086 cp_parser_skip_to_end_of_block_or_statement (parser);
2087 return true;
2090 /* Consume tokens up to, and including, the next non-nested closing `)'.
2091 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2092 are doing error recovery. Returns -1 if OR_COMMA is true and we
2093 found an unnested comma. */
2095 static int
2096 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2097 bool recovering,
2098 bool or_comma,
2099 bool consume_paren)
2101 unsigned paren_depth = 0;
2102 unsigned brace_depth = 0;
2103 int result;
2105 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2106 && !cp_parser_committed_to_tentative_parse (parser))
2107 return 0;
2109 while (true)
2111 cp_token *token;
2113 /* If we've run out of tokens, then there is no closing `)'. */
2114 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2116 result = 0;
2117 break;
2120 token = cp_lexer_peek_token (parser->lexer);
2122 /* This matches the processing in skip_to_end_of_statement. */
2123 if (token->type == CPP_SEMICOLON && !brace_depth)
2125 result = 0;
2126 break;
2128 if (token->type == CPP_OPEN_BRACE)
2129 ++brace_depth;
2130 if (token->type == CPP_CLOSE_BRACE)
2132 if (!brace_depth--)
2134 result = 0;
2135 break;
2138 if (recovering && or_comma && token->type == CPP_COMMA
2139 && !brace_depth && !paren_depth)
2141 result = -1;
2142 break;
2145 if (!brace_depth)
2147 /* If it is an `(', we have entered another level of nesting. */
2148 if (token->type == CPP_OPEN_PAREN)
2149 ++paren_depth;
2150 /* If it is a `)', then we might be done. */
2151 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2153 if (consume_paren)
2154 cp_lexer_consume_token (parser->lexer);
2156 result = 1;
2157 break;
2162 /* Consume the token. */
2163 cp_lexer_consume_token (parser->lexer);
2166 return result;
2169 /* Consume tokens until we reach the end of the current statement.
2170 Normally, that will be just before consuming a `;'. However, if a
2171 non-nested `}' comes first, then we stop before consuming that. */
2173 static void
2174 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2176 unsigned nesting_depth = 0;
2178 while (true)
2180 cp_token *token;
2182 /* Peek at the next token. */
2183 token = cp_lexer_peek_token (parser->lexer);
2184 /* If we've run out of tokens, stop. */
2185 if (token->type == CPP_EOF)
2186 break;
2187 /* If the next token is a `;', we have reached the end of the
2188 statement. */
2189 if (token->type == CPP_SEMICOLON && !nesting_depth)
2190 break;
2191 /* If the next token is a non-nested `}', then we have reached
2192 the end of the current block. */
2193 if (token->type == CPP_CLOSE_BRACE)
2195 /* If this is a non-nested `}', stop before consuming it.
2196 That way, when confronted with something like:
2198 { 3 + }
2200 we stop before consuming the closing `}', even though we
2201 have not yet reached a `;'. */
2202 if (nesting_depth == 0)
2203 break;
2204 /* If it is the closing `}' for a block that we have
2205 scanned, stop -- but only after consuming the token.
2206 That way given:
2208 void f g () { ... }
2209 typedef int I;
2211 we will stop after the body of the erroneously declared
2212 function, but before consuming the following `typedef'
2213 declaration. */
2214 if (--nesting_depth == 0)
2216 cp_lexer_consume_token (parser->lexer);
2217 break;
2220 /* If it the next token is a `{', then we are entering a new
2221 block. Consume the entire block. */
2222 else if (token->type == CPP_OPEN_BRACE)
2223 ++nesting_depth;
2224 /* Consume the token. */
2225 cp_lexer_consume_token (parser->lexer);
2229 /* This function is called at the end of a statement or declaration.
2230 If the next token is a semicolon, it is consumed; otherwise, error
2231 recovery is attempted. */
2233 static void
2234 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2236 /* Look for the trailing `;'. */
2237 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2239 /* If there is additional (erroneous) input, skip to the end of
2240 the statement. */
2241 cp_parser_skip_to_end_of_statement (parser);
2242 /* If the next token is now a `;', consume it. */
2243 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2244 cp_lexer_consume_token (parser->lexer);
2248 /* Skip tokens until we have consumed an entire block, or until we
2249 have consumed a non-nested `;'. */
2251 static void
2252 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2254 unsigned nesting_depth = 0;
2256 while (true)
2258 cp_token *token;
2260 /* Peek at the next token. */
2261 token = cp_lexer_peek_token (parser->lexer);
2262 /* If we've run out of tokens, stop. */
2263 if (token->type == CPP_EOF)
2264 break;
2265 /* If the next token is a `;', we have reached the end of the
2266 statement. */
2267 if (token->type == CPP_SEMICOLON && !nesting_depth)
2269 /* Consume the `;'. */
2270 cp_lexer_consume_token (parser->lexer);
2271 break;
2273 /* Consume the token. */
2274 token = cp_lexer_consume_token (parser->lexer);
2275 /* If the next token is a non-nested `}', then we have reached
2276 the end of the current block. */
2277 if (token->type == CPP_CLOSE_BRACE
2278 && (nesting_depth == 0 || --nesting_depth == 0))
2279 break;
2280 /* If it the next token is a `{', then we are entering a new
2281 block. Consume the entire block. */
2282 if (token->type == CPP_OPEN_BRACE)
2283 ++nesting_depth;
2287 /* Skip tokens until a non-nested closing curly brace is the next
2288 token. */
2290 static void
2291 cp_parser_skip_to_closing_brace (cp_parser *parser)
2293 unsigned nesting_depth = 0;
2295 while (true)
2297 cp_token *token;
2299 /* Peek at the next token. */
2300 token = cp_lexer_peek_token (parser->lexer);
2301 /* If we've run out of tokens, stop. */
2302 if (token->type == CPP_EOF)
2303 break;
2304 /* If the next token is a non-nested `}', then we have reached
2305 the end of the current block. */
2306 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2307 break;
2308 /* If it the next token is a `{', then we are entering a new
2309 block. Consume the entire block. */
2310 else if (token->type == CPP_OPEN_BRACE)
2311 ++nesting_depth;
2312 /* Consume the token. */
2313 cp_lexer_consume_token (parser->lexer);
2317 /* This is a simple wrapper around make_typename_type. When the id is
2318 an unresolved identifier node, we can provide a superior diagnostic
2319 using cp_parser_diagnose_invalid_type_name. */
2321 static tree
2322 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2324 tree result;
2325 if (TREE_CODE (id) == IDENTIFIER_NODE)
2327 result = make_typename_type (scope, id, /*complain=*/0);
2328 if (result == error_mark_node)
2329 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2330 return result;
2332 return make_typename_type (scope, id, tf_error);
2336 /* Create a new C++ parser. */
2338 static cp_parser *
2339 cp_parser_new (void)
2341 cp_parser *parser;
2342 cp_lexer *lexer;
2344 /* cp_lexer_new_main is called before calling ggc_alloc because
2345 cp_lexer_new_main might load a PCH file. */
2346 lexer = cp_lexer_new_main ();
2348 parser = GGC_CNEW (cp_parser);
2349 parser->lexer = lexer;
2350 parser->context = cp_parser_context_new (NULL);
2352 /* For now, we always accept GNU extensions. */
2353 parser->allow_gnu_extensions_p = 1;
2355 /* The `>' token is a greater-than operator, not the end of a
2356 template-id. */
2357 parser->greater_than_is_operator_p = true;
2359 parser->default_arg_ok_p = true;
2361 /* We are not parsing a constant-expression. */
2362 parser->integral_constant_expression_p = false;
2363 parser->allow_non_integral_constant_expression_p = false;
2364 parser->non_integral_constant_expression_p = false;
2366 /* Local variable names are not forbidden. */
2367 parser->local_variables_forbidden_p = false;
2369 /* We are not processing an `extern "C"' declaration. */
2370 parser->in_unbraced_linkage_specification_p = false;
2372 /* We are not processing a declarator. */
2373 parser->in_declarator_p = false;
2375 /* We are not processing a template-argument-list. */
2376 parser->in_template_argument_list_p = false;
2378 /* We are not in an iteration statement. */
2379 parser->in_iteration_statement_p = false;
2381 /* We are not in a switch statement. */
2382 parser->in_switch_statement_p = false;
2384 /* We are not parsing a type-id inside an expression. */
2385 parser->in_type_id_in_expr_p = false;
2387 /* String literals should be translated to the execution character set. */
2388 parser->translate_strings_p = true;
2390 /* The unparsed function queue is empty. */
2391 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2393 /* There are no classes being defined. */
2394 parser->num_classes_being_defined = 0;
2396 /* No template parameters apply. */
2397 parser->num_template_parameter_lists = 0;
2399 return parser;
2402 /* Lexical conventions [gram.lex] */
2404 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2405 identifier. */
2407 static tree
2408 cp_parser_identifier (cp_parser* parser)
2410 cp_token *token;
2412 /* Look for the identifier. */
2413 token = cp_parser_require (parser, CPP_NAME, "identifier");
2414 /* Return the value. */
2415 return token ? token->value : error_mark_node;
2418 /* Parse a sequence of adjacent string constants. Returns a
2419 TREE_STRING representing the combined, nul-terminated string
2420 constant. If TRANSLATE is true, translate the string to the
2421 execution character set. If WIDE_OK is true, a wide string is
2422 invalid here.
2424 C++98 [lex.string] says that if a narrow string literal token is
2425 adjacent to a wide string literal token, the behavior is undefined.
2426 However, C99 6.4.5p4 says that this results in a wide string literal.
2427 We follow C99 here, for consistency with the C front end.
2429 This code is largely lifted from lex_string() in c-lex.c.
2431 FUTURE: ObjC++ will need to handle @-strings here. */
2432 static tree
2433 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2435 tree value;
2436 bool wide = false;
2437 size_t count;
2438 struct obstack str_ob;
2439 cpp_string str, istr, *strs;
2440 cp_token *tok;
2442 tok = cp_lexer_peek_token (parser->lexer);
2443 if (!cp_parser_is_string_literal (tok))
2445 cp_parser_error (parser, "expected string-literal");
2446 return error_mark_node;
2449 /* Try to avoid the overhead of creating and destroying an obstack
2450 for the common case of just one string. */
2451 if (!cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
2453 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2454 str.len = TREE_STRING_LENGTH (tok->value);
2455 count = 1;
2456 if (tok->type == CPP_WSTRING)
2457 wide = true;
2458 cp_lexer_consume_token (parser->lexer);
2460 strs = &str;
2462 else
2464 gcc_obstack_init (&str_ob);
2465 count = 0;
2469 count++;
2470 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2471 str.len = TREE_STRING_LENGTH (tok->value);
2472 if (tok->type == CPP_WSTRING)
2473 wide = true;
2475 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2477 /* We do it this way so that, if we have to issue semantic
2478 errors on this string literal, the source position will
2479 be that of the first token of the string. */
2480 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
2481 cp_lexer_consume_token (parser->lexer);
2483 while (cp_parser_is_string_literal (tok));
2485 strs = (cpp_string *) obstack_finish (&str_ob);
2488 if (wide && !wide_ok)
2490 cp_parser_error (parser, "a wide string is invalid in this context");
2491 wide = false;
2494 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2495 (parse_in, strs, count, &istr, wide))
2497 value = build_string (istr.len, (char *)istr.text);
2498 free ((void *)istr.text);
2500 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2501 value = fix_string_type (value);
2503 else
2504 /* cpp_interpret_string has issued an error. */
2505 value = error_mark_node;
2507 if (count > 1)
2508 obstack_free (&str_ob, 0);
2510 return value;
2514 /* Basic concepts [gram.basic] */
2516 /* Parse a translation-unit.
2518 translation-unit:
2519 declaration-seq [opt]
2521 Returns TRUE if all went well. */
2523 static bool
2524 cp_parser_translation_unit (cp_parser* parser)
2526 /* The address of the first non-permanent object on the declarator
2527 obstack. */
2528 static void *declarator_obstack_base;
2530 bool success;
2532 /* Create the declarator obstack, if necessary. */
2533 if (!cp_error_declarator)
2535 gcc_obstack_init (&declarator_obstack);
2536 /* Create the error declarator. */
2537 cp_error_declarator = make_declarator (cdk_error);
2538 /* Create the empty parameter list. */
2539 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2540 /* Remember where the base of the declarator obstack lies. */
2541 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2544 while (true)
2546 cp_parser_declaration_seq_opt (parser);
2548 /* If there are no tokens left then all went well. */
2549 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2551 /* Consume the EOF token. */
2552 cp_parser_require (parser, CPP_EOF, "end-of-file");
2554 /* Get rid of the token array; we don't need it any more. */
2555 cp_lexer_destroy (parser->lexer);
2556 parser->lexer = NULL;
2558 /* Finish up. */
2559 finish_translation_unit ();
2561 success = true;
2562 break;
2564 else
2566 cp_parser_error (parser, "expected declaration");
2567 success = false;
2568 break;
2572 /* Make sure the declarator obstack was fully cleaned up. */
2573 gcc_assert (obstack_next_free (&declarator_obstack)
2574 == declarator_obstack_base);
2576 /* All went well. */
2577 return success;
2580 /* Expressions [gram.expr] */
2582 /* Parse a primary-expression.
2584 primary-expression:
2585 literal
2586 this
2587 ( expression )
2588 id-expression
2590 GNU Extensions:
2592 primary-expression:
2593 ( compound-statement )
2594 __builtin_va_arg ( assignment-expression , type-id )
2596 literal:
2597 __null
2599 Returns a representation of the expression.
2601 *IDK indicates what kind of id-expression (if any) was present.
2603 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2604 used as the operand of a pointer-to-member. In that case,
2605 *QUALIFYING_CLASS gives the class that is used as the qualifying
2606 class in the pointer-to-member. */
2608 static tree
2609 cp_parser_primary_expression (cp_parser *parser,
2610 cp_id_kind *idk,
2611 tree *qualifying_class)
2613 cp_token *token;
2615 /* Assume the primary expression is not an id-expression. */
2616 *idk = CP_ID_KIND_NONE;
2617 /* And that it cannot be used as pointer-to-member. */
2618 *qualifying_class = NULL_TREE;
2620 /* Peek at the next token. */
2621 token = cp_lexer_peek_token (parser->lexer);
2622 switch (token->type)
2624 /* literal:
2625 integer-literal
2626 character-literal
2627 floating-literal
2628 string-literal
2629 boolean-literal */
2630 case CPP_CHAR:
2631 case CPP_WCHAR:
2632 case CPP_NUMBER:
2633 token = cp_lexer_consume_token (parser->lexer);
2634 return token->value;
2636 case CPP_STRING:
2637 case CPP_WSTRING:
2638 /* ??? Should wide strings be allowed when parser->translate_strings_p
2639 is false (i.e. in attributes)? If not, we can kill the third
2640 argument to cp_parser_string_literal. */
2641 return cp_parser_string_literal (parser,
2642 parser->translate_strings_p,
2643 true);
2645 case CPP_OPEN_PAREN:
2647 tree expr;
2648 bool saved_greater_than_is_operator_p;
2650 /* Consume the `('. */
2651 cp_lexer_consume_token (parser->lexer);
2652 /* Within a parenthesized expression, a `>' token is always
2653 the greater-than operator. */
2654 saved_greater_than_is_operator_p
2655 = parser->greater_than_is_operator_p;
2656 parser->greater_than_is_operator_p = true;
2657 /* If we see `( { ' then we are looking at the beginning of
2658 a GNU statement-expression. */
2659 if (cp_parser_allow_gnu_extensions_p (parser)
2660 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2662 /* Statement-expressions are not allowed by the standard. */
2663 if (pedantic)
2664 pedwarn ("ISO C++ forbids braced-groups within expressions");
2666 /* And they're not allowed outside of a function-body; you
2667 cannot, for example, write:
2669 int i = ({ int j = 3; j + 1; });
2671 at class or namespace scope. */
2672 if (!at_function_scope_p ())
2673 error ("statement-expressions are allowed only inside functions");
2674 /* Start the statement-expression. */
2675 expr = begin_stmt_expr ();
2676 /* Parse the compound-statement. */
2677 cp_parser_compound_statement (parser, expr, false);
2678 /* Finish up. */
2679 expr = finish_stmt_expr (expr, false);
2681 else
2683 /* Parse the parenthesized expression. */
2684 expr = cp_parser_expression (parser);
2685 /* Let the front end know that this expression was
2686 enclosed in parentheses. This matters in case, for
2687 example, the expression is of the form `A::B', since
2688 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2689 not. */
2690 finish_parenthesized_expr (expr);
2692 /* The `>' token might be the end of a template-id or
2693 template-parameter-list now. */
2694 parser->greater_than_is_operator_p
2695 = saved_greater_than_is_operator_p;
2696 /* Consume the `)'. */
2697 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2698 cp_parser_skip_to_end_of_statement (parser);
2700 return expr;
2703 case CPP_KEYWORD:
2704 switch (token->keyword)
2706 /* These two are the boolean literals. */
2707 case RID_TRUE:
2708 cp_lexer_consume_token (parser->lexer);
2709 return boolean_true_node;
2710 case RID_FALSE:
2711 cp_lexer_consume_token (parser->lexer);
2712 return boolean_false_node;
2714 /* The `__null' literal. */
2715 case RID_NULL:
2716 cp_lexer_consume_token (parser->lexer);
2717 return null_node;
2719 /* Recognize the `this' keyword. */
2720 case RID_THIS:
2721 cp_lexer_consume_token (parser->lexer);
2722 if (parser->local_variables_forbidden_p)
2724 error ("`this' may not be used in this context");
2725 return error_mark_node;
2727 /* Pointers cannot appear in constant-expressions. */
2728 if (cp_parser_non_integral_constant_expression (parser,
2729 "`this'"))
2730 return error_mark_node;
2731 return finish_this_expr ();
2733 /* The `operator' keyword can be the beginning of an
2734 id-expression. */
2735 case RID_OPERATOR:
2736 goto id_expression;
2738 case RID_FUNCTION_NAME:
2739 case RID_PRETTY_FUNCTION_NAME:
2740 case RID_C99_FUNCTION_NAME:
2741 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2742 __func__ are the names of variables -- but they are
2743 treated specially. Therefore, they are handled here,
2744 rather than relying on the generic id-expression logic
2745 below. Grammatically, these names are id-expressions.
2747 Consume the token. */
2748 token = cp_lexer_consume_token (parser->lexer);
2749 /* Look up the name. */
2750 return finish_fname (token->value);
2752 case RID_VA_ARG:
2754 tree expression;
2755 tree type;
2757 /* The `__builtin_va_arg' construct is used to handle
2758 `va_arg'. Consume the `__builtin_va_arg' token. */
2759 cp_lexer_consume_token (parser->lexer);
2760 /* Look for the opening `('. */
2761 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2762 /* Now, parse the assignment-expression. */
2763 expression = cp_parser_assignment_expression (parser);
2764 /* Look for the `,'. */
2765 cp_parser_require (parser, CPP_COMMA, "`,'");
2766 /* Parse the type-id. */
2767 type = cp_parser_type_id (parser);
2768 /* Look for the closing `)'. */
2769 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2770 /* Using `va_arg' in a constant-expression is not
2771 allowed. */
2772 if (cp_parser_non_integral_constant_expression (parser,
2773 "`va_arg'"))
2774 return error_mark_node;
2775 return build_x_va_arg (expression, type);
2778 case RID_OFFSETOF:
2779 return cp_parser_builtin_offsetof (parser);
2781 default:
2782 cp_parser_error (parser, "expected primary-expression");
2783 return error_mark_node;
2786 /* An id-expression can start with either an identifier, a
2787 `::' as the beginning of a qualified-id, or the "operator"
2788 keyword. */
2789 case CPP_NAME:
2790 case CPP_SCOPE:
2791 case CPP_TEMPLATE_ID:
2792 case CPP_NESTED_NAME_SPECIFIER:
2794 tree id_expression;
2795 tree decl;
2796 const char *error_msg;
2798 id_expression:
2799 /* Parse the id-expression. */
2800 id_expression
2801 = cp_parser_id_expression (parser,
2802 /*template_keyword_p=*/false,
2803 /*check_dependency_p=*/true,
2804 /*template_p=*/NULL,
2805 /*declarator_p=*/false);
2806 if (id_expression == error_mark_node)
2807 return error_mark_node;
2808 /* If we have a template-id, then no further lookup is
2809 required. If the template-id was for a template-class, we
2810 will sometimes have a TYPE_DECL at this point. */
2811 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2812 || TREE_CODE (id_expression) == TYPE_DECL)
2813 decl = id_expression;
2814 /* Look up the name. */
2815 else
2817 bool ambiguous_p;
2819 decl = cp_parser_lookup_name (parser, id_expression,
2820 /*is_type=*/false,
2821 /*is_template=*/false,
2822 /*is_namespace=*/false,
2823 /*check_dependency=*/true,
2824 &ambiguous_p);
2825 /* If the lookup was ambiguous, an error will already have
2826 been issued. */
2827 if (ambiguous_p)
2828 return error_mark_node;
2829 /* If name lookup gives us a SCOPE_REF, then the
2830 qualifying scope was dependent. Just propagate the
2831 name. */
2832 if (TREE_CODE (decl) == SCOPE_REF)
2834 if (TYPE_P (TREE_OPERAND (decl, 0)))
2835 *qualifying_class = TREE_OPERAND (decl, 0);
2836 return decl;
2838 /* Check to see if DECL is a local variable in a context
2839 where that is forbidden. */
2840 if (parser->local_variables_forbidden_p
2841 && local_variable_p (decl))
2843 /* It might be that we only found DECL because we are
2844 trying to be generous with pre-ISO scoping rules.
2845 For example, consider:
2847 int i;
2848 void g() {
2849 for (int i = 0; i < 10; ++i) {}
2850 extern void f(int j = i);
2853 Here, name look up will originally find the out
2854 of scope `i'. We need to issue a warning message,
2855 but then use the global `i'. */
2856 decl = check_for_out_of_scope_variable (decl);
2857 if (local_variable_p (decl))
2859 error ("local variable `%D' may not appear in this context",
2860 decl);
2861 return error_mark_node;
2866 decl = finish_id_expression (id_expression, decl, parser->scope,
2867 idk, qualifying_class,
2868 parser->integral_constant_expression_p,
2869 parser->allow_non_integral_constant_expression_p,
2870 &parser->non_integral_constant_expression_p,
2871 &error_msg);
2872 if (error_msg)
2873 cp_parser_error (parser, error_msg);
2874 return decl;
2877 /* Anything else is an error. */
2878 default:
2879 cp_parser_error (parser, "expected primary-expression");
2880 return error_mark_node;
2884 /* Parse an id-expression.
2886 id-expression:
2887 unqualified-id
2888 qualified-id
2890 qualified-id:
2891 :: [opt] nested-name-specifier template [opt] unqualified-id
2892 :: identifier
2893 :: operator-function-id
2894 :: template-id
2896 Return a representation of the unqualified portion of the
2897 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2898 a `::' or nested-name-specifier.
2900 Often, if the id-expression was a qualified-id, the caller will
2901 want to make a SCOPE_REF to represent the qualified-id. This
2902 function does not do this in order to avoid wastefully creating
2903 SCOPE_REFs when they are not required.
2905 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2906 `template' keyword.
2908 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2909 uninstantiated templates.
2911 If *TEMPLATE_P is non-NULL, it is set to true iff the
2912 `template' keyword is used to explicitly indicate that the entity
2913 named is a template.
2915 If DECLARATOR_P is true, the id-expression is appearing as part of
2916 a declarator, rather than as part of an expression. */
2918 static tree
2919 cp_parser_id_expression (cp_parser *parser,
2920 bool template_keyword_p,
2921 bool check_dependency_p,
2922 bool *template_p,
2923 bool declarator_p)
2925 bool global_scope_p;
2926 bool nested_name_specifier_p;
2928 /* Assume the `template' keyword was not used. */
2929 if (template_p)
2930 *template_p = false;
2932 /* Look for the optional `::' operator. */
2933 global_scope_p
2934 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2935 != NULL_TREE);
2936 /* Look for the optional nested-name-specifier. */
2937 nested_name_specifier_p
2938 = (cp_parser_nested_name_specifier_opt (parser,
2939 /*typename_keyword_p=*/false,
2940 check_dependency_p,
2941 /*type_p=*/false,
2942 declarator_p)
2943 != NULL_TREE);
2944 /* If there is a nested-name-specifier, then we are looking at
2945 the first qualified-id production. */
2946 if (nested_name_specifier_p)
2948 tree saved_scope;
2949 tree saved_object_scope;
2950 tree saved_qualifying_scope;
2951 tree unqualified_id;
2952 bool is_template;
2954 /* See if the next token is the `template' keyword. */
2955 if (!template_p)
2956 template_p = &is_template;
2957 *template_p = cp_parser_optional_template_keyword (parser);
2958 /* Name lookup we do during the processing of the
2959 unqualified-id might obliterate SCOPE. */
2960 saved_scope = parser->scope;
2961 saved_object_scope = parser->object_scope;
2962 saved_qualifying_scope = parser->qualifying_scope;
2963 /* Process the final unqualified-id. */
2964 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2965 check_dependency_p,
2966 declarator_p);
2967 /* Restore the SAVED_SCOPE for our caller. */
2968 parser->scope = saved_scope;
2969 parser->object_scope = saved_object_scope;
2970 parser->qualifying_scope = saved_qualifying_scope;
2972 return unqualified_id;
2974 /* Otherwise, if we are in global scope, then we are looking at one
2975 of the other qualified-id productions. */
2976 else if (global_scope_p)
2978 cp_token *token;
2979 tree id;
2981 /* Peek at the next token. */
2982 token = cp_lexer_peek_token (parser->lexer);
2984 /* If it's an identifier, and the next token is not a "<", then
2985 we can avoid the template-id case. This is an optimization
2986 for this common case. */
2987 if (token->type == CPP_NAME
2988 && !cp_parser_nth_token_starts_template_argument_list_p
2989 (parser, 2))
2990 return cp_parser_identifier (parser);
2992 cp_parser_parse_tentatively (parser);
2993 /* Try a template-id. */
2994 id = cp_parser_template_id (parser,
2995 /*template_keyword_p=*/false,
2996 /*check_dependency_p=*/true,
2997 declarator_p);
2998 /* If that worked, we're done. */
2999 if (cp_parser_parse_definitely (parser))
3000 return id;
3002 /* Peek at the next token. (Changes in the token buffer may
3003 have invalidated the pointer obtained above.) */
3004 token = cp_lexer_peek_token (parser->lexer);
3006 switch (token->type)
3008 case CPP_NAME:
3009 return cp_parser_identifier (parser);
3011 case CPP_KEYWORD:
3012 if (token->keyword == RID_OPERATOR)
3013 return cp_parser_operator_function_id (parser);
3014 /* Fall through. */
3016 default:
3017 cp_parser_error (parser, "expected id-expression");
3018 return error_mark_node;
3021 else
3022 return cp_parser_unqualified_id (parser, template_keyword_p,
3023 /*check_dependency_p=*/true,
3024 declarator_p);
3027 /* Parse an unqualified-id.
3029 unqualified-id:
3030 identifier
3031 operator-function-id
3032 conversion-function-id
3033 ~ class-name
3034 template-id
3036 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3037 keyword, in a construct like `A::template ...'.
3039 Returns a representation of unqualified-id. For the `identifier'
3040 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3041 production a BIT_NOT_EXPR is returned; the operand of the
3042 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3043 other productions, see the documentation accompanying the
3044 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3045 names are looked up in uninstantiated templates. If DECLARATOR_P
3046 is true, the unqualified-id is appearing as part of a declarator,
3047 rather than as part of an expression. */
3049 static tree
3050 cp_parser_unqualified_id (cp_parser* parser,
3051 bool template_keyword_p,
3052 bool check_dependency_p,
3053 bool declarator_p)
3055 cp_token *token;
3057 /* Peek at the next token. */
3058 token = cp_lexer_peek_token (parser->lexer);
3060 switch (token->type)
3062 case CPP_NAME:
3064 tree id;
3066 /* We don't know yet whether or not this will be a
3067 template-id. */
3068 cp_parser_parse_tentatively (parser);
3069 /* Try a template-id. */
3070 id = cp_parser_template_id (parser, template_keyword_p,
3071 check_dependency_p,
3072 declarator_p);
3073 /* If it worked, we're done. */
3074 if (cp_parser_parse_definitely (parser))
3075 return id;
3076 /* Otherwise, it's an ordinary identifier. */
3077 return cp_parser_identifier (parser);
3080 case CPP_TEMPLATE_ID:
3081 return cp_parser_template_id (parser, template_keyword_p,
3082 check_dependency_p,
3083 declarator_p);
3085 case CPP_COMPL:
3087 tree type_decl;
3088 tree qualifying_scope;
3089 tree object_scope;
3090 tree scope;
3092 /* Consume the `~' token. */
3093 cp_lexer_consume_token (parser->lexer);
3094 /* Parse the class-name. The standard, as written, seems to
3095 say that:
3097 template <typename T> struct S { ~S (); };
3098 template <typename T> S<T>::~S() {}
3100 is invalid, since `~' must be followed by a class-name, but
3101 `S<T>' is dependent, and so not known to be a class.
3102 That's not right; we need to look in uninstantiated
3103 templates. A further complication arises from:
3105 template <typename T> void f(T t) {
3106 t.T::~T();
3109 Here, it is not possible to look up `T' in the scope of `T'
3110 itself. We must look in both the current scope, and the
3111 scope of the containing complete expression.
3113 Yet another issue is:
3115 struct S {
3116 int S;
3117 ~S();
3120 S::~S() {}
3122 The standard does not seem to say that the `S' in `~S'
3123 should refer to the type `S' and not the data member
3124 `S::S'. */
3126 /* DR 244 says that we look up the name after the "~" in the
3127 same scope as we looked up the qualifying name. That idea
3128 isn't fully worked out; it's more complicated than that. */
3129 scope = parser->scope;
3130 object_scope = parser->object_scope;
3131 qualifying_scope = parser->qualifying_scope;
3133 /* If the name is of the form "X::~X" it's OK. */
3134 if (scope && TYPE_P (scope)
3135 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3136 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3137 == CPP_OPEN_PAREN)
3138 && (cp_lexer_peek_token (parser->lexer)->value
3139 == TYPE_IDENTIFIER (scope)))
3141 cp_lexer_consume_token (parser->lexer);
3142 return build_nt (BIT_NOT_EXPR, scope);
3145 /* If there was an explicit qualification (S::~T), first look
3146 in the scope given by the qualification (i.e., S). */
3147 if (scope)
3149 cp_parser_parse_tentatively (parser);
3150 type_decl = cp_parser_class_name (parser,
3151 /*typename_keyword_p=*/false,
3152 /*template_keyword_p=*/false,
3153 /*type_p=*/false,
3154 /*check_dependency=*/false,
3155 /*class_head_p=*/false,
3156 declarator_p);
3157 if (cp_parser_parse_definitely (parser))
3158 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3160 /* In "N::S::~S", look in "N" as well. */
3161 if (scope && qualifying_scope)
3163 cp_parser_parse_tentatively (parser);
3164 parser->scope = qualifying_scope;
3165 parser->object_scope = NULL_TREE;
3166 parser->qualifying_scope = NULL_TREE;
3167 type_decl
3168 = cp_parser_class_name (parser,
3169 /*typename_keyword_p=*/false,
3170 /*template_keyword_p=*/false,
3171 /*type_p=*/false,
3172 /*check_dependency=*/false,
3173 /*class_head_p=*/false,
3174 declarator_p);
3175 if (cp_parser_parse_definitely (parser))
3176 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3178 /* In "p->S::~T", look in the scope given by "*p" as well. */
3179 else if (object_scope)
3181 cp_parser_parse_tentatively (parser);
3182 parser->scope = object_scope;
3183 parser->object_scope = NULL_TREE;
3184 parser->qualifying_scope = NULL_TREE;
3185 type_decl
3186 = cp_parser_class_name (parser,
3187 /*typename_keyword_p=*/false,
3188 /*template_keyword_p=*/false,
3189 /*type_p=*/false,
3190 /*check_dependency=*/false,
3191 /*class_head_p=*/false,
3192 declarator_p);
3193 if (cp_parser_parse_definitely (parser))
3194 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3196 /* Look in the surrounding context. */
3197 parser->scope = NULL_TREE;
3198 parser->object_scope = NULL_TREE;
3199 parser->qualifying_scope = NULL_TREE;
3200 type_decl
3201 = cp_parser_class_name (parser,
3202 /*typename_keyword_p=*/false,
3203 /*template_keyword_p=*/false,
3204 /*type_p=*/false,
3205 /*check_dependency=*/false,
3206 /*class_head_p=*/false,
3207 declarator_p);
3208 /* If an error occurred, assume that the name of the
3209 destructor is the same as the name of the qualifying
3210 class. That allows us to keep parsing after running
3211 into ill-formed destructor names. */
3212 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3213 return build_nt (BIT_NOT_EXPR, scope);
3214 else if (type_decl == error_mark_node)
3215 return error_mark_node;
3217 /* [class.dtor]
3219 A typedef-name that names a class shall not be used as the
3220 identifier in the declarator for a destructor declaration. */
3221 if (declarator_p
3222 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3223 && !DECL_SELF_REFERENCE_P (type_decl))
3224 error ("typedef-name `%D' used as destructor declarator",
3225 type_decl);
3227 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3230 case CPP_KEYWORD:
3231 if (token->keyword == RID_OPERATOR)
3233 tree id;
3235 /* This could be a template-id, so we try that first. */
3236 cp_parser_parse_tentatively (parser);
3237 /* Try a template-id. */
3238 id = cp_parser_template_id (parser, template_keyword_p,
3239 /*check_dependency_p=*/true,
3240 declarator_p);
3241 /* If that worked, we're done. */
3242 if (cp_parser_parse_definitely (parser))
3243 return id;
3244 /* We still don't know whether we're looking at an
3245 operator-function-id or a conversion-function-id. */
3246 cp_parser_parse_tentatively (parser);
3247 /* Try an operator-function-id. */
3248 id = cp_parser_operator_function_id (parser);
3249 /* If that didn't work, try a conversion-function-id. */
3250 if (!cp_parser_parse_definitely (parser))
3251 id = cp_parser_conversion_function_id (parser);
3253 return id;
3255 /* Fall through. */
3257 default:
3258 cp_parser_error (parser, "expected unqualified-id");
3259 return error_mark_node;
3263 /* Parse an (optional) nested-name-specifier.
3265 nested-name-specifier:
3266 class-or-namespace-name :: nested-name-specifier [opt]
3267 class-or-namespace-name :: template nested-name-specifier [opt]
3269 PARSER->SCOPE should be set appropriately before this function is
3270 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3271 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3272 in name lookups.
3274 Sets PARSER->SCOPE to the class (TYPE) or namespace
3275 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3276 it unchanged if there is no nested-name-specifier. Returns the new
3277 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3279 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3280 part of a declaration and/or decl-specifier. */
3282 static tree
3283 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3284 bool typename_keyword_p,
3285 bool check_dependency_p,
3286 bool type_p,
3287 bool is_declaration)
3289 bool success = false;
3290 tree access_check = NULL_TREE;
3291 ptrdiff_t start;
3292 cp_token* token;
3294 /* If the next token corresponds to a nested name specifier, there
3295 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3296 false, it may have been true before, in which case something
3297 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3298 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3299 CHECK_DEPENDENCY_P is false, we have to fall through into the
3300 main loop. */
3301 if (check_dependency_p
3302 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3304 cp_parser_pre_parsed_nested_name_specifier (parser);
3305 return parser->scope;
3308 /* Remember where the nested-name-specifier starts. */
3309 if (cp_parser_parsing_tentatively (parser)
3310 && !cp_parser_committed_to_tentative_parse (parser))
3312 token = cp_lexer_peek_token (parser->lexer);
3313 start = cp_lexer_token_difference (parser->lexer,
3314 parser->lexer->buffer,
3315 token);
3317 else
3318 start = -1;
3320 push_deferring_access_checks (dk_deferred);
3322 while (true)
3324 tree new_scope;
3325 tree old_scope;
3326 tree saved_qualifying_scope;
3327 bool template_keyword_p;
3329 /* Spot cases that cannot be the beginning of a
3330 nested-name-specifier. */
3331 token = cp_lexer_peek_token (parser->lexer);
3333 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3334 the already parsed nested-name-specifier. */
3335 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3337 /* Grab the nested-name-specifier and continue the loop. */
3338 cp_parser_pre_parsed_nested_name_specifier (parser);
3339 success = true;
3340 continue;
3343 /* Spot cases that cannot be the beginning of a
3344 nested-name-specifier. On the second and subsequent times
3345 through the loop, we look for the `template' keyword. */
3346 if (success && token->keyword == RID_TEMPLATE)
3348 /* A template-id can start a nested-name-specifier. */
3349 else if (token->type == CPP_TEMPLATE_ID)
3351 else
3353 /* If the next token is not an identifier, then it is
3354 definitely not a class-or-namespace-name. */
3355 if (token->type != CPP_NAME)
3356 break;
3357 /* If the following token is neither a `<' (to begin a
3358 template-id), nor a `::', then we are not looking at a
3359 nested-name-specifier. */
3360 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3361 if (token->type != CPP_SCOPE
3362 && !cp_parser_nth_token_starts_template_argument_list_p
3363 (parser, 2))
3364 break;
3367 /* The nested-name-specifier is optional, so we parse
3368 tentatively. */
3369 cp_parser_parse_tentatively (parser);
3371 /* Look for the optional `template' keyword, if this isn't the
3372 first time through the loop. */
3373 if (success)
3374 template_keyword_p = cp_parser_optional_template_keyword (parser);
3375 else
3376 template_keyword_p = false;
3378 /* Save the old scope since the name lookup we are about to do
3379 might destroy it. */
3380 old_scope = parser->scope;
3381 saved_qualifying_scope = parser->qualifying_scope;
3382 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3383 look up names in "X<T>::I" in order to determine that "Y" is
3384 a template. So, if we have a typename at this point, we make
3385 an effort to look through it. */
3386 if (is_declaration
3387 && !typename_keyword_p
3388 && parser->scope
3389 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3390 parser->scope = resolve_typename_type (parser->scope,
3391 /*only_current_p=*/false);
3392 /* Parse the qualifying entity. */
3393 new_scope
3394 = cp_parser_class_or_namespace_name (parser,
3395 typename_keyword_p,
3396 template_keyword_p,
3397 check_dependency_p,
3398 type_p,
3399 is_declaration);
3400 /* Look for the `::' token. */
3401 cp_parser_require (parser, CPP_SCOPE, "`::'");
3403 /* If we found what we wanted, we keep going; otherwise, we're
3404 done. */
3405 if (!cp_parser_parse_definitely (parser))
3407 bool error_p = false;
3409 /* Restore the OLD_SCOPE since it was valid before the
3410 failed attempt at finding the last
3411 class-or-namespace-name. */
3412 parser->scope = old_scope;
3413 parser->qualifying_scope = saved_qualifying_scope;
3414 /* If the next token is an identifier, and the one after
3415 that is a `::', then any valid interpretation would have
3416 found a class-or-namespace-name. */
3417 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3418 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3419 == CPP_SCOPE)
3420 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3421 != CPP_COMPL))
3423 token = cp_lexer_consume_token (parser->lexer);
3424 if (!error_p)
3426 tree decl;
3428 decl = cp_parser_lookup_name_simple (parser, token->value);
3429 if (TREE_CODE (decl) == TEMPLATE_DECL)
3430 error ("`%D' used without template parameters",
3431 decl);
3432 else
3433 cp_parser_name_lookup_error
3434 (parser, token->value, decl,
3435 "is not a class or namespace");
3436 parser->scope = NULL_TREE;
3437 error_p = true;
3438 /* Treat this as a successful nested-name-specifier
3439 due to:
3441 [basic.lookup.qual]
3443 If the name found is not a class-name (clause
3444 _class_) or namespace-name (_namespace.def_), the
3445 program is ill-formed. */
3446 success = true;
3448 cp_lexer_consume_token (parser->lexer);
3450 break;
3453 /* We've found one valid nested-name-specifier. */
3454 success = true;
3455 /* Make sure we look in the right scope the next time through
3456 the loop. */
3457 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3458 ? TREE_TYPE (new_scope)
3459 : new_scope);
3460 /* If it is a class scope, try to complete it; we are about to
3461 be looking up names inside the class. */
3462 if (TYPE_P (parser->scope)
3463 /* Since checking types for dependency can be expensive,
3464 avoid doing it if the type is already complete. */
3465 && !COMPLETE_TYPE_P (parser->scope)
3466 /* Do not try to complete dependent types. */
3467 && !dependent_type_p (parser->scope))
3468 complete_type (parser->scope);
3471 /* Retrieve any deferred checks. Do not pop this access checks yet
3472 so the memory will not be reclaimed during token replacing below. */
3473 access_check = get_deferred_access_checks ();
3475 /* If parsing tentatively, replace the sequence of tokens that makes
3476 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3477 token. That way, should we re-parse the token stream, we will
3478 not have to repeat the effort required to do the parse, nor will
3479 we issue duplicate error messages. */
3480 if (success && start >= 0)
3482 /* Find the token that corresponds to the start of the
3483 template-id. */
3484 token = cp_lexer_advance_token (parser->lexer,
3485 parser->lexer->buffer,
3486 start);
3488 /* Reset the contents of the START token. */
3489 token->type = CPP_NESTED_NAME_SPECIFIER;
3490 token->value = build_tree_list (access_check, parser->scope);
3491 TREE_TYPE (token->value) = parser->qualifying_scope;
3492 token->keyword = RID_MAX;
3493 /* Purge all subsequent tokens. */
3494 cp_lexer_purge_tokens_after (parser->lexer, token);
3497 pop_deferring_access_checks ();
3498 return success ? parser->scope : NULL_TREE;
3501 /* Parse a nested-name-specifier. See
3502 cp_parser_nested_name_specifier_opt for details. This function
3503 behaves identically, except that it will an issue an error if no
3504 nested-name-specifier is present, and it will return
3505 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3506 is present. */
3508 static tree
3509 cp_parser_nested_name_specifier (cp_parser *parser,
3510 bool typename_keyword_p,
3511 bool check_dependency_p,
3512 bool type_p,
3513 bool is_declaration)
3515 tree scope;
3517 /* Look for the nested-name-specifier. */
3518 scope = cp_parser_nested_name_specifier_opt (parser,
3519 typename_keyword_p,
3520 check_dependency_p,
3521 type_p,
3522 is_declaration);
3523 /* If it was not present, issue an error message. */
3524 if (!scope)
3526 cp_parser_error (parser, "expected nested-name-specifier");
3527 parser->scope = NULL_TREE;
3528 return error_mark_node;
3531 return scope;
3534 /* Parse a class-or-namespace-name.
3536 class-or-namespace-name:
3537 class-name
3538 namespace-name
3540 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3541 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3542 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3543 TYPE_P is TRUE iff the next name should be taken as a class-name,
3544 even the same name is declared to be another entity in the same
3545 scope.
3547 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3548 specified by the class-or-namespace-name. If neither is found the
3549 ERROR_MARK_NODE is returned. */
3551 static tree
3552 cp_parser_class_or_namespace_name (cp_parser *parser,
3553 bool typename_keyword_p,
3554 bool template_keyword_p,
3555 bool check_dependency_p,
3556 bool type_p,
3557 bool is_declaration)
3559 tree saved_scope;
3560 tree saved_qualifying_scope;
3561 tree saved_object_scope;
3562 tree scope;
3563 bool only_class_p;
3565 /* Before we try to parse the class-name, we must save away the
3566 current PARSER->SCOPE since cp_parser_class_name will destroy
3567 it. */
3568 saved_scope = parser->scope;
3569 saved_qualifying_scope = parser->qualifying_scope;
3570 saved_object_scope = parser->object_scope;
3571 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3572 there is no need to look for a namespace-name. */
3573 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3574 if (!only_class_p)
3575 cp_parser_parse_tentatively (parser);
3576 scope = cp_parser_class_name (parser,
3577 typename_keyword_p,
3578 template_keyword_p,
3579 type_p,
3580 check_dependency_p,
3581 /*class_head_p=*/false,
3582 is_declaration);
3583 /* If that didn't work, try for a namespace-name. */
3584 if (!only_class_p && !cp_parser_parse_definitely (parser))
3586 /* Restore the saved scope. */
3587 parser->scope = saved_scope;
3588 parser->qualifying_scope = saved_qualifying_scope;
3589 parser->object_scope = saved_object_scope;
3590 /* If we are not looking at an identifier followed by the scope
3591 resolution operator, then this is not part of a
3592 nested-name-specifier. (Note that this function is only used
3593 to parse the components of a nested-name-specifier.) */
3594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3595 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3596 return error_mark_node;
3597 scope = cp_parser_namespace_name (parser);
3600 return scope;
3603 /* Parse a postfix-expression.
3605 postfix-expression:
3606 primary-expression
3607 postfix-expression [ expression ]
3608 postfix-expression ( expression-list [opt] )
3609 simple-type-specifier ( expression-list [opt] )
3610 typename :: [opt] nested-name-specifier identifier
3611 ( expression-list [opt] )
3612 typename :: [opt] nested-name-specifier template [opt] template-id
3613 ( expression-list [opt] )
3614 postfix-expression . template [opt] id-expression
3615 postfix-expression -> template [opt] id-expression
3616 postfix-expression . pseudo-destructor-name
3617 postfix-expression -> pseudo-destructor-name
3618 postfix-expression ++
3619 postfix-expression --
3620 dynamic_cast < type-id > ( expression )
3621 static_cast < type-id > ( expression )
3622 reinterpret_cast < type-id > ( expression )
3623 const_cast < type-id > ( expression )
3624 typeid ( expression )
3625 typeid ( type-id )
3627 GNU Extension:
3629 postfix-expression:
3630 ( type-id ) { initializer-list , [opt] }
3632 This extension is a GNU version of the C99 compound-literal
3633 construct. (The C99 grammar uses `type-name' instead of `type-id',
3634 but they are essentially the same concept.)
3636 If ADDRESS_P is true, the postfix expression is the operand of the
3637 `&' operator.
3639 Returns a representation of the expression. */
3641 static tree
3642 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3644 cp_token *token;
3645 enum rid keyword;
3646 cp_id_kind idk = CP_ID_KIND_NONE;
3647 tree postfix_expression = NULL_TREE;
3648 /* Non-NULL only if the current postfix-expression can be used to
3649 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3650 class used to qualify the member. */
3651 tree qualifying_class = NULL_TREE;
3653 /* Peek at the next token. */
3654 token = cp_lexer_peek_token (parser->lexer);
3655 /* Some of the productions are determined by keywords. */
3656 keyword = token->keyword;
3657 switch (keyword)
3659 case RID_DYNCAST:
3660 case RID_STATCAST:
3661 case RID_REINTCAST:
3662 case RID_CONSTCAST:
3664 tree type;
3665 tree expression;
3666 const char *saved_message;
3668 /* All of these can be handled in the same way from the point
3669 of view of parsing. Begin by consuming the token
3670 identifying the cast. */
3671 cp_lexer_consume_token (parser->lexer);
3673 /* New types cannot be defined in the cast. */
3674 saved_message = parser->type_definition_forbidden_message;
3675 parser->type_definition_forbidden_message
3676 = "types may not be defined in casts";
3678 /* Look for the opening `<'. */
3679 cp_parser_require (parser, CPP_LESS, "`<'");
3680 /* Parse the type to which we are casting. */
3681 type = cp_parser_type_id (parser);
3682 /* Look for the closing `>'. */
3683 cp_parser_require (parser, CPP_GREATER, "`>'");
3684 /* Restore the old message. */
3685 parser->type_definition_forbidden_message = saved_message;
3687 /* And the expression which is being cast. */
3688 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3689 expression = cp_parser_expression (parser);
3690 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3692 /* Only type conversions to integral or enumeration types
3693 can be used in constant-expressions. */
3694 if (parser->integral_constant_expression_p
3695 && !dependent_type_p (type)
3696 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3697 && (cp_parser_non_integral_constant_expression
3698 (parser,
3699 "a cast to a type other than an integral or "
3700 "enumeration type")))
3701 return error_mark_node;
3703 switch (keyword)
3705 case RID_DYNCAST:
3706 postfix_expression
3707 = build_dynamic_cast (type, expression);
3708 break;
3709 case RID_STATCAST:
3710 postfix_expression
3711 = build_static_cast (type, expression);
3712 break;
3713 case RID_REINTCAST:
3714 postfix_expression
3715 = build_reinterpret_cast (type, expression);
3716 break;
3717 case RID_CONSTCAST:
3718 postfix_expression
3719 = build_const_cast (type, expression);
3720 break;
3721 default:
3722 gcc_unreachable ();
3725 break;
3727 case RID_TYPEID:
3729 tree type;
3730 const char *saved_message;
3731 bool saved_in_type_id_in_expr_p;
3733 /* Consume the `typeid' token. */
3734 cp_lexer_consume_token (parser->lexer);
3735 /* Look for the `(' token. */
3736 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3737 /* Types cannot be defined in a `typeid' expression. */
3738 saved_message = parser->type_definition_forbidden_message;
3739 parser->type_definition_forbidden_message
3740 = "types may not be defined in a `typeid\' expression";
3741 /* We can't be sure yet whether we're looking at a type-id or an
3742 expression. */
3743 cp_parser_parse_tentatively (parser);
3744 /* Try a type-id first. */
3745 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3746 parser->in_type_id_in_expr_p = true;
3747 type = cp_parser_type_id (parser);
3748 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3749 /* Look for the `)' token. Otherwise, we can't be sure that
3750 we're not looking at an expression: consider `typeid (int
3751 (3))', for example. */
3752 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3753 /* If all went well, simply lookup the type-id. */
3754 if (cp_parser_parse_definitely (parser))
3755 postfix_expression = get_typeid (type);
3756 /* Otherwise, fall back to the expression variant. */
3757 else
3759 tree expression;
3761 /* Look for an expression. */
3762 expression = cp_parser_expression (parser);
3763 /* Compute its typeid. */
3764 postfix_expression = build_typeid (expression);
3765 /* Look for the `)' token. */
3766 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3768 /* `typeid' may not appear in an integral constant expression. */
3769 if (cp_parser_non_integral_constant_expression(parser,
3770 "`typeid' operator"))
3771 return error_mark_node;
3772 /* Restore the saved message. */
3773 parser->type_definition_forbidden_message = saved_message;
3775 break;
3777 case RID_TYPENAME:
3779 bool template_p = false;
3780 tree id;
3781 tree type;
3783 /* Consume the `typename' token. */
3784 cp_lexer_consume_token (parser->lexer);
3785 /* Look for the optional `::' operator. */
3786 cp_parser_global_scope_opt (parser,
3787 /*current_scope_valid_p=*/false);
3788 /* Look for the nested-name-specifier. */
3789 cp_parser_nested_name_specifier (parser,
3790 /*typename_keyword_p=*/true,
3791 /*check_dependency_p=*/true,
3792 /*type_p=*/true,
3793 /*is_declaration=*/true);
3794 /* Look for the optional `template' keyword. */
3795 template_p = cp_parser_optional_template_keyword (parser);
3796 /* We don't know whether we're looking at a template-id or an
3797 identifier. */
3798 cp_parser_parse_tentatively (parser);
3799 /* Try a template-id. */
3800 id = cp_parser_template_id (parser, template_p,
3801 /*check_dependency_p=*/true,
3802 /*is_declaration=*/true);
3803 /* If that didn't work, try an identifier. */
3804 if (!cp_parser_parse_definitely (parser))
3805 id = cp_parser_identifier (parser);
3806 /* If we look up a template-id in a non-dependent qualifying
3807 scope, there's no need to create a dependent type. */
3808 if (TREE_CODE (id) == TYPE_DECL
3809 && !dependent_type_p (parser->scope))
3810 type = TREE_TYPE (id);
3811 /* Create a TYPENAME_TYPE to represent the type to which the
3812 functional cast is being performed. */
3813 else
3814 type = make_typename_type (parser->scope, id,
3815 /*complain=*/1);
3817 postfix_expression = cp_parser_functional_cast (parser, type);
3819 break;
3821 default:
3823 tree type;
3825 /* If the next thing is a simple-type-specifier, we may be
3826 looking at a functional cast. We could also be looking at
3827 an id-expression. So, we try the functional cast, and if
3828 that doesn't work we fall back to the primary-expression. */
3829 cp_parser_parse_tentatively (parser);
3830 /* Look for the simple-type-specifier. */
3831 type = cp_parser_simple_type_specifier (parser,
3832 /*decl_specs=*/NULL,
3833 CP_PARSER_FLAGS_NONE);
3834 /* Parse the cast itself. */
3835 if (!cp_parser_error_occurred (parser))
3836 postfix_expression
3837 = cp_parser_functional_cast (parser, type);
3838 /* If that worked, we're done. */
3839 if (cp_parser_parse_definitely (parser))
3840 break;
3842 /* If the functional-cast didn't work out, try a
3843 compound-literal. */
3844 if (cp_parser_allow_gnu_extensions_p (parser)
3845 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3847 tree initializer_list = NULL_TREE;
3848 bool saved_in_type_id_in_expr_p;
3850 cp_parser_parse_tentatively (parser);
3851 /* Consume the `('. */
3852 cp_lexer_consume_token (parser->lexer);
3853 /* Parse the type. */
3854 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3855 parser->in_type_id_in_expr_p = true;
3856 type = cp_parser_type_id (parser);
3857 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3858 /* Look for the `)'. */
3859 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3860 /* Look for the `{'. */
3861 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3862 /* If things aren't going well, there's no need to
3863 keep going. */
3864 if (!cp_parser_error_occurred (parser))
3866 bool non_constant_p;
3867 /* Parse the initializer-list. */
3868 initializer_list
3869 = cp_parser_initializer_list (parser, &non_constant_p);
3870 /* Allow a trailing `,'. */
3871 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3872 cp_lexer_consume_token (parser->lexer);
3873 /* Look for the final `}'. */
3874 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3876 /* If that worked, we're definitely looking at a
3877 compound-literal expression. */
3878 if (cp_parser_parse_definitely (parser))
3880 /* Warn the user that a compound literal is not
3881 allowed in standard C++. */
3882 if (pedantic)
3883 pedwarn ("ISO C++ forbids compound-literals");
3884 /* Form the representation of the compound-literal. */
3885 postfix_expression
3886 = finish_compound_literal (type, initializer_list);
3887 break;
3891 /* It must be a primary-expression. */
3892 postfix_expression = cp_parser_primary_expression (parser,
3893 &idk,
3894 &qualifying_class);
3896 break;
3899 /* If we were avoiding committing to the processing of a
3900 qualified-id until we knew whether or not we had a
3901 pointer-to-member, we now know. */
3902 if (qualifying_class)
3904 bool done;
3906 /* Peek at the next token. */
3907 token = cp_lexer_peek_token (parser->lexer);
3908 done = (token->type != CPP_OPEN_SQUARE
3909 && token->type != CPP_OPEN_PAREN
3910 && token->type != CPP_DOT
3911 && token->type != CPP_DEREF
3912 && token->type != CPP_PLUS_PLUS
3913 && token->type != CPP_MINUS_MINUS);
3915 postfix_expression = finish_qualified_id_expr (qualifying_class,
3916 postfix_expression,
3917 done,
3918 address_p);
3919 if (done)
3920 return postfix_expression;
3923 /* Keep looping until the postfix-expression is complete. */
3924 while (true)
3926 if (idk == CP_ID_KIND_UNQUALIFIED
3927 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3928 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3929 /* It is not a Koenig lookup function call. */
3930 postfix_expression
3931 = unqualified_name_lookup_error (postfix_expression);
3933 /* Peek at the next token. */
3934 token = cp_lexer_peek_token (parser->lexer);
3936 switch (token->type)
3938 case CPP_OPEN_SQUARE:
3939 postfix_expression
3940 = cp_parser_postfix_open_square_expression (parser,
3941 postfix_expression,
3942 false);
3943 idk = CP_ID_KIND_NONE;
3944 break;
3946 case CPP_OPEN_PAREN:
3947 /* postfix-expression ( expression-list [opt] ) */
3949 bool koenig_p;
3950 tree args = (cp_parser_parenthesized_expression_list
3951 (parser, false, /*non_constant_p=*/NULL));
3953 if (args == error_mark_node)
3955 postfix_expression = error_mark_node;
3956 break;
3959 /* Function calls are not permitted in
3960 constant-expressions. */
3961 if (cp_parser_non_integral_constant_expression (parser,
3962 "a function call"))
3964 postfix_expression = error_mark_node;
3965 break;
3968 koenig_p = false;
3969 if (idk == CP_ID_KIND_UNQUALIFIED)
3971 /* We do not perform argument-dependent lookup if
3972 normal lookup finds a non-function, in accordance
3973 with the expected resolution of DR 218. */
3974 if (args
3975 && (is_overloaded_fn (postfix_expression)
3976 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3978 koenig_p = true;
3979 postfix_expression
3980 = perform_koenig_lookup (postfix_expression, args);
3982 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3983 postfix_expression
3984 = unqualified_fn_lookup_error (postfix_expression);
3987 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3989 tree instance = TREE_OPERAND (postfix_expression, 0);
3990 tree fn = TREE_OPERAND (postfix_expression, 1);
3992 if (processing_template_decl
3993 && (type_dependent_expression_p (instance)
3994 || (!BASELINK_P (fn)
3995 && TREE_CODE (fn) != FIELD_DECL)
3996 || type_dependent_expression_p (fn)
3997 || any_type_dependent_arguments_p (args)))
3999 postfix_expression
4000 = build_min_nt (CALL_EXPR, postfix_expression,
4001 args, NULL_TREE);
4002 break;
4005 if (BASELINK_P (fn))
4006 postfix_expression
4007 = (build_new_method_call
4008 (instance, fn, args, NULL_TREE,
4009 (idk == CP_ID_KIND_QUALIFIED
4010 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4011 else
4012 postfix_expression
4013 = finish_call_expr (postfix_expression, args,
4014 /*disallow_virtual=*/false,
4015 /*koenig_p=*/false);
4017 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4018 || TREE_CODE (postfix_expression) == MEMBER_REF
4019 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4020 postfix_expression = (build_offset_ref_call_from_tree
4021 (postfix_expression, args));
4022 else if (idk == CP_ID_KIND_QUALIFIED)
4023 /* A call to a static class member, or a namespace-scope
4024 function. */
4025 postfix_expression
4026 = finish_call_expr (postfix_expression, args,
4027 /*disallow_virtual=*/true,
4028 koenig_p);
4029 else
4030 /* All other function calls. */
4031 postfix_expression
4032 = finish_call_expr (postfix_expression, args,
4033 /*disallow_virtual=*/false,
4034 koenig_p);
4036 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4037 idk = CP_ID_KIND_NONE;
4039 break;
4041 case CPP_DOT:
4042 case CPP_DEREF:
4043 /* postfix-expression . template [opt] id-expression
4044 postfix-expression . pseudo-destructor-name
4045 postfix-expression -> template [opt] id-expression
4046 postfix-expression -> pseudo-destructor-name */
4048 /* Consume the `.' or `->' operator. */
4049 cp_lexer_consume_token (parser->lexer);
4051 postfix_expression
4052 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4053 postfix_expression,
4054 false, &idk);
4055 break;
4057 case CPP_PLUS_PLUS:
4058 /* postfix-expression ++ */
4059 /* Consume the `++' token. */
4060 cp_lexer_consume_token (parser->lexer);
4061 /* Generate a representation for the complete expression. */
4062 postfix_expression
4063 = finish_increment_expr (postfix_expression,
4064 POSTINCREMENT_EXPR);
4065 /* Increments may not appear in constant-expressions. */
4066 if (cp_parser_non_integral_constant_expression (parser,
4067 "an increment"))
4068 postfix_expression = error_mark_node;
4069 idk = CP_ID_KIND_NONE;
4070 break;
4072 case CPP_MINUS_MINUS:
4073 /* postfix-expression -- */
4074 /* Consume the `--' token. */
4075 cp_lexer_consume_token (parser->lexer);
4076 /* Generate a representation for the complete expression. */
4077 postfix_expression
4078 = finish_increment_expr (postfix_expression,
4079 POSTDECREMENT_EXPR);
4080 /* Decrements may not appear in constant-expressions. */
4081 if (cp_parser_non_integral_constant_expression (parser,
4082 "a decrement"))
4083 postfix_expression = error_mark_node;
4084 idk = CP_ID_KIND_NONE;
4085 break;
4087 default:
4088 return postfix_expression;
4092 /* We should never get here. */
4093 gcc_unreachable ();
4094 return error_mark_node;
4097 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4098 by cp_parser_builtin_offsetof. We're looking for
4100 postfix-expression [ expression ]
4102 FOR_OFFSETOF is set if we're being called in that context, which
4103 changes how we deal with integer constant expressions. */
4105 static tree
4106 cp_parser_postfix_open_square_expression (cp_parser *parser,
4107 tree postfix_expression,
4108 bool for_offsetof)
4110 tree index;
4112 /* Consume the `[' token. */
4113 cp_lexer_consume_token (parser->lexer);
4115 /* Parse the index expression. */
4116 /* ??? For offsetof, there is a question of what to allow here. If
4117 offsetof is not being used in an integral constant expression context,
4118 then we *could* get the right answer by computing the value at runtime.
4119 If we are in an integral constant expression context, then we might
4120 could accept any constant expression; hard to say without analysis.
4121 Rather than open the barn door too wide right away, allow only integer
4122 constant expressions here. */
4123 if (for_offsetof)
4124 index = cp_parser_constant_expression (parser, false, NULL);
4125 else
4126 index = cp_parser_expression (parser);
4128 /* Look for the closing `]'. */
4129 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4131 /* Build the ARRAY_REF. */
4132 postfix_expression = grok_array_decl (postfix_expression, index);
4134 /* When not doing offsetof, array references are not permitted in
4135 constant-expressions. */
4136 if (!for_offsetof
4137 && (cp_parser_non_integral_constant_expression
4138 (parser, "an array reference")))
4139 postfix_expression = error_mark_node;
4141 return postfix_expression;
4144 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4145 by cp_parser_builtin_offsetof. We're looking for
4147 postfix-expression . template [opt] id-expression
4148 postfix-expression . pseudo-destructor-name
4149 postfix-expression -> template [opt] id-expression
4150 postfix-expression -> pseudo-destructor-name
4152 FOR_OFFSETOF is set if we're being called in that context. That sorta
4153 limits what of the above we'll actually accept, but nevermind.
4154 TOKEN_TYPE is the "." or "->" token, which will already have been
4155 removed from the stream. */
4157 static tree
4158 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4159 enum cpp_ttype token_type,
4160 tree postfix_expression,
4161 bool for_offsetof, cp_id_kind *idk)
4163 tree name;
4164 bool dependent_p;
4165 bool template_p;
4166 tree scope = NULL_TREE;
4168 /* If this is a `->' operator, dereference the pointer. */
4169 if (token_type == CPP_DEREF)
4170 postfix_expression = build_x_arrow (postfix_expression);
4171 /* Check to see whether or not the expression is type-dependent. */
4172 dependent_p = type_dependent_expression_p (postfix_expression);
4173 /* The identifier following the `->' or `.' is not qualified. */
4174 parser->scope = NULL_TREE;
4175 parser->qualifying_scope = NULL_TREE;
4176 parser->object_scope = NULL_TREE;
4177 *idk = CP_ID_KIND_NONE;
4178 /* Enter the scope corresponding to the type of the object
4179 given by the POSTFIX_EXPRESSION. */
4180 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4182 scope = TREE_TYPE (postfix_expression);
4183 /* According to the standard, no expression should ever have
4184 reference type. Unfortunately, we do not currently match
4185 the standard in this respect in that our internal representation
4186 of an expression may have reference type even when the standard
4187 says it does not. Therefore, we have to manually obtain the
4188 underlying type here. */
4189 scope = non_reference (scope);
4190 /* The type of the POSTFIX_EXPRESSION must be complete. */
4191 scope = complete_type_or_else (scope, NULL_TREE);
4192 /* Let the name lookup machinery know that we are processing a
4193 class member access expression. */
4194 parser->context->object_type = scope;
4195 /* If something went wrong, we want to be able to discern that case,
4196 as opposed to the case where there was no SCOPE due to the type
4197 of expression being dependent. */
4198 if (!scope)
4199 scope = error_mark_node;
4200 /* If the SCOPE was erroneous, make the various semantic analysis
4201 functions exit quickly -- and without issuing additional error
4202 messages. */
4203 if (scope == error_mark_node)
4204 postfix_expression = error_mark_node;
4207 /* If the SCOPE is not a scalar type, we are looking at an
4208 ordinary class member access expression, rather than a
4209 pseudo-destructor-name. */
4210 if (!scope || !SCALAR_TYPE_P (scope))
4212 template_p = cp_parser_optional_template_keyword (parser);
4213 /* Parse the id-expression. */
4214 name = cp_parser_id_expression (parser, template_p,
4215 /*check_dependency_p=*/true,
4216 /*template_p=*/NULL,
4217 /*declarator_p=*/false);
4218 /* In general, build a SCOPE_REF if the member name is qualified.
4219 However, if the name was not dependent and has already been
4220 resolved; there is no need to build the SCOPE_REF. For example;
4222 struct X { void f(); };
4223 template <typename T> void f(T* t) { t->X::f(); }
4225 Even though "t" is dependent, "X::f" is not and has been resolved
4226 to a BASELINK; there is no need to include scope information. */
4228 /* But we do need to remember that there was an explicit scope for
4229 virtual function calls. */
4230 if (parser->scope)
4231 *idk = CP_ID_KIND_QUALIFIED;
4233 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4235 name = build_nt (SCOPE_REF, parser->scope, name);
4236 parser->scope = NULL_TREE;
4237 parser->qualifying_scope = NULL_TREE;
4238 parser->object_scope = NULL_TREE;
4240 if (scope && name && BASELINK_P (name))
4241 adjust_result_of_qualified_name_lookup
4242 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4243 postfix_expression
4244 = finish_class_member_access_expr (postfix_expression, name);
4246 /* Otherwise, try the pseudo-destructor-name production. */
4247 else
4249 tree s = NULL_TREE;
4250 tree type;
4252 /* Parse the pseudo-destructor-name. */
4253 cp_parser_pseudo_destructor_name (parser, &s, &type);
4254 /* Form the call. */
4255 postfix_expression
4256 = finish_pseudo_destructor_expr (postfix_expression,
4257 s, TREE_TYPE (type));
4260 /* We no longer need to look up names in the scope of the object on
4261 the left-hand side of the `.' or `->' operator. */
4262 parser->context->object_type = NULL_TREE;
4264 /* Outside of offsetof, these operators may not appear in
4265 constant-expressions. */
4266 if (!for_offsetof
4267 && (cp_parser_non_integral_constant_expression
4268 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4269 postfix_expression = error_mark_node;
4271 return postfix_expression;
4274 /* Parse a parenthesized expression-list.
4276 expression-list:
4277 assignment-expression
4278 expression-list, assignment-expression
4280 attribute-list:
4281 expression-list
4282 identifier
4283 identifier, expression-list
4285 Returns a TREE_LIST. The TREE_VALUE of each node is a
4286 representation of an assignment-expression. Note that a TREE_LIST
4287 is returned even if there is only a single expression in the list.
4288 error_mark_node is returned if the ( and or ) are
4289 missing. NULL_TREE is returned on no expressions. The parentheses
4290 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4291 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4292 indicates whether or not all of the expressions in the list were
4293 constant. */
4295 static tree
4296 cp_parser_parenthesized_expression_list (cp_parser* parser,
4297 bool is_attribute_list,
4298 bool *non_constant_p)
4300 tree expression_list = NULL_TREE;
4301 tree identifier = NULL_TREE;
4303 /* Assume all the expressions will be constant. */
4304 if (non_constant_p)
4305 *non_constant_p = false;
4307 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4308 return error_mark_node;
4310 /* Consume expressions until there are no more. */
4311 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4312 while (true)
4314 tree expr;
4316 /* At the beginning of attribute lists, check to see if the
4317 next token is an identifier. */
4318 if (is_attribute_list
4319 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4321 cp_token *token;
4323 /* Consume the identifier. */
4324 token = cp_lexer_consume_token (parser->lexer);
4325 /* Save the identifier. */
4326 identifier = token->value;
4328 else
4330 /* Parse the next assignment-expression. */
4331 if (non_constant_p)
4333 bool expr_non_constant_p;
4334 expr = (cp_parser_constant_expression
4335 (parser, /*allow_non_constant_p=*/true,
4336 &expr_non_constant_p));
4337 if (expr_non_constant_p)
4338 *non_constant_p = true;
4340 else
4341 expr = cp_parser_assignment_expression (parser);
4343 /* Add it to the list. We add error_mark_node
4344 expressions to the list, so that we can still tell if
4345 the correct form for a parenthesized expression-list
4346 is found. That gives better errors. */
4347 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4349 if (expr == error_mark_node)
4350 goto skip_comma;
4353 /* After the first item, attribute lists look the same as
4354 expression lists. */
4355 is_attribute_list = false;
4357 get_comma:;
4358 /* If the next token isn't a `,', then we are done. */
4359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4360 break;
4362 /* Otherwise, consume the `,' and keep going. */
4363 cp_lexer_consume_token (parser->lexer);
4366 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4368 int ending;
4370 skip_comma:;
4371 /* We try and resync to an unnested comma, as that will give the
4372 user better diagnostics. */
4373 ending = cp_parser_skip_to_closing_parenthesis (parser,
4374 /*recovering=*/true,
4375 /*or_comma=*/true,
4376 /*consume_paren=*/true);
4377 if (ending < 0)
4378 goto get_comma;
4379 if (!ending)
4380 return error_mark_node;
4383 /* We built up the list in reverse order so we must reverse it now. */
4384 expression_list = nreverse (expression_list);
4385 if (identifier)
4386 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4388 return expression_list;
4391 /* Parse a pseudo-destructor-name.
4393 pseudo-destructor-name:
4394 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4395 :: [opt] nested-name-specifier template template-id :: ~ type-name
4396 :: [opt] nested-name-specifier [opt] ~ type-name
4398 If either of the first two productions is used, sets *SCOPE to the
4399 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4400 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4401 or ERROR_MARK_NODE if the parse fails. */
4403 static void
4404 cp_parser_pseudo_destructor_name (cp_parser* parser,
4405 tree* scope,
4406 tree* type)
4408 bool nested_name_specifier_p;
4410 /* Assume that things will not work out. */
4411 *type = error_mark_node;
4413 /* Look for the optional `::' operator. */
4414 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4415 /* Look for the optional nested-name-specifier. */
4416 nested_name_specifier_p
4417 = (cp_parser_nested_name_specifier_opt (parser,
4418 /*typename_keyword_p=*/false,
4419 /*check_dependency_p=*/true,
4420 /*type_p=*/false,
4421 /*is_declaration=*/true)
4422 != NULL_TREE);
4423 /* Now, if we saw a nested-name-specifier, we might be doing the
4424 second production. */
4425 if (nested_name_specifier_p
4426 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4428 /* Consume the `template' keyword. */
4429 cp_lexer_consume_token (parser->lexer);
4430 /* Parse the template-id. */
4431 cp_parser_template_id (parser,
4432 /*template_keyword_p=*/true,
4433 /*check_dependency_p=*/false,
4434 /*is_declaration=*/true);
4435 /* Look for the `::' token. */
4436 cp_parser_require (parser, CPP_SCOPE, "`::'");
4438 /* If the next token is not a `~', then there might be some
4439 additional qualification. */
4440 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4442 /* Look for the type-name. */
4443 *scope = TREE_TYPE (cp_parser_type_name (parser));
4445 if (*scope == error_mark_node)
4446 return;
4448 /* If we don't have ::~, then something has gone wrong. Since
4449 the only caller of this function is looking for something
4450 after `.' or `->' after a scalar type, most likely the
4451 program is trying to get a member of a non-aggregate
4452 type. */
4453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4454 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4456 cp_parser_error (parser, "request for member of non-aggregate type");
4457 return;
4460 /* Look for the `::' token. */
4461 cp_parser_require (parser, CPP_SCOPE, "`::'");
4463 else
4464 *scope = NULL_TREE;
4466 /* Look for the `~'. */
4467 cp_parser_require (parser, CPP_COMPL, "`~'");
4468 /* Look for the type-name again. We are not responsible for
4469 checking that it matches the first type-name. */
4470 *type = cp_parser_type_name (parser);
4473 /* Parse a unary-expression.
4475 unary-expression:
4476 postfix-expression
4477 ++ cast-expression
4478 -- cast-expression
4479 unary-operator cast-expression
4480 sizeof unary-expression
4481 sizeof ( type-id )
4482 new-expression
4483 delete-expression
4485 GNU Extensions:
4487 unary-expression:
4488 __extension__ cast-expression
4489 __alignof__ unary-expression
4490 __alignof__ ( type-id )
4491 __real__ cast-expression
4492 __imag__ cast-expression
4493 && identifier
4495 ADDRESS_P is true iff the unary-expression is appearing as the
4496 operand of the `&' operator.
4498 Returns a representation of the expression. */
4500 static tree
4501 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4503 cp_token *token;
4504 enum tree_code unary_operator;
4506 /* Peek at the next token. */
4507 token = cp_lexer_peek_token (parser->lexer);
4508 /* Some keywords give away the kind of expression. */
4509 if (token->type == CPP_KEYWORD)
4511 enum rid keyword = token->keyword;
4513 switch (keyword)
4515 case RID_ALIGNOF:
4516 case RID_SIZEOF:
4518 tree operand;
4519 enum tree_code op;
4521 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4522 /* Consume the token. */
4523 cp_lexer_consume_token (parser->lexer);
4524 /* Parse the operand. */
4525 operand = cp_parser_sizeof_operand (parser, keyword);
4527 if (TYPE_P (operand))
4528 return cxx_sizeof_or_alignof_type (operand, op, true);
4529 else
4530 return cxx_sizeof_or_alignof_expr (operand, op);
4533 case RID_NEW:
4534 return cp_parser_new_expression (parser);
4536 case RID_DELETE:
4537 return cp_parser_delete_expression (parser);
4539 case RID_EXTENSION:
4541 /* The saved value of the PEDANTIC flag. */
4542 int saved_pedantic;
4543 tree expr;
4545 /* Save away the PEDANTIC flag. */
4546 cp_parser_extension_opt (parser, &saved_pedantic);
4547 /* Parse the cast-expression. */
4548 expr = cp_parser_simple_cast_expression (parser);
4549 /* Restore the PEDANTIC flag. */
4550 pedantic = saved_pedantic;
4552 return expr;
4555 case RID_REALPART:
4556 case RID_IMAGPART:
4558 tree expression;
4560 /* Consume the `__real__' or `__imag__' token. */
4561 cp_lexer_consume_token (parser->lexer);
4562 /* Parse the cast-expression. */
4563 expression = cp_parser_simple_cast_expression (parser);
4564 /* Create the complete representation. */
4565 return build_x_unary_op ((keyword == RID_REALPART
4566 ? REALPART_EXPR : IMAGPART_EXPR),
4567 expression);
4569 break;
4571 default:
4572 break;
4576 /* Look for the `:: new' and `:: delete', which also signal the
4577 beginning of a new-expression, or delete-expression,
4578 respectively. If the next token is `::', then it might be one of
4579 these. */
4580 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4582 enum rid keyword;
4584 /* See if the token after the `::' is one of the keywords in
4585 which we're interested. */
4586 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4587 /* If it's `new', we have a new-expression. */
4588 if (keyword == RID_NEW)
4589 return cp_parser_new_expression (parser);
4590 /* Similarly, for `delete'. */
4591 else if (keyword == RID_DELETE)
4592 return cp_parser_delete_expression (parser);
4595 /* Look for a unary operator. */
4596 unary_operator = cp_parser_unary_operator (token);
4597 /* The `++' and `--' operators can be handled similarly, even though
4598 they are not technically unary-operators in the grammar. */
4599 if (unary_operator == ERROR_MARK)
4601 if (token->type == CPP_PLUS_PLUS)
4602 unary_operator = PREINCREMENT_EXPR;
4603 else if (token->type == CPP_MINUS_MINUS)
4604 unary_operator = PREDECREMENT_EXPR;
4605 /* Handle the GNU address-of-label extension. */
4606 else if (cp_parser_allow_gnu_extensions_p (parser)
4607 && token->type == CPP_AND_AND)
4609 tree identifier;
4611 /* Consume the '&&' token. */
4612 cp_lexer_consume_token (parser->lexer);
4613 /* Look for the identifier. */
4614 identifier = cp_parser_identifier (parser);
4615 /* Create an expression representing the address. */
4616 return finish_label_address_expr (identifier);
4619 if (unary_operator != ERROR_MARK)
4621 tree cast_expression;
4622 tree expression = error_mark_node;
4623 const char *non_constant_p = NULL;
4625 /* Consume the operator token. */
4626 token = cp_lexer_consume_token (parser->lexer);
4627 /* Parse the cast-expression. */
4628 cast_expression
4629 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4630 /* Now, build an appropriate representation. */
4631 switch (unary_operator)
4633 case INDIRECT_REF:
4634 non_constant_p = "`*'";
4635 expression = build_x_indirect_ref (cast_expression, "unary *");
4636 break;
4638 case ADDR_EXPR:
4639 non_constant_p = "`&'";
4640 /* Fall through. */
4641 case BIT_NOT_EXPR:
4642 expression = build_x_unary_op (unary_operator, cast_expression);
4643 break;
4645 case PREINCREMENT_EXPR:
4646 case PREDECREMENT_EXPR:
4647 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4648 ? "`++'" : "`--'");
4649 /* Fall through. */
4650 case CONVERT_EXPR:
4651 case NEGATE_EXPR:
4652 case TRUTH_NOT_EXPR:
4653 expression = finish_unary_op_expr (unary_operator, cast_expression);
4654 break;
4656 default:
4657 gcc_unreachable ();
4660 if (non_constant_p
4661 && cp_parser_non_integral_constant_expression (parser,
4662 non_constant_p))
4663 expression = error_mark_node;
4665 return expression;
4668 return cp_parser_postfix_expression (parser, address_p);
4671 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4672 unary-operator, the corresponding tree code is returned. */
4674 static enum tree_code
4675 cp_parser_unary_operator (cp_token* token)
4677 switch (token->type)
4679 case CPP_MULT:
4680 return INDIRECT_REF;
4682 case CPP_AND:
4683 return ADDR_EXPR;
4685 case CPP_PLUS:
4686 return CONVERT_EXPR;
4688 case CPP_MINUS:
4689 return NEGATE_EXPR;
4691 case CPP_NOT:
4692 return TRUTH_NOT_EXPR;
4694 case CPP_COMPL:
4695 return BIT_NOT_EXPR;
4697 default:
4698 return ERROR_MARK;
4702 /* Parse a new-expression.
4704 new-expression:
4705 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4706 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4708 Returns a representation of the expression. */
4710 static tree
4711 cp_parser_new_expression (cp_parser* parser)
4713 bool global_scope_p;
4714 tree placement;
4715 tree type;
4716 tree initializer;
4717 tree nelts;
4719 /* Look for the optional `::' operator. */
4720 global_scope_p
4721 = (cp_parser_global_scope_opt (parser,
4722 /*current_scope_valid_p=*/false)
4723 != NULL_TREE);
4724 /* Look for the `new' operator. */
4725 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4726 /* There's no easy way to tell a new-placement from the
4727 `( type-id )' construct. */
4728 cp_parser_parse_tentatively (parser);
4729 /* Look for a new-placement. */
4730 placement = cp_parser_new_placement (parser);
4731 /* If that didn't work out, there's no new-placement. */
4732 if (!cp_parser_parse_definitely (parser))
4733 placement = NULL_TREE;
4735 /* If the next token is a `(', then we have a parenthesized
4736 type-id. */
4737 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4739 /* Consume the `('. */
4740 cp_lexer_consume_token (parser->lexer);
4741 /* Parse the type-id. */
4742 type = cp_parser_type_id (parser);
4743 /* Look for the closing `)'. */
4744 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4745 /* There should not be a direct-new-declarator in this production,
4746 but GCC used to allowed this, so we check and emit a sensible error
4747 message for this case. */
4748 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4750 error ("array bound forbidden after parenthesized type-id");
4751 inform ("try removing the parentheses around the type-id");
4752 cp_parser_direct_new_declarator (parser);
4754 nelts = integer_one_node;
4756 /* Otherwise, there must be a new-type-id. */
4757 else
4758 type = cp_parser_new_type_id (parser, &nelts);
4760 /* If the next token is a `(', then we have a new-initializer. */
4761 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4762 initializer = cp_parser_new_initializer (parser);
4763 else
4764 initializer = NULL_TREE;
4766 /* A new-expression may not appear in an integral constant
4767 expression. */
4768 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4769 return error_mark_node;
4771 /* Create a representation of the new-expression. */
4772 return build_new (placement, type, nelts, initializer, global_scope_p);
4775 /* Parse a new-placement.
4777 new-placement:
4778 ( expression-list )
4780 Returns the same representation as for an expression-list. */
4782 static tree
4783 cp_parser_new_placement (cp_parser* parser)
4785 tree expression_list;
4787 /* Parse the expression-list. */
4788 expression_list = (cp_parser_parenthesized_expression_list
4789 (parser, false, /*non_constant_p=*/NULL));
4791 return expression_list;
4794 /* Parse a new-type-id.
4796 new-type-id:
4797 type-specifier-seq new-declarator [opt]
4799 Returns the TYPE allocated. If the new-type-id indicates an array
4800 type, *NELTS is set to the number of elements in the last array
4801 bound; the TYPE will not include the last array bound. */
4803 static tree
4804 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4806 cp_decl_specifier_seq type_specifier_seq;
4807 cp_declarator *new_declarator;
4808 cp_declarator *declarator;
4809 cp_declarator *outer_declarator;
4810 const char *saved_message;
4811 tree type;
4813 /* The type-specifier sequence must not contain type definitions.
4814 (It cannot contain declarations of new types either, but if they
4815 are not definitions we will catch that because they are not
4816 complete.) */
4817 saved_message = parser->type_definition_forbidden_message;
4818 parser->type_definition_forbidden_message
4819 = "types may not be defined in a new-type-id";
4820 /* Parse the type-specifier-seq. */
4821 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4822 /* Restore the old message. */
4823 parser->type_definition_forbidden_message = saved_message;
4824 /* Parse the new-declarator. */
4825 new_declarator = cp_parser_new_declarator_opt (parser);
4827 /* Determine the number of elements in the last array dimension, if
4828 any. */
4829 *nelts = NULL_TREE;
4830 /* Skip down to the last array dimension. */
4831 declarator = new_declarator;
4832 outer_declarator = NULL;
4833 while (declarator && (declarator->kind == cdk_pointer
4834 || declarator->kind == cdk_ptrmem))
4836 outer_declarator = declarator;
4837 declarator = declarator->declarator;
4839 while (declarator
4840 && declarator->kind == cdk_array
4841 && declarator->declarator
4842 && declarator->declarator->kind == cdk_array)
4844 outer_declarator = declarator;
4845 declarator = declarator->declarator;
4848 if (declarator && declarator->kind == cdk_array)
4850 *nelts = declarator->u.array.bounds;
4851 if (*nelts == error_mark_node)
4852 *nelts = integer_one_node;
4853 else if (!processing_template_decl)
4855 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4856 false))
4857 pedwarn ("size in array new must have integral type");
4858 *nelts = save_expr (cp_convert (sizetype, *nelts));
4859 if (*nelts == integer_zero_node)
4860 warning ("zero size array reserves no space");
4862 if (outer_declarator)
4863 outer_declarator->declarator = declarator->declarator;
4864 else
4865 new_declarator = NULL;
4868 type = groktypename (&type_specifier_seq, new_declarator);
4869 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4871 *nelts = array_type_nelts_top (type);
4872 type = TREE_TYPE (type);
4874 return type;
4877 /* Parse an (optional) new-declarator.
4879 new-declarator:
4880 ptr-operator new-declarator [opt]
4881 direct-new-declarator
4883 Returns the declarator. */
4885 static cp_declarator *
4886 cp_parser_new_declarator_opt (cp_parser* parser)
4888 enum tree_code code;
4889 tree type;
4890 cp_cv_quals cv_quals;
4892 /* We don't know if there's a ptr-operator next, or not. */
4893 cp_parser_parse_tentatively (parser);
4894 /* Look for a ptr-operator. */
4895 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4896 /* If that worked, look for more new-declarators. */
4897 if (cp_parser_parse_definitely (parser))
4899 cp_declarator *declarator;
4901 /* Parse another optional declarator. */
4902 declarator = cp_parser_new_declarator_opt (parser);
4904 /* Create the representation of the declarator. */
4905 if (type)
4906 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4907 else if (code == INDIRECT_REF)
4908 declarator = make_pointer_declarator (cv_quals, declarator);
4909 else
4910 declarator = make_reference_declarator (cv_quals, declarator);
4912 return declarator;
4915 /* If the next token is a `[', there is a direct-new-declarator. */
4916 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4917 return cp_parser_direct_new_declarator (parser);
4919 return NULL;
4922 /* Parse a direct-new-declarator.
4924 direct-new-declarator:
4925 [ expression ]
4926 direct-new-declarator [constant-expression]
4930 static cp_declarator *
4931 cp_parser_direct_new_declarator (cp_parser* parser)
4933 cp_declarator *declarator = NULL;
4935 while (true)
4937 tree expression;
4939 /* Look for the opening `['. */
4940 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4941 /* The first expression is not required to be constant. */
4942 if (!declarator)
4944 expression = cp_parser_expression (parser);
4945 /* The standard requires that the expression have integral
4946 type. DR 74 adds enumeration types. We believe that the
4947 real intent is that these expressions be handled like the
4948 expression in a `switch' condition, which also allows
4949 classes with a single conversion to integral or
4950 enumeration type. */
4951 if (!processing_template_decl)
4953 expression
4954 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4955 expression,
4956 /*complain=*/true);
4957 if (!expression)
4959 error ("expression in new-declarator must have integral or enumeration type");
4960 expression = error_mark_node;
4964 /* But all the other expressions must be. */
4965 else
4966 expression
4967 = cp_parser_constant_expression (parser,
4968 /*allow_non_constant=*/false,
4969 NULL);
4970 /* Look for the closing `]'. */
4971 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4973 /* Add this bound to the declarator. */
4974 declarator = make_array_declarator (declarator, expression);
4976 /* If the next token is not a `[', then there are no more
4977 bounds. */
4978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4979 break;
4982 return declarator;
4985 /* Parse a new-initializer.
4987 new-initializer:
4988 ( expression-list [opt] )
4990 Returns a representation of the expression-list. If there is no
4991 expression-list, VOID_ZERO_NODE is returned. */
4993 static tree
4994 cp_parser_new_initializer (cp_parser* parser)
4996 tree expression_list;
4998 expression_list = (cp_parser_parenthesized_expression_list
4999 (parser, false, /*non_constant_p=*/NULL));
5000 if (!expression_list)
5001 expression_list = void_zero_node;
5003 return expression_list;
5006 /* Parse a delete-expression.
5008 delete-expression:
5009 :: [opt] delete cast-expression
5010 :: [opt] delete [ ] cast-expression
5012 Returns a representation of the expression. */
5014 static tree
5015 cp_parser_delete_expression (cp_parser* parser)
5017 bool global_scope_p;
5018 bool array_p;
5019 tree expression;
5021 /* Look for the optional `::' operator. */
5022 global_scope_p
5023 = (cp_parser_global_scope_opt (parser,
5024 /*current_scope_valid_p=*/false)
5025 != NULL_TREE);
5026 /* Look for the `delete' keyword. */
5027 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5028 /* See if the array syntax is in use. */
5029 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5031 /* Consume the `[' token. */
5032 cp_lexer_consume_token (parser->lexer);
5033 /* Look for the `]' token. */
5034 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5035 /* Remember that this is the `[]' construct. */
5036 array_p = true;
5038 else
5039 array_p = false;
5041 /* Parse the cast-expression. */
5042 expression = cp_parser_simple_cast_expression (parser);
5044 /* A delete-expression may not appear in an integral constant
5045 expression. */
5046 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5047 return error_mark_node;
5049 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5052 /* Parse a cast-expression.
5054 cast-expression:
5055 unary-expression
5056 ( type-id ) cast-expression
5058 Returns a representation of the expression. */
5060 static tree
5061 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5063 /* If it's a `(', then we might be looking at a cast. */
5064 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5066 tree type = NULL_TREE;
5067 tree expr = NULL_TREE;
5068 bool compound_literal_p;
5069 const char *saved_message;
5071 /* There's no way to know yet whether or not this is a cast.
5072 For example, `(int (3))' is a unary-expression, while `(int)
5073 3' is a cast. So, we resort to parsing tentatively. */
5074 cp_parser_parse_tentatively (parser);
5075 /* Types may not be defined in a cast. */
5076 saved_message = parser->type_definition_forbidden_message;
5077 parser->type_definition_forbidden_message
5078 = "types may not be defined in casts";
5079 /* Consume the `('. */
5080 cp_lexer_consume_token (parser->lexer);
5081 /* A very tricky bit is that `(struct S) { 3 }' is a
5082 compound-literal (which we permit in C++ as an extension).
5083 But, that construct is not a cast-expression -- it is a
5084 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5085 is legal; if the compound-literal were a cast-expression,
5086 you'd need an extra set of parentheses.) But, if we parse
5087 the type-id, and it happens to be a class-specifier, then we
5088 will commit to the parse at that point, because we cannot
5089 undo the action that is done when creating a new class. So,
5090 then we cannot back up and do a postfix-expression.
5092 Therefore, we scan ahead to the closing `)', and check to see
5093 if the token after the `)' is a `{'. If so, we are not
5094 looking at a cast-expression.
5096 Save tokens so that we can put them back. */
5097 cp_lexer_save_tokens (parser->lexer);
5098 /* Skip tokens until the next token is a closing parenthesis.
5099 If we find the closing `)', and the next token is a `{', then
5100 we are looking at a compound-literal. */
5101 compound_literal_p
5102 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5103 /*consume_paren=*/true)
5104 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5105 /* Roll back the tokens we skipped. */
5106 cp_lexer_rollback_tokens (parser->lexer);
5107 /* If we were looking at a compound-literal, simulate an error
5108 so that the call to cp_parser_parse_definitely below will
5109 fail. */
5110 if (compound_literal_p)
5111 cp_parser_simulate_error (parser);
5112 else
5114 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5115 parser->in_type_id_in_expr_p = true;
5116 /* Look for the type-id. */
5117 type = cp_parser_type_id (parser);
5118 /* Look for the closing `)'. */
5119 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5120 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5123 /* Restore the saved message. */
5124 parser->type_definition_forbidden_message = saved_message;
5126 /* If ok so far, parse the dependent expression. We cannot be
5127 sure it is a cast. Consider `(T ())'. It is a parenthesized
5128 ctor of T, but looks like a cast to function returning T
5129 without a dependent expression. */
5130 if (!cp_parser_error_occurred (parser))
5131 expr = cp_parser_simple_cast_expression (parser);
5133 if (cp_parser_parse_definitely (parser))
5135 /* Warn about old-style casts, if so requested. */
5136 if (warn_old_style_cast
5137 && !in_system_header
5138 && !VOID_TYPE_P (type)
5139 && current_lang_name != lang_name_c)
5140 warning ("use of old-style cast");
5142 /* Only type conversions to integral or enumeration types
5143 can be used in constant-expressions. */
5144 if (parser->integral_constant_expression_p
5145 && !dependent_type_p (type)
5146 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5147 && (cp_parser_non_integral_constant_expression
5148 (parser,
5149 "a cast to a type other than an integral or "
5150 "enumeration type")))
5151 return error_mark_node;
5153 /* Perform the cast. */
5154 expr = build_c_cast (type, expr);
5155 return expr;
5159 /* If we get here, then it's not a cast, so it must be a
5160 unary-expression. */
5161 return cp_parser_unary_expression (parser, address_p);
5164 /* Parse a pm-expression.
5166 pm-expression:
5167 cast-expression
5168 pm-expression .* cast-expression
5169 pm-expression ->* cast-expression
5171 Returns a representation of the expression. */
5173 static tree
5174 cp_parser_pm_expression (cp_parser* parser)
5176 static const cp_parser_token_tree_map map = {
5177 { CPP_DEREF_STAR, MEMBER_REF },
5178 { CPP_DOT_STAR, DOTSTAR_EXPR },
5179 { CPP_EOF, ERROR_MARK }
5182 return cp_parser_binary_expression (parser, map,
5183 cp_parser_simple_cast_expression);
5186 /* Parse a multiplicative-expression.
5188 multiplicative-expression:
5189 pm-expression
5190 multiplicative-expression * pm-expression
5191 multiplicative-expression / pm-expression
5192 multiplicative-expression % pm-expression
5194 Returns a representation of the expression. */
5196 static tree
5197 cp_parser_multiplicative_expression (cp_parser* parser)
5199 static const cp_parser_token_tree_map map = {
5200 { CPP_MULT, MULT_EXPR },
5201 { CPP_DIV, TRUNC_DIV_EXPR },
5202 { CPP_MOD, TRUNC_MOD_EXPR },
5203 { CPP_EOF, ERROR_MARK }
5206 return cp_parser_binary_expression (parser,
5207 map,
5208 cp_parser_pm_expression);
5211 /* Parse an additive-expression.
5213 additive-expression:
5214 multiplicative-expression
5215 additive-expression + multiplicative-expression
5216 additive-expression - multiplicative-expression
5218 Returns a representation of the expression. */
5220 static tree
5221 cp_parser_additive_expression (cp_parser* parser)
5223 static const cp_parser_token_tree_map map = {
5224 { CPP_PLUS, PLUS_EXPR },
5225 { CPP_MINUS, MINUS_EXPR },
5226 { CPP_EOF, ERROR_MARK }
5229 return cp_parser_binary_expression (parser,
5230 map,
5231 cp_parser_multiplicative_expression);
5234 /* Parse a shift-expression.
5236 shift-expression:
5237 additive-expression
5238 shift-expression << additive-expression
5239 shift-expression >> additive-expression
5241 Returns a representation of the expression. */
5243 static tree
5244 cp_parser_shift_expression (cp_parser* parser)
5246 static const cp_parser_token_tree_map map = {
5247 { CPP_LSHIFT, LSHIFT_EXPR },
5248 { CPP_RSHIFT, RSHIFT_EXPR },
5249 { CPP_EOF, ERROR_MARK }
5252 return cp_parser_binary_expression (parser,
5253 map,
5254 cp_parser_additive_expression);
5257 /* Parse a relational-expression.
5259 relational-expression:
5260 shift-expression
5261 relational-expression < shift-expression
5262 relational-expression > shift-expression
5263 relational-expression <= shift-expression
5264 relational-expression >= shift-expression
5266 GNU Extension:
5268 relational-expression:
5269 relational-expression <? shift-expression
5270 relational-expression >? shift-expression
5272 Returns a representation of the expression. */
5274 static tree
5275 cp_parser_relational_expression (cp_parser* parser)
5277 static const cp_parser_token_tree_map map = {
5278 { CPP_LESS, LT_EXPR },
5279 { CPP_GREATER, GT_EXPR },
5280 { CPP_LESS_EQ, LE_EXPR },
5281 { CPP_GREATER_EQ, GE_EXPR },
5282 { CPP_MIN, MIN_EXPR },
5283 { CPP_MAX, MAX_EXPR },
5284 { CPP_EOF, ERROR_MARK }
5287 return cp_parser_binary_expression (parser,
5288 map,
5289 cp_parser_shift_expression);
5292 /* Parse an equality-expression.
5294 equality-expression:
5295 relational-expression
5296 equality-expression == relational-expression
5297 equality-expression != relational-expression
5299 Returns a representation of the expression. */
5301 static tree
5302 cp_parser_equality_expression (cp_parser* parser)
5304 static const cp_parser_token_tree_map map = {
5305 { CPP_EQ_EQ, EQ_EXPR },
5306 { CPP_NOT_EQ, NE_EXPR },
5307 { CPP_EOF, ERROR_MARK }
5310 return cp_parser_binary_expression (parser,
5311 map,
5312 cp_parser_relational_expression);
5315 /* Parse an and-expression.
5317 and-expression:
5318 equality-expression
5319 and-expression & equality-expression
5321 Returns a representation of the expression. */
5323 static tree
5324 cp_parser_and_expression (cp_parser* parser)
5326 static const cp_parser_token_tree_map map = {
5327 { CPP_AND, BIT_AND_EXPR },
5328 { CPP_EOF, ERROR_MARK }
5331 return cp_parser_binary_expression (parser,
5332 map,
5333 cp_parser_equality_expression);
5336 /* Parse an exclusive-or-expression.
5338 exclusive-or-expression:
5339 and-expression
5340 exclusive-or-expression ^ and-expression
5342 Returns a representation of the expression. */
5344 static tree
5345 cp_parser_exclusive_or_expression (cp_parser* parser)
5347 static const cp_parser_token_tree_map map = {
5348 { CPP_XOR, BIT_XOR_EXPR },
5349 { CPP_EOF, ERROR_MARK }
5352 return cp_parser_binary_expression (parser,
5353 map,
5354 cp_parser_and_expression);
5358 /* Parse an inclusive-or-expression.
5360 inclusive-or-expression:
5361 exclusive-or-expression
5362 inclusive-or-expression | exclusive-or-expression
5364 Returns a representation of the expression. */
5366 static tree
5367 cp_parser_inclusive_or_expression (cp_parser* parser)
5369 static const cp_parser_token_tree_map map = {
5370 { CPP_OR, BIT_IOR_EXPR },
5371 { CPP_EOF, ERROR_MARK }
5374 return cp_parser_binary_expression (parser,
5375 map,
5376 cp_parser_exclusive_or_expression);
5379 /* Parse a logical-and-expression.
5381 logical-and-expression:
5382 inclusive-or-expression
5383 logical-and-expression && inclusive-or-expression
5385 Returns a representation of the expression. */
5387 static tree
5388 cp_parser_logical_and_expression (cp_parser* parser)
5390 static const cp_parser_token_tree_map map = {
5391 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5392 { CPP_EOF, ERROR_MARK }
5395 return cp_parser_binary_expression (parser,
5396 map,
5397 cp_parser_inclusive_or_expression);
5400 /* Parse a logical-or-expression.
5402 logical-or-expression:
5403 logical-and-expression
5404 logical-or-expression || logical-and-expression
5406 Returns a representation of the expression. */
5408 static tree
5409 cp_parser_logical_or_expression (cp_parser* parser)
5411 static const cp_parser_token_tree_map map = {
5412 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5413 { CPP_EOF, ERROR_MARK }
5416 return cp_parser_binary_expression (parser,
5417 map,
5418 cp_parser_logical_and_expression);
5421 /* Parse the `? expression : assignment-expression' part of a
5422 conditional-expression. The LOGICAL_OR_EXPR is the
5423 logical-or-expression that started the conditional-expression.
5424 Returns a representation of the entire conditional-expression.
5426 This routine is used by cp_parser_assignment_expression.
5428 ? expression : assignment-expression
5430 GNU Extensions:
5432 ? : assignment-expression */
5434 static tree
5435 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5437 tree expr;
5438 tree assignment_expr;
5440 /* Consume the `?' token. */
5441 cp_lexer_consume_token (parser->lexer);
5442 if (cp_parser_allow_gnu_extensions_p (parser)
5443 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5444 /* Implicit true clause. */
5445 expr = NULL_TREE;
5446 else
5447 /* Parse the expression. */
5448 expr = cp_parser_expression (parser);
5450 /* The next token should be a `:'. */
5451 cp_parser_require (parser, CPP_COLON, "`:'");
5452 /* Parse the assignment-expression. */
5453 assignment_expr = cp_parser_assignment_expression (parser);
5455 /* Build the conditional-expression. */
5456 return build_x_conditional_expr (logical_or_expr,
5457 expr,
5458 assignment_expr);
5461 /* Parse an assignment-expression.
5463 assignment-expression:
5464 conditional-expression
5465 logical-or-expression assignment-operator assignment_expression
5466 throw-expression
5468 Returns a representation for the expression. */
5470 static tree
5471 cp_parser_assignment_expression (cp_parser* parser)
5473 tree expr;
5475 /* If the next token is the `throw' keyword, then we're looking at
5476 a throw-expression. */
5477 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5478 expr = cp_parser_throw_expression (parser);
5479 /* Otherwise, it must be that we are looking at a
5480 logical-or-expression. */
5481 else
5483 /* Parse the logical-or-expression. */
5484 expr = cp_parser_logical_or_expression (parser);
5485 /* If the next token is a `?' then we're actually looking at a
5486 conditional-expression. */
5487 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5488 return cp_parser_question_colon_clause (parser, expr);
5489 else
5491 enum tree_code assignment_operator;
5493 /* If it's an assignment-operator, we're using the second
5494 production. */
5495 assignment_operator
5496 = cp_parser_assignment_operator_opt (parser);
5497 if (assignment_operator != ERROR_MARK)
5499 tree rhs;
5501 /* Parse the right-hand side of the assignment. */
5502 rhs = cp_parser_assignment_expression (parser);
5503 /* An assignment may not appear in a
5504 constant-expression. */
5505 if (cp_parser_non_integral_constant_expression (parser,
5506 "an assignment"))
5507 return error_mark_node;
5508 /* Build the assignment expression. */
5509 expr = build_x_modify_expr (expr,
5510 assignment_operator,
5511 rhs);
5516 return expr;
5519 /* Parse an (optional) assignment-operator.
5521 assignment-operator: one of
5522 = *= /= %= += -= >>= <<= &= ^= |=
5524 GNU Extension:
5526 assignment-operator: one of
5527 <?= >?=
5529 If the next token is an assignment operator, the corresponding tree
5530 code is returned, and the token is consumed. For example, for
5531 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5532 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5533 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5534 operator, ERROR_MARK is returned. */
5536 static enum tree_code
5537 cp_parser_assignment_operator_opt (cp_parser* parser)
5539 enum tree_code op;
5540 cp_token *token;
5542 /* Peek at the next toen. */
5543 token = cp_lexer_peek_token (parser->lexer);
5545 switch (token->type)
5547 case CPP_EQ:
5548 op = NOP_EXPR;
5549 break;
5551 case CPP_MULT_EQ:
5552 op = MULT_EXPR;
5553 break;
5555 case CPP_DIV_EQ:
5556 op = TRUNC_DIV_EXPR;
5557 break;
5559 case CPP_MOD_EQ:
5560 op = TRUNC_MOD_EXPR;
5561 break;
5563 case CPP_PLUS_EQ:
5564 op = PLUS_EXPR;
5565 break;
5567 case CPP_MINUS_EQ:
5568 op = MINUS_EXPR;
5569 break;
5571 case CPP_RSHIFT_EQ:
5572 op = RSHIFT_EXPR;
5573 break;
5575 case CPP_LSHIFT_EQ:
5576 op = LSHIFT_EXPR;
5577 break;
5579 case CPP_AND_EQ:
5580 op = BIT_AND_EXPR;
5581 break;
5583 case CPP_XOR_EQ:
5584 op = BIT_XOR_EXPR;
5585 break;
5587 case CPP_OR_EQ:
5588 op = BIT_IOR_EXPR;
5589 break;
5591 case CPP_MIN_EQ:
5592 op = MIN_EXPR;
5593 break;
5595 case CPP_MAX_EQ:
5596 op = MAX_EXPR;
5597 break;
5599 default:
5600 /* Nothing else is an assignment operator. */
5601 op = ERROR_MARK;
5604 /* If it was an assignment operator, consume it. */
5605 if (op != ERROR_MARK)
5606 cp_lexer_consume_token (parser->lexer);
5608 return op;
5611 /* Parse an expression.
5613 expression:
5614 assignment-expression
5615 expression , assignment-expression
5617 Returns a representation of the expression. */
5619 static tree
5620 cp_parser_expression (cp_parser* parser)
5622 tree expression = NULL_TREE;
5624 while (true)
5626 tree assignment_expression;
5628 /* Parse the next assignment-expression. */
5629 assignment_expression
5630 = cp_parser_assignment_expression (parser);
5631 /* If this is the first assignment-expression, we can just
5632 save it away. */
5633 if (!expression)
5634 expression = assignment_expression;
5635 else
5636 expression = build_x_compound_expr (expression,
5637 assignment_expression);
5638 /* If the next token is not a comma, then we are done with the
5639 expression. */
5640 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5641 break;
5642 /* Consume the `,'. */
5643 cp_lexer_consume_token (parser->lexer);
5644 /* A comma operator cannot appear in a constant-expression. */
5645 if (cp_parser_non_integral_constant_expression (parser,
5646 "a comma operator"))
5647 expression = error_mark_node;
5650 return expression;
5653 /* Parse a constant-expression.
5655 constant-expression:
5656 conditional-expression
5658 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5659 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5660 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5661 is false, NON_CONSTANT_P should be NULL. */
5663 static tree
5664 cp_parser_constant_expression (cp_parser* parser,
5665 bool allow_non_constant_p,
5666 bool *non_constant_p)
5668 bool saved_integral_constant_expression_p;
5669 bool saved_allow_non_integral_constant_expression_p;
5670 bool saved_non_integral_constant_expression_p;
5671 tree expression;
5673 /* It might seem that we could simply parse the
5674 conditional-expression, and then check to see if it were
5675 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5676 one that the compiler can figure out is constant, possibly after
5677 doing some simplifications or optimizations. The standard has a
5678 precise definition of constant-expression, and we must honor
5679 that, even though it is somewhat more restrictive.
5681 For example:
5683 int i[(2, 3)];
5685 is not a legal declaration, because `(2, 3)' is not a
5686 constant-expression. The `,' operator is forbidden in a
5687 constant-expression. However, GCC's constant-folding machinery
5688 will fold this operation to an INTEGER_CST for `3'. */
5690 /* Save the old settings. */
5691 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5692 saved_allow_non_integral_constant_expression_p
5693 = parser->allow_non_integral_constant_expression_p;
5694 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5695 /* We are now parsing a constant-expression. */
5696 parser->integral_constant_expression_p = true;
5697 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5698 parser->non_integral_constant_expression_p = false;
5699 /* Although the grammar says "conditional-expression", we parse an
5700 "assignment-expression", which also permits "throw-expression"
5701 and the use of assignment operators. In the case that
5702 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5703 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5704 actually essential that we look for an assignment-expression.
5705 For example, cp_parser_initializer_clauses uses this function to
5706 determine whether a particular assignment-expression is in fact
5707 constant. */
5708 expression = cp_parser_assignment_expression (parser);
5709 /* Restore the old settings. */
5710 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5711 parser->allow_non_integral_constant_expression_p
5712 = saved_allow_non_integral_constant_expression_p;
5713 if (allow_non_constant_p)
5714 *non_constant_p = parser->non_integral_constant_expression_p;
5715 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5717 return expression;
5720 /* Parse __builtin_offsetof.
5722 offsetof-expression:
5723 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5725 offsetof-member-designator:
5726 id-expression
5727 | offsetof-member-designator "." id-expression
5728 | offsetof-member-designator "[" expression "]"
5731 static tree
5732 cp_parser_builtin_offsetof (cp_parser *parser)
5734 int save_ice_p, save_non_ice_p;
5735 tree type, expr;
5736 cp_id_kind dummy;
5738 /* We're about to accept non-integral-constant things, but will
5739 definitely yield an integral constant expression. Save and
5740 restore these values around our local parsing. */
5741 save_ice_p = parser->integral_constant_expression_p;
5742 save_non_ice_p = parser->non_integral_constant_expression_p;
5744 /* Consume the "__builtin_offsetof" token. */
5745 cp_lexer_consume_token (parser->lexer);
5746 /* Consume the opening `('. */
5747 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5748 /* Parse the type-id. */
5749 type = cp_parser_type_id (parser);
5750 /* Look for the `,'. */
5751 cp_parser_require (parser, CPP_COMMA, "`,'");
5753 /* Build the (type *)null that begins the traditional offsetof macro. */
5754 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5756 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5757 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5758 true, &dummy);
5759 while (true)
5761 cp_token *token = cp_lexer_peek_token (parser->lexer);
5762 switch (token->type)
5764 case CPP_OPEN_SQUARE:
5765 /* offsetof-member-designator "[" expression "]" */
5766 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5767 break;
5769 case CPP_DOT:
5770 /* offsetof-member-designator "." identifier */
5771 cp_lexer_consume_token (parser->lexer);
5772 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5773 true, &dummy);
5774 break;
5776 case CPP_CLOSE_PAREN:
5777 /* Consume the ")" token. */
5778 cp_lexer_consume_token (parser->lexer);
5779 goto success;
5781 default:
5782 /* Error. We know the following require will fail, but
5783 that gives the proper error message. */
5784 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5785 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5786 expr = error_mark_node;
5787 goto failure;
5791 success:
5792 /* If we're processing a template, we can't finish the semantics yet.
5793 Otherwise we can fold the entire expression now. */
5794 if (processing_template_decl)
5795 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5796 else
5797 expr = fold_offsetof (expr);
5799 failure:
5800 parser->integral_constant_expression_p = save_ice_p;
5801 parser->non_integral_constant_expression_p = save_non_ice_p;
5803 return expr;
5806 /* Statements [gram.stmt.stmt] */
5808 /* Parse a statement.
5810 statement:
5811 labeled-statement
5812 expression-statement
5813 compound-statement
5814 selection-statement
5815 iteration-statement
5816 jump-statement
5817 declaration-statement
5818 try-block */
5820 static void
5821 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5823 tree statement;
5824 cp_token *token;
5825 location_t statement_location;
5827 /* There is no statement yet. */
5828 statement = NULL_TREE;
5829 /* Peek at the next token. */
5830 token = cp_lexer_peek_token (parser->lexer);
5831 /* Remember the location of the first token in the statement. */
5832 statement_location = token->location;
5833 /* If this is a keyword, then that will often determine what kind of
5834 statement we have. */
5835 if (token->type == CPP_KEYWORD)
5837 enum rid keyword = token->keyword;
5839 switch (keyword)
5841 case RID_CASE:
5842 case RID_DEFAULT:
5843 statement = cp_parser_labeled_statement (parser,
5844 in_statement_expr);
5845 break;
5847 case RID_IF:
5848 case RID_SWITCH:
5849 statement = cp_parser_selection_statement (parser);
5850 break;
5852 case RID_WHILE:
5853 case RID_DO:
5854 case RID_FOR:
5855 statement = cp_parser_iteration_statement (parser);
5856 break;
5858 case RID_BREAK:
5859 case RID_CONTINUE:
5860 case RID_RETURN:
5861 case RID_GOTO:
5862 statement = cp_parser_jump_statement (parser);
5863 break;
5865 case RID_TRY:
5866 statement = cp_parser_try_block (parser);
5867 break;
5869 default:
5870 /* It might be a keyword like `int' that can start a
5871 declaration-statement. */
5872 break;
5875 else if (token->type == CPP_NAME)
5877 /* If the next token is a `:', then we are looking at a
5878 labeled-statement. */
5879 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5880 if (token->type == CPP_COLON)
5881 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5883 /* Anything that starts with a `{' must be a compound-statement. */
5884 else if (token->type == CPP_OPEN_BRACE)
5885 statement = cp_parser_compound_statement (parser, NULL, false);
5886 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5887 a statement all its own. */
5888 else if (token->type == CPP_PRAGMA)
5890 cp_lexer_handle_pragma (parser->lexer);
5891 return;
5894 /* Everything else must be a declaration-statement or an
5895 expression-statement. Try for the declaration-statement
5896 first, unless we are looking at a `;', in which case we know that
5897 we have an expression-statement. */
5898 if (!statement)
5900 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5902 cp_parser_parse_tentatively (parser);
5903 /* Try to parse the declaration-statement. */
5904 cp_parser_declaration_statement (parser);
5905 /* If that worked, we're done. */
5906 if (cp_parser_parse_definitely (parser))
5907 return;
5909 /* Look for an expression-statement instead. */
5910 statement = cp_parser_expression_statement (parser, in_statement_expr);
5913 /* Set the line number for the statement. */
5914 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5915 SET_EXPR_LOCATION (statement, statement_location);
5918 /* Parse a labeled-statement.
5920 labeled-statement:
5921 identifier : statement
5922 case constant-expression : statement
5923 default : statement
5925 GNU Extension:
5927 labeled-statement:
5928 case constant-expression ... constant-expression : statement
5930 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5931 For an ordinary label, returns a LABEL_EXPR. */
5933 static tree
5934 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5936 cp_token *token;
5937 tree statement = error_mark_node;
5939 /* The next token should be an identifier. */
5940 token = cp_lexer_peek_token (parser->lexer);
5941 if (token->type != CPP_NAME
5942 && token->type != CPP_KEYWORD)
5944 cp_parser_error (parser, "expected labeled-statement");
5945 return error_mark_node;
5948 switch (token->keyword)
5950 case RID_CASE:
5952 tree expr, expr_hi;
5953 cp_token *ellipsis;
5955 /* Consume the `case' token. */
5956 cp_lexer_consume_token (parser->lexer);
5957 /* Parse the constant-expression. */
5958 expr = cp_parser_constant_expression (parser,
5959 /*allow_non_constant_p=*/false,
5960 NULL);
5962 ellipsis = cp_lexer_peek_token (parser->lexer);
5963 if (ellipsis->type == CPP_ELLIPSIS)
5965 /* Consume the `...' token. */
5966 cp_lexer_consume_token (parser->lexer);
5967 expr_hi =
5968 cp_parser_constant_expression (parser,
5969 /*allow_non_constant_p=*/false,
5970 NULL);
5971 /* We don't need to emit warnings here, as the common code
5972 will do this for us. */
5974 else
5975 expr_hi = NULL_TREE;
5977 if (!parser->in_switch_statement_p)
5978 error ("case label `%E' not within a switch statement", expr);
5979 else
5980 statement = finish_case_label (expr, expr_hi);
5982 break;
5984 case RID_DEFAULT:
5985 /* Consume the `default' token. */
5986 cp_lexer_consume_token (parser->lexer);
5987 if (!parser->in_switch_statement_p)
5988 error ("case label not within a switch statement");
5989 else
5990 statement = finish_case_label (NULL_TREE, NULL_TREE);
5991 break;
5993 default:
5994 /* Anything else must be an ordinary label. */
5995 statement = finish_label_stmt (cp_parser_identifier (parser));
5996 break;
5999 /* Require the `:' token. */
6000 cp_parser_require (parser, CPP_COLON, "`:'");
6001 /* Parse the labeled statement. */
6002 cp_parser_statement (parser, in_statement_expr);
6004 /* Return the label, in the case of a `case' or `default' label. */
6005 return statement;
6008 /* Parse an expression-statement.
6010 expression-statement:
6011 expression [opt] ;
6013 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6014 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6015 indicates whether this expression-statement is part of an
6016 expression statement. */
6018 static tree
6019 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6021 tree statement = NULL_TREE;
6023 /* If the next token is a ';', then there is no expression
6024 statement. */
6025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6026 statement = cp_parser_expression (parser);
6028 /* Consume the final `;'. */
6029 cp_parser_consume_semicolon_at_end_of_statement (parser);
6031 if (in_statement_expr
6032 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6034 /* This is the final expression statement of a statement
6035 expression. */
6036 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6038 else if (statement)
6039 statement = finish_expr_stmt (statement);
6040 else
6041 finish_stmt ();
6043 return statement;
6046 /* Parse a compound-statement.
6048 compound-statement:
6049 { statement-seq [opt] }
6051 Returns a tree representing the statement. */
6053 static tree
6054 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6055 bool in_try)
6057 tree compound_stmt;
6059 /* Consume the `{'. */
6060 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6061 return error_mark_node;
6062 /* Begin the compound-statement. */
6063 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6064 /* Parse an (optional) statement-seq. */
6065 cp_parser_statement_seq_opt (parser, in_statement_expr);
6066 /* Finish the compound-statement. */
6067 finish_compound_stmt (compound_stmt);
6068 /* Consume the `}'. */
6069 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6071 return compound_stmt;
6074 /* Parse an (optional) statement-seq.
6076 statement-seq:
6077 statement
6078 statement-seq [opt] statement */
6080 static void
6081 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6083 /* Scan statements until there aren't any more. */
6084 while (true)
6086 /* If we're looking at a `}', then we've run out of statements. */
6087 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6088 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6089 break;
6091 /* Parse the statement. */
6092 cp_parser_statement (parser, in_statement_expr);
6096 /* Parse a selection-statement.
6098 selection-statement:
6099 if ( condition ) statement
6100 if ( condition ) statement else statement
6101 switch ( condition ) statement
6103 Returns the new IF_STMT or SWITCH_STMT. */
6105 static tree
6106 cp_parser_selection_statement (cp_parser* parser)
6108 cp_token *token;
6109 enum rid keyword;
6111 /* Peek at the next token. */
6112 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6114 /* See what kind of keyword it is. */
6115 keyword = token->keyword;
6116 switch (keyword)
6118 case RID_IF:
6119 case RID_SWITCH:
6121 tree statement;
6122 tree condition;
6124 /* Look for the `('. */
6125 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6127 cp_parser_skip_to_end_of_statement (parser);
6128 return error_mark_node;
6131 /* Begin the selection-statement. */
6132 if (keyword == RID_IF)
6133 statement = begin_if_stmt ();
6134 else
6135 statement = begin_switch_stmt ();
6137 /* Parse the condition. */
6138 condition = cp_parser_condition (parser);
6139 /* Look for the `)'. */
6140 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6141 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6142 /*consume_paren=*/true);
6144 if (keyword == RID_IF)
6146 /* Add the condition. */
6147 finish_if_stmt_cond (condition, statement);
6149 /* Parse the then-clause. */
6150 cp_parser_implicitly_scoped_statement (parser);
6151 finish_then_clause (statement);
6153 /* If the next token is `else', parse the else-clause. */
6154 if (cp_lexer_next_token_is_keyword (parser->lexer,
6155 RID_ELSE))
6157 /* Consume the `else' keyword. */
6158 cp_lexer_consume_token (parser->lexer);
6159 begin_else_clause (statement);
6160 /* Parse the else-clause. */
6161 cp_parser_implicitly_scoped_statement (parser);
6162 finish_else_clause (statement);
6165 /* Now we're all done with the if-statement. */
6166 finish_if_stmt (statement);
6168 else
6170 bool in_switch_statement_p;
6172 /* Add the condition. */
6173 finish_switch_cond (condition, statement);
6175 /* Parse the body of the switch-statement. */
6176 in_switch_statement_p = parser->in_switch_statement_p;
6177 parser->in_switch_statement_p = true;
6178 cp_parser_implicitly_scoped_statement (parser);
6179 parser->in_switch_statement_p = in_switch_statement_p;
6181 /* Now we're all done with the switch-statement. */
6182 finish_switch_stmt (statement);
6185 return statement;
6187 break;
6189 default:
6190 cp_parser_error (parser, "expected selection-statement");
6191 return error_mark_node;
6195 /* Parse a condition.
6197 condition:
6198 expression
6199 type-specifier-seq declarator = assignment-expression
6201 GNU Extension:
6203 condition:
6204 type-specifier-seq declarator asm-specification [opt]
6205 attributes [opt] = assignment-expression
6207 Returns the expression that should be tested. */
6209 static tree
6210 cp_parser_condition (cp_parser* parser)
6212 cp_decl_specifier_seq type_specifiers;
6213 const char *saved_message;
6215 /* Try the declaration first. */
6216 cp_parser_parse_tentatively (parser);
6217 /* New types are not allowed in the type-specifier-seq for a
6218 condition. */
6219 saved_message = parser->type_definition_forbidden_message;
6220 parser->type_definition_forbidden_message
6221 = "types may not be defined in conditions";
6222 /* Parse the type-specifier-seq. */
6223 cp_parser_type_specifier_seq (parser, &type_specifiers);
6224 /* Restore the saved message. */
6225 parser->type_definition_forbidden_message = saved_message;
6226 /* If all is well, we might be looking at a declaration. */
6227 if (!cp_parser_error_occurred (parser))
6229 tree decl;
6230 tree asm_specification;
6231 tree attributes;
6232 cp_declarator *declarator;
6233 tree initializer = NULL_TREE;
6235 /* Parse the declarator. */
6236 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6237 /*ctor_dtor_or_conv_p=*/NULL,
6238 /*parenthesized_p=*/NULL);
6239 /* Parse the attributes. */
6240 attributes = cp_parser_attributes_opt (parser);
6241 /* Parse the asm-specification. */
6242 asm_specification = cp_parser_asm_specification_opt (parser);
6243 /* If the next token is not an `=', then we might still be
6244 looking at an expression. For example:
6246 if (A(a).x)
6248 looks like a decl-specifier-seq and a declarator -- but then
6249 there is no `=', so this is an expression. */
6250 cp_parser_require (parser, CPP_EQ, "`='");
6251 /* If we did see an `=', then we are looking at a declaration
6252 for sure. */
6253 if (cp_parser_parse_definitely (parser))
6255 bool pop_p;
6257 /* Create the declaration. */
6258 decl = start_decl (declarator, &type_specifiers,
6259 /*initialized_p=*/true,
6260 attributes, /*prefix_attributes=*/NULL_TREE,
6261 &pop_p);
6262 /* Parse the assignment-expression. */
6263 initializer = cp_parser_assignment_expression (parser);
6265 /* Process the initializer. */
6266 cp_finish_decl (decl,
6267 initializer,
6268 asm_specification,
6269 LOOKUP_ONLYCONVERTING);
6271 if (pop_p)
6272 pop_scope (DECL_CONTEXT (decl));
6274 return convert_from_reference (decl);
6277 /* If we didn't even get past the declarator successfully, we are
6278 definitely not looking at a declaration. */
6279 else
6280 cp_parser_abort_tentative_parse (parser);
6282 /* Otherwise, we are looking at an expression. */
6283 return cp_parser_expression (parser);
6286 /* Parse an iteration-statement.
6288 iteration-statement:
6289 while ( condition ) statement
6290 do statement while ( expression ) ;
6291 for ( for-init-statement condition [opt] ; expression [opt] )
6292 statement
6294 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6296 static tree
6297 cp_parser_iteration_statement (cp_parser* parser)
6299 cp_token *token;
6300 enum rid keyword;
6301 tree statement;
6302 bool in_iteration_statement_p;
6305 /* Peek at the next token. */
6306 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6307 if (!token)
6308 return error_mark_node;
6310 /* Remember whether or not we are already within an iteration
6311 statement. */
6312 in_iteration_statement_p = parser->in_iteration_statement_p;
6314 /* See what kind of keyword it is. */
6315 keyword = token->keyword;
6316 switch (keyword)
6318 case RID_WHILE:
6320 tree condition;
6322 /* Begin the while-statement. */
6323 statement = begin_while_stmt ();
6324 /* Look for the `('. */
6325 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6326 /* Parse the condition. */
6327 condition = cp_parser_condition (parser);
6328 finish_while_stmt_cond (condition, statement);
6329 /* Look for the `)'. */
6330 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6331 /* Parse the dependent statement. */
6332 parser->in_iteration_statement_p = true;
6333 cp_parser_already_scoped_statement (parser);
6334 parser->in_iteration_statement_p = in_iteration_statement_p;
6335 /* We're done with the while-statement. */
6336 finish_while_stmt (statement);
6338 break;
6340 case RID_DO:
6342 tree expression;
6344 /* Begin the do-statement. */
6345 statement = begin_do_stmt ();
6346 /* Parse the body of the do-statement. */
6347 parser->in_iteration_statement_p = true;
6348 cp_parser_implicitly_scoped_statement (parser);
6349 parser->in_iteration_statement_p = in_iteration_statement_p;
6350 finish_do_body (statement);
6351 /* Look for the `while' keyword. */
6352 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6353 /* Look for the `('. */
6354 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6355 /* Parse the expression. */
6356 expression = cp_parser_expression (parser);
6357 /* We're done with the do-statement. */
6358 finish_do_stmt (expression, statement);
6359 /* Look for the `)'. */
6360 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6361 /* Look for the `;'. */
6362 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6364 break;
6366 case RID_FOR:
6368 tree condition = NULL_TREE;
6369 tree expression = NULL_TREE;
6371 /* Begin the for-statement. */
6372 statement = begin_for_stmt ();
6373 /* Look for the `('. */
6374 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6375 /* Parse the initialization. */
6376 cp_parser_for_init_statement (parser);
6377 finish_for_init_stmt (statement);
6379 /* If there's a condition, process it. */
6380 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6381 condition = cp_parser_condition (parser);
6382 finish_for_cond (condition, statement);
6383 /* Look for the `;'. */
6384 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6386 /* If there's an expression, process it. */
6387 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6388 expression = cp_parser_expression (parser);
6389 finish_for_expr (expression, statement);
6390 /* Look for the `)'. */
6391 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6393 /* Parse the body of the for-statement. */
6394 parser->in_iteration_statement_p = true;
6395 cp_parser_already_scoped_statement (parser);
6396 parser->in_iteration_statement_p = in_iteration_statement_p;
6398 /* We're done with the for-statement. */
6399 finish_for_stmt (statement);
6401 break;
6403 default:
6404 cp_parser_error (parser, "expected iteration-statement");
6405 statement = error_mark_node;
6406 break;
6409 return statement;
6412 /* Parse a for-init-statement.
6414 for-init-statement:
6415 expression-statement
6416 simple-declaration */
6418 static void
6419 cp_parser_for_init_statement (cp_parser* parser)
6421 /* If the next token is a `;', then we have an empty
6422 expression-statement. Grammatically, this is also a
6423 simple-declaration, but an invalid one, because it does not
6424 declare anything. Therefore, if we did not handle this case
6425 specially, we would issue an error message about an invalid
6426 declaration. */
6427 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6429 /* We're going to speculatively look for a declaration, falling back
6430 to an expression, if necessary. */
6431 cp_parser_parse_tentatively (parser);
6432 /* Parse the declaration. */
6433 cp_parser_simple_declaration (parser,
6434 /*function_definition_allowed_p=*/false);
6435 /* If the tentative parse failed, then we shall need to look for an
6436 expression-statement. */
6437 if (cp_parser_parse_definitely (parser))
6438 return;
6441 cp_parser_expression_statement (parser, false);
6444 /* Parse a jump-statement.
6446 jump-statement:
6447 break ;
6448 continue ;
6449 return expression [opt] ;
6450 goto identifier ;
6452 GNU extension:
6454 jump-statement:
6455 goto * expression ;
6457 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6459 static tree
6460 cp_parser_jump_statement (cp_parser* parser)
6462 tree statement = error_mark_node;
6463 cp_token *token;
6464 enum rid keyword;
6466 /* Peek at the next token. */
6467 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6468 if (!token)
6469 return error_mark_node;
6471 /* See what kind of keyword it is. */
6472 keyword = token->keyword;
6473 switch (keyword)
6475 case RID_BREAK:
6476 if (!parser->in_switch_statement_p
6477 && !parser->in_iteration_statement_p)
6479 error ("break statement not within loop or switch");
6480 statement = error_mark_node;
6482 else
6483 statement = finish_break_stmt ();
6484 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6485 break;
6487 case RID_CONTINUE:
6488 if (!parser->in_iteration_statement_p)
6490 error ("continue statement not within a loop");
6491 statement = error_mark_node;
6493 else
6494 statement = finish_continue_stmt ();
6495 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6496 break;
6498 case RID_RETURN:
6500 tree expr;
6502 /* If the next token is a `;', then there is no
6503 expression. */
6504 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6505 expr = cp_parser_expression (parser);
6506 else
6507 expr = NULL_TREE;
6508 /* Build the return-statement. */
6509 statement = finish_return_stmt (expr);
6510 /* Look for the final `;'. */
6511 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6513 break;
6515 case RID_GOTO:
6516 /* Create the goto-statement. */
6517 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6519 /* Issue a warning about this use of a GNU extension. */
6520 if (pedantic)
6521 pedwarn ("ISO C++ forbids computed gotos");
6522 /* Consume the '*' token. */
6523 cp_lexer_consume_token (parser->lexer);
6524 /* Parse the dependent expression. */
6525 finish_goto_stmt (cp_parser_expression (parser));
6527 else
6528 finish_goto_stmt (cp_parser_identifier (parser));
6529 /* Look for the final `;'. */
6530 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6531 break;
6533 default:
6534 cp_parser_error (parser, "expected jump-statement");
6535 break;
6538 return statement;
6541 /* Parse a declaration-statement.
6543 declaration-statement:
6544 block-declaration */
6546 static void
6547 cp_parser_declaration_statement (cp_parser* parser)
6549 void *p;
6551 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6552 p = obstack_alloc (&declarator_obstack, 0);
6554 /* Parse the block-declaration. */
6555 cp_parser_block_declaration (parser, /*statement_p=*/true);
6557 /* Free any declarators allocated. */
6558 obstack_free (&declarator_obstack, p);
6560 /* Finish off the statement. */
6561 finish_stmt ();
6564 /* Some dependent statements (like `if (cond) statement'), are
6565 implicitly in their own scope. In other words, if the statement is
6566 a single statement (as opposed to a compound-statement), it is
6567 none-the-less treated as if it were enclosed in braces. Any
6568 declarations appearing in the dependent statement are out of scope
6569 after control passes that point. This function parses a statement,
6570 but ensures that is in its own scope, even if it is not a
6571 compound-statement.
6573 Returns the new statement. */
6575 static tree
6576 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6578 tree statement;
6580 /* If the token is not a `{', then we must take special action. */
6581 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6583 /* Create a compound-statement. */
6584 statement = begin_compound_stmt (0);
6585 /* Parse the dependent-statement. */
6586 cp_parser_statement (parser, false);
6587 /* Finish the dummy compound-statement. */
6588 finish_compound_stmt (statement);
6590 /* Otherwise, we simply parse the statement directly. */
6591 else
6592 statement = cp_parser_compound_statement (parser, NULL, false);
6594 /* Return the statement. */
6595 return statement;
6598 /* For some dependent statements (like `while (cond) statement'), we
6599 have already created a scope. Therefore, even if the dependent
6600 statement is a compound-statement, we do not want to create another
6601 scope. */
6603 static void
6604 cp_parser_already_scoped_statement (cp_parser* parser)
6606 /* If the token is a `{', then we must take special action. */
6607 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6608 cp_parser_statement (parser, false);
6609 else
6611 /* Avoid calling cp_parser_compound_statement, so that we
6612 don't create a new scope. Do everything else by hand. */
6613 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6614 cp_parser_statement_seq_opt (parser, false);
6615 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6619 /* Declarations [gram.dcl.dcl] */
6621 /* Parse an optional declaration-sequence.
6623 declaration-seq:
6624 declaration
6625 declaration-seq declaration */
6627 static void
6628 cp_parser_declaration_seq_opt (cp_parser* parser)
6630 while (true)
6632 cp_token *token;
6634 token = cp_lexer_peek_token (parser->lexer);
6636 if (token->type == CPP_CLOSE_BRACE
6637 || token->type == CPP_EOF)
6638 break;
6640 if (token->type == CPP_SEMICOLON)
6642 /* A declaration consisting of a single semicolon is
6643 invalid. Allow it unless we're being pedantic. */
6644 if (pedantic && !in_system_header)
6645 pedwarn ("extra `;'");
6646 cp_lexer_consume_token (parser->lexer);
6647 continue;
6650 if (token->type == CPP_PRAGMA)
6652 /* A top-level declaration can consist solely of a #pragma.
6653 A nested declaration cannot, so this is done here and not
6654 in cp_parser_declaration. (A #pragma at block scope is
6655 handled in cp_parser_statement.) */
6656 cp_lexer_handle_pragma (parser->lexer);
6657 continue;
6660 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6661 parser to enter or exit implicit `extern "C"' blocks. */
6662 while (pending_lang_change > 0)
6664 push_lang_context (lang_name_c);
6665 --pending_lang_change;
6667 while (pending_lang_change < 0)
6669 pop_lang_context ();
6670 ++pending_lang_change;
6673 /* Parse the declaration itself. */
6674 cp_parser_declaration (parser);
6678 /* Parse a declaration.
6680 declaration:
6681 block-declaration
6682 function-definition
6683 template-declaration
6684 explicit-instantiation
6685 explicit-specialization
6686 linkage-specification
6687 namespace-definition
6689 GNU extension:
6691 declaration:
6692 __extension__ declaration */
6694 static void
6695 cp_parser_declaration (cp_parser* parser)
6697 cp_token token1;
6698 cp_token token2;
6699 int saved_pedantic;
6700 void *p;
6702 /* Check for the `__extension__' keyword. */
6703 if (cp_parser_extension_opt (parser, &saved_pedantic))
6705 /* Parse the qualified declaration. */
6706 cp_parser_declaration (parser);
6707 /* Restore the PEDANTIC flag. */
6708 pedantic = saved_pedantic;
6710 return;
6713 /* Try to figure out what kind of declaration is present. */
6714 token1 = *cp_lexer_peek_token (parser->lexer);
6716 if (token1.type != CPP_EOF)
6717 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6719 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6720 p = obstack_alloc (&declarator_obstack, 0);
6722 /* If the next token is `extern' and the following token is a string
6723 literal, then we have a linkage specification. */
6724 if (token1.keyword == RID_EXTERN
6725 && cp_parser_is_string_literal (&token2))
6726 cp_parser_linkage_specification (parser);
6727 /* If the next token is `template', then we have either a template
6728 declaration, an explicit instantiation, or an explicit
6729 specialization. */
6730 else if (token1.keyword == RID_TEMPLATE)
6732 /* `template <>' indicates a template specialization. */
6733 if (token2.type == CPP_LESS
6734 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6735 cp_parser_explicit_specialization (parser);
6736 /* `template <' indicates a template declaration. */
6737 else if (token2.type == CPP_LESS)
6738 cp_parser_template_declaration (parser, /*member_p=*/false);
6739 /* Anything else must be an explicit instantiation. */
6740 else
6741 cp_parser_explicit_instantiation (parser);
6743 /* If the next token is `export', then we have a template
6744 declaration. */
6745 else if (token1.keyword == RID_EXPORT)
6746 cp_parser_template_declaration (parser, /*member_p=*/false);
6747 /* If the next token is `extern', 'static' or 'inline' and the one
6748 after that is `template', we have a GNU extended explicit
6749 instantiation directive. */
6750 else if (cp_parser_allow_gnu_extensions_p (parser)
6751 && (token1.keyword == RID_EXTERN
6752 || token1.keyword == RID_STATIC
6753 || token1.keyword == RID_INLINE)
6754 && token2.keyword == RID_TEMPLATE)
6755 cp_parser_explicit_instantiation (parser);
6756 /* If the next token is `namespace', check for a named or unnamed
6757 namespace definition. */
6758 else if (token1.keyword == RID_NAMESPACE
6759 && (/* A named namespace definition. */
6760 (token2.type == CPP_NAME
6761 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6762 == CPP_OPEN_BRACE))
6763 /* An unnamed namespace definition. */
6764 || token2.type == CPP_OPEN_BRACE))
6765 cp_parser_namespace_definition (parser);
6766 /* We must have either a block declaration or a function
6767 definition. */
6768 else
6769 /* Try to parse a block-declaration, or a function-definition. */
6770 cp_parser_block_declaration (parser, /*statement_p=*/false);
6772 /* Free any declarators allocated. */
6773 obstack_free (&declarator_obstack, p);
6776 /* Parse a block-declaration.
6778 block-declaration:
6779 simple-declaration
6780 asm-definition
6781 namespace-alias-definition
6782 using-declaration
6783 using-directive
6785 GNU Extension:
6787 block-declaration:
6788 __extension__ block-declaration
6789 label-declaration
6791 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6792 part of a declaration-statement. */
6794 static void
6795 cp_parser_block_declaration (cp_parser *parser,
6796 bool statement_p)
6798 cp_token *token1;
6799 int saved_pedantic;
6801 /* Check for the `__extension__' keyword. */
6802 if (cp_parser_extension_opt (parser, &saved_pedantic))
6804 /* Parse the qualified declaration. */
6805 cp_parser_block_declaration (parser, statement_p);
6806 /* Restore the PEDANTIC flag. */
6807 pedantic = saved_pedantic;
6809 return;
6812 /* Peek at the next token to figure out which kind of declaration is
6813 present. */
6814 token1 = cp_lexer_peek_token (parser->lexer);
6816 /* If the next keyword is `asm', we have an asm-definition. */
6817 if (token1->keyword == RID_ASM)
6819 if (statement_p)
6820 cp_parser_commit_to_tentative_parse (parser);
6821 cp_parser_asm_definition (parser);
6823 /* If the next keyword is `namespace', we have a
6824 namespace-alias-definition. */
6825 else if (token1->keyword == RID_NAMESPACE)
6826 cp_parser_namespace_alias_definition (parser);
6827 /* If the next keyword is `using', we have either a
6828 using-declaration or a using-directive. */
6829 else if (token1->keyword == RID_USING)
6831 cp_token *token2;
6833 if (statement_p)
6834 cp_parser_commit_to_tentative_parse (parser);
6835 /* If the token after `using' is `namespace', then we have a
6836 using-directive. */
6837 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6838 if (token2->keyword == RID_NAMESPACE)
6839 cp_parser_using_directive (parser);
6840 /* Otherwise, it's a using-declaration. */
6841 else
6842 cp_parser_using_declaration (parser);
6844 /* If the next keyword is `__label__' we have a label declaration. */
6845 else if (token1->keyword == RID_LABEL)
6847 if (statement_p)
6848 cp_parser_commit_to_tentative_parse (parser);
6849 cp_parser_label_declaration (parser);
6851 /* Anything else must be a simple-declaration. */
6852 else
6853 cp_parser_simple_declaration (parser, !statement_p);
6856 /* Parse a simple-declaration.
6858 simple-declaration:
6859 decl-specifier-seq [opt] init-declarator-list [opt] ;
6861 init-declarator-list:
6862 init-declarator
6863 init-declarator-list , init-declarator
6865 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6866 function-definition as a simple-declaration. */
6868 static void
6869 cp_parser_simple_declaration (cp_parser* parser,
6870 bool function_definition_allowed_p)
6872 cp_decl_specifier_seq decl_specifiers;
6873 int declares_class_or_enum;
6874 bool saw_declarator;
6876 /* Defer access checks until we know what is being declared; the
6877 checks for names appearing in the decl-specifier-seq should be
6878 done as if we were in the scope of the thing being declared. */
6879 push_deferring_access_checks (dk_deferred);
6881 /* Parse the decl-specifier-seq. We have to keep track of whether
6882 or not the decl-specifier-seq declares a named class or
6883 enumeration type, since that is the only case in which the
6884 init-declarator-list is allowed to be empty.
6886 [dcl.dcl]
6888 In a simple-declaration, the optional init-declarator-list can be
6889 omitted only when declaring a class or enumeration, that is when
6890 the decl-specifier-seq contains either a class-specifier, an
6891 elaborated-type-specifier, or an enum-specifier. */
6892 cp_parser_decl_specifier_seq (parser,
6893 CP_PARSER_FLAGS_OPTIONAL,
6894 &decl_specifiers,
6895 &declares_class_or_enum);
6896 /* We no longer need to defer access checks. */
6897 stop_deferring_access_checks ();
6899 /* In a block scope, a valid declaration must always have a
6900 decl-specifier-seq. By not trying to parse declarators, we can
6901 resolve the declaration/expression ambiguity more quickly. */
6902 if (!function_definition_allowed_p
6903 && !decl_specifiers.any_specifiers_p)
6905 cp_parser_error (parser, "expected declaration");
6906 goto done;
6909 /* If the next two tokens are both identifiers, the code is
6910 erroneous. The usual cause of this situation is code like:
6912 T t;
6914 where "T" should name a type -- but does not. */
6915 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6917 /* If parsing tentatively, we should commit; we really are
6918 looking at a declaration. */
6919 cp_parser_commit_to_tentative_parse (parser);
6920 /* Give up. */
6921 goto done;
6924 /* If we have seen at least one decl-specifier, and the next token
6925 is not a parenthesis, then we must be looking at a declaration.
6926 (After "int (" we might be looking at a functional cast.) */
6927 if (decl_specifiers.any_specifiers_p
6928 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6929 cp_parser_commit_to_tentative_parse (parser);
6931 /* Keep going until we hit the `;' at the end of the simple
6932 declaration. */
6933 saw_declarator = false;
6934 while (cp_lexer_next_token_is_not (parser->lexer,
6935 CPP_SEMICOLON))
6937 cp_token *token;
6938 bool function_definition_p;
6939 tree decl;
6941 saw_declarator = true;
6942 /* Parse the init-declarator. */
6943 decl = cp_parser_init_declarator (parser, &decl_specifiers,
6944 function_definition_allowed_p,
6945 /*member_p=*/false,
6946 declares_class_or_enum,
6947 &function_definition_p);
6948 /* If an error occurred while parsing tentatively, exit quickly.
6949 (That usually happens when in the body of a function; each
6950 statement is treated as a declaration-statement until proven
6951 otherwise.) */
6952 if (cp_parser_error_occurred (parser))
6953 goto done;
6954 /* Handle function definitions specially. */
6955 if (function_definition_p)
6957 /* If the next token is a `,', then we are probably
6958 processing something like:
6960 void f() {}, *p;
6962 which is erroneous. */
6963 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6964 error ("mixing declarations and function-definitions is forbidden");
6965 /* Otherwise, we're done with the list of declarators. */
6966 else
6968 pop_deferring_access_checks ();
6969 return;
6972 /* The next token should be either a `,' or a `;'. */
6973 token = cp_lexer_peek_token (parser->lexer);
6974 /* If it's a `,', there are more declarators to come. */
6975 if (token->type == CPP_COMMA)
6976 cp_lexer_consume_token (parser->lexer);
6977 /* If it's a `;', we are done. */
6978 else if (token->type == CPP_SEMICOLON)
6979 break;
6980 /* Anything else is an error. */
6981 else
6983 /* If we have already issued an error message we don't need
6984 to issue another one. */
6985 if (decl != error_mark_node
6986 || (cp_parser_parsing_tentatively (parser)
6987 && !cp_parser_committed_to_tentative_parse (parser)))
6988 cp_parser_error (parser, "expected `,' or `;'");
6989 /* Skip tokens until we reach the end of the statement. */
6990 cp_parser_skip_to_end_of_statement (parser);
6991 /* If the next token is now a `;', consume it. */
6992 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6993 cp_lexer_consume_token (parser->lexer);
6994 goto done;
6996 /* After the first time around, a function-definition is not
6997 allowed -- even if it was OK at first. For example:
6999 int i, f() {}
7001 is not valid. */
7002 function_definition_allowed_p = false;
7005 /* Issue an error message if no declarators are present, and the
7006 decl-specifier-seq does not itself declare a class or
7007 enumeration. */
7008 if (!saw_declarator)
7010 if (cp_parser_declares_only_class_p (parser))
7011 shadow_tag (&decl_specifiers);
7012 /* Perform any deferred access checks. */
7013 perform_deferred_access_checks ();
7016 /* Consume the `;'. */
7017 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7019 done:
7020 pop_deferring_access_checks ();
7023 /* Parse a decl-specifier-seq.
7025 decl-specifier-seq:
7026 decl-specifier-seq [opt] decl-specifier
7028 decl-specifier:
7029 storage-class-specifier
7030 type-specifier
7031 function-specifier
7032 friend
7033 typedef
7035 GNU Extension:
7037 decl-specifier:
7038 attributes
7040 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7042 The parser flags FLAGS is used to control type-specifier parsing.
7044 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7045 flags:
7047 1: one of the decl-specifiers is an elaborated-type-specifier
7048 (i.e., a type declaration)
7049 2: one of the decl-specifiers is an enum-specifier or a
7050 class-specifier (i.e., a type definition)
7054 static void
7055 cp_parser_decl_specifier_seq (cp_parser* parser,
7056 cp_parser_flags flags,
7057 cp_decl_specifier_seq *decl_specs,
7058 int* declares_class_or_enum)
7060 bool constructor_possible_p = !parser->in_declarator_p;
7062 /* Clear DECL_SPECS. */
7063 clear_decl_specs (decl_specs);
7065 /* Assume no class or enumeration type is declared. */
7066 *declares_class_or_enum = 0;
7068 /* Keep reading specifiers until there are no more to read. */
7069 while (true)
7071 bool constructor_p;
7072 bool found_decl_spec;
7073 cp_token *token;
7075 /* Peek at the next token. */
7076 token = cp_lexer_peek_token (parser->lexer);
7077 /* Handle attributes. */
7078 if (token->keyword == RID_ATTRIBUTE)
7080 /* Parse the attributes. */
7081 decl_specs->attributes
7082 = chainon (decl_specs->attributes,
7083 cp_parser_attributes_opt (parser));
7084 continue;
7086 /* Assume we will find a decl-specifier keyword. */
7087 found_decl_spec = true;
7088 /* If the next token is an appropriate keyword, we can simply
7089 add it to the list. */
7090 switch (token->keyword)
7092 /* decl-specifier:
7093 friend */
7094 case RID_FRIEND:
7095 if (decl_specs->specs[(int) ds_friend]++)
7096 error ("duplicate `friend'");
7097 /* Consume the token. */
7098 cp_lexer_consume_token (parser->lexer);
7099 break;
7101 /* function-specifier:
7102 inline
7103 virtual
7104 explicit */
7105 case RID_INLINE:
7106 case RID_VIRTUAL:
7107 case RID_EXPLICIT:
7108 cp_parser_function_specifier_opt (parser, decl_specs);
7109 break;
7111 /* decl-specifier:
7112 typedef */
7113 case RID_TYPEDEF:
7114 ++decl_specs->specs[(int) ds_typedef];
7115 /* Consume the token. */
7116 cp_lexer_consume_token (parser->lexer);
7117 /* A constructor declarator cannot appear in a typedef. */
7118 constructor_possible_p = false;
7119 /* The "typedef" keyword can only occur in a declaration; we
7120 may as well commit at this point. */
7121 cp_parser_commit_to_tentative_parse (parser);
7122 break;
7124 /* storage-class-specifier:
7125 auto
7126 register
7127 static
7128 extern
7129 mutable
7131 GNU Extension:
7132 thread */
7133 case RID_AUTO:
7134 /* Consume the token. */
7135 cp_lexer_consume_token (parser->lexer);
7136 cp_parser_set_storage_class (decl_specs, sc_auto);
7137 break;
7138 case RID_REGISTER:
7139 /* Consume the token. */
7140 cp_lexer_consume_token (parser->lexer);
7141 cp_parser_set_storage_class (decl_specs, sc_register);
7142 break;
7143 case RID_STATIC:
7144 /* Consume the token. */
7145 cp_lexer_consume_token (parser->lexer);
7146 if (decl_specs->specs[(int) ds_thread])
7148 error ("`__thread' before `static'");
7149 decl_specs->specs[(int) ds_thread] = 0;
7151 cp_parser_set_storage_class (decl_specs, sc_static);
7152 break;
7153 case RID_EXTERN:
7154 /* Consume the token. */
7155 cp_lexer_consume_token (parser->lexer);
7156 if (decl_specs->specs[(int) ds_thread])
7158 error ("`__thread' before `extern'");
7159 decl_specs->specs[(int) ds_thread] = 0;
7161 cp_parser_set_storage_class (decl_specs, sc_extern);
7162 break;
7163 case RID_MUTABLE:
7164 /* Consume the token. */
7165 cp_lexer_consume_token (parser->lexer);
7166 cp_parser_set_storage_class (decl_specs, sc_mutable);
7167 break;
7168 case RID_THREAD:
7169 /* Consume the token. */
7170 cp_lexer_consume_token (parser->lexer);
7171 ++decl_specs->specs[(int) ds_thread];
7172 break;
7174 default:
7175 /* We did not yet find a decl-specifier yet. */
7176 found_decl_spec = false;
7177 break;
7180 /* Constructors are a special case. The `S' in `S()' is not a
7181 decl-specifier; it is the beginning of the declarator. */
7182 constructor_p
7183 = (!found_decl_spec
7184 && constructor_possible_p
7185 && (cp_parser_constructor_declarator_p
7186 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7188 /* If we don't have a DECL_SPEC yet, then we must be looking at
7189 a type-specifier. */
7190 if (!found_decl_spec && !constructor_p)
7192 int decl_spec_declares_class_or_enum;
7193 bool is_cv_qualifier;
7194 tree type_spec;
7196 type_spec
7197 = cp_parser_type_specifier (parser, flags,
7198 decl_specs,
7199 /*is_declaration=*/true,
7200 &decl_spec_declares_class_or_enum,
7201 &is_cv_qualifier);
7203 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7205 /* If this type-specifier referenced a user-defined type
7206 (a typedef, class-name, etc.), then we can't allow any
7207 more such type-specifiers henceforth.
7209 [dcl.spec]
7211 The longest sequence of decl-specifiers that could
7212 possibly be a type name is taken as the
7213 decl-specifier-seq of a declaration. The sequence shall
7214 be self-consistent as described below.
7216 [dcl.type]
7218 As a general rule, at most one type-specifier is allowed
7219 in the complete decl-specifier-seq of a declaration. The
7220 only exceptions are the following:
7222 -- const or volatile can be combined with any other
7223 type-specifier.
7225 -- signed or unsigned can be combined with char, long,
7226 short, or int.
7228 -- ..
7230 Example:
7232 typedef char* Pc;
7233 void g (const int Pc);
7235 Here, Pc is *not* part of the decl-specifier seq; it's
7236 the declarator. Therefore, once we see a type-specifier
7237 (other than a cv-qualifier), we forbid any additional
7238 user-defined types. We *do* still allow things like `int
7239 int' to be considered a decl-specifier-seq, and issue the
7240 error message later. */
7241 if (type_spec && !is_cv_qualifier)
7242 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7243 /* A constructor declarator cannot follow a type-specifier. */
7244 if (type_spec)
7246 constructor_possible_p = false;
7247 found_decl_spec = true;
7251 /* If we still do not have a DECL_SPEC, then there are no more
7252 decl-specifiers. */
7253 if (!found_decl_spec)
7254 break;
7256 decl_specs->any_specifiers_p = true;
7257 /* After we see one decl-specifier, further decl-specifiers are
7258 always optional. */
7259 flags |= CP_PARSER_FLAGS_OPTIONAL;
7262 /* Don't allow a friend specifier with a class definition. */
7263 if (decl_specs->specs[(int) ds_friend] != 0
7264 && (*declares_class_or_enum & 2))
7265 error ("class definition may not be declared a friend");
7268 /* Parse an (optional) storage-class-specifier.
7270 storage-class-specifier:
7271 auto
7272 register
7273 static
7274 extern
7275 mutable
7277 GNU Extension:
7279 storage-class-specifier:
7280 thread
7282 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7284 static tree
7285 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7287 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7289 case RID_AUTO:
7290 case RID_REGISTER:
7291 case RID_STATIC:
7292 case RID_EXTERN:
7293 case RID_MUTABLE:
7294 case RID_THREAD:
7295 /* Consume the token. */
7296 return cp_lexer_consume_token (parser->lexer)->value;
7298 default:
7299 return NULL_TREE;
7303 /* Parse an (optional) function-specifier.
7305 function-specifier:
7306 inline
7307 virtual
7308 explicit
7310 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7311 Updates DECL_SPECS, if it is non-NULL. */
7313 static tree
7314 cp_parser_function_specifier_opt (cp_parser* parser,
7315 cp_decl_specifier_seq *decl_specs)
7317 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7319 case RID_INLINE:
7320 if (decl_specs)
7321 ++decl_specs->specs[(int) ds_inline];
7322 break;
7324 case RID_VIRTUAL:
7325 if (decl_specs)
7326 ++decl_specs->specs[(int) ds_virtual];
7327 break;
7329 case RID_EXPLICIT:
7330 if (decl_specs)
7331 ++decl_specs->specs[(int) ds_explicit];
7332 break;
7334 default:
7335 return NULL_TREE;
7338 /* Consume the token. */
7339 return cp_lexer_consume_token (parser->lexer)->value;
7342 /* Parse a linkage-specification.
7344 linkage-specification:
7345 extern string-literal { declaration-seq [opt] }
7346 extern string-literal declaration */
7348 static void
7349 cp_parser_linkage_specification (cp_parser* parser)
7351 tree linkage;
7353 /* Look for the `extern' keyword. */
7354 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7356 /* Look for the string-literal. */
7357 linkage = cp_parser_string_literal (parser, false, false);
7359 /* Transform the literal into an identifier. If the literal is a
7360 wide-character string, or contains embedded NULs, then we can't
7361 handle it as the user wants. */
7362 if (strlen (TREE_STRING_POINTER (linkage))
7363 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7365 cp_parser_error (parser, "invalid linkage-specification");
7366 /* Assume C++ linkage. */
7367 linkage = lang_name_cplusplus;
7369 else
7370 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7372 /* We're now using the new linkage. */
7373 push_lang_context (linkage);
7375 /* If the next token is a `{', then we're using the first
7376 production. */
7377 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7379 /* Consume the `{' token. */
7380 cp_lexer_consume_token (parser->lexer);
7381 /* Parse the declarations. */
7382 cp_parser_declaration_seq_opt (parser);
7383 /* Look for the closing `}'. */
7384 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7386 /* Otherwise, there's just one declaration. */
7387 else
7389 bool saved_in_unbraced_linkage_specification_p;
7391 saved_in_unbraced_linkage_specification_p
7392 = parser->in_unbraced_linkage_specification_p;
7393 parser->in_unbraced_linkage_specification_p = true;
7394 have_extern_spec = true;
7395 cp_parser_declaration (parser);
7396 have_extern_spec = false;
7397 parser->in_unbraced_linkage_specification_p
7398 = saved_in_unbraced_linkage_specification_p;
7401 /* We're done with the linkage-specification. */
7402 pop_lang_context ();
7405 /* Special member functions [gram.special] */
7407 /* Parse a conversion-function-id.
7409 conversion-function-id:
7410 operator conversion-type-id
7412 Returns an IDENTIFIER_NODE representing the operator. */
7414 static tree
7415 cp_parser_conversion_function_id (cp_parser* parser)
7417 tree type;
7418 tree saved_scope;
7419 tree saved_qualifying_scope;
7420 tree saved_object_scope;
7421 bool pop_p = false;
7423 /* Look for the `operator' token. */
7424 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7425 return error_mark_node;
7426 /* When we parse the conversion-type-id, the current scope will be
7427 reset. However, we need that information in able to look up the
7428 conversion function later, so we save it here. */
7429 saved_scope = parser->scope;
7430 saved_qualifying_scope = parser->qualifying_scope;
7431 saved_object_scope = parser->object_scope;
7432 /* We must enter the scope of the class so that the names of
7433 entities declared within the class are available in the
7434 conversion-type-id. For example, consider:
7436 struct S {
7437 typedef int I;
7438 operator I();
7441 S::operator I() { ... }
7443 In order to see that `I' is a type-name in the definition, we
7444 must be in the scope of `S'. */
7445 if (saved_scope)
7446 pop_p = push_scope (saved_scope);
7447 /* Parse the conversion-type-id. */
7448 type = cp_parser_conversion_type_id (parser);
7449 /* Leave the scope of the class, if any. */
7450 if (pop_p)
7451 pop_scope (saved_scope);
7452 /* Restore the saved scope. */
7453 parser->scope = saved_scope;
7454 parser->qualifying_scope = saved_qualifying_scope;
7455 parser->object_scope = saved_object_scope;
7456 /* If the TYPE is invalid, indicate failure. */
7457 if (type == error_mark_node)
7458 return error_mark_node;
7459 return mangle_conv_op_name_for_type (type);
7462 /* Parse a conversion-type-id:
7464 conversion-type-id:
7465 type-specifier-seq conversion-declarator [opt]
7467 Returns the TYPE specified. */
7469 static tree
7470 cp_parser_conversion_type_id (cp_parser* parser)
7472 tree attributes;
7473 cp_decl_specifier_seq type_specifiers;
7474 cp_declarator *declarator;
7476 /* Parse the attributes. */
7477 attributes = cp_parser_attributes_opt (parser);
7478 /* Parse the type-specifiers. */
7479 cp_parser_type_specifier_seq (parser, &type_specifiers);
7480 /* If that didn't work, stop. */
7481 if (type_specifiers.type == error_mark_node)
7482 return error_mark_node;
7483 /* Parse the conversion-declarator. */
7484 declarator = cp_parser_conversion_declarator_opt (parser);
7486 return grokdeclarator (declarator, &type_specifiers, TYPENAME,
7487 /*initialized=*/0, &attributes);
7490 /* Parse an (optional) conversion-declarator.
7492 conversion-declarator:
7493 ptr-operator conversion-declarator [opt]
7497 static cp_declarator *
7498 cp_parser_conversion_declarator_opt (cp_parser* parser)
7500 enum tree_code code;
7501 tree class_type;
7502 cp_cv_quals cv_quals;
7504 /* We don't know if there's a ptr-operator next, or not. */
7505 cp_parser_parse_tentatively (parser);
7506 /* Try the ptr-operator. */
7507 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7508 /* If it worked, look for more conversion-declarators. */
7509 if (cp_parser_parse_definitely (parser))
7511 cp_declarator *declarator;
7513 /* Parse another optional declarator. */
7514 declarator = cp_parser_conversion_declarator_opt (parser);
7516 /* Create the representation of the declarator. */
7517 if (class_type)
7518 declarator = make_ptrmem_declarator (cv_quals, class_type,
7519 declarator);
7520 else if (code == INDIRECT_REF)
7521 declarator = make_pointer_declarator (cv_quals, declarator);
7522 else
7523 declarator = make_reference_declarator (cv_quals, declarator);
7525 return declarator;
7528 return NULL;
7531 /* Parse an (optional) ctor-initializer.
7533 ctor-initializer:
7534 : mem-initializer-list
7536 Returns TRUE iff the ctor-initializer was actually present. */
7538 static bool
7539 cp_parser_ctor_initializer_opt (cp_parser* parser)
7541 /* If the next token is not a `:', then there is no
7542 ctor-initializer. */
7543 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7545 /* Do default initialization of any bases and members. */
7546 if (DECL_CONSTRUCTOR_P (current_function_decl))
7547 finish_mem_initializers (NULL_TREE);
7549 return false;
7552 /* Consume the `:' token. */
7553 cp_lexer_consume_token (parser->lexer);
7554 /* And the mem-initializer-list. */
7555 cp_parser_mem_initializer_list (parser);
7557 return true;
7560 /* Parse a mem-initializer-list.
7562 mem-initializer-list:
7563 mem-initializer
7564 mem-initializer , mem-initializer-list */
7566 static void
7567 cp_parser_mem_initializer_list (cp_parser* parser)
7569 tree mem_initializer_list = NULL_TREE;
7571 /* Let the semantic analysis code know that we are starting the
7572 mem-initializer-list. */
7573 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7574 error ("only constructors take base initializers");
7576 /* Loop through the list. */
7577 while (true)
7579 tree mem_initializer;
7581 /* Parse the mem-initializer. */
7582 mem_initializer = cp_parser_mem_initializer (parser);
7583 /* Add it to the list, unless it was erroneous. */
7584 if (mem_initializer)
7586 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7587 mem_initializer_list = mem_initializer;
7589 /* If the next token is not a `,', we're done. */
7590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7591 break;
7592 /* Consume the `,' token. */
7593 cp_lexer_consume_token (parser->lexer);
7596 /* Perform semantic analysis. */
7597 if (DECL_CONSTRUCTOR_P (current_function_decl))
7598 finish_mem_initializers (mem_initializer_list);
7601 /* Parse a mem-initializer.
7603 mem-initializer:
7604 mem-initializer-id ( expression-list [opt] )
7606 GNU extension:
7608 mem-initializer:
7609 ( expression-list [opt] )
7611 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7612 class) or FIELD_DECL (for a non-static data member) to initialize;
7613 the TREE_VALUE is the expression-list. */
7615 static tree
7616 cp_parser_mem_initializer (cp_parser* parser)
7618 tree mem_initializer_id;
7619 tree expression_list;
7620 tree member;
7622 /* Find out what is being initialized. */
7623 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7625 pedwarn ("anachronistic old-style base class initializer");
7626 mem_initializer_id = NULL_TREE;
7628 else
7629 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7630 member = expand_member_init (mem_initializer_id);
7631 if (member && !DECL_P (member))
7632 in_base_initializer = 1;
7634 expression_list
7635 = cp_parser_parenthesized_expression_list (parser, false,
7636 /*non_constant_p=*/NULL);
7637 if (!expression_list)
7638 expression_list = void_type_node;
7640 in_base_initializer = 0;
7642 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7645 /* Parse a mem-initializer-id.
7647 mem-initializer-id:
7648 :: [opt] nested-name-specifier [opt] class-name
7649 identifier
7651 Returns a TYPE indicating the class to be initializer for the first
7652 production. Returns an IDENTIFIER_NODE indicating the data member
7653 to be initialized for the second production. */
7655 static tree
7656 cp_parser_mem_initializer_id (cp_parser* parser)
7658 bool global_scope_p;
7659 bool nested_name_specifier_p;
7660 bool template_p = false;
7661 tree id;
7663 /* `typename' is not allowed in this context ([temp.res]). */
7664 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7666 error ("keyword `typename' not allowed in this context (a qualified "
7667 "member initializer is implicitly a type)");
7668 cp_lexer_consume_token (parser->lexer);
7670 /* Look for the optional `::' operator. */
7671 global_scope_p
7672 = (cp_parser_global_scope_opt (parser,
7673 /*current_scope_valid_p=*/false)
7674 != NULL_TREE);
7675 /* Look for the optional nested-name-specifier. The simplest way to
7676 implement:
7678 [temp.res]
7680 The keyword `typename' is not permitted in a base-specifier or
7681 mem-initializer; in these contexts a qualified name that
7682 depends on a template-parameter is implicitly assumed to be a
7683 type name.
7685 is to assume that we have seen the `typename' keyword at this
7686 point. */
7687 nested_name_specifier_p
7688 = (cp_parser_nested_name_specifier_opt (parser,
7689 /*typename_keyword_p=*/true,
7690 /*check_dependency_p=*/true,
7691 /*type_p=*/true,
7692 /*is_declaration=*/true)
7693 != NULL_TREE);
7694 if (nested_name_specifier_p)
7695 template_p = cp_parser_optional_template_keyword (parser);
7696 /* If there is a `::' operator or a nested-name-specifier, then we
7697 are definitely looking for a class-name. */
7698 if (global_scope_p || nested_name_specifier_p)
7699 return cp_parser_class_name (parser,
7700 /*typename_keyword_p=*/true,
7701 /*template_keyword_p=*/template_p,
7702 /*type_p=*/false,
7703 /*check_dependency_p=*/true,
7704 /*class_head_p=*/false,
7705 /*is_declaration=*/true);
7706 /* Otherwise, we could also be looking for an ordinary identifier. */
7707 cp_parser_parse_tentatively (parser);
7708 /* Try a class-name. */
7709 id = cp_parser_class_name (parser,
7710 /*typename_keyword_p=*/true,
7711 /*template_keyword_p=*/false,
7712 /*type_p=*/false,
7713 /*check_dependency_p=*/true,
7714 /*class_head_p=*/false,
7715 /*is_declaration=*/true);
7716 /* If we found one, we're done. */
7717 if (cp_parser_parse_definitely (parser))
7718 return id;
7719 /* Otherwise, look for an ordinary identifier. */
7720 return cp_parser_identifier (parser);
7723 /* Overloading [gram.over] */
7725 /* Parse an operator-function-id.
7727 operator-function-id:
7728 operator operator
7730 Returns an IDENTIFIER_NODE for the operator which is a
7731 human-readable spelling of the identifier, e.g., `operator +'. */
7733 static tree
7734 cp_parser_operator_function_id (cp_parser* parser)
7736 /* Look for the `operator' keyword. */
7737 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7738 return error_mark_node;
7739 /* And then the name of the operator itself. */
7740 return cp_parser_operator (parser);
7743 /* Parse an operator.
7745 operator:
7746 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7747 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7748 || ++ -- , ->* -> () []
7750 GNU Extensions:
7752 operator:
7753 <? >? <?= >?=
7755 Returns an IDENTIFIER_NODE for the operator which is a
7756 human-readable spelling of the identifier, e.g., `operator +'. */
7758 static tree
7759 cp_parser_operator (cp_parser* parser)
7761 tree id = NULL_TREE;
7762 cp_token *token;
7764 /* Peek at the next token. */
7765 token = cp_lexer_peek_token (parser->lexer);
7766 /* Figure out which operator we have. */
7767 switch (token->type)
7769 case CPP_KEYWORD:
7771 enum tree_code op;
7773 /* The keyword should be either `new' or `delete'. */
7774 if (token->keyword == RID_NEW)
7775 op = NEW_EXPR;
7776 else if (token->keyword == RID_DELETE)
7777 op = DELETE_EXPR;
7778 else
7779 break;
7781 /* Consume the `new' or `delete' token. */
7782 cp_lexer_consume_token (parser->lexer);
7784 /* Peek at the next token. */
7785 token = cp_lexer_peek_token (parser->lexer);
7786 /* If it's a `[' token then this is the array variant of the
7787 operator. */
7788 if (token->type == CPP_OPEN_SQUARE)
7790 /* Consume the `[' token. */
7791 cp_lexer_consume_token (parser->lexer);
7792 /* Look for the `]' token. */
7793 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7794 id = ansi_opname (op == NEW_EXPR
7795 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7797 /* Otherwise, we have the non-array variant. */
7798 else
7799 id = ansi_opname (op);
7801 return id;
7804 case CPP_PLUS:
7805 id = ansi_opname (PLUS_EXPR);
7806 break;
7808 case CPP_MINUS:
7809 id = ansi_opname (MINUS_EXPR);
7810 break;
7812 case CPP_MULT:
7813 id = ansi_opname (MULT_EXPR);
7814 break;
7816 case CPP_DIV:
7817 id = ansi_opname (TRUNC_DIV_EXPR);
7818 break;
7820 case CPP_MOD:
7821 id = ansi_opname (TRUNC_MOD_EXPR);
7822 break;
7824 case CPP_XOR:
7825 id = ansi_opname (BIT_XOR_EXPR);
7826 break;
7828 case CPP_AND:
7829 id = ansi_opname (BIT_AND_EXPR);
7830 break;
7832 case CPP_OR:
7833 id = ansi_opname (BIT_IOR_EXPR);
7834 break;
7836 case CPP_COMPL:
7837 id = ansi_opname (BIT_NOT_EXPR);
7838 break;
7840 case CPP_NOT:
7841 id = ansi_opname (TRUTH_NOT_EXPR);
7842 break;
7844 case CPP_EQ:
7845 id = ansi_assopname (NOP_EXPR);
7846 break;
7848 case CPP_LESS:
7849 id = ansi_opname (LT_EXPR);
7850 break;
7852 case CPP_GREATER:
7853 id = ansi_opname (GT_EXPR);
7854 break;
7856 case CPP_PLUS_EQ:
7857 id = ansi_assopname (PLUS_EXPR);
7858 break;
7860 case CPP_MINUS_EQ:
7861 id = ansi_assopname (MINUS_EXPR);
7862 break;
7864 case CPP_MULT_EQ:
7865 id = ansi_assopname (MULT_EXPR);
7866 break;
7868 case CPP_DIV_EQ:
7869 id = ansi_assopname (TRUNC_DIV_EXPR);
7870 break;
7872 case CPP_MOD_EQ:
7873 id = ansi_assopname (TRUNC_MOD_EXPR);
7874 break;
7876 case CPP_XOR_EQ:
7877 id = ansi_assopname (BIT_XOR_EXPR);
7878 break;
7880 case CPP_AND_EQ:
7881 id = ansi_assopname (BIT_AND_EXPR);
7882 break;
7884 case CPP_OR_EQ:
7885 id = ansi_assopname (BIT_IOR_EXPR);
7886 break;
7888 case CPP_LSHIFT:
7889 id = ansi_opname (LSHIFT_EXPR);
7890 break;
7892 case CPP_RSHIFT:
7893 id = ansi_opname (RSHIFT_EXPR);
7894 break;
7896 case CPP_LSHIFT_EQ:
7897 id = ansi_assopname (LSHIFT_EXPR);
7898 break;
7900 case CPP_RSHIFT_EQ:
7901 id = ansi_assopname (RSHIFT_EXPR);
7902 break;
7904 case CPP_EQ_EQ:
7905 id = ansi_opname (EQ_EXPR);
7906 break;
7908 case CPP_NOT_EQ:
7909 id = ansi_opname (NE_EXPR);
7910 break;
7912 case CPP_LESS_EQ:
7913 id = ansi_opname (LE_EXPR);
7914 break;
7916 case CPP_GREATER_EQ:
7917 id = ansi_opname (GE_EXPR);
7918 break;
7920 case CPP_AND_AND:
7921 id = ansi_opname (TRUTH_ANDIF_EXPR);
7922 break;
7924 case CPP_OR_OR:
7925 id = ansi_opname (TRUTH_ORIF_EXPR);
7926 break;
7928 case CPP_PLUS_PLUS:
7929 id = ansi_opname (POSTINCREMENT_EXPR);
7930 break;
7932 case CPP_MINUS_MINUS:
7933 id = ansi_opname (PREDECREMENT_EXPR);
7934 break;
7936 case CPP_COMMA:
7937 id = ansi_opname (COMPOUND_EXPR);
7938 break;
7940 case CPP_DEREF_STAR:
7941 id = ansi_opname (MEMBER_REF);
7942 break;
7944 case CPP_DEREF:
7945 id = ansi_opname (COMPONENT_REF);
7946 break;
7948 case CPP_OPEN_PAREN:
7949 /* Consume the `('. */
7950 cp_lexer_consume_token (parser->lexer);
7951 /* Look for the matching `)'. */
7952 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7953 return ansi_opname (CALL_EXPR);
7955 case CPP_OPEN_SQUARE:
7956 /* Consume the `['. */
7957 cp_lexer_consume_token (parser->lexer);
7958 /* Look for the matching `]'. */
7959 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7960 return ansi_opname (ARRAY_REF);
7962 /* Extensions. */
7963 case CPP_MIN:
7964 id = ansi_opname (MIN_EXPR);
7965 break;
7967 case CPP_MAX:
7968 id = ansi_opname (MAX_EXPR);
7969 break;
7971 case CPP_MIN_EQ:
7972 id = ansi_assopname (MIN_EXPR);
7973 break;
7975 case CPP_MAX_EQ:
7976 id = ansi_assopname (MAX_EXPR);
7977 break;
7979 default:
7980 /* Anything else is an error. */
7981 break;
7984 /* If we have selected an identifier, we need to consume the
7985 operator token. */
7986 if (id)
7987 cp_lexer_consume_token (parser->lexer);
7988 /* Otherwise, no valid operator name was present. */
7989 else
7991 cp_parser_error (parser, "expected operator");
7992 id = error_mark_node;
7995 return id;
7998 /* Parse a template-declaration.
8000 template-declaration:
8001 export [opt] template < template-parameter-list > declaration
8003 If MEMBER_P is TRUE, this template-declaration occurs within a
8004 class-specifier.
8006 The grammar rule given by the standard isn't correct. What
8007 is really meant is:
8009 template-declaration:
8010 export [opt] template-parameter-list-seq
8011 decl-specifier-seq [opt] init-declarator [opt] ;
8012 export [opt] template-parameter-list-seq
8013 function-definition
8015 template-parameter-list-seq:
8016 template-parameter-list-seq [opt]
8017 template < template-parameter-list > */
8019 static void
8020 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8022 /* Check for `export'. */
8023 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8025 /* Consume the `export' token. */
8026 cp_lexer_consume_token (parser->lexer);
8027 /* Warn that we do not support `export'. */
8028 warning ("keyword `export' not implemented, and will be ignored");
8031 cp_parser_template_declaration_after_export (parser, member_p);
8034 /* Parse a template-parameter-list.
8036 template-parameter-list:
8037 template-parameter
8038 template-parameter-list , template-parameter
8040 Returns a TREE_LIST. Each node represents a template parameter.
8041 The nodes are connected via their TREE_CHAINs. */
8043 static tree
8044 cp_parser_template_parameter_list (cp_parser* parser)
8046 tree parameter_list = NULL_TREE;
8048 while (true)
8050 tree parameter;
8051 cp_token *token;
8052 bool is_non_type;
8054 /* Parse the template-parameter. */
8055 parameter = cp_parser_template_parameter (parser, &is_non_type);
8056 /* Add it to the list. */
8057 parameter_list = process_template_parm (parameter_list,
8058 parameter,
8059 is_non_type);
8060 /* Peek at the next token. */
8061 token = cp_lexer_peek_token (parser->lexer);
8062 /* If it's not a `,', we're done. */
8063 if (token->type != CPP_COMMA)
8064 break;
8065 /* Otherwise, consume the `,' token. */
8066 cp_lexer_consume_token (parser->lexer);
8069 return parameter_list;
8072 /* Parse a template-parameter.
8074 template-parameter:
8075 type-parameter
8076 parameter-declaration
8078 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
8079 TREE_PURPOSE is the default value, if any. *IS_NON_TYPE is set to
8080 true iff this parameter is a non-type parameter. */
8082 static tree
8083 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8085 cp_token *token;
8086 cp_parameter_declarator *parameter_declarator;
8088 /* Assume it is a type parameter or a template parameter. */
8089 *is_non_type = false;
8090 /* Peek at the next token. */
8091 token = cp_lexer_peek_token (parser->lexer);
8092 /* If it is `class' or `template', we have a type-parameter. */
8093 if (token->keyword == RID_TEMPLATE)
8094 return cp_parser_type_parameter (parser);
8095 /* If it is `class' or `typename' we do not know yet whether it is a
8096 type parameter or a non-type parameter. Consider:
8098 template <typename T, typename T::X X> ...
8102 template <class C, class D*> ...
8104 Here, the first parameter is a type parameter, and the second is
8105 a non-type parameter. We can tell by looking at the token after
8106 the identifier -- if it is a `,', `=', or `>' then we have a type
8107 parameter. */
8108 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8110 /* Peek at the token after `class' or `typename'. */
8111 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8112 /* If it's an identifier, skip it. */
8113 if (token->type == CPP_NAME)
8114 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8115 /* Now, see if the token looks like the end of a template
8116 parameter. */
8117 if (token->type == CPP_COMMA
8118 || token->type == CPP_EQ
8119 || token->type == CPP_GREATER)
8120 return cp_parser_type_parameter (parser);
8123 /* Otherwise, it is a non-type parameter.
8125 [temp.param]
8127 When parsing a default template-argument for a non-type
8128 template-parameter, the first non-nested `>' is taken as the end
8129 of the template parameter-list rather than a greater-than
8130 operator. */
8131 *is_non_type = true;
8132 parameter_declarator
8133 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8134 /*parenthesized_p=*/NULL);
8135 return (build_tree_list
8136 (parameter_declarator->default_argument,
8137 grokdeclarator (parameter_declarator->declarator,
8138 &parameter_declarator->decl_specifiers,
8139 PARM, /*initialized=*/0,
8140 /*attrlist=*/NULL)));
8143 /* Parse a type-parameter.
8145 type-parameter:
8146 class identifier [opt]
8147 class identifier [opt] = type-id
8148 typename identifier [opt]
8149 typename identifier [opt] = type-id
8150 template < template-parameter-list > class identifier [opt]
8151 template < template-parameter-list > class identifier [opt]
8152 = id-expression
8154 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8155 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8156 the declaration of the parameter. */
8158 static tree
8159 cp_parser_type_parameter (cp_parser* parser)
8161 cp_token *token;
8162 tree parameter;
8164 /* Look for a keyword to tell us what kind of parameter this is. */
8165 token = cp_parser_require (parser, CPP_KEYWORD,
8166 "`class', `typename', or `template'");
8167 if (!token)
8168 return error_mark_node;
8170 switch (token->keyword)
8172 case RID_CLASS:
8173 case RID_TYPENAME:
8175 tree identifier;
8176 tree default_argument;
8178 /* If the next token is an identifier, then it names the
8179 parameter. */
8180 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8181 identifier = cp_parser_identifier (parser);
8182 else
8183 identifier = NULL_TREE;
8185 /* Create the parameter. */
8186 parameter = finish_template_type_parm (class_type_node, identifier);
8188 /* If the next token is an `=', we have a default argument. */
8189 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8191 /* Consume the `=' token. */
8192 cp_lexer_consume_token (parser->lexer);
8193 /* Parse the default-argument. */
8194 default_argument = cp_parser_type_id (parser);
8196 else
8197 default_argument = NULL_TREE;
8199 /* Create the combined representation of the parameter and the
8200 default argument. */
8201 parameter = build_tree_list (default_argument, parameter);
8203 break;
8205 case RID_TEMPLATE:
8207 tree parameter_list;
8208 tree identifier;
8209 tree default_argument;
8211 /* Look for the `<'. */
8212 cp_parser_require (parser, CPP_LESS, "`<'");
8213 /* Parse the template-parameter-list. */
8214 begin_template_parm_list ();
8215 parameter_list
8216 = cp_parser_template_parameter_list (parser);
8217 parameter_list = end_template_parm_list (parameter_list);
8218 /* Look for the `>'. */
8219 cp_parser_require (parser, CPP_GREATER, "`>'");
8220 /* Look for the `class' keyword. */
8221 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8222 /* If the next token is an `=', then there is a
8223 default-argument. If the next token is a `>', we are at
8224 the end of the parameter-list. If the next token is a `,',
8225 then we are at the end of this parameter. */
8226 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8227 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8228 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8229 identifier = cp_parser_identifier (parser);
8230 else
8231 identifier = NULL_TREE;
8232 /* Create the template parameter. */
8233 parameter = finish_template_template_parm (class_type_node,
8234 identifier);
8236 /* If the next token is an `=', then there is a
8237 default-argument. */
8238 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8240 bool is_template;
8242 /* Consume the `='. */
8243 cp_lexer_consume_token (parser->lexer);
8244 /* Parse the id-expression. */
8245 default_argument
8246 = cp_parser_id_expression (parser,
8247 /*template_keyword_p=*/false,
8248 /*check_dependency_p=*/true,
8249 /*template_p=*/&is_template,
8250 /*declarator_p=*/false);
8251 if (TREE_CODE (default_argument) == TYPE_DECL)
8252 /* If the id-expression was a template-id that refers to
8253 a template-class, we already have the declaration here,
8254 so no further lookup is needed. */
8256 else
8257 /* Look up the name. */
8258 default_argument
8259 = cp_parser_lookup_name (parser, default_argument,
8260 /*is_type=*/false,
8261 /*is_template=*/is_template,
8262 /*is_namespace=*/false,
8263 /*check_dependency=*/true,
8264 /*ambiguous_p=*/NULL);
8265 /* See if the default argument is valid. */
8266 default_argument
8267 = check_template_template_default_arg (default_argument);
8269 else
8270 default_argument = NULL_TREE;
8272 /* Create the combined representation of the parameter and the
8273 default argument. */
8274 parameter = build_tree_list (default_argument, parameter);
8276 break;
8278 default:
8279 /* Anything else is an error. */
8280 cp_parser_error (parser,
8281 "expected `class', `typename', or `template'");
8282 parameter = error_mark_node;
8285 return parameter;
8288 /* Parse a template-id.
8290 template-id:
8291 template-name < template-argument-list [opt] >
8293 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8294 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8295 returned. Otherwise, if the template-name names a function, or set
8296 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8297 names a class, returns a TYPE_DECL for the specialization.
8299 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8300 uninstantiated templates. */
8302 static tree
8303 cp_parser_template_id (cp_parser *parser,
8304 bool template_keyword_p,
8305 bool check_dependency_p,
8306 bool is_declaration)
8308 tree template;
8309 tree arguments;
8310 tree template_id;
8311 ptrdiff_t start_of_id;
8312 tree access_check = NULL_TREE;
8313 cp_token *next_token, *next_token_2;
8314 bool is_identifier;
8316 /* If the next token corresponds to a template-id, there is no need
8317 to reparse it. */
8318 next_token = cp_lexer_peek_token (parser->lexer);
8319 if (next_token->type == CPP_TEMPLATE_ID)
8321 tree value;
8322 tree check;
8324 /* Get the stored value. */
8325 value = cp_lexer_consume_token (parser->lexer)->value;
8326 /* Perform any access checks that were deferred. */
8327 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8328 perform_or_defer_access_check (TREE_PURPOSE (check),
8329 TREE_VALUE (check));
8330 /* Return the stored value. */
8331 return TREE_VALUE (value);
8334 /* Avoid performing name lookup if there is no possibility of
8335 finding a template-id. */
8336 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8337 || (next_token->type == CPP_NAME
8338 && !cp_parser_nth_token_starts_template_argument_list_p
8339 (parser, 2)))
8341 cp_parser_error (parser, "expected template-id");
8342 return error_mark_node;
8345 /* Remember where the template-id starts. */
8346 if (cp_parser_parsing_tentatively (parser)
8347 && !cp_parser_committed_to_tentative_parse (parser))
8349 next_token = cp_lexer_peek_token (parser->lexer);
8350 start_of_id = cp_lexer_token_difference (parser->lexer,
8351 parser->lexer->buffer,
8352 next_token);
8354 else
8355 start_of_id = -1;
8357 push_deferring_access_checks (dk_deferred);
8359 /* Parse the template-name. */
8360 is_identifier = false;
8361 template = cp_parser_template_name (parser, template_keyword_p,
8362 check_dependency_p,
8363 is_declaration,
8364 &is_identifier);
8365 if (template == error_mark_node || is_identifier)
8367 pop_deferring_access_checks ();
8368 return template;
8371 /* If we find the sequence `[:' after a template-name, it's probably
8372 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8373 parse correctly the argument list. */
8374 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
8375 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8376 if (next_token->type == CPP_OPEN_SQUARE
8377 && next_token->flags & DIGRAPH
8378 && next_token_2->type == CPP_COLON
8379 && !(next_token_2->flags & PREV_WHITE))
8381 cp_parser_parse_tentatively (parser);
8382 /* Change `:' into `::'. */
8383 next_token_2->type = CPP_SCOPE;
8384 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8385 CPP_LESS. */
8386 cp_lexer_consume_token (parser->lexer);
8387 /* Parse the arguments. */
8388 arguments = cp_parser_enclosed_template_argument_list (parser);
8389 if (!cp_parser_parse_definitely (parser))
8391 /* If we couldn't parse an argument list, then we revert our changes
8392 and return simply an error. Maybe this is not a template-id
8393 after all. */
8394 next_token_2->type = CPP_COLON;
8395 cp_parser_error (parser, "expected `<'");
8396 pop_deferring_access_checks ();
8397 return error_mark_node;
8399 /* Otherwise, emit an error about the invalid digraph, but continue
8400 parsing because we got our argument list. */
8401 pedwarn ("`<::' cannot begin a template-argument list");
8402 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8403 "between `<' and `::'");
8404 if (!flag_permissive)
8406 static bool hint;
8407 if (!hint)
8409 inform ("(if you use `-fpermissive' G++ will accept your code)");
8410 hint = true;
8414 else
8416 /* Look for the `<' that starts the template-argument-list. */
8417 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8419 pop_deferring_access_checks ();
8420 return error_mark_node;
8422 /* Parse the arguments. */
8423 arguments = cp_parser_enclosed_template_argument_list (parser);
8426 /* Build a representation of the specialization. */
8427 if (TREE_CODE (template) == IDENTIFIER_NODE)
8428 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8429 else if (DECL_CLASS_TEMPLATE_P (template)
8430 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8431 template_id
8432 = finish_template_type (template, arguments,
8433 cp_lexer_next_token_is (parser->lexer,
8434 CPP_SCOPE));
8435 else
8437 /* If it's not a class-template or a template-template, it should be
8438 a function-template. */
8439 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8440 || TREE_CODE (template) == OVERLOAD
8441 || BASELINK_P (template)));
8443 template_id = lookup_template_function (template, arguments);
8446 /* Retrieve any deferred checks. Do not pop this access checks yet
8447 so the memory will not be reclaimed during token replacing below. */
8448 access_check = get_deferred_access_checks ();
8450 /* If parsing tentatively, replace the sequence of tokens that makes
8451 up the template-id with a CPP_TEMPLATE_ID token. That way,
8452 should we re-parse the token stream, we will not have to repeat
8453 the effort required to do the parse, nor will we issue duplicate
8454 error messages about problems during instantiation of the
8455 template. */
8456 if (start_of_id >= 0)
8458 cp_token *token;
8460 /* Find the token that corresponds to the start of the
8461 template-id. */
8462 token = cp_lexer_advance_token (parser->lexer,
8463 parser->lexer->buffer,
8464 start_of_id);
8466 /* Reset the contents of the START_OF_ID token. */
8467 token->type = CPP_TEMPLATE_ID;
8468 token->value = build_tree_list (access_check, template_id);
8469 token->keyword = RID_MAX;
8470 /* Purge all subsequent tokens. */
8471 cp_lexer_purge_tokens_after (parser->lexer, token);
8474 pop_deferring_access_checks ();
8475 return template_id;
8478 /* Parse a template-name.
8480 template-name:
8481 identifier
8483 The standard should actually say:
8485 template-name:
8486 identifier
8487 operator-function-id
8489 A defect report has been filed about this issue.
8491 A conversion-function-id cannot be a template name because they cannot
8492 be part of a template-id. In fact, looking at this code:
8494 a.operator K<int>()
8496 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8497 It is impossible to call a templated conversion-function-id with an
8498 explicit argument list, since the only allowed template parameter is
8499 the type to which it is converting.
8501 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8502 `template' keyword, in a construction like:
8504 T::template f<3>()
8506 In that case `f' is taken to be a template-name, even though there
8507 is no way of knowing for sure.
8509 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8510 name refers to a set of overloaded functions, at least one of which
8511 is a template, or an IDENTIFIER_NODE with the name of the template,
8512 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8513 names are looked up inside uninstantiated templates. */
8515 static tree
8516 cp_parser_template_name (cp_parser* parser,
8517 bool template_keyword_p,
8518 bool check_dependency_p,
8519 bool is_declaration,
8520 bool *is_identifier)
8522 tree identifier;
8523 tree decl;
8524 tree fns;
8526 /* If the next token is `operator', then we have either an
8527 operator-function-id or a conversion-function-id. */
8528 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8530 /* We don't know whether we're looking at an
8531 operator-function-id or a conversion-function-id. */
8532 cp_parser_parse_tentatively (parser);
8533 /* Try an operator-function-id. */
8534 identifier = cp_parser_operator_function_id (parser);
8535 /* If that didn't work, try a conversion-function-id. */
8536 if (!cp_parser_parse_definitely (parser))
8538 cp_parser_error (parser, "expected template-name");
8539 return error_mark_node;
8542 /* Look for the identifier. */
8543 else
8544 identifier = cp_parser_identifier (parser);
8546 /* If we didn't find an identifier, we don't have a template-id. */
8547 if (identifier == error_mark_node)
8548 return error_mark_node;
8550 /* If the name immediately followed the `template' keyword, then it
8551 is a template-name. However, if the next token is not `<', then
8552 we do not treat it as a template-name, since it is not being used
8553 as part of a template-id. This enables us to handle constructs
8554 like:
8556 template <typename T> struct S { S(); };
8557 template <typename T> S<T>::S();
8559 correctly. We would treat `S' as a template -- if it were `S<T>'
8560 -- but we do not if there is no `<'. */
8562 if (processing_template_decl
8563 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8565 /* In a declaration, in a dependent context, we pretend that the
8566 "template" keyword was present in order to improve error
8567 recovery. For example, given:
8569 template <typename T> void f(T::X<int>);
8571 we want to treat "X<int>" as a template-id. */
8572 if (is_declaration
8573 && !template_keyword_p
8574 && parser->scope && TYPE_P (parser->scope)
8575 && check_dependency_p
8576 && dependent_type_p (parser->scope)
8577 /* Do not do this for dtors (or ctors), since they never
8578 need the template keyword before their name. */
8579 && !constructor_name_p (identifier, parser->scope))
8581 ptrdiff_t start;
8582 cp_token* token;
8583 /* Explain what went wrong. */
8584 error ("non-template `%D' used as template", identifier);
8585 inform ("use `%T::template %D' to indicate that it is a template",
8586 parser->scope, identifier);
8587 /* If parsing tentatively, find the location of the "<"
8588 token. */
8589 if (cp_parser_parsing_tentatively (parser)
8590 && !cp_parser_committed_to_tentative_parse (parser))
8592 cp_parser_simulate_error (parser);
8593 token = cp_lexer_peek_token (parser->lexer);
8594 token = cp_lexer_prev_token (parser->lexer, token);
8595 start = cp_lexer_token_difference (parser->lexer,
8596 parser->lexer->buffer,
8597 token);
8599 else
8600 start = -1;
8601 /* Parse the template arguments so that we can issue error
8602 messages about them. */
8603 cp_lexer_consume_token (parser->lexer);
8604 cp_parser_enclosed_template_argument_list (parser);
8605 /* Skip tokens until we find a good place from which to
8606 continue parsing. */
8607 cp_parser_skip_to_closing_parenthesis (parser,
8608 /*recovering=*/true,
8609 /*or_comma=*/true,
8610 /*consume_paren=*/false);
8611 /* If parsing tentatively, permanently remove the
8612 template argument list. That will prevent duplicate
8613 error messages from being issued about the missing
8614 "template" keyword. */
8615 if (start >= 0)
8617 token = cp_lexer_advance_token (parser->lexer,
8618 parser->lexer->buffer,
8619 start);
8620 cp_lexer_purge_tokens_after (parser->lexer, token);
8622 if (is_identifier)
8623 *is_identifier = true;
8624 return identifier;
8627 /* If the "template" keyword is present, then there is generally
8628 no point in doing name-lookup, so we just return IDENTIFIER.
8629 But, if the qualifying scope is non-dependent then we can
8630 (and must) do name-lookup normally. */
8631 if (template_keyword_p
8632 && (!parser->scope
8633 || (TYPE_P (parser->scope)
8634 && dependent_type_p (parser->scope))))
8635 return identifier;
8638 /* Look up the name. */
8639 decl = cp_parser_lookup_name (parser, identifier,
8640 /*is_type=*/false,
8641 /*is_template=*/false,
8642 /*is_namespace=*/false,
8643 check_dependency_p,
8644 /*ambiguous_p=*/NULL);
8645 decl = maybe_get_template_decl_from_type_decl (decl);
8647 /* If DECL is a template, then the name was a template-name. */
8648 if (TREE_CODE (decl) == TEMPLATE_DECL)
8650 else
8652 /* The standard does not explicitly indicate whether a name that
8653 names a set of overloaded declarations, some of which are
8654 templates, is a template-name. However, such a name should
8655 be a template-name; otherwise, there is no way to form a
8656 template-id for the overloaded templates. */
8657 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8658 if (TREE_CODE (fns) == OVERLOAD)
8660 tree fn;
8662 for (fn = fns; fn; fn = OVL_NEXT (fn))
8663 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8664 break;
8666 else
8668 /* Otherwise, the name does not name a template. */
8669 cp_parser_error (parser, "expected template-name");
8670 return error_mark_node;
8674 /* If DECL is dependent, and refers to a function, then just return
8675 its name; we will look it up again during template instantiation. */
8676 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8678 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8679 if (TYPE_P (scope) && dependent_type_p (scope))
8680 return identifier;
8683 return decl;
8686 /* Parse a template-argument-list.
8688 template-argument-list:
8689 template-argument
8690 template-argument-list , template-argument
8692 Returns a TREE_VEC containing the arguments. */
8694 static tree
8695 cp_parser_template_argument_list (cp_parser* parser)
8697 tree fixed_args[10];
8698 unsigned n_args = 0;
8699 unsigned alloced = 10;
8700 tree *arg_ary = fixed_args;
8701 tree vec;
8702 bool saved_in_template_argument_list_p;
8704 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8705 parser->in_template_argument_list_p = true;
8708 tree argument;
8710 if (n_args)
8711 /* Consume the comma. */
8712 cp_lexer_consume_token (parser->lexer);
8714 /* Parse the template-argument. */
8715 argument = cp_parser_template_argument (parser);
8716 if (n_args == alloced)
8718 alloced *= 2;
8720 if (arg_ary == fixed_args)
8722 arg_ary = xmalloc (sizeof (tree) * alloced);
8723 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8725 else
8726 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8728 arg_ary[n_args++] = argument;
8730 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8732 vec = make_tree_vec (n_args);
8734 while (n_args--)
8735 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8737 if (arg_ary != fixed_args)
8738 free (arg_ary);
8739 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8740 return vec;
8743 /* Parse a template-argument.
8745 template-argument:
8746 assignment-expression
8747 type-id
8748 id-expression
8750 The representation is that of an assignment-expression, type-id, or
8751 id-expression -- except that the qualified id-expression is
8752 evaluated, so that the value returned is either a DECL or an
8753 OVERLOAD.
8755 Although the standard says "assignment-expression", it forbids
8756 throw-expressions or assignments in the template argument.
8757 Therefore, we use "conditional-expression" instead. */
8759 static tree
8760 cp_parser_template_argument (cp_parser* parser)
8762 tree argument;
8763 bool template_p;
8764 bool address_p;
8765 bool maybe_type_id = false;
8766 cp_token *token;
8767 cp_id_kind idk;
8768 tree qualifying_class;
8770 /* There's really no way to know what we're looking at, so we just
8771 try each alternative in order.
8773 [temp.arg]
8775 In a template-argument, an ambiguity between a type-id and an
8776 expression is resolved to a type-id, regardless of the form of
8777 the corresponding template-parameter.
8779 Therefore, we try a type-id first. */
8780 cp_parser_parse_tentatively (parser);
8781 argument = cp_parser_type_id (parser);
8782 /* If there was no error parsing the type-id but the next token is a '>>',
8783 we probably found a typo for '> >'. But there are type-id which are
8784 also valid expressions. For instance:
8786 struct X { int operator >> (int); };
8787 template <int V> struct Foo {};
8788 Foo<X () >> 5> r;
8790 Here 'X()' is a valid type-id of a function type, but the user just
8791 wanted to write the expression "X() >> 5". Thus, we remember that we
8792 found a valid type-id, but we still try to parse the argument as an
8793 expression to see what happens. */
8794 if (!cp_parser_error_occurred (parser)
8795 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8797 maybe_type_id = true;
8798 cp_parser_abort_tentative_parse (parser);
8800 else
8802 /* If the next token isn't a `,' or a `>', then this argument wasn't
8803 really finished. This means that the argument is not a valid
8804 type-id. */
8805 if (!cp_parser_next_token_ends_template_argument_p (parser))
8806 cp_parser_error (parser, "expected template-argument");
8807 /* If that worked, we're done. */
8808 if (cp_parser_parse_definitely (parser))
8809 return argument;
8811 /* We're still not sure what the argument will be. */
8812 cp_parser_parse_tentatively (parser);
8813 /* Try a template. */
8814 argument = cp_parser_id_expression (parser,
8815 /*template_keyword_p=*/false,
8816 /*check_dependency_p=*/true,
8817 &template_p,
8818 /*declarator_p=*/false);
8819 /* If the next token isn't a `,' or a `>', then this argument wasn't
8820 really finished. */
8821 if (!cp_parser_next_token_ends_template_argument_p (parser))
8822 cp_parser_error (parser, "expected template-argument");
8823 if (!cp_parser_error_occurred (parser))
8825 /* Figure out what is being referred to. If the id-expression
8826 was for a class template specialization, then we will have a
8827 TYPE_DECL at this point. There is no need to do name lookup
8828 at this point in that case. */
8829 if (TREE_CODE (argument) != TYPE_DECL)
8830 argument = cp_parser_lookup_name (parser, argument,
8831 /*is_type=*/false,
8832 /*is_template=*/template_p,
8833 /*is_namespace=*/false,
8834 /*check_dependency=*/true,
8835 /*ambiguous_p=*/NULL);
8836 if (TREE_CODE (argument) != TEMPLATE_DECL
8837 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8838 cp_parser_error (parser, "expected template-name");
8840 if (cp_parser_parse_definitely (parser))
8841 return argument;
8842 /* It must be a non-type argument. There permitted cases are given
8843 in [temp.arg.nontype]:
8845 -- an integral constant-expression of integral or enumeration
8846 type; or
8848 -- the name of a non-type template-parameter; or
8850 -- the name of an object or function with external linkage...
8852 -- the address of an object or function with external linkage...
8854 -- a pointer to member... */
8855 /* Look for a non-type template parameter. */
8856 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8858 cp_parser_parse_tentatively (parser);
8859 argument = cp_parser_primary_expression (parser,
8860 &idk,
8861 &qualifying_class);
8862 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8863 || !cp_parser_next_token_ends_template_argument_p (parser))
8864 cp_parser_simulate_error (parser);
8865 if (cp_parser_parse_definitely (parser))
8866 return argument;
8868 /* If the next token is "&", the argument must be the address of an
8869 object or function with external linkage. */
8870 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8871 if (address_p)
8872 cp_lexer_consume_token (parser->lexer);
8873 /* See if we might have an id-expression. */
8874 token = cp_lexer_peek_token (parser->lexer);
8875 if (token->type == CPP_NAME
8876 || token->keyword == RID_OPERATOR
8877 || token->type == CPP_SCOPE
8878 || token->type == CPP_TEMPLATE_ID
8879 || token->type == CPP_NESTED_NAME_SPECIFIER)
8881 cp_parser_parse_tentatively (parser);
8882 argument = cp_parser_primary_expression (parser,
8883 &idk,
8884 &qualifying_class);
8885 if (cp_parser_error_occurred (parser)
8886 || !cp_parser_next_token_ends_template_argument_p (parser))
8887 cp_parser_abort_tentative_parse (parser);
8888 else
8890 if (qualifying_class)
8891 argument = finish_qualified_id_expr (qualifying_class,
8892 argument,
8893 /*done=*/true,
8894 address_p);
8895 if (TREE_CODE (argument) == VAR_DECL)
8897 /* A variable without external linkage might still be a
8898 valid constant-expression, so no error is issued here
8899 if the external-linkage check fails. */
8900 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8901 cp_parser_simulate_error (parser);
8903 else if (is_overloaded_fn (argument))
8904 /* All overloaded functions are allowed; if the external
8905 linkage test does not pass, an error will be issued
8906 later. */
8908 else if (address_p
8909 && (TREE_CODE (argument) == OFFSET_REF
8910 || TREE_CODE (argument) == SCOPE_REF))
8911 /* A pointer-to-member. */
8913 else
8914 cp_parser_simulate_error (parser);
8916 if (cp_parser_parse_definitely (parser))
8918 if (address_p)
8919 argument = build_x_unary_op (ADDR_EXPR, argument);
8920 return argument;
8924 /* If the argument started with "&", there are no other valid
8925 alternatives at this point. */
8926 if (address_p)
8928 cp_parser_error (parser, "invalid non-type template argument");
8929 return error_mark_node;
8931 /* If the argument wasn't successfully parsed as a type-id followed
8932 by '>>', the argument can only be a constant expression now.
8933 Otherwise, we try parsing the constant-expression tentatively,
8934 because the argument could really be a type-id. */
8935 if (maybe_type_id)
8936 cp_parser_parse_tentatively (parser);
8937 argument = cp_parser_constant_expression (parser,
8938 /*allow_non_constant_p=*/false,
8939 /*non_constant_p=*/NULL);
8940 argument = fold_non_dependent_expr (argument);
8941 if (!maybe_type_id)
8942 return argument;
8943 if (!cp_parser_next_token_ends_template_argument_p (parser))
8944 cp_parser_error (parser, "expected template-argument");
8945 if (cp_parser_parse_definitely (parser))
8946 return argument;
8947 /* We did our best to parse the argument as a non type-id, but that
8948 was the only alternative that matched (albeit with a '>' after
8949 it). We can assume it's just a typo from the user, and a
8950 diagnostic will then be issued. */
8951 return cp_parser_type_id (parser);
8954 /* Parse an explicit-instantiation.
8956 explicit-instantiation:
8957 template declaration
8959 Although the standard says `declaration', what it really means is:
8961 explicit-instantiation:
8962 template decl-specifier-seq [opt] declarator [opt] ;
8964 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8965 supposed to be allowed. A defect report has been filed about this
8966 issue.
8968 GNU Extension:
8970 explicit-instantiation:
8971 storage-class-specifier template
8972 decl-specifier-seq [opt] declarator [opt] ;
8973 function-specifier template
8974 decl-specifier-seq [opt] declarator [opt] ; */
8976 static void
8977 cp_parser_explicit_instantiation (cp_parser* parser)
8979 int declares_class_or_enum;
8980 cp_decl_specifier_seq decl_specifiers;
8981 tree extension_specifier = NULL_TREE;
8983 /* Look for an (optional) storage-class-specifier or
8984 function-specifier. */
8985 if (cp_parser_allow_gnu_extensions_p (parser))
8987 extension_specifier
8988 = cp_parser_storage_class_specifier_opt (parser);
8989 if (!extension_specifier)
8990 extension_specifier
8991 = cp_parser_function_specifier_opt (parser,
8992 /*decl_specs=*/NULL);
8995 /* Look for the `template' keyword. */
8996 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8997 /* Let the front end know that we are processing an explicit
8998 instantiation. */
8999 begin_explicit_instantiation ();
9000 /* [temp.explicit] says that we are supposed to ignore access
9001 control while processing explicit instantiation directives. */
9002 push_deferring_access_checks (dk_no_check);
9003 /* Parse a decl-specifier-seq. */
9004 cp_parser_decl_specifier_seq (parser,
9005 CP_PARSER_FLAGS_OPTIONAL,
9006 &decl_specifiers,
9007 &declares_class_or_enum);
9008 /* If there was exactly one decl-specifier, and it declared a class,
9009 and there's no declarator, then we have an explicit type
9010 instantiation. */
9011 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9013 tree type;
9015 type = check_tag_decl (&decl_specifiers);
9016 /* Turn access control back on for names used during
9017 template instantiation. */
9018 pop_deferring_access_checks ();
9019 if (type)
9020 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9022 else
9024 cp_declarator *declarator;
9025 tree decl;
9027 /* Parse the declarator. */
9028 declarator
9029 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9030 /*ctor_dtor_or_conv_p=*/NULL,
9031 /*parenthesized_p=*/NULL);
9032 cp_parser_check_for_definition_in_return_type (declarator,
9033 declares_class_or_enum);
9034 if (declarator != cp_error_declarator)
9036 decl = grokdeclarator (declarator, &decl_specifiers,
9037 NORMAL, 0, NULL);
9038 /* Turn access control back on for names used during
9039 template instantiation. */
9040 pop_deferring_access_checks ();
9041 /* Do the explicit instantiation. */
9042 do_decl_instantiation (decl, extension_specifier);
9044 else
9046 pop_deferring_access_checks ();
9047 /* Skip the body of the explicit instantiation. */
9048 cp_parser_skip_to_end_of_statement (parser);
9051 /* We're done with the instantiation. */
9052 end_explicit_instantiation ();
9054 cp_parser_consume_semicolon_at_end_of_statement (parser);
9057 /* Parse an explicit-specialization.
9059 explicit-specialization:
9060 template < > declaration
9062 Although the standard says `declaration', what it really means is:
9064 explicit-specialization:
9065 template <> decl-specifier [opt] init-declarator [opt] ;
9066 template <> function-definition
9067 template <> explicit-specialization
9068 template <> template-declaration */
9070 static void
9071 cp_parser_explicit_specialization (cp_parser* parser)
9073 /* Look for the `template' keyword. */
9074 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9075 /* Look for the `<'. */
9076 cp_parser_require (parser, CPP_LESS, "`<'");
9077 /* Look for the `>'. */
9078 cp_parser_require (parser, CPP_GREATER, "`>'");
9079 /* We have processed another parameter list. */
9080 ++parser->num_template_parameter_lists;
9081 /* Let the front end know that we are beginning a specialization. */
9082 begin_specialization ();
9084 /* If the next keyword is `template', we need to figure out whether
9085 or not we're looking a template-declaration. */
9086 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9088 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9089 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9090 cp_parser_template_declaration_after_export (parser,
9091 /*member_p=*/false);
9092 else
9093 cp_parser_explicit_specialization (parser);
9095 else
9096 /* Parse the dependent declaration. */
9097 cp_parser_single_declaration (parser,
9098 /*member_p=*/false,
9099 /*friend_p=*/NULL);
9101 /* We're done with the specialization. */
9102 end_specialization ();
9103 /* We're done with this parameter list. */
9104 --parser->num_template_parameter_lists;
9107 /* Parse a type-specifier.
9109 type-specifier:
9110 simple-type-specifier
9111 class-specifier
9112 enum-specifier
9113 elaborated-type-specifier
9114 cv-qualifier
9116 GNU Extension:
9118 type-specifier:
9119 __complex__
9121 Returns a representation of the type-specifier. For a
9122 class-specifier, enum-specifier, or elaborated-type-specifier, a
9123 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9125 The parser flags FLAGS is used to control type-specifier parsing.
9127 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9128 in a decl-specifier-seq.
9130 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9131 class-specifier, enum-specifier, or elaborated-type-specifier, then
9132 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9133 if a type is declared; 2 if it is defined. Otherwise, it is set to
9134 zero.
9136 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9137 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9138 is set to FALSE. */
9140 static tree
9141 cp_parser_type_specifier (cp_parser* parser,
9142 cp_parser_flags flags,
9143 cp_decl_specifier_seq *decl_specs,
9144 bool is_declaration,
9145 int* declares_class_or_enum,
9146 bool* is_cv_qualifier)
9148 tree type_spec = NULL_TREE;
9149 cp_token *token;
9150 enum rid keyword;
9151 cp_decl_spec ds = ds_last;
9153 /* Assume this type-specifier does not declare a new type. */
9154 if (declares_class_or_enum)
9155 *declares_class_or_enum = 0;
9156 /* And that it does not specify a cv-qualifier. */
9157 if (is_cv_qualifier)
9158 *is_cv_qualifier = false;
9159 /* Peek at the next token. */
9160 token = cp_lexer_peek_token (parser->lexer);
9162 /* If we're looking at a keyword, we can use that to guide the
9163 production we choose. */
9164 keyword = token->keyword;
9165 switch (keyword)
9167 case RID_ENUM:
9168 /* 'enum' [identifier] '{' introduces an enum-specifier;
9169 'enum' <anything else> introduces an elaborated-type-specifier. */
9170 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9171 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9172 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9173 == CPP_OPEN_BRACE))
9175 type_spec = cp_parser_enum_specifier (parser);
9176 if (declares_class_or_enum)
9177 *declares_class_or_enum = 2;
9178 if (decl_specs)
9179 cp_parser_set_decl_spec_type (decl_specs,
9180 type_spec,
9181 /*user_defined_p=*/true);
9182 return type_spec;
9184 else
9185 goto elaborated_type_specifier;
9187 /* Any of these indicate either a class-specifier, or an
9188 elaborated-type-specifier. */
9189 case RID_CLASS:
9190 case RID_STRUCT:
9191 case RID_UNION:
9192 /* Parse tentatively so that we can back up if we don't find a
9193 class-specifier. */
9194 cp_parser_parse_tentatively (parser);
9195 /* Look for the class-specifier. */
9196 type_spec = cp_parser_class_specifier (parser);
9197 /* If that worked, we're done. */
9198 if (cp_parser_parse_definitely (parser))
9200 if (declares_class_or_enum)
9201 *declares_class_or_enum = 2;
9202 if (decl_specs)
9203 cp_parser_set_decl_spec_type (decl_specs,
9204 type_spec,
9205 /*user_defined_p=*/true);
9206 return type_spec;
9209 /* Fall through. */
9210 elaborated_type_specifier:
9211 /* We're declaring (not defining) a class or enum. */
9212 if (declares_class_or_enum)
9213 *declares_class_or_enum = 1;
9215 /* Fall through. */
9216 case RID_TYPENAME:
9217 /* Look for an elaborated-type-specifier. */
9218 type_spec
9219 = (cp_parser_elaborated_type_specifier
9220 (parser,
9221 decl_specs && decl_specs->specs[(int) ds_friend],
9222 is_declaration));
9223 if (decl_specs)
9224 cp_parser_set_decl_spec_type (decl_specs,
9225 type_spec,
9226 /*user_defined_p=*/true);
9227 return type_spec;
9229 case RID_CONST:
9230 ds = ds_const;
9231 if (is_cv_qualifier)
9232 *is_cv_qualifier = true;
9233 break;
9235 case RID_VOLATILE:
9236 ds = ds_volatile;
9237 if (is_cv_qualifier)
9238 *is_cv_qualifier = true;
9239 break;
9241 case RID_RESTRICT:
9242 ds = ds_restrict;
9243 if (is_cv_qualifier)
9244 *is_cv_qualifier = true;
9245 break;
9247 case RID_COMPLEX:
9248 /* The `__complex__' keyword is a GNU extension. */
9249 ds = ds_complex;
9250 break;
9252 default:
9253 break;
9256 /* Handle simple keywords. */
9257 if (ds != ds_last)
9259 if (decl_specs)
9261 ++decl_specs->specs[(int)ds];
9262 decl_specs->any_specifiers_p = true;
9264 return cp_lexer_consume_token (parser->lexer)->value;
9267 /* If we do not already have a type-specifier, assume we are looking
9268 at a simple-type-specifier. */
9269 type_spec = cp_parser_simple_type_specifier (parser,
9270 decl_specs,
9271 flags);
9273 /* If we didn't find a type-specifier, and a type-specifier was not
9274 optional in this context, issue an error message. */
9275 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9277 cp_parser_error (parser, "expected type specifier");
9278 return error_mark_node;
9281 return type_spec;
9284 /* Parse a simple-type-specifier.
9286 simple-type-specifier:
9287 :: [opt] nested-name-specifier [opt] type-name
9288 :: [opt] nested-name-specifier template template-id
9289 char
9290 wchar_t
9291 bool
9292 short
9294 long
9295 signed
9296 unsigned
9297 float
9298 double
9299 void
9301 GNU Extension:
9303 simple-type-specifier:
9304 __typeof__ unary-expression
9305 __typeof__ ( type-id )
9307 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9308 appropriately updated. */
9310 static tree
9311 cp_parser_simple_type_specifier (cp_parser* parser,
9312 cp_decl_specifier_seq *decl_specs,
9313 cp_parser_flags flags)
9315 tree type = NULL_TREE;
9316 cp_token *token;
9318 /* Peek at the next token. */
9319 token = cp_lexer_peek_token (parser->lexer);
9321 /* If we're looking at a keyword, things are easy. */
9322 switch (token->keyword)
9324 case RID_CHAR:
9325 if (decl_specs)
9326 decl_specs->explicit_char_p = true;
9327 type = char_type_node;
9328 break;
9329 case RID_WCHAR:
9330 type = wchar_type_node;
9331 break;
9332 case RID_BOOL:
9333 type = boolean_type_node;
9334 break;
9335 case RID_SHORT:
9336 if (decl_specs)
9337 ++decl_specs->specs[(int) ds_short];
9338 type = short_integer_type_node;
9339 break;
9340 case RID_INT:
9341 if (decl_specs)
9342 decl_specs->explicit_int_p = true;
9343 type = integer_type_node;
9344 break;
9345 case RID_LONG:
9346 if (decl_specs)
9347 ++decl_specs->specs[(int) ds_long];
9348 type = long_integer_type_node;
9349 break;
9350 case RID_SIGNED:
9351 if (decl_specs)
9352 ++decl_specs->specs[(int) ds_signed];
9353 type = integer_type_node;
9354 break;
9355 case RID_UNSIGNED:
9356 if (decl_specs)
9357 ++decl_specs->specs[(int) ds_unsigned];
9358 type = unsigned_type_node;
9359 break;
9360 case RID_FLOAT:
9361 type = float_type_node;
9362 break;
9363 case RID_DOUBLE:
9364 type = double_type_node;
9365 break;
9366 case RID_VOID:
9367 type = void_type_node;
9368 break;
9370 case RID_TYPEOF:
9371 /* Consume the `typeof' token. */
9372 cp_lexer_consume_token (parser->lexer);
9373 /* Parse the operand to `typeof'. */
9374 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9375 /* If it is not already a TYPE, take its type. */
9376 if (!TYPE_P (type))
9377 type = finish_typeof (type);
9379 if (decl_specs)
9380 cp_parser_set_decl_spec_type (decl_specs, type,
9381 /*user_defined_p=*/true);
9383 return type;
9385 default:
9386 break;
9389 /* If the type-specifier was for a built-in type, we're done. */
9390 if (type)
9392 tree id;
9394 /* Record the type. */
9395 if (decl_specs
9396 && (token->keyword != RID_SIGNED
9397 && token->keyword != RID_UNSIGNED
9398 && token->keyword != RID_SHORT
9399 && token->keyword != RID_LONG))
9400 cp_parser_set_decl_spec_type (decl_specs,
9401 type,
9402 /*user_defined=*/false);
9403 if (decl_specs)
9404 decl_specs->any_specifiers_p = true;
9406 /* Consume the token. */
9407 id = cp_lexer_consume_token (parser->lexer)->value;
9409 /* There is no valid C++ program where a non-template type is
9410 followed by a "<". That usually indicates that the user thought
9411 that the type was a template. */
9412 cp_parser_check_for_invalid_template_id (parser, type);
9414 return TYPE_NAME (type);
9417 /* The type-specifier must be a user-defined type. */
9418 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9420 bool qualified_p;
9421 bool global_p;
9423 /* Don't gobble tokens or issue error messages if this is an
9424 optional type-specifier. */
9425 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9426 cp_parser_parse_tentatively (parser);
9428 /* Look for the optional `::' operator. */
9429 global_p
9430 = (cp_parser_global_scope_opt (parser,
9431 /*current_scope_valid_p=*/false)
9432 != NULL_TREE);
9433 /* Look for the nested-name specifier. */
9434 qualified_p
9435 = (cp_parser_nested_name_specifier_opt (parser,
9436 /*typename_keyword_p=*/false,
9437 /*check_dependency_p=*/true,
9438 /*type_p=*/false,
9439 /*is_declaration=*/false)
9440 != NULL_TREE);
9441 /* If we have seen a nested-name-specifier, and the next token
9442 is `template', then we are using the template-id production. */
9443 if (parser->scope
9444 && cp_parser_optional_template_keyword (parser))
9446 /* Look for the template-id. */
9447 type = cp_parser_template_id (parser,
9448 /*template_keyword_p=*/true,
9449 /*check_dependency_p=*/true,
9450 /*is_declaration=*/false);
9451 /* If the template-id did not name a type, we are out of
9452 luck. */
9453 if (TREE_CODE (type) != TYPE_DECL)
9455 cp_parser_error (parser, "expected template-id for type");
9456 type = NULL_TREE;
9459 /* Otherwise, look for a type-name. */
9460 else
9461 type = cp_parser_type_name (parser);
9462 /* Keep track of all name-lookups performed in class scopes. */
9463 if (type
9464 && !global_p
9465 && !qualified_p
9466 && TREE_CODE (type) == TYPE_DECL
9467 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9468 maybe_note_name_used_in_class (DECL_NAME (type), type);
9469 /* If it didn't work out, we don't have a TYPE. */
9470 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9471 && !cp_parser_parse_definitely (parser))
9472 type = NULL_TREE;
9473 if (type && decl_specs)
9474 cp_parser_set_decl_spec_type (decl_specs, type,
9475 /*user_defined=*/true);
9478 /* If we didn't get a type-name, issue an error message. */
9479 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9481 cp_parser_error (parser, "expected type-name");
9482 return error_mark_node;
9485 /* There is no valid C++ program where a non-template type is
9486 followed by a "<". That usually indicates that the user thought
9487 that the type was a template. */
9488 if (type && type != error_mark_node)
9489 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9491 return type;
9494 /* Parse a type-name.
9496 type-name:
9497 class-name
9498 enum-name
9499 typedef-name
9501 enum-name:
9502 identifier
9504 typedef-name:
9505 identifier
9507 Returns a TYPE_DECL for the the type. */
9509 static tree
9510 cp_parser_type_name (cp_parser* parser)
9512 tree type_decl;
9513 tree identifier;
9515 /* We can't know yet whether it is a class-name or not. */
9516 cp_parser_parse_tentatively (parser);
9517 /* Try a class-name. */
9518 type_decl = cp_parser_class_name (parser,
9519 /*typename_keyword_p=*/false,
9520 /*template_keyword_p=*/false,
9521 /*type_p=*/false,
9522 /*check_dependency_p=*/true,
9523 /*class_head_p=*/false,
9524 /*is_declaration=*/false);
9525 /* If it's not a class-name, keep looking. */
9526 if (!cp_parser_parse_definitely (parser))
9528 /* It must be a typedef-name or an enum-name. */
9529 identifier = cp_parser_identifier (parser);
9530 if (identifier == error_mark_node)
9531 return error_mark_node;
9533 /* Look up the type-name. */
9534 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9535 /* Issue an error if we did not find a type-name. */
9536 if (TREE_CODE (type_decl) != TYPE_DECL)
9538 if (!cp_parser_simulate_error (parser))
9539 cp_parser_name_lookup_error (parser, identifier, type_decl,
9540 "is not a type");
9541 type_decl = error_mark_node;
9543 /* Remember that the name was used in the definition of the
9544 current class so that we can check later to see if the
9545 meaning would have been different after the class was
9546 entirely defined. */
9547 else if (type_decl != error_mark_node
9548 && !parser->scope)
9549 maybe_note_name_used_in_class (identifier, type_decl);
9552 return type_decl;
9556 /* Parse an elaborated-type-specifier. Note that the grammar given
9557 here incorporates the resolution to DR68.
9559 elaborated-type-specifier:
9560 class-key :: [opt] nested-name-specifier [opt] identifier
9561 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9562 enum :: [opt] nested-name-specifier [opt] identifier
9563 typename :: [opt] nested-name-specifier identifier
9564 typename :: [opt] nested-name-specifier template [opt]
9565 template-id
9567 GNU extension:
9569 elaborated-type-specifier:
9570 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9571 class-key attributes :: [opt] nested-name-specifier [opt]
9572 template [opt] template-id
9573 enum attributes :: [opt] nested-name-specifier [opt] identifier
9575 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9576 declared `friend'. If IS_DECLARATION is TRUE, then this
9577 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9578 something is being declared.
9580 Returns the TYPE specified. */
9582 static tree
9583 cp_parser_elaborated_type_specifier (cp_parser* parser,
9584 bool is_friend,
9585 bool is_declaration)
9587 enum tag_types tag_type;
9588 tree identifier;
9589 tree type = NULL_TREE;
9590 tree attributes = NULL_TREE;
9592 /* See if we're looking at the `enum' keyword. */
9593 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9595 /* Consume the `enum' token. */
9596 cp_lexer_consume_token (parser->lexer);
9597 /* Remember that it's an enumeration type. */
9598 tag_type = enum_type;
9599 /* Parse the attributes. */
9600 attributes = cp_parser_attributes_opt (parser);
9602 /* Or, it might be `typename'. */
9603 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9604 RID_TYPENAME))
9606 /* Consume the `typename' token. */
9607 cp_lexer_consume_token (parser->lexer);
9608 /* Remember that it's a `typename' type. */
9609 tag_type = typename_type;
9610 /* The `typename' keyword is only allowed in templates. */
9611 if (!processing_template_decl)
9612 pedwarn ("using `typename' outside of template");
9614 /* Otherwise it must be a class-key. */
9615 else
9617 tag_type = cp_parser_class_key (parser);
9618 if (tag_type == none_type)
9619 return error_mark_node;
9620 /* Parse the attributes. */
9621 attributes = cp_parser_attributes_opt (parser);
9624 /* Look for the `::' operator. */
9625 cp_parser_global_scope_opt (parser,
9626 /*current_scope_valid_p=*/false);
9627 /* Look for the nested-name-specifier. */
9628 if (tag_type == typename_type)
9630 if (cp_parser_nested_name_specifier (parser,
9631 /*typename_keyword_p=*/true,
9632 /*check_dependency_p=*/true,
9633 /*type_p=*/true,
9634 is_declaration)
9635 == error_mark_node)
9636 return error_mark_node;
9638 else
9639 /* Even though `typename' is not present, the proposed resolution
9640 to Core Issue 180 says that in `class A<T>::B', `B' should be
9641 considered a type-name, even if `A<T>' is dependent. */
9642 cp_parser_nested_name_specifier_opt (parser,
9643 /*typename_keyword_p=*/true,
9644 /*check_dependency_p=*/true,
9645 /*type_p=*/true,
9646 is_declaration);
9647 /* For everything but enumeration types, consider a template-id. */
9648 if (tag_type != enum_type)
9650 bool template_p = false;
9651 tree decl;
9653 /* Allow the `template' keyword. */
9654 template_p = cp_parser_optional_template_keyword (parser);
9655 /* If we didn't see `template', we don't know if there's a
9656 template-id or not. */
9657 if (!template_p)
9658 cp_parser_parse_tentatively (parser);
9659 /* Parse the template-id. */
9660 decl = cp_parser_template_id (parser, template_p,
9661 /*check_dependency_p=*/true,
9662 is_declaration);
9663 /* If we didn't find a template-id, look for an ordinary
9664 identifier. */
9665 if (!template_p && !cp_parser_parse_definitely (parser))
9667 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9668 in effect, then we must assume that, upon instantiation, the
9669 template will correspond to a class. */
9670 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9671 && tag_type == typename_type)
9672 type = make_typename_type (parser->scope, decl,
9673 /*complain=*/1);
9674 else
9675 type = TREE_TYPE (decl);
9678 /* For an enumeration type, consider only a plain identifier. */
9679 if (!type)
9681 identifier = cp_parser_identifier (parser);
9683 if (identifier == error_mark_node)
9685 parser->scope = NULL_TREE;
9686 return error_mark_node;
9689 /* For a `typename', we needn't call xref_tag. */
9690 if (tag_type == typename_type)
9691 return cp_parser_make_typename_type (parser, parser->scope,
9692 identifier);
9693 /* Look up a qualified name in the usual way. */
9694 if (parser->scope)
9696 tree decl;
9698 /* In an elaborated-type-specifier, names are assumed to name
9699 types, so we set IS_TYPE to TRUE when calling
9700 cp_parser_lookup_name. */
9701 decl = cp_parser_lookup_name (parser, identifier,
9702 /*is_type=*/true,
9703 /*is_template=*/false,
9704 /*is_namespace=*/false,
9705 /*check_dependency=*/true,
9706 /*ambiguous_p=*/NULL);
9708 /* If we are parsing friend declaration, DECL may be a
9709 TEMPLATE_DECL tree node here. However, we need to check
9710 whether this TEMPLATE_DECL results in valid code. Consider
9711 the following example:
9713 namespace N {
9714 template <class T> class C {};
9716 class X {
9717 template <class T> friend class N::C; // #1, valid code
9719 template <class T> class Y {
9720 friend class N::C; // #2, invalid code
9723 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9724 name lookup of `N::C'. We see that friend declaration must
9725 be template for the code to be valid. Note that
9726 processing_template_decl does not work here since it is
9727 always 1 for the above two cases. */
9729 decl = (cp_parser_maybe_treat_template_as_class
9730 (decl, /*tag_name_p=*/is_friend
9731 && parser->num_template_parameter_lists));
9733 if (TREE_CODE (decl) != TYPE_DECL)
9735 error ("expected type-name");
9736 return error_mark_node;
9739 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9740 check_elaborated_type_specifier
9741 (tag_type, decl,
9742 (parser->num_template_parameter_lists
9743 || DECL_SELF_REFERENCE_P (decl)));
9745 type = TREE_TYPE (decl);
9747 else
9749 /* An elaborated-type-specifier sometimes introduces a new type and
9750 sometimes names an existing type. Normally, the rule is that it
9751 introduces a new type only if there is not an existing type of
9752 the same name already in scope. For example, given:
9754 struct S {};
9755 void f() { struct S s; }
9757 the `struct S' in the body of `f' is the same `struct S' as in
9758 the global scope; the existing definition is used. However, if
9759 there were no global declaration, this would introduce a new
9760 local class named `S'.
9762 An exception to this rule applies to the following code:
9764 namespace N { struct S; }
9766 Here, the elaborated-type-specifier names a new type
9767 unconditionally; even if there is already an `S' in the
9768 containing scope this declaration names a new type.
9769 This exception only applies if the elaborated-type-specifier
9770 forms the complete declaration:
9772 [class.name]
9774 A declaration consisting solely of `class-key identifier ;' is
9775 either a redeclaration of the name in the current scope or a
9776 forward declaration of the identifier as a class name. It
9777 introduces the name into the current scope.
9779 We are in this situation precisely when the next token is a `;'.
9781 An exception to the exception is that a `friend' declaration does
9782 *not* name a new type; i.e., given:
9784 struct S { friend struct T; };
9786 `T' is not a new type in the scope of `S'.
9788 Also, `new struct S' or `sizeof (struct S)' never results in the
9789 definition of a new type; a new type can only be declared in a
9790 declaration context. */
9792 /* Warn about attributes. They are ignored. */
9793 if (attributes)
9794 warning ("type attributes are honored only at type definition");
9796 type = xref_tag (tag_type, identifier,
9797 (is_friend
9798 || !is_declaration
9799 || cp_lexer_next_token_is_not (parser->lexer,
9800 CPP_SEMICOLON)),
9801 parser->num_template_parameter_lists);
9804 if (tag_type != enum_type)
9805 cp_parser_check_class_key (tag_type, type);
9807 /* A "<" cannot follow an elaborated type specifier. If that
9808 happens, the user was probably trying to form a template-id. */
9809 cp_parser_check_for_invalid_template_id (parser, type);
9811 return type;
9814 /* Parse an enum-specifier.
9816 enum-specifier:
9817 enum identifier [opt] { enumerator-list [opt] }
9819 Returns an ENUM_TYPE representing the enumeration. */
9821 static tree
9822 cp_parser_enum_specifier (cp_parser* parser)
9824 tree identifier;
9825 tree type;
9827 /* Caller guarantees that the current token is 'enum', an identifier
9828 possibly follows, and the token after that is an opening brace.
9829 If we don't have an identifier, fabricate an anonymous name for
9830 the enumeration being defined. */
9831 cp_lexer_consume_token (parser->lexer);
9833 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9834 identifier = cp_parser_identifier (parser);
9835 else
9836 identifier = make_anon_name ();
9838 cp_lexer_consume_token (parser->lexer);
9840 /* Issue an error message if type-definitions are forbidden here. */
9841 cp_parser_check_type_definition (parser);
9843 /* Create the new type. */
9844 type = start_enum (identifier);
9846 /* If the next token is not '}', then there are some enumerators. */
9847 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9848 cp_parser_enumerator_list (parser, type);
9850 /* Consume the final '}'. */
9851 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9853 /* Finish up the enumeration. */
9854 finish_enum (type);
9856 return type;
9859 /* Parse an enumerator-list. The enumerators all have the indicated
9860 TYPE.
9862 enumerator-list:
9863 enumerator-definition
9864 enumerator-list , enumerator-definition */
9866 static void
9867 cp_parser_enumerator_list (cp_parser* parser, tree type)
9869 while (true)
9871 /* Parse an enumerator-definition. */
9872 cp_parser_enumerator_definition (parser, type);
9874 /* If the next token is not a ',', we've reached the end of
9875 the list. */
9876 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9877 break;
9878 /* Otherwise, consume the `,' and keep going. */
9879 cp_lexer_consume_token (parser->lexer);
9880 /* If the next token is a `}', there is a trailing comma. */
9881 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9883 if (pedantic && !in_system_header)
9884 pedwarn ("comma at end of enumerator list");
9885 break;
9890 /* Parse an enumerator-definition. The enumerator has the indicated
9891 TYPE.
9893 enumerator-definition:
9894 enumerator
9895 enumerator = constant-expression
9897 enumerator:
9898 identifier */
9900 static void
9901 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9903 tree identifier;
9904 tree value;
9906 /* Look for the identifier. */
9907 identifier = cp_parser_identifier (parser);
9908 if (identifier == error_mark_node)
9909 return;
9911 /* If the next token is an '=', then there is an explicit value. */
9912 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9914 /* Consume the `=' token. */
9915 cp_lexer_consume_token (parser->lexer);
9916 /* Parse the value. */
9917 value = cp_parser_constant_expression (parser,
9918 /*allow_non_constant_p=*/false,
9919 NULL);
9921 else
9922 value = NULL_TREE;
9924 /* Create the enumerator. */
9925 build_enumerator (identifier, value, type);
9928 /* Parse a namespace-name.
9930 namespace-name:
9931 original-namespace-name
9932 namespace-alias
9934 Returns the NAMESPACE_DECL for the namespace. */
9936 static tree
9937 cp_parser_namespace_name (cp_parser* parser)
9939 tree identifier;
9940 tree namespace_decl;
9942 /* Get the name of the namespace. */
9943 identifier = cp_parser_identifier (parser);
9944 if (identifier == error_mark_node)
9945 return error_mark_node;
9947 /* Look up the identifier in the currently active scope. Look only
9948 for namespaces, due to:
9950 [basic.lookup.udir]
9952 When looking up a namespace-name in a using-directive or alias
9953 definition, only namespace names are considered.
9955 And:
9957 [basic.lookup.qual]
9959 During the lookup of a name preceding the :: scope resolution
9960 operator, object, function, and enumerator names are ignored.
9962 (Note that cp_parser_class_or_namespace_name only calls this
9963 function if the token after the name is the scope resolution
9964 operator.) */
9965 namespace_decl = cp_parser_lookup_name (parser, identifier,
9966 /*is_type=*/false,
9967 /*is_template=*/false,
9968 /*is_namespace=*/true,
9969 /*check_dependency=*/true,
9970 /*ambiguous_p=*/NULL);
9971 /* If it's not a namespace, issue an error. */
9972 if (namespace_decl == error_mark_node
9973 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9975 cp_parser_error (parser, "expected namespace-name");
9976 namespace_decl = error_mark_node;
9979 return namespace_decl;
9982 /* Parse a namespace-definition.
9984 namespace-definition:
9985 named-namespace-definition
9986 unnamed-namespace-definition
9988 named-namespace-definition:
9989 original-namespace-definition
9990 extension-namespace-definition
9992 original-namespace-definition:
9993 namespace identifier { namespace-body }
9995 extension-namespace-definition:
9996 namespace original-namespace-name { namespace-body }
9998 unnamed-namespace-definition:
9999 namespace { namespace-body } */
10001 static void
10002 cp_parser_namespace_definition (cp_parser* parser)
10004 tree identifier;
10006 /* Look for the `namespace' keyword. */
10007 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10009 /* Get the name of the namespace. We do not attempt to distinguish
10010 between an original-namespace-definition and an
10011 extension-namespace-definition at this point. The semantic
10012 analysis routines are responsible for that. */
10013 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10014 identifier = cp_parser_identifier (parser);
10015 else
10016 identifier = NULL_TREE;
10018 /* Look for the `{' to start the namespace. */
10019 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10020 /* Start the namespace. */
10021 push_namespace (identifier);
10022 /* Parse the body of the namespace. */
10023 cp_parser_namespace_body (parser);
10024 /* Finish the namespace. */
10025 pop_namespace ();
10026 /* Look for the final `}'. */
10027 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10030 /* Parse a namespace-body.
10032 namespace-body:
10033 declaration-seq [opt] */
10035 static void
10036 cp_parser_namespace_body (cp_parser* parser)
10038 cp_parser_declaration_seq_opt (parser);
10041 /* Parse a namespace-alias-definition.
10043 namespace-alias-definition:
10044 namespace identifier = qualified-namespace-specifier ; */
10046 static void
10047 cp_parser_namespace_alias_definition (cp_parser* parser)
10049 tree identifier;
10050 tree namespace_specifier;
10052 /* Look for the `namespace' keyword. */
10053 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10054 /* Look for the identifier. */
10055 identifier = cp_parser_identifier (parser);
10056 if (identifier == error_mark_node)
10057 return;
10058 /* Look for the `=' token. */
10059 cp_parser_require (parser, CPP_EQ, "`='");
10060 /* Look for the qualified-namespace-specifier. */
10061 namespace_specifier
10062 = cp_parser_qualified_namespace_specifier (parser);
10063 /* Look for the `;' token. */
10064 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10066 /* Register the alias in the symbol table. */
10067 do_namespace_alias (identifier, namespace_specifier);
10070 /* Parse a qualified-namespace-specifier.
10072 qualified-namespace-specifier:
10073 :: [opt] nested-name-specifier [opt] namespace-name
10075 Returns a NAMESPACE_DECL corresponding to the specified
10076 namespace. */
10078 static tree
10079 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10081 /* Look for the optional `::'. */
10082 cp_parser_global_scope_opt (parser,
10083 /*current_scope_valid_p=*/false);
10085 /* Look for the optional nested-name-specifier. */
10086 cp_parser_nested_name_specifier_opt (parser,
10087 /*typename_keyword_p=*/false,
10088 /*check_dependency_p=*/true,
10089 /*type_p=*/false,
10090 /*is_declaration=*/true);
10092 return cp_parser_namespace_name (parser);
10095 /* Parse a using-declaration.
10097 using-declaration:
10098 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10099 using :: unqualified-id ; */
10101 static void
10102 cp_parser_using_declaration (cp_parser* parser)
10104 cp_token *token;
10105 bool typename_p = false;
10106 bool global_scope_p;
10107 tree decl;
10108 tree identifier;
10109 tree scope;
10110 tree qscope;
10112 /* Look for the `using' keyword. */
10113 cp_parser_require_keyword (parser, RID_USING, "`using'");
10115 /* Peek at the next token. */
10116 token = cp_lexer_peek_token (parser->lexer);
10117 /* See if it's `typename'. */
10118 if (token->keyword == RID_TYPENAME)
10120 /* Remember that we've seen it. */
10121 typename_p = true;
10122 /* Consume the `typename' token. */
10123 cp_lexer_consume_token (parser->lexer);
10126 /* Look for the optional global scope qualification. */
10127 global_scope_p
10128 = (cp_parser_global_scope_opt (parser,
10129 /*current_scope_valid_p=*/false)
10130 != NULL_TREE);
10132 /* If we saw `typename', or didn't see `::', then there must be a
10133 nested-name-specifier present. */
10134 if (typename_p || !global_scope_p)
10135 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10136 /*check_dependency_p=*/true,
10137 /*type_p=*/false,
10138 /*is_declaration=*/true);
10139 /* Otherwise, we could be in either of the two productions. In that
10140 case, treat the nested-name-specifier as optional. */
10141 else
10142 qscope = cp_parser_nested_name_specifier_opt (parser,
10143 /*typename_keyword_p=*/false,
10144 /*check_dependency_p=*/true,
10145 /*type_p=*/false,
10146 /*is_declaration=*/true);
10147 if (!qscope)
10148 qscope = global_namespace;
10150 /* Parse the unqualified-id. */
10151 identifier = cp_parser_unqualified_id (parser,
10152 /*template_keyword_p=*/false,
10153 /*check_dependency_p=*/true,
10154 /*declarator_p=*/true);
10156 /* The function we call to handle a using-declaration is different
10157 depending on what scope we are in. */
10158 if (identifier == error_mark_node)
10160 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10161 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10162 /* [namespace.udecl]
10164 A using declaration shall not name a template-id. */
10165 error ("a template-id may not appear in a using-declaration");
10166 else
10168 scope = current_scope ();
10169 if (scope && TYPE_P (scope))
10171 /* Create the USING_DECL. */
10172 decl = do_class_using_decl (build_nt (SCOPE_REF,
10173 parser->scope,
10174 identifier));
10175 /* Add it to the list of members in this class. */
10176 finish_member_declaration (decl);
10178 else
10180 decl = cp_parser_lookup_name_simple (parser, identifier);
10181 if (decl == error_mark_node)
10182 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10183 else if (scope)
10184 do_local_using_decl (decl, qscope, identifier);
10185 else
10186 do_toplevel_using_decl (decl, qscope, identifier);
10190 /* Look for the final `;'. */
10191 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10194 /* Parse a using-directive.
10196 using-directive:
10197 using namespace :: [opt] nested-name-specifier [opt]
10198 namespace-name ; */
10200 static void
10201 cp_parser_using_directive (cp_parser* parser)
10203 tree namespace_decl;
10204 tree attribs;
10206 /* Look for the `using' keyword. */
10207 cp_parser_require_keyword (parser, RID_USING, "`using'");
10208 /* And the `namespace' keyword. */
10209 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10210 /* Look for the optional `::' operator. */
10211 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10212 /* And the optional nested-name-specifier. */
10213 cp_parser_nested_name_specifier_opt (parser,
10214 /*typename_keyword_p=*/false,
10215 /*check_dependency_p=*/true,
10216 /*type_p=*/false,
10217 /*is_declaration=*/true);
10218 /* Get the namespace being used. */
10219 namespace_decl = cp_parser_namespace_name (parser);
10220 /* And any specified attributes. */
10221 attribs = cp_parser_attributes_opt (parser);
10222 /* Update the symbol table. */
10223 parse_using_directive (namespace_decl, attribs);
10224 /* Look for the final `;'. */
10225 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10228 /* Parse an asm-definition.
10230 asm-definition:
10231 asm ( string-literal ) ;
10233 GNU Extension:
10235 asm-definition:
10236 asm volatile [opt] ( string-literal ) ;
10237 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10238 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10239 : asm-operand-list [opt] ) ;
10240 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10241 : asm-operand-list [opt]
10242 : asm-operand-list [opt] ) ; */
10244 static void
10245 cp_parser_asm_definition (cp_parser* parser)
10247 tree string;
10248 tree outputs = NULL_TREE;
10249 tree inputs = NULL_TREE;
10250 tree clobbers = NULL_TREE;
10251 tree asm_stmt;
10252 bool volatile_p = false;
10253 bool extended_p = false;
10255 /* Look for the `asm' keyword. */
10256 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10257 /* See if the next token is `volatile'. */
10258 if (cp_parser_allow_gnu_extensions_p (parser)
10259 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10261 /* Remember that we saw the `volatile' keyword. */
10262 volatile_p = true;
10263 /* Consume the token. */
10264 cp_lexer_consume_token (parser->lexer);
10266 /* Look for the opening `('. */
10267 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10268 return;
10269 /* Look for the string. */
10270 string = cp_parser_string_literal (parser, false, false);
10271 if (string == error_mark_node)
10273 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10274 /*consume_paren=*/true);
10275 return;
10278 /* If we're allowing GNU extensions, check for the extended assembly
10279 syntax. Unfortunately, the `:' tokens need not be separated by
10280 a space in C, and so, for compatibility, we tolerate that here
10281 too. Doing that means that we have to treat the `::' operator as
10282 two `:' tokens. */
10283 if (cp_parser_allow_gnu_extensions_p (parser)
10284 && at_function_scope_p ()
10285 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10286 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10288 bool inputs_p = false;
10289 bool clobbers_p = false;
10291 /* The extended syntax was used. */
10292 extended_p = true;
10294 /* Look for outputs. */
10295 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10297 /* Consume the `:'. */
10298 cp_lexer_consume_token (parser->lexer);
10299 /* Parse the output-operands. */
10300 if (cp_lexer_next_token_is_not (parser->lexer,
10301 CPP_COLON)
10302 && cp_lexer_next_token_is_not (parser->lexer,
10303 CPP_SCOPE)
10304 && cp_lexer_next_token_is_not (parser->lexer,
10305 CPP_CLOSE_PAREN))
10306 outputs = cp_parser_asm_operand_list (parser);
10308 /* If the next token is `::', there are no outputs, and the
10309 next token is the beginning of the inputs. */
10310 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10311 /* The inputs are coming next. */
10312 inputs_p = true;
10314 /* Look for inputs. */
10315 if (inputs_p
10316 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10318 /* Consume the `:' or `::'. */
10319 cp_lexer_consume_token (parser->lexer);
10320 /* Parse the output-operands. */
10321 if (cp_lexer_next_token_is_not (parser->lexer,
10322 CPP_COLON)
10323 && cp_lexer_next_token_is_not (parser->lexer,
10324 CPP_CLOSE_PAREN))
10325 inputs = cp_parser_asm_operand_list (parser);
10327 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10328 /* The clobbers are coming next. */
10329 clobbers_p = true;
10331 /* Look for clobbers. */
10332 if (clobbers_p
10333 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10335 /* Consume the `:' or `::'. */
10336 cp_lexer_consume_token (parser->lexer);
10337 /* Parse the clobbers. */
10338 if (cp_lexer_next_token_is_not (parser->lexer,
10339 CPP_CLOSE_PAREN))
10340 clobbers = cp_parser_asm_clobber_list (parser);
10343 /* Look for the closing `)'. */
10344 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10345 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10346 /*consume_paren=*/true);
10347 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10349 /* Create the ASM_EXPR. */
10350 if (at_function_scope_p ())
10352 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10353 inputs, clobbers);
10354 /* If the extended syntax was not used, mark the ASM_EXPR. */
10355 if (!extended_p)
10356 ASM_INPUT_P (asm_stmt) = 1;
10358 else
10359 assemble_asm (string);
10362 /* Declarators [gram.dcl.decl] */
10364 /* Parse an init-declarator.
10366 init-declarator:
10367 declarator initializer [opt]
10369 GNU Extension:
10371 init-declarator:
10372 declarator asm-specification [opt] attributes [opt] initializer [opt]
10374 function-definition:
10375 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10376 function-body
10377 decl-specifier-seq [opt] declarator function-try-block
10379 GNU Extension:
10381 function-definition:
10382 __extension__ function-definition
10384 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10385 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10386 then this declarator appears in a class scope. The new DECL created
10387 by this declarator is returned.
10389 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10390 for a function-definition here as well. If the declarator is a
10391 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10392 be TRUE upon return. By that point, the function-definition will
10393 have been completely parsed.
10395 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10396 is FALSE. */
10398 static tree
10399 cp_parser_init_declarator (cp_parser* parser,
10400 cp_decl_specifier_seq *decl_specifiers,
10401 bool function_definition_allowed_p,
10402 bool member_p,
10403 int declares_class_or_enum,
10404 bool* function_definition_p)
10406 cp_token *token;
10407 cp_declarator *declarator;
10408 tree prefix_attributes;
10409 tree attributes;
10410 tree asm_specification;
10411 tree initializer;
10412 tree decl = NULL_TREE;
10413 tree scope;
10414 bool is_initialized;
10415 bool is_parenthesized_init;
10416 bool is_non_constant_init;
10417 int ctor_dtor_or_conv_p;
10418 bool friend_p;
10419 bool pop_p = false;
10421 /* Gather the attributes that were provided with the
10422 decl-specifiers. */
10423 prefix_attributes = decl_specifiers->attributes;
10425 /* Assume that this is not the declarator for a function
10426 definition. */
10427 if (function_definition_p)
10428 *function_definition_p = false;
10430 /* Defer access checks while parsing the declarator; we cannot know
10431 what names are accessible until we know what is being
10432 declared. */
10433 resume_deferring_access_checks ();
10435 /* Parse the declarator. */
10436 declarator
10437 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10438 &ctor_dtor_or_conv_p,
10439 /*parenthesized_p=*/NULL);
10440 /* Gather up the deferred checks. */
10441 stop_deferring_access_checks ();
10443 /* If the DECLARATOR was erroneous, there's no need to go
10444 further. */
10445 if (declarator == cp_error_declarator)
10446 return error_mark_node;
10448 cp_parser_check_for_definition_in_return_type (declarator,
10449 declares_class_or_enum);
10451 /* Figure out what scope the entity declared by the DECLARATOR is
10452 located in. `grokdeclarator' sometimes changes the scope, so
10453 we compute it now. */
10454 scope = get_scope_of_declarator (declarator);
10456 /* If we're allowing GNU extensions, look for an asm-specification
10457 and attributes. */
10458 if (cp_parser_allow_gnu_extensions_p (parser))
10460 /* Look for an asm-specification. */
10461 asm_specification = cp_parser_asm_specification_opt (parser);
10462 /* And attributes. */
10463 attributes = cp_parser_attributes_opt (parser);
10465 else
10467 asm_specification = NULL_TREE;
10468 attributes = NULL_TREE;
10471 /* Peek at the next token. */
10472 token = cp_lexer_peek_token (parser->lexer);
10473 /* Check to see if the token indicates the start of a
10474 function-definition. */
10475 if (cp_parser_token_starts_function_definition_p (token))
10477 if (!function_definition_allowed_p)
10479 /* If a function-definition should not appear here, issue an
10480 error message. */
10481 cp_parser_error (parser,
10482 "a function-definition is not allowed here");
10483 return error_mark_node;
10485 else
10487 /* Neither attributes nor an asm-specification are allowed
10488 on a function-definition. */
10489 if (asm_specification)
10490 error ("an asm-specification is not allowed on a function-definition");
10491 if (attributes)
10492 error ("attributes are not allowed on a function-definition");
10493 /* This is a function-definition. */
10494 *function_definition_p = true;
10496 /* Parse the function definition. */
10497 if (member_p)
10498 decl = cp_parser_save_member_function_body (parser,
10499 decl_specifiers,
10500 declarator,
10501 prefix_attributes);
10502 else
10503 decl
10504 = (cp_parser_function_definition_from_specifiers_and_declarator
10505 (parser, decl_specifiers, prefix_attributes, declarator));
10507 return decl;
10511 /* [dcl.dcl]
10513 Only in function declarations for constructors, destructors, and
10514 type conversions can the decl-specifier-seq be omitted.
10516 We explicitly postpone this check past the point where we handle
10517 function-definitions because we tolerate function-definitions
10518 that are missing their return types in some modes. */
10519 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10521 cp_parser_error (parser,
10522 "expected constructor, destructor, or type conversion");
10523 return error_mark_node;
10526 /* An `=' or an `(' indicates an initializer. */
10527 is_initialized = (token->type == CPP_EQ
10528 || token->type == CPP_OPEN_PAREN);
10529 /* If the init-declarator isn't initialized and isn't followed by a
10530 `,' or `;', it's not a valid init-declarator. */
10531 if (!is_initialized
10532 && token->type != CPP_COMMA
10533 && token->type != CPP_SEMICOLON)
10535 cp_parser_error (parser, "expected initializer");
10536 return error_mark_node;
10539 /* Because start_decl has side-effects, we should only call it if we
10540 know we're going ahead. By this point, we know that we cannot
10541 possibly be looking at any other construct. */
10542 cp_parser_commit_to_tentative_parse (parser);
10544 /* If the decl specifiers were bad, issue an error now that we're
10545 sure this was intended to be a declarator. Then continue
10546 declaring the variable(s), as int, to try to cut down on further
10547 errors. */
10548 if (decl_specifiers->any_specifiers_p
10549 && decl_specifiers->type == error_mark_node)
10551 cp_parser_error (parser, "invalid type in declaration");
10552 decl_specifiers->type = integer_type_node;
10555 /* Check to see whether or not this declaration is a friend. */
10556 friend_p = cp_parser_friend_p (decl_specifiers);
10558 /* Check that the number of template-parameter-lists is OK. */
10559 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10560 return error_mark_node;
10562 /* Enter the newly declared entry in the symbol table. If we're
10563 processing a declaration in a class-specifier, we wait until
10564 after processing the initializer. */
10565 if (!member_p)
10567 if (parser->in_unbraced_linkage_specification_p)
10569 decl_specifiers->storage_class = sc_extern;
10570 have_extern_spec = false;
10572 decl = start_decl (declarator, decl_specifiers,
10573 is_initialized, attributes, prefix_attributes,
10574 &pop_p);
10576 else if (scope)
10577 /* Enter the SCOPE. That way unqualified names appearing in the
10578 initializer will be looked up in SCOPE. */
10579 pop_p = push_scope (scope);
10581 /* Perform deferred access control checks, now that we know in which
10582 SCOPE the declared entity resides. */
10583 if (!member_p && decl)
10585 tree saved_current_function_decl = NULL_TREE;
10587 /* If the entity being declared is a function, pretend that we
10588 are in its scope. If it is a `friend', it may have access to
10589 things that would not otherwise be accessible. */
10590 if (TREE_CODE (decl) == FUNCTION_DECL)
10592 saved_current_function_decl = current_function_decl;
10593 current_function_decl = decl;
10596 /* Perform the access control checks for the declarator and the
10597 the decl-specifiers. */
10598 perform_deferred_access_checks ();
10600 /* Restore the saved value. */
10601 if (TREE_CODE (decl) == FUNCTION_DECL)
10602 current_function_decl = saved_current_function_decl;
10605 /* Parse the initializer. */
10606 if (is_initialized)
10607 initializer = cp_parser_initializer (parser,
10608 &is_parenthesized_init,
10609 &is_non_constant_init);
10610 else
10612 initializer = NULL_TREE;
10613 is_parenthesized_init = false;
10614 is_non_constant_init = true;
10617 /* The old parser allows attributes to appear after a parenthesized
10618 initializer. Mark Mitchell proposed removing this functionality
10619 on the GCC mailing lists on 2002-08-13. This parser accepts the
10620 attributes -- but ignores them. */
10621 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10622 if (cp_parser_attributes_opt (parser))
10623 warning ("attributes after parenthesized initializer ignored");
10625 /* For an in-class declaration, use `grokfield' to create the
10626 declaration. */
10627 if (member_p)
10629 if (pop_p)
10630 pop_scope (scope);
10631 decl = grokfield (declarator, decl_specifiers,
10632 initializer, /*asmspec=*/NULL_TREE,
10633 /*attributes=*/NULL_TREE);
10634 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10635 cp_parser_save_default_args (parser, decl);
10638 /* Finish processing the declaration. But, skip friend
10639 declarations. */
10640 if (!friend_p && decl && decl != error_mark_node)
10642 cp_finish_decl (decl,
10643 initializer,
10644 asm_specification,
10645 /* If the initializer is in parentheses, then this is
10646 a direct-initialization, which means that an
10647 `explicit' constructor is OK. Otherwise, an
10648 `explicit' constructor cannot be used. */
10649 ((is_parenthesized_init || !is_initialized)
10650 ? 0 : LOOKUP_ONLYCONVERTING));
10651 if (pop_p)
10652 pop_scope (DECL_CONTEXT (decl));
10655 /* Remember whether or not variables were initialized by
10656 constant-expressions. */
10657 if (decl && TREE_CODE (decl) == VAR_DECL
10658 && is_initialized && !is_non_constant_init)
10659 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10661 return decl;
10664 /* Parse a declarator.
10666 declarator:
10667 direct-declarator
10668 ptr-operator declarator
10670 abstract-declarator:
10671 ptr-operator abstract-declarator [opt]
10672 direct-abstract-declarator
10674 GNU Extensions:
10676 declarator:
10677 attributes [opt] direct-declarator
10678 attributes [opt] ptr-operator declarator
10680 abstract-declarator:
10681 attributes [opt] ptr-operator abstract-declarator [opt]
10682 attributes [opt] direct-abstract-declarator
10684 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10685 detect constructor, destructor or conversion operators. It is set
10686 to -1 if the declarator is a name, and +1 if it is a
10687 function. Otherwise it is set to zero. Usually you just want to
10688 test for >0, but internally the negative value is used.
10690 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10691 a decl-specifier-seq unless it declares a constructor, destructor,
10692 or conversion. It might seem that we could check this condition in
10693 semantic analysis, rather than parsing, but that makes it difficult
10694 to handle something like `f()'. We want to notice that there are
10695 no decl-specifiers, and therefore realize that this is an
10696 expression, not a declaration.)
10698 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10699 the declarator is a direct-declarator of the form "(...)". */
10701 static cp_declarator *
10702 cp_parser_declarator (cp_parser* parser,
10703 cp_parser_declarator_kind dcl_kind,
10704 int* ctor_dtor_or_conv_p,
10705 bool* parenthesized_p)
10707 cp_token *token;
10708 cp_declarator *declarator;
10709 enum tree_code code;
10710 cp_cv_quals cv_quals;
10711 tree class_type;
10712 tree attributes = NULL_TREE;
10714 /* Assume this is not a constructor, destructor, or type-conversion
10715 operator. */
10716 if (ctor_dtor_or_conv_p)
10717 *ctor_dtor_or_conv_p = 0;
10719 if (cp_parser_allow_gnu_extensions_p (parser))
10720 attributes = cp_parser_attributes_opt (parser);
10722 /* Peek at the next token. */
10723 token = cp_lexer_peek_token (parser->lexer);
10725 /* Check for the ptr-operator production. */
10726 cp_parser_parse_tentatively (parser);
10727 /* Parse the ptr-operator. */
10728 code = cp_parser_ptr_operator (parser,
10729 &class_type,
10730 &cv_quals);
10731 /* If that worked, then we have a ptr-operator. */
10732 if (cp_parser_parse_definitely (parser))
10734 /* If a ptr-operator was found, then this declarator was not
10735 parenthesized. */
10736 if (parenthesized_p)
10737 *parenthesized_p = true;
10738 /* The dependent declarator is optional if we are parsing an
10739 abstract-declarator. */
10740 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10741 cp_parser_parse_tentatively (parser);
10743 /* Parse the dependent declarator. */
10744 declarator = cp_parser_declarator (parser, dcl_kind,
10745 /*ctor_dtor_or_conv_p=*/NULL,
10746 /*parenthesized_p=*/NULL);
10748 /* If we are parsing an abstract-declarator, we must handle the
10749 case where the dependent declarator is absent. */
10750 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10751 && !cp_parser_parse_definitely (parser))
10752 declarator = NULL;
10754 /* Build the representation of the ptr-operator. */
10755 if (class_type)
10756 declarator = make_ptrmem_declarator (cv_quals,
10757 class_type,
10758 declarator);
10759 else if (code == INDIRECT_REF)
10760 declarator = make_pointer_declarator (cv_quals, declarator);
10761 else
10762 declarator = make_reference_declarator (cv_quals, declarator);
10764 /* Everything else is a direct-declarator. */
10765 else
10767 if (parenthesized_p)
10768 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10769 CPP_OPEN_PAREN);
10770 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10771 ctor_dtor_or_conv_p);
10774 if (attributes && declarator != cp_error_declarator)
10775 declarator->attributes = attributes;
10777 return declarator;
10780 /* Parse a direct-declarator or direct-abstract-declarator.
10782 direct-declarator:
10783 declarator-id
10784 direct-declarator ( parameter-declaration-clause )
10785 cv-qualifier-seq [opt]
10786 exception-specification [opt]
10787 direct-declarator [ constant-expression [opt] ]
10788 ( declarator )
10790 direct-abstract-declarator:
10791 direct-abstract-declarator [opt]
10792 ( parameter-declaration-clause )
10793 cv-qualifier-seq [opt]
10794 exception-specification [opt]
10795 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10796 ( abstract-declarator )
10798 Returns a representation of the declarator. DCL_KIND is
10799 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10800 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10801 we are parsing a direct-declarator. It is
10802 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10803 of ambiguity we prefer an abstract declarator, as per
10804 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10805 cp_parser_declarator. */
10807 static cp_declarator *
10808 cp_parser_direct_declarator (cp_parser* parser,
10809 cp_parser_declarator_kind dcl_kind,
10810 int* ctor_dtor_or_conv_p)
10812 cp_token *token;
10813 cp_declarator *declarator = NULL;
10814 tree scope = NULL_TREE;
10815 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10816 bool saved_in_declarator_p = parser->in_declarator_p;
10817 bool first = true;
10818 bool pop_p = false;
10820 while (true)
10822 /* Peek at the next token. */
10823 token = cp_lexer_peek_token (parser->lexer);
10824 if (token->type == CPP_OPEN_PAREN)
10826 /* This is either a parameter-declaration-clause, or a
10827 parenthesized declarator. When we know we are parsing a
10828 named declarator, it must be a parenthesized declarator
10829 if FIRST is true. For instance, `(int)' is a
10830 parameter-declaration-clause, with an omitted
10831 direct-abstract-declarator. But `((*))', is a
10832 parenthesized abstract declarator. Finally, when T is a
10833 template parameter `(T)' is a
10834 parameter-declaration-clause, and not a parenthesized
10835 named declarator.
10837 We first try and parse a parameter-declaration-clause,
10838 and then try a nested declarator (if FIRST is true).
10840 It is not an error for it not to be a
10841 parameter-declaration-clause, even when FIRST is
10842 false. Consider,
10844 int i (int);
10845 int i (3);
10847 The first is the declaration of a function while the
10848 second is a the definition of a variable, including its
10849 initializer.
10851 Having seen only the parenthesis, we cannot know which of
10852 these two alternatives should be selected. Even more
10853 complex are examples like:
10855 int i (int (a));
10856 int i (int (3));
10858 The former is a function-declaration; the latter is a
10859 variable initialization.
10861 Thus again, we try a parameter-declaration-clause, and if
10862 that fails, we back out and return. */
10864 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10866 cp_parameter_declarator *params;
10867 unsigned saved_num_template_parameter_lists;
10869 cp_parser_parse_tentatively (parser);
10871 /* Consume the `('. */
10872 cp_lexer_consume_token (parser->lexer);
10873 if (first)
10875 /* If this is going to be an abstract declarator, we're
10876 in a declarator and we can't have default args. */
10877 parser->default_arg_ok_p = false;
10878 parser->in_declarator_p = true;
10881 /* Inside the function parameter list, surrounding
10882 template-parameter-lists do not apply. */
10883 saved_num_template_parameter_lists
10884 = parser->num_template_parameter_lists;
10885 parser->num_template_parameter_lists = 0;
10887 /* Parse the parameter-declaration-clause. */
10888 params = cp_parser_parameter_declaration_clause (parser);
10890 parser->num_template_parameter_lists
10891 = saved_num_template_parameter_lists;
10893 /* If all went well, parse the cv-qualifier-seq and the
10894 exception-specification. */
10895 if (cp_parser_parse_definitely (parser))
10897 cp_cv_quals cv_quals;
10898 tree exception_specification;
10900 if (ctor_dtor_or_conv_p)
10901 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10902 first = false;
10903 /* Consume the `)'. */
10904 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10906 /* Parse the cv-qualifier-seq. */
10907 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10908 /* And the exception-specification. */
10909 exception_specification
10910 = cp_parser_exception_specification_opt (parser);
10912 /* Create the function-declarator. */
10913 declarator = make_call_declarator (declarator,
10914 params,
10915 cv_quals,
10916 exception_specification);
10917 /* Any subsequent parameter lists are to do with
10918 return type, so are not those of the declared
10919 function. */
10920 parser->default_arg_ok_p = false;
10922 /* Repeat the main loop. */
10923 continue;
10927 /* If this is the first, we can try a parenthesized
10928 declarator. */
10929 if (first)
10931 bool saved_in_type_id_in_expr_p;
10933 parser->default_arg_ok_p = saved_default_arg_ok_p;
10934 parser->in_declarator_p = saved_in_declarator_p;
10936 /* Consume the `('. */
10937 cp_lexer_consume_token (parser->lexer);
10938 /* Parse the nested declarator. */
10939 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10940 parser->in_type_id_in_expr_p = true;
10941 declarator
10942 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10943 /*parenthesized_p=*/NULL);
10944 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10945 first = false;
10946 /* Expect a `)'. */
10947 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10948 declarator = cp_error_declarator;
10949 if (declarator == cp_error_declarator)
10950 break;
10952 goto handle_declarator;
10954 /* Otherwise, we must be done. */
10955 else
10956 break;
10958 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10959 && token->type == CPP_OPEN_SQUARE)
10961 /* Parse an array-declarator. */
10962 tree bounds;
10964 if (ctor_dtor_or_conv_p)
10965 *ctor_dtor_or_conv_p = 0;
10967 first = false;
10968 parser->default_arg_ok_p = false;
10969 parser->in_declarator_p = true;
10970 /* Consume the `['. */
10971 cp_lexer_consume_token (parser->lexer);
10972 /* Peek at the next token. */
10973 token = cp_lexer_peek_token (parser->lexer);
10974 /* If the next token is `]', then there is no
10975 constant-expression. */
10976 if (token->type != CPP_CLOSE_SQUARE)
10978 bool non_constant_p;
10980 bounds
10981 = cp_parser_constant_expression (parser,
10982 /*allow_non_constant=*/true,
10983 &non_constant_p);
10984 if (!non_constant_p)
10985 bounds = fold_non_dependent_expr (bounds);
10987 else
10988 bounds = NULL_TREE;
10989 /* Look for the closing `]'. */
10990 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10992 declarator = cp_error_declarator;
10993 break;
10996 declarator = make_array_declarator (declarator, bounds);
10998 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11000 tree id;
11002 /* Parse a declarator-id */
11003 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11004 cp_parser_parse_tentatively (parser);
11005 id = cp_parser_declarator_id (parser);
11006 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11008 if (!cp_parser_parse_definitely (parser))
11009 id = error_mark_node;
11010 else if (TREE_CODE (id) != IDENTIFIER_NODE)
11012 cp_parser_error (parser, "expected unqualified-id");
11013 id = error_mark_node;
11017 if (id == error_mark_node)
11019 declarator = cp_error_declarator;
11020 break;
11023 if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11025 tree scope = TREE_OPERAND (id, 0);
11027 /* In the declaration of a member of a template class
11028 outside of the class itself, the SCOPE will sometimes
11029 be a TYPENAME_TYPE. For example, given:
11031 template <typename T>
11032 int S<T>::R::i = 3;
11034 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11035 this context, we must resolve S<T>::R to an ordinary
11036 type, rather than a typename type.
11038 The reason we normally avoid resolving TYPENAME_TYPEs
11039 is that a specialization of `S' might render
11040 `S<T>::R' not a type. However, if `S' is
11041 specialized, then this `i' will not be used, so there
11042 is no harm in resolving the types here. */
11043 if (TREE_CODE (scope) == TYPENAME_TYPE)
11045 tree type;
11047 /* Resolve the TYPENAME_TYPE. */
11048 type = resolve_typename_type (scope,
11049 /*only_current_p=*/false);
11050 /* If that failed, the declarator is invalid. */
11051 if (type == error_mark_node)
11052 error ("`%T::%D' is not a type",
11053 TYPE_CONTEXT (scope),
11054 TYPE_IDENTIFIER (scope));
11055 /* Build a new DECLARATOR. */
11056 id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11060 declarator = make_id_declarator (id);
11061 if (id)
11063 tree class_type;
11064 tree unqualified_name;
11066 if (TREE_CODE (id) == SCOPE_REF
11067 && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11069 class_type = TREE_OPERAND (id, 0);
11070 unqualified_name = TREE_OPERAND (id, 1);
11072 else
11074 class_type = current_class_type;
11075 unqualified_name = id;
11078 if (class_type)
11080 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11081 declarator->u.id.sfk = sfk_destructor;
11082 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11083 declarator->u.id.sfk = sfk_conversion;
11084 else if (constructor_name_p (unqualified_name,
11085 class_type)
11086 || (TREE_CODE (unqualified_name) == TYPE_DECL
11087 && same_type_p (TREE_TYPE (unqualified_name),
11088 class_type)))
11089 declarator->u.id.sfk = sfk_constructor;
11091 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11092 *ctor_dtor_or_conv_p = -1;
11093 if (TREE_CODE (id) == SCOPE_REF
11094 && TREE_CODE (unqualified_name) == TYPE_DECL
11095 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11097 error ("invalid use of constructor as a template");
11098 inform ("use `%T::%D' instead of `%T::%T' to name the "
11099 "constructor in a qualified name", class_type,
11100 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11101 class_type, class_type);
11106 handle_declarator:;
11107 scope = get_scope_of_declarator (declarator);
11108 if (scope)
11109 /* Any names that appear after the declarator-id for a
11110 member are looked up in the containing scope. */
11111 pop_p = push_scope (scope);
11112 parser->in_declarator_p = true;
11113 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11114 || (declarator && declarator->kind == cdk_id))
11115 /* Default args are only allowed on function
11116 declarations. */
11117 parser->default_arg_ok_p = saved_default_arg_ok_p;
11118 else
11119 parser->default_arg_ok_p = false;
11121 first = false;
11123 /* We're done. */
11124 else
11125 break;
11128 /* For an abstract declarator, we might wind up with nothing at this
11129 point. That's an error; the declarator is not optional. */
11130 if (!declarator)
11131 cp_parser_error (parser, "expected declarator");
11133 /* If we entered a scope, we must exit it now. */
11134 if (pop_p)
11135 pop_scope (scope);
11137 parser->default_arg_ok_p = saved_default_arg_ok_p;
11138 parser->in_declarator_p = saved_in_declarator_p;
11140 return declarator;
11143 /* Parse a ptr-operator.
11145 ptr-operator:
11146 * cv-qualifier-seq [opt]
11148 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11150 GNU Extension:
11152 ptr-operator:
11153 & cv-qualifier-seq [opt]
11155 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11156 Returns ADDR_EXPR if a reference was used. In the case of a
11157 pointer-to-member, *TYPE is filled in with the TYPE containing the
11158 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11159 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11160 ERROR_MARK if an error occurred. */
11162 static enum tree_code
11163 cp_parser_ptr_operator (cp_parser* parser,
11164 tree* type,
11165 cp_cv_quals *cv_quals)
11167 enum tree_code code = ERROR_MARK;
11168 cp_token *token;
11170 /* Assume that it's not a pointer-to-member. */
11171 *type = NULL_TREE;
11172 /* And that there are no cv-qualifiers. */
11173 *cv_quals = TYPE_UNQUALIFIED;
11175 /* Peek at the next token. */
11176 token = cp_lexer_peek_token (parser->lexer);
11177 /* If it's a `*' or `&' we have a pointer or reference. */
11178 if (token->type == CPP_MULT || token->type == CPP_AND)
11180 /* Remember which ptr-operator we were processing. */
11181 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11183 /* Consume the `*' or `&'. */
11184 cp_lexer_consume_token (parser->lexer);
11186 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11187 `&', if we are allowing GNU extensions. (The only qualifier
11188 that can legally appear after `&' is `restrict', but that is
11189 enforced during semantic analysis. */
11190 if (code == INDIRECT_REF
11191 || cp_parser_allow_gnu_extensions_p (parser))
11192 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11194 else
11196 /* Try the pointer-to-member case. */
11197 cp_parser_parse_tentatively (parser);
11198 /* Look for the optional `::' operator. */
11199 cp_parser_global_scope_opt (parser,
11200 /*current_scope_valid_p=*/false);
11201 /* Look for the nested-name specifier. */
11202 cp_parser_nested_name_specifier (parser,
11203 /*typename_keyword_p=*/false,
11204 /*check_dependency_p=*/true,
11205 /*type_p=*/false,
11206 /*is_declaration=*/false);
11207 /* If we found it, and the next token is a `*', then we are
11208 indeed looking at a pointer-to-member operator. */
11209 if (!cp_parser_error_occurred (parser)
11210 && cp_parser_require (parser, CPP_MULT, "`*'"))
11212 /* The type of which the member is a member is given by the
11213 current SCOPE. */
11214 *type = parser->scope;
11215 /* The next name will not be qualified. */
11216 parser->scope = NULL_TREE;
11217 parser->qualifying_scope = NULL_TREE;
11218 parser->object_scope = NULL_TREE;
11219 /* Indicate that the `*' operator was used. */
11220 code = INDIRECT_REF;
11221 /* Look for the optional cv-qualifier-seq. */
11222 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11224 /* If that didn't work we don't have a ptr-operator. */
11225 if (!cp_parser_parse_definitely (parser))
11226 cp_parser_error (parser, "expected ptr-operator");
11229 return code;
11232 /* Parse an (optional) cv-qualifier-seq.
11234 cv-qualifier-seq:
11235 cv-qualifier cv-qualifier-seq [opt]
11237 cv-qualifier:
11238 const
11239 volatile
11241 GNU Extension:
11243 cv-qualifier:
11244 __restrict__
11246 Returns a bitmask representing the cv-qualifiers. */
11248 static cp_cv_quals
11249 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11251 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11253 while (true)
11255 cp_token *token;
11256 cp_cv_quals cv_qualifier;
11258 /* Peek at the next token. */
11259 token = cp_lexer_peek_token (parser->lexer);
11260 /* See if it's a cv-qualifier. */
11261 switch (token->keyword)
11263 case RID_CONST:
11264 cv_qualifier = TYPE_QUAL_CONST;
11265 break;
11267 case RID_VOLATILE:
11268 cv_qualifier = TYPE_QUAL_VOLATILE;
11269 break;
11271 case RID_RESTRICT:
11272 cv_qualifier = TYPE_QUAL_RESTRICT;
11273 break;
11275 default:
11276 cv_qualifier = TYPE_UNQUALIFIED;
11277 break;
11280 if (!cv_qualifier)
11281 break;
11283 if (cv_quals & cv_qualifier)
11285 error ("duplicate cv-qualifier");
11286 cp_lexer_purge_token (parser->lexer);
11288 else
11290 cp_lexer_consume_token (parser->lexer);
11291 cv_quals |= cv_qualifier;
11295 return cv_quals;
11298 /* Parse a declarator-id.
11300 declarator-id:
11301 id-expression
11302 :: [opt] nested-name-specifier [opt] type-name
11304 In the `id-expression' case, the value returned is as for
11305 cp_parser_id_expression if the id-expression was an unqualified-id.
11306 If the id-expression was a qualified-id, then a SCOPE_REF is
11307 returned. The first operand is the scope (either a NAMESPACE_DECL
11308 or TREE_TYPE), but the second is still just a representation of an
11309 unqualified-id. */
11311 static tree
11312 cp_parser_declarator_id (cp_parser* parser)
11314 tree id_expression;
11316 /* The expression must be an id-expression. Assume that qualified
11317 names are the names of types so that:
11319 template <class T>
11320 int S<T>::R::i = 3;
11322 will work; we must treat `S<T>::R' as the name of a type.
11323 Similarly, assume that qualified names are templates, where
11324 required, so that:
11326 template <class T>
11327 int S<T>::R<T>::i = 3;
11329 will work, too. */
11330 id_expression = cp_parser_id_expression (parser,
11331 /*template_keyword_p=*/false,
11332 /*check_dependency_p=*/false,
11333 /*template_p=*/NULL,
11334 /*declarator_p=*/true);
11335 /* If the name was qualified, create a SCOPE_REF to represent
11336 that. */
11337 if (parser->scope)
11339 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11340 parser->scope = NULL_TREE;
11343 return id_expression;
11346 /* Parse a type-id.
11348 type-id:
11349 type-specifier-seq abstract-declarator [opt]
11351 Returns the TYPE specified. */
11353 static tree
11354 cp_parser_type_id (cp_parser* parser)
11356 cp_decl_specifier_seq type_specifier_seq;
11357 cp_declarator *abstract_declarator;
11359 /* Parse the type-specifier-seq. */
11360 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11361 if (type_specifier_seq.type == error_mark_node)
11362 return error_mark_node;
11364 /* There might or might not be an abstract declarator. */
11365 cp_parser_parse_tentatively (parser);
11366 /* Look for the declarator. */
11367 abstract_declarator
11368 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11369 /*parenthesized_p=*/NULL);
11370 /* Check to see if there really was a declarator. */
11371 if (!cp_parser_parse_definitely (parser))
11372 abstract_declarator = NULL;
11374 return groktypename (&type_specifier_seq, abstract_declarator);
11377 /* Parse a type-specifier-seq.
11379 type-specifier-seq:
11380 type-specifier type-specifier-seq [opt]
11382 GNU extension:
11384 type-specifier-seq:
11385 attributes type-specifier-seq [opt]
11387 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11389 static void
11390 cp_parser_type_specifier_seq (cp_parser* parser,
11391 cp_decl_specifier_seq *type_specifier_seq)
11393 bool seen_type_specifier = false;
11395 /* Clear the TYPE_SPECIFIER_SEQ. */
11396 clear_decl_specs (type_specifier_seq);
11398 /* Parse the type-specifiers and attributes. */
11399 while (true)
11401 tree type_specifier;
11403 /* Check for attributes first. */
11404 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11406 type_specifier_seq->attributes =
11407 chainon (type_specifier_seq->attributes,
11408 cp_parser_attributes_opt (parser));
11409 continue;
11412 /* Look for the type-specifier. */
11413 type_specifier = cp_parser_type_specifier (parser,
11414 CP_PARSER_FLAGS_OPTIONAL,
11415 type_specifier_seq,
11416 /*is_declaration=*/false,
11417 NULL,
11418 NULL);
11419 /* If the first type-specifier could not be found, this is not a
11420 type-specifier-seq at all. */
11421 if (!seen_type_specifier && !type_specifier)
11423 cp_parser_error (parser, "expected type-specifier");
11424 type_specifier_seq->type = error_mark_node;
11425 return;
11427 /* If subsequent type-specifiers could not be found, the
11428 type-specifier-seq is complete. */
11429 else if (seen_type_specifier && !type_specifier)
11430 break;
11432 seen_type_specifier = true;
11435 return;
11438 /* Parse a parameter-declaration-clause.
11440 parameter-declaration-clause:
11441 parameter-declaration-list [opt] ... [opt]
11442 parameter-declaration-list , ...
11444 Returns a representation for the parameter declarations. A return
11445 value of NULL indicates a parameter-declaration-clause consisting
11446 only of an ellipsis. */
11448 static cp_parameter_declarator *
11449 cp_parser_parameter_declaration_clause (cp_parser* parser)
11451 cp_parameter_declarator *parameters;
11452 cp_token *token;
11453 bool ellipsis_p;
11454 bool is_error;
11456 /* Peek at the next token. */
11457 token = cp_lexer_peek_token (parser->lexer);
11458 /* Check for trivial parameter-declaration-clauses. */
11459 if (token->type == CPP_ELLIPSIS)
11461 /* Consume the `...' token. */
11462 cp_lexer_consume_token (parser->lexer);
11463 return NULL;
11465 else if (token->type == CPP_CLOSE_PAREN)
11466 /* There are no parameters. */
11468 #ifndef NO_IMPLICIT_EXTERN_C
11469 if (in_system_header && current_class_type == NULL
11470 && current_lang_name == lang_name_c)
11471 return NULL;
11472 else
11473 #endif
11474 return no_parameters;
11476 /* Check for `(void)', too, which is a special case. */
11477 else if (token->keyword == RID_VOID
11478 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11479 == CPP_CLOSE_PAREN))
11481 /* Consume the `void' token. */
11482 cp_lexer_consume_token (parser->lexer);
11483 /* There are no parameters. */
11484 return no_parameters;
11487 /* Parse the parameter-declaration-list. */
11488 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11489 /* If a parse error occurred while parsing the
11490 parameter-declaration-list, then the entire
11491 parameter-declaration-clause is erroneous. */
11492 if (is_error)
11493 return NULL;
11495 /* Peek at the next token. */
11496 token = cp_lexer_peek_token (parser->lexer);
11497 /* If it's a `,', the clause should terminate with an ellipsis. */
11498 if (token->type == CPP_COMMA)
11500 /* Consume the `,'. */
11501 cp_lexer_consume_token (parser->lexer);
11502 /* Expect an ellipsis. */
11503 ellipsis_p
11504 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11506 /* It might also be `...' if the optional trailing `,' was
11507 omitted. */
11508 else if (token->type == CPP_ELLIPSIS)
11510 /* Consume the `...' token. */
11511 cp_lexer_consume_token (parser->lexer);
11512 /* And remember that we saw it. */
11513 ellipsis_p = true;
11515 else
11516 ellipsis_p = false;
11518 /* Finish the parameter list. */
11519 if (parameters && ellipsis_p)
11520 parameters->ellipsis_p = true;
11522 return parameters;
11525 /* Parse a parameter-declaration-list.
11527 parameter-declaration-list:
11528 parameter-declaration
11529 parameter-declaration-list , parameter-declaration
11531 Returns a representation of the parameter-declaration-list, as for
11532 cp_parser_parameter_declaration_clause. However, the
11533 `void_list_node' is never appended to the list. Upon return,
11534 *IS_ERROR will be true iff an error occurred. */
11536 static cp_parameter_declarator *
11537 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11539 cp_parameter_declarator *parameters = NULL;
11540 cp_parameter_declarator **tail = &parameters;
11542 /* Assume all will go well. */
11543 *is_error = false;
11545 /* Look for more parameters. */
11546 while (true)
11548 cp_parameter_declarator *parameter;
11549 bool parenthesized_p;
11550 /* Parse the parameter. */
11551 parameter
11552 = cp_parser_parameter_declaration (parser,
11553 /*template_parm_p=*/false,
11554 &parenthesized_p);
11556 /* If a parse error occurred parsing the parameter declaration,
11557 then the entire parameter-declaration-list is erroneous. */
11558 if (!parameter)
11560 *is_error = true;
11561 parameters = NULL;
11562 break;
11564 /* Add the new parameter to the list. */
11565 *tail = parameter;
11566 tail = &parameter->next;
11568 /* Peek at the next token. */
11569 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11570 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11571 /* The parameter-declaration-list is complete. */
11572 break;
11573 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11575 cp_token *token;
11577 /* Peek at the next token. */
11578 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11579 /* If it's an ellipsis, then the list is complete. */
11580 if (token->type == CPP_ELLIPSIS)
11581 break;
11582 /* Otherwise, there must be more parameters. Consume the
11583 `,'. */
11584 cp_lexer_consume_token (parser->lexer);
11585 /* When parsing something like:
11587 int i(float f, double d)
11589 we can tell after seeing the declaration for "f" that we
11590 are not looking at an initialization of a variable "i",
11591 but rather at the declaration of a function "i".
11593 Due to the fact that the parsing of template arguments
11594 (as specified to a template-id) requires backtracking we
11595 cannot use this technique when inside a template argument
11596 list. */
11597 if (!parser->in_template_argument_list_p
11598 && !parser->in_type_id_in_expr_p
11599 && cp_parser_parsing_tentatively (parser)
11600 && !cp_parser_committed_to_tentative_parse (parser)
11601 /* However, a parameter-declaration of the form
11602 "foat(f)" (which is a valid declaration of a
11603 parameter "f") can also be interpreted as an
11604 expression (the conversion of "f" to "float"). */
11605 && !parenthesized_p)
11606 cp_parser_commit_to_tentative_parse (parser);
11608 else
11610 cp_parser_error (parser, "expected `,' or `...'");
11611 if (!cp_parser_parsing_tentatively (parser)
11612 || cp_parser_committed_to_tentative_parse (parser))
11613 cp_parser_skip_to_closing_parenthesis (parser,
11614 /*recovering=*/true,
11615 /*or_comma=*/false,
11616 /*consume_paren=*/false);
11617 break;
11621 return parameters;
11624 /* Parse a parameter declaration.
11626 parameter-declaration:
11627 decl-specifier-seq declarator
11628 decl-specifier-seq declarator = assignment-expression
11629 decl-specifier-seq abstract-declarator [opt]
11630 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11632 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11633 declares a template parameter. (In that case, a non-nested `>'
11634 token encountered during the parsing of the assignment-expression
11635 is not interpreted as a greater-than operator.)
11637 Returns a representation of the parameter, or NULL if an error
11638 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11639 true iff the declarator is of the form "(p)". */
11641 static cp_parameter_declarator *
11642 cp_parser_parameter_declaration (cp_parser *parser,
11643 bool template_parm_p,
11644 bool *parenthesized_p)
11646 int declares_class_or_enum;
11647 bool greater_than_is_operator_p;
11648 cp_decl_specifier_seq decl_specifiers;
11649 cp_declarator *declarator;
11650 tree default_argument;
11651 cp_token *token;
11652 const char *saved_message;
11654 /* In a template parameter, `>' is not an operator.
11656 [temp.param]
11658 When parsing a default template-argument for a non-type
11659 template-parameter, the first non-nested `>' is taken as the end
11660 of the template parameter-list rather than a greater-than
11661 operator. */
11662 greater_than_is_operator_p = !template_parm_p;
11664 /* Type definitions may not appear in parameter types. */
11665 saved_message = parser->type_definition_forbidden_message;
11666 parser->type_definition_forbidden_message
11667 = "types may not be defined in parameter types";
11669 /* Parse the declaration-specifiers. */
11670 cp_parser_decl_specifier_seq (parser,
11671 CP_PARSER_FLAGS_NONE,
11672 &decl_specifiers,
11673 &declares_class_or_enum);
11674 /* If an error occurred, there's no reason to attempt to parse the
11675 rest of the declaration. */
11676 if (cp_parser_error_occurred (parser))
11678 parser->type_definition_forbidden_message = saved_message;
11679 return NULL;
11682 /* Peek at the next token. */
11683 token = cp_lexer_peek_token (parser->lexer);
11684 /* If the next token is a `)', `,', `=', `>', or `...', then there
11685 is no declarator. */
11686 if (token->type == CPP_CLOSE_PAREN
11687 || token->type == CPP_COMMA
11688 || token->type == CPP_EQ
11689 || token->type == CPP_ELLIPSIS
11690 || token->type == CPP_GREATER)
11692 declarator = NULL;
11693 if (parenthesized_p)
11694 *parenthesized_p = false;
11696 /* Otherwise, there should be a declarator. */
11697 else
11699 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11700 parser->default_arg_ok_p = false;
11702 /* After seeing a decl-specifier-seq, if the next token is not a
11703 "(", there is no possibility that the code is a valid
11704 expression. Therefore, if parsing tentatively, we commit at
11705 this point. */
11706 if (!parser->in_template_argument_list_p
11707 /* In an expression context, having seen:
11709 (int((char ...
11711 we cannot be sure whether we are looking at a
11712 function-type (taking a "char" as a parameter) or a cast
11713 of some object of type "char" to "int". */
11714 && !parser->in_type_id_in_expr_p
11715 && cp_parser_parsing_tentatively (parser)
11716 && !cp_parser_committed_to_tentative_parse (parser)
11717 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11718 cp_parser_commit_to_tentative_parse (parser);
11719 /* Parse the declarator. */
11720 declarator = cp_parser_declarator (parser,
11721 CP_PARSER_DECLARATOR_EITHER,
11722 /*ctor_dtor_or_conv_p=*/NULL,
11723 parenthesized_p);
11724 parser->default_arg_ok_p = saved_default_arg_ok_p;
11725 /* After the declarator, allow more attributes. */
11726 decl_specifiers.attributes
11727 = chainon (decl_specifiers.attributes,
11728 cp_parser_attributes_opt (parser));
11731 /* The restriction on defining new types applies only to the type
11732 of the parameter, not to the default argument. */
11733 parser->type_definition_forbidden_message = saved_message;
11735 /* If the next token is `=', then process a default argument. */
11736 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11738 bool saved_greater_than_is_operator_p;
11739 /* Consume the `='. */
11740 cp_lexer_consume_token (parser->lexer);
11742 /* If we are defining a class, then the tokens that make up the
11743 default argument must be saved and processed later. */
11744 if (!template_parm_p && at_class_scope_p ()
11745 && TYPE_BEING_DEFINED (current_class_type))
11747 unsigned depth = 0;
11748 cp_token *first_token;
11749 cp_token *token;
11751 /* Add tokens until we have processed the entire default
11752 argument. We add the range [first_token, token). */
11753 first_token = cp_lexer_peek_token (parser->lexer);
11754 while (true)
11756 bool done = false;
11758 /* Peek at the next token. */
11759 token = cp_lexer_peek_token (parser->lexer);
11760 /* What we do depends on what token we have. */
11761 switch (token->type)
11763 /* In valid code, a default argument must be
11764 immediately followed by a `,' `)', or `...'. */
11765 case CPP_COMMA:
11766 case CPP_CLOSE_PAREN:
11767 case CPP_ELLIPSIS:
11768 /* If we run into a non-nested `;', `}', or `]',
11769 then the code is invalid -- but the default
11770 argument is certainly over. */
11771 case CPP_SEMICOLON:
11772 case CPP_CLOSE_BRACE:
11773 case CPP_CLOSE_SQUARE:
11774 if (depth == 0)
11775 done = true;
11776 /* Update DEPTH, if necessary. */
11777 else if (token->type == CPP_CLOSE_PAREN
11778 || token->type == CPP_CLOSE_BRACE
11779 || token->type == CPP_CLOSE_SQUARE)
11780 --depth;
11781 break;
11783 case CPP_OPEN_PAREN:
11784 case CPP_OPEN_SQUARE:
11785 case CPP_OPEN_BRACE:
11786 ++depth;
11787 break;
11789 case CPP_GREATER:
11790 /* If we see a non-nested `>', and `>' is not an
11791 operator, then it marks the end of the default
11792 argument. */
11793 if (!depth && !greater_than_is_operator_p)
11794 done = true;
11795 break;
11797 /* If we run out of tokens, issue an error message. */
11798 case CPP_EOF:
11799 error ("file ends in default argument");
11800 done = true;
11801 break;
11803 case CPP_NAME:
11804 case CPP_SCOPE:
11805 /* In these cases, we should look for template-ids.
11806 For example, if the default argument is
11807 `X<int, double>()', we need to do name lookup to
11808 figure out whether or not `X' is a template; if
11809 so, the `,' does not end the default argument.
11811 That is not yet done. */
11812 break;
11814 default:
11815 break;
11818 /* If we've reached the end, stop. */
11819 if (done)
11820 break;
11822 /* Add the token to the token block. */
11823 token = cp_lexer_consume_token (parser->lexer);
11826 /* Create a DEFAULT_ARG to represented the unparsed default
11827 argument. */
11828 default_argument = make_node (DEFAULT_ARG);
11829 DEFARG_TOKENS (default_argument)
11830 = cp_token_cache_new (first_token, token);
11832 /* Outside of a class definition, we can just parse the
11833 assignment-expression. */
11834 else
11836 bool saved_local_variables_forbidden_p;
11838 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11839 set correctly. */
11840 saved_greater_than_is_operator_p
11841 = parser->greater_than_is_operator_p;
11842 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11843 /* Local variable names (and the `this' keyword) may not
11844 appear in a default argument. */
11845 saved_local_variables_forbidden_p
11846 = parser->local_variables_forbidden_p;
11847 parser->local_variables_forbidden_p = true;
11848 /* Parse the assignment-expression. */
11849 default_argument = cp_parser_assignment_expression (parser);
11850 /* Restore saved state. */
11851 parser->greater_than_is_operator_p
11852 = saved_greater_than_is_operator_p;
11853 parser->local_variables_forbidden_p
11854 = saved_local_variables_forbidden_p;
11856 if (!parser->default_arg_ok_p)
11858 if (!flag_pedantic_errors)
11859 warning ("deprecated use of default argument for parameter of non-function");
11860 else
11862 error ("default arguments are only permitted for function parameters");
11863 default_argument = NULL_TREE;
11867 else
11868 default_argument = NULL_TREE;
11870 return make_parameter_declarator (&decl_specifiers,
11871 declarator,
11872 default_argument);
11875 /* Parse a function-body.
11877 function-body:
11878 compound_statement */
11880 static void
11881 cp_parser_function_body (cp_parser *parser)
11883 cp_parser_compound_statement (parser, NULL, false);
11886 /* Parse a ctor-initializer-opt followed by a function-body. Return
11887 true if a ctor-initializer was present. */
11889 static bool
11890 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11892 tree body;
11893 bool ctor_initializer_p;
11895 /* Begin the function body. */
11896 body = begin_function_body ();
11897 /* Parse the optional ctor-initializer. */
11898 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11899 /* Parse the function-body. */
11900 cp_parser_function_body (parser);
11901 /* Finish the function body. */
11902 finish_function_body (body);
11904 return ctor_initializer_p;
11907 /* Parse an initializer.
11909 initializer:
11910 = initializer-clause
11911 ( expression-list )
11913 Returns a expression representing the initializer. If no
11914 initializer is present, NULL_TREE is returned.
11916 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11917 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11918 set to FALSE if there is no initializer present. If there is an
11919 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11920 is set to true; otherwise it is set to false. */
11922 static tree
11923 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11924 bool* non_constant_p)
11926 cp_token *token;
11927 tree init;
11929 /* Peek at the next token. */
11930 token = cp_lexer_peek_token (parser->lexer);
11932 /* Let our caller know whether or not this initializer was
11933 parenthesized. */
11934 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11935 /* Assume that the initializer is constant. */
11936 *non_constant_p = false;
11938 if (token->type == CPP_EQ)
11940 /* Consume the `='. */
11941 cp_lexer_consume_token (parser->lexer);
11942 /* Parse the initializer-clause. */
11943 init = cp_parser_initializer_clause (parser, non_constant_p);
11945 else if (token->type == CPP_OPEN_PAREN)
11946 init = cp_parser_parenthesized_expression_list (parser, false,
11947 non_constant_p);
11948 else
11950 /* Anything else is an error. */
11951 cp_parser_error (parser, "expected initializer");
11952 init = error_mark_node;
11955 return init;
11958 /* Parse an initializer-clause.
11960 initializer-clause:
11961 assignment-expression
11962 { initializer-list , [opt] }
11965 Returns an expression representing the initializer.
11967 If the `assignment-expression' production is used the value
11968 returned is simply a representation for the expression.
11970 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11971 the elements of the initializer-list (or NULL_TREE, if the last
11972 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11973 NULL_TREE. There is no way to detect whether or not the optional
11974 trailing `,' was provided. NON_CONSTANT_P is as for
11975 cp_parser_initializer. */
11977 static tree
11978 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11980 tree initializer;
11982 /* If it is not a `{', then we are looking at an
11983 assignment-expression. */
11984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11986 initializer
11987 = cp_parser_constant_expression (parser,
11988 /*allow_non_constant_p=*/true,
11989 non_constant_p);
11990 if (!*non_constant_p)
11991 initializer = fold_non_dependent_expr (initializer);
11993 else
11995 /* Consume the `{' token. */
11996 cp_lexer_consume_token (parser->lexer);
11997 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11998 initializer = make_node (CONSTRUCTOR);
11999 /* If it's not a `}', then there is a non-trivial initializer. */
12000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12002 /* Parse the initializer list. */
12003 CONSTRUCTOR_ELTS (initializer)
12004 = cp_parser_initializer_list (parser, non_constant_p);
12005 /* A trailing `,' token is allowed. */
12006 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12007 cp_lexer_consume_token (parser->lexer);
12009 /* Now, there should be a trailing `}'. */
12010 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12013 return initializer;
12016 /* Parse an initializer-list.
12018 initializer-list:
12019 initializer-clause
12020 initializer-list , initializer-clause
12022 GNU Extension:
12024 initializer-list:
12025 identifier : initializer-clause
12026 initializer-list, identifier : initializer-clause
12028 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12029 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12030 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12031 as for cp_parser_initializer. */
12033 static tree
12034 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12036 tree initializers = NULL_TREE;
12038 /* Assume all of the expressions are constant. */
12039 *non_constant_p = false;
12041 /* Parse the rest of the list. */
12042 while (true)
12044 cp_token *token;
12045 tree identifier;
12046 tree initializer;
12047 bool clause_non_constant_p;
12049 /* If the next token is an identifier and the following one is a
12050 colon, we are looking at the GNU designated-initializer
12051 syntax. */
12052 if (cp_parser_allow_gnu_extensions_p (parser)
12053 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12054 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12056 /* Consume the identifier. */
12057 identifier = cp_lexer_consume_token (parser->lexer)->value;
12058 /* Consume the `:'. */
12059 cp_lexer_consume_token (parser->lexer);
12061 else
12062 identifier = NULL_TREE;
12064 /* Parse the initializer. */
12065 initializer = cp_parser_initializer_clause (parser,
12066 &clause_non_constant_p);
12067 /* If any clause is non-constant, so is the entire initializer. */
12068 if (clause_non_constant_p)
12069 *non_constant_p = true;
12070 /* Add it to the list. */
12071 initializers = tree_cons (identifier, initializer, initializers);
12073 /* If the next token is not a comma, we have reached the end of
12074 the list. */
12075 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12076 break;
12078 /* Peek at the next token. */
12079 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12080 /* If the next token is a `}', then we're still done. An
12081 initializer-clause can have a trailing `,' after the
12082 initializer-list and before the closing `}'. */
12083 if (token->type == CPP_CLOSE_BRACE)
12084 break;
12086 /* Consume the `,' token. */
12087 cp_lexer_consume_token (parser->lexer);
12090 /* The initializers were built up in reverse order, so we need to
12091 reverse them now. */
12092 return nreverse (initializers);
12095 /* Classes [gram.class] */
12097 /* Parse a class-name.
12099 class-name:
12100 identifier
12101 template-id
12103 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12104 to indicate that names looked up in dependent types should be
12105 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12106 keyword has been used to indicate that the name that appears next
12107 is a template. TYPE_P is true iff the next name should be treated
12108 as class-name, even if it is declared to be some other kind of name
12109 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12110 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
12111 being defined in a class-head.
12113 Returns the TYPE_DECL representing the class. */
12115 static tree
12116 cp_parser_class_name (cp_parser *parser,
12117 bool typename_keyword_p,
12118 bool template_keyword_p,
12119 bool type_p,
12120 bool check_dependency_p,
12121 bool class_head_p,
12122 bool is_declaration)
12124 tree decl;
12125 tree scope;
12126 bool typename_p;
12127 cp_token *token;
12129 /* All class-names start with an identifier. */
12130 token = cp_lexer_peek_token (parser->lexer);
12131 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12133 cp_parser_error (parser, "expected class-name");
12134 return error_mark_node;
12137 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12138 to a template-id, so we save it here. */
12139 scope = parser->scope;
12140 if (scope == error_mark_node)
12141 return error_mark_node;
12143 /* Any name names a type if we're following the `typename' keyword
12144 in a qualified name where the enclosing scope is type-dependent. */
12145 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12146 && dependent_type_p (scope));
12147 /* Handle the common case (an identifier, but not a template-id)
12148 efficiently. */
12149 if (token->type == CPP_NAME
12150 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12152 tree identifier;
12154 /* Look for the identifier. */
12155 identifier = cp_parser_identifier (parser);
12156 /* If the next token isn't an identifier, we are certainly not
12157 looking at a class-name. */
12158 if (identifier == error_mark_node)
12159 decl = error_mark_node;
12160 /* If we know this is a type-name, there's no need to look it
12161 up. */
12162 else if (typename_p)
12163 decl = identifier;
12164 else
12166 /* If the next token is a `::', then the name must be a type
12167 name.
12169 [basic.lookup.qual]
12171 During the lookup for a name preceding the :: scope
12172 resolution operator, object, function, and enumerator
12173 names are ignored. */
12174 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12175 type_p = true;
12176 /* Look up the name. */
12177 decl = cp_parser_lookup_name (parser, identifier,
12178 type_p,
12179 /*is_template=*/false,
12180 /*is_namespace=*/false,
12181 check_dependency_p,
12182 /*ambiguous_p=*/NULL);
12185 else
12187 /* Try a template-id. */
12188 decl = cp_parser_template_id (parser, template_keyword_p,
12189 check_dependency_p,
12190 is_declaration);
12191 if (decl == error_mark_node)
12192 return error_mark_node;
12195 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12197 /* If this is a typename, create a TYPENAME_TYPE. */
12198 if (typename_p && decl != error_mark_node)
12200 decl = make_typename_type (scope, decl, /*complain=*/1);
12201 if (decl != error_mark_node)
12202 decl = TYPE_NAME (decl);
12205 /* Check to see that it is really the name of a class. */
12206 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12207 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12208 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12209 /* Situations like this:
12211 template <typename T> struct A {
12212 typename T::template X<int>::I i;
12215 are problematic. Is `T::template X<int>' a class-name? The
12216 standard does not seem to be definitive, but there is no other
12217 valid interpretation of the following `::'. Therefore, those
12218 names are considered class-names. */
12219 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12220 else if (decl == error_mark_node
12221 || TREE_CODE (decl) != TYPE_DECL
12222 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12224 cp_parser_error (parser, "expected class-name");
12225 return error_mark_node;
12228 return decl;
12231 /* Parse a class-specifier.
12233 class-specifier:
12234 class-head { member-specification [opt] }
12236 Returns the TREE_TYPE representing the class. */
12238 static tree
12239 cp_parser_class_specifier (cp_parser* parser)
12241 cp_token *token;
12242 tree type;
12243 tree attributes = NULL_TREE;
12244 int has_trailing_semicolon;
12245 bool nested_name_specifier_p;
12246 unsigned saved_num_template_parameter_lists;
12247 bool pop_p = false;
12248 tree scope = NULL_TREE;
12250 push_deferring_access_checks (dk_no_deferred);
12252 /* Parse the class-head. */
12253 type = cp_parser_class_head (parser,
12254 &nested_name_specifier_p,
12255 &attributes);
12256 /* If the class-head was a semantic disaster, skip the entire body
12257 of the class. */
12258 if (!type)
12260 cp_parser_skip_to_end_of_block_or_statement (parser);
12261 pop_deferring_access_checks ();
12262 return error_mark_node;
12265 /* Look for the `{'. */
12266 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12268 pop_deferring_access_checks ();
12269 return error_mark_node;
12272 /* Issue an error message if type-definitions are forbidden here. */
12273 cp_parser_check_type_definition (parser);
12274 /* Remember that we are defining one more class. */
12275 ++parser->num_classes_being_defined;
12276 /* Inside the class, surrounding template-parameter-lists do not
12277 apply. */
12278 saved_num_template_parameter_lists
12279 = parser->num_template_parameter_lists;
12280 parser->num_template_parameter_lists = 0;
12282 /* Start the class. */
12283 if (nested_name_specifier_p)
12285 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12286 pop_p = push_scope (scope);
12288 type = begin_class_definition (type);
12290 if (type == error_mark_node)
12291 /* If the type is erroneous, skip the entire body of the class. */
12292 cp_parser_skip_to_closing_brace (parser);
12293 else
12294 /* Parse the member-specification. */
12295 cp_parser_member_specification_opt (parser);
12297 /* Look for the trailing `}'. */
12298 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12299 /* We get better error messages by noticing a common problem: a
12300 missing trailing `;'. */
12301 token = cp_lexer_peek_token (parser->lexer);
12302 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12303 /* Look for trailing attributes to apply to this class. */
12304 if (cp_parser_allow_gnu_extensions_p (parser))
12306 tree sub_attr = cp_parser_attributes_opt (parser);
12307 attributes = chainon (attributes, sub_attr);
12309 if (type != error_mark_node)
12310 type = finish_struct (type, attributes);
12311 if (pop_p)
12312 pop_scope (scope);
12313 /* If this class is not itself within the scope of another class,
12314 then we need to parse the bodies of all of the queued function
12315 definitions. Note that the queued functions defined in a class
12316 are not always processed immediately following the
12317 class-specifier for that class. Consider:
12319 struct A {
12320 struct B { void f() { sizeof (A); } };
12323 If `f' were processed before the processing of `A' were
12324 completed, there would be no way to compute the size of `A'.
12325 Note that the nesting we are interested in here is lexical --
12326 not the semantic nesting given by TYPE_CONTEXT. In particular,
12327 for:
12329 struct A { struct B; };
12330 struct A::B { void f() { } };
12332 there is no need to delay the parsing of `A::B::f'. */
12333 if (--parser->num_classes_being_defined == 0)
12335 tree queue_entry;
12336 tree fn;
12337 tree class_type;
12338 bool pop_p;
12340 /* In a first pass, parse default arguments to the functions.
12341 Then, in a second pass, parse the bodies of the functions.
12342 This two-phased approach handles cases like:
12344 struct S {
12345 void f() { g(); }
12346 void g(int i = 3);
12350 class_type = NULL_TREE;
12351 pop_p = false;
12352 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12353 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12354 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12355 TREE_PURPOSE (parser->unparsed_functions_queues)
12356 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12358 fn = TREE_VALUE (queue_entry);
12359 /* If there are default arguments that have not yet been processed,
12360 take care of them now. */
12361 if (class_type != TREE_PURPOSE (queue_entry))
12363 if (pop_p)
12364 pop_scope (class_type);
12365 class_type = TREE_PURPOSE (queue_entry);
12366 pop_p = push_scope (class_type);
12368 /* Make sure that any template parameters are in scope. */
12369 maybe_begin_member_template_processing (fn);
12370 /* Parse the default argument expressions. */
12371 cp_parser_late_parsing_default_args (parser, fn);
12372 /* Remove any template parameters from the symbol table. */
12373 maybe_end_member_template_processing ();
12375 if (pop_p)
12376 pop_scope (class_type);
12377 /* Now parse the body of the functions. */
12378 for (TREE_VALUE (parser->unparsed_functions_queues)
12379 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12380 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12381 TREE_VALUE (parser->unparsed_functions_queues)
12382 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12384 /* Figure out which function we need to process. */
12385 fn = TREE_VALUE (queue_entry);
12387 /* A hack to prevent garbage collection. */
12388 function_depth++;
12390 /* Parse the function. */
12391 cp_parser_late_parsing_for_member (parser, fn);
12392 function_depth--;
12396 /* Put back any saved access checks. */
12397 pop_deferring_access_checks ();
12399 /* Restore the count of active template-parameter-lists. */
12400 parser->num_template_parameter_lists
12401 = saved_num_template_parameter_lists;
12403 return type;
12406 /* Parse a class-head.
12408 class-head:
12409 class-key identifier [opt] base-clause [opt]
12410 class-key nested-name-specifier identifier base-clause [opt]
12411 class-key nested-name-specifier [opt] template-id
12412 base-clause [opt]
12414 GNU Extensions:
12415 class-key attributes identifier [opt] base-clause [opt]
12416 class-key attributes nested-name-specifier identifier base-clause [opt]
12417 class-key attributes nested-name-specifier [opt] template-id
12418 base-clause [opt]
12420 Returns the TYPE of the indicated class. Sets
12421 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12422 involving a nested-name-specifier was used, and FALSE otherwise.
12424 Returns NULL_TREE if the class-head is syntactically valid, but
12425 semantically invalid in a way that means we should skip the entire
12426 body of the class. */
12428 static tree
12429 cp_parser_class_head (cp_parser* parser,
12430 bool* nested_name_specifier_p,
12431 tree *attributes_p)
12433 tree nested_name_specifier;
12434 enum tag_types class_key;
12435 tree id = NULL_TREE;
12436 tree type = NULL_TREE;
12437 tree attributes;
12438 bool template_id_p = false;
12439 bool qualified_p = false;
12440 bool invalid_nested_name_p = false;
12441 bool invalid_explicit_specialization_p = false;
12442 bool pop_p = false;
12443 unsigned num_templates;
12444 tree bases;
12446 /* Assume no nested-name-specifier will be present. */
12447 *nested_name_specifier_p = false;
12448 /* Assume no template parameter lists will be used in defining the
12449 type. */
12450 num_templates = 0;
12452 /* Look for the class-key. */
12453 class_key = cp_parser_class_key (parser);
12454 if (class_key == none_type)
12455 return error_mark_node;
12457 /* Parse the attributes. */
12458 attributes = cp_parser_attributes_opt (parser);
12460 /* If the next token is `::', that is invalid -- but sometimes
12461 people do try to write:
12463 struct ::S {};
12465 Handle this gracefully by accepting the extra qualifier, and then
12466 issuing an error about it later if this really is a
12467 class-head. If it turns out just to be an elaborated type
12468 specifier, remain silent. */
12469 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12470 qualified_p = true;
12472 push_deferring_access_checks (dk_no_check);
12474 /* Determine the name of the class. Begin by looking for an
12475 optional nested-name-specifier. */
12476 nested_name_specifier
12477 = cp_parser_nested_name_specifier_opt (parser,
12478 /*typename_keyword_p=*/false,
12479 /*check_dependency_p=*/false,
12480 /*type_p=*/false,
12481 /*is_declaration=*/false);
12482 /* If there was a nested-name-specifier, then there *must* be an
12483 identifier. */
12484 if (nested_name_specifier)
12486 /* Although the grammar says `identifier', it really means
12487 `class-name' or `template-name'. You are only allowed to
12488 define a class that has already been declared with this
12489 syntax.
12491 The proposed resolution for Core Issue 180 says that whever
12492 you see `class T::X' you should treat `X' as a type-name.
12494 It is OK to define an inaccessible class; for example:
12496 class A { class B; };
12497 class A::B {};
12499 We do not know if we will see a class-name, or a
12500 template-name. We look for a class-name first, in case the
12501 class-name is a template-id; if we looked for the
12502 template-name first we would stop after the template-name. */
12503 cp_parser_parse_tentatively (parser);
12504 type = cp_parser_class_name (parser,
12505 /*typename_keyword_p=*/false,
12506 /*template_keyword_p=*/false,
12507 /*type_p=*/true,
12508 /*check_dependency_p=*/false,
12509 /*class_head_p=*/true,
12510 /*is_declaration=*/false);
12511 /* If that didn't work, ignore the nested-name-specifier. */
12512 if (!cp_parser_parse_definitely (parser))
12514 invalid_nested_name_p = true;
12515 id = cp_parser_identifier (parser);
12516 if (id == error_mark_node)
12517 id = NULL_TREE;
12519 /* If we could not find a corresponding TYPE, treat this
12520 declaration like an unqualified declaration. */
12521 if (type == error_mark_node)
12522 nested_name_specifier = NULL_TREE;
12523 /* Otherwise, count the number of templates used in TYPE and its
12524 containing scopes. */
12525 else
12527 tree scope;
12529 for (scope = TREE_TYPE (type);
12530 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12531 scope = (TYPE_P (scope)
12532 ? TYPE_CONTEXT (scope)
12533 : DECL_CONTEXT (scope)))
12534 if (TYPE_P (scope)
12535 && CLASS_TYPE_P (scope)
12536 && CLASSTYPE_TEMPLATE_INFO (scope)
12537 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12538 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12539 ++num_templates;
12542 /* Otherwise, the identifier is optional. */
12543 else
12545 /* We don't know whether what comes next is a template-id,
12546 an identifier, or nothing at all. */
12547 cp_parser_parse_tentatively (parser);
12548 /* Check for a template-id. */
12549 id = cp_parser_template_id (parser,
12550 /*template_keyword_p=*/false,
12551 /*check_dependency_p=*/true,
12552 /*is_declaration=*/true);
12553 /* If that didn't work, it could still be an identifier. */
12554 if (!cp_parser_parse_definitely (parser))
12556 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12557 id = cp_parser_identifier (parser);
12558 else
12559 id = NULL_TREE;
12561 else
12563 template_id_p = true;
12564 ++num_templates;
12568 pop_deferring_access_checks ();
12570 if (id)
12571 cp_parser_check_for_invalid_template_id (parser, id);
12573 /* If it's not a `:' or a `{' then we can't really be looking at a
12574 class-head, since a class-head only appears as part of a
12575 class-specifier. We have to detect this situation before calling
12576 xref_tag, since that has irreversible side-effects. */
12577 if (!cp_parser_next_token_starts_class_definition_p (parser))
12579 cp_parser_error (parser, "expected `{' or `:'");
12580 return error_mark_node;
12583 /* At this point, we're going ahead with the class-specifier, even
12584 if some other problem occurs. */
12585 cp_parser_commit_to_tentative_parse (parser);
12586 /* Issue the error about the overly-qualified name now. */
12587 if (qualified_p)
12588 cp_parser_error (parser,
12589 "global qualification of class name is invalid");
12590 else if (invalid_nested_name_p)
12591 cp_parser_error (parser,
12592 "qualified name does not name a class");
12593 else if (nested_name_specifier)
12595 tree scope;
12596 /* Figure out in what scope the declaration is being placed. */
12597 scope = current_scope ();
12598 if (!scope)
12599 scope = current_namespace;
12600 /* If that scope does not contain the scope in which the
12601 class was originally declared, the program is invalid. */
12602 if (scope && !is_ancestor (scope, nested_name_specifier))
12604 error ("declaration of `%D' in `%D' which does not "
12605 "enclose `%D'", type, scope, nested_name_specifier);
12606 type = NULL_TREE;
12607 goto done;
12609 /* [dcl.meaning]
12611 A declarator-id shall not be qualified exception of the
12612 definition of a ... nested class outside of its class
12613 ... [or] a the definition or explicit instantiation of a
12614 class member of a namespace outside of its namespace. */
12615 if (scope == nested_name_specifier)
12617 pedwarn ("extra qualification ignored");
12618 nested_name_specifier = NULL_TREE;
12619 num_templates = 0;
12622 /* An explicit-specialization must be preceded by "template <>". If
12623 it is not, try to recover gracefully. */
12624 if (at_namespace_scope_p ()
12625 && parser->num_template_parameter_lists == 0
12626 && template_id_p)
12628 error ("an explicit specialization must be preceded by 'template <>'");
12629 invalid_explicit_specialization_p = true;
12630 /* Take the same action that would have been taken by
12631 cp_parser_explicit_specialization. */
12632 ++parser->num_template_parameter_lists;
12633 begin_specialization ();
12635 /* There must be no "return" statements between this point and the
12636 end of this function; set "type "to the correct return value and
12637 use "goto done;" to return. */
12638 /* Make sure that the right number of template parameters were
12639 present. */
12640 if (!cp_parser_check_template_parameters (parser, num_templates))
12642 /* If something went wrong, there is no point in even trying to
12643 process the class-definition. */
12644 type = NULL_TREE;
12645 goto done;
12648 /* Look up the type. */
12649 if (template_id_p)
12651 type = TREE_TYPE (id);
12652 maybe_process_partial_specialization (type);
12654 else if (!nested_name_specifier)
12656 /* If the class was unnamed, create a dummy name. */
12657 if (!id)
12658 id = make_anon_name ();
12659 type = xref_tag (class_key, id, /*globalize=*/false,
12660 parser->num_template_parameter_lists);
12662 else
12664 tree class_type;
12665 bool pop_p = false;
12667 /* Given:
12669 template <typename T> struct S { struct T };
12670 template <typename T> struct S<T>::T { };
12672 we will get a TYPENAME_TYPE when processing the definition of
12673 `S::T'. We need to resolve it to the actual type before we
12674 try to define it. */
12675 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12677 class_type = resolve_typename_type (TREE_TYPE (type),
12678 /*only_current_p=*/false);
12679 if (class_type != error_mark_node)
12680 type = TYPE_NAME (class_type);
12681 else
12683 cp_parser_error (parser, "could not resolve typename type");
12684 type = error_mark_node;
12688 maybe_process_partial_specialization (TREE_TYPE (type));
12689 class_type = current_class_type;
12690 /* Enter the scope indicated by the nested-name-specifier. */
12691 if (nested_name_specifier)
12692 pop_p = push_scope (nested_name_specifier);
12693 /* Get the canonical version of this type. */
12694 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12695 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12696 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12697 type = push_template_decl (type);
12698 type = TREE_TYPE (type);
12699 if (nested_name_specifier)
12701 *nested_name_specifier_p = true;
12702 if (pop_p)
12703 pop_scope (nested_name_specifier);
12706 /* Indicate whether this class was declared as a `class' or as a
12707 `struct'. */
12708 if (TREE_CODE (type) == RECORD_TYPE)
12709 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12710 cp_parser_check_class_key (class_key, type);
12712 /* Enter the scope containing the class; the names of base classes
12713 should be looked up in that context. For example, given:
12715 struct A { struct B {}; struct C; };
12716 struct A::C : B {};
12718 is valid. */
12719 if (nested_name_specifier)
12720 pop_p = push_scope (nested_name_specifier);
12722 bases = NULL_TREE;
12724 /* Get the list of base-classes, if there is one. */
12725 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12726 bases = cp_parser_base_clause (parser);
12728 /* Process the base classes. */
12729 xref_basetypes (type, bases);
12731 /* Leave the scope given by the nested-name-specifier. We will
12732 enter the class scope itself while processing the members. */
12733 if (pop_p)
12734 pop_scope (nested_name_specifier);
12736 done:
12737 if (invalid_explicit_specialization_p)
12739 end_specialization ();
12740 --parser->num_template_parameter_lists;
12742 *attributes_p = attributes;
12743 return type;
12746 /* Parse a class-key.
12748 class-key:
12749 class
12750 struct
12751 union
12753 Returns the kind of class-key specified, or none_type to indicate
12754 error. */
12756 static enum tag_types
12757 cp_parser_class_key (cp_parser* parser)
12759 cp_token *token;
12760 enum tag_types tag_type;
12762 /* Look for the class-key. */
12763 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12764 if (!token)
12765 return none_type;
12767 /* Check to see if the TOKEN is a class-key. */
12768 tag_type = cp_parser_token_is_class_key (token);
12769 if (!tag_type)
12770 cp_parser_error (parser, "expected class-key");
12771 return tag_type;
12774 /* Parse an (optional) member-specification.
12776 member-specification:
12777 member-declaration member-specification [opt]
12778 access-specifier : member-specification [opt] */
12780 static void
12781 cp_parser_member_specification_opt (cp_parser* parser)
12783 while (true)
12785 cp_token *token;
12786 enum rid keyword;
12788 /* Peek at the next token. */
12789 token = cp_lexer_peek_token (parser->lexer);
12790 /* If it's a `}', or EOF then we've seen all the members. */
12791 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12792 break;
12794 /* See if this token is a keyword. */
12795 keyword = token->keyword;
12796 switch (keyword)
12798 case RID_PUBLIC:
12799 case RID_PROTECTED:
12800 case RID_PRIVATE:
12801 /* Consume the access-specifier. */
12802 cp_lexer_consume_token (parser->lexer);
12803 /* Remember which access-specifier is active. */
12804 current_access_specifier = token->value;
12805 /* Look for the `:'. */
12806 cp_parser_require (parser, CPP_COLON, "`:'");
12807 break;
12809 default:
12810 /* Otherwise, the next construction must be a
12811 member-declaration. */
12812 cp_parser_member_declaration (parser);
12817 /* Parse a member-declaration.
12819 member-declaration:
12820 decl-specifier-seq [opt] member-declarator-list [opt] ;
12821 function-definition ; [opt]
12822 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12823 using-declaration
12824 template-declaration
12826 member-declarator-list:
12827 member-declarator
12828 member-declarator-list , member-declarator
12830 member-declarator:
12831 declarator pure-specifier [opt]
12832 declarator constant-initializer [opt]
12833 identifier [opt] : constant-expression
12835 GNU Extensions:
12837 member-declaration:
12838 __extension__ member-declaration
12840 member-declarator:
12841 declarator attributes [opt] pure-specifier [opt]
12842 declarator attributes [opt] constant-initializer [opt]
12843 identifier [opt] attributes [opt] : constant-expression */
12845 static void
12846 cp_parser_member_declaration (cp_parser* parser)
12848 cp_decl_specifier_seq decl_specifiers;
12849 tree prefix_attributes;
12850 tree decl;
12851 int declares_class_or_enum;
12852 bool friend_p;
12853 cp_token *token;
12854 int saved_pedantic;
12856 /* Check for the `__extension__' keyword. */
12857 if (cp_parser_extension_opt (parser, &saved_pedantic))
12859 /* Recurse. */
12860 cp_parser_member_declaration (parser);
12861 /* Restore the old value of the PEDANTIC flag. */
12862 pedantic = saved_pedantic;
12864 return;
12867 /* Check for a template-declaration. */
12868 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12870 /* Parse the template-declaration. */
12871 cp_parser_template_declaration (parser, /*member_p=*/true);
12873 return;
12876 /* Check for a using-declaration. */
12877 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12879 /* Parse the using-declaration. */
12880 cp_parser_using_declaration (parser);
12882 return;
12885 /* Parse the decl-specifier-seq. */
12886 cp_parser_decl_specifier_seq (parser,
12887 CP_PARSER_FLAGS_OPTIONAL,
12888 &decl_specifiers,
12889 &declares_class_or_enum);
12890 prefix_attributes = decl_specifiers.attributes;
12891 decl_specifiers.attributes = NULL_TREE;
12892 /* Check for an invalid type-name. */
12893 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12894 return;
12895 /* If there is no declarator, then the decl-specifier-seq should
12896 specify a type. */
12897 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12899 /* If there was no decl-specifier-seq, and the next token is a
12900 `;', then we have something like:
12902 struct S { ; };
12904 [class.mem]
12906 Each member-declaration shall declare at least one member
12907 name of the class. */
12908 if (!decl_specifiers.any_specifiers_p)
12910 if (pedantic)
12911 pedwarn ("extra semicolon");
12913 else
12915 tree type;
12917 /* See if this declaration is a friend. */
12918 friend_p = cp_parser_friend_p (&decl_specifiers);
12919 /* If there were decl-specifiers, check to see if there was
12920 a class-declaration. */
12921 type = check_tag_decl (&decl_specifiers);
12922 /* Nested classes have already been added to the class, but
12923 a `friend' needs to be explicitly registered. */
12924 if (friend_p)
12926 /* If the `friend' keyword was present, the friend must
12927 be introduced with a class-key. */
12928 if (!declares_class_or_enum)
12929 error ("a class-key must be used when declaring a friend");
12930 /* In this case:
12932 template <typename T> struct A {
12933 friend struct A<T>::B;
12936 A<T>::B will be represented by a TYPENAME_TYPE, and
12937 therefore not recognized by check_tag_decl. */
12938 if (!type
12939 && decl_specifiers.type
12940 && TYPE_P (decl_specifiers.type))
12941 type = decl_specifiers.type;
12942 if (!type || !TYPE_P (type))
12943 error ("friend declaration does not name a class or "
12944 "function");
12945 else
12946 make_friend_class (current_class_type, type,
12947 /*complain=*/true);
12949 /* If there is no TYPE, an error message will already have
12950 been issued. */
12951 else if (!type || type == error_mark_node)
12953 /* An anonymous aggregate has to be handled specially; such
12954 a declaration really declares a data member (with a
12955 particular type), as opposed to a nested class. */
12956 else if (ANON_AGGR_TYPE_P (type))
12958 /* Remove constructors and such from TYPE, now that we
12959 know it is an anonymous aggregate. */
12960 fixup_anonymous_aggr (type);
12961 /* And make the corresponding data member. */
12962 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12963 /* Add it to the class. */
12964 finish_member_declaration (decl);
12966 else
12967 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12970 else
12972 /* See if these declarations will be friends. */
12973 friend_p = cp_parser_friend_p (&decl_specifiers);
12975 /* Keep going until we hit the `;' at the end of the
12976 declaration. */
12977 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12979 tree attributes = NULL_TREE;
12980 tree first_attribute;
12982 /* Peek at the next token. */
12983 token = cp_lexer_peek_token (parser->lexer);
12985 /* Check for a bitfield declaration. */
12986 if (token->type == CPP_COLON
12987 || (token->type == CPP_NAME
12988 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12989 == CPP_COLON))
12991 tree identifier;
12992 tree width;
12994 /* Get the name of the bitfield. Note that we cannot just
12995 check TOKEN here because it may have been invalidated by
12996 the call to cp_lexer_peek_nth_token above. */
12997 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12998 identifier = cp_parser_identifier (parser);
12999 else
13000 identifier = NULL_TREE;
13002 /* Consume the `:' token. */
13003 cp_lexer_consume_token (parser->lexer);
13004 /* Get the width of the bitfield. */
13005 width
13006 = cp_parser_constant_expression (parser,
13007 /*allow_non_constant=*/false,
13008 NULL);
13010 /* Look for attributes that apply to the bitfield. */
13011 attributes = cp_parser_attributes_opt (parser);
13012 /* Remember which attributes are prefix attributes and
13013 which are not. */
13014 first_attribute = attributes;
13015 /* Combine the attributes. */
13016 attributes = chainon (prefix_attributes, attributes);
13018 /* Create the bitfield declaration. */
13019 decl = grokbitfield (identifier
13020 ? make_id_declarator (identifier)
13021 : NULL,
13022 &decl_specifiers,
13023 width);
13024 /* Apply the attributes. */
13025 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13027 else
13029 cp_declarator *declarator;
13030 tree initializer;
13031 tree asm_specification;
13032 int ctor_dtor_or_conv_p;
13034 /* Parse the declarator. */
13035 declarator
13036 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13037 &ctor_dtor_or_conv_p,
13038 /*parenthesized_p=*/NULL);
13040 /* If something went wrong parsing the declarator, make sure
13041 that we at least consume some tokens. */
13042 if (declarator == cp_error_declarator)
13044 /* Skip to the end of the statement. */
13045 cp_parser_skip_to_end_of_statement (parser);
13046 /* If the next token is not a semicolon, that is
13047 probably because we just skipped over the body of
13048 a function. So, we consume a semicolon if
13049 present, but do not issue an error message if it
13050 is not present. */
13051 if (cp_lexer_next_token_is (parser->lexer,
13052 CPP_SEMICOLON))
13053 cp_lexer_consume_token (parser->lexer);
13054 return;
13057 cp_parser_check_for_definition_in_return_type
13058 (declarator, declares_class_or_enum);
13060 /* Look for an asm-specification. */
13061 asm_specification = cp_parser_asm_specification_opt (parser);
13062 /* Look for attributes that apply to the declaration. */
13063 attributes = cp_parser_attributes_opt (parser);
13064 /* Remember which attributes are prefix attributes and
13065 which are not. */
13066 first_attribute = attributes;
13067 /* Combine the attributes. */
13068 attributes = chainon (prefix_attributes, attributes);
13070 /* If it's an `=', then we have a constant-initializer or a
13071 pure-specifier. It is not correct to parse the
13072 initializer before registering the member declaration
13073 since the member declaration should be in scope while
13074 its initializer is processed. However, the rest of the
13075 front end does not yet provide an interface that allows
13076 us to handle this correctly. */
13077 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13079 /* In [class.mem]:
13081 A pure-specifier shall be used only in the declaration of
13082 a virtual function.
13084 A member-declarator can contain a constant-initializer
13085 only if it declares a static member of integral or
13086 enumeration type.
13088 Therefore, if the DECLARATOR is for a function, we look
13089 for a pure-specifier; otherwise, we look for a
13090 constant-initializer. When we call `grokfield', it will
13091 perform more stringent semantics checks. */
13092 if (declarator->kind == cdk_function)
13093 initializer = cp_parser_pure_specifier (parser);
13094 else
13095 /* Parse the initializer. */
13096 initializer = cp_parser_constant_initializer (parser);
13098 /* Otherwise, there is no initializer. */
13099 else
13100 initializer = NULL_TREE;
13102 /* See if we are probably looking at a function
13103 definition. We are certainly not looking at at a
13104 member-declarator. Calling `grokfield' has
13105 side-effects, so we must not do it unless we are sure
13106 that we are looking at a member-declarator. */
13107 if (cp_parser_token_starts_function_definition_p
13108 (cp_lexer_peek_token (parser->lexer)))
13110 /* The grammar does not allow a pure-specifier to be
13111 used when a member function is defined. (It is
13112 possible that this fact is an oversight in the
13113 standard, since a pure function may be defined
13114 outside of the class-specifier. */
13115 if (initializer)
13116 error ("pure-specifier on function-definition");
13117 decl = cp_parser_save_member_function_body (parser,
13118 &decl_specifiers,
13119 declarator,
13120 attributes);
13121 /* If the member was not a friend, declare it here. */
13122 if (!friend_p)
13123 finish_member_declaration (decl);
13124 /* Peek at the next token. */
13125 token = cp_lexer_peek_token (parser->lexer);
13126 /* If the next token is a semicolon, consume it. */
13127 if (token->type == CPP_SEMICOLON)
13128 cp_lexer_consume_token (parser->lexer);
13129 return;
13131 else
13133 /* Create the declaration. */
13134 decl = grokfield (declarator, &decl_specifiers,
13135 initializer, asm_specification,
13136 attributes);
13137 /* Any initialization must have been from a
13138 constant-expression. */
13139 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13140 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13144 /* Reset PREFIX_ATTRIBUTES. */
13145 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13146 attributes = TREE_CHAIN (attributes);
13147 if (attributes)
13148 TREE_CHAIN (attributes) = NULL_TREE;
13150 /* If there is any qualification still in effect, clear it
13151 now; we will be starting fresh with the next declarator. */
13152 parser->scope = NULL_TREE;
13153 parser->qualifying_scope = NULL_TREE;
13154 parser->object_scope = NULL_TREE;
13155 /* If it's a `,', then there are more declarators. */
13156 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13157 cp_lexer_consume_token (parser->lexer);
13158 /* If the next token isn't a `;', then we have a parse error. */
13159 else if (cp_lexer_next_token_is_not (parser->lexer,
13160 CPP_SEMICOLON))
13162 cp_parser_error (parser, "expected `;'");
13163 /* Skip tokens until we find a `;'. */
13164 cp_parser_skip_to_end_of_statement (parser);
13166 break;
13169 if (decl)
13171 /* Add DECL to the list of members. */
13172 if (!friend_p)
13173 finish_member_declaration (decl);
13175 if (TREE_CODE (decl) == FUNCTION_DECL)
13176 cp_parser_save_default_args (parser, decl);
13181 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13184 /* Parse a pure-specifier.
13186 pure-specifier:
13189 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13190 Otherwise, ERROR_MARK_NODE is returned. */
13192 static tree
13193 cp_parser_pure_specifier (cp_parser* parser)
13195 cp_token *token;
13197 /* Look for the `=' token. */
13198 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13199 return error_mark_node;
13200 /* Look for the `0' token. */
13201 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13202 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13203 to get information from the lexer about how the number was
13204 spelled in order to fix this problem. */
13205 if (!token || !integer_zerop (token->value))
13206 return error_mark_node;
13208 return integer_zero_node;
13211 /* Parse a constant-initializer.
13213 constant-initializer:
13214 = constant-expression
13216 Returns a representation of the constant-expression. */
13218 static tree
13219 cp_parser_constant_initializer (cp_parser* parser)
13221 /* Look for the `=' token. */
13222 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13223 return error_mark_node;
13225 /* It is invalid to write:
13227 struct S { static const int i = { 7 }; };
13230 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13232 cp_parser_error (parser,
13233 "a brace-enclosed initializer is not allowed here");
13234 /* Consume the opening brace. */
13235 cp_lexer_consume_token (parser->lexer);
13236 /* Skip the initializer. */
13237 cp_parser_skip_to_closing_brace (parser);
13238 /* Look for the trailing `}'. */
13239 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13241 return error_mark_node;
13244 return cp_parser_constant_expression (parser,
13245 /*allow_non_constant=*/false,
13246 NULL);
13249 /* Derived classes [gram.class.derived] */
13251 /* Parse a base-clause.
13253 base-clause:
13254 : base-specifier-list
13256 base-specifier-list:
13257 base-specifier
13258 base-specifier-list , base-specifier
13260 Returns a TREE_LIST representing the base-classes, in the order in
13261 which they were declared. The representation of each node is as
13262 described by cp_parser_base_specifier.
13264 In the case that no bases are specified, this function will return
13265 NULL_TREE, not ERROR_MARK_NODE. */
13267 static tree
13268 cp_parser_base_clause (cp_parser* parser)
13270 tree bases = NULL_TREE;
13272 /* Look for the `:' that begins the list. */
13273 cp_parser_require (parser, CPP_COLON, "`:'");
13275 /* Scan the base-specifier-list. */
13276 while (true)
13278 cp_token *token;
13279 tree base;
13281 /* Look for the base-specifier. */
13282 base = cp_parser_base_specifier (parser);
13283 /* Add BASE to the front of the list. */
13284 if (base != error_mark_node)
13286 TREE_CHAIN (base) = bases;
13287 bases = base;
13289 /* Peek at the next token. */
13290 token = cp_lexer_peek_token (parser->lexer);
13291 /* If it's not a comma, then the list is complete. */
13292 if (token->type != CPP_COMMA)
13293 break;
13294 /* Consume the `,'. */
13295 cp_lexer_consume_token (parser->lexer);
13298 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13299 base class had a qualified name. However, the next name that
13300 appears is certainly not qualified. */
13301 parser->scope = NULL_TREE;
13302 parser->qualifying_scope = NULL_TREE;
13303 parser->object_scope = NULL_TREE;
13305 return nreverse (bases);
13308 /* Parse a base-specifier.
13310 base-specifier:
13311 :: [opt] nested-name-specifier [opt] class-name
13312 virtual access-specifier [opt] :: [opt] nested-name-specifier
13313 [opt] class-name
13314 access-specifier virtual [opt] :: [opt] nested-name-specifier
13315 [opt] class-name
13317 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13318 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13319 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13320 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13322 static tree
13323 cp_parser_base_specifier (cp_parser* parser)
13325 cp_token *token;
13326 bool done = false;
13327 bool virtual_p = false;
13328 bool duplicate_virtual_error_issued_p = false;
13329 bool duplicate_access_error_issued_p = false;
13330 bool class_scope_p, template_p;
13331 tree access = access_default_node;
13332 tree type;
13334 /* Process the optional `virtual' and `access-specifier'. */
13335 while (!done)
13337 /* Peek at the next token. */
13338 token = cp_lexer_peek_token (parser->lexer);
13339 /* Process `virtual'. */
13340 switch (token->keyword)
13342 case RID_VIRTUAL:
13343 /* If `virtual' appears more than once, issue an error. */
13344 if (virtual_p && !duplicate_virtual_error_issued_p)
13346 cp_parser_error (parser,
13347 "`virtual' specified more than once in base-specified");
13348 duplicate_virtual_error_issued_p = true;
13351 virtual_p = true;
13353 /* Consume the `virtual' token. */
13354 cp_lexer_consume_token (parser->lexer);
13356 break;
13358 case RID_PUBLIC:
13359 case RID_PROTECTED:
13360 case RID_PRIVATE:
13361 /* If more than one access specifier appears, issue an
13362 error. */
13363 if (access != access_default_node
13364 && !duplicate_access_error_issued_p)
13366 cp_parser_error (parser,
13367 "more than one access specifier in base-specified");
13368 duplicate_access_error_issued_p = true;
13371 access = ridpointers[(int) token->keyword];
13373 /* Consume the access-specifier. */
13374 cp_lexer_consume_token (parser->lexer);
13376 break;
13378 default:
13379 done = true;
13380 break;
13383 /* It is not uncommon to see programs mechanically, erroneously, use
13384 the 'typename' keyword to denote (dependent) qualified types
13385 as base classes. */
13386 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13388 if (!processing_template_decl)
13389 error ("keyword `typename' not allowed outside of templates");
13390 else
13391 error ("keyword `typename' not allowed in this context "
13392 "(the base class is implicitly a type)");
13393 cp_lexer_consume_token (parser->lexer);
13396 /* Look for the optional `::' operator. */
13397 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13398 /* Look for the nested-name-specifier. The simplest way to
13399 implement:
13401 [temp.res]
13403 The keyword `typename' is not permitted in a base-specifier or
13404 mem-initializer; in these contexts a qualified name that
13405 depends on a template-parameter is implicitly assumed to be a
13406 type name.
13408 is to pretend that we have seen the `typename' keyword at this
13409 point. */
13410 cp_parser_nested_name_specifier_opt (parser,
13411 /*typename_keyword_p=*/true,
13412 /*check_dependency_p=*/true,
13413 /*type_p=*/true,
13414 /*is_declaration=*/true);
13415 /* If the base class is given by a qualified name, assume that names
13416 we see are type names or templates, as appropriate. */
13417 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13418 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13420 /* Finally, look for the class-name. */
13421 type = cp_parser_class_name (parser,
13422 class_scope_p,
13423 template_p,
13424 /*type_p=*/true,
13425 /*check_dependency_p=*/true,
13426 /*class_head_p=*/false,
13427 /*is_declaration=*/true);
13429 if (type == error_mark_node)
13430 return error_mark_node;
13432 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13435 /* Exception handling [gram.exception] */
13437 /* Parse an (optional) exception-specification.
13439 exception-specification:
13440 throw ( type-id-list [opt] )
13442 Returns a TREE_LIST representing the exception-specification. The
13443 TREE_VALUE of each node is a type. */
13445 static tree
13446 cp_parser_exception_specification_opt (cp_parser* parser)
13448 cp_token *token;
13449 tree type_id_list;
13451 /* Peek at the next token. */
13452 token = cp_lexer_peek_token (parser->lexer);
13453 /* If it's not `throw', then there's no exception-specification. */
13454 if (!cp_parser_is_keyword (token, RID_THROW))
13455 return NULL_TREE;
13457 /* Consume the `throw'. */
13458 cp_lexer_consume_token (parser->lexer);
13460 /* Look for the `('. */
13461 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13463 /* Peek at the next token. */
13464 token = cp_lexer_peek_token (parser->lexer);
13465 /* If it's not a `)', then there is a type-id-list. */
13466 if (token->type != CPP_CLOSE_PAREN)
13468 const char *saved_message;
13470 /* Types may not be defined in an exception-specification. */
13471 saved_message = parser->type_definition_forbidden_message;
13472 parser->type_definition_forbidden_message
13473 = "types may not be defined in an exception-specification";
13474 /* Parse the type-id-list. */
13475 type_id_list = cp_parser_type_id_list (parser);
13476 /* Restore the saved message. */
13477 parser->type_definition_forbidden_message = saved_message;
13479 else
13480 type_id_list = empty_except_spec;
13482 /* Look for the `)'. */
13483 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13485 return type_id_list;
13488 /* Parse an (optional) type-id-list.
13490 type-id-list:
13491 type-id
13492 type-id-list , type-id
13494 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13495 in the order that the types were presented. */
13497 static tree
13498 cp_parser_type_id_list (cp_parser* parser)
13500 tree types = NULL_TREE;
13502 while (true)
13504 cp_token *token;
13505 tree type;
13507 /* Get the next type-id. */
13508 type = cp_parser_type_id (parser);
13509 /* Add it to the list. */
13510 types = add_exception_specifier (types, type, /*complain=*/1);
13511 /* Peek at the next token. */
13512 token = cp_lexer_peek_token (parser->lexer);
13513 /* If it is not a `,', we are done. */
13514 if (token->type != CPP_COMMA)
13515 break;
13516 /* Consume the `,'. */
13517 cp_lexer_consume_token (parser->lexer);
13520 return nreverse (types);
13523 /* Parse a try-block.
13525 try-block:
13526 try compound-statement handler-seq */
13528 static tree
13529 cp_parser_try_block (cp_parser* parser)
13531 tree try_block;
13533 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13534 try_block = begin_try_block ();
13535 cp_parser_compound_statement (parser, NULL, true);
13536 finish_try_block (try_block);
13537 cp_parser_handler_seq (parser);
13538 finish_handler_sequence (try_block);
13540 return try_block;
13543 /* Parse a function-try-block.
13545 function-try-block:
13546 try ctor-initializer [opt] function-body handler-seq */
13548 static bool
13549 cp_parser_function_try_block (cp_parser* parser)
13551 tree try_block;
13552 bool ctor_initializer_p;
13554 /* Look for the `try' keyword. */
13555 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13556 return false;
13557 /* Let the rest of the front-end know where we are. */
13558 try_block = begin_function_try_block ();
13559 /* Parse the function-body. */
13560 ctor_initializer_p
13561 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13562 /* We're done with the `try' part. */
13563 finish_function_try_block (try_block);
13564 /* Parse the handlers. */
13565 cp_parser_handler_seq (parser);
13566 /* We're done with the handlers. */
13567 finish_function_handler_sequence (try_block);
13569 return ctor_initializer_p;
13572 /* Parse a handler-seq.
13574 handler-seq:
13575 handler handler-seq [opt] */
13577 static void
13578 cp_parser_handler_seq (cp_parser* parser)
13580 while (true)
13582 cp_token *token;
13584 /* Parse the handler. */
13585 cp_parser_handler (parser);
13586 /* Peek at the next token. */
13587 token = cp_lexer_peek_token (parser->lexer);
13588 /* If it's not `catch' then there are no more handlers. */
13589 if (!cp_parser_is_keyword (token, RID_CATCH))
13590 break;
13594 /* Parse a handler.
13596 handler:
13597 catch ( exception-declaration ) compound-statement */
13599 static void
13600 cp_parser_handler (cp_parser* parser)
13602 tree handler;
13603 tree declaration;
13605 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13606 handler = begin_handler ();
13607 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13608 declaration = cp_parser_exception_declaration (parser);
13609 finish_handler_parms (declaration, handler);
13610 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13611 cp_parser_compound_statement (parser, NULL, false);
13612 finish_handler (handler);
13615 /* Parse an exception-declaration.
13617 exception-declaration:
13618 type-specifier-seq declarator
13619 type-specifier-seq abstract-declarator
13620 type-specifier-seq
13623 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13624 ellipsis variant is used. */
13626 static tree
13627 cp_parser_exception_declaration (cp_parser* parser)
13629 tree decl;
13630 cp_decl_specifier_seq type_specifiers;
13631 cp_declarator *declarator;
13632 const char *saved_message;
13634 /* If it's an ellipsis, it's easy to handle. */
13635 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13637 /* Consume the `...' token. */
13638 cp_lexer_consume_token (parser->lexer);
13639 return NULL_TREE;
13642 /* Types may not be defined in exception-declarations. */
13643 saved_message = parser->type_definition_forbidden_message;
13644 parser->type_definition_forbidden_message
13645 = "types may not be defined in exception-declarations";
13647 /* Parse the type-specifier-seq. */
13648 cp_parser_type_specifier_seq (parser, &type_specifiers);
13649 /* If it's a `)', then there is no declarator. */
13650 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13651 declarator = NULL;
13652 else
13653 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13654 /*ctor_dtor_or_conv_p=*/NULL,
13655 /*parenthesized_p=*/NULL);
13657 /* Restore the saved message. */
13658 parser->type_definition_forbidden_message = saved_message;
13660 if (type_specifiers.any_specifiers_p)
13662 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13663 if (decl == NULL_TREE)
13664 error ("invalid catch parameter");
13666 else
13667 decl = NULL_TREE;
13669 return decl;
13672 /* Parse a throw-expression.
13674 throw-expression:
13675 throw assignment-expression [opt]
13677 Returns a THROW_EXPR representing the throw-expression. */
13679 static tree
13680 cp_parser_throw_expression (cp_parser* parser)
13682 tree expression;
13683 cp_token* token;
13685 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13686 token = cp_lexer_peek_token (parser->lexer);
13687 /* Figure out whether or not there is an assignment-expression
13688 following the "throw" keyword. */
13689 if (token->type == CPP_COMMA
13690 || token->type == CPP_SEMICOLON
13691 || token->type == CPP_CLOSE_PAREN
13692 || token->type == CPP_CLOSE_SQUARE
13693 || token->type == CPP_CLOSE_BRACE
13694 || token->type == CPP_COLON)
13695 expression = NULL_TREE;
13696 else
13697 expression = cp_parser_assignment_expression (parser);
13699 return build_throw (expression);
13702 /* GNU Extensions */
13704 /* Parse an (optional) asm-specification.
13706 asm-specification:
13707 asm ( string-literal )
13709 If the asm-specification is present, returns a STRING_CST
13710 corresponding to the string-literal. Otherwise, returns
13711 NULL_TREE. */
13713 static tree
13714 cp_parser_asm_specification_opt (cp_parser* parser)
13716 cp_token *token;
13717 tree asm_specification;
13719 /* Peek at the next token. */
13720 token = cp_lexer_peek_token (parser->lexer);
13721 /* If the next token isn't the `asm' keyword, then there's no
13722 asm-specification. */
13723 if (!cp_parser_is_keyword (token, RID_ASM))
13724 return NULL_TREE;
13726 /* Consume the `asm' token. */
13727 cp_lexer_consume_token (parser->lexer);
13728 /* Look for the `('. */
13729 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13731 /* Look for the string-literal. */
13732 asm_specification = cp_parser_string_literal (parser, false, false);
13734 /* Look for the `)'. */
13735 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13737 return asm_specification;
13740 /* Parse an asm-operand-list.
13742 asm-operand-list:
13743 asm-operand
13744 asm-operand-list , asm-operand
13746 asm-operand:
13747 string-literal ( expression )
13748 [ string-literal ] string-literal ( expression )
13750 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13751 each node is the expression. The TREE_PURPOSE is itself a
13752 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13753 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13754 is a STRING_CST for the string literal before the parenthesis. */
13756 static tree
13757 cp_parser_asm_operand_list (cp_parser* parser)
13759 tree asm_operands = NULL_TREE;
13761 while (true)
13763 tree string_literal;
13764 tree expression;
13765 tree name;
13767 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13769 /* Consume the `[' token. */
13770 cp_lexer_consume_token (parser->lexer);
13771 /* Read the operand name. */
13772 name = cp_parser_identifier (parser);
13773 if (name != error_mark_node)
13774 name = build_string (IDENTIFIER_LENGTH (name),
13775 IDENTIFIER_POINTER (name));
13776 /* Look for the closing `]'. */
13777 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13779 else
13780 name = NULL_TREE;
13781 /* Look for the string-literal. */
13782 string_literal = cp_parser_string_literal (parser, false, false);
13784 /* Look for the `('. */
13785 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13786 /* Parse the expression. */
13787 expression = cp_parser_expression (parser);
13788 /* Look for the `)'. */
13789 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13791 /* Add this operand to the list. */
13792 asm_operands = tree_cons (build_tree_list (name, string_literal),
13793 expression,
13794 asm_operands);
13795 /* If the next token is not a `,', there are no more
13796 operands. */
13797 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13798 break;
13799 /* Consume the `,'. */
13800 cp_lexer_consume_token (parser->lexer);
13803 return nreverse (asm_operands);
13806 /* Parse an asm-clobber-list.
13808 asm-clobber-list:
13809 string-literal
13810 asm-clobber-list , string-literal
13812 Returns a TREE_LIST, indicating the clobbers in the order that they
13813 appeared. The TREE_VALUE of each node is a STRING_CST. */
13815 static tree
13816 cp_parser_asm_clobber_list (cp_parser* parser)
13818 tree clobbers = NULL_TREE;
13820 while (true)
13822 tree string_literal;
13824 /* Look for the string literal. */
13825 string_literal = cp_parser_string_literal (parser, false, false);
13826 /* Add it to the list. */
13827 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13828 /* If the next token is not a `,', then the list is
13829 complete. */
13830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13831 break;
13832 /* Consume the `,' token. */
13833 cp_lexer_consume_token (parser->lexer);
13836 return clobbers;
13839 /* Parse an (optional) series of attributes.
13841 attributes:
13842 attributes attribute
13844 attribute:
13845 __attribute__ (( attribute-list [opt] ))
13847 The return value is as for cp_parser_attribute_list. */
13849 static tree
13850 cp_parser_attributes_opt (cp_parser* parser)
13852 tree attributes = NULL_TREE;
13854 while (true)
13856 cp_token *token;
13857 tree attribute_list;
13859 /* Peek at the next token. */
13860 token = cp_lexer_peek_token (parser->lexer);
13861 /* If it's not `__attribute__', then we're done. */
13862 if (token->keyword != RID_ATTRIBUTE)
13863 break;
13865 /* Consume the `__attribute__' keyword. */
13866 cp_lexer_consume_token (parser->lexer);
13867 /* Look for the two `(' tokens. */
13868 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13869 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13871 /* Peek at the next token. */
13872 token = cp_lexer_peek_token (parser->lexer);
13873 if (token->type != CPP_CLOSE_PAREN)
13874 /* Parse the attribute-list. */
13875 attribute_list = cp_parser_attribute_list (parser);
13876 else
13877 /* If the next token is a `)', then there is no attribute
13878 list. */
13879 attribute_list = NULL;
13881 /* Look for the two `)' tokens. */
13882 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13883 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13885 /* Add these new attributes to the list. */
13886 attributes = chainon (attributes, attribute_list);
13889 return attributes;
13892 /* Parse an attribute-list.
13894 attribute-list:
13895 attribute
13896 attribute-list , attribute
13898 attribute:
13899 identifier
13900 identifier ( identifier )
13901 identifier ( identifier , expression-list )
13902 identifier ( expression-list )
13904 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13905 TREE_PURPOSE of each node is the identifier indicating which
13906 attribute is in use. The TREE_VALUE represents the arguments, if
13907 any. */
13909 static tree
13910 cp_parser_attribute_list (cp_parser* parser)
13912 tree attribute_list = NULL_TREE;
13913 bool save_translate_strings_p = parser->translate_strings_p;
13915 parser->translate_strings_p = false;
13916 while (true)
13918 cp_token *token;
13919 tree identifier;
13920 tree attribute;
13922 /* Look for the identifier. We also allow keywords here; for
13923 example `__attribute__ ((const))' is legal. */
13924 token = cp_lexer_peek_token (parser->lexer);
13925 if (token->type != CPP_NAME
13926 && token->type != CPP_KEYWORD)
13927 return error_mark_node;
13928 /* Consume the token. */
13929 token = cp_lexer_consume_token (parser->lexer);
13931 /* Save away the identifier that indicates which attribute this is. */
13932 identifier = token->value;
13933 attribute = build_tree_list (identifier, NULL_TREE);
13935 /* Peek at the next token. */
13936 token = cp_lexer_peek_token (parser->lexer);
13937 /* If it's an `(', then parse the attribute arguments. */
13938 if (token->type == CPP_OPEN_PAREN)
13940 tree arguments;
13942 arguments = (cp_parser_parenthesized_expression_list
13943 (parser, true, /*non_constant_p=*/NULL));
13944 /* Save the identifier and arguments away. */
13945 TREE_VALUE (attribute) = arguments;
13948 /* Add this attribute to the list. */
13949 TREE_CHAIN (attribute) = attribute_list;
13950 attribute_list = attribute;
13952 /* Now, look for more attributes. */
13953 token = cp_lexer_peek_token (parser->lexer);
13954 /* If the next token isn't a `,', we're done. */
13955 if (token->type != CPP_COMMA)
13956 break;
13958 /* Consume the comma and keep going. */
13959 cp_lexer_consume_token (parser->lexer);
13961 parser->translate_strings_p = save_translate_strings_p;
13963 /* We built up the list in reverse order. */
13964 return nreverse (attribute_list);
13967 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13968 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13969 current value of the PEDANTIC flag, regardless of whether or not
13970 the `__extension__' keyword is present. The caller is responsible
13971 for restoring the value of the PEDANTIC flag. */
13973 static bool
13974 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13976 /* Save the old value of the PEDANTIC flag. */
13977 *saved_pedantic = pedantic;
13979 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13981 /* Consume the `__extension__' token. */
13982 cp_lexer_consume_token (parser->lexer);
13983 /* We're not being pedantic while the `__extension__' keyword is
13984 in effect. */
13985 pedantic = 0;
13987 return true;
13990 return false;
13993 /* Parse a label declaration.
13995 label-declaration:
13996 __label__ label-declarator-seq ;
13998 label-declarator-seq:
13999 identifier , label-declarator-seq
14000 identifier */
14002 static void
14003 cp_parser_label_declaration (cp_parser* parser)
14005 /* Look for the `__label__' keyword. */
14006 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14008 while (true)
14010 tree identifier;
14012 /* Look for an identifier. */
14013 identifier = cp_parser_identifier (parser);
14014 /* Declare it as a lobel. */
14015 finish_label_decl (identifier);
14016 /* If the next token is a `;', stop. */
14017 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14018 break;
14019 /* Look for the `,' separating the label declarations. */
14020 cp_parser_require (parser, CPP_COMMA, "`,'");
14023 /* Look for the final `;'. */
14024 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14027 /* Support Functions */
14029 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14030 NAME should have one of the representations used for an
14031 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14032 is returned. If PARSER->SCOPE is a dependent type, then a
14033 SCOPE_REF is returned.
14035 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14036 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14037 was formed. Abstractly, such entities should not be passed to this
14038 function, because they do not need to be looked up, but it is
14039 simpler to check for this special case here, rather than at the
14040 call-sites.
14042 In cases not explicitly covered above, this function returns a
14043 DECL, OVERLOAD, or baselink representing the result of the lookup.
14044 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14045 is returned.
14047 If IS_TYPE is TRUE, bindings that do not refer to types are
14048 ignored.
14050 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14051 ignored.
14053 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14054 are ignored.
14056 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14057 types.
14059 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14060 results in an ambiguity, and false otherwise. */
14062 static tree
14063 cp_parser_lookup_name (cp_parser *parser, tree name,
14064 bool is_type, bool is_template, bool is_namespace,
14065 bool check_dependency,
14066 bool *ambiguous_p)
14068 tree decl;
14069 tree object_type = parser->context->object_type;
14071 /* Assume that the lookup will be unambiguous. */
14072 if (ambiguous_p)
14073 *ambiguous_p = false;
14075 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14076 no longer valid. Note that if we are parsing tentatively, and
14077 the parse fails, OBJECT_TYPE will be automatically restored. */
14078 parser->context->object_type = NULL_TREE;
14080 if (name == error_mark_node)
14081 return error_mark_node;
14083 /* A template-id has already been resolved; there is no lookup to
14084 do. */
14085 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14086 return name;
14087 if (BASELINK_P (name))
14089 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14090 == TEMPLATE_ID_EXPR);
14091 return name;
14094 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14095 it should already have been checked to make sure that the name
14096 used matches the type being destroyed. */
14097 if (TREE_CODE (name) == BIT_NOT_EXPR)
14099 tree type;
14101 /* Figure out to which type this destructor applies. */
14102 if (parser->scope)
14103 type = parser->scope;
14104 else if (object_type)
14105 type = object_type;
14106 else
14107 type = current_class_type;
14108 /* If that's not a class type, there is no destructor. */
14109 if (!type || !CLASS_TYPE_P (type))
14110 return error_mark_node;
14111 if (!CLASSTYPE_DESTRUCTORS (type))
14112 return error_mark_node;
14113 /* If it was a class type, return the destructor. */
14114 return CLASSTYPE_DESTRUCTORS (type);
14117 /* By this point, the NAME should be an ordinary identifier. If
14118 the id-expression was a qualified name, the qualifying scope is
14119 stored in PARSER->SCOPE at this point. */
14120 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14122 /* Perform the lookup. */
14123 if (parser->scope)
14125 bool dependent_p;
14127 if (parser->scope == error_mark_node)
14128 return error_mark_node;
14130 /* If the SCOPE is dependent, the lookup must be deferred until
14131 the template is instantiated -- unless we are explicitly
14132 looking up names in uninstantiated templates. Even then, we
14133 cannot look up the name if the scope is not a class type; it
14134 might, for example, be a template type parameter. */
14135 dependent_p = (TYPE_P (parser->scope)
14136 && !(parser->in_declarator_p
14137 && currently_open_class (parser->scope))
14138 && dependent_type_p (parser->scope));
14139 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14140 && dependent_p)
14142 if (is_type)
14143 /* The resolution to Core Issue 180 says that `struct A::B'
14144 should be considered a type-name, even if `A' is
14145 dependent. */
14146 decl = TYPE_NAME (make_typename_type (parser->scope,
14147 name,
14148 /*complain=*/1));
14149 else if (is_template)
14150 decl = make_unbound_class_template (parser->scope,
14151 name,
14152 /*complain=*/1);
14153 else
14154 decl = build_nt (SCOPE_REF, parser->scope, name);
14156 else
14158 bool pop_p = false;
14160 /* If PARSER->SCOPE is a dependent type, then it must be a
14161 class type, and we must not be checking dependencies;
14162 otherwise, we would have processed this lookup above. So
14163 that PARSER->SCOPE is not considered a dependent base by
14164 lookup_member, we must enter the scope here. */
14165 if (dependent_p)
14166 pop_p = push_scope (parser->scope);
14167 /* If the PARSER->SCOPE is a a template specialization, it
14168 may be instantiated during name lookup. In that case,
14169 errors may be issued. Even if we rollback the current
14170 tentative parse, those errors are valid. */
14171 decl = lookup_qualified_name (parser->scope, name, is_type,
14172 /*complain=*/true);
14173 if (pop_p)
14174 pop_scope (parser->scope);
14176 parser->qualifying_scope = parser->scope;
14177 parser->object_scope = NULL_TREE;
14179 else if (object_type)
14181 tree object_decl = NULL_TREE;
14182 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14183 OBJECT_TYPE is not a class. */
14184 if (CLASS_TYPE_P (object_type))
14185 /* If the OBJECT_TYPE is a template specialization, it may
14186 be instantiated during name lookup. In that case, errors
14187 may be issued. Even if we rollback the current tentative
14188 parse, those errors are valid. */
14189 object_decl = lookup_member (object_type,
14190 name,
14191 /*protect=*/0, is_type);
14192 /* Look it up in the enclosing context, too. */
14193 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14194 /*block_p=*/true, is_namespace,
14195 /*flags=*/0);
14196 parser->object_scope = object_type;
14197 parser->qualifying_scope = NULL_TREE;
14198 if (object_decl)
14199 decl = object_decl;
14201 else
14203 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14204 /*block_p=*/true, is_namespace,
14205 /*flags=*/0);
14206 parser->qualifying_scope = NULL_TREE;
14207 parser->object_scope = NULL_TREE;
14210 /* If the lookup failed, let our caller know. */
14211 if (!decl
14212 || decl == error_mark_node
14213 || (TREE_CODE (decl) == FUNCTION_DECL
14214 && DECL_ANTICIPATED (decl)))
14215 return error_mark_node;
14217 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14218 if (TREE_CODE (decl) == TREE_LIST)
14220 if (ambiguous_p)
14221 *ambiguous_p = true;
14222 /* The error message we have to print is too complicated for
14223 cp_parser_error, so we incorporate its actions directly. */
14224 if (!cp_parser_simulate_error (parser))
14226 error ("reference to `%D' is ambiguous", name);
14227 print_candidates (decl);
14229 return error_mark_node;
14232 gcc_assert (DECL_P (decl)
14233 || TREE_CODE (decl) == OVERLOAD
14234 || TREE_CODE (decl) == SCOPE_REF
14235 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14236 || BASELINK_P (decl));
14238 /* If we have resolved the name of a member declaration, check to
14239 see if the declaration is accessible. When the name resolves to
14240 set of overloaded functions, accessibility is checked when
14241 overload resolution is done.
14243 During an explicit instantiation, access is not checked at all,
14244 as per [temp.explicit]. */
14245 if (DECL_P (decl))
14246 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14248 return decl;
14251 /* Like cp_parser_lookup_name, but for use in the typical case where
14252 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14253 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14255 static tree
14256 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14258 return cp_parser_lookup_name (parser, name,
14259 /*is_type=*/false,
14260 /*is_template=*/false,
14261 /*is_namespace=*/false,
14262 /*check_dependency=*/true,
14263 /*ambiguous_p=*/NULL);
14266 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14267 the current context, return the TYPE_DECL. If TAG_NAME_P is
14268 true, the DECL indicates the class being defined in a class-head,
14269 or declared in an elaborated-type-specifier.
14271 Otherwise, return DECL. */
14273 static tree
14274 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14276 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14277 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14279 struct A {
14280 template <typename T> struct B;
14283 template <typename T> struct A::B {};
14285 Similarly, in a elaborated-type-specifier:
14287 namespace N { struct X{}; }
14289 struct A {
14290 template <typename T> friend struct N::X;
14293 However, if the DECL refers to a class type, and we are in
14294 the scope of the class, then the name lookup automatically
14295 finds the TYPE_DECL created by build_self_reference rather
14296 than a TEMPLATE_DECL. For example, in:
14298 template <class T> struct S {
14299 S s;
14302 there is no need to handle such case. */
14304 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14305 return DECL_TEMPLATE_RESULT (decl);
14307 return decl;
14310 /* If too many, or too few, template-parameter lists apply to the
14311 declarator, issue an error message. Returns TRUE if all went well,
14312 and FALSE otherwise. */
14314 static bool
14315 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14316 cp_declarator *declarator)
14318 unsigned num_templates;
14320 /* We haven't seen any classes that involve template parameters yet. */
14321 num_templates = 0;
14323 switch (declarator->kind)
14325 case cdk_id:
14326 if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14328 tree scope;
14329 tree member;
14331 scope = TREE_OPERAND (declarator->u.id.name, 0);
14332 member = TREE_OPERAND (declarator->u.id.name, 1);
14334 while (scope && CLASS_TYPE_P (scope))
14336 /* You're supposed to have one `template <...>'
14337 for every template class, but you don't need one
14338 for a full specialization. For example:
14340 template <class T> struct S{};
14341 template <> struct S<int> { void f(); };
14342 void S<int>::f () {}
14344 is correct; there shouldn't be a `template <>' for
14345 the definition of `S<int>::f'. */
14346 if (CLASSTYPE_TEMPLATE_INFO (scope)
14347 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14348 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14349 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14350 ++num_templates;
14352 scope = TYPE_CONTEXT (scope);
14356 /* If the DECLARATOR has the form `X<y>' then it uses one
14357 additional level of template parameters. */
14358 if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14359 ++num_templates;
14361 return cp_parser_check_template_parameters (parser,
14362 num_templates);
14364 case cdk_function:
14365 case cdk_array:
14366 case cdk_pointer:
14367 case cdk_reference:
14368 case cdk_ptrmem:
14369 return (cp_parser_check_declarator_template_parameters
14370 (parser, declarator->declarator));
14372 case cdk_error:
14373 return true;
14375 default:
14376 gcc_unreachable ();
14378 return false;
14381 /* NUM_TEMPLATES were used in the current declaration. If that is
14382 invalid, return FALSE and issue an error messages. Otherwise,
14383 return TRUE. */
14385 static bool
14386 cp_parser_check_template_parameters (cp_parser* parser,
14387 unsigned num_templates)
14389 /* If there are more template classes than parameter lists, we have
14390 something like:
14392 template <class T> void S<T>::R<T>::f (); */
14393 if (parser->num_template_parameter_lists < num_templates)
14395 error ("too few template-parameter-lists");
14396 return false;
14398 /* If there are the same number of template classes and parameter
14399 lists, that's OK. */
14400 if (parser->num_template_parameter_lists == num_templates)
14401 return true;
14402 /* If there are more, but only one more, then we are referring to a
14403 member template. That's OK too. */
14404 if (parser->num_template_parameter_lists == num_templates + 1)
14405 return true;
14406 /* Otherwise, there are too many template parameter lists. We have
14407 something like:
14409 template <class T> template <class U> void S::f(); */
14410 error ("too many template-parameter-lists");
14411 return false;
14414 /* Parse a binary-expression of the general form:
14416 binary-expression:
14417 <expr>
14418 binary-expression <token> <expr>
14420 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
14421 to parser the <expr>s. If the first production is used, then the
14422 value returned by FN is returned directly. Otherwise, a node with
14423 the indicated EXPR_TYPE is returned, with operands corresponding to
14424 the two sub-expressions. */
14426 static tree
14427 cp_parser_binary_expression (cp_parser* parser,
14428 const cp_parser_token_tree_map token_tree_map,
14429 cp_parser_expression_fn fn)
14431 tree lhs;
14433 /* Parse the first expression. */
14434 lhs = (*fn) (parser);
14435 /* Now, look for more expressions. */
14436 while (true)
14438 cp_token *token;
14439 const cp_parser_token_tree_map_node *map_node;
14440 tree rhs;
14442 /* Peek at the next token. */
14443 token = cp_lexer_peek_token (parser->lexer);
14444 /* If the token is `>', and that's not an operator at the
14445 moment, then we're done. */
14446 if (token->type == CPP_GREATER
14447 && !parser->greater_than_is_operator_p)
14448 break;
14449 /* If we find one of the tokens we want, build the corresponding
14450 tree representation. */
14451 for (map_node = token_tree_map;
14452 map_node->token_type != CPP_EOF;
14453 ++map_node)
14454 if (map_node->token_type == token->type)
14456 /* Assume that an overloaded operator will not be used. */
14457 bool overloaded_p = false;
14459 /* Consume the operator token. */
14460 cp_lexer_consume_token (parser->lexer);
14461 /* Parse the right-hand side of the expression. */
14462 rhs = (*fn) (parser);
14463 /* Build the binary tree node. */
14464 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14465 &overloaded_p);
14466 /* If the binary operator required the use of an
14467 overloaded operator, then this expression cannot be an
14468 integral constant-expression. An overloaded operator
14469 can be used even if both operands are otherwise
14470 permissible in an integral constant-expression if at
14471 least one of the operands is of enumeration type. */
14472 if (overloaded_p
14473 && (cp_parser_non_integral_constant_expression
14474 (parser, "calls to overloaded operators")))
14475 lhs = error_mark_node;
14476 break;
14479 /* If the token wasn't one of the ones we want, we're done. */
14480 if (map_node->token_type == CPP_EOF)
14481 break;
14484 return lhs;
14487 /* Parse an optional `::' token indicating that the following name is
14488 from the global namespace. If so, PARSER->SCOPE is set to the
14489 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14490 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14491 Returns the new value of PARSER->SCOPE, if the `::' token is
14492 present, and NULL_TREE otherwise. */
14494 static tree
14495 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14497 cp_token *token;
14499 /* Peek at the next token. */
14500 token = cp_lexer_peek_token (parser->lexer);
14501 /* If we're looking at a `::' token then we're starting from the
14502 global namespace, not our current location. */
14503 if (token->type == CPP_SCOPE)
14505 /* Consume the `::' token. */
14506 cp_lexer_consume_token (parser->lexer);
14507 /* Set the SCOPE so that we know where to start the lookup. */
14508 parser->scope = global_namespace;
14509 parser->qualifying_scope = global_namespace;
14510 parser->object_scope = NULL_TREE;
14512 return parser->scope;
14514 else if (!current_scope_valid_p)
14516 parser->scope = NULL_TREE;
14517 parser->qualifying_scope = NULL_TREE;
14518 parser->object_scope = NULL_TREE;
14521 return NULL_TREE;
14524 /* Returns TRUE if the upcoming token sequence is the start of a
14525 constructor declarator. If FRIEND_P is true, the declarator is
14526 preceded by the `friend' specifier. */
14528 static bool
14529 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14531 bool constructor_p;
14532 tree type_decl = NULL_TREE;
14533 bool nested_name_p;
14534 cp_token *next_token;
14536 /* The common case is that this is not a constructor declarator, so
14537 try to avoid doing lots of work if at all possible. It's not
14538 valid declare a constructor at function scope. */
14539 if (at_function_scope_p ())
14540 return false;
14541 /* And only certain tokens can begin a constructor declarator. */
14542 next_token = cp_lexer_peek_token (parser->lexer);
14543 if (next_token->type != CPP_NAME
14544 && next_token->type != CPP_SCOPE
14545 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14546 && next_token->type != CPP_TEMPLATE_ID)
14547 return false;
14549 /* Parse tentatively; we are going to roll back all of the tokens
14550 consumed here. */
14551 cp_parser_parse_tentatively (parser);
14552 /* Assume that we are looking at a constructor declarator. */
14553 constructor_p = true;
14555 /* Look for the optional `::' operator. */
14556 cp_parser_global_scope_opt (parser,
14557 /*current_scope_valid_p=*/false);
14558 /* Look for the nested-name-specifier. */
14559 nested_name_p
14560 = (cp_parser_nested_name_specifier_opt (parser,
14561 /*typename_keyword_p=*/false,
14562 /*check_dependency_p=*/false,
14563 /*type_p=*/false,
14564 /*is_declaration=*/false)
14565 != NULL_TREE);
14566 /* Outside of a class-specifier, there must be a
14567 nested-name-specifier. */
14568 if (!nested_name_p &&
14569 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14570 || friend_p))
14571 constructor_p = false;
14572 /* If we still think that this might be a constructor-declarator,
14573 look for a class-name. */
14574 if (constructor_p)
14576 /* If we have:
14578 template <typename T> struct S { S(); };
14579 template <typename T> S<T>::S ();
14581 we must recognize that the nested `S' names a class.
14582 Similarly, for:
14584 template <typename T> S<T>::S<T> ();
14586 we must recognize that the nested `S' names a template. */
14587 type_decl = cp_parser_class_name (parser,
14588 /*typename_keyword_p=*/false,
14589 /*template_keyword_p=*/false,
14590 /*type_p=*/false,
14591 /*check_dependency_p=*/false,
14592 /*class_head_p=*/false,
14593 /*is_declaration=*/false);
14594 /* If there was no class-name, then this is not a constructor. */
14595 constructor_p = !cp_parser_error_occurred (parser);
14598 /* If we're still considering a constructor, we have to see a `(',
14599 to begin the parameter-declaration-clause, followed by either a
14600 `)', an `...', or a decl-specifier. We need to check for a
14601 type-specifier to avoid being fooled into thinking that:
14603 S::S (f) (int);
14605 is a constructor. (It is actually a function named `f' that
14606 takes one parameter (of type `int') and returns a value of type
14607 `S::S'. */
14608 if (constructor_p
14609 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14612 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14613 /* A parameter declaration begins with a decl-specifier,
14614 which is either the "attribute" keyword, a storage class
14615 specifier, or (usually) a type-specifier. */
14616 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14617 && !cp_parser_storage_class_specifier_opt (parser))
14619 tree type;
14620 bool pop_p = false;
14621 unsigned saved_num_template_parameter_lists;
14623 /* Names appearing in the type-specifier should be looked up
14624 in the scope of the class. */
14625 if (current_class_type)
14626 type = NULL_TREE;
14627 else
14629 type = TREE_TYPE (type_decl);
14630 if (TREE_CODE (type) == TYPENAME_TYPE)
14632 type = resolve_typename_type (type,
14633 /*only_current_p=*/false);
14634 if (type == error_mark_node)
14636 cp_parser_abort_tentative_parse (parser);
14637 return false;
14640 pop_p = push_scope (type);
14643 /* Inside the constructor parameter list, surrounding
14644 template-parameter-lists do not apply. */
14645 saved_num_template_parameter_lists
14646 = parser->num_template_parameter_lists;
14647 parser->num_template_parameter_lists = 0;
14649 /* Look for the type-specifier. */
14650 cp_parser_type_specifier (parser,
14651 CP_PARSER_FLAGS_NONE,
14652 /*decl_specs=*/NULL,
14653 /*is_declarator=*/true,
14654 /*declares_class_or_enum=*/NULL,
14655 /*is_cv_qualifier=*/NULL);
14657 parser->num_template_parameter_lists
14658 = saved_num_template_parameter_lists;
14660 /* Leave the scope of the class. */
14661 if (pop_p)
14662 pop_scope (type);
14664 constructor_p = !cp_parser_error_occurred (parser);
14667 else
14668 constructor_p = false;
14669 /* We did not really want to consume any tokens. */
14670 cp_parser_abort_tentative_parse (parser);
14672 return constructor_p;
14675 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14676 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14677 they must be performed once we are in the scope of the function.
14679 Returns the function defined. */
14681 static tree
14682 cp_parser_function_definition_from_specifiers_and_declarator
14683 (cp_parser* parser,
14684 cp_decl_specifier_seq *decl_specifiers,
14685 tree attributes,
14686 const cp_declarator *declarator)
14688 tree fn;
14689 bool success_p;
14691 /* Begin the function-definition. */
14692 success_p = start_function (decl_specifiers, declarator, attributes);
14694 /* The things we're about to see are not directly qualified by any
14695 template headers we've seen thus far. */
14696 reset_specialization ();
14698 /* If there were names looked up in the decl-specifier-seq that we
14699 did not check, check them now. We must wait until we are in the
14700 scope of the function to perform the checks, since the function
14701 might be a friend. */
14702 perform_deferred_access_checks ();
14704 if (!success_p)
14706 /* Skip the entire function. */
14707 error ("invalid function declaration");
14708 cp_parser_skip_to_end_of_block_or_statement (parser);
14709 fn = error_mark_node;
14711 else
14712 fn = cp_parser_function_definition_after_declarator (parser,
14713 /*inline_p=*/false);
14715 return fn;
14718 /* Parse the part of a function-definition that follows the
14719 declarator. INLINE_P is TRUE iff this function is an inline
14720 function defined with a class-specifier.
14722 Returns the function defined. */
14724 static tree
14725 cp_parser_function_definition_after_declarator (cp_parser* parser,
14726 bool inline_p)
14728 tree fn;
14729 bool ctor_initializer_p = false;
14730 bool saved_in_unbraced_linkage_specification_p;
14731 unsigned saved_num_template_parameter_lists;
14733 /* If the next token is `return', then the code may be trying to
14734 make use of the "named return value" extension that G++ used to
14735 support. */
14736 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14738 /* Consume the `return' keyword. */
14739 cp_lexer_consume_token (parser->lexer);
14740 /* Look for the identifier that indicates what value is to be
14741 returned. */
14742 cp_parser_identifier (parser);
14743 /* Issue an error message. */
14744 error ("named return values are no longer supported");
14745 /* Skip tokens until we reach the start of the function body. */
14746 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14747 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14748 cp_lexer_consume_token (parser->lexer);
14750 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14751 anything declared inside `f'. */
14752 saved_in_unbraced_linkage_specification_p
14753 = parser->in_unbraced_linkage_specification_p;
14754 parser->in_unbraced_linkage_specification_p = false;
14755 /* Inside the function, surrounding template-parameter-lists do not
14756 apply. */
14757 saved_num_template_parameter_lists
14758 = parser->num_template_parameter_lists;
14759 parser->num_template_parameter_lists = 0;
14760 /* If the next token is `try', then we are looking at a
14761 function-try-block. */
14762 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14763 ctor_initializer_p = cp_parser_function_try_block (parser);
14764 /* A function-try-block includes the function-body, so we only do
14765 this next part if we're not processing a function-try-block. */
14766 else
14767 ctor_initializer_p
14768 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14770 /* Finish the function. */
14771 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14772 (inline_p ? 2 : 0));
14773 /* Generate code for it, if necessary. */
14774 expand_or_defer_fn (fn);
14775 /* Restore the saved values. */
14776 parser->in_unbraced_linkage_specification_p
14777 = saved_in_unbraced_linkage_specification_p;
14778 parser->num_template_parameter_lists
14779 = saved_num_template_parameter_lists;
14781 return fn;
14784 /* Parse a template-declaration, assuming that the `export' (and
14785 `extern') keywords, if present, has already been scanned. MEMBER_P
14786 is as for cp_parser_template_declaration. */
14788 static void
14789 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14791 tree decl = NULL_TREE;
14792 tree parameter_list;
14793 bool friend_p = false;
14795 /* Look for the `template' keyword. */
14796 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14797 return;
14799 /* And the `<'. */
14800 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14801 return;
14803 /* If the next token is `>', then we have an invalid
14804 specialization. Rather than complain about an invalid template
14805 parameter, issue an error message here. */
14806 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14808 cp_parser_error (parser, "invalid explicit specialization");
14809 begin_specialization ();
14810 parameter_list = NULL_TREE;
14812 else
14814 /* Parse the template parameters. */
14815 begin_template_parm_list ();
14816 parameter_list = cp_parser_template_parameter_list (parser);
14817 parameter_list = end_template_parm_list (parameter_list);
14820 /* Look for the `>'. */
14821 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14822 /* We just processed one more parameter list. */
14823 ++parser->num_template_parameter_lists;
14824 /* If the next token is `template', there are more template
14825 parameters. */
14826 if (cp_lexer_next_token_is_keyword (parser->lexer,
14827 RID_TEMPLATE))
14828 cp_parser_template_declaration_after_export (parser, member_p);
14829 else
14831 /* There are no access checks when parsing a template, as we do not
14832 know if a specialization will be a friend. */
14833 push_deferring_access_checks (dk_no_check);
14835 decl = cp_parser_single_declaration (parser,
14836 member_p,
14837 &friend_p);
14839 pop_deferring_access_checks ();
14841 /* If this is a member template declaration, let the front
14842 end know. */
14843 if (member_p && !friend_p && decl)
14845 if (TREE_CODE (decl) == TYPE_DECL)
14846 cp_parser_check_access_in_redeclaration (decl);
14848 decl = finish_member_template_decl (decl);
14850 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14851 make_friend_class (current_class_type, TREE_TYPE (decl),
14852 /*complain=*/true);
14854 /* We are done with the current parameter list. */
14855 --parser->num_template_parameter_lists;
14857 /* Finish up. */
14858 finish_template_decl (parameter_list);
14860 /* Register member declarations. */
14861 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14862 finish_member_declaration (decl);
14864 /* If DECL is a function template, we must return to parse it later.
14865 (Even though there is no definition, there might be default
14866 arguments that need handling.) */
14867 if (member_p && decl
14868 && (TREE_CODE (decl) == FUNCTION_DECL
14869 || DECL_FUNCTION_TEMPLATE_P (decl)))
14870 TREE_VALUE (parser->unparsed_functions_queues)
14871 = tree_cons (NULL_TREE, decl,
14872 TREE_VALUE (parser->unparsed_functions_queues));
14875 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14876 `function-definition' sequence. MEMBER_P is true, this declaration
14877 appears in a class scope.
14879 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14880 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14882 static tree
14883 cp_parser_single_declaration (cp_parser* parser,
14884 bool member_p,
14885 bool* friend_p)
14887 int declares_class_or_enum;
14888 tree decl = NULL_TREE;
14889 cp_decl_specifier_seq decl_specifiers;
14890 bool function_definition_p = false;
14892 /* Defer access checks until we know what is being declared. */
14893 push_deferring_access_checks (dk_deferred);
14895 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14896 alternative. */
14897 cp_parser_decl_specifier_seq (parser,
14898 CP_PARSER_FLAGS_OPTIONAL,
14899 &decl_specifiers,
14900 &declares_class_or_enum);
14901 if (friend_p)
14902 *friend_p = cp_parser_friend_p (&decl_specifiers);
14903 /* Gather up the access checks that occurred the
14904 decl-specifier-seq. */
14905 stop_deferring_access_checks ();
14907 /* Check for the declaration of a template class. */
14908 if (declares_class_or_enum)
14910 if (cp_parser_declares_only_class_p (parser))
14912 decl = shadow_tag (&decl_specifiers);
14913 if (decl && decl != error_mark_node)
14914 decl = TYPE_NAME (decl);
14915 else
14916 decl = error_mark_node;
14919 else
14920 decl = NULL_TREE;
14921 /* If it's not a template class, try for a template function. If
14922 the next token is a `;', then this declaration does not declare
14923 anything. But, if there were errors in the decl-specifiers, then
14924 the error might well have come from an attempted class-specifier.
14925 In that case, there's no need to warn about a missing declarator. */
14926 if (!decl
14927 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14928 || decl_specifiers.type != error_mark_node))
14929 decl = cp_parser_init_declarator (parser,
14930 &decl_specifiers,
14931 /*function_definition_allowed_p=*/true,
14932 member_p,
14933 declares_class_or_enum,
14934 &function_definition_p);
14936 pop_deferring_access_checks ();
14938 /* Clear any current qualification; whatever comes next is the start
14939 of something new. */
14940 parser->scope = NULL_TREE;
14941 parser->qualifying_scope = NULL_TREE;
14942 parser->object_scope = NULL_TREE;
14943 /* Look for a trailing `;' after the declaration. */
14944 if (!function_definition_p
14945 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14946 cp_parser_skip_to_end_of_block_or_statement (parser);
14948 return decl;
14951 /* Parse a cast-expression that is not the operand of a unary "&". */
14953 static tree
14954 cp_parser_simple_cast_expression (cp_parser *parser)
14956 return cp_parser_cast_expression (parser, /*address_p=*/false);
14959 /* Parse a functional cast to TYPE. Returns an expression
14960 representing the cast. */
14962 static tree
14963 cp_parser_functional_cast (cp_parser* parser, tree type)
14965 tree expression_list;
14966 tree cast;
14968 expression_list
14969 = cp_parser_parenthesized_expression_list (parser, false,
14970 /*non_constant_p=*/NULL);
14972 cast = build_functional_cast (type, expression_list);
14973 /* [expr.const]/1: In an integral constant expression "only type
14974 conversions to integral or enumeration type can be used". */
14975 if (cast != error_mark_node && !type_dependent_expression_p (type)
14976 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14978 if (cp_parser_non_integral_constant_expression
14979 (parser, "a call to a constructor"))
14980 return error_mark_node;
14982 return cast;
14985 /* Save the tokens that make up the body of a member function defined
14986 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14987 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14988 specifiers applied to the declaration. Returns the FUNCTION_DECL
14989 for the member function. */
14991 static tree
14992 cp_parser_save_member_function_body (cp_parser* parser,
14993 cp_decl_specifier_seq *decl_specifiers,
14994 cp_declarator *declarator,
14995 tree attributes)
14997 cp_token *first;
14998 cp_token *last;
14999 tree fn;
15001 /* Create the function-declaration. */
15002 fn = start_method (decl_specifiers, declarator, attributes);
15003 /* If something went badly wrong, bail out now. */
15004 if (fn == error_mark_node)
15006 /* If there's a function-body, skip it. */
15007 if (cp_parser_token_starts_function_definition_p
15008 (cp_lexer_peek_token (parser->lexer)))
15009 cp_parser_skip_to_end_of_block_or_statement (parser);
15010 return error_mark_node;
15013 /* Remember it, if there default args to post process. */
15014 cp_parser_save_default_args (parser, fn);
15016 /* Save away the tokens that make up the body of the
15017 function. */
15018 first = parser->lexer->next_token;
15019 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15020 /* Handle function try blocks. */
15021 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15022 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15023 last = parser->lexer->next_token;
15025 /* Save away the inline definition; we will process it when the
15026 class is complete. */
15027 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15028 DECL_PENDING_INLINE_P (fn) = 1;
15030 /* We need to know that this was defined in the class, so that
15031 friend templates are handled correctly. */
15032 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15034 /* We're done with the inline definition. */
15035 finish_method (fn);
15037 /* Add FN to the queue of functions to be parsed later. */
15038 TREE_VALUE (parser->unparsed_functions_queues)
15039 = tree_cons (NULL_TREE, fn,
15040 TREE_VALUE (parser->unparsed_functions_queues));
15042 return fn;
15045 /* Parse a template-argument-list, as well as the trailing ">" (but
15046 not the opening ">"). See cp_parser_template_argument_list for the
15047 return value. */
15049 static tree
15050 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15052 tree arguments;
15053 tree saved_scope;
15054 tree saved_qualifying_scope;
15055 tree saved_object_scope;
15056 bool saved_greater_than_is_operator_p;
15058 /* [temp.names]
15060 When parsing a template-id, the first non-nested `>' is taken as
15061 the end of the template-argument-list rather than a greater-than
15062 operator. */
15063 saved_greater_than_is_operator_p
15064 = parser->greater_than_is_operator_p;
15065 parser->greater_than_is_operator_p = false;
15066 /* Parsing the argument list may modify SCOPE, so we save it
15067 here. */
15068 saved_scope = parser->scope;
15069 saved_qualifying_scope = parser->qualifying_scope;
15070 saved_object_scope = parser->object_scope;
15071 /* Parse the template-argument-list itself. */
15072 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15073 arguments = NULL_TREE;
15074 else
15075 arguments = cp_parser_template_argument_list (parser);
15076 /* Look for the `>' that ends the template-argument-list. If we find
15077 a '>>' instead, it's probably just a typo. */
15078 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15080 if (!saved_greater_than_is_operator_p)
15082 /* If we're in a nested template argument list, the '>>' has to be
15083 a typo for '> >'. We emit the error message, but we continue
15084 parsing and we push a '>' as next token, so that the argument
15085 list will be parsed correctly.. */
15086 cp_token* token;
15087 error ("`>>' should be `> >' within a nested template argument list");
15088 token = cp_lexer_peek_token (parser->lexer);
15089 token->type = CPP_GREATER;
15091 else
15093 /* If this is not a nested template argument list, the '>>' is
15094 a typo for '>'. Emit an error message and continue. */
15095 error ("spurious `>>', use `>' to terminate a template argument list");
15096 cp_lexer_consume_token (parser->lexer);
15099 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
15100 error ("missing `>' to terminate the template argument list");
15101 /* The `>' token might be a greater-than operator again now. */
15102 parser->greater_than_is_operator_p
15103 = saved_greater_than_is_operator_p;
15104 /* Restore the SAVED_SCOPE. */
15105 parser->scope = saved_scope;
15106 parser->qualifying_scope = saved_qualifying_scope;
15107 parser->object_scope = saved_object_scope;
15109 return arguments;
15112 /* MEMBER_FUNCTION is a member function, or a friend. If default
15113 arguments, or the body of the function have not yet been parsed,
15114 parse them now. */
15116 static void
15117 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15119 cp_lexer *saved_lexer;
15121 /* If this member is a template, get the underlying
15122 FUNCTION_DECL. */
15123 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15124 member_function = DECL_TEMPLATE_RESULT (member_function);
15126 /* There should not be any class definitions in progress at this
15127 point; the bodies of members are only parsed outside of all class
15128 definitions. */
15129 gcc_assert (parser->num_classes_being_defined == 0);
15130 /* While we're parsing the member functions we might encounter more
15131 classes. We want to handle them right away, but we don't want
15132 them getting mixed up with functions that are currently in the
15133 queue. */
15134 parser->unparsed_functions_queues
15135 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15137 /* Make sure that any template parameters are in scope. */
15138 maybe_begin_member_template_processing (member_function);
15140 /* If the body of the function has not yet been parsed, parse it
15141 now. */
15142 if (DECL_PENDING_INLINE_P (member_function))
15144 tree function_scope;
15145 cp_token_cache *tokens;
15147 /* The function is no longer pending; we are processing it. */
15148 tokens = DECL_PENDING_INLINE_INFO (member_function);
15149 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15150 DECL_PENDING_INLINE_P (member_function) = 0;
15151 /* If this was an inline function in a local class, enter the scope
15152 of the containing function. */
15153 function_scope = decl_function_context (member_function);
15154 if (function_scope)
15155 push_function_context_to (function_scope);
15157 /* Save away the current lexer. */
15158 saved_lexer = parser->lexer;
15159 /* Make a new lexer to feed us the tokens saved for this function. */
15160 parser->lexer = cp_lexer_new_from_tokens (tokens);
15161 parser->lexer->next = saved_lexer;
15163 /* Set the current source position to be the location of the first
15164 token in the saved inline body. */
15165 cp_lexer_peek_token (parser->lexer);
15167 /* Let the front end know that we going to be defining this
15168 function. */
15169 start_preparsed_function (member_function, NULL_TREE,
15170 SF_PRE_PARSED | SF_INCLASS_INLINE);
15172 /* Now, parse the body of the function. */
15173 cp_parser_function_definition_after_declarator (parser,
15174 /*inline_p=*/true);
15176 /* Leave the scope of the containing function. */
15177 if (function_scope)
15178 pop_function_context_from (function_scope);
15179 /* Restore the lexer. */
15180 parser->lexer = saved_lexer;
15183 /* Remove any template parameters from the symbol table. */
15184 maybe_end_member_template_processing ();
15186 /* Restore the queue. */
15187 parser->unparsed_functions_queues
15188 = TREE_CHAIN (parser->unparsed_functions_queues);
15191 /* If DECL contains any default args, remember it on the unparsed
15192 functions queue. */
15194 static void
15195 cp_parser_save_default_args (cp_parser* parser, tree decl)
15197 tree probe;
15199 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15200 probe;
15201 probe = TREE_CHAIN (probe))
15202 if (TREE_PURPOSE (probe))
15204 TREE_PURPOSE (parser->unparsed_functions_queues)
15205 = tree_cons (current_class_type, decl,
15206 TREE_PURPOSE (parser->unparsed_functions_queues));
15207 break;
15209 return;
15212 /* FN is a FUNCTION_DECL which may contains a parameter with an
15213 unparsed DEFAULT_ARG. Parse the default args now. This function
15214 assumes that the current scope is the scope in which the default
15215 argument should be processed. */
15217 static void
15218 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15220 cp_lexer *saved_lexer;
15221 cp_token_cache *tokens;
15222 bool saved_local_variables_forbidden_p;
15223 tree parameters;
15225 /* While we're parsing the default args, we might (due to the
15226 statement expression extension) encounter more classes. We want
15227 to handle them right away, but we don't want them getting mixed
15228 up with default args that are currently in the queue. */
15229 parser->unparsed_functions_queues
15230 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15232 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
15233 parameters;
15234 parameters = TREE_CHAIN (parameters))
15236 if (!TREE_PURPOSE (parameters)
15237 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
15238 continue;
15240 /* Save away the current lexer. */
15241 saved_lexer = parser->lexer;
15242 /* Create a new one, using the tokens we have saved. */
15243 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
15244 parser->lexer = cp_lexer_new_from_tokens (tokens);
15246 /* Set the current source position to be the location of the
15247 first token in the default argument. */
15248 cp_lexer_peek_token (parser->lexer);
15250 /* Local variable names (and the `this' keyword) may not appear
15251 in a default argument. */
15252 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15253 parser->local_variables_forbidden_p = true;
15254 /* Parse the assignment-expression. */
15255 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
15257 /* If the token stream has not been completely used up, then
15258 there was extra junk after the end of the default
15259 argument. */
15260 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15261 cp_parser_error (parser, "expected `,'");
15263 /* Restore saved state. */
15264 parser->lexer = saved_lexer;
15265 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15268 /* Restore the queue. */
15269 parser->unparsed_functions_queues
15270 = TREE_CHAIN (parser->unparsed_functions_queues);
15273 /* Parse the operand of `sizeof' (or a similar operator). Returns
15274 either a TYPE or an expression, depending on the form of the
15275 input. The KEYWORD indicates which kind of expression we have
15276 encountered. */
15278 static tree
15279 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15281 static const char *format;
15282 tree expr = NULL_TREE;
15283 const char *saved_message;
15284 bool saved_integral_constant_expression_p;
15286 /* Initialize FORMAT the first time we get here. */
15287 if (!format)
15288 format = "types may not be defined in `%s' expressions";
15290 /* Types cannot be defined in a `sizeof' expression. Save away the
15291 old message. */
15292 saved_message = parser->type_definition_forbidden_message;
15293 /* And create the new one. */
15294 parser->type_definition_forbidden_message
15295 = xmalloc (strlen (format)
15296 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15297 + 1 /* `\0' */);
15298 sprintf ((char *) parser->type_definition_forbidden_message,
15299 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15301 /* The restrictions on constant-expressions do not apply inside
15302 sizeof expressions. */
15303 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15304 parser->integral_constant_expression_p = false;
15306 /* Do not actually evaluate the expression. */
15307 ++skip_evaluation;
15308 /* If it's a `(', then we might be looking at the type-id
15309 construction. */
15310 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15312 tree type;
15313 bool saved_in_type_id_in_expr_p;
15315 /* We can't be sure yet whether we're looking at a type-id or an
15316 expression. */
15317 cp_parser_parse_tentatively (parser);
15318 /* Consume the `('. */
15319 cp_lexer_consume_token (parser->lexer);
15320 /* Parse the type-id. */
15321 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15322 parser->in_type_id_in_expr_p = true;
15323 type = cp_parser_type_id (parser);
15324 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15325 /* Now, look for the trailing `)'. */
15326 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15327 /* If all went well, then we're done. */
15328 if (cp_parser_parse_definitely (parser))
15330 cp_decl_specifier_seq decl_specs;
15332 /* Build a trivial decl-specifier-seq. */
15333 clear_decl_specs (&decl_specs);
15334 decl_specs.type = type;
15336 /* Call grokdeclarator to figure out what type this is. */
15337 expr = grokdeclarator (NULL,
15338 &decl_specs,
15339 TYPENAME,
15340 /*initialized=*/0,
15341 /*attrlist=*/NULL);
15345 /* If the type-id production did not work out, then we must be
15346 looking at the unary-expression production. */
15347 if (!expr)
15348 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15349 /* Go back to evaluating expressions. */
15350 --skip_evaluation;
15352 /* Free the message we created. */
15353 free ((char *) parser->type_definition_forbidden_message);
15354 /* And restore the old one. */
15355 parser->type_definition_forbidden_message = saved_message;
15356 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15358 return expr;
15361 /* If the current declaration has no declarator, return true. */
15363 static bool
15364 cp_parser_declares_only_class_p (cp_parser *parser)
15366 /* If the next token is a `;' or a `,' then there is no
15367 declarator. */
15368 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15369 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15372 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15374 static void
15375 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15376 cp_storage_class storage_class)
15378 if (decl_specs->storage_class != sc_none)
15379 decl_specs->multiple_storage_classes_p = true;
15380 else
15381 decl_specs->storage_class = storage_class;
15384 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15385 is true, the type is a user-defined type; otherwise it is a
15386 built-in type specified by a keyword. */
15388 static void
15389 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15390 tree type_spec,
15391 bool user_defined_p)
15393 decl_specs->any_specifiers_p = true;
15395 /* If the user tries to redeclare a built-in type (with, for example,
15396 in "typedef int wchar_t;") we remember that this is what
15397 happened. In system headers, we ignore these declarations so
15398 that G++ can work with system headers that are not C++-safe. */
15399 if (decl_specs->specs[(int) ds_typedef]
15400 && !user_defined_p
15401 && (decl_specs->type
15402 || decl_specs->specs[(int) ds_long]
15403 || decl_specs->specs[(int) ds_short]
15404 || decl_specs->specs[(int) ds_unsigned]
15405 || decl_specs->specs[(int) ds_signed]))
15407 decl_specs->redefined_builtin_type = type_spec;
15408 if (!decl_specs->type)
15410 decl_specs->type = type_spec;
15411 decl_specs->user_defined_type_p = false;
15414 else if (decl_specs->type)
15415 decl_specs->multiple_types_p = true;
15416 else
15418 decl_specs->type = type_spec;
15419 decl_specs->user_defined_type_p = user_defined_p;
15420 decl_specs->redefined_builtin_type = NULL_TREE;
15424 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15425 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15427 static bool
15428 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15430 return decl_specifiers->specs[(int) ds_friend] != 0;
15433 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15434 issue an error message indicating that TOKEN_DESC was expected.
15436 Returns the token consumed, if the token had the appropriate type.
15437 Otherwise, returns NULL. */
15439 static cp_token *
15440 cp_parser_require (cp_parser* parser,
15441 enum cpp_ttype type,
15442 const char* token_desc)
15444 if (cp_lexer_next_token_is (parser->lexer, type))
15445 return cp_lexer_consume_token (parser->lexer);
15446 else
15448 /* Output the MESSAGE -- unless we're parsing tentatively. */
15449 if (!cp_parser_simulate_error (parser))
15451 char *message = concat ("expected ", token_desc, NULL);
15452 cp_parser_error (parser, message);
15453 free (message);
15455 return NULL;
15459 /* Like cp_parser_require, except that tokens will be skipped until
15460 the desired token is found. An error message is still produced if
15461 the next token is not as expected. */
15463 static void
15464 cp_parser_skip_until_found (cp_parser* parser,
15465 enum cpp_ttype type,
15466 const char* token_desc)
15468 cp_token *token;
15469 unsigned nesting_depth = 0;
15471 if (cp_parser_require (parser, type, token_desc))
15472 return;
15474 /* Skip tokens until the desired token is found. */
15475 while (true)
15477 /* Peek at the next token. */
15478 token = cp_lexer_peek_token (parser->lexer);
15479 /* If we've reached the token we want, consume it and
15480 stop. */
15481 if (token->type == type && !nesting_depth)
15483 cp_lexer_consume_token (parser->lexer);
15484 return;
15486 /* If we've run out of tokens, stop. */
15487 if (token->type == CPP_EOF)
15488 return;
15489 if (token->type == CPP_OPEN_BRACE
15490 || token->type == CPP_OPEN_PAREN
15491 || token->type == CPP_OPEN_SQUARE)
15492 ++nesting_depth;
15493 else if (token->type == CPP_CLOSE_BRACE
15494 || token->type == CPP_CLOSE_PAREN
15495 || token->type == CPP_CLOSE_SQUARE)
15497 if (nesting_depth-- == 0)
15498 return;
15500 /* Consume this token. */
15501 cp_lexer_consume_token (parser->lexer);
15505 /* If the next token is the indicated keyword, consume it. Otherwise,
15506 issue an error message indicating that TOKEN_DESC was expected.
15508 Returns the token consumed, if the token had the appropriate type.
15509 Otherwise, returns NULL. */
15511 static cp_token *
15512 cp_parser_require_keyword (cp_parser* parser,
15513 enum rid keyword,
15514 const char* token_desc)
15516 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15518 if (token && token->keyword != keyword)
15520 dyn_string_t error_msg;
15522 /* Format the error message. */
15523 error_msg = dyn_string_new (0);
15524 dyn_string_append_cstr (error_msg, "expected ");
15525 dyn_string_append_cstr (error_msg, token_desc);
15526 cp_parser_error (parser, error_msg->s);
15527 dyn_string_delete (error_msg);
15528 return NULL;
15531 return token;
15534 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15535 function-definition. */
15537 static bool
15538 cp_parser_token_starts_function_definition_p (cp_token* token)
15540 return (/* An ordinary function-body begins with an `{'. */
15541 token->type == CPP_OPEN_BRACE
15542 /* A ctor-initializer begins with a `:'. */
15543 || token->type == CPP_COLON
15544 /* A function-try-block begins with `try'. */
15545 || token->keyword == RID_TRY
15546 /* The named return value extension begins with `return'. */
15547 || token->keyword == RID_RETURN);
15550 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15551 definition. */
15553 static bool
15554 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15556 cp_token *token;
15558 token = cp_lexer_peek_token (parser->lexer);
15559 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15562 /* Returns TRUE iff the next token is the "," or ">" ending a
15563 template-argument. ">>" is also accepted (after the full
15564 argument was parsed) because it's probably a typo for "> >",
15565 and there is a specific diagnostic for this. */
15567 static bool
15568 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15570 cp_token *token;
15572 token = cp_lexer_peek_token (parser->lexer);
15573 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15574 || token->type == CPP_RSHIFT);
15577 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15578 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15580 static bool
15581 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15582 size_t n)
15584 cp_token *token;
15586 token = cp_lexer_peek_nth_token (parser->lexer, n);
15587 if (token->type == CPP_LESS)
15588 return true;
15589 /* Check for the sequence `<::' in the original code. It would be lexed as
15590 `[:', where `[' is a digraph, and there is no whitespace before
15591 `:'. */
15592 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15594 cp_token *token2;
15595 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15596 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15597 return true;
15599 return false;
15602 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15603 or none_type otherwise. */
15605 static enum tag_types
15606 cp_parser_token_is_class_key (cp_token* token)
15608 switch (token->keyword)
15610 case RID_CLASS:
15611 return class_type;
15612 case RID_STRUCT:
15613 return record_type;
15614 case RID_UNION:
15615 return union_type;
15617 default:
15618 return none_type;
15622 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15624 static void
15625 cp_parser_check_class_key (enum tag_types class_key, tree type)
15627 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15628 pedwarn ("`%s' tag used in naming `%#T'",
15629 class_key == union_type ? "union"
15630 : class_key == record_type ? "struct" : "class",
15631 type);
15634 /* Issue an error message if DECL is redeclared with different
15635 access than its original declaration [class.access.spec/3].
15636 This applies to nested classes and nested class templates.
15637 [class.mem/1]. */
15639 static void cp_parser_check_access_in_redeclaration (tree decl)
15641 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15642 return;
15644 if ((TREE_PRIVATE (decl)
15645 != (current_access_specifier == access_private_node))
15646 || (TREE_PROTECTED (decl)
15647 != (current_access_specifier == access_protected_node)))
15648 error ("%D redeclared with different access", decl);
15651 /* Look for the `template' keyword, as a syntactic disambiguator.
15652 Return TRUE iff it is present, in which case it will be
15653 consumed. */
15655 static bool
15656 cp_parser_optional_template_keyword (cp_parser *parser)
15658 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15660 /* The `template' keyword can only be used within templates;
15661 outside templates the parser can always figure out what is a
15662 template and what is not. */
15663 if (!processing_template_decl)
15665 error ("`template' (as a disambiguator) is only allowed "
15666 "within templates");
15667 /* If this part of the token stream is rescanned, the same
15668 error message would be generated. So, we purge the token
15669 from the stream. */
15670 cp_lexer_purge_token (parser->lexer);
15671 return false;
15673 else
15675 /* Consume the `template' keyword. */
15676 cp_lexer_consume_token (parser->lexer);
15677 return true;
15681 return false;
15684 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15685 set PARSER->SCOPE, and perform other related actions. */
15687 static void
15688 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15690 tree value;
15691 tree check;
15693 /* Get the stored value. */
15694 value = cp_lexer_consume_token (parser->lexer)->value;
15695 /* Perform any access checks that were deferred. */
15696 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15697 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15698 /* Set the scope from the stored value. */
15699 parser->scope = TREE_VALUE (value);
15700 parser->qualifying_scope = TREE_TYPE (value);
15701 parser->object_scope = NULL_TREE;
15704 /* Consume tokens up through a non-nested END token. */
15706 static void
15707 cp_parser_cache_group (cp_parser *parser,
15708 enum cpp_ttype end,
15709 unsigned depth)
15711 while (true)
15713 cp_token *token;
15715 /* Abort a parenthesized expression if we encounter a brace. */
15716 if ((end == CPP_CLOSE_PAREN || depth == 0)
15717 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15718 return;
15719 /* If we've reached the end of the file, stop. */
15720 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15721 return;
15722 /* Consume the next token. */
15723 token = cp_lexer_consume_token (parser->lexer);
15724 /* See if it starts a new group. */
15725 if (token->type == CPP_OPEN_BRACE)
15727 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15728 if (depth == 0)
15729 return;
15731 else if (token->type == CPP_OPEN_PAREN)
15732 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15733 else if (token->type == end)
15734 return;
15738 /* Begin parsing tentatively. We always save tokens while parsing
15739 tentatively so that if the tentative parsing fails we can restore the
15740 tokens. */
15742 static void
15743 cp_parser_parse_tentatively (cp_parser* parser)
15745 /* Enter a new parsing context. */
15746 parser->context = cp_parser_context_new (parser->context);
15747 /* Begin saving tokens. */
15748 cp_lexer_save_tokens (parser->lexer);
15749 /* In order to avoid repetitive access control error messages,
15750 access checks are queued up until we are no longer parsing
15751 tentatively. */
15752 push_deferring_access_checks (dk_deferred);
15755 /* Commit to the currently active tentative parse. */
15757 static void
15758 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15760 cp_parser_context *context;
15761 cp_lexer *lexer;
15763 /* Mark all of the levels as committed. */
15764 lexer = parser->lexer;
15765 for (context = parser->context; context->next; context = context->next)
15767 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15768 break;
15769 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15770 while (!cp_lexer_saving_tokens (lexer))
15771 lexer = lexer->next;
15772 cp_lexer_commit_tokens (lexer);
15776 /* Abort the currently active tentative parse. All consumed tokens
15777 will be rolled back, and no diagnostics will be issued. */
15779 static void
15780 cp_parser_abort_tentative_parse (cp_parser* parser)
15782 cp_parser_simulate_error (parser);
15783 /* Now, pretend that we want to see if the construct was
15784 successfully parsed. */
15785 cp_parser_parse_definitely (parser);
15788 /* Stop parsing tentatively. If a parse error has occurred, restore the
15789 token stream. Otherwise, commit to the tokens we have consumed.
15790 Returns true if no error occurred; false otherwise. */
15792 static bool
15793 cp_parser_parse_definitely (cp_parser* parser)
15795 bool error_occurred;
15796 cp_parser_context *context;
15798 /* Remember whether or not an error occurred, since we are about to
15799 destroy that information. */
15800 error_occurred = cp_parser_error_occurred (parser);
15801 /* Remove the topmost context from the stack. */
15802 context = parser->context;
15803 parser->context = context->next;
15804 /* If no parse errors occurred, commit to the tentative parse. */
15805 if (!error_occurred)
15807 /* Commit to the tokens read tentatively, unless that was
15808 already done. */
15809 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15810 cp_lexer_commit_tokens (parser->lexer);
15812 pop_to_parent_deferring_access_checks ();
15814 /* Otherwise, if errors occurred, roll back our state so that things
15815 are just as they were before we began the tentative parse. */
15816 else
15818 cp_lexer_rollback_tokens (parser->lexer);
15819 pop_deferring_access_checks ();
15821 /* Add the context to the front of the free list. */
15822 context->next = cp_parser_context_free_list;
15823 cp_parser_context_free_list = context;
15825 return !error_occurred;
15828 /* Returns true if we are parsing tentatively -- but have decided that
15829 we will stick with this tentative parse, even if errors occur. */
15831 static bool
15832 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15834 return (cp_parser_parsing_tentatively (parser)
15835 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15838 /* Returns nonzero iff an error has occurred during the most recent
15839 tentative parse. */
15841 static bool
15842 cp_parser_error_occurred (cp_parser* parser)
15844 return (cp_parser_parsing_tentatively (parser)
15845 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15848 /* Returns nonzero if GNU extensions are allowed. */
15850 static bool
15851 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15853 return parser->allow_gnu_extensions_p;
15857 /* The parser. */
15859 static GTY (()) cp_parser *the_parser;
15861 /* External interface. */
15863 /* Parse one entire translation unit. */
15865 void
15866 c_parse_file (void)
15868 bool error_occurred;
15869 static bool already_called = false;
15871 if (already_called)
15873 sorry ("inter-module optimizations not implemented for C++");
15874 return;
15876 already_called = true;
15878 the_parser = cp_parser_new ();
15879 push_deferring_access_checks (flag_access_control
15880 ? dk_no_deferred : dk_no_check);
15881 error_occurred = cp_parser_translation_unit (the_parser);
15882 the_parser = NULL;
15885 /* This variable must be provided by every front end. */
15887 int yydebug;
15889 #include "gt-cp-parser.h"