This commit was manufactured by cvs2svn to create branch
[official-gcc.git] / gcc / cp / parser.c
blob82d665ed81044443b1ab56bf5b9247096fa4a308
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 = c_lex_with_flags (&token->value, &token->flags);
384 token->location = input_location;
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;
712 /* Stop emitting debugging information. */
714 static void
715 cp_lexer_stop_debugging (cp_lexer* lexer)
717 --lexer->debugging_p;
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 /* If not parsing tentatively, issue a diagnostic of the form
1791 FILE:LINE: MESSAGE before TOKEN
1792 where TOKEN is the next token in the input stream. MESSAGE
1793 (specified by the caller) is usually of the form "expected
1794 OTHER-TOKEN". */
1796 static void
1797 cp_parser_error (cp_parser* parser, const char* message)
1799 if (!cp_parser_simulate_error (parser))
1801 cp_token *token = cp_lexer_peek_token (parser->lexer);
1802 /* This diagnostic makes more sense if it is tagged to the line
1803 of the token we just peeked at. */
1804 cp_lexer_set_source_position_from_token (token);
1805 if (token->type == CPP_PRAGMA)
1807 error ("%<#pragma%> is not allowed here");
1808 cp_lexer_purge_token (parser->lexer);
1809 return;
1811 c_parse_error (message,
1812 /* Because c_parser_error does not understand
1813 CPP_KEYWORD, keywords are treated like
1814 identifiers. */
1815 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1816 token->value);
1820 /* Issue an error about name-lookup failing. NAME is the
1821 IDENTIFIER_NODE DECL is the result of
1822 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1823 the thing that we hoped to find. */
1825 static void
1826 cp_parser_name_lookup_error (cp_parser* parser,
1827 tree name,
1828 tree decl,
1829 const char* desired)
1831 /* If name lookup completely failed, tell the user that NAME was not
1832 declared. */
1833 if (decl == error_mark_node)
1835 if (parser->scope && parser->scope != global_namespace)
1836 error ("%<%D::%D%> has not been declared",
1837 parser->scope, name);
1838 else if (parser->scope == global_namespace)
1839 error ("%<::%D%> has not been declared", name);
1840 else if (parser->object_scope
1841 && !CLASS_TYPE_P (parser->object_scope))
1842 error ("request for member %qD in non-class type %qT",
1843 name, parser->object_scope);
1844 else if (parser->object_scope)
1845 error ("%<%T::%D%> has not been declared",
1846 parser->object_scope, name);
1847 else
1848 error ("%qD has not been declared", name);
1850 else if (parser->scope && parser->scope != global_namespace)
1851 error ("%<%D::%D%> %s", parser->scope, name, desired);
1852 else if (parser->scope == global_namespace)
1853 error ("%<::%D%> %s", name, desired);
1854 else
1855 error ("%qD %s", name, desired);
1858 /* If we are parsing tentatively, remember that an error has occurred
1859 during this tentative parse. Returns true if the error was
1860 simulated; false if a message should be issued by the caller. */
1862 static bool
1863 cp_parser_simulate_error (cp_parser* parser)
1865 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1867 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1868 return true;
1870 return false;
1873 /* This function is called when a type is defined. If type
1874 definitions are forbidden at this point, an error message is
1875 issued. */
1877 static void
1878 cp_parser_check_type_definition (cp_parser* parser)
1880 /* If types are forbidden here, issue a message. */
1881 if (parser->type_definition_forbidden_message)
1882 /* Use `%s' to print the string in case there are any escape
1883 characters in the message. */
1884 error ("%s", parser->type_definition_forbidden_message);
1887 /* This function is called when the DECLARATOR is processed. The TYPE
1888 was a type defined in the decl-specifiers. If it is invalid to
1889 define a type in the decl-specifiers for DECLARATOR, an error is
1890 issued. */
1892 static void
1893 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1894 tree type)
1896 /* [dcl.fct] forbids type definitions in return types.
1897 Unfortunately, it's not easy to know whether or not we are
1898 processing a return type until after the fact. */
1899 while (declarator
1900 && (declarator->kind == cdk_pointer
1901 || declarator->kind == cdk_reference
1902 || declarator->kind == cdk_ptrmem))
1903 declarator = declarator->declarator;
1904 if (declarator
1905 && declarator->kind == cdk_function)
1907 error ("new types may not be defined in a return type");
1908 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1909 type);
1913 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1914 "<" in any valid C++ program. If the next token is indeed "<",
1915 issue a message warning the user about what appears to be an
1916 invalid attempt to form a template-id. */
1918 static void
1919 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1920 tree type)
1922 cp_token_position start = 0;
1924 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1926 if (TYPE_P (type))
1927 error ("%qT is not a template", type);
1928 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1929 error ("%qE is not a template", type);
1930 else
1931 error ("invalid template-id");
1932 /* Remember the location of the invalid "<". */
1933 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1934 start = cp_lexer_token_position (parser->lexer, true);
1935 /* Consume the "<". */
1936 cp_lexer_consume_token (parser->lexer);
1937 /* Parse the template arguments. */
1938 cp_parser_enclosed_template_argument_list (parser);
1939 /* Permanently remove the invalid template arguments so that
1940 this error message is not issued again. */
1941 if (start)
1942 cp_lexer_purge_tokens_after (parser->lexer, start);
1946 /* If parsing an integral constant-expression, issue an error message
1947 about the fact that THING appeared and return true. Otherwise,
1948 return false. In either case, set
1949 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
1951 static bool
1952 cp_parser_non_integral_constant_expression (cp_parser *parser,
1953 const char *thing)
1955 parser->non_integral_constant_expression_p = true;
1956 if (parser->integral_constant_expression_p)
1958 if (!parser->allow_non_integral_constant_expression_p)
1960 error ("%s cannot appear in a constant-expression", thing);
1961 return true;
1964 return false;
1967 /* Emit a diagnostic for an invalid type name. SCOPE is the
1968 qualifying scope (or NULL, if none) for ID. This function commits
1969 to the current active tentative parse, if any. (Otherwise, the
1970 problematic construct might be encountered again later, resulting
1971 in duplicate error messages.) */
1973 static void
1974 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1976 tree decl, old_scope;
1977 /* Try to lookup the identifier. */
1978 old_scope = parser->scope;
1979 parser->scope = scope;
1980 decl = cp_parser_lookup_name_simple (parser, id);
1981 parser->scope = old_scope;
1982 /* If the lookup found a template-name, it means that the user forgot
1983 to specify an argument list. Emit an useful error message. */
1984 if (TREE_CODE (decl) == TEMPLATE_DECL)
1985 error ("invalid use of template-name %qE without an argument list",
1986 decl);
1987 else if (!parser->scope)
1989 /* Issue an error message. */
1990 error ("%qE does not name a type", id);
1991 /* If we're in a template class, it's possible that the user was
1992 referring to a type from a base class. For example:
1994 template <typename T> struct A { typedef T X; };
1995 template <typename T> struct B : public A<T> { X x; };
1997 The user should have said "typename A<T>::X". */
1998 if (processing_template_decl && current_class_type)
2000 tree b;
2002 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2004 b = TREE_CHAIN (b))
2006 tree base_type = BINFO_TYPE (b);
2007 if (CLASS_TYPE_P (base_type)
2008 && dependent_type_p (base_type))
2010 tree field;
2011 /* Go from a particular instantiation of the
2012 template (which will have an empty TYPE_FIELDs),
2013 to the main version. */
2014 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2015 for (field = TYPE_FIELDS (base_type);
2016 field;
2017 field = TREE_CHAIN (field))
2018 if (TREE_CODE (field) == TYPE_DECL
2019 && DECL_NAME (field) == id)
2021 inform ("(perhaps %<typename %T::%E%> was intended)",
2022 BINFO_TYPE (b), id);
2023 break;
2025 if (field)
2026 break;
2031 /* Here we diagnose qualified-ids where the scope is actually correct,
2032 but the identifier does not resolve to a valid type name. */
2033 else
2035 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2036 error ("%qE in namespace %qE does not name a type",
2037 id, parser->scope);
2038 else if (TYPE_P (parser->scope))
2039 error ("%qE in class %qT does not name a type", id, parser->scope);
2040 else
2041 gcc_unreachable ();
2043 cp_parser_commit_to_tentative_parse (parser);
2046 /* Check for a common situation where a type-name should be present,
2047 but is not, and issue a sensible error message. Returns true if an
2048 invalid type-name was detected.
2050 The situation handled by this function are variable declarations of the
2051 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2052 Usually, `ID' should name a type, but if we got here it means that it
2053 does not. We try to emit the best possible error message depending on
2054 how exactly the id-expression looks like.
2057 static bool
2058 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2060 tree id;
2062 cp_parser_parse_tentatively (parser);
2063 id = cp_parser_id_expression (parser,
2064 /*template_keyword_p=*/false,
2065 /*check_dependency_p=*/true,
2066 /*template_p=*/NULL,
2067 /*declarator_p=*/true);
2068 /* After the id-expression, there should be a plain identifier,
2069 otherwise this is not a simple variable declaration. Also, if
2070 the scope is dependent, we cannot do much. */
2071 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2072 || (parser->scope && TYPE_P (parser->scope)
2073 && dependent_type_p (parser->scope)))
2075 cp_parser_abort_tentative_parse (parser);
2076 return false;
2078 if (!cp_parser_parse_definitely (parser)
2079 || TREE_CODE (id) != IDENTIFIER_NODE)
2080 return false;
2082 /* Emit a diagnostic for the invalid type. */
2083 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2084 /* Skip to the end of the declaration; there's no point in
2085 trying to process it. */
2086 cp_parser_skip_to_end_of_block_or_statement (parser);
2087 return true;
2090 /* Consume tokens up to, and including, the next non-nested closing `)'.
2091 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2092 are doing error recovery. Returns -1 if OR_COMMA is true and we
2093 found an unnested comma. */
2095 static int
2096 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2097 bool recovering,
2098 bool or_comma,
2099 bool consume_paren)
2101 unsigned paren_depth = 0;
2102 unsigned brace_depth = 0;
2103 int result;
2105 if (recovering && !or_comma
2106 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2107 return 0;
2109 while (true)
2111 cp_token *token;
2113 /* If we've run out of tokens, then there is no closing `)'. */
2114 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2116 result = 0;
2117 break;
2120 token = cp_lexer_peek_token (parser->lexer);
2122 /* This matches the processing in skip_to_end_of_statement. */
2123 if (token->type == CPP_SEMICOLON && !brace_depth)
2125 result = 0;
2126 break;
2128 if (token->type == CPP_OPEN_BRACE)
2129 ++brace_depth;
2130 if (token->type == CPP_CLOSE_BRACE)
2132 if (!brace_depth--)
2134 result = 0;
2135 break;
2138 if (recovering && or_comma && token->type == CPP_COMMA
2139 && !brace_depth && !paren_depth)
2141 result = -1;
2142 break;
2145 if (!brace_depth)
2147 /* If it is an `(', we have entered another level of nesting. */
2148 if (token->type == CPP_OPEN_PAREN)
2149 ++paren_depth;
2150 /* If it is a `)', then we might be done. */
2151 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2153 if (consume_paren)
2154 cp_lexer_consume_token (parser->lexer);
2156 result = 1;
2157 break;
2162 /* Consume the token. */
2163 cp_lexer_consume_token (parser->lexer);
2166 return result;
2169 /* Consume tokens until we reach the end of the current statement.
2170 Normally, that will be just before consuming a `;'. However, if a
2171 non-nested `}' comes first, then we stop before consuming that. */
2173 static void
2174 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2176 unsigned nesting_depth = 0;
2178 while (true)
2180 cp_token *token;
2182 /* Peek at the next token. */
2183 token = cp_lexer_peek_token (parser->lexer);
2184 /* If we've run out of tokens, stop. */
2185 if (token->type == CPP_EOF)
2186 break;
2187 /* If the next token is a `;', we have reached the end of the
2188 statement. */
2189 if (token->type == CPP_SEMICOLON && !nesting_depth)
2190 break;
2191 /* If the next token is a non-nested `}', then we have reached
2192 the end of the current block. */
2193 if (token->type == CPP_CLOSE_BRACE)
2195 /* If this is a non-nested `}', stop before consuming it.
2196 That way, when confronted with something like:
2198 { 3 + }
2200 we stop before consuming the closing `}', even though we
2201 have not yet reached a `;'. */
2202 if (nesting_depth == 0)
2203 break;
2204 /* If it is the closing `}' for a block that we have
2205 scanned, stop -- but only after consuming the token.
2206 That way given:
2208 void f g () { ... }
2209 typedef int I;
2211 we will stop after the body of the erroneously declared
2212 function, but before consuming the following `typedef'
2213 declaration. */
2214 if (--nesting_depth == 0)
2216 cp_lexer_consume_token (parser->lexer);
2217 break;
2220 /* If it the next token is a `{', then we are entering a new
2221 block. Consume the entire block. */
2222 else if (token->type == CPP_OPEN_BRACE)
2223 ++nesting_depth;
2224 /* Consume the token. */
2225 cp_lexer_consume_token (parser->lexer);
2229 /* This function is called at the end of a statement or declaration.
2230 If the next token is a semicolon, it is consumed; otherwise, error
2231 recovery is attempted. */
2233 static void
2234 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2236 /* Look for the trailing `;'. */
2237 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2239 /* If there is additional (erroneous) input, skip to the end of
2240 the statement. */
2241 cp_parser_skip_to_end_of_statement (parser);
2242 /* If the next token is now a `;', consume it. */
2243 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2244 cp_lexer_consume_token (parser->lexer);
2248 /* Skip tokens until we have consumed an entire block, or until we
2249 have consumed a non-nested `;'. */
2251 static void
2252 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2254 unsigned nesting_depth = 0;
2256 while (true)
2258 cp_token *token;
2260 /* Peek at the next token. */
2261 token = cp_lexer_peek_token (parser->lexer);
2262 /* If we've run out of tokens, stop. */
2263 if (token->type == CPP_EOF)
2264 break;
2265 /* If the next token is a `;', we have reached the end of the
2266 statement. */
2267 if (token->type == CPP_SEMICOLON && !nesting_depth)
2269 /* Consume the `;'. */
2270 cp_lexer_consume_token (parser->lexer);
2271 break;
2273 /* Consume the token. */
2274 token = cp_lexer_consume_token (parser->lexer);
2275 /* If the next token is a non-nested `}', then we have reached
2276 the end of the current block. */
2277 if (token->type == CPP_CLOSE_BRACE
2278 && (nesting_depth == 0 || --nesting_depth == 0))
2279 break;
2280 /* If it the next token is a `{', then we are entering a new
2281 block. Consume the entire block. */
2282 if (token->type == CPP_OPEN_BRACE)
2283 ++nesting_depth;
2287 /* Skip tokens until a non-nested closing curly brace is the next
2288 token. */
2290 static void
2291 cp_parser_skip_to_closing_brace (cp_parser *parser)
2293 unsigned nesting_depth = 0;
2295 while (true)
2297 cp_token *token;
2299 /* Peek at the next token. */
2300 token = cp_lexer_peek_token (parser->lexer);
2301 /* If we've run out of tokens, stop. */
2302 if (token->type == CPP_EOF)
2303 break;
2304 /* If the next token is a non-nested `}', then we have reached
2305 the end of the current block. */
2306 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2307 break;
2308 /* If it the next token is a `{', then we are entering a new
2309 block. Consume the entire block. */
2310 else if (token->type == CPP_OPEN_BRACE)
2311 ++nesting_depth;
2312 /* Consume the token. */
2313 cp_lexer_consume_token (parser->lexer);
2317 /* This is a simple wrapper around make_typename_type. When the id is
2318 an unresolved identifier node, we can provide a superior diagnostic
2319 using cp_parser_diagnose_invalid_type_name. */
2321 static tree
2322 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2324 tree result;
2325 if (TREE_CODE (id) == IDENTIFIER_NODE)
2327 result = make_typename_type (scope, id, typename_type,
2328 /*complain=*/0);
2329 if (result == error_mark_node)
2330 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2331 return result;
2333 return make_typename_type (scope, id, typename_type, tf_error);
2337 /* Create a new C++ parser. */
2339 static cp_parser *
2340 cp_parser_new (void)
2342 cp_parser *parser;
2343 cp_lexer *lexer;
2344 unsigned i;
2346 /* cp_lexer_new_main is called before calling ggc_alloc because
2347 cp_lexer_new_main might load a PCH file. */
2348 lexer = cp_lexer_new_main ();
2350 /* Initialize the binops_by_token so that we can get the tree
2351 directly from the token. */
2352 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2353 binops_by_token[binops[i].token_type] = binops[i];
2355 parser = GGC_CNEW (cp_parser);
2356 parser->lexer = lexer;
2357 parser->context = cp_parser_context_new (NULL);
2359 /* For now, we always accept GNU extensions. */
2360 parser->allow_gnu_extensions_p = 1;
2362 /* The `>' token is a greater-than operator, not the end of a
2363 template-id. */
2364 parser->greater_than_is_operator_p = true;
2366 parser->default_arg_ok_p = true;
2368 /* We are not parsing a constant-expression. */
2369 parser->integral_constant_expression_p = false;
2370 parser->allow_non_integral_constant_expression_p = false;
2371 parser->non_integral_constant_expression_p = false;
2373 /* Local variable names are not forbidden. */
2374 parser->local_variables_forbidden_p = false;
2376 /* We are not processing an `extern "C"' declaration. */
2377 parser->in_unbraced_linkage_specification_p = false;
2379 /* We are not processing a declarator. */
2380 parser->in_declarator_p = false;
2382 /* We are not processing a template-argument-list. */
2383 parser->in_template_argument_list_p = false;
2385 /* We are not in an iteration statement. */
2386 parser->in_iteration_statement_p = false;
2388 /* We are not in a switch statement. */
2389 parser->in_switch_statement_p = false;
2391 /* We are not parsing a type-id inside an expression. */
2392 parser->in_type_id_in_expr_p = false;
2394 /* Declarations aren't implicitly extern "C". */
2395 parser->implicit_extern_c = false;
2397 /* String literals should be translated to the execution character set. */
2398 parser->translate_strings_p = true;
2400 /* The unparsed function queue is empty. */
2401 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2403 /* There are no classes being defined. */
2404 parser->num_classes_being_defined = 0;
2406 /* No template parameters apply. */
2407 parser->num_template_parameter_lists = 0;
2409 return parser;
2412 /* Create a cp_lexer structure which will emit the tokens in CACHE
2413 and push it onto the parser's lexer stack. This is used for delayed
2414 parsing of in-class method bodies and default arguments, and should
2415 not be confused with tentative parsing. */
2416 static void
2417 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2419 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2420 lexer->next = parser->lexer;
2421 parser->lexer = lexer;
2423 /* Move the current source position to that of the first token in the
2424 new lexer. */
2425 cp_lexer_set_source_position_from_token (lexer->next_token);
2428 /* Pop the top lexer off the parser stack. This is never used for the
2429 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2430 static void
2431 cp_parser_pop_lexer (cp_parser *parser)
2433 cp_lexer *lexer = parser->lexer;
2434 parser->lexer = lexer->next;
2435 cp_lexer_destroy (lexer);
2437 /* Put the current source position back where it was before this
2438 lexer was pushed. */
2439 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2442 /* Lexical conventions [gram.lex] */
2444 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2445 identifier. */
2447 static tree
2448 cp_parser_identifier (cp_parser* parser)
2450 cp_token *token;
2452 /* Look for the identifier. */
2453 token = cp_parser_require (parser, CPP_NAME, "identifier");
2454 /* Return the value. */
2455 return token ? token->value : error_mark_node;
2458 /* Parse a sequence of adjacent string constants. Returns a
2459 TREE_STRING representing the combined, nul-terminated string
2460 constant. If TRANSLATE is true, translate the string to the
2461 execution character set. If WIDE_OK is true, a wide string is
2462 invalid here.
2464 C++98 [lex.string] says that if a narrow string literal token is
2465 adjacent to a wide string literal token, the behavior is undefined.
2466 However, C99 6.4.5p4 says that this results in a wide string literal.
2467 We follow C99 here, for consistency with the C front end.
2469 This code is largely lifted from lex_string() in c-lex.c.
2471 FUTURE: ObjC++ will need to handle @-strings here. */
2472 static tree
2473 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2475 tree value;
2476 bool wide = false;
2477 size_t count;
2478 struct obstack str_ob;
2479 cpp_string str, istr, *strs;
2480 cp_token *tok;
2482 tok = cp_lexer_peek_token (parser->lexer);
2483 if (!cp_parser_is_string_literal (tok))
2485 cp_parser_error (parser, "expected string-literal");
2486 return error_mark_node;
2489 /* Try to avoid the overhead of creating and destroying an obstack
2490 for the common case of just one string. */
2491 if (!cp_parser_is_string_literal
2492 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2494 cp_lexer_consume_token (parser->lexer);
2496 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2497 str.len = TREE_STRING_LENGTH (tok->value);
2498 count = 1;
2499 if (tok->type == CPP_WSTRING)
2500 wide = true;
2502 strs = &str;
2504 else
2506 gcc_obstack_init (&str_ob);
2507 count = 0;
2511 cp_lexer_consume_token (parser->lexer);
2512 count++;
2513 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2514 str.len = TREE_STRING_LENGTH (tok->value);
2515 if (tok->type == CPP_WSTRING)
2516 wide = true;
2518 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2520 tok = cp_lexer_peek_token (parser->lexer);
2522 while (cp_parser_is_string_literal (tok));
2524 strs = (cpp_string *) obstack_finish (&str_ob);
2527 if (wide && !wide_ok)
2529 cp_parser_error (parser, "a wide string is invalid in this context");
2530 wide = false;
2533 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2534 (parse_in, strs, count, &istr, wide))
2536 value = build_string (istr.len, (char *)istr.text);
2537 free ((void *)istr.text);
2539 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2540 value = fix_string_type (value);
2542 else
2543 /* cpp_interpret_string has issued an error. */
2544 value = error_mark_node;
2546 if (count > 1)
2547 obstack_free (&str_ob, 0);
2549 return value;
2553 /* Basic concepts [gram.basic] */
2555 /* Parse a translation-unit.
2557 translation-unit:
2558 declaration-seq [opt]
2560 Returns TRUE if all went well. */
2562 static bool
2563 cp_parser_translation_unit (cp_parser* parser)
2565 /* The address of the first non-permanent object on the declarator
2566 obstack. */
2567 static void *declarator_obstack_base;
2569 bool success;
2571 /* Create the declarator obstack, if necessary. */
2572 if (!cp_error_declarator)
2574 gcc_obstack_init (&declarator_obstack);
2575 /* Create the error declarator. */
2576 cp_error_declarator = make_declarator (cdk_error);
2577 /* Create the empty parameter list. */
2578 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2579 /* Remember where the base of the declarator obstack lies. */
2580 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2583 while (true)
2585 cp_parser_declaration_seq_opt (parser);
2587 /* If there are no tokens left then all went well. */
2588 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2590 /* Get rid of the token array; we don't need it any more. */
2591 cp_lexer_destroy (parser->lexer);
2592 parser->lexer = NULL;
2594 /* This file might have been a context that's implicitly extern
2595 "C". If so, pop the lang context. (Only relevant for PCH.) */
2596 if (parser->implicit_extern_c)
2598 pop_lang_context ();
2599 parser->implicit_extern_c = false;
2602 /* Finish up. */
2603 finish_translation_unit ();
2605 success = true;
2606 break;
2608 else
2610 cp_parser_error (parser, "expected declaration");
2611 success = false;
2612 break;
2616 /* Make sure the declarator obstack was fully cleaned up. */
2617 gcc_assert (obstack_next_free (&declarator_obstack)
2618 == declarator_obstack_base);
2620 /* All went well. */
2621 return success;
2624 /* Expressions [gram.expr] */
2626 /* Parse a primary-expression.
2628 primary-expression:
2629 literal
2630 this
2631 ( expression )
2632 id-expression
2634 GNU Extensions:
2636 primary-expression:
2637 ( compound-statement )
2638 __builtin_va_arg ( assignment-expression , type-id )
2640 literal:
2641 __null
2643 CAST_P is true if this primary expression is the target of a cast.
2645 Returns a representation of the expression.
2647 *IDK indicates what kind of id-expression (if any) was present.
2649 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2650 used as the operand of a pointer-to-member. In that case,
2651 *QUALIFYING_CLASS gives the class that is used as the qualifying
2652 class in the pointer-to-member. */
2654 static tree
2655 cp_parser_primary_expression (cp_parser *parser,
2656 bool cast_p,
2657 cp_id_kind *idk,
2658 tree *qualifying_class)
2660 cp_token *token;
2662 /* Assume the primary expression is not an id-expression. */
2663 *idk = CP_ID_KIND_NONE;
2664 /* And that it cannot be used as pointer-to-member. */
2665 *qualifying_class = NULL_TREE;
2667 /* Peek at the next token. */
2668 token = cp_lexer_peek_token (parser->lexer);
2669 switch (token->type)
2671 /* literal:
2672 integer-literal
2673 character-literal
2674 floating-literal
2675 string-literal
2676 boolean-literal */
2677 case CPP_CHAR:
2678 case CPP_WCHAR:
2679 case CPP_NUMBER:
2680 token = cp_lexer_consume_token (parser->lexer);
2681 /* Floating-point literals are only allowed in an integral
2682 constant expression if they are cast to an integral or
2683 enumeration type. */
2684 if (TREE_CODE (token->value) == REAL_CST
2685 && parser->integral_constant_expression_p
2686 && pedantic)
2688 /* CAST_P will be set even in invalid code like "int(2.7 +
2689 ...)". Therefore, we have to check that the next token
2690 is sure to end the cast. */
2691 if (cast_p)
2693 cp_token *next_token;
2695 next_token = cp_lexer_peek_token (parser->lexer);
2696 if (/* The comma at the end of an
2697 enumerator-definition. */
2698 next_token->type != CPP_COMMA
2699 /* The curly brace at the end of an enum-specifier. */
2700 && next_token->type != CPP_CLOSE_BRACE
2701 /* The end of a statement. */
2702 && next_token->type != CPP_SEMICOLON
2703 /* The end of the cast-expression. */
2704 && next_token->type != CPP_CLOSE_PAREN
2705 /* The end of an array bound. */
2706 && next_token->type != CPP_CLOSE_SQUARE)
2707 cast_p = false;
2710 /* If we are within a cast, then the constraint that the
2711 cast is to an integral or enumeration type will be
2712 checked at that point. If we are not within a cast, then
2713 this code is invalid. */
2714 if (!cast_p)
2715 cp_parser_non_integral_constant_expression
2716 (parser, "floating-point literal");
2718 return token->value;
2720 case CPP_STRING:
2721 case CPP_WSTRING:
2722 /* ??? Should wide strings be allowed when parser->translate_strings_p
2723 is false (i.e. in attributes)? If not, we can kill the third
2724 argument to cp_parser_string_literal. */
2725 return cp_parser_string_literal (parser,
2726 parser->translate_strings_p,
2727 true);
2729 case CPP_OPEN_PAREN:
2731 tree expr;
2732 bool saved_greater_than_is_operator_p;
2734 /* Consume the `('. */
2735 cp_lexer_consume_token (parser->lexer);
2736 /* Within a parenthesized expression, a `>' token is always
2737 the greater-than operator. */
2738 saved_greater_than_is_operator_p
2739 = parser->greater_than_is_operator_p;
2740 parser->greater_than_is_operator_p = true;
2741 /* If we see `( { ' then we are looking at the beginning of
2742 a GNU statement-expression. */
2743 if (cp_parser_allow_gnu_extensions_p (parser)
2744 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2746 /* Statement-expressions are not allowed by the standard. */
2747 if (pedantic)
2748 pedwarn ("ISO C++ forbids braced-groups within expressions");
2750 /* And they're not allowed outside of a function-body; you
2751 cannot, for example, write:
2753 int i = ({ int j = 3; j + 1; });
2755 at class or namespace scope. */
2756 if (!at_function_scope_p ())
2757 error ("statement-expressions are allowed only inside functions");
2758 /* Start the statement-expression. */
2759 expr = begin_stmt_expr ();
2760 /* Parse the compound-statement. */
2761 cp_parser_compound_statement (parser, expr, false);
2762 /* Finish up. */
2763 expr = finish_stmt_expr (expr, false);
2765 else
2767 /* Parse the parenthesized expression. */
2768 expr = cp_parser_expression (parser, cast_p);
2769 /* Let the front end know that this expression was
2770 enclosed in parentheses. This matters in case, for
2771 example, the expression is of the form `A::B', since
2772 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2773 not. */
2774 finish_parenthesized_expr (expr);
2776 /* The `>' token might be the end of a template-id or
2777 template-parameter-list now. */
2778 parser->greater_than_is_operator_p
2779 = saved_greater_than_is_operator_p;
2780 /* Consume the `)'. */
2781 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2782 cp_parser_skip_to_end_of_statement (parser);
2784 return expr;
2787 case CPP_KEYWORD:
2788 switch (token->keyword)
2790 /* These two are the boolean literals. */
2791 case RID_TRUE:
2792 cp_lexer_consume_token (parser->lexer);
2793 return boolean_true_node;
2794 case RID_FALSE:
2795 cp_lexer_consume_token (parser->lexer);
2796 return boolean_false_node;
2798 /* The `__null' literal. */
2799 case RID_NULL:
2800 cp_lexer_consume_token (parser->lexer);
2801 return null_node;
2803 /* Recognize the `this' keyword. */
2804 case RID_THIS:
2805 cp_lexer_consume_token (parser->lexer);
2806 if (parser->local_variables_forbidden_p)
2808 error ("%<this%> may not be used in this context");
2809 return error_mark_node;
2811 /* Pointers cannot appear in constant-expressions. */
2812 if (cp_parser_non_integral_constant_expression (parser,
2813 "`this'"))
2814 return error_mark_node;
2815 return finish_this_expr ();
2817 /* The `operator' keyword can be the beginning of an
2818 id-expression. */
2819 case RID_OPERATOR:
2820 goto id_expression;
2822 case RID_FUNCTION_NAME:
2823 case RID_PRETTY_FUNCTION_NAME:
2824 case RID_C99_FUNCTION_NAME:
2825 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2826 __func__ are the names of variables -- but they are
2827 treated specially. Therefore, they are handled here,
2828 rather than relying on the generic id-expression logic
2829 below. Grammatically, these names are id-expressions.
2831 Consume the token. */
2832 token = cp_lexer_consume_token (parser->lexer);
2833 /* Look up the name. */
2834 return finish_fname (token->value);
2836 case RID_VA_ARG:
2838 tree expression;
2839 tree type;
2841 /* The `__builtin_va_arg' construct is used to handle
2842 `va_arg'. Consume the `__builtin_va_arg' token. */
2843 cp_lexer_consume_token (parser->lexer);
2844 /* Look for the opening `('. */
2845 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2846 /* Now, parse the assignment-expression. */
2847 expression = cp_parser_assignment_expression (parser,
2848 /*cast_p=*/false);
2849 /* Look for the `,'. */
2850 cp_parser_require (parser, CPP_COMMA, "`,'");
2851 /* Parse the type-id. */
2852 type = cp_parser_type_id (parser);
2853 /* Look for the closing `)'. */
2854 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2855 /* Using `va_arg' in a constant-expression is not
2856 allowed. */
2857 if (cp_parser_non_integral_constant_expression (parser,
2858 "`va_arg'"))
2859 return error_mark_node;
2860 return build_x_va_arg (expression, type);
2863 case RID_OFFSETOF:
2864 return cp_parser_builtin_offsetof (parser);
2866 default:
2867 cp_parser_error (parser, "expected primary-expression");
2868 return error_mark_node;
2871 /* An id-expression can start with either an identifier, a
2872 `::' as the beginning of a qualified-id, or the "operator"
2873 keyword. */
2874 case CPP_NAME:
2875 case CPP_SCOPE:
2876 case CPP_TEMPLATE_ID:
2877 case CPP_NESTED_NAME_SPECIFIER:
2879 tree id_expression;
2880 tree decl;
2881 const char *error_msg;
2883 id_expression:
2884 /* Parse the id-expression. */
2885 id_expression
2886 = cp_parser_id_expression (parser,
2887 /*template_keyword_p=*/false,
2888 /*check_dependency_p=*/true,
2889 /*template_p=*/NULL,
2890 /*declarator_p=*/false);
2891 if (id_expression == error_mark_node)
2892 return error_mark_node;
2893 /* If we have a template-id, then no further lookup is
2894 required. If the template-id was for a template-class, we
2895 will sometimes have a TYPE_DECL at this point. */
2896 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2897 || TREE_CODE (id_expression) == TYPE_DECL)
2898 decl = id_expression;
2899 /* Look up the name. */
2900 else
2902 bool ambiguous_p;
2904 decl = cp_parser_lookup_name (parser, id_expression,
2905 none_type,
2906 /*is_template=*/false,
2907 /*is_namespace=*/false,
2908 /*check_dependency=*/true,
2909 &ambiguous_p);
2910 /* If the lookup was ambiguous, an error will already have
2911 been issued. */
2912 if (ambiguous_p)
2913 return error_mark_node;
2914 /* If name lookup gives us a SCOPE_REF, then the
2915 qualifying scope was dependent. Just propagate the
2916 name. */
2917 if (TREE_CODE (decl) == SCOPE_REF)
2919 if (TYPE_P (TREE_OPERAND (decl, 0)))
2920 *qualifying_class = TREE_OPERAND (decl, 0);
2921 return decl;
2923 /* Check to see if DECL is a local variable in a context
2924 where that is forbidden. */
2925 if (parser->local_variables_forbidden_p
2926 && local_variable_p (decl))
2928 /* It might be that we only found DECL because we are
2929 trying to be generous with pre-ISO scoping rules.
2930 For example, consider:
2932 int i;
2933 void g() {
2934 for (int i = 0; i < 10; ++i) {}
2935 extern void f(int j = i);
2938 Here, name look up will originally find the out
2939 of scope `i'. We need to issue a warning message,
2940 but then use the global `i'. */
2941 decl = check_for_out_of_scope_variable (decl);
2942 if (local_variable_p (decl))
2944 error ("local variable %qD may not appear in this context",
2945 decl);
2946 return error_mark_node;
2951 decl = finish_id_expression (id_expression, decl, parser->scope,
2952 idk, qualifying_class,
2953 parser->integral_constant_expression_p,
2954 parser->allow_non_integral_constant_expression_p,
2955 &parser->non_integral_constant_expression_p,
2956 &error_msg);
2957 if (error_msg)
2958 cp_parser_error (parser, error_msg);
2959 return decl;
2962 /* Anything else is an error. */
2963 default:
2964 cp_parser_error (parser, "expected primary-expression");
2965 return error_mark_node;
2969 /* Parse an id-expression.
2971 id-expression:
2972 unqualified-id
2973 qualified-id
2975 qualified-id:
2976 :: [opt] nested-name-specifier template [opt] unqualified-id
2977 :: identifier
2978 :: operator-function-id
2979 :: template-id
2981 Return a representation of the unqualified portion of the
2982 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2983 a `::' or nested-name-specifier.
2985 Often, if the id-expression was a qualified-id, the caller will
2986 want to make a SCOPE_REF to represent the qualified-id. This
2987 function does not do this in order to avoid wastefully creating
2988 SCOPE_REFs when they are not required.
2990 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2991 `template' keyword.
2993 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2994 uninstantiated templates.
2996 If *TEMPLATE_P is non-NULL, it is set to true iff the
2997 `template' keyword is used to explicitly indicate that the entity
2998 named is a template.
3000 If DECLARATOR_P is true, the id-expression is appearing as part of
3001 a declarator, rather than as part of an expression. */
3003 static tree
3004 cp_parser_id_expression (cp_parser *parser,
3005 bool template_keyword_p,
3006 bool check_dependency_p,
3007 bool *template_p,
3008 bool declarator_p)
3010 bool global_scope_p;
3011 bool nested_name_specifier_p;
3013 /* Assume the `template' keyword was not used. */
3014 if (template_p)
3015 *template_p = false;
3017 /* Look for the optional `::' operator. */
3018 global_scope_p
3019 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3020 != NULL_TREE);
3021 /* Look for the optional nested-name-specifier. */
3022 nested_name_specifier_p
3023 = (cp_parser_nested_name_specifier_opt (parser,
3024 /*typename_keyword_p=*/false,
3025 check_dependency_p,
3026 /*type_p=*/false,
3027 declarator_p)
3028 != NULL_TREE);
3029 /* If there is a nested-name-specifier, then we are looking at
3030 the first qualified-id production. */
3031 if (nested_name_specifier_p)
3033 tree saved_scope;
3034 tree saved_object_scope;
3035 tree saved_qualifying_scope;
3036 tree unqualified_id;
3037 bool is_template;
3039 /* See if the next token is the `template' keyword. */
3040 if (!template_p)
3041 template_p = &is_template;
3042 *template_p = cp_parser_optional_template_keyword (parser);
3043 /* Name lookup we do during the processing of the
3044 unqualified-id might obliterate SCOPE. */
3045 saved_scope = parser->scope;
3046 saved_object_scope = parser->object_scope;
3047 saved_qualifying_scope = parser->qualifying_scope;
3048 /* Process the final unqualified-id. */
3049 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3050 check_dependency_p,
3051 declarator_p);
3052 /* Restore the SAVED_SCOPE for our caller. */
3053 parser->scope = saved_scope;
3054 parser->object_scope = saved_object_scope;
3055 parser->qualifying_scope = saved_qualifying_scope;
3057 return unqualified_id;
3059 /* Otherwise, if we are in global scope, then we are looking at one
3060 of the other qualified-id productions. */
3061 else if (global_scope_p)
3063 cp_token *token;
3064 tree id;
3066 /* Peek at the next token. */
3067 token = cp_lexer_peek_token (parser->lexer);
3069 /* If it's an identifier, and the next token is not a "<", then
3070 we can avoid the template-id case. This is an optimization
3071 for this common case. */
3072 if (token->type == CPP_NAME
3073 && !cp_parser_nth_token_starts_template_argument_list_p
3074 (parser, 2))
3075 return cp_parser_identifier (parser);
3077 cp_parser_parse_tentatively (parser);
3078 /* Try a template-id. */
3079 id = cp_parser_template_id (parser,
3080 /*template_keyword_p=*/false,
3081 /*check_dependency_p=*/true,
3082 declarator_p);
3083 /* If that worked, we're done. */
3084 if (cp_parser_parse_definitely (parser))
3085 return id;
3087 /* Peek at the next token. (Changes in the token buffer may
3088 have invalidated the pointer obtained above.) */
3089 token = cp_lexer_peek_token (parser->lexer);
3091 switch (token->type)
3093 case CPP_NAME:
3094 return cp_parser_identifier (parser);
3096 case CPP_KEYWORD:
3097 if (token->keyword == RID_OPERATOR)
3098 return cp_parser_operator_function_id (parser);
3099 /* Fall through. */
3101 default:
3102 cp_parser_error (parser, "expected id-expression");
3103 return error_mark_node;
3106 else
3107 return cp_parser_unqualified_id (parser, template_keyword_p,
3108 /*check_dependency_p=*/true,
3109 declarator_p);
3112 /* Parse an unqualified-id.
3114 unqualified-id:
3115 identifier
3116 operator-function-id
3117 conversion-function-id
3118 ~ class-name
3119 template-id
3121 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3122 keyword, in a construct like `A::template ...'.
3124 Returns a representation of unqualified-id. For the `identifier'
3125 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3126 production a BIT_NOT_EXPR is returned; the operand of the
3127 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3128 other productions, see the documentation accompanying the
3129 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3130 names are looked up in uninstantiated templates. If DECLARATOR_P
3131 is true, the unqualified-id is appearing as part of a declarator,
3132 rather than as part of an expression. */
3134 static tree
3135 cp_parser_unqualified_id (cp_parser* parser,
3136 bool template_keyword_p,
3137 bool check_dependency_p,
3138 bool declarator_p)
3140 cp_token *token;
3142 /* Peek at the next token. */
3143 token = cp_lexer_peek_token (parser->lexer);
3145 switch (token->type)
3147 case CPP_NAME:
3149 tree id;
3151 /* We don't know yet whether or not this will be a
3152 template-id. */
3153 cp_parser_parse_tentatively (parser);
3154 /* Try a template-id. */
3155 id = cp_parser_template_id (parser, template_keyword_p,
3156 check_dependency_p,
3157 declarator_p);
3158 /* If it worked, we're done. */
3159 if (cp_parser_parse_definitely (parser))
3160 return id;
3161 /* Otherwise, it's an ordinary identifier. */
3162 return cp_parser_identifier (parser);
3165 case CPP_TEMPLATE_ID:
3166 return cp_parser_template_id (parser, template_keyword_p,
3167 check_dependency_p,
3168 declarator_p);
3170 case CPP_COMPL:
3172 tree type_decl;
3173 tree qualifying_scope;
3174 tree object_scope;
3175 tree scope;
3177 /* Consume the `~' token. */
3178 cp_lexer_consume_token (parser->lexer);
3179 /* Parse the class-name. The standard, as written, seems to
3180 say that:
3182 template <typename T> struct S { ~S (); };
3183 template <typename T> S<T>::~S() {}
3185 is invalid, since `~' must be followed by a class-name, but
3186 `S<T>' is dependent, and so not known to be a class.
3187 That's not right; we need to look in uninstantiated
3188 templates. A further complication arises from:
3190 template <typename T> void f(T t) {
3191 t.T::~T();
3194 Here, it is not possible to look up `T' in the scope of `T'
3195 itself. We must look in both the current scope, and the
3196 scope of the containing complete expression.
3198 Yet another issue is:
3200 struct S {
3201 int S;
3202 ~S();
3205 S::~S() {}
3207 The standard does not seem to say that the `S' in `~S'
3208 should refer to the type `S' and not the data member
3209 `S::S'. */
3211 /* DR 244 says that we look up the name after the "~" in the
3212 same scope as we looked up the qualifying name. That idea
3213 isn't fully worked out; it's more complicated than that. */
3214 scope = parser->scope;
3215 object_scope = parser->object_scope;
3216 qualifying_scope = parser->qualifying_scope;
3218 /* If the name is of the form "X::~X" it's OK. */
3219 if (scope && TYPE_P (scope)
3220 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3221 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3222 == CPP_OPEN_PAREN)
3223 && (cp_lexer_peek_token (parser->lexer)->value
3224 == TYPE_IDENTIFIER (scope)))
3226 cp_lexer_consume_token (parser->lexer);
3227 return build_nt (BIT_NOT_EXPR, scope);
3230 /* If there was an explicit qualification (S::~T), first look
3231 in the scope given by the qualification (i.e., S). */
3232 if (scope)
3234 cp_parser_parse_tentatively (parser);
3235 type_decl = cp_parser_class_name (parser,
3236 /*typename_keyword_p=*/false,
3237 /*template_keyword_p=*/false,
3238 none_type,
3239 /*check_dependency=*/false,
3240 /*class_head_p=*/false,
3241 declarator_p);
3242 if (cp_parser_parse_definitely (parser))
3243 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3245 /* In "N::S::~S", look in "N" as well. */
3246 if (scope && qualifying_scope)
3248 cp_parser_parse_tentatively (parser);
3249 parser->scope = qualifying_scope;
3250 parser->object_scope = NULL_TREE;
3251 parser->qualifying_scope = NULL_TREE;
3252 type_decl
3253 = cp_parser_class_name (parser,
3254 /*typename_keyword_p=*/false,
3255 /*template_keyword_p=*/false,
3256 none_type,
3257 /*check_dependency=*/false,
3258 /*class_head_p=*/false,
3259 declarator_p);
3260 if (cp_parser_parse_definitely (parser))
3261 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3263 /* In "p->S::~T", look in the scope given by "*p" as well. */
3264 else if (object_scope)
3266 cp_parser_parse_tentatively (parser);
3267 parser->scope = object_scope;
3268 parser->object_scope = NULL_TREE;
3269 parser->qualifying_scope = NULL_TREE;
3270 type_decl
3271 = cp_parser_class_name (parser,
3272 /*typename_keyword_p=*/false,
3273 /*template_keyword_p=*/false,
3274 none_type,
3275 /*check_dependency=*/false,
3276 /*class_head_p=*/false,
3277 declarator_p);
3278 if (cp_parser_parse_definitely (parser))
3279 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3281 /* Look in the surrounding context. */
3282 parser->scope = NULL_TREE;
3283 parser->object_scope = NULL_TREE;
3284 parser->qualifying_scope = NULL_TREE;
3285 type_decl
3286 = cp_parser_class_name (parser,
3287 /*typename_keyword_p=*/false,
3288 /*template_keyword_p=*/false,
3289 none_type,
3290 /*check_dependency=*/false,
3291 /*class_head_p=*/false,
3292 declarator_p);
3293 /* If an error occurred, assume that the name of the
3294 destructor is the same as the name of the qualifying
3295 class. That allows us to keep parsing after running
3296 into ill-formed destructor names. */
3297 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3298 return build_nt (BIT_NOT_EXPR, scope);
3299 else if (type_decl == error_mark_node)
3300 return error_mark_node;
3302 /* [class.dtor]
3304 A typedef-name that names a class shall not be used as the
3305 identifier in the declarator for a destructor declaration. */
3306 if (declarator_p
3307 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3308 && !DECL_SELF_REFERENCE_P (type_decl)
3309 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3310 error ("typedef-name %qD used as destructor declarator",
3311 type_decl);
3313 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3316 case CPP_KEYWORD:
3317 if (token->keyword == RID_OPERATOR)
3319 tree id;
3321 /* This could be a template-id, so we try that first. */
3322 cp_parser_parse_tentatively (parser);
3323 /* Try a template-id. */
3324 id = cp_parser_template_id (parser, template_keyword_p,
3325 /*check_dependency_p=*/true,
3326 declarator_p);
3327 /* If that worked, we're done. */
3328 if (cp_parser_parse_definitely (parser))
3329 return id;
3330 /* We still don't know whether we're looking at an
3331 operator-function-id or a conversion-function-id. */
3332 cp_parser_parse_tentatively (parser);
3333 /* Try an operator-function-id. */
3334 id = cp_parser_operator_function_id (parser);
3335 /* If that didn't work, try a conversion-function-id. */
3336 if (!cp_parser_parse_definitely (parser))
3337 id = cp_parser_conversion_function_id (parser);
3339 return id;
3341 /* Fall through. */
3343 default:
3344 cp_parser_error (parser, "expected unqualified-id");
3345 return error_mark_node;
3349 /* Parse an (optional) nested-name-specifier.
3351 nested-name-specifier:
3352 class-or-namespace-name :: nested-name-specifier [opt]
3353 class-or-namespace-name :: template nested-name-specifier [opt]
3355 PARSER->SCOPE should be set appropriately before this function is
3356 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3357 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3358 in name lookups.
3360 Sets PARSER->SCOPE to the class (TYPE) or namespace
3361 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3362 it unchanged if there is no nested-name-specifier. Returns the new
3363 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3365 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3366 part of a declaration and/or decl-specifier. */
3368 static tree
3369 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3370 bool typename_keyword_p,
3371 bool check_dependency_p,
3372 bool type_p,
3373 bool is_declaration)
3375 bool success = false;
3376 tree access_check = NULL_TREE;
3377 cp_token_position start = 0;
3378 cp_token *token;
3380 /* If the next token corresponds to a nested name specifier, there
3381 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3382 false, it may have been true before, in which case something
3383 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3384 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3385 CHECK_DEPENDENCY_P is false, we have to fall through into the
3386 main loop. */
3387 if (check_dependency_p
3388 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3390 cp_parser_pre_parsed_nested_name_specifier (parser);
3391 return parser->scope;
3394 /* Remember where the nested-name-specifier starts. */
3395 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3396 start = cp_lexer_token_position (parser->lexer, false);
3398 push_deferring_access_checks (dk_deferred);
3400 while (true)
3402 tree new_scope;
3403 tree old_scope;
3404 tree saved_qualifying_scope;
3405 bool template_keyword_p;
3407 /* Spot cases that cannot be the beginning of a
3408 nested-name-specifier. */
3409 token = cp_lexer_peek_token (parser->lexer);
3411 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3412 the already parsed nested-name-specifier. */
3413 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3415 /* Grab the nested-name-specifier and continue the loop. */
3416 cp_parser_pre_parsed_nested_name_specifier (parser);
3417 success = true;
3418 continue;
3421 /* Spot cases that cannot be the beginning of a
3422 nested-name-specifier. On the second and subsequent times
3423 through the loop, we look for the `template' keyword. */
3424 if (success && token->keyword == RID_TEMPLATE)
3426 /* A template-id can start a nested-name-specifier. */
3427 else if (token->type == CPP_TEMPLATE_ID)
3429 else
3431 /* If the next token is not an identifier, then it is
3432 definitely not a class-or-namespace-name. */
3433 if (token->type != CPP_NAME)
3434 break;
3435 /* If the following token is neither a `<' (to begin a
3436 template-id), nor a `::', then we are not looking at a
3437 nested-name-specifier. */
3438 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3439 if (token->type != CPP_SCOPE
3440 && !cp_parser_nth_token_starts_template_argument_list_p
3441 (parser, 2))
3442 break;
3445 /* The nested-name-specifier is optional, so we parse
3446 tentatively. */
3447 cp_parser_parse_tentatively (parser);
3449 /* Look for the optional `template' keyword, if this isn't the
3450 first time through the loop. */
3451 if (success)
3452 template_keyword_p = cp_parser_optional_template_keyword (parser);
3453 else
3454 template_keyword_p = false;
3456 /* Save the old scope since the name lookup we are about to do
3457 might destroy it. */
3458 old_scope = parser->scope;
3459 saved_qualifying_scope = parser->qualifying_scope;
3460 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3461 look up names in "X<T>::I" in order to determine that "Y" is
3462 a template. So, if we have a typename at this point, we make
3463 an effort to look through it. */
3464 if (is_declaration
3465 && !typename_keyword_p
3466 && parser->scope
3467 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3468 parser->scope = resolve_typename_type (parser->scope,
3469 /*only_current_p=*/false);
3470 /* Parse the qualifying entity. */
3471 new_scope
3472 = cp_parser_class_or_namespace_name (parser,
3473 typename_keyword_p,
3474 template_keyword_p,
3475 check_dependency_p,
3476 type_p,
3477 is_declaration);
3478 /* Look for the `::' token. */
3479 cp_parser_require (parser, CPP_SCOPE, "`::'");
3481 /* If we found what we wanted, we keep going; otherwise, we're
3482 done. */
3483 if (!cp_parser_parse_definitely (parser))
3485 bool error_p = false;
3487 /* Restore the OLD_SCOPE since it was valid before the
3488 failed attempt at finding the last
3489 class-or-namespace-name. */
3490 parser->scope = old_scope;
3491 parser->qualifying_scope = saved_qualifying_scope;
3492 /* If the next token is an identifier, and the one after
3493 that is a `::', then any valid interpretation would have
3494 found a class-or-namespace-name. */
3495 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3496 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3497 == CPP_SCOPE)
3498 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3499 != CPP_COMPL))
3501 token = cp_lexer_consume_token (parser->lexer);
3502 if (!error_p)
3504 tree decl;
3506 decl = cp_parser_lookup_name_simple (parser, token->value);
3507 if (TREE_CODE (decl) == TEMPLATE_DECL)
3508 error ("%qD used without template parameters", decl);
3509 else
3510 cp_parser_name_lookup_error
3511 (parser, token->value, decl,
3512 "is not a class or namespace");
3513 parser->scope = NULL_TREE;
3514 error_p = true;
3515 /* Treat this as a successful nested-name-specifier
3516 due to:
3518 [basic.lookup.qual]
3520 If the name found is not a class-name (clause
3521 _class_) or namespace-name (_namespace.def_), the
3522 program is ill-formed. */
3523 success = true;
3525 cp_lexer_consume_token (parser->lexer);
3527 break;
3530 /* We've found one valid nested-name-specifier. */
3531 success = true;
3532 /* Make sure we look in the right scope the next time through
3533 the loop. */
3534 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3535 ? TREE_TYPE (new_scope)
3536 : new_scope);
3537 /* If it is a class scope, try to complete it; we are about to
3538 be looking up names inside the class. */
3539 if (TYPE_P (parser->scope)
3540 /* Since checking types for dependency can be expensive,
3541 avoid doing it if the type is already complete. */
3542 && !COMPLETE_TYPE_P (parser->scope)
3543 /* Do not try to complete dependent types. */
3544 && !dependent_type_p (parser->scope))
3545 complete_type (parser->scope);
3548 /* Retrieve any deferred checks. Do not pop this access checks yet
3549 so the memory will not be reclaimed during token replacing below. */
3550 access_check = get_deferred_access_checks ();
3552 /* If parsing tentatively, replace the sequence of tokens that makes
3553 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3554 token. That way, should we re-parse the token stream, we will
3555 not have to repeat the effort required to do the parse, nor will
3556 we issue duplicate error messages. */
3557 if (success && start)
3559 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3561 /* Reset the contents of the START token. */
3562 token->type = CPP_NESTED_NAME_SPECIFIER;
3563 token->value = build_tree_list (access_check, parser->scope);
3564 TREE_TYPE (token->value) = parser->qualifying_scope;
3565 token->keyword = RID_MAX;
3567 /* Purge all subsequent tokens. */
3568 cp_lexer_purge_tokens_after (parser->lexer, start);
3571 pop_deferring_access_checks ();
3572 return success ? parser->scope : NULL_TREE;
3575 /* Parse a nested-name-specifier. See
3576 cp_parser_nested_name_specifier_opt for details. This function
3577 behaves identically, except that it will an issue an error if no
3578 nested-name-specifier is present, and it will return
3579 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3580 is present. */
3582 static tree
3583 cp_parser_nested_name_specifier (cp_parser *parser,
3584 bool typename_keyword_p,
3585 bool check_dependency_p,
3586 bool type_p,
3587 bool is_declaration)
3589 tree scope;
3591 /* Look for the nested-name-specifier. */
3592 scope = cp_parser_nested_name_specifier_opt (parser,
3593 typename_keyword_p,
3594 check_dependency_p,
3595 type_p,
3596 is_declaration);
3597 /* If it was not present, issue an error message. */
3598 if (!scope)
3600 cp_parser_error (parser, "expected nested-name-specifier");
3601 parser->scope = NULL_TREE;
3602 return error_mark_node;
3605 return scope;
3608 /* Parse a class-or-namespace-name.
3610 class-or-namespace-name:
3611 class-name
3612 namespace-name
3614 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3615 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3616 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3617 TYPE_P is TRUE iff the next name should be taken as a class-name,
3618 even the same name is declared to be another entity in the same
3619 scope.
3621 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3622 specified by the class-or-namespace-name. If neither is found the
3623 ERROR_MARK_NODE is returned. */
3625 static tree
3626 cp_parser_class_or_namespace_name (cp_parser *parser,
3627 bool typename_keyword_p,
3628 bool template_keyword_p,
3629 bool check_dependency_p,
3630 bool type_p,
3631 bool is_declaration)
3633 tree saved_scope;
3634 tree saved_qualifying_scope;
3635 tree saved_object_scope;
3636 tree scope;
3637 bool only_class_p;
3639 /* Before we try to parse the class-name, we must save away the
3640 current PARSER->SCOPE since cp_parser_class_name will destroy
3641 it. */
3642 saved_scope = parser->scope;
3643 saved_qualifying_scope = parser->qualifying_scope;
3644 saved_object_scope = parser->object_scope;
3645 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3646 there is no need to look for a namespace-name. */
3647 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3648 if (!only_class_p)
3649 cp_parser_parse_tentatively (parser);
3650 scope = cp_parser_class_name (parser,
3651 typename_keyword_p,
3652 template_keyword_p,
3653 type_p ? class_type : none_type,
3654 check_dependency_p,
3655 /*class_head_p=*/false,
3656 is_declaration);
3657 /* If that didn't work, try for a namespace-name. */
3658 if (!only_class_p && !cp_parser_parse_definitely (parser))
3660 /* Restore the saved scope. */
3661 parser->scope = saved_scope;
3662 parser->qualifying_scope = saved_qualifying_scope;
3663 parser->object_scope = saved_object_scope;
3664 /* If we are not looking at an identifier followed by the scope
3665 resolution operator, then this is not part of a
3666 nested-name-specifier. (Note that this function is only used
3667 to parse the components of a nested-name-specifier.) */
3668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3669 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3670 return error_mark_node;
3671 scope = cp_parser_namespace_name (parser);
3674 return scope;
3677 /* Parse a postfix-expression.
3679 postfix-expression:
3680 primary-expression
3681 postfix-expression [ expression ]
3682 postfix-expression ( expression-list [opt] )
3683 simple-type-specifier ( expression-list [opt] )
3684 typename :: [opt] nested-name-specifier identifier
3685 ( expression-list [opt] )
3686 typename :: [opt] nested-name-specifier template [opt] template-id
3687 ( expression-list [opt] )
3688 postfix-expression . template [opt] id-expression
3689 postfix-expression -> template [opt] id-expression
3690 postfix-expression . pseudo-destructor-name
3691 postfix-expression -> pseudo-destructor-name
3692 postfix-expression ++
3693 postfix-expression --
3694 dynamic_cast < type-id > ( expression )
3695 static_cast < type-id > ( expression )
3696 reinterpret_cast < type-id > ( expression )
3697 const_cast < type-id > ( expression )
3698 typeid ( expression )
3699 typeid ( type-id )
3701 GNU Extension:
3703 postfix-expression:
3704 ( type-id ) { initializer-list , [opt] }
3706 This extension is a GNU version of the C99 compound-literal
3707 construct. (The C99 grammar uses `type-name' instead of `type-id',
3708 but they are essentially the same concept.)
3710 If ADDRESS_P is true, the postfix expression is the operand of the
3711 `&' operator. CAST_P is true if this expression is the target of a
3712 cast.
3714 Returns a representation of the expression. */
3716 static tree
3717 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3719 cp_token *token;
3720 enum rid keyword;
3721 cp_id_kind idk = CP_ID_KIND_NONE;
3722 tree postfix_expression = NULL_TREE;
3723 /* Non-NULL only if the current postfix-expression can be used to
3724 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3725 class used to qualify the member. */
3726 tree qualifying_class = NULL_TREE;
3728 /* Peek at the next token. */
3729 token = cp_lexer_peek_token (parser->lexer);
3730 /* Some of the productions are determined by keywords. */
3731 keyword = token->keyword;
3732 switch (keyword)
3734 case RID_DYNCAST:
3735 case RID_STATCAST:
3736 case RID_REINTCAST:
3737 case RID_CONSTCAST:
3739 tree type;
3740 tree expression;
3741 const char *saved_message;
3743 /* All of these can be handled in the same way from the point
3744 of view of parsing. Begin by consuming the token
3745 identifying the cast. */
3746 cp_lexer_consume_token (parser->lexer);
3748 /* New types cannot be defined in the cast. */
3749 saved_message = parser->type_definition_forbidden_message;
3750 parser->type_definition_forbidden_message
3751 = "types may not be defined in casts";
3753 /* Look for the opening `<'. */
3754 cp_parser_require (parser, CPP_LESS, "`<'");
3755 /* Parse the type to which we are casting. */
3756 type = cp_parser_type_id (parser);
3757 /* Look for the closing `>'. */
3758 cp_parser_require (parser, CPP_GREATER, "`>'");
3759 /* Restore the old message. */
3760 parser->type_definition_forbidden_message = saved_message;
3762 /* And the expression which is being cast. */
3763 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3764 expression = cp_parser_expression (parser, /*cast_p=*/true);
3765 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3767 /* Only type conversions to integral or enumeration types
3768 can be used in constant-expressions. */
3769 if (parser->integral_constant_expression_p
3770 && !dependent_type_p (type)
3771 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3772 && (cp_parser_non_integral_constant_expression
3773 (parser,
3774 "a cast to a type other than an integral or "
3775 "enumeration type")))
3776 return error_mark_node;
3778 switch (keyword)
3780 case RID_DYNCAST:
3781 postfix_expression
3782 = build_dynamic_cast (type, expression);
3783 break;
3784 case RID_STATCAST:
3785 postfix_expression
3786 = build_static_cast (type, expression);
3787 break;
3788 case RID_REINTCAST:
3789 postfix_expression
3790 = build_reinterpret_cast (type, expression);
3791 break;
3792 case RID_CONSTCAST:
3793 postfix_expression
3794 = build_const_cast (type, expression);
3795 break;
3796 default:
3797 gcc_unreachable ();
3800 break;
3802 case RID_TYPEID:
3804 tree type;
3805 const char *saved_message;
3806 bool saved_in_type_id_in_expr_p;
3808 /* Consume the `typeid' token. */
3809 cp_lexer_consume_token (parser->lexer);
3810 /* Look for the `(' token. */
3811 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3812 /* Types cannot be defined in a `typeid' expression. */
3813 saved_message = parser->type_definition_forbidden_message;
3814 parser->type_definition_forbidden_message
3815 = "types may not be defined in a `typeid\' expression";
3816 /* We can't be sure yet whether we're looking at a type-id or an
3817 expression. */
3818 cp_parser_parse_tentatively (parser);
3819 /* Try a type-id first. */
3820 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3821 parser->in_type_id_in_expr_p = true;
3822 type = cp_parser_type_id (parser);
3823 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3824 /* Look for the `)' token. Otherwise, we can't be sure that
3825 we're not looking at an expression: consider `typeid (int
3826 (3))', for example. */
3827 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3828 /* If all went well, simply lookup the type-id. */
3829 if (cp_parser_parse_definitely (parser))
3830 postfix_expression = get_typeid (type);
3831 /* Otherwise, fall back to the expression variant. */
3832 else
3834 tree expression;
3836 /* Look for an expression. */
3837 expression = cp_parser_expression (parser, /*cast_p=*/false);
3838 /* Compute its typeid. */
3839 postfix_expression = build_typeid (expression);
3840 /* Look for the `)' token. */
3841 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3843 /* `typeid' may not appear in an integral constant expression. */
3844 if (cp_parser_non_integral_constant_expression(parser,
3845 "`typeid' operator"))
3846 return error_mark_node;
3847 /* Restore the saved message. */
3848 parser->type_definition_forbidden_message = saved_message;
3850 break;
3852 case RID_TYPENAME:
3854 bool template_p = false;
3855 tree id;
3856 tree type;
3858 /* Consume the `typename' token. */
3859 cp_lexer_consume_token (parser->lexer);
3860 /* Look for the optional `::' operator. */
3861 cp_parser_global_scope_opt (parser,
3862 /*current_scope_valid_p=*/false);
3863 /* Look for the nested-name-specifier. */
3864 cp_parser_nested_name_specifier (parser,
3865 /*typename_keyword_p=*/true,
3866 /*check_dependency_p=*/true,
3867 /*type_p=*/true,
3868 /*is_declaration=*/true);
3869 /* Look for the optional `template' keyword. */
3870 template_p = cp_parser_optional_template_keyword (parser);
3871 /* We don't know whether we're looking at a template-id or an
3872 identifier. */
3873 cp_parser_parse_tentatively (parser);
3874 /* Try a template-id. */
3875 id = cp_parser_template_id (parser, template_p,
3876 /*check_dependency_p=*/true,
3877 /*is_declaration=*/true);
3878 /* If that didn't work, try an identifier. */
3879 if (!cp_parser_parse_definitely (parser))
3880 id = cp_parser_identifier (parser);
3881 /* If we look up a template-id in a non-dependent qualifying
3882 scope, there's no need to create a dependent type. */
3883 if (TREE_CODE (id) == TYPE_DECL
3884 && !dependent_type_p (parser->scope))
3885 type = TREE_TYPE (id);
3886 /* Create a TYPENAME_TYPE to represent the type to which the
3887 functional cast is being performed. */
3888 else
3889 type = make_typename_type (parser->scope, id,
3890 typename_type,
3891 /*complain=*/1);
3893 postfix_expression = cp_parser_functional_cast (parser, type);
3895 break;
3897 default:
3899 tree type;
3901 /* If the next thing is a simple-type-specifier, we may be
3902 looking at a functional cast. We could also be looking at
3903 an id-expression. So, we try the functional cast, and if
3904 that doesn't work we fall back to the primary-expression. */
3905 cp_parser_parse_tentatively (parser);
3906 /* Look for the simple-type-specifier. */
3907 type = cp_parser_simple_type_specifier (parser,
3908 /*decl_specs=*/NULL,
3909 CP_PARSER_FLAGS_NONE);
3910 /* Parse the cast itself. */
3911 if (!cp_parser_error_occurred (parser))
3912 postfix_expression
3913 = cp_parser_functional_cast (parser, type);
3914 /* If that worked, we're done. */
3915 if (cp_parser_parse_definitely (parser))
3916 break;
3918 /* If the functional-cast didn't work out, try a
3919 compound-literal. */
3920 if (cp_parser_allow_gnu_extensions_p (parser)
3921 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3923 tree initializer_list = NULL_TREE;
3924 bool saved_in_type_id_in_expr_p;
3926 cp_parser_parse_tentatively (parser);
3927 /* Consume the `('. */
3928 cp_lexer_consume_token (parser->lexer);
3929 /* Parse the type. */
3930 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3931 parser->in_type_id_in_expr_p = true;
3932 type = cp_parser_type_id (parser);
3933 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3934 /* Look for the `)'. */
3935 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3936 /* Look for the `{'. */
3937 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3938 /* If things aren't going well, there's no need to
3939 keep going. */
3940 if (!cp_parser_error_occurred (parser))
3942 bool non_constant_p;
3943 /* Parse the initializer-list. */
3944 initializer_list
3945 = cp_parser_initializer_list (parser, &non_constant_p);
3946 /* Allow a trailing `,'. */
3947 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3948 cp_lexer_consume_token (parser->lexer);
3949 /* Look for the final `}'. */
3950 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3952 /* If that worked, we're definitely looking at a
3953 compound-literal expression. */
3954 if (cp_parser_parse_definitely (parser))
3956 /* Warn the user that a compound literal is not
3957 allowed in standard C++. */
3958 if (pedantic)
3959 pedwarn ("ISO C++ forbids compound-literals");
3960 /* Form the representation of the compound-literal. */
3961 postfix_expression
3962 = finish_compound_literal (type, initializer_list);
3963 break;
3967 /* It must be a primary-expression. */
3968 postfix_expression = cp_parser_primary_expression (parser,
3969 cast_p,
3970 &idk,
3971 &qualifying_class);
3973 break;
3976 /* If we were avoiding committing to the processing of a
3977 qualified-id until we knew whether or not we had a
3978 pointer-to-member, we now know. */
3979 if (qualifying_class)
3981 bool done;
3983 /* Peek at the next token. */
3984 token = cp_lexer_peek_token (parser->lexer);
3985 done = (token->type != CPP_OPEN_SQUARE
3986 && token->type != CPP_OPEN_PAREN
3987 && token->type != CPP_DOT
3988 && token->type != CPP_DEREF
3989 && token->type != CPP_PLUS_PLUS
3990 && token->type != CPP_MINUS_MINUS);
3992 postfix_expression = finish_qualified_id_expr (qualifying_class,
3993 postfix_expression,
3994 done,
3995 address_p);
3996 if (done)
3997 return postfix_expression;
4000 /* Keep looping until the postfix-expression is complete. */
4001 while (true)
4003 if (idk == CP_ID_KIND_UNQUALIFIED
4004 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4005 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4006 /* It is not a Koenig lookup function call. */
4007 postfix_expression
4008 = unqualified_name_lookup_error (postfix_expression);
4010 /* Peek at the next token. */
4011 token = cp_lexer_peek_token (parser->lexer);
4013 switch (token->type)
4015 case CPP_OPEN_SQUARE:
4016 postfix_expression
4017 = cp_parser_postfix_open_square_expression (parser,
4018 postfix_expression,
4019 false);
4020 idk = CP_ID_KIND_NONE;
4021 break;
4023 case CPP_OPEN_PAREN:
4024 /* postfix-expression ( expression-list [opt] ) */
4026 bool koenig_p;
4027 tree args = (cp_parser_parenthesized_expression_list
4028 (parser, false,
4029 /*cast_p=*/false,
4030 /*non_constant_p=*/NULL));
4032 if (args == error_mark_node)
4034 postfix_expression = error_mark_node;
4035 break;
4038 /* Function calls are not permitted in
4039 constant-expressions. */
4040 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4041 && cp_parser_non_integral_constant_expression (parser,
4042 "a function call"))
4044 postfix_expression = error_mark_node;
4045 break;
4048 koenig_p = false;
4049 if (idk == CP_ID_KIND_UNQUALIFIED)
4051 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4053 if (args)
4055 koenig_p = true;
4056 postfix_expression
4057 = perform_koenig_lookup (postfix_expression, args);
4059 else
4060 postfix_expression
4061 = unqualified_fn_lookup_error (postfix_expression);
4063 /* We do not perform argument-dependent lookup if
4064 normal lookup finds a non-function, in accordance
4065 with the expected resolution of DR 218. */
4066 else if (args && is_overloaded_fn (postfix_expression))
4068 tree fn = get_first_fn (postfix_expression);
4070 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4071 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4073 /* Only do argument dependent lookup if regular
4074 lookup does not find a set of member functions.
4075 [basic.lookup.koenig]/2a */
4076 if (!DECL_FUNCTION_MEMBER_P (fn))
4078 koenig_p = true;
4079 postfix_expression
4080 = perform_koenig_lookup (postfix_expression, args);
4085 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4087 tree instance = TREE_OPERAND (postfix_expression, 0);
4088 tree fn = TREE_OPERAND (postfix_expression, 1);
4090 if (processing_template_decl
4091 && (type_dependent_expression_p (instance)
4092 || (!BASELINK_P (fn)
4093 && TREE_CODE (fn) != FIELD_DECL)
4094 || type_dependent_expression_p (fn)
4095 || any_type_dependent_arguments_p (args)))
4097 postfix_expression
4098 = build_min_nt (CALL_EXPR, postfix_expression,
4099 args, NULL_TREE);
4100 break;
4103 if (BASELINK_P (fn))
4104 postfix_expression
4105 = (build_new_method_call
4106 (instance, fn, args, NULL_TREE,
4107 (idk == CP_ID_KIND_QUALIFIED
4108 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4109 else
4110 postfix_expression
4111 = finish_call_expr (postfix_expression, args,
4112 /*disallow_virtual=*/false,
4113 /*koenig_p=*/false);
4115 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4116 || TREE_CODE (postfix_expression) == MEMBER_REF
4117 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4118 postfix_expression = (build_offset_ref_call_from_tree
4119 (postfix_expression, args));
4120 else if (idk == CP_ID_KIND_QUALIFIED)
4121 /* A call to a static class member, or a namespace-scope
4122 function. */
4123 postfix_expression
4124 = finish_call_expr (postfix_expression, args,
4125 /*disallow_virtual=*/true,
4126 koenig_p);
4127 else
4128 /* All other function calls. */
4129 postfix_expression
4130 = finish_call_expr (postfix_expression, args,
4131 /*disallow_virtual=*/false,
4132 koenig_p);
4134 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4135 idk = CP_ID_KIND_NONE;
4137 break;
4139 case CPP_DOT:
4140 case CPP_DEREF:
4141 /* postfix-expression . template [opt] id-expression
4142 postfix-expression . pseudo-destructor-name
4143 postfix-expression -> template [opt] id-expression
4144 postfix-expression -> pseudo-destructor-name */
4146 /* Consume the `.' or `->' operator. */
4147 cp_lexer_consume_token (parser->lexer);
4149 postfix_expression
4150 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4151 postfix_expression,
4152 false, &idk);
4153 break;
4155 case CPP_PLUS_PLUS:
4156 /* postfix-expression ++ */
4157 /* Consume the `++' token. */
4158 cp_lexer_consume_token (parser->lexer);
4159 /* Generate a representation for the complete expression. */
4160 postfix_expression
4161 = finish_increment_expr (postfix_expression,
4162 POSTINCREMENT_EXPR);
4163 /* Increments may not appear in constant-expressions. */
4164 if (cp_parser_non_integral_constant_expression (parser,
4165 "an increment"))
4166 postfix_expression = error_mark_node;
4167 idk = CP_ID_KIND_NONE;
4168 break;
4170 case CPP_MINUS_MINUS:
4171 /* postfix-expression -- */
4172 /* Consume the `--' token. */
4173 cp_lexer_consume_token (parser->lexer);
4174 /* Generate a representation for the complete expression. */
4175 postfix_expression
4176 = finish_increment_expr (postfix_expression,
4177 POSTDECREMENT_EXPR);
4178 /* Decrements may not appear in constant-expressions. */
4179 if (cp_parser_non_integral_constant_expression (parser,
4180 "a decrement"))
4181 postfix_expression = error_mark_node;
4182 idk = CP_ID_KIND_NONE;
4183 break;
4185 default:
4186 return postfix_expression;
4190 /* We should never get here. */
4191 gcc_unreachable ();
4192 return error_mark_node;
4195 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4196 by cp_parser_builtin_offsetof. We're looking for
4198 postfix-expression [ expression ]
4200 FOR_OFFSETOF is set if we're being called in that context, which
4201 changes how we deal with integer constant expressions. */
4203 static tree
4204 cp_parser_postfix_open_square_expression (cp_parser *parser,
4205 tree postfix_expression,
4206 bool for_offsetof)
4208 tree index;
4210 /* Consume the `[' token. */
4211 cp_lexer_consume_token (parser->lexer);
4213 /* Parse the index expression. */
4214 /* ??? For offsetof, there is a question of what to allow here. If
4215 offsetof is not being used in an integral constant expression context,
4216 then we *could* get the right answer by computing the value at runtime.
4217 If we are in an integral constant expression context, then we might
4218 could accept any constant expression; hard to say without analysis.
4219 Rather than open the barn door too wide right away, allow only integer
4220 constant expressions here. */
4221 if (for_offsetof)
4222 index = cp_parser_constant_expression (parser, false, NULL);
4223 else
4224 index = cp_parser_expression (parser, /*cast_p=*/false);
4226 /* Look for the closing `]'. */
4227 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4229 /* Build the ARRAY_REF. */
4230 postfix_expression = grok_array_decl (postfix_expression, index);
4232 /* When not doing offsetof, array references are not permitted in
4233 constant-expressions. */
4234 if (!for_offsetof
4235 && (cp_parser_non_integral_constant_expression
4236 (parser, "an array reference")))
4237 postfix_expression = error_mark_node;
4239 return postfix_expression;
4242 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4243 by cp_parser_builtin_offsetof. We're looking for
4245 postfix-expression . template [opt] id-expression
4246 postfix-expression . pseudo-destructor-name
4247 postfix-expression -> template [opt] id-expression
4248 postfix-expression -> pseudo-destructor-name
4250 FOR_OFFSETOF is set if we're being called in that context. That sorta
4251 limits what of the above we'll actually accept, but nevermind.
4252 TOKEN_TYPE is the "." or "->" token, which will already have been
4253 removed from the stream. */
4255 static tree
4256 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4257 enum cpp_ttype token_type,
4258 tree postfix_expression,
4259 bool for_offsetof, cp_id_kind *idk)
4261 tree name;
4262 bool dependent_p;
4263 bool template_p;
4264 bool pseudo_destructor_p;
4265 tree scope = NULL_TREE;
4267 /* If this is a `->' operator, dereference the pointer. */
4268 if (token_type == CPP_DEREF)
4269 postfix_expression = build_x_arrow (postfix_expression);
4270 /* Check to see whether or not the expression is type-dependent. */
4271 dependent_p = type_dependent_expression_p (postfix_expression);
4272 /* The identifier following the `->' or `.' is not qualified. */
4273 parser->scope = NULL_TREE;
4274 parser->qualifying_scope = NULL_TREE;
4275 parser->object_scope = NULL_TREE;
4276 *idk = CP_ID_KIND_NONE;
4277 /* Enter the scope corresponding to the type of the object
4278 given by the POSTFIX_EXPRESSION. */
4279 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4281 scope = TREE_TYPE (postfix_expression);
4282 /* According to the standard, no expression should ever have
4283 reference type. Unfortunately, we do not currently match
4284 the standard in this respect in that our internal representation
4285 of an expression may have reference type even when the standard
4286 says it does not. Therefore, we have to manually obtain the
4287 underlying type here. */
4288 scope = non_reference (scope);
4289 /* The type of the POSTFIX_EXPRESSION must be complete. */
4290 scope = complete_type_or_else (scope, NULL_TREE);
4291 /* Let the name lookup machinery know that we are processing a
4292 class member access expression. */
4293 parser->context->object_type = scope;
4294 /* If something went wrong, we want to be able to discern that case,
4295 as opposed to the case where there was no SCOPE due to the type
4296 of expression being dependent. */
4297 if (!scope)
4298 scope = error_mark_node;
4299 /* If the SCOPE was erroneous, make the various semantic analysis
4300 functions exit quickly -- and without issuing additional error
4301 messages. */
4302 if (scope == error_mark_node)
4303 postfix_expression = error_mark_node;
4306 /* Assume this expression is not a pseudo-destructor access. */
4307 pseudo_destructor_p = false;
4309 /* If the SCOPE is a scalar type, then, if this is a valid program,
4310 we must be looking at a pseudo-destructor-name. */
4311 if (scope && SCALAR_TYPE_P (scope))
4313 tree s;
4314 tree type;
4316 cp_parser_parse_tentatively (parser);
4317 /* Parse the pseudo-destructor-name. */
4318 s = NULL_TREE;
4319 cp_parser_pseudo_destructor_name (parser, &s, &type);
4320 if (cp_parser_parse_definitely (parser))
4322 pseudo_destructor_p = true;
4323 postfix_expression
4324 = finish_pseudo_destructor_expr (postfix_expression,
4325 s, TREE_TYPE (type));
4329 if (!pseudo_destructor_p)
4331 /* If the SCOPE is not a scalar type, we are looking at an
4332 ordinary class member access expression, rather than a
4333 pseudo-destructor-name. */
4334 template_p = cp_parser_optional_template_keyword (parser);
4335 /* Parse the id-expression. */
4336 name = cp_parser_id_expression (parser, template_p,
4337 /*check_dependency_p=*/true,
4338 /*template_p=*/NULL,
4339 /*declarator_p=*/false);
4340 /* In general, build a SCOPE_REF if the member name is qualified.
4341 However, if the name was not dependent and has already been
4342 resolved; there is no need to build the SCOPE_REF. For example;
4344 struct X { void f(); };
4345 template <typename T> void f(T* t) { t->X::f(); }
4347 Even though "t" is dependent, "X::f" is not and has been resolved
4348 to a BASELINK; there is no need to include scope information. */
4350 /* But we do need to remember that there was an explicit scope for
4351 virtual function calls. */
4352 if (parser->scope)
4353 *idk = CP_ID_KIND_QUALIFIED;
4355 /* If the name is a template-id that names a type, we will get a
4356 TYPE_DECL here. That is invalid code. */
4357 if (TREE_CODE (name) == TYPE_DECL)
4359 error ("invalid use of %qD", name);
4360 postfix_expression = error_mark_node;
4362 else
4364 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4366 name = build_nt (SCOPE_REF, parser->scope, name);
4367 parser->scope = NULL_TREE;
4368 parser->qualifying_scope = NULL_TREE;
4369 parser->object_scope = NULL_TREE;
4371 if (scope && name && BASELINK_P (name))
4372 adjust_result_of_qualified_name_lookup
4373 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4374 postfix_expression
4375 = finish_class_member_access_expr (postfix_expression, name);
4379 /* We no longer need to look up names in the scope of the object on
4380 the left-hand side of the `.' or `->' operator. */
4381 parser->context->object_type = NULL_TREE;
4383 /* Outside of offsetof, these operators may not appear in
4384 constant-expressions. */
4385 if (!for_offsetof
4386 && (cp_parser_non_integral_constant_expression
4387 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4388 postfix_expression = error_mark_node;
4390 return postfix_expression;
4393 /* Parse a parenthesized expression-list.
4395 expression-list:
4396 assignment-expression
4397 expression-list, assignment-expression
4399 attribute-list:
4400 expression-list
4401 identifier
4402 identifier, expression-list
4404 CAST_P is true if this expression is the target of a cast.
4406 Returns a TREE_LIST. The TREE_VALUE of each node is a
4407 representation of an assignment-expression. Note that a TREE_LIST
4408 is returned even if there is only a single expression in the list.
4409 error_mark_node is returned if the ( and or ) are
4410 missing. NULL_TREE is returned on no expressions. The parentheses
4411 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4412 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4413 indicates whether or not all of the expressions in the list were
4414 constant. */
4416 static tree
4417 cp_parser_parenthesized_expression_list (cp_parser* parser,
4418 bool is_attribute_list,
4419 bool cast_p,
4420 bool *non_constant_p)
4422 tree expression_list = NULL_TREE;
4423 bool fold_expr_p = is_attribute_list;
4424 tree identifier = NULL_TREE;
4426 /* Assume all the expressions will be constant. */
4427 if (non_constant_p)
4428 *non_constant_p = false;
4430 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4431 return error_mark_node;
4433 /* Consume expressions until there are no more. */
4434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4435 while (true)
4437 tree expr;
4439 /* At the beginning of attribute lists, check to see if the
4440 next token is an identifier. */
4441 if (is_attribute_list
4442 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4444 cp_token *token;
4446 /* Consume the identifier. */
4447 token = cp_lexer_consume_token (parser->lexer);
4448 /* Save the identifier. */
4449 identifier = token->value;
4451 else
4453 /* Parse the next assignment-expression. */
4454 if (non_constant_p)
4456 bool expr_non_constant_p;
4457 expr = (cp_parser_constant_expression
4458 (parser, /*allow_non_constant_p=*/true,
4459 &expr_non_constant_p));
4460 if (expr_non_constant_p)
4461 *non_constant_p = true;
4463 else
4464 expr = cp_parser_assignment_expression (parser, cast_p);
4466 if (fold_expr_p)
4467 expr = fold_non_dependent_expr (expr);
4469 /* Add it to the list. We add error_mark_node
4470 expressions to the list, so that we can still tell if
4471 the correct form for a parenthesized expression-list
4472 is found. That gives better errors. */
4473 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4475 if (expr == error_mark_node)
4476 goto skip_comma;
4479 /* After the first item, attribute lists look the same as
4480 expression lists. */
4481 is_attribute_list = false;
4483 get_comma:;
4484 /* If the next token isn't a `,', then we are done. */
4485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4486 break;
4488 /* Otherwise, consume the `,' and keep going. */
4489 cp_lexer_consume_token (parser->lexer);
4492 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4494 int ending;
4496 skip_comma:;
4497 /* We try and resync to an unnested comma, as that will give the
4498 user better diagnostics. */
4499 ending = cp_parser_skip_to_closing_parenthesis (parser,
4500 /*recovering=*/true,
4501 /*or_comma=*/true,
4502 /*consume_paren=*/true);
4503 if (ending < 0)
4504 goto get_comma;
4505 if (!ending)
4506 return error_mark_node;
4509 /* We built up the list in reverse order so we must reverse it now. */
4510 expression_list = nreverse (expression_list);
4511 if (identifier)
4512 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4514 return expression_list;
4517 /* Parse a pseudo-destructor-name.
4519 pseudo-destructor-name:
4520 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4521 :: [opt] nested-name-specifier template template-id :: ~ type-name
4522 :: [opt] nested-name-specifier [opt] ~ type-name
4524 If either of the first two productions is used, sets *SCOPE to the
4525 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4526 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4527 or ERROR_MARK_NODE if the parse fails. */
4529 static void
4530 cp_parser_pseudo_destructor_name (cp_parser* parser,
4531 tree* scope,
4532 tree* type)
4534 bool nested_name_specifier_p;
4536 /* Assume that things will not work out. */
4537 *type = error_mark_node;
4539 /* Look for the optional `::' operator. */
4540 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4541 /* Look for the optional nested-name-specifier. */
4542 nested_name_specifier_p
4543 = (cp_parser_nested_name_specifier_opt (parser,
4544 /*typename_keyword_p=*/false,
4545 /*check_dependency_p=*/true,
4546 /*type_p=*/false,
4547 /*is_declaration=*/true)
4548 != NULL_TREE);
4549 /* Now, if we saw a nested-name-specifier, we might be doing the
4550 second production. */
4551 if (nested_name_specifier_p
4552 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4554 /* Consume the `template' keyword. */
4555 cp_lexer_consume_token (parser->lexer);
4556 /* Parse the template-id. */
4557 cp_parser_template_id (parser,
4558 /*template_keyword_p=*/true,
4559 /*check_dependency_p=*/false,
4560 /*is_declaration=*/true);
4561 /* Look for the `::' token. */
4562 cp_parser_require (parser, CPP_SCOPE, "`::'");
4564 /* If the next token is not a `~', then there might be some
4565 additional qualification. */
4566 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4568 /* Look for the type-name. */
4569 *scope = TREE_TYPE (cp_parser_type_name (parser));
4571 if (*scope == error_mark_node)
4572 return;
4574 /* If we don't have ::~, then something has gone wrong. Since
4575 the only caller of this function is looking for something
4576 after `.' or `->' after a scalar type, most likely the
4577 program is trying to get a member of a non-aggregate
4578 type. */
4579 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4580 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4582 cp_parser_error (parser, "request for member of non-aggregate type");
4583 return;
4586 /* Look for the `::' token. */
4587 cp_parser_require (parser, CPP_SCOPE, "`::'");
4589 else
4590 *scope = NULL_TREE;
4592 /* Look for the `~'. */
4593 cp_parser_require (parser, CPP_COMPL, "`~'");
4594 /* Look for the type-name again. We are not responsible for
4595 checking that it matches the first type-name. */
4596 *type = cp_parser_type_name (parser);
4599 /* Parse a unary-expression.
4601 unary-expression:
4602 postfix-expression
4603 ++ cast-expression
4604 -- cast-expression
4605 unary-operator cast-expression
4606 sizeof unary-expression
4607 sizeof ( type-id )
4608 new-expression
4609 delete-expression
4611 GNU Extensions:
4613 unary-expression:
4614 __extension__ cast-expression
4615 __alignof__ unary-expression
4616 __alignof__ ( type-id )
4617 __real__ cast-expression
4618 __imag__ cast-expression
4619 && identifier
4621 ADDRESS_P is true iff the unary-expression is appearing as the
4622 operand of the `&' operator. CAST_P is true if this expression is
4623 the target of a cast.
4625 Returns a representation of the expression. */
4627 static tree
4628 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4630 cp_token *token;
4631 enum tree_code unary_operator;
4633 /* Peek at the next token. */
4634 token = cp_lexer_peek_token (parser->lexer);
4635 /* Some keywords give away the kind of expression. */
4636 if (token->type == CPP_KEYWORD)
4638 enum rid keyword = token->keyword;
4640 switch (keyword)
4642 case RID_ALIGNOF:
4643 case RID_SIZEOF:
4645 tree operand;
4646 enum tree_code op;
4648 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4649 /* Consume the token. */
4650 cp_lexer_consume_token (parser->lexer);
4651 /* Parse the operand. */
4652 operand = cp_parser_sizeof_operand (parser, keyword);
4654 if (TYPE_P (operand))
4655 return cxx_sizeof_or_alignof_type (operand, op, true);
4656 else
4657 return cxx_sizeof_or_alignof_expr (operand, op);
4660 case RID_NEW:
4661 return cp_parser_new_expression (parser);
4663 case RID_DELETE:
4664 return cp_parser_delete_expression (parser);
4666 case RID_EXTENSION:
4668 /* The saved value of the PEDANTIC flag. */
4669 int saved_pedantic;
4670 tree expr;
4672 /* Save away the PEDANTIC flag. */
4673 cp_parser_extension_opt (parser, &saved_pedantic);
4674 /* Parse the cast-expression. */
4675 expr = cp_parser_simple_cast_expression (parser);
4676 /* Restore the PEDANTIC flag. */
4677 pedantic = saved_pedantic;
4679 return expr;
4682 case RID_REALPART:
4683 case RID_IMAGPART:
4685 tree expression;
4687 /* Consume the `__real__' or `__imag__' token. */
4688 cp_lexer_consume_token (parser->lexer);
4689 /* Parse the cast-expression. */
4690 expression = cp_parser_simple_cast_expression (parser);
4691 /* Create the complete representation. */
4692 return build_x_unary_op ((keyword == RID_REALPART
4693 ? REALPART_EXPR : IMAGPART_EXPR),
4694 expression);
4696 break;
4698 default:
4699 break;
4703 /* Look for the `:: new' and `:: delete', which also signal the
4704 beginning of a new-expression, or delete-expression,
4705 respectively. If the next token is `::', then it might be one of
4706 these. */
4707 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4709 enum rid keyword;
4711 /* See if the token after the `::' is one of the keywords in
4712 which we're interested. */
4713 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4714 /* If it's `new', we have a new-expression. */
4715 if (keyword == RID_NEW)
4716 return cp_parser_new_expression (parser);
4717 /* Similarly, for `delete'. */
4718 else if (keyword == RID_DELETE)
4719 return cp_parser_delete_expression (parser);
4722 /* Look for a unary operator. */
4723 unary_operator = cp_parser_unary_operator (token);
4724 /* The `++' and `--' operators can be handled similarly, even though
4725 they are not technically unary-operators in the grammar. */
4726 if (unary_operator == ERROR_MARK)
4728 if (token->type == CPP_PLUS_PLUS)
4729 unary_operator = PREINCREMENT_EXPR;
4730 else if (token->type == CPP_MINUS_MINUS)
4731 unary_operator = PREDECREMENT_EXPR;
4732 /* Handle the GNU address-of-label extension. */
4733 else if (cp_parser_allow_gnu_extensions_p (parser)
4734 && token->type == CPP_AND_AND)
4736 tree identifier;
4738 /* Consume the '&&' token. */
4739 cp_lexer_consume_token (parser->lexer);
4740 /* Look for the identifier. */
4741 identifier = cp_parser_identifier (parser);
4742 /* Create an expression representing the address. */
4743 return finish_label_address_expr (identifier);
4746 if (unary_operator != ERROR_MARK)
4748 tree cast_expression;
4749 tree expression = error_mark_node;
4750 const char *non_constant_p = NULL;
4752 /* Consume the operator token. */
4753 token = cp_lexer_consume_token (parser->lexer);
4754 /* Parse the cast-expression. */
4755 cast_expression
4756 = cp_parser_cast_expression (parser,
4757 unary_operator == ADDR_EXPR,
4758 /*cast_p=*/false);
4759 /* Now, build an appropriate representation. */
4760 switch (unary_operator)
4762 case INDIRECT_REF:
4763 non_constant_p = "`*'";
4764 expression = build_x_indirect_ref (cast_expression, "unary *");
4765 break;
4767 case ADDR_EXPR:
4768 non_constant_p = "`&'";
4769 /* Fall through. */
4770 case BIT_NOT_EXPR:
4771 expression = build_x_unary_op (unary_operator, cast_expression);
4772 break;
4774 case PREINCREMENT_EXPR:
4775 case PREDECREMENT_EXPR:
4776 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4777 ? "`++'" : "`--'");
4778 /* Fall through. */
4779 case CONVERT_EXPR:
4780 case NEGATE_EXPR:
4781 case TRUTH_NOT_EXPR:
4782 expression = finish_unary_op_expr (unary_operator, cast_expression);
4783 break;
4785 default:
4786 gcc_unreachable ();
4789 if (non_constant_p
4790 && cp_parser_non_integral_constant_expression (parser,
4791 non_constant_p))
4792 expression = error_mark_node;
4794 return expression;
4797 return cp_parser_postfix_expression (parser, address_p, cast_p);
4800 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4801 unary-operator, the corresponding tree code is returned. */
4803 static enum tree_code
4804 cp_parser_unary_operator (cp_token* token)
4806 switch (token->type)
4808 case CPP_MULT:
4809 return INDIRECT_REF;
4811 case CPP_AND:
4812 return ADDR_EXPR;
4814 case CPP_PLUS:
4815 return CONVERT_EXPR;
4817 case CPP_MINUS:
4818 return NEGATE_EXPR;
4820 case CPP_NOT:
4821 return TRUTH_NOT_EXPR;
4823 case CPP_COMPL:
4824 return BIT_NOT_EXPR;
4826 default:
4827 return ERROR_MARK;
4831 /* Parse a new-expression.
4833 new-expression:
4834 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4835 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4837 Returns a representation of the expression. */
4839 static tree
4840 cp_parser_new_expression (cp_parser* parser)
4842 bool global_scope_p;
4843 tree placement;
4844 tree type;
4845 tree initializer;
4846 tree nelts;
4848 /* Look for the optional `::' operator. */
4849 global_scope_p
4850 = (cp_parser_global_scope_opt (parser,
4851 /*current_scope_valid_p=*/false)
4852 != NULL_TREE);
4853 /* Look for the `new' operator. */
4854 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4855 /* There's no easy way to tell a new-placement from the
4856 `( type-id )' construct. */
4857 cp_parser_parse_tentatively (parser);
4858 /* Look for a new-placement. */
4859 placement = cp_parser_new_placement (parser);
4860 /* If that didn't work out, there's no new-placement. */
4861 if (!cp_parser_parse_definitely (parser))
4862 placement = NULL_TREE;
4864 /* If the next token is a `(', then we have a parenthesized
4865 type-id. */
4866 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4868 /* Consume the `('. */
4869 cp_lexer_consume_token (parser->lexer);
4870 /* Parse the type-id. */
4871 type = cp_parser_type_id (parser);
4872 /* Look for the closing `)'. */
4873 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4874 /* There should not be a direct-new-declarator in this production,
4875 but GCC used to allowed this, so we check and emit a sensible error
4876 message for this case. */
4877 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4879 error ("array bound forbidden after parenthesized type-id");
4880 inform ("try removing the parentheses around the type-id");
4881 cp_parser_direct_new_declarator (parser);
4883 nelts = NULL_TREE;
4885 /* Otherwise, there must be a new-type-id. */
4886 else
4887 type = cp_parser_new_type_id (parser, &nelts);
4889 /* If the next token is a `(', then we have a new-initializer. */
4890 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4891 initializer = cp_parser_new_initializer (parser);
4892 else
4893 initializer = NULL_TREE;
4895 /* A new-expression may not appear in an integral constant
4896 expression. */
4897 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4898 return error_mark_node;
4900 /* Create a representation of the new-expression. */
4901 return build_new (placement, type, nelts, initializer, global_scope_p);
4904 /* Parse a new-placement.
4906 new-placement:
4907 ( expression-list )
4909 Returns the same representation as for an expression-list. */
4911 static tree
4912 cp_parser_new_placement (cp_parser* parser)
4914 tree expression_list;
4916 /* Parse the expression-list. */
4917 expression_list = (cp_parser_parenthesized_expression_list
4918 (parser, false, /*cast_p=*/false,
4919 /*non_constant_p=*/NULL));
4921 return expression_list;
4924 /* Parse a new-type-id.
4926 new-type-id:
4927 type-specifier-seq new-declarator [opt]
4929 Returns the TYPE allocated. If the new-type-id indicates an array
4930 type, *NELTS is set to the number of elements in the last array
4931 bound; the TYPE will not include the last array bound. */
4933 static tree
4934 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4936 cp_decl_specifier_seq type_specifier_seq;
4937 cp_declarator *new_declarator;
4938 cp_declarator *declarator;
4939 cp_declarator *outer_declarator;
4940 const char *saved_message;
4941 tree type;
4943 /* The type-specifier sequence must not contain type definitions.
4944 (It cannot contain declarations of new types either, but if they
4945 are not definitions we will catch that because they are not
4946 complete.) */
4947 saved_message = parser->type_definition_forbidden_message;
4948 parser->type_definition_forbidden_message
4949 = "types may not be defined in a new-type-id";
4950 /* Parse the type-specifier-seq. */
4951 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4952 /* Restore the old message. */
4953 parser->type_definition_forbidden_message = saved_message;
4954 /* Parse the new-declarator. */
4955 new_declarator = cp_parser_new_declarator_opt (parser);
4957 /* Determine the number of elements in the last array dimension, if
4958 any. */
4959 *nelts = NULL_TREE;
4960 /* Skip down to the last array dimension. */
4961 declarator = new_declarator;
4962 outer_declarator = NULL;
4963 while (declarator && (declarator->kind == cdk_pointer
4964 || declarator->kind == cdk_ptrmem))
4966 outer_declarator = declarator;
4967 declarator = declarator->declarator;
4969 while (declarator
4970 && declarator->kind == cdk_array
4971 && declarator->declarator
4972 && declarator->declarator->kind == cdk_array)
4974 outer_declarator = declarator;
4975 declarator = declarator->declarator;
4978 if (declarator && declarator->kind == cdk_array)
4980 *nelts = declarator->u.array.bounds;
4981 if (*nelts == error_mark_node)
4982 *nelts = integer_one_node;
4984 if (outer_declarator)
4985 outer_declarator->declarator = declarator->declarator;
4986 else
4987 new_declarator = NULL;
4990 type = groktypename (&type_specifier_seq, new_declarator);
4991 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4993 *nelts = array_type_nelts_top (type);
4994 type = TREE_TYPE (type);
4996 return type;
4999 /* Parse an (optional) new-declarator.
5001 new-declarator:
5002 ptr-operator new-declarator [opt]
5003 direct-new-declarator
5005 Returns the declarator. */
5007 static cp_declarator *
5008 cp_parser_new_declarator_opt (cp_parser* parser)
5010 enum tree_code code;
5011 tree type;
5012 cp_cv_quals cv_quals;
5014 /* We don't know if there's a ptr-operator next, or not. */
5015 cp_parser_parse_tentatively (parser);
5016 /* Look for a ptr-operator. */
5017 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5018 /* If that worked, look for more new-declarators. */
5019 if (cp_parser_parse_definitely (parser))
5021 cp_declarator *declarator;
5023 /* Parse another optional declarator. */
5024 declarator = cp_parser_new_declarator_opt (parser);
5026 /* Create the representation of the declarator. */
5027 if (type)
5028 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5029 else if (code == INDIRECT_REF)
5030 declarator = make_pointer_declarator (cv_quals, declarator);
5031 else
5032 declarator = make_reference_declarator (cv_quals, declarator);
5034 return declarator;
5037 /* If the next token is a `[', there is a direct-new-declarator. */
5038 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5039 return cp_parser_direct_new_declarator (parser);
5041 return NULL;
5044 /* Parse a direct-new-declarator.
5046 direct-new-declarator:
5047 [ expression ]
5048 direct-new-declarator [constant-expression]
5052 static cp_declarator *
5053 cp_parser_direct_new_declarator (cp_parser* parser)
5055 cp_declarator *declarator = NULL;
5057 while (true)
5059 tree expression;
5061 /* Look for the opening `['. */
5062 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5063 /* The first expression is not required to be constant. */
5064 if (!declarator)
5066 expression = cp_parser_expression (parser, /*cast_p=*/false);
5067 /* The standard requires that the expression have integral
5068 type. DR 74 adds enumeration types. We believe that the
5069 real intent is that these expressions be handled like the
5070 expression in a `switch' condition, which also allows
5071 classes with a single conversion to integral or
5072 enumeration type. */
5073 if (!processing_template_decl)
5075 expression
5076 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5077 expression,
5078 /*complain=*/true);
5079 if (!expression)
5081 error ("expression in new-declarator must have integral "
5082 "or enumeration type");
5083 expression = error_mark_node;
5087 /* But all the other expressions must be. */
5088 else
5089 expression
5090 = cp_parser_constant_expression (parser,
5091 /*allow_non_constant=*/false,
5092 NULL);
5093 /* Look for the closing `]'. */
5094 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5096 /* Add this bound to the declarator. */
5097 declarator = make_array_declarator (declarator, expression);
5099 /* If the next token is not a `[', then there are no more
5100 bounds. */
5101 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5102 break;
5105 return declarator;
5108 /* Parse a new-initializer.
5110 new-initializer:
5111 ( expression-list [opt] )
5113 Returns a representation of the expression-list. If there is no
5114 expression-list, VOID_ZERO_NODE is returned. */
5116 static tree
5117 cp_parser_new_initializer (cp_parser* parser)
5119 tree expression_list;
5121 expression_list = (cp_parser_parenthesized_expression_list
5122 (parser, false, /*cast_p=*/false,
5123 /*non_constant_p=*/NULL));
5124 if (!expression_list)
5125 expression_list = void_zero_node;
5127 return expression_list;
5130 /* Parse a delete-expression.
5132 delete-expression:
5133 :: [opt] delete cast-expression
5134 :: [opt] delete [ ] cast-expression
5136 Returns a representation of the expression. */
5138 static tree
5139 cp_parser_delete_expression (cp_parser* parser)
5141 bool global_scope_p;
5142 bool array_p;
5143 tree expression;
5145 /* Look for the optional `::' operator. */
5146 global_scope_p
5147 = (cp_parser_global_scope_opt (parser,
5148 /*current_scope_valid_p=*/false)
5149 != NULL_TREE);
5150 /* Look for the `delete' keyword. */
5151 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5152 /* See if the array syntax is in use. */
5153 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5155 /* Consume the `[' token. */
5156 cp_lexer_consume_token (parser->lexer);
5157 /* Look for the `]' token. */
5158 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5159 /* Remember that this is the `[]' construct. */
5160 array_p = true;
5162 else
5163 array_p = false;
5165 /* Parse the cast-expression. */
5166 expression = cp_parser_simple_cast_expression (parser);
5168 /* A delete-expression may not appear in an integral constant
5169 expression. */
5170 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5171 return error_mark_node;
5173 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5176 /* Parse a cast-expression.
5178 cast-expression:
5179 unary-expression
5180 ( type-id ) cast-expression
5182 ADDRESS_P is true iff the unary-expression is appearing as the
5183 operand of the `&' operator. CAST_P is true if this expression is
5184 the target of a cast.
5186 Returns a representation of the expression. */
5188 static tree
5189 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5191 /* If it's a `(', then we might be looking at a cast. */
5192 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5194 tree type = NULL_TREE;
5195 tree expr = NULL_TREE;
5196 bool compound_literal_p;
5197 const char *saved_message;
5199 /* There's no way to know yet whether or not this is a cast.
5200 For example, `(int (3))' is a unary-expression, while `(int)
5201 3' is a cast. So, we resort to parsing tentatively. */
5202 cp_parser_parse_tentatively (parser);
5203 /* Types may not be defined in a cast. */
5204 saved_message = parser->type_definition_forbidden_message;
5205 parser->type_definition_forbidden_message
5206 = "types may not be defined in casts";
5207 /* Consume the `('. */
5208 cp_lexer_consume_token (parser->lexer);
5209 /* A very tricky bit is that `(struct S) { 3 }' is a
5210 compound-literal (which we permit in C++ as an extension).
5211 But, that construct is not a cast-expression -- it is a
5212 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5213 is legal; if the compound-literal were a cast-expression,
5214 you'd need an extra set of parentheses.) But, if we parse
5215 the type-id, and it happens to be a class-specifier, then we
5216 will commit to the parse at that point, because we cannot
5217 undo the action that is done when creating a new class. So,
5218 then we cannot back up and do a postfix-expression.
5220 Therefore, we scan ahead to the closing `)', and check to see
5221 if the token after the `)' is a `{'. If so, we are not
5222 looking at a cast-expression.
5224 Save tokens so that we can put them back. */
5225 cp_lexer_save_tokens (parser->lexer);
5226 /* Skip tokens until the next token is a closing parenthesis.
5227 If we find the closing `)', and the next token is a `{', then
5228 we are looking at a compound-literal. */
5229 compound_literal_p
5230 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5231 /*consume_paren=*/true)
5232 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5233 /* Roll back the tokens we skipped. */
5234 cp_lexer_rollback_tokens (parser->lexer);
5235 /* If we were looking at a compound-literal, simulate an error
5236 so that the call to cp_parser_parse_definitely below will
5237 fail. */
5238 if (compound_literal_p)
5239 cp_parser_simulate_error (parser);
5240 else
5242 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5243 parser->in_type_id_in_expr_p = true;
5244 /* Look for the type-id. */
5245 type = cp_parser_type_id (parser);
5246 /* Look for the closing `)'. */
5247 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5248 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5251 /* Restore the saved message. */
5252 parser->type_definition_forbidden_message = saved_message;
5254 /* If ok so far, parse the dependent expression. We cannot be
5255 sure it is a cast. Consider `(T ())'. It is a parenthesized
5256 ctor of T, but looks like a cast to function returning T
5257 without a dependent expression. */
5258 if (!cp_parser_error_occurred (parser))
5259 expr = cp_parser_cast_expression (parser,
5260 /*address_p=*/false,
5261 /*cast_p=*/true);
5263 if (cp_parser_parse_definitely (parser))
5265 /* Warn about old-style casts, if so requested. */
5266 if (warn_old_style_cast
5267 && !in_system_header
5268 && !VOID_TYPE_P (type)
5269 && current_lang_name != lang_name_c)
5270 warning ("use of old-style cast");
5272 /* Only type conversions to integral or enumeration types
5273 can be used in constant-expressions. */
5274 if (parser->integral_constant_expression_p
5275 && !dependent_type_p (type)
5276 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5277 && (cp_parser_non_integral_constant_expression
5278 (parser,
5279 "a cast to a type other than an integral or "
5280 "enumeration type")))
5281 return error_mark_node;
5283 /* Perform the cast. */
5284 expr = build_c_cast (type, expr);
5285 return expr;
5289 /* If we get here, then it's not a cast, so it must be a
5290 unary-expression. */
5291 return cp_parser_unary_expression (parser, address_p, cast_p);
5294 /* Parse a binary expression of the general form:
5296 pm-expression:
5297 cast-expression
5298 pm-expression .* cast-expression
5299 pm-expression ->* cast-expression
5301 multiplicative-expression:
5302 pm-expression
5303 multiplicative-expression * pm-expression
5304 multiplicative-expression / pm-expression
5305 multiplicative-expression % pm-expression
5307 additive-expression:
5308 multiplicative-expression
5309 additive-expression + multiplicative-expression
5310 additive-expression - multiplicative-expression
5312 shift-expression:
5313 additive-expression
5314 shift-expression << additive-expression
5315 shift-expression >> additive-expression
5317 relational-expression:
5318 shift-expression
5319 relational-expression < shift-expression
5320 relational-expression > shift-expression
5321 relational-expression <= shift-expression
5322 relational-expression >= shift-expression
5324 GNU Extension:
5326 relational-expression:
5327 relational-expression <? shift-expression
5328 relational-expression >? shift-expression
5330 equality-expression:
5331 relational-expression
5332 equality-expression == relational-expression
5333 equality-expression != relational-expression
5335 and-expression:
5336 equality-expression
5337 and-expression & equality-expression
5339 exclusive-or-expression:
5340 and-expression
5341 exclusive-or-expression ^ and-expression
5343 inclusive-or-expression:
5344 exclusive-or-expression
5345 inclusive-or-expression | exclusive-or-expression
5347 logical-and-expression:
5348 inclusive-or-expression
5349 logical-and-expression && inclusive-or-expression
5351 logical-or-expression:
5352 logical-and-expression
5353 logical-or-expression || logical-and-expression
5355 All these are implemented with a single function like:
5357 binary-expression:
5358 simple-cast-expression
5359 binary-expression <token> binary-expression
5361 CAST_P is true if this expression is the target of a cast.
5363 The binops_by_token map is used to get the tree codes for each <token> type.
5364 binary-expressions are associated according to a precedence table. */
5366 #define TOKEN_PRECEDENCE(token) \
5367 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5368 ? PREC_NOT_OPERATOR \
5369 : binops_by_token[token->type].prec)
5371 static tree
5372 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5374 cp_parser_expression_stack stack;
5375 cp_parser_expression_stack_entry *sp = &stack[0];
5376 tree lhs, rhs;
5377 cp_token *token;
5378 enum tree_code tree_type;
5379 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5380 bool overloaded_p;
5382 /* Parse the first expression. */
5383 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5385 for (;;)
5387 /* Get an operator token. */
5388 token = cp_lexer_peek_token (parser->lexer);
5389 new_prec = TOKEN_PRECEDENCE (token);
5391 /* Popping an entry off the stack means we completed a subexpression:
5392 - either we found a token which is not an operator (`>' where it is not
5393 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5394 will happen repeatedly;
5395 - or, we found an operator which has lower priority. This is the case
5396 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5397 parsing `3 * 4'. */
5398 if (new_prec <= prec)
5400 if (sp == stack)
5401 break;
5402 else
5403 goto pop;
5406 get_rhs:
5407 tree_type = binops_by_token[token->type].tree_type;
5409 /* We used the operator token. */
5410 cp_lexer_consume_token (parser->lexer);
5412 /* Extract another operand. It may be the RHS of this expression
5413 or the LHS of a new, higher priority expression. */
5414 rhs = cp_parser_simple_cast_expression (parser);
5416 /* Get another operator token. Look up its precedence to avoid
5417 building a useless (immediately popped) stack entry for common
5418 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5419 token = cp_lexer_peek_token (parser->lexer);
5420 lookahead_prec = TOKEN_PRECEDENCE (token);
5421 if (lookahead_prec > new_prec)
5423 /* ... and prepare to parse the RHS of the new, higher priority
5424 expression. Since precedence levels on the stack are
5425 monotonically increasing, we do not have to care about
5426 stack overflows. */
5427 sp->prec = prec;
5428 sp->tree_type = tree_type;
5429 sp->lhs = lhs;
5430 sp++;
5431 lhs = rhs;
5432 prec = new_prec;
5433 new_prec = lookahead_prec;
5434 goto get_rhs;
5436 pop:
5437 /* If the stack is not empty, we have parsed into LHS the right side
5438 (`4' in the example above) of an expression we had suspended.
5439 We can use the information on the stack to recover the LHS (`3')
5440 from the stack together with the tree code (`MULT_EXPR'), and
5441 the precedence of the higher level subexpression
5442 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5443 which will be used to actually build the additive expression. */
5444 --sp;
5445 prec = sp->prec;
5446 tree_type = sp->tree_type;
5447 rhs = lhs;
5448 lhs = sp->lhs;
5451 overloaded_p = false;
5452 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5454 /* If the binary operator required the use of an overloaded operator,
5455 then this expression cannot be an integral constant-expression.
5456 An overloaded operator can be used even if both operands are
5457 otherwise permissible in an integral constant-expression if at
5458 least one of the operands is of enumeration type. */
5460 if (overloaded_p
5461 && (cp_parser_non_integral_constant_expression
5462 (parser, "calls to overloaded operators")))
5463 return error_mark_node;
5466 return lhs;
5470 /* Parse the `? expression : assignment-expression' part of a
5471 conditional-expression. The LOGICAL_OR_EXPR is the
5472 logical-or-expression that started the conditional-expression.
5473 Returns a representation of the entire conditional-expression.
5475 This routine is used by cp_parser_assignment_expression.
5477 ? expression : assignment-expression
5479 GNU Extensions:
5481 ? : assignment-expression */
5483 static tree
5484 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5486 tree expr;
5487 tree assignment_expr;
5489 /* Consume the `?' token. */
5490 cp_lexer_consume_token (parser->lexer);
5491 if (cp_parser_allow_gnu_extensions_p (parser)
5492 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5493 /* Implicit true clause. */
5494 expr = NULL_TREE;
5495 else
5496 /* Parse the expression. */
5497 expr = cp_parser_expression (parser, /*cast_p=*/false);
5499 /* The next token should be a `:'. */
5500 cp_parser_require (parser, CPP_COLON, "`:'");
5501 /* Parse the assignment-expression. */
5502 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5504 /* Build the conditional-expression. */
5505 return build_x_conditional_expr (logical_or_expr,
5506 expr,
5507 assignment_expr);
5510 /* Parse an assignment-expression.
5512 assignment-expression:
5513 conditional-expression
5514 logical-or-expression assignment-operator assignment_expression
5515 throw-expression
5517 CAST_P is true if this expression is the target of a cast.
5519 Returns a representation for the expression. */
5521 static tree
5522 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5524 tree expr;
5526 /* If the next token is the `throw' keyword, then we're looking at
5527 a throw-expression. */
5528 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5529 expr = cp_parser_throw_expression (parser);
5530 /* Otherwise, it must be that we are looking at a
5531 logical-or-expression. */
5532 else
5534 /* Parse the binary expressions (logical-or-expression). */
5535 expr = cp_parser_binary_expression (parser, cast_p);
5536 /* If the next token is a `?' then we're actually looking at a
5537 conditional-expression. */
5538 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5539 return cp_parser_question_colon_clause (parser, expr);
5540 else
5542 enum tree_code assignment_operator;
5544 /* If it's an assignment-operator, we're using the second
5545 production. */
5546 assignment_operator
5547 = cp_parser_assignment_operator_opt (parser);
5548 if (assignment_operator != ERROR_MARK)
5550 tree rhs;
5552 /* Parse the right-hand side of the assignment. */
5553 rhs = cp_parser_assignment_expression (parser, cast_p);
5554 /* An assignment may not appear in a
5555 constant-expression. */
5556 if (cp_parser_non_integral_constant_expression (parser,
5557 "an assignment"))
5558 return error_mark_node;
5559 /* Build the assignment expression. */
5560 expr = build_x_modify_expr (expr,
5561 assignment_operator,
5562 rhs);
5567 return expr;
5570 /* Parse an (optional) assignment-operator.
5572 assignment-operator: one of
5573 = *= /= %= += -= >>= <<= &= ^= |=
5575 GNU Extension:
5577 assignment-operator: one of
5578 <?= >?=
5580 If the next token is an assignment operator, the corresponding tree
5581 code is returned, and the token is consumed. For example, for
5582 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5583 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5584 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5585 operator, ERROR_MARK is returned. */
5587 static enum tree_code
5588 cp_parser_assignment_operator_opt (cp_parser* parser)
5590 enum tree_code op;
5591 cp_token *token;
5593 /* Peek at the next toen. */
5594 token = cp_lexer_peek_token (parser->lexer);
5596 switch (token->type)
5598 case CPP_EQ:
5599 op = NOP_EXPR;
5600 break;
5602 case CPP_MULT_EQ:
5603 op = MULT_EXPR;
5604 break;
5606 case CPP_DIV_EQ:
5607 op = TRUNC_DIV_EXPR;
5608 break;
5610 case CPP_MOD_EQ:
5611 op = TRUNC_MOD_EXPR;
5612 break;
5614 case CPP_PLUS_EQ:
5615 op = PLUS_EXPR;
5616 break;
5618 case CPP_MINUS_EQ:
5619 op = MINUS_EXPR;
5620 break;
5622 case CPP_RSHIFT_EQ:
5623 op = RSHIFT_EXPR;
5624 break;
5626 case CPP_LSHIFT_EQ:
5627 op = LSHIFT_EXPR;
5628 break;
5630 case CPP_AND_EQ:
5631 op = BIT_AND_EXPR;
5632 break;
5634 case CPP_XOR_EQ:
5635 op = BIT_XOR_EXPR;
5636 break;
5638 case CPP_OR_EQ:
5639 op = BIT_IOR_EXPR;
5640 break;
5642 case CPP_MIN_EQ:
5643 op = MIN_EXPR;
5644 break;
5646 case CPP_MAX_EQ:
5647 op = MAX_EXPR;
5648 break;
5650 default:
5651 /* Nothing else is an assignment operator. */
5652 op = ERROR_MARK;
5655 /* If it was an assignment operator, consume it. */
5656 if (op != ERROR_MARK)
5657 cp_lexer_consume_token (parser->lexer);
5659 return op;
5662 /* Parse an expression.
5664 expression:
5665 assignment-expression
5666 expression , assignment-expression
5668 CAST_P is true if this expression is the target of a cast.
5670 Returns a representation of the expression. */
5672 static tree
5673 cp_parser_expression (cp_parser* parser, bool cast_p)
5675 tree expression = NULL_TREE;
5677 while (true)
5679 tree assignment_expression;
5681 /* Parse the next assignment-expression. */
5682 assignment_expression
5683 = cp_parser_assignment_expression (parser, cast_p);
5684 /* If this is the first assignment-expression, we can just
5685 save it away. */
5686 if (!expression)
5687 expression = assignment_expression;
5688 else
5689 expression = build_x_compound_expr (expression,
5690 assignment_expression);
5691 /* If the next token is not a comma, then we are done with the
5692 expression. */
5693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5694 break;
5695 /* Consume the `,'. */
5696 cp_lexer_consume_token (parser->lexer);
5697 /* A comma operator cannot appear in a constant-expression. */
5698 if (cp_parser_non_integral_constant_expression (parser,
5699 "a comma operator"))
5700 expression = error_mark_node;
5703 return expression;
5706 /* Parse a constant-expression.
5708 constant-expression:
5709 conditional-expression
5711 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5712 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5713 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5714 is false, NON_CONSTANT_P should be NULL. */
5716 static tree
5717 cp_parser_constant_expression (cp_parser* parser,
5718 bool allow_non_constant_p,
5719 bool *non_constant_p)
5721 bool saved_integral_constant_expression_p;
5722 bool saved_allow_non_integral_constant_expression_p;
5723 bool saved_non_integral_constant_expression_p;
5724 tree expression;
5726 /* It might seem that we could simply parse the
5727 conditional-expression, and then check to see if it were
5728 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5729 one that the compiler can figure out is constant, possibly after
5730 doing some simplifications or optimizations. The standard has a
5731 precise definition of constant-expression, and we must honor
5732 that, even though it is somewhat more restrictive.
5734 For example:
5736 int i[(2, 3)];
5738 is not a legal declaration, because `(2, 3)' is not a
5739 constant-expression. The `,' operator is forbidden in a
5740 constant-expression. However, GCC's constant-folding machinery
5741 will fold this operation to an INTEGER_CST for `3'. */
5743 /* Save the old settings. */
5744 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5745 saved_allow_non_integral_constant_expression_p
5746 = parser->allow_non_integral_constant_expression_p;
5747 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5748 /* We are now parsing a constant-expression. */
5749 parser->integral_constant_expression_p = true;
5750 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5751 parser->non_integral_constant_expression_p = false;
5752 /* Although the grammar says "conditional-expression", we parse an
5753 "assignment-expression", which also permits "throw-expression"
5754 and the use of assignment operators. In the case that
5755 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5756 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5757 actually essential that we look for an assignment-expression.
5758 For example, cp_parser_initializer_clauses uses this function to
5759 determine whether a particular assignment-expression is in fact
5760 constant. */
5761 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5762 /* Restore the old settings. */
5763 parser->integral_constant_expression_p
5764 = saved_integral_constant_expression_p;
5765 parser->allow_non_integral_constant_expression_p
5766 = saved_allow_non_integral_constant_expression_p;
5767 if (allow_non_constant_p)
5768 *non_constant_p = parser->non_integral_constant_expression_p;
5769 else if (parser->non_integral_constant_expression_p)
5770 expression = error_mark_node;
5771 parser->non_integral_constant_expression_p
5772 = saved_non_integral_constant_expression_p;
5774 return expression;
5777 /* Parse __builtin_offsetof.
5779 offsetof-expression:
5780 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5782 offsetof-member-designator:
5783 id-expression
5784 | offsetof-member-designator "." id-expression
5785 | offsetof-member-designator "[" expression "]"
5788 static tree
5789 cp_parser_builtin_offsetof (cp_parser *parser)
5791 int save_ice_p, save_non_ice_p;
5792 tree type, expr;
5793 cp_id_kind dummy;
5795 /* We're about to accept non-integral-constant things, but will
5796 definitely yield an integral constant expression. Save and
5797 restore these values around our local parsing. */
5798 save_ice_p = parser->integral_constant_expression_p;
5799 save_non_ice_p = parser->non_integral_constant_expression_p;
5801 /* Consume the "__builtin_offsetof" token. */
5802 cp_lexer_consume_token (parser->lexer);
5803 /* Consume the opening `('. */
5804 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5805 /* Parse the type-id. */
5806 type = cp_parser_type_id (parser);
5807 /* Look for the `,'. */
5808 cp_parser_require (parser, CPP_COMMA, "`,'");
5810 /* Build the (type *)null that begins the traditional offsetof macro. */
5811 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5813 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5814 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5815 true, &dummy);
5816 while (true)
5818 cp_token *token = cp_lexer_peek_token (parser->lexer);
5819 switch (token->type)
5821 case CPP_OPEN_SQUARE:
5822 /* offsetof-member-designator "[" expression "]" */
5823 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5824 break;
5826 case CPP_DOT:
5827 /* offsetof-member-designator "." identifier */
5828 cp_lexer_consume_token (parser->lexer);
5829 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5830 true, &dummy);
5831 break;
5833 case CPP_CLOSE_PAREN:
5834 /* Consume the ")" token. */
5835 cp_lexer_consume_token (parser->lexer);
5836 goto success;
5838 default:
5839 /* Error. We know the following require will fail, but
5840 that gives the proper error message. */
5841 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5842 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5843 expr = error_mark_node;
5844 goto failure;
5848 success:
5849 /* If we're processing a template, we can't finish the semantics yet.
5850 Otherwise we can fold the entire expression now. */
5851 if (processing_template_decl)
5852 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5853 else
5854 expr = fold_offsetof (expr);
5856 failure:
5857 parser->integral_constant_expression_p = save_ice_p;
5858 parser->non_integral_constant_expression_p = save_non_ice_p;
5860 return expr;
5863 /* Statements [gram.stmt.stmt] */
5865 /* Parse a statement.
5867 statement:
5868 labeled-statement
5869 expression-statement
5870 compound-statement
5871 selection-statement
5872 iteration-statement
5873 jump-statement
5874 declaration-statement
5875 try-block */
5877 static void
5878 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5880 tree statement;
5881 cp_token *token;
5882 location_t statement_location;
5884 /* There is no statement yet. */
5885 statement = NULL_TREE;
5886 /* Peek at the next token. */
5887 token = cp_lexer_peek_token (parser->lexer);
5888 /* Remember the location of the first token in the statement. */
5889 statement_location = token->location;
5890 /* If this is a keyword, then that will often determine what kind of
5891 statement we have. */
5892 if (token->type == CPP_KEYWORD)
5894 enum rid keyword = token->keyword;
5896 switch (keyword)
5898 case RID_CASE:
5899 case RID_DEFAULT:
5900 statement = cp_parser_labeled_statement (parser,
5901 in_statement_expr);
5902 break;
5904 case RID_IF:
5905 case RID_SWITCH:
5906 statement = cp_parser_selection_statement (parser);
5907 break;
5909 case RID_WHILE:
5910 case RID_DO:
5911 case RID_FOR:
5912 statement = cp_parser_iteration_statement (parser);
5913 break;
5915 case RID_BREAK:
5916 case RID_CONTINUE:
5917 case RID_RETURN:
5918 case RID_GOTO:
5919 statement = cp_parser_jump_statement (parser);
5920 break;
5922 case RID_TRY:
5923 statement = cp_parser_try_block (parser);
5924 break;
5926 default:
5927 /* It might be a keyword like `int' that can start a
5928 declaration-statement. */
5929 break;
5932 else if (token->type == CPP_NAME)
5934 /* If the next token is a `:', then we are looking at a
5935 labeled-statement. */
5936 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5937 if (token->type == CPP_COLON)
5938 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5940 /* Anything that starts with a `{' must be a compound-statement. */
5941 else if (token->type == CPP_OPEN_BRACE)
5942 statement = cp_parser_compound_statement (parser, NULL, false);
5943 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5944 a statement all its own. */
5945 else if (token->type == CPP_PRAGMA)
5947 cp_lexer_handle_pragma (parser->lexer);
5948 return;
5951 /* Everything else must be a declaration-statement or an
5952 expression-statement. Try for the declaration-statement
5953 first, unless we are looking at a `;', in which case we know that
5954 we have an expression-statement. */
5955 if (!statement)
5957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5959 cp_parser_parse_tentatively (parser);
5960 /* Try to parse the declaration-statement. */
5961 cp_parser_declaration_statement (parser);
5962 /* If that worked, we're done. */
5963 if (cp_parser_parse_definitely (parser))
5964 return;
5966 /* Look for an expression-statement instead. */
5967 statement = cp_parser_expression_statement (parser, in_statement_expr);
5970 /* Set the line number for the statement. */
5971 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5972 SET_EXPR_LOCATION (statement, statement_location);
5975 /* Parse a labeled-statement.
5977 labeled-statement:
5978 identifier : statement
5979 case constant-expression : statement
5980 default : statement
5982 GNU Extension:
5984 labeled-statement:
5985 case constant-expression ... constant-expression : statement
5987 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5988 For an ordinary label, returns a LABEL_EXPR. */
5990 static tree
5991 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5993 cp_token *token;
5994 tree statement = error_mark_node;
5996 /* The next token should be an identifier. */
5997 token = cp_lexer_peek_token (parser->lexer);
5998 if (token->type != CPP_NAME
5999 && token->type != CPP_KEYWORD)
6001 cp_parser_error (parser, "expected labeled-statement");
6002 return error_mark_node;
6005 switch (token->keyword)
6007 case RID_CASE:
6009 tree expr, expr_hi;
6010 cp_token *ellipsis;
6012 /* Consume the `case' token. */
6013 cp_lexer_consume_token (parser->lexer);
6014 /* Parse the constant-expression. */
6015 expr = cp_parser_constant_expression (parser,
6016 /*allow_non_constant_p=*/false,
6017 NULL);
6019 ellipsis = cp_lexer_peek_token (parser->lexer);
6020 if (ellipsis->type == CPP_ELLIPSIS)
6022 /* Consume the `...' token. */
6023 cp_lexer_consume_token (parser->lexer);
6024 expr_hi =
6025 cp_parser_constant_expression (parser,
6026 /*allow_non_constant_p=*/false,
6027 NULL);
6028 /* We don't need to emit warnings here, as the common code
6029 will do this for us. */
6031 else
6032 expr_hi = NULL_TREE;
6034 if (!parser->in_switch_statement_p)
6035 error ("case label %qE not within a switch statement", expr);
6036 else
6037 statement = finish_case_label (expr, expr_hi);
6039 break;
6041 case RID_DEFAULT:
6042 /* Consume the `default' token. */
6043 cp_lexer_consume_token (parser->lexer);
6044 if (!parser->in_switch_statement_p)
6045 error ("case label not within a switch statement");
6046 else
6047 statement = finish_case_label (NULL_TREE, NULL_TREE);
6048 break;
6050 default:
6051 /* Anything else must be an ordinary label. */
6052 statement = finish_label_stmt (cp_parser_identifier (parser));
6053 break;
6056 /* Require the `:' token. */
6057 cp_parser_require (parser, CPP_COLON, "`:'");
6058 /* Parse the labeled statement. */
6059 cp_parser_statement (parser, in_statement_expr);
6061 /* Return the label, in the case of a `case' or `default' label. */
6062 return statement;
6065 /* Parse an expression-statement.
6067 expression-statement:
6068 expression [opt] ;
6070 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6071 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6072 indicates whether this expression-statement is part of an
6073 expression statement. */
6075 static tree
6076 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6078 tree statement = NULL_TREE;
6080 /* If the next token is a ';', then there is no expression
6081 statement. */
6082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6083 statement = cp_parser_expression (parser, /*cast_p=*/false);
6085 /* Consume the final `;'. */
6086 cp_parser_consume_semicolon_at_end_of_statement (parser);
6088 if (in_statement_expr
6089 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6090 /* This is the final expression statement of a statement
6091 expression. */
6092 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6093 else if (statement)
6094 statement = finish_expr_stmt (statement);
6095 else
6096 finish_stmt ();
6098 return statement;
6101 /* Parse a compound-statement.
6103 compound-statement:
6104 { statement-seq [opt] }
6106 Returns a tree representing the statement. */
6108 static tree
6109 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6110 bool in_try)
6112 tree compound_stmt;
6114 /* Consume the `{'. */
6115 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6116 return error_mark_node;
6117 /* Begin the compound-statement. */
6118 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6119 /* Parse an (optional) statement-seq. */
6120 cp_parser_statement_seq_opt (parser, in_statement_expr);
6121 /* Finish the compound-statement. */
6122 finish_compound_stmt (compound_stmt);
6123 /* Consume the `}'. */
6124 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6126 return compound_stmt;
6129 /* Parse an (optional) statement-seq.
6131 statement-seq:
6132 statement
6133 statement-seq [opt] statement */
6135 static void
6136 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6138 /* Scan statements until there aren't any more. */
6139 while (true)
6141 /* If we're looking at a `}', then we've run out of statements. */
6142 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6143 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6144 break;
6146 /* Parse the statement. */
6147 cp_parser_statement (parser, in_statement_expr);
6151 /* Parse a selection-statement.
6153 selection-statement:
6154 if ( condition ) statement
6155 if ( condition ) statement else statement
6156 switch ( condition ) statement
6158 Returns the new IF_STMT or SWITCH_STMT. */
6160 static tree
6161 cp_parser_selection_statement (cp_parser* parser)
6163 cp_token *token;
6164 enum rid keyword;
6166 /* Peek at the next token. */
6167 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6169 /* See what kind of keyword it is. */
6170 keyword = token->keyword;
6171 switch (keyword)
6173 case RID_IF:
6174 case RID_SWITCH:
6176 tree statement;
6177 tree condition;
6179 /* Look for the `('. */
6180 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6182 cp_parser_skip_to_end_of_statement (parser);
6183 return error_mark_node;
6186 /* Begin the selection-statement. */
6187 if (keyword == RID_IF)
6188 statement = begin_if_stmt ();
6189 else
6190 statement = begin_switch_stmt ();
6192 /* Parse the condition. */
6193 condition = cp_parser_condition (parser);
6194 /* Look for the `)'. */
6195 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6196 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6197 /*consume_paren=*/true);
6199 if (keyword == RID_IF)
6201 /* Add the condition. */
6202 finish_if_stmt_cond (condition, statement);
6204 /* Parse the then-clause. */
6205 cp_parser_implicitly_scoped_statement (parser);
6206 finish_then_clause (statement);
6208 /* If the next token is `else', parse the else-clause. */
6209 if (cp_lexer_next_token_is_keyword (parser->lexer,
6210 RID_ELSE))
6212 /* Consume the `else' keyword. */
6213 cp_lexer_consume_token (parser->lexer);
6214 begin_else_clause (statement);
6215 /* Parse the else-clause. */
6216 cp_parser_implicitly_scoped_statement (parser);
6217 finish_else_clause (statement);
6220 /* Now we're all done with the if-statement. */
6221 finish_if_stmt (statement);
6223 else
6225 bool in_switch_statement_p;
6227 /* Add the condition. */
6228 finish_switch_cond (condition, statement);
6230 /* Parse the body of the switch-statement. */
6231 in_switch_statement_p = parser->in_switch_statement_p;
6232 parser->in_switch_statement_p = true;
6233 cp_parser_implicitly_scoped_statement (parser);
6234 parser->in_switch_statement_p = in_switch_statement_p;
6236 /* Now we're all done with the switch-statement. */
6237 finish_switch_stmt (statement);
6240 return statement;
6242 break;
6244 default:
6245 cp_parser_error (parser, "expected selection-statement");
6246 return error_mark_node;
6250 /* Parse a condition.
6252 condition:
6253 expression
6254 type-specifier-seq declarator = assignment-expression
6256 GNU Extension:
6258 condition:
6259 type-specifier-seq declarator asm-specification [opt]
6260 attributes [opt] = assignment-expression
6262 Returns the expression that should be tested. */
6264 static tree
6265 cp_parser_condition (cp_parser* parser)
6267 cp_decl_specifier_seq type_specifiers;
6268 const char *saved_message;
6270 /* Try the declaration first. */
6271 cp_parser_parse_tentatively (parser);
6272 /* New types are not allowed in the type-specifier-seq for a
6273 condition. */
6274 saved_message = parser->type_definition_forbidden_message;
6275 parser->type_definition_forbidden_message
6276 = "types may not be defined in conditions";
6277 /* Parse the type-specifier-seq. */
6278 cp_parser_type_specifier_seq (parser, &type_specifiers);
6279 /* Restore the saved message. */
6280 parser->type_definition_forbidden_message = saved_message;
6281 /* If all is well, we might be looking at a declaration. */
6282 if (!cp_parser_error_occurred (parser))
6284 tree decl;
6285 tree asm_specification;
6286 tree attributes;
6287 cp_declarator *declarator;
6288 tree initializer = NULL_TREE;
6290 /* Parse the declarator. */
6291 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6292 /*ctor_dtor_or_conv_p=*/NULL,
6293 /*parenthesized_p=*/NULL,
6294 /*member_p=*/false);
6295 /* Parse the attributes. */
6296 attributes = cp_parser_attributes_opt (parser);
6297 /* Parse the asm-specification. */
6298 asm_specification = cp_parser_asm_specification_opt (parser);
6299 /* If the next token is not an `=', then we might still be
6300 looking at an expression. For example:
6302 if (A(a).x)
6304 looks like a decl-specifier-seq and a declarator -- but then
6305 there is no `=', so this is an expression. */
6306 cp_parser_require (parser, CPP_EQ, "`='");
6307 /* If we did see an `=', then we are looking at a declaration
6308 for sure. */
6309 if (cp_parser_parse_definitely (parser))
6311 tree pushed_scope;
6313 /* Create the declaration. */
6314 decl = start_decl (declarator, &type_specifiers,
6315 /*initialized_p=*/true,
6316 attributes, /*prefix_attributes=*/NULL_TREE,
6317 &pushed_scope);
6318 /* Parse the assignment-expression. */
6319 initializer = cp_parser_assignment_expression (parser,
6320 /*cast_p=*/false);
6322 /* Process the initializer. */
6323 cp_finish_decl (decl,
6324 initializer,
6325 asm_specification,
6326 LOOKUP_ONLYCONVERTING);
6328 if (pushed_scope)
6329 pop_scope (pushed_scope);
6331 return convert_from_reference (decl);
6334 /* If we didn't even get past the declarator successfully, we are
6335 definitely not looking at a declaration. */
6336 else
6337 cp_parser_abort_tentative_parse (parser);
6339 /* Otherwise, we are looking at an expression. */
6340 return cp_parser_expression (parser, /*cast_p=*/false);
6343 /* Parse an iteration-statement.
6345 iteration-statement:
6346 while ( condition ) statement
6347 do statement while ( expression ) ;
6348 for ( for-init-statement condition [opt] ; expression [opt] )
6349 statement
6351 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6353 static tree
6354 cp_parser_iteration_statement (cp_parser* parser)
6356 cp_token *token;
6357 enum rid keyword;
6358 tree statement;
6359 bool in_iteration_statement_p;
6362 /* Peek at the next token. */
6363 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6364 if (!token)
6365 return error_mark_node;
6367 /* Remember whether or not we are already within an iteration
6368 statement. */
6369 in_iteration_statement_p = parser->in_iteration_statement_p;
6371 /* See what kind of keyword it is. */
6372 keyword = token->keyword;
6373 switch (keyword)
6375 case RID_WHILE:
6377 tree condition;
6379 /* Begin the while-statement. */
6380 statement = begin_while_stmt ();
6381 /* Look for the `('. */
6382 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6383 /* Parse the condition. */
6384 condition = cp_parser_condition (parser);
6385 finish_while_stmt_cond (condition, statement);
6386 /* Look for the `)'. */
6387 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6388 /* Parse the dependent statement. */
6389 parser->in_iteration_statement_p = true;
6390 cp_parser_already_scoped_statement (parser);
6391 parser->in_iteration_statement_p = in_iteration_statement_p;
6392 /* We're done with the while-statement. */
6393 finish_while_stmt (statement);
6395 break;
6397 case RID_DO:
6399 tree expression;
6401 /* Begin the do-statement. */
6402 statement = begin_do_stmt ();
6403 /* Parse the body of the do-statement. */
6404 parser->in_iteration_statement_p = true;
6405 cp_parser_implicitly_scoped_statement (parser);
6406 parser->in_iteration_statement_p = in_iteration_statement_p;
6407 finish_do_body (statement);
6408 /* Look for the `while' keyword. */
6409 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6410 /* Look for the `('. */
6411 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6412 /* Parse the expression. */
6413 expression = cp_parser_expression (parser, /*cast_p=*/false);
6414 /* We're done with the do-statement. */
6415 finish_do_stmt (expression, statement);
6416 /* Look for the `)'. */
6417 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6418 /* Look for the `;'. */
6419 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6421 break;
6423 case RID_FOR:
6425 tree condition = NULL_TREE;
6426 tree expression = NULL_TREE;
6428 /* Begin the for-statement. */
6429 statement = begin_for_stmt ();
6430 /* Look for the `('. */
6431 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6432 /* Parse the initialization. */
6433 cp_parser_for_init_statement (parser);
6434 finish_for_init_stmt (statement);
6436 /* If there's a condition, process it. */
6437 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6438 condition = cp_parser_condition (parser);
6439 finish_for_cond (condition, statement);
6440 /* Look for the `;'. */
6441 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6443 /* If there's an expression, process it. */
6444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6445 expression = cp_parser_expression (parser, /*cast_p=*/false);
6446 finish_for_expr (expression, statement);
6447 /* Look for the `)'. */
6448 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6450 /* Parse the body of the for-statement. */
6451 parser->in_iteration_statement_p = true;
6452 cp_parser_already_scoped_statement (parser);
6453 parser->in_iteration_statement_p = in_iteration_statement_p;
6455 /* We're done with the for-statement. */
6456 finish_for_stmt (statement);
6458 break;
6460 default:
6461 cp_parser_error (parser, "expected iteration-statement");
6462 statement = error_mark_node;
6463 break;
6466 return statement;
6469 /* Parse a for-init-statement.
6471 for-init-statement:
6472 expression-statement
6473 simple-declaration */
6475 static void
6476 cp_parser_for_init_statement (cp_parser* parser)
6478 /* If the next token is a `;', then we have an empty
6479 expression-statement. Grammatically, this is also a
6480 simple-declaration, but an invalid one, because it does not
6481 declare anything. Therefore, if we did not handle this case
6482 specially, we would issue an error message about an invalid
6483 declaration. */
6484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6486 /* We're going to speculatively look for a declaration, falling back
6487 to an expression, if necessary. */
6488 cp_parser_parse_tentatively (parser);
6489 /* Parse the declaration. */
6490 cp_parser_simple_declaration (parser,
6491 /*function_definition_allowed_p=*/false);
6492 /* If the tentative parse failed, then we shall need to look for an
6493 expression-statement. */
6494 if (cp_parser_parse_definitely (parser))
6495 return;
6498 cp_parser_expression_statement (parser, false);
6501 /* Parse a jump-statement.
6503 jump-statement:
6504 break ;
6505 continue ;
6506 return expression [opt] ;
6507 goto identifier ;
6509 GNU extension:
6511 jump-statement:
6512 goto * expression ;
6514 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6516 static tree
6517 cp_parser_jump_statement (cp_parser* parser)
6519 tree statement = error_mark_node;
6520 cp_token *token;
6521 enum rid keyword;
6523 /* Peek at the next token. */
6524 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6525 if (!token)
6526 return error_mark_node;
6528 /* See what kind of keyword it is. */
6529 keyword = token->keyword;
6530 switch (keyword)
6532 case RID_BREAK:
6533 if (!parser->in_switch_statement_p
6534 && !parser->in_iteration_statement_p)
6536 error ("break statement not within loop or switch");
6537 statement = error_mark_node;
6539 else
6540 statement = finish_break_stmt ();
6541 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6542 break;
6544 case RID_CONTINUE:
6545 if (!parser->in_iteration_statement_p)
6547 error ("continue statement not within a loop");
6548 statement = error_mark_node;
6550 else
6551 statement = finish_continue_stmt ();
6552 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6553 break;
6555 case RID_RETURN:
6557 tree expr;
6559 /* If the next token is a `;', then there is no
6560 expression. */
6561 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6562 expr = cp_parser_expression (parser, /*cast_p=*/false);
6563 else
6564 expr = NULL_TREE;
6565 /* Build the return-statement. */
6566 statement = finish_return_stmt (expr);
6567 /* Look for the final `;'. */
6568 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6570 break;
6572 case RID_GOTO:
6573 /* Create the goto-statement. */
6574 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6576 /* Issue a warning about this use of a GNU extension. */
6577 if (pedantic)
6578 pedwarn ("ISO C++ forbids computed gotos");
6579 /* Consume the '*' token. */
6580 cp_lexer_consume_token (parser->lexer);
6581 /* Parse the dependent expression. */
6582 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6584 else
6585 finish_goto_stmt (cp_parser_identifier (parser));
6586 /* Look for the final `;'. */
6587 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6588 break;
6590 default:
6591 cp_parser_error (parser, "expected jump-statement");
6592 break;
6595 return statement;
6598 /* Parse a declaration-statement.
6600 declaration-statement:
6601 block-declaration */
6603 static void
6604 cp_parser_declaration_statement (cp_parser* parser)
6606 void *p;
6608 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6609 p = obstack_alloc (&declarator_obstack, 0);
6611 /* Parse the block-declaration. */
6612 cp_parser_block_declaration (parser, /*statement_p=*/true);
6614 /* Free any declarators allocated. */
6615 obstack_free (&declarator_obstack, p);
6617 /* Finish off the statement. */
6618 finish_stmt ();
6621 /* Some dependent statements (like `if (cond) statement'), are
6622 implicitly in their own scope. In other words, if the statement is
6623 a single statement (as opposed to a compound-statement), it is
6624 none-the-less treated as if it were enclosed in braces. Any
6625 declarations appearing in the dependent statement are out of scope
6626 after control passes that point. This function parses a statement,
6627 but ensures that is in its own scope, even if it is not a
6628 compound-statement.
6630 Returns the new statement. */
6632 static tree
6633 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6635 tree statement;
6637 /* If the token is not a `{', then we must take special action. */
6638 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6640 /* Create a compound-statement. */
6641 statement = begin_compound_stmt (0);
6642 /* Parse the dependent-statement. */
6643 cp_parser_statement (parser, false);
6644 /* Finish the dummy compound-statement. */
6645 finish_compound_stmt (statement);
6647 /* Otherwise, we simply parse the statement directly. */
6648 else
6649 statement = cp_parser_compound_statement (parser, NULL, false);
6651 /* Return the statement. */
6652 return statement;
6655 /* For some dependent statements (like `while (cond) statement'), we
6656 have already created a scope. Therefore, even if the dependent
6657 statement is a compound-statement, we do not want to create another
6658 scope. */
6660 static void
6661 cp_parser_already_scoped_statement (cp_parser* parser)
6663 /* If the token is a `{', then we must take special action. */
6664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6665 cp_parser_statement (parser, false);
6666 else
6668 /* Avoid calling cp_parser_compound_statement, so that we
6669 don't create a new scope. Do everything else by hand. */
6670 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6671 cp_parser_statement_seq_opt (parser, false);
6672 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6676 /* Declarations [gram.dcl.dcl] */
6678 /* Parse an optional declaration-sequence.
6680 declaration-seq:
6681 declaration
6682 declaration-seq declaration */
6684 static void
6685 cp_parser_declaration_seq_opt (cp_parser* parser)
6687 while (true)
6689 cp_token *token;
6691 token = cp_lexer_peek_token (parser->lexer);
6693 if (token->type == CPP_CLOSE_BRACE
6694 || token->type == CPP_EOF)
6695 break;
6697 if (token->type == CPP_SEMICOLON)
6699 /* A declaration consisting of a single semicolon is
6700 invalid. Allow it unless we're being pedantic. */
6701 cp_lexer_consume_token (parser->lexer);
6702 if (pedantic && !in_system_header)
6703 pedwarn ("extra %<;%>");
6704 continue;
6707 /* If we're entering or exiting a region that's implicitly
6708 extern "C", modify the lang context appropriately. */
6709 if (!parser->implicit_extern_c && token->implicit_extern_c)
6711 push_lang_context (lang_name_c);
6712 parser->implicit_extern_c = true;
6714 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6716 pop_lang_context ();
6717 parser->implicit_extern_c = false;
6720 if (token->type == CPP_PRAGMA)
6722 /* A top-level declaration can consist solely of a #pragma.
6723 A nested declaration cannot, so this is done here and not
6724 in cp_parser_declaration. (A #pragma at block scope is
6725 handled in cp_parser_statement.) */
6726 cp_lexer_handle_pragma (parser->lexer);
6727 continue;
6730 /* Parse the declaration itself. */
6731 cp_parser_declaration (parser);
6735 /* Parse a declaration.
6737 declaration:
6738 block-declaration
6739 function-definition
6740 template-declaration
6741 explicit-instantiation
6742 explicit-specialization
6743 linkage-specification
6744 namespace-definition
6746 GNU extension:
6748 declaration:
6749 __extension__ declaration */
6751 static void
6752 cp_parser_declaration (cp_parser* parser)
6754 cp_token token1;
6755 cp_token token2;
6756 int saved_pedantic;
6757 void *p;
6759 /* Check for the `__extension__' keyword. */
6760 if (cp_parser_extension_opt (parser, &saved_pedantic))
6762 /* Parse the qualified declaration. */
6763 cp_parser_declaration (parser);
6764 /* Restore the PEDANTIC flag. */
6765 pedantic = saved_pedantic;
6767 return;
6770 /* Try to figure out what kind of declaration is present. */
6771 token1 = *cp_lexer_peek_token (parser->lexer);
6773 if (token1.type != CPP_EOF)
6774 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6776 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6777 p = obstack_alloc (&declarator_obstack, 0);
6779 /* If the next token is `extern' and the following token is a string
6780 literal, then we have a linkage specification. */
6781 if (token1.keyword == RID_EXTERN
6782 && cp_parser_is_string_literal (&token2))
6783 cp_parser_linkage_specification (parser);
6784 /* If the next token is `template', then we have either a template
6785 declaration, an explicit instantiation, or an explicit
6786 specialization. */
6787 else if (token1.keyword == RID_TEMPLATE)
6789 /* `template <>' indicates a template specialization. */
6790 if (token2.type == CPP_LESS
6791 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6792 cp_parser_explicit_specialization (parser);
6793 /* `template <' indicates a template declaration. */
6794 else if (token2.type == CPP_LESS)
6795 cp_parser_template_declaration (parser, /*member_p=*/false);
6796 /* Anything else must be an explicit instantiation. */
6797 else
6798 cp_parser_explicit_instantiation (parser);
6800 /* If the next token is `export', then we have a template
6801 declaration. */
6802 else if (token1.keyword == RID_EXPORT)
6803 cp_parser_template_declaration (parser, /*member_p=*/false);
6804 /* If the next token is `extern', 'static' or 'inline' and the one
6805 after that is `template', we have a GNU extended explicit
6806 instantiation directive. */
6807 else if (cp_parser_allow_gnu_extensions_p (parser)
6808 && (token1.keyword == RID_EXTERN
6809 || token1.keyword == RID_STATIC
6810 || token1.keyword == RID_INLINE)
6811 && token2.keyword == RID_TEMPLATE)
6812 cp_parser_explicit_instantiation (parser);
6813 /* If the next token is `namespace', check for a named or unnamed
6814 namespace definition. */
6815 else if (token1.keyword == RID_NAMESPACE
6816 && (/* A named namespace definition. */
6817 (token2.type == CPP_NAME
6818 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6819 == CPP_OPEN_BRACE))
6820 /* An unnamed namespace definition. */
6821 || token2.type == CPP_OPEN_BRACE))
6822 cp_parser_namespace_definition (parser);
6823 /* We must have either a block declaration or a function
6824 definition. */
6825 else
6826 /* Try to parse a block-declaration, or a function-definition. */
6827 cp_parser_block_declaration (parser, /*statement_p=*/false);
6829 /* Free any declarators allocated. */
6830 obstack_free (&declarator_obstack, p);
6833 /* Parse a block-declaration.
6835 block-declaration:
6836 simple-declaration
6837 asm-definition
6838 namespace-alias-definition
6839 using-declaration
6840 using-directive
6842 GNU Extension:
6844 block-declaration:
6845 __extension__ block-declaration
6846 label-declaration
6848 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6849 part of a declaration-statement. */
6851 static void
6852 cp_parser_block_declaration (cp_parser *parser,
6853 bool statement_p)
6855 cp_token *token1;
6856 int saved_pedantic;
6858 /* Check for the `__extension__' keyword. */
6859 if (cp_parser_extension_opt (parser, &saved_pedantic))
6861 /* Parse the qualified declaration. */
6862 cp_parser_block_declaration (parser, statement_p);
6863 /* Restore the PEDANTIC flag. */
6864 pedantic = saved_pedantic;
6866 return;
6869 /* Peek at the next token to figure out which kind of declaration is
6870 present. */
6871 token1 = cp_lexer_peek_token (parser->lexer);
6873 /* If the next keyword is `asm', we have an asm-definition. */
6874 if (token1->keyword == RID_ASM)
6876 if (statement_p)
6877 cp_parser_commit_to_tentative_parse (parser);
6878 cp_parser_asm_definition (parser);
6880 /* If the next keyword is `namespace', we have a
6881 namespace-alias-definition. */
6882 else if (token1->keyword == RID_NAMESPACE)
6883 cp_parser_namespace_alias_definition (parser);
6884 /* If the next keyword is `using', we have either a
6885 using-declaration or a using-directive. */
6886 else if (token1->keyword == RID_USING)
6888 cp_token *token2;
6890 if (statement_p)
6891 cp_parser_commit_to_tentative_parse (parser);
6892 /* If the token after `using' is `namespace', then we have a
6893 using-directive. */
6894 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6895 if (token2->keyword == RID_NAMESPACE)
6896 cp_parser_using_directive (parser);
6897 /* Otherwise, it's a using-declaration. */
6898 else
6899 cp_parser_using_declaration (parser);
6901 /* If the next keyword is `__label__' we have a label declaration. */
6902 else if (token1->keyword == RID_LABEL)
6904 if (statement_p)
6905 cp_parser_commit_to_tentative_parse (parser);
6906 cp_parser_label_declaration (parser);
6908 /* Anything else must be a simple-declaration. */
6909 else
6910 cp_parser_simple_declaration (parser, !statement_p);
6913 /* Parse a simple-declaration.
6915 simple-declaration:
6916 decl-specifier-seq [opt] init-declarator-list [opt] ;
6918 init-declarator-list:
6919 init-declarator
6920 init-declarator-list , init-declarator
6922 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6923 function-definition as a simple-declaration. */
6925 static void
6926 cp_parser_simple_declaration (cp_parser* parser,
6927 bool function_definition_allowed_p)
6929 cp_decl_specifier_seq decl_specifiers;
6930 int declares_class_or_enum;
6931 bool saw_declarator;
6933 /* Defer access checks until we know what is being declared; the
6934 checks for names appearing in the decl-specifier-seq should be
6935 done as if we were in the scope of the thing being declared. */
6936 push_deferring_access_checks (dk_deferred);
6938 /* Parse the decl-specifier-seq. We have to keep track of whether
6939 or not the decl-specifier-seq declares a named class or
6940 enumeration type, since that is the only case in which the
6941 init-declarator-list is allowed to be empty.
6943 [dcl.dcl]
6945 In a simple-declaration, the optional init-declarator-list can be
6946 omitted only when declaring a class or enumeration, that is when
6947 the decl-specifier-seq contains either a class-specifier, an
6948 elaborated-type-specifier, or an enum-specifier. */
6949 cp_parser_decl_specifier_seq (parser,
6950 CP_PARSER_FLAGS_OPTIONAL,
6951 &decl_specifiers,
6952 &declares_class_or_enum);
6953 /* We no longer need to defer access checks. */
6954 stop_deferring_access_checks ();
6956 /* In a block scope, a valid declaration must always have a
6957 decl-specifier-seq. By not trying to parse declarators, we can
6958 resolve the declaration/expression ambiguity more quickly. */
6959 if (!function_definition_allowed_p
6960 && !decl_specifiers.any_specifiers_p)
6962 cp_parser_error (parser, "expected declaration");
6963 goto done;
6966 /* If the next two tokens are both identifiers, the code is
6967 erroneous. The usual cause of this situation is code like:
6969 T t;
6971 where "T" should name a type -- but does not. */
6972 if (!decl_specifiers.type
6973 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
6975 /* If parsing tentatively, we should commit; we really are
6976 looking at a declaration. */
6977 cp_parser_commit_to_tentative_parse (parser);
6978 /* Give up. */
6979 goto done;
6982 /* If we have seen at least one decl-specifier, and the next token
6983 is not a parenthesis, then we must be looking at a declaration.
6984 (After "int (" we might be looking at a functional cast.) */
6985 if (decl_specifiers.any_specifiers_p
6986 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6987 cp_parser_commit_to_tentative_parse (parser);
6989 /* Keep going until we hit the `;' at the end of the simple
6990 declaration. */
6991 saw_declarator = false;
6992 while (cp_lexer_next_token_is_not (parser->lexer,
6993 CPP_SEMICOLON))
6995 cp_token *token;
6996 bool function_definition_p;
6997 tree decl;
6999 saw_declarator = true;
7000 /* Parse the init-declarator. */
7001 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7002 function_definition_allowed_p,
7003 /*member_p=*/false,
7004 declares_class_or_enum,
7005 &function_definition_p);
7006 /* If an error occurred while parsing tentatively, exit quickly.
7007 (That usually happens when in the body of a function; each
7008 statement is treated as a declaration-statement until proven
7009 otherwise.) */
7010 if (cp_parser_error_occurred (parser))
7011 goto done;
7012 /* Handle function definitions specially. */
7013 if (function_definition_p)
7015 /* If the next token is a `,', then we are probably
7016 processing something like:
7018 void f() {}, *p;
7020 which is erroneous. */
7021 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7022 error ("mixing declarations and function-definitions is forbidden");
7023 /* Otherwise, we're done with the list of declarators. */
7024 else
7026 pop_deferring_access_checks ();
7027 return;
7030 /* The next token should be either a `,' or a `;'. */
7031 token = cp_lexer_peek_token (parser->lexer);
7032 /* If it's a `,', there are more declarators to come. */
7033 if (token->type == CPP_COMMA)
7034 cp_lexer_consume_token (parser->lexer);
7035 /* If it's a `;', we are done. */
7036 else if (token->type == CPP_SEMICOLON)
7037 break;
7038 /* Anything else is an error. */
7039 else
7041 /* If we have already issued an error message we don't need
7042 to issue another one. */
7043 if (decl != error_mark_node
7044 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7045 cp_parser_error (parser, "expected %<,%> or %<;%>");
7046 /* Skip tokens until we reach the end of the statement. */
7047 cp_parser_skip_to_end_of_statement (parser);
7048 /* If the next token is now a `;', consume it. */
7049 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7050 cp_lexer_consume_token (parser->lexer);
7051 goto done;
7053 /* After the first time around, a function-definition is not
7054 allowed -- even if it was OK at first. For example:
7056 int i, f() {}
7058 is not valid. */
7059 function_definition_allowed_p = false;
7062 /* Issue an error message if no declarators are present, and the
7063 decl-specifier-seq does not itself declare a class or
7064 enumeration. */
7065 if (!saw_declarator)
7067 if (cp_parser_declares_only_class_p (parser))
7068 shadow_tag (&decl_specifiers);
7069 /* Perform any deferred access checks. */
7070 perform_deferred_access_checks ();
7073 /* Consume the `;'. */
7074 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7076 done:
7077 pop_deferring_access_checks ();
7080 /* Parse a decl-specifier-seq.
7082 decl-specifier-seq:
7083 decl-specifier-seq [opt] decl-specifier
7085 decl-specifier:
7086 storage-class-specifier
7087 type-specifier
7088 function-specifier
7089 friend
7090 typedef
7092 GNU Extension:
7094 decl-specifier:
7095 attributes
7097 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7099 The parser flags FLAGS is used to control type-specifier parsing.
7101 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7102 flags:
7104 1: one of the decl-specifiers is an elaborated-type-specifier
7105 (i.e., a type declaration)
7106 2: one of the decl-specifiers is an enum-specifier or a
7107 class-specifier (i.e., a type definition)
7111 static void
7112 cp_parser_decl_specifier_seq (cp_parser* parser,
7113 cp_parser_flags flags,
7114 cp_decl_specifier_seq *decl_specs,
7115 int* declares_class_or_enum)
7117 bool constructor_possible_p = !parser->in_declarator_p;
7119 /* Clear DECL_SPECS. */
7120 clear_decl_specs (decl_specs);
7122 /* Assume no class or enumeration type is declared. */
7123 *declares_class_or_enum = 0;
7125 /* Keep reading specifiers until there are no more to read. */
7126 while (true)
7128 bool constructor_p;
7129 bool found_decl_spec;
7130 cp_token *token;
7132 /* Peek at the next token. */
7133 token = cp_lexer_peek_token (parser->lexer);
7134 /* Handle attributes. */
7135 if (token->keyword == RID_ATTRIBUTE)
7137 /* Parse the attributes. */
7138 decl_specs->attributes
7139 = chainon (decl_specs->attributes,
7140 cp_parser_attributes_opt (parser));
7141 continue;
7143 /* Assume we will find a decl-specifier keyword. */
7144 found_decl_spec = true;
7145 /* If the next token is an appropriate keyword, we can simply
7146 add it to the list. */
7147 switch (token->keyword)
7149 /* decl-specifier:
7150 friend */
7151 case RID_FRIEND:
7152 if (decl_specs->specs[(int) ds_friend]++)
7153 error ("duplicate %<friend%>");
7154 /* Consume the token. */
7155 cp_lexer_consume_token (parser->lexer);
7156 break;
7158 /* function-specifier:
7159 inline
7160 virtual
7161 explicit */
7162 case RID_INLINE:
7163 case RID_VIRTUAL:
7164 case RID_EXPLICIT:
7165 cp_parser_function_specifier_opt (parser, decl_specs);
7166 break;
7168 /* decl-specifier:
7169 typedef */
7170 case RID_TYPEDEF:
7171 ++decl_specs->specs[(int) ds_typedef];
7172 /* Consume the token. */
7173 cp_lexer_consume_token (parser->lexer);
7174 /* A constructor declarator cannot appear in a typedef. */
7175 constructor_possible_p = false;
7176 /* The "typedef" keyword can only occur in a declaration; we
7177 may as well commit at this point. */
7178 cp_parser_commit_to_tentative_parse (parser);
7179 break;
7181 /* storage-class-specifier:
7182 auto
7183 register
7184 static
7185 extern
7186 mutable
7188 GNU Extension:
7189 thread */
7190 case RID_AUTO:
7191 /* Consume the token. */
7192 cp_lexer_consume_token (parser->lexer);
7193 cp_parser_set_storage_class (decl_specs, sc_auto);
7194 break;
7195 case RID_REGISTER:
7196 /* Consume the token. */
7197 cp_lexer_consume_token (parser->lexer);
7198 cp_parser_set_storage_class (decl_specs, sc_register);
7199 break;
7200 case RID_STATIC:
7201 /* Consume the token. */
7202 cp_lexer_consume_token (parser->lexer);
7203 if (decl_specs->specs[(int) ds_thread])
7205 error ("%<__thread%> before %<static%>");
7206 decl_specs->specs[(int) ds_thread] = 0;
7208 cp_parser_set_storage_class (decl_specs, sc_static);
7209 break;
7210 case RID_EXTERN:
7211 /* Consume the token. */
7212 cp_lexer_consume_token (parser->lexer);
7213 if (decl_specs->specs[(int) ds_thread])
7215 error ("%<__thread%> before %<extern%>");
7216 decl_specs->specs[(int) ds_thread] = 0;
7218 cp_parser_set_storage_class (decl_specs, sc_extern);
7219 break;
7220 case RID_MUTABLE:
7221 /* Consume the token. */
7222 cp_lexer_consume_token (parser->lexer);
7223 cp_parser_set_storage_class (decl_specs, sc_mutable);
7224 break;
7225 case RID_THREAD:
7226 /* Consume the token. */
7227 cp_lexer_consume_token (parser->lexer);
7228 ++decl_specs->specs[(int) ds_thread];
7229 break;
7231 default:
7232 /* We did not yet find a decl-specifier yet. */
7233 found_decl_spec = false;
7234 break;
7237 /* Constructors are a special case. The `S' in `S()' is not a
7238 decl-specifier; it is the beginning of the declarator. */
7239 constructor_p
7240 = (!found_decl_spec
7241 && constructor_possible_p
7242 && (cp_parser_constructor_declarator_p
7243 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7245 /* If we don't have a DECL_SPEC yet, then we must be looking at
7246 a type-specifier. */
7247 if (!found_decl_spec && !constructor_p)
7249 int decl_spec_declares_class_or_enum;
7250 bool is_cv_qualifier;
7251 tree type_spec;
7253 type_spec
7254 = cp_parser_type_specifier (parser, flags,
7255 decl_specs,
7256 /*is_declaration=*/true,
7257 &decl_spec_declares_class_or_enum,
7258 &is_cv_qualifier);
7260 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7262 /* If this type-specifier referenced a user-defined type
7263 (a typedef, class-name, etc.), then we can't allow any
7264 more such type-specifiers henceforth.
7266 [dcl.spec]
7268 The longest sequence of decl-specifiers that could
7269 possibly be a type name is taken as the
7270 decl-specifier-seq of a declaration. The sequence shall
7271 be self-consistent as described below.
7273 [dcl.type]
7275 As a general rule, at most one type-specifier is allowed
7276 in the complete decl-specifier-seq of a declaration. The
7277 only exceptions are the following:
7279 -- const or volatile can be combined with any other
7280 type-specifier.
7282 -- signed or unsigned can be combined with char, long,
7283 short, or int.
7285 -- ..
7287 Example:
7289 typedef char* Pc;
7290 void g (const int Pc);
7292 Here, Pc is *not* part of the decl-specifier seq; it's
7293 the declarator. Therefore, once we see a type-specifier
7294 (other than a cv-qualifier), we forbid any additional
7295 user-defined types. We *do* still allow things like `int
7296 int' to be considered a decl-specifier-seq, and issue the
7297 error message later. */
7298 if (type_spec && !is_cv_qualifier)
7299 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7300 /* A constructor declarator cannot follow a type-specifier. */
7301 if (type_spec)
7303 constructor_possible_p = false;
7304 found_decl_spec = true;
7308 /* If we still do not have a DECL_SPEC, then there are no more
7309 decl-specifiers. */
7310 if (!found_decl_spec)
7311 break;
7313 decl_specs->any_specifiers_p = true;
7314 /* After we see one decl-specifier, further decl-specifiers are
7315 always optional. */
7316 flags |= CP_PARSER_FLAGS_OPTIONAL;
7319 /* Don't allow a friend specifier with a class definition. */
7320 if (decl_specs->specs[(int) ds_friend] != 0
7321 && (*declares_class_or_enum & 2))
7322 error ("class definition may not be declared a friend");
7325 /* Parse an (optional) storage-class-specifier.
7327 storage-class-specifier:
7328 auto
7329 register
7330 static
7331 extern
7332 mutable
7334 GNU Extension:
7336 storage-class-specifier:
7337 thread
7339 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7341 static tree
7342 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7344 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7346 case RID_AUTO:
7347 case RID_REGISTER:
7348 case RID_STATIC:
7349 case RID_EXTERN:
7350 case RID_MUTABLE:
7351 case RID_THREAD:
7352 /* Consume the token. */
7353 return cp_lexer_consume_token (parser->lexer)->value;
7355 default:
7356 return NULL_TREE;
7360 /* Parse an (optional) function-specifier.
7362 function-specifier:
7363 inline
7364 virtual
7365 explicit
7367 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7368 Updates DECL_SPECS, if it is non-NULL. */
7370 static tree
7371 cp_parser_function_specifier_opt (cp_parser* parser,
7372 cp_decl_specifier_seq *decl_specs)
7374 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7376 case RID_INLINE:
7377 if (decl_specs)
7378 ++decl_specs->specs[(int) ds_inline];
7379 break;
7381 case RID_VIRTUAL:
7382 if (decl_specs)
7383 ++decl_specs->specs[(int) ds_virtual];
7384 break;
7386 case RID_EXPLICIT:
7387 if (decl_specs)
7388 ++decl_specs->specs[(int) ds_explicit];
7389 break;
7391 default:
7392 return NULL_TREE;
7395 /* Consume the token. */
7396 return cp_lexer_consume_token (parser->lexer)->value;
7399 /* Parse a linkage-specification.
7401 linkage-specification:
7402 extern string-literal { declaration-seq [opt] }
7403 extern string-literal declaration */
7405 static void
7406 cp_parser_linkage_specification (cp_parser* parser)
7408 tree linkage;
7410 /* Look for the `extern' keyword. */
7411 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7413 /* Look for the string-literal. */
7414 linkage = cp_parser_string_literal (parser, false, false);
7416 /* Transform the literal into an identifier. If the literal is a
7417 wide-character string, or contains embedded NULs, then we can't
7418 handle it as the user wants. */
7419 if (strlen (TREE_STRING_POINTER (linkage))
7420 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7422 cp_parser_error (parser, "invalid linkage-specification");
7423 /* Assume C++ linkage. */
7424 linkage = lang_name_cplusplus;
7426 else
7427 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7429 /* We're now using the new linkage. */
7430 push_lang_context (linkage);
7432 /* If the next token is a `{', then we're using the first
7433 production. */
7434 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7436 /* Consume the `{' token. */
7437 cp_lexer_consume_token (parser->lexer);
7438 /* Parse the declarations. */
7439 cp_parser_declaration_seq_opt (parser);
7440 /* Look for the closing `}'. */
7441 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7443 /* Otherwise, there's just one declaration. */
7444 else
7446 bool saved_in_unbraced_linkage_specification_p;
7448 saved_in_unbraced_linkage_specification_p
7449 = parser->in_unbraced_linkage_specification_p;
7450 parser->in_unbraced_linkage_specification_p = true;
7451 have_extern_spec = true;
7452 cp_parser_declaration (parser);
7453 have_extern_spec = false;
7454 parser->in_unbraced_linkage_specification_p
7455 = saved_in_unbraced_linkage_specification_p;
7458 /* We're done with the linkage-specification. */
7459 pop_lang_context ();
7462 /* Special member functions [gram.special] */
7464 /* Parse a conversion-function-id.
7466 conversion-function-id:
7467 operator conversion-type-id
7469 Returns an IDENTIFIER_NODE representing the operator. */
7471 static tree
7472 cp_parser_conversion_function_id (cp_parser* parser)
7474 tree type;
7475 tree saved_scope;
7476 tree saved_qualifying_scope;
7477 tree saved_object_scope;
7478 tree pushed_scope = NULL_TREE;
7480 /* Look for the `operator' token. */
7481 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7482 return error_mark_node;
7483 /* When we parse the conversion-type-id, the current scope will be
7484 reset. However, we need that information in able to look up the
7485 conversion function later, so we save it here. */
7486 saved_scope = parser->scope;
7487 saved_qualifying_scope = parser->qualifying_scope;
7488 saved_object_scope = parser->object_scope;
7489 /* We must enter the scope of the class so that the names of
7490 entities declared within the class are available in the
7491 conversion-type-id. For example, consider:
7493 struct S {
7494 typedef int I;
7495 operator I();
7498 S::operator I() { ... }
7500 In order to see that `I' is a type-name in the definition, we
7501 must be in the scope of `S'. */
7502 if (saved_scope)
7503 pushed_scope = push_scope (saved_scope);
7504 /* Parse the conversion-type-id. */
7505 type = cp_parser_conversion_type_id (parser);
7506 /* Leave the scope of the class, if any. */
7507 if (pushed_scope)
7508 pop_scope (pushed_scope);
7509 /* Restore the saved scope. */
7510 parser->scope = saved_scope;
7511 parser->qualifying_scope = saved_qualifying_scope;
7512 parser->object_scope = saved_object_scope;
7513 /* If the TYPE is invalid, indicate failure. */
7514 if (type == error_mark_node)
7515 return error_mark_node;
7516 return mangle_conv_op_name_for_type (type);
7519 /* Parse a conversion-type-id:
7521 conversion-type-id:
7522 type-specifier-seq conversion-declarator [opt]
7524 Returns the TYPE specified. */
7526 static tree
7527 cp_parser_conversion_type_id (cp_parser* parser)
7529 tree attributes;
7530 cp_decl_specifier_seq type_specifiers;
7531 cp_declarator *declarator;
7532 tree type_specified;
7534 /* Parse the attributes. */
7535 attributes = cp_parser_attributes_opt (parser);
7536 /* Parse the type-specifiers. */
7537 cp_parser_type_specifier_seq (parser, &type_specifiers);
7538 /* If that didn't work, stop. */
7539 if (type_specifiers.type == error_mark_node)
7540 return error_mark_node;
7541 /* Parse the conversion-declarator. */
7542 declarator = cp_parser_conversion_declarator_opt (parser);
7544 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7545 /*initialized=*/0, &attributes);
7546 if (attributes)
7547 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7548 return type_specified;
7551 /* Parse an (optional) conversion-declarator.
7553 conversion-declarator:
7554 ptr-operator conversion-declarator [opt]
7558 static cp_declarator *
7559 cp_parser_conversion_declarator_opt (cp_parser* parser)
7561 enum tree_code code;
7562 tree class_type;
7563 cp_cv_quals cv_quals;
7565 /* We don't know if there's a ptr-operator next, or not. */
7566 cp_parser_parse_tentatively (parser);
7567 /* Try the ptr-operator. */
7568 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7569 /* If it worked, look for more conversion-declarators. */
7570 if (cp_parser_parse_definitely (parser))
7572 cp_declarator *declarator;
7574 /* Parse another optional declarator. */
7575 declarator = cp_parser_conversion_declarator_opt (parser);
7577 /* Create the representation of the declarator. */
7578 if (class_type)
7579 declarator = make_ptrmem_declarator (cv_quals, class_type,
7580 declarator);
7581 else if (code == INDIRECT_REF)
7582 declarator = make_pointer_declarator (cv_quals, declarator);
7583 else
7584 declarator = make_reference_declarator (cv_quals, declarator);
7586 return declarator;
7589 return NULL;
7592 /* Parse an (optional) ctor-initializer.
7594 ctor-initializer:
7595 : mem-initializer-list
7597 Returns TRUE iff the ctor-initializer was actually present. */
7599 static bool
7600 cp_parser_ctor_initializer_opt (cp_parser* parser)
7602 /* If the next token is not a `:', then there is no
7603 ctor-initializer. */
7604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7606 /* Do default initialization of any bases and members. */
7607 if (DECL_CONSTRUCTOR_P (current_function_decl))
7608 finish_mem_initializers (NULL_TREE);
7610 return false;
7613 /* Consume the `:' token. */
7614 cp_lexer_consume_token (parser->lexer);
7615 /* And the mem-initializer-list. */
7616 cp_parser_mem_initializer_list (parser);
7618 return true;
7621 /* Parse a mem-initializer-list.
7623 mem-initializer-list:
7624 mem-initializer
7625 mem-initializer , mem-initializer-list */
7627 static void
7628 cp_parser_mem_initializer_list (cp_parser* parser)
7630 tree mem_initializer_list = NULL_TREE;
7632 /* Let the semantic analysis code know that we are starting the
7633 mem-initializer-list. */
7634 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7635 error ("only constructors take base initializers");
7637 /* Loop through the list. */
7638 while (true)
7640 tree mem_initializer;
7642 /* Parse the mem-initializer. */
7643 mem_initializer = cp_parser_mem_initializer (parser);
7644 /* Add it to the list, unless it was erroneous. */
7645 if (mem_initializer)
7647 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7648 mem_initializer_list = mem_initializer;
7650 /* If the next token is not a `,', we're done. */
7651 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7652 break;
7653 /* Consume the `,' token. */
7654 cp_lexer_consume_token (parser->lexer);
7657 /* Perform semantic analysis. */
7658 if (DECL_CONSTRUCTOR_P (current_function_decl))
7659 finish_mem_initializers (mem_initializer_list);
7662 /* Parse a mem-initializer.
7664 mem-initializer:
7665 mem-initializer-id ( expression-list [opt] )
7667 GNU extension:
7669 mem-initializer:
7670 ( expression-list [opt] )
7672 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7673 class) or FIELD_DECL (for a non-static data member) to initialize;
7674 the TREE_VALUE is the expression-list. */
7676 static tree
7677 cp_parser_mem_initializer (cp_parser* parser)
7679 tree mem_initializer_id;
7680 tree expression_list;
7681 tree member;
7683 /* Find out what is being initialized. */
7684 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7686 pedwarn ("anachronistic old-style base class initializer");
7687 mem_initializer_id = NULL_TREE;
7689 else
7690 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7691 member = expand_member_init (mem_initializer_id);
7692 if (member && !DECL_P (member))
7693 in_base_initializer = 1;
7695 expression_list
7696 = cp_parser_parenthesized_expression_list (parser, false,
7697 /*cast_p=*/false,
7698 /*non_constant_p=*/NULL);
7699 if (!expression_list)
7700 expression_list = void_type_node;
7702 in_base_initializer = 0;
7704 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7707 /* Parse a mem-initializer-id.
7709 mem-initializer-id:
7710 :: [opt] nested-name-specifier [opt] class-name
7711 identifier
7713 Returns a TYPE indicating the class to be initializer for the first
7714 production. Returns an IDENTIFIER_NODE indicating the data member
7715 to be initialized for the second production. */
7717 static tree
7718 cp_parser_mem_initializer_id (cp_parser* parser)
7720 bool global_scope_p;
7721 bool nested_name_specifier_p;
7722 bool template_p = false;
7723 tree id;
7725 /* `typename' is not allowed in this context ([temp.res]). */
7726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7728 error ("keyword %<typename%> not allowed in this context (a qualified "
7729 "member initializer is implicitly a type)");
7730 cp_lexer_consume_token (parser->lexer);
7732 /* Look for the optional `::' operator. */
7733 global_scope_p
7734 = (cp_parser_global_scope_opt (parser,
7735 /*current_scope_valid_p=*/false)
7736 != NULL_TREE);
7737 /* Look for the optional nested-name-specifier. The simplest way to
7738 implement:
7740 [temp.res]
7742 The keyword `typename' is not permitted in a base-specifier or
7743 mem-initializer; in these contexts a qualified name that
7744 depends on a template-parameter is implicitly assumed to be a
7745 type name.
7747 is to assume that we have seen the `typename' keyword at this
7748 point. */
7749 nested_name_specifier_p
7750 = (cp_parser_nested_name_specifier_opt (parser,
7751 /*typename_keyword_p=*/true,
7752 /*check_dependency_p=*/true,
7753 /*type_p=*/true,
7754 /*is_declaration=*/true)
7755 != NULL_TREE);
7756 if (nested_name_specifier_p)
7757 template_p = cp_parser_optional_template_keyword (parser);
7758 /* If there is a `::' operator or a nested-name-specifier, then we
7759 are definitely looking for a class-name. */
7760 if (global_scope_p || nested_name_specifier_p)
7761 return cp_parser_class_name (parser,
7762 /*typename_keyword_p=*/true,
7763 /*template_keyword_p=*/template_p,
7764 none_type,
7765 /*check_dependency_p=*/true,
7766 /*class_head_p=*/false,
7767 /*is_declaration=*/true);
7768 /* Otherwise, we could also be looking for an ordinary identifier. */
7769 cp_parser_parse_tentatively (parser);
7770 /* Try a class-name. */
7771 id = cp_parser_class_name (parser,
7772 /*typename_keyword_p=*/true,
7773 /*template_keyword_p=*/false,
7774 none_type,
7775 /*check_dependency_p=*/true,
7776 /*class_head_p=*/false,
7777 /*is_declaration=*/true);
7778 /* If we found one, we're done. */
7779 if (cp_parser_parse_definitely (parser))
7780 return id;
7781 /* Otherwise, look for an ordinary identifier. */
7782 return cp_parser_identifier (parser);
7785 /* Overloading [gram.over] */
7787 /* Parse an operator-function-id.
7789 operator-function-id:
7790 operator operator
7792 Returns an IDENTIFIER_NODE for the operator which is a
7793 human-readable spelling of the identifier, e.g., `operator +'. */
7795 static tree
7796 cp_parser_operator_function_id (cp_parser* parser)
7798 /* Look for the `operator' keyword. */
7799 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7800 return error_mark_node;
7801 /* And then the name of the operator itself. */
7802 return cp_parser_operator (parser);
7805 /* Parse an operator.
7807 operator:
7808 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7809 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7810 || ++ -- , ->* -> () []
7812 GNU Extensions:
7814 operator:
7815 <? >? <?= >?=
7817 Returns an IDENTIFIER_NODE for the operator which is a
7818 human-readable spelling of the identifier, e.g., `operator +'. */
7820 static tree
7821 cp_parser_operator (cp_parser* parser)
7823 tree id = NULL_TREE;
7824 cp_token *token;
7826 /* Peek at the next token. */
7827 token = cp_lexer_peek_token (parser->lexer);
7828 /* Figure out which operator we have. */
7829 switch (token->type)
7831 case CPP_KEYWORD:
7833 enum tree_code op;
7835 /* The keyword should be either `new' or `delete'. */
7836 if (token->keyword == RID_NEW)
7837 op = NEW_EXPR;
7838 else if (token->keyword == RID_DELETE)
7839 op = DELETE_EXPR;
7840 else
7841 break;
7843 /* Consume the `new' or `delete' token. */
7844 cp_lexer_consume_token (parser->lexer);
7846 /* Peek at the next token. */
7847 token = cp_lexer_peek_token (parser->lexer);
7848 /* If it's a `[' token then this is the array variant of the
7849 operator. */
7850 if (token->type == CPP_OPEN_SQUARE)
7852 /* Consume the `[' token. */
7853 cp_lexer_consume_token (parser->lexer);
7854 /* Look for the `]' token. */
7855 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7856 id = ansi_opname (op == NEW_EXPR
7857 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7859 /* Otherwise, we have the non-array variant. */
7860 else
7861 id = ansi_opname (op);
7863 return id;
7866 case CPP_PLUS:
7867 id = ansi_opname (PLUS_EXPR);
7868 break;
7870 case CPP_MINUS:
7871 id = ansi_opname (MINUS_EXPR);
7872 break;
7874 case CPP_MULT:
7875 id = ansi_opname (MULT_EXPR);
7876 break;
7878 case CPP_DIV:
7879 id = ansi_opname (TRUNC_DIV_EXPR);
7880 break;
7882 case CPP_MOD:
7883 id = ansi_opname (TRUNC_MOD_EXPR);
7884 break;
7886 case CPP_XOR:
7887 id = ansi_opname (BIT_XOR_EXPR);
7888 break;
7890 case CPP_AND:
7891 id = ansi_opname (BIT_AND_EXPR);
7892 break;
7894 case CPP_OR:
7895 id = ansi_opname (BIT_IOR_EXPR);
7896 break;
7898 case CPP_COMPL:
7899 id = ansi_opname (BIT_NOT_EXPR);
7900 break;
7902 case CPP_NOT:
7903 id = ansi_opname (TRUTH_NOT_EXPR);
7904 break;
7906 case CPP_EQ:
7907 id = ansi_assopname (NOP_EXPR);
7908 break;
7910 case CPP_LESS:
7911 id = ansi_opname (LT_EXPR);
7912 break;
7914 case CPP_GREATER:
7915 id = ansi_opname (GT_EXPR);
7916 break;
7918 case CPP_PLUS_EQ:
7919 id = ansi_assopname (PLUS_EXPR);
7920 break;
7922 case CPP_MINUS_EQ:
7923 id = ansi_assopname (MINUS_EXPR);
7924 break;
7926 case CPP_MULT_EQ:
7927 id = ansi_assopname (MULT_EXPR);
7928 break;
7930 case CPP_DIV_EQ:
7931 id = ansi_assopname (TRUNC_DIV_EXPR);
7932 break;
7934 case CPP_MOD_EQ:
7935 id = ansi_assopname (TRUNC_MOD_EXPR);
7936 break;
7938 case CPP_XOR_EQ:
7939 id = ansi_assopname (BIT_XOR_EXPR);
7940 break;
7942 case CPP_AND_EQ:
7943 id = ansi_assopname (BIT_AND_EXPR);
7944 break;
7946 case CPP_OR_EQ:
7947 id = ansi_assopname (BIT_IOR_EXPR);
7948 break;
7950 case CPP_LSHIFT:
7951 id = ansi_opname (LSHIFT_EXPR);
7952 break;
7954 case CPP_RSHIFT:
7955 id = ansi_opname (RSHIFT_EXPR);
7956 break;
7958 case CPP_LSHIFT_EQ:
7959 id = ansi_assopname (LSHIFT_EXPR);
7960 break;
7962 case CPP_RSHIFT_EQ:
7963 id = ansi_assopname (RSHIFT_EXPR);
7964 break;
7966 case CPP_EQ_EQ:
7967 id = ansi_opname (EQ_EXPR);
7968 break;
7970 case CPP_NOT_EQ:
7971 id = ansi_opname (NE_EXPR);
7972 break;
7974 case CPP_LESS_EQ:
7975 id = ansi_opname (LE_EXPR);
7976 break;
7978 case CPP_GREATER_EQ:
7979 id = ansi_opname (GE_EXPR);
7980 break;
7982 case CPP_AND_AND:
7983 id = ansi_opname (TRUTH_ANDIF_EXPR);
7984 break;
7986 case CPP_OR_OR:
7987 id = ansi_opname (TRUTH_ORIF_EXPR);
7988 break;
7990 case CPP_PLUS_PLUS:
7991 id = ansi_opname (POSTINCREMENT_EXPR);
7992 break;
7994 case CPP_MINUS_MINUS:
7995 id = ansi_opname (PREDECREMENT_EXPR);
7996 break;
7998 case CPP_COMMA:
7999 id = ansi_opname (COMPOUND_EXPR);
8000 break;
8002 case CPP_DEREF_STAR:
8003 id = ansi_opname (MEMBER_REF);
8004 break;
8006 case CPP_DEREF:
8007 id = ansi_opname (COMPONENT_REF);
8008 break;
8010 case CPP_OPEN_PAREN:
8011 /* Consume the `('. */
8012 cp_lexer_consume_token (parser->lexer);
8013 /* Look for the matching `)'. */
8014 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8015 return ansi_opname (CALL_EXPR);
8017 case CPP_OPEN_SQUARE:
8018 /* Consume the `['. */
8019 cp_lexer_consume_token (parser->lexer);
8020 /* Look for the matching `]'. */
8021 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8022 return ansi_opname (ARRAY_REF);
8024 /* Extensions. */
8025 case CPP_MIN:
8026 id = ansi_opname (MIN_EXPR);
8027 break;
8029 case CPP_MAX:
8030 id = ansi_opname (MAX_EXPR);
8031 break;
8033 case CPP_MIN_EQ:
8034 id = ansi_assopname (MIN_EXPR);
8035 break;
8037 case CPP_MAX_EQ:
8038 id = ansi_assopname (MAX_EXPR);
8039 break;
8041 default:
8042 /* Anything else is an error. */
8043 break;
8046 /* If we have selected an identifier, we need to consume the
8047 operator token. */
8048 if (id)
8049 cp_lexer_consume_token (parser->lexer);
8050 /* Otherwise, no valid operator name was present. */
8051 else
8053 cp_parser_error (parser, "expected operator");
8054 id = error_mark_node;
8057 return id;
8060 /* Parse a template-declaration.
8062 template-declaration:
8063 export [opt] template < template-parameter-list > declaration
8065 If MEMBER_P is TRUE, this template-declaration occurs within a
8066 class-specifier.
8068 The grammar rule given by the standard isn't correct. What
8069 is really meant is:
8071 template-declaration:
8072 export [opt] template-parameter-list-seq
8073 decl-specifier-seq [opt] init-declarator [opt] ;
8074 export [opt] template-parameter-list-seq
8075 function-definition
8077 template-parameter-list-seq:
8078 template-parameter-list-seq [opt]
8079 template < template-parameter-list > */
8081 static void
8082 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8084 /* Check for `export'. */
8085 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8087 /* Consume the `export' token. */
8088 cp_lexer_consume_token (parser->lexer);
8089 /* Warn that we do not support `export'. */
8090 warning ("keyword %<export%> not implemented, and will be ignored");
8093 cp_parser_template_declaration_after_export (parser, member_p);
8096 /* Parse a template-parameter-list.
8098 template-parameter-list:
8099 template-parameter
8100 template-parameter-list , template-parameter
8102 Returns a TREE_LIST. Each node represents a template parameter.
8103 The nodes are connected via their TREE_CHAINs. */
8105 static tree
8106 cp_parser_template_parameter_list (cp_parser* parser)
8108 tree parameter_list = NULL_TREE;
8110 while (true)
8112 tree parameter;
8113 cp_token *token;
8114 bool is_non_type;
8116 /* Parse the template-parameter. */
8117 parameter = cp_parser_template_parameter (parser, &is_non_type);
8118 /* Add it to the list. */
8119 if (parameter != error_mark_node)
8120 parameter_list = process_template_parm (parameter_list,
8121 parameter,
8122 is_non_type);
8123 /* Peek at the next token. */
8124 token = cp_lexer_peek_token (parser->lexer);
8125 /* If it's not a `,', we're done. */
8126 if (token->type != CPP_COMMA)
8127 break;
8128 /* Otherwise, consume the `,' token. */
8129 cp_lexer_consume_token (parser->lexer);
8132 return parameter_list;
8135 /* Parse a template-parameter.
8137 template-parameter:
8138 type-parameter
8139 parameter-declaration
8141 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8142 the parameter. The TREE_PURPOSE is the default value, if any.
8143 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8144 iff this parameter is a non-type parameter. */
8146 static tree
8147 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8149 cp_token *token;
8150 cp_parameter_declarator *parameter_declarator;
8151 tree parm;
8153 /* Assume it is a type parameter or a template parameter. */
8154 *is_non_type = false;
8155 /* Peek at the next token. */
8156 token = cp_lexer_peek_token (parser->lexer);
8157 /* If it is `class' or `template', we have a type-parameter. */
8158 if (token->keyword == RID_TEMPLATE)
8159 return cp_parser_type_parameter (parser);
8160 /* If it is `class' or `typename' we do not know yet whether it is a
8161 type parameter or a non-type parameter. Consider:
8163 template <typename T, typename T::X X> ...
8167 template <class C, class D*> ...
8169 Here, the first parameter is a type parameter, and the second is
8170 a non-type parameter. We can tell by looking at the token after
8171 the identifier -- if it is a `,', `=', or `>' then we have a type
8172 parameter. */
8173 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8175 /* Peek at the token after `class' or `typename'. */
8176 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8177 /* If it's an identifier, skip it. */
8178 if (token->type == CPP_NAME)
8179 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8180 /* Now, see if the token looks like the end of a template
8181 parameter. */
8182 if (token->type == CPP_COMMA
8183 || token->type == CPP_EQ
8184 || token->type == CPP_GREATER)
8185 return cp_parser_type_parameter (parser);
8188 /* Otherwise, it is a non-type parameter.
8190 [temp.param]
8192 When parsing a default template-argument for a non-type
8193 template-parameter, the first non-nested `>' is taken as the end
8194 of the template parameter-list rather than a greater-than
8195 operator. */
8196 *is_non_type = true;
8197 parameter_declarator
8198 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8199 /*parenthesized_p=*/NULL);
8200 parm = grokdeclarator (parameter_declarator->declarator,
8201 &parameter_declarator->decl_specifiers,
8202 PARM, /*initialized=*/0,
8203 /*attrlist=*/NULL);
8204 if (parm == error_mark_node)
8205 return error_mark_node;
8206 return build_tree_list (parameter_declarator->default_argument, parm);
8209 /* Parse a type-parameter.
8211 type-parameter:
8212 class identifier [opt]
8213 class identifier [opt] = type-id
8214 typename identifier [opt]
8215 typename identifier [opt] = type-id
8216 template < template-parameter-list > class identifier [opt]
8217 template < template-parameter-list > class identifier [opt]
8218 = id-expression
8220 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8221 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8222 the declaration of the parameter. */
8224 static tree
8225 cp_parser_type_parameter (cp_parser* parser)
8227 cp_token *token;
8228 tree parameter;
8230 /* Look for a keyword to tell us what kind of parameter this is. */
8231 token = cp_parser_require (parser, CPP_KEYWORD,
8232 "`class', `typename', or `template'");
8233 if (!token)
8234 return error_mark_node;
8236 switch (token->keyword)
8238 case RID_CLASS:
8239 case RID_TYPENAME:
8241 tree identifier;
8242 tree default_argument;
8244 /* If the next token is an identifier, then it names the
8245 parameter. */
8246 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8247 identifier = cp_parser_identifier (parser);
8248 else
8249 identifier = NULL_TREE;
8251 /* Create the parameter. */
8252 parameter = finish_template_type_parm (class_type_node, identifier);
8254 /* If the next token is an `=', we have a default argument. */
8255 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8257 /* Consume the `=' token. */
8258 cp_lexer_consume_token (parser->lexer);
8259 /* Parse the default-argument. */
8260 default_argument = cp_parser_type_id (parser);
8262 else
8263 default_argument = NULL_TREE;
8265 /* Create the combined representation of the parameter and the
8266 default argument. */
8267 parameter = build_tree_list (default_argument, parameter);
8269 break;
8271 case RID_TEMPLATE:
8273 tree parameter_list;
8274 tree identifier;
8275 tree default_argument;
8277 /* Look for the `<'. */
8278 cp_parser_require (parser, CPP_LESS, "`<'");
8279 /* Parse the template-parameter-list. */
8280 begin_template_parm_list ();
8281 parameter_list
8282 = cp_parser_template_parameter_list (parser);
8283 parameter_list = end_template_parm_list (parameter_list);
8284 /* Look for the `>'. */
8285 cp_parser_require (parser, CPP_GREATER, "`>'");
8286 /* Look for the `class' keyword. */
8287 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8288 /* If the next token is an `=', then there is a
8289 default-argument. If the next token is a `>', we are at
8290 the end of the parameter-list. If the next token is a `,',
8291 then we are at the end of this parameter. */
8292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8293 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8294 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8296 identifier = cp_parser_identifier (parser);
8297 /* Treat invalid names as if the parameter were nameless. */
8298 if (identifier == error_mark_node)
8299 identifier = NULL_TREE;
8301 else
8302 identifier = NULL_TREE;
8304 /* Create the template parameter. */
8305 parameter = finish_template_template_parm (class_type_node,
8306 identifier);
8308 /* If the next token is an `=', then there is a
8309 default-argument. */
8310 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8312 bool is_template;
8314 /* Consume the `='. */
8315 cp_lexer_consume_token (parser->lexer);
8316 /* Parse the id-expression. */
8317 default_argument
8318 = cp_parser_id_expression (parser,
8319 /*template_keyword_p=*/false,
8320 /*check_dependency_p=*/true,
8321 /*template_p=*/&is_template,
8322 /*declarator_p=*/false);
8323 if (TREE_CODE (default_argument) == TYPE_DECL)
8324 /* If the id-expression was a template-id that refers to
8325 a template-class, we already have the declaration here,
8326 so no further lookup is needed. */
8328 else
8329 /* Look up the name. */
8330 default_argument
8331 = cp_parser_lookup_name (parser, default_argument,
8332 none_type,
8333 /*is_template=*/is_template,
8334 /*is_namespace=*/false,
8335 /*check_dependency=*/true,
8336 /*ambiguous_p=*/NULL);
8337 /* See if the default argument is valid. */
8338 default_argument
8339 = check_template_template_default_arg (default_argument);
8341 else
8342 default_argument = NULL_TREE;
8344 /* Create the combined representation of the parameter and the
8345 default argument. */
8346 parameter = build_tree_list (default_argument, parameter);
8348 break;
8350 default:
8351 gcc_unreachable ();
8352 break;
8355 return parameter;
8358 /* Parse a template-id.
8360 template-id:
8361 template-name < template-argument-list [opt] >
8363 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8364 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8365 returned. Otherwise, if the template-name names a function, or set
8366 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8367 names a class, returns a TYPE_DECL for the specialization.
8369 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8370 uninstantiated templates. */
8372 static tree
8373 cp_parser_template_id (cp_parser *parser,
8374 bool template_keyword_p,
8375 bool check_dependency_p,
8376 bool is_declaration)
8378 tree template;
8379 tree arguments;
8380 tree template_id;
8381 cp_token_position start_of_id = 0;
8382 tree access_check = NULL_TREE;
8383 cp_token *next_token, *next_token_2;
8384 bool is_identifier;
8386 /* If the next token corresponds to a template-id, there is no need
8387 to reparse it. */
8388 next_token = cp_lexer_peek_token (parser->lexer);
8389 if (next_token->type == CPP_TEMPLATE_ID)
8391 tree value;
8392 tree check;
8394 /* Get the stored value. */
8395 value = cp_lexer_consume_token (parser->lexer)->value;
8396 /* Perform any access checks that were deferred. */
8397 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8398 perform_or_defer_access_check (TREE_PURPOSE (check),
8399 TREE_VALUE (check));
8400 /* Return the stored value. */
8401 return TREE_VALUE (value);
8404 /* Avoid performing name lookup if there is no possibility of
8405 finding a template-id. */
8406 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8407 || (next_token->type == CPP_NAME
8408 && !cp_parser_nth_token_starts_template_argument_list_p
8409 (parser, 2)))
8411 cp_parser_error (parser, "expected template-id");
8412 return error_mark_node;
8415 /* Remember where the template-id starts. */
8416 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8417 start_of_id = cp_lexer_token_position (parser->lexer, false);
8419 push_deferring_access_checks (dk_deferred);
8421 /* Parse the template-name. */
8422 is_identifier = false;
8423 template = cp_parser_template_name (parser, template_keyword_p,
8424 check_dependency_p,
8425 is_declaration,
8426 &is_identifier);
8427 if (template == error_mark_node || is_identifier)
8429 pop_deferring_access_checks ();
8430 return template;
8433 /* If we find the sequence `[:' after a template-name, it's probably
8434 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8435 parse correctly the argument list. */
8436 next_token = cp_lexer_peek_token (parser->lexer);
8437 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8438 if (next_token->type == CPP_OPEN_SQUARE
8439 && next_token->flags & DIGRAPH
8440 && next_token_2->type == CPP_COLON
8441 && !(next_token_2->flags & PREV_WHITE))
8443 cp_parser_parse_tentatively (parser);
8444 /* Change `:' into `::'. */
8445 next_token_2->type = CPP_SCOPE;
8446 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8447 CPP_LESS. */
8448 cp_lexer_consume_token (parser->lexer);
8449 /* Parse the arguments. */
8450 arguments = cp_parser_enclosed_template_argument_list (parser);
8451 if (!cp_parser_parse_definitely (parser))
8453 /* If we couldn't parse an argument list, then we revert our changes
8454 and return simply an error. Maybe this is not a template-id
8455 after all. */
8456 next_token_2->type = CPP_COLON;
8457 cp_parser_error (parser, "expected %<<%>");
8458 pop_deferring_access_checks ();
8459 return error_mark_node;
8461 /* Otherwise, emit an error about the invalid digraph, but continue
8462 parsing because we got our argument list. */
8463 pedwarn ("%<<::%> cannot begin a template-argument list");
8464 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8465 "between %<<%> and %<::%>");
8466 if (!flag_permissive)
8468 static bool hint;
8469 if (!hint)
8471 inform ("(if you use -fpermissive G++ will accept your code)");
8472 hint = true;
8476 else
8478 /* Look for the `<' that starts the template-argument-list. */
8479 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8481 pop_deferring_access_checks ();
8482 return error_mark_node;
8484 /* Parse the arguments. */
8485 arguments = cp_parser_enclosed_template_argument_list (parser);
8488 /* Build a representation of the specialization. */
8489 if (TREE_CODE (template) == IDENTIFIER_NODE)
8490 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8491 else if (DECL_CLASS_TEMPLATE_P (template)
8492 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8493 template_id
8494 = finish_template_type (template, arguments,
8495 cp_lexer_next_token_is (parser->lexer,
8496 CPP_SCOPE));
8497 else
8499 /* If it's not a class-template or a template-template, it should be
8500 a function-template. */
8501 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8502 || TREE_CODE (template) == OVERLOAD
8503 || BASELINK_P (template)));
8505 template_id = lookup_template_function (template, arguments);
8508 /* Retrieve any deferred checks. Do not pop this access checks yet
8509 so the memory will not be reclaimed during token replacing below. */
8510 access_check = get_deferred_access_checks ();
8512 /* If parsing tentatively, replace the sequence of tokens that makes
8513 up the template-id with a CPP_TEMPLATE_ID token. That way,
8514 should we re-parse the token stream, we will not have to repeat
8515 the effort required to do the parse, nor will we issue duplicate
8516 error messages about problems during instantiation of the
8517 template. */
8518 if (start_of_id)
8520 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8522 /* Reset the contents of the START_OF_ID token. */
8523 token->type = CPP_TEMPLATE_ID;
8524 token->value = build_tree_list (access_check, template_id);
8525 token->keyword = RID_MAX;
8527 /* Purge all subsequent tokens. */
8528 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8530 /* ??? Can we actually assume that, if template_id ==
8531 error_mark_node, we will have issued a diagnostic to the
8532 user, as opposed to simply marking the tentative parse as
8533 failed? */
8534 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8535 error ("parse error in template argument list");
8538 pop_deferring_access_checks ();
8539 return template_id;
8542 /* Parse a template-name.
8544 template-name:
8545 identifier
8547 The standard should actually say:
8549 template-name:
8550 identifier
8551 operator-function-id
8553 A defect report has been filed about this issue.
8555 A conversion-function-id cannot be a template name because they cannot
8556 be part of a template-id. In fact, looking at this code:
8558 a.operator K<int>()
8560 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8561 It is impossible to call a templated conversion-function-id with an
8562 explicit argument list, since the only allowed template parameter is
8563 the type to which it is converting.
8565 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8566 `template' keyword, in a construction like:
8568 T::template f<3>()
8570 In that case `f' is taken to be a template-name, even though there
8571 is no way of knowing for sure.
8573 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8574 name refers to a set of overloaded functions, at least one of which
8575 is a template, or an IDENTIFIER_NODE with the name of the template,
8576 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8577 names are looked up inside uninstantiated templates. */
8579 static tree
8580 cp_parser_template_name (cp_parser* parser,
8581 bool template_keyword_p,
8582 bool check_dependency_p,
8583 bool is_declaration,
8584 bool *is_identifier)
8586 tree identifier;
8587 tree decl;
8588 tree fns;
8590 /* If the next token is `operator', then we have either an
8591 operator-function-id or a conversion-function-id. */
8592 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8594 /* We don't know whether we're looking at an
8595 operator-function-id or a conversion-function-id. */
8596 cp_parser_parse_tentatively (parser);
8597 /* Try an operator-function-id. */
8598 identifier = cp_parser_operator_function_id (parser);
8599 /* If that didn't work, try a conversion-function-id. */
8600 if (!cp_parser_parse_definitely (parser))
8602 cp_parser_error (parser, "expected template-name");
8603 return error_mark_node;
8606 /* Look for the identifier. */
8607 else
8608 identifier = cp_parser_identifier (parser);
8610 /* If we didn't find an identifier, we don't have a template-id. */
8611 if (identifier == error_mark_node)
8612 return error_mark_node;
8614 /* If the name immediately followed the `template' keyword, then it
8615 is a template-name. However, if the next token is not `<', then
8616 we do not treat it as a template-name, since it is not being used
8617 as part of a template-id. This enables us to handle constructs
8618 like:
8620 template <typename T> struct S { S(); };
8621 template <typename T> S<T>::S();
8623 correctly. We would treat `S' as a template -- if it were `S<T>'
8624 -- but we do not if there is no `<'. */
8626 if (processing_template_decl
8627 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8629 /* In a declaration, in a dependent context, we pretend that the
8630 "template" keyword was present in order to improve error
8631 recovery. For example, given:
8633 template <typename T> void f(T::X<int>);
8635 we want to treat "X<int>" as a template-id. */
8636 if (is_declaration
8637 && !template_keyword_p
8638 && parser->scope && TYPE_P (parser->scope)
8639 && check_dependency_p
8640 && dependent_type_p (parser->scope)
8641 /* Do not do this for dtors (or ctors), since they never
8642 need the template keyword before their name. */
8643 && !constructor_name_p (identifier, parser->scope))
8645 cp_token_position start = 0;
8647 /* Explain what went wrong. */
8648 error ("non-template %qD used as template", identifier);
8649 inform ("use %<%T::template %D%> to indicate that it is a template",
8650 parser->scope, identifier);
8651 /* If parsing tentatively, find the location of the "<" token. */
8652 if (cp_parser_simulate_error (parser))
8653 start = cp_lexer_token_position (parser->lexer, true);
8654 /* Parse the template arguments so that we can issue error
8655 messages about them. */
8656 cp_lexer_consume_token (parser->lexer);
8657 cp_parser_enclosed_template_argument_list (parser);
8658 /* Skip tokens until we find a good place from which to
8659 continue parsing. */
8660 cp_parser_skip_to_closing_parenthesis (parser,
8661 /*recovering=*/true,
8662 /*or_comma=*/true,
8663 /*consume_paren=*/false);
8664 /* If parsing tentatively, permanently remove the
8665 template argument list. That will prevent duplicate
8666 error messages from being issued about the missing
8667 "template" keyword. */
8668 if (start)
8669 cp_lexer_purge_tokens_after (parser->lexer, start);
8670 if (is_identifier)
8671 *is_identifier = true;
8672 return identifier;
8675 /* If the "template" keyword is present, then there is generally
8676 no point in doing name-lookup, so we just return IDENTIFIER.
8677 But, if the qualifying scope is non-dependent then we can
8678 (and must) do name-lookup normally. */
8679 if (template_keyword_p
8680 && (!parser->scope
8681 || (TYPE_P (parser->scope)
8682 && dependent_type_p (parser->scope))))
8683 return identifier;
8686 /* Look up the name. */
8687 decl = cp_parser_lookup_name (parser, identifier,
8688 none_type,
8689 /*is_template=*/false,
8690 /*is_namespace=*/false,
8691 check_dependency_p,
8692 /*ambiguous_p=*/NULL);
8693 decl = maybe_get_template_decl_from_type_decl (decl);
8695 /* If DECL is a template, then the name was a template-name. */
8696 if (TREE_CODE (decl) == TEMPLATE_DECL)
8698 else
8700 /* The standard does not explicitly indicate whether a name that
8701 names a set of overloaded declarations, some of which are
8702 templates, is a template-name. However, such a name should
8703 be a template-name; otherwise, there is no way to form a
8704 template-id for the overloaded templates. */
8705 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8706 if (TREE_CODE (fns) == OVERLOAD)
8708 tree fn;
8710 for (fn = fns; fn; fn = OVL_NEXT (fn))
8711 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8712 break;
8714 else
8716 /* Otherwise, the name does not name a template. */
8717 cp_parser_error (parser, "expected template-name");
8718 return error_mark_node;
8722 /* If DECL is dependent, and refers to a function, then just return
8723 its name; we will look it up again during template instantiation. */
8724 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8726 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8727 if (TYPE_P (scope) && dependent_type_p (scope))
8728 return identifier;
8731 return decl;
8734 /* Parse a template-argument-list.
8736 template-argument-list:
8737 template-argument
8738 template-argument-list , template-argument
8740 Returns a TREE_VEC containing the arguments. */
8742 static tree
8743 cp_parser_template_argument_list (cp_parser* parser)
8745 tree fixed_args[10];
8746 unsigned n_args = 0;
8747 unsigned alloced = 10;
8748 tree *arg_ary = fixed_args;
8749 tree vec;
8750 bool saved_in_template_argument_list_p;
8752 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8753 parser->in_template_argument_list_p = true;
8756 tree argument;
8758 if (n_args)
8759 /* Consume the comma. */
8760 cp_lexer_consume_token (parser->lexer);
8762 /* Parse the template-argument. */
8763 argument = cp_parser_template_argument (parser);
8764 if (n_args == alloced)
8766 alloced *= 2;
8768 if (arg_ary == fixed_args)
8770 arg_ary = xmalloc (sizeof (tree) * alloced);
8771 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8773 else
8774 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8776 arg_ary[n_args++] = argument;
8778 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8780 vec = make_tree_vec (n_args);
8782 while (n_args--)
8783 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8785 if (arg_ary != fixed_args)
8786 free (arg_ary);
8787 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8788 return vec;
8791 /* Parse a template-argument.
8793 template-argument:
8794 assignment-expression
8795 type-id
8796 id-expression
8798 The representation is that of an assignment-expression, type-id, or
8799 id-expression -- except that the qualified id-expression is
8800 evaluated, so that the value returned is either a DECL or an
8801 OVERLOAD.
8803 Although the standard says "assignment-expression", it forbids
8804 throw-expressions or assignments in the template argument.
8805 Therefore, we use "conditional-expression" instead. */
8807 static tree
8808 cp_parser_template_argument (cp_parser* parser)
8810 tree argument;
8811 bool template_p;
8812 bool address_p;
8813 bool maybe_type_id = false;
8814 cp_token *token;
8815 cp_id_kind idk;
8816 tree qualifying_class;
8818 /* There's really no way to know what we're looking at, so we just
8819 try each alternative in order.
8821 [temp.arg]
8823 In a template-argument, an ambiguity between a type-id and an
8824 expression is resolved to a type-id, regardless of the form of
8825 the corresponding template-parameter.
8827 Therefore, we try a type-id first. */
8828 cp_parser_parse_tentatively (parser);
8829 argument = cp_parser_type_id (parser);
8830 /* If there was no error parsing the type-id but the next token is a '>>',
8831 we probably found a typo for '> >'. But there are type-id which are
8832 also valid expressions. For instance:
8834 struct X { int operator >> (int); };
8835 template <int V> struct Foo {};
8836 Foo<X () >> 5> r;
8838 Here 'X()' is a valid type-id of a function type, but the user just
8839 wanted to write the expression "X() >> 5". Thus, we remember that we
8840 found a valid type-id, but we still try to parse the argument as an
8841 expression to see what happens. */
8842 if (!cp_parser_error_occurred (parser)
8843 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8845 maybe_type_id = true;
8846 cp_parser_abort_tentative_parse (parser);
8848 else
8850 /* If the next token isn't a `,' or a `>', then this argument wasn't
8851 really finished. This means that the argument is not a valid
8852 type-id. */
8853 if (!cp_parser_next_token_ends_template_argument_p (parser))
8854 cp_parser_error (parser, "expected template-argument");
8855 /* If that worked, we're done. */
8856 if (cp_parser_parse_definitely (parser))
8857 return argument;
8859 /* We're still not sure what the argument will be. */
8860 cp_parser_parse_tentatively (parser);
8861 /* Try a template. */
8862 argument = cp_parser_id_expression (parser,
8863 /*template_keyword_p=*/false,
8864 /*check_dependency_p=*/true,
8865 &template_p,
8866 /*declarator_p=*/false);
8867 /* If the next token isn't a `,' or a `>', then this argument wasn't
8868 really finished. */
8869 if (!cp_parser_next_token_ends_template_argument_p (parser))
8870 cp_parser_error (parser, "expected template-argument");
8871 if (!cp_parser_error_occurred (parser))
8873 /* Figure out what is being referred to. If the id-expression
8874 was for a class template specialization, then we will have a
8875 TYPE_DECL at this point. There is no need to do name lookup
8876 at this point in that case. */
8877 if (TREE_CODE (argument) != TYPE_DECL)
8878 argument = cp_parser_lookup_name (parser, argument,
8879 none_type,
8880 /*is_template=*/template_p,
8881 /*is_namespace=*/false,
8882 /*check_dependency=*/true,
8883 /*ambiguous_p=*/NULL);
8884 if (TREE_CODE (argument) != TEMPLATE_DECL
8885 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8886 cp_parser_error (parser, "expected template-name");
8888 if (cp_parser_parse_definitely (parser))
8889 return argument;
8890 /* It must be a non-type argument. There permitted cases are given
8891 in [temp.arg.nontype]:
8893 -- an integral constant-expression of integral or enumeration
8894 type; or
8896 -- the name of a non-type template-parameter; or
8898 -- the name of an object or function with external linkage...
8900 -- the address of an object or function with external linkage...
8902 -- a pointer to member... */
8903 /* Look for a non-type template parameter. */
8904 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8906 cp_parser_parse_tentatively (parser);
8907 argument = cp_parser_primary_expression (parser,
8908 /*cast_p=*/false,
8909 &idk,
8910 &qualifying_class);
8911 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8912 || !cp_parser_next_token_ends_template_argument_p (parser))
8913 cp_parser_simulate_error (parser);
8914 if (cp_parser_parse_definitely (parser))
8915 return argument;
8918 /* If the next token is "&", the argument must be the address of an
8919 object or function with external linkage. */
8920 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8921 if (address_p)
8922 cp_lexer_consume_token (parser->lexer);
8923 /* See if we might have an id-expression. */
8924 token = cp_lexer_peek_token (parser->lexer);
8925 if (token->type == CPP_NAME
8926 || token->keyword == RID_OPERATOR
8927 || token->type == CPP_SCOPE
8928 || token->type == CPP_TEMPLATE_ID
8929 || token->type == CPP_NESTED_NAME_SPECIFIER)
8931 cp_parser_parse_tentatively (parser);
8932 argument = cp_parser_primary_expression (parser,
8933 /*cast_p=*/false,
8934 &idk,
8935 &qualifying_class);
8936 if (cp_parser_error_occurred (parser)
8937 || !cp_parser_next_token_ends_template_argument_p (parser))
8938 cp_parser_abort_tentative_parse (parser);
8939 else
8941 if (TREE_CODE (argument) == INDIRECT_REF)
8943 gcc_assert (REFERENCE_REF_P (argument));
8944 argument = TREE_OPERAND (argument, 0);
8947 if (qualifying_class)
8948 argument = finish_qualified_id_expr (qualifying_class,
8949 argument,
8950 /*done=*/true,
8951 address_p);
8952 if (TREE_CODE (argument) == VAR_DECL)
8954 /* A variable without external linkage might still be a
8955 valid constant-expression, so no error is issued here
8956 if the external-linkage check fails. */
8957 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8958 cp_parser_simulate_error (parser);
8960 else if (is_overloaded_fn (argument))
8961 /* All overloaded functions are allowed; if the external
8962 linkage test does not pass, an error will be issued
8963 later. */
8965 else if (address_p
8966 && (TREE_CODE (argument) == OFFSET_REF
8967 || TREE_CODE (argument) == SCOPE_REF))
8968 /* A pointer-to-member. */
8970 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
8972 else
8973 cp_parser_simulate_error (parser);
8975 if (cp_parser_parse_definitely (parser))
8977 if (address_p)
8978 argument = build_x_unary_op (ADDR_EXPR, argument);
8979 return argument;
8983 /* If the argument started with "&", there are no other valid
8984 alternatives at this point. */
8985 if (address_p)
8987 cp_parser_error (parser, "invalid non-type template argument");
8988 return error_mark_node;
8991 /* If the argument wasn't successfully parsed as a type-id followed
8992 by '>>', the argument can only be a constant expression now.
8993 Otherwise, we try parsing the constant-expression tentatively,
8994 because the argument could really be a type-id. */
8995 if (maybe_type_id)
8996 cp_parser_parse_tentatively (parser);
8997 argument = cp_parser_constant_expression (parser,
8998 /*allow_non_constant_p=*/false,
8999 /*non_constant_p=*/NULL);
9000 argument = fold_non_dependent_expr (argument);
9001 if (!maybe_type_id)
9002 return argument;
9003 if (!cp_parser_next_token_ends_template_argument_p (parser))
9004 cp_parser_error (parser, "expected template-argument");
9005 if (cp_parser_parse_definitely (parser))
9006 return argument;
9007 /* We did our best to parse the argument as a non type-id, but that
9008 was the only alternative that matched (albeit with a '>' after
9009 it). We can assume it's just a typo from the user, and a
9010 diagnostic will then be issued. */
9011 return cp_parser_type_id (parser);
9014 /* Parse an explicit-instantiation.
9016 explicit-instantiation:
9017 template declaration
9019 Although the standard says `declaration', what it really means is:
9021 explicit-instantiation:
9022 template decl-specifier-seq [opt] declarator [opt] ;
9024 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9025 supposed to be allowed. A defect report has been filed about this
9026 issue.
9028 GNU Extension:
9030 explicit-instantiation:
9031 storage-class-specifier template
9032 decl-specifier-seq [opt] declarator [opt] ;
9033 function-specifier template
9034 decl-specifier-seq [opt] declarator [opt] ; */
9036 static void
9037 cp_parser_explicit_instantiation (cp_parser* parser)
9039 int declares_class_or_enum;
9040 cp_decl_specifier_seq decl_specifiers;
9041 tree extension_specifier = NULL_TREE;
9043 /* Look for an (optional) storage-class-specifier or
9044 function-specifier. */
9045 if (cp_parser_allow_gnu_extensions_p (parser))
9047 extension_specifier
9048 = cp_parser_storage_class_specifier_opt (parser);
9049 if (!extension_specifier)
9050 extension_specifier
9051 = cp_parser_function_specifier_opt (parser,
9052 /*decl_specs=*/NULL);
9055 /* Look for the `template' keyword. */
9056 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9057 /* Let the front end know that we are processing an explicit
9058 instantiation. */
9059 begin_explicit_instantiation ();
9060 /* [temp.explicit] says that we are supposed to ignore access
9061 control while processing explicit instantiation directives. */
9062 push_deferring_access_checks (dk_no_check);
9063 /* Parse a decl-specifier-seq. */
9064 cp_parser_decl_specifier_seq (parser,
9065 CP_PARSER_FLAGS_OPTIONAL,
9066 &decl_specifiers,
9067 &declares_class_or_enum);
9068 /* If there was exactly one decl-specifier, and it declared a class,
9069 and there's no declarator, then we have an explicit type
9070 instantiation. */
9071 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9073 tree type;
9075 type = check_tag_decl (&decl_specifiers);
9076 /* Turn access control back on for names used during
9077 template instantiation. */
9078 pop_deferring_access_checks ();
9079 if (type)
9080 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9082 else
9084 cp_declarator *declarator;
9085 tree decl;
9087 /* Parse the declarator. */
9088 declarator
9089 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9090 /*ctor_dtor_or_conv_p=*/NULL,
9091 /*parenthesized_p=*/NULL,
9092 /*member_p=*/false);
9093 if (declares_class_or_enum & 2)
9094 cp_parser_check_for_definition_in_return_type (declarator,
9095 decl_specifiers.type);
9096 if (declarator != cp_error_declarator)
9098 decl = grokdeclarator (declarator, &decl_specifiers,
9099 NORMAL, 0, NULL);
9100 /* Turn access control back on for names used during
9101 template instantiation. */
9102 pop_deferring_access_checks ();
9103 /* Do the explicit instantiation. */
9104 do_decl_instantiation (decl, extension_specifier);
9106 else
9108 pop_deferring_access_checks ();
9109 /* Skip the body of the explicit instantiation. */
9110 cp_parser_skip_to_end_of_statement (parser);
9113 /* We're done with the instantiation. */
9114 end_explicit_instantiation ();
9116 cp_parser_consume_semicolon_at_end_of_statement (parser);
9119 /* Parse an explicit-specialization.
9121 explicit-specialization:
9122 template < > declaration
9124 Although the standard says `declaration', what it really means is:
9126 explicit-specialization:
9127 template <> decl-specifier [opt] init-declarator [opt] ;
9128 template <> function-definition
9129 template <> explicit-specialization
9130 template <> template-declaration */
9132 static void
9133 cp_parser_explicit_specialization (cp_parser* parser)
9135 /* Look for the `template' keyword. */
9136 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9137 /* Look for the `<'. */
9138 cp_parser_require (parser, CPP_LESS, "`<'");
9139 /* Look for the `>'. */
9140 cp_parser_require (parser, CPP_GREATER, "`>'");
9141 /* We have processed another parameter list. */
9142 ++parser->num_template_parameter_lists;
9143 /* Let the front end know that we are beginning a specialization. */
9144 begin_specialization ();
9146 /* If the next keyword is `template', we need to figure out whether
9147 or not we're looking a template-declaration. */
9148 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9150 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9151 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9152 cp_parser_template_declaration_after_export (parser,
9153 /*member_p=*/false);
9154 else
9155 cp_parser_explicit_specialization (parser);
9157 else
9158 /* Parse the dependent declaration. */
9159 cp_parser_single_declaration (parser,
9160 /*member_p=*/false,
9161 /*friend_p=*/NULL);
9163 /* We're done with the specialization. */
9164 end_specialization ();
9165 /* We're done with this parameter list. */
9166 --parser->num_template_parameter_lists;
9169 /* Parse a type-specifier.
9171 type-specifier:
9172 simple-type-specifier
9173 class-specifier
9174 enum-specifier
9175 elaborated-type-specifier
9176 cv-qualifier
9178 GNU Extension:
9180 type-specifier:
9181 __complex__
9183 Returns a representation of the type-specifier. For a
9184 class-specifier, enum-specifier, or elaborated-type-specifier, a
9185 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9187 The parser flags FLAGS is used to control type-specifier parsing.
9189 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9190 in a decl-specifier-seq.
9192 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9193 class-specifier, enum-specifier, or elaborated-type-specifier, then
9194 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9195 if a type is declared; 2 if it is defined. Otherwise, it is set to
9196 zero.
9198 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9199 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9200 is set to FALSE. */
9202 static tree
9203 cp_parser_type_specifier (cp_parser* parser,
9204 cp_parser_flags flags,
9205 cp_decl_specifier_seq *decl_specs,
9206 bool is_declaration,
9207 int* declares_class_or_enum,
9208 bool* is_cv_qualifier)
9210 tree type_spec = NULL_TREE;
9211 cp_token *token;
9212 enum rid keyword;
9213 cp_decl_spec ds = ds_last;
9215 /* Assume this type-specifier does not declare a new type. */
9216 if (declares_class_or_enum)
9217 *declares_class_or_enum = 0;
9218 /* And that it does not specify a cv-qualifier. */
9219 if (is_cv_qualifier)
9220 *is_cv_qualifier = false;
9221 /* Peek at the next token. */
9222 token = cp_lexer_peek_token (parser->lexer);
9224 /* If we're looking at a keyword, we can use that to guide the
9225 production we choose. */
9226 keyword = token->keyword;
9227 switch (keyword)
9229 case RID_ENUM:
9230 /* 'enum' [identifier] '{' introduces an enum-specifier;
9231 'enum' <anything else> introduces an elaborated-type-specifier. */
9232 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9233 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9234 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9235 == CPP_OPEN_BRACE))
9237 if (parser->num_template_parameter_lists)
9239 error ("template declaration of %qs", "enum");
9240 cp_parser_skip_to_end_of_block_or_statement (parser);
9241 type_spec = error_mark_node;
9243 else
9244 type_spec = cp_parser_enum_specifier (parser);
9246 if (declares_class_or_enum)
9247 *declares_class_or_enum = 2;
9248 if (decl_specs)
9249 cp_parser_set_decl_spec_type (decl_specs,
9250 type_spec,
9251 /*user_defined_p=*/true);
9252 return type_spec;
9254 else
9255 goto elaborated_type_specifier;
9257 /* Any of these indicate either a class-specifier, or an
9258 elaborated-type-specifier. */
9259 case RID_CLASS:
9260 case RID_STRUCT:
9261 case RID_UNION:
9262 /* Parse tentatively so that we can back up if we don't find a
9263 class-specifier. */
9264 cp_parser_parse_tentatively (parser);
9265 /* Look for the class-specifier. */
9266 type_spec = cp_parser_class_specifier (parser);
9267 /* If that worked, we're done. */
9268 if (cp_parser_parse_definitely (parser))
9270 if (declares_class_or_enum)
9271 *declares_class_or_enum = 2;
9272 if (decl_specs)
9273 cp_parser_set_decl_spec_type (decl_specs,
9274 type_spec,
9275 /*user_defined_p=*/true);
9276 return type_spec;
9279 /* Fall through. */
9280 elaborated_type_specifier:
9281 /* We're declaring (not defining) a class or enum. */
9282 if (declares_class_or_enum)
9283 *declares_class_or_enum = 1;
9285 /* Fall through. */
9286 case RID_TYPENAME:
9287 /* Look for an elaborated-type-specifier. */
9288 type_spec
9289 = (cp_parser_elaborated_type_specifier
9290 (parser,
9291 decl_specs && decl_specs->specs[(int) ds_friend],
9292 is_declaration));
9293 if (decl_specs)
9294 cp_parser_set_decl_spec_type (decl_specs,
9295 type_spec,
9296 /*user_defined_p=*/true);
9297 return type_spec;
9299 case RID_CONST:
9300 ds = ds_const;
9301 if (is_cv_qualifier)
9302 *is_cv_qualifier = true;
9303 break;
9305 case RID_VOLATILE:
9306 ds = ds_volatile;
9307 if (is_cv_qualifier)
9308 *is_cv_qualifier = true;
9309 break;
9311 case RID_RESTRICT:
9312 ds = ds_restrict;
9313 if (is_cv_qualifier)
9314 *is_cv_qualifier = true;
9315 break;
9317 case RID_COMPLEX:
9318 /* The `__complex__' keyword is a GNU extension. */
9319 ds = ds_complex;
9320 break;
9322 default:
9323 break;
9326 /* Handle simple keywords. */
9327 if (ds != ds_last)
9329 if (decl_specs)
9331 ++decl_specs->specs[(int)ds];
9332 decl_specs->any_specifiers_p = true;
9334 return cp_lexer_consume_token (parser->lexer)->value;
9337 /* If we do not already have a type-specifier, assume we are looking
9338 at a simple-type-specifier. */
9339 type_spec = cp_parser_simple_type_specifier (parser,
9340 decl_specs,
9341 flags);
9343 /* If we didn't find a type-specifier, and a type-specifier was not
9344 optional in this context, issue an error message. */
9345 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9347 cp_parser_error (parser, "expected type specifier");
9348 return error_mark_node;
9351 return type_spec;
9354 /* Parse a simple-type-specifier.
9356 simple-type-specifier:
9357 :: [opt] nested-name-specifier [opt] type-name
9358 :: [opt] nested-name-specifier template template-id
9359 char
9360 wchar_t
9361 bool
9362 short
9364 long
9365 signed
9366 unsigned
9367 float
9368 double
9369 void
9371 GNU Extension:
9373 simple-type-specifier:
9374 __typeof__ unary-expression
9375 __typeof__ ( type-id )
9377 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9378 appropriately updated. */
9380 static tree
9381 cp_parser_simple_type_specifier (cp_parser* parser,
9382 cp_decl_specifier_seq *decl_specs,
9383 cp_parser_flags flags)
9385 tree type = NULL_TREE;
9386 cp_token *token;
9388 /* Peek at the next token. */
9389 token = cp_lexer_peek_token (parser->lexer);
9391 /* If we're looking at a keyword, things are easy. */
9392 switch (token->keyword)
9394 case RID_CHAR:
9395 if (decl_specs)
9396 decl_specs->explicit_char_p = true;
9397 type = char_type_node;
9398 break;
9399 case RID_WCHAR:
9400 type = wchar_type_node;
9401 break;
9402 case RID_BOOL:
9403 type = boolean_type_node;
9404 break;
9405 case RID_SHORT:
9406 if (decl_specs)
9407 ++decl_specs->specs[(int) ds_short];
9408 type = short_integer_type_node;
9409 break;
9410 case RID_INT:
9411 if (decl_specs)
9412 decl_specs->explicit_int_p = true;
9413 type = integer_type_node;
9414 break;
9415 case RID_LONG:
9416 if (decl_specs)
9417 ++decl_specs->specs[(int) ds_long];
9418 type = long_integer_type_node;
9419 break;
9420 case RID_SIGNED:
9421 if (decl_specs)
9422 ++decl_specs->specs[(int) ds_signed];
9423 type = integer_type_node;
9424 break;
9425 case RID_UNSIGNED:
9426 if (decl_specs)
9427 ++decl_specs->specs[(int) ds_unsigned];
9428 type = unsigned_type_node;
9429 break;
9430 case RID_FLOAT:
9431 type = float_type_node;
9432 break;
9433 case RID_DOUBLE:
9434 type = double_type_node;
9435 break;
9436 case RID_VOID:
9437 type = void_type_node;
9438 break;
9440 case RID_TYPEOF:
9441 /* Consume the `typeof' token. */
9442 cp_lexer_consume_token (parser->lexer);
9443 /* Parse the operand to `typeof'. */
9444 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9445 /* If it is not already a TYPE, take its type. */
9446 if (!TYPE_P (type))
9447 type = finish_typeof (type);
9449 if (decl_specs)
9450 cp_parser_set_decl_spec_type (decl_specs, type,
9451 /*user_defined_p=*/true);
9453 return type;
9455 default:
9456 break;
9459 /* If the type-specifier was for a built-in type, we're done. */
9460 if (type)
9462 tree id;
9464 /* Record the type. */
9465 if (decl_specs
9466 && (token->keyword != RID_SIGNED
9467 && token->keyword != RID_UNSIGNED
9468 && token->keyword != RID_SHORT
9469 && token->keyword != RID_LONG))
9470 cp_parser_set_decl_spec_type (decl_specs,
9471 type,
9472 /*user_defined=*/false);
9473 if (decl_specs)
9474 decl_specs->any_specifiers_p = true;
9476 /* Consume the token. */
9477 id = cp_lexer_consume_token (parser->lexer)->value;
9479 /* There is no valid C++ program where a non-template type is
9480 followed by a "<". That usually indicates that the user thought
9481 that the type was a template. */
9482 cp_parser_check_for_invalid_template_id (parser, type);
9484 return TYPE_NAME (type);
9487 /* The type-specifier must be a user-defined type. */
9488 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9490 bool qualified_p;
9491 bool global_p;
9493 /* Don't gobble tokens or issue error messages if this is an
9494 optional type-specifier. */
9495 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9496 cp_parser_parse_tentatively (parser);
9498 /* Look for the optional `::' operator. */
9499 global_p
9500 = (cp_parser_global_scope_opt (parser,
9501 /*current_scope_valid_p=*/false)
9502 != NULL_TREE);
9503 /* Look for the nested-name specifier. */
9504 qualified_p
9505 = (cp_parser_nested_name_specifier_opt (parser,
9506 /*typename_keyword_p=*/false,
9507 /*check_dependency_p=*/true,
9508 /*type_p=*/false,
9509 /*is_declaration=*/false)
9510 != NULL_TREE);
9511 /* If we have seen a nested-name-specifier, and the next token
9512 is `template', then we are using the template-id production. */
9513 if (parser->scope
9514 && cp_parser_optional_template_keyword (parser))
9516 /* Look for the template-id. */
9517 type = cp_parser_template_id (parser,
9518 /*template_keyword_p=*/true,
9519 /*check_dependency_p=*/true,
9520 /*is_declaration=*/false);
9521 /* If the template-id did not name a type, we are out of
9522 luck. */
9523 if (TREE_CODE (type) != TYPE_DECL)
9525 cp_parser_error (parser, "expected template-id for type");
9526 type = NULL_TREE;
9529 /* Otherwise, look for a type-name. */
9530 else
9531 type = cp_parser_type_name (parser);
9532 /* Keep track of all name-lookups performed in class scopes. */
9533 if (type
9534 && !global_p
9535 && !qualified_p
9536 && TREE_CODE (type) == TYPE_DECL
9537 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9538 maybe_note_name_used_in_class (DECL_NAME (type), type);
9539 /* If it didn't work out, we don't have a TYPE. */
9540 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9541 && !cp_parser_parse_definitely (parser))
9542 type = NULL_TREE;
9543 if (type && decl_specs)
9544 cp_parser_set_decl_spec_type (decl_specs, type,
9545 /*user_defined=*/true);
9548 /* If we didn't get a type-name, issue an error message. */
9549 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9551 cp_parser_error (parser, "expected type-name");
9552 return error_mark_node;
9555 /* There is no valid C++ program where a non-template type is
9556 followed by a "<". That usually indicates that the user thought
9557 that the type was a template. */
9558 if (type && type != error_mark_node)
9559 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9561 return type;
9564 /* Parse a type-name.
9566 type-name:
9567 class-name
9568 enum-name
9569 typedef-name
9571 enum-name:
9572 identifier
9574 typedef-name:
9575 identifier
9577 Returns a TYPE_DECL for the the type. */
9579 static tree
9580 cp_parser_type_name (cp_parser* parser)
9582 tree type_decl;
9583 tree identifier;
9585 /* We can't know yet whether it is a class-name or not. */
9586 cp_parser_parse_tentatively (parser);
9587 /* Try a class-name. */
9588 type_decl = cp_parser_class_name (parser,
9589 /*typename_keyword_p=*/false,
9590 /*template_keyword_p=*/false,
9591 none_type,
9592 /*check_dependency_p=*/true,
9593 /*class_head_p=*/false,
9594 /*is_declaration=*/false);
9595 /* If it's not a class-name, keep looking. */
9596 if (!cp_parser_parse_definitely (parser))
9598 /* It must be a typedef-name or an enum-name. */
9599 identifier = cp_parser_identifier (parser);
9600 if (identifier == error_mark_node)
9601 return error_mark_node;
9603 /* Look up the type-name. */
9604 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9605 /* Issue an error if we did not find a type-name. */
9606 if (TREE_CODE (type_decl) != TYPE_DECL)
9608 if (!cp_parser_simulate_error (parser))
9609 cp_parser_name_lookup_error (parser, identifier, type_decl,
9610 "is not a type");
9611 type_decl = error_mark_node;
9613 /* Remember that the name was used in the definition of the
9614 current class so that we can check later to see if the
9615 meaning would have been different after the class was
9616 entirely defined. */
9617 else if (type_decl != error_mark_node
9618 && !parser->scope)
9619 maybe_note_name_used_in_class (identifier, type_decl);
9622 return type_decl;
9626 /* Parse an elaborated-type-specifier. Note that the grammar given
9627 here incorporates the resolution to DR68.
9629 elaborated-type-specifier:
9630 class-key :: [opt] nested-name-specifier [opt] identifier
9631 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9632 enum :: [opt] nested-name-specifier [opt] identifier
9633 typename :: [opt] nested-name-specifier identifier
9634 typename :: [opt] nested-name-specifier template [opt]
9635 template-id
9637 GNU extension:
9639 elaborated-type-specifier:
9640 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9641 class-key attributes :: [opt] nested-name-specifier [opt]
9642 template [opt] template-id
9643 enum attributes :: [opt] nested-name-specifier [opt] identifier
9645 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9646 declared `friend'. If IS_DECLARATION is TRUE, then this
9647 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9648 something is being declared.
9650 Returns the TYPE specified. */
9652 static tree
9653 cp_parser_elaborated_type_specifier (cp_parser* parser,
9654 bool is_friend,
9655 bool is_declaration)
9657 enum tag_types tag_type;
9658 tree identifier;
9659 tree type = NULL_TREE;
9660 tree attributes = NULL_TREE;
9662 /* See if we're looking at the `enum' keyword. */
9663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9665 /* Consume the `enum' token. */
9666 cp_lexer_consume_token (parser->lexer);
9667 /* Remember that it's an enumeration type. */
9668 tag_type = enum_type;
9669 /* Parse the attributes. */
9670 attributes = cp_parser_attributes_opt (parser);
9672 /* Or, it might be `typename'. */
9673 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9674 RID_TYPENAME))
9676 /* Consume the `typename' token. */
9677 cp_lexer_consume_token (parser->lexer);
9678 /* Remember that it's a `typename' type. */
9679 tag_type = typename_type;
9680 /* The `typename' keyword is only allowed in templates. */
9681 if (!processing_template_decl)
9682 pedwarn ("using %<typename%> outside of template");
9684 /* Otherwise it must be a class-key. */
9685 else
9687 tag_type = cp_parser_class_key (parser);
9688 if (tag_type == none_type)
9689 return error_mark_node;
9690 /* Parse the attributes. */
9691 attributes = cp_parser_attributes_opt (parser);
9694 /* Look for the `::' operator. */
9695 cp_parser_global_scope_opt (parser,
9696 /*current_scope_valid_p=*/false);
9697 /* Look for the nested-name-specifier. */
9698 if (tag_type == typename_type)
9700 if (cp_parser_nested_name_specifier (parser,
9701 /*typename_keyword_p=*/true,
9702 /*check_dependency_p=*/true,
9703 /*type_p=*/true,
9704 is_declaration)
9705 == error_mark_node)
9706 return error_mark_node;
9708 else
9709 /* Even though `typename' is not present, the proposed resolution
9710 to Core Issue 180 says that in `class A<T>::B', `B' should be
9711 considered a type-name, even if `A<T>' is dependent. */
9712 cp_parser_nested_name_specifier_opt (parser,
9713 /*typename_keyword_p=*/true,
9714 /*check_dependency_p=*/true,
9715 /*type_p=*/true,
9716 is_declaration);
9717 /* For everything but enumeration types, consider a template-id. */
9718 if (tag_type != enum_type)
9720 bool template_p = false;
9721 tree decl;
9723 /* Allow the `template' keyword. */
9724 template_p = cp_parser_optional_template_keyword (parser);
9725 /* If we didn't see `template', we don't know if there's a
9726 template-id or not. */
9727 if (!template_p)
9728 cp_parser_parse_tentatively (parser);
9729 /* Parse the template-id. */
9730 decl = cp_parser_template_id (parser, template_p,
9731 /*check_dependency_p=*/true,
9732 is_declaration);
9733 /* If we didn't find a template-id, look for an ordinary
9734 identifier. */
9735 if (!template_p && !cp_parser_parse_definitely (parser))
9737 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9738 in effect, then we must assume that, upon instantiation, the
9739 template will correspond to a class. */
9740 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9741 && tag_type == typename_type)
9742 type = make_typename_type (parser->scope, decl,
9743 typename_type,
9744 /*complain=*/1);
9745 else
9746 type = TREE_TYPE (decl);
9749 /* For an enumeration type, consider only a plain identifier. */
9750 if (!type)
9752 identifier = cp_parser_identifier (parser);
9754 if (identifier == error_mark_node)
9756 parser->scope = NULL_TREE;
9757 return error_mark_node;
9760 /* For a `typename', we needn't call xref_tag. */
9761 if (tag_type == typename_type
9762 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9763 return cp_parser_make_typename_type (parser, parser->scope,
9764 identifier);
9765 /* Look up a qualified name in the usual way. */
9766 if (parser->scope)
9768 tree decl;
9770 decl = cp_parser_lookup_name (parser, identifier,
9771 tag_type,
9772 /*is_template=*/false,
9773 /*is_namespace=*/false,
9774 /*check_dependency=*/true,
9775 /*ambiguous_p=*/NULL);
9777 /* If we are parsing friend declaration, DECL may be a
9778 TEMPLATE_DECL tree node here. However, we need to check
9779 whether this TEMPLATE_DECL results in valid code. Consider
9780 the following example:
9782 namespace N {
9783 template <class T> class C {};
9785 class X {
9786 template <class T> friend class N::C; // #1, valid code
9788 template <class T> class Y {
9789 friend class N::C; // #2, invalid code
9792 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9793 name lookup of `N::C'. We see that friend declaration must
9794 be template for the code to be valid. Note that
9795 processing_template_decl does not work here since it is
9796 always 1 for the above two cases. */
9798 decl = (cp_parser_maybe_treat_template_as_class
9799 (decl, /*tag_name_p=*/is_friend
9800 && parser->num_template_parameter_lists));
9802 if (TREE_CODE (decl) != TYPE_DECL)
9804 cp_parser_diagnose_invalid_type_name (parser,
9805 parser->scope,
9806 identifier);
9807 return error_mark_node;
9810 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9811 check_elaborated_type_specifier
9812 (tag_type, decl,
9813 (parser->num_template_parameter_lists
9814 || DECL_SELF_REFERENCE_P (decl)));
9816 type = TREE_TYPE (decl);
9818 else
9820 /* An elaborated-type-specifier sometimes introduces a new type and
9821 sometimes names an existing type. Normally, the rule is that it
9822 introduces a new type only if there is not an existing type of
9823 the same name already in scope. For example, given:
9825 struct S {};
9826 void f() { struct S s; }
9828 the `struct S' in the body of `f' is the same `struct S' as in
9829 the global scope; the existing definition is used. However, if
9830 there were no global declaration, this would introduce a new
9831 local class named `S'.
9833 An exception to this rule applies to the following code:
9835 namespace N { struct S; }
9837 Here, the elaborated-type-specifier names a new type
9838 unconditionally; even if there is already an `S' in the
9839 containing scope this declaration names a new type.
9840 This exception only applies if the elaborated-type-specifier
9841 forms the complete declaration:
9843 [class.name]
9845 A declaration consisting solely of `class-key identifier ;' is
9846 either a redeclaration of the name in the current scope or a
9847 forward declaration of the identifier as a class name. It
9848 introduces the name into the current scope.
9850 We are in this situation precisely when the next token is a `;'.
9852 An exception to the exception is that a `friend' declaration does
9853 *not* name a new type; i.e., given:
9855 struct S { friend struct T; };
9857 `T' is not a new type in the scope of `S'.
9859 Also, `new struct S' or `sizeof (struct S)' never results in the
9860 definition of a new type; a new type can only be declared in a
9861 declaration context. */
9863 tag_scope ts;
9864 if (is_friend)
9865 /* Friends have special name lookup rules. */
9866 ts = ts_within_enclosing_non_class;
9867 else if (is_declaration
9868 && cp_lexer_next_token_is (parser->lexer,
9869 CPP_SEMICOLON))
9870 /* This is a `class-key identifier ;' */
9871 ts = ts_current;
9872 else
9873 ts = ts_global;
9875 /* Warn about attributes. They are ignored. */
9876 if (attributes)
9877 warning ("type attributes are honored only at type definition");
9879 type = xref_tag (tag_type, identifier, ts,
9880 parser->num_template_parameter_lists);
9883 if (tag_type != enum_type)
9884 cp_parser_check_class_key (tag_type, type);
9886 /* A "<" cannot follow an elaborated type specifier. If that
9887 happens, the user was probably trying to form a template-id. */
9888 cp_parser_check_for_invalid_template_id (parser, type);
9890 return type;
9893 /* Parse an enum-specifier.
9895 enum-specifier:
9896 enum identifier [opt] { enumerator-list [opt] }
9898 GNU Extensions:
9899 enum identifier [opt] { enumerator-list [opt] } attributes
9901 Returns an ENUM_TYPE representing the enumeration. */
9903 static tree
9904 cp_parser_enum_specifier (cp_parser* parser)
9906 tree identifier;
9907 tree type;
9909 /* Caller guarantees that the current token is 'enum', an identifier
9910 possibly follows, and the token after that is an opening brace.
9911 If we don't have an identifier, fabricate an anonymous name for
9912 the enumeration being defined. */
9913 cp_lexer_consume_token (parser->lexer);
9915 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9916 identifier = cp_parser_identifier (parser);
9917 else
9918 identifier = make_anon_name ();
9920 /* Issue an error message if type-definitions are forbidden here. */
9921 cp_parser_check_type_definition (parser);
9923 /* Create the new type. We do this before consuming the opening brace
9924 so the enum will be recorded as being on the line of its tag (or the
9925 'enum' keyword, if there is no tag). */
9926 type = start_enum (identifier);
9928 /* Consume the opening brace. */
9929 cp_lexer_consume_token (parser->lexer);
9931 /* If the next token is not '}', then there are some enumerators. */
9932 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9933 cp_parser_enumerator_list (parser, type);
9935 /* Consume the final '}'. */
9936 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9938 /* Look for trailing attributes to apply to this enumeration, and
9939 apply them if appropriate. */
9940 if (cp_parser_allow_gnu_extensions_p (parser))
9942 tree trailing_attr = cp_parser_attributes_opt (parser);
9943 cplus_decl_attributes (&type,
9944 trailing_attr,
9945 (int) ATTR_FLAG_TYPE_IN_PLACE);
9948 /* Finish up the enumeration. */
9949 finish_enum (type);
9951 return type;
9954 /* Parse an enumerator-list. The enumerators all have the indicated
9955 TYPE.
9957 enumerator-list:
9958 enumerator-definition
9959 enumerator-list , enumerator-definition */
9961 static void
9962 cp_parser_enumerator_list (cp_parser* parser, tree type)
9964 while (true)
9966 /* Parse an enumerator-definition. */
9967 cp_parser_enumerator_definition (parser, type);
9969 /* If the next token is not a ',', we've reached the end of
9970 the list. */
9971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9972 break;
9973 /* Otherwise, consume the `,' and keep going. */
9974 cp_lexer_consume_token (parser->lexer);
9975 /* If the next token is a `}', there is a trailing comma. */
9976 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9978 if (pedantic && !in_system_header)
9979 pedwarn ("comma at end of enumerator list");
9980 break;
9985 /* Parse an enumerator-definition. The enumerator has the indicated
9986 TYPE.
9988 enumerator-definition:
9989 enumerator
9990 enumerator = constant-expression
9992 enumerator:
9993 identifier */
9995 static void
9996 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9998 tree identifier;
9999 tree value;
10001 /* Look for the identifier. */
10002 identifier = cp_parser_identifier (parser);
10003 if (identifier == error_mark_node)
10004 return;
10006 /* If the next token is an '=', then there is an explicit value. */
10007 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10009 /* Consume the `=' token. */
10010 cp_lexer_consume_token (parser->lexer);
10011 /* Parse the value. */
10012 value = cp_parser_constant_expression (parser,
10013 /*allow_non_constant_p=*/false,
10014 NULL);
10016 else
10017 value = NULL_TREE;
10019 /* Create the enumerator. */
10020 build_enumerator (identifier, value, type);
10023 /* Parse a namespace-name.
10025 namespace-name:
10026 original-namespace-name
10027 namespace-alias
10029 Returns the NAMESPACE_DECL for the namespace. */
10031 static tree
10032 cp_parser_namespace_name (cp_parser* parser)
10034 tree identifier;
10035 tree namespace_decl;
10037 /* Get the name of the namespace. */
10038 identifier = cp_parser_identifier (parser);
10039 if (identifier == error_mark_node)
10040 return error_mark_node;
10042 /* Look up the identifier in the currently active scope. Look only
10043 for namespaces, due to:
10045 [basic.lookup.udir]
10047 When looking up a namespace-name in a using-directive or alias
10048 definition, only namespace names are considered.
10050 And:
10052 [basic.lookup.qual]
10054 During the lookup of a name preceding the :: scope resolution
10055 operator, object, function, and enumerator names are ignored.
10057 (Note that cp_parser_class_or_namespace_name only calls this
10058 function if the token after the name is the scope resolution
10059 operator.) */
10060 namespace_decl = cp_parser_lookup_name (parser, identifier,
10061 none_type,
10062 /*is_template=*/false,
10063 /*is_namespace=*/true,
10064 /*check_dependency=*/true,
10065 /*ambiguous_p=*/NULL);
10066 /* If it's not a namespace, issue an error. */
10067 if (namespace_decl == error_mark_node
10068 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10070 cp_parser_error (parser, "expected namespace-name");
10071 namespace_decl = error_mark_node;
10074 return namespace_decl;
10077 /* Parse a namespace-definition.
10079 namespace-definition:
10080 named-namespace-definition
10081 unnamed-namespace-definition
10083 named-namespace-definition:
10084 original-namespace-definition
10085 extension-namespace-definition
10087 original-namespace-definition:
10088 namespace identifier { namespace-body }
10090 extension-namespace-definition:
10091 namespace original-namespace-name { namespace-body }
10093 unnamed-namespace-definition:
10094 namespace { namespace-body } */
10096 static void
10097 cp_parser_namespace_definition (cp_parser* parser)
10099 tree identifier;
10101 /* Look for the `namespace' keyword. */
10102 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10104 /* Get the name of the namespace. We do not attempt to distinguish
10105 between an original-namespace-definition and an
10106 extension-namespace-definition at this point. The semantic
10107 analysis routines are responsible for that. */
10108 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10109 identifier = cp_parser_identifier (parser);
10110 else
10111 identifier = NULL_TREE;
10113 /* Look for the `{' to start the namespace. */
10114 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10115 /* Start the namespace. */
10116 push_namespace (identifier);
10117 /* Parse the body of the namespace. */
10118 cp_parser_namespace_body (parser);
10119 /* Finish the namespace. */
10120 pop_namespace ();
10121 /* Look for the final `}'. */
10122 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10125 /* Parse a namespace-body.
10127 namespace-body:
10128 declaration-seq [opt] */
10130 static void
10131 cp_parser_namespace_body (cp_parser* parser)
10133 cp_parser_declaration_seq_opt (parser);
10136 /* Parse a namespace-alias-definition.
10138 namespace-alias-definition:
10139 namespace identifier = qualified-namespace-specifier ; */
10141 static void
10142 cp_parser_namespace_alias_definition (cp_parser* parser)
10144 tree identifier;
10145 tree namespace_specifier;
10147 /* Look for the `namespace' keyword. */
10148 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10149 /* Look for the identifier. */
10150 identifier = cp_parser_identifier (parser);
10151 if (identifier == error_mark_node)
10152 return;
10153 /* Look for the `=' token. */
10154 cp_parser_require (parser, CPP_EQ, "`='");
10155 /* Look for the qualified-namespace-specifier. */
10156 namespace_specifier
10157 = cp_parser_qualified_namespace_specifier (parser);
10158 /* Look for the `;' token. */
10159 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10161 /* Register the alias in the symbol table. */
10162 do_namespace_alias (identifier, namespace_specifier);
10165 /* Parse a qualified-namespace-specifier.
10167 qualified-namespace-specifier:
10168 :: [opt] nested-name-specifier [opt] namespace-name
10170 Returns a NAMESPACE_DECL corresponding to the specified
10171 namespace. */
10173 static tree
10174 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10176 /* Look for the optional `::'. */
10177 cp_parser_global_scope_opt (parser,
10178 /*current_scope_valid_p=*/false);
10180 /* Look for the optional nested-name-specifier. */
10181 cp_parser_nested_name_specifier_opt (parser,
10182 /*typename_keyword_p=*/false,
10183 /*check_dependency_p=*/true,
10184 /*type_p=*/false,
10185 /*is_declaration=*/true);
10187 return cp_parser_namespace_name (parser);
10190 /* Parse a using-declaration.
10192 using-declaration:
10193 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10194 using :: unqualified-id ; */
10196 static void
10197 cp_parser_using_declaration (cp_parser* parser)
10199 cp_token *token;
10200 bool typename_p = false;
10201 bool global_scope_p;
10202 tree decl;
10203 tree identifier;
10204 tree qscope;
10206 /* Look for the `using' keyword. */
10207 cp_parser_require_keyword (parser, RID_USING, "`using'");
10209 /* Peek at the next token. */
10210 token = cp_lexer_peek_token (parser->lexer);
10211 /* See if it's `typename'. */
10212 if (token->keyword == RID_TYPENAME)
10214 /* Remember that we've seen it. */
10215 typename_p = true;
10216 /* Consume the `typename' token. */
10217 cp_lexer_consume_token (parser->lexer);
10220 /* Look for the optional global scope qualification. */
10221 global_scope_p
10222 = (cp_parser_global_scope_opt (parser,
10223 /*current_scope_valid_p=*/false)
10224 != NULL_TREE);
10226 /* If we saw `typename', or didn't see `::', then there must be a
10227 nested-name-specifier present. */
10228 if (typename_p || !global_scope_p)
10229 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10230 /*check_dependency_p=*/true,
10231 /*type_p=*/false,
10232 /*is_declaration=*/true);
10233 /* Otherwise, we could be in either of the two productions. In that
10234 case, treat the nested-name-specifier as optional. */
10235 else
10236 qscope = cp_parser_nested_name_specifier_opt (parser,
10237 /*typename_keyword_p=*/false,
10238 /*check_dependency_p=*/true,
10239 /*type_p=*/false,
10240 /*is_declaration=*/true);
10241 if (!qscope)
10242 qscope = global_namespace;
10244 /* Parse the unqualified-id. */
10245 identifier = cp_parser_unqualified_id (parser,
10246 /*template_keyword_p=*/false,
10247 /*check_dependency_p=*/true,
10248 /*declarator_p=*/true);
10250 /* The function we call to handle a using-declaration is different
10251 depending on what scope we are in. */
10252 if (identifier == error_mark_node)
10254 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10255 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10256 /* [namespace.udecl]
10258 A using declaration shall not name a template-id. */
10259 error ("a template-id may not appear in a using-declaration");
10260 else
10262 if (at_class_scope_p ())
10264 /* Create the USING_DECL. */
10265 decl = do_class_using_decl (parser->scope, identifier);
10266 /* Add it to the list of members in this class. */
10267 finish_member_declaration (decl);
10269 else
10271 decl = cp_parser_lookup_name_simple (parser, identifier);
10272 if (decl == error_mark_node)
10273 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10274 else if (!at_namespace_scope_p ())
10275 do_local_using_decl (decl, qscope, identifier);
10276 else
10277 do_toplevel_using_decl (decl, qscope, identifier);
10281 /* Look for the final `;'. */
10282 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10285 /* Parse a using-directive.
10287 using-directive:
10288 using namespace :: [opt] nested-name-specifier [opt]
10289 namespace-name ; */
10291 static void
10292 cp_parser_using_directive (cp_parser* parser)
10294 tree namespace_decl;
10295 tree attribs;
10297 /* Look for the `using' keyword. */
10298 cp_parser_require_keyword (parser, RID_USING, "`using'");
10299 /* And the `namespace' keyword. */
10300 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10301 /* Look for the optional `::' operator. */
10302 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10303 /* And the optional nested-name-specifier. */
10304 cp_parser_nested_name_specifier_opt (parser,
10305 /*typename_keyword_p=*/false,
10306 /*check_dependency_p=*/true,
10307 /*type_p=*/false,
10308 /*is_declaration=*/true);
10309 /* Get the namespace being used. */
10310 namespace_decl = cp_parser_namespace_name (parser);
10311 /* And any specified attributes. */
10312 attribs = cp_parser_attributes_opt (parser);
10313 /* Update the symbol table. */
10314 parse_using_directive (namespace_decl, attribs);
10315 /* Look for the final `;'. */
10316 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10319 /* Parse an asm-definition.
10321 asm-definition:
10322 asm ( string-literal ) ;
10324 GNU Extension:
10326 asm-definition:
10327 asm volatile [opt] ( string-literal ) ;
10328 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10329 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10330 : asm-operand-list [opt] ) ;
10331 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10332 : asm-operand-list [opt]
10333 : asm-operand-list [opt] ) ; */
10335 static void
10336 cp_parser_asm_definition (cp_parser* parser)
10338 tree string;
10339 tree outputs = NULL_TREE;
10340 tree inputs = NULL_TREE;
10341 tree clobbers = NULL_TREE;
10342 tree asm_stmt;
10343 bool volatile_p = false;
10344 bool extended_p = false;
10346 /* Look for the `asm' keyword. */
10347 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10348 /* See if the next token is `volatile'. */
10349 if (cp_parser_allow_gnu_extensions_p (parser)
10350 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10352 /* Remember that we saw the `volatile' keyword. */
10353 volatile_p = true;
10354 /* Consume the token. */
10355 cp_lexer_consume_token (parser->lexer);
10357 /* Look for the opening `('. */
10358 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10359 return;
10360 /* Look for the string. */
10361 string = cp_parser_string_literal (parser, false, false);
10362 if (string == error_mark_node)
10364 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10365 /*consume_paren=*/true);
10366 return;
10369 /* If we're allowing GNU extensions, check for the extended assembly
10370 syntax. Unfortunately, the `:' tokens need not be separated by
10371 a space in C, and so, for compatibility, we tolerate that here
10372 too. Doing that means that we have to treat the `::' operator as
10373 two `:' tokens. */
10374 if (cp_parser_allow_gnu_extensions_p (parser)
10375 && at_function_scope_p ()
10376 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10377 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10379 bool inputs_p = false;
10380 bool clobbers_p = false;
10382 /* The extended syntax was used. */
10383 extended_p = true;
10385 /* Look for outputs. */
10386 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10388 /* Consume the `:'. */
10389 cp_lexer_consume_token (parser->lexer);
10390 /* Parse the output-operands. */
10391 if (cp_lexer_next_token_is_not (parser->lexer,
10392 CPP_COLON)
10393 && cp_lexer_next_token_is_not (parser->lexer,
10394 CPP_SCOPE)
10395 && cp_lexer_next_token_is_not (parser->lexer,
10396 CPP_CLOSE_PAREN))
10397 outputs = cp_parser_asm_operand_list (parser);
10399 /* If the next token is `::', there are no outputs, and the
10400 next token is the beginning of the inputs. */
10401 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10402 /* The inputs are coming next. */
10403 inputs_p = true;
10405 /* Look for inputs. */
10406 if (inputs_p
10407 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10409 /* Consume the `:' or `::'. */
10410 cp_lexer_consume_token (parser->lexer);
10411 /* Parse the output-operands. */
10412 if (cp_lexer_next_token_is_not (parser->lexer,
10413 CPP_COLON)
10414 && cp_lexer_next_token_is_not (parser->lexer,
10415 CPP_CLOSE_PAREN))
10416 inputs = cp_parser_asm_operand_list (parser);
10418 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10419 /* The clobbers are coming next. */
10420 clobbers_p = true;
10422 /* Look for clobbers. */
10423 if (clobbers_p
10424 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10426 /* Consume the `:' or `::'. */
10427 cp_lexer_consume_token (parser->lexer);
10428 /* Parse the clobbers. */
10429 if (cp_lexer_next_token_is_not (parser->lexer,
10430 CPP_CLOSE_PAREN))
10431 clobbers = cp_parser_asm_clobber_list (parser);
10434 /* Look for the closing `)'. */
10435 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10436 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10437 /*consume_paren=*/true);
10438 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10440 /* Create the ASM_EXPR. */
10441 if (at_function_scope_p ())
10443 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10444 inputs, clobbers);
10445 /* If the extended syntax was not used, mark the ASM_EXPR. */
10446 if (!extended_p)
10448 tree temp = asm_stmt;
10449 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10450 temp = TREE_OPERAND (temp, 0);
10452 ASM_INPUT_P (temp) = 1;
10455 else
10456 assemble_asm (string);
10459 /* Declarators [gram.dcl.decl] */
10461 /* Parse an init-declarator.
10463 init-declarator:
10464 declarator initializer [opt]
10466 GNU Extension:
10468 init-declarator:
10469 declarator asm-specification [opt] attributes [opt] initializer [opt]
10471 function-definition:
10472 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10473 function-body
10474 decl-specifier-seq [opt] declarator function-try-block
10476 GNU Extension:
10478 function-definition:
10479 __extension__ function-definition
10481 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10482 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10483 then this declarator appears in a class scope. The new DECL created
10484 by this declarator is returned.
10486 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10487 for a function-definition here as well. If the declarator is a
10488 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10489 be TRUE upon return. By that point, the function-definition will
10490 have been completely parsed.
10492 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10493 is FALSE. */
10495 static tree
10496 cp_parser_init_declarator (cp_parser* parser,
10497 cp_decl_specifier_seq *decl_specifiers,
10498 bool function_definition_allowed_p,
10499 bool member_p,
10500 int declares_class_or_enum,
10501 bool* function_definition_p)
10503 cp_token *token;
10504 cp_declarator *declarator;
10505 tree prefix_attributes;
10506 tree attributes;
10507 tree asm_specification;
10508 tree initializer;
10509 tree decl = NULL_TREE;
10510 tree scope;
10511 bool is_initialized;
10512 bool is_parenthesized_init;
10513 bool is_non_constant_init;
10514 int ctor_dtor_or_conv_p;
10515 bool friend_p;
10516 tree pushed_scope = NULL;
10518 /* Gather the attributes that were provided with the
10519 decl-specifiers. */
10520 prefix_attributes = decl_specifiers->attributes;
10522 /* Assume that this is not the declarator for a function
10523 definition. */
10524 if (function_definition_p)
10525 *function_definition_p = false;
10527 /* Defer access checks while parsing the declarator; we cannot know
10528 what names are accessible until we know what is being
10529 declared. */
10530 resume_deferring_access_checks ();
10532 /* Parse the declarator. */
10533 declarator
10534 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10535 &ctor_dtor_or_conv_p,
10536 /*parenthesized_p=*/NULL,
10537 /*member_p=*/false);
10538 /* Gather up the deferred checks. */
10539 stop_deferring_access_checks ();
10541 /* If the DECLARATOR was erroneous, there's no need to go
10542 further. */
10543 if (declarator == cp_error_declarator)
10544 return error_mark_node;
10546 if (declares_class_or_enum & 2)
10547 cp_parser_check_for_definition_in_return_type (declarator,
10548 decl_specifiers->type);
10550 /* Figure out what scope the entity declared by the DECLARATOR is
10551 located in. `grokdeclarator' sometimes changes the scope, so
10552 we compute it now. */
10553 scope = get_scope_of_declarator (declarator);
10555 /* If we're allowing GNU extensions, look for an asm-specification
10556 and attributes. */
10557 if (cp_parser_allow_gnu_extensions_p (parser))
10559 /* Look for an asm-specification. */
10560 asm_specification = cp_parser_asm_specification_opt (parser);
10561 /* And attributes. */
10562 attributes = cp_parser_attributes_opt (parser);
10564 else
10566 asm_specification = NULL_TREE;
10567 attributes = NULL_TREE;
10570 /* Peek at the next token. */
10571 token = cp_lexer_peek_token (parser->lexer);
10572 /* Check to see if the token indicates the start of a
10573 function-definition. */
10574 if (cp_parser_token_starts_function_definition_p (token))
10576 if (!function_definition_allowed_p)
10578 /* If a function-definition should not appear here, issue an
10579 error message. */
10580 cp_parser_error (parser,
10581 "a function-definition is not allowed here");
10582 return error_mark_node;
10584 else
10586 /* Neither attributes nor an asm-specification are allowed
10587 on a function-definition. */
10588 if (asm_specification)
10589 error ("an asm-specification is not allowed on a function-definition");
10590 if (attributes)
10591 error ("attributes are not allowed on a function-definition");
10592 /* This is a function-definition. */
10593 *function_definition_p = true;
10595 /* Parse the function definition. */
10596 if (member_p)
10597 decl = cp_parser_save_member_function_body (parser,
10598 decl_specifiers,
10599 declarator,
10600 prefix_attributes);
10601 else
10602 decl
10603 = (cp_parser_function_definition_from_specifiers_and_declarator
10604 (parser, decl_specifiers, prefix_attributes, declarator));
10606 return decl;
10610 /* [dcl.dcl]
10612 Only in function declarations for constructors, destructors, and
10613 type conversions can the decl-specifier-seq be omitted.
10615 We explicitly postpone this check past the point where we handle
10616 function-definitions because we tolerate function-definitions
10617 that are missing their return types in some modes. */
10618 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10620 cp_parser_error (parser,
10621 "expected constructor, destructor, or type conversion");
10622 return error_mark_node;
10625 /* An `=' or an `(' indicates an initializer. */
10626 is_initialized = (token->type == CPP_EQ
10627 || token->type == CPP_OPEN_PAREN);
10628 /* If the init-declarator isn't initialized and isn't followed by a
10629 `,' or `;', it's not a valid init-declarator. */
10630 if (!is_initialized
10631 && token->type != CPP_COMMA
10632 && token->type != CPP_SEMICOLON)
10634 cp_parser_error (parser, "expected initializer");
10635 return error_mark_node;
10638 /* Because start_decl has side-effects, we should only call it if we
10639 know we're going ahead. By this point, we know that we cannot
10640 possibly be looking at any other construct. */
10641 cp_parser_commit_to_tentative_parse (parser);
10643 /* If the decl specifiers were bad, issue an error now that we're
10644 sure this was intended to be a declarator. Then continue
10645 declaring the variable(s), as int, to try to cut down on further
10646 errors. */
10647 if (decl_specifiers->any_specifiers_p
10648 && decl_specifiers->type == error_mark_node)
10650 cp_parser_error (parser, "invalid type in declaration");
10651 decl_specifiers->type = integer_type_node;
10654 /* Check to see whether or not this declaration is a friend. */
10655 friend_p = cp_parser_friend_p (decl_specifiers);
10657 /* Check that the number of template-parameter-lists is OK. */
10658 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10659 return error_mark_node;
10661 /* Enter the newly declared entry in the symbol table. If we're
10662 processing a declaration in a class-specifier, we wait until
10663 after processing the initializer. */
10664 if (!member_p)
10666 if (parser->in_unbraced_linkage_specification_p)
10668 decl_specifiers->storage_class = sc_extern;
10669 have_extern_spec = false;
10671 decl = start_decl (declarator, decl_specifiers,
10672 is_initialized, attributes, prefix_attributes,
10673 &pushed_scope);
10675 else if (scope)
10676 /* Enter the SCOPE. That way unqualified names appearing in the
10677 initializer will be looked up in SCOPE. */
10678 pushed_scope = push_scope (scope);
10680 /* Perform deferred access control checks, now that we know in which
10681 SCOPE the declared entity resides. */
10682 if (!member_p && decl)
10684 tree saved_current_function_decl = NULL_TREE;
10686 /* If the entity being declared is a function, pretend that we
10687 are in its scope. If it is a `friend', it may have access to
10688 things that would not otherwise be accessible. */
10689 if (TREE_CODE (decl) == FUNCTION_DECL)
10691 saved_current_function_decl = current_function_decl;
10692 current_function_decl = decl;
10695 /* Perform the access control checks for the declarator and the
10696 the decl-specifiers. */
10697 perform_deferred_access_checks ();
10699 /* Restore the saved value. */
10700 if (TREE_CODE (decl) == FUNCTION_DECL)
10701 current_function_decl = saved_current_function_decl;
10704 /* Parse the initializer. */
10705 if (is_initialized)
10706 initializer = cp_parser_initializer (parser,
10707 &is_parenthesized_init,
10708 &is_non_constant_init);
10709 else
10711 initializer = NULL_TREE;
10712 is_parenthesized_init = false;
10713 is_non_constant_init = true;
10716 /* The old parser allows attributes to appear after a parenthesized
10717 initializer. Mark Mitchell proposed removing this functionality
10718 on the GCC mailing lists on 2002-08-13. This parser accepts the
10719 attributes -- but ignores them. */
10720 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10721 if (cp_parser_attributes_opt (parser))
10722 warning ("attributes after parenthesized initializer ignored");
10724 /* For an in-class declaration, use `grokfield' to create the
10725 declaration. */
10726 if (member_p)
10728 if (pushed_scope)
10730 pop_scope (pushed_scope);
10731 pushed_scope = false;
10733 decl = grokfield (declarator, decl_specifiers,
10734 initializer, /*asmspec=*/NULL_TREE,
10735 /*attributes=*/NULL_TREE);
10736 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10737 cp_parser_save_default_args (parser, decl);
10740 /* Finish processing the declaration. But, skip friend
10741 declarations. */
10742 if (!friend_p && decl && decl != error_mark_node)
10744 cp_finish_decl (decl,
10745 initializer,
10746 asm_specification,
10747 /* If the initializer is in parentheses, then this is
10748 a direct-initialization, which means that an
10749 `explicit' constructor is OK. Otherwise, an
10750 `explicit' constructor cannot be used. */
10751 ((is_parenthesized_init || !is_initialized)
10752 ? 0 : LOOKUP_ONLYCONVERTING));
10754 if (!friend_p && pushed_scope)
10755 pop_scope (pushed_scope);
10757 /* Remember whether or not variables were initialized by
10758 constant-expressions. */
10759 if (decl && TREE_CODE (decl) == VAR_DECL
10760 && is_initialized && !is_non_constant_init)
10761 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10763 return decl;
10766 /* Parse a declarator.
10768 declarator:
10769 direct-declarator
10770 ptr-operator declarator
10772 abstract-declarator:
10773 ptr-operator abstract-declarator [opt]
10774 direct-abstract-declarator
10776 GNU Extensions:
10778 declarator:
10779 attributes [opt] direct-declarator
10780 attributes [opt] ptr-operator declarator
10782 abstract-declarator:
10783 attributes [opt] ptr-operator abstract-declarator [opt]
10784 attributes [opt] direct-abstract-declarator
10786 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10787 detect constructor, destructor or conversion operators. It is set
10788 to -1 if the declarator is a name, and +1 if it is a
10789 function. Otherwise it is set to zero. Usually you just want to
10790 test for >0, but internally the negative value is used.
10792 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10793 a decl-specifier-seq unless it declares a constructor, destructor,
10794 or conversion. It might seem that we could check this condition in
10795 semantic analysis, rather than parsing, but that makes it difficult
10796 to handle something like `f()'. We want to notice that there are
10797 no decl-specifiers, and therefore realize that this is an
10798 expression, not a declaration.)
10800 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10801 the declarator is a direct-declarator of the form "(...)".
10803 MEMBER_P is true iff this declarator is a member-declarator. */
10805 static cp_declarator *
10806 cp_parser_declarator (cp_parser* parser,
10807 cp_parser_declarator_kind dcl_kind,
10808 int* ctor_dtor_or_conv_p,
10809 bool* parenthesized_p,
10810 bool member_p)
10812 cp_token *token;
10813 cp_declarator *declarator;
10814 enum tree_code code;
10815 cp_cv_quals cv_quals;
10816 tree class_type;
10817 tree attributes = NULL_TREE;
10819 /* Assume this is not a constructor, destructor, or type-conversion
10820 operator. */
10821 if (ctor_dtor_or_conv_p)
10822 *ctor_dtor_or_conv_p = 0;
10824 if (cp_parser_allow_gnu_extensions_p (parser))
10825 attributes = cp_parser_attributes_opt (parser);
10827 /* Peek at the next token. */
10828 token = cp_lexer_peek_token (parser->lexer);
10830 /* Check for the ptr-operator production. */
10831 cp_parser_parse_tentatively (parser);
10832 /* Parse the ptr-operator. */
10833 code = cp_parser_ptr_operator (parser,
10834 &class_type,
10835 &cv_quals);
10836 /* If that worked, then we have a ptr-operator. */
10837 if (cp_parser_parse_definitely (parser))
10839 /* If a ptr-operator was found, then this declarator was not
10840 parenthesized. */
10841 if (parenthesized_p)
10842 *parenthesized_p = true;
10843 /* The dependent declarator is optional if we are parsing an
10844 abstract-declarator. */
10845 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10846 cp_parser_parse_tentatively (parser);
10848 /* Parse the dependent declarator. */
10849 declarator = cp_parser_declarator (parser, dcl_kind,
10850 /*ctor_dtor_or_conv_p=*/NULL,
10851 /*parenthesized_p=*/NULL,
10852 /*member_p=*/false);
10854 /* If we are parsing an abstract-declarator, we must handle the
10855 case where the dependent declarator is absent. */
10856 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10857 && !cp_parser_parse_definitely (parser))
10858 declarator = NULL;
10860 /* Build the representation of the ptr-operator. */
10861 if (class_type)
10862 declarator = make_ptrmem_declarator (cv_quals,
10863 class_type,
10864 declarator);
10865 else if (code == INDIRECT_REF)
10866 declarator = make_pointer_declarator (cv_quals, declarator);
10867 else
10868 declarator = make_reference_declarator (cv_quals, declarator);
10870 /* Everything else is a direct-declarator. */
10871 else
10873 if (parenthesized_p)
10874 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10875 CPP_OPEN_PAREN);
10876 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10877 ctor_dtor_or_conv_p,
10878 member_p);
10881 if (attributes && declarator != cp_error_declarator)
10882 declarator->attributes = attributes;
10884 return declarator;
10887 /* Parse a direct-declarator or direct-abstract-declarator.
10889 direct-declarator:
10890 declarator-id
10891 direct-declarator ( parameter-declaration-clause )
10892 cv-qualifier-seq [opt]
10893 exception-specification [opt]
10894 direct-declarator [ constant-expression [opt] ]
10895 ( declarator )
10897 direct-abstract-declarator:
10898 direct-abstract-declarator [opt]
10899 ( parameter-declaration-clause )
10900 cv-qualifier-seq [opt]
10901 exception-specification [opt]
10902 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10903 ( abstract-declarator )
10905 Returns a representation of the declarator. DCL_KIND is
10906 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10907 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10908 we are parsing a direct-declarator. It is
10909 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10910 of ambiguity we prefer an abstract declarator, as per
10911 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10912 cp_parser_declarator. */
10914 static cp_declarator *
10915 cp_parser_direct_declarator (cp_parser* parser,
10916 cp_parser_declarator_kind dcl_kind,
10917 int* ctor_dtor_or_conv_p,
10918 bool member_p)
10920 cp_token *token;
10921 cp_declarator *declarator = NULL;
10922 tree scope = NULL_TREE;
10923 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10924 bool saved_in_declarator_p = parser->in_declarator_p;
10925 bool first = true;
10926 tree pushed_scope = NULL_TREE;
10928 while (true)
10930 /* Peek at the next token. */
10931 token = cp_lexer_peek_token (parser->lexer);
10932 if (token->type == CPP_OPEN_PAREN)
10934 /* This is either a parameter-declaration-clause, or a
10935 parenthesized declarator. When we know we are parsing a
10936 named declarator, it must be a parenthesized declarator
10937 if FIRST is true. For instance, `(int)' is a
10938 parameter-declaration-clause, with an omitted
10939 direct-abstract-declarator. But `((*))', is a
10940 parenthesized abstract declarator. Finally, when T is a
10941 template parameter `(T)' is a
10942 parameter-declaration-clause, and not a parenthesized
10943 named declarator.
10945 We first try and parse a parameter-declaration-clause,
10946 and then try a nested declarator (if FIRST is true).
10948 It is not an error for it not to be a
10949 parameter-declaration-clause, even when FIRST is
10950 false. Consider,
10952 int i (int);
10953 int i (3);
10955 The first is the declaration of a function while the
10956 second is a the definition of a variable, including its
10957 initializer.
10959 Having seen only the parenthesis, we cannot know which of
10960 these two alternatives should be selected. Even more
10961 complex are examples like:
10963 int i (int (a));
10964 int i (int (3));
10966 The former is a function-declaration; the latter is a
10967 variable initialization.
10969 Thus again, we try a parameter-declaration-clause, and if
10970 that fails, we back out and return. */
10972 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10974 cp_parameter_declarator *params;
10975 unsigned saved_num_template_parameter_lists;
10977 /* In a member-declarator, the only valid interpretation
10978 of a parenthesis is the start of a
10979 parameter-declaration-clause. (It is invalid to
10980 initialize a static data member with a parenthesized
10981 initializer; only the "=" form of initialization is
10982 permitted.) */
10983 if (!member_p)
10984 cp_parser_parse_tentatively (parser);
10986 /* Consume the `('. */
10987 cp_lexer_consume_token (parser->lexer);
10988 if (first)
10990 /* If this is going to be an abstract declarator, we're
10991 in a declarator and we can't have default args. */
10992 parser->default_arg_ok_p = false;
10993 parser->in_declarator_p = true;
10996 /* Inside the function parameter list, surrounding
10997 template-parameter-lists do not apply. */
10998 saved_num_template_parameter_lists
10999 = parser->num_template_parameter_lists;
11000 parser->num_template_parameter_lists = 0;
11002 /* Parse the parameter-declaration-clause. */
11003 params = cp_parser_parameter_declaration_clause (parser);
11005 parser->num_template_parameter_lists
11006 = saved_num_template_parameter_lists;
11008 /* If all went well, parse the cv-qualifier-seq and the
11009 exception-specification. */
11010 if (member_p || cp_parser_parse_definitely (parser))
11012 cp_cv_quals cv_quals;
11013 tree exception_specification;
11015 if (ctor_dtor_or_conv_p)
11016 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11017 first = false;
11018 /* Consume the `)'. */
11019 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11021 /* Parse the cv-qualifier-seq. */
11022 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11023 /* And the exception-specification. */
11024 exception_specification
11025 = cp_parser_exception_specification_opt (parser);
11027 /* Create the function-declarator. */
11028 declarator = make_call_declarator (declarator,
11029 params,
11030 cv_quals,
11031 exception_specification);
11032 /* Any subsequent parameter lists are to do with
11033 return type, so are not those of the declared
11034 function. */
11035 parser->default_arg_ok_p = false;
11037 /* Repeat the main loop. */
11038 continue;
11042 /* If this is the first, we can try a parenthesized
11043 declarator. */
11044 if (first)
11046 bool saved_in_type_id_in_expr_p;
11048 parser->default_arg_ok_p = saved_default_arg_ok_p;
11049 parser->in_declarator_p = saved_in_declarator_p;
11051 /* Consume the `('. */
11052 cp_lexer_consume_token (parser->lexer);
11053 /* Parse the nested declarator. */
11054 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11055 parser->in_type_id_in_expr_p = true;
11056 declarator
11057 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11058 /*parenthesized_p=*/NULL,
11059 member_p);
11060 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11061 first = false;
11062 /* Expect a `)'. */
11063 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11064 declarator = cp_error_declarator;
11065 if (declarator == cp_error_declarator)
11066 break;
11068 goto handle_declarator;
11070 /* Otherwise, we must be done. */
11071 else
11072 break;
11074 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11075 && token->type == CPP_OPEN_SQUARE)
11077 /* Parse an array-declarator. */
11078 tree bounds;
11080 if (ctor_dtor_or_conv_p)
11081 *ctor_dtor_or_conv_p = 0;
11083 first = false;
11084 parser->default_arg_ok_p = false;
11085 parser->in_declarator_p = true;
11086 /* Consume the `['. */
11087 cp_lexer_consume_token (parser->lexer);
11088 /* Peek at the next token. */
11089 token = cp_lexer_peek_token (parser->lexer);
11090 /* If the next token is `]', then there is no
11091 constant-expression. */
11092 if (token->type != CPP_CLOSE_SQUARE)
11094 bool non_constant_p;
11096 bounds
11097 = cp_parser_constant_expression (parser,
11098 /*allow_non_constant=*/true,
11099 &non_constant_p);
11100 if (!non_constant_p)
11101 bounds = fold_non_dependent_expr (bounds);
11102 else if (!at_function_scope_p ())
11104 error ("array bound is not an integer constant");
11105 bounds = error_mark_node;
11108 else
11109 bounds = NULL_TREE;
11110 /* Look for the closing `]'. */
11111 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11113 declarator = cp_error_declarator;
11114 break;
11117 declarator = make_array_declarator (declarator, bounds);
11119 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11121 tree qualifying_scope;
11122 tree unqualified_name;
11124 /* Parse a declarator-id */
11125 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11126 cp_parser_parse_tentatively (parser);
11127 unqualified_name = cp_parser_declarator_id (parser);
11128 qualifying_scope = parser->scope;
11129 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11131 if (!cp_parser_parse_definitely (parser))
11132 unqualified_name = error_mark_node;
11133 else if (qualifying_scope
11134 || (TREE_CODE (unqualified_name)
11135 != IDENTIFIER_NODE))
11137 cp_parser_error (parser, "expected unqualified-id");
11138 unqualified_name = error_mark_node;
11142 if (unqualified_name == error_mark_node)
11144 declarator = cp_error_declarator;
11145 break;
11148 if (qualifying_scope && at_namespace_scope_p ()
11149 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11151 /* In the declaration of a member of a template class
11152 outside of the class itself, the SCOPE will sometimes
11153 be a TYPENAME_TYPE. For example, given:
11155 template <typename T>
11156 int S<T>::R::i = 3;
11158 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11159 this context, we must resolve S<T>::R to an ordinary
11160 type, rather than a typename type.
11162 The reason we normally avoid resolving TYPENAME_TYPEs
11163 is that a specialization of `S' might render
11164 `S<T>::R' not a type. However, if `S' is
11165 specialized, then this `i' will not be used, so there
11166 is no harm in resolving the types here. */
11167 tree type;
11169 /* Resolve the TYPENAME_TYPE. */
11170 type = resolve_typename_type (qualifying_scope,
11171 /*only_current_p=*/false);
11172 /* If that failed, the declarator is invalid. */
11173 if (type == error_mark_node)
11174 error ("%<%T::%D%> is not a type",
11175 TYPE_CONTEXT (qualifying_scope),
11176 TYPE_IDENTIFIER (qualifying_scope));
11177 qualifying_scope = type;
11180 declarator = make_id_declarator (qualifying_scope,
11181 unqualified_name);
11182 if (unqualified_name)
11184 tree class_type;
11186 if (qualifying_scope
11187 && CLASS_TYPE_P (qualifying_scope))
11188 class_type = qualifying_scope;
11189 else
11190 class_type = current_class_type;
11192 if (class_type)
11194 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11195 declarator->u.id.sfk = sfk_destructor;
11196 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11197 declarator->u.id.sfk = sfk_conversion;
11198 else if (/* There's no way to declare a constructor
11199 for an anonymous type, even if the type
11200 got a name for linkage purposes. */
11201 !TYPE_WAS_ANONYMOUS (class_type)
11202 && (constructor_name_p (unqualified_name,
11203 class_type)
11204 || (TREE_CODE (unqualified_name) == TYPE_DECL
11205 && (same_type_p
11206 (TREE_TYPE (unqualified_name),
11207 class_type)))))
11208 declarator->u.id.sfk = sfk_constructor;
11210 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11211 *ctor_dtor_or_conv_p = -1;
11212 if (qualifying_scope
11213 && TREE_CODE (unqualified_name) == TYPE_DECL
11214 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11216 error ("invalid use of constructor as a template");
11217 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11218 "the constructor in a qualified name",
11219 class_type,
11220 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11221 class_type, class_type);
11226 handle_declarator:;
11227 scope = get_scope_of_declarator (declarator);
11228 if (scope)
11229 /* Any names that appear after the declarator-id for a
11230 member are looked up in the containing scope. */
11231 pushed_scope = push_scope (scope);
11232 parser->in_declarator_p = true;
11233 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11234 || (declarator && declarator->kind == cdk_id))
11235 /* Default args are only allowed on function
11236 declarations. */
11237 parser->default_arg_ok_p = saved_default_arg_ok_p;
11238 else
11239 parser->default_arg_ok_p = false;
11241 first = false;
11243 /* We're done. */
11244 else
11245 break;
11248 /* For an abstract declarator, we might wind up with nothing at this
11249 point. That's an error; the declarator is not optional. */
11250 if (!declarator)
11251 cp_parser_error (parser, "expected declarator");
11253 /* If we entered a scope, we must exit it now. */
11254 if (pushed_scope)
11255 pop_scope (pushed_scope);
11257 parser->default_arg_ok_p = saved_default_arg_ok_p;
11258 parser->in_declarator_p = saved_in_declarator_p;
11260 return declarator;
11263 /* Parse a ptr-operator.
11265 ptr-operator:
11266 * cv-qualifier-seq [opt]
11268 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11270 GNU Extension:
11272 ptr-operator:
11273 & cv-qualifier-seq [opt]
11275 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11276 Returns ADDR_EXPR if a reference was used. In the case of a
11277 pointer-to-member, *TYPE is filled in with the TYPE containing the
11278 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11279 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11280 ERROR_MARK if an error occurred. */
11282 static enum tree_code
11283 cp_parser_ptr_operator (cp_parser* parser,
11284 tree* type,
11285 cp_cv_quals *cv_quals)
11287 enum tree_code code = ERROR_MARK;
11288 cp_token *token;
11290 /* Assume that it's not a pointer-to-member. */
11291 *type = NULL_TREE;
11292 /* And that there are no cv-qualifiers. */
11293 *cv_quals = TYPE_UNQUALIFIED;
11295 /* Peek at the next token. */
11296 token = cp_lexer_peek_token (parser->lexer);
11297 /* If it's a `*' or `&' we have a pointer or reference. */
11298 if (token->type == CPP_MULT || token->type == CPP_AND)
11300 /* Remember which ptr-operator we were processing. */
11301 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11303 /* Consume the `*' or `&'. */
11304 cp_lexer_consume_token (parser->lexer);
11306 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11307 `&', if we are allowing GNU extensions. (The only qualifier
11308 that can legally appear after `&' is `restrict', but that is
11309 enforced during semantic analysis. */
11310 if (code == INDIRECT_REF
11311 || cp_parser_allow_gnu_extensions_p (parser))
11312 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11314 else
11316 /* Try the pointer-to-member case. */
11317 cp_parser_parse_tentatively (parser);
11318 /* Look for the optional `::' operator. */
11319 cp_parser_global_scope_opt (parser,
11320 /*current_scope_valid_p=*/false);
11321 /* Look for the nested-name specifier. */
11322 cp_parser_nested_name_specifier (parser,
11323 /*typename_keyword_p=*/false,
11324 /*check_dependency_p=*/true,
11325 /*type_p=*/false,
11326 /*is_declaration=*/false);
11327 /* If we found it, and the next token is a `*', then we are
11328 indeed looking at a pointer-to-member operator. */
11329 if (!cp_parser_error_occurred (parser)
11330 && cp_parser_require (parser, CPP_MULT, "`*'"))
11332 /* The type of which the member is a member is given by the
11333 current SCOPE. */
11334 *type = parser->scope;
11335 /* The next name will not be qualified. */
11336 parser->scope = NULL_TREE;
11337 parser->qualifying_scope = NULL_TREE;
11338 parser->object_scope = NULL_TREE;
11339 /* Indicate that the `*' operator was used. */
11340 code = INDIRECT_REF;
11341 /* Look for the optional cv-qualifier-seq. */
11342 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11344 /* If that didn't work we don't have a ptr-operator. */
11345 if (!cp_parser_parse_definitely (parser))
11346 cp_parser_error (parser, "expected ptr-operator");
11349 return code;
11352 /* Parse an (optional) cv-qualifier-seq.
11354 cv-qualifier-seq:
11355 cv-qualifier cv-qualifier-seq [opt]
11357 cv-qualifier:
11358 const
11359 volatile
11361 GNU Extension:
11363 cv-qualifier:
11364 __restrict__
11366 Returns a bitmask representing the cv-qualifiers. */
11368 static cp_cv_quals
11369 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11371 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11373 while (true)
11375 cp_token *token;
11376 cp_cv_quals cv_qualifier;
11378 /* Peek at the next token. */
11379 token = cp_lexer_peek_token (parser->lexer);
11380 /* See if it's a cv-qualifier. */
11381 switch (token->keyword)
11383 case RID_CONST:
11384 cv_qualifier = TYPE_QUAL_CONST;
11385 break;
11387 case RID_VOLATILE:
11388 cv_qualifier = TYPE_QUAL_VOLATILE;
11389 break;
11391 case RID_RESTRICT:
11392 cv_qualifier = TYPE_QUAL_RESTRICT;
11393 break;
11395 default:
11396 cv_qualifier = TYPE_UNQUALIFIED;
11397 break;
11400 if (!cv_qualifier)
11401 break;
11403 if (cv_quals & cv_qualifier)
11405 error ("duplicate cv-qualifier");
11406 cp_lexer_purge_token (parser->lexer);
11408 else
11410 cp_lexer_consume_token (parser->lexer);
11411 cv_quals |= cv_qualifier;
11415 return cv_quals;
11418 /* Parse a declarator-id.
11420 declarator-id:
11421 id-expression
11422 :: [opt] nested-name-specifier [opt] type-name
11424 In the `id-expression' case, the value returned is as for
11425 cp_parser_id_expression if the id-expression was an unqualified-id.
11426 If the id-expression was a qualified-id, then a SCOPE_REF is
11427 returned. The first operand is the scope (either a NAMESPACE_DECL
11428 or TREE_TYPE), but the second is still just a representation of an
11429 unqualified-id. */
11431 static tree
11432 cp_parser_declarator_id (cp_parser* parser)
11434 /* The expression must be an id-expression. Assume that qualified
11435 names are the names of types so that:
11437 template <class T>
11438 int S<T>::R::i = 3;
11440 will work; we must treat `S<T>::R' as the name of a type.
11441 Similarly, assume that qualified names are templates, where
11442 required, so that:
11444 template <class T>
11445 int S<T>::R<T>::i = 3;
11447 will work, too. */
11448 return cp_parser_id_expression (parser,
11449 /*template_keyword_p=*/false,
11450 /*check_dependency_p=*/false,
11451 /*template_p=*/NULL,
11452 /*declarator_p=*/true);
11455 /* Parse a type-id.
11457 type-id:
11458 type-specifier-seq abstract-declarator [opt]
11460 Returns the TYPE specified. */
11462 static tree
11463 cp_parser_type_id (cp_parser* parser)
11465 cp_decl_specifier_seq type_specifier_seq;
11466 cp_declarator *abstract_declarator;
11468 /* Parse the type-specifier-seq. */
11469 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11470 if (type_specifier_seq.type == error_mark_node)
11471 return error_mark_node;
11473 /* There might or might not be an abstract declarator. */
11474 cp_parser_parse_tentatively (parser);
11475 /* Look for the declarator. */
11476 abstract_declarator
11477 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11478 /*parenthesized_p=*/NULL,
11479 /*member_p=*/false);
11480 /* Check to see if there really was a declarator. */
11481 if (!cp_parser_parse_definitely (parser))
11482 abstract_declarator = NULL;
11484 return groktypename (&type_specifier_seq, abstract_declarator);
11487 /* Parse a type-specifier-seq.
11489 type-specifier-seq:
11490 type-specifier type-specifier-seq [opt]
11492 GNU extension:
11494 type-specifier-seq:
11495 attributes type-specifier-seq [opt]
11497 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11499 static void
11500 cp_parser_type_specifier_seq (cp_parser* parser,
11501 cp_decl_specifier_seq *type_specifier_seq)
11503 bool seen_type_specifier = false;
11505 /* Clear the TYPE_SPECIFIER_SEQ. */
11506 clear_decl_specs (type_specifier_seq);
11508 /* Parse the type-specifiers and attributes. */
11509 while (true)
11511 tree type_specifier;
11513 /* Check for attributes first. */
11514 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11516 type_specifier_seq->attributes =
11517 chainon (type_specifier_seq->attributes,
11518 cp_parser_attributes_opt (parser));
11519 continue;
11522 /* Look for the type-specifier. */
11523 type_specifier = cp_parser_type_specifier (parser,
11524 CP_PARSER_FLAGS_OPTIONAL,
11525 type_specifier_seq,
11526 /*is_declaration=*/false,
11527 NULL,
11528 NULL);
11529 /* If the first type-specifier could not be found, this is not a
11530 type-specifier-seq at all. */
11531 if (!seen_type_specifier && !type_specifier)
11533 cp_parser_error (parser, "expected type-specifier");
11534 type_specifier_seq->type = error_mark_node;
11535 return;
11537 /* If subsequent type-specifiers could not be found, the
11538 type-specifier-seq is complete. */
11539 else if (seen_type_specifier && !type_specifier)
11540 break;
11542 seen_type_specifier = true;
11545 return;
11548 /* Parse a parameter-declaration-clause.
11550 parameter-declaration-clause:
11551 parameter-declaration-list [opt] ... [opt]
11552 parameter-declaration-list , ...
11554 Returns a representation for the parameter declarations. A return
11555 value of NULL indicates a parameter-declaration-clause consisting
11556 only of an ellipsis. */
11558 static cp_parameter_declarator *
11559 cp_parser_parameter_declaration_clause (cp_parser* parser)
11561 cp_parameter_declarator *parameters;
11562 cp_token *token;
11563 bool ellipsis_p;
11564 bool is_error;
11566 /* Peek at the next token. */
11567 token = cp_lexer_peek_token (parser->lexer);
11568 /* Check for trivial parameter-declaration-clauses. */
11569 if (token->type == CPP_ELLIPSIS)
11571 /* Consume the `...' token. */
11572 cp_lexer_consume_token (parser->lexer);
11573 return NULL;
11575 else if (token->type == CPP_CLOSE_PAREN)
11576 /* There are no parameters. */
11578 #ifndef NO_IMPLICIT_EXTERN_C
11579 if (in_system_header && current_class_type == NULL
11580 && current_lang_name == lang_name_c)
11581 return NULL;
11582 else
11583 #endif
11584 return no_parameters;
11586 /* Check for `(void)', too, which is a special case. */
11587 else if (token->keyword == RID_VOID
11588 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11589 == CPP_CLOSE_PAREN))
11591 /* Consume the `void' token. */
11592 cp_lexer_consume_token (parser->lexer);
11593 /* There are no parameters. */
11594 return no_parameters;
11597 /* Parse the parameter-declaration-list. */
11598 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11599 /* If a parse error occurred while parsing the
11600 parameter-declaration-list, then the entire
11601 parameter-declaration-clause is erroneous. */
11602 if (is_error)
11603 return NULL;
11605 /* Peek at the next token. */
11606 token = cp_lexer_peek_token (parser->lexer);
11607 /* If it's a `,', the clause should terminate with an ellipsis. */
11608 if (token->type == CPP_COMMA)
11610 /* Consume the `,'. */
11611 cp_lexer_consume_token (parser->lexer);
11612 /* Expect an ellipsis. */
11613 ellipsis_p
11614 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11616 /* It might also be `...' if the optional trailing `,' was
11617 omitted. */
11618 else if (token->type == CPP_ELLIPSIS)
11620 /* Consume the `...' token. */
11621 cp_lexer_consume_token (parser->lexer);
11622 /* And remember that we saw it. */
11623 ellipsis_p = true;
11625 else
11626 ellipsis_p = false;
11628 /* Finish the parameter list. */
11629 if (parameters && ellipsis_p)
11630 parameters->ellipsis_p = true;
11632 return parameters;
11635 /* Parse a parameter-declaration-list.
11637 parameter-declaration-list:
11638 parameter-declaration
11639 parameter-declaration-list , parameter-declaration
11641 Returns a representation of the parameter-declaration-list, as for
11642 cp_parser_parameter_declaration_clause. However, the
11643 `void_list_node' is never appended to the list. Upon return,
11644 *IS_ERROR will be true iff an error occurred. */
11646 static cp_parameter_declarator *
11647 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11649 cp_parameter_declarator *parameters = NULL;
11650 cp_parameter_declarator **tail = &parameters;
11652 /* Assume all will go well. */
11653 *is_error = false;
11655 /* Look for more parameters. */
11656 while (true)
11658 cp_parameter_declarator *parameter;
11659 bool parenthesized_p;
11660 /* Parse the parameter. */
11661 parameter
11662 = cp_parser_parameter_declaration (parser,
11663 /*template_parm_p=*/false,
11664 &parenthesized_p);
11666 /* If a parse error occurred parsing the parameter declaration,
11667 then the entire parameter-declaration-list is erroneous. */
11668 if (!parameter)
11670 *is_error = true;
11671 parameters = NULL;
11672 break;
11674 /* Add the new parameter to the list. */
11675 *tail = parameter;
11676 tail = &parameter->next;
11678 /* Peek at the next token. */
11679 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11680 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11681 /* The parameter-declaration-list is complete. */
11682 break;
11683 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11685 cp_token *token;
11687 /* Peek at the next token. */
11688 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11689 /* If it's an ellipsis, then the list is complete. */
11690 if (token->type == CPP_ELLIPSIS)
11691 break;
11692 /* Otherwise, there must be more parameters. Consume the
11693 `,'. */
11694 cp_lexer_consume_token (parser->lexer);
11695 /* When parsing something like:
11697 int i(float f, double d)
11699 we can tell after seeing the declaration for "f" that we
11700 are not looking at an initialization of a variable "i",
11701 but rather at the declaration of a function "i".
11703 Due to the fact that the parsing of template arguments
11704 (as specified to a template-id) requires backtracking we
11705 cannot use this technique when inside a template argument
11706 list. */
11707 if (!parser->in_template_argument_list_p
11708 && !parser->in_type_id_in_expr_p
11709 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11710 /* However, a parameter-declaration of the form
11711 "foat(f)" (which is a valid declaration of a
11712 parameter "f") can also be interpreted as an
11713 expression (the conversion of "f" to "float"). */
11714 && !parenthesized_p)
11715 cp_parser_commit_to_tentative_parse (parser);
11717 else
11719 cp_parser_error (parser, "expected %<,%> or %<...%>");
11720 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11721 cp_parser_skip_to_closing_parenthesis (parser,
11722 /*recovering=*/true,
11723 /*or_comma=*/false,
11724 /*consume_paren=*/false);
11725 break;
11729 return parameters;
11732 /* Parse a parameter declaration.
11734 parameter-declaration:
11735 decl-specifier-seq declarator
11736 decl-specifier-seq declarator = assignment-expression
11737 decl-specifier-seq abstract-declarator [opt]
11738 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11740 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11741 declares a template parameter. (In that case, a non-nested `>'
11742 token encountered during the parsing of the assignment-expression
11743 is not interpreted as a greater-than operator.)
11745 Returns a representation of the parameter, or NULL if an error
11746 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11747 true iff the declarator is of the form "(p)". */
11749 static cp_parameter_declarator *
11750 cp_parser_parameter_declaration (cp_parser *parser,
11751 bool template_parm_p,
11752 bool *parenthesized_p)
11754 int declares_class_or_enum;
11755 bool greater_than_is_operator_p;
11756 cp_decl_specifier_seq decl_specifiers;
11757 cp_declarator *declarator;
11758 tree default_argument;
11759 cp_token *token;
11760 const char *saved_message;
11762 /* In a template parameter, `>' is not an operator.
11764 [temp.param]
11766 When parsing a default template-argument for a non-type
11767 template-parameter, the first non-nested `>' is taken as the end
11768 of the template parameter-list rather than a greater-than
11769 operator. */
11770 greater_than_is_operator_p = !template_parm_p;
11772 /* Type definitions may not appear in parameter types. */
11773 saved_message = parser->type_definition_forbidden_message;
11774 parser->type_definition_forbidden_message
11775 = "types may not be defined in parameter types";
11777 /* Parse the declaration-specifiers. */
11778 cp_parser_decl_specifier_seq (parser,
11779 CP_PARSER_FLAGS_NONE,
11780 &decl_specifiers,
11781 &declares_class_or_enum);
11782 /* If an error occurred, there's no reason to attempt to parse the
11783 rest of the declaration. */
11784 if (cp_parser_error_occurred (parser))
11786 parser->type_definition_forbidden_message = saved_message;
11787 return NULL;
11790 /* Peek at the next token. */
11791 token = cp_lexer_peek_token (parser->lexer);
11792 /* If the next token is a `)', `,', `=', `>', or `...', then there
11793 is no declarator. */
11794 if (token->type == CPP_CLOSE_PAREN
11795 || token->type == CPP_COMMA
11796 || token->type == CPP_EQ
11797 || token->type == CPP_ELLIPSIS
11798 || token->type == CPP_GREATER)
11800 declarator = NULL;
11801 if (parenthesized_p)
11802 *parenthesized_p = false;
11804 /* Otherwise, there should be a declarator. */
11805 else
11807 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11808 parser->default_arg_ok_p = false;
11810 /* After seeing a decl-specifier-seq, if the next token is not a
11811 "(", there is no possibility that the code is a valid
11812 expression. Therefore, if parsing tentatively, we commit at
11813 this point. */
11814 if (!parser->in_template_argument_list_p
11815 /* In an expression context, having seen:
11817 (int((char ...
11819 we cannot be sure whether we are looking at a
11820 function-type (taking a "char" as a parameter) or a cast
11821 of some object of type "char" to "int". */
11822 && !parser->in_type_id_in_expr_p
11823 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11824 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11825 cp_parser_commit_to_tentative_parse (parser);
11826 /* Parse the declarator. */
11827 declarator = cp_parser_declarator (parser,
11828 CP_PARSER_DECLARATOR_EITHER,
11829 /*ctor_dtor_or_conv_p=*/NULL,
11830 parenthesized_p,
11831 /*member_p=*/false);
11832 parser->default_arg_ok_p = saved_default_arg_ok_p;
11833 /* After the declarator, allow more attributes. */
11834 decl_specifiers.attributes
11835 = chainon (decl_specifiers.attributes,
11836 cp_parser_attributes_opt (parser));
11839 /* The restriction on defining new types applies only to the type
11840 of the parameter, not to the default argument. */
11841 parser->type_definition_forbidden_message = saved_message;
11843 /* If the next token is `=', then process a default argument. */
11844 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11846 bool saved_greater_than_is_operator_p;
11847 /* Consume the `='. */
11848 cp_lexer_consume_token (parser->lexer);
11850 /* If we are defining a class, then the tokens that make up the
11851 default argument must be saved and processed later. */
11852 if (!template_parm_p && at_class_scope_p ()
11853 && TYPE_BEING_DEFINED (current_class_type))
11855 unsigned depth = 0;
11856 cp_token *first_token;
11857 cp_token *token;
11859 /* Add tokens until we have processed the entire default
11860 argument. We add the range [first_token, token). */
11861 first_token = cp_lexer_peek_token (parser->lexer);
11862 while (true)
11864 bool done = false;
11866 /* Peek at the next token. */
11867 token = cp_lexer_peek_token (parser->lexer);
11868 /* What we do depends on what token we have. */
11869 switch (token->type)
11871 /* In valid code, a default argument must be
11872 immediately followed by a `,' `)', or `...'. */
11873 case CPP_COMMA:
11874 case CPP_CLOSE_PAREN:
11875 case CPP_ELLIPSIS:
11876 /* If we run into a non-nested `;', `}', or `]',
11877 then the code is invalid -- but the default
11878 argument is certainly over. */
11879 case CPP_SEMICOLON:
11880 case CPP_CLOSE_BRACE:
11881 case CPP_CLOSE_SQUARE:
11882 if (depth == 0)
11883 done = true;
11884 /* Update DEPTH, if necessary. */
11885 else if (token->type == CPP_CLOSE_PAREN
11886 || token->type == CPP_CLOSE_BRACE
11887 || token->type == CPP_CLOSE_SQUARE)
11888 --depth;
11889 break;
11891 case CPP_OPEN_PAREN:
11892 case CPP_OPEN_SQUARE:
11893 case CPP_OPEN_BRACE:
11894 ++depth;
11895 break;
11897 case CPP_GREATER:
11898 /* If we see a non-nested `>', and `>' is not an
11899 operator, then it marks the end of the default
11900 argument. */
11901 if (!depth && !greater_than_is_operator_p)
11902 done = true;
11903 break;
11905 /* If we run out of tokens, issue an error message. */
11906 case CPP_EOF:
11907 error ("file ends in default argument");
11908 done = true;
11909 break;
11911 case CPP_NAME:
11912 case CPP_SCOPE:
11913 /* In these cases, we should look for template-ids.
11914 For example, if the default argument is
11915 `X<int, double>()', we need to do name lookup to
11916 figure out whether or not `X' is a template; if
11917 so, the `,' does not end the default argument.
11919 That is not yet done. */
11920 break;
11922 default:
11923 break;
11926 /* If we've reached the end, stop. */
11927 if (done)
11928 break;
11930 /* Add the token to the token block. */
11931 token = cp_lexer_consume_token (parser->lexer);
11934 /* Create a DEFAULT_ARG to represented the unparsed default
11935 argument. */
11936 default_argument = make_node (DEFAULT_ARG);
11937 DEFARG_TOKENS (default_argument)
11938 = cp_token_cache_new (first_token, token);
11940 /* Outside of a class definition, we can just parse the
11941 assignment-expression. */
11942 else
11944 bool saved_local_variables_forbidden_p;
11946 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11947 set correctly. */
11948 saved_greater_than_is_operator_p
11949 = parser->greater_than_is_operator_p;
11950 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11951 /* Local variable names (and the `this' keyword) may not
11952 appear in a default argument. */
11953 saved_local_variables_forbidden_p
11954 = parser->local_variables_forbidden_p;
11955 parser->local_variables_forbidden_p = true;
11956 /* Parse the assignment-expression. */
11957 default_argument
11958 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
11959 /* Restore saved state. */
11960 parser->greater_than_is_operator_p
11961 = saved_greater_than_is_operator_p;
11962 parser->local_variables_forbidden_p
11963 = saved_local_variables_forbidden_p;
11965 if (!parser->default_arg_ok_p)
11967 if (!flag_pedantic_errors)
11968 warning ("deprecated use of default argument for parameter of non-function");
11969 else
11971 error ("default arguments are only permitted for function parameters");
11972 default_argument = NULL_TREE;
11976 else
11977 default_argument = NULL_TREE;
11979 return make_parameter_declarator (&decl_specifiers,
11980 declarator,
11981 default_argument);
11984 /* Parse a function-body.
11986 function-body:
11987 compound_statement */
11989 static void
11990 cp_parser_function_body (cp_parser *parser)
11992 cp_parser_compound_statement (parser, NULL, false);
11995 /* Parse a ctor-initializer-opt followed by a function-body. Return
11996 true if a ctor-initializer was present. */
11998 static bool
11999 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12001 tree body;
12002 bool ctor_initializer_p;
12004 /* Begin the function body. */
12005 body = begin_function_body ();
12006 /* Parse the optional ctor-initializer. */
12007 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12008 /* Parse the function-body. */
12009 cp_parser_function_body (parser);
12010 /* Finish the function body. */
12011 finish_function_body (body);
12013 return ctor_initializer_p;
12016 /* Parse an initializer.
12018 initializer:
12019 = initializer-clause
12020 ( expression-list )
12022 Returns a expression representing the initializer. If no
12023 initializer is present, NULL_TREE is returned.
12025 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12026 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12027 set to FALSE if there is no initializer present. If there is an
12028 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12029 is set to true; otherwise it is set to false. */
12031 static tree
12032 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12033 bool* non_constant_p)
12035 cp_token *token;
12036 tree init;
12038 /* Peek at the next token. */
12039 token = cp_lexer_peek_token (parser->lexer);
12041 /* Let our caller know whether or not this initializer was
12042 parenthesized. */
12043 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12044 /* Assume that the initializer is constant. */
12045 *non_constant_p = false;
12047 if (token->type == CPP_EQ)
12049 /* Consume the `='. */
12050 cp_lexer_consume_token (parser->lexer);
12051 /* Parse the initializer-clause. */
12052 init = cp_parser_initializer_clause (parser, non_constant_p);
12054 else if (token->type == CPP_OPEN_PAREN)
12055 init = cp_parser_parenthesized_expression_list (parser, false,
12056 /*cast_p=*/false,
12057 non_constant_p);
12058 else
12060 /* Anything else is an error. */
12061 cp_parser_error (parser, "expected initializer");
12062 init = error_mark_node;
12065 return init;
12068 /* Parse an initializer-clause.
12070 initializer-clause:
12071 assignment-expression
12072 { initializer-list , [opt] }
12075 Returns an expression representing the initializer.
12077 If the `assignment-expression' production is used the value
12078 returned is simply a representation for the expression.
12080 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12081 the elements of the initializer-list (or NULL_TREE, if the last
12082 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12083 NULL_TREE. There is no way to detect whether or not the optional
12084 trailing `,' was provided. NON_CONSTANT_P is as for
12085 cp_parser_initializer. */
12087 static tree
12088 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12090 tree initializer;
12092 /* Assume the expression is constant. */
12093 *non_constant_p = false;
12095 /* If it is not a `{', then we are looking at an
12096 assignment-expression. */
12097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12099 initializer
12100 = cp_parser_constant_expression (parser,
12101 /*allow_non_constant_p=*/true,
12102 non_constant_p);
12103 if (!*non_constant_p)
12104 initializer = fold_non_dependent_expr (initializer);
12106 else
12108 /* Consume the `{' token. */
12109 cp_lexer_consume_token (parser->lexer);
12110 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12111 initializer = make_node (CONSTRUCTOR);
12112 /* If it's not a `}', then there is a non-trivial initializer. */
12113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12115 /* Parse the initializer list. */
12116 CONSTRUCTOR_ELTS (initializer)
12117 = cp_parser_initializer_list (parser, non_constant_p);
12118 /* A trailing `,' token is allowed. */
12119 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12120 cp_lexer_consume_token (parser->lexer);
12122 /* Now, there should be a trailing `}'. */
12123 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12126 return initializer;
12129 /* Parse an initializer-list.
12131 initializer-list:
12132 initializer-clause
12133 initializer-list , initializer-clause
12135 GNU Extension:
12137 initializer-list:
12138 identifier : initializer-clause
12139 initializer-list, identifier : initializer-clause
12141 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12142 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12143 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12144 as for cp_parser_initializer. */
12146 static tree
12147 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12149 tree initializers = NULL_TREE;
12151 /* Assume all of the expressions are constant. */
12152 *non_constant_p = false;
12154 /* Parse the rest of the list. */
12155 while (true)
12157 cp_token *token;
12158 tree identifier;
12159 tree initializer;
12160 bool clause_non_constant_p;
12162 /* If the next token is an identifier and the following one is a
12163 colon, we are looking at the GNU designated-initializer
12164 syntax. */
12165 if (cp_parser_allow_gnu_extensions_p (parser)
12166 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12167 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12169 /* Consume the identifier. */
12170 identifier = cp_lexer_consume_token (parser->lexer)->value;
12171 /* Consume the `:'. */
12172 cp_lexer_consume_token (parser->lexer);
12174 else
12175 identifier = NULL_TREE;
12177 /* Parse the initializer. */
12178 initializer = cp_parser_initializer_clause (parser,
12179 &clause_non_constant_p);
12180 /* If any clause is non-constant, so is the entire initializer. */
12181 if (clause_non_constant_p)
12182 *non_constant_p = true;
12183 /* Add it to the list. */
12184 initializers = tree_cons (identifier, initializer, initializers);
12186 /* If the next token is not a comma, we have reached the end of
12187 the list. */
12188 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12189 break;
12191 /* Peek at the next token. */
12192 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12193 /* If the next token is a `}', then we're still done. An
12194 initializer-clause can have a trailing `,' after the
12195 initializer-list and before the closing `}'. */
12196 if (token->type == CPP_CLOSE_BRACE)
12197 break;
12199 /* Consume the `,' token. */
12200 cp_lexer_consume_token (parser->lexer);
12203 /* The initializers were built up in reverse order, so we need to
12204 reverse them now. */
12205 return nreverse (initializers);
12208 /* Classes [gram.class] */
12210 /* Parse a class-name.
12212 class-name:
12213 identifier
12214 template-id
12216 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12217 to indicate that names looked up in dependent types should be
12218 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12219 keyword has been used to indicate that the name that appears next
12220 is a template. TAG_TYPE indicates the explicit tag given before
12221 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12222 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12223 is the class being defined in a class-head.
12225 Returns the TYPE_DECL representing the class. */
12227 static tree
12228 cp_parser_class_name (cp_parser *parser,
12229 bool typename_keyword_p,
12230 bool template_keyword_p,
12231 enum tag_types tag_type,
12232 bool check_dependency_p,
12233 bool class_head_p,
12234 bool is_declaration)
12236 tree decl;
12237 tree scope;
12238 bool typename_p;
12239 cp_token *token;
12241 /* All class-names start with an identifier. */
12242 token = cp_lexer_peek_token (parser->lexer);
12243 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12245 cp_parser_error (parser, "expected class-name");
12246 return error_mark_node;
12249 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12250 to a template-id, so we save it here. */
12251 scope = parser->scope;
12252 if (scope == error_mark_node)
12253 return error_mark_node;
12255 /* Any name names a type if we're following the `typename' keyword
12256 in a qualified name where the enclosing scope is type-dependent. */
12257 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12258 && dependent_type_p (scope));
12259 /* Handle the common case (an identifier, but not a template-id)
12260 efficiently. */
12261 if (token->type == CPP_NAME
12262 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12264 tree identifier;
12266 /* Look for the identifier. */
12267 identifier = cp_parser_identifier (parser);
12268 /* If the next token isn't an identifier, we are certainly not
12269 looking at a class-name. */
12270 if (identifier == error_mark_node)
12271 decl = error_mark_node;
12272 /* If we know this is a type-name, there's no need to look it
12273 up. */
12274 else if (typename_p)
12275 decl = identifier;
12276 else
12278 /* If the next token is a `::', then the name must be a type
12279 name.
12281 [basic.lookup.qual]
12283 During the lookup for a name preceding the :: scope
12284 resolution operator, object, function, and enumerator
12285 names are ignored. */
12286 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12287 tag_type = typename_type;
12288 /* Look up the name. */
12289 decl = cp_parser_lookup_name (parser, identifier,
12290 tag_type,
12291 /*is_template=*/false,
12292 /*is_namespace=*/false,
12293 check_dependency_p,
12294 /*ambiguous_p=*/NULL);
12297 else
12299 /* Try a template-id. */
12300 decl = cp_parser_template_id (parser, template_keyword_p,
12301 check_dependency_p,
12302 is_declaration);
12303 if (decl == error_mark_node)
12304 return error_mark_node;
12307 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12309 /* If this is a typename, create a TYPENAME_TYPE. */
12310 if (typename_p && decl != error_mark_node)
12312 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12313 if (decl != error_mark_node)
12314 decl = TYPE_NAME (decl);
12317 /* Check to see that it is really the name of a class. */
12318 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12319 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12320 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12321 /* Situations like this:
12323 template <typename T> struct A {
12324 typename T::template X<int>::I i;
12327 are problematic. Is `T::template X<int>' a class-name? The
12328 standard does not seem to be definitive, but there is no other
12329 valid interpretation of the following `::'. Therefore, those
12330 names are considered class-names. */
12331 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12332 else if (decl == error_mark_node
12333 || TREE_CODE (decl) != TYPE_DECL
12334 || TREE_TYPE (decl) == error_mark_node
12335 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12337 cp_parser_error (parser, "expected class-name");
12338 return error_mark_node;
12341 return decl;
12344 /* Parse a class-specifier.
12346 class-specifier:
12347 class-head { member-specification [opt] }
12349 Returns the TREE_TYPE representing the class. */
12351 static tree
12352 cp_parser_class_specifier (cp_parser* parser)
12354 cp_token *token;
12355 tree type;
12356 tree attributes = NULL_TREE;
12357 int has_trailing_semicolon;
12358 bool nested_name_specifier_p;
12359 unsigned saved_num_template_parameter_lists;
12360 tree old_scope = NULL_TREE;
12361 tree scope = NULL_TREE;
12363 push_deferring_access_checks (dk_no_deferred);
12365 /* Parse the class-head. */
12366 type = cp_parser_class_head (parser,
12367 &nested_name_specifier_p,
12368 &attributes);
12369 /* If the class-head was a semantic disaster, skip the entire body
12370 of the class. */
12371 if (!type)
12373 cp_parser_skip_to_end_of_block_or_statement (parser);
12374 pop_deferring_access_checks ();
12375 return error_mark_node;
12378 /* Look for the `{'. */
12379 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12381 pop_deferring_access_checks ();
12382 return error_mark_node;
12385 /* Issue an error message if type-definitions are forbidden here. */
12386 cp_parser_check_type_definition (parser);
12387 /* Remember that we are defining one more class. */
12388 ++parser->num_classes_being_defined;
12389 /* Inside the class, surrounding template-parameter-lists do not
12390 apply. */
12391 saved_num_template_parameter_lists
12392 = parser->num_template_parameter_lists;
12393 parser->num_template_parameter_lists = 0;
12395 /* Start the class. */
12396 if (nested_name_specifier_p)
12398 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12399 old_scope = push_inner_scope (scope);
12401 type = begin_class_definition (type);
12403 if (type == error_mark_node)
12404 /* If the type is erroneous, skip the entire body of the class. */
12405 cp_parser_skip_to_closing_brace (parser);
12406 else
12407 /* Parse the member-specification. */
12408 cp_parser_member_specification_opt (parser);
12410 /* Look for the trailing `}'. */
12411 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12412 /* We get better error messages by noticing a common problem: a
12413 missing trailing `;'. */
12414 token = cp_lexer_peek_token (parser->lexer);
12415 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12416 /* Look for trailing attributes to apply to this class. */
12417 if (cp_parser_allow_gnu_extensions_p (parser))
12419 tree sub_attr = cp_parser_attributes_opt (parser);
12420 attributes = chainon (attributes, sub_attr);
12422 if (type != error_mark_node)
12423 type = finish_struct (type, attributes);
12424 if (nested_name_specifier_p)
12425 pop_inner_scope (old_scope, scope);
12426 /* If this class is not itself within the scope of another class,
12427 then we need to parse the bodies of all of the queued function
12428 definitions. Note that the queued functions defined in a class
12429 are not always processed immediately following the
12430 class-specifier for that class. Consider:
12432 struct A {
12433 struct B { void f() { sizeof (A); } };
12436 If `f' were processed before the processing of `A' were
12437 completed, there would be no way to compute the size of `A'.
12438 Note that the nesting we are interested in here is lexical --
12439 not the semantic nesting given by TYPE_CONTEXT. In particular,
12440 for:
12442 struct A { struct B; };
12443 struct A::B { void f() { } };
12445 there is no need to delay the parsing of `A::B::f'. */
12446 if (--parser->num_classes_being_defined == 0)
12448 tree queue_entry;
12449 tree fn;
12450 tree class_type = NULL_TREE;
12451 tree pushed_scope = NULL_TREE;
12453 /* In a first pass, parse default arguments to the functions.
12454 Then, in a second pass, parse the bodies of the functions.
12455 This two-phased approach handles cases like:
12457 struct S {
12458 void f() { g(); }
12459 void g(int i = 3);
12463 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12464 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12465 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12466 TREE_PURPOSE (parser->unparsed_functions_queues)
12467 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12469 fn = TREE_VALUE (queue_entry);
12470 /* If there are default arguments that have not yet been processed,
12471 take care of them now. */
12472 if (class_type != TREE_PURPOSE (queue_entry))
12474 if (pushed_scope)
12475 pop_scope (pushed_scope);
12476 class_type = TREE_PURPOSE (queue_entry);
12477 pushed_scope = push_scope (class_type);
12479 /* Make sure that any template parameters are in scope. */
12480 maybe_begin_member_template_processing (fn);
12481 /* Parse the default argument expressions. */
12482 cp_parser_late_parsing_default_args (parser, fn);
12483 /* Remove any template parameters from the symbol table. */
12484 maybe_end_member_template_processing ();
12486 if (pushed_scope)
12487 pop_scope (pushed_scope);
12488 /* Now parse the body of the functions. */
12489 for (TREE_VALUE (parser->unparsed_functions_queues)
12490 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12491 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12492 TREE_VALUE (parser->unparsed_functions_queues)
12493 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12495 /* Figure out which function we need to process. */
12496 fn = TREE_VALUE (queue_entry);
12498 /* A hack to prevent garbage collection. */
12499 function_depth++;
12501 /* Parse the function. */
12502 cp_parser_late_parsing_for_member (parser, fn);
12503 function_depth--;
12507 /* Put back any saved access checks. */
12508 pop_deferring_access_checks ();
12510 /* Restore the count of active template-parameter-lists. */
12511 parser->num_template_parameter_lists
12512 = saved_num_template_parameter_lists;
12514 return type;
12517 /* Parse a class-head.
12519 class-head:
12520 class-key identifier [opt] base-clause [opt]
12521 class-key nested-name-specifier identifier base-clause [opt]
12522 class-key nested-name-specifier [opt] template-id
12523 base-clause [opt]
12525 GNU Extensions:
12526 class-key attributes identifier [opt] base-clause [opt]
12527 class-key attributes nested-name-specifier identifier base-clause [opt]
12528 class-key attributes nested-name-specifier [opt] template-id
12529 base-clause [opt]
12531 Returns the TYPE of the indicated class. Sets
12532 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12533 involving a nested-name-specifier was used, and FALSE otherwise.
12535 Returns error_mark_node if this is not a class-head.
12537 Returns NULL_TREE if the class-head is syntactically valid, but
12538 semantically invalid in a way that means we should skip the entire
12539 body of the class. */
12541 static tree
12542 cp_parser_class_head (cp_parser* parser,
12543 bool* nested_name_specifier_p,
12544 tree *attributes_p)
12546 tree nested_name_specifier;
12547 enum tag_types class_key;
12548 tree id = NULL_TREE;
12549 tree type = NULL_TREE;
12550 tree attributes;
12551 bool template_id_p = false;
12552 bool qualified_p = false;
12553 bool invalid_nested_name_p = false;
12554 bool invalid_explicit_specialization_p = false;
12555 tree pushed_scope = NULL_TREE;
12556 unsigned num_templates;
12557 tree bases;
12559 /* Assume no nested-name-specifier will be present. */
12560 *nested_name_specifier_p = false;
12561 /* Assume no template parameter lists will be used in defining the
12562 type. */
12563 num_templates = 0;
12565 /* Look for the class-key. */
12566 class_key = cp_parser_class_key (parser);
12567 if (class_key == none_type)
12568 return error_mark_node;
12570 /* Parse the attributes. */
12571 attributes = cp_parser_attributes_opt (parser);
12573 /* If the next token is `::', that is invalid -- but sometimes
12574 people do try to write:
12576 struct ::S {};
12578 Handle this gracefully by accepting the extra qualifier, and then
12579 issuing an error about it later if this really is a
12580 class-head. If it turns out just to be an elaborated type
12581 specifier, remain silent. */
12582 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12583 qualified_p = true;
12585 push_deferring_access_checks (dk_no_check);
12587 /* Determine the name of the class. Begin by looking for an
12588 optional nested-name-specifier. */
12589 nested_name_specifier
12590 = cp_parser_nested_name_specifier_opt (parser,
12591 /*typename_keyword_p=*/false,
12592 /*check_dependency_p=*/false,
12593 /*type_p=*/false,
12594 /*is_declaration=*/false);
12595 /* If there was a nested-name-specifier, then there *must* be an
12596 identifier. */
12597 if (nested_name_specifier)
12599 /* Although the grammar says `identifier', it really means
12600 `class-name' or `template-name'. You are only allowed to
12601 define a class that has already been declared with this
12602 syntax.
12604 The proposed resolution for Core Issue 180 says that whever
12605 you see `class T::X' you should treat `X' as a type-name.
12607 It is OK to define an inaccessible class; for example:
12609 class A { class B; };
12610 class A::B {};
12612 We do not know if we will see a class-name, or a
12613 template-name. We look for a class-name first, in case the
12614 class-name is a template-id; if we looked for the
12615 template-name first we would stop after the template-name. */
12616 cp_parser_parse_tentatively (parser);
12617 type = cp_parser_class_name (parser,
12618 /*typename_keyword_p=*/false,
12619 /*template_keyword_p=*/false,
12620 class_type,
12621 /*check_dependency_p=*/false,
12622 /*class_head_p=*/true,
12623 /*is_declaration=*/false);
12624 /* If that didn't work, ignore the nested-name-specifier. */
12625 if (!cp_parser_parse_definitely (parser))
12627 invalid_nested_name_p = true;
12628 id = cp_parser_identifier (parser);
12629 if (id == error_mark_node)
12630 id = NULL_TREE;
12632 /* If we could not find a corresponding TYPE, treat this
12633 declaration like an unqualified declaration. */
12634 if (type == error_mark_node)
12635 nested_name_specifier = NULL_TREE;
12636 /* Otherwise, count the number of templates used in TYPE and its
12637 containing scopes. */
12638 else
12640 tree scope;
12642 for (scope = TREE_TYPE (type);
12643 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12644 scope = (TYPE_P (scope)
12645 ? TYPE_CONTEXT (scope)
12646 : DECL_CONTEXT (scope)))
12647 if (TYPE_P (scope)
12648 && CLASS_TYPE_P (scope)
12649 && CLASSTYPE_TEMPLATE_INFO (scope)
12650 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12651 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12652 ++num_templates;
12655 /* Otherwise, the identifier is optional. */
12656 else
12658 /* We don't know whether what comes next is a template-id,
12659 an identifier, or nothing at all. */
12660 cp_parser_parse_tentatively (parser);
12661 /* Check for a template-id. */
12662 id = cp_parser_template_id (parser,
12663 /*template_keyword_p=*/false,
12664 /*check_dependency_p=*/true,
12665 /*is_declaration=*/true);
12666 /* If that didn't work, it could still be an identifier. */
12667 if (!cp_parser_parse_definitely (parser))
12669 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12670 id = cp_parser_identifier (parser);
12671 else
12672 id = NULL_TREE;
12674 else
12676 template_id_p = true;
12677 ++num_templates;
12681 pop_deferring_access_checks ();
12683 if (id)
12684 cp_parser_check_for_invalid_template_id (parser, id);
12686 /* If it's not a `:' or a `{' then we can't really be looking at a
12687 class-head, since a class-head only appears as part of a
12688 class-specifier. We have to detect this situation before calling
12689 xref_tag, since that has irreversible side-effects. */
12690 if (!cp_parser_next_token_starts_class_definition_p (parser))
12692 cp_parser_error (parser, "expected %<{%> or %<:%>");
12693 return error_mark_node;
12696 /* At this point, we're going ahead with the class-specifier, even
12697 if some other problem occurs. */
12698 cp_parser_commit_to_tentative_parse (parser);
12699 /* Issue the error about the overly-qualified name now. */
12700 if (qualified_p)
12701 cp_parser_error (parser,
12702 "global qualification of class name is invalid");
12703 else if (invalid_nested_name_p)
12704 cp_parser_error (parser,
12705 "qualified name does not name a class");
12706 else if (nested_name_specifier)
12708 tree scope;
12710 /* Reject typedef-names in class heads. */
12711 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12713 error ("invalid class name in declaration of %qD", type);
12714 type = NULL_TREE;
12715 goto done;
12718 /* Figure out in what scope the declaration is being placed. */
12719 scope = current_scope ();
12720 /* If that scope does not contain the scope in which the
12721 class was originally declared, the program is invalid. */
12722 if (scope && !is_ancestor (scope, nested_name_specifier))
12724 error ("declaration of %qD in %qD which does not enclose %qD",
12725 type, scope, nested_name_specifier);
12726 type = NULL_TREE;
12727 goto done;
12729 /* [dcl.meaning]
12731 A declarator-id shall not be qualified exception of the
12732 definition of a ... nested class outside of its class
12733 ... [or] a the definition or explicit instantiation of a
12734 class member of a namespace outside of its namespace. */
12735 if (scope == nested_name_specifier)
12737 pedwarn ("extra qualification ignored");
12738 nested_name_specifier = NULL_TREE;
12739 num_templates = 0;
12742 /* An explicit-specialization must be preceded by "template <>". If
12743 it is not, try to recover gracefully. */
12744 if (at_namespace_scope_p ()
12745 && parser->num_template_parameter_lists == 0
12746 && template_id_p)
12748 error ("an explicit specialization must be preceded by %<template <>%>");
12749 invalid_explicit_specialization_p = true;
12750 /* Take the same action that would have been taken by
12751 cp_parser_explicit_specialization. */
12752 ++parser->num_template_parameter_lists;
12753 begin_specialization ();
12755 /* There must be no "return" statements between this point and the
12756 end of this function; set "type "to the correct return value and
12757 use "goto done;" to return. */
12758 /* Make sure that the right number of template parameters were
12759 present. */
12760 if (!cp_parser_check_template_parameters (parser, num_templates))
12762 /* If something went wrong, there is no point in even trying to
12763 process the class-definition. */
12764 type = NULL_TREE;
12765 goto done;
12768 /* Look up the type. */
12769 if (template_id_p)
12771 type = TREE_TYPE (id);
12772 maybe_process_partial_specialization (type);
12773 if (nested_name_specifier)
12774 pushed_scope = push_scope (nested_name_specifier);
12776 else if (nested_name_specifier)
12778 tree class_type;
12780 /* Given:
12782 template <typename T> struct S { struct T };
12783 template <typename T> struct S<T>::T { };
12785 we will get a TYPENAME_TYPE when processing the definition of
12786 `S::T'. We need to resolve it to the actual type before we
12787 try to define it. */
12788 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12790 class_type = resolve_typename_type (TREE_TYPE (type),
12791 /*only_current_p=*/false);
12792 if (class_type != error_mark_node)
12793 type = TYPE_NAME (class_type);
12794 else
12796 cp_parser_error (parser, "could not resolve typename type");
12797 type = error_mark_node;
12801 maybe_process_partial_specialization (TREE_TYPE (type));
12802 class_type = current_class_type;
12803 /* Enter the scope indicated by the nested-name-specifier. */
12804 pushed_scope = push_scope (nested_name_specifier);
12805 /* Get the canonical version of this type. */
12806 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12807 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12808 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12810 type = push_template_decl (type);
12811 if (type == error_mark_node)
12813 type = NULL_TREE;
12814 goto done;
12818 type = TREE_TYPE (type);
12819 *nested_name_specifier_p = true;
12821 else /* The name is not a nested name. */
12823 /* If the class was unnamed, create a dummy name. */
12824 if (!id)
12825 id = make_anon_name ();
12826 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12827 parser->num_template_parameter_lists);
12830 /* Indicate whether this class was declared as a `class' or as a
12831 `struct'. */
12832 if (TREE_CODE (type) == RECORD_TYPE)
12833 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12834 cp_parser_check_class_key (class_key, type);
12836 /* We will have entered the scope containing the class; the names of
12837 base classes should be looked up in that context. For example,
12838 given:
12840 struct A { struct B {}; struct C; };
12841 struct A::C : B {};
12843 is valid. */
12844 bases = NULL_TREE;
12846 /* Get the list of base-classes, if there is one. */
12847 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12848 bases = cp_parser_base_clause (parser);
12850 /* Process the base classes. */
12851 xref_basetypes (type, bases);
12853 done:
12854 /* Leave the scope given by the nested-name-specifier. We will
12855 enter the class scope itself while processing the members. */
12856 if (pushed_scope)
12857 pop_scope (pushed_scope);
12859 if (invalid_explicit_specialization_p)
12861 end_specialization ();
12862 --parser->num_template_parameter_lists;
12864 *attributes_p = attributes;
12865 return type;
12868 /* Parse a class-key.
12870 class-key:
12871 class
12872 struct
12873 union
12875 Returns the kind of class-key specified, or none_type to indicate
12876 error. */
12878 static enum tag_types
12879 cp_parser_class_key (cp_parser* parser)
12881 cp_token *token;
12882 enum tag_types tag_type;
12884 /* Look for the class-key. */
12885 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12886 if (!token)
12887 return none_type;
12889 /* Check to see if the TOKEN is a class-key. */
12890 tag_type = cp_parser_token_is_class_key (token);
12891 if (!tag_type)
12892 cp_parser_error (parser, "expected class-key");
12893 return tag_type;
12896 /* Parse an (optional) member-specification.
12898 member-specification:
12899 member-declaration member-specification [opt]
12900 access-specifier : member-specification [opt] */
12902 static void
12903 cp_parser_member_specification_opt (cp_parser* parser)
12905 while (true)
12907 cp_token *token;
12908 enum rid keyword;
12910 /* Peek at the next token. */
12911 token = cp_lexer_peek_token (parser->lexer);
12912 /* If it's a `}', or EOF then we've seen all the members. */
12913 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12914 break;
12916 /* See if this token is a keyword. */
12917 keyword = token->keyword;
12918 switch (keyword)
12920 case RID_PUBLIC:
12921 case RID_PROTECTED:
12922 case RID_PRIVATE:
12923 /* Consume the access-specifier. */
12924 cp_lexer_consume_token (parser->lexer);
12925 /* Remember which access-specifier is active. */
12926 current_access_specifier = token->value;
12927 /* Look for the `:'. */
12928 cp_parser_require (parser, CPP_COLON, "`:'");
12929 break;
12931 default:
12932 /* Accept #pragmas at class scope. */
12933 if (token->type == CPP_PRAGMA)
12935 cp_lexer_handle_pragma (parser->lexer);
12936 break;
12939 /* Otherwise, the next construction must be a
12940 member-declaration. */
12941 cp_parser_member_declaration (parser);
12946 /* Parse a member-declaration.
12948 member-declaration:
12949 decl-specifier-seq [opt] member-declarator-list [opt] ;
12950 function-definition ; [opt]
12951 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12952 using-declaration
12953 template-declaration
12955 member-declarator-list:
12956 member-declarator
12957 member-declarator-list , member-declarator
12959 member-declarator:
12960 declarator pure-specifier [opt]
12961 declarator constant-initializer [opt]
12962 identifier [opt] : constant-expression
12964 GNU Extensions:
12966 member-declaration:
12967 __extension__ member-declaration
12969 member-declarator:
12970 declarator attributes [opt] pure-specifier [opt]
12971 declarator attributes [opt] constant-initializer [opt]
12972 identifier [opt] attributes [opt] : constant-expression */
12974 static void
12975 cp_parser_member_declaration (cp_parser* parser)
12977 cp_decl_specifier_seq decl_specifiers;
12978 tree prefix_attributes;
12979 tree decl;
12980 int declares_class_or_enum;
12981 bool friend_p;
12982 cp_token *token;
12983 int saved_pedantic;
12985 /* Check for the `__extension__' keyword. */
12986 if (cp_parser_extension_opt (parser, &saved_pedantic))
12988 /* Recurse. */
12989 cp_parser_member_declaration (parser);
12990 /* Restore the old value of the PEDANTIC flag. */
12991 pedantic = saved_pedantic;
12993 return;
12996 /* Check for a template-declaration. */
12997 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12999 /* Parse the template-declaration. */
13000 cp_parser_template_declaration (parser, /*member_p=*/true);
13002 return;
13005 /* Check for a using-declaration. */
13006 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13008 /* Parse the using-declaration. */
13009 cp_parser_using_declaration (parser);
13011 return;
13014 /* Parse the decl-specifier-seq. */
13015 cp_parser_decl_specifier_seq (parser,
13016 CP_PARSER_FLAGS_OPTIONAL,
13017 &decl_specifiers,
13018 &declares_class_or_enum);
13019 prefix_attributes = decl_specifiers.attributes;
13020 decl_specifiers.attributes = NULL_TREE;
13021 /* Check for an invalid type-name. */
13022 if (!decl_specifiers.type
13023 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13024 return;
13025 /* If there is no declarator, then the decl-specifier-seq should
13026 specify a type. */
13027 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13029 /* If there was no decl-specifier-seq, and the next token is a
13030 `;', then we have something like:
13032 struct S { ; };
13034 [class.mem]
13036 Each member-declaration shall declare at least one member
13037 name of the class. */
13038 if (!decl_specifiers.any_specifiers_p)
13040 cp_token *token = cp_lexer_peek_token (parser->lexer);
13041 if (pedantic && !token->in_system_header)
13042 pedwarn ("%Hextra %<;%>", &token->location);
13044 else
13046 tree type;
13048 /* See if this declaration is a friend. */
13049 friend_p = cp_parser_friend_p (&decl_specifiers);
13050 /* If there were decl-specifiers, check to see if there was
13051 a class-declaration. */
13052 type = check_tag_decl (&decl_specifiers);
13053 /* Nested classes have already been added to the class, but
13054 a `friend' needs to be explicitly registered. */
13055 if (friend_p)
13057 /* If the `friend' keyword was present, the friend must
13058 be introduced with a class-key. */
13059 if (!declares_class_or_enum)
13060 error ("a class-key must be used when declaring a friend");
13061 /* In this case:
13063 template <typename T> struct A {
13064 friend struct A<T>::B;
13067 A<T>::B will be represented by a TYPENAME_TYPE, and
13068 therefore not recognized by check_tag_decl. */
13069 if (!type
13070 && decl_specifiers.type
13071 && TYPE_P (decl_specifiers.type))
13072 type = decl_specifiers.type;
13073 if (!type || !TYPE_P (type))
13074 error ("friend declaration does not name a class or "
13075 "function");
13076 else
13077 make_friend_class (current_class_type, type,
13078 /*complain=*/true);
13080 /* If there is no TYPE, an error message will already have
13081 been issued. */
13082 else if (!type || type == error_mark_node)
13084 /* An anonymous aggregate has to be handled specially; such
13085 a declaration really declares a data member (with a
13086 particular type), as opposed to a nested class. */
13087 else if (ANON_AGGR_TYPE_P (type))
13089 /* Remove constructors and such from TYPE, now that we
13090 know it is an anonymous aggregate. */
13091 fixup_anonymous_aggr (type);
13092 /* And make the corresponding data member. */
13093 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13094 /* Add it to the class. */
13095 finish_member_declaration (decl);
13097 else
13098 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13101 else
13103 /* See if these declarations will be friends. */
13104 friend_p = cp_parser_friend_p (&decl_specifiers);
13106 /* Keep going until we hit the `;' at the end of the
13107 declaration. */
13108 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13110 tree attributes = NULL_TREE;
13111 tree first_attribute;
13113 /* Peek at the next token. */
13114 token = cp_lexer_peek_token (parser->lexer);
13116 /* Check for a bitfield declaration. */
13117 if (token->type == CPP_COLON
13118 || (token->type == CPP_NAME
13119 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13120 == CPP_COLON))
13122 tree identifier;
13123 tree width;
13125 /* Get the name of the bitfield. Note that we cannot just
13126 check TOKEN here because it may have been invalidated by
13127 the call to cp_lexer_peek_nth_token above. */
13128 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13129 identifier = cp_parser_identifier (parser);
13130 else
13131 identifier = NULL_TREE;
13133 /* Consume the `:' token. */
13134 cp_lexer_consume_token (parser->lexer);
13135 /* Get the width of the bitfield. */
13136 width
13137 = cp_parser_constant_expression (parser,
13138 /*allow_non_constant=*/false,
13139 NULL);
13141 /* Look for attributes that apply to the bitfield. */
13142 attributes = cp_parser_attributes_opt (parser);
13143 /* Remember which attributes are prefix attributes and
13144 which are not. */
13145 first_attribute = attributes;
13146 /* Combine the attributes. */
13147 attributes = chainon (prefix_attributes, attributes);
13149 /* Create the bitfield declaration. */
13150 decl = grokbitfield (identifier
13151 ? make_id_declarator (NULL_TREE,
13152 identifier)
13153 : NULL,
13154 &decl_specifiers,
13155 width);
13156 /* Apply the attributes. */
13157 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13159 else
13161 cp_declarator *declarator;
13162 tree initializer;
13163 tree asm_specification;
13164 int ctor_dtor_or_conv_p;
13166 /* Parse the declarator. */
13167 declarator
13168 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13169 &ctor_dtor_or_conv_p,
13170 /*parenthesized_p=*/NULL,
13171 /*member_p=*/true);
13173 /* If something went wrong parsing the declarator, make sure
13174 that we at least consume some tokens. */
13175 if (declarator == cp_error_declarator)
13177 /* Skip to the end of the statement. */
13178 cp_parser_skip_to_end_of_statement (parser);
13179 /* If the next token is not a semicolon, that is
13180 probably because we just skipped over the body of
13181 a function. So, we consume a semicolon if
13182 present, but do not issue an error message if it
13183 is not present. */
13184 if (cp_lexer_next_token_is (parser->lexer,
13185 CPP_SEMICOLON))
13186 cp_lexer_consume_token (parser->lexer);
13187 return;
13190 if (declares_class_or_enum & 2)
13191 cp_parser_check_for_definition_in_return_type
13192 (declarator, decl_specifiers.type);
13194 /* Look for an asm-specification. */
13195 asm_specification = cp_parser_asm_specification_opt (parser);
13196 /* Look for attributes that apply to the declaration. */
13197 attributes = cp_parser_attributes_opt (parser);
13198 /* Remember which attributes are prefix attributes and
13199 which are not. */
13200 first_attribute = attributes;
13201 /* Combine the attributes. */
13202 attributes = chainon (prefix_attributes, attributes);
13204 /* If it's an `=', then we have a constant-initializer or a
13205 pure-specifier. It is not correct to parse the
13206 initializer before registering the member declaration
13207 since the member declaration should be in scope while
13208 its initializer is processed. However, the rest of the
13209 front end does not yet provide an interface that allows
13210 us to handle this correctly. */
13211 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13213 /* In [class.mem]:
13215 A pure-specifier shall be used only in the declaration of
13216 a virtual function.
13218 A member-declarator can contain a constant-initializer
13219 only if it declares a static member of integral or
13220 enumeration type.
13222 Therefore, if the DECLARATOR is for a function, we look
13223 for a pure-specifier; otherwise, we look for a
13224 constant-initializer. When we call `grokfield', it will
13225 perform more stringent semantics checks. */
13226 if (declarator->kind == cdk_function)
13227 initializer = cp_parser_pure_specifier (parser);
13228 else
13229 /* Parse the initializer. */
13230 initializer = cp_parser_constant_initializer (parser);
13232 /* Otherwise, there is no initializer. */
13233 else
13234 initializer = NULL_TREE;
13236 /* See if we are probably looking at a function
13237 definition. We are certainly not looking at a
13238 member-declarator. Calling `grokfield' has
13239 side-effects, so we must not do it unless we are sure
13240 that we are looking at a member-declarator. */
13241 if (cp_parser_token_starts_function_definition_p
13242 (cp_lexer_peek_token (parser->lexer)))
13244 /* The grammar does not allow a pure-specifier to be
13245 used when a member function is defined. (It is
13246 possible that this fact is an oversight in the
13247 standard, since a pure function may be defined
13248 outside of the class-specifier. */
13249 if (initializer)
13250 error ("pure-specifier on function-definition");
13251 decl = cp_parser_save_member_function_body (parser,
13252 &decl_specifiers,
13253 declarator,
13254 attributes);
13255 /* If the member was not a friend, declare it here. */
13256 if (!friend_p)
13257 finish_member_declaration (decl);
13258 /* Peek at the next token. */
13259 token = cp_lexer_peek_token (parser->lexer);
13260 /* If the next token is a semicolon, consume it. */
13261 if (token->type == CPP_SEMICOLON)
13262 cp_lexer_consume_token (parser->lexer);
13263 return;
13265 else
13267 /* Create the declaration. */
13268 decl = grokfield (declarator, &decl_specifiers,
13269 initializer, asm_specification,
13270 attributes);
13271 /* Any initialization must have been from a
13272 constant-expression. */
13273 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13274 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13278 /* Reset PREFIX_ATTRIBUTES. */
13279 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13280 attributes = TREE_CHAIN (attributes);
13281 if (attributes)
13282 TREE_CHAIN (attributes) = NULL_TREE;
13284 /* If there is any qualification still in effect, clear it
13285 now; we will be starting fresh with the next declarator. */
13286 parser->scope = NULL_TREE;
13287 parser->qualifying_scope = NULL_TREE;
13288 parser->object_scope = NULL_TREE;
13289 /* If it's a `,', then there are more declarators. */
13290 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13291 cp_lexer_consume_token (parser->lexer);
13292 /* If the next token isn't a `;', then we have a parse error. */
13293 else if (cp_lexer_next_token_is_not (parser->lexer,
13294 CPP_SEMICOLON))
13296 cp_parser_error (parser, "expected %<;%>");
13297 /* Skip tokens until we find a `;'. */
13298 cp_parser_skip_to_end_of_statement (parser);
13300 break;
13303 if (decl)
13305 /* Add DECL to the list of members. */
13306 if (!friend_p)
13307 finish_member_declaration (decl);
13309 if (TREE_CODE (decl) == FUNCTION_DECL)
13310 cp_parser_save_default_args (parser, decl);
13315 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13318 /* Parse a pure-specifier.
13320 pure-specifier:
13323 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13324 Otherwise, ERROR_MARK_NODE is returned. */
13326 static tree
13327 cp_parser_pure_specifier (cp_parser* parser)
13329 cp_token *token;
13331 /* Look for the `=' token. */
13332 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13333 return error_mark_node;
13334 /* Look for the `0' token. */
13335 token = cp_lexer_consume_token (parser->lexer);
13336 if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13338 cp_parser_error (parser,
13339 "invalid pure specifier (only `= 0' is allowed)");
13340 cp_parser_skip_to_end_of_statement (parser);
13341 return error_mark_node;
13344 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13345 We need to get information from the lexer about how the number
13346 was spelled in order to fix this problem. */
13347 return integer_zero_node;
13350 /* Parse a constant-initializer.
13352 constant-initializer:
13353 = constant-expression
13355 Returns a representation of the constant-expression. */
13357 static tree
13358 cp_parser_constant_initializer (cp_parser* parser)
13360 /* Look for the `=' token. */
13361 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13362 return error_mark_node;
13364 /* It is invalid to write:
13366 struct S { static const int i = { 7 }; };
13369 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13371 cp_parser_error (parser,
13372 "a brace-enclosed initializer is not allowed here");
13373 /* Consume the opening brace. */
13374 cp_lexer_consume_token (parser->lexer);
13375 /* Skip the initializer. */
13376 cp_parser_skip_to_closing_brace (parser);
13377 /* Look for the trailing `}'. */
13378 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13380 return error_mark_node;
13383 return cp_parser_constant_expression (parser,
13384 /*allow_non_constant=*/false,
13385 NULL);
13388 /* Derived classes [gram.class.derived] */
13390 /* Parse a base-clause.
13392 base-clause:
13393 : base-specifier-list
13395 base-specifier-list:
13396 base-specifier
13397 base-specifier-list , base-specifier
13399 Returns a TREE_LIST representing the base-classes, in the order in
13400 which they were declared. The representation of each node is as
13401 described by cp_parser_base_specifier.
13403 In the case that no bases are specified, this function will return
13404 NULL_TREE, not ERROR_MARK_NODE. */
13406 static tree
13407 cp_parser_base_clause (cp_parser* parser)
13409 tree bases = NULL_TREE;
13411 /* Look for the `:' that begins the list. */
13412 cp_parser_require (parser, CPP_COLON, "`:'");
13414 /* Scan the base-specifier-list. */
13415 while (true)
13417 cp_token *token;
13418 tree base;
13420 /* Look for the base-specifier. */
13421 base = cp_parser_base_specifier (parser);
13422 /* Add BASE to the front of the list. */
13423 if (base != error_mark_node)
13425 TREE_CHAIN (base) = bases;
13426 bases = base;
13428 /* Peek at the next token. */
13429 token = cp_lexer_peek_token (parser->lexer);
13430 /* If it's not a comma, then the list is complete. */
13431 if (token->type != CPP_COMMA)
13432 break;
13433 /* Consume the `,'. */
13434 cp_lexer_consume_token (parser->lexer);
13437 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13438 base class had a qualified name. However, the next name that
13439 appears is certainly not qualified. */
13440 parser->scope = NULL_TREE;
13441 parser->qualifying_scope = NULL_TREE;
13442 parser->object_scope = NULL_TREE;
13444 return nreverse (bases);
13447 /* Parse a base-specifier.
13449 base-specifier:
13450 :: [opt] nested-name-specifier [opt] class-name
13451 virtual access-specifier [opt] :: [opt] nested-name-specifier
13452 [opt] class-name
13453 access-specifier virtual [opt] :: [opt] nested-name-specifier
13454 [opt] class-name
13456 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13457 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13458 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13459 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13461 static tree
13462 cp_parser_base_specifier (cp_parser* parser)
13464 cp_token *token;
13465 bool done = false;
13466 bool virtual_p = false;
13467 bool duplicate_virtual_error_issued_p = false;
13468 bool duplicate_access_error_issued_p = false;
13469 bool class_scope_p, template_p;
13470 tree access = access_default_node;
13471 tree type;
13473 /* Process the optional `virtual' and `access-specifier'. */
13474 while (!done)
13476 /* Peek at the next token. */
13477 token = cp_lexer_peek_token (parser->lexer);
13478 /* Process `virtual'. */
13479 switch (token->keyword)
13481 case RID_VIRTUAL:
13482 /* If `virtual' appears more than once, issue an error. */
13483 if (virtual_p && !duplicate_virtual_error_issued_p)
13485 cp_parser_error (parser,
13486 "%<virtual%> specified more than once in base-specified");
13487 duplicate_virtual_error_issued_p = true;
13490 virtual_p = true;
13492 /* Consume the `virtual' token. */
13493 cp_lexer_consume_token (parser->lexer);
13495 break;
13497 case RID_PUBLIC:
13498 case RID_PROTECTED:
13499 case RID_PRIVATE:
13500 /* If more than one access specifier appears, issue an
13501 error. */
13502 if (access != access_default_node
13503 && !duplicate_access_error_issued_p)
13505 cp_parser_error (parser,
13506 "more than one access specifier in base-specified");
13507 duplicate_access_error_issued_p = true;
13510 access = ridpointers[(int) token->keyword];
13512 /* Consume the access-specifier. */
13513 cp_lexer_consume_token (parser->lexer);
13515 break;
13517 default:
13518 done = true;
13519 break;
13522 /* It is not uncommon to see programs mechanically, erroneously, use
13523 the 'typename' keyword to denote (dependent) qualified types
13524 as base classes. */
13525 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13527 if (!processing_template_decl)
13528 error ("keyword %<typename%> not allowed outside of templates");
13529 else
13530 error ("keyword %<typename%> not allowed in this context "
13531 "(the base class is implicitly a type)");
13532 cp_lexer_consume_token (parser->lexer);
13535 /* Look for the optional `::' operator. */
13536 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13537 /* Look for the nested-name-specifier. The simplest way to
13538 implement:
13540 [temp.res]
13542 The keyword `typename' is not permitted in a base-specifier or
13543 mem-initializer; in these contexts a qualified name that
13544 depends on a template-parameter is implicitly assumed to be a
13545 type name.
13547 is to pretend that we have seen the `typename' keyword at this
13548 point. */
13549 cp_parser_nested_name_specifier_opt (parser,
13550 /*typename_keyword_p=*/true,
13551 /*check_dependency_p=*/true,
13552 typename_type,
13553 /*is_declaration=*/true);
13554 /* If the base class is given by a qualified name, assume that names
13555 we see are type names or templates, as appropriate. */
13556 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13557 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13559 /* Finally, look for the class-name. */
13560 type = cp_parser_class_name (parser,
13561 class_scope_p,
13562 template_p,
13563 typename_type,
13564 /*check_dependency_p=*/true,
13565 /*class_head_p=*/false,
13566 /*is_declaration=*/true);
13568 if (type == error_mark_node)
13569 return error_mark_node;
13571 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13574 /* Exception handling [gram.exception] */
13576 /* Parse an (optional) exception-specification.
13578 exception-specification:
13579 throw ( type-id-list [opt] )
13581 Returns a TREE_LIST representing the exception-specification. The
13582 TREE_VALUE of each node is a type. */
13584 static tree
13585 cp_parser_exception_specification_opt (cp_parser* parser)
13587 cp_token *token;
13588 tree type_id_list;
13590 /* Peek at the next token. */
13591 token = cp_lexer_peek_token (parser->lexer);
13592 /* If it's not `throw', then there's no exception-specification. */
13593 if (!cp_parser_is_keyword (token, RID_THROW))
13594 return NULL_TREE;
13596 /* Consume the `throw'. */
13597 cp_lexer_consume_token (parser->lexer);
13599 /* Look for the `('. */
13600 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13602 /* Peek at the next token. */
13603 token = cp_lexer_peek_token (parser->lexer);
13604 /* If it's not a `)', then there is a type-id-list. */
13605 if (token->type != CPP_CLOSE_PAREN)
13607 const char *saved_message;
13609 /* Types may not be defined in an exception-specification. */
13610 saved_message = parser->type_definition_forbidden_message;
13611 parser->type_definition_forbidden_message
13612 = "types may not be defined in an exception-specification";
13613 /* Parse the type-id-list. */
13614 type_id_list = cp_parser_type_id_list (parser);
13615 /* Restore the saved message. */
13616 parser->type_definition_forbidden_message = saved_message;
13618 else
13619 type_id_list = empty_except_spec;
13621 /* Look for the `)'. */
13622 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13624 return type_id_list;
13627 /* Parse an (optional) type-id-list.
13629 type-id-list:
13630 type-id
13631 type-id-list , type-id
13633 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13634 in the order that the types were presented. */
13636 static tree
13637 cp_parser_type_id_list (cp_parser* parser)
13639 tree types = NULL_TREE;
13641 while (true)
13643 cp_token *token;
13644 tree type;
13646 /* Get the next type-id. */
13647 type = cp_parser_type_id (parser);
13648 /* Add it to the list. */
13649 types = add_exception_specifier (types, type, /*complain=*/1);
13650 /* Peek at the next token. */
13651 token = cp_lexer_peek_token (parser->lexer);
13652 /* If it is not a `,', we are done. */
13653 if (token->type != CPP_COMMA)
13654 break;
13655 /* Consume the `,'. */
13656 cp_lexer_consume_token (parser->lexer);
13659 return nreverse (types);
13662 /* Parse a try-block.
13664 try-block:
13665 try compound-statement handler-seq */
13667 static tree
13668 cp_parser_try_block (cp_parser* parser)
13670 tree try_block;
13672 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13673 try_block = begin_try_block ();
13674 cp_parser_compound_statement (parser, NULL, true);
13675 finish_try_block (try_block);
13676 cp_parser_handler_seq (parser);
13677 finish_handler_sequence (try_block);
13679 return try_block;
13682 /* Parse a function-try-block.
13684 function-try-block:
13685 try ctor-initializer [opt] function-body handler-seq */
13687 static bool
13688 cp_parser_function_try_block (cp_parser* parser)
13690 tree try_block;
13691 bool ctor_initializer_p;
13693 /* Look for the `try' keyword. */
13694 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13695 return false;
13696 /* Let the rest of the front-end know where we are. */
13697 try_block = begin_function_try_block ();
13698 /* Parse the function-body. */
13699 ctor_initializer_p
13700 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13701 /* We're done with the `try' part. */
13702 finish_function_try_block (try_block);
13703 /* Parse the handlers. */
13704 cp_parser_handler_seq (parser);
13705 /* We're done with the handlers. */
13706 finish_function_handler_sequence (try_block);
13708 return ctor_initializer_p;
13711 /* Parse a handler-seq.
13713 handler-seq:
13714 handler handler-seq [opt] */
13716 static void
13717 cp_parser_handler_seq (cp_parser* parser)
13719 while (true)
13721 cp_token *token;
13723 /* Parse the handler. */
13724 cp_parser_handler (parser);
13725 /* Peek at the next token. */
13726 token = cp_lexer_peek_token (parser->lexer);
13727 /* If it's not `catch' then there are no more handlers. */
13728 if (!cp_parser_is_keyword (token, RID_CATCH))
13729 break;
13733 /* Parse a handler.
13735 handler:
13736 catch ( exception-declaration ) compound-statement */
13738 static void
13739 cp_parser_handler (cp_parser* parser)
13741 tree handler;
13742 tree declaration;
13744 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13745 handler = begin_handler ();
13746 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13747 declaration = cp_parser_exception_declaration (parser);
13748 finish_handler_parms (declaration, handler);
13749 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13750 cp_parser_compound_statement (parser, NULL, false);
13751 finish_handler (handler);
13754 /* Parse an exception-declaration.
13756 exception-declaration:
13757 type-specifier-seq declarator
13758 type-specifier-seq abstract-declarator
13759 type-specifier-seq
13762 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13763 ellipsis variant is used. */
13765 static tree
13766 cp_parser_exception_declaration (cp_parser* parser)
13768 tree decl;
13769 cp_decl_specifier_seq type_specifiers;
13770 cp_declarator *declarator;
13771 const char *saved_message;
13773 /* If it's an ellipsis, it's easy to handle. */
13774 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13776 /* Consume the `...' token. */
13777 cp_lexer_consume_token (parser->lexer);
13778 return NULL_TREE;
13781 /* Types may not be defined in exception-declarations. */
13782 saved_message = parser->type_definition_forbidden_message;
13783 parser->type_definition_forbidden_message
13784 = "types may not be defined in exception-declarations";
13786 /* Parse the type-specifier-seq. */
13787 cp_parser_type_specifier_seq (parser, &type_specifiers);
13788 /* If it's a `)', then there is no declarator. */
13789 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13790 declarator = NULL;
13791 else
13792 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13793 /*ctor_dtor_or_conv_p=*/NULL,
13794 /*parenthesized_p=*/NULL,
13795 /*member_p=*/false);
13797 /* Restore the saved message. */
13798 parser->type_definition_forbidden_message = saved_message;
13800 if (type_specifiers.any_specifiers_p)
13802 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13803 if (decl == NULL_TREE)
13804 error ("invalid catch parameter");
13806 else
13807 decl = NULL_TREE;
13809 return decl;
13812 /* Parse a throw-expression.
13814 throw-expression:
13815 throw assignment-expression [opt]
13817 Returns a THROW_EXPR representing the throw-expression. */
13819 static tree
13820 cp_parser_throw_expression (cp_parser* parser)
13822 tree expression;
13823 cp_token* token;
13825 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13826 token = cp_lexer_peek_token (parser->lexer);
13827 /* Figure out whether or not there is an assignment-expression
13828 following the "throw" keyword. */
13829 if (token->type == CPP_COMMA
13830 || token->type == CPP_SEMICOLON
13831 || token->type == CPP_CLOSE_PAREN
13832 || token->type == CPP_CLOSE_SQUARE
13833 || token->type == CPP_CLOSE_BRACE
13834 || token->type == CPP_COLON)
13835 expression = NULL_TREE;
13836 else
13837 expression = cp_parser_assignment_expression (parser,
13838 /*cast_p=*/false);
13840 return build_throw (expression);
13843 /* GNU Extensions */
13845 /* Parse an (optional) asm-specification.
13847 asm-specification:
13848 asm ( string-literal )
13850 If the asm-specification is present, returns a STRING_CST
13851 corresponding to the string-literal. Otherwise, returns
13852 NULL_TREE. */
13854 static tree
13855 cp_parser_asm_specification_opt (cp_parser* parser)
13857 cp_token *token;
13858 tree asm_specification;
13860 /* Peek at the next token. */
13861 token = cp_lexer_peek_token (parser->lexer);
13862 /* If the next token isn't the `asm' keyword, then there's no
13863 asm-specification. */
13864 if (!cp_parser_is_keyword (token, RID_ASM))
13865 return NULL_TREE;
13867 /* Consume the `asm' token. */
13868 cp_lexer_consume_token (parser->lexer);
13869 /* Look for the `('. */
13870 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13872 /* Look for the string-literal. */
13873 asm_specification = cp_parser_string_literal (parser, false, false);
13875 /* Look for the `)'. */
13876 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13878 return asm_specification;
13881 /* Parse an asm-operand-list.
13883 asm-operand-list:
13884 asm-operand
13885 asm-operand-list , asm-operand
13887 asm-operand:
13888 string-literal ( expression )
13889 [ string-literal ] string-literal ( expression )
13891 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13892 each node is the expression. The TREE_PURPOSE is itself a
13893 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13894 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13895 is a STRING_CST for the string literal before the parenthesis. */
13897 static tree
13898 cp_parser_asm_operand_list (cp_parser* parser)
13900 tree asm_operands = NULL_TREE;
13902 while (true)
13904 tree string_literal;
13905 tree expression;
13906 tree name;
13908 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13910 /* Consume the `[' token. */
13911 cp_lexer_consume_token (parser->lexer);
13912 /* Read the operand name. */
13913 name = cp_parser_identifier (parser);
13914 if (name != error_mark_node)
13915 name = build_string (IDENTIFIER_LENGTH (name),
13916 IDENTIFIER_POINTER (name));
13917 /* Look for the closing `]'. */
13918 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13920 else
13921 name = NULL_TREE;
13922 /* Look for the string-literal. */
13923 string_literal = cp_parser_string_literal (parser, false, false);
13925 /* Look for the `('. */
13926 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13927 /* Parse the expression. */
13928 expression = cp_parser_expression (parser, /*cast_p=*/false);
13929 /* Look for the `)'. */
13930 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13932 /* Add this operand to the list. */
13933 asm_operands = tree_cons (build_tree_list (name, string_literal),
13934 expression,
13935 asm_operands);
13936 /* If the next token is not a `,', there are no more
13937 operands. */
13938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13939 break;
13940 /* Consume the `,'. */
13941 cp_lexer_consume_token (parser->lexer);
13944 return nreverse (asm_operands);
13947 /* Parse an asm-clobber-list.
13949 asm-clobber-list:
13950 string-literal
13951 asm-clobber-list , string-literal
13953 Returns a TREE_LIST, indicating the clobbers in the order that they
13954 appeared. The TREE_VALUE of each node is a STRING_CST. */
13956 static tree
13957 cp_parser_asm_clobber_list (cp_parser* parser)
13959 tree clobbers = NULL_TREE;
13961 while (true)
13963 tree string_literal;
13965 /* Look for the string literal. */
13966 string_literal = cp_parser_string_literal (parser, false, false);
13967 /* Add it to the list. */
13968 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13969 /* If the next token is not a `,', then the list is
13970 complete. */
13971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13972 break;
13973 /* Consume the `,' token. */
13974 cp_lexer_consume_token (parser->lexer);
13977 return clobbers;
13980 /* Parse an (optional) series of attributes.
13982 attributes:
13983 attributes attribute
13985 attribute:
13986 __attribute__ (( attribute-list [opt] ))
13988 The return value is as for cp_parser_attribute_list. */
13990 static tree
13991 cp_parser_attributes_opt (cp_parser* parser)
13993 tree attributes = NULL_TREE;
13995 while (true)
13997 cp_token *token;
13998 tree attribute_list;
14000 /* Peek at the next token. */
14001 token = cp_lexer_peek_token (parser->lexer);
14002 /* If it's not `__attribute__', then we're done. */
14003 if (token->keyword != RID_ATTRIBUTE)
14004 break;
14006 /* Consume the `__attribute__' keyword. */
14007 cp_lexer_consume_token (parser->lexer);
14008 /* Look for the two `(' tokens. */
14009 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14010 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14012 /* Peek at the next token. */
14013 token = cp_lexer_peek_token (parser->lexer);
14014 if (token->type != CPP_CLOSE_PAREN)
14015 /* Parse the attribute-list. */
14016 attribute_list = cp_parser_attribute_list (parser);
14017 else
14018 /* If the next token is a `)', then there is no attribute
14019 list. */
14020 attribute_list = NULL;
14022 /* Look for the two `)' tokens. */
14023 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14024 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14026 /* Add these new attributes to the list. */
14027 attributes = chainon (attributes, attribute_list);
14030 return attributes;
14033 /* Parse an attribute-list.
14035 attribute-list:
14036 attribute
14037 attribute-list , attribute
14039 attribute:
14040 identifier
14041 identifier ( identifier )
14042 identifier ( identifier , expression-list )
14043 identifier ( expression-list )
14045 Returns a TREE_LIST. Each node corresponds to an attribute. THe
14046 TREE_PURPOSE of each node is the identifier indicating which
14047 attribute is in use. The TREE_VALUE represents the arguments, if
14048 any. */
14050 static tree
14051 cp_parser_attribute_list (cp_parser* parser)
14053 tree attribute_list = NULL_TREE;
14054 bool save_translate_strings_p = parser->translate_strings_p;
14056 parser->translate_strings_p = false;
14057 while (true)
14059 cp_token *token;
14060 tree identifier;
14061 tree attribute;
14063 /* Look for the identifier. We also allow keywords here; for
14064 example `__attribute__ ((const))' is legal. */
14065 token = cp_lexer_peek_token (parser->lexer);
14066 if (token->type != CPP_NAME
14067 && token->type != CPP_KEYWORD)
14068 return error_mark_node;
14069 /* Consume the token. */
14070 token = cp_lexer_consume_token (parser->lexer);
14072 /* Save away the identifier that indicates which attribute this is. */
14073 identifier = token->value;
14074 attribute = build_tree_list (identifier, NULL_TREE);
14076 /* Peek at the next token. */
14077 token = cp_lexer_peek_token (parser->lexer);
14078 /* If it's an `(', then parse the attribute arguments. */
14079 if (token->type == CPP_OPEN_PAREN)
14081 tree arguments;
14083 arguments = (cp_parser_parenthesized_expression_list
14084 (parser, true, /*cast_p=*/false,
14085 /*non_constant_p=*/NULL));
14086 /* Save the identifier and arguments away. */
14087 TREE_VALUE (attribute) = arguments;
14090 /* Add this attribute to the list. */
14091 TREE_CHAIN (attribute) = attribute_list;
14092 attribute_list = attribute;
14094 /* Now, look for more attributes. */
14095 token = cp_lexer_peek_token (parser->lexer);
14096 /* If the next token isn't a `,', we're done. */
14097 if (token->type != CPP_COMMA)
14098 break;
14100 /* Consume the comma and keep going. */
14101 cp_lexer_consume_token (parser->lexer);
14103 parser->translate_strings_p = save_translate_strings_p;
14105 /* We built up the list in reverse order. */
14106 return nreverse (attribute_list);
14109 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14110 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14111 current value of the PEDANTIC flag, regardless of whether or not
14112 the `__extension__' keyword is present. The caller is responsible
14113 for restoring the value of the PEDANTIC flag. */
14115 static bool
14116 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14118 /* Save the old value of the PEDANTIC flag. */
14119 *saved_pedantic = pedantic;
14121 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14123 /* Consume the `__extension__' token. */
14124 cp_lexer_consume_token (parser->lexer);
14125 /* We're not being pedantic while the `__extension__' keyword is
14126 in effect. */
14127 pedantic = 0;
14129 return true;
14132 return false;
14135 /* Parse a label declaration.
14137 label-declaration:
14138 __label__ label-declarator-seq ;
14140 label-declarator-seq:
14141 identifier , label-declarator-seq
14142 identifier */
14144 static void
14145 cp_parser_label_declaration (cp_parser* parser)
14147 /* Look for the `__label__' keyword. */
14148 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14150 while (true)
14152 tree identifier;
14154 /* Look for an identifier. */
14155 identifier = cp_parser_identifier (parser);
14156 /* Declare it as a lobel. */
14157 finish_label_decl (identifier);
14158 /* If the next token is a `;', stop. */
14159 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14160 break;
14161 /* Look for the `,' separating the label declarations. */
14162 cp_parser_require (parser, CPP_COMMA, "`,'");
14165 /* Look for the final `;'. */
14166 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14169 /* Support Functions */
14171 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14172 NAME should have one of the representations used for an
14173 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14174 is returned. If PARSER->SCOPE is a dependent type, then a
14175 SCOPE_REF is returned.
14177 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14178 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14179 was formed. Abstractly, such entities should not be passed to this
14180 function, because they do not need to be looked up, but it is
14181 simpler to check for this special case here, rather than at the
14182 call-sites.
14184 In cases not explicitly covered above, this function returns a
14185 DECL, OVERLOAD, or baselink representing the result of the lookup.
14186 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14187 is returned.
14189 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14190 (e.g., "struct") that was used. In that case bindings that do not
14191 refer to types are ignored.
14193 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14194 ignored.
14196 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14197 are ignored.
14199 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14200 types.
14202 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14203 results in an ambiguity, and false otherwise. */
14205 static tree
14206 cp_parser_lookup_name (cp_parser *parser, tree name,
14207 enum tag_types tag_type,
14208 bool is_template, bool is_namespace,
14209 bool check_dependency,
14210 bool *ambiguous_p)
14212 tree decl;
14213 tree object_type = parser->context->object_type;
14215 /* Assume that the lookup will be unambiguous. */
14216 if (ambiguous_p)
14217 *ambiguous_p = false;
14219 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14220 no longer valid. Note that if we are parsing tentatively, and
14221 the parse fails, OBJECT_TYPE will be automatically restored. */
14222 parser->context->object_type = NULL_TREE;
14224 if (name == error_mark_node)
14225 return error_mark_node;
14227 /* A template-id has already been resolved; there is no lookup to
14228 do. */
14229 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14230 return name;
14231 if (BASELINK_P (name))
14233 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14234 == TEMPLATE_ID_EXPR);
14235 return name;
14238 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14239 it should already have been checked to make sure that the name
14240 used matches the type being destroyed. */
14241 if (TREE_CODE (name) == BIT_NOT_EXPR)
14243 tree type;
14245 /* Figure out to which type this destructor applies. */
14246 if (parser->scope)
14247 type = parser->scope;
14248 else if (object_type)
14249 type = object_type;
14250 else
14251 type = current_class_type;
14252 /* If that's not a class type, there is no destructor. */
14253 if (!type || !CLASS_TYPE_P (type))
14254 return error_mark_node;
14255 if (!CLASSTYPE_DESTRUCTORS (type))
14256 return error_mark_node;
14257 /* If it was a class type, return the destructor. */
14258 return CLASSTYPE_DESTRUCTORS (type);
14261 /* By this point, the NAME should be an ordinary identifier. If
14262 the id-expression was a qualified name, the qualifying scope is
14263 stored in PARSER->SCOPE at this point. */
14264 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14266 /* Perform the lookup. */
14267 if (parser->scope)
14269 bool dependent_p;
14271 if (parser->scope == error_mark_node)
14272 return error_mark_node;
14274 /* If the SCOPE is dependent, the lookup must be deferred until
14275 the template is instantiated -- unless we are explicitly
14276 looking up names in uninstantiated templates. Even then, we
14277 cannot look up the name if the scope is not a class type; it
14278 might, for example, be a template type parameter. */
14279 dependent_p = (TYPE_P (parser->scope)
14280 && !(parser->in_declarator_p
14281 && currently_open_class (parser->scope))
14282 && dependent_type_p (parser->scope));
14283 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14284 && dependent_p)
14286 if (tag_type)
14288 tree type;
14290 /* The resolution to Core Issue 180 says that `struct
14291 A::B' should be considered a type-name, even if `A'
14292 is dependent. */
14293 type = make_typename_type (parser->scope, name, tag_type,
14294 /*complain=*/1);
14295 decl = TYPE_NAME (type);
14297 else if (is_template)
14298 decl = make_unbound_class_template (parser->scope,
14299 name, NULL_TREE,
14300 /*complain=*/1);
14301 else
14302 decl = build_nt (SCOPE_REF, parser->scope, name);
14304 else
14306 tree pushed_scope = NULL_TREE;
14308 /* If PARSER->SCOPE is a dependent type, then it must be a
14309 class type, and we must not be checking dependencies;
14310 otherwise, we would have processed this lookup above. So
14311 that PARSER->SCOPE is not considered a dependent base by
14312 lookup_member, we must enter the scope here. */
14313 if (dependent_p)
14314 pushed_scope = push_scope (parser->scope);
14315 /* If the PARSER->SCOPE is a a template specialization, it
14316 may be instantiated during name lookup. In that case,
14317 errors may be issued. Even if we rollback the current
14318 tentative parse, those errors are valid. */
14319 decl = lookup_qualified_name (parser->scope, name,
14320 tag_type != none_type,
14321 /*complain=*/true);
14322 if (pushed_scope)
14323 pop_scope (pushed_scope);
14325 parser->qualifying_scope = parser->scope;
14326 parser->object_scope = NULL_TREE;
14328 else if (object_type)
14330 tree object_decl = NULL_TREE;
14331 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14332 OBJECT_TYPE is not a class. */
14333 if (CLASS_TYPE_P (object_type))
14334 /* If the OBJECT_TYPE is a template specialization, it may
14335 be instantiated during name lookup. In that case, errors
14336 may be issued. Even if we rollback the current tentative
14337 parse, those errors are valid. */
14338 object_decl = lookup_member (object_type,
14339 name,
14340 /*protect=*/0,
14341 tag_type != none_type);
14342 /* Look it up in the enclosing context, too. */
14343 decl = lookup_name_real (name, tag_type != none_type,
14344 /*nonclass=*/0,
14345 /*block_p=*/true, is_namespace,
14346 /*flags=*/0);
14347 parser->object_scope = object_type;
14348 parser->qualifying_scope = NULL_TREE;
14349 if (object_decl)
14350 decl = object_decl;
14352 else
14354 decl = lookup_name_real (name, tag_type != none_type,
14355 /*nonclass=*/0,
14356 /*block_p=*/true, is_namespace,
14357 /*flags=*/0);
14358 parser->qualifying_scope = NULL_TREE;
14359 parser->object_scope = NULL_TREE;
14362 /* If the lookup failed, let our caller know. */
14363 if (!decl
14364 || decl == error_mark_node
14365 || (TREE_CODE (decl) == FUNCTION_DECL
14366 && DECL_ANTICIPATED (decl)))
14367 return error_mark_node;
14369 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14370 if (TREE_CODE (decl) == TREE_LIST)
14372 if (ambiguous_p)
14373 *ambiguous_p = true;
14374 /* The error message we have to print is too complicated for
14375 cp_parser_error, so we incorporate its actions directly. */
14376 if (!cp_parser_simulate_error (parser))
14378 error ("reference to %qD is ambiguous", name);
14379 print_candidates (decl);
14381 return error_mark_node;
14384 gcc_assert (DECL_P (decl)
14385 || TREE_CODE (decl) == OVERLOAD
14386 || TREE_CODE (decl) == SCOPE_REF
14387 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14388 || BASELINK_P (decl));
14390 /* If we have resolved the name of a member declaration, check to
14391 see if the declaration is accessible. When the name resolves to
14392 set of overloaded functions, accessibility is checked when
14393 overload resolution is done.
14395 During an explicit instantiation, access is not checked at all,
14396 as per [temp.explicit]. */
14397 if (DECL_P (decl))
14398 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14400 return decl;
14403 /* Like cp_parser_lookup_name, but for use in the typical case where
14404 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14405 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14407 static tree
14408 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14410 return cp_parser_lookup_name (parser, name,
14411 none_type,
14412 /*is_template=*/false,
14413 /*is_namespace=*/false,
14414 /*check_dependency=*/true,
14415 /*ambiguous_p=*/NULL);
14418 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14419 the current context, return the TYPE_DECL. If TAG_NAME_P is
14420 true, the DECL indicates the class being defined in a class-head,
14421 or declared in an elaborated-type-specifier.
14423 Otherwise, return DECL. */
14425 static tree
14426 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14428 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14429 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14431 struct A {
14432 template <typename T> struct B;
14435 template <typename T> struct A::B {};
14437 Similarly, in a elaborated-type-specifier:
14439 namespace N { struct X{}; }
14441 struct A {
14442 template <typename T> friend struct N::X;
14445 However, if the DECL refers to a class type, and we are in
14446 the scope of the class, then the name lookup automatically
14447 finds the TYPE_DECL created by build_self_reference rather
14448 than a TEMPLATE_DECL. For example, in:
14450 template <class T> struct S {
14451 S s;
14454 there is no need to handle such case. */
14456 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14457 return DECL_TEMPLATE_RESULT (decl);
14459 return decl;
14462 /* If too many, or too few, template-parameter lists apply to the
14463 declarator, issue an error message. Returns TRUE if all went well,
14464 and FALSE otherwise. */
14466 static bool
14467 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14468 cp_declarator *declarator)
14470 unsigned num_templates;
14472 /* We haven't seen any classes that involve template parameters yet. */
14473 num_templates = 0;
14475 switch (declarator->kind)
14477 case cdk_id:
14478 if (declarator->u.id.qualifying_scope)
14480 tree scope;
14481 tree member;
14483 scope = declarator->u.id.qualifying_scope;
14484 member = declarator->u.id.unqualified_name;
14486 while (scope && CLASS_TYPE_P (scope))
14488 /* You're supposed to have one `template <...>'
14489 for every template class, but you don't need one
14490 for a full specialization. For example:
14492 template <class T> struct S{};
14493 template <> struct S<int> { void f(); };
14494 void S<int>::f () {}
14496 is correct; there shouldn't be a `template <>' for
14497 the definition of `S<int>::f'. */
14498 if (CLASSTYPE_TEMPLATE_INFO (scope)
14499 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14500 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14501 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14502 ++num_templates;
14504 scope = TYPE_CONTEXT (scope);
14507 else if (TREE_CODE (declarator->u.id.unqualified_name)
14508 == TEMPLATE_ID_EXPR)
14509 /* If the DECLARATOR has the form `X<y>' then it uses one
14510 additional level of template parameters. */
14511 ++num_templates;
14513 return cp_parser_check_template_parameters (parser,
14514 num_templates);
14516 case cdk_function:
14517 case cdk_array:
14518 case cdk_pointer:
14519 case cdk_reference:
14520 case cdk_ptrmem:
14521 return (cp_parser_check_declarator_template_parameters
14522 (parser, declarator->declarator));
14524 case cdk_error:
14525 return true;
14527 default:
14528 gcc_unreachable ();
14530 return false;
14533 /* NUM_TEMPLATES were used in the current declaration. If that is
14534 invalid, return FALSE and issue an error messages. Otherwise,
14535 return TRUE. */
14537 static bool
14538 cp_parser_check_template_parameters (cp_parser* parser,
14539 unsigned num_templates)
14541 /* If there are more template classes than parameter lists, we have
14542 something like:
14544 template <class T> void S<T>::R<T>::f (); */
14545 if (parser->num_template_parameter_lists < num_templates)
14547 error ("too few template-parameter-lists");
14548 return false;
14550 /* If there are the same number of template classes and parameter
14551 lists, that's OK. */
14552 if (parser->num_template_parameter_lists == num_templates)
14553 return true;
14554 /* If there are more, but only one more, then we are referring to a
14555 member template. That's OK too. */
14556 if (parser->num_template_parameter_lists == num_templates + 1)
14557 return true;
14558 /* Otherwise, there are too many template parameter lists. We have
14559 something like:
14561 template <class T> template <class U> void S::f(); */
14562 error ("too many template-parameter-lists");
14563 return false;
14566 /* Parse an optional `::' token indicating that the following name is
14567 from the global namespace. If so, PARSER->SCOPE is set to the
14568 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14569 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14570 Returns the new value of PARSER->SCOPE, if the `::' token is
14571 present, and NULL_TREE otherwise. */
14573 static tree
14574 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14576 cp_token *token;
14578 /* Peek at the next token. */
14579 token = cp_lexer_peek_token (parser->lexer);
14580 /* If we're looking at a `::' token then we're starting from the
14581 global namespace, not our current location. */
14582 if (token->type == CPP_SCOPE)
14584 /* Consume the `::' token. */
14585 cp_lexer_consume_token (parser->lexer);
14586 /* Set the SCOPE so that we know where to start the lookup. */
14587 parser->scope = global_namespace;
14588 parser->qualifying_scope = global_namespace;
14589 parser->object_scope = NULL_TREE;
14591 return parser->scope;
14593 else if (!current_scope_valid_p)
14595 parser->scope = NULL_TREE;
14596 parser->qualifying_scope = NULL_TREE;
14597 parser->object_scope = NULL_TREE;
14600 return NULL_TREE;
14603 /* Returns TRUE if the upcoming token sequence is the start of a
14604 constructor declarator. If FRIEND_P is true, the declarator is
14605 preceded by the `friend' specifier. */
14607 static bool
14608 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14610 bool constructor_p;
14611 tree type_decl = NULL_TREE;
14612 bool nested_name_p;
14613 cp_token *next_token;
14615 /* The common case is that this is not a constructor declarator, so
14616 try to avoid doing lots of work if at all possible. It's not
14617 valid declare a constructor at function scope. */
14618 if (at_function_scope_p ())
14619 return false;
14620 /* And only certain tokens can begin a constructor declarator. */
14621 next_token = cp_lexer_peek_token (parser->lexer);
14622 if (next_token->type != CPP_NAME
14623 && next_token->type != CPP_SCOPE
14624 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14625 && next_token->type != CPP_TEMPLATE_ID)
14626 return false;
14628 /* Parse tentatively; we are going to roll back all of the tokens
14629 consumed here. */
14630 cp_parser_parse_tentatively (parser);
14631 /* Assume that we are looking at a constructor declarator. */
14632 constructor_p = true;
14634 /* Look for the optional `::' operator. */
14635 cp_parser_global_scope_opt (parser,
14636 /*current_scope_valid_p=*/false);
14637 /* Look for the nested-name-specifier. */
14638 nested_name_p
14639 = (cp_parser_nested_name_specifier_opt (parser,
14640 /*typename_keyword_p=*/false,
14641 /*check_dependency_p=*/false,
14642 /*type_p=*/false,
14643 /*is_declaration=*/false)
14644 != NULL_TREE);
14645 /* Outside of a class-specifier, there must be a
14646 nested-name-specifier. */
14647 if (!nested_name_p &&
14648 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14649 || friend_p))
14650 constructor_p = false;
14651 /* If we still think that this might be a constructor-declarator,
14652 look for a class-name. */
14653 if (constructor_p)
14655 /* If we have:
14657 template <typename T> struct S { S(); };
14658 template <typename T> S<T>::S ();
14660 we must recognize that the nested `S' names a class.
14661 Similarly, for:
14663 template <typename T> S<T>::S<T> ();
14665 we must recognize that the nested `S' names a template. */
14666 type_decl = cp_parser_class_name (parser,
14667 /*typename_keyword_p=*/false,
14668 /*template_keyword_p=*/false,
14669 none_type,
14670 /*check_dependency_p=*/false,
14671 /*class_head_p=*/false,
14672 /*is_declaration=*/false);
14673 /* If there was no class-name, then this is not a constructor. */
14674 constructor_p = !cp_parser_error_occurred (parser);
14677 /* If we're still considering a constructor, we have to see a `(',
14678 to begin the parameter-declaration-clause, followed by either a
14679 `)', an `...', or a decl-specifier. We need to check for a
14680 type-specifier to avoid being fooled into thinking that:
14682 S::S (f) (int);
14684 is a constructor. (It is actually a function named `f' that
14685 takes one parameter (of type `int') and returns a value of type
14686 `S::S'. */
14687 if (constructor_p
14688 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14691 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14692 /* A parameter declaration begins with a decl-specifier,
14693 which is either the "attribute" keyword, a storage class
14694 specifier, or (usually) a type-specifier. */
14695 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14696 && !cp_parser_storage_class_specifier_opt (parser))
14698 tree type;
14699 tree pushed_scope = NULL_TREE;
14700 unsigned saved_num_template_parameter_lists;
14702 /* Names appearing in the type-specifier should be looked up
14703 in the scope of the class. */
14704 if (current_class_type)
14705 type = NULL_TREE;
14706 else
14708 type = TREE_TYPE (type_decl);
14709 if (TREE_CODE (type) == TYPENAME_TYPE)
14711 type = resolve_typename_type (type,
14712 /*only_current_p=*/false);
14713 if (type == error_mark_node)
14715 cp_parser_abort_tentative_parse (parser);
14716 return false;
14719 pushed_scope = push_scope (type);
14722 /* Inside the constructor parameter list, surrounding
14723 template-parameter-lists do not apply. */
14724 saved_num_template_parameter_lists
14725 = parser->num_template_parameter_lists;
14726 parser->num_template_parameter_lists = 0;
14728 /* Look for the type-specifier. */
14729 cp_parser_type_specifier (parser,
14730 CP_PARSER_FLAGS_NONE,
14731 /*decl_specs=*/NULL,
14732 /*is_declarator=*/true,
14733 /*declares_class_or_enum=*/NULL,
14734 /*is_cv_qualifier=*/NULL);
14736 parser->num_template_parameter_lists
14737 = saved_num_template_parameter_lists;
14739 /* Leave the scope of the class. */
14740 if (pushed_scope)
14741 pop_scope (pushed_scope);
14743 constructor_p = !cp_parser_error_occurred (parser);
14746 else
14747 constructor_p = false;
14748 /* We did not really want to consume any tokens. */
14749 cp_parser_abort_tentative_parse (parser);
14751 return constructor_p;
14754 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14755 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14756 they must be performed once we are in the scope of the function.
14758 Returns the function defined. */
14760 static tree
14761 cp_parser_function_definition_from_specifiers_and_declarator
14762 (cp_parser* parser,
14763 cp_decl_specifier_seq *decl_specifiers,
14764 tree attributes,
14765 const cp_declarator *declarator)
14767 tree fn;
14768 bool success_p;
14770 /* Begin the function-definition. */
14771 success_p = start_function (decl_specifiers, declarator, attributes);
14773 /* The things we're about to see are not directly qualified by any
14774 template headers we've seen thus far. */
14775 reset_specialization ();
14777 /* If there were names looked up in the decl-specifier-seq that we
14778 did not check, check them now. We must wait until we are in the
14779 scope of the function to perform the checks, since the function
14780 might be a friend. */
14781 perform_deferred_access_checks ();
14783 if (!success_p)
14785 /* Skip the entire function. */
14786 error ("invalid function declaration");
14787 cp_parser_skip_to_end_of_block_or_statement (parser);
14788 fn = error_mark_node;
14790 else
14791 fn = cp_parser_function_definition_after_declarator (parser,
14792 /*inline_p=*/false);
14794 return fn;
14797 /* Parse the part of a function-definition that follows the
14798 declarator. INLINE_P is TRUE iff this function is an inline
14799 function defined with a class-specifier.
14801 Returns the function defined. */
14803 static tree
14804 cp_parser_function_definition_after_declarator (cp_parser* parser,
14805 bool inline_p)
14807 tree fn;
14808 bool ctor_initializer_p = false;
14809 bool saved_in_unbraced_linkage_specification_p;
14810 unsigned saved_num_template_parameter_lists;
14812 /* If the next token is `return', then the code may be trying to
14813 make use of the "named return value" extension that G++ used to
14814 support. */
14815 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14817 /* Consume the `return' keyword. */
14818 cp_lexer_consume_token (parser->lexer);
14819 /* Look for the identifier that indicates what value is to be
14820 returned. */
14821 cp_parser_identifier (parser);
14822 /* Issue an error message. */
14823 error ("named return values are no longer supported");
14824 /* Skip tokens until we reach the start of the function body. */
14825 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14826 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14827 cp_lexer_consume_token (parser->lexer);
14829 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14830 anything declared inside `f'. */
14831 saved_in_unbraced_linkage_specification_p
14832 = parser->in_unbraced_linkage_specification_p;
14833 parser->in_unbraced_linkage_specification_p = false;
14834 /* Inside the function, surrounding template-parameter-lists do not
14835 apply. */
14836 saved_num_template_parameter_lists
14837 = parser->num_template_parameter_lists;
14838 parser->num_template_parameter_lists = 0;
14839 /* If the next token is `try', then we are looking at a
14840 function-try-block. */
14841 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14842 ctor_initializer_p = cp_parser_function_try_block (parser);
14843 /* A function-try-block includes the function-body, so we only do
14844 this next part if we're not processing a function-try-block. */
14845 else
14846 ctor_initializer_p
14847 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14849 /* Finish the function. */
14850 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14851 (inline_p ? 2 : 0));
14852 /* Generate code for it, if necessary. */
14853 expand_or_defer_fn (fn);
14854 /* Restore the saved values. */
14855 parser->in_unbraced_linkage_specification_p
14856 = saved_in_unbraced_linkage_specification_p;
14857 parser->num_template_parameter_lists
14858 = saved_num_template_parameter_lists;
14860 return fn;
14863 /* Parse a template-declaration, assuming that the `export' (and
14864 `extern') keywords, if present, has already been scanned. MEMBER_P
14865 is as for cp_parser_template_declaration. */
14867 static void
14868 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14870 tree decl = NULL_TREE;
14871 tree parameter_list;
14872 bool friend_p = false;
14874 /* Look for the `template' keyword. */
14875 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14876 return;
14878 /* And the `<'. */
14879 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14880 return;
14882 /* If the next token is `>', then we have an invalid
14883 specialization. Rather than complain about an invalid template
14884 parameter, issue an error message here. */
14885 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14887 cp_parser_error (parser, "invalid explicit specialization");
14888 begin_specialization ();
14889 parameter_list = NULL_TREE;
14891 else
14893 /* Parse the template parameters. */
14894 begin_template_parm_list ();
14895 parameter_list = cp_parser_template_parameter_list (parser);
14896 parameter_list = end_template_parm_list (parameter_list);
14899 /* Look for the `>'. */
14900 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14901 /* We just processed one more parameter list. */
14902 ++parser->num_template_parameter_lists;
14903 /* If the next token is `template', there are more template
14904 parameters. */
14905 if (cp_lexer_next_token_is_keyword (parser->lexer,
14906 RID_TEMPLATE))
14907 cp_parser_template_declaration_after_export (parser, member_p);
14908 else
14910 /* There are no access checks when parsing a template, as we do not
14911 know if a specialization will be a friend. */
14912 push_deferring_access_checks (dk_no_check);
14914 decl = cp_parser_single_declaration (parser,
14915 member_p,
14916 &friend_p);
14918 pop_deferring_access_checks ();
14920 /* If this is a member template declaration, let the front
14921 end know. */
14922 if (member_p && !friend_p && decl)
14924 if (TREE_CODE (decl) == TYPE_DECL)
14925 cp_parser_check_access_in_redeclaration (decl);
14927 decl = finish_member_template_decl (decl);
14929 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14930 make_friend_class (current_class_type, TREE_TYPE (decl),
14931 /*complain=*/true);
14933 /* We are done with the current parameter list. */
14934 --parser->num_template_parameter_lists;
14936 /* Finish up. */
14937 finish_template_decl (parameter_list);
14939 /* Register member declarations. */
14940 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14941 finish_member_declaration (decl);
14943 /* If DECL is a function template, we must return to parse it later.
14944 (Even though there is no definition, there might be default
14945 arguments that need handling.) */
14946 if (member_p && decl
14947 && (TREE_CODE (decl) == FUNCTION_DECL
14948 || DECL_FUNCTION_TEMPLATE_P (decl)))
14949 TREE_VALUE (parser->unparsed_functions_queues)
14950 = tree_cons (NULL_TREE, decl,
14951 TREE_VALUE (parser->unparsed_functions_queues));
14954 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14955 `function-definition' sequence. MEMBER_P is true, this declaration
14956 appears in a class scope.
14958 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14959 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14961 static tree
14962 cp_parser_single_declaration (cp_parser* parser,
14963 bool member_p,
14964 bool* friend_p)
14966 int declares_class_or_enum;
14967 tree decl = NULL_TREE;
14968 cp_decl_specifier_seq decl_specifiers;
14969 bool function_definition_p = false;
14971 /* This function is only used when processing a template
14972 declaration. */
14973 gcc_assert (innermost_scope_kind () == sk_template_parms
14974 || innermost_scope_kind () == sk_template_spec);
14976 /* Defer access checks until we know what is being declared. */
14977 push_deferring_access_checks (dk_deferred);
14979 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14980 alternative. */
14981 cp_parser_decl_specifier_seq (parser,
14982 CP_PARSER_FLAGS_OPTIONAL,
14983 &decl_specifiers,
14984 &declares_class_or_enum);
14985 if (friend_p)
14986 *friend_p = cp_parser_friend_p (&decl_specifiers);
14988 /* There are no template typedefs. */
14989 if (decl_specifiers.specs[(int) ds_typedef])
14991 error ("template declaration of %qs", "typedef");
14992 decl = error_mark_node;
14995 /* Gather up the access checks that occurred the
14996 decl-specifier-seq. */
14997 stop_deferring_access_checks ();
14999 /* Check for the declaration of a template class. */
15000 if (declares_class_or_enum)
15002 if (cp_parser_declares_only_class_p (parser))
15004 decl = shadow_tag (&decl_specifiers);
15006 /* In this case:
15008 struct C {
15009 friend template <typename T> struct A<T>::B;
15012 A<T>::B will be represented by a TYPENAME_TYPE, and
15013 therefore not recognized by shadow_tag. */
15014 if (friend_p && *friend_p
15015 && !decl
15016 && decl_specifiers.type
15017 && TYPE_P (decl_specifiers.type))
15018 decl = decl_specifiers.type;
15020 if (decl && decl != error_mark_node)
15021 decl = TYPE_NAME (decl);
15022 else
15023 decl = error_mark_node;
15026 /* If it's not a template class, try for a template function. If
15027 the next token is a `;', then this declaration does not declare
15028 anything. But, if there were errors in the decl-specifiers, then
15029 the error might well have come from an attempted class-specifier.
15030 In that case, there's no need to warn about a missing declarator. */
15031 if (!decl
15032 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15033 || decl_specifiers.type != error_mark_node))
15034 decl = cp_parser_init_declarator (parser,
15035 &decl_specifiers,
15036 /*function_definition_allowed_p=*/true,
15037 member_p,
15038 declares_class_or_enum,
15039 &function_definition_p);
15041 pop_deferring_access_checks ();
15043 /* Clear any current qualification; whatever comes next is the start
15044 of something new. */
15045 parser->scope = NULL_TREE;
15046 parser->qualifying_scope = NULL_TREE;
15047 parser->object_scope = NULL_TREE;
15048 /* Look for a trailing `;' after the declaration. */
15049 if (!function_definition_p
15050 && (decl == error_mark_node
15051 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15052 cp_parser_skip_to_end_of_block_or_statement (parser);
15054 return decl;
15057 /* Parse a cast-expression that is not the operand of a unary "&". */
15059 static tree
15060 cp_parser_simple_cast_expression (cp_parser *parser)
15062 return cp_parser_cast_expression (parser, /*address_p=*/false,
15063 /*cast_p=*/false);
15066 /* Parse a functional cast to TYPE. Returns an expression
15067 representing the cast. */
15069 static tree
15070 cp_parser_functional_cast (cp_parser* parser, tree type)
15072 tree expression_list;
15073 tree cast;
15075 expression_list
15076 = cp_parser_parenthesized_expression_list (parser, false,
15077 /*cast_p=*/true,
15078 /*non_constant_p=*/NULL);
15080 cast = build_functional_cast (type, expression_list);
15081 /* [expr.const]/1: In an integral constant expression "only type
15082 conversions to integral or enumeration type can be used". */
15083 if (cast != error_mark_node && !type_dependent_expression_p (type)
15084 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15086 if (cp_parser_non_integral_constant_expression
15087 (parser, "a call to a constructor"))
15088 return error_mark_node;
15090 return cast;
15093 /* Save the tokens that make up the body of a member function defined
15094 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15095 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15096 specifiers applied to the declaration. Returns the FUNCTION_DECL
15097 for the member function. */
15099 static tree
15100 cp_parser_save_member_function_body (cp_parser* parser,
15101 cp_decl_specifier_seq *decl_specifiers,
15102 cp_declarator *declarator,
15103 tree attributes)
15105 cp_token *first;
15106 cp_token *last;
15107 tree fn;
15109 /* Create the function-declaration. */
15110 fn = start_method (decl_specifiers, declarator, attributes);
15111 /* If something went badly wrong, bail out now. */
15112 if (fn == error_mark_node)
15114 /* If there's a function-body, skip it. */
15115 if (cp_parser_token_starts_function_definition_p
15116 (cp_lexer_peek_token (parser->lexer)))
15117 cp_parser_skip_to_end_of_block_or_statement (parser);
15118 return error_mark_node;
15121 /* Remember it, if there default args to post process. */
15122 cp_parser_save_default_args (parser, fn);
15124 /* Save away the tokens that make up the body of the
15125 function. */
15126 first = parser->lexer->next_token;
15127 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15128 /* Handle function try blocks. */
15129 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15130 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15131 last = parser->lexer->next_token;
15133 /* Save away the inline definition; we will process it when the
15134 class is complete. */
15135 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15136 DECL_PENDING_INLINE_P (fn) = 1;
15138 /* We need to know that this was defined in the class, so that
15139 friend templates are handled correctly. */
15140 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15142 /* We're done with the inline definition. */
15143 finish_method (fn);
15145 /* Add FN to the queue of functions to be parsed later. */
15146 TREE_VALUE (parser->unparsed_functions_queues)
15147 = tree_cons (NULL_TREE, fn,
15148 TREE_VALUE (parser->unparsed_functions_queues));
15150 return fn;
15153 /* Parse a template-argument-list, as well as the trailing ">" (but
15154 not the opening ">"). See cp_parser_template_argument_list for the
15155 return value. */
15157 static tree
15158 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15160 tree arguments;
15161 tree saved_scope;
15162 tree saved_qualifying_scope;
15163 tree saved_object_scope;
15164 bool saved_greater_than_is_operator_p;
15166 /* [temp.names]
15168 When parsing a template-id, the first non-nested `>' is taken as
15169 the end of the template-argument-list rather than a greater-than
15170 operator. */
15171 saved_greater_than_is_operator_p
15172 = parser->greater_than_is_operator_p;
15173 parser->greater_than_is_operator_p = false;
15174 /* Parsing the argument list may modify SCOPE, so we save it
15175 here. */
15176 saved_scope = parser->scope;
15177 saved_qualifying_scope = parser->qualifying_scope;
15178 saved_object_scope = parser->object_scope;
15179 /* Parse the template-argument-list itself. */
15180 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15181 arguments = NULL_TREE;
15182 else
15183 arguments = cp_parser_template_argument_list (parser);
15184 /* Look for the `>' that ends the template-argument-list. If we find
15185 a '>>' instead, it's probably just a typo. */
15186 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15188 if (!saved_greater_than_is_operator_p)
15190 /* If we're in a nested template argument list, the '>>' has
15191 to be a typo for '> >'. We emit the error message, but we
15192 continue parsing and we push a '>' as next token, so that
15193 the argument list will be parsed correctly. Note that the
15194 global source location is still on the token before the
15195 '>>', so we need to say explicitly where we want it. */
15196 cp_token *token = cp_lexer_peek_token (parser->lexer);
15197 error ("%H%<>>%> should be %<> >%> "
15198 "within a nested template argument list",
15199 &token->location);
15201 /* ??? Proper recovery should terminate two levels of
15202 template argument list here. */
15203 token->type = CPP_GREATER;
15205 else
15207 /* If this is not a nested template argument list, the '>>'
15208 is a typo for '>'. Emit an error message and continue.
15209 Same deal about the token location, but here we can get it
15210 right by consuming the '>>' before issuing the diagnostic. */
15211 cp_lexer_consume_token (parser->lexer);
15212 error ("spurious %<>>%>, use %<>%> to terminate "
15213 "a template argument list");
15216 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15217 error ("missing %<>%> to terminate the template argument list");
15218 else
15219 /* It's what we want, a '>'; consume it. */
15220 cp_lexer_consume_token (parser->lexer);
15221 /* The `>' token might be a greater-than operator again now. */
15222 parser->greater_than_is_operator_p
15223 = saved_greater_than_is_operator_p;
15224 /* Restore the SAVED_SCOPE. */
15225 parser->scope = saved_scope;
15226 parser->qualifying_scope = saved_qualifying_scope;
15227 parser->object_scope = saved_object_scope;
15229 return arguments;
15232 /* MEMBER_FUNCTION is a member function, or a friend. If default
15233 arguments, or the body of the function have not yet been parsed,
15234 parse them now. */
15236 static void
15237 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15239 /* If this member is a template, get the underlying
15240 FUNCTION_DECL. */
15241 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15242 member_function = DECL_TEMPLATE_RESULT (member_function);
15244 /* There should not be any class definitions in progress at this
15245 point; the bodies of members are only parsed outside of all class
15246 definitions. */
15247 gcc_assert (parser->num_classes_being_defined == 0);
15248 /* While we're parsing the member functions we might encounter more
15249 classes. We want to handle them right away, but we don't want
15250 them getting mixed up with functions that are currently in the
15251 queue. */
15252 parser->unparsed_functions_queues
15253 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15255 /* Make sure that any template parameters are in scope. */
15256 maybe_begin_member_template_processing (member_function);
15258 /* If the body of the function has not yet been parsed, parse it
15259 now. */
15260 if (DECL_PENDING_INLINE_P (member_function))
15262 tree function_scope;
15263 cp_token_cache *tokens;
15265 /* The function is no longer pending; we are processing it. */
15266 tokens = DECL_PENDING_INLINE_INFO (member_function);
15267 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15268 DECL_PENDING_INLINE_P (member_function) = 0;
15269 /* If this was an inline function in a local class, enter the scope
15270 of the containing function. */
15271 function_scope = decl_function_context (member_function);
15272 if (function_scope)
15273 push_function_context_to (function_scope);
15275 /* Push the body of the function onto the lexer stack. */
15276 cp_parser_push_lexer_for_tokens (parser, tokens);
15278 /* Let the front end know that we going to be defining this
15279 function. */
15280 start_preparsed_function (member_function, NULL_TREE,
15281 SF_PRE_PARSED | SF_INCLASS_INLINE);
15283 /* Now, parse the body of the function. */
15284 cp_parser_function_definition_after_declarator (parser,
15285 /*inline_p=*/true);
15287 /* Leave the scope of the containing function. */
15288 if (function_scope)
15289 pop_function_context_from (function_scope);
15290 cp_parser_pop_lexer (parser);
15293 /* Remove any template parameters from the symbol table. */
15294 maybe_end_member_template_processing ();
15296 /* Restore the queue. */
15297 parser->unparsed_functions_queues
15298 = TREE_CHAIN (parser->unparsed_functions_queues);
15301 /* If DECL contains any default args, remember it on the unparsed
15302 functions queue. */
15304 static void
15305 cp_parser_save_default_args (cp_parser* parser, tree decl)
15307 tree probe;
15309 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15310 probe;
15311 probe = TREE_CHAIN (probe))
15312 if (TREE_PURPOSE (probe))
15314 TREE_PURPOSE (parser->unparsed_functions_queues)
15315 = tree_cons (current_class_type, decl,
15316 TREE_PURPOSE (parser->unparsed_functions_queues));
15317 break;
15319 return;
15322 /* FN is a FUNCTION_DECL which may contains a parameter with an
15323 unparsed DEFAULT_ARG. Parse the default args now. This function
15324 assumes that the current scope is the scope in which the default
15325 argument should be processed. */
15327 static void
15328 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15330 bool saved_local_variables_forbidden_p;
15331 tree parm;
15333 /* While we're parsing the default args, we might (due to the
15334 statement expression extension) encounter more classes. We want
15335 to handle them right away, but we don't want them getting mixed
15336 up with default args that are currently in the queue. */
15337 parser->unparsed_functions_queues
15338 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15340 /* Local variable names (and the `this' keyword) may not appear
15341 in a default argument. */
15342 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15343 parser->local_variables_forbidden_p = true;
15345 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15346 parm;
15347 parm = TREE_CHAIN (parm))
15349 cp_token_cache *tokens;
15351 if (!TREE_PURPOSE (parm)
15352 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15353 continue;
15355 /* Push the saved tokens for the default argument onto the parser's
15356 lexer stack. */
15357 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15358 cp_parser_push_lexer_for_tokens (parser, tokens);
15360 /* Parse the assignment-expression. */
15361 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser,
15362 /*cast_p=*/false);
15364 /* If the token stream has not been completely used up, then
15365 there was extra junk after the end of the default
15366 argument. */
15367 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15368 cp_parser_error (parser, "expected %<,%>");
15370 /* Revert to the main lexer. */
15371 cp_parser_pop_lexer (parser);
15374 /* Restore the state of local_variables_forbidden_p. */
15375 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15377 /* Restore the queue. */
15378 parser->unparsed_functions_queues
15379 = TREE_CHAIN (parser->unparsed_functions_queues);
15382 /* Parse the operand of `sizeof' (or a similar operator). Returns
15383 either a TYPE or an expression, depending on the form of the
15384 input. The KEYWORD indicates which kind of expression we have
15385 encountered. */
15387 static tree
15388 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15390 static const char *format;
15391 tree expr = NULL_TREE;
15392 const char *saved_message;
15393 bool saved_integral_constant_expression_p;
15394 bool saved_non_integral_constant_expression_p;
15396 /* Initialize FORMAT the first time we get here. */
15397 if (!format)
15398 format = "types may not be defined in '%s' expressions";
15400 /* Types cannot be defined in a `sizeof' expression. Save away the
15401 old message. */
15402 saved_message = parser->type_definition_forbidden_message;
15403 /* And create the new one. */
15404 parser->type_definition_forbidden_message
15405 = xmalloc (strlen (format)
15406 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15407 + 1 /* `\0' */);
15408 sprintf ((char *) parser->type_definition_forbidden_message,
15409 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15411 /* The restrictions on constant-expressions do not apply inside
15412 sizeof expressions. */
15413 saved_integral_constant_expression_p
15414 = parser->integral_constant_expression_p;
15415 saved_non_integral_constant_expression_p
15416 = parser->non_integral_constant_expression_p;
15417 parser->integral_constant_expression_p = false;
15419 /* Do not actually evaluate the expression. */
15420 ++skip_evaluation;
15421 /* If it's a `(', then we might be looking at the type-id
15422 construction. */
15423 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15425 tree type;
15426 bool saved_in_type_id_in_expr_p;
15428 /* We can't be sure yet whether we're looking at a type-id or an
15429 expression. */
15430 cp_parser_parse_tentatively (parser);
15431 /* Consume the `('. */
15432 cp_lexer_consume_token (parser->lexer);
15433 /* Parse the type-id. */
15434 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15435 parser->in_type_id_in_expr_p = true;
15436 type = cp_parser_type_id (parser);
15437 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15438 /* Now, look for the trailing `)'. */
15439 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15440 /* If all went well, then we're done. */
15441 if (cp_parser_parse_definitely (parser))
15443 cp_decl_specifier_seq decl_specs;
15445 /* Build a trivial decl-specifier-seq. */
15446 clear_decl_specs (&decl_specs);
15447 decl_specs.type = type;
15449 /* Call grokdeclarator to figure out what type this is. */
15450 expr = grokdeclarator (NULL,
15451 &decl_specs,
15452 TYPENAME,
15453 /*initialized=*/0,
15454 /*attrlist=*/NULL);
15458 /* If the type-id production did not work out, then we must be
15459 looking at the unary-expression production. */
15460 if (!expr)
15461 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15462 /*cast_p=*/false);
15463 /* Go back to evaluating expressions. */
15464 --skip_evaluation;
15466 /* Free the message we created. */
15467 free ((char *) parser->type_definition_forbidden_message);
15468 /* And restore the old one. */
15469 parser->type_definition_forbidden_message = saved_message;
15470 parser->integral_constant_expression_p
15471 = saved_integral_constant_expression_p;
15472 parser->non_integral_constant_expression_p
15473 = saved_non_integral_constant_expression_p;
15475 return expr;
15478 /* If the current declaration has no declarator, return true. */
15480 static bool
15481 cp_parser_declares_only_class_p (cp_parser *parser)
15483 /* If the next token is a `;' or a `,' then there is no
15484 declarator. */
15485 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15486 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15489 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15491 static void
15492 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15493 cp_storage_class storage_class)
15495 if (decl_specs->storage_class != sc_none)
15496 decl_specs->multiple_storage_classes_p = true;
15497 else
15498 decl_specs->storage_class = storage_class;
15501 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15502 is true, the type is a user-defined type; otherwise it is a
15503 built-in type specified by a keyword. */
15505 static void
15506 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15507 tree type_spec,
15508 bool user_defined_p)
15510 decl_specs->any_specifiers_p = true;
15512 /* If the user tries to redeclare bool or wchar_t (with, for
15513 example, in "typedef int wchar_t;") we remember that this is what
15514 happened. In system headers, we ignore these declarations so
15515 that G++ can work with system headers that are not C++-safe. */
15516 if (decl_specs->specs[(int) ds_typedef]
15517 && !user_defined_p
15518 && (type_spec == boolean_type_node
15519 || type_spec == wchar_type_node)
15520 && (decl_specs->type
15521 || decl_specs->specs[(int) ds_long]
15522 || decl_specs->specs[(int) ds_short]
15523 || decl_specs->specs[(int) ds_unsigned]
15524 || decl_specs->specs[(int) ds_signed]))
15526 decl_specs->redefined_builtin_type = type_spec;
15527 if (!decl_specs->type)
15529 decl_specs->type = type_spec;
15530 decl_specs->user_defined_type_p = false;
15533 else if (decl_specs->type)
15534 decl_specs->multiple_types_p = true;
15535 else
15537 decl_specs->type = type_spec;
15538 decl_specs->user_defined_type_p = user_defined_p;
15539 decl_specs->redefined_builtin_type = NULL_TREE;
15543 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15544 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15546 static bool
15547 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15549 return decl_specifiers->specs[(int) ds_friend] != 0;
15552 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15553 issue an error message indicating that TOKEN_DESC was expected.
15555 Returns the token consumed, if the token had the appropriate type.
15556 Otherwise, returns NULL. */
15558 static cp_token *
15559 cp_parser_require (cp_parser* parser,
15560 enum cpp_ttype type,
15561 const char* token_desc)
15563 if (cp_lexer_next_token_is (parser->lexer, type))
15564 return cp_lexer_consume_token (parser->lexer);
15565 else
15567 /* Output the MESSAGE -- unless we're parsing tentatively. */
15568 if (!cp_parser_simulate_error (parser))
15570 char *message = concat ("expected ", token_desc, NULL);
15571 cp_parser_error (parser, message);
15572 free (message);
15574 return NULL;
15578 /* Like cp_parser_require, except that tokens will be skipped until
15579 the desired token is found. An error message is still produced if
15580 the next token is not as expected. */
15582 static void
15583 cp_parser_skip_until_found (cp_parser* parser,
15584 enum cpp_ttype type,
15585 const char* token_desc)
15587 cp_token *token;
15588 unsigned nesting_depth = 0;
15590 if (cp_parser_require (parser, type, token_desc))
15591 return;
15593 /* Skip tokens until the desired token is found. */
15594 while (true)
15596 /* Peek at the next token. */
15597 token = cp_lexer_peek_token (parser->lexer);
15598 /* If we've reached the token we want, consume it and
15599 stop. */
15600 if (token->type == type && !nesting_depth)
15602 cp_lexer_consume_token (parser->lexer);
15603 return;
15605 /* If we've run out of tokens, stop. */
15606 if (token->type == CPP_EOF)
15607 return;
15608 if (token->type == CPP_OPEN_BRACE
15609 || token->type == CPP_OPEN_PAREN
15610 || token->type == CPP_OPEN_SQUARE)
15611 ++nesting_depth;
15612 else if (token->type == CPP_CLOSE_BRACE
15613 || token->type == CPP_CLOSE_PAREN
15614 || token->type == CPP_CLOSE_SQUARE)
15616 if (nesting_depth-- == 0)
15617 return;
15619 /* Consume this token. */
15620 cp_lexer_consume_token (parser->lexer);
15624 /* If the next token is the indicated keyword, consume it. Otherwise,
15625 issue an error message indicating that TOKEN_DESC was expected.
15627 Returns the token consumed, if the token had the appropriate type.
15628 Otherwise, returns NULL. */
15630 static cp_token *
15631 cp_parser_require_keyword (cp_parser* parser,
15632 enum rid keyword,
15633 const char* token_desc)
15635 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15637 if (token && token->keyword != keyword)
15639 dyn_string_t error_msg;
15641 /* Format the error message. */
15642 error_msg = dyn_string_new (0);
15643 dyn_string_append_cstr (error_msg, "expected ");
15644 dyn_string_append_cstr (error_msg, token_desc);
15645 cp_parser_error (parser, error_msg->s);
15646 dyn_string_delete (error_msg);
15647 return NULL;
15650 return token;
15653 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15654 function-definition. */
15656 static bool
15657 cp_parser_token_starts_function_definition_p (cp_token* token)
15659 return (/* An ordinary function-body begins with an `{'. */
15660 token->type == CPP_OPEN_BRACE
15661 /* A ctor-initializer begins with a `:'. */
15662 || token->type == CPP_COLON
15663 /* A function-try-block begins with `try'. */
15664 || token->keyword == RID_TRY
15665 /* The named return value extension begins with `return'. */
15666 || token->keyword == RID_RETURN);
15669 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15670 definition. */
15672 static bool
15673 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15675 cp_token *token;
15677 token = cp_lexer_peek_token (parser->lexer);
15678 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15681 /* Returns TRUE iff the next token is the "," or ">" ending a
15682 template-argument. */
15684 static bool
15685 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15687 cp_token *token;
15689 token = cp_lexer_peek_token (parser->lexer);
15690 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15693 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15694 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15696 static bool
15697 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15698 size_t n)
15700 cp_token *token;
15702 token = cp_lexer_peek_nth_token (parser->lexer, n);
15703 if (token->type == CPP_LESS)
15704 return true;
15705 /* Check for the sequence `<::' in the original code. It would be lexed as
15706 `[:', where `[' is a digraph, and there is no whitespace before
15707 `:'. */
15708 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15710 cp_token *token2;
15711 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15712 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15713 return true;
15715 return false;
15718 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15719 or none_type otherwise. */
15721 static enum tag_types
15722 cp_parser_token_is_class_key (cp_token* token)
15724 switch (token->keyword)
15726 case RID_CLASS:
15727 return class_type;
15728 case RID_STRUCT:
15729 return record_type;
15730 case RID_UNION:
15731 return union_type;
15733 default:
15734 return none_type;
15738 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15740 static void
15741 cp_parser_check_class_key (enum tag_types class_key, tree type)
15743 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15744 pedwarn ("%qs tag used in naming %q#T",
15745 class_key == union_type ? "union"
15746 : class_key == record_type ? "struct" : "class",
15747 type);
15750 /* Issue an error message if DECL is redeclared with different
15751 access than its original declaration [class.access.spec/3].
15752 This applies to nested classes and nested class templates.
15753 [class.mem/1]. */
15755 static void
15756 cp_parser_check_access_in_redeclaration (tree decl)
15758 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15759 return;
15761 if ((TREE_PRIVATE (decl)
15762 != (current_access_specifier == access_private_node))
15763 || (TREE_PROTECTED (decl)
15764 != (current_access_specifier == access_protected_node)))
15765 error ("%qD redeclared with different access", decl);
15768 /* Look for the `template' keyword, as a syntactic disambiguator.
15769 Return TRUE iff it is present, in which case it will be
15770 consumed. */
15772 static bool
15773 cp_parser_optional_template_keyword (cp_parser *parser)
15775 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15777 /* The `template' keyword can only be used within templates;
15778 outside templates the parser can always figure out what is a
15779 template and what is not. */
15780 if (!processing_template_decl)
15782 error ("%<template%> (as a disambiguator) is only allowed "
15783 "within templates");
15784 /* If this part of the token stream is rescanned, the same
15785 error message would be generated. So, we purge the token
15786 from the stream. */
15787 cp_lexer_purge_token (parser->lexer);
15788 return false;
15790 else
15792 /* Consume the `template' keyword. */
15793 cp_lexer_consume_token (parser->lexer);
15794 return true;
15798 return false;
15801 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15802 set PARSER->SCOPE, and perform other related actions. */
15804 static void
15805 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15807 tree value;
15808 tree check;
15810 /* Get the stored value. */
15811 value = cp_lexer_consume_token (parser->lexer)->value;
15812 /* Perform any access checks that were deferred. */
15813 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15814 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15815 /* Set the scope from the stored value. */
15816 parser->scope = TREE_VALUE (value);
15817 parser->qualifying_scope = TREE_TYPE (value);
15818 parser->object_scope = NULL_TREE;
15821 /* Consume tokens up through a non-nested END token. */
15823 static void
15824 cp_parser_cache_group (cp_parser *parser,
15825 enum cpp_ttype end,
15826 unsigned depth)
15828 while (true)
15830 cp_token *token;
15832 /* Abort a parenthesized expression if we encounter a brace. */
15833 if ((end == CPP_CLOSE_PAREN || depth == 0)
15834 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15835 return;
15836 /* If we've reached the end of the file, stop. */
15837 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15838 return;
15839 /* Consume the next token. */
15840 token = cp_lexer_consume_token (parser->lexer);
15841 /* See if it starts a new group. */
15842 if (token->type == CPP_OPEN_BRACE)
15844 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15845 if (depth == 0)
15846 return;
15848 else if (token->type == CPP_OPEN_PAREN)
15849 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15850 else if (token->type == end)
15851 return;
15855 /* Begin parsing tentatively. We always save tokens while parsing
15856 tentatively so that if the tentative parsing fails we can restore the
15857 tokens. */
15859 static void
15860 cp_parser_parse_tentatively (cp_parser* parser)
15862 /* Enter a new parsing context. */
15863 parser->context = cp_parser_context_new (parser->context);
15864 /* Begin saving tokens. */
15865 cp_lexer_save_tokens (parser->lexer);
15866 /* In order to avoid repetitive access control error messages,
15867 access checks are queued up until we are no longer parsing
15868 tentatively. */
15869 push_deferring_access_checks (dk_deferred);
15872 /* Commit to the currently active tentative parse. */
15874 static void
15875 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15877 cp_parser_context *context;
15878 cp_lexer *lexer;
15880 /* Mark all of the levels as committed. */
15881 lexer = parser->lexer;
15882 for (context = parser->context; context->next; context = context->next)
15884 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15885 break;
15886 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15887 while (!cp_lexer_saving_tokens (lexer))
15888 lexer = lexer->next;
15889 cp_lexer_commit_tokens (lexer);
15893 /* Abort the currently active tentative parse. All consumed tokens
15894 will be rolled back, and no diagnostics will be issued. */
15896 static void
15897 cp_parser_abort_tentative_parse (cp_parser* parser)
15899 cp_parser_simulate_error (parser);
15900 /* Now, pretend that we want to see if the construct was
15901 successfully parsed. */
15902 cp_parser_parse_definitely (parser);
15905 /* Stop parsing tentatively. If a parse error has occurred, restore the
15906 token stream. Otherwise, commit to the tokens we have consumed.
15907 Returns true if no error occurred; false otherwise. */
15909 static bool
15910 cp_parser_parse_definitely (cp_parser* parser)
15912 bool error_occurred;
15913 cp_parser_context *context;
15915 /* Remember whether or not an error occurred, since we are about to
15916 destroy that information. */
15917 error_occurred = cp_parser_error_occurred (parser);
15918 /* Remove the topmost context from the stack. */
15919 context = parser->context;
15920 parser->context = context->next;
15921 /* If no parse errors occurred, commit to the tentative parse. */
15922 if (!error_occurred)
15924 /* Commit to the tokens read tentatively, unless that was
15925 already done. */
15926 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15927 cp_lexer_commit_tokens (parser->lexer);
15929 pop_to_parent_deferring_access_checks ();
15931 /* Otherwise, if errors occurred, roll back our state so that things
15932 are just as they were before we began the tentative parse. */
15933 else
15935 cp_lexer_rollback_tokens (parser->lexer);
15936 pop_deferring_access_checks ();
15938 /* Add the context to the front of the free list. */
15939 context->next = cp_parser_context_free_list;
15940 cp_parser_context_free_list = context;
15942 return !error_occurred;
15945 /* Returns true if we are parsing tentatively and are not committed to
15946 this tentative parse. */
15948 static bool
15949 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
15951 return (cp_parser_parsing_tentatively (parser)
15952 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
15955 /* Returns nonzero iff an error has occurred during the most recent
15956 tentative parse. */
15958 static bool
15959 cp_parser_error_occurred (cp_parser* parser)
15961 return (cp_parser_parsing_tentatively (parser)
15962 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15965 /* Returns nonzero if GNU extensions are allowed. */
15967 static bool
15968 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15970 return parser->allow_gnu_extensions_p;
15974 /* The parser. */
15976 static GTY (()) cp_parser *the_parser;
15978 /* External interface. */
15980 /* Parse one entire translation unit. */
15982 void
15983 c_parse_file (void)
15985 bool error_occurred;
15986 static bool already_called = false;
15988 if (already_called)
15990 sorry ("inter-module optimizations not implemented for C++");
15991 return;
15993 already_called = true;
15995 the_parser = cp_parser_new ();
15996 push_deferring_access_checks (flag_access_control
15997 ? dk_no_deferred : dk_no_check);
15998 error_occurred = cp_parser_translation_unit (the_parser);
15999 the_parser = NULL;
16002 /* This variable must be provided by every front end. */
16004 int yydebug;
16006 #include "gt-cp-parser.h"