PR target/4198
[official-gcc.git] / gcc / cp / parser.c
blobd682f3a38aa366dd917c91b6deae51c6da6665c6
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
41 /* The lexer. */
43 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
44 and c-lex.c) and the C++ parser. */
46 /* A C++ token. */
48 typedef struct cp_token GTY (())
50 /* The kind of token. */
51 ENUM_BITFIELD (cpp_ttype) type : 8;
52 /* If this token is a keyword, this value indicates which keyword.
53 Otherwise, this value is RID_MAX. */
54 ENUM_BITFIELD (rid) keyword : 8;
55 /* Token flags. */
56 unsigned char flags;
57 /* True if this token is from a system header. */
58 BOOL_BITFIELD in_system_header : 1;
59 /* True if this token is from a context where it is implicitly extern "C" */
60 BOOL_BITFIELD implicit_extern_c : 1;
61 /* The value associated with this token, if any. */
62 tree value;
63 /* The location at which this token was found. */
64 location_t location;
65 } cp_token;
67 /* We use a stack of token pointer for saving token sets. */
68 typedef struct cp_token *cp_token_position;
69 DEF_VEC_MALLOC_P (cp_token_position);
71 static const cp_token eof_token =
73 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
74 #if USE_MAPPED_LOCATION
76 #else
77 {0, 0}
78 #endif
81 /* The cp_lexer structure represents the C++ lexer. It is responsible
82 for managing the token stream from the preprocessor and supplying
83 it to the parser. Tokens are never added to the cp_lexer after
84 it is created. */
86 typedef struct cp_lexer GTY (())
88 /* The memory allocated for the buffer. NULL if this lexer does not
89 own the token buffer. */
90 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
91 /* If the lexer owns the buffer, this is the number of tokens in the
92 buffer. */
93 size_t buffer_length;
95 /* A pointer just past the last available token. The tokens
96 in this lexer are [buffer, last_token). */
97 cp_token_position GTY ((skip)) last_token;
99 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
100 no more available tokens. */
101 cp_token_position GTY ((skip)) next_token;
103 /* A stack indicating positions at which cp_lexer_save_tokens was
104 called. The top entry is the most recent position at which we
105 began saving tokens. If the stack is non-empty, we are saving
106 tokens. */
107 VEC (cp_token_position) *GTY ((skip)) saved_tokens;
109 /* True if we should output debugging information. */
110 bool debugging_p;
112 /* The next lexer in a linked list of lexers. */
113 struct cp_lexer *next;
114 } cp_lexer;
116 /* cp_token_cache is a range of tokens. There is no need to represent
117 allocate heap memory for it, since tokens are never removed from the
118 lexer's array. There is also no need for the GC to walk through
119 a cp_token_cache, since everything in here is referenced through
120 a lexer. */
122 typedef struct cp_token_cache GTY(())
124 /* The beginning of the token range. */
125 cp_token * GTY((skip)) first;
127 /* Points immediately after the last token in the range. */
128 cp_token * GTY ((skip)) last;
129 } cp_token_cache;
131 /* Prototypes. */
133 static cp_lexer *cp_lexer_new_main
134 (void);
135 static cp_lexer *cp_lexer_new_from_tokens
136 (cp_token_cache *tokens);
137 static void cp_lexer_destroy
138 (cp_lexer *);
139 static int cp_lexer_saving_tokens
140 (const cp_lexer *);
141 static cp_token_position cp_lexer_token_position
142 (cp_lexer *, bool);
143 static cp_token *cp_lexer_token_at
144 (cp_lexer *, cp_token_position);
145 static void cp_lexer_get_preprocessor_token
146 (cp_lexer *, cp_token *);
147 static inline cp_token *cp_lexer_peek_token
148 (cp_lexer *);
149 static cp_token *cp_lexer_peek_nth_token
150 (cp_lexer *, size_t);
151 static inline bool cp_lexer_next_token_is
152 (cp_lexer *, enum cpp_ttype);
153 static bool cp_lexer_next_token_is_not
154 (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_keyword
156 (cp_lexer *, enum rid);
157 static cp_token *cp_lexer_consume_token
158 (cp_lexer *);
159 static void cp_lexer_purge_token
160 (cp_lexer *);
161 static void cp_lexer_purge_tokens_after
162 (cp_lexer *, cp_token_position);
163 static void cp_lexer_handle_pragma
164 (cp_lexer *);
165 static void cp_lexer_save_tokens
166 (cp_lexer *);
167 static void cp_lexer_commit_tokens
168 (cp_lexer *);
169 static void cp_lexer_rollback_tokens
170 (cp_lexer *);
171 #ifdef ENABLE_CHECKING
172 static void cp_lexer_print_token
173 (FILE *, cp_token *);
174 static inline bool cp_lexer_debugging_p
175 (cp_lexer *);
176 static void cp_lexer_start_debugging
177 (cp_lexer *) ATTRIBUTE_UNUSED;
178 static void cp_lexer_stop_debugging
179 (cp_lexer *) ATTRIBUTE_UNUSED;
180 #else
181 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
182 about passing NULL to functions that require non-NULL arguments
183 (fputs, fprintf). It will never be used, so all we need is a value
184 of the right type that's guaranteed not to be NULL. */
185 #define cp_lexer_debug_stream stdout
186 #define cp_lexer_print_token(str, tok) (void) 0
187 #define cp_lexer_debugging_p(lexer) 0
188 #endif /* ENABLE_CHECKING */
190 static cp_token_cache *cp_token_cache_new
191 (cp_token *, cp_token *);
193 /* Manifest constants. */
194 #define CP_LEXER_BUFFER_SIZE 10000
195 #define CP_SAVED_TOKEN_STACK 5
197 /* A token type for keywords, as opposed to ordinary identifiers. */
198 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
200 /* A token type for template-ids. If a template-id is processed while
201 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
202 the value of the CPP_TEMPLATE_ID is whatever was returned by
203 cp_parser_template_id. */
204 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
206 /* A token type for nested-name-specifiers. If a
207 nested-name-specifier is processed while parsing tentatively, it is
208 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
209 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
210 cp_parser_nested_name_specifier_opt. */
211 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
213 /* A token type for tokens that are not tokens at all; these are used
214 to represent slots in the array where there used to be a token
215 that has now been deleted. */
216 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
218 /* The number of token types, including C++-specific ones. */
219 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
221 /* Variables. */
223 #ifdef ENABLE_CHECKING
224 /* The stream to which debugging output should be written. */
225 static FILE *cp_lexer_debug_stream;
226 #endif /* ENABLE_CHECKING */
228 /* Create a new main C++ lexer, the lexer that gets tokens from the
229 preprocessor. */
231 static cp_lexer *
232 cp_lexer_new_main (void)
234 cp_token first_token;
235 cp_lexer *lexer;
236 cp_token *pos;
237 size_t alloc;
238 size_t space;
239 cp_token *buffer;
241 /* It's possible that lexing the first token will load a PCH file,
242 which is a GC collection point. So we have to grab the first
243 token before allocating any memory. Pragmas must not be deferred
244 as -fpch-preprocess can generate a pragma to load the PCH file in
245 the preprocessed output used by -save-temps. */
246 cp_lexer_get_preprocessor_token (NULL, &first_token);
248 /* Tell cpplib we want CPP_PRAGMA tokens. */
249 cpp_get_options (parse_in)->defer_pragmas = true;
251 /* Tell c_lex not to merge string constants. */
252 c_lex_return_raw_strings = true;
254 c_common_no_more_pch ();
256 /* Allocate the memory. */
257 lexer = GGC_CNEW (cp_lexer);
259 #ifdef ENABLE_CHECKING
260 /* Initially we are not debugging. */
261 lexer->debugging_p = false;
262 #endif /* ENABLE_CHECKING */
263 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
265 /* Create the buffer. */
266 alloc = CP_LEXER_BUFFER_SIZE;
267 buffer = ggc_alloc (alloc * sizeof (cp_token));
269 /* Put the first token in the buffer. */
270 space = alloc;
271 pos = buffer;
272 *pos = first_token;
274 /* Get the remaining tokens from the preprocessor. */
275 while (pos->type != CPP_EOF)
277 pos++;
278 if (!--space)
280 space = alloc;
281 alloc *= 2;
282 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
283 pos = buffer + space;
285 cp_lexer_get_preprocessor_token (lexer, pos);
287 lexer->buffer = buffer;
288 lexer->buffer_length = alloc - space;
289 lexer->last_token = pos;
290 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
292 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
293 direct calls to c_lex. Those callers all expect c_lex to do
294 string constant concatenation. */
295 c_lex_return_raw_strings = false;
297 gcc_assert (lexer->next_token->type != CPP_PURGED);
298 return lexer;
301 /* Create a new lexer whose token stream is primed with the tokens in
302 CACHE. When these tokens are exhausted, no new tokens will be read. */
304 static cp_lexer *
305 cp_lexer_new_from_tokens (cp_token_cache *cache)
307 cp_token *first = cache->first;
308 cp_token *last = cache->last;
309 cp_lexer *lexer = GGC_CNEW (cp_lexer);
311 /* We do not own the buffer. */
312 lexer->buffer = NULL;
313 lexer->buffer_length = 0;
314 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
315 lexer->last_token = last;
317 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
319 #ifdef ENABLE_CHECKING
320 /* Initially we are not debugging. */
321 lexer->debugging_p = false;
322 #endif
324 gcc_assert (lexer->next_token->type != CPP_PURGED);
325 return lexer;
328 /* Frees all resources associated with LEXER. */
330 static void
331 cp_lexer_destroy (cp_lexer *lexer)
333 if (lexer->buffer)
334 ggc_free (lexer->buffer);
335 VEC_free (cp_token_position, lexer->saved_tokens);
336 ggc_free (lexer);
339 /* Returns nonzero if debugging information should be output. */
341 #ifdef ENABLE_CHECKING
343 static inline bool
344 cp_lexer_debugging_p (cp_lexer *lexer)
346 return lexer->debugging_p;
349 #endif /* ENABLE_CHECKING */
351 static inline cp_token_position
352 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
354 gcc_assert (!previous_p || lexer->next_token != &eof_token);
356 return lexer->next_token - previous_p;
359 static inline cp_token *
360 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
362 return pos;
365 /* nonzero if we are presently saving tokens. */
367 static inline int
368 cp_lexer_saving_tokens (const cp_lexer* lexer)
370 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
373 /* Store the next token from the preprocessor in *TOKEN. Return true
374 if we reach EOF. */
376 static void
377 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
378 cp_token *token)
380 static int is_extern_c = 0;
382 /* Get a new token from the preprocessor. */
383 token->type
384 = c_lex_with_flags (&token->value, &token->location, &token->flags);
385 token->in_system_header = in_system_header;
387 /* On some systems, some header files are surrounded by an
388 implicit extern "C" block. Set a flag in the token if it
389 comes from such a header. */
390 is_extern_c += pending_lang_change;
391 pending_lang_change = 0;
392 token->implicit_extern_c = is_extern_c > 0;
394 /* Check to see if this token is a keyword. */
395 if (token->type == CPP_NAME
396 && C_IS_RESERVED_WORD (token->value))
398 /* Mark this token as a keyword. */
399 token->type = CPP_KEYWORD;
400 /* Record which keyword. */
401 token->keyword = C_RID_CODE (token->value);
402 /* Update the value. Some keywords are mapped to particular
403 entities, rather than simply having the value of the
404 corresponding IDENTIFIER_NODE. For example, `__const' is
405 mapped to `const'. */
406 token->value = ridpointers[token->keyword];
408 else
409 token->keyword = RID_MAX;
412 /* Update the globals input_location and in_system_header from TOKEN. */
413 static inline void
414 cp_lexer_set_source_position_from_token (cp_token *token)
416 if (token->type != CPP_EOF)
418 input_location = token->location;
419 in_system_header = token->in_system_header;
423 /* Return a pointer to the next token in the token stream, but do not
424 consume it. */
426 static inline cp_token *
427 cp_lexer_peek_token (cp_lexer *lexer)
429 if (cp_lexer_debugging_p (lexer))
431 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
432 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
433 putc ('\n', cp_lexer_debug_stream);
435 return lexer->next_token;
438 /* Return true if the next token has the indicated TYPE. */
440 static inline bool
441 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
443 return cp_lexer_peek_token (lexer)->type == type;
446 /* Return true if the next token does not have the indicated TYPE. */
448 static inline bool
449 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
451 return !cp_lexer_next_token_is (lexer, type);
454 /* Return true if the next token is the indicated KEYWORD. */
456 static inline bool
457 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
459 cp_token *token;
461 /* Peek at the next token. */
462 token = cp_lexer_peek_token (lexer);
463 /* Check to see if it is the indicated keyword. */
464 return token->keyword == keyword;
467 /* Return a pointer to the Nth token in the token stream. If N is 1,
468 then this is precisely equivalent to cp_lexer_peek_token (except
469 that it is not inline). One would like to disallow that case, but
470 there is one case (cp_parser_nth_token_starts_template_id) where
471 the caller passes a variable for N and it might be 1. */
473 static cp_token *
474 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
476 cp_token *token;
478 /* N is 1-based, not zero-based. */
479 gcc_assert (n > 0 && lexer->next_token != &eof_token);
481 if (cp_lexer_debugging_p (lexer))
482 fprintf (cp_lexer_debug_stream,
483 "cp_lexer: peeking ahead %ld at token: ", (long)n);
485 --n;
486 token = lexer->next_token;
487 while (n != 0)
489 ++token;
490 if (token == lexer->last_token)
492 token = (cp_token *)&eof_token;
493 break;
496 if (token->type != CPP_PURGED)
497 --n;
500 if (cp_lexer_debugging_p (lexer))
502 cp_lexer_print_token (cp_lexer_debug_stream, token);
503 putc ('\n', cp_lexer_debug_stream);
506 return token;
509 /* Return the next token, and advance the lexer's next_token pointer
510 to point to the next non-purged token. */
512 static cp_token *
513 cp_lexer_consume_token (cp_lexer* lexer)
515 cp_token *token = lexer->next_token;
517 gcc_assert (token != &eof_token);
521 lexer->next_token++;
522 if (lexer->next_token == lexer->last_token)
524 lexer->next_token = (cp_token *)&eof_token;
525 break;
529 while (lexer->next_token->type == CPP_PURGED);
531 cp_lexer_set_source_position_from_token (token);
533 /* Provide debugging output. */
534 if (cp_lexer_debugging_p (lexer))
536 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
537 cp_lexer_print_token (cp_lexer_debug_stream, token);
538 putc ('\n', cp_lexer_debug_stream);
541 return token;
544 /* Permanently remove the next token from the token stream, and
545 advance the next_token pointer to refer to the next non-purged
546 token. */
548 static void
549 cp_lexer_purge_token (cp_lexer *lexer)
551 cp_token *tok = lexer->next_token;
553 gcc_assert (tok != &eof_token);
554 tok->type = CPP_PURGED;
555 tok->location = UNKNOWN_LOCATION;
556 tok->value = NULL_TREE;
557 tok->keyword = RID_MAX;
561 tok++;
562 if (tok == lexer->last_token)
564 tok = (cp_token *)&eof_token;
565 break;
568 while (tok->type == CPP_PURGED);
569 lexer->next_token = tok;
572 /* Permanently remove all tokens after TOK, up to, but not
573 including, the token that will be returned next by
574 cp_lexer_peek_token. */
576 static void
577 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
579 cp_token *peek = lexer->next_token;
581 if (peek == &eof_token)
582 peek = lexer->last_token;
584 gcc_assert (tok < peek);
586 for ( tok += 1; tok != peek; tok += 1)
588 tok->type = CPP_PURGED;
589 tok->location = UNKNOWN_LOCATION;
590 tok->value = NULL_TREE;
591 tok->keyword = RID_MAX;
595 /* Consume and handle a pragma token. */
596 static void
597 cp_lexer_handle_pragma (cp_lexer *lexer)
599 cpp_string s;
600 cp_token *token = cp_lexer_consume_token (lexer);
601 gcc_assert (token->type == CPP_PRAGMA);
602 gcc_assert (token->value);
604 s.len = TREE_STRING_LENGTH (token->value);
605 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
607 cpp_handle_deferred_pragma (parse_in, &s);
609 /* Clearing token->value here means that we will get an ICE if we
610 try to process this #pragma again (which should be impossible). */
611 token->value = NULL;
614 /* Begin saving tokens. All tokens consumed after this point will be
615 preserved. */
617 static void
618 cp_lexer_save_tokens (cp_lexer* lexer)
620 /* Provide debugging output. */
621 if (cp_lexer_debugging_p (lexer))
622 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
624 VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
627 /* Commit to the portion of the token stream most recently saved. */
629 static void
630 cp_lexer_commit_tokens (cp_lexer* lexer)
632 /* Provide debugging output. */
633 if (cp_lexer_debugging_p (lexer))
634 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
636 VEC_pop (cp_token_position, lexer->saved_tokens);
639 /* Return all tokens saved since the last call to cp_lexer_save_tokens
640 to the token stream. Stop saving tokens. */
642 static void
643 cp_lexer_rollback_tokens (cp_lexer* lexer)
645 /* Provide debugging output. */
646 if (cp_lexer_debugging_p (lexer))
647 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
649 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
652 /* Print a representation of the TOKEN on the STREAM. */
654 #ifdef ENABLE_CHECKING
656 static void
657 cp_lexer_print_token (FILE * stream, cp_token *token)
659 /* We don't use cpp_type2name here because the parser defines
660 a few tokens of its own. */
661 static const char *const token_names[] = {
662 /* cpplib-defined token types */
663 #define OP(e, s) #e,
664 #define TK(e, s) #e,
665 TTYPE_TABLE
666 #undef OP
667 #undef TK
668 /* C++ parser token types - see "Manifest constants", above. */
669 "KEYWORD",
670 "TEMPLATE_ID",
671 "NESTED_NAME_SPECIFIER",
672 "PURGED"
675 /* If we have a name for the token, print it out. Otherwise, we
676 simply give the numeric code. */
677 gcc_assert (token->type < ARRAY_SIZE(token_names));
678 fputs (token_names[token->type], stream);
680 /* For some tokens, print the associated data. */
681 switch (token->type)
683 case CPP_KEYWORD:
684 /* Some keywords have a value that is not an IDENTIFIER_NODE.
685 For example, `struct' is mapped to an INTEGER_CST. */
686 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
687 break;
688 /* else fall through */
689 case CPP_NAME:
690 fputs (IDENTIFIER_POINTER (token->value), stream);
691 break;
693 case CPP_STRING:
694 case CPP_WSTRING:
695 case CPP_PRAGMA:
696 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
697 break;
699 default:
700 break;
704 /* Start emitting debugging information. */
706 static void
707 cp_lexer_start_debugging (cp_lexer* lexer)
709 lexer->debugging_p = true;
712 /* Stop emitting debugging information. */
714 static void
715 cp_lexer_stop_debugging (cp_lexer* lexer)
717 lexer->debugging_p = false;
720 #endif /* ENABLE_CHECKING */
722 /* Create a new cp_token_cache, representing a range of tokens. */
724 static cp_token_cache *
725 cp_token_cache_new (cp_token *first, cp_token *last)
727 cp_token_cache *cache = GGC_NEW (cp_token_cache);
728 cache->first = first;
729 cache->last = last;
730 return cache;
734 /* Decl-specifiers. */
736 static void clear_decl_specs
737 (cp_decl_specifier_seq *);
739 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
741 static void
742 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
744 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
747 /* Declarators. */
749 /* Nothing other than the parser should be creating declarators;
750 declarators are a semi-syntactic representation of C++ entities.
751 Other parts of the front end that need to create entities (like
752 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
754 static cp_declarator *make_call_declarator
755 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
756 static cp_declarator *make_array_declarator
757 (cp_declarator *, tree);
758 static cp_declarator *make_pointer_declarator
759 (cp_cv_quals, cp_declarator *);
760 static cp_declarator *make_reference_declarator
761 (cp_cv_quals, cp_declarator *);
762 static cp_parameter_declarator *make_parameter_declarator
763 (cp_decl_specifier_seq *, cp_declarator *, tree);
764 static cp_declarator *make_ptrmem_declarator
765 (cp_cv_quals, tree, cp_declarator *);
767 cp_declarator *cp_error_declarator;
769 /* The obstack on which declarators and related data structures are
770 allocated. */
771 static struct obstack declarator_obstack;
773 /* Alloc BYTES from the declarator memory pool. */
775 static inline void *
776 alloc_declarator (size_t bytes)
778 return obstack_alloc (&declarator_obstack, bytes);
781 /* Allocate a declarator of the indicated KIND. Clear fields that are
782 common to all declarators. */
784 static cp_declarator *
785 make_declarator (cp_declarator_kind kind)
787 cp_declarator *declarator;
789 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
790 declarator->kind = kind;
791 declarator->attributes = NULL_TREE;
792 declarator->declarator = NULL;
794 return declarator;
797 /* Make a declarator for a generalized identifier. If non-NULL, the
798 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
799 just UNQUALIFIED_NAME. */
801 static cp_declarator *
802 make_id_declarator (tree qualifying_scope, tree unqualified_name)
804 cp_declarator *declarator;
806 /* It is valid to write:
808 class C { void f(); };
809 typedef C D;
810 void D::f();
812 The standard is not clear about whether `typedef const C D' is
813 legal; as of 2002-09-15 the committee is considering that
814 question. EDG 3.0 allows that syntax. Therefore, we do as
815 well. */
816 if (qualifying_scope && TYPE_P (qualifying_scope))
817 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
819 declarator = make_declarator (cdk_id);
820 declarator->u.id.qualifying_scope = qualifying_scope;
821 declarator->u.id.unqualified_name = unqualified_name;
822 declarator->u.id.sfk = sfk_none;
824 return declarator;
827 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
828 of modifiers such as const or volatile to apply to the pointer
829 type, represented as identifiers. */
831 cp_declarator *
832 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
834 cp_declarator *declarator;
836 declarator = make_declarator (cdk_pointer);
837 declarator->declarator = target;
838 declarator->u.pointer.qualifiers = cv_qualifiers;
839 declarator->u.pointer.class_type = NULL_TREE;
841 return declarator;
844 /* Like make_pointer_declarator -- but for references. */
846 cp_declarator *
847 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
849 cp_declarator *declarator;
851 declarator = make_declarator (cdk_reference);
852 declarator->declarator = target;
853 declarator->u.pointer.qualifiers = cv_qualifiers;
854 declarator->u.pointer.class_type = NULL_TREE;
856 return declarator;
859 /* Like make_pointer_declarator -- but for a pointer to a non-static
860 member of CLASS_TYPE. */
862 cp_declarator *
863 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
864 cp_declarator *pointee)
866 cp_declarator *declarator;
868 declarator = make_declarator (cdk_ptrmem);
869 declarator->declarator = pointee;
870 declarator->u.pointer.qualifiers = cv_qualifiers;
871 declarator->u.pointer.class_type = class_type;
873 return declarator;
876 /* Make a declarator for the function given by TARGET, with the
877 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
878 "const"-qualified member function. The EXCEPTION_SPECIFICATION
879 indicates what exceptions can be thrown. */
881 cp_declarator *
882 make_call_declarator (cp_declarator *target,
883 cp_parameter_declarator *parms,
884 cp_cv_quals cv_qualifiers,
885 tree exception_specification)
887 cp_declarator *declarator;
889 declarator = make_declarator (cdk_function);
890 declarator->declarator = target;
891 declarator->u.function.parameters = parms;
892 declarator->u.function.qualifiers = cv_qualifiers;
893 declarator->u.function.exception_specification = exception_specification;
895 return declarator;
898 /* Make a declarator for an array of BOUNDS elements, each of which is
899 defined by ELEMENT. */
901 cp_declarator *
902 make_array_declarator (cp_declarator *element, tree bounds)
904 cp_declarator *declarator;
906 declarator = make_declarator (cdk_array);
907 declarator->declarator = element;
908 declarator->u.array.bounds = bounds;
910 return declarator;
913 cp_parameter_declarator *no_parameters;
915 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
916 DECLARATOR and DEFAULT_ARGUMENT. */
918 cp_parameter_declarator *
919 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
920 cp_declarator *declarator,
921 tree default_argument)
923 cp_parameter_declarator *parameter;
925 parameter = ((cp_parameter_declarator *)
926 alloc_declarator (sizeof (cp_parameter_declarator)));
927 parameter->next = NULL;
928 if (decl_specifiers)
929 parameter->decl_specifiers = *decl_specifiers;
930 else
931 clear_decl_specs (&parameter->decl_specifiers);
932 parameter->declarator = declarator;
933 parameter->default_argument = default_argument;
934 parameter->ellipsis_p = false;
936 return parameter;
939 /* The parser. */
941 /* Overview
942 --------
944 A cp_parser parses the token stream as specified by the C++
945 grammar. Its job is purely parsing, not semantic analysis. For
946 example, the parser breaks the token stream into declarators,
947 expressions, statements, and other similar syntactic constructs.
948 It does not check that the types of the expressions on either side
949 of an assignment-statement are compatible, or that a function is
950 not declared with a parameter of type `void'.
952 The parser invokes routines elsewhere in the compiler to perform
953 semantic analysis and to build up the abstract syntax tree for the
954 code processed.
956 The parser (and the template instantiation code, which is, in a
957 way, a close relative of parsing) are the only parts of the
958 compiler that should be calling push_scope and pop_scope, or
959 related functions. The parser (and template instantiation code)
960 keeps track of what scope is presently active; everything else
961 should simply honor that. (The code that generates static
962 initializers may also need to set the scope, in order to check
963 access control correctly when emitting the initializers.)
965 Methodology
966 -----------
968 The parser is of the standard recursive-descent variety. Upcoming
969 tokens in the token stream are examined in order to determine which
970 production to use when parsing a non-terminal. Some C++ constructs
971 require arbitrary look ahead to disambiguate. For example, it is
972 impossible, in the general case, to tell whether a statement is an
973 expression or declaration without scanning the entire statement.
974 Therefore, the parser is capable of "parsing tentatively." When the
975 parser is not sure what construct comes next, it enters this mode.
976 Then, while we attempt to parse the construct, the parser queues up
977 error messages, rather than issuing them immediately, and saves the
978 tokens it consumes. If the construct is parsed successfully, the
979 parser "commits", i.e., it issues any queued error messages and
980 the tokens that were being preserved are permanently discarded.
981 If, however, the construct is not parsed successfully, the parser
982 rolls back its state completely so that it can resume parsing using
983 a different alternative.
985 Future Improvements
986 -------------------
988 The performance of the parser could probably be improved substantially.
989 We could often eliminate the need to parse tentatively by looking ahead
990 a little bit. In some places, this approach might not entirely eliminate
991 the need to parse tentatively, but it might still speed up the average
992 case. */
994 /* Flags that are passed to some parsing functions. These values can
995 be bitwise-ored together. */
997 typedef enum cp_parser_flags
999 /* No flags. */
1000 CP_PARSER_FLAGS_NONE = 0x0,
1001 /* The construct is optional. If it is not present, then no error
1002 should be issued. */
1003 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1004 /* When parsing a type-specifier, do not allow user-defined types. */
1005 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1006 } cp_parser_flags;
1008 /* The different kinds of declarators we want to parse. */
1010 typedef enum cp_parser_declarator_kind
1012 /* We want an abstract declarator. */
1013 CP_PARSER_DECLARATOR_ABSTRACT,
1014 /* We want a named declarator. */
1015 CP_PARSER_DECLARATOR_NAMED,
1016 /* We don't mind, but the name must be an unqualified-id. */
1017 CP_PARSER_DECLARATOR_EITHER
1018 } cp_parser_declarator_kind;
1020 /* The precedence values used to parse binary expressions. The minimum value
1021 of PREC must be 1, because zero is reserved to quickly discriminate
1022 binary operators from other tokens. */
1024 enum cp_parser_prec
1026 PREC_NOT_OPERATOR,
1027 PREC_LOGICAL_OR_EXPRESSION,
1028 PREC_LOGICAL_AND_EXPRESSION,
1029 PREC_INCLUSIVE_OR_EXPRESSION,
1030 PREC_EXCLUSIVE_OR_EXPRESSION,
1031 PREC_AND_EXPRESSION,
1032 PREC_EQUALITY_EXPRESSION,
1033 PREC_RELATIONAL_EXPRESSION,
1034 PREC_SHIFT_EXPRESSION,
1035 PREC_ADDITIVE_EXPRESSION,
1036 PREC_MULTIPLICATIVE_EXPRESSION,
1037 PREC_PM_EXPRESSION,
1038 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1041 /* A mapping from a token type to a corresponding tree node type, with a
1042 precedence value. */
1044 typedef struct cp_parser_binary_operations_map_node
1046 /* The token type. */
1047 enum cpp_ttype token_type;
1048 /* The corresponding tree code. */
1049 enum tree_code tree_type;
1050 /* The precedence of this operator. */
1051 enum cp_parser_prec prec;
1052 } cp_parser_binary_operations_map_node;
1054 /* The status of a tentative parse. */
1056 typedef enum cp_parser_status_kind
1058 /* No errors have occurred. */
1059 CP_PARSER_STATUS_KIND_NO_ERROR,
1060 /* An error has occurred. */
1061 CP_PARSER_STATUS_KIND_ERROR,
1062 /* We are committed to this tentative parse, whether or not an error
1063 has occurred. */
1064 CP_PARSER_STATUS_KIND_COMMITTED
1065 } cp_parser_status_kind;
1067 typedef struct cp_parser_expression_stack_entry
1069 tree lhs;
1070 enum tree_code tree_type;
1071 int prec;
1072 } cp_parser_expression_stack_entry;
1074 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1075 entries because precedence levels on the stack are monotonically
1076 increasing. */
1077 typedef struct cp_parser_expression_stack_entry
1078 cp_parser_expression_stack[NUM_PREC_VALUES];
1080 /* Context that is saved and restored when parsing tentatively. */
1081 typedef struct cp_parser_context GTY (())
1083 /* If this is a tentative parsing context, the status of the
1084 tentative parse. */
1085 enum cp_parser_status_kind status;
1086 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1087 that are looked up in this context must be looked up both in the
1088 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1089 the context of the containing expression. */
1090 tree object_type;
1092 /* The next parsing context in the stack. */
1093 struct cp_parser_context *next;
1094 } cp_parser_context;
1096 /* Prototypes. */
1098 /* Constructors and destructors. */
1100 static cp_parser_context *cp_parser_context_new
1101 (cp_parser_context *);
1103 /* Class variables. */
1105 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1107 /* The operator-precedence table used by cp_parser_binary_expression.
1108 Transformed into an associative array (binops_by_token) by
1109 cp_parser_new. */
1111 static const cp_parser_binary_operations_map_node binops[] = {
1112 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1113 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1115 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1116 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1117 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1119 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1120 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1122 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1123 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1125 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1126 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1127 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1128 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1129 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1130 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1132 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1133 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1135 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1137 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1139 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1141 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1143 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1146 /* The same as binops, but initialized by cp_parser_new so that
1147 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1148 for speed. */
1149 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1151 /* Constructors and destructors. */
1153 /* Construct a new context. The context below this one on the stack
1154 is given by NEXT. */
1156 static cp_parser_context *
1157 cp_parser_context_new (cp_parser_context* next)
1159 cp_parser_context *context;
1161 /* Allocate the storage. */
1162 if (cp_parser_context_free_list != NULL)
1164 /* Pull the first entry from the free list. */
1165 context = cp_parser_context_free_list;
1166 cp_parser_context_free_list = context->next;
1167 memset (context, 0, sizeof (*context));
1169 else
1170 context = GGC_CNEW (cp_parser_context);
1172 /* No errors have occurred yet in this context. */
1173 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1174 /* If this is not the bottomost context, copy information that we
1175 need from the previous context. */
1176 if (next)
1178 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1179 expression, then we are parsing one in this context, too. */
1180 context->object_type = next->object_type;
1181 /* Thread the stack. */
1182 context->next = next;
1185 return context;
1188 /* The cp_parser structure represents the C++ parser. */
1190 typedef struct cp_parser GTY(())
1192 /* The lexer from which we are obtaining tokens. */
1193 cp_lexer *lexer;
1195 /* The scope in which names should be looked up. If NULL_TREE, then
1196 we look up names in the scope that is currently open in the
1197 source program. If non-NULL, this is either a TYPE or
1198 NAMESPACE_DECL for the scope in which we should look.
1200 This value is not cleared automatically after a name is looked
1201 up, so we must be careful to clear it before starting a new look
1202 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1203 will look up `Z' in the scope of `X', rather than the current
1204 scope.) Unfortunately, it is difficult to tell when name lookup
1205 is complete, because we sometimes peek at a token, look it up,
1206 and then decide not to consume it. */
1207 tree scope;
1209 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1210 last lookup took place. OBJECT_SCOPE is used if an expression
1211 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1212 respectively. QUALIFYING_SCOPE is used for an expression of the
1213 form "X::Y"; it refers to X. */
1214 tree object_scope;
1215 tree qualifying_scope;
1217 /* A stack of parsing contexts. All but the bottom entry on the
1218 stack will be tentative contexts.
1220 We parse tentatively in order to determine which construct is in
1221 use in some situations. For example, in order to determine
1222 whether a statement is an expression-statement or a
1223 declaration-statement we parse it tentatively as a
1224 declaration-statement. If that fails, we then reparse the same
1225 token stream as an expression-statement. */
1226 cp_parser_context *context;
1228 /* True if we are parsing GNU C++. If this flag is not set, then
1229 GNU extensions are not recognized. */
1230 bool allow_gnu_extensions_p;
1232 /* TRUE if the `>' token should be interpreted as the greater-than
1233 operator. FALSE if it is the end of a template-id or
1234 template-parameter-list. */
1235 bool greater_than_is_operator_p;
1237 /* TRUE if default arguments are allowed within a parameter list
1238 that starts at this point. FALSE if only a gnu extension makes
1239 them permissible. */
1240 bool default_arg_ok_p;
1242 /* TRUE if we are parsing an integral constant-expression. See
1243 [expr.const] for a precise definition. */
1244 bool integral_constant_expression_p;
1246 /* TRUE if we are parsing an integral constant-expression -- but a
1247 non-constant expression should be permitted as well. This flag
1248 is used when parsing an array bound so that GNU variable-length
1249 arrays are tolerated. */
1250 bool allow_non_integral_constant_expression_p;
1252 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1253 been seen that makes the expression non-constant. */
1254 bool non_integral_constant_expression_p;
1256 /* TRUE if local variable names and `this' are forbidden in the
1257 current context. */
1258 bool local_variables_forbidden_p;
1260 /* TRUE if the declaration we are parsing is part of a
1261 linkage-specification of the form `extern string-literal
1262 declaration'. */
1263 bool in_unbraced_linkage_specification_p;
1265 /* TRUE if we are presently parsing a declarator, after the
1266 direct-declarator. */
1267 bool in_declarator_p;
1269 /* TRUE if we are presently parsing a template-argument-list. */
1270 bool in_template_argument_list_p;
1272 /* TRUE if we are presently parsing the body of an
1273 iteration-statement. */
1274 bool in_iteration_statement_p;
1276 /* TRUE if we are presently parsing the body of a switch
1277 statement. */
1278 bool in_switch_statement_p;
1280 /* TRUE if we are parsing a type-id in an expression context. In
1281 such a situation, both "type (expr)" and "type (type)" are valid
1282 alternatives. */
1283 bool in_type_id_in_expr_p;
1285 /* TRUE if we are currently in a header file where declarations are
1286 implicitly extern "C". */
1287 bool implicit_extern_c;
1289 /* TRUE if strings in expressions should be translated to the execution
1290 character set. */
1291 bool translate_strings_p;
1293 /* If non-NULL, then we are parsing a construct where new type
1294 definitions are not permitted. The string stored here will be
1295 issued as an error message if a type is defined. */
1296 const char *type_definition_forbidden_message;
1298 /* A list of lists. The outer list is a stack, used for member
1299 functions of local classes. At each level there are two sub-list,
1300 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1301 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1302 TREE_VALUE's. The functions are chained in reverse declaration
1303 order.
1305 The TREE_PURPOSE sublist contains those functions with default
1306 arguments that need post processing, and the TREE_VALUE sublist
1307 contains those functions with definitions that need post
1308 processing.
1310 These lists can only be processed once the outermost class being
1311 defined is complete. */
1312 tree unparsed_functions_queues;
1314 /* The number of classes whose definitions are currently in
1315 progress. */
1316 unsigned num_classes_being_defined;
1318 /* The number of template parameter lists that apply directly to the
1319 current declaration. */
1320 unsigned num_template_parameter_lists;
1321 } cp_parser;
1323 /* The type of a function that parses some kind of expression. */
1324 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1326 /* Prototypes. */
1328 /* Constructors and destructors. */
1330 static cp_parser *cp_parser_new
1331 (void);
1333 /* Routines to parse various constructs.
1335 Those that return `tree' will return the error_mark_node (rather
1336 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1337 Sometimes, they will return an ordinary node if error-recovery was
1338 attempted, even though a parse error occurred. So, to check
1339 whether or not a parse error occurred, you should always use
1340 cp_parser_error_occurred. If the construct is optional (indicated
1341 either by an `_opt' in the name of the function that does the
1342 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1343 the construct is not present. */
1345 /* Lexical conventions [gram.lex] */
1347 static tree cp_parser_identifier
1348 (cp_parser *);
1349 static tree cp_parser_string_literal
1350 (cp_parser *, bool, bool);
1352 /* Basic concepts [gram.basic] */
1354 static bool cp_parser_translation_unit
1355 (cp_parser *);
1357 /* Expressions [gram.expr] */
1359 static tree cp_parser_primary_expression
1360 (cp_parser *, bool, cp_id_kind *, tree *);
1361 static tree cp_parser_id_expression
1362 (cp_parser *, bool, bool, bool *, bool);
1363 static tree cp_parser_unqualified_id
1364 (cp_parser *, bool, bool, bool);
1365 static tree cp_parser_nested_name_specifier_opt
1366 (cp_parser *, bool, bool, bool, bool);
1367 static tree cp_parser_nested_name_specifier
1368 (cp_parser *, bool, bool, bool, bool);
1369 static tree cp_parser_class_or_namespace_name
1370 (cp_parser *, bool, bool, bool, bool, bool);
1371 static tree cp_parser_postfix_expression
1372 (cp_parser *, bool, bool);
1373 static tree cp_parser_postfix_open_square_expression
1374 (cp_parser *, tree, bool);
1375 static tree cp_parser_postfix_dot_deref_expression
1376 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1377 static tree cp_parser_parenthesized_expression_list
1378 (cp_parser *, bool, bool, bool *);
1379 static void cp_parser_pseudo_destructor_name
1380 (cp_parser *, tree *, tree *);
1381 static tree cp_parser_unary_expression
1382 (cp_parser *, bool, bool);
1383 static enum tree_code cp_parser_unary_operator
1384 (cp_token *);
1385 static tree cp_parser_new_expression
1386 (cp_parser *);
1387 static tree cp_parser_new_placement
1388 (cp_parser *);
1389 static tree cp_parser_new_type_id
1390 (cp_parser *, tree *);
1391 static cp_declarator *cp_parser_new_declarator_opt
1392 (cp_parser *);
1393 static cp_declarator *cp_parser_direct_new_declarator
1394 (cp_parser *);
1395 static tree cp_parser_new_initializer
1396 (cp_parser *);
1397 static tree cp_parser_delete_expression
1398 (cp_parser *);
1399 static tree cp_parser_cast_expression
1400 (cp_parser *, bool, bool);
1401 static tree cp_parser_binary_expression
1402 (cp_parser *, bool);
1403 static tree cp_parser_question_colon_clause
1404 (cp_parser *, tree);
1405 static tree cp_parser_assignment_expression
1406 (cp_parser *, bool);
1407 static enum tree_code cp_parser_assignment_operator_opt
1408 (cp_parser *);
1409 static tree cp_parser_expression
1410 (cp_parser *, bool);
1411 static tree cp_parser_constant_expression
1412 (cp_parser *, bool, bool *);
1413 static tree cp_parser_builtin_offsetof
1414 (cp_parser *);
1416 /* Statements [gram.stmt.stmt] */
1418 static void cp_parser_statement
1419 (cp_parser *, tree);
1420 static tree cp_parser_labeled_statement
1421 (cp_parser *, tree);
1422 static tree cp_parser_expression_statement
1423 (cp_parser *, tree);
1424 static tree cp_parser_compound_statement
1425 (cp_parser *, tree, bool);
1426 static void cp_parser_statement_seq_opt
1427 (cp_parser *, tree);
1428 static tree cp_parser_selection_statement
1429 (cp_parser *);
1430 static tree cp_parser_condition
1431 (cp_parser *);
1432 static tree cp_parser_iteration_statement
1433 (cp_parser *);
1434 static void cp_parser_for_init_statement
1435 (cp_parser *);
1436 static tree cp_parser_jump_statement
1437 (cp_parser *);
1438 static void cp_parser_declaration_statement
1439 (cp_parser *);
1441 static tree cp_parser_implicitly_scoped_statement
1442 (cp_parser *);
1443 static void cp_parser_already_scoped_statement
1444 (cp_parser *);
1446 /* Declarations [gram.dcl.dcl] */
1448 static void cp_parser_declaration_seq_opt
1449 (cp_parser *);
1450 static void cp_parser_declaration
1451 (cp_parser *);
1452 static void cp_parser_block_declaration
1453 (cp_parser *, bool);
1454 static void cp_parser_simple_declaration
1455 (cp_parser *, bool);
1456 static void cp_parser_decl_specifier_seq
1457 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1458 static tree cp_parser_storage_class_specifier_opt
1459 (cp_parser *);
1460 static tree cp_parser_function_specifier_opt
1461 (cp_parser *, cp_decl_specifier_seq *);
1462 static tree cp_parser_type_specifier
1463 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1464 int *, bool *);
1465 static tree cp_parser_simple_type_specifier
1466 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1467 static tree cp_parser_type_name
1468 (cp_parser *);
1469 static tree cp_parser_elaborated_type_specifier
1470 (cp_parser *, bool, bool);
1471 static tree cp_parser_enum_specifier
1472 (cp_parser *);
1473 static void cp_parser_enumerator_list
1474 (cp_parser *, tree);
1475 static void cp_parser_enumerator_definition
1476 (cp_parser *, tree);
1477 static tree cp_parser_namespace_name
1478 (cp_parser *);
1479 static void cp_parser_namespace_definition
1480 (cp_parser *);
1481 static void cp_parser_namespace_body
1482 (cp_parser *);
1483 static tree cp_parser_qualified_namespace_specifier
1484 (cp_parser *);
1485 static void cp_parser_namespace_alias_definition
1486 (cp_parser *);
1487 static void cp_parser_using_declaration
1488 (cp_parser *);
1489 static void cp_parser_using_directive
1490 (cp_parser *);
1491 static void cp_parser_asm_definition
1492 (cp_parser *);
1493 static void cp_parser_linkage_specification
1494 (cp_parser *);
1496 /* Declarators [gram.dcl.decl] */
1498 static tree cp_parser_init_declarator
1499 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1500 static cp_declarator *cp_parser_declarator
1501 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1502 static cp_declarator *cp_parser_direct_declarator
1503 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1504 static enum tree_code cp_parser_ptr_operator
1505 (cp_parser *, tree *, cp_cv_quals *);
1506 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1507 (cp_parser *);
1508 static tree cp_parser_declarator_id
1509 (cp_parser *);
1510 static tree cp_parser_type_id
1511 (cp_parser *);
1512 static void cp_parser_type_specifier_seq
1513 (cp_parser *, cp_decl_specifier_seq *);
1514 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1515 (cp_parser *);
1516 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1517 (cp_parser *, bool *);
1518 static cp_parameter_declarator *cp_parser_parameter_declaration
1519 (cp_parser *, bool, bool *);
1520 static void cp_parser_function_body
1521 (cp_parser *);
1522 static tree cp_parser_initializer
1523 (cp_parser *, bool *, bool *);
1524 static tree cp_parser_initializer_clause
1525 (cp_parser *, bool *);
1526 static tree cp_parser_initializer_list
1527 (cp_parser *, bool *);
1529 static bool cp_parser_ctor_initializer_opt_and_function_body
1530 (cp_parser *);
1532 /* Classes [gram.class] */
1534 static tree cp_parser_class_name
1535 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1536 static tree cp_parser_class_specifier
1537 (cp_parser *);
1538 static tree cp_parser_class_head
1539 (cp_parser *, bool *, tree *);
1540 static enum tag_types cp_parser_class_key
1541 (cp_parser *);
1542 static void cp_parser_member_specification_opt
1543 (cp_parser *);
1544 static void cp_parser_member_declaration
1545 (cp_parser *);
1546 static tree cp_parser_pure_specifier
1547 (cp_parser *);
1548 static tree cp_parser_constant_initializer
1549 (cp_parser *);
1551 /* Derived classes [gram.class.derived] */
1553 static tree cp_parser_base_clause
1554 (cp_parser *);
1555 static tree cp_parser_base_specifier
1556 (cp_parser *);
1558 /* Special member functions [gram.special] */
1560 static tree cp_parser_conversion_function_id
1561 (cp_parser *);
1562 static tree cp_parser_conversion_type_id
1563 (cp_parser *);
1564 static cp_declarator *cp_parser_conversion_declarator_opt
1565 (cp_parser *);
1566 static bool cp_parser_ctor_initializer_opt
1567 (cp_parser *);
1568 static void cp_parser_mem_initializer_list
1569 (cp_parser *);
1570 static tree cp_parser_mem_initializer
1571 (cp_parser *);
1572 static tree cp_parser_mem_initializer_id
1573 (cp_parser *);
1575 /* Overloading [gram.over] */
1577 static tree cp_parser_operator_function_id
1578 (cp_parser *);
1579 static tree cp_parser_operator
1580 (cp_parser *);
1582 /* Templates [gram.temp] */
1584 static void cp_parser_template_declaration
1585 (cp_parser *, bool);
1586 static tree cp_parser_template_parameter_list
1587 (cp_parser *);
1588 static tree cp_parser_template_parameter
1589 (cp_parser *, bool *);
1590 static tree cp_parser_type_parameter
1591 (cp_parser *);
1592 static tree cp_parser_template_id
1593 (cp_parser *, bool, bool, bool);
1594 static tree cp_parser_template_name
1595 (cp_parser *, bool, bool, bool, bool *);
1596 static tree cp_parser_template_argument_list
1597 (cp_parser *);
1598 static tree cp_parser_template_argument
1599 (cp_parser *);
1600 static void cp_parser_explicit_instantiation
1601 (cp_parser *);
1602 static void cp_parser_explicit_specialization
1603 (cp_parser *);
1605 /* Exception handling [gram.exception] */
1607 static tree cp_parser_try_block
1608 (cp_parser *);
1609 static bool cp_parser_function_try_block
1610 (cp_parser *);
1611 static void cp_parser_handler_seq
1612 (cp_parser *);
1613 static void cp_parser_handler
1614 (cp_parser *);
1615 static tree cp_parser_exception_declaration
1616 (cp_parser *);
1617 static tree cp_parser_throw_expression
1618 (cp_parser *);
1619 static tree cp_parser_exception_specification_opt
1620 (cp_parser *);
1621 static tree cp_parser_type_id_list
1622 (cp_parser *);
1624 /* GNU Extensions */
1626 static tree cp_parser_asm_specification_opt
1627 (cp_parser *);
1628 static tree cp_parser_asm_operand_list
1629 (cp_parser *);
1630 static tree cp_parser_asm_clobber_list
1631 (cp_parser *);
1632 static tree cp_parser_attributes_opt
1633 (cp_parser *);
1634 static tree cp_parser_attribute_list
1635 (cp_parser *);
1636 static bool cp_parser_extension_opt
1637 (cp_parser *, int *);
1638 static void cp_parser_label_declaration
1639 (cp_parser *);
1641 /* Utility Routines */
1643 static tree cp_parser_lookup_name
1644 (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1645 static tree cp_parser_lookup_name_simple
1646 (cp_parser *, tree);
1647 static tree cp_parser_maybe_treat_template_as_class
1648 (tree, bool);
1649 static bool cp_parser_check_declarator_template_parameters
1650 (cp_parser *, cp_declarator *);
1651 static bool cp_parser_check_template_parameters
1652 (cp_parser *, unsigned);
1653 static tree cp_parser_simple_cast_expression
1654 (cp_parser *);
1655 static tree cp_parser_global_scope_opt
1656 (cp_parser *, bool);
1657 static bool cp_parser_constructor_declarator_p
1658 (cp_parser *, bool);
1659 static tree cp_parser_function_definition_from_specifiers_and_declarator
1660 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1661 static tree cp_parser_function_definition_after_declarator
1662 (cp_parser *, bool);
1663 static void cp_parser_template_declaration_after_export
1664 (cp_parser *, bool);
1665 static tree cp_parser_single_declaration
1666 (cp_parser *, bool, bool *);
1667 static tree cp_parser_functional_cast
1668 (cp_parser *, tree);
1669 static tree cp_parser_save_member_function_body
1670 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1671 static tree cp_parser_enclosed_template_argument_list
1672 (cp_parser *);
1673 static void cp_parser_save_default_args
1674 (cp_parser *, tree);
1675 static void cp_parser_late_parsing_for_member
1676 (cp_parser *, tree);
1677 static void cp_parser_late_parsing_default_args
1678 (cp_parser *, tree);
1679 static tree cp_parser_sizeof_operand
1680 (cp_parser *, enum rid);
1681 static bool cp_parser_declares_only_class_p
1682 (cp_parser *);
1683 static void cp_parser_set_storage_class
1684 (cp_decl_specifier_seq *, cp_storage_class);
1685 static void cp_parser_set_decl_spec_type
1686 (cp_decl_specifier_seq *, tree, bool);
1687 static bool cp_parser_friend_p
1688 (const cp_decl_specifier_seq *);
1689 static cp_token *cp_parser_require
1690 (cp_parser *, enum cpp_ttype, const char *);
1691 static cp_token *cp_parser_require_keyword
1692 (cp_parser *, enum rid, const char *);
1693 static bool cp_parser_token_starts_function_definition_p
1694 (cp_token *);
1695 static bool cp_parser_next_token_starts_class_definition_p
1696 (cp_parser *);
1697 static bool cp_parser_next_token_ends_template_argument_p
1698 (cp_parser *);
1699 static bool cp_parser_nth_token_starts_template_argument_list_p
1700 (cp_parser *, size_t);
1701 static enum tag_types cp_parser_token_is_class_key
1702 (cp_token *);
1703 static void cp_parser_check_class_key
1704 (enum tag_types, tree type);
1705 static void cp_parser_check_access_in_redeclaration
1706 (tree type);
1707 static bool cp_parser_optional_template_keyword
1708 (cp_parser *);
1709 static void cp_parser_pre_parsed_nested_name_specifier
1710 (cp_parser *);
1711 static void cp_parser_cache_group
1712 (cp_parser *, enum cpp_ttype, unsigned);
1713 static void cp_parser_parse_tentatively
1714 (cp_parser *);
1715 static void cp_parser_commit_to_tentative_parse
1716 (cp_parser *);
1717 static void cp_parser_abort_tentative_parse
1718 (cp_parser *);
1719 static bool cp_parser_parse_definitely
1720 (cp_parser *);
1721 static inline bool cp_parser_parsing_tentatively
1722 (cp_parser *);
1723 static bool cp_parser_uncommitted_to_tentative_parse_p
1724 (cp_parser *);
1725 static void cp_parser_error
1726 (cp_parser *, const char *);
1727 static void cp_parser_name_lookup_error
1728 (cp_parser *, tree, tree, const char *);
1729 static bool cp_parser_simulate_error
1730 (cp_parser *);
1731 static void cp_parser_check_type_definition
1732 (cp_parser *);
1733 static void cp_parser_check_for_definition_in_return_type
1734 (cp_declarator *, tree);
1735 static void cp_parser_check_for_invalid_template_id
1736 (cp_parser *, tree);
1737 static bool cp_parser_non_integral_constant_expression
1738 (cp_parser *, const char *);
1739 static void cp_parser_diagnose_invalid_type_name
1740 (cp_parser *, tree, tree);
1741 static bool cp_parser_parse_and_diagnose_invalid_type_name
1742 (cp_parser *);
1743 static int cp_parser_skip_to_closing_parenthesis
1744 (cp_parser *, bool, bool, bool);
1745 static void cp_parser_skip_to_end_of_statement
1746 (cp_parser *);
1747 static void cp_parser_consume_semicolon_at_end_of_statement
1748 (cp_parser *);
1749 static void cp_parser_skip_to_end_of_block_or_statement
1750 (cp_parser *);
1751 static void cp_parser_skip_to_closing_brace
1752 (cp_parser *);
1753 static void cp_parser_skip_until_found
1754 (cp_parser *, enum cpp_ttype, const char *);
1755 static bool cp_parser_error_occurred
1756 (cp_parser *);
1757 static bool cp_parser_allow_gnu_extensions_p
1758 (cp_parser *);
1759 static bool cp_parser_is_string_literal
1760 (cp_token *);
1761 static bool cp_parser_is_keyword
1762 (cp_token *, enum rid);
1763 static tree cp_parser_make_typename_type
1764 (cp_parser *, tree, tree);
1766 /* Returns nonzero if we are parsing tentatively. */
1768 static inline bool
1769 cp_parser_parsing_tentatively (cp_parser* parser)
1771 return parser->context->next != NULL;
1774 /* Returns nonzero if TOKEN is a string literal. */
1776 static bool
1777 cp_parser_is_string_literal (cp_token* token)
1779 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1782 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1784 static bool
1785 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1787 return token->keyword == keyword;
1790 /* A minimum or maximum operator has been seen. As these are
1791 deprecated, issue a warning. */
1793 static inline void
1794 cp_parser_warn_min_max (void)
1796 if (warn_deprecated && !in_system_header)
1797 warning ("minimum/maximum operators are deprecated");
1800 /* If not parsing tentatively, issue a diagnostic of the form
1801 FILE:LINE: MESSAGE before TOKEN
1802 where TOKEN is the next token in the input stream. MESSAGE
1803 (specified by the caller) is usually of the form "expected
1804 OTHER-TOKEN". */
1806 static void
1807 cp_parser_error (cp_parser* parser, const char* message)
1809 if (!cp_parser_simulate_error (parser))
1811 cp_token *token = cp_lexer_peek_token (parser->lexer);
1812 /* This diagnostic makes more sense if it is tagged to the line
1813 of the token we just peeked at. */
1814 cp_lexer_set_source_position_from_token (token);
1815 if (token->type == CPP_PRAGMA)
1817 error ("%<#pragma%> is not allowed here");
1818 cp_lexer_purge_token (parser->lexer);
1819 return;
1821 c_parse_error (message,
1822 /* Because c_parser_error does not understand
1823 CPP_KEYWORD, keywords are treated like
1824 identifiers. */
1825 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1826 token->value);
1830 /* Issue an error about name-lookup failing. NAME is the
1831 IDENTIFIER_NODE DECL is the result of
1832 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1833 the thing that we hoped to find. */
1835 static void
1836 cp_parser_name_lookup_error (cp_parser* parser,
1837 tree name,
1838 tree decl,
1839 const char* desired)
1841 /* If name lookup completely failed, tell the user that NAME was not
1842 declared. */
1843 if (decl == error_mark_node)
1845 if (parser->scope && parser->scope != global_namespace)
1846 error ("%<%D::%D%> has not been declared",
1847 parser->scope, name);
1848 else if (parser->scope == global_namespace)
1849 error ("%<::%D%> has not been declared", name);
1850 else if (parser->object_scope
1851 && !CLASS_TYPE_P (parser->object_scope))
1852 error ("request for member %qD in non-class type %qT",
1853 name, parser->object_scope);
1854 else if (parser->object_scope)
1855 error ("%<%T::%D%> has not been declared",
1856 parser->object_scope, name);
1857 else
1858 error ("%qD has not been declared", name);
1860 else if (parser->scope && parser->scope != global_namespace)
1861 error ("%<%D::%D%> %s", parser->scope, name, desired);
1862 else if (parser->scope == global_namespace)
1863 error ("%<::%D%> %s", name, desired);
1864 else
1865 error ("%qD %s", name, desired);
1868 /* If we are parsing tentatively, remember that an error has occurred
1869 during this tentative parse. Returns true if the error was
1870 simulated; false if a message should be issued by the caller. */
1872 static bool
1873 cp_parser_simulate_error (cp_parser* parser)
1875 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1877 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1878 return true;
1880 return false;
1883 /* This function is called when a type is defined. If type
1884 definitions are forbidden at this point, an error message is
1885 issued. */
1887 static void
1888 cp_parser_check_type_definition (cp_parser* parser)
1890 /* If types are forbidden here, issue a message. */
1891 if (parser->type_definition_forbidden_message)
1892 /* Use `%s' to print the string in case there are any escape
1893 characters in the message. */
1894 error ("%s", parser->type_definition_forbidden_message);
1897 /* This function is called when the DECLARATOR is processed. The TYPE
1898 was a type defined in the decl-specifiers. If it is invalid to
1899 define a type in the decl-specifiers for DECLARATOR, an error is
1900 issued. */
1902 static void
1903 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1904 tree type)
1906 /* [dcl.fct] forbids type definitions in return types.
1907 Unfortunately, it's not easy to know whether or not we are
1908 processing a return type until after the fact. */
1909 while (declarator
1910 && (declarator->kind == cdk_pointer
1911 || declarator->kind == cdk_reference
1912 || declarator->kind == cdk_ptrmem))
1913 declarator = declarator->declarator;
1914 if (declarator
1915 && declarator->kind == cdk_function)
1917 error ("new types may not be defined in a return type");
1918 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1919 type);
1923 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1924 "<" in any valid C++ program. If the next token is indeed "<",
1925 issue a message warning the user about what appears to be an
1926 invalid attempt to form a template-id. */
1928 static void
1929 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1930 tree type)
1932 cp_token_position start = 0;
1934 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1936 if (TYPE_P (type))
1937 error ("%qT is not a template", type);
1938 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1939 error ("%qE is not a template", type);
1940 else
1941 error ("invalid template-id");
1942 /* Remember the location of the invalid "<". */
1943 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1944 start = cp_lexer_token_position (parser->lexer, true);
1945 /* Consume the "<". */
1946 cp_lexer_consume_token (parser->lexer);
1947 /* Parse the template arguments. */
1948 cp_parser_enclosed_template_argument_list (parser);
1949 /* Permanently remove the invalid template arguments so that
1950 this error message is not issued again. */
1951 if (start)
1952 cp_lexer_purge_tokens_after (parser->lexer, start);
1956 /* If parsing an integral constant-expression, issue an error message
1957 about the fact that THING appeared and return true. Otherwise,
1958 return false. In either case, set
1959 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
1961 static bool
1962 cp_parser_non_integral_constant_expression (cp_parser *parser,
1963 const char *thing)
1965 parser->non_integral_constant_expression_p = true;
1966 if (parser->integral_constant_expression_p)
1968 if (!parser->allow_non_integral_constant_expression_p)
1970 error ("%s cannot appear in a constant-expression", thing);
1971 return true;
1974 return false;
1977 /* Emit a diagnostic for an invalid type name. SCOPE is the
1978 qualifying scope (or NULL, if none) for ID. This function commits
1979 to the current active tentative parse, if any. (Otherwise, the
1980 problematic construct might be encountered again later, resulting
1981 in duplicate error messages.) */
1983 static void
1984 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1986 tree decl, old_scope;
1987 /* Try to lookup the identifier. */
1988 old_scope = parser->scope;
1989 parser->scope = scope;
1990 decl = cp_parser_lookup_name_simple (parser, id);
1991 parser->scope = old_scope;
1992 /* If the lookup found a template-name, it means that the user forgot
1993 to specify an argument list. Emit an useful error message. */
1994 if (TREE_CODE (decl) == TEMPLATE_DECL)
1995 error ("invalid use of template-name %qE without an argument list",
1996 decl);
1997 else if (!parser->scope)
1999 /* Issue an error message. */
2000 error ("%qE does not name a type", id);
2001 /* If we're in a template class, it's possible that the user was
2002 referring to a type from a base class. For example:
2004 template <typename T> struct A { typedef T X; };
2005 template <typename T> struct B : public A<T> { X x; };
2007 The user should have said "typename A<T>::X". */
2008 if (processing_template_decl && current_class_type
2009 && TYPE_BINFO (current_class_type))
2011 tree b;
2013 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2015 b = TREE_CHAIN (b))
2017 tree base_type = BINFO_TYPE (b);
2018 if (CLASS_TYPE_P (base_type)
2019 && dependent_type_p (base_type))
2021 tree field;
2022 /* Go from a particular instantiation of the
2023 template (which will have an empty TYPE_FIELDs),
2024 to the main version. */
2025 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2026 for (field = TYPE_FIELDS (base_type);
2027 field;
2028 field = TREE_CHAIN (field))
2029 if (TREE_CODE (field) == TYPE_DECL
2030 && DECL_NAME (field) == id)
2032 inform ("(perhaps %<typename %T::%E%> was intended)",
2033 BINFO_TYPE (b), id);
2034 break;
2036 if (field)
2037 break;
2042 /* Here we diagnose qualified-ids where the scope is actually correct,
2043 but the identifier does not resolve to a valid type name. */
2044 else
2046 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2047 error ("%qE in namespace %qE does not name a type",
2048 id, parser->scope);
2049 else if (TYPE_P (parser->scope))
2050 error ("%qE in class %qT does not name a type", id, parser->scope);
2051 else
2052 gcc_unreachable ();
2054 cp_parser_commit_to_tentative_parse (parser);
2057 /* Check for a common situation where a type-name should be present,
2058 but is not, and issue a sensible error message. Returns true if an
2059 invalid type-name was detected.
2061 The situation handled by this function are variable declarations of the
2062 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2063 Usually, `ID' should name a type, but if we got here it means that it
2064 does not. We try to emit the best possible error message depending on
2065 how exactly the id-expression looks like.
2068 static bool
2069 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2071 tree id;
2073 cp_parser_parse_tentatively (parser);
2074 id = cp_parser_id_expression (parser,
2075 /*template_keyword_p=*/false,
2076 /*check_dependency_p=*/true,
2077 /*template_p=*/NULL,
2078 /*declarator_p=*/true);
2079 /* After the id-expression, there should be a plain identifier,
2080 otherwise this is not a simple variable declaration. Also, if
2081 the scope is dependent, we cannot do much. */
2082 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2083 || (parser->scope && TYPE_P (parser->scope)
2084 && dependent_type_p (parser->scope)))
2086 cp_parser_abort_tentative_parse (parser);
2087 return false;
2089 if (!cp_parser_parse_definitely (parser)
2090 || TREE_CODE (id) != IDENTIFIER_NODE)
2091 return false;
2093 /* Emit a diagnostic for the invalid type. */
2094 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2095 /* Skip to the end of the declaration; there's no point in
2096 trying to process it. */
2097 cp_parser_skip_to_end_of_block_or_statement (parser);
2098 return true;
2101 /* Consume tokens up to, and including, the next non-nested closing `)'.
2102 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2103 are doing error recovery. Returns -1 if OR_COMMA is true and we
2104 found an unnested comma. */
2106 static int
2107 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2108 bool recovering,
2109 bool or_comma,
2110 bool consume_paren)
2112 unsigned paren_depth = 0;
2113 unsigned brace_depth = 0;
2114 int result;
2116 if (recovering && !or_comma
2117 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2118 return 0;
2120 while (true)
2122 cp_token *token;
2124 /* If we've run out of tokens, then there is no closing `)'. */
2125 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2127 result = 0;
2128 break;
2131 token = cp_lexer_peek_token (parser->lexer);
2133 /* This matches the processing in skip_to_end_of_statement. */
2134 if (token->type == CPP_SEMICOLON && !brace_depth)
2136 result = 0;
2137 break;
2139 if (token->type == CPP_OPEN_BRACE)
2140 ++brace_depth;
2141 if (token->type == CPP_CLOSE_BRACE)
2143 if (!brace_depth--)
2145 result = 0;
2146 break;
2149 if (recovering && or_comma && token->type == CPP_COMMA
2150 && !brace_depth && !paren_depth)
2152 result = -1;
2153 break;
2156 if (!brace_depth)
2158 /* If it is an `(', we have entered another level of nesting. */
2159 if (token->type == CPP_OPEN_PAREN)
2160 ++paren_depth;
2161 /* If it is a `)', then we might be done. */
2162 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2164 if (consume_paren)
2165 cp_lexer_consume_token (parser->lexer);
2167 result = 1;
2168 break;
2173 /* Consume the token. */
2174 cp_lexer_consume_token (parser->lexer);
2177 return result;
2180 /* Consume tokens until we reach the end of the current statement.
2181 Normally, that will be just before consuming a `;'. However, if a
2182 non-nested `}' comes first, then we stop before consuming that. */
2184 static void
2185 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2187 unsigned nesting_depth = 0;
2189 while (true)
2191 cp_token *token;
2193 /* Peek at the next token. */
2194 token = cp_lexer_peek_token (parser->lexer);
2195 /* If we've run out of tokens, stop. */
2196 if (token->type == CPP_EOF)
2197 break;
2198 /* If the next token is a `;', we have reached the end of the
2199 statement. */
2200 if (token->type == CPP_SEMICOLON && !nesting_depth)
2201 break;
2202 /* If the next token is a non-nested `}', then we have reached
2203 the end of the current block. */
2204 if (token->type == CPP_CLOSE_BRACE)
2206 /* If this is a non-nested `}', stop before consuming it.
2207 That way, when confronted with something like:
2209 { 3 + }
2211 we stop before consuming the closing `}', even though we
2212 have not yet reached a `;'. */
2213 if (nesting_depth == 0)
2214 break;
2215 /* If it is the closing `}' for a block that we have
2216 scanned, stop -- but only after consuming the token.
2217 That way given:
2219 void f g () { ... }
2220 typedef int I;
2222 we will stop after the body of the erroneously declared
2223 function, but before consuming the following `typedef'
2224 declaration. */
2225 if (--nesting_depth == 0)
2227 cp_lexer_consume_token (parser->lexer);
2228 break;
2231 /* If it the next token is a `{', then we are entering a new
2232 block. Consume the entire block. */
2233 else if (token->type == CPP_OPEN_BRACE)
2234 ++nesting_depth;
2235 /* Consume the token. */
2236 cp_lexer_consume_token (parser->lexer);
2240 /* This function is called at the end of a statement or declaration.
2241 If the next token is a semicolon, it is consumed; otherwise, error
2242 recovery is attempted. */
2244 static void
2245 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2247 /* Look for the trailing `;'. */
2248 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2250 /* If there is additional (erroneous) input, skip to the end of
2251 the statement. */
2252 cp_parser_skip_to_end_of_statement (parser);
2253 /* If the next token is now a `;', consume it. */
2254 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2255 cp_lexer_consume_token (parser->lexer);
2259 /* Skip tokens until we have consumed an entire block, or until we
2260 have consumed a non-nested `;'. */
2262 static void
2263 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2265 unsigned nesting_depth = 0;
2267 while (true)
2269 cp_token *token;
2271 /* Peek at the next token. */
2272 token = cp_lexer_peek_token (parser->lexer);
2273 /* If we've run out of tokens, stop. */
2274 if (token->type == CPP_EOF)
2275 break;
2276 /* If the next token is a `;', we have reached the end of the
2277 statement. */
2278 if (token->type == CPP_SEMICOLON && !nesting_depth)
2280 /* Consume the `;'. */
2281 cp_lexer_consume_token (parser->lexer);
2282 break;
2284 /* Consume the token. */
2285 token = cp_lexer_consume_token (parser->lexer);
2286 /* If the next token is a non-nested `}', then we have reached
2287 the end of the current block. */
2288 if (token->type == CPP_CLOSE_BRACE
2289 && (nesting_depth == 0 || --nesting_depth == 0))
2290 break;
2291 /* If it the next token is a `{', then we are entering a new
2292 block. Consume the entire block. */
2293 if (token->type == CPP_OPEN_BRACE)
2294 ++nesting_depth;
2298 /* Skip tokens until a non-nested closing curly brace is the next
2299 token. */
2301 static void
2302 cp_parser_skip_to_closing_brace (cp_parser *parser)
2304 unsigned nesting_depth = 0;
2306 while (true)
2308 cp_token *token;
2310 /* Peek at the next token. */
2311 token = cp_lexer_peek_token (parser->lexer);
2312 /* If we've run out of tokens, stop. */
2313 if (token->type == CPP_EOF)
2314 break;
2315 /* If the next token is a non-nested `}', then we have reached
2316 the end of the current block. */
2317 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2318 break;
2319 /* If it the next token is a `{', then we are entering a new
2320 block. Consume the entire block. */
2321 else if (token->type == CPP_OPEN_BRACE)
2322 ++nesting_depth;
2323 /* Consume the token. */
2324 cp_lexer_consume_token (parser->lexer);
2328 /* This is a simple wrapper around make_typename_type. When the id is
2329 an unresolved identifier node, we can provide a superior diagnostic
2330 using cp_parser_diagnose_invalid_type_name. */
2332 static tree
2333 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2335 tree result;
2336 if (TREE_CODE (id) == IDENTIFIER_NODE)
2338 result = make_typename_type (scope, id, typename_type,
2339 /*complain=*/0);
2340 if (result == error_mark_node)
2341 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2342 return result;
2344 return make_typename_type (scope, id, typename_type, tf_error);
2348 /* Create a new C++ parser. */
2350 static cp_parser *
2351 cp_parser_new (void)
2353 cp_parser *parser;
2354 cp_lexer *lexer;
2355 unsigned i;
2357 /* cp_lexer_new_main is called before calling ggc_alloc because
2358 cp_lexer_new_main might load a PCH file. */
2359 lexer = cp_lexer_new_main ();
2361 /* Initialize the binops_by_token so that we can get the tree
2362 directly from the token. */
2363 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2364 binops_by_token[binops[i].token_type] = binops[i];
2366 parser = GGC_CNEW (cp_parser);
2367 parser->lexer = lexer;
2368 parser->context = cp_parser_context_new (NULL);
2370 /* For now, we always accept GNU extensions. */
2371 parser->allow_gnu_extensions_p = 1;
2373 /* The `>' token is a greater-than operator, not the end of a
2374 template-id. */
2375 parser->greater_than_is_operator_p = true;
2377 parser->default_arg_ok_p = true;
2379 /* We are not parsing a constant-expression. */
2380 parser->integral_constant_expression_p = false;
2381 parser->allow_non_integral_constant_expression_p = false;
2382 parser->non_integral_constant_expression_p = false;
2384 /* Local variable names are not forbidden. */
2385 parser->local_variables_forbidden_p = false;
2387 /* We are not processing an `extern "C"' declaration. */
2388 parser->in_unbraced_linkage_specification_p = false;
2390 /* We are not processing a declarator. */
2391 parser->in_declarator_p = false;
2393 /* We are not processing a template-argument-list. */
2394 parser->in_template_argument_list_p = false;
2396 /* We are not in an iteration statement. */
2397 parser->in_iteration_statement_p = false;
2399 /* We are not in a switch statement. */
2400 parser->in_switch_statement_p = false;
2402 /* We are not parsing a type-id inside an expression. */
2403 parser->in_type_id_in_expr_p = false;
2405 /* Declarations aren't implicitly extern "C". */
2406 parser->implicit_extern_c = false;
2408 /* String literals should be translated to the execution character set. */
2409 parser->translate_strings_p = true;
2411 /* The unparsed function queue is empty. */
2412 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2414 /* There are no classes being defined. */
2415 parser->num_classes_being_defined = 0;
2417 /* No template parameters apply. */
2418 parser->num_template_parameter_lists = 0;
2420 return parser;
2423 /* Create a cp_lexer structure which will emit the tokens in CACHE
2424 and push it onto the parser's lexer stack. This is used for delayed
2425 parsing of in-class method bodies and default arguments, and should
2426 not be confused with tentative parsing. */
2427 static void
2428 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2430 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2431 lexer->next = parser->lexer;
2432 parser->lexer = lexer;
2434 /* Move the current source position to that of the first token in the
2435 new lexer. */
2436 cp_lexer_set_source_position_from_token (lexer->next_token);
2439 /* Pop the top lexer off the parser stack. This is never used for the
2440 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2441 static void
2442 cp_parser_pop_lexer (cp_parser *parser)
2444 cp_lexer *lexer = parser->lexer;
2445 parser->lexer = lexer->next;
2446 cp_lexer_destroy (lexer);
2448 /* Put the current source position back where it was before this
2449 lexer was pushed. */
2450 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2453 /* Lexical conventions [gram.lex] */
2455 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2456 identifier. */
2458 static tree
2459 cp_parser_identifier (cp_parser* parser)
2461 cp_token *token;
2463 /* Look for the identifier. */
2464 token = cp_parser_require (parser, CPP_NAME, "identifier");
2465 /* Return the value. */
2466 return token ? token->value : error_mark_node;
2469 /* Parse a sequence of adjacent string constants. Returns a
2470 TREE_STRING representing the combined, nul-terminated string
2471 constant. If TRANSLATE is true, translate the string to the
2472 execution character set. If WIDE_OK is true, a wide string is
2473 invalid here.
2475 C++98 [lex.string] says that if a narrow string literal token is
2476 adjacent to a wide string literal token, the behavior is undefined.
2477 However, C99 6.4.5p4 says that this results in a wide string literal.
2478 We follow C99 here, for consistency with the C front end.
2480 This code is largely lifted from lex_string() in c-lex.c.
2482 FUTURE: ObjC++ will need to handle @-strings here. */
2483 static tree
2484 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2486 tree value;
2487 bool wide = false;
2488 size_t count;
2489 struct obstack str_ob;
2490 cpp_string str, istr, *strs;
2491 cp_token *tok;
2493 tok = cp_lexer_peek_token (parser->lexer);
2494 if (!cp_parser_is_string_literal (tok))
2496 cp_parser_error (parser, "expected string-literal");
2497 return error_mark_node;
2500 /* Try to avoid the overhead of creating and destroying an obstack
2501 for the common case of just one string. */
2502 if (!cp_parser_is_string_literal
2503 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2505 cp_lexer_consume_token (parser->lexer);
2507 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2508 str.len = TREE_STRING_LENGTH (tok->value);
2509 count = 1;
2510 if (tok->type == CPP_WSTRING)
2511 wide = true;
2513 strs = &str;
2515 else
2517 gcc_obstack_init (&str_ob);
2518 count = 0;
2522 cp_lexer_consume_token (parser->lexer);
2523 count++;
2524 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2525 str.len = TREE_STRING_LENGTH (tok->value);
2526 if (tok->type == CPP_WSTRING)
2527 wide = true;
2529 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2531 tok = cp_lexer_peek_token (parser->lexer);
2533 while (cp_parser_is_string_literal (tok));
2535 strs = (cpp_string *) obstack_finish (&str_ob);
2538 if (wide && !wide_ok)
2540 cp_parser_error (parser, "a wide string is invalid in this context");
2541 wide = false;
2544 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2545 (parse_in, strs, count, &istr, wide))
2547 value = build_string (istr.len, (char *)istr.text);
2548 free ((void *)istr.text);
2550 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2551 value = fix_string_type (value);
2553 else
2554 /* cpp_interpret_string has issued an error. */
2555 value = error_mark_node;
2557 if (count > 1)
2558 obstack_free (&str_ob, 0);
2560 return value;
2564 /* Basic concepts [gram.basic] */
2566 /* Parse a translation-unit.
2568 translation-unit:
2569 declaration-seq [opt]
2571 Returns TRUE if all went well. */
2573 static bool
2574 cp_parser_translation_unit (cp_parser* parser)
2576 /* The address of the first non-permanent object on the declarator
2577 obstack. */
2578 static void *declarator_obstack_base;
2580 bool success;
2582 /* Create the declarator obstack, if necessary. */
2583 if (!cp_error_declarator)
2585 gcc_obstack_init (&declarator_obstack);
2586 /* Create the error declarator. */
2587 cp_error_declarator = make_declarator (cdk_error);
2588 /* Create the empty parameter list. */
2589 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2590 /* Remember where the base of the declarator obstack lies. */
2591 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2594 while (true)
2596 cp_parser_declaration_seq_opt (parser);
2598 /* If there are no tokens left then all went well. */
2599 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2601 /* Get rid of the token array; we don't need it any more. */
2602 cp_lexer_destroy (parser->lexer);
2603 parser->lexer = NULL;
2605 /* This file might have been a context that's implicitly extern
2606 "C". If so, pop the lang context. (Only relevant for PCH.) */
2607 if (parser->implicit_extern_c)
2609 pop_lang_context ();
2610 parser->implicit_extern_c = false;
2613 /* Finish up. */
2614 finish_translation_unit ();
2616 success = true;
2617 break;
2619 else
2621 cp_parser_error (parser, "expected declaration");
2622 success = false;
2623 break;
2627 /* Make sure the declarator obstack was fully cleaned up. */
2628 gcc_assert (obstack_next_free (&declarator_obstack)
2629 == declarator_obstack_base);
2631 /* All went well. */
2632 return success;
2635 /* Expressions [gram.expr] */
2637 /* Parse a primary-expression.
2639 primary-expression:
2640 literal
2641 this
2642 ( expression )
2643 id-expression
2645 GNU Extensions:
2647 primary-expression:
2648 ( compound-statement )
2649 __builtin_va_arg ( assignment-expression , type-id )
2651 literal:
2652 __null
2654 CAST_P is true if this primary expression is the target of a cast.
2656 Returns a representation of the expression.
2658 *IDK indicates what kind of id-expression (if any) was present.
2660 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2661 used as the operand of a pointer-to-member. In that case,
2662 *QUALIFYING_CLASS gives the class that is used as the qualifying
2663 class in the pointer-to-member. */
2665 static tree
2666 cp_parser_primary_expression (cp_parser *parser,
2667 bool cast_p,
2668 cp_id_kind *idk,
2669 tree *qualifying_class)
2671 cp_token *token;
2673 /* Assume the primary expression is not an id-expression. */
2674 *idk = CP_ID_KIND_NONE;
2675 /* And that it cannot be used as pointer-to-member. */
2676 *qualifying_class = NULL_TREE;
2678 /* Peek at the next token. */
2679 token = cp_lexer_peek_token (parser->lexer);
2680 switch (token->type)
2682 /* literal:
2683 integer-literal
2684 character-literal
2685 floating-literal
2686 string-literal
2687 boolean-literal */
2688 case CPP_CHAR:
2689 case CPP_WCHAR:
2690 case CPP_NUMBER:
2691 token = cp_lexer_consume_token (parser->lexer);
2692 /* Floating-point literals are only allowed in an integral
2693 constant expression if they are cast to an integral or
2694 enumeration type. */
2695 if (TREE_CODE (token->value) == REAL_CST
2696 && parser->integral_constant_expression_p
2697 && pedantic)
2699 /* CAST_P will be set even in invalid code like "int(2.7 +
2700 ...)". Therefore, we have to check that the next token
2701 is sure to end the cast. */
2702 if (cast_p)
2704 cp_token *next_token;
2706 next_token = cp_lexer_peek_token (parser->lexer);
2707 if (/* The comma at the end of an
2708 enumerator-definition. */
2709 next_token->type != CPP_COMMA
2710 /* The curly brace at the end of an enum-specifier. */
2711 && next_token->type != CPP_CLOSE_BRACE
2712 /* The end of a statement. */
2713 && next_token->type != CPP_SEMICOLON
2714 /* The end of the cast-expression. */
2715 && next_token->type != CPP_CLOSE_PAREN
2716 /* The end of an array bound. */
2717 && next_token->type != CPP_CLOSE_SQUARE)
2718 cast_p = false;
2721 /* If we are within a cast, then the constraint that the
2722 cast is to an integral or enumeration type will be
2723 checked at that point. If we are not within a cast, then
2724 this code is invalid. */
2725 if (!cast_p)
2726 cp_parser_non_integral_constant_expression
2727 (parser, "floating-point literal");
2729 return token->value;
2731 case CPP_STRING:
2732 case CPP_WSTRING:
2733 /* ??? Should wide strings be allowed when parser->translate_strings_p
2734 is false (i.e. in attributes)? If not, we can kill the third
2735 argument to cp_parser_string_literal. */
2736 return cp_parser_string_literal (parser,
2737 parser->translate_strings_p,
2738 true);
2740 case CPP_OPEN_PAREN:
2742 tree expr;
2743 bool saved_greater_than_is_operator_p;
2745 /* Consume the `('. */
2746 cp_lexer_consume_token (parser->lexer);
2747 /* Within a parenthesized expression, a `>' token is always
2748 the greater-than operator. */
2749 saved_greater_than_is_operator_p
2750 = parser->greater_than_is_operator_p;
2751 parser->greater_than_is_operator_p = true;
2752 /* If we see `( { ' then we are looking at the beginning of
2753 a GNU statement-expression. */
2754 if (cp_parser_allow_gnu_extensions_p (parser)
2755 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2757 /* Statement-expressions are not allowed by the standard. */
2758 if (pedantic)
2759 pedwarn ("ISO C++ forbids braced-groups within expressions");
2761 /* And they're not allowed outside of a function-body; you
2762 cannot, for example, write:
2764 int i = ({ int j = 3; j + 1; });
2766 at class or namespace scope. */
2767 if (!at_function_scope_p ())
2768 error ("statement-expressions are allowed only inside functions");
2769 /* Start the statement-expression. */
2770 expr = begin_stmt_expr ();
2771 /* Parse the compound-statement. */
2772 cp_parser_compound_statement (parser, expr, false);
2773 /* Finish up. */
2774 expr = finish_stmt_expr (expr, false);
2776 else
2778 /* Parse the parenthesized expression. */
2779 expr = cp_parser_expression (parser, cast_p);
2780 /* Let the front end know that this expression was
2781 enclosed in parentheses. This matters in case, for
2782 example, the expression is of the form `A::B', since
2783 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2784 not. */
2785 finish_parenthesized_expr (expr);
2787 /* The `>' token might be the end of a template-id or
2788 template-parameter-list now. */
2789 parser->greater_than_is_operator_p
2790 = saved_greater_than_is_operator_p;
2791 /* Consume the `)'. */
2792 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2793 cp_parser_skip_to_end_of_statement (parser);
2795 return expr;
2798 case CPP_KEYWORD:
2799 switch (token->keyword)
2801 /* These two are the boolean literals. */
2802 case RID_TRUE:
2803 cp_lexer_consume_token (parser->lexer);
2804 return boolean_true_node;
2805 case RID_FALSE:
2806 cp_lexer_consume_token (parser->lexer);
2807 return boolean_false_node;
2809 /* The `__null' literal. */
2810 case RID_NULL:
2811 cp_lexer_consume_token (parser->lexer);
2812 return null_node;
2814 /* Recognize the `this' keyword. */
2815 case RID_THIS:
2816 cp_lexer_consume_token (parser->lexer);
2817 if (parser->local_variables_forbidden_p)
2819 error ("%<this%> may not be used in this context");
2820 return error_mark_node;
2822 /* Pointers cannot appear in constant-expressions. */
2823 if (cp_parser_non_integral_constant_expression (parser,
2824 "`this'"))
2825 return error_mark_node;
2826 return finish_this_expr ();
2828 /* The `operator' keyword can be the beginning of an
2829 id-expression. */
2830 case RID_OPERATOR:
2831 goto id_expression;
2833 case RID_FUNCTION_NAME:
2834 case RID_PRETTY_FUNCTION_NAME:
2835 case RID_C99_FUNCTION_NAME:
2836 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2837 __func__ are the names of variables -- but they are
2838 treated specially. Therefore, they are handled here,
2839 rather than relying on the generic id-expression logic
2840 below. Grammatically, these names are id-expressions.
2842 Consume the token. */
2843 token = cp_lexer_consume_token (parser->lexer);
2844 /* Look up the name. */
2845 return finish_fname (token->value);
2847 case RID_VA_ARG:
2849 tree expression;
2850 tree type;
2852 /* The `__builtin_va_arg' construct is used to handle
2853 `va_arg'. Consume the `__builtin_va_arg' token. */
2854 cp_lexer_consume_token (parser->lexer);
2855 /* Look for the opening `('. */
2856 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2857 /* Now, parse the assignment-expression. */
2858 expression = cp_parser_assignment_expression (parser,
2859 /*cast_p=*/false);
2860 /* Look for the `,'. */
2861 cp_parser_require (parser, CPP_COMMA, "`,'");
2862 /* Parse the type-id. */
2863 type = cp_parser_type_id (parser);
2864 /* Look for the closing `)'. */
2865 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2866 /* Using `va_arg' in a constant-expression is not
2867 allowed. */
2868 if (cp_parser_non_integral_constant_expression (parser,
2869 "`va_arg'"))
2870 return error_mark_node;
2871 return build_x_va_arg (expression, type);
2874 case RID_OFFSETOF:
2875 return cp_parser_builtin_offsetof (parser);
2877 default:
2878 cp_parser_error (parser, "expected primary-expression");
2879 return error_mark_node;
2882 /* An id-expression can start with either an identifier, a
2883 `::' as the beginning of a qualified-id, or the "operator"
2884 keyword. */
2885 case CPP_NAME:
2886 case CPP_SCOPE:
2887 case CPP_TEMPLATE_ID:
2888 case CPP_NESTED_NAME_SPECIFIER:
2890 tree id_expression;
2891 tree decl;
2892 const char *error_msg;
2894 id_expression:
2895 /* Parse the id-expression. */
2896 id_expression
2897 = cp_parser_id_expression (parser,
2898 /*template_keyword_p=*/false,
2899 /*check_dependency_p=*/true,
2900 /*template_p=*/NULL,
2901 /*declarator_p=*/false);
2902 if (id_expression == error_mark_node)
2903 return error_mark_node;
2904 /* If we have a template-id, then no further lookup is
2905 required. If the template-id was for a template-class, we
2906 will sometimes have a TYPE_DECL at this point. */
2907 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2908 || TREE_CODE (id_expression) == TYPE_DECL)
2909 decl = id_expression;
2910 /* Look up the name. */
2911 else
2913 bool ambiguous_p;
2915 decl = cp_parser_lookup_name (parser, id_expression,
2916 none_type,
2917 /*is_template=*/false,
2918 /*is_namespace=*/false,
2919 /*check_dependency=*/true,
2920 &ambiguous_p);
2921 /* If the lookup was ambiguous, an error will already have
2922 been issued. */
2923 if (ambiguous_p)
2924 return error_mark_node;
2925 /* If name lookup gives us a SCOPE_REF, then the
2926 qualifying scope was dependent. Just propagate the
2927 name. */
2928 if (TREE_CODE (decl) == SCOPE_REF)
2930 if (TYPE_P (TREE_OPERAND (decl, 0)))
2931 *qualifying_class = TREE_OPERAND (decl, 0);
2932 return decl;
2934 /* Check to see if DECL is a local variable in a context
2935 where that is forbidden. */
2936 if (parser->local_variables_forbidden_p
2937 && local_variable_p (decl))
2939 /* It might be that we only found DECL because we are
2940 trying to be generous with pre-ISO scoping rules.
2941 For example, consider:
2943 int i;
2944 void g() {
2945 for (int i = 0; i < 10; ++i) {}
2946 extern void f(int j = i);
2949 Here, name look up will originally find the out
2950 of scope `i'. We need to issue a warning message,
2951 but then use the global `i'. */
2952 decl = check_for_out_of_scope_variable (decl);
2953 if (local_variable_p (decl))
2955 error ("local variable %qD may not appear in this context",
2956 decl);
2957 return error_mark_node;
2962 decl = finish_id_expression (id_expression, decl, parser->scope,
2963 idk, qualifying_class,
2964 parser->integral_constant_expression_p,
2965 parser->allow_non_integral_constant_expression_p,
2966 &parser->non_integral_constant_expression_p,
2967 &error_msg);
2968 if (error_msg)
2969 cp_parser_error (parser, error_msg);
2970 return decl;
2973 /* Anything else is an error. */
2974 default:
2975 cp_parser_error (parser, "expected primary-expression");
2976 return error_mark_node;
2980 /* Parse an id-expression.
2982 id-expression:
2983 unqualified-id
2984 qualified-id
2986 qualified-id:
2987 :: [opt] nested-name-specifier template [opt] unqualified-id
2988 :: identifier
2989 :: operator-function-id
2990 :: template-id
2992 Return a representation of the unqualified portion of the
2993 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2994 a `::' or nested-name-specifier.
2996 Often, if the id-expression was a qualified-id, the caller will
2997 want to make a SCOPE_REF to represent the qualified-id. This
2998 function does not do this in order to avoid wastefully creating
2999 SCOPE_REFs when they are not required.
3001 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3002 `template' keyword.
3004 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3005 uninstantiated templates.
3007 If *TEMPLATE_P is non-NULL, it is set to true iff the
3008 `template' keyword is used to explicitly indicate that the entity
3009 named is a template.
3011 If DECLARATOR_P is true, the id-expression is appearing as part of
3012 a declarator, rather than as part of an expression. */
3014 static tree
3015 cp_parser_id_expression (cp_parser *parser,
3016 bool template_keyword_p,
3017 bool check_dependency_p,
3018 bool *template_p,
3019 bool declarator_p)
3021 bool global_scope_p;
3022 bool nested_name_specifier_p;
3024 /* Assume the `template' keyword was not used. */
3025 if (template_p)
3026 *template_p = false;
3028 /* Look for the optional `::' operator. */
3029 global_scope_p
3030 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3031 != NULL_TREE);
3032 /* Look for the optional nested-name-specifier. */
3033 nested_name_specifier_p
3034 = (cp_parser_nested_name_specifier_opt (parser,
3035 /*typename_keyword_p=*/false,
3036 check_dependency_p,
3037 /*type_p=*/false,
3038 declarator_p)
3039 != NULL_TREE);
3040 /* If there is a nested-name-specifier, then we are looking at
3041 the first qualified-id production. */
3042 if (nested_name_specifier_p)
3044 tree saved_scope;
3045 tree saved_object_scope;
3046 tree saved_qualifying_scope;
3047 tree unqualified_id;
3048 bool is_template;
3050 /* See if the next token is the `template' keyword. */
3051 if (!template_p)
3052 template_p = &is_template;
3053 *template_p = cp_parser_optional_template_keyword (parser);
3054 /* Name lookup we do during the processing of the
3055 unqualified-id might obliterate SCOPE. */
3056 saved_scope = parser->scope;
3057 saved_object_scope = parser->object_scope;
3058 saved_qualifying_scope = parser->qualifying_scope;
3059 /* Process the final unqualified-id. */
3060 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3061 check_dependency_p,
3062 declarator_p);
3063 /* Restore the SAVED_SCOPE for our caller. */
3064 parser->scope = saved_scope;
3065 parser->object_scope = saved_object_scope;
3066 parser->qualifying_scope = saved_qualifying_scope;
3068 return unqualified_id;
3070 /* Otherwise, if we are in global scope, then we are looking at one
3071 of the other qualified-id productions. */
3072 else if (global_scope_p)
3074 cp_token *token;
3075 tree id;
3077 /* Peek at the next token. */
3078 token = cp_lexer_peek_token (parser->lexer);
3080 /* If it's an identifier, and the next token is not a "<", then
3081 we can avoid the template-id case. This is an optimization
3082 for this common case. */
3083 if (token->type == CPP_NAME
3084 && !cp_parser_nth_token_starts_template_argument_list_p
3085 (parser, 2))
3086 return cp_parser_identifier (parser);
3088 cp_parser_parse_tentatively (parser);
3089 /* Try a template-id. */
3090 id = cp_parser_template_id (parser,
3091 /*template_keyword_p=*/false,
3092 /*check_dependency_p=*/true,
3093 declarator_p);
3094 /* If that worked, we're done. */
3095 if (cp_parser_parse_definitely (parser))
3096 return id;
3098 /* Peek at the next token. (Changes in the token buffer may
3099 have invalidated the pointer obtained above.) */
3100 token = cp_lexer_peek_token (parser->lexer);
3102 switch (token->type)
3104 case CPP_NAME:
3105 return cp_parser_identifier (parser);
3107 case CPP_KEYWORD:
3108 if (token->keyword == RID_OPERATOR)
3109 return cp_parser_operator_function_id (parser);
3110 /* Fall through. */
3112 default:
3113 cp_parser_error (parser, "expected id-expression");
3114 return error_mark_node;
3117 else
3118 return cp_parser_unqualified_id (parser, template_keyword_p,
3119 /*check_dependency_p=*/true,
3120 declarator_p);
3123 /* Parse an unqualified-id.
3125 unqualified-id:
3126 identifier
3127 operator-function-id
3128 conversion-function-id
3129 ~ class-name
3130 template-id
3132 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3133 keyword, in a construct like `A::template ...'.
3135 Returns a representation of unqualified-id. For the `identifier'
3136 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3137 production a BIT_NOT_EXPR is returned; the operand of the
3138 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3139 other productions, see the documentation accompanying the
3140 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3141 names are looked up in uninstantiated templates. If DECLARATOR_P
3142 is true, the unqualified-id is appearing as part of a declarator,
3143 rather than as part of an expression. */
3145 static tree
3146 cp_parser_unqualified_id (cp_parser* parser,
3147 bool template_keyword_p,
3148 bool check_dependency_p,
3149 bool declarator_p)
3151 cp_token *token;
3153 /* Peek at the next token. */
3154 token = cp_lexer_peek_token (parser->lexer);
3156 switch (token->type)
3158 case CPP_NAME:
3160 tree id;
3162 /* We don't know yet whether or not this will be a
3163 template-id. */
3164 cp_parser_parse_tentatively (parser);
3165 /* Try a template-id. */
3166 id = cp_parser_template_id (parser, template_keyword_p,
3167 check_dependency_p,
3168 declarator_p);
3169 /* If it worked, we're done. */
3170 if (cp_parser_parse_definitely (parser))
3171 return id;
3172 /* Otherwise, it's an ordinary identifier. */
3173 return cp_parser_identifier (parser);
3176 case CPP_TEMPLATE_ID:
3177 return cp_parser_template_id (parser, template_keyword_p,
3178 check_dependency_p,
3179 declarator_p);
3181 case CPP_COMPL:
3183 tree type_decl;
3184 tree qualifying_scope;
3185 tree object_scope;
3186 tree scope;
3187 bool done;
3189 /* Consume the `~' token. */
3190 cp_lexer_consume_token (parser->lexer);
3191 /* Parse the class-name. The standard, as written, seems to
3192 say that:
3194 template <typename T> struct S { ~S (); };
3195 template <typename T> S<T>::~S() {}
3197 is invalid, since `~' must be followed by a class-name, but
3198 `S<T>' is dependent, and so not known to be a class.
3199 That's not right; we need to look in uninstantiated
3200 templates. A further complication arises from:
3202 template <typename T> void f(T t) {
3203 t.T::~T();
3206 Here, it is not possible to look up `T' in the scope of `T'
3207 itself. We must look in both the current scope, and the
3208 scope of the containing complete expression.
3210 Yet another issue is:
3212 struct S {
3213 int S;
3214 ~S();
3217 S::~S() {}
3219 The standard does not seem to say that the `S' in `~S'
3220 should refer to the type `S' and not the data member
3221 `S::S'. */
3223 /* DR 244 says that we look up the name after the "~" in the
3224 same scope as we looked up the qualifying name. That idea
3225 isn't fully worked out; it's more complicated than that. */
3226 scope = parser->scope;
3227 object_scope = parser->object_scope;
3228 qualifying_scope = parser->qualifying_scope;
3230 /* If the name is of the form "X::~X" it's OK. */
3231 if (scope && TYPE_P (scope)
3232 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3233 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3234 == CPP_OPEN_PAREN)
3235 && (cp_lexer_peek_token (parser->lexer)->value
3236 == TYPE_IDENTIFIER (scope)))
3238 cp_lexer_consume_token (parser->lexer);
3239 return build_nt (BIT_NOT_EXPR, scope);
3242 /* If there was an explicit qualification (S::~T), first look
3243 in the scope given by the qualification (i.e., S). */
3244 done = false;
3245 type_decl = NULL_TREE;
3246 if (scope)
3248 cp_parser_parse_tentatively (parser);
3249 type_decl = cp_parser_class_name (parser,
3250 /*typename_keyword_p=*/false,
3251 /*template_keyword_p=*/false,
3252 none_type,
3253 /*check_dependency=*/false,
3254 /*class_head_p=*/false,
3255 declarator_p);
3256 if (cp_parser_parse_definitely (parser))
3257 done = true;
3259 /* In "N::S::~S", look in "N" as well. */
3260 if (!done && scope && qualifying_scope)
3262 cp_parser_parse_tentatively (parser);
3263 parser->scope = qualifying_scope;
3264 parser->object_scope = NULL_TREE;
3265 parser->qualifying_scope = NULL_TREE;
3266 type_decl
3267 = cp_parser_class_name (parser,
3268 /*typename_keyword_p=*/false,
3269 /*template_keyword_p=*/false,
3270 none_type,
3271 /*check_dependency=*/false,
3272 /*class_head_p=*/false,
3273 declarator_p);
3274 if (cp_parser_parse_definitely (parser))
3275 done = true;
3277 /* In "p->S::~T", look in the scope given by "*p" as well. */
3278 else if (!done && object_scope)
3280 cp_parser_parse_tentatively (parser);
3281 parser->scope = object_scope;
3282 parser->object_scope = NULL_TREE;
3283 parser->qualifying_scope = NULL_TREE;
3284 type_decl
3285 = cp_parser_class_name (parser,
3286 /*typename_keyword_p=*/false,
3287 /*template_keyword_p=*/false,
3288 none_type,
3289 /*check_dependency=*/false,
3290 /*class_head_p=*/false,
3291 declarator_p);
3292 if (cp_parser_parse_definitely (parser))
3293 done = true;
3295 /* Look in the surrounding context. */
3296 if (!done)
3298 parser->scope = NULL_TREE;
3299 parser->object_scope = NULL_TREE;
3300 parser->qualifying_scope = NULL_TREE;
3301 type_decl
3302 = cp_parser_class_name (parser,
3303 /*typename_keyword_p=*/false,
3304 /*template_keyword_p=*/false,
3305 none_type,
3306 /*check_dependency=*/false,
3307 /*class_head_p=*/false,
3308 declarator_p);
3310 /* If an error occurred, assume that the name of the
3311 destructor is the same as the name of the qualifying
3312 class. That allows us to keep parsing after running
3313 into ill-formed destructor names. */
3314 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3315 return build_nt (BIT_NOT_EXPR, scope);
3316 else if (type_decl == error_mark_node)
3317 return error_mark_node;
3319 /* [class.dtor]
3321 A typedef-name that names a class shall not be used as the
3322 identifier in the declarator for a destructor declaration. */
3323 if (declarator_p
3324 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3325 && !DECL_SELF_REFERENCE_P (type_decl)
3326 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3327 error ("typedef-name %qD used as destructor declarator",
3328 type_decl);
3330 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3333 case CPP_KEYWORD:
3334 if (token->keyword == RID_OPERATOR)
3336 tree id;
3338 /* This could be a template-id, so we try that first. */
3339 cp_parser_parse_tentatively (parser);
3340 /* Try a template-id. */
3341 id = cp_parser_template_id (parser, template_keyword_p,
3342 /*check_dependency_p=*/true,
3343 declarator_p);
3344 /* If that worked, we're done. */
3345 if (cp_parser_parse_definitely (parser))
3346 return id;
3347 /* We still don't know whether we're looking at an
3348 operator-function-id or a conversion-function-id. */
3349 cp_parser_parse_tentatively (parser);
3350 /* Try an operator-function-id. */
3351 id = cp_parser_operator_function_id (parser);
3352 /* If that didn't work, try a conversion-function-id. */
3353 if (!cp_parser_parse_definitely (parser))
3354 id = cp_parser_conversion_function_id (parser);
3356 return id;
3358 /* Fall through. */
3360 default:
3361 cp_parser_error (parser, "expected unqualified-id");
3362 return error_mark_node;
3366 /* Parse an (optional) nested-name-specifier.
3368 nested-name-specifier:
3369 class-or-namespace-name :: nested-name-specifier [opt]
3370 class-or-namespace-name :: template nested-name-specifier [opt]
3372 PARSER->SCOPE should be set appropriately before this function is
3373 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3374 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3375 in name lookups.
3377 Sets PARSER->SCOPE to the class (TYPE) or namespace
3378 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3379 it unchanged if there is no nested-name-specifier. Returns the new
3380 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3382 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3383 part of a declaration and/or decl-specifier. */
3385 static tree
3386 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3387 bool typename_keyword_p,
3388 bool check_dependency_p,
3389 bool type_p,
3390 bool is_declaration)
3392 bool success = false;
3393 tree access_check = NULL_TREE;
3394 cp_token_position start = 0;
3395 cp_token *token;
3397 /* If the next token corresponds to a nested name specifier, there
3398 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3399 false, it may have been true before, in which case something
3400 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3401 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3402 CHECK_DEPENDENCY_P is false, we have to fall through into the
3403 main loop. */
3404 if (check_dependency_p
3405 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3407 cp_parser_pre_parsed_nested_name_specifier (parser);
3408 return parser->scope;
3411 /* Remember where the nested-name-specifier starts. */
3412 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3413 start = cp_lexer_token_position (parser->lexer, false);
3415 push_deferring_access_checks (dk_deferred);
3417 while (true)
3419 tree new_scope;
3420 tree old_scope;
3421 tree saved_qualifying_scope;
3422 bool template_keyword_p;
3424 /* Spot cases that cannot be the beginning of a
3425 nested-name-specifier. */
3426 token = cp_lexer_peek_token (parser->lexer);
3428 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3429 the already parsed nested-name-specifier. */
3430 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3432 /* Grab the nested-name-specifier and continue the loop. */
3433 cp_parser_pre_parsed_nested_name_specifier (parser);
3434 success = true;
3435 continue;
3438 /* Spot cases that cannot be the beginning of a
3439 nested-name-specifier. On the second and subsequent times
3440 through the loop, we look for the `template' keyword. */
3441 if (success && token->keyword == RID_TEMPLATE)
3443 /* A template-id can start a nested-name-specifier. */
3444 else if (token->type == CPP_TEMPLATE_ID)
3446 else
3448 /* If the next token is not an identifier, then it is
3449 definitely not a class-or-namespace-name. */
3450 if (token->type != CPP_NAME)
3451 break;
3452 /* If the following token is neither a `<' (to begin a
3453 template-id), nor a `::', then we are not looking at a
3454 nested-name-specifier. */
3455 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3456 if (token->type != CPP_SCOPE
3457 && !cp_parser_nth_token_starts_template_argument_list_p
3458 (parser, 2))
3459 break;
3462 /* The nested-name-specifier is optional, so we parse
3463 tentatively. */
3464 cp_parser_parse_tentatively (parser);
3466 /* Look for the optional `template' keyword, if this isn't the
3467 first time through the loop. */
3468 if (success)
3469 template_keyword_p = cp_parser_optional_template_keyword (parser);
3470 else
3471 template_keyword_p = false;
3473 /* Save the old scope since the name lookup we are about to do
3474 might destroy it. */
3475 old_scope = parser->scope;
3476 saved_qualifying_scope = parser->qualifying_scope;
3477 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3478 look up names in "X<T>::I" in order to determine that "Y" is
3479 a template. So, if we have a typename at this point, we make
3480 an effort to look through it. */
3481 if (is_declaration
3482 && !typename_keyword_p
3483 && parser->scope
3484 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3485 parser->scope = resolve_typename_type (parser->scope,
3486 /*only_current_p=*/false);
3487 /* Parse the qualifying entity. */
3488 new_scope
3489 = cp_parser_class_or_namespace_name (parser,
3490 typename_keyword_p,
3491 template_keyword_p,
3492 check_dependency_p,
3493 type_p,
3494 is_declaration);
3495 /* Look for the `::' token. */
3496 cp_parser_require (parser, CPP_SCOPE, "`::'");
3498 /* If we found what we wanted, we keep going; otherwise, we're
3499 done. */
3500 if (!cp_parser_parse_definitely (parser))
3502 bool error_p = false;
3504 /* Restore the OLD_SCOPE since it was valid before the
3505 failed attempt at finding the last
3506 class-or-namespace-name. */
3507 parser->scope = old_scope;
3508 parser->qualifying_scope = saved_qualifying_scope;
3509 /* If the next token is an identifier, and the one after
3510 that is a `::', then any valid interpretation would have
3511 found a class-or-namespace-name. */
3512 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3513 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3514 == CPP_SCOPE)
3515 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3516 != CPP_COMPL))
3518 token = cp_lexer_consume_token (parser->lexer);
3519 if (!error_p)
3521 tree decl;
3523 decl = cp_parser_lookup_name_simple (parser, token->value);
3524 if (TREE_CODE (decl) == TEMPLATE_DECL)
3525 error ("%qD used without template parameters", decl);
3526 else
3527 cp_parser_name_lookup_error
3528 (parser, token->value, decl,
3529 "is not a class or namespace");
3530 parser->scope = NULL_TREE;
3531 error_p = true;
3532 /* Treat this as a successful nested-name-specifier
3533 due to:
3535 [basic.lookup.qual]
3537 If the name found is not a class-name (clause
3538 _class_) or namespace-name (_namespace.def_), the
3539 program is ill-formed. */
3540 success = true;
3542 cp_lexer_consume_token (parser->lexer);
3544 break;
3547 /* We've found one valid nested-name-specifier. */
3548 success = true;
3549 /* Make sure we look in the right scope the next time through
3550 the loop. */
3551 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3552 ? TREE_TYPE (new_scope)
3553 : new_scope);
3554 /* If it is a class scope, try to complete it; we are about to
3555 be looking up names inside the class. */
3556 if (TYPE_P (parser->scope)
3557 /* Since checking types for dependency can be expensive,
3558 avoid doing it if the type is already complete. */
3559 && !COMPLETE_TYPE_P (parser->scope)
3560 /* Do not try to complete dependent types. */
3561 && !dependent_type_p (parser->scope))
3562 complete_type (parser->scope);
3565 /* Retrieve any deferred checks. Do not pop this access checks yet
3566 so the memory will not be reclaimed during token replacing below. */
3567 access_check = get_deferred_access_checks ();
3569 /* If parsing tentatively, replace the sequence of tokens that makes
3570 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3571 token. That way, should we re-parse the token stream, we will
3572 not have to repeat the effort required to do the parse, nor will
3573 we issue duplicate error messages. */
3574 if (success && start)
3576 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3578 /* Reset the contents of the START token. */
3579 token->type = CPP_NESTED_NAME_SPECIFIER;
3580 token->value = build_tree_list (access_check, parser->scope);
3581 TREE_TYPE (token->value) = parser->qualifying_scope;
3582 token->keyword = RID_MAX;
3584 /* Purge all subsequent tokens. */
3585 cp_lexer_purge_tokens_after (parser->lexer, start);
3588 pop_deferring_access_checks ();
3589 return success ? parser->scope : NULL_TREE;
3592 /* Parse a nested-name-specifier. See
3593 cp_parser_nested_name_specifier_opt for details. This function
3594 behaves identically, except that it will an issue an error if no
3595 nested-name-specifier is present, and it will return
3596 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3597 is present. */
3599 static tree
3600 cp_parser_nested_name_specifier (cp_parser *parser,
3601 bool typename_keyword_p,
3602 bool check_dependency_p,
3603 bool type_p,
3604 bool is_declaration)
3606 tree scope;
3608 /* Look for the nested-name-specifier. */
3609 scope = cp_parser_nested_name_specifier_opt (parser,
3610 typename_keyword_p,
3611 check_dependency_p,
3612 type_p,
3613 is_declaration);
3614 /* If it was not present, issue an error message. */
3615 if (!scope)
3617 cp_parser_error (parser, "expected nested-name-specifier");
3618 parser->scope = NULL_TREE;
3619 return error_mark_node;
3622 return scope;
3625 /* Parse a class-or-namespace-name.
3627 class-or-namespace-name:
3628 class-name
3629 namespace-name
3631 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3632 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3633 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3634 TYPE_P is TRUE iff the next name should be taken as a class-name,
3635 even the same name is declared to be another entity in the same
3636 scope.
3638 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3639 specified by the class-or-namespace-name. If neither is found the
3640 ERROR_MARK_NODE is returned. */
3642 static tree
3643 cp_parser_class_or_namespace_name (cp_parser *parser,
3644 bool typename_keyword_p,
3645 bool template_keyword_p,
3646 bool check_dependency_p,
3647 bool type_p,
3648 bool is_declaration)
3650 tree saved_scope;
3651 tree saved_qualifying_scope;
3652 tree saved_object_scope;
3653 tree scope;
3654 bool only_class_p;
3656 /* Before we try to parse the class-name, we must save away the
3657 current PARSER->SCOPE since cp_parser_class_name will destroy
3658 it. */
3659 saved_scope = parser->scope;
3660 saved_qualifying_scope = parser->qualifying_scope;
3661 saved_object_scope = parser->object_scope;
3662 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3663 there is no need to look for a namespace-name. */
3664 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3665 if (!only_class_p)
3666 cp_parser_parse_tentatively (parser);
3667 scope = cp_parser_class_name (parser,
3668 typename_keyword_p,
3669 template_keyword_p,
3670 type_p ? class_type : none_type,
3671 check_dependency_p,
3672 /*class_head_p=*/false,
3673 is_declaration);
3674 /* If that didn't work, try for a namespace-name. */
3675 if (!only_class_p && !cp_parser_parse_definitely (parser))
3677 /* Restore the saved scope. */
3678 parser->scope = saved_scope;
3679 parser->qualifying_scope = saved_qualifying_scope;
3680 parser->object_scope = saved_object_scope;
3681 /* If we are not looking at an identifier followed by the scope
3682 resolution operator, then this is not part of a
3683 nested-name-specifier. (Note that this function is only used
3684 to parse the components of a nested-name-specifier.) */
3685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3686 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3687 return error_mark_node;
3688 scope = cp_parser_namespace_name (parser);
3691 return scope;
3694 /* Parse a postfix-expression.
3696 postfix-expression:
3697 primary-expression
3698 postfix-expression [ expression ]
3699 postfix-expression ( expression-list [opt] )
3700 simple-type-specifier ( expression-list [opt] )
3701 typename :: [opt] nested-name-specifier identifier
3702 ( expression-list [opt] )
3703 typename :: [opt] nested-name-specifier template [opt] template-id
3704 ( expression-list [opt] )
3705 postfix-expression . template [opt] id-expression
3706 postfix-expression -> template [opt] id-expression
3707 postfix-expression . pseudo-destructor-name
3708 postfix-expression -> pseudo-destructor-name
3709 postfix-expression ++
3710 postfix-expression --
3711 dynamic_cast < type-id > ( expression )
3712 static_cast < type-id > ( expression )
3713 reinterpret_cast < type-id > ( expression )
3714 const_cast < type-id > ( expression )
3715 typeid ( expression )
3716 typeid ( type-id )
3718 GNU Extension:
3720 postfix-expression:
3721 ( type-id ) { initializer-list , [opt] }
3723 This extension is a GNU version of the C99 compound-literal
3724 construct. (The C99 grammar uses `type-name' instead of `type-id',
3725 but they are essentially the same concept.)
3727 If ADDRESS_P is true, the postfix expression is the operand of the
3728 `&' operator. CAST_P is true if this expression is the target of a
3729 cast.
3731 Returns a representation of the expression. */
3733 static tree
3734 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3736 cp_token *token;
3737 enum rid keyword;
3738 cp_id_kind idk = CP_ID_KIND_NONE;
3739 tree postfix_expression = NULL_TREE;
3740 /* Non-NULL only if the current postfix-expression can be used to
3741 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3742 class used to qualify the member. */
3743 tree qualifying_class = NULL_TREE;
3745 /* Peek at the next token. */
3746 token = cp_lexer_peek_token (parser->lexer);
3747 /* Some of the productions are determined by keywords. */
3748 keyword = token->keyword;
3749 switch (keyword)
3751 case RID_DYNCAST:
3752 case RID_STATCAST:
3753 case RID_REINTCAST:
3754 case RID_CONSTCAST:
3756 tree type;
3757 tree expression;
3758 const char *saved_message;
3760 /* All of these can be handled in the same way from the point
3761 of view of parsing. Begin by consuming the token
3762 identifying the cast. */
3763 cp_lexer_consume_token (parser->lexer);
3765 /* New types cannot be defined in the cast. */
3766 saved_message = parser->type_definition_forbidden_message;
3767 parser->type_definition_forbidden_message
3768 = "types may not be defined in casts";
3770 /* Look for the opening `<'. */
3771 cp_parser_require (parser, CPP_LESS, "`<'");
3772 /* Parse the type to which we are casting. */
3773 type = cp_parser_type_id (parser);
3774 /* Look for the closing `>'. */
3775 cp_parser_require (parser, CPP_GREATER, "`>'");
3776 /* Restore the old message. */
3777 parser->type_definition_forbidden_message = saved_message;
3779 /* And the expression which is being cast. */
3780 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3781 expression = cp_parser_expression (parser, /*cast_p=*/true);
3782 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3784 /* Only type conversions to integral or enumeration types
3785 can be used in constant-expressions. */
3786 if (parser->integral_constant_expression_p
3787 && !dependent_type_p (type)
3788 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3789 && (cp_parser_non_integral_constant_expression
3790 (parser,
3791 "a cast to a type other than an integral or "
3792 "enumeration type")))
3793 return error_mark_node;
3795 switch (keyword)
3797 case RID_DYNCAST:
3798 postfix_expression
3799 = build_dynamic_cast (type, expression);
3800 break;
3801 case RID_STATCAST:
3802 postfix_expression
3803 = build_static_cast (type, expression);
3804 break;
3805 case RID_REINTCAST:
3806 postfix_expression
3807 = build_reinterpret_cast (type, expression);
3808 break;
3809 case RID_CONSTCAST:
3810 postfix_expression
3811 = build_const_cast (type, expression);
3812 break;
3813 default:
3814 gcc_unreachable ();
3817 break;
3819 case RID_TYPEID:
3821 tree type;
3822 const char *saved_message;
3823 bool saved_in_type_id_in_expr_p;
3825 /* Consume the `typeid' token. */
3826 cp_lexer_consume_token (parser->lexer);
3827 /* Look for the `(' token. */
3828 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3829 /* Types cannot be defined in a `typeid' expression. */
3830 saved_message = parser->type_definition_forbidden_message;
3831 parser->type_definition_forbidden_message
3832 = "types may not be defined in a `typeid\' expression";
3833 /* We can't be sure yet whether we're looking at a type-id or an
3834 expression. */
3835 cp_parser_parse_tentatively (parser);
3836 /* Try a type-id first. */
3837 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3838 parser->in_type_id_in_expr_p = true;
3839 type = cp_parser_type_id (parser);
3840 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3841 /* Look for the `)' token. Otherwise, we can't be sure that
3842 we're not looking at an expression: consider `typeid (int
3843 (3))', for example. */
3844 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3845 /* If all went well, simply lookup the type-id. */
3846 if (cp_parser_parse_definitely (parser))
3847 postfix_expression = get_typeid (type);
3848 /* Otherwise, fall back to the expression variant. */
3849 else
3851 tree expression;
3853 /* Look for an expression. */
3854 expression = cp_parser_expression (parser, /*cast_p=*/false);
3855 /* Compute its typeid. */
3856 postfix_expression = build_typeid (expression);
3857 /* Look for the `)' token. */
3858 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3860 /* `typeid' may not appear in an integral constant expression. */
3861 if (cp_parser_non_integral_constant_expression(parser,
3862 "`typeid' operator"))
3863 return error_mark_node;
3864 /* Restore the saved message. */
3865 parser->type_definition_forbidden_message = saved_message;
3867 break;
3869 case RID_TYPENAME:
3871 bool template_p = false;
3872 tree id;
3873 tree type;
3874 tree scope;
3876 /* Consume the `typename' token. */
3877 cp_lexer_consume_token (parser->lexer);
3878 /* Look for the optional `::' operator. */
3879 cp_parser_global_scope_opt (parser,
3880 /*current_scope_valid_p=*/false);
3881 /* Look for the nested-name-specifier. In case of error here,
3882 consume the trailing id to avoid subsequent error messages
3883 for usual cases. */
3884 scope = cp_parser_nested_name_specifier (parser,
3885 /*typename_keyword_p=*/true,
3886 /*check_dependency_p=*/true,
3887 /*type_p=*/true,
3888 /*is_declaration=*/true);
3890 /* Look for the optional `template' keyword. */
3891 template_p = cp_parser_optional_template_keyword (parser);
3892 /* We don't know whether we're looking at a template-id or an
3893 identifier. */
3894 cp_parser_parse_tentatively (parser);
3895 /* Try a template-id. */
3896 id = cp_parser_template_id (parser, template_p,
3897 /*check_dependency_p=*/true,
3898 /*is_declaration=*/true);
3899 /* If that didn't work, try an identifier. */
3900 if (!cp_parser_parse_definitely (parser))
3901 id = cp_parser_identifier (parser);
3903 /* Don't process id if nested name specifier is invalid. */
3904 if (scope == error_mark_node)
3905 return error_mark_node;
3906 /* If we look up a template-id in a non-dependent qualifying
3907 scope, there's no need to create a dependent type. */
3908 else if (TREE_CODE (id) == TYPE_DECL
3909 && !dependent_type_p (parser->scope))
3910 type = TREE_TYPE (id);
3911 /* Create a TYPENAME_TYPE to represent the type to which the
3912 functional cast is being performed. */
3913 else
3914 type = make_typename_type (parser->scope, id,
3915 typename_type,
3916 /*complain=*/1);
3918 postfix_expression = cp_parser_functional_cast (parser, type);
3920 break;
3922 default:
3924 tree type;
3926 /* If the next thing is a simple-type-specifier, we may be
3927 looking at a functional cast. We could also be looking at
3928 an id-expression. So, we try the functional cast, and if
3929 that doesn't work we fall back to the primary-expression. */
3930 cp_parser_parse_tentatively (parser);
3931 /* Look for the simple-type-specifier. */
3932 type = cp_parser_simple_type_specifier (parser,
3933 /*decl_specs=*/NULL,
3934 CP_PARSER_FLAGS_NONE);
3935 /* Parse the cast itself. */
3936 if (!cp_parser_error_occurred (parser))
3937 postfix_expression
3938 = cp_parser_functional_cast (parser, type);
3939 /* If that worked, we're done. */
3940 if (cp_parser_parse_definitely (parser))
3941 break;
3943 /* If the functional-cast didn't work out, try a
3944 compound-literal. */
3945 if (cp_parser_allow_gnu_extensions_p (parser)
3946 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3948 tree initializer_list = NULL_TREE;
3949 bool saved_in_type_id_in_expr_p;
3951 cp_parser_parse_tentatively (parser);
3952 /* Consume the `('. */
3953 cp_lexer_consume_token (parser->lexer);
3954 /* Parse the type. */
3955 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3956 parser->in_type_id_in_expr_p = true;
3957 type = cp_parser_type_id (parser);
3958 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3959 /* Look for the `)'. */
3960 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3961 /* Look for the `{'. */
3962 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3963 /* If things aren't going well, there's no need to
3964 keep going. */
3965 if (!cp_parser_error_occurred (parser))
3967 bool non_constant_p;
3968 /* Parse the initializer-list. */
3969 initializer_list
3970 = cp_parser_initializer_list (parser, &non_constant_p);
3971 /* Allow a trailing `,'. */
3972 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3973 cp_lexer_consume_token (parser->lexer);
3974 /* Look for the final `}'. */
3975 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3977 /* If that worked, we're definitely looking at a
3978 compound-literal expression. */
3979 if (cp_parser_parse_definitely (parser))
3981 /* Warn the user that a compound literal is not
3982 allowed in standard C++. */
3983 if (pedantic)
3984 pedwarn ("ISO C++ forbids compound-literals");
3985 /* Form the representation of the compound-literal. */
3986 postfix_expression
3987 = finish_compound_literal (type, initializer_list);
3988 break;
3992 /* It must be a primary-expression. */
3993 postfix_expression = cp_parser_primary_expression (parser,
3994 cast_p,
3995 &idk,
3996 &qualifying_class);
3998 break;
4001 /* If we were avoiding committing to the processing of a
4002 qualified-id until we knew whether or not we had a
4003 pointer-to-member, we now know. */
4004 if (qualifying_class)
4006 bool done;
4008 /* Peek at the next token. */
4009 token = cp_lexer_peek_token (parser->lexer);
4010 done = (token->type != CPP_OPEN_SQUARE
4011 && token->type != CPP_OPEN_PAREN
4012 && token->type != CPP_DOT
4013 && token->type != CPP_DEREF
4014 && token->type != CPP_PLUS_PLUS
4015 && token->type != CPP_MINUS_MINUS);
4017 postfix_expression = finish_qualified_id_expr (qualifying_class,
4018 postfix_expression,
4019 done,
4020 address_p);
4021 if (done)
4022 return postfix_expression;
4025 /* Keep looping until the postfix-expression is complete. */
4026 while (true)
4028 if (idk == CP_ID_KIND_UNQUALIFIED
4029 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4030 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4031 /* It is not a Koenig lookup function call. */
4032 postfix_expression
4033 = unqualified_name_lookup_error (postfix_expression);
4035 /* Peek at the next token. */
4036 token = cp_lexer_peek_token (parser->lexer);
4038 switch (token->type)
4040 case CPP_OPEN_SQUARE:
4041 postfix_expression
4042 = cp_parser_postfix_open_square_expression (parser,
4043 postfix_expression,
4044 false);
4045 idk = CP_ID_KIND_NONE;
4046 break;
4048 case CPP_OPEN_PAREN:
4049 /* postfix-expression ( expression-list [opt] ) */
4051 bool koenig_p;
4052 tree args = (cp_parser_parenthesized_expression_list
4053 (parser, false,
4054 /*cast_p=*/false,
4055 /*non_constant_p=*/NULL));
4057 if (args == error_mark_node)
4059 postfix_expression = error_mark_node;
4060 break;
4063 /* Function calls are not permitted in
4064 constant-expressions. */
4065 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4066 && cp_parser_non_integral_constant_expression (parser,
4067 "a function call"))
4069 postfix_expression = error_mark_node;
4070 break;
4073 koenig_p = false;
4074 if (idk == CP_ID_KIND_UNQUALIFIED)
4076 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4078 if (args)
4080 koenig_p = true;
4081 postfix_expression
4082 = perform_koenig_lookup (postfix_expression, args);
4084 else
4085 postfix_expression
4086 = unqualified_fn_lookup_error (postfix_expression);
4088 /* We do not perform argument-dependent lookup if
4089 normal lookup finds a non-function, in accordance
4090 with the expected resolution of DR 218. */
4091 else if (args && is_overloaded_fn (postfix_expression))
4093 tree fn = get_first_fn (postfix_expression);
4095 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4096 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4098 /* Only do argument dependent lookup if regular
4099 lookup does not find a set of member functions.
4100 [basic.lookup.koenig]/2a */
4101 if (!DECL_FUNCTION_MEMBER_P (fn))
4103 koenig_p = true;
4104 postfix_expression
4105 = perform_koenig_lookup (postfix_expression, args);
4110 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4112 tree instance = TREE_OPERAND (postfix_expression, 0);
4113 tree fn = TREE_OPERAND (postfix_expression, 1);
4115 if (processing_template_decl
4116 && (type_dependent_expression_p (instance)
4117 || (!BASELINK_P (fn)
4118 && TREE_CODE (fn) != FIELD_DECL)
4119 || type_dependent_expression_p (fn)
4120 || any_type_dependent_arguments_p (args)))
4122 postfix_expression
4123 = build_min_nt (CALL_EXPR, postfix_expression,
4124 args, NULL_TREE);
4125 break;
4128 if (BASELINK_P (fn))
4129 postfix_expression
4130 = (build_new_method_call
4131 (instance, fn, args, NULL_TREE,
4132 (idk == CP_ID_KIND_QUALIFIED
4133 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4134 else
4135 postfix_expression
4136 = finish_call_expr (postfix_expression, args,
4137 /*disallow_virtual=*/false,
4138 /*koenig_p=*/false);
4140 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4141 || TREE_CODE (postfix_expression) == MEMBER_REF
4142 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4143 postfix_expression = (build_offset_ref_call_from_tree
4144 (postfix_expression, args));
4145 else if (idk == CP_ID_KIND_QUALIFIED)
4146 /* A call to a static class member, or a namespace-scope
4147 function. */
4148 postfix_expression
4149 = finish_call_expr (postfix_expression, args,
4150 /*disallow_virtual=*/true,
4151 koenig_p);
4152 else
4153 /* All other function calls. */
4154 postfix_expression
4155 = finish_call_expr (postfix_expression, args,
4156 /*disallow_virtual=*/false,
4157 koenig_p);
4159 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4160 idk = CP_ID_KIND_NONE;
4162 break;
4164 case CPP_DOT:
4165 case CPP_DEREF:
4166 /* postfix-expression . template [opt] id-expression
4167 postfix-expression . pseudo-destructor-name
4168 postfix-expression -> template [opt] id-expression
4169 postfix-expression -> pseudo-destructor-name */
4171 /* Consume the `.' or `->' operator. */
4172 cp_lexer_consume_token (parser->lexer);
4174 postfix_expression
4175 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4176 postfix_expression,
4177 false, &idk);
4178 break;
4180 case CPP_PLUS_PLUS:
4181 /* postfix-expression ++ */
4182 /* Consume the `++' token. */
4183 cp_lexer_consume_token (parser->lexer);
4184 /* Generate a representation for the complete expression. */
4185 postfix_expression
4186 = finish_increment_expr (postfix_expression,
4187 POSTINCREMENT_EXPR);
4188 /* Increments may not appear in constant-expressions. */
4189 if (cp_parser_non_integral_constant_expression (parser,
4190 "an increment"))
4191 postfix_expression = error_mark_node;
4192 idk = CP_ID_KIND_NONE;
4193 break;
4195 case CPP_MINUS_MINUS:
4196 /* postfix-expression -- */
4197 /* Consume the `--' token. */
4198 cp_lexer_consume_token (parser->lexer);
4199 /* Generate a representation for the complete expression. */
4200 postfix_expression
4201 = finish_increment_expr (postfix_expression,
4202 POSTDECREMENT_EXPR);
4203 /* Decrements may not appear in constant-expressions. */
4204 if (cp_parser_non_integral_constant_expression (parser,
4205 "a decrement"))
4206 postfix_expression = error_mark_node;
4207 idk = CP_ID_KIND_NONE;
4208 break;
4210 default:
4211 return postfix_expression;
4215 /* We should never get here. */
4216 gcc_unreachable ();
4217 return error_mark_node;
4220 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4221 by cp_parser_builtin_offsetof. We're looking for
4223 postfix-expression [ expression ]
4225 FOR_OFFSETOF is set if we're being called in that context, which
4226 changes how we deal with integer constant expressions. */
4228 static tree
4229 cp_parser_postfix_open_square_expression (cp_parser *parser,
4230 tree postfix_expression,
4231 bool for_offsetof)
4233 tree index;
4235 /* Consume the `[' token. */
4236 cp_lexer_consume_token (parser->lexer);
4238 /* Parse the index expression. */
4239 /* ??? For offsetof, there is a question of what to allow here. If
4240 offsetof is not being used in an integral constant expression context,
4241 then we *could* get the right answer by computing the value at runtime.
4242 If we are in an integral constant expression context, then we might
4243 could accept any constant expression; hard to say without analysis.
4244 Rather than open the barn door too wide right away, allow only integer
4245 constant expressions here. */
4246 if (for_offsetof)
4247 index = cp_parser_constant_expression (parser, false, NULL);
4248 else
4249 index = cp_parser_expression (parser, /*cast_p=*/false);
4251 /* Look for the closing `]'. */
4252 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4254 /* Build the ARRAY_REF. */
4255 postfix_expression = grok_array_decl (postfix_expression, index);
4257 /* When not doing offsetof, array references are not permitted in
4258 constant-expressions. */
4259 if (!for_offsetof
4260 && (cp_parser_non_integral_constant_expression
4261 (parser, "an array reference")))
4262 postfix_expression = error_mark_node;
4264 return postfix_expression;
4267 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4268 by cp_parser_builtin_offsetof. We're looking for
4270 postfix-expression . template [opt] id-expression
4271 postfix-expression . pseudo-destructor-name
4272 postfix-expression -> template [opt] id-expression
4273 postfix-expression -> pseudo-destructor-name
4275 FOR_OFFSETOF is set if we're being called in that context. That sorta
4276 limits what of the above we'll actually accept, but nevermind.
4277 TOKEN_TYPE is the "." or "->" token, which will already have been
4278 removed from the stream. */
4280 static tree
4281 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4282 enum cpp_ttype token_type,
4283 tree postfix_expression,
4284 bool for_offsetof, cp_id_kind *idk)
4286 tree name;
4287 bool dependent_p;
4288 bool template_p;
4289 bool pseudo_destructor_p;
4290 tree scope = NULL_TREE;
4292 /* If this is a `->' operator, dereference the pointer. */
4293 if (token_type == CPP_DEREF)
4294 postfix_expression = build_x_arrow (postfix_expression);
4295 /* Check to see whether or not the expression is type-dependent. */
4296 dependent_p = type_dependent_expression_p (postfix_expression);
4297 /* The identifier following the `->' or `.' is not qualified. */
4298 parser->scope = NULL_TREE;
4299 parser->qualifying_scope = NULL_TREE;
4300 parser->object_scope = NULL_TREE;
4301 *idk = CP_ID_KIND_NONE;
4302 /* Enter the scope corresponding to the type of the object
4303 given by the POSTFIX_EXPRESSION. */
4304 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4306 scope = TREE_TYPE (postfix_expression);
4307 /* According to the standard, no expression should ever have
4308 reference type. Unfortunately, we do not currently match
4309 the standard in this respect in that our internal representation
4310 of an expression may have reference type even when the standard
4311 says it does not. Therefore, we have to manually obtain the
4312 underlying type here. */
4313 scope = non_reference (scope);
4314 /* The type of the POSTFIX_EXPRESSION must be complete. */
4315 scope = complete_type_or_else (scope, NULL_TREE);
4316 /* Let the name lookup machinery know that we are processing a
4317 class member access expression. */
4318 parser->context->object_type = scope;
4319 /* If something went wrong, we want to be able to discern that case,
4320 as opposed to the case where there was no SCOPE due to the type
4321 of expression being dependent. */
4322 if (!scope)
4323 scope = error_mark_node;
4324 /* If the SCOPE was erroneous, make the various semantic analysis
4325 functions exit quickly -- and without issuing additional error
4326 messages. */
4327 if (scope == error_mark_node)
4328 postfix_expression = error_mark_node;
4331 /* Assume this expression is not a pseudo-destructor access. */
4332 pseudo_destructor_p = false;
4334 /* If the SCOPE is a scalar type, then, if this is a valid program,
4335 we must be looking at a pseudo-destructor-name. */
4336 if (scope && SCALAR_TYPE_P (scope))
4338 tree s;
4339 tree type;
4341 cp_parser_parse_tentatively (parser);
4342 /* Parse the pseudo-destructor-name. */
4343 s = NULL_TREE;
4344 cp_parser_pseudo_destructor_name (parser, &s, &type);
4345 if (cp_parser_parse_definitely (parser))
4347 pseudo_destructor_p = true;
4348 postfix_expression
4349 = finish_pseudo_destructor_expr (postfix_expression,
4350 s, TREE_TYPE (type));
4354 if (!pseudo_destructor_p)
4356 /* If the SCOPE is not a scalar type, we are looking at an
4357 ordinary class member access expression, rather than a
4358 pseudo-destructor-name. */
4359 template_p = cp_parser_optional_template_keyword (parser);
4360 /* Parse the id-expression. */
4361 name = cp_parser_id_expression (parser, template_p,
4362 /*check_dependency_p=*/true,
4363 /*template_p=*/NULL,
4364 /*declarator_p=*/false);
4365 /* In general, build a SCOPE_REF if the member name is qualified.
4366 However, if the name was not dependent and has already been
4367 resolved; there is no need to build the SCOPE_REF. For example;
4369 struct X { void f(); };
4370 template <typename T> void f(T* t) { t->X::f(); }
4372 Even though "t" is dependent, "X::f" is not and has been resolved
4373 to a BASELINK; there is no need to include scope information. */
4375 /* But we do need to remember that there was an explicit scope for
4376 virtual function calls. */
4377 if (parser->scope)
4378 *idk = CP_ID_KIND_QUALIFIED;
4380 /* If the name is a template-id that names a type, we will get a
4381 TYPE_DECL here. That is invalid code. */
4382 if (TREE_CODE (name) == TYPE_DECL)
4384 error ("invalid use of %qD", name);
4385 postfix_expression = error_mark_node;
4387 else
4389 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4391 name = build_nt (SCOPE_REF, parser->scope, name);
4392 parser->scope = NULL_TREE;
4393 parser->qualifying_scope = NULL_TREE;
4394 parser->object_scope = NULL_TREE;
4396 if (scope && name && BASELINK_P (name))
4397 adjust_result_of_qualified_name_lookup
4398 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4399 postfix_expression
4400 = finish_class_member_access_expr (postfix_expression, name);
4404 /* We no longer need to look up names in the scope of the object on
4405 the left-hand side of the `.' or `->' operator. */
4406 parser->context->object_type = NULL_TREE;
4408 /* Outside of offsetof, these operators may not appear in
4409 constant-expressions. */
4410 if (!for_offsetof
4411 && (cp_parser_non_integral_constant_expression
4412 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4413 postfix_expression = error_mark_node;
4415 return postfix_expression;
4418 /* Parse a parenthesized expression-list.
4420 expression-list:
4421 assignment-expression
4422 expression-list, assignment-expression
4424 attribute-list:
4425 expression-list
4426 identifier
4427 identifier, expression-list
4429 CAST_P is true if this expression is the target of a cast.
4431 Returns a TREE_LIST. The TREE_VALUE of each node is a
4432 representation of an assignment-expression. Note that a TREE_LIST
4433 is returned even if there is only a single expression in the list.
4434 error_mark_node is returned if the ( and or ) are
4435 missing. NULL_TREE is returned on no expressions. The parentheses
4436 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4437 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4438 indicates whether or not all of the expressions in the list were
4439 constant. */
4441 static tree
4442 cp_parser_parenthesized_expression_list (cp_parser* parser,
4443 bool is_attribute_list,
4444 bool cast_p,
4445 bool *non_constant_p)
4447 tree expression_list = NULL_TREE;
4448 bool fold_expr_p = is_attribute_list;
4449 tree identifier = NULL_TREE;
4451 /* Assume all the expressions will be constant. */
4452 if (non_constant_p)
4453 *non_constant_p = false;
4455 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4456 return error_mark_node;
4458 /* Consume expressions until there are no more. */
4459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4460 while (true)
4462 tree expr;
4464 /* At the beginning of attribute lists, check to see if the
4465 next token is an identifier. */
4466 if (is_attribute_list
4467 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4469 cp_token *token;
4471 /* Consume the identifier. */
4472 token = cp_lexer_consume_token (parser->lexer);
4473 /* Save the identifier. */
4474 identifier = token->value;
4476 else
4478 /* Parse the next assignment-expression. */
4479 if (non_constant_p)
4481 bool expr_non_constant_p;
4482 expr = (cp_parser_constant_expression
4483 (parser, /*allow_non_constant_p=*/true,
4484 &expr_non_constant_p));
4485 if (expr_non_constant_p)
4486 *non_constant_p = true;
4488 else
4489 expr = cp_parser_assignment_expression (parser, cast_p);
4491 if (fold_expr_p)
4492 expr = fold_non_dependent_expr (expr);
4494 /* Add it to the list. We add error_mark_node
4495 expressions to the list, so that we can still tell if
4496 the correct form for a parenthesized expression-list
4497 is found. That gives better errors. */
4498 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4500 if (expr == error_mark_node)
4501 goto skip_comma;
4504 /* After the first item, attribute lists look the same as
4505 expression lists. */
4506 is_attribute_list = false;
4508 get_comma:;
4509 /* If the next token isn't a `,', then we are done. */
4510 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4511 break;
4513 /* Otherwise, consume the `,' and keep going. */
4514 cp_lexer_consume_token (parser->lexer);
4517 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4519 int ending;
4521 skip_comma:;
4522 /* We try and resync to an unnested comma, as that will give the
4523 user better diagnostics. */
4524 ending = cp_parser_skip_to_closing_parenthesis (parser,
4525 /*recovering=*/true,
4526 /*or_comma=*/true,
4527 /*consume_paren=*/true);
4528 if (ending < 0)
4529 goto get_comma;
4530 if (!ending)
4531 return error_mark_node;
4534 /* We built up the list in reverse order so we must reverse it now. */
4535 expression_list = nreverse (expression_list);
4536 if (identifier)
4537 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4539 return expression_list;
4542 /* Parse a pseudo-destructor-name.
4544 pseudo-destructor-name:
4545 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4546 :: [opt] nested-name-specifier template template-id :: ~ type-name
4547 :: [opt] nested-name-specifier [opt] ~ type-name
4549 If either of the first two productions is used, sets *SCOPE to the
4550 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4551 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4552 or ERROR_MARK_NODE if the parse fails. */
4554 static void
4555 cp_parser_pseudo_destructor_name (cp_parser* parser,
4556 tree* scope,
4557 tree* type)
4559 bool nested_name_specifier_p;
4561 /* Assume that things will not work out. */
4562 *type = error_mark_node;
4564 /* Look for the optional `::' operator. */
4565 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4566 /* Look for the optional nested-name-specifier. */
4567 nested_name_specifier_p
4568 = (cp_parser_nested_name_specifier_opt (parser,
4569 /*typename_keyword_p=*/false,
4570 /*check_dependency_p=*/true,
4571 /*type_p=*/false,
4572 /*is_declaration=*/true)
4573 != NULL_TREE);
4574 /* Now, if we saw a nested-name-specifier, we might be doing the
4575 second production. */
4576 if (nested_name_specifier_p
4577 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4579 /* Consume the `template' keyword. */
4580 cp_lexer_consume_token (parser->lexer);
4581 /* Parse the template-id. */
4582 cp_parser_template_id (parser,
4583 /*template_keyword_p=*/true,
4584 /*check_dependency_p=*/false,
4585 /*is_declaration=*/true);
4586 /* Look for the `::' token. */
4587 cp_parser_require (parser, CPP_SCOPE, "`::'");
4589 /* If the next token is not a `~', then there might be some
4590 additional qualification. */
4591 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4593 /* Look for the type-name. */
4594 *scope = TREE_TYPE (cp_parser_type_name (parser));
4596 if (*scope == error_mark_node)
4597 return;
4599 /* If we don't have ::~, then something has gone wrong. Since
4600 the only caller of this function is looking for something
4601 after `.' or `->' after a scalar type, most likely the
4602 program is trying to get a member of a non-aggregate
4603 type. */
4604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4605 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4607 cp_parser_error (parser, "request for member of non-aggregate type");
4608 return;
4611 /* Look for the `::' token. */
4612 cp_parser_require (parser, CPP_SCOPE, "`::'");
4614 else
4615 *scope = NULL_TREE;
4617 /* Look for the `~'. */
4618 cp_parser_require (parser, CPP_COMPL, "`~'");
4619 /* Look for the type-name again. We are not responsible for
4620 checking that it matches the first type-name. */
4621 *type = cp_parser_type_name (parser);
4624 /* Parse a unary-expression.
4626 unary-expression:
4627 postfix-expression
4628 ++ cast-expression
4629 -- cast-expression
4630 unary-operator cast-expression
4631 sizeof unary-expression
4632 sizeof ( type-id )
4633 new-expression
4634 delete-expression
4636 GNU Extensions:
4638 unary-expression:
4639 __extension__ cast-expression
4640 __alignof__ unary-expression
4641 __alignof__ ( type-id )
4642 __real__ cast-expression
4643 __imag__ cast-expression
4644 && identifier
4646 ADDRESS_P is true iff the unary-expression is appearing as the
4647 operand of the `&' operator. CAST_P is true if this expression is
4648 the target of a cast.
4650 Returns a representation of the expression. */
4652 static tree
4653 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4655 cp_token *token;
4656 enum tree_code unary_operator;
4658 /* Peek at the next token. */
4659 token = cp_lexer_peek_token (parser->lexer);
4660 /* Some keywords give away the kind of expression. */
4661 if (token->type == CPP_KEYWORD)
4663 enum rid keyword = token->keyword;
4665 switch (keyword)
4667 case RID_ALIGNOF:
4668 case RID_SIZEOF:
4670 tree operand;
4671 enum tree_code op;
4673 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4674 /* Consume the token. */
4675 cp_lexer_consume_token (parser->lexer);
4676 /* Parse the operand. */
4677 operand = cp_parser_sizeof_operand (parser, keyword);
4679 if (TYPE_P (operand))
4680 return cxx_sizeof_or_alignof_type (operand, op, true);
4681 else
4682 return cxx_sizeof_or_alignof_expr (operand, op);
4685 case RID_NEW:
4686 return cp_parser_new_expression (parser);
4688 case RID_DELETE:
4689 return cp_parser_delete_expression (parser);
4691 case RID_EXTENSION:
4693 /* The saved value of the PEDANTIC flag. */
4694 int saved_pedantic;
4695 tree expr;
4697 /* Save away the PEDANTIC flag. */
4698 cp_parser_extension_opt (parser, &saved_pedantic);
4699 /* Parse the cast-expression. */
4700 expr = cp_parser_simple_cast_expression (parser);
4701 /* Restore the PEDANTIC flag. */
4702 pedantic = saved_pedantic;
4704 return expr;
4707 case RID_REALPART:
4708 case RID_IMAGPART:
4710 tree expression;
4712 /* Consume the `__real__' or `__imag__' token. */
4713 cp_lexer_consume_token (parser->lexer);
4714 /* Parse the cast-expression. */
4715 expression = cp_parser_simple_cast_expression (parser);
4716 /* Create the complete representation. */
4717 return build_x_unary_op ((keyword == RID_REALPART
4718 ? REALPART_EXPR : IMAGPART_EXPR),
4719 expression);
4721 break;
4723 default:
4724 break;
4728 /* Look for the `:: new' and `:: delete', which also signal the
4729 beginning of a new-expression, or delete-expression,
4730 respectively. If the next token is `::', then it might be one of
4731 these. */
4732 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4734 enum rid keyword;
4736 /* See if the token after the `::' is one of the keywords in
4737 which we're interested. */
4738 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4739 /* If it's `new', we have a new-expression. */
4740 if (keyword == RID_NEW)
4741 return cp_parser_new_expression (parser);
4742 /* Similarly, for `delete'. */
4743 else if (keyword == RID_DELETE)
4744 return cp_parser_delete_expression (parser);
4747 /* Look for a unary operator. */
4748 unary_operator = cp_parser_unary_operator (token);
4749 /* The `++' and `--' operators can be handled similarly, even though
4750 they are not technically unary-operators in the grammar. */
4751 if (unary_operator == ERROR_MARK)
4753 if (token->type == CPP_PLUS_PLUS)
4754 unary_operator = PREINCREMENT_EXPR;
4755 else if (token->type == CPP_MINUS_MINUS)
4756 unary_operator = PREDECREMENT_EXPR;
4757 /* Handle the GNU address-of-label extension. */
4758 else if (cp_parser_allow_gnu_extensions_p (parser)
4759 && token->type == CPP_AND_AND)
4761 tree identifier;
4763 /* Consume the '&&' token. */
4764 cp_lexer_consume_token (parser->lexer);
4765 /* Look for the identifier. */
4766 identifier = cp_parser_identifier (parser);
4767 /* Create an expression representing the address. */
4768 return finish_label_address_expr (identifier);
4771 if (unary_operator != ERROR_MARK)
4773 tree cast_expression;
4774 tree expression = error_mark_node;
4775 const char *non_constant_p = NULL;
4777 /* Consume the operator token. */
4778 token = cp_lexer_consume_token (parser->lexer);
4779 /* Parse the cast-expression. */
4780 cast_expression
4781 = cp_parser_cast_expression (parser,
4782 unary_operator == ADDR_EXPR,
4783 /*cast_p=*/false);
4784 /* Now, build an appropriate representation. */
4785 switch (unary_operator)
4787 case INDIRECT_REF:
4788 non_constant_p = "`*'";
4789 expression = build_x_indirect_ref (cast_expression, "unary *");
4790 break;
4792 case ADDR_EXPR:
4793 non_constant_p = "`&'";
4794 /* Fall through. */
4795 case BIT_NOT_EXPR:
4796 expression = build_x_unary_op (unary_operator, cast_expression);
4797 break;
4799 case PREINCREMENT_EXPR:
4800 case PREDECREMENT_EXPR:
4801 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4802 ? "`++'" : "`--'");
4803 /* Fall through. */
4804 case CONVERT_EXPR:
4805 case NEGATE_EXPR:
4806 case TRUTH_NOT_EXPR:
4807 expression = finish_unary_op_expr (unary_operator, cast_expression);
4808 break;
4810 default:
4811 gcc_unreachable ();
4814 if (non_constant_p
4815 && cp_parser_non_integral_constant_expression (parser,
4816 non_constant_p))
4817 expression = error_mark_node;
4819 return expression;
4822 return cp_parser_postfix_expression (parser, address_p, cast_p);
4825 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4826 unary-operator, the corresponding tree code is returned. */
4828 static enum tree_code
4829 cp_parser_unary_operator (cp_token* token)
4831 switch (token->type)
4833 case CPP_MULT:
4834 return INDIRECT_REF;
4836 case CPP_AND:
4837 return ADDR_EXPR;
4839 case CPP_PLUS:
4840 return CONVERT_EXPR;
4842 case CPP_MINUS:
4843 return NEGATE_EXPR;
4845 case CPP_NOT:
4846 return TRUTH_NOT_EXPR;
4848 case CPP_COMPL:
4849 return BIT_NOT_EXPR;
4851 default:
4852 return ERROR_MARK;
4856 /* Parse a new-expression.
4858 new-expression:
4859 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4860 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4862 Returns a representation of the expression. */
4864 static tree
4865 cp_parser_new_expression (cp_parser* parser)
4867 bool global_scope_p;
4868 tree placement;
4869 tree type;
4870 tree initializer;
4871 tree nelts;
4873 /* Look for the optional `::' operator. */
4874 global_scope_p
4875 = (cp_parser_global_scope_opt (parser,
4876 /*current_scope_valid_p=*/false)
4877 != NULL_TREE);
4878 /* Look for the `new' operator. */
4879 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4880 /* There's no easy way to tell a new-placement from the
4881 `( type-id )' construct. */
4882 cp_parser_parse_tentatively (parser);
4883 /* Look for a new-placement. */
4884 placement = cp_parser_new_placement (parser);
4885 /* If that didn't work out, there's no new-placement. */
4886 if (!cp_parser_parse_definitely (parser))
4887 placement = NULL_TREE;
4889 /* If the next token is a `(', then we have a parenthesized
4890 type-id. */
4891 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4893 /* Consume the `('. */
4894 cp_lexer_consume_token (parser->lexer);
4895 /* Parse the type-id. */
4896 type = cp_parser_type_id (parser);
4897 /* Look for the closing `)'. */
4898 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4899 /* There should not be a direct-new-declarator in this production,
4900 but GCC used to allowed this, so we check and emit a sensible error
4901 message for this case. */
4902 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4904 error ("array bound forbidden after parenthesized type-id");
4905 inform ("try removing the parentheses around the type-id");
4906 cp_parser_direct_new_declarator (parser);
4908 nelts = NULL_TREE;
4910 /* Otherwise, there must be a new-type-id. */
4911 else
4912 type = cp_parser_new_type_id (parser, &nelts);
4914 /* If the next token is a `(', then we have a new-initializer. */
4915 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4916 initializer = cp_parser_new_initializer (parser);
4917 else
4918 initializer = NULL_TREE;
4920 /* A new-expression may not appear in an integral constant
4921 expression. */
4922 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4923 return error_mark_node;
4925 /* Create a representation of the new-expression. */
4926 return build_new (placement, type, nelts, initializer, global_scope_p);
4929 /* Parse a new-placement.
4931 new-placement:
4932 ( expression-list )
4934 Returns the same representation as for an expression-list. */
4936 static tree
4937 cp_parser_new_placement (cp_parser* parser)
4939 tree expression_list;
4941 /* Parse the expression-list. */
4942 expression_list = (cp_parser_parenthesized_expression_list
4943 (parser, false, /*cast_p=*/false,
4944 /*non_constant_p=*/NULL));
4946 return expression_list;
4949 /* Parse a new-type-id.
4951 new-type-id:
4952 type-specifier-seq new-declarator [opt]
4954 Returns the TYPE allocated. If the new-type-id indicates an array
4955 type, *NELTS is set to the number of elements in the last array
4956 bound; the TYPE will not include the last array bound. */
4958 static tree
4959 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4961 cp_decl_specifier_seq type_specifier_seq;
4962 cp_declarator *new_declarator;
4963 cp_declarator *declarator;
4964 cp_declarator *outer_declarator;
4965 const char *saved_message;
4966 tree type;
4968 /* The type-specifier sequence must not contain type definitions.
4969 (It cannot contain declarations of new types either, but if they
4970 are not definitions we will catch that because they are not
4971 complete.) */
4972 saved_message = parser->type_definition_forbidden_message;
4973 parser->type_definition_forbidden_message
4974 = "types may not be defined in a new-type-id";
4975 /* Parse the type-specifier-seq. */
4976 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4977 /* Restore the old message. */
4978 parser->type_definition_forbidden_message = saved_message;
4979 /* Parse the new-declarator. */
4980 new_declarator = cp_parser_new_declarator_opt (parser);
4982 /* Determine the number of elements in the last array dimension, if
4983 any. */
4984 *nelts = NULL_TREE;
4985 /* Skip down to the last array dimension. */
4986 declarator = new_declarator;
4987 outer_declarator = NULL;
4988 while (declarator && (declarator->kind == cdk_pointer
4989 || declarator->kind == cdk_ptrmem))
4991 outer_declarator = declarator;
4992 declarator = declarator->declarator;
4994 while (declarator
4995 && declarator->kind == cdk_array
4996 && declarator->declarator
4997 && declarator->declarator->kind == cdk_array)
4999 outer_declarator = declarator;
5000 declarator = declarator->declarator;
5003 if (declarator && declarator->kind == cdk_array)
5005 *nelts = declarator->u.array.bounds;
5006 if (*nelts == error_mark_node)
5007 *nelts = integer_one_node;
5009 if (outer_declarator)
5010 outer_declarator->declarator = declarator->declarator;
5011 else
5012 new_declarator = NULL;
5015 type = groktypename (&type_specifier_seq, new_declarator);
5016 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5018 *nelts = array_type_nelts_top (type);
5019 type = TREE_TYPE (type);
5021 return type;
5024 /* Parse an (optional) new-declarator.
5026 new-declarator:
5027 ptr-operator new-declarator [opt]
5028 direct-new-declarator
5030 Returns the declarator. */
5032 static cp_declarator *
5033 cp_parser_new_declarator_opt (cp_parser* parser)
5035 enum tree_code code;
5036 tree type;
5037 cp_cv_quals cv_quals;
5039 /* We don't know if there's a ptr-operator next, or not. */
5040 cp_parser_parse_tentatively (parser);
5041 /* Look for a ptr-operator. */
5042 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5043 /* If that worked, look for more new-declarators. */
5044 if (cp_parser_parse_definitely (parser))
5046 cp_declarator *declarator;
5048 /* Parse another optional declarator. */
5049 declarator = cp_parser_new_declarator_opt (parser);
5051 /* Create the representation of the declarator. */
5052 if (type)
5053 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5054 else if (code == INDIRECT_REF)
5055 declarator = make_pointer_declarator (cv_quals, declarator);
5056 else
5057 declarator = make_reference_declarator (cv_quals, declarator);
5059 return declarator;
5062 /* If the next token is a `[', there is a direct-new-declarator. */
5063 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5064 return cp_parser_direct_new_declarator (parser);
5066 return NULL;
5069 /* Parse a direct-new-declarator.
5071 direct-new-declarator:
5072 [ expression ]
5073 direct-new-declarator [constant-expression]
5077 static cp_declarator *
5078 cp_parser_direct_new_declarator (cp_parser* parser)
5080 cp_declarator *declarator = NULL;
5082 while (true)
5084 tree expression;
5086 /* Look for the opening `['. */
5087 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5088 /* The first expression is not required to be constant. */
5089 if (!declarator)
5091 expression = cp_parser_expression (parser, /*cast_p=*/false);
5092 /* The standard requires that the expression have integral
5093 type. DR 74 adds enumeration types. We believe that the
5094 real intent is that these expressions be handled like the
5095 expression in a `switch' condition, which also allows
5096 classes with a single conversion to integral or
5097 enumeration type. */
5098 if (!processing_template_decl)
5100 expression
5101 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5102 expression,
5103 /*complain=*/true);
5104 if (!expression)
5106 error ("expression in new-declarator must have integral "
5107 "or enumeration type");
5108 expression = error_mark_node;
5112 /* But all the other expressions must be. */
5113 else
5114 expression
5115 = cp_parser_constant_expression (parser,
5116 /*allow_non_constant=*/false,
5117 NULL);
5118 /* Look for the closing `]'. */
5119 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5121 /* Add this bound to the declarator. */
5122 declarator = make_array_declarator (declarator, expression);
5124 /* If the next token is not a `[', then there are no more
5125 bounds. */
5126 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5127 break;
5130 return declarator;
5133 /* Parse a new-initializer.
5135 new-initializer:
5136 ( expression-list [opt] )
5138 Returns a representation of the expression-list. If there is no
5139 expression-list, VOID_ZERO_NODE is returned. */
5141 static tree
5142 cp_parser_new_initializer (cp_parser* parser)
5144 tree expression_list;
5146 expression_list = (cp_parser_parenthesized_expression_list
5147 (parser, false, /*cast_p=*/false,
5148 /*non_constant_p=*/NULL));
5149 if (!expression_list)
5150 expression_list = void_zero_node;
5152 return expression_list;
5155 /* Parse a delete-expression.
5157 delete-expression:
5158 :: [opt] delete cast-expression
5159 :: [opt] delete [ ] cast-expression
5161 Returns a representation of the expression. */
5163 static tree
5164 cp_parser_delete_expression (cp_parser* parser)
5166 bool global_scope_p;
5167 bool array_p;
5168 tree expression;
5170 /* Look for the optional `::' operator. */
5171 global_scope_p
5172 = (cp_parser_global_scope_opt (parser,
5173 /*current_scope_valid_p=*/false)
5174 != NULL_TREE);
5175 /* Look for the `delete' keyword. */
5176 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5177 /* See if the array syntax is in use. */
5178 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5180 /* Consume the `[' token. */
5181 cp_lexer_consume_token (parser->lexer);
5182 /* Look for the `]' token. */
5183 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5184 /* Remember that this is the `[]' construct. */
5185 array_p = true;
5187 else
5188 array_p = false;
5190 /* Parse the cast-expression. */
5191 expression = cp_parser_simple_cast_expression (parser);
5193 /* A delete-expression may not appear in an integral constant
5194 expression. */
5195 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5196 return error_mark_node;
5198 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5201 /* Parse a cast-expression.
5203 cast-expression:
5204 unary-expression
5205 ( type-id ) cast-expression
5207 ADDRESS_P is true iff the unary-expression is appearing as the
5208 operand of the `&' operator. CAST_P is true if this expression is
5209 the target of a cast.
5211 Returns a representation of the expression. */
5213 static tree
5214 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5216 /* If it's a `(', then we might be looking at a cast. */
5217 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5219 tree type = NULL_TREE;
5220 tree expr = NULL_TREE;
5221 bool compound_literal_p;
5222 const char *saved_message;
5224 /* There's no way to know yet whether or not this is a cast.
5225 For example, `(int (3))' is a unary-expression, while `(int)
5226 3' is a cast. So, we resort to parsing tentatively. */
5227 cp_parser_parse_tentatively (parser);
5228 /* Types may not be defined in a cast. */
5229 saved_message = parser->type_definition_forbidden_message;
5230 parser->type_definition_forbidden_message
5231 = "types may not be defined in casts";
5232 /* Consume the `('. */
5233 cp_lexer_consume_token (parser->lexer);
5234 /* A very tricky bit is that `(struct S) { 3 }' is a
5235 compound-literal (which we permit in C++ as an extension).
5236 But, that construct is not a cast-expression -- it is a
5237 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5238 is legal; if the compound-literal were a cast-expression,
5239 you'd need an extra set of parentheses.) But, if we parse
5240 the type-id, and it happens to be a class-specifier, then we
5241 will commit to the parse at that point, because we cannot
5242 undo the action that is done when creating a new class. So,
5243 then we cannot back up and do a postfix-expression.
5245 Therefore, we scan ahead to the closing `)', and check to see
5246 if the token after the `)' is a `{'. If so, we are not
5247 looking at a cast-expression.
5249 Save tokens so that we can put them back. */
5250 cp_lexer_save_tokens (parser->lexer);
5251 /* Skip tokens until the next token is a closing parenthesis.
5252 If we find the closing `)', and the next token is a `{', then
5253 we are looking at a compound-literal. */
5254 compound_literal_p
5255 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5256 /*consume_paren=*/true)
5257 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5258 /* Roll back the tokens we skipped. */
5259 cp_lexer_rollback_tokens (parser->lexer);
5260 /* If we were looking at a compound-literal, simulate an error
5261 so that the call to cp_parser_parse_definitely below will
5262 fail. */
5263 if (compound_literal_p)
5264 cp_parser_simulate_error (parser);
5265 else
5267 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5268 parser->in_type_id_in_expr_p = true;
5269 /* Look for the type-id. */
5270 type = cp_parser_type_id (parser);
5271 /* Look for the closing `)'. */
5272 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5273 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5276 /* Restore the saved message. */
5277 parser->type_definition_forbidden_message = saved_message;
5279 /* If ok so far, parse the dependent expression. We cannot be
5280 sure it is a cast. Consider `(T ())'. It is a parenthesized
5281 ctor of T, but looks like a cast to function returning T
5282 without a dependent expression. */
5283 if (!cp_parser_error_occurred (parser))
5284 expr = cp_parser_cast_expression (parser,
5285 /*address_p=*/false,
5286 /*cast_p=*/true);
5288 if (cp_parser_parse_definitely (parser))
5290 /* Warn about old-style casts, if so requested. */
5291 if (warn_old_style_cast
5292 && !in_system_header
5293 && !VOID_TYPE_P (type)
5294 && current_lang_name != lang_name_c)
5295 warning ("use of old-style cast");
5297 /* Only type conversions to integral or enumeration types
5298 can be used in constant-expressions. */
5299 if (parser->integral_constant_expression_p
5300 && !dependent_type_p (type)
5301 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5302 && (cp_parser_non_integral_constant_expression
5303 (parser,
5304 "a cast to a type other than an integral or "
5305 "enumeration type")))
5306 return error_mark_node;
5308 /* Perform the cast. */
5309 expr = build_c_cast (type, expr);
5310 return expr;
5314 /* If we get here, then it's not a cast, so it must be a
5315 unary-expression. */
5316 return cp_parser_unary_expression (parser, address_p, cast_p);
5319 /* Parse a binary expression of the general form:
5321 pm-expression:
5322 cast-expression
5323 pm-expression .* cast-expression
5324 pm-expression ->* cast-expression
5326 multiplicative-expression:
5327 pm-expression
5328 multiplicative-expression * pm-expression
5329 multiplicative-expression / pm-expression
5330 multiplicative-expression % pm-expression
5332 additive-expression:
5333 multiplicative-expression
5334 additive-expression + multiplicative-expression
5335 additive-expression - multiplicative-expression
5337 shift-expression:
5338 additive-expression
5339 shift-expression << additive-expression
5340 shift-expression >> additive-expression
5342 relational-expression:
5343 shift-expression
5344 relational-expression < shift-expression
5345 relational-expression > shift-expression
5346 relational-expression <= shift-expression
5347 relational-expression >= shift-expression
5349 GNU Extension:
5351 relational-expression:
5352 relational-expression <? shift-expression
5353 relational-expression >? shift-expression
5355 equality-expression:
5356 relational-expression
5357 equality-expression == relational-expression
5358 equality-expression != relational-expression
5360 and-expression:
5361 equality-expression
5362 and-expression & equality-expression
5364 exclusive-or-expression:
5365 and-expression
5366 exclusive-or-expression ^ and-expression
5368 inclusive-or-expression:
5369 exclusive-or-expression
5370 inclusive-or-expression | exclusive-or-expression
5372 logical-and-expression:
5373 inclusive-or-expression
5374 logical-and-expression && inclusive-or-expression
5376 logical-or-expression:
5377 logical-and-expression
5378 logical-or-expression || logical-and-expression
5380 All these are implemented with a single function like:
5382 binary-expression:
5383 simple-cast-expression
5384 binary-expression <token> binary-expression
5386 CAST_P is true if this expression is the target of a cast.
5388 The binops_by_token map is used to get the tree codes for each <token> type.
5389 binary-expressions are associated according to a precedence table. */
5391 #define TOKEN_PRECEDENCE(token) \
5392 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5393 ? PREC_NOT_OPERATOR \
5394 : binops_by_token[token->type].prec)
5396 static tree
5397 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5399 cp_parser_expression_stack stack;
5400 cp_parser_expression_stack_entry *sp = &stack[0];
5401 tree lhs, rhs;
5402 cp_token *token;
5403 enum tree_code tree_type;
5404 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5405 bool overloaded_p;
5407 /* Parse the first expression. */
5408 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5410 for (;;)
5412 /* Get an operator token. */
5413 token = cp_lexer_peek_token (parser->lexer);
5414 if (token->type == CPP_MIN || token->type == CPP_MAX)
5415 cp_parser_warn_min_max ();
5417 new_prec = TOKEN_PRECEDENCE (token);
5419 /* Popping an entry off the stack means we completed a subexpression:
5420 - either we found a token which is not an operator (`>' where it is not
5421 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5422 will happen repeatedly;
5423 - or, we found an operator which has lower priority. This is the case
5424 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5425 parsing `3 * 4'. */
5426 if (new_prec <= prec)
5428 if (sp == stack)
5429 break;
5430 else
5431 goto pop;
5434 get_rhs:
5435 tree_type = binops_by_token[token->type].tree_type;
5437 /* We used the operator token. */
5438 cp_lexer_consume_token (parser->lexer);
5440 /* Extract another operand. It may be the RHS of this expression
5441 or the LHS of a new, higher priority expression. */
5442 rhs = cp_parser_simple_cast_expression (parser);
5444 /* Get another operator token. Look up its precedence to avoid
5445 building a useless (immediately popped) stack entry for common
5446 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5447 token = cp_lexer_peek_token (parser->lexer);
5448 lookahead_prec = TOKEN_PRECEDENCE (token);
5449 if (lookahead_prec > new_prec)
5451 /* ... and prepare to parse the RHS of the new, higher priority
5452 expression. Since precedence levels on the stack are
5453 monotonically increasing, we do not have to care about
5454 stack overflows. */
5455 sp->prec = prec;
5456 sp->tree_type = tree_type;
5457 sp->lhs = lhs;
5458 sp++;
5459 lhs = rhs;
5460 prec = new_prec;
5461 new_prec = lookahead_prec;
5462 goto get_rhs;
5464 pop:
5465 /* If the stack is not empty, we have parsed into LHS the right side
5466 (`4' in the example above) of an expression we had suspended.
5467 We can use the information on the stack to recover the LHS (`3')
5468 from the stack together with the tree code (`MULT_EXPR'), and
5469 the precedence of the higher level subexpression
5470 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5471 which will be used to actually build the additive expression. */
5472 --sp;
5473 prec = sp->prec;
5474 tree_type = sp->tree_type;
5475 rhs = lhs;
5476 lhs = sp->lhs;
5479 overloaded_p = false;
5480 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5482 /* If the binary operator required the use of an overloaded operator,
5483 then this expression cannot be an integral constant-expression.
5484 An overloaded operator can be used even if both operands are
5485 otherwise permissible in an integral constant-expression if at
5486 least one of the operands is of enumeration type. */
5488 if (overloaded_p
5489 && (cp_parser_non_integral_constant_expression
5490 (parser, "calls to overloaded operators")))
5491 return error_mark_node;
5494 return lhs;
5498 /* Parse the `? expression : assignment-expression' part of a
5499 conditional-expression. The LOGICAL_OR_EXPR is the
5500 logical-or-expression that started the conditional-expression.
5501 Returns a representation of the entire conditional-expression.
5503 This routine is used by cp_parser_assignment_expression.
5505 ? expression : assignment-expression
5507 GNU Extensions:
5509 ? : assignment-expression */
5511 static tree
5512 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5514 tree expr;
5515 tree assignment_expr;
5517 /* Consume the `?' token. */
5518 cp_lexer_consume_token (parser->lexer);
5519 if (cp_parser_allow_gnu_extensions_p (parser)
5520 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5521 /* Implicit true clause. */
5522 expr = NULL_TREE;
5523 else
5524 /* Parse the expression. */
5525 expr = cp_parser_expression (parser, /*cast_p=*/false);
5527 /* The next token should be a `:'. */
5528 cp_parser_require (parser, CPP_COLON, "`:'");
5529 /* Parse the assignment-expression. */
5530 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5532 /* Build the conditional-expression. */
5533 return build_x_conditional_expr (logical_or_expr,
5534 expr,
5535 assignment_expr);
5538 /* Parse an assignment-expression.
5540 assignment-expression:
5541 conditional-expression
5542 logical-or-expression assignment-operator assignment_expression
5543 throw-expression
5545 CAST_P is true if this expression is the target of a cast.
5547 Returns a representation for the expression. */
5549 static tree
5550 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5552 tree expr;
5554 /* If the next token is the `throw' keyword, then we're looking at
5555 a throw-expression. */
5556 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5557 expr = cp_parser_throw_expression (parser);
5558 /* Otherwise, it must be that we are looking at a
5559 logical-or-expression. */
5560 else
5562 /* Parse the binary expressions (logical-or-expression). */
5563 expr = cp_parser_binary_expression (parser, cast_p);
5564 /* If the next token is a `?' then we're actually looking at a
5565 conditional-expression. */
5566 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5567 return cp_parser_question_colon_clause (parser, expr);
5568 else
5570 enum tree_code assignment_operator;
5572 /* If it's an assignment-operator, we're using the second
5573 production. */
5574 assignment_operator
5575 = cp_parser_assignment_operator_opt (parser);
5576 if (assignment_operator != ERROR_MARK)
5578 tree rhs;
5580 /* Parse the right-hand side of the assignment. */
5581 rhs = cp_parser_assignment_expression (parser, cast_p);
5582 /* An assignment may not appear in a
5583 constant-expression. */
5584 if (cp_parser_non_integral_constant_expression (parser,
5585 "an assignment"))
5586 return error_mark_node;
5587 /* Build the assignment expression. */
5588 expr = build_x_modify_expr (expr,
5589 assignment_operator,
5590 rhs);
5595 return expr;
5598 /* Parse an (optional) assignment-operator.
5600 assignment-operator: one of
5601 = *= /= %= += -= >>= <<= &= ^= |=
5603 GNU Extension:
5605 assignment-operator: one of
5606 <?= >?=
5608 If the next token is an assignment operator, the corresponding tree
5609 code is returned, and the token is consumed. For example, for
5610 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5611 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5612 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5613 operator, ERROR_MARK is returned. */
5615 static enum tree_code
5616 cp_parser_assignment_operator_opt (cp_parser* parser)
5618 enum tree_code op;
5619 cp_token *token;
5621 /* Peek at the next toen. */
5622 token = cp_lexer_peek_token (parser->lexer);
5624 switch (token->type)
5626 case CPP_EQ:
5627 op = NOP_EXPR;
5628 break;
5630 case CPP_MULT_EQ:
5631 op = MULT_EXPR;
5632 break;
5634 case CPP_DIV_EQ:
5635 op = TRUNC_DIV_EXPR;
5636 break;
5638 case CPP_MOD_EQ:
5639 op = TRUNC_MOD_EXPR;
5640 break;
5642 case CPP_PLUS_EQ:
5643 op = PLUS_EXPR;
5644 break;
5646 case CPP_MINUS_EQ:
5647 op = MINUS_EXPR;
5648 break;
5650 case CPP_RSHIFT_EQ:
5651 op = RSHIFT_EXPR;
5652 break;
5654 case CPP_LSHIFT_EQ:
5655 op = LSHIFT_EXPR;
5656 break;
5658 case CPP_AND_EQ:
5659 op = BIT_AND_EXPR;
5660 break;
5662 case CPP_XOR_EQ:
5663 op = BIT_XOR_EXPR;
5664 break;
5666 case CPP_OR_EQ:
5667 op = BIT_IOR_EXPR;
5668 break;
5670 case CPP_MIN_EQ:
5671 op = MIN_EXPR;
5672 cp_parser_warn_min_max ();
5673 break;
5675 case CPP_MAX_EQ:
5676 op = MAX_EXPR;
5677 cp_parser_warn_min_max ();
5678 break;
5680 default:
5681 /* Nothing else is an assignment operator. */
5682 op = ERROR_MARK;
5685 /* If it was an assignment operator, consume it. */
5686 if (op != ERROR_MARK)
5687 cp_lexer_consume_token (parser->lexer);
5689 return op;
5692 /* Parse an expression.
5694 expression:
5695 assignment-expression
5696 expression , assignment-expression
5698 CAST_P is true if this expression is the target of a cast.
5700 Returns a representation of the expression. */
5702 static tree
5703 cp_parser_expression (cp_parser* parser, bool cast_p)
5705 tree expression = NULL_TREE;
5707 while (true)
5709 tree assignment_expression;
5711 /* Parse the next assignment-expression. */
5712 assignment_expression
5713 = cp_parser_assignment_expression (parser, cast_p);
5714 /* If this is the first assignment-expression, we can just
5715 save it away. */
5716 if (!expression)
5717 expression = assignment_expression;
5718 else
5719 expression = build_x_compound_expr (expression,
5720 assignment_expression);
5721 /* If the next token is not a comma, then we are done with the
5722 expression. */
5723 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5724 break;
5725 /* Consume the `,'. */
5726 cp_lexer_consume_token (parser->lexer);
5727 /* A comma operator cannot appear in a constant-expression. */
5728 if (cp_parser_non_integral_constant_expression (parser,
5729 "a comma operator"))
5730 expression = error_mark_node;
5733 return expression;
5736 /* Parse a constant-expression.
5738 constant-expression:
5739 conditional-expression
5741 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5742 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5743 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5744 is false, NON_CONSTANT_P should be NULL. */
5746 static tree
5747 cp_parser_constant_expression (cp_parser* parser,
5748 bool allow_non_constant_p,
5749 bool *non_constant_p)
5751 bool saved_integral_constant_expression_p;
5752 bool saved_allow_non_integral_constant_expression_p;
5753 bool saved_non_integral_constant_expression_p;
5754 tree expression;
5756 /* It might seem that we could simply parse the
5757 conditional-expression, and then check to see if it were
5758 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5759 one that the compiler can figure out is constant, possibly after
5760 doing some simplifications or optimizations. The standard has a
5761 precise definition of constant-expression, and we must honor
5762 that, even though it is somewhat more restrictive.
5764 For example:
5766 int i[(2, 3)];
5768 is not a legal declaration, because `(2, 3)' is not a
5769 constant-expression. The `,' operator is forbidden in a
5770 constant-expression. However, GCC's constant-folding machinery
5771 will fold this operation to an INTEGER_CST for `3'. */
5773 /* Save the old settings. */
5774 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5775 saved_allow_non_integral_constant_expression_p
5776 = parser->allow_non_integral_constant_expression_p;
5777 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5778 /* We are now parsing a constant-expression. */
5779 parser->integral_constant_expression_p = true;
5780 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5781 parser->non_integral_constant_expression_p = false;
5782 /* Although the grammar says "conditional-expression", we parse an
5783 "assignment-expression", which also permits "throw-expression"
5784 and the use of assignment operators. In the case that
5785 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5786 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5787 actually essential that we look for an assignment-expression.
5788 For example, cp_parser_initializer_clauses uses this function to
5789 determine whether a particular assignment-expression is in fact
5790 constant. */
5791 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5792 /* Restore the old settings. */
5793 parser->integral_constant_expression_p
5794 = saved_integral_constant_expression_p;
5795 parser->allow_non_integral_constant_expression_p
5796 = saved_allow_non_integral_constant_expression_p;
5797 if (allow_non_constant_p)
5798 *non_constant_p = parser->non_integral_constant_expression_p;
5799 else if (parser->non_integral_constant_expression_p)
5800 expression = error_mark_node;
5801 parser->non_integral_constant_expression_p
5802 = saved_non_integral_constant_expression_p;
5804 return expression;
5807 /* Parse __builtin_offsetof.
5809 offsetof-expression:
5810 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5812 offsetof-member-designator:
5813 id-expression
5814 | offsetof-member-designator "." id-expression
5815 | offsetof-member-designator "[" expression "]"
5818 static tree
5819 cp_parser_builtin_offsetof (cp_parser *parser)
5821 int save_ice_p, save_non_ice_p;
5822 tree type, expr;
5823 cp_id_kind dummy;
5825 /* We're about to accept non-integral-constant things, but will
5826 definitely yield an integral constant expression. Save and
5827 restore these values around our local parsing. */
5828 save_ice_p = parser->integral_constant_expression_p;
5829 save_non_ice_p = parser->non_integral_constant_expression_p;
5831 /* Consume the "__builtin_offsetof" token. */
5832 cp_lexer_consume_token (parser->lexer);
5833 /* Consume the opening `('. */
5834 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5835 /* Parse the type-id. */
5836 type = cp_parser_type_id (parser);
5837 /* Look for the `,'. */
5838 cp_parser_require (parser, CPP_COMMA, "`,'");
5840 /* Build the (type *)null that begins the traditional offsetof macro. */
5841 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5843 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5844 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5845 true, &dummy);
5846 while (true)
5848 cp_token *token = cp_lexer_peek_token (parser->lexer);
5849 switch (token->type)
5851 case CPP_OPEN_SQUARE:
5852 /* offsetof-member-designator "[" expression "]" */
5853 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5854 break;
5856 case CPP_DOT:
5857 /* offsetof-member-designator "." identifier */
5858 cp_lexer_consume_token (parser->lexer);
5859 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5860 true, &dummy);
5861 break;
5863 case CPP_CLOSE_PAREN:
5864 /* Consume the ")" token. */
5865 cp_lexer_consume_token (parser->lexer);
5866 goto success;
5868 default:
5869 /* Error. We know the following require will fail, but
5870 that gives the proper error message. */
5871 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5872 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5873 expr = error_mark_node;
5874 goto failure;
5878 success:
5879 /* If we're processing a template, we can't finish the semantics yet.
5880 Otherwise we can fold the entire expression now. */
5881 if (processing_template_decl)
5882 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5883 else
5884 expr = fold_offsetof (expr);
5886 failure:
5887 parser->integral_constant_expression_p = save_ice_p;
5888 parser->non_integral_constant_expression_p = save_non_ice_p;
5890 return expr;
5893 /* Statements [gram.stmt.stmt] */
5895 /* Parse a statement.
5897 statement:
5898 labeled-statement
5899 expression-statement
5900 compound-statement
5901 selection-statement
5902 iteration-statement
5903 jump-statement
5904 declaration-statement
5905 try-block */
5907 static void
5908 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5910 tree statement;
5911 cp_token *token;
5912 location_t statement_location;
5914 /* There is no statement yet. */
5915 statement = NULL_TREE;
5916 /* Peek at the next token. */
5917 token = cp_lexer_peek_token (parser->lexer);
5918 /* Remember the location of the first token in the statement. */
5919 statement_location = token->location;
5920 /* If this is a keyword, then that will often determine what kind of
5921 statement we have. */
5922 if (token->type == CPP_KEYWORD)
5924 enum rid keyword = token->keyword;
5926 switch (keyword)
5928 case RID_CASE:
5929 case RID_DEFAULT:
5930 statement = cp_parser_labeled_statement (parser,
5931 in_statement_expr);
5932 break;
5934 case RID_IF:
5935 case RID_SWITCH:
5936 statement = cp_parser_selection_statement (parser);
5937 break;
5939 case RID_WHILE:
5940 case RID_DO:
5941 case RID_FOR:
5942 statement = cp_parser_iteration_statement (parser);
5943 break;
5945 case RID_BREAK:
5946 case RID_CONTINUE:
5947 case RID_RETURN:
5948 case RID_GOTO:
5949 statement = cp_parser_jump_statement (parser);
5950 break;
5952 case RID_TRY:
5953 statement = cp_parser_try_block (parser);
5954 break;
5956 default:
5957 /* It might be a keyword like `int' that can start a
5958 declaration-statement. */
5959 break;
5962 else if (token->type == CPP_NAME)
5964 /* If the next token is a `:', then we are looking at a
5965 labeled-statement. */
5966 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5967 if (token->type == CPP_COLON)
5968 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5970 /* Anything that starts with a `{' must be a compound-statement. */
5971 else if (token->type == CPP_OPEN_BRACE)
5972 statement = cp_parser_compound_statement (parser, NULL, false);
5973 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5974 a statement all its own. */
5975 else if (token->type == CPP_PRAGMA)
5977 cp_lexer_handle_pragma (parser->lexer);
5978 return;
5981 /* Everything else must be a declaration-statement or an
5982 expression-statement. Try for the declaration-statement
5983 first, unless we are looking at a `;', in which case we know that
5984 we have an expression-statement. */
5985 if (!statement)
5987 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5989 cp_parser_parse_tentatively (parser);
5990 /* Try to parse the declaration-statement. */
5991 cp_parser_declaration_statement (parser);
5992 /* If that worked, we're done. */
5993 if (cp_parser_parse_definitely (parser))
5994 return;
5996 /* Look for an expression-statement instead. */
5997 statement = cp_parser_expression_statement (parser, in_statement_expr);
6000 /* Set the line number for the statement. */
6001 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6002 SET_EXPR_LOCATION (statement, statement_location);
6005 /* Parse a labeled-statement.
6007 labeled-statement:
6008 identifier : statement
6009 case constant-expression : statement
6010 default : statement
6012 GNU Extension:
6014 labeled-statement:
6015 case constant-expression ... constant-expression : statement
6017 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6018 For an ordinary label, returns a LABEL_EXPR. */
6020 static tree
6021 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6023 cp_token *token;
6024 tree statement = error_mark_node;
6026 /* The next token should be an identifier. */
6027 token = cp_lexer_peek_token (parser->lexer);
6028 if (token->type != CPP_NAME
6029 && token->type != CPP_KEYWORD)
6031 cp_parser_error (parser, "expected labeled-statement");
6032 return error_mark_node;
6035 switch (token->keyword)
6037 case RID_CASE:
6039 tree expr, expr_hi;
6040 cp_token *ellipsis;
6042 /* Consume the `case' token. */
6043 cp_lexer_consume_token (parser->lexer);
6044 /* Parse the constant-expression. */
6045 expr = cp_parser_constant_expression (parser,
6046 /*allow_non_constant_p=*/false,
6047 NULL);
6049 ellipsis = cp_lexer_peek_token (parser->lexer);
6050 if (ellipsis->type == CPP_ELLIPSIS)
6052 /* Consume the `...' token. */
6053 cp_lexer_consume_token (parser->lexer);
6054 expr_hi =
6055 cp_parser_constant_expression (parser,
6056 /*allow_non_constant_p=*/false,
6057 NULL);
6058 /* We don't need to emit warnings here, as the common code
6059 will do this for us. */
6061 else
6062 expr_hi = NULL_TREE;
6064 if (!parser->in_switch_statement_p)
6065 error ("case label %qE not within a switch statement", expr);
6066 else
6067 statement = finish_case_label (expr, expr_hi);
6069 break;
6071 case RID_DEFAULT:
6072 /* Consume the `default' token. */
6073 cp_lexer_consume_token (parser->lexer);
6074 if (!parser->in_switch_statement_p)
6075 error ("case label not within a switch statement");
6076 else
6077 statement = finish_case_label (NULL_TREE, NULL_TREE);
6078 break;
6080 default:
6081 /* Anything else must be an ordinary label. */
6082 statement = finish_label_stmt (cp_parser_identifier (parser));
6083 break;
6086 /* Require the `:' token. */
6087 cp_parser_require (parser, CPP_COLON, "`:'");
6088 /* Parse the labeled statement. */
6089 cp_parser_statement (parser, in_statement_expr);
6091 /* Return the label, in the case of a `case' or `default' label. */
6092 return statement;
6095 /* Parse an expression-statement.
6097 expression-statement:
6098 expression [opt] ;
6100 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6101 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6102 indicates whether this expression-statement is part of an
6103 expression statement. */
6105 static tree
6106 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6108 tree statement = NULL_TREE;
6110 /* If the next token is a ';', then there is no expression
6111 statement. */
6112 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6113 statement = cp_parser_expression (parser, /*cast_p=*/false);
6115 /* Consume the final `;'. */
6116 cp_parser_consume_semicolon_at_end_of_statement (parser);
6118 if (in_statement_expr
6119 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6120 /* This is the final expression statement of a statement
6121 expression. */
6122 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6123 else if (statement)
6124 statement = finish_expr_stmt (statement);
6125 else
6126 finish_stmt ();
6128 return statement;
6131 /* Parse a compound-statement.
6133 compound-statement:
6134 { statement-seq [opt] }
6136 Returns a tree representing the statement. */
6138 static tree
6139 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6140 bool in_try)
6142 tree compound_stmt;
6144 /* Consume the `{'. */
6145 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6146 return error_mark_node;
6147 /* Begin the compound-statement. */
6148 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6149 /* Parse an (optional) statement-seq. */
6150 cp_parser_statement_seq_opt (parser, in_statement_expr);
6151 /* Finish the compound-statement. */
6152 finish_compound_stmt (compound_stmt);
6153 /* Consume the `}'. */
6154 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6156 return compound_stmt;
6159 /* Parse an (optional) statement-seq.
6161 statement-seq:
6162 statement
6163 statement-seq [opt] statement */
6165 static void
6166 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6168 /* Scan statements until there aren't any more. */
6169 while (true)
6171 /* If we're looking at a `}', then we've run out of statements. */
6172 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6173 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6174 break;
6176 /* Parse the statement. */
6177 cp_parser_statement (parser, in_statement_expr);
6181 /* Parse a selection-statement.
6183 selection-statement:
6184 if ( condition ) statement
6185 if ( condition ) statement else statement
6186 switch ( condition ) statement
6188 Returns the new IF_STMT or SWITCH_STMT. */
6190 static tree
6191 cp_parser_selection_statement (cp_parser* parser)
6193 cp_token *token;
6194 enum rid keyword;
6196 /* Peek at the next token. */
6197 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6199 /* See what kind of keyword it is. */
6200 keyword = token->keyword;
6201 switch (keyword)
6203 case RID_IF:
6204 case RID_SWITCH:
6206 tree statement;
6207 tree condition;
6209 /* Look for the `('. */
6210 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6212 cp_parser_skip_to_end_of_statement (parser);
6213 return error_mark_node;
6216 /* Begin the selection-statement. */
6217 if (keyword == RID_IF)
6218 statement = begin_if_stmt ();
6219 else
6220 statement = begin_switch_stmt ();
6222 /* Parse the condition. */
6223 condition = cp_parser_condition (parser);
6224 /* Look for the `)'. */
6225 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6226 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6227 /*consume_paren=*/true);
6229 if (keyword == RID_IF)
6231 /* Add the condition. */
6232 finish_if_stmt_cond (condition, statement);
6234 /* Parse the then-clause. */
6235 cp_parser_implicitly_scoped_statement (parser);
6236 finish_then_clause (statement);
6238 /* If the next token is `else', parse the else-clause. */
6239 if (cp_lexer_next_token_is_keyword (parser->lexer,
6240 RID_ELSE))
6242 /* Consume the `else' keyword. */
6243 cp_lexer_consume_token (parser->lexer);
6244 begin_else_clause (statement);
6245 /* Parse the else-clause. */
6246 cp_parser_implicitly_scoped_statement (parser);
6247 finish_else_clause (statement);
6250 /* Now we're all done with the if-statement. */
6251 finish_if_stmt (statement);
6253 else
6255 bool in_switch_statement_p;
6257 /* Add the condition. */
6258 finish_switch_cond (condition, statement);
6260 /* Parse the body of the switch-statement. */
6261 in_switch_statement_p = parser->in_switch_statement_p;
6262 parser->in_switch_statement_p = true;
6263 cp_parser_implicitly_scoped_statement (parser);
6264 parser->in_switch_statement_p = in_switch_statement_p;
6266 /* Now we're all done with the switch-statement. */
6267 finish_switch_stmt (statement);
6270 return statement;
6272 break;
6274 default:
6275 cp_parser_error (parser, "expected selection-statement");
6276 return error_mark_node;
6280 /* Parse a condition.
6282 condition:
6283 expression
6284 type-specifier-seq declarator = assignment-expression
6286 GNU Extension:
6288 condition:
6289 type-specifier-seq declarator asm-specification [opt]
6290 attributes [opt] = assignment-expression
6292 Returns the expression that should be tested. */
6294 static tree
6295 cp_parser_condition (cp_parser* parser)
6297 cp_decl_specifier_seq type_specifiers;
6298 const char *saved_message;
6300 /* Try the declaration first. */
6301 cp_parser_parse_tentatively (parser);
6302 /* New types are not allowed in the type-specifier-seq for a
6303 condition. */
6304 saved_message = parser->type_definition_forbidden_message;
6305 parser->type_definition_forbidden_message
6306 = "types may not be defined in conditions";
6307 /* Parse the type-specifier-seq. */
6308 cp_parser_type_specifier_seq (parser, &type_specifiers);
6309 /* Restore the saved message. */
6310 parser->type_definition_forbidden_message = saved_message;
6311 /* If all is well, we might be looking at a declaration. */
6312 if (!cp_parser_error_occurred (parser))
6314 tree decl;
6315 tree asm_specification;
6316 tree attributes;
6317 cp_declarator *declarator;
6318 tree initializer = NULL_TREE;
6320 /* Parse the declarator. */
6321 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6322 /*ctor_dtor_or_conv_p=*/NULL,
6323 /*parenthesized_p=*/NULL,
6324 /*member_p=*/false);
6325 /* Parse the attributes. */
6326 attributes = cp_parser_attributes_opt (parser);
6327 /* Parse the asm-specification. */
6328 asm_specification = cp_parser_asm_specification_opt (parser);
6329 /* If the next token is not an `=', then we might still be
6330 looking at an expression. For example:
6332 if (A(a).x)
6334 looks like a decl-specifier-seq and a declarator -- but then
6335 there is no `=', so this is an expression. */
6336 cp_parser_require (parser, CPP_EQ, "`='");
6337 /* If we did see an `=', then we are looking at a declaration
6338 for sure. */
6339 if (cp_parser_parse_definitely (parser))
6341 tree pushed_scope;
6343 /* Create the declaration. */
6344 decl = start_decl (declarator, &type_specifiers,
6345 /*initialized_p=*/true,
6346 attributes, /*prefix_attributes=*/NULL_TREE,
6347 &pushed_scope);
6348 /* Parse the assignment-expression. */
6349 initializer = cp_parser_assignment_expression (parser,
6350 /*cast_p=*/false);
6352 /* Process the initializer. */
6353 cp_finish_decl (decl,
6354 initializer,
6355 asm_specification,
6356 LOOKUP_ONLYCONVERTING);
6358 if (pushed_scope)
6359 pop_scope (pushed_scope);
6361 return convert_from_reference (decl);
6364 /* If we didn't even get past the declarator successfully, we are
6365 definitely not looking at a declaration. */
6366 else
6367 cp_parser_abort_tentative_parse (parser);
6369 /* Otherwise, we are looking at an expression. */
6370 return cp_parser_expression (parser, /*cast_p=*/false);
6373 /* Parse an iteration-statement.
6375 iteration-statement:
6376 while ( condition ) statement
6377 do statement while ( expression ) ;
6378 for ( for-init-statement condition [opt] ; expression [opt] )
6379 statement
6381 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6383 static tree
6384 cp_parser_iteration_statement (cp_parser* parser)
6386 cp_token *token;
6387 enum rid keyword;
6388 tree statement;
6389 bool in_iteration_statement_p;
6392 /* Peek at the next token. */
6393 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6394 if (!token)
6395 return error_mark_node;
6397 /* Remember whether or not we are already within an iteration
6398 statement. */
6399 in_iteration_statement_p = parser->in_iteration_statement_p;
6401 /* See what kind of keyword it is. */
6402 keyword = token->keyword;
6403 switch (keyword)
6405 case RID_WHILE:
6407 tree condition;
6409 /* Begin the while-statement. */
6410 statement = begin_while_stmt ();
6411 /* Look for the `('. */
6412 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6413 /* Parse the condition. */
6414 condition = cp_parser_condition (parser);
6415 finish_while_stmt_cond (condition, statement);
6416 /* Look for the `)'. */
6417 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6418 /* Parse the dependent statement. */
6419 parser->in_iteration_statement_p = true;
6420 cp_parser_already_scoped_statement (parser);
6421 parser->in_iteration_statement_p = in_iteration_statement_p;
6422 /* We're done with the while-statement. */
6423 finish_while_stmt (statement);
6425 break;
6427 case RID_DO:
6429 tree expression;
6431 /* Begin the do-statement. */
6432 statement = begin_do_stmt ();
6433 /* Parse the body of the do-statement. */
6434 parser->in_iteration_statement_p = true;
6435 cp_parser_implicitly_scoped_statement (parser);
6436 parser->in_iteration_statement_p = in_iteration_statement_p;
6437 finish_do_body (statement);
6438 /* Look for the `while' keyword. */
6439 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6440 /* Look for the `('. */
6441 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6442 /* Parse the expression. */
6443 expression = cp_parser_expression (parser, /*cast_p=*/false);
6444 /* We're done with the do-statement. */
6445 finish_do_stmt (expression, statement);
6446 /* Look for the `)'. */
6447 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6448 /* Look for the `;'. */
6449 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6451 break;
6453 case RID_FOR:
6455 tree condition = NULL_TREE;
6456 tree expression = NULL_TREE;
6458 /* Begin the for-statement. */
6459 statement = begin_for_stmt ();
6460 /* Look for the `('. */
6461 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6462 /* Parse the initialization. */
6463 cp_parser_for_init_statement (parser);
6464 finish_for_init_stmt (statement);
6466 /* If there's a condition, process it. */
6467 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6468 condition = cp_parser_condition (parser);
6469 finish_for_cond (condition, statement);
6470 /* Look for the `;'. */
6471 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6473 /* If there's an expression, process it. */
6474 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6475 expression = cp_parser_expression (parser, /*cast_p=*/false);
6476 finish_for_expr (expression, statement);
6477 /* Look for the `)'. */
6478 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6480 /* Parse the body of the for-statement. */
6481 parser->in_iteration_statement_p = true;
6482 cp_parser_already_scoped_statement (parser);
6483 parser->in_iteration_statement_p = in_iteration_statement_p;
6485 /* We're done with the for-statement. */
6486 finish_for_stmt (statement);
6488 break;
6490 default:
6491 cp_parser_error (parser, "expected iteration-statement");
6492 statement = error_mark_node;
6493 break;
6496 return statement;
6499 /* Parse a for-init-statement.
6501 for-init-statement:
6502 expression-statement
6503 simple-declaration */
6505 static void
6506 cp_parser_for_init_statement (cp_parser* parser)
6508 /* If the next token is a `;', then we have an empty
6509 expression-statement. Grammatically, this is also a
6510 simple-declaration, but an invalid one, because it does not
6511 declare anything. Therefore, if we did not handle this case
6512 specially, we would issue an error message about an invalid
6513 declaration. */
6514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6516 /* We're going to speculatively look for a declaration, falling back
6517 to an expression, if necessary. */
6518 cp_parser_parse_tentatively (parser);
6519 /* Parse the declaration. */
6520 cp_parser_simple_declaration (parser,
6521 /*function_definition_allowed_p=*/false);
6522 /* If the tentative parse failed, then we shall need to look for an
6523 expression-statement. */
6524 if (cp_parser_parse_definitely (parser))
6525 return;
6528 cp_parser_expression_statement (parser, false);
6531 /* Parse a jump-statement.
6533 jump-statement:
6534 break ;
6535 continue ;
6536 return expression [opt] ;
6537 goto identifier ;
6539 GNU extension:
6541 jump-statement:
6542 goto * expression ;
6544 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6546 static tree
6547 cp_parser_jump_statement (cp_parser* parser)
6549 tree statement = error_mark_node;
6550 cp_token *token;
6551 enum rid keyword;
6553 /* Peek at the next token. */
6554 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6555 if (!token)
6556 return error_mark_node;
6558 /* See what kind of keyword it is. */
6559 keyword = token->keyword;
6560 switch (keyword)
6562 case RID_BREAK:
6563 if (!parser->in_switch_statement_p
6564 && !parser->in_iteration_statement_p)
6566 error ("break statement not within loop or switch");
6567 statement = error_mark_node;
6569 else
6570 statement = finish_break_stmt ();
6571 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6572 break;
6574 case RID_CONTINUE:
6575 if (!parser->in_iteration_statement_p)
6577 error ("continue statement not within a loop");
6578 statement = error_mark_node;
6580 else
6581 statement = finish_continue_stmt ();
6582 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6583 break;
6585 case RID_RETURN:
6587 tree expr;
6589 /* If the next token is a `;', then there is no
6590 expression. */
6591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6592 expr = cp_parser_expression (parser, /*cast_p=*/false);
6593 else
6594 expr = NULL_TREE;
6595 /* Build the return-statement. */
6596 statement = finish_return_stmt (expr);
6597 /* Look for the final `;'. */
6598 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6600 break;
6602 case RID_GOTO:
6603 /* Create the goto-statement. */
6604 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6606 /* Issue a warning about this use of a GNU extension. */
6607 if (pedantic)
6608 pedwarn ("ISO C++ forbids computed gotos");
6609 /* Consume the '*' token. */
6610 cp_lexer_consume_token (parser->lexer);
6611 /* Parse the dependent expression. */
6612 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6614 else
6615 finish_goto_stmt (cp_parser_identifier (parser));
6616 /* Look for the final `;'. */
6617 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6618 break;
6620 default:
6621 cp_parser_error (parser, "expected jump-statement");
6622 break;
6625 return statement;
6628 /* Parse a declaration-statement.
6630 declaration-statement:
6631 block-declaration */
6633 static void
6634 cp_parser_declaration_statement (cp_parser* parser)
6636 void *p;
6638 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6639 p = obstack_alloc (&declarator_obstack, 0);
6641 /* Parse the block-declaration. */
6642 cp_parser_block_declaration (parser, /*statement_p=*/true);
6644 /* Free any declarators allocated. */
6645 obstack_free (&declarator_obstack, p);
6647 /* Finish off the statement. */
6648 finish_stmt ();
6651 /* Some dependent statements (like `if (cond) statement'), are
6652 implicitly in their own scope. In other words, if the statement is
6653 a single statement (as opposed to a compound-statement), it is
6654 none-the-less treated as if it were enclosed in braces. Any
6655 declarations appearing in the dependent statement are out of scope
6656 after control passes that point. This function parses a statement,
6657 but ensures that is in its own scope, even if it is not a
6658 compound-statement.
6660 Returns the new statement. */
6662 static tree
6663 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6665 tree statement;
6667 /* If the token is not a `{', then we must take special action. */
6668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6670 /* Create a compound-statement. */
6671 statement = begin_compound_stmt (0);
6672 /* Parse the dependent-statement. */
6673 cp_parser_statement (parser, false);
6674 /* Finish the dummy compound-statement. */
6675 finish_compound_stmt (statement);
6677 /* Otherwise, we simply parse the statement directly. */
6678 else
6679 statement = cp_parser_compound_statement (parser, NULL, false);
6681 /* Return the statement. */
6682 return statement;
6685 /* For some dependent statements (like `while (cond) statement'), we
6686 have already created a scope. Therefore, even if the dependent
6687 statement is a compound-statement, we do not want to create another
6688 scope. */
6690 static void
6691 cp_parser_already_scoped_statement (cp_parser* parser)
6693 /* If the token is a `{', then we must take special action. */
6694 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6695 cp_parser_statement (parser, false);
6696 else
6698 /* Avoid calling cp_parser_compound_statement, so that we
6699 don't create a new scope. Do everything else by hand. */
6700 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6701 cp_parser_statement_seq_opt (parser, false);
6702 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6706 /* Declarations [gram.dcl.dcl] */
6708 /* Parse an optional declaration-sequence.
6710 declaration-seq:
6711 declaration
6712 declaration-seq declaration */
6714 static void
6715 cp_parser_declaration_seq_opt (cp_parser* parser)
6717 while (true)
6719 cp_token *token;
6721 token = cp_lexer_peek_token (parser->lexer);
6723 if (token->type == CPP_CLOSE_BRACE
6724 || token->type == CPP_EOF)
6725 break;
6727 if (token->type == CPP_SEMICOLON)
6729 /* A declaration consisting of a single semicolon is
6730 invalid. Allow it unless we're being pedantic. */
6731 cp_lexer_consume_token (parser->lexer);
6732 if (pedantic && !in_system_header)
6733 pedwarn ("extra %<;%>");
6734 continue;
6737 /* If we're entering or exiting a region that's implicitly
6738 extern "C", modify the lang context appropriately. */
6739 if (!parser->implicit_extern_c && token->implicit_extern_c)
6741 push_lang_context (lang_name_c);
6742 parser->implicit_extern_c = true;
6744 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6746 pop_lang_context ();
6747 parser->implicit_extern_c = false;
6750 if (token->type == CPP_PRAGMA)
6752 /* A top-level declaration can consist solely of a #pragma.
6753 A nested declaration cannot, so this is done here and not
6754 in cp_parser_declaration. (A #pragma at block scope is
6755 handled in cp_parser_statement.) */
6756 cp_lexer_handle_pragma (parser->lexer);
6757 continue;
6760 /* Parse the declaration itself. */
6761 cp_parser_declaration (parser);
6765 /* Parse a declaration.
6767 declaration:
6768 block-declaration
6769 function-definition
6770 template-declaration
6771 explicit-instantiation
6772 explicit-specialization
6773 linkage-specification
6774 namespace-definition
6776 GNU extension:
6778 declaration:
6779 __extension__ declaration */
6781 static void
6782 cp_parser_declaration (cp_parser* parser)
6784 cp_token token1;
6785 cp_token token2;
6786 int saved_pedantic;
6787 void *p;
6789 /* Check for the `__extension__' keyword. */
6790 if (cp_parser_extension_opt (parser, &saved_pedantic))
6792 /* Parse the qualified declaration. */
6793 cp_parser_declaration (parser);
6794 /* Restore the PEDANTIC flag. */
6795 pedantic = saved_pedantic;
6797 return;
6800 /* Try to figure out what kind of declaration is present. */
6801 token1 = *cp_lexer_peek_token (parser->lexer);
6803 if (token1.type != CPP_EOF)
6804 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6806 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6807 p = obstack_alloc (&declarator_obstack, 0);
6809 /* If the next token is `extern' and the following token is a string
6810 literal, then we have a linkage specification. */
6811 if (token1.keyword == RID_EXTERN
6812 && cp_parser_is_string_literal (&token2))
6813 cp_parser_linkage_specification (parser);
6814 /* If the next token is `template', then we have either a template
6815 declaration, an explicit instantiation, or an explicit
6816 specialization. */
6817 else if (token1.keyword == RID_TEMPLATE)
6819 /* `template <>' indicates a template specialization. */
6820 if (token2.type == CPP_LESS
6821 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6822 cp_parser_explicit_specialization (parser);
6823 /* `template <' indicates a template declaration. */
6824 else if (token2.type == CPP_LESS)
6825 cp_parser_template_declaration (parser, /*member_p=*/false);
6826 /* Anything else must be an explicit instantiation. */
6827 else
6828 cp_parser_explicit_instantiation (parser);
6830 /* If the next token is `export', then we have a template
6831 declaration. */
6832 else if (token1.keyword == RID_EXPORT)
6833 cp_parser_template_declaration (parser, /*member_p=*/false);
6834 /* If the next token is `extern', 'static' or 'inline' and the one
6835 after that is `template', we have a GNU extended explicit
6836 instantiation directive. */
6837 else if (cp_parser_allow_gnu_extensions_p (parser)
6838 && (token1.keyword == RID_EXTERN
6839 || token1.keyword == RID_STATIC
6840 || token1.keyword == RID_INLINE)
6841 && token2.keyword == RID_TEMPLATE)
6842 cp_parser_explicit_instantiation (parser);
6843 /* If the next token is `namespace', check for a named or unnamed
6844 namespace definition. */
6845 else if (token1.keyword == RID_NAMESPACE
6846 && (/* A named namespace definition. */
6847 (token2.type == CPP_NAME
6848 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6849 == CPP_OPEN_BRACE))
6850 /* An unnamed namespace definition. */
6851 || token2.type == CPP_OPEN_BRACE))
6852 cp_parser_namespace_definition (parser);
6853 /* We must have either a block declaration or a function
6854 definition. */
6855 else
6856 /* Try to parse a block-declaration, or a function-definition. */
6857 cp_parser_block_declaration (parser, /*statement_p=*/false);
6859 /* Free any declarators allocated. */
6860 obstack_free (&declarator_obstack, p);
6863 /* Parse a block-declaration.
6865 block-declaration:
6866 simple-declaration
6867 asm-definition
6868 namespace-alias-definition
6869 using-declaration
6870 using-directive
6872 GNU Extension:
6874 block-declaration:
6875 __extension__ block-declaration
6876 label-declaration
6878 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6879 part of a declaration-statement. */
6881 static void
6882 cp_parser_block_declaration (cp_parser *parser,
6883 bool statement_p)
6885 cp_token *token1;
6886 int saved_pedantic;
6888 /* Check for the `__extension__' keyword. */
6889 if (cp_parser_extension_opt (parser, &saved_pedantic))
6891 /* Parse the qualified declaration. */
6892 cp_parser_block_declaration (parser, statement_p);
6893 /* Restore the PEDANTIC flag. */
6894 pedantic = saved_pedantic;
6896 return;
6899 /* Peek at the next token to figure out which kind of declaration is
6900 present. */
6901 token1 = cp_lexer_peek_token (parser->lexer);
6903 /* If the next keyword is `asm', we have an asm-definition. */
6904 if (token1->keyword == RID_ASM)
6906 if (statement_p)
6907 cp_parser_commit_to_tentative_parse (parser);
6908 cp_parser_asm_definition (parser);
6910 /* If the next keyword is `namespace', we have a
6911 namespace-alias-definition. */
6912 else if (token1->keyword == RID_NAMESPACE)
6913 cp_parser_namespace_alias_definition (parser);
6914 /* If the next keyword is `using', we have either a
6915 using-declaration or a using-directive. */
6916 else if (token1->keyword == RID_USING)
6918 cp_token *token2;
6920 if (statement_p)
6921 cp_parser_commit_to_tentative_parse (parser);
6922 /* If the token after `using' is `namespace', then we have a
6923 using-directive. */
6924 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6925 if (token2->keyword == RID_NAMESPACE)
6926 cp_parser_using_directive (parser);
6927 /* Otherwise, it's a using-declaration. */
6928 else
6929 cp_parser_using_declaration (parser);
6931 /* If the next keyword is `__label__' we have a label declaration. */
6932 else if (token1->keyword == RID_LABEL)
6934 if (statement_p)
6935 cp_parser_commit_to_tentative_parse (parser);
6936 cp_parser_label_declaration (parser);
6938 /* Anything else must be a simple-declaration. */
6939 else
6940 cp_parser_simple_declaration (parser, !statement_p);
6943 /* Parse a simple-declaration.
6945 simple-declaration:
6946 decl-specifier-seq [opt] init-declarator-list [opt] ;
6948 init-declarator-list:
6949 init-declarator
6950 init-declarator-list , init-declarator
6952 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6953 function-definition as a simple-declaration. */
6955 static void
6956 cp_parser_simple_declaration (cp_parser* parser,
6957 bool function_definition_allowed_p)
6959 cp_decl_specifier_seq decl_specifiers;
6960 int declares_class_or_enum;
6961 bool saw_declarator;
6963 /* Defer access checks until we know what is being declared; the
6964 checks for names appearing in the decl-specifier-seq should be
6965 done as if we were in the scope of the thing being declared. */
6966 push_deferring_access_checks (dk_deferred);
6968 /* Parse the decl-specifier-seq. We have to keep track of whether
6969 or not the decl-specifier-seq declares a named class or
6970 enumeration type, since that is the only case in which the
6971 init-declarator-list is allowed to be empty.
6973 [dcl.dcl]
6975 In a simple-declaration, the optional init-declarator-list can be
6976 omitted only when declaring a class or enumeration, that is when
6977 the decl-specifier-seq contains either a class-specifier, an
6978 elaborated-type-specifier, or an enum-specifier. */
6979 cp_parser_decl_specifier_seq (parser,
6980 CP_PARSER_FLAGS_OPTIONAL,
6981 &decl_specifiers,
6982 &declares_class_or_enum);
6983 /* We no longer need to defer access checks. */
6984 stop_deferring_access_checks ();
6986 /* In a block scope, a valid declaration must always have a
6987 decl-specifier-seq. By not trying to parse declarators, we can
6988 resolve the declaration/expression ambiguity more quickly. */
6989 if (!function_definition_allowed_p
6990 && !decl_specifiers.any_specifiers_p)
6992 cp_parser_error (parser, "expected declaration");
6993 goto done;
6996 /* If the next two tokens are both identifiers, the code is
6997 erroneous. The usual cause of this situation is code like:
6999 T t;
7001 where "T" should name a type -- but does not. */
7002 if (!decl_specifiers.type
7003 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7005 /* If parsing tentatively, we should commit; we really are
7006 looking at a declaration. */
7007 cp_parser_commit_to_tentative_parse (parser);
7008 /* Give up. */
7009 goto done;
7012 /* If we have seen at least one decl-specifier, and the next token
7013 is not a parenthesis, then we must be looking at a declaration.
7014 (After "int (" we might be looking at a functional cast.) */
7015 if (decl_specifiers.any_specifiers_p
7016 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7017 cp_parser_commit_to_tentative_parse (parser);
7019 /* Keep going until we hit the `;' at the end of the simple
7020 declaration. */
7021 saw_declarator = false;
7022 while (cp_lexer_next_token_is_not (parser->lexer,
7023 CPP_SEMICOLON))
7025 cp_token *token;
7026 bool function_definition_p;
7027 tree decl;
7029 saw_declarator = true;
7030 /* Parse the init-declarator. */
7031 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7032 function_definition_allowed_p,
7033 /*member_p=*/false,
7034 declares_class_or_enum,
7035 &function_definition_p);
7036 /* If an error occurred while parsing tentatively, exit quickly.
7037 (That usually happens when in the body of a function; each
7038 statement is treated as a declaration-statement until proven
7039 otherwise.) */
7040 if (cp_parser_error_occurred (parser))
7041 goto done;
7042 /* Handle function definitions specially. */
7043 if (function_definition_p)
7045 /* If the next token is a `,', then we are probably
7046 processing something like:
7048 void f() {}, *p;
7050 which is erroneous. */
7051 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7052 error ("mixing declarations and function-definitions is forbidden");
7053 /* Otherwise, we're done with the list of declarators. */
7054 else
7056 pop_deferring_access_checks ();
7057 return;
7060 /* The next token should be either a `,' or a `;'. */
7061 token = cp_lexer_peek_token (parser->lexer);
7062 /* If it's a `,', there are more declarators to come. */
7063 if (token->type == CPP_COMMA)
7064 cp_lexer_consume_token (parser->lexer);
7065 /* If it's a `;', we are done. */
7066 else if (token->type == CPP_SEMICOLON)
7067 break;
7068 /* Anything else is an error. */
7069 else
7071 /* If we have already issued an error message we don't need
7072 to issue another one. */
7073 if (decl != error_mark_node
7074 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7075 cp_parser_error (parser, "expected %<,%> or %<;%>");
7076 /* Skip tokens until we reach the end of the statement. */
7077 cp_parser_skip_to_end_of_statement (parser);
7078 /* If the next token is now a `;', consume it. */
7079 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7080 cp_lexer_consume_token (parser->lexer);
7081 goto done;
7083 /* After the first time around, a function-definition is not
7084 allowed -- even if it was OK at first. For example:
7086 int i, f() {}
7088 is not valid. */
7089 function_definition_allowed_p = false;
7092 /* Issue an error message if no declarators are present, and the
7093 decl-specifier-seq does not itself declare a class or
7094 enumeration. */
7095 if (!saw_declarator)
7097 if (cp_parser_declares_only_class_p (parser))
7098 shadow_tag (&decl_specifiers);
7099 /* Perform any deferred access checks. */
7100 perform_deferred_access_checks ();
7103 /* Consume the `;'. */
7104 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7106 done:
7107 pop_deferring_access_checks ();
7110 /* Parse a decl-specifier-seq.
7112 decl-specifier-seq:
7113 decl-specifier-seq [opt] decl-specifier
7115 decl-specifier:
7116 storage-class-specifier
7117 type-specifier
7118 function-specifier
7119 friend
7120 typedef
7122 GNU Extension:
7124 decl-specifier:
7125 attributes
7127 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7129 The parser flags FLAGS is used to control type-specifier parsing.
7131 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7132 flags:
7134 1: one of the decl-specifiers is an elaborated-type-specifier
7135 (i.e., a type declaration)
7136 2: one of the decl-specifiers is an enum-specifier or a
7137 class-specifier (i.e., a type definition)
7141 static void
7142 cp_parser_decl_specifier_seq (cp_parser* parser,
7143 cp_parser_flags flags,
7144 cp_decl_specifier_seq *decl_specs,
7145 int* declares_class_or_enum)
7147 bool constructor_possible_p = !parser->in_declarator_p;
7149 /* Clear DECL_SPECS. */
7150 clear_decl_specs (decl_specs);
7152 /* Assume no class or enumeration type is declared. */
7153 *declares_class_or_enum = 0;
7155 /* Keep reading specifiers until there are no more to read. */
7156 while (true)
7158 bool constructor_p;
7159 bool found_decl_spec;
7160 cp_token *token;
7162 /* Peek at the next token. */
7163 token = cp_lexer_peek_token (parser->lexer);
7164 /* Handle attributes. */
7165 if (token->keyword == RID_ATTRIBUTE)
7167 /* Parse the attributes. */
7168 decl_specs->attributes
7169 = chainon (decl_specs->attributes,
7170 cp_parser_attributes_opt (parser));
7171 continue;
7173 /* Assume we will find a decl-specifier keyword. */
7174 found_decl_spec = true;
7175 /* If the next token is an appropriate keyword, we can simply
7176 add it to the list. */
7177 switch (token->keyword)
7179 /* decl-specifier:
7180 friend */
7181 case RID_FRIEND:
7182 if (decl_specs->specs[(int) ds_friend]++)
7183 error ("duplicate %<friend%>");
7184 /* Consume the token. */
7185 cp_lexer_consume_token (parser->lexer);
7186 break;
7188 /* function-specifier:
7189 inline
7190 virtual
7191 explicit */
7192 case RID_INLINE:
7193 case RID_VIRTUAL:
7194 case RID_EXPLICIT:
7195 cp_parser_function_specifier_opt (parser, decl_specs);
7196 break;
7198 /* decl-specifier:
7199 typedef */
7200 case RID_TYPEDEF:
7201 ++decl_specs->specs[(int) ds_typedef];
7202 /* Consume the token. */
7203 cp_lexer_consume_token (parser->lexer);
7204 /* A constructor declarator cannot appear in a typedef. */
7205 constructor_possible_p = false;
7206 /* The "typedef" keyword can only occur in a declaration; we
7207 may as well commit at this point. */
7208 cp_parser_commit_to_tentative_parse (parser);
7209 break;
7211 /* storage-class-specifier:
7212 auto
7213 register
7214 static
7215 extern
7216 mutable
7218 GNU Extension:
7219 thread */
7220 case RID_AUTO:
7221 /* Consume the token. */
7222 cp_lexer_consume_token (parser->lexer);
7223 cp_parser_set_storage_class (decl_specs, sc_auto);
7224 break;
7225 case RID_REGISTER:
7226 /* Consume the token. */
7227 cp_lexer_consume_token (parser->lexer);
7228 cp_parser_set_storage_class (decl_specs, sc_register);
7229 break;
7230 case RID_STATIC:
7231 /* Consume the token. */
7232 cp_lexer_consume_token (parser->lexer);
7233 if (decl_specs->specs[(int) ds_thread])
7235 error ("%<__thread%> before %<static%>");
7236 decl_specs->specs[(int) ds_thread] = 0;
7238 cp_parser_set_storage_class (decl_specs, sc_static);
7239 break;
7240 case RID_EXTERN:
7241 /* Consume the token. */
7242 cp_lexer_consume_token (parser->lexer);
7243 if (decl_specs->specs[(int) ds_thread])
7245 error ("%<__thread%> before %<extern%>");
7246 decl_specs->specs[(int) ds_thread] = 0;
7248 cp_parser_set_storage_class (decl_specs, sc_extern);
7249 break;
7250 case RID_MUTABLE:
7251 /* Consume the token. */
7252 cp_lexer_consume_token (parser->lexer);
7253 cp_parser_set_storage_class (decl_specs, sc_mutable);
7254 break;
7255 case RID_THREAD:
7256 /* Consume the token. */
7257 cp_lexer_consume_token (parser->lexer);
7258 ++decl_specs->specs[(int) ds_thread];
7259 break;
7261 default:
7262 /* We did not yet find a decl-specifier yet. */
7263 found_decl_spec = false;
7264 break;
7267 /* Constructors are a special case. The `S' in `S()' is not a
7268 decl-specifier; it is the beginning of the declarator. */
7269 constructor_p
7270 = (!found_decl_spec
7271 && constructor_possible_p
7272 && (cp_parser_constructor_declarator_p
7273 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7275 /* If we don't have a DECL_SPEC yet, then we must be looking at
7276 a type-specifier. */
7277 if (!found_decl_spec && !constructor_p)
7279 int decl_spec_declares_class_or_enum;
7280 bool is_cv_qualifier;
7281 tree type_spec;
7283 type_spec
7284 = cp_parser_type_specifier (parser, flags,
7285 decl_specs,
7286 /*is_declaration=*/true,
7287 &decl_spec_declares_class_or_enum,
7288 &is_cv_qualifier);
7290 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7292 /* If this type-specifier referenced a user-defined type
7293 (a typedef, class-name, etc.), then we can't allow any
7294 more such type-specifiers henceforth.
7296 [dcl.spec]
7298 The longest sequence of decl-specifiers that could
7299 possibly be a type name is taken as the
7300 decl-specifier-seq of a declaration. The sequence shall
7301 be self-consistent as described below.
7303 [dcl.type]
7305 As a general rule, at most one type-specifier is allowed
7306 in the complete decl-specifier-seq of a declaration. The
7307 only exceptions are the following:
7309 -- const or volatile can be combined with any other
7310 type-specifier.
7312 -- signed or unsigned can be combined with char, long,
7313 short, or int.
7315 -- ..
7317 Example:
7319 typedef char* Pc;
7320 void g (const int Pc);
7322 Here, Pc is *not* part of the decl-specifier seq; it's
7323 the declarator. Therefore, once we see a type-specifier
7324 (other than a cv-qualifier), we forbid any additional
7325 user-defined types. We *do* still allow things like `int
7326 int' to be considered a decl-specifier-seq, and issue the
7327 error message later. */
7328 if (type_spec && !is_cv_qualifier)
7329 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7330 /* A constructor declarator cannot follow a type-specifier. */
7331 if (type_spec)
7333 constructor_possible_p = false;
7334 found_decl_spec = true;
7338 /* If we still do not have a DECL_SPEC, then there are no more
7339 decl-specifiers. */
7340 if (!found_decl_spec)
7341 break;
7343 decl_specs->any_specifiers_p = true;
7344 /* After we see one decl-specifier, further decl-specifiers are
7345 always optional. */
7346 flags |= CP_PARSER_FLAGS_OPTIONAL;
7349 /* Don't allow a friend specifier with a class definition. */
7350 if (decl_specs->specs[(int) ds_friend] != 0
7351 && (*declares_class_or_enum & 2))
7352 error ("class definition may not be declared a friend");
7355 /* Parse an (optional) storage-class-specifier.
7357 storage-class-specifier:
7358 auto
7359 register
7360 static
7361 extern
7362 mutable
7364 GNU Extension:
7366 storage-class-specifier:
7367 thread
7369 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7371 static tree
7372 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7374 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7376 case RID_AUTO:
7377 case RID_REGISTER:
7378 case RID_STATIC:
7379 case RID_EXTERN:
7380 case RID_MUTABLE:
7381 case RID_THREAD:
7382 /* Consume the token. */
7383 return cp_lexer_consume_token (parser->lexer)->value;
7385 default:
7386 return NULL_TREE;
7390 /* Parse an (optional) function-specifier.
7392 function-specifier:
7393 inline
7394 virtual
7395 explicit
7397 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7398 Updates DECL_SPECS, if it is non-NULL. */
7400 static tree
7401 cp_parser_function_specifier_opt (cp_parser* parser,
7402 cp_decl_specifier_seq *decl_specs)
7404 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7406 case RID_INLINE:
7407 if (decl_specs)
7408 ++decl_specs->specs[(int) ds_inline];
7409 break;
7411 case RID_VIRTUAL:
7412 if (decl_specs)
7413 ++decl_specs->specs[(int) ds_virtual];
7414 break;
7416 case RID_EXPLICIT:
7417 if (decl_specs)
7418 ++decl_specs->specs[(int) ds_explicit];
7419 break;
7421 default:
7422 return NULL_TREE;
7425 /* Consume the token. */
7426 return cp_lexer_consume_token (parser->lexer)->value;
7429 /* Parse a linkage-specification.
7431 linkage-specification:
7432 extern string-literal { declaration-seq [opt] }
7433 extern string-literal declaration */
7435 static void
7436 cp_parser_linkage_specification (cp_parser* parser)
7438 tree linkage;
7440 /* Look for the `extern' keyword. */
7441 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7443 /* Look for the string-literal. */
7444 linkage = cp_parser_string_literal (parser, false, false);
7446 /* Transform the literal into an identifier. If the literal is a
7447 wide-character string, or contains embedded NULs, then we can't
7448 handle it as the user wants. */
7449 if (strlen (TREE_STRING_POINTER (linkage))
7450 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7452 cp_parser_error (parser, "invalid linkage-specification");
7453 /* Assume C++ linkage. */
7454 linkage = lang_name_cplusplus;
7456 else
7457 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7459 /* We're now using the new linkage. */
7460 push_lang_context (linkage);
7462 /* If the next token is a `{', then we're using the first
7463 production. */
7464 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7466 /* Consume the `{' token. */
7467 cp_lexer_consume_token (parser->lexer);
7468 /* Parse the declarations. */
7469 cp_parser_declaration_seq_opt (parser);
7470 /* Look for the closing `}'. */
7471 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7473 /* Otherwise, there's just one declaration. */
7474 else
7476 bool saved_in_unbraced_linkage_specification_p;
7478 saved_in_unbraced_linkage_specification_p
7479 = parser->in_unbraced_linkage_specification_p;
7480 parser->in_unbraced_linkage_specification_p = true;
7481 have_extern_spec = true;
7482 cp_parser_declaration (parser);
7483 have_extern_spec = false;
7484 parser->in_unbraced_linkage_specification_p
7485 = saved_in_unbraced_linkage_specification_p;
7488 /* We're done with the linkage-specification. */
7489 pop_lang_context ();
7492 /* Special member functions [gram.special] */
7494 /* Parse a conversion-function-id.
7496 conversion-function-id:
7497 operator conversion-type-id
7499 Returns an IDENTIFIER_NODE representing the operator. */
7501 static tree
7502 cp_parser_conversion_function_id (cp_parser* parser)
7504 tree type;
7505 tree saved_scope;
7506 tree saved_qualifying_scope;
7507 tree saved_object_scope;
7508 tree pushed_scope = NULL_TREE;
7510 /* Look for the `operator' token. */
7511 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7512 return error_mark_node;
7513 /* When we parse the conversion-type-id, the current scope will be
7514 reset. However, we need that information in able to look up the
7515 conversion function later, so we save it here. */
7516 saved_scope = parser->scope;
7517 saved_qualifying_scope = parser->qualifying_scope;
7518 saved_object_scope = parser->object_scope;
7519 /* We must enter the scope of the class so that the names of
7520 entities declared within the class are available in the
7521 conversion-type-id. For example, consider:
7523 struct S {
7524 typedef int I;
7525 operator I();
7528 S::operator I() { ... }
7530 In order to see that `I' is a type-name in the definition, we
7531 must be in the scope of `S'. */
7532 if (saved_scope)
7533 pushed_scope = push_scope (saved_scope);
7534 /* Parse the conversion-type-id. */
7535 type = cp_parser_conversion_type_id (parser);
7536 /* Leave the scope of the class, if any. */
7537 if (pushed_scope)
7538 pop_scope (pushed_scope);
7539 /* Restore the saved scope. */
7540 parser->scope = saved_scope;
7541 parser->qualifying_scope = saved_qualifying_scope;
7542 parser->object_scope = saved_object_scope;
7543 /* If the TYPE is invalid, indicate failure. */
7544 if (type == error_mark_node)
7545 return error_mark_node;
7546 return mangle_conv_op_name_for_type (type);
7549 /* Parse a conversion-type-id:
7551 conversion-type-id:
7552 type-specifier-seq conversion-declarator [opt]
7554 Returns the TYPE specified. */
7556 static tree
7557 cp_parser_conversion_type_id (cp_parser* parser)
7559 tree attributes;
7560 cp_decl_specifier_seq type_specifiers;
7561 cp_declarator *declarator;
7562 tree type_specified;
7564 /* Parse the attributes. */
7565 attributes = cp_parser_attributes_opt (parser);
7566 /* Parse the type-specifiers. */
7567 cp_parser_type_specifier_seq (parser, &type_specifiers);
7568 /* If that didn't work, stop. */
7569 if (type_specifiers.type == error_mark_node)
7570 return error_mark_node;
7571 /* Parse the conversion-declarator. */
7572 declarator = cp_parser_conversion_declarator_opt (parser);
7574 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7575 /*initialized=*/0, &attributes);
7576 if (attributes)
7577 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7578 return type_specified;
7581 /* Parse an (optional) conversion-declarator.
7583 conversion-declarator:
7584 ptr-operator conversion-declarator [opt]
7588 static cp_declarator *
7589 cp_parser_conversion_declarator_opt (cp_parser* parser)
7591 enum tree_code code;
7592 tree class_type;
7593 cp_cv_quals cv_quals;
7595 /* We don't know if there's a ptr-operator next, or not. */
7596 cp_parser_parse_tentatively (parser);
7597 /* Try the ptr-operator. */
7598 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7599 /* If it worked, look for more conversion-declarators. */
7600 if (cp_parser_parse_definitely (parser))
7602 cp_declarator *declarator;
7604 /* Parse another optional declarator. */
7605 declarator = cp_parser_conversion_declarator_opt (parser);
7607 /* Create the representation of the declarator. */
7608 if (class_type)
7609 declarator = make_ptrmem_declarator (cv_quals, class_type,
7610 declarator);
7611 else if (code == INDIRECT_REF)
7612 declarator = make_pointer_declarator (cv_quals, declarator);
7613 else
7614 declarator = make_reference_declarator (cv_quals, declarator);
7616 return declarator;
7619 return NULL;
7622 /* Parse an (optional) ctor-initializer.
7624 ctor-initializer:
7625 : mem-initializer-list
7627 Returns TRUE iff the ctor-initializer was actually present. */
7629 static bool
7630 cp_parser_ctor_initializer_opt (cp_parser* parser)
7632 /* If the next token is not a `:', then there is no
7633 ctor-initializer. */
7634 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7636 /* Do default initialization of any bases and members. */
7637 if (DECL_CONSTRUCTOR_P (current_function_decl))
7638 finish_mem_initializers (NULL_TREE);
7640 return false;
7643 /* Consume the `:' token. */
7644 cp_lexer_consume_token (parser->lexer);
7645 /* And the mem-initializer-list. */
7646 cp_parser_mem_initializer_list (parser);
7648 return true;
7651 /* Parse a mem-initializer-list.
7653 mem-initializer-list:
7654 mem-initializer
7655 mem-initializer , mem-initializer-list */
7657 static void
7658 cp_parser_mem_initializer_list (cp_parser* parser)
7660 tree mem_initializer_list = NULL_TREE;
7662 /* Let the semantic analysis code know that we are starting the
7663 mem-initializer-list. */
7664 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7665 error ("only constructors take base initializers");
7667 /* Loop through the list. */
7668 while (true)
7670 tree mem_initializer;
7672 /* Parse the mem-initializer. */
7673 mem_initializer = cp_parser_mem_initializer (parser);
7674 /* Add it to the list, unless it was erroneous. */
7675 if (mem_initializer)
7677 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7678 mem_initializer_list = mem_initializer;
7680 /* If the next token is not a `,', we're done. */
7681 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7682 break;
7683 /* Consume the `,' token. */
7684 cp_lexer_consume_token (parser->lexer);
7687 /* Perform semantic analysis. */
7688 if (DECL_CONSTRUCTOR_P (current_function_decl))
7689 finish_mem_initializers (mem_initializer_list);
7692 /* Parse a mem-initializer.
7694 mem-initializer:
7695 mem-initializer-id ( expression-list [opt] )
7697 GNU extension:
7699 mem-initializer:
7700 ( expression-list [opt] )
7702 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7703 class) or FIELD_DECL (for a non-static data member) to initialize;
7704 the TREE_VALUE is the expression-list. */
7706 static tree
7707 cp_parser_mem_initializer (cp_parser* parser)
7709 tree mem_initializer_id;
7710 tree expression_list;
7711 tree member;
7713 /* Find out what is being initialized. */
7714 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7716 pedwarn ("anachronistic old-style base class initializer");
7717 mem_initializer_id = NULL_TREE;
7719 else
7720 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7721 member = expand_member_init (mem_initializer_id);
7722 if (member && !DECL_P (member))
7723 in_base_initializer = 1;
7725 expression_list
7726 = cp_parser_parenthesized_expression_list (parser, false,
7727 /*cast_p=*/false,
7728 /*non_constant_p=*/NULL);
7729 if (!expression_list)
7730 expression_list = void_type_node;
7732 in_base_initializer = 0;
7734 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7737 /* Parse a mem-initializer-id.
7739 mem-initializer-id:
7740 :: [opt] nested-name-specifier [opt] class-name
7741 identifier
7743 Returns a TYPE indicating the class to be initializer for the first
7744 production. Returns an IDENTIFIER_NODE indicating the data member
7745 to be initialized for the second production. */
7747 static tree
7748 cp_parser_mem_initializer_id (cp_parser* parser)
7750 bool global_scope_p;
7751 bool nested_name_specifier_p;
7752 bool template_p = false;
7753 tree id;
7755 /* `typename' is not allowed in this context ([temp.res]). */
7756 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7758 error ("keyword %<typename%> not allowed in this context (a qualified "
7759 "member initializer is implicitly a type)");
7760 cp_lexer_consume_token (parser->lexer);
7762 /* Look for the optional `::' operator. */
7763 global_scope_p
7764 = (cp_parser_global_scope_opt (parser,
7765 /*current_scope_valid_p=*/false)
7766 != NULL_TREE);
7767 /* Look for the optional nested-name-specifier. The simplest way to
7768 implement:
7770 [temp.res]
7772 The keyword `typename' is not permitted in a base-specifier or
7773 mem-initializer; in these contexts a qualified name that
7774 depends on a template-parameter is implicitly assumed to be a
7775 type name.
7777 is to assume that we have seen the `typename' keyword at this
7778 point. */
7779 nested_name_specifier_p
7780 = (cp_parser_nested_name_specifier_opt (parser,
7781 /*typename_keyword_p=*/true,
7782 /*check_dependency_p=*/true,
7783 /*type_p=*/true,
7784 /*is_declaration=*/true)
7785 != NULL_TREE);
7786 if (nested_name_specifier_p)
7787 template_p = cp_parser_optional_template_keyword (parser);
7788 /* If there is a `::' operator or a nested-name-specifier, then we
7789 are definitely looking for a class-name. */
7790 if (global_scope_p || nested_name_specifier_p)
7791 return cp_parser_class_name (parser,
7792 /*typename_keyword_p=*/true,
7793 /*template_keyword_p=*/template_p,
7794 none_type,
7795 /*check_dependency_p=*/true,
7796 /*class_head_p=*/false,
7797 /*is_declaration=*/true);
7798 /* Otherwise, we could also be looking for an ordinary identifier. */
7799 cp_parser_parse_tentatively (parser);
7800 /* Try a class-name. */
7801 id = cp_parser_class_name (parser,
7802 /*typename_keyword_p=*/true,
7803 /*template_keyword_p=*/false,
7804 none_type,
7805 /*check_dependency_p=*/true,
7806 /*class_head_p=*/false,
7807 /*is_declaration=*/true);
7808 /* If we found one, we're done. */
7809 if (cp_parser_parse_definitely (parser))
7810 return id;
7811 /* Otherwise, look for an ordinary identifier. */
7812 return cp_parser_identifier (parser);
7815 /* Overloading [gram.over] */
7817 /* Parse an operator-function-id.
7819 operator-function-id:
7820 operator operator
7822 Returns an IDENTIFIER_NODE for the operator which is a
7823 human-readable spelling of the identifier, e.g., `operator +'. */
7825 static tree
7826 cp_parser_operator_function_id (cp_parser* parser)
7828 /* Look for the `operator' keyword. */
7829 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7830 return error_mark_node;
7831 /* And then the name of the operator itself. */
7832 return cp_parser_operator (parser);
7835 /* Parse an operator.
7837 operator:
7838 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7839 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7840 || ++ -- , ->* -> () []
7842 GNU Extensions:
7844 operator:
7845 <? >? <?= >?=
7847 Returns an IDENTIFIER_NODE for the operator which is a
7848 human-readable spelling of the identifier, e.g., `operator +'. */
7850 static tree
7851 cp_parser_operator (cp_parser* parser)
7853 tree id = NULL_TREE;
7854 cp_token *token;
7856 /* Peek at the next token. */
7857 token = cp_lexer_peek_token (parser->lexer);
7858 /* Figure out which operator we have. */
7859 switch (token->type)
7861 case CPP_KEYWORD:
7863 enum tree_code op;
7865 /* The keyword should be either `new' or `delete'. */
7866 if (token->keyword == RID_NEW)
7867 op = NEW_EXPR;
7868 else if (token->keyword == RID_DELETE)
7869 op = DELETE_EXPR;
7870 else
7871 break;
7873 /* Consume the `new' or `delete' token. */
7874 cp_lexer_consume_token (parser->lexer);
7876 /* Peek at the next token. */
7877 token = cp_lexer_peek_token (parser->lexer);
7878 /* If it's a `[' token then this is the array variant of the
7879 operator. */
7880 if (token->type == CPP_OPEN_SQUARE)
7882 /* Consume the `[' token. */
7883 cp_lexer_consume_token (parser->lexer);
7884 /* Look for the `]' token. */
7885 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7886 id = ansi_opname (op == NEW_EXPR
7887 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7889 /* Otherwise, we have the non-array variant. */
7890 else
7891 id = ansi_opname (op);
7893 return id;
7896 case CPP_PLUS:
7897 id = ansi_opname (PLUS_EXPR);
7898 break;
7900 case CPP_MINUS:
7901 id = ansi_opname (MINUS_EXPR);
7902 break;
7904 case CPP_MULT:
7905 id = ansi_opname (MULT_EXPR);
7906 break;
7908 case CPP_DIV:
7909 id = ansi_opname (TRUNC_DIV_EXPR);
7910 break;
7912 case CPP_MOD:
7913 id = ansi_opname (TRUNC_MOD_EXPR);
7914 break;
7916 case CPP_XOR:
7917 id = ansi_opname (BIT_XOR_EXPR);
7918 break;
7920 case CPP_AND:
7921 id = ansi_opname (BIT_AND_EXPR);
7922 break;
7924 case CPP_OR:
7925 id = ansi_opname (BIT_IOR_EXPR);
7926 break;
7928 case CPP_COMPL:
7929 id = ansi_opname (BIT_NOT_EXPR);
7930 break;
7932 case CPP_NOT:
7933 id = ansi_opname (TRUTH_NOT_EXPR);
7934 break;
7936 case CPP_EQ:
7937 id = ansi_assopname (NOP_EXPR);
7938 break;
7940 case CPP_LESS:
7941 id = ansi_opname (LT_EXPR);
7942 break;
7944 case CPP_GREATER:
7945 id = ansi_opname (GT_EXPR);
7946 break;
7948 case CPP_PLUS_EQ:
7949 id = ansi_assopname (PLUS_EXPR);
7950 break;
7952 case CPP_MINUS_EQ:
7953 id = ansi_assopname (MINUS_EXPR);
7954 break;
7956 case CPP_MULT_EQ:
7957 id = ansi_assopname (MULT_EXPR);
7958 break;
7960 case CPP_DIV_EQ:
7961 id = ansi_assopname (TRUNC_DIV_EXPR);
7962 break;
7964 case CPP_MOD_EQ:
7965 id = ansi_assopname (TRUNC_MOD_EXPR);
7966 break;
7968 case CPP_XOR_EQ:
7969 id = ansi_assopname (BIT_XOR_EXPR);
7970 break;
7972 case CPP_AND_EQ:
7973 id = ansi_assopname (BIT_AND_EXPR);
7974 break;
7976 case CPP_OR_EQ:
7977 id = ansi_assopname (BIT_IOR_EXPR);
7978 break;
7980 case CPP_LSHIFT:
7981 id = ansi_opname (LSHIFT_EXPR);
7982 break;
7984 case CPP_RSHIFT:
7985 id = ansi_opname (RSHIFT_EXPR);
7986 break;
7988 case CPP_LSHIFT_EQ:
7989 id = ansi_assopname (LSHIFT_EXPR);
7990 break;
7992 case CPP_RSHIFT_EQ:
7993 id = ansi_assopname (RSHIFT_EXPR);
7994 break;
7996 case CPP_EQ_EQ:
7997 id = ansi_opname (EQ_EXPR);
7998 break;
8000 case CPP_NOT_EQ:
8001 id = ansi_opname (NE_EXPR);
8002 break;
8004 case CPP_LESS_EQ:
8005 id = ansi_opname (LE_EXPR);
8006 break;
8008 case CPP_GREATER_EQ:
8009 id = ansi_opname (GE_EXPR);
8010 break;
8012 case CPP_AND_AND:
8013 id = ansi_opname (TRUTH_ANDIF_EXPR);
8014 break;
8016 case CPP_OR_OR:
8017 id = ansi_opname (TRUTH_ORIF_EXPR);
8018 break;
8020 case CPP_PLUS_PLUS:
8021 id = ansi_opname (POSTINCREMENT_EXPR);
8022 break;
8024 case CPP_MINUS_MINUS:
8025 id = ansi_opname (PREDECREMENT_EXPR);
8026 break;
8028 case CPP_COMMA:
8029 id = ansi_opname (COMPOUND_EXPR);
8030 break;
8032 case CPP_DEREF_STAR:
8033 id = ansi_opname (MEMBER_REF);
8034 break;
8036 case CPP_DEREF:
8037 id = ansi_opname (COMPONENT_REF);
8038 break;
8040 case CPP_OPEN_PAREN:
8041 /* Consume the `('. */
8042 cp_lexer_consume_token (parser->lexer);
8043 /* Look for the matching `)'. */
8044 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8045 return ansi_opname (CALL_EXPR);
8047 case CPP_OPEN_SQUARE:
8048 /* Consume the `['. */
8049 cp_lexer_consume_token (parser->lexer);
8050 /* Look for the matching `]'. */
8051 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8052 return ansi_opname (ARRAY_REF);
8054 /* Extensions. */
8055 case CPP_MIN:
8056 id = ansi_opname (MIN_EXPR);
8057 cp_parser_warn_min_max ();
8058 break;
8060 case CPP_MAX:
8061 id = ansi_opname (MAX_EXPR);
8062 cp_parser_warn_min_max ();
8063 break;
8065 case CPP_MIN_EQ:
8066 id = ansi_assopname (MIN_EXPR);
8067 cp_parser_warn_min_max ();
8068 break;
8070 case CPP_MAX_EQ:
8071 id = ansi_assopname (MAX_EXPR);
8072 cp_parser_warn_min_max ();
8073 break;
8075 default:
8076 /* Anything else is an error. */
8077 break;
8080 /* If we have selected an identifier, we need to consume the
8081 operator token. */
8082 if (id)
8083 cp_lexer_consume_token (parser->lexer);
8084 /* Otherwise, no valid operator name was present. */
8085 else
8087 cp_parser_error (parser, "expected operator");
8088 id = error_mark_node;
8091 return id;
8094 /* Parse a template-declaration.
8096 template-declaration:
8097 export [opt] template < template-parameter-list > declaration
8099 If MEMBER_P is TRUE, this template-declaration occurs within a
8100 class-specifier.
8102 The grammar rule given by the standard isn't correct. What
8103 is really meant is:
8105 template-declaration:
8106 export [opt] template-parameter-list-seq
8107 decl-specifier-seq [opt] init-declarator [opt] ;
8108 export [opt] template-parameter-list-seq
8109 function-definition
8111 template-parameter-list-seq:
8112 template-parameter-list-seq [opt]
8113 template < template-parameter-list > */
8115 static void
8116 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8118 /* Check for `export'. */
8119 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8121 /* Consume the `export' token. */
8122 cp_lexer_consume_token (parser->lexer);
8123 /* Warn that we do not support `export'. */
8124 warning ("keyword %<export%> not implemented, and will be ignored");
8127 cp_parser_template_declaration_after_export (parser, member_p);
8130 /* Parse a template-parameter-list.
8132 template-parameter-list:
8133 template-parameter
8134 template-parameter-list , template-parameter
8136 Returns a TREE_LIST. Each node represents a template parameter.
8137 The nodes are connected via their TREE_CHAINs. */
8139 static tree
8140 cp_parser_template_parameter_list (cp_parser* parser)
8142 tree parameter_list = NULL_TREE;
8144 while (true)
8146 tree parameter;
8147 cp_token *token;
8148 bool is_non_type;
8150 /* Parse the template-parameter. */
8151 parameter = cp_parser_template_parameter (parser, &is_non_type);
8152 /* Add it to the list. */
8153 if (parameter != error_mark_node)
8154 parameter_list = process_template_parm (parameter_list,
8155 parameter,
8156 is_non_type);
8157 /* Peek at the next token. */
8158 token = cp_lexer_peek_token (parser->lexer);
8159 /* If it's not a `,', we're done. */
8160 if (token->type != CPP_COMMA)
8161 break;
8162 /* Otherwise, consume the `,' token. */
8163 cp_lexer_consume_token (parser->lexer);
8166 return parameter_list;
8169 /* Parse a template-parameter.
8171 template-parameter:
8172 type-parameter
8173 parameter-declaration
8175 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8176 the parameter. The TREE_PURPOSE is the default value, if any.
8177 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8178 iff this parameter is a non-type parameter. */
8180 static tree
8181 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8183 cp_token *token;
8184 cp_parameter_declarator *parameter_declarator;
8185 tree parm;
8187 /* Assume it is a type parameter or a template parameter. */
8188 *is_non_type = false;
8189 /* Peek at the next token. */
8190 token = cp_lexer_peek_token (parser->lexer);
8191 /* If it is `class' or `template', we have a type-parameter. */
8192 if (token->keyword == RID_TEMPLATE)
8193 return cp_parser_type_parameter (parser);
8194 /* If it is `class' or `typename' we do not know yet whether it is a
8195 type parameter or a non-type parameter. Consider:
8197 template <typename T, typename T::X X> ...
8201 template <class C, class D*> ...
8203 Here, the first parameter is a type parameter, and the second is
8204 a non-type parameter. We can tell by looking at the token after
8205 the identifier -- if it is a `,', `=', or `>' then we have a type
8206 parameter. */
8207 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8209 /* Peek at the token after `class' or `typename'. */
8210 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8211 /* If it's an identifier, skip it. */
8212 if (token->type == CPP_NAME)
8213 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8214 /* Now, see if the token looks like the end of a template
8215 parameter. */
8216 if (token->type == CPP_COMMA
8217 || token->type == CPP_EQ
8218 || token->type == CPP_GREATER)
8219 return cp_parser_type_parameter (parser);
8222 /* Otherwise, it is a non-type parameter.
8224 [temp.param]
8226 When parsing a default template-argument for a non-type
8227 template-parameter, the first non-nested `>' is taken as the end
8228 of the template parameter-list rather than a greater-than
8229 operator. */
8230 *is_non_type = true;
8231 parameter_declarator
8232 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8233 /*parenthesized_p=*/NULL);
8234 parm = grokdeclarator (parameter_declarator->declarator,
8235 &parameter_declarator->decl_specifiers,
8236 PARM, /*initialized=*/0,
8237 /*attrlist=*/NULL);
8238 if (parm == error_mark_node)
8239 return error_mark_node;
8240 return build_tree_list (parameter_declarator->default_argument, parm);
8243 /* Parse a type-parameter.
8245 type-parameter:
8246 class identifier [opt]
8247 class identifier [opt] = type-id
8248 typename identifier [opt]
8249 typename identifier [opt] = type-id
8250 template < template-parameter-list > class identifier [opt]
8251 template < template-parameter-list > class identifier [opt]
8252 = id-expression
8254 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8255 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8256 the declaration of the parameter. */
8258 static tree
8259 cp_parser_type_parameter (cp_parser* parser)
8261 cp_token *token;
8262 tree parameter;
8264 /* Look for a keyword to tell us what kind of parameter this is. */
8265 token = cp_parser_require (parser, CPP_KEYWORD,
8266 "`class', `typename', or `template'");
8267 if (!token)
8268 return error_mark_node;
8270 switch (token->keyword)
8272 case RID_CLASS:
8273 case RID_TYPENAME:
8275 tree identifier;
8276 tree default_argument;
8278 /* If the next token is an identifier, then it names the
8279 parameter. */
8280 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8281 identifier = cp_parser_identifier (parser);
8282 else
8283 identifier = NULL_TREE;
8285 /* Create the parameter. */
8286 parameter = finish_template_type_parm (class_type_node, identifier);
8288 /* If the next token is an `=', we have a default argument. */
8289 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8291 /* Consume the `=' token. */
8292 cp_lexer_consume_token (parser->lexer);
8293 /* Parse the default-argument. */
8294 default_argument = cp_parser_type_id (parser);
8296 else
8297 default_argument = NULL_TREE;
8299 /* Create the combined representation of the parameter and the
8300 default argument. */
8301 parameter = build_tree_list (default_argument, parameter);
8303 break;
8305 case RID_TEMPLATE:
8307 tree parameter_list;
8308 tree identifier;
8309 tree default_argument;
8311 /* Look for the `<'. */
8312 cp_parser_require (parser, CPP_LESS, "`<'");
8313 /* Parse the template-parameter-list. */
8314 begin_template_parm_list ();
8315 parameter_list
8316 = cp_parser_template_parameter_list (parser);
8317 parameter_list = end_template_parm_list (parameter_list);
8318 /* Look for the `>'. */
8319 cp_parser_require (parser, CPP_GREATER, "`>'");
8320 /* Look for the `class' keyword. */
8321 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8322 /* If the next token is an `=', then there is a
8323 default-argument. If the next token is a `>', we are at
8324 the end of the parameter-list. If the next token is a `,',
8325 then we are at the end of this parameter. */
8326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8327 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8328 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8330 identifier = cp_parser_identifier (parser);
8331 /* Treat invalid names as if the parameter were nameless. */
8332 if (identifier == error_mark_node)
8333 identifier = NULL_TREE;
8335 else
8336 identifier = NULL_TREE;
8338 /* Create the template parameter. */
8339 parameter = finish_template_template_parm (class_type_node,
8340 identifier);
8342 /* If the next token is an `=', then there is a
8343 default-argument. */
8344 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8346 bool is_template;
8348 /* Consume the `='. */
8349 cp_lexer_consume_token (parser->lexer);
8350 /* Parse the id-expression. */
8351 default_argument
8352 = cp_parser_id_expression (parser,
8353 /*template_keyword_p=*/false,
8354 /*check_dependency_p=*/true,
8355 /*template_p=*/&is_template,
8356 /*declarator_p=*/false);
8357 if (TREE_CODE (default_argument) == TYPE_DECL)
8358 /* If the id-expression was a template-id that refers to
8359 a template-class, we already have the declaration here,
8360 so no further lookup is needed. */
8362 else
8363 /* Look up the name. */
8364 default_argument
8365 = cp_parser_lookup_name (parser, default_argument,
8366 none_type,
8367 /*is_template=*/is_template,
8368 /*is_namespace=*/false,
8369 /*check_dependency=*/true,
8370 /*ambiguous_p=*/NULL);
8371 /* See if the default argument is valid. */
8372 default_argument
8373 = check_template_template_default_arg (default_argument);
8375 else
8376 default_argument = NULL_TREE;
8378 /* Create the combined representation of the parameter and the
8379 default argument. */
8380 parameter = build_tree_list (default_argument, parameter);
8382 break;
8384 default:
8385 gcc_unreachable ();
8386 break;
8389 return parameter;
8392 /* Parse a template-id.
8394 template-id:
8395 template-name < template-argument-list [opt] >
8397 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8398 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8399 returned. Otherwise, if the template-name names a function, or set
8400 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8401 names a class, returns a TYPE_DECL for the specialization.
8403 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8404 uninstantiated templates. */
8406 static tree
8407 cp_parser_template_id (cp_parser *parser,
8408 bool template_keyword_p,
8409 bool check_dependency_p,
8410 bool is_declaration)
8412 tree template;
8413 tree arguments;
8414 tree template_id;
8415 cp_token_position start_of_id = 0;
8416 tree access_check = NULL_TREE;
8417 cp_token *next_token, *next_token_2;
8418 bool is_identifier;
8420 /* If the next token corresponds to a template-id, there is no need
8421 to reparse it. */
8422 next_token = cp_lexer_peek_token (parser->lexer);
8423 if (next_token->type == CPP_TEMPLATE_ID)
8425 tree value;
8426 tree check;
8428 /* Get the stored value. */
8429 value = cp_lexer_consume_token (parser->lexer)->value;
8430 /* Perform any access checks that were deferred. */
8431 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8432 perform_or_defer_access_check (TREE_PURPOSE (check),
8433 TREE_VALUE (check));
8434 /* Return the stored value. */
8435 return TREE_VALUE (value);
8438 /* Avoid performing name lookup if there is no possibility of
8439 finding a template-id. */
8440 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8441 || (next_token->type == CPP_NAME
8442 && !cp_parser_nth_token_starts_template_argument_list_p
8443 (parser, 2)))
8445 cp_parser_error (parser, "expected template-id");
8446 return error_mark_node;
8449 /* Remember where the template-id starts. */
8450 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8451 start_of_id = cp_lexer_token_position (parser->lexer, false);
8453 push_deferring_access_checks (dk_deferred);
8455 /* Parse the template-name. */
8456 is_identifier = false;
8457 template = cp_parser_template_name (parser, template_keyword_p,
8458 check_dependency_p,
8459 is_declaration,
8460 &is_identifier);
8461 if (template == error_mark_node || is_identifier)
8463 pop_deferring_access_checks ();
8464 return template;
8467 /* If we find the sequence `[:' after a template-name, it's probably
8468 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8469 parse correctly the argument list. */
8470 next_token = cp_lexer_peek_token (parser->lexer);
8471 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8472 if (next_token->type == CPP_OPEN_SQUARE
8473 && next_token->flags & DIGRAPH
8474 && next_token_2->type == CPP_COLON
8475 && !(next_token_2->flags & PREV_WHITE))
8477 cp_parser_parse_tentatively (parser);
8478 /* Change `:' into `::'. */
8479 next_token_2->type = CPP_SCOPE;
8480 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8481 CPP_LESS. */
8482 cp_lexer_consume_token (parser->lexer);
8483 /* Parse the arguments. */
8484 arguments = cp_parser_enclosed_template_argument_list (parser);
8485 if (!cp_parser_parse_definitely (parser))
8487 /* If we couldn't parse an argument list, then we revert our changes
8488 and return simply an error. Maybe this is not a template-id
8489 after all. */
8490 next_token_2->type = CPP_COLON;
8491 cp_parser_error (parser, "expected %<<%>");
8492 pop_deferring_access_checks ();
8493 return error_mark_node;
8495 /* Otherwise, emit an error about the invalid digraph, but continue
8496 parsing because we got our argument list. */
8497 pedwarn ("%<<::%> cannot begin a template-argument list");
8498 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8499 "between %<<%> and %<::%>");
8500 if (!flag_permissive)
8502 static bool hint;
8503 if (!hint)
8505 inform ("(if you use -fpermissive G++ will accept your code)");
8506 hint = true;
8510 else
8512 /* Look for the `<' that starts the template-argument-list. */
8513 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8515 pop_deferring_access_checks ();
8516 return error_mark_node;
8518 /* Parse the arguments. */
8519 arguments = cp_parser_enclosed_template_argument_list (parser);
8522 /* Build a representation of the specialization. */
8523 if (TREE_CODE (template) == IDENTIFIER_NODE)
8524 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8525 else if (DECL_CLASS_TEMPLATE_P (template)
8526 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8527 template_id
8528 = finish_template_type (template, arguments,
8529 cp_lexer_next_token_is (parser->lexer,
8530 CPP_SCOPE));
8531 else
8533 /* If it's not a class-template or a template-template, it should be
8534 a function-template. */
8535 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8536 || TREE_CODE (template) == OVERLOAD
8537 || BASELINK_P (template)));
8539 template_id = lookup_template_function (template, arguments);
8542 /* Retrieve any deferred checks. Do not pop this access checks yet
8543 so the memory will not be reclaimed during token replacing below. */
8544 access_check = get_deferred_access_checks ();
8546 /* If parsing tentatively, replace the sequence of tokens that makes
8547 up the template-id with a CPP_TEMPLATE_ID token. That way,
8548 should we re-parse the token stream, we will not have to repeat
8549 the effort required to do the parse, nor will we issue duplicate
8550 error messages about problems during instantiation of the
8551 template. */
8552 if (start_of_id)
8554 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8556 /* Reset the contents of the START_OF_ID token. */
8557 token->type = CPP_TEMPLATE_ID;
8558 token->value = build_tree_list (access_check, template_id);
8559 token->keyword = RID_MAX;
8561 /* Purge all subsequent tokens. */
8562 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8564 /* ??? Can we actually assume that, if template_id ==
8565 error_mark_node, we will have issued a diagnostic to the
8566 user, as opposed to simply marking the tentative parse as
8567 failed? */
8568 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8569 error ("parse error in template argument list");
8572 pop_deferring_access_checks ();
8573 return template_id;
8576 /* Parse a template-name.
8578 template-name:
8579 identifier
8581 The standard should actually say:
8583 template-name:
8584 identifier
8585 operator-function-id
8587 A defect report has been filed about this issue.
8589 A conversion-function-id cannot be a template name because they cannot
8590 be part of a template-id. In fact, looking at this code:
8592 a.operator K<int>()
8594 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8595 It is impossible to call a templated conversion-function-id with an
8596 explicit argument list, since the only allowed template parameter is
8597 the type to which it is converting.
8599 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8600 `template' keyword, in a construction like:
8602 T::template f<3>()
8604 In that case `f' is taken to be a template-name, even though there
8605 is no way of knowing for sure.
8607 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8608 name refers to a set of overloaded functions, at least one of which
8609 is a template, or an IDENTIFIER_NODE with the name of the template,
8610 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8611 names are looked up inside uninstantiated templates. */
8613 static tree
8614 cp_parser_template_name (cp_parser* parser,
8615 bool template_keyword_p,
8616 bool check_dependency_p,
8617 bool is_declaration,
8618 bool *is_identifier)
8620 tree identifier;
8621 tree decl;
8622 tree fns;
8624 /* If the next token is `operator', then we have either an
8625 operator-function-id or a conversion-function-id. */
8626 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8628 /* We don't know whether we're looking at an
8629 operator-function-id or a conversion-function-id. */
8630 cp_parser_parse_tentatively (parser);
8631 /* Try an operator-function-id. */
8632 identifier = cp_parser_operator_function_id (parser);
8633 /* If that didn't work, try a conversion-function-id. */
8634 if (!cp_parser_parse_definitely (parser))
8636 cp_parser_error (parser, "expected template-name");
8637 return error_mark_node;
8640 /* Look for the identifier. */
8641 else
8642 identifier = cp_parser_identifier (parser);
8644 /* If we didn't find an identifier, we don't have a template-id. */
8645 if (identifier == error_mark_node)
8646 return error_mark_node;
8648 /* If the name immediately followed the `template' keyword, then it
8649 is a template-name. However, if the next token is not `<', then
8650 we do not treat it as a template-name, since it is not being used
8651 as part of a template-id. This enables us to handle constructs
8652 like:
8654 template <typename T> struct S { S(); };
8655 template <typename T> S<T>::S();
8657 correctly. We would treat `S' as a template -- if it were `S<T>'
8658 -- but we do not if there is no `<'. */
8660 if (processing_template_decl
8661 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8663 /* In a declaration, in a dependent context, we pretend that the
8664 "template" keyword was present in order to improve error
8665 recovery. For example, given:
8667 template <typename T> void f(T::X<int>);
8669 we want to treat "X<int>" as a template-id. */
8670 if (is_declaration
8671 && !template_keyword_p
8672 && parser->scope && TYPE_P (parser->scope)
8673 && check_dependency_p
8674 && dependent_type_p (parser->scope)
8675 /* Do not do this for dtors (or ctors), since they never
8676 need the template keyword before their name. */
8677 && !constructor_name_p (identifier, parser->scope))
8679 cp_token_position start = 0;
8681 /* Explain what went wrong. */
8682 error ("non-template %qD used as template", identifier);
8683 inform ("use %<%T::template %D%> to indicate that it is a template",
8684 parser->scope, identifier);
8685 /* If parsing tentatively, find the location of the "<" token. */
8686 if (cp_parser_simulate_error (parser))
8687 start = cp_lexer_token_position (parser->lexer, true);
8688 /* Parse the template arguments so that we can issue error
8689 messages about them. */
8690 cp_lexer_consume_token (parser->lexer);
8691 cp_parser_enclosed_template_argument_list (parser);
8692 /* Skip tokens until we find a good place from which to
8693 continue parsing. */
8694 cp_parser_skip_to_closing_parenthesis (parser,
8695 /*recovering=*/true,
8696 /*or_comma=*/true,
8697 /*consume_paren=*/false);
8698 /* If parsing tentatively, permanently remove the
8699 template argument list. That will prevent duplicate
8700 error messages from being issued about the missing
8701 "template" keyword. */
8702 if (start)
8703 cp_lexer_purge_tokens_after (parser->lexer, start);
8704 if (is_identifier)
8705 *is_identifier = true;
8706 return identifier;
8709 /* If the "template" keyword is present, then there is generally
8710 no point in doing name-lookup, so we just return IDENTIFIER.
8711 But, if the qualifying scope is non-dependent then we can
8712 (and must) do name-lookup normally. */
8713 if (template_keyword_p
8714 && (!parser->scope
8715 || (TYPE_P (parser->scope)
8716 && dependent_type_p (parser->scope))))
8717 return identifier;
8720 /* Look up the name. */
8721 decl = cp_parser_lookup_name (parser, identifier,
8722 none_type,
8723 /*is_template=*/false,
8724 /*is_namespace=*/false,
8725 check_dependency_p,
8726 /*ambiguous_p=*/NULL);
8727 decl = maybe_get_template_decl_from_type_decl (decl);
8729 /* If DECL is a template, then the name was a template-name. */
8730 if (TREE_CODE (decl) == TEMPLATE_DECL)
8732 else
8734 /* The standard does not explicitly indicate whether a name that
8735 names a set of overloaded declarations, some of which are
8736 templates, is a template-name. However, such a name should
8737 be a template-name; otherwise, there is no way to form a
8738 template-id for the overloaded templates. */
8739 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8740 if (TREE_CODE (fns) == OVERLOAD)
8742 tree fn;
8744 for (fn = fns; fn; fn = OVL_NEXT (fn))
8745 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8746 break;
8748 else
8750 /* Otherwise, the name does not name a template. */
8751 cp_parser_error (parser, "expected template-name");
8752 return error_mark_node;
8756 /* If DECL is dependent, and refers to a function, then just return
8757 its name; we will look it up again during template instantiation. */
8758 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8760 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8761 if (TYPE_P (scope) && dependent_type_p (scope))
8762 return identifier;
8765 return decl;
8768 /* Parse a template-argument-list.
8770 template-argument-list:
8771 template-argument
8772 template-argument-list , template-argument
8774 Returns a TREE_VEC containing the arguments. */
8776 static tree
8777 cp_parser_template_argument_list (cp_parser* parser)
8779 tree fixed_args[10];
8780 unsigned n_args = 0;
8781 unsigned alloced = 10;
8782 tree *arg_ary = fixed_args;
8783 tree vec;
8784 bool saved_in_template_argument_list_p;
8786 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8787 parser->in_template_argument_list_p = true;
8790 tree argument;
8792 if (n_args)
8793 /* Consume the comma. */
8794 cp_lexer_consume_token (parser->lexer);
8796 /* Parse the template-argument. */
8797 argument = cp_parser_template_argument (parser);
8798 if (n_args == alloced)
8800 alloced *= 2;
8802 if (arg_ary == fixed_args)
8804 arg_ary = xmalloc (sizeof (tree) * alloced);
8805 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8807 else
8808 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8810 arg_ary[n_args++] = argument;
8812 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8814 vec = make_tree_vec (n_args);
8816 while (n_args--)
8817 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8819 if (arg_ary != fixed_args)
8820 free (arg_ary);
8821 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8822 return vec;
8825 /* Parse a template-argument.
8827 template-argument:
8828 assignment-expression
8829 type-id
8830 id-expression
8832 The representation is that of an assignment-expression, type-id, or
8833 id-expression -- except that the qualified id-expression is
8834 evaluated, so that the value returned is either a DECL or an
8835 OVERLOAD.
8837 Although the standard says "assignment-expression", it forbids
8838 throw-expressions or assignments in the template argument.
8839 Therefore, we use "conditional-expression" instead. */
8841 static tree
8842 cp_parser_template_argument (cp_parser* parser)
8844 tree argument;
8845 bool template_p;
8846 bool address_p;
8847 bool maybe_type_id = false;
8848 cp_token *token;
8849 cp_id_kind idk;
8850 tree qualifying_class;
8852 /* There's really no way to know what we're looking at, so we just
8853 try each alternative in order.
8855 [temp.arg]
8857 In a template-argument, an ambiguity between a type-id and an
8858 expression is resolved to a type-id, regardless of the form of
8859 the corresponding template-parameter.
8861 Therefore, we try a type-id first. */
8862 cp_parser_parse_tentatively (parser);
8863 argument = cp_parser_type_id (parser);
8864 /* If there was no error parsing the type-id but the next token is a '>>',
8865 we probably found a typo for '> >'. But there are type-id which are
8866 also valid expressions. For instance:
8868 struct X { int operator >> (int); };
8869 template <int V> struct Foo {};
8870 Foo<X () >> 5> r;
8872 Here 'X()' is a valid type-id of a function type, but the user just
8873 wanted to write the expression "X() >> 5". Thus, we remember that we
8874 found a valid type-id, but we still try to parse the argument as an
8875 expression to see what happens. */
8876 if (!cp_parser_error_occurred (parser)
8877 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8879 maybe_type_id = true;
8880 cp_parser_abort_tentative_parse (parser);
8882 else
8884 /* If the next token isn't a `,' or a `>', then this argument wasn't
8885 really finished. This means that the argument is not a valid
8886 type-id. */
8887 if (!cp_parser_next_token_ends_template_argument_p (parser))
8888 cp_parser_error (parser, "expected template-argument");
8889 /* If that worked, we're done. */
8890 if (cp_parser_parse_definitely (parser))
8891 return argument;
8893 /* We're still not sure what the argument will be. */
8894 cp_parser_parse_tentatively (parser);
8895 /* Try a template. */
8896 argument = cp_parser_id_expression (parser,
8897 /*template_keyword_p=*/false,
8898 /*check_dependency_p=*/true,
8899 &template_p,
8900 /*declarator_p=*/false);
8901 /* If the next token isn't a `,' or a `>', then this argument wasn't
8902 really finished. */
8903 if (!cp_parser_next_token_ends_template_argument_p (parser))
8904 cp_parser_error (parser, "expected template-argument");
8905 if (!cp_parser_error_occurred (parser))
8907 /* Figure out what is being referred to. If the id-expression
8908 was for a class template specialization, then we will have a
8909 TYPE_DECL at this point. There is no need to do name lookup
8910 at this point in that case. */
8911 if (TREE_CODE (argument) != TYPE_DECL)
8912 argument = cp_parser_lookup_name (parser, argument,
8913 none_type,
8914 /*is_template=*/template_p,
8915 /*is_namespace=*/false,
8916 /*check_dependency=*/true,
8917 /*ambiguous_p=*/NULL);
8918 if (TREE_CODE (argument) != TEMPLATE_DECL
8919 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8920 cp_parser_error (parser, "expected template-name");
8922 if (cp_parser_parse_definitely (parser))
8923 return argument;
8924 /* It must be a non-type argument. There permitted cases are given
8925 in [temp.arg.nontype]:
8927 -- an integral constant-expression of integral or enumeration
8928 type; or
8930 -- the name of a non-type template-parameter; or
8932 -- the name of an object or function with external linkage...
8934 -- the address of an object or function with external linkage...
8936 -- a pointer to member... */
8937 /* Look for a non-type template parameter. */
8938 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8940 cp_parser_parse_tentatively (parser);
8941 argument = cp_parser_primary_expression (parser,
8942 /*cast_p=*/false,
8943 &idk,
8944 &qualifying_class);
8945 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8946 || !cp_parser_next_token_ends_template_argument_p (parser))
8947 cp_parser_simulate_error (parser);
8948 if (cp_parser_parse_definitely (parser))
8949 return argument;
8952 /* If the next token is "&", the argument must be the address of an
8953 object or function with external linkage. */
8954 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8955 if (address_p)
8956 cp_lexer_consume_token (parser->lexer);
8957 /* See if we might have an id-expression. */
8958 token = cp_lexer_peek_token (parser->lexer);
8959 if (token->type == CPP_NAME
8960 || token->keyword == RID_OPERATOR
8961 || token->type == CPP_SCOPE
8962 || token->type == CPP_TEMPLATE_ID
8963 || token->type == CPP_NESTED_NAME_SPECIFIER)
8965 cp_parser_parse_tentatively (parser);
8966 argument = cp_parser_primary_expression (parser,
8967 /*cast_p=*/false,
8968 &idk,
8969 &qualifying_class);
8970 if (cp_parser_error_occurred (parser)
8971 || !cp_parser_next_token_ends_template_argument_p (parser))
8972 cp_parser_abort_tentative_parse (parser);
8973 else
8975 if (TREE_CODE (argument) == INDIRECT_REF)
8977 gcc_assert (REFERENCE_REF_P (argument));
8978 argument = TREE_OPERAND (argument, 0);
8981 if (qualifying_class)
8982 argument = finish_qualified_id_expr (qualifying_class,
8983 argument,
8984 /*done=*/true,
8985 address_p);
8986 if (TREE_CODE (argument) == VAR_DECL)
8988 /* A variable without external linkage might still be a
8989 valid constant-expression, so no error is issued here
8990 if the external-linkage check fails. */
8991 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8992 cp_parser_simulate_error (parser);
8994 else if (is_overloaded_fn (argument))
8995 /* All overloaded functions are allowed; if the external
8996 linkage test does not pass, an error will be issued
8997 later. */
8999 else if (address_p
9000 && (TREE_CODE (argument) == OFFSET_REF
9001 || TREE_CODE (argument) == SCOPE_REF))
9002 /* A pointer-to-member. */
9004 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9006 else
9007 cp_parser_simulate_error (parser);
9009 if (cp_parser_parse_definitely (parser))
9011 if (address_p)
9012 argument = build_x_unary_op (ADDR_EXPR, argument);
9013 return argument;
9017 /* If the argument started with "&", there are no other valid
9018 alternatives at this point. */
9019 if (address_p)
9021 cp_parser_error (parser, "invalid non-type template argument");
9022 return error_mark_node;
9025 /* If the argument wasn't successfully parsed as a type-id followed
9026 by '>>', the argument can only be a constant expression now.
9027 Otherwise, we try parsing the constant-expression tentatively,
9028 because the argument could really be a type-id. */
9029 if (maybe_type_id)
9030 cp_parser_parse_tentatively (parser);
9031 argument = cp_parser_constant_expression (parser,
9032 /*allow_non_constant_p=*/false,
9033 /*non_constant_p=*/NULL);
9034 argument = fold_non_dependent_expr (argument);
9035 if (!maybe_type_id)
9036 return argument;
9037 if (!cp_parser_next_token_ends_template_argument_p (parser))
9038 cp_parser_error (parser, "expected template-argument");
9039 if (cp_parser_parse_definitely (parser))
9040 return argument;
9041 /* We did our best to parse the argument as a non type-id, but that
9042 was the only alternative that matched (albeit with a '>' after
9043 it). We can assume it's just a typo from the user, and a
9044 diagnostic will then be issued. */
9045 return cp_parser_type_id (parser);
9048 /* Parse an explicit-instantiation.
9050 explicit-instantiation:
9051 template declaration
9053 Although the standard says `declaration', what it really means is:
9055 explicit-instantiation:
9056 template decl-specifier-seq [opt] declarator [opt] ;
9058 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9059 supposed to be allowed. A defect report has been filed about this
9060 issue.
9062 GNU Extension:
9064 explicit-instantiation:
9065 storage-class-specifier template
9066 decl-specifier-seq [opt] declarator [opt] ;
9067 function-specifier template
9068 decl-specifier-seq [opt] declarator [opt] ; */
9070 static void
9071 cp_parser_explicit_instantiation (cp_parser* parser)
9073 int declares_class_or_enum;
9074 cp_decl_specifier_seq decl_specifiers;
9075 tree extension_specifier = NULL_TREE;
9077 /* Look for an (optional) storage-class-specifier or
9078 function-specifier. */
9079 if (cp_parser_allow_gnu_extensions_p (parser))
9081 extension_specifier
9082 = cp_parser_storage_class_specifier_opt (parser);
9083 if (!extension_specifier)
9084 extension_specifier
9085 = cp_parser_function_specifier_opt (parser,
9086 /*decl_specs=*/NULL);
9089 /* Look for the `template' keyword. */
9090 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9091 /* Let the front end know that we are processing an explicit
9092 instantiation. */
9093 begin_explicit_instantiation ();
9094 /* [temp.explicit] says that we are supposed to ignore access
9095 control while processing explicit instantiation directives. */
9096 push_deferring_access_checks (dk_no_check);
9097 /* Parse a decl-specifier-seq. */
9098 cp_parser_decl_specifier_seq (parser,
9099 CP_PARSER_FLAGS_OPTIONAL,
9100 &decl_specifiers,
9101 &declares_class_or_enum);
9102 /* If there was exactly one decl-specifier, and it declared a class,
9103 and there's no declarator, then we have an explicit type
9104 instantiation. */
9105 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9107 tree type;
9109 type = check_tag_decl (&decl_specifiers);
9110 /* Turn access control back on for names used during
9111 template instantiation. */
9112 pop_deferring_access_checks ();
9113 if (type)
9114 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9116 else
9118 cp_declarator *declarator;
9119 tree decl;
9121 /* Parse the declarator. */
9122 declarator
9123 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9124 /*ctor_dtor_or_conv_p=*/NULL,
9125 /*parenthesized_p=*/NULL,
9126 /*member_p=*/false);
9127 if (declares_class_or_enum & 2)
9128 cp_parser_check_for_definition_in_return_type (declarator,
9129 decl_specifiers.type);
9130 if (declarator != cp_error_declarator)
9132 decl = grokdeclarator (declarator, &decl_specifiers,
9133 NORMAL, 0, NULL);
9134 /* Turn access control back on for names used during
9135 template instantiation. */
9136 pop_deferring_access_checks ();
9137 /* Do the explicit instantiation. */
9138 do_decl_instantiation (decl, extension_specifier);
9140 else
9142 pop_deferring_access_checks ();
9143 /* Skip the body of the explicit instantiation. */
9144 cp_parser_skip_to_end_of_statement (parser);
9147 /* We're done with the instantiation. */
9148 end_explicit_instantiation ();
9150 cp_parser_consume_semicolon_at_end_of_statement (parser);
9153 /* Parse an explicit-specialization.
9155 explicit-specialization:
9156 template < > declaration
9158 Although the standard says `declaration', what it really means is:
9160 explicit-specialization:
9161 template <> decl-specifier [opt] init-declarator [opt] ;
9162 template <> function-definition
9163 template <> explicit-specialization
9164 template <> template-declaration */
9166 static void
9167 cp_parser_explicit_specialization (cp_parser* parser)
9169 /* Look for the `template' keyword. */
9170 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9171 /* Look for the `<'. */
9172 cp_parser_require (parser, CPP_LESS, "`<'");
9173 /* Look for the `>'. */
9174 cp_parser_require (parser, CPP_GREATER, "`>'");
9175 /* We have processed another parameter list. */
9176 ++parser->num_template_parameter_lists;
9177 /* Let the front end know that we are beginning a specialization. */
9178 begin_specialization ();
9180 /* If the next keyword is `template', we need to figure out whether
9181 or not we're looking a template-declaration. */
9182 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9184 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9185 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9186 cp_parser_template_declaration_after_export (parser,
9187 /*member_p=*/false);
9188 else
9189 cp_parser_explicit_specialization (parser);
9191 else
9192 /* Parse the dependent declaration. */
9193 cp_parser_single_declaration (parser,
9194 /*member_p=*/false,
9195 /*friend_p=*/NULL);
9197 /* We're done with the specialization. */
9198 end_specialization ();
9199 /* We're done with this parameter list. */
9200 --parser->num_template_parameter_lists;
9203 /* Parse a type-specifier.
9205 type-specifier:
9206 simple-type-specifier
9207 class-specifier
9208 enum-specifier
9209 elaborated-type-specifier
9210 cv-qualifier
9212 GNU Extension:
9214 type-specifier:
9215 __complex__
9217 Returns a representation of the type-specifier. For a
9218 class-specifier, enum-specifier, or elaborated-type-specifier, a
9219 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9221 The parser flags FLAGS is used to control type-specifier parsing.
9223 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9224 in a decl-specifier-seq.
9226 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9227 class-specifier, enum-specifier, or elaborated-type-specifier, then
9228 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9229 if a type is declared; 2 if it is defined. Otherwise, it is set to
9230 zero.
9232 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9233 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9234 is set to FALSE. */
9236 static tree
9237 cp_parser_type_specifier (cp_parser* parser,
9238 cp_parser_flags flags,
9239 cp_decl_specifier_seq *decl_specs,
9240 bool is_declaration,
9241 int* declares_class_or_enum,
9242 bool* is_cv_qualifier)
9244 tree type_spec = NULL_TREE;
9245 cp_token *token;
9246 enum rid keyword;
9247 cp_decl_spec ds = ds_last;
9249 /* Assume this type-specifier does not declare a new type. */
9250 if (declares_class_or_enum)
9251 *declares_class_or_enum = 0;
9252 /* And that it does not specify a cv-qualifier. */
9253 if (is_cv_qualifier)
9254 *is_cv_qualifier = false;
9255 /* Peek at the next token. */
9256 token = cp_lexer_peek_token (parser->lexer);
9258 /* If we're looking at a keyword, we can use that to guide the
9259 production we choose. */
9260 keyword = token->keyword;
9261 switch (keyword)
9263 case RID_ENUM:
9264 /* 'enum' [identifier] '{' introduces an enum-specifier;
9265 'enum' <anything else> introduces an elaborated-type-specifier. */
9266 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9267 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9268 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9269 == CPP_OPEN_BRACE))
9271 if (parser->num_template_parameter_lists)
9273 error ("template declaration of %qs", "enum");
9274 cp_parser_skip_to_end_of_block_or_statement (parser);
9275 type_spec = error_mark_node;
9277 else
9278 type_spec = cp_parser_enum_specifier (parser);
9280 if (declares_class_or_enum)
9281 *declares_class_or_enum = 2;
9282 if (decl_specs)
9283 cp_parser_set_decl_spec_type (decl_specs,
9284 type_spec,
9285 /*user_defined_p=*/true);
9286 return type_spec;
9288 else
9289 goto elaborated_type_specifier;
9291 /* Any of these indicate either a class-specifier, or an
9292 elaborated-type-specifier. */
9293 case RID_CLASS:
9294 case RID_STRUCT:
9295 case RID_UNION:
9296 /* Parse tentatively so that we can back up if we don't find a
9297 class-specifier. */
9298 cp_parser_parse_tentatively (parser);
9299 /* Look for the class-specifier. */
9300 type_spec = cp_parser_class_specifier (parser);
9301 /* If that worked, we're done. */
9302 if (cp_parser_parse_definitely (parser))
9304 if (declares_class_or_enum)
9305 *declares_class_or_enum = 2;
9306 if (decl_specs)
9307 cp_parser_set_decl_spec_type (decl_specs,
9308 type_spec,
9309 /*user_defined_p=*/true);
9310 return type_spec;
9313 /* Fall through. */
9314 elaborated_type_specifier:
9315 /* We're declaring (not defining) a class or enum. */
9316 if (declares_class_or_enum)
9317 *declares_class_or_enum = 1;
9319 /* Fall through. */
9320 case RID_TYPENAME:
9321 /* Look for an elaborated-type-specifier. */
9322 type_spec
9323 = (cp_parser_elaborated_type_specifier
9324 (parser,
9325 decl_specs && decl_specs->specs[(int) ds_friend],
9326 is_declaration));
9327 if (decl_specs)
9328 cp_parser_set_decl_spec_type (decl_specs,
9329 type_spec,
9330 /*user_defined_p=*/true);
9331 return type_spec;
9333 case RID_CONST:
9334 ds = ds_const;
9335 if (is_cv_qualifier)
9336 *is_cv_qualifier = true;
9337 break;
9339 case RID_VOLATILE:
9340 ds = ds_volatile;
9341 if (is_cv_qualifier)
9342 *is_cv_qualifier = true;
9343 break;
9345 case RID_RESTRICT:
9346 ds = ds_restrict;
9347 if (is_cv_qualifier)
9348 *is_cv_qualifier = true;
9349 break;
9351 case RID_COMPLEX:
9352 /* The `__complex__' keyword is a GNU extension. */
9353 ds = ds_complex;
9354 break;
9356 default:
9357 break;
9360 /* Handle simple keywords. */
9361 if (ds != ds_last)
9363 if (decl_specs)
9365 ++decl_specs->specs[(int)ds];
9366 decl_specs->any_specifiers_p = true;
9368 return cp_lexer_consume_token (parser->lexer)->value;
9371 /* If we do not already have a type-specifier, assume we are looking
9372 at a simple-type-specifier. */
9373 type_spec = cp_parser_simple_type_specifier (parser,
9374 decl_specs,
9375 flags);
9377 /* If we didn't find a type-specifier, and a type-specifier was not
9378 optional in this context, issue an error message. */
9379 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9381 cp_parser_error (parser, "expected type specifier");
9382 return error_mark_node;
9385 return type_spec;
9388 /* Parse a simple-type-specifier.
9390 simple-type-specifier:
9391 :: [opt] nested-name-specifier [opt] type-name
9392 :: [opt] nested-name-specifier template template-id
9393 char
9394 wchar_t
9395 bool
9396 short
9398 long
9399 signed
9400 unsigned
9401 float
9402 double
9403 void
9405 GNU Extension:
9407 simple-type-specifier:
9408 __typeof__ unary-expression
9409 __typeof__ ( type-id )
9411 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9412 appropriately updated. */
9414 static tree
9415 cp_parser_simple_type_specifier (cp_parser* parser,
9416 cp_decl_specifier_seq *decl_specs,
9417 cp_parser_flags flags)
9419 tree type = NULL_TREE;
9420 cp_token *token;
9422 /* Peek at the next token. */
9423 token = cp_lexer_peek_token (parser->lexer);
9425 /* If we're looking at a keyword, things are easy. */
9426 switch (token->keyword)
9428 case RID_CHAR:
9429 if (decl_specs)
9430 decl_specs->explicit_char_p = true;
9431 type = char_type_node;
9432 break;
9433 case RID_WCHAR:
9434 type = wchar_type_node;
9435 break;
9436 case RID_BOOL:
9437 type = boolean_type_node;
9438 break;
9439 case RID_SHORT:
9440 if (decl_specs)
9441 ++decl_specs->specs[(int) ds_short];
9442 type = short_integer_type_node;
9443 break;
9444 case RID_INT:
9445 if (decl_specs)
9446 decl_specs->explicit_int_p = true;
9447 type = integer_type_node;
9448 break;
9449 case RID_LONG:
9450 if (decl_specs)
9451 ++decl_specs->specs[(int) ds_long];
9452 type = long_integer_type_node;
9453 break;
9454 case RID_SIGNED:
9455 if (decl_specs)
9456 ++decl_specs->specs[(int) ds_signed];
9457 type = integer_type_node;
9458 break;
9459 case RID_UNSIGNED:
9460 if (decl_specs)
9461 ++decl_specs->specs[(int) ds_unsigned];
9462 type = unsigned_type_node;
9463 break;
9464 case RID_FLOAT:
9465 type = float_type_node;
9466 break;
9467 case RID_DOUBLE:
9468 type = double_type_node;
9469 break;
9470 case RID_VOID:
9471 type = void_type_node;
9472 break;
9474 case RID_TYPEOF:
9475 /* Consume the `typeof' token. */
9476 cp_lexer_consume_token (parser->lexer);
9477 /* Parse the operand to `typeof'. */
9478 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9479 /* If it is not already a TYPE, take its type. */
9480 if (!TYPE_P (type))
9481 type = finish_typeof (type);
9483 if (decl_specs)
9484 cp_parser_set_decl_spec_type (decl_specs, type,
9485 /*user_defined_p=*/true);
9487 return type;
9489 default:
9490 break;
9493 /* If the type-specifier was for a built-in type, we're done. */
9494 if (type)
9496 tree id;
9498 /* Record the type. */
9499 if (decl_specs
9500 && (token->keyword != RID_SIGNED
9501 && token->keyword != RID_UNSIGNED
9502 && token->keyword != RID_SHORT
9503 && token->keyword != RID_LONG))
9504 cp_parser_set_decl_spec_type (decl_specs,
9505 type,
9506 /*user_defined=*/false);
9507 if (decl_specs)
9508 decl_specs->any_specifiers_p = true;
9510 /* Consume the token. */
9511 id = cp_lexer_consume_token (parser->lexer)->value;
9513 /* There is no valid C++ program where a non-template type is
9514 followed by a "<". That usually indicates that the user thought
9515 that the type was a template. */
9516 cp_parser_check_for_invalid_template_id (parser, type);
9518 return TYPE_NAME (type);
9521 /* The type-specifier must be a user-defined type. */
9522 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9524 bool qualified_p;
9525 bool global_p;
9527 /* Don't gobble tokens or issue error messages if this is an
9528 optional type-specifier. */
9529 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9530 cp_parser_parse_tentatively (parser);
9532 /* Look for the optional `::' operator. */
9533 global_p
9534 = (cp_parser_global_scope_opt (parser,
9535 /*current_scope_valid_p=*/false)
9536 != NULL_TREE);
9537 /* Look for the nested-name specifier. */
9538 qualified_p
9539 = (cp_parser_nested_name_specifier_opt (parser,
9540 /*typename_keyword_p=*/false,
9541 /*check_dependency_p=*/true,
9542 /*type_p=*/false,
9543 /*is_declaration=*/false)
9544 != NULL_TREE);
9545 /* If we have seen a nested-name-specifier, and the next token
9546 is `template', then we are using the template-id production. */
9547 if (parser->scope
9548 && cp_parser_optional_template_keyword (parser))
9550 /* Look for the template-id. */
9551 type = cp_parser_template_id (parser,
9552 /*template_keyword_p=*/true,
9553 /*check_dependency_p=*/true,
9554 /*is_declaration=*/false);
9555 /* If the template-id did not name a type, we are out of
9556 luck. */
9557 if (TREE_CODE (type) != TYPE_DECL)
9559 cp_parser_error (parser, "expected template-id for type");
9560 type = NULL_TREE;
9563 /* Otherwise, look for a type-name. */
9564 else
9565 type = cp_parser_type_name (parser);
9566 /* Keep track of all name-lookups performed in class scopes. */
9567 if (type
9568 && !global_p
9569 && !qualified_p
9570 && TREE_CODE (type) == TYPE_DECL
9571 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9572 maybe_note_name_used_in_class (DECL_NAME (type), type);
9573 /* If it didn't work out, we don't have a TYPE. */
9574 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9575 && !cp_parser_parse_definitely (parser))
9576 type = NULL_TREE;
9577 if (type && decl_specs)
9578 cp_parser_set_decl_spec_type (decl_specs, type,
9579 /*user_defined=*/true);
9582 /* If we didn't get a type-name, issue an error message. */
9583 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9585 cp_parser_error (parser, "expected type-name");
9586 return error_mark_node;
9589 /* There is no valid C++ program where a non-template type is
9590 followed by a "<". That usually indicates that the user thought
9591 that the type was a template. */
9592 if (type && type != error_mark_node)
9593 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9595 return type;
9598 /* Parse a type-name.
9600 type-name:
9601 class-name
9602 enum-name
9603 typedef-name
9605 enum-name:
9606 identifier
9608 typedef-name:
9609 identifier
9611 Returns a TYPE_DECL for the type. */
9613 static tree
9614 cp_parser_type_name (cp_parser* parser)
9616 tree type_decl;
9617 tree identifier;
9619 /* We can't know yet whether it is a class-name or not. */
9620 cp_parser_parse_tentatively (parser);
9621 /* Try a class-name. */
9622 type_decl = cp_parser_class_name (parser,
9623 /*typename_keyword_p=*/false,
9624 /*template_keyword_p=*/false,
9625 none_type,
9626 /*check_dependency_p=*/true,
9627 /*class_head_p=*/false,
9628 /*is_declaration=*/false);
9629 /* If it's not a class-name, keep looking. */
9630 if (!cp_parser_parse_definitely (parser))
9632 /* It must be a typedef-name or an enum-name. */
9633 identifier = cp_parser_identifier (parser);
9634 if (identifier == error_mark_node)
9635 return error_mark_node;
9637 /* Look up the type-name. */
9638 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9639 /* Issue an error if we did not find a type-name. */
9640 if (TREE_CODE (type_decl) != TYPE_DECL)
9642 if (!cp_parser_simulate_error (parser))
9643 cp_parser_name_lookup_error (parser, identifier, type_decl,
9644 "is not a type");
9645 type_decl = error_mark_node;
9647 /* Remember that the name was used in the definition of the
9648 current class so that we can check later to see if the
9649 meaning would have been different after the class was
9650 entirely defined. */
9651 else if (type_decl != error_mark_node
9652 && !parser->scope)
9653 maybe_note_name_used_in_class (identifier, type_decl);
9656 return type_decl;
9660 /* Parse an elaborated-type-specifier. Note that the grammar given
9661 here incorporates the resolution to DR68.
9663 elaborated-type-specifier:
9664 class-key :: [opt] nested-name-specifier [opt] identifier
9665 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9666 enum :: [opt] nested-name-specifier [opt] identifier
9667 typename :: [opt] nested-name-specifier identifier
9668 typename :: [opt] nested-name-specifier template [opt]
9669 template-id
9671 GNU extension:
9673 elaborated-type-specifier:
9674 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9675 class-key attributes :: [opt] nested-name-specifier [opt]
9676 template [opt] template-id
9677 enum attributes :: [opt] nested-name-specifier [opt] identifier
9679 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9680 declared `friend'. If IS_DECLARATION is TRUE, then this
9681 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9682 something is being declared.
9684 Returns the TYPE specified. */
9686 static tree
9687 cp_parser_elaborated_type_specifier (cp_parser* parser,
9688 bool is_friend,
9689 bool is_declaration)
9691 enum tag_types tag_type;
9692 tree identifier;
9693 tree type = NULL_TREE;
9694 tree attributes = NULL_TREE;
9696 /* See if we're looking at the `enum' keyword. */
9697 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9699 /* Consume the `enum' token. */
9700 cp_lexer_consume_token (parser->lexer);
9701 /* Remember that it's an enumeration type. */
9702 tag_type = enum_type;
9703 /* Parse the attributes. */
9704 attributes = cp_parser_attributes_opt (parser);
9706 /* Or, it might be `typename'. */
9707 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9708 RID_TYPENAME))
9710 /* Consume the `typename' token. */
9711 cp_lexer_consume_token (parser->lexer);
9712 /* Remember that it's a `typename' type. */
9713 tag_type = typename_type;
9714 /* The `typename' keyword is only allowed in templates. */
9715 if (!processing_template_decl)
9716 pedwarn ("using %<typename%> outside of template");
9718 /* Otherwise it must be a class-key. */
9719 else
9721 tag_type = cp_parser_class_key (parser);
9722 if (tag_type == none_type)
9723 return error_mark_node;
9724 /* Parse the attributes. */
9725 attributes = cp_parser_attributes_opt (parser);
9728 /* Look for the `::' operator. */
9729 cp_parser_global_scope_opt (parser,
9730 /*current_scope_valid_p=*/false);
9731 /* Look for the nested-name-specifier. */
9732 if (tag_type == typename_type)
9734 if (cp_parser_nested_name_specifier (parser,
9735 /*typename_keyword_p=*/true,
9736 /*check_dependency_p=*/true,
9737 /*type_p=*/true,
9738 is_declaration)
9739 == error_mark_node)
9740 return error_mark_node;
9742 else
9743 /* Even though `typename' is not present, the proposed resolution
9744 to Core Issue 180 says that in `class A<T>::B', `B' should be
9745 considered a type-name, even if `A<T>' is dependent. */
9746 cp_parser_nested_name_specifier_opt (parser,
9747 /*typename_keyword_p=*/true,
9748 /*check_dependency_p=*/true,
9749 /*type_p=*/true,
9750 is_declaration);
9751 /* For everything but enumeration types, consider a template-id. */
9752 if (tag_type != enum_type)
9754 bool template_p = false;
9755 tree decl;
9757 /* Allow the `template' keyword. */
9758 template_p = cp_parser_optional_template_keyword (parser);
9759 /* If we didn't see `template', we don't know if there's a
9760 template-id or not. */
9761 if (!template_p)
9762 cp_parser_parse_tentatively (parser);
9763 /* Parse the template-id. */
9764 decl = cp_parser_template_id (parser, template_p,
9765 /*check_dependency_p=*/true,
9766 is_declaration);
9767 /* If we didn't find a template-id, look for an ordinary
9768 identifier. */
9769 if (!template_p && !cp_parser_parse_definitely (parser))
9771 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9772 in effect, then we must assume that, upon instantiation, the
9773 template will correspond to a class. */
9774 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9775 && tag_type == typename_type)
9776 type = make_typename_type (parser->scope, decl,
9777 typename_type,
9778 /*complain=*/1);
9779 else
9780 type = TREE_TYPE (decl);
9783 /* For an enumeration type, consider only a plain identifier. */
9784 if (!type)
9786 identifier = cp_parser_identifier (parser);
9788 if (identifier == error_mark_node)
9790 parser->scope = NULL_TREE;
9791 return error_mark_node;
9794 /* For a `typename', we needn't call xref_tag. */
9795 if (tag_type == typename_type
9796 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9797 return cp_parser_make_typename_type (parser, parser->scope,
9798 identifier);
9799 /* Look up a qualified name in the usual way. */
9800 if (parser->scope)
9802 tree decl;
9804 decl = cp_parser_lookup_name (parser, identifier,
9805 tag_type,
9806 /*is_template=*/false,
9807 /*is_namespace=*/false,
9808 /*check_dependency=*/true,
9809 /*ambiguous_p=*/NULL);
9811 /* If we are parsing friend declaration, DECL may be a
9812 TEMPLATE_DECL tree node here. However, we need to check
9813 whether this TEMPLATE_DECL results in valid code. Consider
9814 the following example:
9816 namespace N {
9817 template <class T> class C {};
9819 class X {
9820 template <class T> friend class N::C; // #1, valid code
9822 template <class T> class Y {
9823 friend class N::C; // #2, invalid code
9826 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9827 name lookup of `N::C'. We see that friend declaration must
9828 be template for the code to be valid. Note that
9829 processing_template_decl does not work here since it is
9830 always 1 for the above two cases. */
9832 decl = (cp_parser_maybe_treat_template_as_class
9833 (decl, /*tag_name_p=*/is_friend
9834 && parser->num_template_parameter_lists));
9836 if (TREE_CODE (decl) != TYPE_DECL)
9838 cp_parser_diagnose_invalid_type_name (parser,
9839 parser->scope,
9840 identifier);
9841 return error_mark_node;
9844 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9845 check_elaborated_type_specifier
9846 (tag_type, decl,
9847 (parser->num_template_parameter_lists
9848 || DECL_SELF_REFERENCE_P (decl)));
9850 type = TREE_TYPE (decl);
9852 else
9854 /* An elaborated-type-specifier sometimes introduces a new type and
9855 sometimes names an existing type. Normally, the rule is that it
9856 introduces a new type only if there is not an existing type of
9857 the same name already in scope. For example, given:
9859 struct S {};
9860 void f() { struct S s; }
9862 the `struct S' in the body of `f' is the same `struct S' as in
9863 the global scope; the existing definition is used. However, if
9864 there were no global declaration, this would introduce a new
9865 local class named `S'.
9867 An exception to this rule applies to the following code:
9869 namespace N { struct S; }
9871 Here, the elaborated-type-specifier names a new type
9872 unconditionally; even if there is already an `S' in the
9873 containing scope this declaration names a new type.
9874 This exception only applies if the elaborated-type-specifier
9875 forms the complete declaration:
9877 [class.name]
9879 A declaration consisting solely of `class-key identifier ;' is
9880 either a redeclaration of the name in the current scope or a
9881 forward declaration of the identifier as a class name. It
9882 introduces the name into the current scope.
9884 We are in this situation precisely when the next token is a `;'.
9886 An exception to the exception is that a `friend' declaration does
9887 *not* name a new type; i.e., given:
9889 struct S { friend struct T; };
9891 `T' is not a new type in the scope of `S'.
9893 Also, `new struct S' or `sizeof (struct S)' never results in the
9894 definition of a new type; a new type can only be declared in a
9895 declaration context. */
9897 tag_scope ts;
9898 if (is_friend)
9899 /* Friends have special name lookup rules. */
9900 ts = ts_within_enclosing_non_class;
9901 else if (is_declaration
9902 && cp_lexer_next_token_is (parser->lexer,
9903 CPP_SEMICOLON))
9904 /* This is a `class-key identifier ;' */
9905 ts = ts_current;
9906 else
9907 ts = ts_global;
9909 /* Warn about attributes. They are ignored. */
9910 if (attributes)
9911 warning ("type attributes are honored only at type definition");
9913 type = xref_tag (tag_type, identifier, ts,
9914 parser->num_template_parameter_lists);
9917 if (tag_type != enum_type)
9918 cp_parser_check_class_key (tag_type, type);
9920 /* A "<" cannot follow an elaborated type specifier. If that
9921 happens, the user was probably trying to form a template-id. */
9922 cp_parser_check_for_invalid_template_id (parser, type);
9924 return type;
9927 /* Parse an enum-specifier.
9929 enum-specifier:
9930 enum identifier [opt] { enumerator-list [opt] }
9932 GNU Extensions:
9933 enum identifier [opt] { enumerator-list [opt] } attributes
9935 Returns an ENUM_TYPE representing the enumeration. */
9937 static tree
9938 cp_parser_enum_specifier (cp_parser* parser)
9940 tree identifier;
9941 tree type;
9943 /* Caller guarantees that the current token is 'enum', an identifier
9944 possibly follows, and the token after that is an opening brace.
9945 If we don't have an identifier, fabricate an anonymous name for
9946 the enumeration being defined. */
9947 cp_lexer_consume_token (parser->lexer);
9949 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9950 identifier = cp_parser_identifier (parser);
9951 else
9952 identifier = make_anon_name ();
9954 /* Issue an error message if type-definitions are forbidden here. */
9955 cp_parser_check_type_definition (parser);
9957 /* Create the new type. We do this before consuming the opening brace
9958 so the enum will be recorded as being on the line of its tag (or the
9959 'enum' keyword, if there is no tag). */
9960 type = start_enum (identifier);
9962 /* Consume the opening brace. */
9963 cp_lexer_consume_token (parser->lexer);
9965 /* If the next token is not '}', then there are some enumerators. */
9966 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9967 cp_parser_enumerator_list (parser, type);
9969 /* Consume the final '}'. */
9970 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9972 /* Look for trailing attributes to apply to this enumeration, and
9973 apply them if appropriate. */
9974 if (cp_parser_allow_gnu_extensions_p (parser))
9976 tree trailing_attr = cp_parser_attributes_opt (parser);
9977 cplus_decl_attributes (&type,
9978 trailing_attr,
9979 (int) ATTR_FLAG_TYPE_IN_PLACE);
9982 /* Finish up the enumeration. */
9983 finish_enum (type);
9985 return type;
9988 /* Parse an enumerator-list. The enumerators all have the indicated
9989 TYPE.
9991 enumerator-list:
9992 enumerator-definition
9993 enumerator-list , enumerator-definition */
9995 static void
9996 cp_parser_enumerator_list (cp_parser* parser, tree type)
9998 while (true)
10000 /* Parse an enumerator-definition. */
10001 cp_parser_enumerator_definition (parser, type);
10003 /* If the next token is not a ',', we've reached the end of
10004 the list. */
10005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10006 break;
10007 /* Otherwise, consume the `,' and keep going. */
10008 cp_lexer_consume_token (parser->lexer);
10009 /* If the next token is a `}', there is a trailing comma. */
10010 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10012 if (pedantic && !in_system_header)
10013 pedwarn ("comma at end of enumerator list");
10014 break;
10019 /* Parse an enumerator-definition. The enumerator has the indicated
10020 TYPE.
10022 enumerator-definition:
10023 enumerator
10024 enumerator = constant-expression
10026 enumerator:
10027 identifier */
10029 static void
10030 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10032 tree identifier;
10033 tree value;
10035 /* Look for the identifier. */
10036 identifier = cp_parser_identifier (parser);
10037 if (identifier == error_mark_node)
10038 return;
10040 /* If the next token is an '=', then there is an explicit value. */
10041 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10043 /* Consume the `=' token. */
10044 cp_lexer_consume_token (parser->lexer);
10045 /* Parse the value. */
10046 value = cp_parser_constant_expression (parser,
10047 /*allow_non_constant_p=*/false,
10048 NULL);
10050 else
10051 value = NULL_TREE;
10053 /* Create the enumerator. */
10054 build_enumerator (identifier, value, type);
10057 /* Parse a namespace-name.
10059 namespace-name:
10060 original-namespace-name
10061 namespace-alias
10063 Returns the NAMESPACE_DECL for the namespace. */
10065 static tree
10066 cp_parser_namespace_name (cp_parser* parser)
10068 tree identifier;
10069 tree namespace_decl;
10071 /* Get the name of the namespace. */
10072 identifier = cp_parser_identifier (parser);
10073 if (identifier == error_mark_node)
10074 return error_mark_node;
10076 /* Look up the identifier in the currently active scope. Look only
10077 for namespaces, due to:
10079 [basic.lookup.udir]
10081 When looking up a namespace-name in a using-directive or alias
10082 definition, only namespace names are considered.
10084 And:
10086 [basic.lookup.qual]
10088 During the lookup of a name preceding the :: scope resolution
10089 operator, object, function, and enumerator names are ignored.
10091 (Note that cp_parser_class_or_namespace_name only calls this
10092 function if the token after the name is the scope resolution
10093 operator.) */
10094 namespace_decl = cp_parser_lookup_name (parser, identifier,
10095 none_type,
10096 /*is_template=*/false,
10097 /*is_namespace=*/true,
10098 /*check_dependency=*/true,
10099 /*ambiguous_p=*/NULL);
10100 /* If it's not a namespace, issue an error. */
10101 if (namespace_decl == error_mark_node
10102 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10104 cp_parser_error (parser, "expected namespace-name");
10105 namespace_decl = error_mark_node;
10108 return namespace_decl;
10111 /* Parse a namespace-definition.
10113 namespace-definition:
10114 named-namespace-definition
10115 unnamed-namespace-definition
10117 named-namespace-definition:
10118 original-namespace-definition
10119 extension-namespace-definition
10121 original-namespace-definition:
10122 namespace identifier { namespace-body }
10124 extension-namespace-definition:
10125 namespace original-namespace-name { namespace-body }
10127 unnamed-namespace-definition:
10128 namespace { namespace-body } */
10130 static void
10131 cp_parser_namespace_definition (cp_parser* parser)
10133 tree identifier;
10135 /* Look for the `namespace' keyword. */
10136 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10138 /* Get the name of the namespace. We do not attempt to distinguish
10139 between an original-namespace-definition and an
10140 extension-namespace-definition at this point. The semantic
10141 analysis routines are responsible for that. */
10142 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10143 identifier = cp_parser_identifier (parser);
10144 else
10145 identifier = NULL_TREE;
10147 /* Look for the `{' to start the namespace. */
10148 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10149 /* Start the namespace. */
10150 push_namespace (identifier);
10151 /* Parse the body of the namespace. */
10152 cp_parser_namespace_body (parser);
10153 /* Finish the namespace. */
10154 pop_namespace ();
10155 /* Look for the final `}'. */
10156 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10159 /* Parse a namespace-body.
10161 namespace-body:
10162 declaration-seq [opt] */
10164 static void
10165 cp_parser_namespace_body (cp_parser* parser)
10167 cp_parser_declaration_seq_opt (parser);
10170 /* Parse a namespace-alias-definition.
10172 namespace-alias-definition:
10173 namespace identifier = qualified-namespace-specifier ; */
10175 static void
10176 cp_parser_namespace_alias_definition (cp_parser* parser)
10178 tree identifier;
10179 tree namespace_specifier;
10181 /* Look for the `namespace' keyword. */
10182 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10183 /* Look for the identifier. */
10184 identifier = cp_parser_identifier (parser);
10185 if (identifier == error_mark_node)
10186 return;
10187 /* Look for the `=' token. */
10188 cp_parser_require (parser, CPP_EQ, "`='");
10189 /* Look for the qualified-namespace-specifier. */
10190 namespace_specifier
10191 = cp_parser_qualified_namespace_specifier (parser);
10192 /* Look for the `;' token. */
10193 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10195 /* Register the alias in the symbol table. */
10196 do_namespace_alias (identifier, namespace_specifier);
10199 /* Parse a qualified-namespace-specifier.
10201 qualified-namespace-specifier:
10202 :: [opt] nested-name-specifier [opt] namespace-name
10204 Returns a NAMESPACE_DECL corresponding to the specified
10205 namespace. */
10207 static tree
10208 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10210 /* Look for the optional `::'. */
10211 cp_parser_global_scope_opt (parser,
10212 /*current_scope_valid_p=*/false);
10214 /* Look for the optional nested-name-specifier. */
10215 cp_parser_nested_name_specifier_opt (parser,
10216 /*typename_keyword_p=*/false,
10217 /*check_dependency_p=*/true,
10218 /*type_p=*/false,
10219 /*is_declaration=*/true);
10221 return cp_parser_namespace_name (parser);
10224 /* Parse a using-declaration.
10226 using-declaration:
10227 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10228 using :: unqualified-id ; */
10230 static void
10231 cp_parser_using_declaration (cp_parser* parser)
10233 cp_token *token;
10234 bool typename_p = false;
10235 bool global_scope_p;
10236 tree decl;
10237 tree identifier;
10238 tree qscope;
10240 /* Look for the `using' keyword. */
10241 cp_parser_require_keyword (parser, RID_USING, "`using'");
10243 /* Peek at the next token. */
10244 token = cp_lexer_peek_token (parser->lexer);
10245 /* See if it's `typename'. */
10246 if (token->keyword == RID_TYPENAME)
10248 /* Remember that we've seen it. */
10249 typename_p = true;
10250 /* Consume the `typename' token. */
10251 cp_lexer_consume_token (parser->lexer);
10254 /* Look for the optional global scope qualification. */
10255 global_scope_p
10256 = (cp_parser_global_scope_opt (parser,
10257 /*current_scope_valid_p=*/false)
10258 != NULL_TREE);
10260 /* If we saw `typename', or didn't see `::', then there must be a
10261 nested-name-specifier present. */
10262 if (typename_p || !global_scope_p)
10263 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10264 /*check_dependency_p=*/true,
10265 /*type_p=*/false,
10266 /*is_declaration=*/true);
10267 /* Otherwise, we could be in either of the two productions. In that
10268 case, treat the nested-name-specifier as optional. */
10269 else
10270 qscope = cp_parser_nested_name_specifier_opt (parser,
10271 /*typename_keyword_p=*/false,
10272 /*check_dependency_p=*/true,
10273 /*type_p=*/false,
10274 /*is_declaration=*/true);
10275 if (!qscope)
10276 qscope = global_namespace;
10278 /* Parse the unqualified-id. */
10279 identifier = cp_parser_unqualified_id (parser,
10280 /*template_keyword_p=*/false,
10281 /*check_dependency_p=*/true,
10282 /*declarator_p=*/true);
10284 /* The function we call to handle a using-declaration is different
10285 depending on what scope we are in. */
10286 if (identifier == error_mark_node)
10288 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10289 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10290 /* [namespace.udecl]
10292 A using declaration shall not name a template-id. */
10293 error ("a template-id may not appear in a using-declaration");
10294 else
10296 if (at_class_scope_p ())
10298 /* Create the USING_DECL. */
10299 decl = do_class_using_decl (parser->scope, identifier);
10300 /* Add it to the list of members in this class. */
10301 finish_member_declaration (decl);
10303 else
10305 decl = cp_parser_lookup_name_simple (parser, identifier);
10306 if (decl == error_mark_node)
10307 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10308 else if (!at_namespace_scope_p ())
10309 do_local_using_decl (decl, qscope, identifier);
10310 else
10311 do_toplevel_using_decl (decl, qscope, identifier);
10315 /* Look for the final `;'. */
10316 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10319 /* Parse a using-directive.
10321 using-directive:
10322 using namespace :: [opt] nested-name-specifier [opt]
10323 namespace-name ; */
10325 static void
10326 cp_parser_using_directive (cp_parser* parser)
10328 tree namespace_decl;
10329 tree attribs;
10331 /* Look for the `using' keyword. */
10332 cp_parser_require_keyword (parser, RID_USING, "`using'");
10333 /* And the `namespace' keyword. */
10334 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10335 /* Look for the optional `::' operator. */
10336 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10337 /* And the optional nested-name-specifier. */
10338 cp_parser_nested_name_specifier_opt (parser,
10339 /*typename_keyword_p=*/false,
10340 /*check_dependency_p=*/true,
10341 /*type_p=*/false,
10342 /*is_declaration=*/true);
10343 /* Get the namespace being used. */
10344 namespace_decl = cp_parser_namespace_name (parser);
10345 /* And any specified attributes. */
10346 attribs = cp_parser_attributes_opt (parser);
10347 /* Update the symbol table. */
10348 parse_using_directive (namespace_decl, attribs);
10349 /* Look for the final `;'. */
10350 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10353 /* Parse an asm-definition.
10355 asm-definition:
10356 asm ( string-literal ) ;
10358 GNU Extension:
10360 asm-definition:
10361 asm volatile [opt] ( string-literal ) ;
10362 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10363 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10364 : asm-operand-list [opt] ) ;
10365 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10366 : asm-operand-list [opt]
10367 : asm-operand-list [opt] ) ; */
10369 static void
10370 cp_parser_asm_definition (cp_parser* parser)
10372 tree string;
10373 tree outputs = NULL_TREE;
10374 tree inputs = NULL_TREE;
10375 tree clobbers = NULL_TREE;
10376 tree asm_stmt;
10377 bool volatile_p = false;
10378 bool extended_p = false;
10380 /* Look for the `asm' keyword. */
10381 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10382 /* See if the next token is `volatile'. */
10383 if (cp_parser_allow_gnu_extensions_p (parser)
10384 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10386 /* Remember that we saw the `volatile' keyword. */
10387 volatile_p = true;
10388 /* Consume the token. */
10389 cp_lexer_consume_token (parser->lexer);
10391 /* Look for the opening `('. */
10392 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10393 return;
10394 /* Look for the string. */
10395 string = cp_parser_string_literal (parser, false, false);
10396 if (string == error_mark_node)
10398 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10399 /*consume_paren=*/true);
10400 return;
10403 /* If we're allowing GNU extensions, check for the extended assembly
10404 syntax. Unfortunately, the `:' tokens need not be separated by
10405 a space in C, and so, for compatibility, we tolerate that here
10406 too. Doing that means that we have to treat the `::' operator as
10407 two `:' tokens. */
10408 if (cp_parser_allow_gnu_extensions_p (parser)
10409 && at_function_scope_p ()
10410 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10411 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10413 bool inputs_p = false;
10414 bool clobbers_p = false;
10416 /* The extended syntax was used. */
10417 extended_p = true;
10419 /* Look for outputs. */
10420 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10422 /* Consume the `:'. */
10423 cp_lexer_consume_token (parser->lexer);
10424 /* Parse the output-operands. */
10425 if (cp_lexer_next_token_is_not (parser->lexer,
10426 CPP_COLON)
10427 && cp_lexer_next_token_is_not (parser->lexer,
10428 CPP_SCOPE)
10429 && cp_lexer_next_token_is_not (parser->lexer,
10430 CPP_CLOSE_PAREN))
10431 outputs = cp_parser_asm_operand_list (parser);
10433 /* If the next token is `::', there are no outputs, and the
10434 next token is the beginning of the inputs. */
10435 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10436 /* The inputs are coming next. */
10437 inputs_p = true;
10439 /* Look for inputs. */
10440 if (inputs_p
10441 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10443 /* Consume the `:' or `::'. */
10444 cp_lexer_consume_token (parser->lexer);
10445 /* Parse the output-operands. */
10446 if (cp_lexer_next_token_is_not (parser->lexer,
10447 CPP_COLON)
10448 && cp_lexer_next_token_is_not (parser->lexer,
10449 CPP_CLOSE_PAREN))
10450 inputs = cp_parser_asm_operand_list (parser);
10452 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10453 /* The clobbers are coming next. */
10454 clobbers_p = true;
10456 /* Look for clobbers. */
10457 if (clobbers_p
10458 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10460 /* Consume the `:' or `::'. */
10461 cp_lexer_consume_token (parser->lexer);
10462 /* Parse the clobbers. */
10463 if (cp_lexer_next_token_is_not (parser->lexer,
10464 CPP_CLOSE_PAREN))
10465 clobbers = cp_parser_asm_clobber_list (parser);
10468 /* Look for the closing `)'. */
10469 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10470 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10471 /*consume_paren=*/true);
10472 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10474 /* Create the ASM_EXPR. */
10475 if (at_function_scope_p ())
10477 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10478 inputs, clobbers);
10479 /* If the extended syntax was not used, mark the ASM_EXPR. */
10480 if (!extended_p)
10482 tree temp = asm_stmt;
10483 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10484 temp = TREE_OPERAND (temp, 0);
10486 ASM_INPUT_P (temp) = 1;
10489 else
10490 assemble_asm (string);
10493 /* Declarators [gram.dcl.decl] */
10495 /* Parse an init-declarator.
10497 init-declarator:
10498 declarator initializer [opt]
10500 GNU Extension:
10502 init-declarator:
10503 declarator asm-specification [opt] attributes [opt] initializer [opt]
10505 function-definition:
10506 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10507 function-body
10508 decl-specifier-seq [opt] declarator function-try-block
10510 GNU Extension:
10512 function-definition:
10513 __extension__ function-definition
10515 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10516 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10517 then this declarator appears in a class scope. The new DECL created
10518 by this declarator is returned.
10520 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10521 for a function-definition here as well. If the declarator is a
10522 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10523 be TRUE upon return. By that point, the function-definition will
10524 have been completely parsed.
10526 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10527 is FALSE. */
10529 static tree
10530 cp_parser_init_declarator (cp_parser* parser,
10531 cp_decl_specifier_seq *decl_specifiers,
10532 bool function_definition_allowed_p,
10533 bool member_p,
10534 int declares_class_or_enum,
10535 bool* function_definition_p)
10537 cp_token *token;
10538 cp_declarator *declarator;
10539 tree prefix_attributes;
10540 tree attributes;
10541 tree asm_specification;
10542 tree initializer;
10543 tree decl = NULL_TREE;
10544 tree scope;
10545 bool is_initialized;
10546 bool is_parenthesized_init;
10547 bool is_non_constant_init;
10548 int ctor_dtor_or_conv_p;
10549 bool friend_p;
10550 tree pushed_scope = NULL;
10552 /* Gather the attributes that were provided with the
10553 decl-specifiers. */
10554 prefix_attributes = decl_specifiers->attributes;
10556 /* Assume that this is not the declarator for a function
10557 definition. */
10558 if (function_definition_p)
10559 *function_definition_p = false;
10561 /* Defer access checks while parsing the declarator; we cannot know
10562 what names are accessible until we know what is being
10563 declared. */
10564 resume_deferring_access_checks ();
10566 /* Parse the declarator. */
10567 declarator
10568 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10569 &ctor_dtor_or_conv_p,
10570 /*parenthesized_p=*/NULL,
10571 /*member_p=*/false);
10572 /* Gather up the deferred checks. */
10573 stop_deferring_access_checks ();
10575 /* If the DECLARATOR was erroneous, there's no need to go
10576 further. */
10577 if (declarator == cp_error_declarator)
10578 return error_mark_node;
10580 if (declares_class_or_enum & 2)
10581 cp_parser_check_for_definition_in_return_type (declarator,
10582 decl_specifiers->type);
10584 /* Figure out what scope the entity declared by the DECLARATOR is
10585 located in. `grokdeclarator' sometimes changes the scope, so
10586 we compute it now. */
10587 scope = get_scope_of_declarator (declarator);
10589 /* If we're allowing GNU extensions, look for an asm-specification
10590 and attributes. */
10591 if (cp_parser_allow_gnu_extensions_p (parser))
10593 /* Look for an asm-specification. */
10594 asm_specification = cp_parser_asm_specification_opt (parser);
10595 /* And attributes. */
10596 attributes = cp_parser_attributes_opt (parser);
10598 else
10600 asm_specification = NULL_TREE;
10601 attributes = NULL_TREE;
10604 /* Peek at the next token. */
10605 token = cp_lexer_peek_token (parser->lexer);
10606 /* Check to see if the token indicates the start of a
10607 function-definition. */
10608 if (cp_parser_token_starts_function_definition_p (token))
10610 if (!function_definition_allowed_p)
10612 /* If a function-definition should not appear here, issue an
10613 error message. */
10614 cp_parser_error (parser,
10615 "a function-definition is not allowed here");
10616 return error_mark_node;
10618 else
10620 /* Neither attributes nor an asm-specification are allowed
10621 on a function-definition. */
10622 if (asm_specification)
10623 error ("an asm-specification is not allowed on a function-definition");
10624 if (attributes)
10625 error ("attributes are not allowed on a function-definition");
10626 /* This is a function-definition. */
10627 *function_definition_p = true;
10629 /* Parse the function definition. */
10630 if (member_p)
10631 decl = cp_parser_save_member_function_body (parser,
10632 decl_specifiers,
10633 declarator,
10634 prefix_attributes);
10635 else
10636 decl
10637 = (cp_parser_function_definition_from_specifiers_and_declarator
10638 (parser, decl_specifiers, prefix_attributes, declarator));
10640 return decl;
10644 /* [dcl.dcl]
10646 Only in function declarations for constructors, destructors, and
10647 type conversions can the decl-specifier-seq be omitted.
10649 We explicitly postpone this check past the point where we handle
10650 function-definitions because we tolerate function-definitions
10651 that are missing their return types in some modes. */
10652 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10654 cp_parser_error (parser,
10655 "expected constructor, destructor, or type conversion");
10656 return error_mark_node;
10659 /* An `=' or an `(' indicates an initializer. */
10660 is_initialized = (token->type == CPP_EQ
10661 || token->type == CPP_OPEN_PAREN);
10662 /* If the init-declarator isn't initialized and isn't followed by a
10663 `,' or `;', it's not a valid init-declarator. */
10664 if (!is_initialized
10665 && token->type != CPP_COMMA
10666 && token->type != CPP_SEMICOLON)
10668 cp_parser_error (parser, "expected initializer");
10669 return error_mark_node;
10672 /* Because start_decl has side-effects, we should only call it if we
10673 know we're going ahead. By this point, we know that we cannot
10674 possibly be looking at any other construct. */
10675 cp_parser_commit_to_tentative_parse (parser);
10677 /* If the decl specifiers were bad, issue an error now that we're
10678 sure this was intended to be a declarator. Then continue
10679 declaring the variable(s), as int, to try to cut down on further
10680 errors. */
10681 if (decl_specifiers->any_specifiers_p
10682 && decl_specifiers->type == error_mark_node)
10684 cp_parser_error (parser, "invalid type in declaration");
10685 decl_specifiers->type = integer_type_node;
10688 /* Check to see whether or not this declaration is a friend. */
10689 friend_p = cp_parser_friend_p (decl_specifiers);
10691 /* Check that the number of template-parameter-lists is OK. */
10692 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10693 return error_mark_node;
10695 /* Enter the newly declared entry in the symbol table. If we're
10696 processing a declaration in a class-specifier, we wait until
10697 after processing the initializer. */
10698 if (!member_p)
10700 if (parser->in_unbraced_linkage_specification_p)
10702 decl_specifiers->storage_class = sc_extern;
10703 have_extern_spec = false;
10705 decl = start_decl (declarator, decl_specifiers,
10706 is_initialized, attributes, prefix_attributes,
10707 &pushed_scope);
10709 else if (scope)
10710 /* Enter the SCOPE. That way unqualified names appearing in the
10711 initializer will be looked up in SCOPE. */
10712 pushed_scope = push_scope (scope);
10714 /* Perform deferred access control checks, now that we know in which
10715 SCOPE the declared entity resides. */
10716 if (!member_p && decl)
10718 tree saved_current_function_decl = NULL_TREE;
10720 /* If the entity being declared is a function, pretend that we
10721 are in its scope. If it is a `friend', it may have access to
10722 things that would not otherwise be accessible. */
10723 if (TREE_CODE (decl) == FUNCTION_DECL)
10725 saved_current_function_decl = current_function_decl;
10726 current_function_decl = decl;
10729 /* Perform the access control checks for the declarator and the
10730 the decl-specifiers. */
10731 perform_deferred_access_checks ();
10733 /* Restore the saved value. */
10734 if (TREE_CODE (decl) == FUNCTION_DECL)
10735 current_function_decl = saved_current_function_decl;
10738 /* Parse the initializer. */
10739 if (is_initialized)
10740 initializer = cp_parser_initializer (parser,
10741 &is_parenthesized_init,
10742 &is_non_constant_init);
10743 else
10745 initializer = NULL_TREE;
10746 is_parenthesized_init = false;
10747 is_non_constant_init = true;
10750 /* The old parser allows attributes to appear after a parenthesized
10751 initializer. Mark Mitchell proposed removing this functionality
10752 on the GCC mailing lists on 2002-08-13. This parser accepts the
10753 attributes -- but ignores them. */
10754 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10755 if (cp_parser_attributes_opt (parser))
10756 warning ("attributes after parenthesized initializer ignored");
10758 /* For an in-class declaration, use `grokfield' to create the
10759 declaration. */
10760 if (member_p)
10762 if (pushed_scope)
10764 pop_scope (pushed_scope);
10765 pushed_scope = false;
10767 decl = grokfield (declarator, decl_specifiers,
10768 initializer, /*asmspec=*/NULL_TREE,
10769 /*attributes=*/NULL_TREE);
10770 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10771 cp_parser_save_default_args (parser, decl);
10774 /* Finish processing the declaration. But, skip friend
10775 declarations. */
10776 if (!friend_p && decl && decl != error_mark_node)
10778 cp_finish_decl (decl,
10779 initializer,
10780 asm_specification,
10781 /* If the initializer is in parentheses, then this is
10782 a direct-initialization, which means that an
10783 `explicit' constructor is OK. Otherwise, an
10784 `explicit' constructor cannot be used. */
10785 ((is_parenthesized_init || !is_initialized)
10786 ? 0 : LOOKUP_ONLYCONVERTING));
10788 if (!friend_p && pushed_scope)
10789 pop_scope (pushed_scope);
10791 /* Remember whether or not variables were initialized by
10792 constant-expressions. */
10793 if (decl && TREE_CODE (decl) == VAR_DECL
10794 && is_initialized && !is_non_constant_init)
10795 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10797 return decl;
10800 /* Parse a declarator.
10802 declarator:
10803 direct-declarator
10804 ptr-operator declarator
10806 abstract-declarator:
10807 ptr-operator abstract-declarator [opt]
10808 direct-abstract-declarator
10810 GNU Extensions:
10812 declarator:
10813 attributes [opt] direct-declarator
10814 attributes [opt] ptr-operator declarator
10816 abstract-declarator:
10817 attributes [opt] ptr-operator abstract-declarator [opt]
10818 attributes [opt] direct-abstract-declarator
10820 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10821 detect constructor, destructor or conversion operators. It is set
10822 to -1 if the declarator is a name, and +1 if it is a
10823 function. Otherwise it is set to zero. Usually you just want to
10824 test for >0, but internally the negative value is used.
10826 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10827 a decl-specifier-seq unless it declares a constructor, destructor,
10828 or conversion. It might seem that we could check this condition in
10829 semantic analysis, rather than parsing, but that makes it difficult
10830 to handle something like `f()'. We want to notice that there are
10831 no decl-specifiers, and therefore realize that this is an
10832 expression, not a declaration.)
10834 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10835 the declarator is a direct-declarator of the form "(...)".
10837 MEMBER_P is true iff this declarator is a member-declarator. */
10839 static cp_declarator *
10840 cp_parser_declarator (cp_parser* parser,
10841 cp_parser_declarator_kind dcl_kind,
10842 int* ctor_dtor_or_conv_p,
10843 bool* parenthesized_p,
10844 bool member_p)
10846 cp_token *token;
10847 cp_declarator *declarator;
10848 enum tree_code code;
10849 cp_cv_quals cv_quals;
10850 tree class_type;
10851 tree attributes = NULL_TREE;
10853 /* Assume this is not a constructor, destructor, or type-conversion
10854 operator. */
10855 if (ctor_dtor_or_conv_p)
10856 *ctor_dtor_or_conv_p = 0;
10858 if (cp_parser_allow_gnu_extensions_p (parser))
10859 attributes = cp_parser_attributes_opt (parser);
10861 /* Peek at the next token. */
10862 token = cp_lexer_peek_token (parser->lexer);
10864 /* Check for the ptr-operator production. */
10865 cp_parser_parse_tentatively (parser);
10866 /* Parse the ptr-operator. */
10867 code = cp_parser_ptr_operator (parser,
10868 &class_type,
10869 &cv_quals);
10870 /* If that worked, then we have a ptr-operator. */
10871 if (cp_parser_parse_definitely (parser))
10873 /* If a ptr-operator was found, then this declarator was not
10874 parenthesized. */
10875 if (parenthesized_p)
10876 *parenthesized_p = true;
10877 /* The dependent declarator is optional if we are parsing an
10878 abstract-declarator. */
10879 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10880 cp_parser_parse_tentatively (parser);
10882 /* Parse the dependent declarator. */
10883 declarator = cp_parser_declarator (parser, dcl_kind,
10884 /*ctor_dtor_or_conv_p=*/NULL,
10885 /*parenthesized_p=*/NULL,
10886 /*member_p=*/false);
10888 /* If we are parsing an abstract-declarator, we must handle the
10889 case where the dependent declarator is absent. */
10890 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10891 && !cp_parser_parse_definitely (parser))
10892 declarator = NULL;
10894 /* Build the representation of the ptr-operator. */
10895 if (class_type)
10896 declarator = make_ptrmem_declarator (cv_quals,
10897 class_type,
10898 declarator);
10899 else if (code == INDIRECT_REF)
10900 declarator = make_pointer_declarator (cv_quals, declarator);
10901 else
10902 declarator = make_reference_declarator (cv_quals, declarator);
10904 /* Everything else is a direct-declarator. */
10905 else
10907 if (parenthesized_p)
10908 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10909 CPP_OPEN_PAREN);
10910 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10911 ctor_dtor_or_conv_p,
10912 member_p);
10915 if (attributes && declarator != cp_error_declarator)
10916 declarator->attributes = attributes;
10918 return declarator;
10921 /* Parse a direct-declarator or direct-abstract-declarator.
10923 direct-declarator:
10924 declarator-id
10925 direct-declarator ( parameter-declaration-clause )
10926 cv-qualifier-seq [opt]
10927 exception-specification [opt]
10928 direct-declarator [ constant-expression [opt] ]
10929 ( declarator )
10931 direct-abstract-declarator:
10932 direct-abstract-declarator [opt]
10933 ( parameter-declaration-clause )
10934 cv-qualifier-seq [opt]
10935 exception-specification [opt]
10936 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10937 ( abstract-declarator )
10939 Returns a representation of the declarator. DCL_KIND is
10940 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10941 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10942 we are parsing a direct-declarator. It is
10943 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10944 of ambiguity we prefer an abstract declarator, as per
10945 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10946 cp_parser_declarator. */
10948 static cp_declarator *
10949 cp_parser_direct_declarator (cp_parser* parser,
10950 cp_parser_declarator_kind dcl_kind,
10951 int* ctor_dtor_or_conv_p,
10952 bool member_p)
10954 cp_token *token;
10955 cp_declarator *declarator = NULL;
10956 tree scope = NULL_TREE;
10957 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10958 bool saved_in_declarator_p = parser->in_declarator_p;
10959 bool first = true;
10960 tree pushed_scope = NULL_TREE;
10962 while (true)
10964 /* Peek at the next token. */
10965 token = cp_lexer_peek_token (parser->lexer);
10966 if (token->type == CPP_OPEN_PAREN)
10968 /* This is either a parameter-declaration-clause, or a
10969 parenthesized declarator. When we know we are parsing a
10970 named declarator, it must be a parenthesized declarator
10971 if FIRST is true. For instance, `(int)' is a
10972 parameter-declaration-clause, with an omitted
10973 direct-abstract-declarator. But `((*))', is a
10974 parenthesized abstract declarator. Finally, when T is a
10975 template parameter `(T)' is a
10976 parameter-declaration-clause, and not a parenthesized
10977 named declarator.
10979 We first try and parse a parameter-declaration-clause,
10980 and then try a nested declarator (if FIRST is true).
10982 It is not an error for it not to be a
10983 parameter-declaration-clause, even when FIRST is
10984 false. Consider,
10986 int i (int);
10987 int i (3);
10989 The first is the declaration of a function while the
10990 second is a the definition of a variable, including its
10991 initializer.
10993 Having seen only the parenthesis, we cannot know which of
10994 these two alternatives should be selected. Even more
10995 complex are examples like:
10997 int i (int (a));
10998 int i (int (3));
11000 The former is a function-declaration; the latter is a
11001 variable initialization.
11003 Thus again, we try a parameter-declaration-clause, and if
11004 that fails, we back out and return. */
11006 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11008 cp_parameter_declarator *params;
11009 unsigned saved_num_template_parameter_lists;
11011 /* In a member-declarator, the only valid interpretation
11012 of a parenthesis is the start of a
11013 parameter-declaration-clause. (It is invalid to
11014 initialize a static data member with a parenthesized
11015 initializer; only the "=" form of initialization is
11016 permitted.) */
11017 if (!member_p)
11018 cp_parser_parse_tentatively (parser);
11020 /* Consume the `('. */
11021 cp_lexer_consume_token (parser->lexer);
11022 if (first)
11024 /* If this is going to be an abstract declarator, we're
11025 in a declarator and we can't have default args. */
11026 parser->default_arg_ok_p = false;
11027 parser->in_declarator_p = true;
11030 /* Inside the function parameter list, surrounding
11031 template-parameter-lists do not apply. */
11032 saved_num_template_parameter_lists
11033 = parser->num_template_parameter_lists;
11034 parser->num_template_parameter_lists = 0;
11036 /* Parse the parameter-declaration-clause. */
11037 params = cp_parser_parameter_declaration_clause (parser);
11039 parser->num_template_parameter_lists
11040 = saved_num_template_parameter_lists;
11042 /* If all went well, parse the cv-qualifier-seq and the
11043 exception-specification. */
11044 if (member_p || cp_parser_parse_definitely (parser))
11046 cp_cv_quals cv_quals;
11047 tree exception_specification;
11049 if (ctor_dtor_or_conv_p)
11050 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11051 first = false;
11052 /* Consume the `)'. */
11053 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11055 /* Parse the cv-qualifier-seq. */
11056 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11057 /* And the exception-specification. */
11058 exception_specification
11059 = cp_parser_exception_specification_opt (parser);
11061 /* Create the function-declarator. */
11062 declarator = make_call_declarator (declarator,
11063 params,
11064 cv_quals,
11065 exception_specification);
11066 /* Any subsequent parameter lists are to do with
11067 return type, so are not those of the declared
11068 function. */
11069 parser->default_arg_ok_p = false;
11071 /* Repeat the main loop. */
11072 continue;
11076 /* If this is the first, we can try a parenthesized
11077 declarator. */
11078 if (first)
11080 bool saved_in_type_id_in_expr_p;
11082 parser->default_arg_ok_p = saved_default_arg_ok_p;
11083 parser->in_declarator_p = saved_in_declarator_p;
11085 /* Consume the `('. */
11086 cp_lexer_consume_token (parser->lexer);
11087 /* Parse the nested declarator. */
11088 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11089 parser->in_type_id_in_expr_p = true;
11090 declarator
11091 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11092 /*parenthesized_p=*/NULL,
11093 member_p);
11094 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11095 first = false;
11096 /* Expect a `)'. */
11097 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11098 declarator = cp_error_declarator;
11099 if (declarator == cp_error_declarator)
11100 break;
11102 goto handle_declarator;
11104 /* Otherwise, we must be done. */
11105 else
11106 break;
11108 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11109 && token->type == CPP_OPEN_SQUARE)
11111 /* Parse an array-declarator. */
11112 tree bounds;
11114 if (ctor_dtor_or_conv_p)
11115 *ctor_dtor_or_conv_p = 0;
11117 first = false;
11118 parser->default_arg_ok_p = false;
11119 parser->in_declarator_p = true;
11120 /* Consume the `['. */
11121 cp_lexer_consume_token (parser->lexer);
11122 /* Peek at the next token. */
11123 token = cp_lexer_peek_token (parser->lexer);
11124 /* If the next token is `]', then there is no
11125 constant-expression. */
11126 if (token->type != CPP_CLOSE_SQUARE)
11128 bool non_constant_p;
11130 bounds
11131 = cp_parser_constant_expression (parser,
11132 /*allow_non_constant=*/true,
11133 &non_constant_p);
11134 if (!non_constant_p)
11135 bounds = fold_non_dependent_expr (bounds);
11136 /* Normally, the array bound must be an integral constant
11137 expression. However, as an extension, we allow VLAs
11138 in function scopes. */
11139 else if (!at_function_scope_p ())
11141 error ("array bound is not an integer constant");
11142 bounds = error_mark_node;
11145 else
11146 bounds = NULL_TREE;
11147 /* Look for the closing `]'. */
11148 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11150 declarator = cp_error_declarator;
11151 break;
11154 declarator = make_array_declarator (declarator, bounds);
11156 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11158 tree qualifying_scope;
11159 tree unqualified_name;
11161 /* Parse a declarator-id */
11162 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11163 cp_parser_parse_tentatively (parser);
11164 unqualified_name = cp_parser_declarator_id (parser);
11165 qualifying_scope = parser->scope;
11166 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11168 if (!cp_parser_parse_definitely (parser))
11169 unqualified_name = error_mark_node;
11170 else if (qualifying_scope
11171 || (TREE_CODE (unqualified_name)
11172 != IDENTIFIER_NODE))
11174 cp_parser_error (parser, "expected unqualified-id");
11175 unqualified_name = error_mark_node;
11179 if (unqualified_name == error_mark_node)
11181 declarator = cp_error_declarator;
11182 break;
11185 if (qualifying_scope && at_namespace_scope_p ()
11186 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11188 /* In the declaration of a member of a template class
11189 outside of the class itself, the SCOPE will sometimes
11190 be a TYPENAME_TYPE. For example, given:
11192 template <typename T>
11193 int S<T>::R::i = 3;
11195 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11196 this context, we must resolve S<T>::R to an ordinary
11197 type, rather than a typename type.
11199 The reason we normally avoid resolving TYPENAME_TYPEs
11200 is that a specialization of `S' might render
11201 `S<T>::R' not a type. However, if `S' is
11202 specialized, then this `i' will not be used, so there
11203 is no harm in resolving the types here. */
11204 tree type;
11206 /* Resolve the TYPENAME_TYPE. */
11207 type = resolve_typename_type (qualifying_scope,
11208 /*only_current_p=*/false);
11209 /* If that failed, the declarator is invalid. */
11210 if (type == error_mark_node)
11211 error ("%<%T::%D%> is not a type",
11212 TYPE_CONTEXT (qualifying_scope),
11213 TYPE_IDENTIFIER (qualifying_scope));
11214 qualifying_scope = type;
11217 declarator = make_id_declarator (qualifying_scope,
11218 unqualified_name);
11219 declarator->id_loc = token->location;
11220 if (unqualified_name)
11222 tree class_type;
11224 if (qualifying_scope
11225 && CLASS_TYPE_P (qualifying_scope))
11226 class_type = qualifying_scope;
11227 else
11228 class_type = current_class_type;
11230 if (class_type)
11232 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11233 declarator->u.id.sfk = sfk_destructor;
11234 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11235 declarator->u.id.sfk = sfk_conversion;
11236 else if (/* There's no way to declare a constructor
11237 for an anonymous type, even if the type
11238 got a name for linkage purposes. */
11239 !TYPE_WAS_ANONYMOUS (class_type)
11240 && (constructor_name_p (unqualified_name,
11241 class_type)
11242 || (TREE_CODE (unqualified_name) == TYPE_DECL
11243 && (same_type_p
11244 (TREE_TYPE (unqualified_name),
11245 class_type)))))
11246 declarator->u.id.sfk = sfk_constructor;
11248 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11249 *ctor_dtor_or_conv_p = -1;
11250 if (qualifying_scope
11251 && TREE_CODE (unqualified_name) == TYPE_DECL
11252 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11254 error ("invalid use of constructor as a template");
11255 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11256 "the constructor in a qualified name",
11257 class_type,
11258 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11259 class_type, class_type);
11264 handle_declarator:;
11265 scope = get_scope_of_declarator (declarator);
11266 if (scope)
11267 /* Any names that appear after the declarator-id for a
11268 member are looked up in the containing scope. */
11269 pushed_scope = push_scope (scope);
11270 parser->in_declarator_p = true;
11271 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11272 || (declarator && declarator->kind == cdk_id))
11273 /* Default args are only allowed on function
11274 declarations. */
11275 parser->default_arg_ok_p = saved_default_arg_ok_p;
11276 else
11277 parser->default_arg_ok_p = false;
11279 first = false;
11281 /* We're done. */
11282 else
11283 break;
11286 /* For an abstract declarator, we might wind up with nothing at this
11287 point. That's an error; the declarator is not optional. */
11288 if (!declarator)
11289 cp_parser_error (parser, "expected declarator");
11291 /* If we entered a scope, we must exit it now. */
11292 if (pushed_scope)
11293 pop_scope (pushed_scope);
11295 parser->default_arg_ok_p = saved_default_arg_ok_p;
11296 parser->in_declarator_p = saved_in_declarator_p;
11298 return declarator;
11301 /* Parse a ptr-operator.
11303 ptr-operator:
11304 * cv-qualifier-seq [opt]
11306 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11308 GNU Extension:
11310 ptr-operator:
11311 & cv-qualifier-seq [opt]
11313 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11314 Returns ADDR_EXPR if a reference was used. In the case of a
11315 pointer-to-member, *TYPE is filled in with the TYPE containing the
11316 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11317 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11318 ERROR_MARK if an error occurred. */
11320 static enum tree_code
11321 cp_parser_ptr_operator (cp_parser* parser,
11322 tree* type,
11323 cp_cv_quals *cv_quals)
11325 enum tree_code code = ERROR_MARK;
11326 cp_token *token;
11328 /* Assume that it's not a pointer-to-member. */
11329 *type = NULL_TREE;
11330 /* And that there are no cv-qualifiers. */
11331 *cv_quals = TYPE_UNQUALIFIED;
11333 /* Peek at the next token. */
11334 token = cp_lexer_peek_token (parser->lexer);
11335 /* If it's a `*' or `&' we have a pointer or reference. */
11336 if (token->type == CPP_MULT || token->type == CPP_AND)
11338 /* Remember which ptr-operator we were processing. */
11339 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11341 /* Consume the `*' or `&'. */
11342 cp_lexer_consume_token (parser->lexer);
11344 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11345 `&', if we are allowing GNU extensions. (The only qualifier
11346 that can legally appear after `&' is `restrict', but that is
11347 enforced during semantic analysis. */
11348 if (code == INDIRECT_REF
11349 || cp_parser_allow_gnu_extensions_p (parser))
11350 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11352 else
11354 /* Try the pointer-to-member case. */
11355 cp_parser_parse_tentatively (parser);
11356 /* Look for the optional `::' operator. */
11357 cp_parser_global_scope_opt (parser,
11358 /*current_scope_valid_p=*/false);
11359 /* Look for the nested-name specifier. */
11360 cp_parser_nested_name_specifier (parser,
11361 /*typename_keyword_p=*/false,
11362 /*check_dependency_p=*/true,
11363 /*type_p=*/false,
11364 /*is_declaration=*/false);
11365 /* If we found it, and the next token is a `*', then we are
11366 indeed looking at a pointer-to-member operator. */
11367 if (!cp_parser_error_occurred (parser)
11368 && cp_parser_require (parser, CPP_MULT, "`*'"))
11370 /* The type of which the member is a member is given by the
11371 current SCOPE. */
11372 *type = parser->scope;
11373 /* The next name will not be qualified. */
11374 parser->scope = NULL_TREE;
11375 parser->qualifying_scope = NULL_TREE;
11376 parser->object_scope = NULL_TREE;
11377 /* Indicate that the `*' operator was used. */
11378 code = INDIRECT_REF;
11379 /* Look for the optional cv-qualifier-seq. */
11380 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11382 /* If that didn't work we don't have a ptr-operator. */
11383 if (!cp_parser_parse_definitely (parser))
11384 cp_parser_error (parser, "expected ptr-operator");
11387 return code;
11390 /* Parse an (optional) cv-qualifier-seq.
11392 cv-qualifier-seq:
11393 cv-qualifier cv-qualifier-seq [opt]
11395 cv-qualifier:
11396 const
11397 volatile
11399 GNU Extension:
11401 cv-qualifier:
11402 __restrict__
11404 Returns a bitmask representing the cv-qualifiers. */
11406 static cp_cv_quals
11407 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11409 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11411 while (true)
11413 cp_token *token;
11414 cp_cv_quals cv_qualifier;
11416 /* Peek at the next token. */
11417 token = cp_lexer_peek_token (parser->lexer);
11418 /* See if it's a cv-qualifier. */
11419 switch (token->keyword)
11421 case RID_CONST:
11422 cv_qualifier = TYPE_QUAL_CONST;
11423 break;
11425 case RID_VOLATILE:
11426 cv_qualifier = TYPE_QUAL_VOLATILE;
11427 break;
11429 case RID_RESTRICT:
11430 cv_qualifier = TYPE_QUAL_RESTRICT;
11431 break;
11433 default:
11434 cv_qualifier = TYPE_UNQUALIFIED;
11435 break;
11438 if (!cv_qualifier)
11439 break;
11441 if (cv_quals & cv_qualifier)
11443 error ("duplicate cv-qualifier");
11444 cp_lexer_purge_token (parser->lexer);
11446 else
11448 cp_lexer_consume_token (parser->lexer);
11449 cv_quals |= cv_qualifier;
11453 return cv_quals;
11456 /* Parse a declarator-id.
11458 declarator-id:
11459 id-expression
11460 :: [opt] nested-name-specifier [opt] type-name
11462 In the `id-expression' case, the value returned is as for
11463 cp_parser_id_expression if the id-expression was an unqualified-id.
11464 If the id-expression was a qualified-id, then a SCOPE_REF is
11465 returned. The first operand is the scope (either a NAMESPACE_DECL
11466 or TREE_TYPE), but the second is still just a representation of an
11467 unqualified-id. */
11469 static tree
11470 cp_parser_declarator_id (cp_parser* parser)
11472 /* The expression must be an id-expression. Assume that qualified
11473 names are the names of types so that:
11475 template <class T>
11476 int S<T>::R::i = 3;
11478 will work; we must treat `S<T>::R' as the name of a type.
11479 Similarly, assume that qualified names are templates, where
11480 required, so that:
11482 template <class T>
11483 int S<T>::R<T>::i = 3;
11485 will work, too. */
11486 return cp_parser_id_expression (parser,
11487 /*template_keyword_p=*/false,
11488 /*check_dependency_p=*/false,
11489 /*template_p=*/NULL,
11490 /*declarator_p=*/true);
11493 /* Parse a type-id.
11495 type-id:
11496 type-specifier-seq abstract-declarator [opt]
11498 Returns the TYPE specified. */
11500 static tree
11501 cp_parser_type_id (cp_parser* parser)
11503 cp_decl_specifier_seq type_specifier_seq;
11504 cp_declarator *abstract_declarator;
11506 /* Parse the type-specifier-seq. */
11507 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11508 if (type_specifier_seq.type == error_mark_node)
11509 return error_mark_node;
11511 /* There might or might not be an abstract declarator. */
11512 cp_parser_parse_tentatively (parser);
11513 /* Look for the declarator. */
11514 abstract_declarator
11515 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11516 /*parenthesized_p=*/NULL,
11517 /*member_p=*/false);
11518 /* Check to see if there really was a declarator. */
11519 if (!cp_parser_parse_definitely (parser))
11520 abstract_declarator = NULL;
11522 return groktypename (&type_specifier_seq, abstract_declarator);
11525 /* Parse a type-specifier-seq.
11527 type-specifier-seq:
11528 type-specifier type-specifier-seq [opt]
11530 GNU extension:
11532 type-specifier-seq:
11533 attributes type-specifier-seq [opt]
11535 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11537 static void
11538 cp_parser_type_specifier_seq (cp_parser* parser,
11539 cp_decl_specifier_seq *type_specifier_seq)
11541 bool seen_type_specifier = false;
11543 /* Clear the TYPE_SPECIFIER_SEQ. */
11544 clear_decl_specs (type_specifier_seq);
11546 /* Parse the type-specifiers and attributes. */
11547 while (true)
11549 tree type_specifier;
11551 /* Check for attributes first. */
11552 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11554 type_specifier_seq->attributes =
11555 chainon (type_specifier_seq->attributes,
11556 cp_parser_attributes_opt (parser));
11557 continue;
11560 /* Look for the type-specifier. */
11561 type_specifier = cp_parser_type_specifier (parser,
11562 CP_PARSER_FLAGS_OPTIONAL,
11563 type_specifier_seq,
11564 /*is_declaration=*/false,
11565 NULL,
11566 NULL);
11567 /* If the first type-specifier could not be found, this is not a
11568 type-specifier-seq at all. */
11569 if (!seen_type_specifier && !type_specifier)
11571 cp_parser_error (parser, "expected type-specifier");
11572 type_specifier_seq->type = error_mark_node;
11573 return;
11575 /* If subsequent type-specifiers could not be found, the
11576 type-specifier-seq is complete. */
11577 else if (seen_type_specifier && !type_specifier)
11578 break;
11580 seen_type_specifier = true;
11583 return;
11586 /* Parse a parameter-declaration-clause.
11588 parameter-declaration-clause:
11589 parameter-declaration-list [opt] ... [opt]
11590 parameter-declaration-list , ...
11592 Returns a representation for the parameter declarations. A return
11593 value of NULL indicates a parameter-declaration-clause consisting
11594 only of an ellipsis. */
11596 static cp_parameter_declarator *
11597 cp_parser_parameter_declaration_clause (cp_parser* parser)
11599 cp_parameter_declarator *parameters;
11600 cp_token *token;
11601 bool ellipsis_p;
11602 bool is_error;
11604 /* Peek at the next token. */
11605 token = cp_lexer_peek_token (parser->lexer);
11606 /* Check for trivial parameter-declaration-clauses. */
11607 if (token->type == CPP_ELLIPSIS)
11609 /* Consume the `...' token. */
11610 cp_lexer_consume_token (parser->lexer);
11611 return NULL;
11613 else if (token->type == CPP_CLOSE_PAREN)
11614 /* There are no parameters. */
11616 #ifndef NO_IMPLICIT_EXTERN_C
11617 if (in_system_header && current_class_type == NULL
11618 && current_lang_name == lang_name_c)
11619 return NULL;
11620 else
11621 #endif
11622 return no_parameters;
11624 /* Check for `(void)', too, which is a special case. */
11625 else if (token->keyword == RID_VOID
11626 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11627 == CPP_CLOSE_PAREN))
11629 /* Consume the `void' token. */
11630 cp_lexer_consume_token (parser->lexer);
11631 /* There are no parameters. */
11632 return no_parameters;
11635 /* Parse the parameter-declaration-list. */
11636 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11637 /* If a parse error occurred while parsing the
11638 parameter-declaration-list, then the entire
11639 parameter-declaration-clause is erroneous. */
11640 if (is_error)
11641 return NULL;
11643 /* Peek at the next token. */
11644 token = cp_lexer_peek_token (parser->lexer);
11645 /* If it's a `,', the clause should terminate with an ellipsis. */
11646 if (token->type == CPP_COMMA)
11648 /* Consume the `,'. */
11649 cp_lexer_consume_token (parser->lexer);
11650 /* Expect an ellipsis. */
11651 ellipsis_p
11652 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11654 /* It might also be `...' if the optional trailing `,' was
11655 omitted. */
11656 else if (token->type == CPP_ELLIPSIS)
11658 /* Consume the `...' token. */
11659 cp_lexer_consume_token (parser->lexer);
11660 /* And remember that we saw it. */
11661 ellipsis_p = true;
11663 else
11664 ellipsis_p = false;
11666 /* Finish the parameter list. */
11667 if (parameters && ellipsis_p)
11668 parameters->ellipsis_p = true;
11670 return parameters;
11673 /* Parse a parameter-declaration-list.
11675 parameter-declaration-list:
11676 parameter-declaration
11677 parameter-declaration-list , parameter-declaration
11679 Returns a representation of the parameter-declaration-list, as for
11680 cp_parser_parameter_declaration_clause. However, the
11681 `void_list_node' is never appended to the list. Upon return,
11682 *IS_ERROR will be true iff an error occurred. */
11684 static cp_parameter_declarator *
11685 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11687 cp_parameter_declarator *parameters = NULL;
11688 cp_parameter_declarator **tail = &parameters;
11690 /* Assume all will go well. */
11691 *is_error = false;
11693 /* Look for more parameters. */
11694 while (true)
11696 cp_parameter_declarator *parameter;
11697 bool parenthesized_p;
11698 /* Parse the parameter. */
11699 parameter
11700 = cp_parser_parameter_declaration (parser,
11701 /*template_parm_p=*/false,
11702 &parenthesized_p);
11704 /* If a parse error occurred parsing the parameter declaration,
11705 then the entire parameter-declaration-list is erroneous. */
11706 if (!parameter)
11708 *is_error = true;
11709 parameters = NULL;
11710 break;
11712 /* Add the new parameter to the list. */
11713 *tail = parameter;
11714 tail = &parameter->next;
11716 /* Peek at the next token. */
11717 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11718 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11719 /* The parameter-declaration-list is complete. */
11720 break;
11721 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11723 cp_token *token;
11725 /* Peek at the next token. */
11726 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11727 /* If it's an ellipsis, then the list is complete. */
11728 if (token->type == CPP_ELLIPSIS)
11729 break;
11730 /* Otherwise, there must be more parameters. Consume the
11731 `,'. */
11732 cp_lexer_consume_token (parser->lexer);
11733 /* When parsing something like:
11735 int i(float f, double d)
11737 we can tell after seeing the declaration for "f" that we
11738 are not looking at an initialization of a variable "i",
11739 but rather at the declaration of a function "i".
11741 Due to the fact that the parsing of template arguments
11742 (as specified to a template-id) requires backtracking we
11743 cannot use this technique when inside a template argument
11744 list. */
11745 if (!parser->in_template_argument_list_p
11746 && !parser->in_type_id_in_expr_p
11747 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11748 /* However, a parameter-declaration of the form
11749 "foat(f)" (which is a valid declaration of a
11750 parameter "f") can also be interpreted as an
11751 expression (the conversion of "f" to "float"). */
11752 && !parenthesized_p)
11753 cp_parser_commit_to_tentative_parse (parser);
11755 else
11757 cp_parser_error (parser, "expected %<,%> or %<...%>");
11758 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11759 cp_parser_skip_to_closing_parenthesis (parser,
11760 /*recovering=*/true,
11761 /*or_comma=*/false,
11762 /*consume_paren=*/false);
11763 break;
11767 return parameters;
11770 /* Parse a parameter declaration.
11772 parameter-declaration:
11773 decl-specifier-seq declarator
11774 decl-specifier-seq declarator = assignment-expression
11775 decl-specifier-seq abstract-declarator [opt]
11776 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11778 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11779 declares a template parameter. (In that case, a non-nested `>'
11780 token encountered during the parsing of the assignment-expression
11781 is not interpreted as a greater-than operator.)
11783 Returns a representation of the parameter, or NULL if an error
11784 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11785 true iff the declarator is of the form "(p)". */
11787 static cp_parameter_declarator *
11788 cp_parser_parameter_declaration (cp_parser *parser,
11789 bool template_parm_p,
11790 bool *parenthesized_p)
11792 int declares_class_or_enum;
11793 bool greater_than_is_operator_p;
11794 cp_decl_specifier_seq decl_specifiers;
11795 cp_declarator *declarator;
11796 tree default_argument;
11797 cp_token *token;
11798 const char *saved_message;
11800 /* In a template parameter, `>' is not an operator.
11802 [temp.param]
11804 When parsing a default template-argument for a non-type
11805 template-parameter, the first non-nested `>' is taken as the end
11806 of the template parameter-list rather than a greater-than
11807 operator. */
11808 greater_than_is_operator_p = !template_parm_p;
11810 /* Type definitions may not appear in parameter types. */
11811 saved_message = parser->type_definition_forbidden_message;
11812 parser->type_definition_forbidden_message
11813 = "types may not be defined in parameter types";
11815 /* Parse the declaration-specifiers. */
11816 cp_parser_decl_specifier_seq (parser,
11817 CP_PARSER_FLAGS_NONE,
11818 &decl_specifiers,
11819 &declares_class_or_enum);
11820 /* If an error occurred, there's no reason to attempt to parse the
11821 rest of the declaration. */
11822 if (cp_parser_error_occurred (parser))
11824 parser->type_definition_forbidden_message = saved_message;
11825 return NULL;
11828 /* Peek at the next token. */
11829 token = cp_lexer_peek_token (parser->lexer);
11830 /* If the next token is a `)', `,', `=', `>', or `...', then there
11831 is no declarator. */
11832 if (token->type == CPP_CLOSE_PAREN
11833 || token->type == CPP_COMMA
11834 || token->type == CPP_EQ
11835 || token->type == CPP_ELLIPSIS
11836 || token->type == CPP_GREATER)
11838 declarator = NULL;
11839 if (parenthesized_p)
11840 *parenthesized_p = false;
11842 /* Otherwise, there should be a declarator. */
11843 else
11845 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11846 parser->default_arg_ok_p = false;
11848 /* After seeing a decl-specifier-seq, if the next token is not a
11849 "(", there is no possibility that the code is a valid
11850 expression. Therefore, if parsing tentatively, we commit at
11851 this point. */
11852 if (!parser->in_template_argument_list_p
11853 /* In an expression context, having seen:
11855 (int((char ...
11857 we cannot be sure whether we are looking at a
11858 function-type (taking a "char" as a parameter) or a cast
11859 of some object of type "char" to "int". */
11860 && !parser->in_type_id_in_expr_p
11861 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11862 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11863 cp_parser_commit_to_tentative_parse (parser);
11864 /* Parse the declarator. */
11865 declarator = cp_parser_declarator (parser,
11866 CP_PARSER_DECLARATOR_EITHER,
11867 /*ctor_dtor_or_conv_p=*/NULL,
11868 parenthesized_p,
11869 /*member_p=*/false);
11870 parser->default_arg_ok_p = saved_default_arg_ok_p;
11871 /* After the declarator, allow more attributes. */
11872 decl_specifiers.attributes
11873 = chainon (decl_specifiers.attributes,
11874 cp_parser_attributes_opt (parser));
11877 /* The restriction on defining new types applies only to the type
11878 of the parameter, not to the default argument. */
11879 parser->type_definition_forbidden_message = saved_message;
11881 /* If the next token is `=', then process a default argument. */
11882 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11884 bool saved_greater_than_is_operator_p;
11885 /* Consume the `='. */
11886 cp_lexer_consume_token (parser->lexer);
11888 /* If we are defining a class, then the tokens that make up the
11889 default argument must be saved and processed later. */
11890 if (!template_parm_p && at_class_scope_p ()
11891 && TYPE_BEING_DEFINED (current_class_type))
11893 unsigned depth = 0;
11894 cp_token *first_token;
11895 cp_token *token;
11897 /* Add tokens until we have processed the entire default
11898 argument. We add the range [first_token, token). */
11899 first_token = cp_lexer_peek_token (parser->lexer);
11900 while (true)
11902 bool done = false;
11904 /* Peek at the next token. */
11905 token = cp_lexer_peek_token (parser->lexer);
11906 /* What we do depends on what token we have. */
11907 switch (token->type)
11909 /* In valid code, a default argument must be
11910 immediately followed by a `,' `)', or `...'. */
11911 case CPP_COMMA:
11912 case CPP_CLOSE_PAREN:
11913 case CPP_ELLIPSIS:
11914 /* If we run into a non-nested `;', `}', or `]',
11915 then the code is invalid -- but the default
11916 argument is certainly over. */
11917 case CPP_SEMICOLON:
11918 case CPP_CLOSE_BRACE:
11919 case CPP_CLOSE_SQUARE:
11920 if (depth == 0)
11921 done = true;
11922 /* Update DEPTH, if necessary. */
11923 else if (token->type == CPP_CLOSE_PAREN
11924 || token->type == CPP_CLOSE_BRACE
11925 || token->type == CPP_CLOSE_SQUARE)
11926 --depth;
11927 break;
11929 case CPP_OPEN_PAREN:
11930 case CPP_OPEN_SQUARE:
11931 case CPP_OPEN_BRACE:
11932 ++depth;
11933 break;
11935 case CPP_GREATER:
11936 /* If we see a non-nested `>', and `>' is not an
11937 operator, then it marks the end of the default
11938 argument. */
11939 if (!depth && !greater_than_is_operator_p)
11940 done = true;
11941 break;
11943 /* If we run out of tokens, issue an error message. */
11944 case CPP_EOF:
11945 error ("file ends in default argument");
11946 done = true;
11947 break;
11949 case CPP_NAME:
11950 case CPP_SCOPE:
11951 /* In these cases, we should look for template-ids.
11952 For example, if the default argument is
11953 `X<int, double>()', we need to do name lookup to
11954 figure out whether or not `X' is a template; if
11955 so, the `,' does not end the default argument.
11957 That is not yet done. */
11958 break;
11960 default:
11961 break;
11964 /* If we've reached the end, stop. */
11965 if (done)
11966 break;
11968 /* Add the token to the token block. */
11969 token = cp_lexer_consume_token (parser->lexer);
11972 /* Create a DEFAULT_ARG to represented the unparsed default
11973 argument. */
11974 default_argument = make_node (DEFAULT_ARG);
11975 DEFARG_TOKENS (default_argument)
11976 = cp_token_cache_new (first_token, token);
11978 /* Outside of a class definition, we can just parse the
11979 assignment-expression. */
11980 else
11982 bool saved_local_variables_forbidden_p;
11984 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11985 set correctly. */
11986 saved_greater_than_is_operator_p
11987 = parser->greater_than_is_operator_p;
11988 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11989 /* Local variable names (and the `this' keyword) may not
11990 appear in a default argument. */
11991 saved_local_variables_forbidden_p
11992 = parser->local_variables_forbidden_p;
11993 parser->local_variables_forbidden_p = true;
11994 /* Parse the assignment-expression. */
11995 default_argument
11996 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
11997 /* Restore saved state. */
11998 parser->greater_than_is_operator_p
11999 = saved_greater_than_is_operator_p;
12000 parser->local_variables_forbidden_p
12001 = saved_local_variables_forbidden_p;
12003 if (!parser->default_arg_ok_p)
12005 if (!flag_pedantic_errors)
12006 warning ("deprecated use of default argument for parameter of non-function");
12007 else
12009 error ("default arguments are only permitted for function parameters");
12010 default_argument = NULL_TREE;
12014 else
12015 default_argument = NULL_TREE;
12017 return make_parameter_declarator (&decl_specifiers,
12018 declarator,
12019 default_argument);
12022 /* Parse a function-body.
12024 function-body:
12025 compound_statement */
12027 static void
12028 cp_parser_function_body (cp_parser *parser)
12030 cp_parser_compound_statement (parser, NULL, false);
12033 /* Parse a ctor-initializer-opt followed by a function-body. Return
12034 true if a ctor-initializer was present. */
12036 static bool
12037 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12039 tree body;
12040 bool ctor_initializer_p;
12042 /* Begin the function body. */
12043 body = begin_function_body ();
12044 /* Parse the optional ctor-initializer. */
12045 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12046 /* Parse the function-body. */
12047 cp_parser_function_body (parser);
12048 /* Finish the function body. */
12049 finish_function_body (body);
12051 return ctor_initializer_p;
12054 /* Parse an initializer.
12056 initializer:
12057 = initializer-clause
12058 ( expression-list )
12060 Returns a expression representing the initializer. If no
12061 initializer is present, NULL_TREE is returned.
12063 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12064 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12065 set to FALSE if there is no initializer present. If there is an
12066 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12067 is set to true; otherwise it is set to false. */
12069 static tree
12070 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12071 bool* non_constant_p)
12073 cp_token *token;
12074 tree init;
12076 /* Peek at the next token. */
12077 token = cp_lexer_peek_token (parser->lexer);
12079 /* Let our caller know whether or not this initializer was
12080 parenthesized. */
12081 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12082 /* Assume that the initializer is constant. */
12083 *non_constant_p = false;
12085 if (token->type == CPP_EQ)
12087 /* Consume the `='. */
12088 cp_lexer_consume_token (parser->lexer);
12089 /* Parse the initializer-clause. */
12090 init = cp_parser_initializer_clause (parser, non_constant_p);
12092 else if (token->type == CPP_OPEN_PAREN)
12093 init = cp_parser_parenthesized_expression_list (parser, false,
12094 /*cast_p=*/false,
12095 non_constant_p);
12096 else
12098 /* Anything else is an error. */
12099 cp_parser_error (parser, "expected initializer");
12100 init = error_mark_node;
12103 return init;
12106 /* Parse an initializer-clause.
12108 initializer-clause:
12109 assignment-expression
12110 { initializer-list , [opt] }
12113 Returns an expression representing the initializer.
12115 If the `assignment-expression' production is used the value
12116 returned is simply a representation for the expression.
12118 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12119 the elements of the initializer-list (or NULL_TREE, if the last
12120 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12121 NULL_TREE. There is no way to detect whether or not the optional
12122 trailing `,' was provided. NON_CONSTANT_P is as for
12123 cp_parser_initializer. */
12125 static tree
12126 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12128 tree initializer;
12130 /* Assume the expression is constant. */
12131 *non_constant_p = false;
12133 /* If it is not a `{', then we are looking at an
12134 assignment-expression. */
12135 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12137 initializer
12138 = cp_parser_constant_expression (parser,
12139 /*allow_non_constant_p=*/true,
12140 non_constant_p);
12141 if (!*non_constant_p)
12142 initializer = fold_non_dependent_expr (initializer);
12144 else
12146 /* Consume the `{' token. */
12147 cp_lexer_consume_token (parser->lexer);
12148 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12149 initializer = make_node (CONSTRUCTOR);
12150 /* If it's not a `}', then there is a non-trivial initializer. */
12151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12153 /* Parse the initializer list. */
12154 CONSTRUCTOR_ELTS (initializer)
12155 = cp_parser_initializer_list (parser, non_constant_p);
12156 /* A trailing `,' token is allowed. */
12157 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12158 cp_lexer_consume_token (parser->lexer);
12160 /* Now, there should be a trailing `}'. */
12161 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12164 return initializer;
12167 /* Parse an initializer-list.
12169 initializer-list:
12170 initializer-clause
12171 initializer-list , initializer-clause
12173 GNU Extension:
12175 initializer-list:
12176 identifier : initializer-clause
12177 initializer-list, identifier : initializer-clause
12179 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12180 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12181 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12182 as for cp_parser_initializer. */
12184 static tree
12185 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12187 tree initializers = NULL_TREE;
12189 /* Assume all of the expressions are constant. */
12190 *non_constant_p = false;
12192 /* Parse the rest of the list. */
12193 while (true)
12195 cp_token *token;
12196 tree identifier;
12197 tree initializer;
12198 bool clause_non_constant_p;
12200 /* If the next token is an identifier and the following one is a
12201 colon, we are looking at the GNU designated-initializer
12202 syntax. */
12203 if (cp_parser_allow_gnu_extensions_p (parser)
12204 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12205 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12207 /* Consume the identifier. */
12208 identifier = cp_lexer_consume_token (parser->lexer)->value;
12209 /* Consume the `:'. */
12210 cp_lexer_consume_token (parser->lexer);
12212 else
12213 identifier = NULL_TREE;
12215 /* Parse the initializer. */
12216 initializer = cp_parser_initializer_clause (parser,
12217 &clause_non_constant_p);
12218 /* If any clause is non-constant, so is the entire initializer. */
12219 if (clause_non_constant_p)
12220 *non_constant_p = true;
12221 /* Add it to the list. */
12222 initializers = tree_cons (identifier, initializer, initializers);
12224 /* If the next token is not a comma, we have reached the end of
12225 the list. */
12226 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12227 break;
12229 /* Peek at the next token. */
12230 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12231 /* If the next token is a `}', then we're still done. An
12232 initializer-clause can have a trailing `,' after the
12233 initializer-list and before the closing `}'. */
12234 if (token->type == CPP_CLOSE_BRACE)
12235 break;
12237 /* Consume the `,' token. */
12238 cp_lexer_consume_token (parser->lexer);
12241 /* The initializers were built up in reverse order, so we need to
12242 reverse them now. */
12243 return nreverse (initializers);
12246 /* Classes [gram.class] */
12248 /* Parse a class-name.
12250 class-name:
12251 identifier
12252 template-id
12254 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12255 to indicate that names looked up in dependent types should be
12256 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12257 keyword has been used to indicate that the name that appears next
12258 is a template. TAG_TYPE indicates the explicit tag given before
12259 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12260 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12261 is the class being defined in a class-head.
12263 Returns the TYPE_DECL representing the class. */
12265 static tree
12266 cp_parser_class_name (cp_parser *parser,
12267 bool typename_keyword_p,
12268 bool template_keyword_p,
12269 enum tag_types tag_type,
12270 bool check_dependency_p,
12271 bool class_head_p,
12272 bool is_declaration)
12274 tree decl;
12275 tree scope;
12276 bool typename_p;
12277 cp_token *token;
12279 /* All class-names start with an identifier. */
12280 token = cp_lexer_peek_token (parser->lexer);
12281 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12283 cp_parser_error (parser, "expected class-name");
12284 return error_mark_node;
12287 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12288 to a template-id, so we save it here. */
12289 scope = parser->scope;
12290 if (scope == error_mark_node)
12291 return error_mark_node;
12293 /* Any name names a type if we're following the `typename' keyword
12294 in a qualified name where the enclosing scope is type-dependent. */
12295 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12296 && dependent_type_p (scope));
12297 /* Handle the common case (an identifier, but not a template-id)
12298 efficiently. */
12299 if (token->type == CPP_NAME
12300 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12302 tree identifier;
12304 /* Look for the identifier. */
12305 identifier = cp_parser_identifier (parser);
12306 /* If the next token isn't an identifier, we are certainly not
12307 looking at a class-name. */
12308 if (identifier == error_mark_node)
12309 decl = error_mark_node;
12310 /* If we know this is a type-name, there's no need to look it
12311 up. */
12312 else if (typename_p)
12313 decl = identifier;
12314 else
12316 /* If the next token is a `::', then the name must be a type
12317 name.
12319 [basic.lookup.qual]
12321 During the lookup for a name preceding the :: scope
12322 resolution operator, object, function, and enumerator
12323 names are ignored. */
12324 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12325 tag_type = typename_type;
12326 /* Look up the name. */
12327 decl = cp_parser_lookup_name (parser, identifier,
12328 tag_type,
12329 /*is_template=*/false,
12330 /*is_namespace=*/false,
12331 check_dependency_p,
12332 /*ambiguous_p=*/NULL);
12335 else
12337 /* Try a template-id. */
12338 decl = cp_parser_template_id (parser, template_keyword_p,
12339 check_dependency_p,
12340 is_declaration);
12341 if (decl == error_mark_node)
12342 return error_mark_node;
12345 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12347 /* If this is a typename, create a TYPENAME_TYPE. */
12348 if (typename_p && decl != error_mark_node)
12350 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12351 if (decl != error_mark_node)
12352 decl = TYPE_NAME (decl);
12355 /* Check to see that it is really the name of a class. */
12356 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12357 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12358 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12359 /* Situations like this:
12361 template <typename T> struct A {
12362 typename T::template X<int>::I i;
12365 are problematic. Is `T::template X<int>' a class-name? The
12366 standard does not seem to be definitive, but there is no other
12367 valid interpretation of the following `::'. Therefore, those
12368 names are considered class-names. */
12369 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12370 else if (decl == error_mark_node
12371 || TREE_CODE (decl) != TYPE_DECL
12372 || TREE_TYPE (decl) == error_mark_node
12373 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12375 cp_parser_error (parser, "expected class-name");
12376 return error_mark_node;
12379 return decl;
12382 /* Parse a class-specifier.
12384 class-specifier:
12385 class-head { member-specification [opt] }
12387 Returns the TREE_TYPE representing the class. */
12389 static tree
12390 cp_parser_class_specifier (cp_parser* parser)
12392 cp_token *token;
12393 tree type;
12394 tree attributes = NULL_TREE;
12395 int has_trailing_semicolon;
12396 bool nested_name_specifier_p;
12397 unsigned saved_num_template_parameter_lists;
12398 tree old_scope = NULL_TREE;
12399 tree scope = NULL_TREE;
12401 push_deferring_access_checks (dk_no_deferred);
12403 /* Parse the class-head. */
12404 type = cp_parser_class_head (parser,
12405 &nested_name_specifier_p,
12406 &attributes);
12407 /* If the class-head was a semantic disaster, skip the entire body
12408 of the class. */
12409 if (!type)
12411 cp_parser_skip_to_end_of_block_or_statement (parser);
12412 pop_deferring_access_checks ();
12413 return error_mark_node;
12416 /* Look for the `{'. */
12417 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12419 pop_deferring_access_checks ();
12420 return error_mark_node;
12423 /* Issue an error message if type-definitions are forbidden here. */
12424 cp_parser_check_type_definition (parser);
12425 /* Remember that we are defining one more class. */
12426 ++parser->num_classes_being_defined;
12427 /* Inside the class, surrounding template-parameter-lists do not
12428 apply. */
12429 saved_num_template_parameter_lists
12430 = parser->num_template_parameter_lists;
12431 parser->num_template_parameter_lists = 0;
12433 /* Start the class. */
12434 if (nested_name_specifier_p)
12436 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12437 old_scope = push_inner_scope (scope);
12439 type = begin_class_definition (type);
12441 if (type == error_mark_node)
12442 /* If the type is erroneous, skip the entire body of the class. */
12443 cp_parser_skip_to_closing_brace (parser);
12444 else
12445 /* Parse the member-specification. */
12446 cp_parser_member_specification_opt (parser);
12448 /* Look for the trailing `}'. */
12449 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12450 /* We get better error messages by noticing a common problem: a
12451 missing trailing `;'. */
12452 token = cp_lexer_peek_token (parser->lexer);
12453 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12454 /* Look for trailing attributes to apply to this class. */
12455 if (cp_parser_allow_gnu_extensions_p (parser))
12457 tree sub_attr = cp_parser_attributes_opt (parser);
12458 attributes = chainon (attributes, sub_attr);
12460 if (type != error_mark_node)
12461 type = finish_struct (type, attributes);
12462 if (nested_name_specifier_p)
12463 pop_inner_scope (old_scope, scope);
12464 /* If this class is not itself within the scope of another class,
12465 then we need to parse the bodies of all of the queued function
12466 definitions. Note that the queued functions defined in a class
12467 are not always processed immediately following the
12468 class-specifier for that class. Consider:
12470 struct A {
12471 struct B { void f() { sizeof (A); } };
12474 If `f' were processed before the processing of `A' were
12475 completed, there would be no way to compute the size of `A'.
12476 Note that the nesting we are interested in here is lexical --
12477 not the semantic nesting given by TYPE_CONTEXT. In particular,
12478 for:
12480 struct A { struct B; };
12481 struct A::B { void f() { } };
12483 there is no need to delay the parsing of `A::B::f'. */
12484 if (--parser->num_classes_being_defined == 0)
12486 tree queue_entry;
12487 tree fn;
12488 tree class_type = NULL_TREE;
12489 tree pushed_scope = NULL_TREE;
12491 /* In a first pass, parse default arguments to the functions.
12492 Then, in a second pass, parse the bodies of the functions.
12493 This two-phased approach handles cases like:
12495 struct S {
12496 void f() { g(); }
12497 void g(int i = 3);
12501 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12502 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12503 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12504 TREE_PURPOSE (parser->unparsed_functions_queues)
12505 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12507 fn = TREE_VALUE (queue_entry);
12508 /* If there are default arguments that have not yet been processed,
12509 take care of them now. */
12510 if (class_type != TREE_PURPOSE (queue_entry))
12512 if (pushed_scope)
12513 pop_scope (pushed_scope);
12514 class_type = TREE_PURPOSE (queue_entry);
12515 pushed_scope = push_scope (class_type);
12517 /* Make sure that any template parameters are in scope. */
12518 maybe_begin_member_template_processing (fn);
12519 /* Parse the default argument expressions. */
12520 cp_parser_late_parsing_default_args (parser, fn);
12521 /* Remove any template parameters from the symbol table. */
12522 maybe_end_member_template_processing ();
12524 if (pushed_scope)
12525 pop_scope (pushed_scope);
12526 /* Now parse the body of the functions. */
12527 for (TREE_VALUE (parser->unparsed_functions_queues)
12528 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12529 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12530 TREE_VALUE (parser->unparsed_functions_queues)
12531 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12533 /* Figure out which function we need to process. */
12534 fn = TREE_VALUE (queue_entry);
12536 /* A hack to prevent garbage collection. */
12537 function_depth++;
12539 /* Parse the function. */
12540 cp_parser_late_parsing_for_member (parser, fn);
12541 function_depth--;
12545 /* Put back any saved access checks. */
12546 pop_deferring_access_checks ();
12548 /* Restore the count of active template-parameter-lists. */
12549 parser->num_template_parameter_lists
12550 = saved_num_template_parameter_lists;
12552 return type;
12555 /* Parse a class-head.
12557 class-head:
12558 class-key identifier [opt] base-clause [opt]
12559 class-key nested-name-specifier identifier base-clause [opt]
12560 class-key nested-name-specifier [opt] template-id
12561 base-clause [opt]
12563 GNU Extensions:
12564 class-key attributes identifier [opt] base-clause [opt]
12565 class-key attributes nested-name-specifier identifier base-clause [opt]
12566 class-key attributes nested-name-specifier [opt] template-id
12567 base-clause [opt]
12569 Returns the TYPE of the indicated class. Sets
12570 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12571 involving a nested-name-specifier was used, and FALSE otherwise.
12573 Returns error_mark_node if this is not a class-head.
12575 Returns NULL_TREE if the class-head is syntactically valid, but
12576 semantically invalid in a way that means we should skip the entire
12577 body of the class. */
12579 static tree
12580 cp_parser_class_head (cp_parser* parser,
12581 bool* nested_name_specifier_p,
12582 tree *attributes_p)
12584 tree nested_name_specifier;
12585 enum tag_types class_key;
12586 tree id = NULL_TREE;
12587 tree type = NULL_TREE;
12588 tree attributes;
12589 bool template_id_p = false;
12590 bool qualified_p = false;
12591 bool invalid_nested_name_p = false;
12592 bool invalid_explicit_specialization_p = false;
12593 tree pushed_scope = NULL_TREE;
12594 unsigned num_templates;
12595 tree bases;
12597 /* Assume no nested-name-specifier will be present. */
12598 *nested_name_specifier_p = false;
12599 /* Assume no template parameter lists will be used in defining the
12600 type. */
12601 num_templates = 0;
12603 /* Look for the class-key. */
12604 class_key = cp_parser_class_key (parser);
12605 if (class_key == none_type)
12606 return error_mark_node;
12608 /* Parse the attributes. */
12609 attributes = cp_parser_attributes_opt (parser);
12611 /* If the next token is `::', that is invalid -- but sometimes
12612 people do try to write:
12614 struct ::S {};
12616 Handle this gracefully by accepting the extra qualifier, and then
12617 issuing an error about it later if this really is a
12618 class-head. If it turns out just to be an elaborated type
12619 specifier, remain silent. */
12620 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12621 qualified_p = true;
12623 push_deferring_access_checks (dk_no_check);
12625 /* Determine the name of the class. Begin by looking for an
12626 optional nested-name-specifier. */
12627 nested_name_specifier
12628 = cp_parser_nested_name_specifier_opt (parser,
12629 /*typename_keyword_p=*/false,
12630 /*check_dependency_p=*/false,
12631 /*type_p=*/false,
12632 /*is_declaration=*/false);
12633 /* If there was a nested-name-specifier, then there *must* be an
12634 identifier. */
12635 if (nested_name_specifier)
12637 /* Although the grammar says `identifier', it really means
12638 `class-name' or `template-name'. You are only allowed to
12639 define a class that has already been declared with this
12640 syntax.
12642 The proposed resolution for Core Issue 180 says that whever
12643 you see `class T::X' you should treat `X' as a type-name.
12645 It is OK to define an inaccessible class; for example:
12647 class A { class B; };
12648 class A::B {};
12650 We do not know if we will see a class-name, or a
12651 template-name. We look for a class-name first, in case the
12652 class-name is a template-id; if we looked for the
12653 template-name first we would stop after the template-name. */
12654 cp_parser_parse_tentatively (parser);
12655 type = cp_parser_class_name (parser,
12656 /*typename_keyword_p=*/false,
12657 /*template_keyword_p=*/false,
12658 class_type,
12659 /*check_dependency_p=*/false,
12660 /*class_head_p=*/true,
12661 /*is_declaration=*/false);
12662 /* If that didn't work, ignore the nested-name-specifier. */
12663 if (!cp_parser_parse_definitely (parser))
12665 invalid_nested_name_p = true;
12666 id = cp_parser_identifier (parser);
12667 if (id == error_mark_node)
12668 id = NULL_TREE;
12670 /* If we could not find a corresponding TYPE, treat this
12671 declaration like an unqualified declaration. */
12672 if (type == error_mark_node)
12673 nested_name_specifier = NULL_TREE;
12674 /* Otherwise, count the number of templates used in TYPE and its
12675 containing scopes. */
12676 else
12678 tree scope;
12680 for (scope = TREE_TYPE (type);
12681 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12682 scope = (TYPE_P (scope)
12683 ? TYPE_CONTEXT (scope)
12684 : DECL_CONTEXT (scope)))
12685 if (TYPE_P (scope)
12686 && CLASS_TYPE_P (scope)
12687 && CLASSTYPE_TEMPLATE_INFO (scope)
12688 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12689 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12690 ++num_templates;
12693 /* Otherwise, the identifier is optional. */
12694 else
12696 /* We don't know whether what comes next is a template-id,
12697 an identifier, or nothing at all. */
12698 cp_parser_parse_tentatively (parser);
12699 /* Check for a template-id. */
12700 id = cp_parser_template_id (parser,
12701 /*template_keyword_p=*/false,
12702 /*check_dependency_p=*/true,
12703 /*is_declaration=*/true);
12704 /* If that didn't work, it could still be an identifier. */
12705 if (!cp_parser_parse_definitely (parser))
12707 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12708 id = cp_parser_identifier (parser);
12709 else
12710 id = NULL_TREE;
12712 else
12714 template_id_p = true;
12715 ++num_templates;
12719 pop_deferring_access_checks ();
12721 if (id)
12722 cp_parser_check_for_invalid_template_id (parser, id);
12724 /* If it's not a `:' or a `{' then we can't really be looking at a
12725 class-head, since a class-head only appears as part of a
12726 class-specifier. We have to detect this situation before calling
12727 xref_tag, since that has irreversible side-effects. */
12728 if (!cp_parser_next_token_starts_class_definition_p (parser))
12730 cp_parser_error (parser, "expected %<{%> or %<:%>");
12731 return error_mark_node;
12734 /* At this point, we're going ahead with the class-specifier, even
12735 if some other problem occurs. */
12736 cp_parser_commit_to_tentative_parse (parser);
12737 /* Issue the error about the overly-qualified name now. */
12738 if (qualified_p)
12739 cp_parser_error (parser,
12740 "global qualification of class name is invalid");
12741 else if (invalid_nested_name_p)
12742 cp_parser_error (parser,
12743 "qualified name does not name a class");
12744 else if (nested_name_specifier)
12746 tree scope;
12748 /* Reject typedef-names in class heads. */
12749 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12751 error ("invalid class name in declaration of %qD", type);
12752 type = NULL_TREE;
12753 goto done;
12756 /* Figure out in what scope the declaration is being placed. */
12757 scope = current_scope ();
12758 /* If that scope does not contain the scope in which the
12759 class was originally declared, the program is invalid. */
12760 if (scope && !is_ancestor (scope, nested_name_specifier))
12762 error ("declaration of %qD in %qD which does not enclose %qD",
12763 type, scope, nested_name_specifier);
12764 type = NULL_TREE;
12765 goto done;
12767 /* [dcl.meaning]
12769 A declarator-id shall not be qualified exception of the
12770 definition of a ... nested class outside of its class
12771 ... [or] a the definition or explicit instantiation of a
12772 class member of a namespace outside of its namespace. */
12773 if (scope == nested_name_specifier)
12775 pedwarn ("extra qualification ignored");
12776 nested_name_specifier = NULL_TREE;
12777 num_templates = 0;
12780 /* An explicit-specialization must be preceded by "template <>". If
12781 it is not, try to recover gracefully. */
12782 if (at_namespace_scope_p ()
12783 && parser->num_template_parameter_lists == 0
12784 && template_id_p)
12786 error ("an explicit specialization must be preceded by %<template <>%>");
12787 invalid_explicit_specialization_p = true;
12788 /* Take the same action that would have been taken by
12789 cp_parser_explicit_specialization. */
12790 ++parser->num_template_parameter_lists;
12791 begin_specialization ();
12793 /* There must be no "return" statements between this point and the
12794 end of this function; set "type "to the correct return value and
12795 use "goto done;" to return. */
12796 /* Make sure that the right number of template parameters were
12797 present. */
12798 if (!cp_parser_check_template_parameters (parser, num_templates))
12800 /* If something went wrong, there is no point in even trying to
12801 process the class-definition. */
12802 type = NULL_TREE;
12803 goto done;
12806 /* Look up the type. */
12807 if (template_id_p)
12809 type = TREE_TYPE (id);
12810 maybe_process_partial_specialization (type);
12811 if (nested_name_specifier)
12812 pushed_scope = push_scope (nested_name_specifier);
12814 else if (nested_name_specifier)
12816 tree class_type;
12818 /* Given:
12820 template <typename T> struct S { struct T };
12821 template <typename T> struct S<T>::T { };
12823 we will get a TYPENAME_TYPE when processing the definition of
12824 `S::T'. We need to resolve it to the actual type before we
12825 try to define it. */
12826 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12828 class_type = resolve_typename_type (TREE_TYPE (type),
12829 /*only_current_p=*/false);
12830 if (class_type != error_mark_node)
12831 type = TYPE_NAME (class_type);
12832 else
12834 cp_parser_error (parser, "could not resolve typename type");
12835 type = error_mark_node;
12839 maybe_process_partial_specialization (TREE_TYPE (type));
12840 class_type = current_class_type;
12841 /* Enter the scope indicated by the nested-name-specifier. */
12842 pushed_scope = push_scope (nested_name_specifier);
12843 /* Get the canonical version of this type. */
12844 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12845 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12846 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12848 type = push_template_decl (type);
12849 if (type == error_mark_node)
12851 type = NULL_TREE;
12852 goto done;
12856 type = TREE_TYPE (type);
12857 *nested_name_specifier_p = true;
12859 else /* The name is not a nested name. */
12861 /* If the class was unnamed, create a dummy name. */
12862 if (!id)
12863 id = make_anon_name ();
12864 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12865 parser->num_template_parameter_lists);
12868 /* Indicate whether this class was declared as a `class' or as a
12869 `struct'. */
12870 if (TREE_CODE (type) == RECORD_TYPE)
12871 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12872 cp_parser_check_class_key (class_key, type);
12874 /* If this type was already complete, and we see another definition,
12875 that's an error. */
12876 if (type != error_mark_node && COMPLETE_TYPE_P (type))
12878 error ("redefinition of %q#T", type);
12879 cp_error_at ("previous definition of %q#T", type);
12880 type = NULL_TREE;
12881 goto done;
12884 /* We will have entered the scope containing the class; the names of
12885 base classes should be looked up in that context. For example:
12887 struct A { struct B {}; struct C; };
12888 struct A::C : B {};
12890 is valid. */
12891 bases = NULL_TREE;
12893 /* Get the list of base-classes, if there is one. */
12894 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12895 bases = cp_parser_base_clause (parser);
12897 /* Process the base classes. */
12898 xref_basetypes (type, bases);
12900 done:
12901 /* Leave the scope given by the nested-name-specifier. We will
12902 enter the class scope itself while processing the members. */
12903 if (pushed_scope)
12904 pop_scope (pushed_scope);
12906 if (invalid_explicit_specialization_p)
12908 end_specialization ();
12909 --parser->num_template_parameter_lists;
12911 *attributes_p = attributes;
12912 return type;
12915 /* Parse a class-key.
12917 class-key:
12918 class
12919 struct
12920 union
12922 Returns the kind of class-key specified, or none_type to indicate
12923 error. */
12925 static enum tag_types
12926 cp_parser_class_key (cp_parser* parser)
12928 cp_token *token;
12929 enum tag_types tag_type;
12931 /* Look for the class-key. */
12932 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12933 if (!token)
12934 return none_type;
12936 /* Check to see if the TOKEN is a class-key. */
12937 tag_type = cp_parser_token_is_class_key (token);
12938 if (!tag_type)
12939 cp_parser_error (parser, "expected class-key");
12940 return tag_type;
12943 /* Parse an (optional) member-specification.
12945 member-specification:
12946 member-declaration member-specification [opt]
12947 access-specifier : member-specification [opt] */
12949 static void
12950 cp_parser_member_specification_opt (cp_parser* parser)
12952 while (true)
12954 cp_token *token;
12955 enum rid keyword;
12957 /* Peek at the next token. */
12958 token = cp_lexer_peek_token (parser->lexer);
12959 /* If it's a `}', or EOF then we've seen all the members. */
12960 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12961 break;
12963 /* See if this token is a keyword. */
12964 keyword = token->keyword;
12965 switch (keyword)
12967 case RID_PUBLIC:
12968 case RID_PROTECTED:
12969 case RID_PRIVATE:
12970 /* Consume the access-specifier. */
12971 cp_lexer_consume_token (parser->lexer);
12972 /* Remember which access-specifier is active. */
12973 current_access_specifier = token->value;
12974 /* Look for the `:'. */
12975 cp_parser_require (parser, CPP_COLON, "`:'");
12976 break;
12978 default:
12979 /* Accept #pragmas at class scope. */
12980 if (token->type == CPP_PRAGMA)
12982 cp_lexer_handle_pragma (parser->lexer);
12983 break;
12986 /* Otherwise, the next construction must be a
12987 member-declaration. */
12988 cp_parser_member_declaration (parser);
12993 /* Parse a member-declaration.
12995 member-declaration:
12996 decl-specifier-seq [opt] member-declarator-list [opt] ;
12997 function-definition ; [opt]
12998 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12999 using-declaration
13000 template-declaration
13002 member-declarator-list:
13003 member-declarator
13004 member-declarator-list , member-declarator
13006 member-declarator:
13007 declarator pure-specifier [opt]
13008 declarator constant-initializer [opt]
13009 identifier [opt] : constant-expression
13011 GNU Extensions:
13013 member-declaration:
13014 __extension__ member-declaration
13016 member-declarator:
13017 declarator attributes [opt] pure-specifier [opt]
13018 declarator attributes [opt] constant-initializer [opt]
13019 identifier [opt] attributes [opt] : constant-expression */
13021 static void
13022 cp_parser_member_declaration (cp_parser* parser)
13024 cp_decl_specifier_seq decl_specifiers;
13025 tree prefix_attributes;
13026 tree decl;
13027 int declares_class_or_enum;
13028 bool friend_p;
13029 cp_token *token;
13030 int saved_pedantic;
13032 /* Check for the `__extension__' keyword. */
13033 if (cp_parser_extension_opt (parser, &saved_pedantic))
13035 /* Recurse. */
13036 cp_parser_member_declaration (parser);
13037 /* Restore the old value of the PEDANTIC flag. */
13038 pedantic = saved_pedantic;
13040 return;
13043 /* Check for a template-declaration. */
13044 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13046 /* Parse the template-declaration. */
13047 cp_parser_template_declaration (parser, /*member_p=*/true);
13049 return;
13052 /* Check for a using-declaration. */
13053 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13055 /* Parse the using-declaration. */
13056 cp_parser_using_declaration (parser);
13058 return;
13061 /* Parse the decl-specifier-seq. */
13062 cp_parser_decl_specifier_seq (parser,
13063 CP_PARSER_FLAGS_OPTIONAL,
13064 &decl_specifiers,
13065 &declares_class_or_enum);
13066 prefix_attributes = decl_specifiers.attributes;
13067 decl_specifiers.attributes = NULL_TREE;
13068 /* Check for an invalid type-name. */
13069 if (!decl_specifiers.type
13070 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13071 return;
13072 /* If there is no declarator, then the decl-specifier-seq should
13073 specify a type. */
13074 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13076 /* If there was no decl-specifier-seq, and the next token is a
13077 `;', then we have something like:
13079 struct S { ; };
13081 [class.mem]
13083 Each member-declaration shall declare at least one member
13084 name of the class. */
13085 if (!decl_specifiers.any_specifiers_p)
13087 cp_token *token = cp_lexer_peek_token (parser->lexer);
13088 if (pedantic && !token->in_system_header)
13089 pedwarn ("%Hextra %<;%>", &token->location);
13091 else
13093 tree type;
13095 /* See if this declaration is a friend. */
13096 friend_p = cp_parser_friend_p (&decl_specifiers);
13097 /* If there were decl-specifiers, check to see if there was
13098 a class-declaration. */
13099 type = check_tag_decl (&decl_specifiers);
13100 /* Nested classes have already been added to the class, but
13101 a `friend' needs to be explicitly registered. */
13102 if (friend_p)
13104 /* If the `friend' keyword was present, the friend must
13105 be introduced with a class-key. */
13106 if (!declares_class_or_enum)
13107 error ("a class-key must be used when declaring a friend");
13108 /* In this case:
13110 template <typename T> struct A {
13111 friend struct A<T>::B;
13114 A<T>::B will be represented by a TYPENAME_TYPE, and
13115 therefore not recognized by check_tag_decl. */
13116 if (!type
13117 && decl_specifiers.type
13118 && TYPE_P (decl_specifiers.type))
13119 type = decl_specifiers.type;
13120 if (!type || !TYPE_P (type))
13121 error ("friend declaration does not name a class or "
13122 "function");
13123 else
13124 make_friend_class (current_class_type, type,
13125 /*complain=*/true);
13127 /* If there is no TYPE, an error message will already have
13128 been issued. */
13129 else if (!type || type == error_mark_node)
13131 /* An anonymous aggregate has to be handled specially; such
13132 a declaration really declares a data member (with a
13133 particular type), as opposed to a nested class. */
13134 else if (ANON_AGGR_TYPE_P (type))
13136 /* Remove constructors and such from TYPE, now that we
13137 know it is an anonymous aggregate. */
13138 fixup_anonymous_aggr (type);
13139 /* And make the corresponding data member. */
13140 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13141 /* Add it to the class. */
13142 finish_member_declaration (decl);
13144 else
13145 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13148 else
13150 /* See if these declarations will be friends. */
13151 friend_p = cp_parser_friend_p (&decl_specifiers);
13153 /* Keep going until we hit the `;' at the end of the
13154 declaration. */
13155 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13157 tree attributes = NULL_TREE;
13158 tree first_attribute;
13160 /* Peek at the next token. */
13161 token = cp_lexer_peek_token (parser->lexer);
13163 /* Check for a bitfield declaration. */
13164 if (token->type == CPP_COLON
13165 || (token->type == CPP_NAME
13166 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13167 == CPP_COLON))
13169 tree identifier;
13170 tree width;
13172 /* Get the name of the bitfield. Note that we cannot just
13173 check TOKEN here because it may have been invalidated by
13174 the call to cp_lexer_peek_nth_token above. */
13175 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13176 identifier = cp_parser_identifier (parser);
13177 else
13178 identifier = NULL_TREE;
13180 /* Consume the `:' token. */
13181 cp_lexer_consume_token (parser->lexer);
13182 /* Get the width of the bitfield. */
13183 width
13184 = cp_parser_constant_expression (parser,
13185 /*allow_non_constant=*/false,
13186 NULL);
13188 /* Look for attributes that apply to the bitfield. */
13189 attributes = cp_parser_attributes_opt (parser);
13190 /* Remember which attributes are prefix attributes and
13191 which are not. */
13192 first_attribute = attributes;
13193 /* Combine the attributes. */
13194 attributes = chainon (prefix_attributes, attributes);
13196 /* Create the bitfield declaration. */
13197 decl = grokbitfield (identifier
13198 ? make_id_declarator (NULL_TREE,
13199 identifier)
13200 : NULL,
13201 &decl_specifiers,
13202 width);
13203 /* Apply the attributes. */
13204 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13206 else
13208 cp_declarator *declarator;
13209 tree initializer;
13210 tree asm_specification;
13211 int ctor_dtor_or_conv_p;
13213 /* Parse the declarator. */
13214 declarator
13215 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13216 &ctor_dtor_or_conv_p,
13217 /*parenthesized_p=*/NULL,
13218 /*member_p=*/true);
13220 /* If something went wrong parsing the declarator, make sure
13221 that we at least consume some tokens. */
13222 if (declarator == cp_error_declarator)
13224 /* Skip to the end of the statement. */
13225 cp_parser_skip_to_end_of_statement (parser);
13226 /* If the next token is not a semicolon, that is
13227 probably because we just skipped over the body of
13228 a function. So, we consume a semicolon if
13229 present, but do not issue an error message if it
13230 is not present. */
13231 if (cp_lexer_next_token_is (parser->lexer,
13232 CPP_SEMICOLON))
13233 cp_lexer_consume_token (parser->lexer);
13234 return;
13237 if (declares_class_or_enum & 2)
13238 cp_parser_check_for_definition_in_return_type
13239 (declarator, decl_specifiers.type);
13241 /* Look for an asm-specification. */
13242 asm_specification = cp_parser_asm_specification_opt (parser);
13243 /* Look for attributes that apply to the declaration. */
13244 attributes = cp_parser_attributes_opt (parser);
13245 /* Remember which attributes are prefix attributes and
13246 which are not. */
13247 first_attribute = attributes;
13248 /* Combine the attributes. */
13249 attributes = chainon (prefix_attributes, attributes);
13251 /* If it's an `=', then we have a constant-initializer or a
13252 pure-specifier. It is not correct to parse the
13253 initializer before registering the member declaration
13254 since the member declaration should be in scope while
13255 its initializer is processed. However, the rest of the
13256 front end does not yet provide an interface that allows
13257 us to handle this correctly. */
13258 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13260 /* In [class.mem]:
13262 A pure-specifier shall be used only in the declaration of
13263 a virtual function.
13265 A member-declarator can contain a constant-initializer
13266 only if it declares a static member of integral or
13267 enumeration type.
13269 Therefore, if the DECLARATOR is for a function, we look
13270 for a pure-specifier; otherwise, we look for a
13271 constant-initializer. When we call `grokfield', it will
13272 perform more stringent semantics checks. */
13273 if (declarator->kind == cdk_function)
13274 initializer = cp_parser_pure_specifier (parser);
13275 else
13276 /* Parse the initializer. */
13277 initializer = cp_parser_constant_initializer (parser);
13279 /* Otherwise, there is no initializer. */
13280 else
13281 initializer = NULL_TREE;
13283 /* See if we are probably looking at a function
13284 definition. We are certainly not looking at a
13285 member-declarator. Calling `grokfield' has
13286 side-effects, so we must not do it unless we are sure
13287 that we are looking at a member-declarator. */
13288 if (cp_parser_token_starts_function_definition_p
13289 (cp_lexer_peek_token (parser->lexer)))
13291 /* The grammar does not allow a pure-specifier to be
13292 used when a member function is defined. (It is
13293 possible that this fact is an oversight in the
13294 standard, since a pure function may be defined
13295 outside of the class-specifier. */
13296 if (initializer)
13297 error ("pure-specifier on function-definition");
13298 decl = cp_parser_save_member_function_body (parser,
13299 &decl_specifiers,
13300 declarator,
13301 attributes);
13302 /* If the member was not a friend, declare it here. */
13303 if (!friend_p)
13304 finish_member_declaration (decl);
13305 /* Peek at the next token. */
13306 token = cp_lexer_peek_token (parser->lexer);
13307 /* If the next token is a semicolon, consume it. */
13308 if (token->type == CPP_SEMICOLON)
13309 cp_lexer_consume_token (parser->lexer);
13310 return;
13312 else
13314 /* Create the declaration. */
13315 decl = grokfield (declarator, &decl_specifiers,
13316 initializer, asm_specification,
13317 attributes);
13318 /* Any initialization must have been from a
13319 constant-expression. */
13320 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13321 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13325 /* Reset PREFIX_ATTRIBUTES. */
13326 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13327 attributes = TREE_CHAIN (attributes);
13328 if (attributes)
13329 TREE_CHAIN (attributes) = NULL_TREE;
13331 /* If there is any qualification still in effect, clear it
13332 now; we will be starting fresh with the next declarator. */
13333 parser->scope = NULL_TREE;
13334 parser->qualifying_scope = NULL_TREE;
13335 parser->object_scope = NULL_TREE;
13336 /* If it's a `,', then there are more declarators. */
13337 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13338 cp_lexer_consume_token (parser->lexer);
13339 /* If the next token isn't a `;', then we have a parse error. */
13340 else if (cp_lexer_next_token_is_not (parser->lexer,
13341 CPP_SEMICOLON))
13343 cp_parser_error (parser, "expected %<;%>");
13344 /* Skip tokens until we find a `;'. */
13345 cp_parser_skip_to_end_of_statement (parser);
13347 break;
13350 if (decl)
13352 /* Add DECL to the list of members. */
13353 if (!friend_p)
13354 finish_member_declaration (decl);
13356 if (TREE_CODE (decl) == FUNCTION_DECL)
13357 cp_parser_save_default_args (parser, decl);
13362 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13365 /* Parse a pure-specifier.
13367 pure-specifier:
13370 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13371 Otherwise, ERROR_MARK_NODE is returned. */
13373 static tree
13374 cp_parser_pure_specifier (cp_parser* parser)
13376 cp_token *token;
13378 /* Look for the `=' token. */
13379 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13380 return error_mark_node;
13381 /* Look for the `0' token. */
13382 token = cp_lexer_consume_token (parser->lexer);
13383 if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13385 cp_parser_error (parser,
13386 "invalid pure specifier (only `= 0' is allowed)");
13387 cp_parser_skip_to_end_of_statement (parser);
13388 return error_mark_node;
13391 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13392 We need to get information from the lexer about how the number
13393 was spelled in order to fix this problem. */
13394 return integer_zero_node;
13397 /* Parse a constant-initializer.
13399 constant-initializer:
13400 = constant-expression
13402 Returns a representation of the constant-expression. */
13404 static tree
13405 cp_parser_constant_initializer (cp_parser* parser)
13407 /* Look for the `=' token. */
13408 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13409 return error_mark_node;
13411 /* It is invalid to write:
13413 struct S { static const int i = { 7 }; };
13416 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13418 cp_parser_error (parser,
13419 "a brace-enclosed initializer is not allowed here");
13420 /* Consume the opening brace. */
13421 cp_lexer_consume_token (parser->lexer);
13422 /* Skip the initializer. */
13423 cp_parser_skip_to_closing_brace (parser);
13424 /* Look for the trailing `}'. */
13425 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13427 return error_mark_node;
13430 return cp_parser_constant_expression (parser,
13431 /*allow_non_constant=*/false,
13432 NULL);
13435 /* Derived classes [gram.class.derived] */
13437 /* Parse a base-clause.
13439 base-clause:
13440 : base-specifier-list
13442 base-specifier-list:
13443 base-specifier
13444 base-specifier-list , base-specifier
13446 Returns a TREE_LIST representing the base-classes, in the order in
13447 which they were declared. The representation of each node is as
13448 described by cp_parser_base_specifier.
13450 In the case that no bases are specified, this function will return
13451 NULL_TREE, not ERROR_MARK_NODE. */
13453 static tree
13454 cp_parser_base_clause (cp_parser* parser)
13456 tree bases = NULL_TREE;
13458 /* Look for the `:' that begins the list. */
13459 cp_parser_require (parser, CPP_COLON, "`:'");
13461 /* Scan the base-specifier-list. */
13462 while (true)
13464 cp_token *token;
13465 tree base;
13467 /* Look for the base-specifier. */
13468 base = cp_parser_base_specifier (parser);
13469 /* Add BASE to the front of the list. */
13470 if (base != error_mark_node)
13472 TREE_CHAIN (base) = bases;
13473 bases = base;
13475 /* Peek at the next token. */
13476 token = cp_lexer_peek_token (parser->lexer);
13477 /* If it's not a comma, then the list is complete. */
13478 if (token->type != CPP_COMMA)
13479 break;
13480 /* Consume the `,'. */
13481 cp_lexer_consume_token (parser->lexer);
13484 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13485 base class had a qualified name. However, the next name that
13486 appears is certainly not qualified. */
13487 parser->scope = NULL_TREE;
13488 parser->qualifying_scope = NULL_TREE;
13489 parser->object_scope = NULL_TREE;
13491 return nreverse (bases);
13494 /* Parse a base-specifier.
13496 base-specifier:
13497 :: [opt] nested-name-specifier [opt] class-name
13498 virtual access-specifier [opt] :: [opt] nested-name-specifier
13499 [opt] class-name
13500 access-specifier virtual [opt] :: [opt] nested-name-specifier
13501 [opt] class-name
13503 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13504 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13505 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13506 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13508 static tree
13509 cp_parser_base_specifier (cp_parser* parser)
13511 cp_token *token;
13512 bool done = false;
13513 bool virtual_p = false;
13514 bool duplicate_virtual_error_issued_p = false;
13515 bool duplicate_access_error_issued_p = false;
13516 bool class_scope_p, template_p;
13517 tree access = access_default_node;
13518 tree type;
13520 /* Process the optional `virtual' and `access-specifier'. */
13521 while (!done)
13523 /* Peek at the next token. */
13524 token = cp_lexer_peek_token (parser->lexer);
13525 /* Process `virtual'. */
13526 switch (token->keyword)
13528 case RID_VIRTUAL:
13529 /* If `virtual' appears more than once, issue an error. */
13530 if (virtual_p && !duplicate_virtual_error_issued_p)
13532 cp_parser_error (parser,
13533 "%<virtual%> specified more than once in base-specified");
13534 duplicate_virtual_error_issued_p = true;
13537 virtual_p = true;
13539 /* Consume the `virtual' token. */
13540 cp_lexer_consume_token (parser->lexer);
13542 break;
13544 case RID_PUBLIC:
13545 case RID_PROTECTED:
13546 case RID_PRIVATE:
13547 /* If more than one access specifier appears, issue an
13548 error. */
13549 if (access != access_default_node
13550 && !duplicate_access_error_issued_p)
13552 cp_parser_error (parser,
13553 "more than one access specifier in base-specified");
13554 duplicate_access_error_issued_p = true;
13557 access = ridpointers[(int) token->keyword];
13559 /* Consume the access-specifier. */
13560 cp_lexer_consume_token (parser->lexer);
13562 break;
13564 default:
13565 done = true;
13566 break;
13569 /* It is not uncommon to see programs mechanically, erroneously, use
13570 the 'typename' keyword to denote (dependent) qualified types
13571 as base classes. */
13572 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13574 if (!processing_template_decl)
13575 error ("keyword %<typename%> not allowed outside of templates");
13576 else
13577 error ("keyword %<typename%> not allowed in this context "
13578 "(the base class is implicitly a type)");
13579 cp_lexer_consume_token (parser->lexer);
13582 /* Look for the optional `::' operator. */
13583 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13584 /* Look for the nested-name-specifier. The simplest way to
13585 implement:
13587 [temp.res]
13589 The keyword `typename' is not permitted in a base-specifier or
13590 mem-initializer; in these contexts a qualified name that
13591 depends on a template-parameter is implicitly assumed to be a
13592 type name.
13594 is to pretend that we have seen the `typename' keyword at this
13595 point. */
13596 cp_parser_nested_name_specifier_opt (parser,
13597 /*typename_keyword_p=*/true,
13598 /*check_dependency_p=*/true,
13599 typename_type,
13600 /*is_declaration=*/true);
13601 /* If the base class is given by a qualified name, assume that names
13602 we see are type names or templates, as appropriate. */
13603 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13604 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13606 /* Finally, look for the class-name. */
13607 type = cp_parser_class_name (parser,
13608 class_scope_p,
13609 template_p,
13610 typename_type,
13611 /*check_dependency_p=*/true,
13612 /*class_head_p=*/false,
13613 /*is_declaration=*/true);
13615 if (type == error_mark_node)
13616 return error_mark_node;
13618 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13621 /* Exception handling [gram.exception] */
13623 /* Parse an (optional) exception-specification.
13625 exception-specification:
13626 throw ( type-id-list [opt] )
13628 Returns a TREE_LIST representing the exception-specification. The
13629 TREE_VALUE of each node is a type. */
13631 static tree
13632 cp_parser_exception_specification_opt (cp_parser* parser)
13634 cp_token *token;
13635 tree type_id_list;
13637 /* Peek at the next token. */
13638 token = cp_lexer_peek_token (parser->lexer);
13639 /* If it's not `throw', then there's no exception-specification. */
13640 if (!cp_parser_is_keyword (token, RID_THROW))
13641 return NULL_TREE;
13643 /* Consume the `throw'. */
13644 cp_lexer_consume_token (parser->lexer);
13646 /* Look for the `('. */
13647 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13649 /* Peek at the next token. */
13650 token = cp_lexer_peek_token (parser->lexer);
13651 /* If it's not a `)', then there is a type-id-list. */
13652 if (token->type != CPP_CLOSE_PAREN)
13654 const char *saved_message;
13656 /* Types may not be defined in an exception-specification. */
13657 saved_message = parser->type_definition_forbidden_message;
13658 parser->type_definition_forbidden_message
13659 = "types may not be defined in an exception-specification";
13660 /* Parse the type-id-list. */
13661 type_id_list = cp_parser_type_id_list (parser);
13662 /* Restore the saved message. */
13663 parser->type_definition_forbidden_message = saved_message;
13665 else
13666 type_id_list = empty_except_spec;
13668 /* Look for the `)'. */
13669 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13671 return type_id_list;
13674 /* Parse an (optional) type-id-list.
13676 type-id-list:
13677 type-id
13678 type-id-list , type-id
13680 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13681 in the order that the types were presented. */
13683 static tree
13684 cp_parser_type_id_list (cp_parser* parser)
13686 tree types = NULL_TREE;
13688 while (true)
13690 cp_token *token;
13691 tree type;
13693 /* Get the next type-id. */
13694 type = cp_parser_type_id (parser);
13695 /* Add it to the list. */
13696 types = add_exception_specifier (types, type, /*complain=*/1);
13697 /* Peek at the next token. */
13698 token = cp_lexer_peek_token (parser->lexer);
13699 /* If it is not a `,', we are done. */
13700 if (token->type != CPP_COMMA)
13701 break;
13702 /* Consume the `,'. */
13703 cp_lexer_consume_token (parser->lexer);
13706 return nreverse (types);
13709 /* Parse a try-block.
13711 try-block:
13712 try compound-statement handler-seq */
13714 static tree
13715 cp_parser_try_block (cp_parser* parser)
13717 tree try_block;
13719 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13720 try_block = begin_try_block ();
13721 cp_parser_compound_statement (parser, NULL, true);
13722 finish_try_block (try_block);
13723 cp_parser_handler_seq (parser);
13724 finish_handler_sequence (try_block);
13726 return try_block;
13729 /* Parse a function-try-block.
13731 function-try-block:
13732 try ctor-initializer [opt] function-body handler-seq */
13734 static bool
13735 cp_parser_function_try_block (cp_parser* parser)
13737 tree try_block;
13738 bool ctor_initializer_p;
13740 /* Look for the `try' keyword. */
13741 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13742 return false;
13743 /* Let the rest of the front-end know where we are. */
13744 try_block = begin_function_try_block ();
13745 /* Parse the function-body. */
13746 ctor_initializer_p
13747 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13748 /* We're done with the `try' part. */
13749 finish_function_try_block (try_block);
13750 /* Parse the handlers. */
13751 cp_parser_handler_seq (parser);
13752 /* We're done with the handlers. */
13753 finish_function_handler_sequence (try_block);
13755 return ctor_initializer_p;
13758 /* Parse a handler-seq.
13760 handler-seq:
13761 handler handler-seq [opt] */
13763 static void
13764 cp_parser_handler_seq (cp_parser* parser)
13766 while (true)
13768 cp_token *token;
13770 /* Parse the handler. */
13771 cp_parser_handler (parser);
13772 /* Peek at the next token. */
13773 token = cp_lexer_peek_token (parser->lexer);
13774 /* If it's not `catch' then there are no more handlers. */
13775 if (!cp_parser_is_keyword (token, RID_CATCH))
13776 break;
13780 /* Parse a handler.
13782 handler:
13783 catch ( exception-declaration ) compound-statement */
13785 static void
13786 cp_parser_handler (cp_parser* parser)
13788 tree handler;
13789 tree declaration;
13791 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13792 handler = begin_handler ();
13793 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13794 declaration = cp_parser_exception_declaration (parser);
13795 finish_handler_parms (declaration, handler);
13796 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13797 cp_parser_compound_statement (parser, NULL, false);
13798 finish_handler (handler);
13801 /* Parse an exception-declaration.
13803 exception-declaration:
13804 type-specifier-seq declarator
13805 type-specifier-seq abstract-declarator
13806 type-specifier-seq
13809 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13810 ellipsis variant is used. */
13812 static tree
13813 cp_parser_exception_declaration (cp_parser* parser)
13815 tree decl;
13816 cp_decl_specifier_seq type_specifiers;
13817 cp_declarator *declarator;
13818 const char *saved_message;
13820 /* If it's an ellipsis, it's easy to handle. */
13821 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13823 /* Consume the `...' token. */
13824 cp_lexer_consume_token (parser->lexer);
13825 return NULL_TREE;
13828 /* Types may not be defined in exception-declarations. */
13829 saved_message = parser->type_definition_forbidden_message;
13830 parser->type_definition_forbidden_message
13831 = "types may not be defined in exception-declarations";
13833 /* Parse the type-specifier-seq. */
13834 cp_parser_type_specifier_seq (parser, &type_specifiers);
13835 /* If it's a `)', then there is no declarator. */
13836 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13837 declarator = NULL;
13838 else
13839 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13840 /*ctor_dtor_or_conv_p=*/NULL,
13841 /*parenthesized_p=*/NULL,
13842 /*member_p=*/false);
13844 /* Restore the saved message. */
13845 parser->type_definition_forbidden_message = saved_message;
13847 if (type_specifiers.any_specifiers_p)
13849 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13850 if (decl == NULL_TREE)
13851 error ("invalid catch parameter");
13853 else
13854 decl = NULL_TREE;
13856 return decl;
13859 /* Parse a throw-expression.
13861 throw-expression:
13862 throw assignment-expression [opt]
13864 Returns a THROW_EXPR representing the throw-expression. */
13866 static tree
13867 cp_parser_throw_expression (cp_parser* parser)
13869 tree expression;
13870 cp_token* token;
13872 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13873 token = cp_lexer_peek_token (parser->lexer);
13874 /* Figure out whether or not there is an assignment-expression
13875 following the "throw" keyword. */
13876 if (token->type == CPP_COMMA
13877 || token->type == CPP_SEMICOLON
13878 || token->type == CPP_CLOSE_PAREN
13879 || token->type == CPP_CLOSE_SQUARE
13880 || token->type == CPP_CLOSE_BRACE
13881 || token->type == CPP_COLON)
13882 expression = NULL_TREE;
13883 else
13884 expression = cp_parser_assignment_expression (parser,
13885 /*cast_p=*/false);
13887 return build_throw (expression);
13890 /* GNU Extensions */
13892 /* Parse an (optional) asm-specification.
13894 asm-specification:
13895 asm ( string-literal )
13897 If the asm-specification is present, returns a STRING_CST
13898 corresponding to the string-literal. Otherwise, returns
13899 NULL_TREE. */
13901 static tree
13902 cp_parser_asm_specification_opt (cp_parser* parser)
13904 cp_token *token;
13905 tree asm_specification;
13907 /* Peek at the next token. */
13908 token = cp_lexer_peek_token (parser->lexer);
13909 /* If the next token isn't the `asm' keyword, then there's no
13910 asm-specification. */
13911 if (!cp_parser_is_keyword (token, RID_ASM))
13912 return NULL_TREE;
13914 /* Consume the `asm' token. */
13915 cp_lexer_consume_token (parser->lexer);
13916 /* Look for the `('. */
13917 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13919 /* Look for the string-literal. */
13920 asm_specification = cp_parser_string_literal (parser, false, false);
13922 /* Look for the `)'. */
13923 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13925 return asm_specification;
13928 /* Parse an asm-operand-list.
13930 asm-operand-list:
13931 asm-operand
13932 asm-operand-list , asm-operand
13934 asm-operand:
13935 string-literal ( expression )
13936 [ string-literal ] string-literal ( expression )
13938 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13939 each node is the expression. The TREE_PURPOSE is itself a
13940 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13941 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13942 is a STRING_CST for the string literal before the parenthesis. */
13944 static tree
13945 cp_parser_asm_operand_list (cp_parser* parser)
13947 tree asm_operands = NULL_TREE;
13949 while (true)
13951 tree string_literal;
13952 tree expression;
13953 tree name;
13955 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13957 /* Consume the `[' token. */
13958 cp_lexer_consume_token (parser->lexer);
13959 /* Read the operand name. */
13960 name = cp_parser_identifier (parser);
13961 if (name != error_mark_node)
13962 name = build_string (IDENTIFIER_LENGTH (name),
13963 IDENTIFIER_POINTER (name));
13964 /* Look for the closing `]'. */
13965 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13967 else
13968 name = NULL_TREE;
13969 /* Look for the string-literal. */
13970 string_literal = cp_parser_string_literal (parser, false, false);
13972 /* Look for the `('. */
13973 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13974 /* Parse the expression. */
13975 expression = cp_parser_expression (parser, /*cast_p=*/false);
13976 /* Look for the `)'. */
13977 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13979 /* Add this operand to the list. */
13980 asm_operands = tree_cons (build_tree_list (name, string_literal),
13981 expression,
13982 asm_operands);
13983 /* If the next token is not a `,', there are no more
13984 operands. */
13985 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13986 break;
13987 /* Consume the `,'. */
13988 cp_lexer_consume_token (parser->lexer);
13991 return nreverse (asm_operands);
13994 /* Parse an asm-clobber-list.
13996 asm-clobber-list:
13997 string-literal
13998 asm-clobber-list , string-literal
14000 Returns a TREE_LIST, indicating the clobbers in the order that they
14001 appeared. The TREE_VALUE of each node is a STRING_CST. */
14003 static tree
14004 cp_parser_asm_clobber_list (cp_parser* parser)
14006 tree clobbers = NULL_TREE;
14008 while (true)
14010 tree string_literal;
14012 /* Look for the string literal. */
14013 string_literal = cp_parser_string_literal (parser, false, false);
14014 /* Add it to the list. */
14015 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14016 /* If the next token is not a `,', then the list is
14017 complete. */
14018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14019 break;
14020 /* Consume the `,' token. */
14021 cp_lexer_consume_token (parser->lexer);
14024 return clobbers;
14027 /* Parse an (optional) series of attributes.
14029 attributes:
14030 attributes attribute
14032 attribute:
14033 __attribute__ (( attribute-list [opt] ))
14035 The return value is as for cp_parser_attribute_list. */
14037 static tree
14038 cp_parser_attributes_opt (cp_parser* parser)
14040 tree attributes = NULL_TREE;
14042 while (true)
14044 cp_token *token;
14045 tree attribute_list;
14047 /* Peek at the next token. */
14048 token = cp_lexer_peek_token (parser->lexer);
14049 /* If it's not `__attribute__', then we're done. */
14050 if (token->keyword != RID_ATTRIBUTE)
14051 break;
14053 /* Consume the `__attribute__' keyword. */
14054 cp_lexer_consume_token (parser->lexer);
14055 /* Look for the two `(' tokens. */
14056 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14057 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14059 /* Peek at the next token. */
14060 token = cp_lexer_peek_token (parser->lexer);
14061 if (token->type != CPP_CLOSE_PAREN)
14062 /* Parse the attribute-list. */
14063 attribute_list = cp_parser_attribute_list (parser);
14064 else
14065 /* If the next token is a `)', then there is no attribute
14066 list. */
14067 attribute_list = NULL;
14069 /* Look for the two `)' tokens. */
14070 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14071 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14073 /* Add these new attributes to the list. */
14074 attributes = chainon (attributes, attribute_list);
14077 return attributes;
14080 /* Parse an attribute-list.
14082 attribute-list:
14083 attribute
14084 attribute-list , attribute
14086 attribute:
14087 identifier
14088 identifier ( identifier )
14089 identifier ( identifier , expression-list )
14090 identifier ( expression-list )
14092 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14093 to an attribute. The TREE_PURPOSE of each node is the identifier
14094 indicating which attribute is in use. The TREE_VALUE represents
14095 the arguments, if any. */
14097 static tree
14098 cp_parser_attribute_list (cp_parser* parser)
14100 tree attribute_list = NULL_TREE;
14101 bool save_translate_strings_p = parser->translate_strings_p;
14103 parser->translate_strings_p = false;
14104 while (true)
14106 cp_token *token;
14107 tree identifier;
14108 tree attribute;
14110 /* Look for the identifier. We also allow keywords here; for
14111 example `__attribute__ ((const))' is legal. */
14112 token = cp_lexer_peek_token (parser->lexer);
14113 if (token->type == CPP_NAME
14114 || token->type == CPP_KEYWORD)
14116 /* Consume the token. */
14117 token = cp_lexer_consume_token (parser->lexer);
14119 /* Save away the identifier that indicates which attribute
14120 this is. */
14121 identifier = token->value;
14122 attribute = build_tree_list (identifier, NULL_TREE);
14124 /* Peek at the next token. */
14125 token = cp_lexer_peek_token (parser->lexer);
14126 /* If it's an `(', then parse the attribute arguments. */
14127 if (token->type == CPP_OPEN_PAREN)
14129 tree arguments;
14131 arguments = (cp_parser_parenthesized_expression_list
14132 (parser, true, /*cast_p=*/false,
14133 /*non_constant_p=*/NULL));
14134 /* Save the identifier and arguments away. */
14135 TREE_VALUE (attribute) = arguments;
14138 /* Add this attribute to the list. */
14139 TREE_CHAIN (attribute) = attribute_list;
14140 attribute_list = attribute;
14142 token = cp_lexer_peek_token (parser->lexer);
14144 /* Now, look for more attributes. If the next token isn't a
14145 `,', we're done. */
14146 if (token->type != CPP_COMMA)
14147 break;
14149 /* Consume the comma and keep going. */
14150 cp_lexer_consume_token (parser->lexer);
14152 parser->translate_strings_p = save_translate_strings_p;
14154 /* We built up the list in reverse order. */
14155 return nreverse (attribute_list);
14158 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14159 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14160 current value of the PEDANTIC flag, regardless of whether or not
14161 the `__extension__' keyword is present. The caller is responsible
14162 for restoring the value of the PEDANTIC flag. */
14164 static bool
14165 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14167 /* Save the old value of the PEDANTIC flag. */
14168 *saved_pedantic = pedantic;
14170 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14172 /* Consume the `__extension__' token. */
14173 cp_lexer_consume_token (parser->lexer);
14174 /* We're not being pedantic while the `__extension__' keyword is
14175 in effect. */
14176 pedantic = 0;
14178 return true;
14181 return false;
14184 /* Parse a label declaration.
14186 label-declaration:
14187 __label__ label-declarator-seq ;
14189 label-declarator-seq:
14190 identifier , label-declarator-seq
14191 identifier */
14193 static void
14194 cp_parser_label_declaration (cp_parser* parser)
14196 /* Look for the `__label__' keyword. */
14197 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14199 while (true)
14201 tree identifier;
14203 /* Look for an identifier. */
14204 identifier = cp_parser_identifier (parser);
14205 /* Declare it as a lobel. */
14206 finish_label_decl (identifier);
14207 /* If the next token is a `;', stop. */
14208 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14209 break;
14210 /* Look for the `,' separating the label declarations. */
14211 cp_parser_require (parser, CPP_COMMA, "`,'");
14214 /* Look for the final `;'. */
14215 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14218 /* Support Functions */
14220 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14221 NAME should have one of the representations used for an
14222 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14223 is returned. If PARSER->SCOPE is a dependent type, then a
14224 SCOPE_REF is returned.
14226 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14227 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14228 was formed. Abstractly, such entities should not be passed to this
14229 function, because they do not need to be looked up, but it is
14230 simpler to check for this special case here, rather than at the
14231 call-sites.
14233 In cases not explicitly covered above, this function returns a
14234 DECL, OVERLOAD, or baselink representing the result of the lookup.
14235 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14236 is returned.
14238 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14239 (e.g., "struct") that was used. In that case bindings that do not
14240 refer to types are ignored.
14242 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14243 ignored.
14245 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14246 are ignored.
14248 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14249 types.
14251 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14252 results in an ambiguity, and false otherwise. */
14254 static tree
14255 cp_parser_lookup_name (cp_parser *parser, tree name,
14256 enum tag_types tag_type,
14257 bool is_template, bool is_namespace,
14258 bool check_dependency,
14259 bool *ambiguous_p)
14261 tree decl;
14262 tree object_type = parser->context->object_type;
14264 /* Assume that the lookup will be unambiguous. */
14265 if (ambiguous_p)
14266 *ambiguous_p = false;
14268 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14269 no longer valid. Note that if we are parsing tentatively, and
14270 the parse fails, OBJECT_TYPE will be automatically restored. */
14271 parser->context->object_type = NULL_TREE;
14273 if (name == error_mark_node)
14274 return error_mark_node;
14276 /* A template-id has already been resolved; there is no lookup to
14277 do. */
14278 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14279 return name;
14280 if (BASELINK_P (name))
14282 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14283 == TEMPLATE_ID_EXPR);
14284 return name;
14287 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14288 it should already have been checked to make sure that the name
14289 used matches the type being destroyed. */
14290 if (TREE_CODE (name) == BIT_NOT_EXPR)
14292 tree type;
14294 /* Figure out to which type this destructor applies. */
14295 if (parser->scope)
14296 type = parser->scope;
14297 else if (object_type)
14298 type = object_type;
14299 else
14300 type = current_class_type;
14301 /* If that's not a class type, there is no destructor. */
14302 if (!type || !CLASS_TYPE_P (type))
14303 return error_mark_node;
14304 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14305 lazily_declare_fn (sfk_destructor, type);
14306 if (!CLASSTYPE_DESTRUCTORS (type))
14307 return error_mark_node;
14308 /* If it was a class type, return the destructor. */
14309 return CLASSTYPE_DESTRUCTORS (type);
14312 /* By this point, the NAME should be an ordinary identifier. If
14313 the id-expression was a qualified name, the qualifying scope is
14314 stored in PARSER->SCOPE at this point. */
14315 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14317 /* Perform the lookup. */
14318 if (parser->scope)
14320 bool dependent_p;
14322 if (parser->scope == error_mark_node)
14323 return error_mark_node;
14325 /* If the SCOPE is dependent, the lookup must be deferred until
14326 the template is instantiated -- unless we are explicitly
14327 looking up names in uninstantiated templates. Even then, we
14328 cannot look up the name if the scope is not a class type; it
14329 might, for example, be a template type parameter. */
14330 dependent_p = (TYPE_P (parser->scope)
14331 && !(parser->in_declarator_p
14332 && currently_open_class (parser->scope))
14333 && dependent_type_p (parser->scope));
14334 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14335 && dependent_p)
14337 if (tag_type)
14339 tree type;
14341 /* The resolution to Core Issue 180 says that `struct
14342 A::B' should be considered a type-name, even if `A'
14343 is dependent. */
14344 type = make_typename_type (parser->scope, name, tag_type,
14345 /*complain=*/1);
14346 decl = TYPE_NAME (type);
14348 else if (is_template)
14349 decl = make_unbound_class_template (parser->scope,
14350 name, NULL_TREE,
14351 /*complain=*/1);
14352 else
14353 decl = build_nt (SCOPE_REF, parser->scope, name);
14355 else
14357 tree pushed_scope = NULL_TREE;
14359 /* If PARSER->SCOPE is a dependent type, then it must be a
14360 class type, and we must not be checking dependencies;
14361 otherwise, we would have processed this lookup above. So
14362 that PARSER->SCOPE is not considered a dependent base by
14363 lookup_member, we must enter the scope here. */
14364 if (dependent_p)
14365 pushed_scope = push_scope (parser->scope);
14366 /* If the PARSER->SCOPE is a template specialization, it
14367 may be instantiated during name lookup. In that case,
14368 errors may be issued. Even if we rollback the current
14369 tentative parse, those errors are valid. */
14370 decl = lookup_qualified_name (parser->scope, name,
14371 tag_type != none_type,
14372 /*complain=*/true);
14373 if (pushed_scope)
14374 pop_scope (pushed_scope);
14376 parser->qualifying_scope = parser->scope;
14377 parser->object_scope = NULL_TREE;
14379 else if (object_type)
14381 tree object_decl = NULL_TREE;
14382 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14383 OBJECT_TYPE is not a class. */
14384 if (CLASS_TYPE_P (object_type))
14385 /* If the OBJECT_TYPE is a template specialization, it may
14386 be instantiated during name lookup. In that case, errors
14387 may be issued. Even if we rollback the current tentative
14388 parse, those errors are valid. */
14389 object_decl = lookup_member (object_type,
14390 name,
14391 /*protect=*/0,
14392 tag_type != none_type);
14393 /* Look it up in the enclosing context, too. */
14394 decl = lookup_name_real (name, tag_type != none_type,
14395 /*nonclass=*/0,
14396 /*block_p=*/true, is_namespace,
14397 /*flags=*/0);
14398 parser->object_scope = object_type;
14399 parser->qualifying_scope = NULL_TREE;
14400 if (object_decl)
14401 decl = object_decl;
14403 else
14405 decl = lookup_name_real (name, tag_type != none_type,
14406 /*nonclass=*/0,
14407 /*block_p=*/true, is_namespace,
14408 /*flags=*/0);
14409 parser->qualifying_scope = NULL_TREE;
14410 parser->object_scope = NULL_TREE;
14413 /* If the lookup failed, let our caller know. */
14414 if (!decl || decl == error_mark_node)
14415 return error_mark_node;
14417 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14418 if (TREE_CODE (decl) == TREE_LIST)
14420 if (ambiguous_p)
14421 *ambiguous_p = true;
14422 /* The error message we have to print is too complicated for
14423 cp_parser_error, so we incorporate its actions directly. */
14424 if (!cp_parser_simulate_error (parser))
14426 error ("reference to %qD is ambiguous", name);
14427 print_candidates (decl);
14429 return error_mark_node;
14432 gcc_assert (DECL_P (decl)
14433 || TREE_CODE (decl) == OVERLOAD
14434 || TREE_CODE (decl) == SCOPE_REF
14435 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14436 || BASELINK_P (decl));
14438 /* If we have resolved the name of a member declaration, check to
14439 see if the declaration is accessible. When the name resolves to
14440 set of overloaded functions, accessibility is checked when
14441 overload resolution is done.
14443 During an explicit instantiation, access is not checked at all,
14444 as per [temp.explicit]. */
14445 if (DECL_P (decl))
14446 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14448 return decl;
14451 /* Like cp_parser_lookup_name, but for use in the typical case where
14452 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14453 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14455 static tree
14456 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14458 return cp_parser_lookup_name (parser, name,
14459 none_type,
14460 /*is_template=*/false,
14461 /*is_namespace=*/false,
14462 /*check_dependency=*/true,
14463 /*ambiguous_p=*/NULL);
14466 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14467 the current context, return the TYPE_DECL. If TAG_NAME_P is
14468 true, the DECL indicates the class being defined in a class-head,
14469 or declared in an elaborated-type-specifier.
14471 Otherwise, return DECL. */
14473 static tree
14474 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14476 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14477 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14479 struct A {
14480 template <typename T> struct B;
14483 template <typename T> struct A::B {};
14485 Similarly, in a elaborated-type-specifier:
14487 namespace N { struct X{}; }
14489 struct A {
14490 template <typename T> friend struct N::X;
14493 However, if the DECL refers to a class type, and we are in
14494 the scope of the class, then the name lookup automatically
14495 finds the TYPE_DECL created by build_self_reference rather
14496 than a TEMPLATE_DECL. For example, in:
14498 template <class T> struct S {
14499 S s;
14502 there is no need to handle such case. */
14504 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14505 return DECL_TEMPLATE_RESULT (decl);
14507 return decl;
14510 /* If too many, or too few, template-parameter lists apply to the
14511 declarator, issue an error message. Returns TRUE if all went well,
14512 and FALSE otherwise. */
14514 static bool
14515 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14516 cp_declarator *declarator)
14518 unsigned num_templates;
14520 /* We haven't seen any classes that involve template parameters yet. */
14521 num_templates = 0;
14523 switch (declarator->kind)
14525 case cdk_id:
14526 if (declarator->u.id.qualifying_scope)
14528 tree scope;
14529 tree member;
14531 scope = declarator->u.id.qualifying_scope;
14532 member = declarator->u.id.unqualified_name;
14534 while (scope && CLASS_TYPE_P (scope))
14536 /* You're supposed to have one `template <...>'
14537 for every template class, but you don't need one
14538 for a full specialization. For example:
14540 template <class T> struct S{};
14541 template <> struct S<int> { void f(); };
14542 void S<int>::f () {}
14544 is correct; there shouldn't be a `template <>' for
14545 the definition of `S<int>::f'. */
14546 if (CLASSTYPE_TEMPLATE_INFO (scope)
14547 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14548 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14549 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14550 ++num_templates;
14552 scope = TYPE_CONTEXT (scope);
14555 else if (TREE_CODE (declarator->u.id.unqualified_name)
14556 == TEMPLATE_ID_EXPR)
14557 /* If the DECLARATOR has the form `X<y>' then it uses one
14558 additional level of template parameters. */
14559 ++num_templates;
14561 return cp_parser_check_template_parameters (parser,
14562 num_templates);
14564 case cdk_function:
14565 case cdk_array:
14566 case cdk_pointer:
14567 case cdk_reference:
14568 case cdk_ptrmem:
14569 return (cp_parser_check_declarator_template_parameters
14570 (parser, declarator->declarator));
14572 case cdk_error:
14573 return true;
14575 default:
14576 gcc_unreachable ();
14578 return false;
14581 /* NUM_TEMPLATES were used in the current declaration. If that is
14582 invalid, return FALSE and issue an error messages. Otherwise,
14583 return TRUE. */
14585 static bool
14586 cp_parser_check_template_parameters (cp_parser* parser,
14587 unsigned num_templates)
14589 /* If there are more template classes than parameter lists, we have
14590 something like:
14592 template <class T> void S<T>::R<T>::f (); */
14593 if (parser->num_template_parameter_lists < num_templates)
14595 error ("too few template-parameter-lists");
14596 return false;
14598 /* If there are the same number of template classes and parameter
14599 lists, that's OK. */
14600 if (parser->num_template_parameter_lists == num_templates)
14601 return true;
14602 /* If there are more, but only one more, then we are referring to a
14603 member template. That's OK too. */
14604 if (parser->num_template_parameter_lists == num_templates + 1)
14605 return true;
14606 /* Otherwise, there are too many template parameter lists. We have
14607 something like:
14609 template <class T> template <class U> void S::f(); */
14610 error ("too many template-parameter-lists");
14611 return false;
14614 /* Parse an optional `::' token indicating that the following name is
14615 from the global namespace. If so, PARSER->SCOPE is set to the
14616 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14617 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14618 Returns the new value of PARSER->SCOPE, if the `::' token is
14619 present, and NULL_TREE otherwise. */
14621 static tree
14622 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14624 cp_token *token;
14626 /* Peek at the next token. */
14627 token = cp_lexer_peek_token (parser->lexer);
14628 /* If we're looking at a `::' token then we're starting from the
14629 global namespace, not our current location. */
14630 if (token->type == CPP_SCOPE)
14632 /* Consume the `::' token. */
14633 cp_lexer_consume_token (parser->lexer);
14634 /* Set the SCOPE so that we know where to start the lookup. */
14635 parser->scope = global_namespace;
14636 parser->qualifying_scope = global_namespace;
14637 parser->object_scope = NULL_TREE;
14639 return parser->scope;
14641 else if (!current_scope_valid_p)
14643 parser->scope = NULL_TREE;
14644 parser->qualifying_scope = NULL_TREE;
14645 parser->object_scope = NULL_TREE;
14648 return NULL_TREE;
14651 /* Returns TRUE if the upcoming token sequence is the start of a
14652 constructor declarator. If FRIEND_P is true, the declarator is
14653 preceded by the `friend' specifier. */
14655 static bool
14656 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14658 bool constructor_p;
14659 tree type_decl = NULL_TREE;
14660 bool nested_name_p;
14661 cp_token *next_token;
14663 /* The common case is that this is not a constructor declarator, so
14664 try to avoid doing lots of work if at all possible. It's not
14665 valid declare a constructor at function scope. */
14666 if (at_function_scope_p ())
14667 return false;
14668 /* And only certain tokens can begin a constructor declarator. */
14669 next_token = cp_lexer_peek_token (parser->lexer);
14670 if (next_token->type != CPP_NAME
14671 && next_token->type != CPP_SCOPE
14672 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14673 && next_token->type != CPP_TEMPLATE_ID)
14674 return false;
14676 /* Parse tentatively; we are going to roll back all of the tokens
14677 consumed here. */
14678 cp_parser_parse_tentatively (parser);
14679 /* Assume that we are looking at a constructor declarator. */
14680 constructor_p = true;
14682 /* Look for the optional `::' operator. */
14683 cp_parser_global_scope_opt (parser,
14684 /*current_scope_valid_p=*/false);
14685 /* Look for the nested-name-specifier. */
14686 nested_name_p
14687 = (cp_parser_nested_name_specifier_opt (parser,
14688 /*typename_keyword_p=*/false,
14689 /*check_dependency_p=*/false,
14690 /*type_p=*/false,
14691 /*is_declaration=*/false)
14692 != NULL_TREE);
14693 /* Outside of a class-specifier, there must be a
14694 nested-name-specifier. */
14695 if (!nested_name_p &&
14696 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14697 || friend_p))
14698 constructor_p = false;
14699 /* If we still think that this might be a constructor-declarator,
14700 look for a class-name. */
14701 if (constructor_p)
14703 /* If we have:
14705 template <typename T> struct S { S(); };
14706 template <typename T> S<T>::S ();
14708 we must recognize that the nested `S' names a class.
14709 Similarly, for:
14711 template <typename T> S<T>::S<T> ();
14713 we must recognize that the nested `S' names a template. */
14714 type_decl = cp_parser_class_name (parser,
14715 /*typename_keyword_p=*/false,
14716 /*template_keyword_p=*/false,
14717 none_type,
14718 /*check_dependency_p=*/false,
14719 /*class_head_p=*/false,
14720 /*is_declaration=*/false);
14721 /* If there was no class-name, then this is not a constructor. */
14722 constructor_p = !cp_parser_error_occurred (parser);
14725 /* If we're still considering a constructor, we have to see a `(',
14726 to begin the parameter-declaration-clause, followed by either a
14727 `)', an `...', or a decl-specifier. We need to check for a
14728 type-specifier to avoid being fooled into thinking that:
14730 S::S (f) (int);
14732 is a constructor. (It is actually a function named `f' that
14733 takes one parameter (of type `int') and returns a value of type
14734 `S::S'. */
14735 if (constructor_p
14736 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14739 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14740 /* A parameter declaration begins with a decl-specifier,
14741 which is either the "attribute" keyword, a storage class
14742 specifier, or (usually) a type-specifier. */
14743 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14744 && !cp_parser_storage_class_specifier_opt (parser))
14746 tree type;
14747 tree pushed_scope = NULL_TREE;
14748 unsigned saved_num_template_parameter_lists;
14750 /* Names appearing in the type-specifier should be looked up
14751 in the scope of the class. */
14752 if (current_class_type)
14753 type = NULL_TREE;
14754 else
14756 type = TREE_TYPE (type_decl);
14757 if (TREE_CODE (type) == TYPENAME_TYPE)
14759 type = resolve_typename_type (type,
14760 /*only_current_p=*/false);
14761 if (type == error_mark_node)
14763 cp_parser_abort_tentative_parse (parser);
14764 return false;
14767 pushed_scope = push_scope (type);
14770 /* Inside the constructor parameter list, surrounding
14771 template-parameter-lists do not apply. */
14772 saved_num_template_parameter_lists
14773 = parser->num_template_parameter_lists;
14774 parser->num_template_parameter_lists = 0;
14776 /* Look for the type-specifier. */
14777 cp_parser_type_specifier (parser,
14778 CP_PARSER_FLAGS_NONE,
14779 /*decl_specs=*/NULL,
14780 /*is_declarator=*/true,
14781 /*declares_class_or_enum=*/NULL,
14782 /*is_cv_qualifier=*/NULL);
14784 parser->num_template_parameter_lists
14785 = saved_num_template_parameter_lists;
14787 /* Leave the scope of the class. */
14788 if (pushed_scope)
14789 pop_scope (pushed_scope);
14791 constructor_p = !cp_parser_error_occurred (parser);
14794 else
14795 constructor_p = false;
14796 /* We did not really want to consume any tokens. */
14797 cp_parser_abort_tentative_parse (parser);
14799 return constructor_p;
14802 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14803 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14804 they must be performed once we are in the scope of the function.
14806 Returns the function defined. */
14808 static tree
14809 cp_parser_function_definition_from_specifiers_and_declarator
14810 (cp_parser* parser,
14811 cp_decl_specifier_seq *decl_specifiers,
14812 tree attributes,
14813 const cp_declarator *declarator)
14815 tree fn;
14816 bool success_p;
14818 /* Begin the function-definition. */
14819 success_p = start_function (decl_specifiers, declarator, attributes);
14821 /* The things we're about to see are not directly qualified by any
14822 template headers we've seen thus far. */
14823 reset_specialization ();
14825 /* If there were names looked up in the decl-specifier-seq that we
14826 did not check, check them now. We must wait until we are in the
14827 scope of the function to perform the checks, since the function
14828 might be a friend. */
14829 perform_deferred_access_checks ();
14831 if (!success_p)
14833 /* Skip the entire function. */
14834 error ("invalid function declaration");
14835 cp_parser_skip_to_end_of_block_or_statement (parser);
14836 fn = error_mark_node;
14838 else
14839 fn = cp_parser_function_definition_after_declarator (parser,
14840 /*inline_p=*/false);
14842 return fn;
14845 /* Parse the part of a function-definition that follows the
14846 declarator. INLINE_P is TRUE iff this function is an inline
14847 function defined with a class-specifier.
14849 Returns the function defined. */
14851 static tree
14852 cp_parser_function_definition_after_declarator (cp_parser* parser,
14853 bool inline_p)
14855 tree fn;
14856 bool ctor_initializer_p = false;
14857 bool saved_in_unbraced_linkage_specification_p;
14858 unsigned saved_num_template_parameter_lists;
14860 /* If the next token is `return', then the code may be trying to
14861 make use of the "named return value" extension that G++ used to
14862 support. */
14863 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14865 /* Consume the `return' keyword. */
14866 cp_lexer_consume_token (parser->lexer);
14867 /* Look for the identifier that indicates what value is to be
14868 returned. */
14869 cp_parser_identifier (parser);
14870 /* Issue an error message. */
14871 error ("named return values are no longer supported");
14872 /* Skip tokens until we reach the start of the function body. */
14873 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14874 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14875 cp_lexer_consume_token (parser->lexer);
14877 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14878 anything declared inside `f'. */
14879 saved_in_unbraced_linkage_specification_p
14880 = parser->in_unbraced_linkage_specification_p;
14881 parser->in_unbraced_linkage_specification_p = false;
14882 /* Inside the function, surrounding template-parameter-lists do not
14883 apply. */
14884 saved_num_template_parameter_lists
14885 = parser->num_template_parameter_lists;
14886 parser->num_template_parameter_lists = 0;
14887 /* If the next token is `try', then we are looking at a
14888 function-try-block. */
14889 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14890 ctor_initializer_p = cp_parser_function_try_block (parser);
14891 /* A function-try-block includes the function-body, so we only do
14892 this next part if we're not processing a function-try-block. */
14893 else
14894 ctor_initializer_p
14895 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14897 /* Finish the function. */
14898 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14899 (inline_p ? 2 : 0));
14900 /* Generate code for it, if necessary. */
14901 expand_or_defer_fn (fn);
14902 /* Restore the saved values. */
14903 parser->in_unbraced_linkage_specification_p
14904 = saved_in_unbraced_linkage_specification_p;
14905 parser->num_template_parameter_lists
14906 = saved_num_template_parameter_lists;
14908 return fn;
14911 /* Parse a template-declaration, assuming that the `export' (and
14912 `extern') keywords, if present, has already been scanned. MEMBER_P
14913 is as for cp_parser_template_declaration. */
14915 static void
14916 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14918 tree decl = NULL_TREE;
14919 tree parameter_list;
14920 bool friend_p = false;
14922 /* Look for the `template' keyword. */
14923 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14924 return;
14926 /* And the `<'. */
14927 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14928 return;
14930 /* If the next token is `>', then we have an invalid
14931 specialization. Rather than complain about an invalid template
14932 parameter, issue an error message here. */
14933 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14935 cp_parser_error (parser, "invalid explicit specialization");
14936 begin_specialization ();
14937 parameter_list = NULL_TREE;
14939 else
14941 /* Parse the template parameters. */
14942 begin_template_parm_list ();
14943 parameter_list = cp_parser_template_parameter_list (parser);
14944 parameter_list = end_template_parm_list (parameter_list);
14947 /* Look for the `>'. */
14948 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14949 /* We just processed one more parameter list. */
14950 ++parser->num_template_parameter_lists;
14951 /* If the next token is `template', there are more template
14952 parameters. */
14953 if (cp_lexer_next_token_is_keyword (parser->lexer,
14954 RID_TEMPLATE))
14955 cp_parser_template_declaration_after_export (parser, member_p);
14956 else
14958 /* There are no access checks when parsing a template, as we do not
14959 know if a specialization will be a friend. */
14960 push_deferring_access_checks (dk_no_check);
14962 decl = cp_parser_single_declaration (parser,
14963 member_p,
14964 &friend_p);
14966 pop_deferring_access_checks ();
14968 /* If this is a member template declaration, let the front
14969 end know. */
14970 if (member_p && !friend_p && decl)
14972 if (TREE_CODE (decl) == TYPE_DECL)
14973 cp_parser_check_access_in_redeclaration (decl);
14975 decl = finish_member_template_decl (decl);
14977 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14978 make_friend_class (current_class_type, TREE_TYPE (decl),
14979 /*complain=*/true);
14981 /* We are done with the current parameter list. */
14982 --parser->num_template_parameter_lists;
14984 /* Finish up. */
14985 finish_template_decl (parameter_list);
14987 /* Register member declarations. */
14988 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14989 finish_member_declaration (decl);
14991 /* If DECL is a function template, we must return to parse it later.
14992 (Even though there is no definition, there might be default
14993 arguments that need handling.) */
14994 if (member_p && decl
14995 && (TREE_CODE (decl) == FUNCTION_DECL
14996 || DECL_FUNCTION_TEMPLATE_P (decl)))
14997 TREE_VALUE (parser->unparsed_functions_queues)
14998 = tree_cons (NULL_TREE, decl,
14999 TREE_VALUE (parser->unparsed_functions_queues));
15002 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15003 `function-definition' sequence. MEMBER_P is true, this declaration
15004 appears in a class scope.
15006 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15007 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15009 static tree
15010 cp_parser_single_declaration (cp_parser* parser,
15011 bool member_p,
15012 bool* friend_p)
15014 int declares_class_or_enum;
15015 tree decl = NULL_TREE;
15016 cp_decl_specifier_seq decl_specifiers;
15017 bool function_definition_p = false;
15019 /* This function is only used when processing a template
15020 declaration. */
15021 gcc_assert (innermost_scope_kind () == sk_template_parms
15022 || innermost_scope_kind () == sk_template_spec);
15024 /* Defer access checks until we know what is being declared. */
15025 push_deferring_access_checks (dk_deferred);
15027 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15028 alternative. */
15029 cp_parser_decl_specifier_seq (parser,
15030 CP_PARSER_FLAGS_OPTIONAL,
15031 &decl_specifiers,
15032 &declares_class_or_enum);
15033 if (friend_p)
15034 *friend_p = cp_parser_friend_p (&decl_specifiers);
15036 /* There are no template typedefs. */
15037 if (decl_specifiers.specs[(int) ds_typedef])
15039 error ("template declaration of %qs", "typedef");
15040 decl = error_mark_node;
15043 /* Gather up the access checks that occurred the
15044 decl-specifier-seq. */
15045 stop_deferring_access_checks ();
15047 /* Check for the declaration of a template class. */
15048 if (declares_class_or_enum)
15050 if (cp_parser_declares_only_class_p (parser))
15052 decl = shadow_tag (&decl_specifiers);
15054 /* In this case:
15056 struct C {
15057 friend template <typename T> struct A<T>::B;
15060 A<T>::B will be represented by a TYPENAME_TYPE, and
15061 therefore not recognized by shadow_tag. */
15062 if (friend_p && *friend_p
15063 && !decl
15064 && decl_specifiers.type
15065 && TYPE_P (decl_specifiers.type))
15066 decl = decl_specifiers.type;
15068 if (decl && decl != error_mark_node)
15069 decl = TYPE_NAME (decl);
15070 else
15071 decl = error_mark_node;
15074 /* If it's not a template class, try for a template function. If
15075 the next token is a `;', then this declaration does not declare
15076 anything. But, if there were errors in the decl-specifiers, then
15077 the error might well have come from an attempted class-specifier.
15078 In that case, there's no need to warn about a missing declarator. */
15079 if (!decl
15080 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15081 || decl_specifiers.type != error_mark_node))
15082 decl = cp_parser_init_declarator (parser,
15083 &decl_specifiers,
15084 /*function_definition_allowed_p=*/true,
15085 member_p,
15086 declares_class_or_enum,
15087 &function_definition_p);
15089 pop_deferring_access_checks ();
15091 /* Clear any current qualification; whatever comes next is the start
15092 of something new. */
15093 parser->scope = NULL_TREE;
15094 parser->qualifying_scope = NULL_TREE;
15095 parser->object_scope = NULL_TREE;
15096 /* Look for a trailing `;' after the declaration. */
15097 if (!function_definition_p
15098 && (decl == error_mark_node
15099 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15100 cp_parser_skip_to_end_of_block_or_statement (parser);
15102 return decl;
15105 /* Parse a cast-expression that is not the operand of a unary "&". */
15107 static tree
15108 cp_parser_simple_cast_expression (cp_parser *parser)
15110 return cp_parser_cast_expression (parser, /*address_p=*/false,
15111 /*cast_p=*/false);
15114 /* Parse a functional cast to TYPE. Returns an expression
15115 representing the cast. */
15117 static tree
15118 cp_parser_functional_cast (cp_parser* parser, tree type)
15120 tree expression_list;
15121 tree cast;
15123 expression_list
15124 = cp_parser_parenthesized_expression_list (parser, false,
15125 /*cast_p=*/true,
15126 /*non_constant_p=*/NULL);
15128 cast = build_functional_cast (type, expression_list);
15129 /* [expr.const]/1: In an integral constant expression "only type
15130 conversions to integral or enumeration type can be used". */
15131 if (cast != error_mark_node && !type_dependent_expression_p (type)
15132 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15134 if (cp_parser_non_integral_constant_expression
15135 (parser, "a call to a constructor"))
15136 return error_mark_node;
15138 return cast;
15141 /* Save the tokens that make up the body of a member function defined
15142 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15143 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15144 specifiers applied to the declaration. Returns the FUNCTION_DECL
15145 for the member function. */
15147 static tree
15148 cp_parser_save_member_function_body (cp_parser* parser,
15149 cp_decl_specifier_seq *decl_specifiers,
15150 cp_declarator *declarator,
15151 tree attributes)
15153 cp_token *first;
15154 cp_token *last;
15155 tree fn;
15157 /* Create the function-declaration. */
15158 fn = start_method (decl_specifiers, declarator, attributes);
15159 /* If something went badly wrong, bail out now. */
15160 if (fn == error_mark_node)
15162 /* If there's a function-body, skip it. */
15163 if (cp_parser_token_starts_function_definition_p
15164 (cp_lexer_peek_token (parser->lexer)))
15165 cp_parser_skip_to_end_of_block_or_statement (parser);
15166 return error_mark_node;
15169 /* Remember it, if there default args to post process. */
15170 cp_parser_save_default_args (parser, fn);
15172 /* Save away the tokens that make up the body of the
15173 function. */
15174 first = parser->lexer->next_token;
15175 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15176 /* Handle function try blocks. */
15177 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15178 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15179 last = parser->lexer->next_token;
15181 /* Save away the inline definition; we will process it when the
15182 class is complete. */
15183 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15184 DECL_PENDING_INLINE_P (fn) = 1;
15186 /* We need to know that this was defined in the class, so that
15187 friend templates are handled correctly. */
15188 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15190 /* We're done with the inline definition. */
15191 finish_method (fn);
15193 /* Add FN to the queue of functions to be parsed later. */
15194 TREE_VALUE (parser->unparsed_functions_queues)
15195 = tree_cons (NULL_TREE, fn,
15196 TREE_VALUE (parser->unparsed_functions_queues));
15198 return fn;
15201 /* Parse a template-argument-list, as well as the trailing ">" (but
15202 not the opening ">"). See cp_parser_template_argument_list for the
15203 return value. */
15205 static tree
15206 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15208 tree arguments;
15209 tree saved_scope;
15210 tree saved_qualifying_scope;
15211 tree saved_object_scope;
15212 bool saved_greater_than_is_operator_p;
15214 /* [temp.names]
15216 When parsing a template-id, the first non-nested `>' is taken as
15217 the end of the template-argument-list rather than a greater-than
15218 operator. */
15219 saved_greater_than_is_operator_p
15220 = parser->greater_than_is_operator_p;
15221 parser->greater_than_is_operator_p = false;
15222 /* Parsing the argument list may modify SCOPE, so we save it
15223 here. */
15224 saved_scope = parser->scope;
15225 saved_qualifying_scope = parser->qualifying_scope;
15226 saved_object_scope = parser->object_scope;
15227 /* Parse the template-argument-list itself. */
15228 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15229 arguments = NULL_TREE;
15230 else
15231 arguments = cp_parser_template_argument_list (parser);
15232 /* Look for the `>' that ends the template-argument-list. If we find
15233 a '>>' instead, it's probably just a typo. */
15234 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15236 if (!saved_greater_than_is_operator_p)
15238 /* If we're in a nested template argument list, the '>>' has
15239 to be a typo for '> >'. We emit the error message, but we
15240 continue parsing and we push a '>' as next token, so that
15241 the argument list will be parsed correctly. Note that the
15242 global source location is still on the token before the
15243 '>>', so we need to say explicitly where we want it. */
15244 cp_token *token = cp_lexer_peek_token (parser->lexer);
15245 error ("%H%<>>%> should be %<> >%> "
15246 "within a nested template argument list",
15247 &token->location);
15249 /* ??? Proper recovery should terminate two levels of
15250 template argument list here. */
15251 token->type = CPP_GREATER;
15253 else
15255 /* If this is not a nested template argument list, the '>>'
15256 is a typo for '>'. Emit an error message and continue.
15257 Same deal about the token location, but here we can get it
15258 right by consuming the '>>' before issuing the diagnostic. */
15259 cp_lexer_consume_token (parser->lexer);
15260 error ("spurious %<>>%>, use %<>%> to terminate "
15261 "a template argument list");
15264 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15265 error ("missing %<>%> to terminate the template argument list");
15266 else
15267 /* It's what we want, a '>'; consume it. */
15268 cp_lexer_consume_token (parser->lexer);
15269 /* The `>' token might be a greater-than operator again now. */
15270 parser->greater_than_is_operator_p
15271 = saved_greater_than_is_operator_p;
15272 /* Restore the SAVED_SCOPE. */
15273 parser->scope = saved_scope;
15274 parser->qualifying_scope = saved_qualifying_scope;
15275 parser->object_scope = saved_object_scope;
15277 return arguments;
15280 /* MEMBER_FUNCTION is a member function, or a friend. If default
15281 arguments, or the body of the function have not yet been parsed,
15282 parse them now. */
15284 static void
15285 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15287 /* If this member is a template, get the underlying
15288 FUNCTION_DECL. */
15289 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15290 member_function = DECL_TEMPLATE_RESULT (member_function);
15292 /* There should not be any class definitions in progress at this
15293 point; the bodies of members are only parsed outside of all class
15294 definitions. */
15295 gcc_assert (parser->num_classes_being_defined == 0);
15296 /* While we're parsing the member functions we might encounter more
15297 classes. We want to handle them right away, but we don't want
15298 them getting mixed up with functions that are currently in the
15299 queue. */
15300 parser->unparsed_functions_queues
15301 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15303 /* Make sure that any template parameters are in scope. */
15304 maybe_begin_member_template_processing (member_function);
15306 /* If the body of the function has not yet been parsed, parse it
15307 now. */
15308 if (DECL_PENDING_INLINE_P (member_function))
15310 tree function_scope;
15311 cp_token_cache *tokens;
15313 /* The function is no longer pending; we are processing it. */
15314 tokens = DECL_PENDING_INLINE_INFO (member_function);
15315 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15316 DECL_PENDING_INLINE_P (member_function) = 0;
15318 /* If this is a local class, enter the scope of the containing
15319 function. */
15320 function_scope = current_function_decl;
15321 if (function_scope)
15322 push_function_context_to (function_scope);
15324 /* Push the body of the function onto the lexer stack. */
15325 cp_parser_push_lexer_for_tokens (parser, tokens);
15327 /* Let the front end know that we going to be defining this
15328 function. */
15329 start_preparsed_function (member_function, NULL_TREE,
15330 SF_PRE_PARSED | SF_INCLASS_INLINE);
15332 /* Now, parse the body of the function. */
15333 cp_parser_function_definition_after_declarator (parser,
15334 /*inline_p=*/true);
15336 /* Leave the scope of the containing function. */
15337 if (function_scope)
15338 pop_function_context_from (function_scope);
15339 cp_parser_pop_lexer (parser);
15342 /* Remove any template parameters from the symbol table. */
15343 maybe_end_member_template_processing ();
15345 /* Restore the queue. */
15346 parser->unparsed_functions_queues
15347 = TREE_CHAIN (parser->unparsed_functions_queues);
15350 /* If DECL contains any default args, remember it on the unparsed
15351 functions queue. */
15353 static void
15354 cp_parser_save_default_args (cp_parser* parser, tree decl)
15356 tree probe;
15358 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15359 probe;
15360 probe = TREE_CHAIN (probe))
15361 if (TREE_PURPOSE (probe))
15363 TREE_PURPOSE (parser->unparsed_functions_queues)
15364 = tree_cons (current_class_type, decl,
15365 TREE_PURPOSE (parser->unparsed_functions_queues));
15366 break;
15368 return;
15371 /* FN is a FUNCTION_DECL which may contains a parameter with an
15372 unparsed DEFAULT_ARG. Parse the default args now. This function
15373 assumes that the current scope is the scope in which the default
15374 argument should be processed. */
15376 static void
15377 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15379 bool saved_local_variables_forbidden_p;
15380 tree parm;
15382 /* While we're parsing the default args, we might (due to the
15383 statement expression extension) encounter more classes. We want
15384 to handle them right away, but we don't want them getting mixed
15385 up with default args that are currently in the queue. */
15386 parser->unparsed_functions_queues
15387 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15389 /* Local variable names (and the `this' keyword) may not appear
15390 in a default argument. */
15391 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15392 parser->local_variables_forbidden_p = true;
15394 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15395 parm;
15396 parm = TREE_CHAIN (parm))
15398 cp_token_cache *tokens;
15400 if (!TREE_PURPOSE (parm)
15401 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15402 continue;
15404 /* Push the saved tokens for the default argument onto the parser's
15405 lexer stack. */
15406 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15407 cp_parser_push_lexer_for_tokens (parser, tokens);
15409 /* Parse the assignment-expression. */
15410 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser,
15411 /*cast_p=*/false);
15413 /* If the token stream has not been completely used up, then
15414 there was extra junk after the end of the default
15415 argument. */
15416 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15417 cp_parser_error (parser, "expected %<,%>");
15419 /* Revert to the main lexer. */
15420 cp_parser_pop_lexer (parser);
15423 /* Restore the state of local_variables_forbidden_p. */
15424 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15426 /* Restore the queue. */
15427 parser->unparsed_functions_queues
15428 = TREE_CHAIN (parser->unparsed_functions_queues);
15431 /* Parse the operand of `sizeof' (or a similar operator). Returns
15432 either a TYPE or an expression, depending on the form of the
15433 input. The KEYWORD indicates which kind of expression we have
15434 encountered. */
15436 static tree
15437 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15439 static const char *format;
15440 tree expr = NULL_TREE;
15441 const char *saved_message;
15442 bool saved_integral_constant_expression_p;
15443 bool saved_non_integral_constant_expression_p;
15445 /* Initialize FORMAT the first time we get here. */
15446 if (!format)
15447 format = "types may not be defined in '%s' expressions";
15449 /* Types cannot be defined in a `sizeof' expression. Save away the
15450 old message. */
15451 saved_message = parser->type_definition_forbidden_message;
15452 /* And create the new one. */
15453 parser->type_definition_forbidden_message
15454 = xmalloc (strlen (format)
15455 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15456 + 1 /* `\0' */);
15457 sprintf ((char *) parser->type_definition_forbidden_message,
15458 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15460 /* The restrictions on constant-expressions do not apply inside
15461 sizeof expressions. */
15462 saved_integral_constant_expression_p
15463 = parser->integral_constant_expression_p;
15464 saved_non_integral_constant_expression_p
15465 = parser->non_integral_constant_expression_p;
15466 parser->integral_constant_expression_p = false;
15468 /* Do not actually evaluate the expression. */
15469 ++skip_evaluation;
15470 /* If it's a `(', then we might be looking at the type-id
15471 construction. */
15472 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15474 tree type;
15475 bool saved_in_type_id_in_expr_p;
15477 /* We can't be sure yet whether we're looking at a type-id or an
15478 expression. */
15479 cp_parser_parse_tentatively (parser);
15480 /* Consume the `('. */
15481 cp_lexer_consume_token (parser->lexer);
15482 /* Parse the type-id. */
15483 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15484 parser->in_type_id_in_expr_p = true;
15485 type = cp_parser_type_id (parser);
15486 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15487 /* Now, look for the trailing `)'. */
15488 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15489 /* If all went well, then we're done. */
15490 if (cp_parser_parse_definitely (parser))
15492 cp_decl_specifier_seq decl_specs;
15494 /* Build a trivial decl-specifier-seq. */
15495 clear_decl_specs (&decl_specs);
15496 decl_specs.type = type;
15498 /* Call grokdeclarator to figure out what type this is. */
15499 expr = grokdeclarator (NULL,
15500 &decl_specs,
15501 TYPENAME,
15502 /*initialized=*/0,
15503 /*attrlist=*/NULL);
15507 /* If the type-id production did not work out, then we must be
15508 looking at the unary-expression production. */
15509 if (!expr)
15510 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15511 /*cast_p=*/false);
15512 /* Go back to evaluating expressions. */
15513 --skip_evaluation;
15515 /* Free the message we created. */
15516 free ((char *) parser->type_definition_forbidden_message);
15517 /* And restore the old one. */
15518 parser->type_definition_forbidden_message = saved_message;
15519 parser->integral_constant_expression_p
15520 = saved_integral_constant_expression_p;
15521 parser->non_integral_constant_expression_p
15522 = saved_non_integral_constant_expression_p;
15524 return expr;
15527 /* If the current declaration has no declarator, return true. */
15529 static bool
15530 cp_parser_declares_only_class_p (cp_parser *parser)
15532 /* If the next token is a `;' or a `,' then there is no
15533 declarator. */
15534 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15535 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15538 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15540 static void
15541 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15542 cp_storage_class storage_class)
15544 if (decl_specs->storage_class != sc_none)
15545 decl_specs->multiple_storage_classes_p = true;
15546 else
15547 decl_specs->storage_class = storage_class;
15550 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15551 is true, the type is a user-defined type; otherwise it is a
15552 built-in type specified by a keyword. */
15554 static void
15555 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15556 tree type_spec,
15557 bool user_defined_p)
15559 decl_specs->any_specifiers_p = true;
15561 /* If the user tries to redeclare bool or wchar_t (with, for
15562 example, in "typedef int wchar_t;") we remember that this is what
15563 happened. In system headers, we ignore these declarations so
15564 that G++ can work with system headers that are not C++-safe. */
15565 if (decl_specs->specs[(int) ds_typedef]
15566 && !user_defined_p
15567 && (type_spec == boolean_type_node
15568 || type_spec == wchar_type_node)
15569 && (decl_specs->type
15570 || decl_specs->specs[(int) ds_long]
15571 || decl_specs->specs[(int) ds_short]
15572 || decl_specs->specs[(int) ds_unsigned]
15573 || decl_specs->specs[(int) ds_signed]))
15575 decl_specs->redefined_builtin_type = type_spec;
15576 if (!decl_specs->type)
15578 decl_specs->type = type_spec;
15579 decl_specs->user_defined_type_p = false;
15582 else if (decl_specs->type)
15583 decl_specs->multiple_types_p = true;
15584 else
15586 decl_specs->type = type_spec;
15587 decl_specs->user_defined_type_p = user_defined_p;
15588 decl_specs->redefined_builtin_type = NULL_TREE;
15592 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15593 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15595 static bool
15596 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15598 return decl_specifiers->specs[(int) ds_friend] != 0;
15601 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15602 issue an error message indicating that TOKEN_DESC was expected.
15604 Returns the token consumed, if the token had the appropriate type.
15605 Otherwise, returns NULL. */
15607 static cp_token *
15608 cp_parser_require (cp_parser* parser,
15609 enum cpp_ttype type,
15610 const char* token_desc)
15612 if (cp_lexer_next_token_is (parser->lexer, type))
15613 return cp_lexer_consume_token (parser->lexer);
15614 else
15616 /* Output the MESSAGE -- unless we're parsing tentatively. */
15617 if (!cp_parser_simulate_error (parser))
15619 char *message = concat ("expected ", token_desc, NULL);
15620 cp_parser_error (parser, message);
15621 free (message);
15623 return NULL;
15627 /* Like cp_parser_require, except that tokens will be skipped until
15628 the desired token is found. An error message is still produced if
15629 the next token is not as expected. */
15631 static void
15632 cp_parser_skip_until_found (cp_parser* parser,
15633 enum cpp_ttype type,
15634 const char* token_desc)
15636 cp_token *token;
15637 unsigned nesting_depth = 0;
15639 if (cp_parser_require (parser, type, token_desc))
15640 return;
15642 /* Skip tokens until the desired token is found. */
15643 while (true)
15645 /* Peek at the next token. */
15646 token = cp_lexer_peek_token (parser->lexer);
15647 /* If we've reached the token we want, consume it and
15648 stop. */
15649 if (token->type == type && !nesting_depth)
15651 cp_lexer_consume_token (parser->lexer);
15652 return;
15654 /* If we've run out of tokens, stop. */
15655 if (token->type == CPP_EOF)
15656 return;
15657 if (token->type == CPP_OPEN_BRACE
15658 || token->type == CPP_OPEN_PAREN
15659 || token->type == CPP_OPEN_SQUARE)
15660 ++nesting_depth;
15661 else if (token->type == CPP_CLOSE_BRACE
15662 || token->type == CPP_CLOSE_PAREN
15663 || token->type == CPP_CLOSE_SQUARE)
15665 if (nesting_depth-- == 0)
15666 return;
15668 /* Consume this token. */
15669 cp_lexer_consume_token (parser->lexer);
15673 /* If the next token is the indicated keyword, consume it. Otherwise,
15674 issue an error message indicating that TOKEN_DESC was expected.
15676 Returns the token consumed, if the token had the appropriate type.
15677 Otherwise, returns NULL. */
15679 static cp_token *
15680 cp_parser_require_keyword (cp_parser* parser,
15681 enum rid keyword,
15682 const char* token_desc)
15684 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15686 if (token && token->keyword != keyword)
15688 dyn_string_t error_msg;
15690 /* Format the error message. */
15691 error_msg = dyn_string_new (0);
15692 dyn_string_append_cstr (error_msg, "expected ");
15693 dyn_string_append_cstr (error_msg, token_desc);
15694 cp_parser_error (parser, error_msg->s);
15695 dyn_string_delete (error_msg);
15696 return NULL;
15699 return token;
15702 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15703 function-definition. */
15705 static bool
15706 cp_parser_token_starts_function_definition_p (cp_token* token)
15708 return (/* An ordinary function-body begins with an `{'. */
15709 token->type == CPP_OPEN_BRACE
15710 /* A ctor-initializer begins with a `:'. */
15711 || token->type == CPP_COLON
15712 /* A function-try-block begins with `try'. */
15713 || token->keyword == RID_TRY
15714 /* The named return value extension begins with `return'. */
15715 || token->keyword == RID_RETURN);
15718 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15719 definition. */
15721 static bool
15722 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15724 cp_token *token;
15726 token = cp_lexer_peek_token (parser->lexer);
15727 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15730 /* Returns TRUE iff the next token is the "," or ">" ending a
15731 template-argument. */
15733 static bool
15734 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15736 cp_token *token;
15738 token = cp_lexer_peek_token (parser->lexer);
15739 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15742 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15743 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15745 static bool
15746 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15747 size_t n)
15749 cp_token *token;
15751 token = cp_lexer_peek_nth_token (parser->lexer, n);
15752 if (token->type == CPP_LESS)
15753 return true;
15754 /* Check for the sequence `<::' in the original code. It would be lexed as
15755 `[:', where `[' is a digraph, and there is no whitespace before
15756 `:'. */
15757 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15759 cp_token *token2;
15760 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15761 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15762 return true;
15764 return false;
15767 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15768 or none_type otherwise. */
15770 static enum tag_types
15771 cp_parser_token_is_class_key (cp_token* token)
15773 switch (token->keyword)
15775 case RID_CLASS:
15776 return class_type;
15777 case RID_STRUCT:
15778 return record_type;
15779 case RID_UNION:
15780 return union_type;
15782 default:
15783 return none_type;
15787 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15789 static void
15790 cp_parser_check_class_key (enum tag_types class_key, tree type)
15792 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15793 pedwarn ("%qs tag used in naming %q#T",
15794 class_key == union_type ? "union"
15795 : class_key == record_type ? "struct" : "class",
15796 type);
15799 /* Issue an error message if DECL is redeclared with different
15800 access than its original declaration [class.access.spec/3].
15801 This applies to nested classes and nested class templates.
15802 [class.mem/1]. */
15804 static void
15805 cp_parser_check_access_in_redeclaration (tree decl)
15807 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15808 return;
15810 if ((TREE_PRIVATE (decl)
15811 != (current_access_specifier == access_private_node))
15812 || (TREE_PROTECTED (decl)
15813 != (current_access_specifier == access_protected_node)))
15814 error ("%qD redeclared with different access", decl);
15817 /* Look for the `template' keyword, as a syntactic disambiguator.
15818 Return TRUE iff it is present, in which case it will be
15819 consumed. */
15821 static bool
15822 cp_parser_optional_template_keyword (cp_parser *parser)
15824 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15826 /* The `template' keyword can only be used within templates;
15827 outside templates the parser can always figure out what is a
15828 template and what is not. */
15829 if (!processing_template_decl)
15831 error ("%<template%> (as a disambiguator) is only allowed "
15832 "within templates");
15833 /* If this part of the token stream is rescanned, the same
15834 error message would be generated. So, we purge the token
15835 from the stream. */
15836 cp_lexer_purge_token (parser->lexer);
15837 return false;
15839 else
15841 /* Consume the `template' keyword. */
15842 cp_lexer_consume_token (parser->lexer);
15843 return true;
15847 return false;
15850 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15851 set PARSER->SCOPE, and perform other related actions. */
15853 static void
15854 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15856 tree value;
15857 tree check;
15859 /* Get the stored value. */
15860 value = cp_lexer_consume_token (parser->lexer)->value;
15861 /* Perform any access checks that were deferred. */
15862 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15863 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15864 /* Set the scope from the stored value. */
15865 parser->scope = TREE_VALUE (value);
15866 parser->qualifying_scope = TREE_TYPE (value);
15867 parser->object_scope = NULL_TREE;
15870 /* Consume tokens up through a non-nested END token. */
15872 static void
15873 cp_parser_cache_group (cp_parser *parser,
15874 enum cpp_ttype end,
15875 unsigned depth)
15877 while (true)
15879 cp_token *token;
15881 /* Abort a parenthesized expression if we encounter a brace. */
15882 if ((end == CPP_CLOSE_PAREN || depth == 0)
15883 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15884 return;
15885 /* If we've reached the end of the file, stop. */
15886 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15887 return;
15888 /* Consume the next token. */
15889 token = cp_lexer_consume_token (parser->lexer);
15890 /* See if it starts a new group. */
15891 if (token->type == CPP_OPEN_BRACE)
15893 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15894 if (depth == 0)
15895 return;
15897 else if (token->type == CPP_OPEN_PAREN)
15898 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15899 else if (token->type == end)
15900 return;
15904 /* Begin parsing tentatively. We always save tokens while parsing
15905 tentatively so that if the tentative parsing fails we can restore the
15906 tokens. */
15908 static void
15909 cp_parser_parse_tentatively (cp_parser* parser)
15911 /* Enter a new parsing context. */
15912 parser->context = cp_parser_context_new (parser->context);
15913 /* Begin saving tokens. */
15914 cp_lexer_save_tokens (parser->lexer);
15915 /* In order to avoid repetitive access control error messages,
15916 access checks are queued up until we are no longer parsing
15917 tentatively. */
15918 push_deferring_access_checks (dk_deferred);
15921 /* Commit to the currently active tentative parse. */
15923 static void
15924 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15926 cp_parser_context *context;
15927 cp_lexer *lexer;
15929 /* Mark all of the levels as committed. */
15930 lexer = parser->lexer;
15931 for (context = parser->context; context->next; context = context->next)
15933 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15934 break;
15935 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15936 while (!cp_lexer_saving_tokens (lexer))
15937 lexer = lexer->next;
15938 cp_lexer_commit_tokens (lexer);
15942 /* Abort the currently active tentative parse. All consumed tokens
15943 will be rolled back, and no diagnostics will be issued. */
15945 static void
15946 cp_parser_abort_tentative_parse (cp_parser* parser)
15948 cp_parser_simulate_error (parser);
15949 /* Now, pretend that we want to see if the construct was
15950 successfully parsed. */
15951 cp_parser_parse_definitely (parser);
15954 /* Stop parsing tentatively. If a parse error has occurred, restore the
15955 token stream. Otherwise, commit to the tokens we have consumed.
15956 Returns true if no error occurred; false otherwise. */
15958 static bool
15959 cp_parser_parse_definitely (cp_parser* parser)
15961 bool error_occurred;
15962 cp_parser_context *context;
15964 /* Remember whether or not an error occurred, since we are about to
15965 destroy that information. */
15966 error_occurred = cp_parser_error_occurred (parser);
15967 /* Remove the topmost context from the stack. */
15968 context = parser->context;
15969 parser->context = context->next;
15970 /* If no parse errors occurred, commit to the tentative parse. */
15971 if (!error_occurred)
15973 /* Commit to the tokens read tentatively, unless that was
15974 already done. */
15975 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15976 cp_lexer_commit_tokens (parser->lexer);
15978 pop_to_parent_deferring_access_checks ();
15980 /* Otherwise, if errors occurred, roll back our state so that things
15981 are just as they were before we began the tentative parse. */
15982 else
15984 cp_lexer_rollback_tokens (parser->lexer);
15985 pop_deferring_access_checks ();
15987 /* Add the context to the front of the free list. */
15988 context->next = cp_parser_context_free_list;
15989 cp_parser_context_free_list = context;
15991 return !error_occurred;
15994 /* Returns true if we are parsing tentatively and are not committed to
15995 this tentative parse. */
15997 static bool
15998 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16000 return (cp_parser_parsing_tentatively (parser)
16001 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16004 /* Returns nonzero iff an error has occurred during the most recent
16005 tentative parse. */
16007 static bool
16008 cp_parser_error_occurred (cp_parser* parser)
16010 return (cp_parser_parsing_tentatively (parser)
16011 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16014 /* Returns nonzero if GNU extensions are allowed. */
16016 static bool
16017 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16019 return parser->allow_gnu_extensions_p;
16023 /* The parser. */
16025 static GTY (()) cp_parser *the_parser;
16027 /* External interface. */
16029 /* Parse one entire translation unit. */
16031 void
16032 c_parse_file (void)
16034 bool error_occurred;
16035 static bool already_called = false;
16037 if (already_called)
16039 sorry ("inter-module optimizations not implemented for C++");
16040 return;
16042 already_called = true;
16044 the_parser = cp_parser_new ();
16045 push_deferring_access_checks (flag_access_control
16046 ? dk_no_deferred : dk_no_check);
16047 error_occurred = cp_parser_translation_unit (the_parser);
16048 the_parser = NULL;
16051 /* This variable must be provided by every front end. */
16053 int yydebug;
16055 #include "gt-cp-parser.h"