Mark Mitchell <mark@codesourcery.com>
[official-gcc.git] / gcc / cp / parser.c
blobe5a03fcfe436a0f497e137691096de7e95fbbc93
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, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, 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"
39 #include "c-common.h"
42 /* The lexer. */
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A C++ token. */
49 typedef struct cp_token GTY (())
51 /* The kind of token. */
52 ENUM_BITFIELD (cpp_ttype) type : 8;
53 /* If this token is a keyword, this value indicates which keyword.
54 Otherwise, this value is RID_MAX. */
55 ENUM_BITFIELD (rid) keyword : 8;
56 /* Token flags. */
57 unsigned char flags;
58 /* True if this token is from a system header. */
59 BOOL_BITFIELD in_system_header : 1;
60 /* True if this token is from a context where it is implicitly extern "C" */
61 BOOL_BITFIELD implicit_extern_c : 1;
62 /* The value associated with this token, if any. */
63 tree value;
64 /* The location at which this token was found. */
65 location_t location;
66 } cp_token;
68 /* We use a stack of token pointer for saving token sets. */
69 typedef struct cp_token *cp_token_position;
70 DEF_VEC_P (cp_token_position);
71 DEF_VEC_ALLOC_P (cp_token_position,heap);
73 static const cp_token eof_token =
75 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
76 #if USE_MAPPED_LOCATION
78 #else
79 {0, 0}
80 #endif
83 /* The cp_lexer structure represents the C++ lexer. It is responsible
84 for managing the token stream from the preprocessor and supplying
85 it to the parser. Tokens are never added to the cp_lexer after
86 it is created. */
88 typedef struct cp_lexer GTY (())
90 /* The memory allocated for the buffer. NULL if this lexer does not
91 own the token buffer. */
92 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
93 /* If the lexer owns the buffer, this is the number of tokens in the
94 buffer. */
95 size_t buffer_length;
97 /* A pointer just past the last available token. The tokens
98 in this lexer are [buffer, last_token). */
99 cp_token_position GTY ((skip)) last_token;
101 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
102 no more available tokens. */
103 cp_token_position GTY ((skip)) next_token;
105 /* A stack indicating positions at which cp_lexer_save_tokens was
106 called. The top entry is the most recent position at which we
107 began saving tokens. If the stack is non-empty, we are saving
108 tokens. */
109 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
111 /* True if we should output debugging information. */
112 bool debugging_p;
114 /* The next lexer in a linked list of lexers. */
115 struct cp_lexer *next;
116 } cp_lexer;
118 /* cp_token_cache is a range of tokens. There is no need to represent
119 allocate heap memory for it, since tokens are never removed from the
120 lexer's array. There is also no need for the GC to walk through
121 a cp_token_cache, since everything in here is referenced through
122 a lexer. */
124 typedef struct cp_token_cache GTY(())
126 /* The beginning of the token range. */
127 cp_token * GTY((skip)) first;
129 /* Points immediately after the last token in the range. */
130 cp_token * GTY ((skip)) last;
131 } cp_token_cache;
133 /* Prototypes. */
135 static cp_lexer *cp_lexer_new_main
136 (void);
137 static cp_lexer *cp_lexer_new_from_tokens
138 (cp_token_cache *tokens);
139 static void cp_lexer_destroy
140 (cp_lexer *);
141 static int cp_lexer_saving_tokens
142 (const cp_lexer *);
143 static cp_token_position cp_lexer_token_position
144 (cp_lexer *, bool);
145 static cp_token *cp_lexer_token_at
146 (cp_lexer *, cp_token_position);
147 static void cp_lexer_get_preprocessor_token
148 (cp_lexer *, cp_token *);
149 static inline cp_token *cp_lexer_peek_token
150 (cp_lexer *);
151 static cp_token *cp_lexer_peek_nth_token
152 (cp_lexer *, size_t);
153 static inline bool cp_lexer_next_token_is
154 (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_not
156 (cp_lexer *, enum cpp_ttype);
157 static bool cp_lexer_next_token_is_keyword
158 (cp_lexer *, enum rid);
159 static cp_token *cp_lexer_consume_token
160 (cp_lexer *);
161 static void cp_lexer_purge_token
162 (cp_lexer *);
163 static void cp_lexer_purge_tokens_after
164 (cp_lexer *, cp_token_position);
165 static void cp_lexer_handle_pragma
166 (cp_lexer *);
167 static void cp_lexer_save_tokens
168 (cp_lexer *);
169 static void cp_lexer_commit_tokens
170 (cp_lexer *);
171 static void cp_lexer_rollback_tokens
172 (cp_lexer *);
173 #ifdef ENABLE_CHECKING
174 static void cp_lexer_print_token
175 (FILE *, cp_token *);
176 static inline bool cp_lexer_debugging_p
177 (cp_lexer *);
178 static void cp_lexer_start_debugging
179 (cp_lexer *) ATTRIBUTE_UNUSED;
180 static void cp_lexer_stop_debugging
181 (cp_lexer *) ATTRIBUTE_UNUSED;
182 #else
183 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
184 about passing NULL to functions that require non-NULL arguments
185 (fputs, fprintf). It will never be used, so all we need is a value
186 of the right type that's guaranteed not to be NULL. */
187 #define cp_lexer_debug_stream stdout
188 #define cp_lexer_print_token(str, tok) (void) 0
189 #define cp_lexer_debugging_p(lexer) 0
190 #endif /* ENABLE_CHECKING */
192 static cp_token_cache *cp_token_cache_new
193 (cp_token *, cp_token *);
195 /* Manifest constants. */
196 #define CP_LEXER_BUFFER_SIZE 10000
197 #define CP_SAVED_TOKEN_STACK 5
199 /* A token type for keywords, as opposed to ordinary identifiers. */
200 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
202 /* A token type for template-ids. If a template-id is processed while
203 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
204 the value of the CPP_TEMPLATE_ID is whatever was returned by
205 cp_parser_template_id. */
206 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
208 /* A token type for nested-name-specifiers. If a
209 nested-name-specifier is processed while parsing tentatively, it is
210 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
211 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
212 cp_parser_nested_name_specifier_opt. */
213 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
215 /* A token type for tokens that are not tokens at all; these are used
216 to represent slots in the array where there used to be a token
217 that has now been deleted. */
218 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
220 /* The number of token types, including C++-specific ones. */
221 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
223 /* Variables. */
225 #ifdef ENABLE_CHECKING
226 /* The stream to which debugging output should be written. */
227 static FILE *cp_lexer_debug_stream;
228 #endif /* ENABLE_CHECKING */
230 /* Create a new main C++ lexer, the lexer that gets tokens from the
231 preprocessor. */
233 static cp_lexer *
234 cp_lexer_new_main (void)
236 cp_token first_token;
237 cp_lexer *lexer;
238 cp_token *pos;
239 size_t alloc;
240 size_t space;
241 cp_token *buffer;
243 /* It's possible that lexing the first token will load a PCH file,
244 which is a GC collection point. So we have to grab the first
245 token before allocating any memory. Pragmas must not be deferred
246 as -fpch-preprocess can generate a pragma to load the PCH file in
247 the preprocessed output used by -save-temps. */
248 cp_lexer_get_preprocessor_token (NULL, &first_token);
250 /* Tell cpplib we want CPP_PRAGMA tokens. */
251 cpp_get_options (parse_in)->defer_pragmas = true;
253 /* Tell c_lex not to merge string constants. */
254 c_lex_return_raw_strings = true;
256 c_common_no_more_pch ();
258 /* Allocate the memory. */
259 lexer = GGC_CNEW (cp_lexer);
261 #ifdef ENABLE_CHECKING
262 /* Initially we are not debugging. */
263 lexer->debugging_p = false;
264 #endif /* ENABLE_CHECKING */
265 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
266 CP_SAVED_TOKEN_STACK);
268 /* Create the buffer. */
269 alloc = CP_LEXER_BUFFER_SIZE;
270 buffer = ggc_alloc (alloc * sizeof (cp_token));
272 /* Put the first token in the buffer. */
273 space = alloc;
274 pos = buffer;
275 *pos = first_token;
277 /* Get the remaining tokens from the preprocessor. */
278 while (pos->type != CPP_EOF)
280 pos++;
281 if (!--space)
283 space = alloc;
284 alloc *= 2;
285 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
286 pos = buffer + space;
288 cp_lexer_get_preprocessor_token (lexer, pos);
290 lexer->buffer = buffer;
291 lexer->buffer_length = alloc - space;
292 lexer->last_token = pos;
293 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
295 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
296 direct calls to c_lex. Those callers all expect c_lex to do
297 string constant concatenation. */
298 c_lex_return_raw_strings = false;
300 gcc_assert (lexer->next_token->type != CPP_PURGED);
301 return lexer;
304 /* Create a new lexer whose token stream is primed with the tokens in
305 CACHE. When these tokens are exhausted, no new tokens will be read. */
307 static cp_lexer *
308 cp_lexer_new_from_tokens (cp_token_cache *cache)
310 cp_token *first = cache->first;
311 cp_token *last = cache->last;
312 cp_lexer *lexer = GGC_CNEW (cp_lexer);
314 /* We do not own the buffer. */
315 lexer->buffer = NULL;
316 lexer->buffer_length = 0;
317 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
318 lexer->last_token = last;
320 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
321 CP_SAVED_TOKEN_STACK);
323 #ifdef ENABLE_CHECKING
324 /* Initially we are not debugging. */
325 lexer->debugging_p = false;
326 #endif
328 gcc_assert (lexer->next_token->type != CPP_PURGED);
329 return lexer;
332 /* Frees all resources associated with LEXER. */
334 static void
335 cp_lexer_destroy (cp_lexer *lexer)
337 if (lexer->buffer)
338 ggc_free (lexer->buffer);
339 VEC_free (cp_token_position, heap, lexer->saved_tokens);
340 ggc_free (lexer);
343 /* Returns nonzero if debugging information should be output. */
345 #ifdef ENABLE_CHECKING
347 static inline bool
348 cp_lexer_debugging_p (cp_lexer *lexer)
350 return lexer->debugging_p;
353 #endif /* ENABLE_CHECKING */
355 static inline cp_token_position
356 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
358 gcc_assert (!previous_p || lexer->next_token != &eof_token);
360 return lexer->next_token - previous_p;
363 static inline cp_token *
364 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
366 return pos;
369 /* nonzero if we are presently saving tokens. */
371 static inline int
372 cp_lexer_saving_tokens (const cp_lexer* lexer)
374 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
377 /* Store the next token from the preprocessor in *TOKEN. Return true
378 if we reach EOF. */
380 static void
381 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
382 cp_token *token)
384 static int is_extern_c = 0;
386 /* Get a new token from the preprocessor. */
387 token->type
388 = c_lex_with_flags (&token->value, &token->location, &token->flags);
389 token->in_system_header = in_system_header;
391 /* On some systems, some header files are surrounded by an
392 implicit extern "C" block. Set a flag in the token if it
393 comes from such a header. */
394 is_extern_c += pending_lang_change;
395 pending_lang_change = 0;
396 token->implicit_extern_c = is_extern_c > 0;
398 /* Check to see if this token is a keyword. */
399 if (token->type == CPP_NAME
400 && C_IS_RESERVED_WORD (token->value))
402 /* Mark this token as a keyword. */
403 token->type = CPP_KEYWORD;
404 /* Record which keyword. */
405 token->keyword = C_RID_CODE (token->value);
406 /* Update the value. Some keywords are mapped to particular
407 entities, rather than simply having the value of the
408 corresponding IDENTIFIER_NODE. For example, `__const' is
409 mapped to `const'. */
410 token->value = ridpointers[token->keyword];
412 /* Handle Objective-C++ keywords. */
413 else if (token->type == CPP_AT_NAME)
415 token->type = CPP_KEYWORD;
416 switch (C_RID_CODE (token->value))
418 /* Map 'class' to '@class', 'private' to '@private', etc. */
419 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
420 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
421 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
422 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
423 case RID_THROW: token->keyword = RID_AT_THROW; break;
424 case RID_TRY: token->keyword = RID_AT_TRY; break;
425 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
426 default: token->keyword = C_RID_CODE (token->value);
429 else
430 token->keyword = RID_MAX;
433 /* Update the globals input_location and in_system_header from TOKEN. */
434 static inline void
435 cp_lexer_set_source_position_from_token (cp_token *token)
437 if (token->type != CPP_EOF)
439 input_location = token->location;
440 in_system_header = token->in_system_header;
444 /* Return a pointer to the next token in the token stream, but do not
445 consume it. */
447 static inline cp_token *
448 cp_lexer_peek_token (cp_lexer *lexer)
450 if (cp_lexer_debugging_p (lexer))
452 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
453 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
454 putc ('\n', cp_lexer_debug_stream);
456 return lexer->next_token;
459 /* Return true if the next token has the indicated TYPE. */
461 static inline bool
462 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
464 return cp_lexer_peek_token (lexer)->type == type;
467 /* Return true if the next token does not have the indicated TYPE. */
469 static inline bool
470 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
472 return !cp_lexer_next_token_is (lexer, type);
475 /* Return true if the next token is the indicated KEYWORD. */
477 static inline bool
478 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
480 cp_token *token;
482 /* Peek at the next token. */
483 token = cp_lexer_peek_token (lexer);
484 /* Check to see if it is the indicated keyword. */
485 return token->keyword == keyword;
488 /* Return a pointer to the Nth token in the token stream. If N is 1,
489 then this is precisely equivalent to cp_lexer_peek_token (except
490 that it is not inline). One would like to disallow that case, but
491 there is one case (cp_parser_nth_token_starts_template_id) where
492 the caller passes a variable for N and it might be 1. */
494 static cp_token *
495 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
497 cp_token *token;
499 /* N is 1-based, not zero-based. */
500 gcc_assert (n > 0);
502 if (cp_lexer_debugging_p (lexer))
503 fprintf (cp_lexer_debug_stream,
504 "cp_lexer: peeking ahead %ld at token: ", (long)n);
506 --n;
507 token = lexer->next_token;
508 gcc_assert (!n || token != &eof_token);
509 while (n != 0)
511 ++token;
512 if (token == lexer->last_token)
514 token = (cp_token *)&eof_token;
515 break;
518 if (token->type != CPP_PURGED)
519 --n;
522 if (cp_lexer_debugging_p (lexer))
524 cp_lexer_print_token (cp_lexer_debug_stream, token);
525 putc ('\n', cp_lexer_debug_stream);
528 return token;
531 /* Return the next token, and advance the lexer's next_token pointer
532 to point to the next non-purged token. */
534 static cp_token *
535 cp_lexer_consume_token (cp_lexer* lexer)
537 cp_token *token = lexer->next_token;
539 gcc_assert (token != &eof_token);
543 lexer->next_token++;
544 if (lexer->next_token == lexer->last_token)
546 lexer->next_token = (cp_token *)&eof_token;
547 break;
551 while (lexer->next_token->type == CPP_PURGED);
553 cp_lexer_set_source_position_from_token (token);
555 /* Provide debugging output. */
556 if (cp_lexer_debugging_p (lexer))
558 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
559 cp_lexer_print_token (cp_lexer_debug_stream, token);
560 putc ('\n', cp_lexer_debug_stream);
563 return token;
566 /* Permanently remove the next token from the token stream, and
567 advance the next_token pointer to refer to the next non-purged
568 token. */
570 static void
571 cp_lexer_purge_token (cp_lexer *lexer)
573 cp_token *tok = lexer->next_token;
575 gcc_assert (tok != &eof_token);
576 tok->type = CPP_PURGED;
577 tok->location = UNKNOWN_LOCATION;
578 tok->value = NULL_TREE;
579 tok->keyword = RID_MAX;
583 tok++;
584 if (tok == lexer->last_token)
586 tok = (cp_token *)&eof_token;
587 break;
590 while (tok->type == CPP_PURGED);
591 lexer->next_token = tok;
594 /* Permanently remove all tokens after TOK, up to, but not
595 including, the token that will be returned next by
596 cp_lexer_peek_token. */
598 static void
599 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
601 cp_token *peek = lexer->next_token;
603 if (peek == &eof_token)
604 peek = lexer->last_token;
606 gcc_assert (tok < peek);
608 for ( tok += 1; tok != peek; tok += 1)
610 tok->type = CPP_PURGED;
611 tok->location = UNKNOWN_LOCATION;
612 tok->value = NULL_TREE;
613 tok->keyword = RID_MAX;
617 /* Consume and handle a pragma token. */
618 static void
619 cp_lexer_handle_pragma (cp_lexer *lexer)
621 cpp_string s;
622 cp_token *token = cp_lexer_consume_token (lexer);
623 gcc_assert (token->type == CPP_PRAGMA);
624 gcc_assert (token->value);
626 s.len = TREE_STRING_LENGTH (token->value);
627 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
629 cpp_handle_deferred_pragma (parse_in, &s);
631 /* Clearing token->value here means that we will get an ICE if we
632 try to process this #pragma again (which should be impossible). */
633 token->value = NULL;
636 /* Begin saving tokens. All tokens consumed after this point will be
637 preserved. */
639 static void
640 cp_lexer_save_tokens (cp_lexer* lexer)
642 /* Provide debugging output. */
643 if (cp_lexer_debugging_p (lexer))
644 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
646 VEC_safe_push (cp_token_position, heap,
647 lexer->saved_tokens, lexer->next_token);
650 /* Commit to the portion of the token stream most recently saved. */
652 static void
653 cp_lexer_commit_tokens (cp_lexer* lexer)
655 /* Provide debugging output. */
656 if (cp_lexer_debugging_p (lexer))
657 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
659 VEC_pop (cp_token_position, lexer->saved_tokens);
662 /* Return all tokens saved since the last call to cp_lexer_save_tokens
663 to the token stream. Stop saving tokens. */
665 static void
666 cp_lexer_rollback_tokens (cp_lexer* lexer)
668 /* Provide debugging output. */
669 if (cp_lexer_debugging_p (lexer))
670 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
672 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
675 /* Print a representation of the TOKEN on the STREAM. */
677 #ifdef ENABLE_CHECKING
679 static void
680 cp_lexer_print_token (FILE * stream, cp_token *token)
682 /* We don't use cpp_type2name here because the parser defines
683 a few tokens of its own. */
684 static const char *const token_names[] = {
685 /* cpplib-defined token types */
686 #define OP(e, s) #e,
687 #define TK(e, s) #e,
688 TTYPE_TABLE
689 #undef OP
690 #undef TK
691 /* C++ parser token types - see "Manifest constants", above. */
692 "KEYWORD",
693 "TEMPLATE_ID",
694 "NESTED_NAME_SPECIFIER",
695 "PURGED"
698 /* If we have a name for the token, print it out. Otherwise, we
699 simply give the numeric code. */
700 gcc_assert (token->type < ARRAY_SIZE(token_names));
701 fputs (token_names[token->type], stream);
703 /* For some tokens, print the associated data. */
704 switch (token->type)
706 case CPP_KEYWORD:
707 /* Some keywords have a value that is not an IDENTIFIER_NODE.
708 For example, `struct' is mapped to an INTEGER_CST. */
709 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
710 break;
711 /* else fall through */
712 case CPP_NAME:
713 fputs (IDENTIFIER_POINTER (token->value), stream);
714 break;
716 case CPP_STRING:
717 case CPP_WSTRING:
718 case CPP_PRAGMA:
719 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
720 break;
722 default:
723 break;
727 /* Start emitting debugging information. */
729 static void
730 cp_lexer_start_debugging (cp_lexer* lexer)
732 lexer->debugging_p = true;
735 /* Stop emitting debugging information. */
737 static void
738 cp_lexer_stop_debugging (cp_lexer* lexer)
740 lexer->debugging_p = false;
743 #endif /* ENABLE_CHECKING */
745 /* Create a new cp_token_cache, representing a range of tokens. */
747 static cp_token_cache *
748 cp_token_cache_new (cp_token *first, cp_token *last)
750 cp_token_cache *cache = GGC_NEW (cp_token_cache);
751 cache->first = first;
752 cache->last = last;
753 return cache;
757 /* Decl-specifiers. */
759 static void clear_decl_specs
760 (cp_decl_specifier_seq *);
762 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
764 static void
765 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
767 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
770 /* Declarators. */
772 /* Nothing other than the parser should be creating declarators;
773 declarators are a semi-syntactic representation of C++ entities.
774 Other parts of the front end that need to create entities (like
775 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
777 static cp_declarator *make_call_declarator
778 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
779 static cp_declarator *make_array_declarator
780 (cp_declarator *, tree);
781 static cp_declarator *make_pointer_declarator
782 (cp_cv_quals, cp_declarator *);
783 static cp_declarator *make_reference_declarator
784 (cp_cv_quals, cp_declarator *);
785 static cp_parameter_declarator *make_parameter_declarator
786 (cp_decl_specifier_seq *, cp_declarator *, tree);
787 static cp_declarator *make_ptrmem_declarator
788 (cp_cv_quals, tree, cp_declarator *);
790 cp_declarator *cp_error_declarator;
792 /* The obstack on which declarators and related data structures are
793 allocated. */
794 static struct obstack declarator_obstack;
796 /* Alloc BYTES from the declarator memory pool. */
798 static inline void *
799 alloc_declarator (size_t bytes)
801 return obstack_alloc (&declarator_obstack, bytes);
804 /* Allocate a declarator of the indicated KIND. Clear fields that are
805 common to all declarators. */
807 static cp_declarator *
808 make_declarator (cp_declarator_kind kind)
810 cp_declarator *declarator;
812 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
813 declarator->kind = kind;
814 declarator->attributes = NULL_TREE;
815 declarator->declarator = NULL;
817 return declarator;
820 /* Make a declarator for a generalized identifier. If non-NULL, the
821 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
822 just UNQUALIFIED_NAME. */
824 static cp_declarator *
825 make_id_declarator (tree qualifying_scope, tree unqualified_name)
827 cp_declarator *declarator;
829 /* It is valid to write:
831 class C { void f(); };
832 typedef C D;
833 void D::f();
835 The standard is not clear about whether `typedef const C D' is
836 legal; as of 2002-09-15 the committee is considering that
837 question. EDG 3.0 allows that syntax. Therefore, we do as
838 well. */
839 if (qualifying_scope && TYPE_P (qualifying_scope))
840 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
842 declarator = make_declarator (cdk_id);
843 declarator->u.id.qualifying_scope = qualifying_scope;
844 declarator->u.id.unqualified_name = unqualified_name;
845 declarator->u.id.sfk = sfk_none;
847 return declarator;
850 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
851 of modifiers such as const or volatile to apply to the pointer
852 type, represented as identifiers. */
854 cp_declarator *
855 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
857 cp_declarator *declarator;
859 declarator = make_declarator (cdk_pointer);
860 declarator->declarator = target;
861 declarator->u.pointer.qualifiers = cv_qualifiers;
862 declarator->u.pointer.class_type = NULL_TREE;
864 return declarator;
867 /* Like make_pointer_declarator -- but for references. */
869 cp_declarator *
870 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
872 cp_declarator *declarator;
874 declarator = make_declarator (cdk_reference);
875 declarator->declarator = target;
876 declarator->u.pointer.qualifiers = cv_qualifiers;
877 declarator->u.pointer.class_type = NULL_TREE;
879 return declarator;
882 /* Like make_pointer_declarator -- but for a pointer to a non-static
883 member of CLASS_TYPE. */
885 cp_declarator *
886 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
887 cp_declarator *pointee)
889 cp_declarator *declarator;
891 declarator = make_declarator (cdk_ptrmem);
892 declarator->declarator = pointee;
893 declarator->u.pointer.qualifiers = cv_qualifiers;
894 declarator->u.pointer.class_type = class_type;
896 return declarator;
899 /* Make a declarator for the function given by TARGET, with the
900 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
901 "const"-qualified member function. The EXCEPTION_SPECIFICATION
902 indicates what exceptions can be thrown. */
904 cp_declarator *
905 make_call_declarator (cp_declarator *target,
906 cp_parameter_declarator *parms,
907 cp_cv_quals cv_qualifiers,
908 tree exception_specification)
910 cp_declarator *declarator;
912 declarator = make_declarator (cdk_function);
913 declarator->declarator = target;
914 declarator->u.function.parameters = parms;
915 declarator->u.function.qualifiers = cv_qualifiers;
916 declarator->u.function.exception_specification = exception_specification;
918 return declarator;
921 /* Make a declarator for an array of BOUNDS elements, each of which is
922 defined by ELEMENT. */
924 cp_declarator *
925 make_array_declarator (cp_declarator *element, tree bounds)
927 cp_declarator *declarator;
929 declarator = make_declarator (cdk_array);
930 declarator->declarator = element;
931 declarator->u.array.bounds = bounds;
933 return declarator;
936 cp_parameter_declarator *no_parameters;
938 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
939 DECLARATOR and DEFAULT_ARGUMENT. */
941 cp_parameter_declarator *
942 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
943 cp_declarator *declarator,
944 tree default_argument)
946 cp_parameter_declarator *parameter;
948 parameter = ((cp_parameter_declarator *)
949 alloc_declarator (sizeof (cp_parameter_declarator)));
950 parameter->next = NULL;
951 if (decl_specifiers)
952 parameter->decl_specifiers = *decl_specifiers;
953 else
954 clear_decl_specs (&parameter->decl_specifiers);
955 parameter->declarator = declarator;
956 parameter->default_argument = default_argument;
957 parameter->ellipsis_p = false;
959 return parameter;
962 /* The parser. */
964 /* Overview
965 --------
967 A cp_parser parses the token stream as specified by the C++
968 grammar. Its job is purely parsing, not semantic analysis. For
969 example, the parser breaks the token stream into declarators,
970 expressions, statements, and other similar syntactic constructs.
971 It does not check that the types of the expressions on either side
972 of an assignment-statement are compatible, or that a function is
973 not declared with a parameter of type `void'.
975 The parser invokes routines elsewhere in the compiler to perform
976 semantic analysis and to build up the abstract syntax tree for the
977 code processed.
979 The parser (and the template instantiation code, which is, in a
980 way, a close relative of parsing) are the only parts of the
981 compiler that should be calling push_scope and pop_scope, or
982 related functions. The parser (and template instantiation code)
983 keeps track of what scope is presently active; everything else
984 should simply honor that. (The code that generates static
985 initializers may also need to set the scope, in order to check
986 access control correctly when emitting the initializers.)
988 Methodology
989 -----------
991 The parser is of the standard recursive-descent variety. Upcoming
992 tokens in the token stream are examined in order to determine which
993 production to use when parsing a non-terminal. Some C++ constructs
994 require arbitrary look ahead to disambiguate. For example, it is
995 impossible, in the general case, to tell whether a statement is an
996 expression or declaration without scanning the entire statement.
997 Therefore, the parser is capable of "parsing tentatively." When the
998 parser is not sure what construct comes next, it enters this mode.
999 Then, while we attempt to parse the construct, the parser queues up
1000 error messages, rather than issuing them immediately, and saves the
1001 tokens it consumes. If the construct is parsed successfully, the
1002 parser "commits", i.e., it issues any queued error messages and
1003 the tokens that were being preserved are permanently discarded.
1004 If, however, the construct is not parsed successfully, the parser
1005 rolls back its state completely so that it can resume parsing using
1006 a different alternative.
1008 Future Improvements
1009 -------------------
1011 The performance of the parser could probably be improved substantially.
1012 We could often eliminate the need to parse tentatively by looking ahead
1013 a little bit. In some places, this approach might not entirely eliminate
1014 the need to parse tentatively, but it might still speed up the average
1015 case. */
1017 /* Flags that are passed to some parsing functions. These values can
1018 be bitwise-ored together. */
1020 typedef enum cp_parser_flags
1022 /* No flags. */
1023 CP_PARSER_FLAGS_NONE = 0x0,
1024 /* The construct is optional. If it is not present, then no error
1025 should be issued. */
1026 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1027 /* When parsing a type-specifier, do not allow user-defined types. */
1028 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1029 } cp_parser_flags;
1031 /* The different kinds of declarators we want to parse. */
1033 typedef enum cp_parser_declarator_kind
1035 /* We want an abstract declarator. */
1036 CP_PARSER_DECLARATOR_ABSTRACT,
1037 /* We want a named declarator. */
1038 CP_PARSER_DECLARATOR_NAMED,
1039 /* We don't mind, but the name must be an unqualified-id. */
1040 CP_PARSER_DECLARATOR_EITHER
1041 } cp_parser_declarator_kind;
1043 /* The precedence values used to parse binary expressions. The minimum value
1044 of PREC must be 1, because zero is reserved to quickly discriminate
1045 binary operators from other tokens. */
1047 enum cp_parser_prec
1049 PREC_NOT_OPERATOR,
1050 PREC_LOGICAL_OR_EXPRESSION,
1051 PREC_LOGICAL_AND_EXPRESSION,
1052 PREC_INCLUSIVE_OR_EXPRESSION,
1053 PREC_EXCLUSIVE_OR_EXPRESSION,
1054 PREC_AND_EXPRESSION,
1055 PREC_EQUALITY_EXPRESSION,
1056 PREC_RELATIONAL_EXPRESSION,
1057 PREC_SHIFT_EXPRESSION,
1058 PREC_ADDITIVE_EXPRESSION,
1059 PREC_MULTIPLICATIVE_EXPRESSION,
1060 PREC_PM_EXPRESSION,
1061 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1064 /* A mapping from a token type to a corresponding tree node type, with a
1065 precedence value. */
1067 typedef struct cp_parser_binary_operations_map_node
1069 /* The token type. */
1070 enum cpp_ttype token_type;
1071 /* The corresponding tree code. */
1072 enum tree_code tree_type;
1073 /* The precedence of this operator. */
1074 enum cp_parser_prec prec;
1075 } cp_parser_binary_operations_map_node;
1077 /* The status of a tentative parse. */
1079 typedef enum cp_parser_status_kind
1081 /* No errors have occurred. */
1082 CP_PARSER_STATUS_KIND_NO_ERROR,
1083 /* An error has occurred. */
1084 CP_PARSER_STATUS_KIND_ERROR,
1085 /* We are committed to this tentative parse, whether or not an error
1086 has occurred. */
1087 CP_PARSER_STATUS_KIND_COMMITTED
1088 } cp_parser_status_kind;
1090 typedef struct cp_parser_expression_stack_entry
1092 tree lhs;
1093 enum tree_code tree_type;
1094 int prec;
1095 } cp_parser_expression_stack_entry;
1097 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1098 entries because precedence levels on the stack are monotonically
1099 increasing. */
1100 typedef struct cp_parser_expression_stack_entry
1101 cp_parser_expression_stack[NUM_PREC_VALUES];
1103 /* Context that is saved and restored when parsing tentatively. */
1104 typedef struct cp_parser_context GTY (())
1106 /* If this is a tentative parsing context, the status of the
1107 tentative parse. */
1108 enum cp_parser_status_kind status;
1109 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1110 that are looked up in this context must be looked up both in the
1111 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112 the context of the containing expression. */
1113 tree object_type;
1115 /* The next parsing context in the stack. */
1116 struct cp_parser_context *next;
1117 } cp_parser_context;
1119 /* Prototypes. */
1121 /* Constructors and destructors. */
1123 static cp_parser_context *cp_parser_context_new
1124 (cp_parser_context *);
1126 /* Class variables. */
1128 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1130 /* The operator-precedence table used by cp_parser_binary_expression.
1131 Transformed into an associative array (binops_by_token) by
1132 cp_parser_new. */
1134 static const cp_parser_binary_operations_map_node binops[] = {
1135 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1136 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1138 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1142 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1143 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1145 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1146 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1148 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1150 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1152 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1153 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1155 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1156 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1158 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1160 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1162 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1164 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1166 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1169 /* The same as binops, but initialized by cp_parser_new so that
1170 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1171 for speed. */
1172 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1174 /* Constructors and destructors. */
1176 /* Construct a new context. The context below this one on the stack
1177 is given by NEXT. */
1179 static cp_parser_context *
1180 cp_parser_context_new (cp_parser_context* next)
1182 cp_parser_context *context;
1184 /* Allocate the storage. */
1185 if (cp_parser_context_free_list != NULL)
1187 /* Pull the first entry from the free list. */
1188 context = cp_parser_context_free_list;
1189 cp_parser_context_free_list = context->next;
1190 memset (context, 0, sizeof (*context));
1192 else
1193 context = GGC_CNEW (cp_parser_context);
1195 /* No errors have occurred yet in this context. */
1196 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1197 /* If this is not the bottomost context, copy information that we
1198 need from the previous context. */
1199 if (next)
1201 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1202 expression, then we are parsing one in this context, too. */
1203 context->object_type = next->object_type;
1204 /* Thread the stack. */
1205 context->next = next;
1208 return context;
1211 /* The cp_parser structure represents the C++ parser. */
1213 typedef struct cp_parser GTY(())
1215 /* The lexer from which we are obtaining tokens. */
1216 cp_lexer *lexer;
1218 /* The scope in which names should be looked up. If NULL_TREE, then
1219 we look up names in the scope that is currently open in the
1220 source program. If non-NULL, this is either a TYPE or
1221 NAMESPACE_DECL for the scope in which we should look. It can
1222 also be ERROR_MARK, when we've parsed a bogus scope.
1224 This value is not cleared automatically after a name is looked
1225 up, so we must be careful to clear it before starting a new look
1226 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1227 will look up `Z' in the scope of `X', rather than the current
1228 scope.) Unfortunately, it is difficult to tell when name lookup
1229 is complete, because we sometimes peek at a token, look it up,
1230 and then decide not to consume it. */
1231 tree scope;
1233 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1234 last lookup took place. OBJECT_SCOPE is used if an expression
1235 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1236 respectively. QUALIFYING_SCOPE is used for an expression of the
1237 form "X::Y"; it refers to X. */
1238 tree object_scope;
1239 tree qualifying_scope;
1241 /* A stack of parsing contexts. All but the bottom entry on the
1242 stack will be tentative contexts.
1244 We parse tentatively in order to determine which construct is in
1245 use in some situations. For example, in order to determine
1246 whether a statement is an expression-statement or a
1247 declaration-statement we parse it tentatively as a
1248 declaration-statement. If that fails, we then reparse the same
1249 token stream as an expression-statement. */
1250 cp_parser_context *context;
1252 /* True if we are parsing GNU C++. If this flag is not set, then
1253 GNU extensions are not recognized. */
1254 bool allow_gnu_extensions_p;
1256 /* TRUE if the `>' token should be interpreted as the greater-than
1257 operator. FALSE if it is the end of a template-id or
1258 template-parameter-list. */
1259 bool greater_than_is_operator_p;
1261 /* TRUE if default arguments are allowed within a parameter list
1262 that starts at this point. FALSE if only a gnu extension makes
1263 them permissible. */
1264 bool default_arg_ok_p;
1266 /* TRUE if we are parsing an integral constant-expression. See
1267 [expr.const] for a precise definition. */
1268 bool integral_constant_expression_p;
1270 /* TRUE if we are parsing an integral constant-expression -- but a
1271 non-constant expression should be permitted as well. This flag
1272 is used when parsing an array bound so that GNU variable-length
1273 arrays are tolerated. */
1274 bool allow_non_integral_constant_expression_p;
1276 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1277 been seen that makes the expression non-constant. */
1278 bool non_integral_constant_expression_p;
1280 /* TRUE if local variable names and `this' are forbidden in the
1281 current context. */
1282 bool local_variables_forbidden_p;
1284 /* TRUE if the declaration we are parsing is part of a
1285 linkage-specification of the form `extern string-literal
1286 declaration'. */
1287 bool in_unbraced_linkage_specification_p;
1289 /* TRUE if we are presently parsing a declarator, after the
1290 direct-declarator. */
1291 bool in_declarator_p;
1293 /* TRUE if we are presently parsing a template-argument-list. */
1294 bool in_template_argument_list_p;
1296 /* TRUE if we are presently parsing the body of an
1297 iteration-statement. */
1298 bool in_iteration_statement_p;
1300 /* TRUE if we are presently parsing the body of a switch
1301 statement. */
1302 bool in_switch_statement_p;
1304 /* TRUE if we are parsing a type-id in an expression context. In
1305 such a situation, both "type (expr)" and "type (type)" are valid
1306 alternatives. */
1307 bool in_type_id_in_expr_p;
1309 /* TRUE if we are currently in a header file where declarations are
1310 implicitly extern "C". */
1311 bool implicit_extern_c;
1313 /* TRUE if strings in expressions should be translated to the execution
1314 character set. */
1315 bool translate_strings_p;
1317 /* If non-NULL, then we are parsing a construct where new type
1318 definitions are not permitted. The string stored here will be
1319 issued as an error message if a type is defined. */
1320 const char *type_definition_forbidden_message;
1322 /* A list of lists. The outer list is a stack, used for member
1323 functions of local classes. At each level there are two sub-list,
1324 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1325 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1326 TREE_VALUE's. The functions are chained in reverse declaration
1327 order.
1329 The TREE_PURPOSE sublist contains those functions with default
1330 arguments that need post processing, and the TREE_VALUE sublist
1331 contains those functions with definitions that need post
1332 processing.
1334 These lists can only be processed once the outermost class being
1335 defined is complete. */
1336 tree unparsed_functions_queues;
1338 /* The number of classes whose definitions are currently in
1339 progress. */
1340 unsigned num_classes_being_defined;
1342 /* The number of template parameter lists that apply directly to the
1343 current declaration. */
1344 unsigned num_template_parameter_lists;
1345 } cp_parser;
1347 /* The type of a function that parses some kind of expression. */
1348 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1350 /* Prototypes. */
1352 /* Constructors and destructors. */
1354 static cp_parser *cp_parser_new
1355 (void);
1357 /* Routines to parse various constructs.
1359 Those that return `tree' will return the error_mark_node (rather
1360 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1361 Sometimes, they will return an ordinary node if error-recovery was
1362 attempted, even though a parse error occurred. So, to check
1363 whether or not a parse error occurred, you should always use
1364 cp_parser_error_occurred. If the construct is optional (indicated
1365 either by an `_opt' in the name of the function that does the
1366 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1367 the construct is not present. */
1369 /* Lexical conventions [gram.lex] */
1371 static tree cp_parser_identifier
1372 (cp_parser *);
1373 static tree cp_parser_string_literal
1374 (cp_parser *, bool, bool);
1376 /* Basic concepts [gram.basic] */
1378 static bool cp_parser_translation_unit
1379 (cp_parser *);
1381 /* Expressions [gram.expr] */
1383 static tree cp_parser_primary_expression
1384 (cp_parser *, bool, cp_id_kind *, tree *);
1385 static tree cp_parser_id_expression
1386 (cp_parser *, bool, bool, bool *, bool);
1387 static tree cp_parser_unqualified_id
1388 (cp_parser *, bool, bool, bool);
1389 static tree cp_parser_nested_name_specifier_opt
1390 (cp_parser *, bool, bool, bool, bool);
1391 static tree cp_parser_nested_name_specifier
1392 (cp_parser *, bool, bool, bool, bool);
1393 static tree cp_parser_class_or_namespace_name
1394 (cp_parser *, bool, bool, bool, bool, bool);
1395 static tree cp_parser_postfix_expression
1396 (cp_parser *, bool, bool);
1397 static tree cp_parser_postfix_open_square_expression
1398 (cp_parser *, tree, bool);
1399 static tree cp_parser_postfix_dot_deref_expression
1400 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1401 static tree cp_parser_parenthesized_expression_list
1402 (cp_parser *, bool, bool, bool *);
1403 static void cp_parser_pseudo_destructor_name
1404 (cp_parser *, tree *, tree *);
1405 static tree cp_parser_unary_expression
1406 (cp_parser *, bool, bool);
1407 static enum tree_code cp_parser_unary_operator
1408 (cp_token *);
1409 static tree cp_parser_new_expression
1410 (cp_parser *);
1411 static tree cp_parser_new_placement
1412 (cp_parser *);
1413 static tree cp_parser_new_type_id
1414 (cp_parser *, tree *);
1415 static cp_declarator *cp_parser_new_declarator_opt
1416 (cp_parser *);
1417 static cp_declarator *cp_parser_direct_new_declarator
1418 (cp_parser *);
1419 static tree cp_parser_new_initializer
1420 (cp_parser *);
1421 static tree cp_parser_delete_expression
1422 (cp_parser *);
1423 static tree cp_parser_cast_expression
1424 (cp_parser *, bool, bool);
1425 static tree cp_parser_binary_expression
1426 (cp_parser *, bool);
1427 static tree cp_parser_question_colon_clause
1428 (cp_parser *, tree);
1429 static tree cp_parser_assignment_expression
1430 (cp_parser *, bool);
1431 static enum tree_code cp_parser_assignment_operator_opt
1432 (cp_parser *);
1433 static tree cp_parser_expression
1434 (cp_parser *, bool);
1435 static tree cp_parser_constant_expression
1436 (cp_parser *, bool, bool *);
1437 static tree cp_parser_builtin_offsetof
1438 (cp_parser *);
1440 /* Statements [gram.stmt.stmt] */
1442 static void cp_parser_statement
1443 (cp_parser *, tree);
1444 static tree cp_parser_labeled_statement
1445 (cp_parser *, tree);
1446 static tree cp_parser_expression_statement
1447 (cp_parser *, tree);
1448 static tree cp_parser_compound_statement
1449 (cp_parser *, tree, bool);
1450 static void cp_parser_statement_seq_opt
1451 (cp_parser *, tree);
1452 static tree cp_parser_selection_statement
1453 (cp_parser *);
1454 static tree cp_parser_condition
1455 (cp_parser *);
1456 static tree cp_parser_iteration_statement
1457 (cp_parser *);
1458 static void cp_parser_for_init_statement
1459 (cp_parser *);
1460 static tree cp_parser_jump_statement
1461 (cp_parser *);
1462 static void cp_parser_declaration_statement
1463 (cp_parser *);
1465 static tree cp_parser_implicitly_scoped_statement
1466 (cp_parser *);
1467 static void cp_parser_already_scoped_statement
1468 (cp_parser *);
1470 /* Declarations [gram.dcl.dcl] */
1472 static void cp_parser_declaration_seq_opt
1473 (cp_parser *);
1474 static void cp_parser_declaration
1475 (cp_parser *);
1476 static void cp_parser_block_declaration
1477 (cp_parser *, bool);
1478 static void cp_parser_simple_declaration
1479 (cp_parser *, bool);
1480 static void cp_parser_decl_specifier_seq
1481 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1482 static tree cp_parser_storage_class_specifier_opt
1483 (cp_parser *);
1484 static tree cp_parser_function_specifier_opt
1485 (cp_parser *, cp_decl_specifier_seq *);
1486 static tree cp_parser_type_specifier
1487 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1488 int *, bool *);
1489 static tree cp_parser_simple_type_specifier
1490 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1491 static tree cp_parser_type_name
1492 (cp_parser *);
1493 static tree cp_parser_elaborated_type_specifier
1494 (cp_parser *, bool, bool);
1495 static tree cp_parser_enum_specifier
1496 (cp_parser *);
1497 static void cp_parser_enumerator_list
1498 (cp_parser *, tree);
1499 static void cp_parser_enumerator_definition
1500 (cp_parser *, tree);
1501 static tree cp_parser_namespace_name
1502 (cp_parser *);
1503 static void cp_parser_namespace_definition
1504 (cp_parser *);
1505 static void cp_parser_namespace_body
1506 (cp_parser *);
1507 static tree cp_parser_qualified_namespace_specifier
1508 (cp_parser *);
1509 static void cp_parser_namespace_alias_definition
1510 (cp_parser *);
1511 static void cp_parser_using_declaration
1512 (cp_parser *);
1513 static void cp_parser_using_directive
1514 (cp_parser *);
1515 static void cp_parser_asm_definition
1516 (cp_parser *);
1517 static void cp_parser_linkage_specification
1518 (cp_parser *);
1520 /* Declarators [gram.dcl.decl] */
1522 static tree cp_parser_init_declarator
1523 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1524 static cp_declarator *cp_parser_declarator
1525 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1526 static cp_declarator *cp_parser_direct_declarator
1527 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1528 static enum tree_code cp_parser_ptr_operator
1529 (cp_parser *, tree *, cp_cv_quals *);
1530 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1531 (cp_parser *);
1532 static tree cp_parser_declarator_id
1533 (cp_parser *);
1534 static tree cp_parser_type_id
1535 (cp_parser *);
1536 static void cp_parser_type_specifier_seq
1537 (cp_parser *, bool, cp_decl_specifier_seq *);
1538 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1539 (cp_parser *);
1540 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1541 (cp_parser *, bool *);
1542 static cp_parameter_declarator *cp_parser_parameter_declaration
1543 (cp_parser *, bool, bool *);
1544 static void cp_parser_function_body
1545 (cp_parser *);
1546 static tree cp_parser_initializer
1547 (cp_parser *, bool *, bool *);
1548 static tree cp_parser_initializer_clause
1549 (cp_parser *, bool *);
1550 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1551 (cp_parser *, bool *);
1553 static bool cp_parser_ctor_initializer_opt_and_function_body
1554 (cp_parser *);
1556 /* Classes [gram.class] */
1558 static tree cp_parser_class_name
1559 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1560 static tree cp_parser_class_specifier
1561 (cp_parser *);
1562 static tree cp_parser_class_head
1563 (cp_parser *, bool *, tree *);
1564 static enum tag_types cp_parser_class_key
1565 (cp_parser *);
1566 static void cp_parser_member_specification_opt
1567 (cp_parser *);
1568 static void cp_parser_member_declaration
1569 (cp_parser *);
1570 static tree cp_parser_pure_specifier
1571 (cp_parser *);
1572 static tree cp_parser_constant_initializer
1573 (cp_parser *);
1575 /* Derived classes [gram.class.derived] */
1577 static tree cp_parser_base_clause
1578 (cp_parser *);
1579 static tree cp_parser_base_specifier
1580 (cp_parser *);
1582 /* Special member functions [gram.special] */
1584 static tree cp_parser_conversion_function_id
1585 (cp_parser *);
1586 static tree cp_parser_conversion_type_id
1587 (cp_parser *);
1588 static cp_declarator *cp_parser_conversion_declarator_opt
1589 (cp_parser *);
1590 static bool cp_parser_ctor_initializer_opt
1591 (cp_parser *);
1592 static void cp_parser_mem_initializer_list
1593 (cp_parser *);
1594 static tree cp_parser_mem_initializer
1595 (cp_parser *);
1596 static tree cp_parser_mem_initializer_id
1597 (cp_parser *);
1599 /* Overloading [gram.over] */
1601 static tree cp_parser_operator_function_id
1602 (cp_parser *);
1603 static tree cp_parser_operator
1604 (cp_parser *);
1606 /* Templates [gram.temp] */
1608 static void cp_parser_template_declaration
1609 (cp_parser *, bool);
1610 static tree cp_parser_template_parameter_list
1611 (cp_parser *);
1612 static tree cp_parser_template_parameter
1613 (cp_parser *, bool *);
1614 static tree cp_parser_type_parameter
1615 (cp_parser *);
1616 static tree cp_parser_template_id
1617 (cp_parser *, bool, bool, bool);
1618 static tree cp_parser_template_name
1619 (cp_parser *, bool, bool, bool, bool *);
1620 static tree cp_parser_template_argument_list
1621 (cp_parser *);
1622 static tree cp_parser_template_argument
1623 (cp_parser *);
1624 static void cp_parser_explicit_instantiation
1625 (cp_parser *);
1626 static void cp_parser_explicit_specialization
1627 (cp_parser *);
1629 /* Exception handling [gram.exception] */
1631 static tree cp_parser_try_block
1632 (cp_parser *);
1633 static bool cp_parser_function_try_block
1634 (cp_parser *);
1635 static void cp_parser_handler_seq
1636 (cp_parser *);
1637 static void cp_parser_handler
1638 (cp_parser *);
1639 static tree cp_parser_exception_declaration
1640 (cp_parser *);
1641 static tree cp_parser_throw_expression
1642 (cp_parser *);
1643 static tree cp_parser_exception_specification_opt
1644 (cp_parser *);
1645 static tree cp_parser_type_id_list
1646 (cp_parser *);
1648 /* GNU Extensions */
1650 static tree cp_parser_asm_specification_opt
1651 (cp_parser *);
1652 static tree cp_parser_asm_operand_list
1653 (cp_parser *);
1654 static tree cp_parser_asm_clobber_list
1655 (cp_parser *);
1656 static tree cp_parser_attributes_opt
1657 (cp_parser *);
1658 static tree cp_parser_attribute_list
1659 (cp_parser *);
1660 static bool cp_parser_extension_opt
1661 (cp_parser *, int *);
1662 static void cp_parser_label_declaration
1663 (cp_parser *);
1665 /* Objective-C++ Productions */
1667 static tree cp_parser_objc_message_receiver
1668 (cp_parser *);
1669 static tree cp_parser_objc_message_args
1670 (cp_parser *);
1671 static tree cp_parser_objc_message_expression
1672 (cp_parser *);
1673 static tree cp_parser_objc_encode_expression
1674 (cp_parser *);
1675 static tree cp_parser_objc_defs_expression
1676 (cp_parser *);
1677 static tree cp_parser_objc_protocol_expression
1678 (cp_parser *);
1679 static tree cp_parser_objc_selector_expression
1680 (cp_parser *);
1681 static tree cp_parser_objc_expression
1682 (cp_parser *);
1683 static bool cp_parser_objc_selector_p
1684 (enum cpp_ttype);
1685 static tree cp_parser_objc_selector
1686 (cp_parser *);
1687 static tree cp_parser_objc_protocol_refs_opt
1688 (cp_parser *);
1689 static void cp_parser_objc_declaration
1690 (cp_parser *);
1691 static tree cp_parser_objc_statement
1692 (cp_parser *);
1694 /* Utility Routines */
1696 static tree cp_parser_lookup_name
1697 (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1698 static tree cp_parser_lookup_name_simple
1699 (cp_parser *, tree);
1700 static tree cp_parser_maybe_treat_template_as_class
1701 (tree, bool);
1702 static bool cp_parser_check_declarator_template_parameters
1703 (cp_parser *, cp_declarator *);
1704 static bool cp_parser_check_template_parameters
1705 (cp_parser *, unsigned);
1706 static tree cp_parser_simple_cast_expression
1707 (cp_parser *);
1708 static tree cp_parser_global_scope_opt
1709 (cp_parser *, bool);
1710 static bool cp_parser_constructor_declarator_p
1711 (cp_parser *, bool);
1712 static tree cp_parser_function_definition_from_specifiers_and_declarator
1713 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1714 static tree cp_parser_function_definition_after_declarator
1715 (cp_parser *, bool);
1716 static void cp_parser_template_declaration_after_export
1717 (cp_parser *, bool);
1718 static tree cp_parser_single_declaration
1719 (cp_parser *, bool, bool *);
1720 static tree cp_parser_functional_cast
1721 (cp_parser *, tree);
1722 static tree cp_parser_save_member_function_body
1723 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1724 static tree cp_parser_enclosed_template_argument_list
1725 (cp_parser *);
1726 static void cp_parser_save_default_args
1727 (cp_parser *, tree);
1728 static void cp_parser_late_parsing_for_member
1729 (cp_parser *, tree);
1730 static void cp_parser_late_parsing_default_args
1731 (cp_parser *, tree);
1732 static tree cp_parser_sizeof_operand
1733 (cp_parser *, enum rid);
1734 static bool cp_parser_declares_only_class_p
1735 (cp_parser *);
1736 static void cp_parser_set_storage_class
1737 (cp_decl_specifier_seq *, cp_storage_class);
1738 static void cp_parser_set_decl_spec_type
1739 (cp_decl_specifier_seq *, tree, bool);
1740 static bool cp_parser_friend_p
1741 (const cp_decl_specifier_seq *);
1742 static cp_token *cp_parser_require
1743 (cp_parser *, enum cpp_ttype, const char *);
1744 static cp_token *cp_parser_require_keyword
1745 (cp_parser *, enum rid, const char *);
1746 static bool cp_parser_token_starts_function_definition_p
1747 (cp_token *);
1748 static bool cp_parser_next_token_starts_class_definition_p
1749 (cp_parser *);
1750 static bool cp_parser_next_token_ends_template_argument_p
1751 (cp_parser *);
1752 static bool cp_parser_nth_token_starts_template_argument_list_p
1753 (cp_parser *, size_t);
1754 static enum tag_types cp_parser_token_is_class_key
1755 (cp_token *);
1756 static void cp_parser_check_class_key
1757 (enum tag_types, tree type);
1758 static void cp_parser_check_access_in_redeclaration
1759 (tree type);
1760 static bool cp_parser_optional_template_keyword
1761 (cp_parser *);
1762 static void cp_parser_pre_parsed_nested_name_specifier
1763 (cp_parser *);
1764 static void cp_parser_cache_group
1765 (cp_parser *, enum cpp_ttype, unsigned);
1766 static void cp_parser_parse_tentatively
1767 (cp_parser *);
1768 static void cp_parser_commit_to_tentative_parse
1769 (cp_parser *);
1770 static void cp_parser_abort_tentative_parse
1771 (cp_parser *);
1772 static bool cp_parser_parse_definitely
1773 (cp_parser *);
1774 static inline bool cp_parser_parsing_tentatively
1775 (cp_parser *);
1776 static bool cp_parser_uncommitted_to_tentative_parse_p
1777 (cp_parser *);
1778 static void cp_parser_error
1779 (cp_parser *, const char *);
1780 static void cp_parser_name_lookup_error
1781 (cp_parser *, tree, tree, const char *);
1782 static bool cp_parser_simulate_error
1783 (cp_parser *);
1784 static void cp_parser_check_type_definition
1785 (cp_parser *);
1786 static void cp_parser_check_for_definition_in_return_type
1787 (cp_declarator *, tree);
1788 static void cp_parser_check_for_invalid_template_id
1789 (cp_parser *, tree);
1790 static bool cp_parser_non_integral_constant_expression
1791 (cp_parser *, const char *);
1792 static void cp_parser_diagnose_invalid_type_name
1793 (cp_parser *, tree, tree);
1794 static bool cp_parser_parse_and_diagnose_invalid_type_name
1795 (cp_parser *);
1796 static int cp_parser_skip_to_closing_parenthesis
1797 (cp_parser *, bool, bool, bool);
1798 static void cp_parser_skip_to_end_of_statement
1799 (cp_parser *);
1800 static void cp_parser_consume_semicolon_at_end_of_statement
1801 (cp_parser *);
1802 static void cp_parser_skip_to_end_of_block_or_statement
1803 (cp_parser *);
1804 static void cp_parser_skip_to_closing_brace
1805 (cp_parser *);
1806 static void cp_parser_skip_until_found
1807 (cp_parser *, enum cpp_ttype, const char *);
1808 static bool cp_parser_error_occurred
1809 (cp_parser *);
1810 static bool cp_parser_allow_gnu_extensions_p
1811 (cp_parser *);
1812 static bool cp_parser_is_string_literal
1813 (cp_token *);
1814 static bool cp_parser_is_keyword
1815 (cp_token *, enum rid);
1816 static tree cp_parser_make_typename_type
1817 (cp_parser *, tree, tree);
1819 /* Returns nonzero if we are parsing tentatively. */
1821 static inline bool
1822 cp_parser_parsing_tentatively (cp_parser* parser)
1824 return parser->context->next != NULL;
1827 /* Returns nonzero if TOKEN is a string literal. */
1829 static bool
1830 cp_parser_is_string_literal (cp_token* token)
1832 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1835 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1837 static bool
1838 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1840 return token->keyword == keyword;
1843 /* A minimum or maximum operator has been seen. As these are
1844 deprecated, issue a warning. */
1846 static inline void
1847 cp_parser_warn_min_max (void)
1849 if (warn_deprecated && !in_system_header)
1850 warning (0, "minimum/maximum operators are deprecated");
1853 /* If not parsing tentatively, issue a diagnostic of the form
1854 FILE:LINE: MESSAGE before TOKEN
1855 where TOKEN is the next token in the input stream. MESSAGE
1856 (specified by the caller) is usually of the form "expected
1857 OTHER-TOKEN". */
1859 static void
1860 cp_parser_error (cp_parser* parser, const char* message)
1862 if (!cp_parser_simulate_error (parser))
1864 cp_token *token = cp_lexer_peek_token (parser->lexer);
1865 /* This diagnostic makes more sense if it is tagged to the line
1866 of the token we just peeked at. */
1867 cp_lexer_set_source_position_from_token (token);
1868 if (token->type == CPP_PRAGMA)
1870 error ("%<#pragma%> is not allowed here");
1871 cp_lexer_purge_token (parser->lexer);
1872 return;
1874 c_parse_error (message,
1875 /* Because c_parser_error does not understand
1876 CPP_KEYWORD, keywords are treated like
1877 identifiers. */
1878 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1879 token->value);
1883 /* Issue an error about name-lookup failing. NAME is the
1884 IDENTIFIER_NODE DECL is the result of
1885 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1886 the thing that we hoped to find. */
1888 static void
1889 cp_parser_name_lookup_error (cp_parser* parser,
1890 tree name,
1891 tree decl,
1892 const char* desired)
1894 /* If name lookup completely failed, tell the user that NAME was not
1895 declared. */
1896 if (decl == error_mark_node)
1898 if (parser->scope && parser->scope != global_namespace)
1899 error ("%<%D::%D%> has not been declared",
1900 parser->scope, name);
1901 else if (parser->scope == global_namespace)
1902 error ("%<::%D%> has not been declared", name);
1903 else if (parser->object_scope
1904 && !CLASS_TYPE_P (parser->object_scope))
1905 error ("request for member %qD in non-class type %qT",
1906 name, parser->object_scope);
1907 else if (parser->object_scope)
1908 error ("%<%T::%D%> has not been declared",
1909 parser->object_scope, name);
1910 else
1911 error ("%qD has not been declared", name);
1913 else if (parser->scope && parser->scope != global_namespace)
1914 error ("%<%D::%D%> %s", parser->scope, name, desired);
1915 else if (parser->scope == global_namespace)
1916 error ("%<::%D%> %s", name, desired);
1917 else
1918 error ("%qD %s", name, desired);
1921 /* If we are parsing tentatively, remember that an error has occurred
1922 during this tentative parse. Returns true if the error was
1923 simulated; false if a message should be issued by the caller. */
1925 static bool
1926 cp_parser_simulate_error (cp_parser* parser)
1928 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1930 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1931 return true;
1933 return false;
1936 /* This function is called when a type is defined. If type
1937 definitions are forbidden at this point, an error message is
1938 issued. */
1940 static void
1941 cp_parser_check_type_definition (cp_parser* parser)
1943 /* If types are forbidden here, issue a message. */
1944 if (parser->type_definition_forbidden_message)
1945 /* Use `%s' to print the string in case there are any escape
1946 characters in the message. */
1947 error ("%s", parser->type_definition_forbidden_message);
1950 /* This function is called when the DECLARATOR is processed. The TYPE
1951 was a type defined in the decl-specifiers. If it is invalid to
1952 define a type in the decl-specifiers for DECLARATOR, an error is
1953 issued. */
1955 static void
1956 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1957 tree type)
1959 /* [dcl.fct] forbids type definitions in return types.
1960 Unfortunately, it's not easy to know whether or not we are
1961 processing a return type until after the fact. */
1962 while (declarator
1963 && (declarator->kind == cdk_pointer
1964 || declarator->kind == cdk_reference
1965 || declarator->kind == cdk_ptrmem))
1966 declarator = declarator->declarator;
1967 if (declarator
1968 && declarator->kind == cdk_function)
1970 error ("new types may not be defined in a return type");
1971 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1972 type);
1976 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1977 "<" in any valid C++ program. If the next token is indeed "<",
1978 issue a message warning the user about what appears to be an
1979 invalid attempt to form a template-id. */
1981 static void
1982 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1983 tree type)
1985 cp_token_position start = 0;
1987 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1989 if (TYPE_P (type))
1990 error ("%qT is not a template", type);
1991 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1992 error ("%qE is not a template", type);
1993 else
1994 error ("invalid template-id");
1995 /* Remember the location of the invalid "<". */
1996 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1997 start = cp_lexer_token_position (parser->lexer, true);
1998 /* Consume the "<". */
1999 cp_lexer_consume_token (parser->lexer);
2000 /* Parse the template arguments. */
2001 cp_parser_enclosed_template_argument_list (parser);
2002 /* Permanently remove the invalid template arguments so that
2003 this error message is not issued again. */
2004 if (start)
2005 cp_lexer_purge_tokens_after (parser->lexer, start);
2009 /* If parsing an integral constant-expression, issue an error message
2010 about the fact that THING appeared and return true. Otherwise,
2011 return false. In either case, set
2012 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2014 static bool
2015 cp_parser_non_integral_constant_expression (cp_parser *parser,
2016 const char *thing)
2018 parser->non_integral_constant_expression_p = true;
2019 if (parser->integral_constant_expression_p)
2021 if (!parser->allow_non_integral_constant_expression_p)
2023 error ("%s cannot appear in a constant-expression", thing);
2024 return true;
2027 return false;
2030 /* Emit a diagnostic for an invalid type name. SCOPE is the
2031 qualifying scope (or NULL, if none) for ID. This function commits
2032 to the current active tentative parse, if any. (Otherwise, the
2033 problematic construct might be encountered again later, resulting
2034 in duplicate error messages.) */
2036 static void
2037 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2039 tree decl, old_scope;
2040 /* Try to lookup the identifier. */
2041 old_scope = parser->scope;
2042 parser->scope = scope;
2043 decl = cp_parser_lookup_name_simple (parser, id);
2044 parser->scope = old_scope;
2045 /* If the lookup found a template-name, it means that the user forgot
2046 to specify an argument list. Emit a useful error message. */
2047 if (TREE_CODE (decl) == TEMPLATE_DECL)
2048 error ("invalid use of template-name %qE without an argument list",
2049 decl);
2050 else if (!parser->scope || parser->scope == error_mark_node)
2052 /* Issue an error message. */
2053 error ("%qE does not name a type", id);
2054 /* If we're in a template class, it's possible that the user was
2055 referring to a type from a base class. For example:
2057 template <typename T> struct A { typedef T X; };
2058 template <typename T> struct B : public A<T> { X x; };
2060 The user should have said "typename A<T>::X". */
2061 if (processing_template_decl && current_class_type
2062 && TYPE_BINFO (current_class_type))
2064 tree b;
2066 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2068 b = TREE_CHAIN (b))
2070 tree base_type = BINFO_TYPE (b);
2071 if (CLASS_TYPE_P (base_type)
2072 && dependent_type_p (base_type))
2074 tree field;
2075 /* Go from a particular instantiation of the
2076 template (which will have an empty TYPE_FIELDs),
2077 to the main version. */
2078 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2079 for (field = TYPE_FIELDS (base_type);
2080 field;
2081 field = TREE_CHAIN (field))
2082 if (TREE_CODE (field) == TYPE_DECL
2083 && DECL_NAME (field) == id)
2085 inform ("(perhaps %<typename %T::%E%> was intended)",
2086 BINFO_TYPE (b), id);
2087 break;
2089 if (field)
2090 break;
2095 /* Here we diagnose qualified-ids where the scope is actually correct,
2096 but the identifier does not resolve to a valid type name. */
2097 else
2099 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2100 error ("%qE in namespace %qE does not name a type",
2101 id, parser->scope);
2102 else if (TYPE_P (parser->scope))
2103 error ("%qE in class %qT does not name a type", id, parser->scope);
2104 else
2105 gcc_unreachable ();
2107 cp_parser_commit_to_tentative_parse (parser);
2110 /* Check for a common situation where a type-name should be present,
2111 but is not, and issue a sensible error message. Returns true if an
2112 invalid type-name was detected.
2114 The situation handled by this function are variable declarations of the
2115 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2116 Usually, `ID' should name a type, but if we got here it means that it
2117 does not. We try to emit the best possible error message depending on
2118 how exactly the id-expression looks like.
2121 static bool
2122 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2124 tree id;
2126 cp_parser_parse_tentatively (parser);
2127 id = cp_parser_id_expression (parser,
2128 /*template_keyword_p=*/false,
2129 /*check_dependency_p=*/true,
2130 /*template_p=*/NULL,
2131 /*declarator_p=*/true);
2132 /* After the id-expression, there should be a plain identifier,
2133 otherwise this is not a simple variable declaration. Also, if
2134 the scope is dependent, we cannot do much. */
2135 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2136 || (parser->scope && TYPE_P (parser->scope)
2137 && dependent_type_p (parser->scope)))
2139 cp_parser_abort_tentative_parse (parser);
2140 return false;
2142 if (!cp_parser_parse_definitely (parser)
2143 || TREE_CODE (id) != IDENTIFIER_NODE)
2144 return false;
2146 /* Emit a diagnostic for the invalid type. */
2147 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2148 /* Skip to the end of the declaration; there's no point in
2149 trying to process it. */
2150 cp_parser_skip_to_end_of_block_or_statement (parser);
2151 return true;
2154 /* Consume tokens up to, and including, the next non-nested closing `)'.
2155 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2156 are doing error recovery. Returns -1 if OR_COMMA is true and we
2157 found an unnested comma. */
2159 static int
2160 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2161 bool recovering,
2162 bool or_comma,
2163 bool consume_paren)
2165 unsigned paren_depth = 0;
2166 unsigned brace_depth = 0;
2167 int result;
2169 if (recovering && !or_comma
2170 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2171 return 0;
2173 while (true)
2175 cp_token *token;
2177 /* If we've run out of tokens, then there is no closing `)'. */
2178 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2180 result = 0;
2181 break;
2184 token = cp_lexer_peek_token (parser->lexer);
2186 /* This matches the processing in skip_to_end_of_statement. */
2187 if (token->type == CPP_SEMICOLON && !brace_depth)
2189 result = 0;
2190 break;
2192 if (token->type == CPP_OPEN_BRACE)
2193 ++brace_depth;
2194 if (token->type == CPP_CLOSE_BRACE)
2196 if (!brace_depth--)
2198 result = 0;
2199 break;
2202 if (recovering && or_comma && token->type == CPP_COMMA
2203 && !brace_depth && !paren_depth)
2205 result = -1;
2206 break;
2209 if (!brace_depth)
2211 /* If it is an `(', we have entered another level of nesting. */
2212 if (token->type == CPP_OPEN_PAREN)
2213 ++paren_depth;
2214 /* If it is a `)', then we might be done. */
2215 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2217 if (consume_paren)
2218 cp_lexer_consume_token (parser->lexer);
2220 result = 1;
2221 break;
2226 /* Consume the token. */
2227 cp_lexer_consume_token (parser->lexer);
2230 return result;
2233 /* Consume tokens until we reach the end of the current statement.
2234 Normally, that will be just before consuming a `;'. However, if a
2235 non-nested `}' comes first, then we stop before consuming that. */
2237 static void
2238 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2240 unsigned nesting_depth = 0;
2242 while (true)
2244 cp_token *token;
2246 /* Peek at the next token. */
2247 token = cp_lexer_peek_token (parser->lexer);
2248 /* If we've run out of tokens, stop. */
2249 if (token->type == CPP_EOF)
2250 break;
2251 /* If the next token is a `;', we have reached the end of the
2252 statement. */
2253 if (token->type == CPP_SEMICOLON && !nesting_depth)
2254 break;
2255 /* If the next token is a non-nested `}', then we have reached
2256 the end of the current block. */
2257 if (token->type == CPP_CLOSE_BRACE)
2259 /* If this is a non-nested `}', stop before consuming it.
2260 That way, when confronted with something like:
2262 { 3 + }
2264 we stop before consuming the closing `}', even though we
2265 have not yet reached a `;'. */
2266 if (nesting_depth == 0)
2267 break;
2268 /* If it is the closing `}' for a block that we have
2269 scanned, stop -- but only after consuming the token.
2270 That way given:
2272 void f g () { ... }
2273 typedef int I;
2275 we will stop after the body of the erroneously declared
2276 function, but before consuming the following `typedef'
2277 declaration. */
2278 if (--nesting_depth == 0)
2280 cp_lexer_consume_token (parser->lexer);
2281 break;
2284 /* If it the next token is a `{', then we are entering a new
2285 block. Consume the entire block. */
2286 else if (token->type == CPP_OPEN_BRACE)
2287 ++nesting_depth;
2288 /* Consume the token. */
2289 cp_lexer_consume_token (parser->lexer);
2293 /* This function is called at the end of a statement or declaration.
2294 If the next token is a semicolon, it is consumed; otherwise, error
2295 recovery is attempted. */
2297 static void
2298 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2300 /* Look for the trailing `;'. */
2301 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2303 /* If there is additional (erroneous) input, skip to the end of
2304 the statement. */
2305 cp_parser_skip_to_end_of_statement (parser);
2306 /* If the next token is now a `;', consume it. */
2307 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2308 cp_lexer_consume_token (parser->lexer);
2312 /* Skip tokens until we have consumed an entire block, or until we
2313 have consumed a non-nested `;'. */
2315 static void
2316 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2318 int nesting_depth = 0;
2320 while (nesting_depth >= 0)
2322 cp_token *token = cp_lexer_peek_token (parser->lexer);
2324 if (token->type == CPP_EOF)
2325 break;
2327 switch (token->type)
2329 case CPP_EOF:
2330 /* If we've run out of tokens, stop. */
2331 nesting_depth = -1;
2332 continue;
2334 case CPP_SEMICOLON:
2335 /* Stop if this is an unnested ';'. */
2336 if (!nesting_depth)
2337 nesting_depth = -1;
2338 break;
2340 case CPP_CLOSE_BRACE:
2341 /* Stop if this is an unnested '}', or closes the outermost
2342 nesting level. */
2343 nesting_depth--;
2344 if (!nesting_depth)
2345 nesting_depth = -1;
2346 break;
2348 case CPP_OPEN_BRACE:
2349 /* Nest. */
2350 nesting_depth++;
2351 break;
2353 default:
2354 break;
2357 /* Consume the token. */
2358 cp_lexer_consume_token (parser->lexer);
2363 /* Skip tokens until a non-nested closing curly brace is the next
2364 token. */
2366 static void
2367 cp_parser_skip_to_closing_brace (cp_parser *parser)
2369 unsigned nesting_depth = 0;
2371 while (true)
2373 cp_token *token;
2375 /* Peek at the next token. */
2376 token = cp_lexer_peek_token (parser->lexer);
2377 /* If we've run out of tokens, stop. */
2378 if (token->type == CPP_EOF)
2379 break;
2380 /* If the next token is a non-nested `}', then we have reached
2381 the end of the current block. */
2382 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2383 break;
2384 /* If it the next token is a `{', then we are entering a new
2385 block. Consume the entire block. */
2386 else if (token->type == CPP_OPEN_BRACE)
2387 ++nesting_depth;
2388 /* Consume the token. */
2389 cp_lexer_consume_token (parser->lexer);
2393 /* This is a simple wrapper around make_typename_type. When the id is
2394 an unresolved identifier node, we can provide a superior diagnostic
2395 using cp_parser_diagnose_invalid_type_name. */
2397 static tree
2398 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2400 tree result;
2401 if (TREE_CODE (id) == IDENTIFIER_NODE)
2403 result = make_typename_type (scope, id, typename_type,
2404 /*complain=*/0);
2405 if (result == error_mark_node)
2406 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2407 return result;
2409 return make_typename_type (scope, id, typename_type, tf_error);
2413 /* Create a new C++ parser. */
2415 static cp_parser *
2416 cp_parser_new (void)
2418 cp_parser *parser;
2419 cp_lexer *lexer;
2420 unsigned i;
2422 /* cp_lexer_new_main is called before calling ggc_alloc because
2423 cp_lexer_new_main might load a PCH file. */
2424 lexer = cp_lexer_new_main ();
2426 /* Initialize the binops_by_token so that we can get the tree
2427 directly from the token. */
2428 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2429 binops_by_token[binops[i].token_type] = binops[i];
2431 parser = GGC_CNEW (cp_parser);
2432 parser->lexer = lexer;
2433 parser->context = cp_parser_context_new (NULL);
2435 /* For now, we always accept GNU extensions. */
2436 parser->allow_gnu_extensions_p = 1;
2438 /* The `>' token is a greater-than operator, not the end of a
2439 template-id. */
2440 parser->greater_than_is_operator_p = true;
2442 parser->default_arg_ok_p = true;
2444 /* We are not parsing a constant-expression. */
2445 parser->integral_constant_expression_p = false;
2446 parser->allow_non_integral_constant_expression_p = false;
2447 parser->non_integral_constant_expression_p = false;
2449 /* Local variable names are not forbidden. */
2450 parser->local_variables_forbidden_p = false;
2452 /* We are not processing an `extern "C"' declaration. */
2453 parser->in_unbraced_linkage_specification_p = false;
2455 /* We are not processing a declarator. */
2456 parser->in_declarator_p = false;
2458 /* We are not processing a template-argument-list. */
2459 parser->in_template_argument_list_p = false;
2461 /* We are not in an iteration statement. */
2462 parser->in_iteration_statement_p = false;
2464 /* We are not in a switch statement. */
2465 parser->in_switch_statement_p = false;
2467 /* We are not parsing a type-id inside an expression. */
2468 parser->in_type_id_in_expr_p = false;
2470 /* Declarations aren't implicitly extern "C". */
2471 parser->implicit_extern_c = false;
2473 /* String literals should be translated to the execution character set. */
2474 parser->translate_strings_p = true;
2476 /* The unparsed function queue is empty. */
2477 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2479 /* There are no classes being defined. */
2480 parser->num_classes_being_defined = 0;
2482 /* No template parameters apply. */
2483 parser->num_template_parameter_lists = 0;
2485 return parser;
2488 /* Create a cp_lexer structure which will emit the tokens in CACHE
2489 and push it onto the parser's lexer stack. This is used for delayed
2490 parsing of in-class method bodies and default arguments, and should
2491 not be confused with tentative parsing. */
2492 static void
2493 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2495 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2496 lexer->next = parser->lexer;
2497 parser->lexer = lexer;
2499 /* Move the current source position to that of the first token in the
2500 new lexer. */
2501 cp_lexer_set_source_position_from_token (lexer->next_token);
2504 /* Pop the top lexer off the parser stack. This is never used for the
2505 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2506 static void
2507 cp_parser_pop_lexer (cp_parser *parser)
2509 cp_lexer *lexer = parser->lexer;
2510 parser->lexer = lexer->next;
2511 cp_lexer_destroy (lexer);
2513 /* Put the current source position back where it was before this
2514 lexer was pushed. */
2515 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2518 /* Lexical conventions [gram.lex] */
2520 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2521 identifier. */
2523 static tree
2524 cp_parser_identifier (cp_parser* parser)
2526 cp_token *token;
2528 /* Look for the identifier. */
2529 token = cp_parser_require (parser, CPP_NAME, "identifier");
2530 /* Return the value. */
2531 return token ? token->value : error_mark_node;
2534 /* Parse a sequence of adjacent string constants. Returns a
2535 TREE_STRING representing the combined, nul-terminated string
2536 constant. If TRANSLATE is true, translate the string to the
2537 execution character set. If WIDE_OK is true, a wide string is
2538 invalid here.
2540 C++98 [lex.string] says that if a narrow string literal token is
2541 adjacent to a wide string literal token, the behavior is undefined.
2542 However, C99 6.4.5p4 says that this results in a wide string literal.
2543 We follow C99 here, for consistency with the C front end.
2545 This code is largely lifted from lex_string() in c-lex.c.
2547 FUTURE: ObjC++ will need to handle @-strings here. */
2548 static tree
2549 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2551 tree value;
2552 bool wide = false;
2553 size_t count;
2554 struct obstack str_ob;
2555 cpp_string str, istr, *strs;
2556 cp_token *tok;
2558 tok = cp_lexer_peek_token (parser->lexer);
2559 if (!cp_parser_is_string_literal (tok))
2561 cp_parser_error (parser, "expected string-literal");
2562 return error_mark_node;
2565 /* Try to avoid the overhead of creating and destroying an obstack
2566 for the common case of just one string. */
2567 if (!cp_parser_is_string_literal
2568 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2570 cp_lexer_consume_token (parser->lexer);
2572 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2573 str.len = TREE_STRING_LENGTH (tok->value);
2574 count = 1;
2575 if (tok->type == CPP_WSTRING)
2576 wide = true;
2578 strs = &str;
2580 else
2582 gcc_obstack_init (&str_ob);
2583 count = 0;
2587 cp_lexer_consume_token (parser->lexer);
2588 count++;
2589 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2590 str.len = TREE_STRING_LENGTH (tok->value);
2591 if (tok->type == CPP_WSTRING)
2592 wide = true;
2594 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2596 tok = cp_lexer_peek_token (parser->lexer);
2598 while (cp_parser_is_string_literal (tok));
2600 strs = (cpp_string *) obstack_finish (&str_ob);
2603 if (wide && !wide_ok)
2605 cp_parser_error (parser, "a wide string is invalid in this context");
2606 wide = false;
2609 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2610 (parse_in, strs, count, &istr, wide))
2612 value = build_string (istr.len, (char *)istr.text);
2613 free ((void *)istr.text);
2615 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2616 value = fix_string_type (value);
2618 else
2619 /* cpp_interpret_string has issued an error. */
2620 value = error_mark_node;
2622 if (count > 1)
2623 obstack_free (&str_ob, 0);
2625 return value;
2629 /* Basic concepts [gram.basic] */
2631 /* Parse a translation-unit.
2633 translation-unit:
2634 declaration-seq [opt]
2636 Returns TRUE if all went well. */
2638 static bool
2639 cp_parser_translation_unit (cp_parser* parser)
2641 /* The address of the first non-permanent object on the declarator
2642 obstack. */
2643 static void *declarator_obstack_base;
2645 bool success;
2647 /* Create the declarator obstack, if necessary. */
2648 if (!cp_error_declarator)
2650 gcc_obstack_init (&declarator_obstack);
2651 /* Create the error declarator. */
2652 cp_error_declarator = make_declarator (cdk_error);
2653 /* Create the empty parameter list. */
2654 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2655 /* Remember where the base of the declarator obstack lies. */
2656 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2659 cp_parser_declaration_seq_opt (parser);
2661 /* If there are no tokens left then all went well. */
2662 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2664 /* Get rid of the token array; we don't need it any more. */
2665 cp_lexer_destroy (parser->lexer);
2666 parser->lexer = NULL;
2668 /* This file might have been a context that's implicitly extern
2669 "C". If so, pop the lang context. (Only relevant for PCH.) */
2670 if (parser->implicit_extern_c)
2672 pop_lang_context ();
2673 parser->implicit_extern_c = false;
2676 /* Finish up. */
2677 finish_translation_unit ();
2679 success = true;
2681 else
2683 cp_parser_error (parser, "expected declaration");
2684 success = false;
2687 /* Make sure the declarator obstack was fully cleaned up. */
2688 gcc_assert (obstack_next_free (&declarator_obstack)
2689 == declarator_obstack_base);
2691 /* All went well. */
2692 return success;
2695 /* Expressions [gram.expr] */
2697 /* Parse a primary-expression.
2699 primary-expression:
2700 literal
2701 this
2702 ( expression )
2703 id-expression
2705 GNU Extensions:
2707 primary-expression:
2708 ( compound-statement )
2709 __builtin_va_arg ( assignment-expression , type-id )
2711 Objective-C++ Extension:
2713 primary-expression:
2714 objc-expression
2716 literal:
2717 __null
2719 CAST_P is true if this primary expression is the target of a cast.
2721 Returns a representation of the expression.
2723 *IDK indicates what kind of id-expression (if any) was present.
2725 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2726 used as the operand of a pointer-to-member. In that case,
2727 *QUALIFYING_CLASS gives the class that is used as the qualifying
2728 class in the pointer-to-member. */
2730 static tree
2731 cp_parser_primary_expression (cp_parser *parser,
2732 bool cast_p,
2733 cp_id_kind *idk,
2734 tree *qualifying_class)
2736 cp_token *token;
2738 /* Assume the primary expression is not an id-expression. */
2739 *idk = CP_ID_KIND_NONE;
2740 /* And that it cannot be used as pointer-to-member. */
2741 *qualifying_class = NULL_TREE;
2743 /* Peek at the next token. */
2744 token = cp_lexer_peek_token (parser->lexer);
2745 switch (token->type)
2747 /* literal:
2748 integer-literal
2749 character-literal
2750 floating-literal
2751 string-literal
2752 boolean-literal */
2753 case CPP_CHAR:
2754 case CPP_WCHAR:
2755 case CPP_NUMBER:
2756 token = cp_lexer_consume_token (parser->lexer);
2757 /* Floating-point literals are only allowed in an integral
2758 constant expression if they are cast to an integral or
2759 enumeration type. */
2760 if (TREE_CODE (token->value) == REAL_CST
2761 && parser->integral_constant_expression_p
2762 && pedantic)
2764 /* CAST_P will be set even in invalid code like "int(2.7 +
2765 ...)". Therefore, we have to check that the next token
2766 is sure to end the cast. */
2767 if (cast_p)
2769 cp_token *next_token;
2771 next_token = cp_lexer_peek_token (parser->lexer);
2772 if (/* The comma at the end of an
2773 enumerator-definition. */
2774 next_token->type != CPP_COMMA
2775 /* The curly brace at the end of an enum-specifier. */
2776 && next_token->type != CPP_CLOSE_BRACE
2777 /* The end of a statement. */
2778 && next_token->type != CPP_SEMICOLON
2779 /* The end of the cast-expression. */
2780 && next_token->type != CPP_CLOSE_PAREN
2781 /* The end of an array bound. */
2782 && next_token->type != CPP_CLOSE_SQUARE
2783 /* The closing ">" in a template-argument-list. */
2784 && (next_token->type != CPP_GREATER
2785 || parser->greater_than_is_operator_p))
2786 cast_p = false;
2789 /* If we are within a cast, then the constraint that the
2790 cast is to an integral or enumeration type will be
2791 checked at that point. If we are not within a cast, then
2792 this code is invalid. */
2793 if (!cast_p)
2794 cp_parser_non_integral_constant_expression
2795 (parser, "floating-point literal");
2797 return token->value;
2799 case CPP_STRING:
2800 case CPP_WSTRING:
2801 /* ??? Should wide strings be allowed when parser->translate_strings_p
2802 is false (i.e. in attributes)? If not, we can kill the third
2803 argument to cp_parser_string_literal. */
2804 return cp_parser_string_literal (parser,
2805 parser->translate_strings_p,
2806 true);
2808 case CPP_OPEN_PAREN:
2810 tree expr;
2811 bool saved_greater_than_is_operator_p;
2813 /* Consume the `('. */
2814 cp_lexer_consume_token (parser->lexer);
2815 /* Within a parenthesized expression, a `>' token is always
2816 the greater-than operator. */
2817 saved_greater_than_is_operator_p
2818 = parser->greater_than_is_operator_p;
2819 parser->greater_than_is_operator_p = true;
2820 /* If we see `( { ' then we are looking at the beginning of
2821 a GNU statement-expression. */
2822 if (cp_parser_allow_gnu_extensions_p (parser)
2823 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2825 /* Statement-expressions are not allowed by the standard. */
2826 if (pedantic)
2827 pedwarn ("ISO C++ forbids braced-groups within expressions");
2829 /* And they're not allowed outside of a function-body; you
2830 cannot, for example, write:
2832 int i = ({ int j = 3; j + 1; });
2834 at class or namespace scope. */
2835 if (!at_function_scope_p ())
2836 error ("statement-expressions are allowed only inside functions");
2837 /* Start the statement-expression. */
2838 expr = begin_stmt_expr ();
2839 /* Parse the compound-statement. */
2840 cp_parser_compound_statement (parser, expr, false);
2841 /* Finish up. */
2842 expr = finish_stmt_expr (expr, false);
2844 else
2846 /* Parse the parenthesized expression. */
2847 expr = cp_parser_expression (parser, cast_p);
2848 /* Let the front end know that this expression was
2849 enclosed in parentheses. This matters in case, for
2850 example, the expression is of the form `A::B', since
2851 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2852 not. */
2853 finish_parenthesized_expr (expr);
2855 /* The `>' token might be the end of a template-id or
2856 template-parameter-list now. */
2857 parser->greater_than_is_operator_p
2858 = saved_greater_than_is_operator_p;
2859 /* Consume the `)'. */
2860 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2861 cp_parser_skip_to_end_of_statement (parser);
2863 return expr;
2866 case CPP_KEYWORD:
2867 switch (token->keyword)
2869 /* These two are the boolean literals. */
2870 case RID_TRUE:
2871 cp_lexer_consume_token (parser->lexer);
2872 return boolean_true_node;
2873 case RID_FALSE:
2874 cp_lexer_consume_token (parser->lexer);
2875 return boolean_false_node;
2877 /* The `__null' literal. */
2878 case RID_NULL:
2879 cp_lexer_consume_token (parser->lexer);
2880 return null_node;
2882 /* Recognize the `this' keyword. */
2883 case RID_THIS:
2884 cp_lexer_consume_token (parser->lexer);
2885 if (parser->local_variables_forbidden_p)
2887 error ("%<this%> may not be used in this context");
2888 return error_mark_node;
2890 /* Pointers cannot appear in constant-expressions. */
2891 if (cp_parser_non_integral_constant_expression (parser,
2892 "`this'"))
2893 return error_mark_node;
2894 return finish_this_expr ();
2896 /* The `operator' keyword can be the beginning of an
2897 id-expression. */
2898 case RID_OPERATOR:
2899 goto id_expression;
2901 case RID_FUNCTION_NAME:
2902 case RID_PRETTY_FUNCTION_NAME:
2903 case RID_C99_FUNCTION_NAME:
2904 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2905 __func__ are the names of variables -- but they are
2906 treated specially. Therefore, they are handled here,
2907 rather than relying on the generic id-expression logic
2908 below. Grammatically, these names are id-expressions.
2910 Consume the token. */
2911 token = cp_lexer_consume_token (parser->lexer);
2912 /* Look up the name. */
2913 return finish_fname (token->value);
2915 case RID_VA_ARG:
2917 tree expression;
2918 tree type;
2920 /* The `__builtin_va_arg' construct is used to handle
2921 `va_arg'. Consume the `__builtin_va_arg' token. */
2922 cp_lexer_consume_token (parser->lexer);
2923 /* Look for the opening `('. */
2924 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2925 /* Now, parse the assignment-expression. */
2926 expression = cp_parser_assignment_expression (parser,
2927 /*cast_p=*/false);
2928 /* Look for the `,'. */
2929 cp_parser_require (parser, CPP_COMMA, "`,'");
2930 /* Parse the type-id. */
2931 type = cp_parser_type_id (parser);
2932 /* Look for the closing `)'. */
2933 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2934 /* Using `va_arg' in a constant-expression is not
2935 allowed. */
2936 if (cp_parser_non_integral_constant_expression (parser,
2937 "`va_arg'"))
2938 return error_mark_node;
2939 return build_x_va_arg (expression, type);
2942 case RID_OFFSETOF:
2943 return cp_parser_builtin_offsetof (parser);
2945 /* Objective-C++ expressions. */
2946 case RID_AT_ENCODE:
2947 case RID_AT_PROTOCOL:
2948 case RID_AT_SELECTOR:
2949 return cp_parser_objc_expression (parser);
2951 default:
2952 cp_parser_error (parser, "expected primary-expression");
2953 return error_mark_node;
2956 /* An id-expression can start with either an identifier, a
2957 `::' as the beginning of a qualified-id, or the "operator"
2958 keyword. */
2959 case CPP_NAME:
2960 case CPP_SCOPE:
2961 case CPP_TEMPLATE_ID:
2962 case CPP_NESTED_NAME_SPECIFIER:
2964 tree id_expression;
2965 tree decl;
2966 const char *error_msg;
2968 id_expression:
2969 /* Parse the id-expression. */
2970 id_expression
2971 = cp_parser_id_expression (parser,
2972 /*template_keyword_p=*/false,
2973 /*check_dependency_p=*/true,
2974 /*template_p=*/NULL,
2975 /*declarator_p=*/false);
2976 if (id_expression == error_mark_node)
2977 return error_mark_node;
2978 /* If we have a template-id, then no further lookup is
2979 required. If the template-id was for a template-class, we
2980 will sometimes have a TYPE_DECL at this point. */
2981 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2982 || TREE_CODE (id_expression) == TYPE_DECL)
2983 decl = id_expression;
2984 /* Look up the name. */
2985 else
2987 bool ambiguous_p;
2989 decl = cp_parser_lookup_name (parser, id_expression,
2990 none_type,
2991 /*is_template=*/false,
2992 /*is_namespace=*/false,
2993 /*check_dependency=*/true,
2994 &ambiguous_p);
2995 /* If the lookup was ambiguous, an error will already have
2996 been issued. */
2997 if (ambiguous_p)
2998 return error_mark_node;
3000 /* In Objective-C++, an instance variable (ivar) may be preferred
3001 to whatever cp_parser_lookup_name() found. */
3002 decl = objc_lookup_ivar (decl, id_expression);
3004 /* If name lookup gives us a SCOPE_REF, then the
3005 qualifying scope was dependent. Just propagate the
3006 name. */
3007 if (TREE_CODE (decl) == SCOPE_REF)
3009 if (TYPE_P (TREE_OPERAND (decl, 0)))
3010 *qualifying_class = TREE_OPERAND (decl, 0);
3011 return decl;
3013 /* Check to see if DECL is a local variable in a context
3014 where that is forbidden. */
3015 if (parser->local_variables_forbidden_p
3016 && local_variable_p (decl))
3018 /* It might be that we only found DECL because we are
3019 trying to be generous with pre-ISO scoping rules.
3020 For example, consider:
3022 int i;
3023 void g() {
3024 for (int i = 0; i < 10; ++i) {}
3025 extern void f(int j = i);
3028 Here, name look up will originally find the out
3029 of scope `i'. We need to issue a warning message,
3030 but then use the global `i'. */
3031 decl = check_for_out_of_scope_variable (decl);
3032 if (local_variable_p (decl))
3034 error ("local variable %qD may not appear in this context",
3035 decl);
3036 return error_mark_node;
3041 decl = finish_id_expression (id_expression, decl, parser->scope,
3042 idk, qualifying_class,
3043 parser->integral_constant_expression_p,
3044 parser->allow_non_integral_constant_expression_p,
3045 &parser->non_integral_constant_expression_p,
3046 &error_msg);
3047 if (error_msg)
3048 cp_parser_error (parser, error_msg);
3049 return decl;
3052 /* Anything else is an error. */
3053 default:
3054 /* ...unless we have an Objective-C++ message or string literal, that is. */
3055 if (c_dialect_objc ()
3056 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3057 return cp_parser_objc_expression (parser);
3059 cp_parser_error (parser, "expected primary-expression");
3060 return error_mark_node;
3064 /* Parse an id-expression.
3066 id-expression:
3067 unqualified-id
3068 qualified-id
3070 qualified-id:
3071 :: [opt] nested-name-specifier template [opt] unqualified-id
3072 :: identifier
3073 :: operator-function-id
3074 :: template-id
3076 Return a representation of the unqualified portion of the
3077 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3078 a `::' or nested-name-specifier.
3080 Often, if the id-expression was a qualified-id, the caller will
3081 want to make a SCOPE_REF to represent the qualified-id. This
3082 function does not do this in order to avoid wastefully creating
3083 SCOPE_REFs when they are not required.
3085 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3086 `template' keyword.
3088 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3089 uninstantiated templates.
3091 If *TEMPLATE_P is non-NULL, it is set to true iff the
3092 `template' keyword is used to explicitly indicate that the entity
3093 named is a template.
3095 If DECLARATOR_P is true, the id-expression is appearing as part of
3096 a declarator, rather than as part of an expression. */
3098 static tree
3099 cp_parser_id_expression (cp_parser *parser,
3100 bool template_keyword_p,
3101 bool check_dependency_p,
3102 bool *template_p,
3103 bool declarator_p)
3105 bool global_scope_p;
3106 bool nested_name_specifier_p;
3108 /* Assume the `template' keyword was not used. */
3109 if (template_p)
3110 *template_p = false;
3112 /* Look for the optional `::' operator. */
3113 global_scope_p
3114 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3115 != NULL_TREE);
3116 /* Look for the optional nested-name-specifier. */
3117 nested_name_specifier_p
3118 = (cp_parser_nested_name_specifier_opt (parser,
3119 /*typename_keyword_p=*/false,
3120 check_dependency_p,
3121 /*type_p=*/false,
3122 declarator_p)
3123 != NULL_TREE);
3124 /* If there is a nested-name-specifier, then we are looking at
3125 the first qualified-id production. */
3126 if (nested_name_specifier_p)
3128 tree saved_scope;
3129 tree saved_object_scope;
3130 tree saved_qualifying_scope;
3131 tree unqualified_id;
3132 bool is_template;
3134 /* See if the next token is the `template' keyword. */
3135 if (!template_p)
3136 template_p = &is_template;
3137 *template_p = cp_parser_optional_template_keyword (parser);
3138 /* Name lookup we do during the processing of the
3139 unqualified-id might obliterate SCOPE. */
3140 saved_scope = parser->scope;
3141 saved_object_scope = parser->object_scope;
3142 saved_qualifying_scope = parser->qualifying_scope;
3143 /* Process the final unqualified-id. */
3144 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3145 check_dependency_p,
3146 declarator_p);
3147 /* Restore the SAVED_SCOPE for our caller. */
3148 parser->scope = saved_scope;
3149 parser->object_scope = saved_object_scope;
3150 parser->qualifying_scope = saved_qualifying_scope;
3152 return unqualified_id;
3154 /* Otherwise, if we are in global scope, then we are looking at one
3155 of the other qualified-id productions. */
3156 else if (global_scope_p)
3158 cp_token *token;
3159 tree id;
3161 /* Peek at the next token. */
3162 token = cp_lexer_peek_token (parser->lexer);
3164 /* If it's an identifier, and the next token is not a "<", then
3165 we can avoid the template-id case. This is an optimization
3166 for this common case. */
3167 if (token->type == CPP_NAME
3168 && !cp_parser_nth_token_starts_template_argument_list_p
3169 (parser, 2))
3170 return cp_parser_identifier (parser);
3172 cp_parser_parse_tentatively (parser);
3173 /* Try a template-id. */
3174 id = cp_parser_template_id (parser,
3175 /*template_keyword_p=*/false,
3176 /*check_dependency_p=*/true,
3177 declarator_p);
3178 /* If that worked, we're done. */
3179 if (cp_parser_parse_definitely (parser))
3180 return id;
3182 /* Peek at the next token. (Changes in the token buffer may
3183 have invalidated the pointer obtained above.) */
3184 token = cp_lexer_peek_token (parser->lexer);
3186 switch (token->type)
3188 case CPP_NAME:
3189 return cp_parser_identifier (parser);
3191 case CPP_KEYWORD:
3192 if (token->keyword == RID_OPERATOR)
3193 return cp_parser_operator_function_id (parser);
3194 /* Fall through. */
3196 default:
3197 cp_parser_error (parser, "expected id-expression");
3198 return error_mark_node;
3201 else
3202 return cp_parser_unqualified_id (parser, template_keyword_p,
3203 /*check_dependency_p=*/true,
3204 declarator_p);
3207 /* Parse an unqualified-id.
3209 unqualified-id:
3210 identifier
3211 operator-function-id
3212 conversion-function-id
3213 ~ class-name
3214 template-id
3216 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3217 keyword, in a construct like `A::template ...'.
3219 Returns a representation of unqualified-id. For the `identifier'
3220 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3221 production a BIT_NOT_EXPR is returned; the operand of the
3222 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3223 other productions, see the documentation accompanying the
3224 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3225 names are looked up in uninstantiated templates. If DECLARATOR_P
3226 is true, the unqualified-id is appearing as part of a declarator,
3227 rather than as part of an expression. */
3229 static tree
3230 cp_parser_unqualified_id (cp_parser* parser,
3231 bool template_keyword_p,
3232 bool check_dependency_p,
3233 bool declarator_p)
3235 cp_token *token;
3237 /* Peek at the next token. */
3238 token = cp_lexer_peek_token (parser->lexer);
3240 switch (token->type)
3242 case CPP_NAME:
3244 tree id;
3246 /* We don't know yet whether or not this will be a
3247 template-id. */
3248 cp_parser_parse_tentatively (parser);
3249 /* Try a template-id. */
3250 id = cp_parser_template_id (parser, template_keyword_p,
3251 check_dependency_p,
3252 declarator_p);
3253 /* If it worked, we're done. */
3254 if (cp_parser_parse_definitely (parser))
3255 return id;
3256 /* Otherwise, it's an ordinary identifier. */
3257 return cp_parser_identifier (parser);
3260 case CPP_TEMPLATE_ID:
3261 return cp_parser_template_id (parser, template_keyword_p,
3262 check_dependency_p,
3263 declarator_p);
3265 case CPP_COMPL:
3267 tree type_decl;
3268 tree qualifying_scope;
3269 tree object_scope;
3270 tree scope;
3271 bool done;
3273 /* Consume the `~' token. */
3274 cp_lexer_consume_token (parser->lexer);
3275 /* Parse the class-name. The standard, as written, seems to
3276 say that:
3278 template <typename T> struct S { ~S (); };
3279 template <typename T> S<T>::~S() {}
3281 is invalid, since `~' must be followed by a class-name, but
3282 `S<T>' is dependent, and so not known to be a class.
3283 That's not right; we need to look in uninstantiated
3284 templates. A further complication arises from:
3286 template <typename T> void f(T t) {
3287 t.T::~T();
3290 Here, it is not possible to look up `T' in the scope of `T'
3291 itself. We must look in both the current scope, and the
3292 scope of the containing complete expression.
3294 Yet another issue is:
3296 struct S {
3297 int S;
3298 ~S();
3301 S::~S() {}
3303 The standard does not seem to say that the `S' in `~S'
3304 should refer to the type `S' and not the data member
3305 `S::S'. */
3307 /* DR 244 says that we look up the name after the "~" in the
3308 same scope as we looked up the qualifying name. That idea
3309 isn't fully worked out; it's more complicated than that. */
3310 scope = parser->scope;
3311 object_scope = parser->object_scope;
3312 qualifying_scope = parser->qualifying_scope;
3314 /* If the name is of the form "X::~X" it's OK. */
3315 if (scope && TYPE_P (scope)
3316 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3317 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3318 == CPP_OPEN_PAREN)
3319 && (cp_lexer_peek_token (parser->lexer)->value
3320 == TYPE_IDENTIFIER (scope)))
3322 cp_lexer_consume_token (parser->lexer);
3323 return build_nt (BIT_NOT_EXPR, scope);
3326 /* If there was an explicit qualification (S::~T), first look
3327 in the scope given by the qualification (i.e., S). */
3328 done = false;
3329 type_decl = NULL_TREE;
3330 if (scope)
3332 cp_parser_parse_tentatively (parser);
3333 type_decl = cp_parser_class_name (parser,
3334 /*typename_keyword_p=*/false,
3335 /*template_keyword_p=*/false,
3336 none_type,
3337 /*check_dependency=*/false,
3338 /*class_head_p=*/false,
3339 declarator_p);
3340 if (cp_parser_parse_definitely (parser))
3341 done = true;
3343 /* In "N::S::~S", look in "N" as well. */
3344 if (!done && scope && qualifying_scope)
3346 cp_parser_parse_tentatively (parser);
3347 parser->scope = qualifying_scope;
3348 parser->object_scope = NULL_TREE;
3349 parser->qualifying_scope = NULL_TREE;
3350 type_decl
3351 = cp_parser_class_name (parser,
3352 /*typename_keyword_p=*/false,
3353 /*template_keyword_p=*/false,
3354 none_type,
3355 /*check_dependency=*/false,
3356 /*class_head_p=*/false,
3357 declarator_p);
3358 if (cp_parser_parse_definitely (parser))
3359 done = true;
3361 /* In "p->S::~T", look in the scope given by "*p" as well. */
3362 else if (!done && object_scope)
3364 cp_parser_parse_tentatively (parser);
3365 parser->scope = object_scope;
3366 parser->object_scope = NULL_TREE;
3367 parser->qualifying_scope = NULL_TREE;
3368 type_decl
3369 = cp_parser_class_name (parser,
3370 /*typename_keyword_p=*/false,
3371 /*template_keyword_p=*/false,
3372 none_type,
3373 /*check_dependency=*/false,
3374 /*class_head_p=*/false,
3375 declarator_p);
3376 if (cp_parser_parse_definitely (parser))
3377 done = true;
3379 /* Look in the surrounding context. */
3380 if (!done)
3382 parser->scope = NULL_TREE;
3383 parser->object_scope = NULL_TREE;
3384 parser->qualifying_scope = NULL_TREE;
3385 type_decl
3386 = cp_parser_class_name (parser,
3387 /*typename_keyword_p=*/false,
3388 /*template_keyword_p=*/false,
3389 none_type,
3390 /*check_dependency=*/false,
3391 /*class_head_p=*/false,
3392 declarator_p);
3394 /* If an error occurred, assume that the name of the
3395 destructor is the same as the name of the qualifying
3396 class. That allows us to keep parsing after running
3397 into ill-formed destructor names. */
3398 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3399 return build_nt (BIT_NOT_EXPR, scope);
3400 else if (type_decl == error_mark_node)
3401 return error_mark_node;
3403 /* [class.dtor]
3405 A typedef-name that names a class shall not be used as the
3406 identifier in the declarator for a destructor declaration. */
3407 if (declarator_p
3408 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3409 && !DECL_SELF_REFERENCE_P (type_decl)
3410 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3411 error ("typedef-name %qD used as destructor declarator",
3412 type_decl);
3414 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3417 case CPP_KEYWORD:
3418 if (token->keyword == RID_OPERATOR)
3420 tree id;
3422 /* This could be a template-id, so we try that first. */
3423 cp_parser_parse_tentatively (parser);
3424 /* Try a template-id. */
3425 id = cp_parser_template_id (parser, template_keyword_p,
3426 /*check_dependency_p=*/true,
3427 declarator_p);
3428 /* If that worked, we're done. */
3429 if (cp_parser_parse_definitely (parser))
3430 return id;
3431 /* We still don't know whether we're looking at an
3432 operator-function-id or a conversion-function-id. */
3433 cp_parser_parse_tentatively (parser);
3434 /* Try an operator-function-id. */
3435 id = cp_parser_operator_function_id (parser);
3436 /* If that didn't work, try a conversion-function-id. */
3437 if (!cp_parser_parse_definitely (parser))
3438 id = cp_parser_conversion_function_id (parser);
3440 return id;
3442 /* Fall through. */
3444 default:
3445 cp_parser_error (parser, "expected unqualified-id");
3446 return error_mark_node;
3450 /* Parse an (optional) nested-name-specifier.
3452 nested-name-specifier:
3453 class-or-namespace-name :: nested-name-specifier [opt]
3454 class-or-namespace-name :: template nested-name-specifier [opt]
3456 PARSER->SCOPE should be set appropriately before this function is
3457 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3458 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3459 in name lookups.
3461 Sets PARSER->SCOPE to the class (TYPE) or namespace
3462 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3463 it unchanged if there is no nested-name-specifier. Returns the new
3464 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3466 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3467 part of a declaration and/or decl-specifier. */
3469 static tree
3470 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3471 bool typename_keyword_p,
3472 bool check_dependency_p,
3473 bool type_p,
3474 bool is_declaration)
3476 bool success = false;
3477 tree access_check = NULL_TREE;
3478 cp_token_position start = 0;
3479 cp_token *token;
3481 /* If the next token corresponds to a nested name specifier, there
3482 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3483 false, it may have been true before, in which case something
3484 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3485 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3486 CHECK_DEPENDENCY_P is false, we have to fall through into the
3487 main loop. */
3488 if (check_dependency_p
3489 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3491 cp_parser_pre_parsed_nested_name_specifier (parser);
3492 return parser->scope;
3495 /* Remember where the nested-name-specifier starts. */
3496 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3497 start = cp_lexer_token_position (parser->lexer, false);
3499 push_deferring_access_checks (dk_deferred);
3501 while (true)
3503 tree new_scope;
3504 tree old_scope;
3505 tree saved_qualifying_scope;
3506 bool template_keyword_p;
3508 /* Spot cases that cannot be the beginning of a
3509 nested-name-specifier. */
3510 token = cp_lexer_peek_token (parser->lexer);
3512 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3513 the already parsed nested-name-specifier. */
3514 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3516 /* Grab the nested-name-specifier and continue the loop. */
3517 cp_parser_pre_parsed_nested_name_specifier (parser);
3518 success = true;
3519 continue;
3522 /* Spot cases that cannot be the beginning of a
3523 nested-name-specifier. On the second and subsequent times
3524 through the loop, we look for the `template' keyword. */
3525 if (success && token->keyword == RID_TEMPLATE)
3527 /* A template-id can start a nested-name-specifier. */
3528 else if (token->type == CPP_TEMPLATE_ID)
3530 else
3532 /* If the next token is not an identifier, then it is
3533 definitely not a class-or-namespace-name. */
3534 if (token->type != CPP_NAME)
3535 break;
3536 /* If the following token is neither a `<' (to begin a
3537 template-id), nor a `::', then we are not looking at a
3538 nested-name-specifier. */
3539 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3540 if (token->type != CPP_SCOPE
3541 && !cp_parser_nth_token_starts_template_argument_list_p
3542 (parser, 2))
3543 break;
3546 /* The nested-name-specifier is optional, so we parse
3547 tentatively. */
3548 cp_parser_parse_tentatively (parser);
3550 /* Look for the optional `template' keyword, if this isn't the
3551 first time through the loop. */
3552 if (success)
3553 template_keyword_p = cp_parser_optional_template_keyword (parser);
3554 else
3555 template_keyword_p = false;
3557 /* Save the old scope since the name lookup we are about to do
3558 might destroy it. */
3559 old_scope = parser->scope;
3560 saved_qualifying_scope = parser->qualifying_scope;
3561 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3562 look up names in "X<T>::I" in order to determine that "Y" is
3563 a template. So, if we have a typename at this point, we make
3564 an effort to look through it. */
3565 if (is_declaration
3566 && !typename_keyword_p
3567 && parser->scope
3568 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3569 parser->scope = resolve_typename_type (parser->scope,
3570 /*only_current_p=*/false);
3571 /* Parse the qualifying entity. */
3572 new_scope
3573 = cp_parser_class_or_namespace_name (parser,
3574 typename_keyword_p,
3575 template_keyword_p,
3576 check_dependency_p,
3577 type_p,
3578 is_declaration);
3579 /* Look for the `::' token. */
3580 cp_parser_require (parser, CPP_SCOPE, "`::'");
3582 /* If we found what we wanted, we keep going; otherwise, we're
3583 done. */
3584 if (!cp_parser_parse_definitely (parser))
3586 bool error_p = false;
3588 /* Restore the OLD_SCOPE since it was valid before the
3589 failed attempt at finding the last
3590 class-or-namespace-name. */
3591 parser->scope = old_scope;
3592 parser->qualifying_scope = saved_qualifying_scope;
3593 /* If the next token is an identifier, and the one after
3594 that is a `::', then any valid interpretation would have
3595 found a class-or-namespace-name. */
3596 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3597 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3598 == CPP_SCOPE)
3599 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3600 != CPP_COMPL))
3602 token = cp_lexer_consume_token (parser->lexer);
3603 if (!error_p)
3605 tree decl;
3607 decl = cp_parser_lookup_name_simple (parser, token->value);
3608 if (TREE_CODE (decl) == TEMPLATE_DECL)
3609 error ("%qD used without template parameters", decl);
3610 else
3611 cp_parser_name_lookup_error
3612 (parser, token->value, decl,
3613 "is not a class or namespace");
3614 parser->scope = NULL_TREE;
3615 error_p = true;
3616 /* Treat this as a successful nested-name-specifier
3617 due to:
3619 [basic.lookup.qual]
3621 If the name found is not a class-name (clause
3622 _class_) or namespace-name (_namespace.def_), the
3623 program is ill-formed. */
3624 success = true;
3626 cp_lexer_consume_token (parser->lexer);
3628 break;
3631 /* We've found one valid nested-name-specifier. */
3632 success = true;
3633 /* Make sure we look in the right scope the next time through
3634 the loop. */
3635 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3636 ? TREE_TYPE (new_scope)
3637 : new_scope);
3638 /* If it is a class scope, try to complete it; we are about to
3639 be looking up names inside the class. */
3640 if (TYPE_P (parser->scope)
3641 /* Since checking types for dependency can be expensive,
3642 avoid doing it if the type is already complete. */
3643 && !COMPLETE_TYPE_P (parser->scope)
3644 /* Do not try to complete dependent types. */
3645 && !dependent_type_p (parser->scope))
3646 complete_type (parser->scope);
3649 /* Retrieve any deferred checks. Do not pop this access checks yet
3650 so the memory will not be reclaimed during token replacing below. */
3651 access_check = get_deferred_access_checks ();
3653 /* If parsing tentatively, replace the sequence of tokens that makes
3654 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3655 token. That way, should we re-parse the token stream, we will
3656 not have to repeat the effort required to do the parse, nor will
3657 we issue duplicate error messages. */
3658 if (success && start)
3660 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3662 /* Reset the contents of the START token. */
3663 token->type = CPP_NESTED_NAME_SPECIFIER;
3664 token->value = build_tree_list (access_check, parser->scope);
3665 TREE_TYPE (token->value) = parser->qualifying_scope;
3666 token->keyword = RID_MAX;
3668 /* Purge all subsequent tokens. */
3669 cp_lexer_purge_tokens_after (parser->lexer, start);
3672 pop_deferring_access_checks ();
3673 return success ? parser->scope : NULL_TREE;
3676 /* Parse a nested-name-specifier. See
3677 cp_parser_nested_name_specifier_opt for details. This function
3678 behaves identically, except that it will an issue an error if no
3679 nested-name-specifier is present. */
3681 static tree
3682 cp_parser_nested_name_specifier (cp_parser *parser,
3683 bool typename_keyword_p,
3684 bool check_dependency_p,
3685 bool type_p,
3686 bool is_declaration)
3688 tree scope;
3690 /* Look for the nested-name-specifier. */
3691 scope = cp_parser_nested_name_specifier_opt (parser,
3692 typename_keyword_p,
3693 check_dependency_p,
3694 type_p,
3695 is_declaration);
3696 /* If it was not present, issue an error message. */
3697 if (!scope)
3699 cp_parser_error (parser, "expected nested-name-specifier");
3700 parser->scope = NULL_TREE;
3703 return scope;
3706 /* Parse a class-or-namespace-name.
3708 class-or-namespace-name:
3709 class-name
3710 namespace-name
3712 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3713 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3714 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3715 TYPE_P is TRUE iff the next name should be taken as a class-name,
3716 even the same name is declared to be another entity in the same
3717 scope.
3719 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3720 specified by the class-or-namespace-name. If neither is found the
3721 ERROR_MARK_NODE is returned. */
3723 static tree
3724 cp_parser_class_or_namespace_name (cp_parser *parser,
3725 bool typename_keyword_p,
3726 bool template_keyword_p,
3727 bool check_dependency_p,
3728 bool type_p,
3729 bool is_declaration)
3731 tree saved_scope;
3732 tree saved_qualifying_scope;
3733 tree saved_object_scope;
3734 tree scope;
3735 bool only_class_p;
3737 /* Before we try to parse the class-name, we must save away the
3738 current PARSER->SCOPE since cp_parser_class_name will destroy
3739 it. */
3740 saved_scope = parser->scope;
3741 saved_qualifying_scope = parser->qualifying_scope;
3742 saved_object_scope = parser->object_scope;
3743 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3744 there is no need to look for a namespace-name. */
3745 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3746 if (!only_class_p)
3747 cp_parser_parse_tentatively (parser);
3748 scope = cp_parser_class_name (parser,
3749 typename_keyword_p,
3750 template_keyword_p,
3751 type_p ? class_type : none_type,
3752 check_dependency_p,
3753 /*class_head_p=*/false,
3754 is_declaration);
3755 /* If that didn't work, try for a namespace-name. */
3756 if (!only_class_p && !cp_parser_parse_definitely (parser))
3758 /* Restore the saved scope. */
3759 parser->scope = saved_scope;
3760 parser->qualifying_scope = saved_qualifying_scope;
3761 parser->object_scope = saved_object_scope;
3762 /* If we are not looking at an identifier followed by the scope
3763 resolution operator, then this is not part of a
3764 nested-name-specifier. (Note that this function is only used
3765 to parse the components of a nested-name-specifier.) */
3766 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3767 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3768 return error_mark_node;
3769 scope = cp_parser_namespace_name (parser);
3772 return scope;
3775 /* Parse a postfix-expression.
3777 postfix-expression:
3778 primary-expression
3779 postfix-expression [ expression ]
3780 postfix-expression ( expression-list [opt] )
3781 simple-type-specifier ( expression-list [opt] )
3782 typename :: [opt] nested-name-specifier identifier
3783 ( expression-list [opt] )
3784 typename :: [opt] nested-name-specifier template [opt] template-id
3785 ( expression-list [opt] )
3786 postfix-expression . template [opt] id-expression
3787 postfix-expression -> template [opt] id-expression
3788 postfix-expression . pseudo-destructor-name
3789 postfix-expression -> pseudo-destructor-name
3790 postfix-expression ++
3791 postfix-expression --
3792 dynamic_cast < type-id > ( expression )
3793 static_cast < type-id > ( expression )
3794 reinterpret_cast < type-id > ( expression )
3795 const_cast < type-id > ( expression )
3796 typeid ( expression )
3797 typeid ( type-id )
3799 GNU Extension:
3801 postfix-expression:
3802 ( type-id ) { initializer-list , [opt] }
3804 This extension is a GNU version of the C99 compound-literal
3805 construct. (The C99 grammar uses `type-name' instead of `type-id',
3806 but they are essentially the same concept.)
3808 If ADDRESS_P is true, the postfix expression is the operand of the
3809 `&' operator. CAST_P is true if this expression is the target of a
3810 cast.
3812 Returns a representation of the expression. */
3814 static tree
3815 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3817 cp_token *token;
3818 enum rid keyword;
3819 cp_id_kind idk = CP_ID_KIND_NONE;
3820 tree postfix_expression = NULL_TREE;
3821 /* Non-NULL only if the current postfix-expression can be used to
3822 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3823 class used to qualify the member. */
3824 tree qualifying_class = NULL_TREE;
3826 /* Peek at the next token. */
3827 token = cp_lexer_peek_token (parser->lexer);
3828 /* Some of the productions are determined by keywords. */
3829 keyword = token->keyword;
3830 switch (keyword)
3832 case RID_DYNCAST:
3833 case RID_STATCAST:
3834 case RID_REINTCAST:
3835 case RID_CONSTCAST:
3837 tree type;
3838 tree expression;
3839 const char *saved_message;
3841 /* All of these can be handled in the same way from the point
3842 of view of parsing. Begin by consuming the token
3843 identifying the cast. */
3844 cp_lexer_consume_token (parser->lexer);
3846 /* New types cannot be defined in the cast. */
3847 saved_message = parser->type_definition_forbidden_message;
3848 parser->type_definition_forbidden_message
3849 = "types may not be defined in casts";
3851 /* Look for the opening `<'. */
3852 cp_parser_require (parser, CPP_LESS, "`<'");
3853 /* Parse the type to which we are casting. */
3854 type = cp_parser_type_id (parser);
3855 /* Look for the closing `>'. */
3856 cp_parser_require (parser, CPP_GREATER, "`>'");
3857 /* Restore the old message. */
3858 parser->type_definition_forbidden_message = saved_message;
3860 /* And the expression which is being cast. */
3861 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3862 expression = cp_parser_expression (parser, /*cast_p=*/true);
3863 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3865 /* Only type conversions to integral or enumeration types
3866 can be used in constant-expressions. */
3867 if (parser->integral_constant_expression_p
3868 && !dependent_type_p (type)
3869 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3870 && (cp_parser_non_integral_constant_expression
3871 (parser,
3872 "a cast to a type other than an integral or "
3873 "enumeration type")))
3874 return error_mark_node;
3876 switch (keyword)
3878 case RID_DYNCAST:
3879 postfix_expression
3880 = build_dynamic_cast (type, expression);
3881 break;
3882 case RID_STATCAST:
3883 postfix_expression
3884 = build_static_cast (type, expression);
3885 break;
3886 case RID_REINTCAST:
3887 postfix_expression
3888 = build_reinterpret_cast (type, expression);
3889 break;
3890 case RID_CONSTCAST:
3891 postfix_expression
3892 = build_const_cast (type, expression);
3893 break;
3894 default:
3895 gcc_unreachable ();
3898 break;
3900 case RID_TYPEID:
3902 tree type;
3903 const char *saved_message;
3904 bool saved_in_type_id_in_expr_p;
3906 /* Consume the `typeid' token. */
3907 cp_lexer_consume_token (parser->lexer);
3908 /* Look for the `(' token. */
3909 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3910 /* Types cannot be defined in a `typeid' expression. */
3911 saved_message = parser->type_definition_forbidden_message;
3912 parser->type_definition_forbidden_message
3913 = "types may not be defined in a `typeid\' expression";
3914 /* We can't be sure yet whether we're looking at a type-id or an
3915 expression. */
3916 cp_parser_parse_tentatively (parser);
3917 /* Try a type-id first. */
3918 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3919 parser->in_type_id_in_expr_p = true;
3920 type = cp_parser_type_id (parser);
3921 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3922 /* Look for the `)' token. Otherwise, we can't be sure that
3923 we're not looking at an expression: consider `typeid (int
3924 (3))', for example. */
3925 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3926 /* If all went well, simply lookup the type-id. */
3927 if (cp_parser_parse_definitely (parser))
3928 postfix_expression = get_typeid (type);
3929 /* Otherwise, fall back to the expression variant. */
3930 else
3932 tree expression;
3934 /* Look for an expression. */
3935 expression = cp_parser_expression (parser, /*cast_p=*/false);
3936 /* Compute its typeid. */
3937 postfix_expression = build_typeid (expression);
3938 /* Look for the `)' token. */
3939 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3941 /* `typeid' may not appear in an integral constant expression. */
3942 if (cp_parser_non_integral_constant_expression(parser,
3943 "`typeid' operator"))
3944 return error_mark_node;
3945 /* Restore the saved message. */
3946 parser->type_definition_forbidden_message = saved_message;
3948 break;
3950 case RID_TYPENAME:
3952 bool template_p = false;
3953 tree id;
3954 tree type;
3955 tree scope;
3957 /* Consume the `typename' token. */
3958 cp_lexer_consume_token (parser->lexer);
3959 /* Look for the optional `::' operator. */
3960 cp_parser_global_scope_opt (parser,
3961 /*current_scope_valid_p=*/false);
3962 /* Look for the nested-name-specifier. In case of error here,
3963 consume the trailing id to avoid subsequent error messages
3964 for usual cases. */
3965 scope = cp_parser_nested_name_specifier (parser,
3966 /*typename_keyword_p=*/true,
3967 /*check_dependency_p=*/true,
3968 /*type_p=*/true,
3969 /*is_declaration=*/true);
3971 /* Look for the optional `template' keyword. */
3972 template_p = cp_parser_optional_template_keyword (parser);
3973 /* We don't know whether we're looking at a template-id or an
3974 identifier. */
3975 cp_parser_parse_tentatively (parser);
3976 /* Try a template-id. */
3977 id = cp_parser_template_id (parser, template_p,
3978 /*check_dependency_p=*/true,
3979 /*is_declaration=*/true);
3980 /* If that didn't work, try an identifier. */
3981 if (!cp_parser_parse_definitely (parser))
3982 id = cp_parser_identifier (parser);
3984 /* Don't process id if nested name specifier is invalid. */
3985 if (!scope || scope == error_mark_node)
3986 return error_mark_node;
3987 /* If we look up a template-id in a non-dependent qualifying
3988 scope, there's no need to create a dependent type. */
3989 if (TREE_CODE (id) == TYPE_DECL
3990 && (!TYPE_P (scope)
3991 || !dependent_type_p (parser->scope)))
3992 type = TREE_TYPE (id);
3993 /* Create a TYPENAME_TYPE to represent the type to which the
3994 functional cast is being performed. */
3995 else
3996 type = make_typename_type (parser->scope, id,
3997 typename_type,
3998 /*complain=*/1);
4000 postfix_expression = cp_parser_functional_cast (parser, type);
4002 break;
4004 default:
4006 tree type;
4008 /* If the next thing is a simple-type-specifier, we may be
4009 looking at a functional cast. We could also be looking at
4010 an id-expression. So, we try the functional cast, and if
4011 that doesn't work we fall back to the primary-expression. */
4012 cp_parser_parse_tentatively (parser);
4013 /* Look for the simple-type-specifier. */
4014 type = cp_parser_simple_type_specifier (parser,
4015 /*decl_specs=*/NULL,
4016 CP_PARSER_FLAGS_NONE);
4017 /* Parse the cast itself. */
4018 if (!cp_parser_error_occurred (parser))
4019 postfix_expression
4020 = cp_parser_functional_cast (parser, type);
4021 /* If that worked, we're done. */
4022 if (cp_parser_parse_definitely (parser))
4023 break;
4025 /* If the functional-cast didn't work out, try a
4026 compound-literal. */
4027 if (cp_parser_allow_gnu_extensions_p (parser)
4028 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4030 VEC(constructor_elt,gc) *initializer_list = NULL;
4031 bool saved_in_type_id_in_expr_p;
4033 cp_parser_parse_tentatively (parser);
4034 /* Consume the `('. */
4035 cp_lexer_consume_token (parser->lexer);
4036 /* Parse the type. */
4037 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4038 parser->in_type_id_in_expr_p = true;
4039 type = cp_parser_type_id (parser);
4040 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4041 /* Look for the `)'. */
4042 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4043 /* Look for the `{'. */
4044 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4045 /* If things aren't going well, there's no need to
4046 keep going. */
4047 if (!cp_parser_error_occurred (parser))
4049 bool non_constant_p;
4050 /* Parse the initializer-list. */
4051 initializer_list
4052 = cp_parser_initializer_list (parser, &non_constant_p);
4053 /* Allow a trailing `,'. */
4054 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4055 cp_lexer_consume_token (parser->lexer);
4056 /* Look for the final `}'. */
4057 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4059 /* If that worked, we're definitely looking at a
4060 compound-literal expression. */
4061 if (cp_parser_parse_definitely (parser))
4063 /* Warn the user that a compound literal is not
4064 allowed in standard C++. */
4065 if (pedantic)
4066 pedwarn ("ISO C++ forbids compound-literals");
4067 /* Form the representation of the compound-literal. */
4068 postfix_expression
4069 = finish_compound_literal (type, initializer_list);
4070 break;
4074 /* It must be a primary-expression. */
4075 postfix_expression = cp_parser_primary_expression (parser,
4076 cast_p,
4077 &idk,
4078 &qualifying_class);
4080 break;
4083 /* If we were avoiding committing to the processing of a
4084 qualified-id until we knew whether or not we had a
4085 pointer-to-member, we now know. */
4086 if (qualifying_class)
4088 bool done;
4090 /* Peek at the next token. */
4091 token = cp_lexer_peek_token (parser->lexer);
4092 done = (token->type != CPP_OPEN_SQUARE
4093 && token->type != CPP_OPEN_PAREN
4094 && token->type != CPP_DOT
4095 && token->type != CPP_DEREF
4096 && token->type != CPP_PLUS_PLUS
4097 && token->type != CPP_MINUS_MINUS);
4099 postfix_expression = finish_qualified_id_expr (qualifying_class,
4100 postfix_expression,
4101 done,
4102 address_p);
4103 if (done)
4104 return postfix_expression;
4107 /* Keep looping until the postfix-expression is complete. */
4108 while (true)
4110 if (idk == CP_ID_KIND_UNQUALIFIED
4111 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4112 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4113 /* It is not a Koenig lookup function call. */
4114 postfix_expression
4115 = unqualified_name_lookup_error (postfix_expression);
4117 /* Peek at the next token. */
4118 token = cp_lexer_peek_token (parser->lexer);
4120 switch (token->type)
4122 case CPP_OPEN_SQUARE:
4123 postfix_expression
4124 = cp_parser_postfix_open_square_expression (parser,
4125 postfix_expression,
4126 false);
4127 idk = CP_ID_KIND_NONE;
4128 break;
4130 case CPP_OPEN_PAREN:
4131 /* postfix-expression ( expression-list [opt] ) */
4133 bool koenig_p;
4134 bool is_builtin_constant_p;
4135 bool saved_integral_constant_expression_p = false;
4136 bool saved_non_integral_constant_expression_p = false;
4137 tree args;
4139 is_builtin_constant_p
4140 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4141 if (is_builtin_constant_p)
4143 /* The whole point of __builtin_constant_p is to allow
4144 non-constant expressions to appear as arguments. */
4145 saved_integral_constant_expression_p
4146 = parser->integral_constant_expression_p;
4147 saved_non_integral_constant_expression_p
4148 = parser->non_integral_constant_expression_p;
4149 parser->integral_constant_expression_p = false;
4151 args = (cp_parser_parenthesized_expression_list
4152 (parser, /*is_attribute_list=*/false,
4153 /*cast_p=*/false,
4154 /*non_constant_p=*/NULL));
4155 if (is_builtin_constant_p)
4157 parser->integral_constant_expression_p
4158 = saved_integral_constant_expression_p;
4159 parser->non_integral_constant_expression_p
4160 = saved_non_integral_constant_expression_p;
4163 if (args == error_mark_node)
4165 postfix_expression = error_mark_node;
4166 break;
4169 /* Function calls are not permitted in
4170 constant-expressions. */
4171 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4172 && cp_parser_non_integral_constant_expression (parser,
4173 "a function call"))
4175 postfix_expression = error_mark_node;
4176 break;
4179 koenig_p = false;
4180 if (idk == CP_ID_KIND_UNQUALIFIED)
4182 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4184 if (args)
4186 koenig_p = true;
4187 postfix_expression
4188 = perform_koenig_lookup (postfix_expression, args);
4190 else
4191 postfix_expression
4192 = unqualified_fn_lookup_error (postfix_expression);
4194 /* We do not perform argument-dependent lookup if
4195 normal lookup finds a non-function, in accordance
4196 with the expected resolution of DR 218. */
4197 else if (args && is_overloaded_fn (postfix_expression))
4199 tree fn = get_first_fn (postfix_expression);
4201 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4202 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4204 /* Only do argument dependent lookup if regular
4205 lookup does not find a set of member functions.
4206 [basic.lookup.koenig]/2a */
4207 if (!DECL_FUNCTION_MEMBER_P (fn))
4209 koenig_p = true;
4210 postfix_expression
4211 = perform_koenig_lookup (postfix_expression, args);
4216 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4218 tree instance = TREE_OPERAND (postfix_expression, 0);
4219 tree fn = TREE_OPERAND (postfix_expression, 1);
4221 if (processing_template_decl
4222 && (type_dependent_expression_p (instance)
4223 || (!BASELINK_P (fn)
4224 && TREE_CODE (fn) != FIELD_DECL)
4225 || type_dependent_expression_p (fn)
4226 || any_type_dependent_arguments_p (args)))
4228 postfix_expression
4229 = build_min_nt (CALL_EXPR, postfix_expression,
4230 args, NULL_TREE);
4231 break;
4234 if (BASELINK_P (fn))
4235 postfix_expression
4236 = (build_new_method_call
4237 (instance, fn, args, NULL_TREE,
4238 (idk == CP_ID_KIND_QUALIFIED
4239 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4240 else
4241 postfix_expression
4242 = finish_call_expr (postfix_expression, args,
4243 /*disallow_virtual=*/false,
4244 /*koenig_p=*/false);
4246 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4247 || TREE_CODE (postfix_expression) == MEMBER_REF
4248 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4249 postfix_expression = (build_offset_ref_call_from_tree
4250 (postfix_expression, args));
4251 else if (idk == CP_ID_KIND_QUALIFIED)
4252 /* A call to a static class member, or a namespace-scope
4253 function. */
4254 postfix_expression
4255 = finish_call_expr (postfix_expression, args,
4256 /*disallow_virtual=*/true,
4257 koenig_p);
4258 else
4259 /* All other function calls. */
4260 postfix_expression
4261 = finish_call_expr (postfix_expression, args,
4262 /*disallow_virtual=*/false,
4263 koenig_p);
4265 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4266 idk = CP_ID_KIND_NONE;
4268 break;
4270 case CPP_DOT:
4271 case CPP_DEREF:
4272 /* postfix-expression . template [opt] id-expression
4273 postfix-expression . pseudo-destructor-name
4274 postfix-expression -> template [opt] id-expression
4275 postfix-expression -> pseudo-destructor-name */
4277 /* Consume the `.' or `->' operator. */
4278 cp_lexer_consume_token (parser->lexer);
4280 postfix_expression
4281 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4282 postfix_expression,
4283 false, &idk);
4284 break;
4286 case CPP_PLUS_PLUS:
4287 /* postfix-expression ++ */
4288 /* Consume the `++' token. */
4289 cp_lexer_consume_token (parser->lexer);
4290 /* Generate a representation for the complete expression. */
4291 postfix_expression
4292 = finish_increment_expr (postfix_expression,
4293 POSTINCREMENT_EXPR);
4294 /* Increments may not appear in constant-expressions. */
4295 if (cp_parser_non_integral_constant_expression (parser,
4296 "an increment"))
4297 postfix_expression = error_mark_node;
4298 idk = CP_ID_KIND_NONE;
4299 break;
4301 case CPP_MINUS_MINUS:
4302 /* postfix-expression -- */
4303 /* Consume the `--' token. */
4304 cp_lexer_consume_token (parser->lexer);
4305 /* Generate a representation for the complete expression. */
4306 postfix_expression
4307 = finish_increment_expr (postfix_expression,
4308 POSTDECREMENT_EXPR);
4309 /* Decrements may not appear in constant-expressions. */
4310 if (cp_parser_non_integral_constant_expression (parser,
4311 "a decrement"))
4312 postfix_expression = error_mark_node;
4313 idk = CP_ID_KIND_NONE;
4314 break;
4316 default:
4317 return postfix_expression;
4321 /* We should never get here. */
4322 gcc_unreachable ();
4323 return error_mark_node;
4326 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4327 by cp_parser_builtin_offsetof. We're looking for
4329 postfix-expression [ expression ]
4331 FOR_OFFSETOF is set if we're being called in that context, which
4332 changes how we deal with integer constant expressions. */
4334 static tree
4335 cp_parser_postfix_open_square_expression (cp_parser *parser,
4336 tree postfix_expression,
4337 bool for_offsetof)
4339 tree index;
4341 /* Consume the `[' token. */
4342 cp_lexer_consume_token (parser->lexer);
4344 /* Parse the index expression. */
4345 /* ??? For offsetof, there is a question of what to allow here. If
4346 offsetof is not being used in an integral constant expression context,
4347 then we *could* get the right answer by computing the value at runtime.
4348 If we are in an integral constant expression context, then we might
4349 could accept any constant expression; hard to say without analysis.
4350 Rather than open the barn door too wide right away, allow only integer
4351 constant expressions here. */
4352 if (for_offsetof)
4353 index = cp_parser_constant_expression (parser, false, NULL);
4354 else
4355 index = cp_parser_expression (parser, /*cast_p=*/false);
4357 /* Look for the closing `]'. */
4358 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4360 /* Build the ARRAY_REF. */
4361 postfix_expression = grok_array_decl (postfix_expression, index);
4363 /* When not doing offsetof, array references are not permitted in
4364 constant-expressions. */
4365 if (!for_offsetof
4366 && (cp_parser_non_integral_constant_expression
4367 (parser, "an array reference")))
4368 postfix_expression = error_mark_node;
4370 return postfix_expression;
4373 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4374 by cp_parser_builtin_offsetof. We're looking for
4376 postfix-expression . template [opt] id-expression
4377 postfix-expression . pseudo-destructor-name
4378 postfix-expression -> template [opt] id-expression
4379 postfix-expression -> pseudo-destructor-name
4381 FOR_OFFSETOF is set if we're being called in that context. That sorta
4382 limits what of the above we'll actually accept, but nevermind.
4383 TOKEN_TYPE is the "." or "->" token, which will already have been
4384 removed from the stream. */
4386 static tree
4387 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4388 enum cpp_ttype token_type,
4389 tree postfix_expression,
4390 bool for_offsetof, cp_id_kind *idk)
4392 tree name;
4393 bool dependent_p;
4394 bool template_p;
4395 bool pseudo_destructor_p;
4396 tree scope = NULL_TREE;
4398 /* If this is a `->' operator, dereference the pointer. */
4399 if (token_type == CPP_DEREF)
4400 postfix_expression = build_x_arrow (postfix_expression);
4401 /* Check to see whether or not the expression is type-dependent. */
4402 dependent_p = type_dependent_expression_p (postfix_expression);
4403 /* The identifier following the `->' or `.' is not qualified. */
4404 parser->scope = NULL_TREE;
4405 parser->qualifying_scope = NULL_TREE;
4406 parser->object_scope = NULL_TREE;
4407 *idk = CP_ID_KIND_NONE;
4408 /* Enter the scope corresponding to the type of the object
4409 given by the POSTFIX_EXPRESSION. */
4410 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4412 scope = TREE_TYPE (postfix_expression);
4413 /* According to the standard, no expression should ever have
4414 reference type. Unfortunately, we do not currently match
4415 the standard in this respect in that our internal representation
4416 of an expression may have reference type even when the standard
4417 says it does not. Therefore, we have to manually obtain the
4418 underlying type here. */
4419 scope = non_reference (scope);
4420 /* The type of the POSTFIX_EXPRESSION must be complete. */
4421 scope = complete_type_or_else (scope, NULL_TREE);
4422 /* Let the name lookup machinery know that we are processing a
4423 class member access expression. */
4424 parser->context->object_type = scope;
4425 /* If something went wrong, we want to be able to discern that case,
4426 as opposed to the case where there was no SCOPE due to the type
4427 of expression being dependent. */
4428 if (!scope)
4429 scope = error_mark_node;
4430 /* If the SCOPE was erroneous, make the various semantic analysis
4431 functions exit quickly -- and without issuing additional error
4432 messages. */
4433 if (scope == error_mark_node)
4434 postfix_expression = error_mark_node;
4437 /* Assume this expression is not a pseudo-destructor access. */
4438 pseudo_destructor_p = false;
4440 /* If the SCOPE is a scalar type, then, if this is a valid program,
4441 we must be looking at a pseudo-destructor-name. */
4442 if (scope && SCALAR_TYPE_P (scope))
4444 tree s;
4445 tree type;
4447 cp_parser_parse_tentatively (parser);
4448 /* Parse the pseudo-destructor-name. */
4449 s = NULL_TREE;
4450 cp_parser_pseudo_destructor_name (parser, &s, &type);
4451 if (cp_parser_parse_definitely (parser))
4453 pseudo_destructor_p = true;
4454 postfix_expression
4455 = finish_pseudo_destructor_expr (postfix_expression,
4456 s, TREE_TYPE (type));
4460 if (!pseudo_destructor_p)
4462 /* If the SCOPE is not a scalar type, we are looking at an
4463 ordinary class member access expression, rather than a
4464 pseudo-destructor-name. */
4465 template_p = cp_parser_optional_template_keyword (parser);
4466 /* Parse the id-expression. */
4467 name = cp_parser_id_expression (parser, template_p,
4468 /*check_dependency_p=*/true,
4469 /*template_p=*/NULL,
4470 /*declarator_p=*/false);
4471 /* In general, build a SCOPE_REF if the member name is qualified.
4472 However, if the name was not dependent and has already been
4473 resolved; there is no need to build the SCOPE_REF. For example;
4475 struct X { void f(); };
4476 template <typename T> void f(T* t) { t->X::f(); }
4478 Even though "t" is dependent, "X::f" is not and has been resolved
4479 to a BASELINK; there is no need to include scope information. */
4481 /* But we do need to remember that there was an explicit scope for
4482 virtual function calls. */
4483 if (parser->scope)
4484 *idk = CP_ID_KIND_QUALIFIED;
4486 /* If the name is a template-id that names a type, we will get a
4487 TYPE_DECL here. That is invalid code. */
4488 if (TREE_CODE (name) == TYPE_DECL)
4490 error ("invalid use of %qD", name);
4491 postfix_expression = error_mark_node;
4493 else
4495 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4497 name = build_nt (SCOPE_REF, parser->scope, name);
4498 parser->scope = NULL_TREE;
4499 parser->qualifying_scope = NULL_TREE;
4500 parser->object_scope = NULL_TREE;
4502 if (scope && name && BASELINK_P (name))
4503 adjust_result_of_qualified_name_lookup
4504 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4505 postfix_expression
4506 = finish_class_member_access_expr (postfix_expression, name);
4510 /* We no longer need to look up names in the scope of the object on
4511 the left-hand side of the `.' or `->' operator. */
4512 parser->context->object_type = NULL_TREE;
4514 /* Outside of offsetof, these operators may not appear in
4515 constant-expressions. */
4516 if (!for_offsetof
4517 && (cp_parser_non_integral_constant_expression
4518 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4519 postfix_expression = error_mark_node;
4521 return postfix_expression;
4524 /* Parse a parenthesized expression-list.
4526 expression-list:
4527 assignment-expression
4528 expression-list, assignment-expression
4530 attribute-list:
4531 expression-list
4532 identifier
4533 identifier, expression-list
4535 CAST_P is true if this expression is the target of a cast.
4537 Returns a TREE_LIST. The TREE_VALUE of each node is a
4538 representation of an assignment-expression. Note that a TREE_LIST
4539 is returned even if there is only a single expression in the list.
4540 error_mark_node is returned if the ( and or ) are
4541 missing. NULL_TREE is returned on no expressions. The parentheses
4542 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4543 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4544 indicates whether or not all of the expressions in the list were
4545 constant. */
4547 static tree
4548 cp_parser_parenthesized_expression_list (cp_parser* parser,
4549 bool is_attribute_list,
4550 bool cast_p,
4551 bool *non_constant_p)
4553 tree expression_list = NULL_TREE;
4554 bool fold_expr_p = is_attribute_list;
4555 tree identifier = NULL_TREE;
4557 /* Assume all the expressions will be constant. */
4558 if (non_constant_p)
4559 *non_constant_p = false;
4561 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4562 return error_mark_node;
4564 /* Consume expressions until there are no more. */
4565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4566 while (true)
4568 tree expr;
4570 /* At the beginning of attribute lists, check to see if the
4571 next token is an identifier. */
4572 if (is_attribute_list
4573 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4575 cp_token *token;
4577 /* Consume the identifier. */
4578 token = cp_lexer_consume_token (parser->lexer);
4579 /* Save the identifier. */
4580 identifier = token->value;
4582 else
4584 /* Parse the next assignment-expression. */
4585 if (non_constant_p)
4587 bool expr_non_constant_p;
4588 expr = (cp_parser_constant_expression
4589 (parser, /*allow_non_constant_p=*/true,
4590 &expr_non_constant_p));
4591 if (expr_non_constant_p)
4592 *non_constant_p = true;
4594 else
4595 expr = cp_parser_assignment_expression (parser, cast_p);
4597 if (fold_expr_p)
4598 expr = fold_non_dependent_expr (expr);
4600 /* Add it to the list. We add error_mark_node
4601 expressions to the list, so that we can still tell if
4602 the correct form for a parenthesized expression-list
4603 is found. That gives better errors. */
4604 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4606 if (expr == error_mark_node)
4607 goto skip_comma;
4610 /* After the first item, attribute lists look the same as
4611 expression lists. */
4612 is_attribute_list = false;
4614 get_comma:;
4615 /* If the next token isn't a `,', then we are done. */
4616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4617 break;
4619 /* Otherwise, consume the `,' and keep going. */
4620 cp_lexer_consume_token (parser->lexer);
4623 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4625 int ending;
4627 skip_comma:;
4628 /* We try and resync to an unnested comma, as that will give the
4629 user better diagnostics. */
4630 ending = cp_parser_skip_to_closing_parenthesis (parser,
4631 /*recovering=*/true,
4632 /*or_comma=*/true,
4633 /*consume_paren=*/true);
4634 if (ending < 0)
4635 goto get_comma;
4636 if (!ending)
4637 return error_mark_node;
4640 /* We built up the list in reverse order so we must reverse it now. */
4641 expression_list = nreverse (expression_list);
4642 if (identifier)
4643 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4645 return expression_list;
4648 /* Parse a pseudo-destructor-name.
4650 pseudo-destructor-name:
4651 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4652 :: [opt] nested-name-specifier template template-id :: ~ type-name
4653 :: [opt] nested-name-specifier [opt] ~ type-name
4655 If either of the first two productions is used, sets *SCOPE to the
4656 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4657 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4658 or ERROR_MARK_NODE if the parse fails. */
4660 static void
4661 cp_parser_pseudo_destructor_name (cp_parser* parser,
4662 tree* scope,
4663 tree* type)
4665 bool nested_name_specifier_p;
4667 /* Assume that things will not work out. */
4668 *type = error_mark_node;
4670 /* Look for the optional `::' operator. */
4671 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4672 /* Look for the optional nested-name-specifier. */
4673 nested_name_specifier_p
4674 = (cp_parser_nested_name_specifier_opt (parser,
4675 /*typename_keyword_p=*/false,
4676 /*check_dependency_p=*/true,
4677 /*type_p=*/false,
4678 /*is_declaration=*/true)
4679 != NULL_TREE);
4680 /* Now, if we saw a nested-name-specifier, we might be doing the
4681 second production. */
4682 if (nested_name_specifier_p
4683 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4685 /* Consume the `template' keyword. */
4686 cp_lexer_consume_token (parser->lexer);
4687 /* Parse the template-id. */
4688 cp_parser_template_id (parser,
4689 /*template_keyword_p=*/true,
4690 /*check_dependency_p=*/false,
4691 /*is_declaration=*/true);
4692 /* Look for the `::' token. */
4693 cp_parser_require (parser, CPP_SCOPE, "`::'");
4695 /* If the next token is not a `~', then there might be some
4696 additional qualification. */
4697 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4699 /* Look for the type-name. */
4700 *scope = TREE_TYPE (cp_parser_type_name (parser));
4702 if (*scope == error_mark_node)
4703 return;
4705 /* If we don't have ::~, then something has gone wrong. Since
4706 the only caller of this function is looking for something
4707 after `.' or `->' after a scalar type, most likely the
4708 program is trying to get a member of a non-aggregate
4709 type. */
4710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4711 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4713 cp_parser_error (parser, "request for member of non-aggregate type");
4714 return;
4717 /* Look for the `::' token. */
4718 cp_parser_require (parser, CPP_SCOPE, "`::'");
4720 else
4721 *scope = NULL_TREE;
4723 /* Look for the `~'. */
4724 cp_parser_require (parser, CPP_COMPL, "`~'");
4725 /* Look for the type-name again. We are not responsible for
4726 checking that it matches the first type-name. */
4727 *type = cp_parser_type_name (parser);
4730 /* Parse a unary-expression.
4732 unary-expression:
4733 postfix-expression
4734 ++ cast-expression
4735 -- cast-expression
4736 unary-operator cast-expression
4737 sizeof unary-expression
4738 sizeof ( type-id )
4739 new-expression
4740 delete-expression
4742 GNU Extensions:
4744 unary-expression:
4745 __extension__ cast-expression
4746 __alignof__ unary-expression
4747 __alignof__ ( type-id )
4748 __real__ cast-expression
4749 __imag__ cast-expression
4750 && identifier
4752 ADDRESS_P is true iff the unary-expression is appearing as the
4753 operand of the `&' operator. CAST_P is true if this expression is
4754 the target of a cast.
4756 Returns a representation of the expression. */
4758 static tree
4759 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4761 cp_token *token;
4762 enum tree_code unary_operator;
4764 /* Peek at the next token. */
4765 token = cp_lexer_peek_token (parser->lexer);
4766 /* Some keywords give away the kind of expression. */
4767 if (token->type == CPP_KEYWORD)
4769 enum rid keyword = token->keyword;
4771 switch (keyword)
4773 case RID_ALIGNOF:
4774 case RID_SIZEOF:
4776 tree operand;
4777 enum tree_code op;
4779 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4780 /* Consume the token. */
4781 cp_lexer_consume_token (parser->lexer);
4782 /* Parse the operand. */
4783 operand = cp_parser_sizeof_operand (parser, keyword);
4785 if (TYPE_P (operand))
4786 return cxx_sizeof_or_alignof_type (operand, op, true);
4787 else
4788 return cxx_sizeof_or_alignof_expr (operand, op);
4791 case RID_NEW:
4792 return cp_parser_new_expression (parser);
4794 case RID_DELETE:
4795 return cp_parser_delete_expression (parser);
4797 case RID_EXTENSION:
4799 /* The saved value of the PEDANTIC flag. */
4800 int saved_pedantic;
4801 tree expr;
4803 /* Save away the PEDANTIC flag. */
4804 cp_parser_extension_opt (parser, &saved_pedantic);
4805 /* Parse the cast-expression. */
4806 expr = cp_parser_simple_cast_expression (parser);
4807 /* Restore the PEDANTIC flag. */
4808 pedantic = saved_pedantic;
4810 return expr;
4813 case RID_REALPART:
4814 case RID_IMAGPART:
4816 tree expression;
4818 /* Consume the `__real__' or `__imag__' token. */
4819 cp_lexer_consume_token (parser->lexer);
4820 /* Parse the cast-expression. */
4821 expression = cp_parser_simple_cast_expression (parser);
4822 /* Create the complete representation. */
4823 return build_x_unary_op ((keyword == RID_REALPART
4824 ? REALPART_EXPR : IMAGPART_EXPR),
4825 expression);
4827 break;
4829 default:
4830 break;
4834 /* Look for the `:: new' and `:: delete', which also signal the
4835 beginning of a new-expression, or delete-expression,
4836 respectively. If the next token is `::', then it might be one of
4837 these. */
4838 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4840 enum rid keyword;
4842 /* See if the token after the `::' is one of the keywords in
4843 which we're interested. */
4844 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4845 /* If it's `new', we have a new-expression. */
4846 if (keyword == RID_NEW)
4847 return cp_parser_new_expression (parser);
4848 /* Similarly, for `delete'. */
4849 else if (keyword == RID_DELETE)
4850 return cp_parser_delete_expression (parser);
4853 /* Look for a unary operator. */
4854 unary_operator = cp_parser_unary_operator (token);
4855 /* The `++' and `--' operators can be handled similarly, even though
4856 they are not technically unary-operators in the grammar. */
4857 if (unary_operator == ERROR_MARK)
4859 if (token->type == CPP_PLUS_PLUS)
4860 unary_operator = PREINCREMENT_EXPR;
4861 else if (token->type == CPP_MINUS_MINUS)
4862 unary_operator = PREDECREMENT_EXPR;
4863 /* Handle the GNU address-of-label extension. */
4864 else if (cp_parser_allow_gnu_extensions_p (parser)
4865 && token->type == CPP_AND_AND)
4867 tree identifier;
4869 /* Consume the '&&' token. */
4870 cp_lexer_consume_token (parser->lexer);
4871 /* Look for the identifier. */
4872 identifier = cp_parser_identifier (parser);
4873 /* Create an expression representing the address. */
4874 return finish_label_address_expr (identifier);
4877 if (unary_operator != ERROR_MARK)
4879 tree cast_expression;
4880 tree expression = error_mark_node;
4881 const char *non_constant_p = NULL;
4883 /* Consume the operator token. */
4884 token = cp_lexer_consume_token (parser->lexer);
4885 /* Parse the cast-expression. */
4886 cast_expression
4887 = cp_parser_cast_expression (parser,
4888 unary_operator == ADDR_EXPR,
4889 /*cast_p=*/false);
4890 /* Now, build an appropriate representation. */
4891 switch (unary_operator)
4893 case INDIRECT_REF:
4894 non_constant_p = "`*'";
4895 expression = build_x_indirect_ref (cast_expression, "unary *");
4896 break;
4898 case ADDR_EXPR:
4899 non_constant_p = "`&'";
4900 /* Fall through. */
4901 case BIT_NOT_EXPR:
4902 expression = build_x_unary_op (unary_operator, cast_expression);
4903 break;
4905 case PREINCREMENT_EXPR:
4906 case PREDECREMENT_EXPR:
4907 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4908 ? "`++'" : "`--'");
4909 /* Fall through. */
4910 case UNARY_PLUS_EXPR:
4911 case NEGATE_EXPR:
4912 case TRUTH_NOT_EXPR:
4913 expression = finish_unary_op_expr (unary_operator, cast_expression);
4914 break;
4916 default:
4917 gcc_unreachable ();
4920 if (non_constant_p
4921 && cp_parser_non_integral_constant_expression (parser,
4922 non_constant_p))
4923 expression = error_mark_node;
4925 return expression;
4928 return cp_parser_postfix_expression (parser, address_p, cast_p);
4931 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4932 unary-operator, the corresponding tree code is returned. */
4934 static enum tree_code
4935 cp_parser_unary_operator (cp_token* token)
4937 switch (token->type)
4939 case CPP_MULT:
4940 return INDIRECT_REF;
4942 case CPP_AND:
4943 return ADDR_EXPR;
4945 case CPP_PLUS:
4946 return UNARY_PLUS_EXPR;
4948 case CPP_MINUS:
4949 return NEGATE_EXPR;
4951 case CPP_NOT:
4952 return TRUTH_NOT_EXPR;
4954 case CPP_COMPL:
4955 return BIT_NOT_EXPR;
4957 default:
4958 return ERROR_MARK;
4962 /* Parse a new-expression.
4964 new-expression:
4965 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4966 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4968 Returns a representation of the expression. */
4970 static tree
4971 cp_parser_new_expression (cp_parser* parser)
4973 bool global_scope_p;
4974 tree placement;
4975 tree type;
4976 tree initializer;
4977 tree nelts;
4979 /* Look for the optional `::' operator. */
4980 global_scope_p
4981 = (cp_parser_global_scope_opt (parser,
4982 /*current_scope_valid_p=*/false)
4983 != NULL_TREE);
4984 /* Look for the `new' operator. */
4985 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4986 /* There's no easy way to tell a new-placement from the
4987 `( type-id )' construct. */
4988 cp_parser_parse_tentatively (parser);
4989 /* Look for a new-placement. */
4990 placement = cp_parser_new_placement (parser);
4991 /* If that didn't work out, there's no new-placement. */
4992 if (!cp_parser_parse_definitely (parser))
4993 placement = NULL_TREE;
4995 /* If the next token is a `(', then we have a parenthesized
4996 type-id. */
4997 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4999 /* Consume the `('. */
5000 cp_lexer_consume_token (parser->lexer);
5001 /* Parse the type-id. */
5002 type = cp_parser_type_id (parser);
5003 /* Look for the closing `)'. */
5004 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5005 /* There should not be a direct-new-declarator in this production,
5006 but GCC used to allowed this, so we check and emit a sensible error
5007 message for this case. */
5008 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5010 error ("array bound forbidden after parenthesized type-id");
5011 inform ("try removing the parentheses around the type-id");
5012 cp_parser_direct_new_declarator (parser);
5014 nelts = NULL_TREE;
5016 /* Otherwise, there must be a new-type-id. */
5017 else
5018 type = cp_parser_new_type_id (parser, &nelts);
5020 /* If the next token is a `(', then we have a new-initializer. */
5021 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5022 initializer = cp_parser_new_initializer (parser);
5023 else
5024 initializer = NULL_TREE;
5026 /* A new-expression may not appear in an integral constant
5027 expression. */
5028 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5029 return error_mark_node;
5031 /* Create a representation of the new-expression. */
5032 return build_new (placement, type, nelts, initializer, global_scope_p);
5035 /* Parse a new-placement.
5037 new-placement:
5038 ( expression-list )
5040 Returns the same representation as for an expression-list. */
5042 static tree
5043 cp_parser_new_placement (cp_parser* parser)
5045 tree expression_list;
5047 /* Parse the expression-list. */
5048 expression_list = (cp_parser_parenthesized_expression_list
5049 (parser, false, /*cast_p=*/false,
5050 /*non_constant_p=*/NULL));
5052 return expression_list;
5055 /* Parse a new-type-id.
5057 new-type-id:
5058 type-specifier-seq new-declarator [opt]
5060 Returns the TYPE allocated. If the new-type-id indicates an array
5061 type, *NELTS is set to the number of elements in the last array
5062 bound; the TYPE will not include the last array bound. */
5064 static tree
5065 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5067 cp_decl_specifier_seq type_specifier_seq;
5068 cp_declarator *new_declarator;
5069 cp_declarator *declarator;
5070 cp_declarator *outer_declarator;
5071 const char *saved_message;
5072 tree type;
5074 /* The type-specifier sequence must not contain type definitions.
5075 (It cannot contain declarations of new types either, but if they
5076 are not definitions we will catch that because they are not
5077 complete.) */
5078 saved_message = parser->type_definition_forbidden_message;
5079 parser->type_definition_forbidden_message
5080 = "types may not be defined in a new-type-id";
5081 /* Parse the type-specifier-seq. */
5082 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5083 &type_specifier_seq);
5084 /* Restore the old message. */
5085 parser->type_definition_forbidden_message = saved_message;
5086 /* Parse the new-declarator. */
5087 new_declarator = cp_parser_new_declarator_opt (parser);
5089 /* Determine the number of elements in the last array dimension, if
5090 any. */
5091 *nelts = NULL_TREE;
5092 /* Skip down to the last array dimension. */
5093 declarator = new_declarator;
5094 outer_declarator = NULL;
5095 while (declarator && (declarator->kind == cdk_pointer
5096 || declarator->kind == cdk_ptrmem))
5098 outer_declarator = declarator;
5099 declarator = declarator->declarator;
5101 while (declarator
5102 && declarator->kind == cdk_array
5103 && declarator->declarator
5104 && declarator->declarator->kind == cdk_array)
5106 outer_declarator = declarator;
5107 declarator = declarator->declarator;
5110 if (declarator && declarator->kind == cdk_array)
5112 *nelts = declarator->u.array.bounds;
5113 if (*nelts == error_mark_node)
5114 *nelts = integer_one_node;
5116 if (outer_declarator)
5117 outer_declarator->declarator = declarator->declarator;
5118 else
5119 new_declarator = NULL;
5122 type = groktypename (&type_specifier_seq, new_declarator);
5123 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5125 *nelts = array_type_nelts_top (type);
5126 type = TREE_TYPE (type);
5128 return type;
5131 /* Parse an (optional) new-declarator.
5133 new-declarator:
5134 ptr-operator new-declarator [opt]
5135 direct-new-declarator
5137 Returns the declarator. */
5139 static cp_declarator *
5140 cp_parser_new_declarator_opt (cp_parser* parser)
5142 enum tree_code code;
5143 tree type;
5144 cp_cv_quals cv_quals;
5146 /* We don't know if there's a ptr-operator next, or not. */
5147 cp_parser_parse_tentatively (parser);
5148 /* Look for a ptr-operator. */
5149 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5150 /* If that worked, look for more new-declarators. */
5151 if (cp_parser_parse_definitely (parser))
5153 cp_declarator *declarator;
5155 /* Parse another optional declarator. */
5156 declarator = cp_parser_new_declarator_opt (parser);
5158 /* Create the representation of the declarator. */
5159 if (type)
5160 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5161 else if (code == INDIRECT_REF)
5162 declarator = make_pointer_declarator (cv_quals, declarator);
5163 else
5164 declarator = make_reference_declarator (cv_quals, declarator);
5166 return declarator;
5169 /* If the next token is a `[', there is a direct-new-declarator. */
5170 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5171 return cp_parser_direct_new_declarator (parser);
5173 return NULL;
5176 /* Parse a direct-new-declarator.
5178 direct-new-declarator:
5179 [ expression ]
5180 direct-new-declarator [constant-expression]
5184 static cp_declarator *
5185 cp_parser_direct_new_declarator (cp_parser* parser)
5187 cp_declarator *declarator = NULL;
5189 while (true)
5191 tree expression;
5193 /* Look for the opening `['. */
5194 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5195 /* The first expression is not required to be constant. */
5196 if (!declarator)
5198 expression = cp_parser_expression (parser, /*cast_p=*/false);
5199 /* The standard requires that the expression have integral
5200 type. DR 74 adds enumeration types. We believe that the
5201 real intent is that these expressions be handled like the
5202 expression in a `switch' condition, which also allows
5203 classes with a single conversion to integral or
5204 enumeration type. */
5205 if (!processing_template_decl)
5207 expression
5208 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5209 expression,
5210 /*complain=*/true);
5211 if (!expression)
5213 error ("expression in new-declarator must have integral "
5214 "or enumeration type");
5215 expression = error_mark_node;
5219 /* But all the other expressions must be. */
5220 else
5221 expression
5222 = cp_parser_constant_expression (parser,
5223 /*allow_non_constant=*/false,
5224 NULL);
5225 /* Look for the closing `]'. */
5226 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5228 /* Add this bound to the declarator. */
5229 declarator = make_array_declarator (declarator, expression);
5231 /* If the next token is not a `[', then there are no more
5232 bounds. */
5233 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5234 break;
5237 return declarator;
5240 /* Parse a new-initializer.
5242 new-initializer:
5243 ( expression-list [opt] )
5245 Returns a representation of the expression-list. If there is no
5246 expression-list, VOID_ZERO_NODE is returned. */
5248 static tree
5249 cp_parser_new_initializer (cp_parser* parser)
5251 tree expression_list;
5253 expression_list = (cp_parser_parenthesized_expression_list
5254 (parser, false, /*cast_p=*/false,
5255 /*non_constant_p=*/NULL));
5256 if (!expression_list)
5257 expression_list = void_zero_node;
5259 return expression_list;
5262 /* Parse a delete-expression.
5264 delete-expression:
5265 :: [opt] delete cast-expression
5266 :: [opt] delete [ ] cast-expression
5268 Returns a representation of the expression. */
5270 static tree
5271 cp_parser_delete_expression (cp_parser* parser)
5273 bool global_scope_p;
5274 bool array_p;
5275 tree expression;
5277 /* Look for the optional `::' operator. */
5278 global_scope_p
5279 = (cp_parser_global_scope_opt (parser,
5280 /*current_scope_valid_p=*/false)
5281 != NULL_TREE);
5282 /* Look for the `delete' keyword. */
5283 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5284 /* See if the array syntax is in use. */
5285 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5287 /* Consume the `[' token. */
5288 cp_lexer_consume_token (parser->lexer);
5289 /* Look for the `]' token. */
5290 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5291 /* Remember that this is the `[]' construct. */
5292 array_p = true;
5294 else
5295 array_p = false;
5297 /* Parse the cast-expression. */
5298 expression = cp_parser_simple_cast_expression (parser);
5300 /* A delete-expression may not appear in an integral constant
5301 expression. */
5302 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5303 return error_mark_node;
5305 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5308 /* Parse a cast-expression.
5310 cast-expression:
5311 unary-expression
5312 ( type-id ) cast-expression
5314 ADDRESS_P is true iff the unary-expression is appearing as the
5315 operand of the `&' operator. CAST_P is true if this expression is
5316 the target of a cast.
5318 Returns a representation of the expression. */
5320 static tree
5321 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5323 /* If it's a `(', then we might be looking at a cast. */
5324 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5326 tree type = NULL_TREE;
5327 tree expr = NULL_TREE;
5328 bool compound_literal_p;
5329 const char *saved_message;
5331 /* There's no way to know yet whether or not this is a cast.
5332 For example, `(int (3))' is a unary-expression, while `(int)
5333 3' is a cast. So, we resort to parsing tentatively. */
5334 cp_parser_parse_tentatively (parser);
5335 /* Types may not be defined in a cast. */
5336 saved_message = parser->type_definition_forbidden_message;
5337 parser->type_definition_forbidden_message
5338 = "types may not be defined in casts";
5339 /* Consume the `('. */
5340 cp_lexer_consume_token (parser->lexer);
5341 /* A very tricky bit is that `(struct S) { 3 }' is a
5342 compound-literal (which we permit in C++ as an extension).
5343 But, that construct is not a cast-expression -- it is a
5344 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5345 is legal; if the compound-literal were a cast-expression,
5346 you'd need an extra set of parentheses.) But, if we parse
5347 the type-id, and it happens to be a class-specifier, then we
5348 will commit to the parse at that point, because we cannot
5349 undo the action that is done when creating a new class. So,
5350 then we cannot back up and do a postfix-expression.
5352 Therefore, we scan ahead to the closing `)', and check to see
5353 if the token after the `)' is a `{'. If so, we are not
5354 looking at a cast-expression.
5356 Save tokens so that we can put them back. */
5357 cp_lexer_save_tokens (parser->lexer);
5358 /* Skip tokens until the next token is a closing parenthesis.
5359 If we find the closing `)', and the next token is a `{', then
5360 we are looking at a compound-literal. */
5361 compound_literal_p
5362 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5363 /*consume_paren=*/true)
5364 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5365 /* Roll back the tokens we skipped. */
5366 cp_lexer_rollback_tokens (parser->lexer);
5367 /* If we were looking at a compound-literal, simulate an error
5368 so that the call to cp_parser_parse_definitely below will
5369 fail. */
5370 if (compound_literal_p)
5371 cp_parser_simulate_error (parser);
5372 else
5374 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5375 parser->in_type_id_in_expr_p = true;
5376 /* Look for the type-id. */
5377 type = cp_parser_type_id (parser);
5378 /* Look for the closing `)'. */
5379 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5380 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5383 /* Restore the saved message. */
5384 parser->type_definition_forbidden_message = saved_message;
5386 /* If ok so far, parse the dependent expression. We cannot be
5387 sure it is a cast. Consider `(T ())'. It is a parenthesized
5388 ctor of T, but looks like a cast to function returning T
5389 without a dependent expression. */
5390 if (!cp_parser_error_occurred (parser))
5391 expr = cp_parser_cast_expression (parser,
5392 /*address_p=*/false,
5393 /*cast_p=*/true);
5395 if (cp_parser_parse_definitely (parser))
5397 /* Warn about old-style casts, if so requested. */
5398 if (warn_old_style_cast
5399 && !in_system_header
5400 && !VOID_TYPE_P (type)
5401 && current_lang_name != lang_name_c)
5402 warning (0, "use of old-style cast");
5404 /* Only type conversions to integral or enumeration types
5405 can be used in constant-expressions. */
5406 if (parser->integral_constant_expression_p
5407 && !dependent_type_p (type)
5408 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5409 && (cp_parser_non_integral_constant_expression
5410 (parser,
5411 "a cast to a type other than an integral or "
5412 "enumeration type")))
5413 return error_mark_node;
5415 /* Perform the cast. */
5416 expr = build_c_cast (type, expr);
5417 return expr;
5421 /* If we get here, then it's not a cast, so it must be a
5422 unary-expression. */
5423 return cp_parser_unary_expression (parser, address_p, cast_p);
5426 /* Parse a binary expression of the general form:
5428 pm-expression:
5429 cast-expression
5430 pm-expression .* cast-expression
5431 pm-expression ->* cast-expression
5433 multiplicative-expression:
5434 pm-expression
5435 multiplicative-expression * pm-expression
5436 multiplicative-expression / pm-expression
5437 multiplicative-expression % pm-expression
5439 additive-expression:
5440 multiplicative-expression
5441 additive-expression + multiplicative-expression
5442 additive-expression - multiplicative-expression
5444 shift-expression:
5445 additive-expression
5446 shift-expression << additive-expression
5447 shift-expression >> additive-expression
5449 relational-expression:
5450 shift-expression
5451 relational-expression < shift-expression
5452 relational-expression > shift-expression
5453 relational-expression <= shift-expression
5454 relational-expression >= shift-expression
5456 GNU Extension:
5458 relational-expression:
5459 relational-expression <? shift-expression
5460 relational-expression >? shift-expression
5462 equality-expression:
5463 relational-expression
5464 equality-expression == relational-expression
5465 equality-expression != relational-expression
5467 and-expression:
5468 equality-expression
5469 and-expression & equality-expression
5471 exclusive-or-expression:
5472 and-expression
5473 exclusive-or-expression ^ and-expression
5475 inclusive-or-expression:
5476 exclusive-or-expression
5477 inclusive-or-expression | exclusive-or-expression
5479 logical-and-expression:
5480 inclusive-or-expression
5481 logical-and-expression && inclusive-or-expression
5483 logical-or-expression:
5484 logical-and-expression
5485 logical-or-expression || logical-and-expression
5487 All these are implemented with a single function like:
5489 binary-expression:
5490 simple-cast-expression
5491 binary-expression <token> binary-expression
5493 CAST_P is true if this expression is the target of a cast.
5495 The binops_by_token map is used to get the tree codes for each <token> type.
5496 binary-expressions are associated according to a precedence table. */
5498 #define TOKEN_PRECEDENCE(token) \
5499 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5500 ? PREC_NOT_OPERATOR \
5501 : binops_by_token[token->type].prec)
5503 static tree
5504 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5506 cp_parser_expression_stack stack;
5507 cp_parser_expression_stack_entry *sp = &stack[0];
5508 tree lhs, rhs;
5509 cp_token *token;
5510 enum tree_code tree_type;
5511 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5512 bool overloaded_p;
5514 /* Parse the first expression. */
5515 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5517 for (;;)
5519 /* Get an operator token. */
5520 token = cp_lexer_peek_token (parser->lexer);
5521 if (token->type == CPP_MIN || token->type == CPP_MAX)
5522 cp_parser_warn_min_max ();
5524 new_prec = TOKEN_PRECEDENCE (token);
5526 /* Popping an entry off the stack means we completed a subexpression:
5527 - either we found a token which is not an operator (`>' where it is not
5528 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5529 will happen repeatedly;
5530 - or, we found an operator which has lower priority. This is the case
5531 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5532 parsing `3 * 4'. */
5533 if (new_prec <= prec)
5535 if (sp == stack)
5536 break;
5537 else
5538 goto pop;
5541 get_rhs:
5542 tree_type = binops_by_token[token->type].tree_type;
5544 /* We used the operator token. */
5545 cp_lexer_consume_token (parser->lexer);
5547 /* Extract another operand. It may be the RHS of this expression
5548 or the LHS of a new, higher priority expression. */
5549 rhs = cp_parser_simple_cast_expression (parser);
5551 /* Get another operator token. Look up its precedence to avoid
5552 building a useless (immediately popped) stack entry for common
5553 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5554 token = cp_lexer_peek_token (parser->lexer);
5555 lookahead_prec = TOKEN_PRECEDENCE (token);
5556 if (lookahead_prec > new_prec)
5558 /* ... and prepare to parse the RHS of the new, higher priority
5559 expression. Since precedence levels on the stack are
5560 monotonically increasing, we do not have to care about
5561 stack overflows. */
5562 sp->prec = prec;
5563 sp->tree_type = tree_type;
5564 sp->lhs = lhs;
5565 sp++;
5566 lhs = rhs;
5567 prec = new_prec;
5568 new_prec = lookahead_prec;
5569 goto get_rhs;
5571 pop:
5572 /* If the stack is not empty, we have parsed into LHS the right side
5573 (`4' in the example above) of an expression we had suspended.
5574 We can use the information on the stack to recover the LHS (`3')
5575 from the stack together with the tree code (`MULT_EXPR'), and
5576 the precedence of the higher level subexpression
5577 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5578 which will be used to actually build the additive expression. */
5579 --sp;
5580 prec = sp->prec;
5581 tree_type = sp->tree_type;
5582 rhs = lhs;
5583 lhs = sp->lhs;
5586 overloaded_p = false;
5587 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5589 /* If the binary operator required the use of an overloaded operator,
5590 then this expression cannot be an integral constant-expression.
5591 An overloaded operator can be used even if both operands are
5592 otherwise permissible in an integral constant-expression if at
5593 least one of the operands is of enumeration type. */
5595 if (overloaded_p
5596 && (cp_parser_non_integral_constant_expression
5597 (parser, "calls to overloaded operators")))
5598 return error_mark_node;
5601 return lhs;
5605 /* Parse the `? expression : assignment-expression' part of a
5606 conditional-expression. The LOGICAL_OR_EXPR is the
5607 logical-or-expression that started the conditional-expression.
5608 Returns a representation of the entire conditional-expression.
5610 This routine is used by cp_parser_assignment_expression.
5612 ? expression : assignment-expression
5614 GNU Extensions:
5616 ? : assignment-expression */
5618 static tree
5619 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5621 tree expr;
5622 tree assignment_expr;
5624 /* Consume the `?' token. */
5625 cp_lexer_consume_token (parser->lexer);
5626 if (cp_parser_allow_gnu_extensions_p (parser)
5627 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5628 /* Implicit true clause. */
5629 expr = NULL_TREE;
5630 else
5631 /* Parse the expression. */
5632 expr = cp_parser_expression (parser, /*cast_p=*/false);
5634 /* The next token should be a `:'. */
5635 cp_parser_require (parser, CPP_COLON, "`:'");
5636 /* Parse the assignment-expression. */
5637 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5639 /* Build the conditional-expression. */
5640 return build_x_conditional_expr (logical_or_expr,
5641 expr,
5642 assignment_expr);
5645 /* Parse an assignment-expression.
5647 assignment-expression:
5648 conditional-expression
5649 logical-or-expression assignment-operator assignment_expression
5650 throw-expression
5652 CAST_P is true if this expression is the target of a cast.
5654 Returns a representation for the expression. */
5656 static tree
5657 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5659 tree expr;
5661 /* If the next token is the `throw' keyword, then we're looking at
5662 a throw-expression. */
5663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5664 expr = cp_parser_throw_expression (parser);
5665 /* Otherwise, it must be that we are looking at a
5666 logical-or-expression. */
5667 else
5669 /* Parse the binary expressions (logical-or-expression). */
5670 expr = cp_parser_binary_expression (parser, cast_p);
5671 /* If the next token is a `?' then we're actually looking at a
5672 conditional-expression. */
5673 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5674 return cp_parser_question_colon_clause (parser, expr);
5675 else
5677 enum tree_code assignment_operator;
5679 /* If it's an assignment-operator, we're using the second
5680 production. */
5681 assignment_operator
5682 = cp_parser_assignment_operator_opt (parser);
5683 if (assignment_operator != ERROR_MARK)
5685 tree rhs;
5687 /* Parse the right-hand side of the assignment. */
5688 rhs = cp_parser_assignment_expression (parser, cast_p);
5689 /* An assignment may not appear in a
5690 constant-expression. */
5691 if (cp_parser_non_integral_constant_expression (parser,
5692 "an assignment"))
5693 return error_mark_node;
5694 /* Build the assignment expression. */
5695 expr = build_x_modify_expr (expr,
5696 assignment_operator,
5697 rhs);
5702 return expr;
5705 /* Parse an (optional) assignment-operator.
5707 assignment-operator: one of
5708 = *= /= %= += -= >>= <<= &= ^= |=
5710 GNU Extension:
5712 assignment-operator: one of
5713 <?= >?=
5715 If the next token is an assignment operator, the corresponding tree
5716 code is returned, and the token is consumed. For example, for
5717 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5718 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5719 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5720 operator, ERROR_MARK is returned. */
5722 static enum tree_code
5723 cp_parser_assignment_operator_opt (cp_parser* parser)
5725 enum tree_code op;
5726 cp_token *token;
5728 /* Peek at the next toen. */
5729 token = cp_lexer_peek_token (parser->lexer);
5731 switch (token->type)
5733 case CPP_EQ:
5734 op = NOP_EXPR;
5735 break;
5737 case CPP_MULT_EQ:
5738 op = MULT_EXPR;
5739 break;
5741 case CPP_DIV_EQ:
5742 op = TRUNC_DIV_EXPR;
5743 break;
5745 case CPP_MOD_EQ:
5746 op = TRUNC_MOD_EXPR;
5747 break;
5749 case CPP_PLUS_EQ:
5750 op = PLUS_EXPR;
5751 break;
5753 case CPP_MINUS_EQ:
5754 op = MINUS_EXPR;
5755 break;
5757 case CPP_RSHIFT_EQ:
5758 op = RSHIFT_EXPR;
5759 break;
5761 case CPP_LSHIFT_EQ:
5762 op = LSHIFT_EXPR;
5763 break;
5765 case CPP_AND_EQ:
5766 op = BIT_AND_EXPR;
5767 break;
5769 case CPP_XOR_EQ:
5770 op = BIT_XOR_EXPR;
5771 break;
5773 case CPP_OR_EQ:
5774 op = BIT_IOR_EXPR;
5775 break;
5777 case CPP_MIN_EQ:
5778 op = MIN_EXPR;
5779 cp_parser_warn_min_max ();
5780 break;
5782 case CPP_MAX_EQ:
5783 op = MAX_EXPR;
5784 cp_parser_warn_min_max ();
5785 break;
5787 default:
5788 /* Nothing else is an assignment operator. */
5789 op = ERROR_MARK;
5792 /* If it was an assignment operator, consume it. */
5793 if (op != ERROR_MARK)
5794 cp_lexer_consume_token (parser->lexer);
5796 return op;
5799 /* Parse an expression.
5801 expression:
5802 assignment-expression
5803 expression , assignment-expression
5805 CAST_P is true if this expression is the target of a cast.
5807 Returns a representation of the expression. */
5809 static tree
5810 cp_parser_expression (cp_parser* parser, bool cast_p)
5812 tree expression = NULL_TREE;
5814 while (true)
5816 tree assignment_expression;
5818 /* Parse the next assignment-expression. */
5819 assignment_expression
5820 = cp_parser_assignment_expression (parser, cast_p);
5821 /* If this is the first assignment-expression, we can just
5822 save it away. */
5823 if (!expression)
5824 expression = assignment_expression;
5825 else
5826 expression = build_x_compound_expr (expression,
5827 assignment_expression);
5828 /* If the next token is not a comma, then we are done with the
5829 expression. */
5830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5831 break;
5832 /* Consume the `,'. */
5833 cp_lexer_consume_token (parser->lexer);
5834 /* A comma operator cannot appear in a constant-expression. */
5835 if (cp_parser_non_integral_constant_expression (parser,
5836 "a comma operator"))
5837 expression = error_mark_node;
5840 return expression;
5843 /* Parse a constant-expression.
5845 constant-expression:
5846 conditional-expression
5848 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5849 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5850 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5851 is false, NON_CONSTANT_P should be NULL. */
5853 static tree
5854 cp_parser_constant_expression (cp_parser* parser,
5855 bool allow_non_constant_p,
5856 bool *non_constant_p)
5858 bool saved_integral_constant_expression_p;
5859 bool saved_allow_non_integral_constant_expression_p;
5860 bool saved_non_integral_constant_expression_p;
5861 tree expression;
5863 /* It might seem that we could simply parse the
5864 conditional-expression, and then check to see if it were
5865 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5866 one that the compiler can figure out is constant, possibly after
5867 doing some simplifications or optimizations. The standard has a
5868 precise definition of constant-expression, and we must honor
5869 that, even though it is somewhat more restrictive.
5871 For example:
5873 int i[(2, 3)];
5875 is not a legal declaration, because `(2, 3)' is not a
5876 constant-expression. The `,' operator is forbidden in a
5877 constant-expression. However, GCC's constant-folding machinery
5878 will fold this operation to an INTEGER_CST for `3'. */
5880 /* Save the old settings. */
5881 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5882 saved_allow_non_integral_constant_expression_p
5883 = parser->allow_non_integral_constant_expression_p;
5884 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5885 /* We are now parsing a constant-expression. */
5886 parser->integral_constant_expression_p = true;
5887 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5888 parser->non_integral_constant_expression_p = false;
5889 /* Although the grammar says "conditional-expression", we parse an
5890 "assignment-expression", which also permits "throw-expression"
5891 and the use of assignment operators. In the case that
5892 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5893 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5894 actually essential that we look for an assignment-expression.
5895 For example, cp_parser_initializer_clauses uses this function to
5896 determine whether a particular assignment-expression is in fact
5897 constant. */
5898 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5899 /* Restore the old settings. */
5900 parser->integral_constant_expression_p
5901 = saved_integral_constant_expression_p;
5902 parser->allow_non_integral_constant_expression_p
5903 = saved_allow_non_integral_constant_expression_p;
5904 if (allow_non_constant_p)
5905 *non_constant_p = parser->non_integral_constant_expression_p;
5906 else if (parser->non_integral_constant_expression_p)
5907 expression = error_mark_node;
5908 parser->non_integral_constant_expression_p
5909 = saved_non_integral_constant_expression_p;
5911 return expression;
5914 /* Parse __builtin_offsetof.
5916 offsetof-expression:
5917 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5919 offsetof-member-designator:
5920 id-expression
5921 | offsetof-member-designator "." id-expression
5922 | offsetof-member-designator "[" expression "]"
5925 static tree
5926 cp_parser_builtin_offsetof (cp_parser *parser)
5928 int save_ice_p, save_non_ice_p;
5929 tree type, expr;
5930 cp_id_kind dummy;
5932 /* We're about to accept non-integral-constant things, but will
5933 definitely yield an integral constant expression. Save and
5934 restore these values around our local parsing. */
5935 save_ice_p = parser->integral_constant_expression_p;
5936 save_non_ice_p = parser->non_integral_constant_expression_p;
5938 /* Consume the "__builtin_offsetof" token. */
5939 cp_lexer_consume_token (parser->lexer);
5940 /* Consume the opening `('. */
5941 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5942 /* Parse the type-id. */
5943 type = cp_parser_type_id (parser);
5944 /* Look for the `,'. */
5945 cp_parser_require (parser, CPP_COMMA, "`,'");
5947 /* Build the (type *)null that begins the traditional offsetof macro. */
5948 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5950 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5951 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5952 true, &dummy);
5953 while (true)
5955 cp_token *token = cp_lexer_peek_token (parser->lexer);
5956 switch (token->type)
5958 case CPP_OPEN_SQUARE:
5959 /* offsetof-member-designator "[" expression "]" */
5960 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5961 break;
5963 case CPP_DOT:
5964 /* offsetof-member-designator "." identifier */
5965 cp_lexer_consume_token (parser->lexer);
5966 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5967 true, &dummy);
5968 break;
5970 case CPP_CLOSE_PAREN:
5971 /* Consume the ")" token. */
5972 cp_lexer_consume_token (parser->lexer);
5973 goto success;
5975 default:
5976 /* Error. We know the following require will fail, but
5977 that gives the proper error message. */
5978 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5979 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5980 expr = error_mark_node;
5981 goto failure;
5985 success:
5986 /* If we're processing a template, we can't finish the semantics yet.
5987 Otherwise we can fold the entire expression now. */
5988 if (processing_template_decl)
5989 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5990 else
5991 expr = fold_offsetof (expr);
5993 failure:
5994 parser->integral_constant_expression_p = save_ice_p;
5995 parser->non_integral_constant_expression_p = save_non_ice_p;
5997 return expr;
6000 /* Statements [gram.stmt.stmt] */
6002 /* Parse a statement.
6004 statement:
6005 labeled-statement
6006 expression-statement
6007 compound-statement
6008 selection-statement
6009 iteration-statement
6010 jump-statement
6011 declaration-statement
6012 try-block */
6014 static void
6015 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6017 tree statement;
6018 cp_token *token;
6019 location_t statement_location;
6021 /* There is no statement yet. */
6022 statement = NULL_TREE;
6023 /* Peek at the next token. */
6024 token = cp_lexer_peek_token (parser->lexer);
6025 /* Remember the location of the first token in the statement. */
6026 statement_location = token->location;
6027 /* If this is a keyword, then that will often determine what kind of
6028 statement we have. */
6029 if (token->type == CPP_KEYWORD)
6031 enum rid keyword = token->keyword;
6033 switch (keyword)
6035 case RID_CASE:
6036 case RID_DEFAULT:
6037 statement = cp_parser_labeled_statement (parser,
6038 in_statement_expr);
6039 break;
6041 case RID_IF:
6042 case RID_SWITCH:
6043 statement = cp_parser_selection_statement (parser);
6044 break;
6046 case RID_WHILE:
6047 case RID_DO:
6048 case RID_FOR:
6049 statement = cp_parser_iteration_statement (parser);
6050 break;
6052 case RID_BREAK:
6053 case RID_CONTINUE:
6054 case RID_RETURN:
6055 case RID_GOTO:
6056 statement = cp_parser_jump_statement (parser);
6057 break;
6059 /* Objective-C++ exception-handling constructs. */
6060 case RID_AT_TRY:
6061 case RID_AT_CATCH:
6062 case RID_AT_FINALLY:
6063 case RID_AT_SYNCHRONIZED:
6064 case RID_AT_THROW:
6065 statement = cp_parser_objc_statement (parser);
6066 break;
6068 case RID_TRY:
6069 statement = cp_parser_try_block (parser);
6070 break;
6072 default:
6073 /* It might be a keyword like `int' that can start a
6074 declaration-statement. */
6075 break;
6078 else if (token->type == CPP_NAME)
6080 /* If the next token is a `:', then we are looking at a
6081 labeled-statement. */
6082 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6083 if (token->type == CPP_COLON)
6084 statement = cp_parser_labeled_statement (parser, in_statement_expr);
6086 /* Anything that starts with a `{' must be a compound-statement. */
6087 else if (token->type == CPP_OPEN_BRACE)
6088 statement = cp_parser_compound_statement (parser, NULL, false);
6089 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6090 a statement all its own. */
6091 else if (token->type == CPP_PRAGMA)
6093 cp_lexer_handle_pragma (parser->lexer);
6094 return;
6097 /* Everything else must be a declaration-statement or an
6098 expression-statement. Try for the declaration-statement
6099 first, unless we are looking at a `;', in which case we know that
6100 we have an expression-statement. */
6101 if (!statement)
6103 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6105 cp_parser_parse_tentatively (parser);
6106 /* Try to parse the declaration-statement. */
6107 cp_parser_declaration_statement (parser);
6108 /* If that worked, we're done. */
6109 if (cp_parser_parse_definitely (parser))
6110 return;
6112 /* Look for an expression-statement instead. */
6113 statement = cp_parser_expression_statement (parser, in_statement_expr);
6116 /* Set the line number for the statement. */
6117 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6118 SET_EXPR_LOCATION (statement, statement_location);
6121 /* Parse a labeled-statement.
6123 labeled-statement:
6124 identifier : statement
6125 case constant-expression : statement
6126 default : statement
6128 GNU Extension:
6130 labeled-statement:
6131 case constant-expression ... constant-expression : statement
6133 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6134 For an ordinary label, returns a LABEL_EXPR. */
6136 static tree
6137 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6139 cp_token *token;
6140 tree statement = error_mark_node;
6142 /* The next token should be an identifier. */
6143 token = cp_lexer_peek_token (parser->lexer);
6144 if (token->type != CPP_NAME
6145 && token->type != CPP_KEYWORD)
6147 cp_parser_error (parser, "expected labeled-statement");
6148 return error_mark_node;
6151 switch (token->keyword)
6153 case RID_CASE:
6155 tree expr, expr_hi;
6156 cp_token *ellipsis;
6158 /* Consume the `case' token. */
6159 cp_lexer_consume_token (parser->lexer);
6160 /* Parse the constant-expression. */
6161 expr = cp_parser_constant_expression (parser,
6162 /*allow_non_constant_p=*/false,
6163 NULL);
6165 ellipsis = cp_lexer_peek_token (parser->lexer);
6166 if (ellipsis->type == CPP_ELLIPSIS)
6168 /* Consume the `...' token. */
6169 cp_lexer_consume_token (parser->lexer);
6170 expr_hi =
6171 cp_parser_constant_expression (parser,
6172 /*allow_non_constant_p=*/false,
6173 NULL);
6174 /* We don't need to emit warnings here, as the common code
6175 will do this for us. */
6177 else
6178 expr_hi = NULL_TREE;
6180 if (!parser->in_switch_statement_p)
6181 error ("case label %qE not within a switch statement", expr);
6182 else
6183 statement = finish_case_label (expr, expr_hi);
6185 break;
6187 case RID_DEFAULT:
6188 /* Consume the `default' token. */
6189 cp_lexer_consume_token (parser->lexer);
6190 if (!parser->in_switch_statement_p)
6191 error ("case label not within a switch statement");
6192 else
6193 statement = finish_case_label (NULL_TREE, NULL_TREE);
6194 break;
6196 default:
6197 /* Anything else must be an ordinary label. */
6198 statement = finish_label_stmt (cp_parser_identifier (parser));
6199 break;
6202 /* Require the `:' token. */
6203 cp_parser_require (parser, CPP_COLON, "`:'");
6204 /* Parse the labeled statement. */
6205 cp_parser_statement (parser, in_statement_expr);
6207 /* Return the label, in the case of a `case' or `default' label. */
6208 return statement;
6211 /* Parse an expression-statement.
6213 expression-statement:
6214 expression [opt] ;
6216 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6217 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6218 indicates whether this expression-statement is part of an
6219 expression statement. */
6221 static tree
6222 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6224 tree statement = NULL_TREE;
6226 /* If the next token is a ';', then there is no expression
6227 statement. */
6228 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6229 statement = cp_parser_expression (parser, /*cast_p=*/false);
6231 /* Consume the final `;'. */
6232 cp_parser_consume_semicolon_at_end_of_statement (parser);
6234 if (in_statement_expr
6235 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6236 /* This is the final expression statement of a statement
6237 expression. */
6238 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6239 else if (statement)
6240 statement = finish_expr_stmt (statement);
6241 else
6242 finish_stmt ();
6244 return statement;
6247 /* Parse a compound-statement.
6249 compound-statement:
6250 { statement-seq [opt] }
6252 Returns a tree representing the statement. */
6254 static tree
6255 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6256 bool in_try)
6258 tree compound_stmt;
6260 /* Consume the `{'. */
6261 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6262 return error_mark_node;
6263 /* Begin the compound-statement. */
6264 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6265 /* Parse an (optional) statement-seq. */
6266 cp_parser_statement_seq_opt (parser, in_statement_expr);
6267 /* Finish the compound-statement. */
6268 finish_compound_stmt (compound_stmt);
6269 /* Consume the `}'. */
6270 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6272 return compound_stmt;
6275 /* Parse an (optional) statement-seq.
6277 statement-seq:
6278 statement
6279 statement-seq [opt] statement */
6281 static void
6282 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6284 /* Scan statements until there aren't any more. */
6285 while (true)
6287 /* If we're looking at a `}', then we've run out of statements. */
6288 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6289 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6290 break;
6292 /* Parse the statement. */
6293 cp_parser_statement (parser, in_statement_expr);
6297 /* Parse a selection-statement.
6299 selection-statement:
6300 if ( condition ) statement
6301 if ( condition ) statement else statement
6302 switch ( condition ) statement
6304 Returns the new IF_STMT or SWITCH_STMT. */
6306 static tree
6307 cp_parser_selection_statement (cp_parser* parser)
6309 cp_token *token;
6310 enum rid keyword;
6312 /* Peek at the next token. */
6313 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6315 /* See what kind of keyword it is. */
6316 keyword = token->keyword;
6317 switch (keyword)
6319 case RID_IF:
6320 case RID_SWITCH:
6322 tree statement;
6323 tree condition;
6325 /* Look for the `('. */
6326 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6328 cp_parser_skip_to_end_of_statement (parser);
6329 return error_mark_node;
6332 /* Begin the selection-statement. */
6333 if (keyword == RID_IF)
6334 statement = begin_if_stmt ();
6335 else
6336 statement = begin_switch_stmt ();
6338 /* Parse the condition. */
6339 condition = cp_parser_condition (parser);
6340 /* Look for the `)'. */
6341 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6342 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6343 /*consume_paren=*/true);
6345 if (keyword == RID_IF)
6347 /* Add the condition. */
6348 finish_if_stmt_cond (condition, statement);
6350 /* Parse the then-clause. */
6351 cp_parser_implicitly_scoped_statement (parser);
6352 finish_then_clause (statement);
6354 /* If the next token is `else', parse the else-clause. */
6355 if (cp_lexer_next_token_is_keyword (parser->lexer,
6356 RID_ELSE))
6358 /* Consume the `else' keyword. */
6359 cp_lexer_consume_token (parser->lexer);
6360 begin_else_clause (statement);
6361 /* Parse the else-clause. */
6362 cp_parser_implicitly_scoped_statement (parser);
6363 finish_else_clause (statement);
6366 /* Now we're all done with the if-statement. */
6367 finish_if_stmt (statement);
6369 else
6371 bool in_switch_statement_p;
6373 /* Add the condition. */
6374 finish_switch_cond (condition, statement);
6376 /* Parse the body of the switch-statement. */
6377 in_switch_statement_p = parser->in_switch_statement_p;
6378 parser->in_switch_statement_p = true;
6379 cp_parser_implicitly_scoped_statement (parser);
6380 parser->in_switch_statement_p = in_switch_statement_p;
6382 /* Now we're all done with the switch-statement. */
6383 finish_switch_stmt (statement);
6386 return statement;
6388 break;
6390 default:
6391 cp_parser_error (parser, "expected selection-statement");
6392 return error_mark_node;
6396 /* Parse a condition.
6398 condition:
6399 expression
6400 type-specifier-seq declarator = assignment-expression
6402 GNU Extension:
6404 condition:
6405 type-specifier-seq declarator asm-specification [opt]
6406 attributes [opt] = assignment-expression
6408 Returns the expression that should be tested. */
6410 static tree
6411 cp_parser_condition (cp_parser* parser)
6413 cp_decl_specifier_seq type_specifiers;
6414 const char *saved_message;
6416 /* Try the declaration first. */
6417 cp_parser_parse_tentatively (parser);
6418 /* New types are not allowed in the type-specifier-seq for a
6419 condition. */
6420 saved_message = parser->type_definition_forbidden_message;
6421 parser->type_definition_forbidden_message
6422 = "types may not be defined in conditions";
6423 /* Parse the type-specifier-seq. */
6424 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6425 &type_specifiers);
6426 /* Restore the saved message. */
6427 parser->type_definition_forbidden_message = saved_message;
6428 /* If all is well, we might be looking at a declaration. */
6429 if (!cp_parser_error_occurred (parser))
6431 tree decl;
6432 tree asm_specification;
6433 tree attributes;
6434 cp_declarator *declarator;
6435 tree initializer = NULL_TREE;
6437 /* Parse the declarator. */
6438 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6439 /*ctor_dtor_or_conv_p=*/NULL,
6440 /*parenthesized_p=*/NULL,
6441 /*member_p=*/false);
6442 /* Parse the attributes. */
6443 attributes = cp_parser_attributes_opt (parser);
6444 /* Parse the asm-specification. */
6445 asm_specification = cp_parser_asm_specification_opt (parser);
6446 /* If the next token is not an `=', then we might still be
6447 looking at an expression. For example:
6449 if (A(a).x)
6451 looks like a decl-specifier-seq and a declarator -- but then
6452 there is no `=', so this is an expression. */
6453 cp_parser_require (parser, CPP_EQ, "`='");
6454 /* If we did see an `=', then we are looking at a declaration
6455 for sure. */
6456 if (cp_parser_parse_definitely (parser))
6458 tree pushed_scope;
6460 /* Create the declaration. */
6461 decl = start_decl (declarator, &type_specifiers,
6462 /*initialized_p=*/true,
6463 attributes, /*prefix_attributes=*/NULL_TREE,
6464 &pushed_scope);
6465 /* Parse the assignment-expression. */
6466 initializer = cp_parser_assignment_expression (parser,
6467 /*cast_p=*/false);
6469 /* Process the initializer. */
6470 cp_finish_decl (decl,
6471 initializer,
6472 asm_specification,
6473 LOOKUP_ONLYCONVERTING);
6475 if (pushed_scope)
6476 pop_scope (pushed_scope);
6478 return convert_from_reference (decl);
6481 /* If we didn't even get past the declarator successfully, we are
6482 definitely not looking at a declaration. */
6483 else
6484 cp_parser_abort_tentative_parse (parser);
6486 /* Otherwise, we are looking at an expression. */
6487 return cp_parser_expression (parser, /*cast_p=*/false);
6490 /* Parse an iteration-statement.
6492 iteration-statement:
6493 while ( condition ) statement
6494 do statement while ( expression ) ;
6495 for ( for-init-statement condition [opt] ; expression [opt] )
6496 statement
6498 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6500 static tree
6501 cp_parser_iteration_statement (cp_parser* parser)
6503 cp_token *token;
6504 enum rid keyword;
6505 tree statement;
6506 bool in_iteration_statement_p;
6509 /* Peek at the next token. */
6510 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6511 if (!token)
6512 return error_mark_node;
6514 /* Remember whether or not we are already within an iteration
6515 statement. */
6516 in_iteration_statement_p = parser->in_iteration_statement_p;
6518 /* See what kind of keyword it is. */
6519 keyword = token->keyword;
6520 switch (keyword)
6522 case RID_WHILE:
6524 tree condition;
6526 /* Begin the while-statement. */
6527 statement = begin_while_stmt ();
6528 /* Look for the `('. */
6529 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6530 /* Parse the condition. */
6531 condition = cp_parser_condition (parser);
6532 finish_while_stmt_cond (condition, statement);
6533 /* Look for the `)'. */
6534 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6535 /* Parse the dependent statement. */
6536 parser->in_iteration_statement_p = true;
6537 cp_parser_already_scoped_statement (parser);
6538 parser->in_iteration_statement_p = in_iteration_statement_p;
6539 /* We're done with the while-statement. */
6540 finish_while_stmt (statement);
6542 break;
6544 case RID_DO:
6546 tree expression;
6548 /* Begin the do-statement. */
6549 statement = begin_do_stmt ();
6550 /* Parse the body of the do-statement. */
6551 parser->in_iteration_statement_p = true;
6552 cp_parser_implicitly_scoped_statement (parser);
6553 parser->in_iteration_statement_p = in_iteration_statement_p;
6554 finish_do_body (statement);
6555 /* Look for the `while' keyword. */
6556 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6557 /* Look for the `('. */
6558 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6559 /* Parse the expression. */
6560 expression = cp_parser_expression (parser, /*cast_p=*/false);
6561 /* We're done with the do-statement. */
6562 finish_do_stmt (expression, statement);
6563 /* Look for the `)'. */
6564 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6565 /* Look for the `;'. */
6566 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6568 break;
6570 case RID_FOR:
6572 tree condition = NULL_TREE;
6573 tree expression = NULL_TREE;
6575 /* Begin the for-statement. */
6576 statement = begin_for_stmt ();
6577 /* Look for the `('. */
6578 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6579 /* Parse the initialization. */
6580 cp_parser_for_init_statement (parser);
6581 finish_for_init_stmt (statement);
6583 /* If there's a condition, process it. */
6584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6585 condition = cp_parser_condition (parser);
6586 finish_for_cond (condition, statement);
6587 /* Look for the `;'. */
6588 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6590 /* If there's an expression, process it. */
6591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6592 expression = cp_parser_expression (parser, /*cast_p=*/false);
6593 finish_for_expr (expression, statement);
6594 /* Look for the `)'. */
6595 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6597 /* Parse the body of the for-statement. */
6598 parser->in_iteration_statement_p = true;
6599 cp_parser_already_scoped_statement (parser);
6600 parser->in_iteration_statement_p = in_iteration_statement_p;
6602 /* We're done with the for-statement. */
6603 finish_for_stmt (statement);
6605 break;
6607 default:
6608 cp_parser_error (parser, "expected iteration-statement");
6609 statement = error_mark_node;
6610 break;
6613 return statement;
6616 /* Parse a for-init-statement.
6618 for-init-statement:
6619 expression-statement
6620 simple-declaration */
6622 static void
6623 cp_parser_for_init_statement (cp_parser* parser)
6625 /* If the next token is a `;', then we have an empty
6626 expression-statement. Grammatically, this is also a
6627 simple-declaration, but an invalid one, because it does not
6628 declare anything. Therefore, if we did not handle this case
6629 specially, we would issue an error message about an invalid
6630 declaration. */
6631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6633 /* We're going to speculatively look for a declaration, falling back
6634 to an expression, if necessary. */
6635 cp_parser_parse_tentatively (parser);
6636 /* Parse the declaration. */
6637 cp_parser_simple_declaration (parser,
6638 /*function_definition_allowed_p=*/false);
6639 /* If the tentative parse failed, then we shall need to look for an
6640 expression-statement. */
6641 if (cp_parser_parse_definitely (parser))
6642 return;
6645 cp_parser_expression_statement (parser, false);
6648 /* Parse a jump-statement.
6650 jump-statement:
6651 break ;
6652 continue ;
6653 return expression [opt] ;
6654 goto identifier ;
6656 GNU extension:
6658 jump-statement:
6659 goto * expression ;
6661 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6663 static tree
6664 cp_parser_jump_statement (cp_parser* parser)
6666 tree statement = error_mark_node;
6667 cp_token *token;
6668 enum rid keyword;
6670 /* Peek at the next token. */
6671 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6672 if (!token)
6673 return error_mark_node;
6675 /* See what kind of keyword it is. */
6676 keyword = token->keyword;
6677 switch (keyword)
6679 case RID_BREAK:
6680 if (!parser->in_switch_statement_p
6681 && !parser->in_iteration_statement_p)
6683 error ("break statement not within loop or switch");
6684 statement = error_mark_node;
6686 else
6687 statement = finish_break_stmt ();
6688 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6689 break;
6691 case RID_CONTINUE:
6692 if (!parser->in_iteration_statement_p)
6694 error ("continue statement not within a loop");
6695 statement = error_mark_node;
6697 else
6698 statement = finish_continue_stmt ();
6699 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6700 break;
6702 case RID_RETURN:
6704 tree expr;
6706 /* If the next token is a `;', then there is no
6707 expression. */
6708 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6709 expr = cp_parser_expression (parser, /*cast_p=*/false);
6710 else
6711 expr = NULL_TREE;
6712 /* Build the return-statement. */
6713 statement = finish_return_stmt (expr);
6714 /* Look for the final `;'. */
6715 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6717 break;
6719 case RID_GOTO:
6720 /* Create the goto-statement. */
6721 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6723 /* Issue a warning about this use of a GNU extension. */
6724 if (pedantic)
6725 pedwarn ("ISO C++ forbids computed gotos");
6726 /* Consume the '*' token. */
6727 cp_lexer_consume_token (parser->lexer);
6728 /* Parse the dependent expression. */
6729 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6731 else
6732 finish_goto_stmt (cp_parser_identifier (parser));
6733 /* Look for the final `;'. */
6734 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6735 break;
6737 default:
6738 cp_parser_error (parser, "expected jump-statement");
6739 break;
6742 return statement;
6745 /* Parse a declaration-statement.
6747 declaration-statement:
6748 block-declaration */
6750 static void
6751 cp_parser_declaration_statement (cp_parser* parser)
6753 void *p;
6755 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6756 p = obstack_alloc (&declarator_obstack, 0);
6758 /* Parse the block-declaration. */
6759 cp_parser_block_declaration (parser, /*statement_p=*/true);
6761 /* Free any declarators allocated. */
6762 obstack_free (&declarator_obstack, p);
6764 /* Finish off the statement. */
6765 finish_stmt ();
6768 /* Some dependent statements (like `if (cond) statement'), are
6769 implicitly in their own scope. In other words, if the statement is
6770 a single statement (as opposed to a compound-statement), it is
6771 none-the-less treated as if it were enclosed in braces. Any
6772 declarations appearing in the dependent statement are out of scope
6773 after control passes that point. This function parses a statement,
6774 but ensures that is in its own scope, even if it is not a
6775 compound-statement.
6777 Returns the new statement. */
6779 static tree
6780 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6782 tree statement;
6784 /* If the token is not a `{', then we must take special action. */
6785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6787 /* Create a compound-statement. */
6788 statement = begin_compound_stmt (0);
6789 /* Parse the dependent-statement. */
6790 cp_parser_statement (parser, false);
6791 /* Finish the dummy compound-statement. */
6792 finish_compound_stmt (statement);
6794 /* Otherwise, we simply parse the statement directly. */
6795 else
6796 statement = cp_parser_compound_statement (parser, NULL, false);
6798 /* Return the statement. */
6799 return statement;
6802 /* For some dependent statements (like `while (cond) statement'), we
6803 have already created a scope. Therefore, even if the dependent
6804 statement is a compound-statement, we do not want to create another
6805 scope. */
6807 static void
6808 cp_parser_already_scoped_statement (cp_parser* parser)
6810 /* If the token is a `{', then we must take special action. */
6811 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6812 cp_parser_statement (parser, false);
6813 else
6815 /* Avoid calling cp_parser_compound_statement, so that we
6816 don't create a new scope. Do everything else by hand. */
6817 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6818 cp_parser_statement_seq_opt (parser, false);
6819 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6823 /* Declarations [gram.dcl.dcl] */
6825 /* Parse an optional declaration-sequence.
6827 declaration-seq:
6828 declaration
6829 declaration-seq declaration */
6831 static void
6832 cp_parser_declaration_seq_opt (cp_parser* parser)
6834 while (true)
6836 cp_token *token;
6838 token = cp_lexer_peek_token (parser->lexer);
6840 if (token->type == CPP_CLOSE_BRACE
6841 || token->type == CPP_EOF)
6842 break;
6844 if (token->type == CPP_SEMICOLON)
6846 /* A declaration consisting of a single semicolon is
6847 invalid. Allow it unless we're being pedantic. */
6848 cp_lexer_consume_token (parser->lexer);
6849 if (pedantic && !in_system_header)
6850 pedwarn ("extra %<;%>");
6851 continue;
6854 /* If we're entering or exiting a region that's implicitly
6855 extern "C", modify the lang context appropriately. */
6856 if (!parser->implicit_extern_c && token->implicit_extern_c)
6858 push_lang_context (lang_name_c);
6859 parser->implicit_extern_c = true;
6861 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6863 pop_lang_context ();
6864 parser->implicit_extern_c = false;
6867 if (token->type == CPP_PRAGMA)
6869 /* A top-level declaration can consist solely of a #pragma.
6870 A nested declaration cannot, so this is done here and not
6871 in cp_parser_declaration. (A #pragma at block scope is
6872 handled in cp_parser_statement.) */
6873 cp_lexer_handle_pragma (parser->lexer);
6874 continue;
6877 /* Parse the declaration itself. */
6878 cp_parser_declaration (parser);
6882 /* Parse a declaration.
6884 declaration:
6885 block-declaration
6886 function-definition
6887 template-declaration
6888 explicit-instantiation
6889 explicit-specialization
6890 linkage-specification
6891 namespace-definition
6893 GNU extension:
6895 declaration:
6896 __extension__ declaration */
6898 static void
6899 cp_parser_declaration (cp_parser* parser)
6901 cp_token token1;
6902 cp_token token2;
6903 int saved_pedantic;
6904 void *p;
6906 /* Check for the `__extension__' keyword. */
6907 if (cp_parser_extension_opt (parser, &saved_pedantic))
6909 /* Parse the qualified declaration. */
6910 cp_parser_declaration (parser);
6911 /* Restore the PEDANTIC flag. */
6912 pedantic = saved_pedantic;
6914 return;
6917 /* Try to figure out what kind of declaration is present. */
6918 token1 = *cp_lexer_peek_token (parser->lexer);
6920 if (token1.type != CPP_EOF)
6921 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6922 else
6923 token2.type = token2.keyword = RID_MAX;
6925 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6926 p = obstack_alloc (&declarator_obstack, 0);
6928 /* If the next token is `extern' and the following token is a string
6929 literal, then we have a linkage specification. */
6930 if (token1.keyword == RID_EXTERN
6931 && cp_parser_is_string_literal (&token2))
6932 cp_parser_linkage_specification (parser);
6933 /* If the next token is `template', then we have either a template
6934 declaration, an explicit instantiation, or an explicit
6935 specialization. */
6936 else if (token1.keyword == RID_TEMPLATE)
6938 /* `template <>' indicates a template specialization. */
6939 if (token2.type == CPP_LESS
6940 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6941 cp_parser_explicit_specialization (parser);
6942 /* `template <' indicates a template declaration. */
6943 else if (token2.type == CPP_LESS)
6944 cp_parser_template_declaration (parser, /*member_p=*/false);
6945 /* Anything else must be an explicit instantiation. */
6946 else
6947 cp_parser_explicit_instantiation (parser);
6949 /* If the next token is `export', then we have a template
6950 declaration. */
6951 else if (token1.keyword == RID_EXPORT)
6952 cp_parser_template_declaration (parser, /*member_p=*/false);
6953 /* If the next token is `extern', 'static' or 'inline' and the one
6954 after that is `template', we have a GNU extended explicit
6955 instantiation directive. */
6956 else if (cp_parser_allow_gnu_extensions_p (parser)
6957 && (token1.keyword == RID_EXTERN
6958 || token1.keyword == RID_STATIC
6959 || token1.keyword == RID_INLINE)
6960 && token2.keyword == RID_TEMPLATE)
6961 cp_parser_explicit_instantiation (parser);
6962 /* If the next token is `namespace', check for a named or unnamed
6963 namespace definition. */
6964 else if (token1.keyword == RID_NAMESPACE
6965 && (/* A named namespace definition. */
6966 (token2.type == CPP_NAME
6967 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6968 == CPP_OPEN_BRACE))
6969 /* An unnamed namespace definition. */
6970 || token2.type == CPP_OPEN_BRACE))
6971 cp_parser_namespace_definition (parser);
6972 /* Objective-C++ declaration/definition. */
6973 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
6974 cp_parser_objc_declaration (parser);
6975 /* We must have either a block declaration or a function
6976 definition. */
6977 else
6978 /* Try to parse a block-declaration, or a function-definition. */
6979 cp_parser_block_declaration (parser, /*statement_p=*/false);
6981 /* Free any declarators allocated. */
6982 obstack_free (&declarator_obstack, p);
6985 /* Parse a block-declaration.
6987 block-declaration:
6988 simple-declaration
6989 asm-definition
6990 namespace-alias-definition
6991 using-declaration
6992 using-directive
6994 GNU Extension:
6996 block-declaration:
6997 __extension__ block-declaration
6998 label-declaration
7000 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7001 part of a declaration-statement. */
7003 static void
7004 cp_parser_block_declaration (cp_parser *parser,
7005 bool statement_p)
7007 cp_token *token1;
7008 int saved_pedantic;
7010 /* Check for the `__extension__' keyword. */
7011 if (cp_parser_extension_opt (parser, &saved_pedantic))
7013 /* Parse the qualified declaration. */
7014 cp_parser_block_declaration (parser, statement_p);
7015 /* Restore the PEDANTIC flag. */
7016 pedantic = saved_pedantic;
7018 return;
7021 /* Peek at the next token to figure out which kind of declaration is
7022 present. */
7023 token1 = cp_lexer_peek_token (parser->lexer);
7025 /* If the next keyword is `asm', we have an asm-definition. */
7026 if (token1->keyword == RID_ASM)
7028 if (statement_p)
7029 cp_parser_commit_to_tentative_parse (parser);
7030 cp_parser_asm_definition (parser);
7032 /* If the next keyword is `namespace', we have a
7033 namespace-alias-definition. */
7034 else if (token1->keyword == RID_NAMESPACE)
7035 cp_parser_namespace_alias_definition (parser);
7036 /* If the next keyword is `using', we have either a
7037 using-declaration or a using-directive. */
7038 else if (token1->keyword == RID_USING)
7040 cp_token *token2;
7042 if (statement_p)
7043 cp_parser_commit_to_tentative_parse (parser);
7044 /* If the token after `using' is `namespace', then we have a
7045 using-directive. */
7046 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7047 if (token2->keyword == RID_NAMESPACE)
7048 cp_parser_using_directive (parser);
7049 /* Otherwise, it's a using-declaration. */
7050 else
7051 cp_parser_using_declaration (parser);
7053 /* If the next keyword is `__label__' we have a label declaration. */
7054 else if (token1->keyword == RID_LABEL)
7056 if (statement_p)
7057 cp_parser_commit_to_tentative_parse (parser);
7058 cp_parser_label_declaration (parser);
7060 /* Anything else must be a simple-declaration. */
7061 else
7062 cp_parser_simple_declaration (parser, !statement_p);
7065 /* Parse a simple-declaration.
7067 simple-declaration:
7068 decl-specifier-seq [opt] init-declarator-list [opt] ;
7070 init-declarator-list:
7071 init-declarator
7072 init-declarator-list , init-declarator
7074 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7075 function-definition as a simple-declaration. */
7077 static void
7078 cp_parser_simple_declaration (cp_parser* parser,
7079 bool function_definition_allowed_p)
7081 cp_decl_specifier_seq decl_specifiers;
7082 int declares_class_or_enum;
7083 bool saw_declarator;
7085 /* Defer access checks until we know what is being declared; the
7086 checks for names appearing in the decl-specifier-seq should be
7087 done as if we were in the scope of the thing being declared. */
7088 push_deferring_access_checks (dk_deferred);
7090 /* Parse the decl-specifier-seq. We have to keep track of whether
7091 or not the decl-specifier-seq declares a named class or
7092 enumeration type, since that is the only case in which the
7093 init-declarator-list is allowed to be empty.
7095 [dcl.dcl]
7097 In a simple-declaration, the optional init-declarator-list can be
7098 omitted only when declaring a class or enumeration, that is when
7099 the decl-specifier-seq contains either a class-specifier, an
7100 elaborated-type-specifier, or an enum-specifier. */
7101 cp_parser_decl_specifier_seq (parser,
7102 CP_PARSER_FLAGS_OPTIONAL,
7103 &decl_specifiers,
7104 &declares_class_or_enum);
7105 /* We no longer need to defer access checks. */
7106 stop_deferring_access_checks ();
7108 /* In a block scope, a valid declaration must always have a
7109 decl-specifier-seq. By not trying to parse declarators, we can
7110 resolve the declaration/expression ambiguity more quickly. */
7111 if (!function_definition_allowed_p
7112 && !decl_specifiers.any_specifiers_p)
7114 cp_parser_error (parser, "expected declaration");
7115 goto done;
7118 /* If the next two tokens are both identifiers, the code is
7119 erroneous. The usual cause of this situation is code like:
7121 T t;
7123 where "T" should name a type -- but does not. */
7124 if (!decl_specifiers.type
7125 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7127 /* If parsing tentatively, we should commit; we really are
7128 looking at a declaration. */
7129 cp_parser_commit_to_tentative_parse (parser);
7130 /* Give up. */
7131 goto done;
7134 /* If we have seen at least one decl-specifier, and the next token
7135 is not a parenthesis, then we must be looking at a declaration.
7136 (After "int (" we might be looking at a functional cast.) */
7137 if (decl_specifiers.any_specifiers_p
7138 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7139 cp_parser_commit_to_tentative_parse (parser);
7141 /* Keep going until we hit the `;' at the end of the simple
7142 declaration. */
7143 saw_declarator = false;
7144 while (cp_lexer_next_token_is_not (parser->lexer,
7145 CPP_SEMICOLON))
7147 cp_token *token;
7148 bool function_definition_p;
7149 tree decl;
7151 saw_declarator = true;
7152 /* Parse the init-declarator. */
7153 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7154 function_definition_allowed_p,
7155 /*member_p=*/false,
7156 declares_class_or_enum,
7157 &function_definition_p);
7158 /* If an error occurred while parsing tentatively, exit quickly.
7159 (That usually happens when in the body of a function; each
7160 statement is treated as a declaration-statement until proven
7161 otherwise.) */
7162 if (cp_parser_error_occurred (parser))
7163 goto done;
7164 /* Handle function definitions specially. */
7165 if (function_definition_p)
7167 /* If the next token is a `,', then we are probably
7168 processing something like:
7170 void f() {}, *p;
7172 which is erroneous. */
7173 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7174 error ("mixing declarations and function-definitions is forbidden");
7175 /* Otherwise, we're done with the list of declarators. */
7176 else
7178 pop_deferring_access_checks ();
7179 return;
7182 /* The next token should be either a `,' or a `;'. */
7183 token = cp_lexer_peek_token (parser->lexer);
7184 /* If it's a `,', there are more declarators to come. */
7185 if (token->type == CPP_COMMA)
7186 cp_lexer_consume_token (parser->lexer);
7187 /* If it's a `;', we are done. */
7188 else if (token->type == CPP_SEMICOLON)
7189 break;
7190 /* Anything else is an error. */
7191 else
7193 /* If we have already issued an error message we don't need
7194 to issue another one. */
7195 if (decl != error_mark_node
7196 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7197 cp_parser_error (parser, "expected %<,%> or %<;%>");
7198 /* Skip tokens until we reach the end of the statement. */
7199 cp_parser_skip_to_end_of_statement (parser);
7200 /* If the next token is now a `;', consume it. */
7201 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7202 cp_lexer_consume_token (parser->lexer);
7203 goto done;
7205 /* After the first time around, a function-definition is not
7206 allowed -- even if it was OK at first. For example:
7208 int i, f() {}
7210 is not valid. */
7211 function_definition_allowed_p = false;
7214 /* Issue an error message if no declarators are present, and the
7215 decl-specifier-seq does not itself declare a class or
7216 enumeration. */
7217 if (!saw_declarator)
7219 if (cp_parser_declares_only_class_p (parser))
7220 shadow_tag (&decl_specifiers);
7221 /* Perform any deferred access checks. */
7222 perform_deferred_access_checks ();
7225 /* Consume the `;'. */
7226 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7228 done:
7229 pop_deferring_access_checks ();
7232 /* Parse a decl-specifier-seq.
7234 decl-specifier-seq:
7235 decl-specifier-seq [opt] decl-specifier
7237 decl-specifier:
7238 storage-class-specifier
7239 type-specifier
7240 function-specifier
7241 friend
7242 typedef
7244 GNU Extension:
7246 decl-specifier:
7247 attributes
7249 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7251 The parser flags FLAGS is used to control type-specifier parsing.
7253 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7254 flags:
7256 1: one of the decl-specifiers is an elaborated-type-specifier
7257 (i.e., a type declaration)
7258 2: one of the decl-specifiers is an enum-specifier or a
7259 class-specifier (i.e., a type definition)
7263 static void
7264 cp_parser_decl_specifier_seq (cp_parser* parser,
7265 cp_parser_flags flags,
7266 cp_decl_specifier_seq *decl_specs,
7267 int* declares_class_or_enum)
7269 bool constructor_possible_p = !parser->in_declarator_p;
7271 /* Clear DECL_SPECS. */
7272 clear_decl_specs (decl_specs);
7274 /* Assume no class or enumeration type is declared. */
7275 *declares_class_or_enum = 0;
7277 /* Keep reading specifiers until there are no more to read. */
7278 while (true)
7280 bool constructor_p;
7281 bool found_decl_spec;
7282 cp_token *token;
7284 /* Peek at the next token. */
7285 token = cp_lexer_peek_token (parser->lexer);
7286 /* Handle attributes. */
7287 if (token->keyword == RID_ATTRIBUTE)
7289 /* Parse the attributes. */
7290 decl_specs->attributes
7291 = chainon (decl_specs->attributes,
7292 cp_parser_attributes_opt (parser));
7293 continue;
7295 /* Assume we will find a decl-specifier keyword. */
7296 found_decl_spec = true;
7297 /* If the next token is an appropriate keyword, we can simply
7298 add it to the list. */
7299 switch (token->keyword)
7301 /* decl-specifier:
7302 friend */
7303 case RID_FRIEND:
7304 if (decl_specs->specs[(int) ds_friend]++)
7305 error ("duplicate %<friend%>");
7306 /* Consume the token. */
7307 cp_lexer_consume_token (parser->lexer);
7308 break;
7310 /* function-specifier:
7311 inline
7312 virtual
7313 explicit */
7314 case RID_INLINE:
7315 case RID_VIRTUAL:
7316 case RID_EXPLICIT:
7317 cp_parser_function_specifier_opt (parser, decl_specs);
7318 break;
7320 /* decl-specifier:
7321 typedef */
7322 case RID_TYPEDEF:
7323 ++decl_specs->specs[(int) ds_typedef];
7324 /* Consume the token. */
7325 cp_lexer_consume_token (parser->lexer);
7326 /* A constructor declarator cannot appear in a typedef. */
7327 constructor_possible_p = false;
7328 /* The "typedef" keyword can only occur in a declaration; we
7329 may as well commit at this point. */
7330 cp_parser_commit_to_tentative_parse (parser);
7331 break;
7333 /* storage-class-specifier:
7334 auto
7335 register
7336 static
7337 extern
7338 mutable
7340 GNU Extension:
7341 thread */
7342 case RID_AUTO:
7343 /* Consume the token. */
7344 cp_lexer_consume_token (parser->lexer);
7345 cp_parser_set_storage_class (decl_specs, sc_auto);
7346 break;
7347 case RID_REGISTER:
7348 /* Consume the token. */
7349 cp_lexer_consume_token (parser->lexer);
7350 cp_parser_set_storage_class (decl_specs, sc_register);
7351 break;
7352 case RID_STATIC:
7353 /* Consume the token. */
7354 cp_lexer_consume_token (parser->lexer);
7355 if (decl_specs->specs[(int) ds_thread])
7357 error ("%<__thread%> before %<static%>");
7358 decl_specs->specs[(int) ds_thread] = 0;
7360 cp_parser_set_storage_class (decl_specs, sc_static);
7361 break;
7362 case RID_EXTERN:
7363 /* Consume the token. */
7364 cp_lexer_consume_token (parser->lexer);
7365 if (decl_specs->specs[(int) ds_thread])
7367 error ("%<__thread%> before %<extern%>");
7368 decl_specs->specs[(int) ds_thread] = 0;
7370 cp_parser_set_storage_class (decl_specs, sc_extern);
7371 break;
7372 case RID_MUTABLE:
7373 /* Consume the token. */
7374 cp_lexer_consume_token (parser->lexer);
7375 cp_parser_set_storage_class (decl_specs, sc_mutable);
7376 break;
7377 case RID_THREAD:
7378 /* Consume the token. */
7379 cp_lexer_consume_token (parser->lexer);
7380 ++decl_specs->specs[(int) ds_thread];
7381 break;
7383 default:
7384 /* We did not yet find a decl-specifier yet. */
7385 found_decl_spec = false;
7386 break;
7389 /* Constructors are a special case. The `S' in `S()' is not a
7390 decl-specifier; it is the beginning of the declarator. */
7391 constructor_p
7392 = (!found_decl_spec
7393 && constructor_possible_p
7394 && (cp_parser_constructor_declarator_p
7395 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7397 /* If we don't have a DECL_SPEC yet, then we must be looking at
7398 a type-specifier. */
7399 if (!found_decl_spec && !constructor_p)
7401 int decl_spec_declares_class_or_enum;
7402 bool is_cv_qualifier;
7403 tree type_spec;
7405 type_spec
7406 = cp_parser_type_specifier (parser, flags,
7407 decl_specs,
7408 /*is_declaration=*/true,
7409 &decl_spec_declares_class_or_enum,
7410 &is_cv_qualifier);
7412 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7414 /* If this type-specifier referenced a user-defined type
7415 (a typedef, class-name, etc.), then we can't allow any
7416 more such type-specifiers henceforth.
7418 [dcl.spec]
7420 The longest sequence of decl-specifiers that could
7421 possibly be a type name is taken as the
7422 decl-specifier-seq of a declaration. The sequence shall
7423 be self-consistent as described below.
7425 [dcl.type]
7427 As a general rule, at most one type-specifier is allowed
7428 in the complete decl-specifier-seq of a declaration. The
7429 only exceptions are the following:
7431 -- const or volatile can be combined with any other
7432 type-specifier.
7434 -- signed or unsigned can be combined with char, long,
7435 short, or int.
7437 -- ..
7439 Example:
7441 typedef char* Pc;
7442 void g (const int Pc);
7444 Here, Pc is *not* part of the decl-specifier seq; it's
7445 the declarator. Therefore, once we see a type-specifier
7446 (other than a cv-qualifier), we forbid any additional
7447 user-defined types. We *do* still allow things like `int
7448 int' to be considered a decl-specifier-seq, and issue the
7449 error message later. */
7450 if (type_spec && !is_cv_qualifier)
7451 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7452 /* A constructor declarator cannot follow a type-specifier. */
7453 if (type_spec)
7455 constructor_possible_p = false;
7456 found_decl_spec = true;
7460 /* If we still do not have a DECL_SPEC, then there are no more
7461 decl-specifiers. */
7462 if (!found_decl_spec)
7463 break;
7465 decl_specs->any_specifiers_p = true;
7466 /* After we see one decl-specifier, further decl-specifiers are
7467 always optional. */
7468 flags |= CP_PARSER_FLAGS_OPTIONAL;
7471 /* Don't allow a friend specifier with a class definition. */
7472 if (decl_specs->specs[(int) ds_friend] != 0
7473 && (*declares_class_or_enum & 2))
7474 error ("class definition may not be declared a friend");
7477 /* Parse an (optional) storage-class-specifier.
7479 storage-class-specifier:
7480 auto
7481 register
7482 static
7483 extern
7484 mutable
7486 GNU Extension:
7488 storage-class-specifier:
7489 thread
7491 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7493 static tree
7494 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7496 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7498 case RID_AUTO:
7499 case RID_REGISTER:
7500 case RID_STATIC:
7501 case RID_EXTERN:
7502 case RID_MUTABLE:
7503 case RID_THREAD:
7504 /* Consume the token. */
7505 return cp_lexer_consume_token (parser->lexer)->value;
7507 default:
7508 return NULL_TREE;
7512 /* Parse an (optional) function-specifier.
7514 function-specifier:
7515 inline
7516 virtual
7517 explicit
7519 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7520 Updates DECL_SPECS, if it is non-NULL. */
7522 static tree
7523 cp_parser_function_specifier_opt (cp_parser* parser,
7524 cp_decl_specifier_seq *decl_specs)
7526 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7528 case RID_INLINE:
7529 if (decl_specs)
7530 ++decl_specs->specs[(int) ds_inline];
7531 break;
7533 case RID_VIRTUAL:
7534 if (decl_specs)
7535 ++decl_specs->specs[(int) ds_virtual];
7536 break;
7538 case RID_EXPLICIT:
7539 if (decl_specs)
7540 ++decl_specs->specs[(int) ds_explicit];
7541 break;
7543 default:
7544 return NULL_TREE;
7547 /* Consume the token. */
7548 return cp_lexer_consume_token (parser->lexer)->value;
7551 /* Parse a linkage-specification.
7553 linkage-specification:
7554 extern string-literal { declaration-seq [opt] }
7555 extern string-literal declaration */
7557 static void
7558 cp_parser_linkage_specification (cp_parser* parser)
7560 tree linkage;
7562 /* Look for the `extern' keyword. */
7563 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7565 /* Look for the string-literal. */
7566 linkage = cp_parser_string_literal (parser, false, false);
7568 /* Transform the literal into an identifier. If the literal is a
7569 wide-character string, or contains embedded NULs, then we can't
7570 handle it as the user wants. */
7571 if (strlen (TREE_STRING_POINTER (linkage))
7572 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7574 cp_parser_error (parser, "invalid linkage-specification");
7575 /* Assume C++ linkage. */
7576 linkage = lang_name_cplusplus;
7578 else
7579 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7581 /* We're now using the new linkage. */
7582 push_lang_context (linkage);
7584 /* If the next token is a `{', then we're using the first
7585 production. */
7586 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7588 /* Consume the `{' token. */
7589 cp_lexer_consume_token (parser->lexer);
7590 /* Parse the declarations. */
7591 cp_parser_declaration_seq_opt (parser);
7592 /* Look for the closing `}'. */
7593 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7595 /* Otherwise, there's just one declaration. */
7596 else
7598 bool saved_in_unbraced_linkage_specification_p;
7600 saved_in_unbraced_linkage_specification_p
7601 = parser->in_unbraced_linkage_specification_p;
7602 parser->in_unbraced_linkage_specification_p = true;
7603 have_extern_spec = true;
7604 cp_parser_declaration (parser);
7605 have_extern_spec = false;
7606 parser->in_unbraced_linkage_specification_p
7607 = saved_in_unbraced_linkage_specification_p;
7610 /* We're done with the linkage-specification. */
7611 pop_lang_context ();
7614 /* Special member functions [gram.special] */
7616 /* Parse a conversion-function-id.
7618 conversion-function-id:
7619 operator conversion-type-id
7621 Returns an IDENTIFIER_NODE representing the operator. */
7623 static tree
7624 cp_parser_conversion_function_id (cp_parser* parser)
7626 tree type;
7627 tree saved_scope;
7628 tree saved_qualifying_scope;
7629 tree saved_object_scope;
7630 tree pushed_scope = NULL_TREE;
7632 /* Look for the `operator' token. */
7633 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7634 return error_mark_node;
7635 /* When we parse the conversion-type-id, the current scope will be
7636 reset. However, we need that information in able to look up the
7637 conversion function later, so we save it here. */
7638 saved_scope = parser->scope;
7639 saved_qualifying_scope = parser->qualifying_scope;
7640 saved_object_scope = parser->object_scope;
7641 /* We must enter the scope of the class so that the names of
7642 entities declared within the class are available in the
7643 conversion-type-id. For example, consider:
7645 struct S {
7646 typedef int I;
7647 operator I();
7650 S::operator I() { ... }
7652 In order to see that `I' is a type-name in the definition, we
7653 must be in the scope of `S'. */
7654 if (saved_scope)
7655 pushed_scope = push_scope (saved_scope);
7656 /* Parse the conversion-type-id. */
7657 type = cp_parser_conversion_type_id (parser);
7658 /* Leave the scope of the class, if any. */
7659 if (pushed_scope)
7660 pop_scope (pushed_scope);
7661 /* Restore the saved scope. */
7662 parser->scope = saved_scope;
7663 parser->qualifying_scope = saved_qualifying_scope;
7664 parser->object_scope = saved_object_scope;
7665 /* If the TYPE is invalid, indicate failure. */
7666 if (type == error_mark_node)
7667 return error_mark_node;
7668 return mangle_conv_op_name_for_type (type);
7671 /* Parse a conversion-type-id:
7673 conversion-type-id:
7674 type-specifier-seq conversion-declarator [opt]
7676 Returns the TYPE specified. */
7678 static tree
7679 cp_parser_conversion_type_id (cp_parser* parser)
7681 tree attributes;
7682 cp_decl_specifier_seq type_specifiers;
7683 cp_declarator *declarator;
7684 tree type_specified;
7686 /* Parse the attributes. */
7687 attributes = cp_parser_attributes_opt (parser);
7688 /* Parse the type-specifiers. */
7689 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7690 &type_specifiers);
7691 /* If that didn't work, stop. */
7692 if (type_specifiers.type == error_mark_node)
7693 return error_mark_node;
7694 /* Parse the conversion-declarator. */
7695 declarator = cp_parser_conversion_declarator_opt (parser);
7697 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7698 /*initialized=*/0, &attributes);
7699 if (attributes)
7700 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7701 return type_specified;
7704 /* Parse an (optional) conversion-declarator.
7706 conversion-declarator:
7707 ptr-operator conversion-declarator [opt]
7711 static cp_declarator *
7712 cp_parser_conversion_declarator_opt (cp_parser* parser)
7714 enum tree_code code;
7715 tree class_type;
7716 cp_cv_quals cv_quals;
7718 /* We don't know if there's a ptr-operator next, or not. */
7719 cp_parser_parse_tentatively (parser);
7720 /* Try the ptr-operator. */
7721 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7722 /* If it worked, look for more conversion-declarators. */
7723 if (cp_parser_parse_definitely (parser))
7725 cp_declarator *declarator;
7727 /* Parse another optional declarator. */
7728 declarator = cp_parser_conversion_declarator_opt (parser);
7730 /* Create the representation of the declarator. */
7731 if (class_type)
7732 declarator = make_ptrmem_declarator (cv_quals, class_type,
7733 declarator);
7734 else if (code == INDIRECT_REF)
7735 declarator = make_pointer_declarator (cv_quals, declarator);
7736 else
7737 declarator = make_reference_declarator (cv_quals, declarator);
7739 return declarator;
7742 return NULL;
7745 /* Parse an (optional) ctor-initializer.
7747 ctor-initializer:
7748 : mem-initializer-list
7750 Returns TRUE iff the ctor-initializer was actually present. */
7752 static bool
7753 cp_parser_ctor_initializer_opt (cp_parser* parser)
7755 /* If the next token is not a `:', then there is no
7756 ctor-initializer. */
7757 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7759 /* Do default initialization of any bases and members. */
7760 if (DECL_CONSTRUCTOR_P (current_function_decl))
7761 finish_mem_initializers (NULL_TREE);
7763 return false;
7766 /* Consume the `:' token. */
7767 cp_lexer_consume_token (parser->lexer);
7768 /* And the mem-initializer-list. */
7769 cp_parser_mem_initializer_list (parser);
7771 return true;
7774 /* Parse a mem-initializer-list.
7776 mem-initializer-list:
7777 mem-initializer
7778 mem-initializer , mem-initializer-list */
7780 static void
7781 cp_parser_mem_initializer_list (cp_parser* parser)
7783 tree mem_initializer_list = NULL_TREE;
7785 /* Let the semantic analysis code know that we are starting the
7786 mem-initializer-list. */
7787 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7788 error ("only constructors take base initializers");
7790 /* Loop through the list. */
7791 while (true)
7793 tree mem_initializer;
7795 /* Parse the mem-initializer. */
7796 mem_initializer = cp_parser_mem_initializer (parser);
7797 /* Add it to the list, unless it was erroneous. */
7798 if (mem_initializer)
7800 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7801 mem_initializer_list = mem_initializer;
7803 /* If the next token is not a `,', we're done. */
7804 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7805 break;
7806 /* Consume the `,' token. */
7807 cp_lexer_consume_token (parser->lexer);
7810 /* Perform semantic analysis. */
7811 if (DECL_CONSTRUCTOR_P (current_function_decl))
7812 finish_mem_initializers (mem_initializer_list);
7815 /* Parse a mem-initializer.
7817 mem-initializer:
7818 mem-initializer-id ( expression-list [opt] )
7820 GNU extension:
7822 mem-initializer:
7823 ( expression-list [opt] )
7825 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7826 class) or FIELD_DECL (for a non-static data member) to initialize;
7827 the TREE_VALUE is the expression-list. */
7829 static tree
7830 cp_parser_mem_initializer (cp_parser* parser)
7832 tree mem_initializer_id;
7833 tree expression_list;
7834 tree member;
7836 /* Find out what is being initialized. */
7837 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7839 pedwarn ("anachronistic old-style base class initializer");
7840 mem_initializer_id = NULL_TREE;
7842 else
7843 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7844 member = expand_member_init (mem_initializer_id);
7845 if (member && !DECL_P (member))
7846 in_base_initializer = 1;
7848 expression_list
7849 = cp_parser_parenthesized_expression_list (parser, false,
7850 /*cast_p=*/false,
7851 /*non_constant_p=*/NULL);
7852 if (!expression_list)
7853 expression_list = void_type_node;
7855 in_base_initializer = 0;
7857 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7860 /* Parse a mem-initializer-id.
7862 mem-initializer-id:
7863 :: [opt] nested-name-specifier [opt] class-name
7864 identifier
7866 Returns a TYPE indicating the class to be initializer for the first
7867 production. Returns an IDENTIFIER_NODE indicating the data member
7868 to be initialized for the second production. */
7870 static tree
7871 cp_parser_mem_initializer_id (cp_parser* parser)
7873 bool global_scope_p;
7874 bool nested_name_specifier_p;
7875 bool template_p = false;
7876 tree id;
7878 /* `typename' is not allowed in this context ([temp.res]). */
7879 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7881 error ("keyword %<typename%> not allowed in this context (a qualified "
7882 "member initializer is implicitly a type)");
7883 cp_lexer_consume_token (parser->lexer);
7885 /* Look for the optional `::' operator. */
7886 global_scope_p
7887 = (cp_parser_global_scope_opt (parser,
7888 /*current_scope_valid_p=*/false)
7889 != NULL_TREE);
7890 /* Look for the optional nested-name-specifier. The simplest way to
7891 implement:
7893 [temp.res]
7895 The keyword `typename' is not permitted in a base-specifier or
7896 mem-initializer; in these contexts a qualified name that
7897 depends on a template-parameter is implicitly assumed to be a
7898 type name.
7900 is to assume that we have seen the `typename' keyword at this
7901 point. */
7902 nested_name_specifier_p
7903 = (cp_parser_nested_name_specifier_opt (parser,
7904 /*typename_keyword_p=*/true,
7905 /*check_dependency_p=*/true,
7906 /*type_p=*/true,
7907 /*is_declaration=*/true)
7908 != NULL_TREE);
7909 if (nested_name_specifier_p)
7910 template_p = cp_parser_optional_template_keyword (parser);
7911 /* If there is a `::' operator or a nested-name-specifier, then we
7912 are definitely looking for a class-name. */
7913 if (global_scope_p || nested_name_specifier_p)
7914 return cp_parser_class_name (parser,
7915 /*typename_keyword_p=*/true,
7916 /*template_keyword_p=*/template_p,
7917 none_type,
7918 /*check_dependency_p=*/true,
7919 /*class_head_p=*/false,
7920 /*is_declaration=*/true);
7921 /* Otherwise, we could also be looking for an ordinary identifier. */
7922 cp_parser_parse_tentatively (parser);
7923 /* Try a class-name. */
7924 id = cp_parser_class_name (parser,
7925 /*typename_keyword_p=*/true,
7926 /*template_keyword_p=*/false,
7927 none_type,
7928 /*check_dependency_p=*/true,
7929 /*class_head_p=*/false,
7930 /*is_declaration=*/true);
7931 /* If we found one, we're done. */
7932 if (cp_parser_parse_definitely (parser))
7933 return id;
7934 /* Otherwise, look for an ordinary identifier. */
7935 return cp_parser_identifier (parser);
7938 /* Overloading [gram.over] */
7940 /* Parse an operator-function-id.
7942 operator-function-id:
7943 operator operator
7945 Returns an IDENTIFIER_NODE for the operator which is a
7946 human-readable spelling of the identifier, e.g., `operator +'. */
7948 static tree
7949 cp_parser_operator_function_id (cp_parser* parser)
7951 /* Look for the `operator' keyword. */
7952 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7953 return error_mark_node;
7954 /* And then the name of the operator itself. */
7955 return cp_parser_operator (parser);
7958 /* Parse an operator.
7960 operator:
7961 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7962 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7963 || ++ -- , ->* -> () []
7965 GNU Extensions:
7967 operator:
7968 <? >? <?= >?=
7970 Returns an IDENTIFIER_NODE for the operator which is a
7971 human-readable spelling of the identifier, e.g., `operator +'. */
7973 static tree
7974 cp_parser_operator (cp_parser* parser)
7976 tree id = NULL_TREE;
7977 cp_token *token;
7979 /* Peek at the next token. */
7980 token = cp_lexer_peek_token (parser->lexer);
7981 /* Figure out which operator we have. */
7982 switch (token->type)
7984 case CPP_KEYWORD:
7986 enum tree_code op;
7988 /* The keyword should be either `new' or `delete'. */
7989 if (token->keyword == RID_NEW)
7990 op = NEW_EXPR;
7991 else if (token->keyword == RID_DELETE)
7992 op = DELETE_EXPR;
7993 else
7994 break;
7996 /* Consume the `new' or `delete' token. */
7997 cp_lexer_consume_token (parser->lexer);
7999 /* Peek at the next token. */
8000 token = cp_lexer_peek_token (parser->lexer);
8001 /* If it's a `[' token then this is the array variant of the
8002 operator. */
8003 if (token->type == CPP_OPEN_SQUARE)
8005 /* Consume the `[' token. */
8006 cp_lexer_consume_token (parser->lexer);
8007 /* Look for the `]' token. */
8008 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8009 id = ansi_opname (op == NEW_EXPR
8010 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8012 /* Otherwise, we have the non-array variant. */
8013 else
8014 id = ansi_opname (op);
8016 return id;
8019 case CPP_PLUS:
8020 id = ansi_opname (PLUS_EXPR);
8021 break;
8023 case CPP_MINUS:
8024 id = ansi_opname (MINUS_EXPR);
8025 break;
8027 case CPP_MULT:
8028 id = ansi_opname (MULT_EXPR);
8029 break;
8031 case CPP_DIV:
8032 id = ansi_opname (TRUNC_DIV_EXPR);
8033 break;
8035 case CPP_MOD:
8036 id = ansi_opname (TRUNC_MOD_EXPR);
8037 break;
8039 case CPP_XOR:
8040 id = ansi_opname (BIT_XOR_EXPR);
8041 break;
8043 case CPP_AND:
8044 id = ansi_opname (BIT_AND_EXPR);
8045 break;
8047 case CPP_OR:
8048 id = ansi_opname (BIT_IOR_EXPR);
8049 break;
8051 case CPP_COMPL:
8052 id = ansi_opname (BIT_NOT_EXPR);
8053 break;
8055 case CPP_NOT:
8056 id = ansi_opname (TRUTH_NOT_EXPR);
8057 break;
8059 case CPP_EQ:
8060 id = ansi_assopname (NOP_EXPR);
8061 break;
8063 case CPP_LESS:
8064 id = ansi_opname (LT_EXPR);
8065 break;
8067 case CPP_GREATER:
8068 id = ansi_opname (GT_EXPR);
8069 break;
8071 case CPP_PLUS_EQ:
8072 id = ansi_assopname (PLUS_EXPR);
8073 break;
8075 case CPP_MINUS_EQ:
8076 id = ansi_assopname (MINUS_EXPR);
8077 break;
8079 case CPP_MULT_EQ:
8080 id = ansi_assopname (MULT_EXPR);
8081 break;
8083 case CPP_DIV_EQ:
8084 id = ansi_assopname (TRUNC_DIV_EXPR);
8085 break;
8087 case CPP_MOD_EQ:
8088 id = ansi_assopname (TRUNC_MOD_EXPR);
8089 break;
8091 case CPP_XOR_EQ:
8092 id = ansi_assopname (BIT_XOR_EXPR);
8093 break;
8095 case CPP_AND_EQ:
8096 id = ansi_assopname (BIT_AND_EXPR);
8097 break;
8099 case CPP_OR_EQ:
8100 id = ansi_assopname (BIT_IOR_EXPR);
8101 break;
8103 case CPP_LSHIFT:
8104 id = ansi_opname (LSHIFT_EXPR);
8105 break;
8107 case CPP_RSHIFT:
8108 id = ansi_opname (RSHIFT_EXPR);
8109 break;
8111 case CPP_LSHIFT_EQ:
8112 id = ansi_assopname (LSHIFT_EXPR);
8113 break;
8115 case CPP_RSHIFT_EQ:
8116 id = ansi_assopname (RSHIFT_EXPR);
8117 break;
8119 case CPP_EQ_EQ:
8120 id = ansi_opname (EQ_EXPR);
8121 break;
8123 case CPP_NOT_EQ:
8124 id = ansi_opname (NE_EXPR);
8125 break;
8127 case CPP_LESS_EQ:
8128 id = ansi_opname (LE_EXPR);
8129 break;
8131 case CPP_GREATER_EQ:
8132 id = ansi_opname (GE_EXPR);
8133 break;
8135 case CPP_AND_AND:
8136 id = ansi_opname (TRUTH_ANDIF_EXPR);
8137 break;
8139 case CPP_OR_OR:
8140 id = ansi_opname (TRUTH_ORIF_EXPR);
8141 break;
8143 case CPP_PLUS_PLUS:
8144 id = ansi_opname (POSTINCREMENT_EXPR);
8145 break;
8147 case CPP_MINUS_MINUS:
8148 id = ansi_opname (PREDECREMENT_EXPR);
8149 break;
8151 case CPP_COMMA:
8152 id = ansi_opname (COMPOUND_EXPR);
8153 break;
8155 case CPP_DEREF_STAR:
8156 id = ansi_opname (MEMBER_REF);
8157 break;
8159 case CPP_DEREF:
8160 id = ansi_opname (COMPONENT_REF);
8161 break;
8163 case CPP_OPEN_PAREN:
8164 /* Consume the `('. */
8165 cp_lexer_consume_token (parser->lexer);
8166 /* Look for the matching `)'. */
8167 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8168 return ansi_opname (CALL_EXPR);
8170 case CPP_OPEN_SQUARE:
8171 /* Consume the `['. */
8172 cp_lexer_consume_token (parser->lexer);
8173 /* Look for the matching `]'. */
8174 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8175 return ansi_opname (ARRAY_REF);
8177 /* Extensions. */
8178 case CPP_MIN:
8179 id = ansi_opname (MIN_EXPR);
8180 cp_parser_warn_min_max ();
8181 break;
8183 case CPP_MAX:
8184 id = ansi_opname (MAX_EXPR);
8185 cp_parser_warn_min_max ();
8186 break;
8188 case CPP_MIN_EQ:
8189 id = ansi_assopname (MIN_EXPR);
8190 cp_parser_warn_min_max ();
8191 break;
8193 case CPP_MAX_EQ:
8194 id = ansi_assopname (MAX_EXPR);
8195 cp_parser_warn_min_max ();
8196 break;
8198 default:
8199 /* Anything else is an error. */
8200 break;
8203 /* If we have selected an identifier, we need to consume the
8204 operator token. */
8205 if (id)
8206 cp_lexer_consume_token (parser->lexer);
8207 /* Otherwise, no valid operator name was present. */
8208 else
8210 cp_parser_error (parser, "expected operator");
8211 id = error_mark_node;
8214 return id;
8217 /* Parse a template-declaration.
8219 template-declaration:
8220 export [opt] template < template-parameter-list > declaration
8222 If MEMBER_P is TRUE, this template-declaration occurs within a
8223 class-specifier.
8225 The grammar rule given by the standard isn't correct. What
8226 is really meant is:
8228 template-declaration:
8229 export [opt] template-parameter-list-seq
8230 decl-specifier-seq [opt] init-declarator [opt] ;
8231 export [opt] template-parameter-list-seq
8232 function-definition
8234 template-parameter-list-seq:
8235 template-parameter-list-seq [opt]
8236 template < template-parameter-list > */
8238 static void
8239 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8241 /* Check for `export'. */
8242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8244 /* Consume the `export' token. */
8245 cp_lexer_consume_token (parser->lexer);
8246 /* Warn that we do not support `export'. */
8247 warning (0, "keyword %<export%> not implemented, and will be ignored");
8250 cp_parser_template_declaration_after_export (parser, member_p);
8253 /* Parse a template-parameter-list.
8255 template-parameter-list:
8256 template-parameter
8257 template-parameter-list , template-parameter
8259 Returns a TREE_LIST. Each node represents a template parameter.
8260 The nodes are connected via their TREE_CHAINs. */
8262 static tree
8263 cp_parser_template_parameter_list (cp_parser* parser)
8265 tree parameter_list = NULL_TREE;
8267 while (true)
8269 tree parameter;
8270 cp_token *token;
8271 bool is_non_type;
8273 /* Parse the template-parameter. */
8274 parameter = cp_parser_template_parameter (parser, &is_non_type);
8275 /* Add it to the list. */
8276 if (parameter != error_mark_node)
8277 parameter_list = process_template_parm (parameter_list,
8278 parameter,
8279 is_non_type);
8280 /* Peek at the next token. */
8281 token = cp_lexer_peek_token (parser->lexer);
8282 /* If it's not a `,', we're done. */
8283 if (token->type != CPP_COMMA)
8284 break;
8285 /* Otherwise, consume the `,' token. */
8286 cp_lexer_consume_token (parser->lexer);
8289 return parameter_list;
8292 /* Parse a template-parameter.
8294 template-parameter:
8295 type-parameter
8296 parameter-declaration
8298 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8299 the parameter. The TREE_PURPOSE is the default value, if any.
8300 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8301 iff this parameter is a non-type parameter. */
8303 static tree
8304 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8306 cp_token *token;
8307 cp_parameter_declarator *parameter_declarator;
8308 tree parm;
8310 /* Assume it is a type parameter or a template parameter. */
8311 *is_non_type = false;
8312 /* Peek at the next token. */
8313 token = cp_lexer_peek_token (parser->lexer);
8314 /* If it is `class' or `template', we have a type-parameter. */
8315 if (token->keyword == RID_TEMPLATE)
8316 return cp_parser_type_parameter (parser);
8317 /* If it is `class' or `typename' we do not know yet whether it is a
8318 type parameter or a non-type parameter. Consider:
8320 template <typename T, typename T::X X> ...
8324 template <class C, class D*> ...
8326 Here, the first parameter is a type parameter, and the second is
8327 a non-type parameter. We can tell by looking at the token after
8328 the identifier -- if it is a `,', `=', or `>' then we have a type
8329 parameter. */
8330 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8332 /* Peek at the token after `class' or `typename'. */
8333 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8334 /* If it's an identifier, skip it. */
8335 if (token->type == CPP_NAME)
8336 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8337 /* Now, see if the token looks like the end of a template
8338 parameter. */
8339 if (token->type == CPP_COMMA
8340 || token->type == CPP_EQ
8341 || token->type == CPP_GREATER)
8342 return cp_parser_type_parameter (parser);
8345 /* Otherwise, it is a non-type parameter.
8347 [temp.param]
8349 When parsing a default template-argument for a non-type
8350 template-parameter, the first non-nested `>' is taken as the end
8351 of the template parameter-list rather than a greater-than
8352 operator. */
8353 *is_non_type = true;
8354 parameter_declarator
8355 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8356 /*parenthesized_p=*/NULL);
8357 parm = grokdeclarator (parameter_declarator->declarator,
8358 &parameter_declarator->decl_specifiers,
8359 PARM, /*initialized=*/0,
8360 /*attrlist=*/NULL);
8361 if (parm == error_mark_node)
8362 return error_mark_node;
8363 return build_tree_list (parameter_declarator->default_argument, parm);
8366 /* Parse a type-parameter.
8368 type-parameter:
8369 class identifier [opt]
8370 class identifier [opt] = type-id
8371 typename identifier [opt]
8372 typename identifier [opt] = type-id
8373 template < template-parameter-list > class identifier [opt]
8374 template < template-parameter-list > class identifier [opt]
8375 = id-expression
8377 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8378 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8379 the declaration of the parameter. */
8381 static tree
8382 cp_parser_type_parameter (cp_parser* parser)
8384 cp_token *token;
8385 tree parameter;
8387 /* Look for a keyword to tell us what kind of parameter this is. */
8388 token = cp_parser_require (parser, CPP_KEYWORD,
8389 "`class', `typename', or `template'");
8390 if (!token)
8391 return error_mark_node;
8393 switch (token->keyword)
8395 case RID_CLASS:
8396 case RID_TYPENAME:
8398 tree identifier;
8399 tree default_argument;
8401 /* If the next token is an identifier, then it names the
8402 parameter. */
8403 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8404 identifier = cp_parser_identifier (parser);
8405 else
8406 identifier = NULL_TREE;
8408 /* Create the parameter. */
8409 parameter = finish_template_type_parm (class_type_node, identifier);
8411 /* If the next token is an `=', we have a default argument. */
8412 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8414 /* Consume the `=' token. */
8415 cp_lexer_consume_token (parser->lexer);
8416 /* Parse the default-argument. */
8417 default_argument = cp_parser_type_id (parser);
8419 else
8420 default_argument = NULL_TREE;
8422 /* Create the combined representation of the parameter and the
8423 default argument. */
8424 parameter = build_tree_list (default_argument, parameter);
8426 break;
8428 case RID_TEMPLATE:
8430 tree parameter_list;
8431 tree identifier;
8432 tree default_argument;
8434 /* Look for the `<'. */
8435 cp_parser_require (parser, CPP_LESS, "`<'");
8436 /* Parse the template-parameter-list. */
8437 begin_template_parm_list ();
8438 parameter_list
8439 = cp_parser_template_parameter_list (parser);
8440 parameter_list = end_template_parm_list (parameter_list);
8441 /* Look for the `>'. */
8442 cp_parser_require (parser, CPP_GREATER, "`>'");
8443 /* Look for the `class' keyword. */
8444 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8445 /* If the next token is an `=', then there is a
8446 default-argument. If the next token is a `>', we are at
8447 the end of the parameter-list. If the next token is a `,',
8448 then we are at the end of this parameter. */
8449 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8450 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8451 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8453 identifier = cp_parser_identifier (parser);
8454 /* Treat invalid names as if the parameter were nameless. */
8455 if (identifier == error_mark_node)
8456 identifier = NULL_TREE;
8458 else
8459 identifier = NULL_TREE;
8461 /* Create the template parameter. */
8462 parameter = finish_template_template_parm (class_type_node,
8463 identifier);
8465 /* If the next token is an `=', then there is a
8466 default-argument. */
8467 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8469 bool is_template;
8471 /* Consume the `='. */
8472 cp_lexer_consume_token (parser->lexer);
8473 /* Parse the id-expression. */
8474 default_argument
8475 = cp_parser_id_expression (parser,
8476 /*template_keyword_p=*/false,
8477 /*check_dependency_p=*/true,
8478 /*template_p=*/&is_template,
8479 /*declarator_p=*/false);
8480 if (TREE_CODE (default_argument) == TYPE_DECL)
8481 /* If the id-expression was a template-id that refers to
8482 a template-class, we already have the declaration here,
8483 so no further lookup is needed. */
8485 else
8486 /* Look up the name. */
8487 default_argument
8488 = cp_parser_lookup_name (parser, default_argument,
8489 none_type,
8490 /*is_template=*/is_template,
8491 /*is_namespace=*/false,
8492 /*check_dependency=*/true,
8493 /*ambiguous_p=*/NULL);
8494 /* See if the default argument is valid. */
8495 default_argument
8496 = check_template_template_default_arg (default_argument);
8498 else
8499 default_argument = NULL_TREE;
8501 /* Create the combined representation of the parameter and the
8502 default argument. */
8503 parameter = build_tree_list (default_argument, parameter);
8505 break;
8507 default:
8508 gcc_unreachable ();
8509 break;
8512 return parameter;
8515 /* Parse a template-id.
8517 template-id:
8518 template-name < template-argument-list [opt] >
8520 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8521 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8522 returned. Otherwise, if the template-name names a function, or set
8523 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8524 names a class, returns a TYPE_DECL for the specialization.
8526 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8527 uninstantiated templates. */
8529 static tree
8530 cp_parser_template_id (cp_parser *parser,
8531 bool template_keyword_p,
8532 bool check_dependency_p,
8533 bool is_declaration)
8535 tree template;
8536 tree arguments;
8537 tree template_id;
8538 cp_token_position start_of_id = 0;
8539 tree access_check = NULL_TREE;
8540 cp_token *next_token, *next_token_2;
8541 bool is_identifier;
8543 /* If the next token corresponds to a template-id, there is no need
8544 to reparse it. */
8545 next_token = cp_lexer_peek_token (parser->lexer);
8546 if (next_token->type == CPP_TEMPLATE_ID)
8548 tree value;
8549 tree check;
8551 /* Get the stored value. */
8552 value = cp_lexer_consume_token (parser->lexer)->value;
8553 /* Perform any access checks that were deferred. */
8554 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8555 perform_or_defer_access_check (TREE_PURPOSE (check),
8556 TREE_VALUE (check));
8557 /* Return the stored value. */
8558 return TREE_VALUE (value);
8561 /* Avoid performing name lookup if there is no possibility of
8562 finding a template-id. */
8563 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8564 || (next_token->type == CPP_NAME
8565 && !cp_parser_nth_token_starts_template_argument_list_p
8566 (parser, 2)))
8568 cp_parser_error (parser, "expected template-id");
8569 return error_mark_node;
8572 /* Remember where the template-id starts. */
8573 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8574 start_of_id = cp_lexer_token_position (parser->lexer, false);
8576 push_deferring_access_checks (dk_deferred);
8578 /* Parse the template-name. */
8579 is_identifier = false;
8580 template = cp_parser_template_name (parser, template_keyword_p,
8581 check_dependency_p,
8582 is_declaration,
8583 &is_identifier);
8584 if (template == error_mark_node || is_identifier)
8586 pop_deferring_access_checks ();
8587 return template;
8590 /* If we find the sequence `[:' after a template-name, it's probably
8591 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8592 parse correctly the argument list. */
8593 next_token = cp_lexer_peek_token (parser->lexer);
8594 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8595 if (next_token->type == CPP_OPEN_SQUARE
8596 && next_token->flags & DIGRAPH
8597 && next_token_2->type == CPP_COLON
8598 && !(next_token_2->flags & PREV_WHITE))
8600 cp_parser_parse_tentatively (parser);
8601 /* Change `:' into `::'. */
8602 next_token_2->type = CPP_SCOPE;
8603 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8604 CPP_LESS. */
8605 cp_lexer_consume_token (parser->lexer);
8606 /* Parse the arguments. */
8607 arguments = cp_parser_enclosed_template_argument_list (parser);
8608 if (!cp_parser_parse_definitely (parser))
8610 /* If we couldn't parse an argument list, then we revert our changes
8611 and return simply an error. Maybe this is not a template-id
8612 after all. */
8613 next_token_2->type = CPP_COLON;
8614 cp_parser_error (parser, "expected %<<%>");
8615 pop_deferring_access_checks ();
8616 return error_mark_node;
8618 /* Otherwise, emit an error about the invalid digraph, but continue
8619 parsing because we got our argument list. */
8620 pedwarn ("%<<::%> cannot begin a template-argument list");
8621 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8622 "between %<<%> and %<::%>");
8623 if (!flag_permissive)
8625 static bool hint;
8626 if (!hint)
8628 inform ("(if you use -fpermissive G++ will accept your code)");
8629 hint = true;
8633 else
8635 /* Look for the `<' that starts the template-argument-list. */
8636 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8638 pop_deferring_access_checks ();
8639 return error_mark_node;
8641 /* Parse the arguments. */
8642 arguments = cp_parser_enclosed_template_argument_list (parser);
8645 /* Build a representation of the specialization. */
8646 if (TREE_CODE (template) == IDENTIFIER_NODE)
8647 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8648 else if (DECL_CLASS_TEMPLATE_P (template)
8649 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8650 template_id
8651 = finish_template_type (template, arguments,
8652 cp_lexer_next_token_is (parser->lexer,
8653 CPP_SCOPE));
8654 else
8656 /* If it's not a class-template or a template-template, it should be
8657 a function-template. */
8658 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8659 || TREE_CODE (template) == OVERLOAD
8660 || BASELINK_P (template)));
8662 template_id = lookup_template_function (template, arguments);
8665 /* Retrieve any deferred checks. Do not pop this access checks yet
8666 so the memory will not be reclaimed during token replacing below. */
8667 access_check = get_deferred_access_checks ();
8669 /* If parsing tentatively, replace the sequence of tokens that makes
8670 up the template-id with a CPP_TEMPLATE_ID token. That way,
8671 should we re-parse the token stream, we will not have to repeat
8672 the effort required to do the parse, nor will we issue duplicate
8673 error messages about problems during instantiation of the
8674 template. */
8675 if (start_of_id)
8677 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8679 /* Reset the contents of the START_OF_ID token. */
8680 token->type = CPP_TEMPLATE_ID;
8681 token->value = build_tree_list (access_check, template_id);
8682 token->keyword = RID_MAX;
8684 /* Purge all subsequent tokens. */
8685 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8687 /* ??? Can we actually assume that, if template_id ==
8688 error_mark_node, we will have issued a diagnostic to the
8689 user, as opposed to simply marking the tentative parse as
8690 failed? */
8691 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8692 error ("parse error in template argument list");
8695 pop_deferring_access_checks ();
8696 return template_id;
8699 /* Parse a template-name.
8701 template-name:
8702 identifier
8704 The standard should actually say:
8706 template-name:
8707 identifier
8708 operator-function-id
8710 A defect report has been filed about this issue.
8712 A conversion-function-id cannot be a template name because they cannot
8713 be part of a template-id. In fact, looking at this code:
8715 a.operator K<int>()
8717 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8718 It is impossible to call a templated conversion-function-id with an
8719 explicit argument list, since the only allowed template parameter is
8720 the type to which it is converting.
8722 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8723 `template' keyword, in a construction like:
8725 T::template f<3>()
8727 In that case `f' is taken to be a template-name, even though there
8728 is no way of knowing for sure.
8730 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8731 name refers to a set of overloaded functions, at least one of which
8732 is a template, or an IDENTIFIER_NODE with the name of the template,
8733 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8734 names are looked up inside uninstantiated templates. */
8736 static tree
8737 cp_parser_template_name (cp_parser* parser,
8738 bool template_keyword_p,
8739 bool check_dependency_p,
8740 bool is_declaration,
8741 bool *is_identifier)
8743 tree identifier;
8744 tree decl;
8745 tree fns;
8747 /* If the next token is `operator', then we have either an
8748 operator-function-id or a conversion-function-id. */
8749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8751 /* We don't know whether we're looking at an
8752 operator-function-id or a conversion-function-id. */
8753 cp_parser_parse_tentatively (parser);
8754 /* Try an operator-function-id. */
8755 identifier = cp_parser_operator_function_id (parser);
8756 /* If that didn't work, try a conversion-function-id. */
8757 if (!cp_parser_parse_definitely (parser))
8759 cp_parser_error (parser, "expected template-name");
8760 return error_mark_node;
8763 /* Look for the identifier. */
8764 else
8765 identifier = cp_parser_identifier (parser);
8767 /* If we didn't find an identifier, we don't have a template-id. */
8768 if (identifier == error_mark_node)
8769 return error_mark_node;
8771 /* If the name immediately followed the `template' keyword, then it
8772 is a template-name. However, if the next token is not `<', then
8773 we do not treat it as a template-name, since it is not being used
8774 as part of a template-id. This enables us to handle constructs
8775 like:
8777 template <typename T> struct S { S(); };
8778 template <typename T> S<T>::S();
8780 correctly. We would treat `S' as a template -- if it were `S<T>'
8781 -- but we do not if there is no `<'. */
8783 if (processing_template_decl
8784 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8786 /* In a declaration, in a dependent context, we pretend that the
8787 "template" keyword was present in order to improve error
8788 recovery. For example, given:
8790 template <typename T> void f(T::X<int>);
8792 we want to treat "X<int>" as a template-id. */
8793 if (is_declaration
8794 && !template_keyword_p
8795 && parser->scope && TYPE_P (parser->scope)
8796 && check_dependency_p
8797 && dependent_type_p (parser->scope)
8798 /* Do not do this for dtors (or ctors), since they never
8799 need the template keyword before their name. */
8800 && !constructor_name_p (identifier, parser->scope))
8802 cp_token_position start = 0;
8804 /* Explain what went wrong. */
8805 error ("non-template %qD used as template", identifier);
8806 inform ("use %<%T::template %D%> to indicate that it is a template",
8807 parser->scope, identifier);
8808 /* If parsing tentatively, find the location of the "<" token. */
8809 if (cp_parser_simulate_error (parser))
8810 start = cp_lexer_token_position (parser->lexer, true);
8811 /* Parse the template arguments so that we can issue error
8812 messages about them. */
8813 cp_lexer_consume_token (parser->lexer);
8814 cp_parser_enclosed_template_argument_list (parser);
8815 /* Skip tokens until we find a good place from which to
8816 continue parsing. */
8817 cp_parser_skip_to_closing_parenthesis (parser,
8818 /*recovering=*/true,
8819 /*or_comma=*/true,
8820 /*consume_paren=*/false);
8821 /* If parsing tentatively, permanently remove the
8822 template argument list. That will prevent duplicate
8823 error messages from being issued about the missing
8824 "template" keyword. */
8825 if (start)
8826 cp_lexer_purge_tokens_after (parser->lexer, start);
8827 if (is_identifier)
8828 *is_identifier = true;
8829 return identifier;
8832 /* If the "template" keyword is present, then there is generally
8833 no point in doing name-lookup, so we just return IDENTIFIER.
8834 But, if the qualifying scope is non-dependent then we can
8835 (and must) do name-lookup normally. */
8836 if (template_keyword_p
8837 && (!parser->scope
8838 || (TYPE_P (parser->scope)
8839 && dependent_type_p (parser->scope))))
8840 return identifier;
8843 /* Look up the name. */
8844 decl = cp_parser_lookup_name (parser, identifier,
8845 none_type,
8846 /*is_template=*/false,
8847 /*is_namespace=*/false,
8848 check_dependency_p,
8849 /*ambiguous_p=*/NULL);
8850 decl = maybe_get_template_decl_from_type_decl (decl);
8852 /* If DECL is a template, then the name was a template-name. */
8853 if (TREE_CODE (decl) == TEMPLATE_DECL)
8855 else
8857 tree fn = NULL_TREE;
8859 /* The standard does not explicitly indicate whether a name that
8860 names a set of overloaded declarations, some of which are
8861 templates, is a template-name. However, such a name should
8862 be a template-name; otherwise, there is no way to form a
8863 template-id for the overloaded templates. */
8864 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8865 if (TREE_CODE (fns) == OVERLOAD)
8866 for (fn = fns; fn; fn = OVL_NEXT (fn))
8867 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8868 break;
8870 if (!fn)
8872 /* The name does not name a template. */
8873 cp_parser_error (parser, "expected template-name");
8874 return error_mark_node;
8878 /* If DECL is dependent, and refers to a function, then just return
8879 its name; we will look it up again during template instantiation. */
8880 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8882 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8883 if (TYPE_P (scope) && dependent_type_p (scope))
8884 return identifier;
8887 return decl;
8890 /* Parse a template-argument-list.
8892 template-argument-list:
8893 template-argument
8894 template-argument-list , template-argument
8896 Returns a TREE_VEC containing the arguments. */
8898 static tree
8899 cp_parser_template_argument_list (cp_parser* parser)
8901 tree fixed_args[10];
8902 unsigned n_args = 0;
8903 unsigned alloced = 10;
8904 tree *arg_ary = fixed_args;
8905 tree vec;
8906 bool saved_in_template_argument_list_p;
8907 bool saved_ice_p;
8908 bool saved_non_ice_p;
8910 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8911 parser->in_template_argument_list_p = true;
8912 /* Even if the template-id appears in an integral
8913 constant-expression, the contents of the argument list do
8914 not. */
8915 saved_ice_p = parser->integral_constant_expression_p;
8916 parser->integral_constant_expression_p = false;
8917 saved_non_ice_p = parser->non_integral_constant_expression_p;
8918 parser->non_integral_constant_expression_p = false;
8921 tree argument;
8923 if (n_args)
8924 /* Consume the comma. */
8925 cp_lexer_consume_token (parser->lexer);
8927 /* Parse the template-argument. */
8928 argument = cp_parser_template_argument (parser);
8929 if (n_args == alloced)
8931 alloced *= 2;
8933 if (arg_ary == fixed_args)
8935 arg_ary = xmalloc (sizeof (tree) * alloced);
8936 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8938 else
8939 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8941 arg_ary[n_args++] = argument;
8943 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8945 vec = make_tree_vec (n_args);
8947 while (n_args--)
8948 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8950 if (arg_ary != fixed_args)
8951 free (arg_ary);
8952 parser->non_integral_constant_expression_p = saved_non_ice_p;
8953 parser->integral_constant_expression_p = saved_ice_p;
8954 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8955 return vec;
8958 /* Parse a template-argument.
8960 template-argument:
8961 assignment-expression
8962 type-id
8963 id-expression
8965 The representation is that of an assignment-expression, type-id, or
8966 id-expression -- except that the qualified id-expression is
8967 evaluated, so that the value returned is either a DECL or an
8968 OVERLOAD.
8970 Although the standard says "assignment-expression", it forbids
8971 throw-expressions or assignments in the template argument.
8972 Therefore, we use "conditional-expression" instead. */
8974 static tree
8975 cp_parser_template_argument (cp_parser* parser)
8977 tree argument;
8978 bool template_p;
8979 bool address_p;
8980 bool maybe_type_id = false;
8981 cp_token *token;
8982 cp_id_kind idk;
8983 tree qualifying_class;
8985 /* There's really no way to know what we're looking at, so we just
8986 try each alternative in order.
8988 [temp.arg]
8990 In a template-argument, an ambiguity between a type-id and an
8991 expression is resolved to a type-id, regardless of the form of
8992 the corresponding template-parameter.
8994 Therefore, we try a type-id first. */
8995 cp_parser_parse_tentatively (parser);
8996 argument = cp_parser_type_id (parser);
8997 /* If there was no error parsing the type-id but the next token is a '>>',
8998 we probably found a typo for '> >'. But there are type-id which are
8999 also valid expressions. For instance:
9001 struct X { int operator >> (int); };
9002 template <int V> struct Foo {};
9003 Foo<X () >> 5> r;
9005 Here 'X()' is a valid type-id of a function type, but the user just
9006 wanted to write the expression "X() >> 5". Thus, we remember that we
9007 found a valid type-id, but we still try to parse the argument as an
9008 expression to see what happens. */
9009 if (!cp_parser_error_occurred (parser)
9010 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9012 maybe_type_id = true;
9013 cp_parser_abort_tentative_parse (parser);
9015 else
9017 /* If the next token isn't a `,' or a `>', then this argument wasn't
9018 really finished. This means that the argument is not a valid
9019 type-id. */
9020 if (!cp_parser_next_token_ends_template_argument_p (parser))
9021 cp_parser_error (parser, "expected template-argument");
9022 /* If that worked, we're done. */
9023 if (cp_parser_parse_definitely (parser))
9024 return argument;
9026 /* We're still not sure what the argument will be. */
9027 cp_parser_parse_tentatively (parser);
9028 /* Try a template. */
9029 argument = cp_parser_id_expression (parser,
9030 /*template_keyword_p=*/false,
9031 /*check_dependency_p=*/true,
9032 &template_p,
9033 /*declarator_p=*/false);
9034 /* If the next token isn't a `,' or a `>', then this argument wasn't
9035 really finished. */
9036 if (!cp_parser_next_token_ends_template_argument_p (parser))
9037 cp_parser_error (parser, "expected template-argument");
9038 if (!cp_parser_error_occurred (parser))
9040 /* Figure out what is being referred to. If the id-expression
9041 was for a class template specialization, then we will have a
9042 TYPE_DECL at this point. There is no need to do name lookup
9043 at this point in that case. */
9044 if (TREE_CODE (argument) != TYPE_DECL)
9045 argument = cp_parser_lookup_name (parser, argument,
9046 none_type,
9047 /*is_template=*/template_p,
9048 /*is_namespace=*/false,
9049 /*check_dependency=*/true,
9050 /*ambiguous_p=*/NULL);
9051 if (TREE_CODE (argument) != TEMPLATE_DECL
9052 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9053 cp_parser_error (parser, "expected template-name");
9055 if (cp_parser_parse_definitely (parser))
9056 return argument;
9057 /* It must be a non-type argument. There permitted cases are given
9058 in [temp.arg.nontype]:
9060 -- an integral constant-expression of integral or enumeration
9061 type; or
9063 -- the name of a non-type template-parameter; or
9065 -- the name of an object or function with external linkage...
9067 -- the address of an object or function with external linkage...
9069 -- a pointer to member... */
9070 /* Look for a non-type template parameter. */
9071 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9073 cp_parser_parse_tentatively (parser);
9074 argument = cp_parser_primary_expression (parser,
9075 /*cast_p=*/false,
9076 &idk,
9077 &qualifying_class);
9078 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9079 || !cp_parser_next_token_ends_template_argument_p (parser))
9080 cp_parser_simulate_error (parser);
9081 if (cp_parser_parse_definitely (parser))
9082 return argument;
9085 /* If the next token is "&", the argument must be the address of an
9086 object or function with external linkage. */
9087 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9088 if (address_p)
9089 cp_lexer_consume_token (parser->lexer);
9090 /* See if we might have an id-expression. */
9091 token = cp_lexer_peek_token (parser->lexer);
9092 if (token->type == CPP_NAME
9093 || token->keyword == RID_OPERATOR
9094 || token->type == CPP_SCOPE
9095 || token->type == CPP_TEMPLATE_ID
9096 || token->type == CPP_NESTED_NAME_SPECIFIER)
9098 cp_parser_parse_tentatively (parser);
9099 argument = cp_parser_primary_expression (parser,
9100 /*cast_p=*/false,
9101 &idk,
9102 &qualifying_class);
9103 if (cp_parser_error_occurred (parser)
9104 || !cp_parser_next_token_ends_template_argument_p (parser))
9105 cp_parser_abort_tentative_parse (parser);
9106 else
9108 if (TREE_CODE (argument) == INDIRECT_REF)
9110 gcc_assert (REFERENCE_REF_P (argument));
9111 argument = TREE_OPERAND (argument, 0);
9114 /* If ADDRESS_P, then we use finish_qualified_id_expr so
9115 that we get a pointer-to-member, if appropriate.
9116 However, if ADDRESS_P is false, we don't want to turn
9117 "T::f" into "(*this).T::f". */
9118 if (qualifying_class && address_p)
9119 argument = finish_qualified_id_expr (qualifying_class,
9120 argument,
9121 /*done=*/true,
9122 /*address_p=*/true);
9123 else if (TREE_CODE (argument) == BASELINK)
9124 /* We don't need the information about what class was used
9125 to name the overloaded functions. */
9126 argument = BASELINK_FUNCTIONS (argument);
9128 if (TREE_CODE (argument) == VAR_DECL)
9130 /* A variable without external linkage might still be a
9131 valid constant-expression, so no error is issued here
9132 if the external-linkage check fails. */
9133 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9134 cp_parser_simulate_error (parser);
9136 else if (is_overloaded_fn (argument))
9137 /* All overloaded functions are allowed; if the external
9138 linkage test does not pass, an error will be issued
9139 later. */
9141 else if (address_p
9142 && (TREE_CODE (argument) == OFFSET_REF
9143 || TREE_CODE (argument) == SCOPE_REF))
9144 /* A pointer-to-member. */
9146 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9148 else
9149 cp_parser_simulate_error (parser);
9151 if (cp_parser_parse_definitely (parser))
9153 if (address_p)
9154 argument = build_x_unary_op (ADDR_EXPR, argument);
9155 return argument;
9159 /* If the argument started with "&", there are no other valid
9160 alternatives at this point. */
9161 if (address_p)
9163 cp_parser_error (parser, "invalid non-type template argument");
9164 return error_mark_node;
9167 /* If the argument wasn't successfully parsed as a type-id followed
9168 by '>>', the argument can only be a constant expression now.
9169 Otherwise, we try parsing the constant-expression tentatively,
9170 because the argument could really be a type-id. */
9171 if (maybe_type_id)
9172 cp_parser_parse_tentatively (parser);
9173 argument = cp_parser_constant_expression (parser,
9174 /*allow_non_constant_p=*/false,
9175 /*non_constant_p=*/NULL);
9176 argument = fold_non_dependent_expr (argument);
9177 if (!maybe_type_id)
9178 return argument;
9179 if (!cp_parser_next_token_ends_template_argument_p (parser))
9180 cp_parser_error (parser, "expected template-argument");
9181 if (cp_parser_parse_definitely (parser))
9182 return argument;
9183 /* We did our best to parse the argument as a non type-id, but that
9184 was the only alternative that matched (albeit with a '>' after
9185 it). We can assume it's just a typo from the user, and a
9186 diagnostic will then be issued. */
9187 return cp_parser_type_id (parser);
9190 /* Parse an explicit-instantiation.
9192 explicit-instantiation:
9193 template declaration
9195 Although the standard says `declaration', what it really means is:
9197 explicit-instantiation:
9198 template decl-specifier-seq [opt] declarator [opt] ;
9200 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9201 supposed to be allowed. A defect report has been filed about this
9202 issue.
9204 GNU Extension:
9206 explicit-instantiation:
9207 storage-class-specifier template
9208 decl-specifier-seq [opt] declarator [opt] ;
9209 function-specifier template
9210 decl-specifier-seq [opt] declarator [opt] ; */
9212 static void
9213 cp_parser_explicit_instantiation (cp_parser* parser)
9215 int declares_class_or_enum;
9216 cp_decl_specifier_seq decl_specifiers;
9217 tree extension_specifier = NULL_TREE;
9219 /* Look for an (optional) storage-class-specifier or
9220 function-specifier. */
9221 if (cp_parser_allow_gnu_extensions_p (parser))
9223 extension_specifier
9224 = cp_parser_storage_class_specifier_opt (parser);
9225 if (!extension_specifier)
9226 extension_specifier
9227 = cp_parser_function_specifier_opt (parser,
9228 /*decl_specs=*/NULL);
9231 /* Look for the `template' keyword. */
9232 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9233 /* Let the front end know that we are processing an explicit
9234 instantiation. */
9235 begin_explicit_instantiation ();
9236 /* [temp.explicit] says that we are supposed to ignore access
9237 control while processing explicit instantiation directives. */
9238 push_deferring_access_checks (dk_no_check);
9239 /* Parse a decl-specifier-seq. */
9240 cp_parser_decl_specifier_seq (parser,
9241 CP_PARSER_FLAGS_OPTIONAL,
9242 &decl_specifiers,
9243 &declares_class_or_enum);
9244 /* If there was exactly one decl-specifier, and it declared a class,
9245 and there's no declarator, then we have an explicit type
9246 instantiation. */
9247 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9249 tree type;
9251 type = check_tag_decl (&decl_specifiers);
9252 /* Turn access control back on for names used during
9253 template instantiation. */
9254 pop_deferring_access_checks ();
9255 if (type)
9256 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9258 else
9260 cp_declarator *declarator;
9261 tree decl;
9263 /* Parse the declarator. */
9264 declarator
9265 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9266 /*ctor_dtor_or_conv_p=*/NULL,
9267 /*parenthesized_p=*/NULL,
9268 /*member_p=*/false);
9269 if (declares_class_or_enum & 2)
9270 cp_parser_check_for_definition_in_return_type (declarator,
9271 decl_specifiers.type);
9272 if (declarator != cp_error_declarator)
9274 decl = grokdeclarator (declarator, &decl_specifiers,
9275 NORMAL, 0, NULL);
9276 /* Turn access control back on for names used during
9277 template instantiation. */
9278 pop_deferring_access_checks ();
9279 /* Do the explicit instantiation. */
9280 do_decl_instantiation (decl, extension_specifier);
9282 else
9284 pop_deferring_access_checks ();
9285 /* Skip the body of the explicit instantiation. */
9286 cp_parser_skip_to_end_of_statement (parser);
9289 /* We're done with the instantiation. */
9290 end_explicit_instantiation ();
9292 cp_parser_consume_semicolon_at_end_of_statement (parser);
9295 /* Parse an explicit-specialization.
9297 explicit-specialization:
9298 template < > declaration
9300 Although the standard says `declaration', what it really means is:
9302 explicit-specialization:
9303 template <> decl-specifier [opt] init-declarator [opt] ;
9304 template <> function-definition
9305 template <> explicit-specialization
9306 template <> template-declaration */
9308 static void
9309 cp_parser_explicit_specialization (cp_parser* parser)
9311 /* Look for the `template' keyword. */
9312 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9313 /* Look for the `<'. */
9314 cp_parser_require (parser, CPP_LESS, "`<'");
9315 /* Look for the `>'. */
9316 cp_parser_require (parser, CPP_GREATER, "`>'");
9317 /* We have processed another parameter list. */
9318 ++parser->num_template_parameter_lists;
9319 /* Let the front end know that we are beginning a specialization. */
9320 begin_specialization ();
9322 /* If the next keyword is `template', we need to figure out whether
9323 or not we're looking a template-declaration. */
9324 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9326 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9327 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9328 cp_parser_template_declaration_after_export (parser,
9329 /*member_p=*/false);
9330 else
9331 cp_parser_explicit_specialization (parser);
9333 else
9334 /* Parse the dependent declaration. */
9335 cp_parser_single_declaration (parser,
9336 /*member_p=*/false,
9337 /*friend_p=*/NULL);
9339 /* We're done with the specialization. */
9340 end_specialization ();
9341 /* We're done with this parameter list. */
9342 --parser->num_template_parameter_lists;
9345 /* Parse a type-specifier.
9347 type-specifier:
9348 simple-type-specifier
9349 class-specifier
9350 enum-specifier
9351 elaborated-type-specifier
9352 cv-qualifier
9354 GNU Extension:
9356 type-specifier:
9357 __complex__
9359 Returns a representation of the type-specifier. For a
9360 class-specifier, enum-specifier, or elaborated-type-specifier, a
9361 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9363 The parser flags FLAGS is used to control type-specifier parsing.
9365 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9366 in a decl-specifier-seq.
9368 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9369 class-specifier, enum-specifier, or elaborated-type-specifier, then
9370 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9371 if a type is declared; 2 if it is defined. Otherwise, it is set to
9372 zero.
9374 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9375 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9376 is set to FALSE. */
9378 static tree
9379 cp_parser_type_specifier (cp_parser* parser,
9380 cp_parser_flags flags,
9381 cp_decl_specifier_seq *decl_specs,
9382 bool is_declaration,
9383 int* declares_class_or_enum,
9384 bool* is_cv_qualifier)
9386 tree type_spec = NULL_TREE;
9387 cp_token *token;
9388 enum rid keyword;
9389 cp_decl_spec ds = ds_last;
9391 /* Assume this type-specifier does not declare a new type. */
9392 if (declares_class_or_enum)
9393 *declares_class_or_enum = 0;
9394 /* And that it does not specify a cv-qualifier. */
9395 if (is_cv_qualifier)
9396 *is_cv_qualifier = false;
9397 /* Peek at the next token. */
9398 token = cp_lexer_peek_token (parser->lexer);
9400 /* If we're looking at a keyword, we can use that to guide the
9401 production we choose. */
9402 keyword = token->keyword;
9403 switch (keyword)
9405 case RID_ENUM:
9406 /* 'enum' [identifier] '{' introduces an enum-specifier;
9407 'enum' <anything else> introduces an elaborated-type-specifier. */
9408 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9409 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9410 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9411 == CPP_OPEN_BRACE))
9413 if (parser->num_template_parameter_lists)
9415 error ("template declaration of %qs", "enum");
9416 cp_parser_skip_to_end_of_block_or_statement (parser);
9417 type_spec = error_mark_node;
9419 else
9420 type_spec = cp_parser_enum_specifier (parser);
9422 if (declares_class_or_enum)
9423 *declares_class_or_enum = 2;
9424 if (decl_specs)
9425 cp_parser_set_decl_spec_type (decl_specs,
9426 type_spec,
9427 /*user_defined_p=*/true);
9428 return type_spec;
9430 else
9431 goto elaborated_type_specifier;
9433 /* Any of these indicate either a class-specifier, or an
9434 elaborated-type-specifier. */
9435 case RID_CLASS:
9436 case RID_STRUCT:
9437 case RID_UNION:
9438 /* Parse tentatively so that we can back up if we don't find a
9439 class-specifier. */
9440 cp_parser_parse_tentatively (parser);
9441 /* Look for the class-specifier. */
9442 type_spec = cp_parser_class_specifier (parser);
9443 /* If that worked, we're done. */
9444 if (cp_parser_parse_definitely (parser))
9446 if (declares_class_or_enum)
9447 *declares_class_or_enum = 2;
9448 if (decl_specs)
9449 cp_parser_set_decl_spec_type (decl_specs,
9450 type_spec,
9451 /*user_defined_p=*/true);
9452 return type_spec;
9455 /* Fall through. */
9456 elaborated_type_specifier:
9457 /* We're declaring (not defining) a class or enum. */
9458 if (declares_class_or_enum)
9459 *declares_class_or_enum = 1;
9461 /* Fall through. */
9462 case RID_TYPENAME:
9463 /* Look for an elaborated-type-specifier. */
9464 type_spec
9465 = (cp_parser_elaborated_type_specifier
9466 (parser,
9467 decl_specs && decl_specs->specs[(int) ds_friend],
9468 is_declaration));
9469 if (decl_specs)
9470 cp_parser_set_decl_spec_type (decl_specs,
9471 type_spec,
9472 /*user_defined_p=*/true);
9473 return type_spec;
9475 case RID_CONST:
9476 ds = ds_const;
9477 if (is_cv_qualifier)
9478 *is_cv_qualifier = true;
9479 break;
9481 case RID_VOLATILE:
9482 ds = ds_volatile;
9483 if (is_cv_qualifier)
9484 *is_cv_qualifier = true;
9485 break;
9487 case RID_RESTRICT:
9488 ds = ds_restrict;
9489 if (is_cv_qualifier)
9490 *is_cv_qualifier = true;
9491 break;
9493 case RID_COMPLEX:
9494 /* The `__complex__' keyword is a GNU extension. */
9495 ds = ds_complex;
9496 break;
9498 default:
9499 break;
9502 /* Handle simple keywords. */
9503 if (ds != ds_last)
9505 if (decl_specs)
9507 ++decl_specs->specs[(int)ds];
9508 decl_specs->any_specifiers_p = true;
9510 return cp_lexer_consume_token (parser->lexer)->value;
9513 /* If we do not already have a type-specifier, assume we are looking
9514 at a simple-type-specifier. */
9515 type_spec = cp_parser_simple_type_specifier (parser,
9516 decl_specs,
9517 flags);
9519 /* If we didn't find a type-specifier, and a type-specifier was not
9520 optional in this context, issue an error message. */
9521 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9523 cp_parser_error (parser, "expected type specifier");
9524 return error_mark_node;
9527 return type_spec;
9530 /* Parse a simple-type-specifier.
9532 simple-type-specifier:
9533 :: [opt] nested-name-specifier [opt] type-name
9534 :: [opt] nested-name-specifier template template-id
9535 char
9536 wchar_t
9537 bool
9538 short
9540 long
9541 signed
9542 unsigned
9543 float
9544 double
9545 void
9547 GNU Extension:
9549 simple-type-specifier:
9550 __typeof__ unary-expression
9551 __typeof__ ( type-id )
9553 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9554 appropriately updated. */
9556 static tree
9557 cp_parser_simple_type_specifier (cp_parser* parser,
9558 cp_decl_specifier_seq *decl_specs,
9559 cp_parser_flags flags)
9561 tree type = NULL_TREE;
9562 cp_token *token;
9564 /* Peek at the next token. */
9565 token = cp_lexer_peek_token (parser->lexer);
9567 /* If we're looking at a keyword, things are easy. */
9568 switch (token->keyword)
9570 case RID_CHAR:
9571 if (decl_specs)
9572 decl_specs->explicit_char_p = true;
9573 type = char_type_node;
9574 break;
9575 case RID_WCHAR:
9576 type = wchar_type_node;
9577 break;
9578 case RID_BOOL:
9579 type = boolean_type_node;
9580 break;
9581 case RID_SHORT:
9582 if (decl_specs)
9583 ++decl_specs->specs[(int) ds_short];
9584 type = short_integer_type_node;
9585 break;
9586 case RID_INT:
9587 if (decl_specs)
9588 decl_specs->explicit_int_p = true;
9589 type = integer_type_node;
9590 break;
9591 case RID_LONG:
9592 if (decl_specs)
9593 ++decl_specs->specs[(int) ds_long];
9594 type = long_integer_type_node;
9595 break;
9596 case RID_SIGNED:
9597 if (decl_specs)
9598 ++decl_specs->specs[(int) ds_signed];
9599 type = integer_type_node;
9600 break;
9601 case RID_UNSIGNED:
9602 if (decl_specs)
9603 ++decl_specs->specs[(int) ds_unsigned];
9604 type = unsigned_type_node;
9605 break;
9606 case RID_FLOAT:
9607 type = float_type_node;
9608 break;
9609 case RID_DOUBLE:
9610 type = double_type_node;
9611 break;
9612 case RID_VOID:
9613 type = void_type_node;
9614 break;
9616 case RID_TYPEOF:
9617 /* Consume the `typeof' token. */
9618 cp_lexer_consume_token (parser->lexer);
9619 /* Parse the operand to `typeof'. */
9620 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9621 /* If it is not already a TYPE, take its type. */
9622 if (!TYPE_P (type))
9623 type = finish_typeof (type);
9625 if (decl_specs)
9626 cp_parser_set_decl_spec_type (decl_specs, type,
9627 /*user_defined_p=*/true);
9629 return type;
9631 default:
9632 break;
9635 /* If the type-specifier was for a built-in type, we're done. */
9636 if (type)
9638 tree id;
9640 /* Record the type. */
9641 if (decl_specs
9642 && (token->keyword != RID_SIGNED
9643 && token->keyword != RID_UNSIGNED
9644 && token->keyword != RID_SHORT
9645 && token->keyword != RID_LONG))
9646 cp_parser_set_decl_spec_type (decl_specs,
9647 type,
9648 /*user_defined=*/false);
9649 if (decl_specs)
9650 decl_specs->any_specifiers_p = true;
9652 /* Consume the token. */
9653 id = cp_lexer_consume_token (parser->lexer)->value;
9655 /* There is no valid C++ program where a non-template type is
9656 followed by a "<". That usually indicates that the user thought
9657 that the type was a template. */
9658 cp_parser_check_for_invalid_template_id (parser, type);
9660 return TYPE_NAME (type);
9663 /* The type-specifier must be a user-defined type. */
9664 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9666 bool qualified_p;
9667 bool global_p;
9669 /* Don't gobble tokens or issue error messages if this is an
9670 optional type-specifier. */
9671 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9672 cp_parser_parse_tentatively (parser);
9674 /* Look for the optional `::' operator. */
9675 global_p
9676 = (cp_parser_global_scope_opt (parser,
9677 /*current_scope_valid_p=*/false)
9678 != NULL_TREE);
9679 /* Look for the nested-name specifier. */
9680 qualified_p
9681 = (cp_parser_nested_name_specifier_opt (parser,
9682 /*typename_keyword_p=*/false,
9683 /*check_dependency_p=*/true,
9684 /*type_p=*/false,
9685 /*is_declaration=*/false)
9686 != NULL_TREE);
9687 /* If we have seen a nested-name-specifier, and the next token
9688 is `template', then we are using the template-id production. */
9689 if (parser->scope
9690 && cp_parser_optional_template_keyword (parser))
9692 /* Look for the template-id. */
9693 type = cp_parser_template_id (parser,
9694 /*template_keyword_p=*/true,
9695 /*check_dependency_p=*/true,
9696 /*is_declaration=*/false);
9697 /* If the template-id did not name a type, we are out of
9698 luck. */
9699 if (TREE_CODE (type) != TYPE_DECL)
9701 cp_parser_error (parser, "expected template-id for type");
9702 type = NULL_TREE;
9705 /* Otherwise, look for a type-name. */
9706 else
9707 type = cp_parser_type_name (parser);
9708 /* Keep track of all name-lookups performed in class scopes. */
9709 if (type
9710 && !global_p
9711 && !qualified_p
9712 && TREE_CODE (type) == TYPE_DECL
9713 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9714 maybe_note_name_used_in_class (DECL_NAME (type), type);
9715 /* If it didn't work out, we don't have a TYPE. */
9716 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9717 && !cp_parser_parse_definitely (parser))
9718 type = NULL_TREE;
9719 if (type && decl_specs)
9720 cp_parser_set_decl_spec_type (decl_specs, type,
9721 /*user_defined=*/true);
9724 /* If we didn't get a type-name, issue an error message. */
9725 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9727 cp_parser_error (parser, "expected type-name");
9728 return error_mark_node;
9731 /* There is no valid C++ program where a non-template type is
9732 followed by a "<". That usually indicates that the user thought
9733 that the type was a template. */
9734 if (type && type != error_mark_node)
9736 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9737 If it is, then the '<'...'>' enclose protocol names rather than
9738 template arguments, and so everything is fine. */
9739 if (c_dialect_objc ()
9740 && (objc_is_id (type) || objc_is_class_name (type)))
9742 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9743 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9745 /* Clobber the "unqualified" type previously entered into
9746 DECL_SPECS with the new, improved protocol-qualified version. */
9747 if (decl_specs)
9748 decl_specs->type = qual_type;
9750 return qual_type;
9753 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9756 return type;
9759 /* Parse a type-name.
9761 type-name:
9762 class-name
9763 enum-name
9764 typedef-name
9766 enum-name:
9767 identifier
9769 typedef-name:
9770 identifier
9772 Returns a TYPE_DECL for the type. */
9774 static tree
9775 cp_parser_type_name (cp_parser* parser)
9777 tree type_decl;
9778 tree identifier;
9780 /* We can't know yet whether it is a class-name or not. */
9781 cp_parser_parse_tentatively (parser);
9782 /* Try a class-name. */
9783 type_decl = cp_parser_class_name (parser,
9784 /*typename_keyword_p=*/false,
9785 /*template_keyword_p=*/false,
9786 none_type,
9787 /*check_dependency_p=*/true,
9788 /*class_head_p=*/false,
9789 /*is_declaration=*/false);
9790 /* If it's not a class-name, keep looking. */
9791 if (!cp_parser_parse_definitely (parser))
9793 /* It must be a typedef-name or an enum-name. */
9794 identifier = cp_parser_identifier (parser);
9795 if (identifier == error_mark_node)
9796 return error_mark_node;
9798 /* Look up the type-name. */
9799 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9801 if (TREE_CODE (type_decl) != TYPE_DECL
9802 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9804 /* See if this is an Objective-C type. */
9805 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9806 tree type = objc_get_protocol_qualified_type (identifier, protos);
9807 if (type)
9808 type_decl = TYPE_NAME (type);
9811 /* Issue an error if we did not find a type-name. */
9812 if (TREE_CODE (type_decl) != TYPE_DECL)
9814 if (!cp_parser_simulate_error (parser))
9815 cp_parser_name_lookup_error (parser, identifier, type_decl,
9816 "is not a type");
9817 type_decl = error_mark_node;
9819 /* Remember that the name was used in the definition of the
9820 current class so that we can check later to see if the
9821 meaning would have been different after the class was
9822 entirely defined. */
9823 else if (type_decl != error_mark_node
9824 && !parser->scope)
9825 maybe_note_name_used_in_class (identifier, type_decl);
9828 return type_decl;
9832 /* Parse an elaborated-type-specifier. Note that the grammar given
9833 here incorporates the resolution to DR68.
9835 elaborated-type-specifier:
9836 class-key :: [opt] nested-name-specifier [opt] identifier
9837 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9838 enum :: [opt] nested-name-specifier [opt] identifier
9839 typename :: [opt] nested-name-specifier identifier
9840 typename :: [opt] nested-name-specifier template [opt]
9841 template-id
9843 GNU extension:
9845 elaborated-type-specifier:
9846 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9847 class-key attributes :: [opt] nested-name-specifier [opt]
9848 template [opt] template-id
9849 enum attributes :: [opt] nested-name-specifier [opt] identifier
9851 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9852 declared `friend'. If IS_DECLARATION is TRUE, then this
9853 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9854 something is being declared.
9856 Returns the TYPE specified. */
9858 static tree
9859 cp_parser_elaborated_type_specifier (cp_parser* parser,
9860 bool is_friend,
9861 bool is_declaration)
9863 enum tag_types tag_type;
9864 tree identifier;
9865 tree type = NULL_TREE;
9866 tree attributes = NULL_TREE;
9868 /* See if we're looking at the `enum' keyword. */
9869 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9871 /* Consume the `enum' token. */
9872 cp_lexer_consume_token (parser->lexer);
9873 /* Remember that it's an enumeration type. */
9874 tag_type = enum_type;
9875 /* Parse the attributes. */
9876 attributes = cp_parser_attributes_opt (parser);
9878 /* Or, it might be `typename'. */
9879 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9880 RID_TYPENAME))
9882 /* Consume the `typename' token. */
9883 cp_lexer_consume_token (parser->lexer);
9884 /* Remember that it's a `typename' type. */
9885 tag_type = typename_type;
9886 /* The `typename' keyword is only allowed in templates. */
9887 if (!processing_template_decl)
9888 pedwarn ("using %<typename%> outside of template");
9890 /* Otherwise it must be a class-key. */
9891 else
9893 tag_type = cp_parser_class_key (parser);
9894 if (tag_type == none_type)
9895 return error_mark_node;
9896 /* Parse the attributes. */
9897 attributes = cp_parser_attributes_opt (parser);
9900 /* Look for the `::' operator. */
9901 cp_parser_global_scope_opt (parser,
9902 /*current_scope_valid_p=*/false);
9903 /* Look for the nested-name-specifier. */
9904 if (tag_type == typename_type)
9906 if (!cp_parser_nested_name_specifier (parser,
9907 /*typename_keyword_p=*/true,
9908 /*check_dependency_p=*/true,
9909 /*type_p=*/true,
9910 is_declaration))
9911 return error_mark_node;
9913 else
9914 /* Even though `typename' is not present, the proposed resolution
9915 to Core Issue 180 says that in `class A<T>::B', `B' should be
9916 considered a type-name, even if `A<T>' is dependent. */
9917 cp_parser_nested_name_specifier_opt (parser,
9918 /*typename_keyword_p=*/true,
9919 /*check_dependency_p=*/true,
9920 /*type_p=*/true,
9921 is_declaration);
9922 /* For everything but enumeration types, consider a template-id. */
9923 if (tag_type != enum_type)
9925 bool template_p = false;
9926 tree decl;
9928 /* Allow the `template' keyword. */
9929 template_p = cp_parser_optional_template_keyword (parser);
9930 /* If we didn't see `template', we don't know if there's a
9931 template-id or not. */
9932 if (!template_p)
9933 cp_parser_parse_tentatively (parser);
9934 /* Parse the template-id. */
9935 decl = cp_parser_template_id (parser, template_p,
9936 /*check_dependency_p=*/true,
9937 is_declaration);
9938 /* If we didn't find a template-id, look for an ordinary
9939 identifier. */
9940 if (!template_p && !cp_parser_parse_definitely (parser))
9942 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9943 in effect, then we must assume that, upon instantiation, the
9944 template will correspond to a class. */
9945 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9946 && tag_type == typename_type)
9947 type = make_typename_type (parser->scope, decl,
9948 typename_type,
9949 /*complain=*/1);
9950 else
9951 type = TREE_TYPE (decl);
9954 /* For an enumeration type, consider only a plain identifier. */
9955 if (!type)
9957 identifier = cp_parser_identifier (parser);
9959 if (identifier == error_mark_node)
9961 parser->scope = NULL_TREE;
9962 return error_mark_node;
9965 /* For a `typename', we needn't call xref_tag. */
9966 if (tag_type == typename_type
9967 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9968 return cp_parser_make_typename_type (parser, parser->scope,
9969 identifier);
9970 /* Look up a qualified name in the usual way. */
9971 if (parser->scope)
9973 tree decl;
9975 decl = cp_parser_lookup_name (parser, identifier,
9976 tag_type,
9977 /*is_template=*/false,
9978 /*is_namespace=*/false,
9979 /*check_dependency=*/true,
9980 /*ambiguous_p=*/NULL);
9982 /* If we are parsing friend declaration, DECL may be a
9983 TEMPLATE_DECL tree node here. However, we need to check
9984 whether this TEMPLATE_DECL results in valid code. Consider
9985 the following example:
9987 namespace N {
9988 template <class T> class C {};
9990 class X {
9991 template <class T> friend class N::C; // #1, valid code
9993 template <class T> class Y {
9994 friend class N::C; // #2, invalid code
9997 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9998 name lookup of `N::C'. We see that friend declaration must
9999 be template for the code to be valid. Note that
10000 processing_template_decl does not work here since it is
10001 always 1 for the above two cases. */
10003 decl = (cp_parser_maybe_treat_template_as_class
10004 (decl, /*tag_name_p=*/is_friend
10005 && parser->num_template_parameter_lists));
10007 if (TREE_CODE (decl) != TYPE_DECL)
10009 cp_parser_diagnose_invalid_type_name (parser,
10010 parser->scope,
10011 identifier);
10012 return error_mark_node;
10015 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10016 check_elaborated_type_specifier
10017 (tag_type, decl,
10018 (parser->num_template_parameter_lists
10019 || DECL_SELF_REFERENCE_P (decl)));
10021 type = TREE_TYPE (decl);
10023 else
10025 /* An elaborated-type-specifier sometimes introduces a new type and
10026 sometimes names an existing type. Normally, the rule is that it
10027 introduces a new type only if there is not an existing type of
10028 the same name already in scope. For example, given:
10030 struct S {};
10031 void f() { struct S s; }
10033 the `struct S' in the body of `f' is the same `struct S' as in
10034 the global scope; the existing definition is used. However, if
10035 there were no global declaration, this would introduce a new
10036 local class named `S'.
10038 An exception to this rule applies to the following code:
10040 namespace N { struct S; }
10042 Here, the elaborated-type-specifier names a new type
10043 unconditionally; even if there is already an `S' in the
10044 containing scope this declaration names a new type.
10045 This exception only applies if the elaborated-type-specifier
10046 forms the complete declaration:
10048 [class.name]
10050 A declaration consisting solely of `class-key identifier ;' is
10051 either a redeclaration of the name in the current scope or a
10052 forward declaration of the identifier as a class name. It
10053 introduces the name into the current scope.
10055 We are in this situation precisely when the next token is a `;'.
10057 An exception to the exception is that a `friend' declaration does
10058 *not* name a new type; i.e., given:
10060 struct S { friend struct T; };
10062 `T' is not a new type in the scope of `S'.
10064 Also, `new struct S' or `sizeof (struct S)' never results in the
10065 definition of a new type; a new type can only be declared in a
10066 declaration context. */
10068 tag_scope ts;
10069 if (is_friend)
10070 /* Friends have special name lookup rules. */
10071 ts = ts_within_enclosing_non_class;
10072 else if (is_declaration
10073 && cp_lexer_next_token_is (parser->lexer,
10074 CPP_SEMICOLON))
10075 /* This is a `class-key identifier ;' */
10076 ts = ts_current;
10077 else
10078 ts = ts_global;
10080 /* Warn about attributes. They are ignored. */
10081 if (attributes)
10082 warning (OPT_Wattributes,
10083 "type attributes are honored only at type definition");
10085 type = xref_tag (tag_type, identifier, ts,
10086 parser->num_template_parameter_lists);
10089 if (tag_type != enum_type)
10090 cp_parser_check_class_key (tag_type, type);
10092 /* A "<" cannot follow an elaborated type specifier. If that
10093 happens, the user was probably trying to form a template-id. */
10094 cp_parser_check_for_invalid_template_id (parser, type);
10096 return type;
10099 /* Parse an enum-specifier.
10101 enum-specifier:
10102 enum identifier [opt] { enumerator-list [opt] }
10104 GNU Extensions:
10105 enum identifier [opt] { enumerator-list [opt] } attributes
10107 Returns an ENUM_TYPE representing the enumeration. */
10109 static tree
10110 cp_parser_enum_specifier (cp_parser* parser)
10112 tree identifier;
10113 tree type;
10115 /* Caller guarantees that the current token is 'enum', an identifier
10116 possibly follows, and the token after that is an opening brace.
10117 If we don't have an identifier, fabricate an anonymous name for
10118 the enumeration being defined. */
10119 cp_lexer_consume_token (parser->lexer);
10121 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10122 identifier = cp_parser_identifier (parser);
10123 else
10124 identifier = make_anon_name ();
10126 /* Issue an error message if type-definitions are forbidden here. */
10127 cp_parser_check_type_definition (parser);
10129 /* Create the new type. We do this before consuming the opening brace
10130 so the enum will be recorded as being on the line of its tag (or the
10131 'enum' keyword, if there is no tag). */
10132 type = start_enum (identifier);
10134 /* Consume the opening brace. */
10135 cp_lexer_consume_token (parser->lexer);
10137 /* If the next token is not '}', then there are some enumerators. */
10138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10139 cp_parser_enumerator_list (parser, type);
10141 /* Consume the final '}'. */
10142 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10144 /* Look for trailing attributes to apply to this enumeration, and
10145 apply them if appropriate. */
10146 if (cp_parser_allow_gnu_extensions_p (parser))
10148 tree trailing_attr = cp_parser_attributes_opt (parser);
10149 cplus_decl_attributes (&type,
10150 trailing_attr,
10151 (int) ATTR_FLAG_TYPE_IN_PLACE);
10154 /* Finish up the enumeration. */
10155 finish_enum (type);
10157 return type;
10160 /* Parse an enumerator-list. The enumerators all have the indicated
10161 TYPE.
10163 enumerator-list:
10164 enumerator-definition
10165 enumerator-list , enumerator-definition */
10167 static void
10168 cp_parser_enumerator_list (cp_parser* parser, tree type)
10170 while (true)
10172 /* Parse an enumerator-definition. */
10173 cp_parser_enumerator_definition (parser, type);
10175 /* If the next token is not a ',', we've reached the end of
10176 the list. */
10177 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10178 break;
10179 /* Otherwise, consume the `,' and keep going. */
10180 cp_lexer_consume_token (parser->lexer);
10181 /* If the next token is a `}', there is a trailing comma. */
10182 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10184 if (pedantic && !in_system_header)
10185 pedwarn ("comma at end of enumerator list");
10186 break;
10191 /* Parse an enumerator-definition. The enumerator has the indicated
10192 TYPE.
10194 enumerator-definition:
10195 enumerator
10196 enumerator = constant-expression
10198 enumerator:
10199 identifier */
10201 static void
10202 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10204 tree identifier;
10205 tree value;
10207 /* Look for the identifier. */
10208 identifier = cp_parser_identifier (parser);
10209 if (identifier == error_mark_node)
10210 return;
10212 /* If the next token is an '=', then there is an explicit value. */
10213 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10215 /* Consume the `=' token. */
10216 cp_lexer_consume_token (parser->lexer);
10217 /* Parse the value. */
10218 value = cp_parser_constant_expression (parser,
10219 /*allow_non_constant_p=*/false,
10220 NULL);
10222 else
10223 value = NULL_TREE;
10225 /* Create the enumerator. */
10226 build_enumerator (identifier, value, type);
10229 /* Parse a namespace-name.
10231 namespace-name:
10232 original-namespace-name
10233 namespace-alias
10235 Returns the NAMESPACE_DECL for the namespace. */
10237 static tree
10238 cp_parser_namespace_name (cp_parser* parser)
10240 tree identifier;
10241 tree namespace_decl;
10243 /* Get the name of the namespace. */
10244 identifier = cp_parser_identifier (parser);
10245 if (identifier == error_mark_node)
10246 return error_mark_node;
10248 /* Look up the identifier in the currently active scope. Look only
10249 for namespaces, due to:
10251 [basic.lookup.udir]
10253 When looking up a namespace-name in a using-directive or alias
10254 definition, only namespace names are considered.
10256 And:
10258 [basic.lookup.qual]
10260 During the lookup of a name preceding the :: scope resolution
10261 operator, object, function, and enumerator names are ignored.
10263 (Note that cp_parser_class_or_namespace_name only calls this
10264 function if the token after the name is the scope resolution
10265 operator.) */
10266 namespace_decl = cp_parser_lookup_name (parser, identifier,
10267 none_type,
10268 /*is_template=*/false,
10269 /*is_namespace=*/true,
10270 /*check_dependency=*/true,
10271 /*ambiguous_p=*/NULL);
10272 /* If it's not a namespace, issue an error. */
10273 if (namespace_decl == error_mark_node
10274 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10276 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10277 error ("%qD is not a namespace-name", identifier);
10278 cp_parser_error (parser, "expected namespace-name");
10279 namespace_decl = error_mark_node;
10282 return namespace_decl;
10285 /* Parse a namespace-definition.
10287 namespace-definition:
10288 named-namespace-definition
10289 unnamed-namespace-definition
10291 named-namespace-definition:
10292 original-namespace-definition
10293 extension-namespace-definition
10295 original-namespace-definition:
10296 namespace identifier { namespace-body }
10298 extension-namespace-definition:
10299 namespace original-namespace-name { namespace-body }
10301 unnamed-namespace-definition:
10302 namespace { namespace-body } */
10304 static void
10305 cp_parser_namespace_definition (cp_parser* parser)
10307 tree identifier;
10309 /* Look for the `namespace' keyword. */
10310 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10312 /* Get the name of the namespace. We do not attempt to distinguish
10313 between an original-namespace-definition and an
10314 extension-namespace-definition at this point. The semantic
10315 analysis routines are responsible for that. */
10316 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10317 identifier = cp_parser_identifier (parser);
10318 else
10319 identifier = NULL_TREE;
10321 /* Look for the `{' to start the namespace. */
10322 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10323 /* Start the namespace. */
10324 push_namespace (identifier);
10325 /* Parse the body of the namespace. */
10326 cp_parser_namespace_body (parser);
10327 /* Finish the namespace. */
10328 pop_namespace ();
10329 /* Look for the final `}'. */
10330 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10333 /* Parse a namespace-body.
10335 namespace-body:
10336 declaration-seq [opt] */
10338 static void
10339 cp_parser_namespace_body (cp_parser* parser)
10341 cp_parser_declaration_seq_opt (parser);
10344 /* Parse a namespace-alias-definition.
10346 namespace-alias-definition:
10347 namespace identifier = qualified-namespace-specifier ; */
10349 static void
10350 cp_parser_namespace_alias_definition (cp_parser* parser)
10352 tree identifier;
10353 tree namespace_specifier;
10355 /* Look for the `namespace' keyword. */
10356 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10357 /* Look for the identifier. */
10358 identifier = cp_parser_identifier (parser);
10359 if (identifier == error_mark_node)
10360 return;
10361 /* Look for the `=' token. */
10362 cp_parser_require (parser, CPP_EQ, "`='");
10363 /* Look for the qualified-namespace-specifier. */
10364 namespace_specifier
10365 = cp_parser_qualified_namespace_specifier (parser);
10366 /* Look for the `;' token. */
10367 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10369 /* Register the alias in the symbol table. */
10370 do_namespace_alias (identifier, namespace_specifier);
10373 /* Parse a qualified-namespace-specifier.
10375 qualified-namespace-specifier:
10376 :: [opt] nested-name-specifier [opt] namespace-name
10378 Returns a NAMESPACE_DECL corresponding to the specified
10379 namespace. */
10381 static tree
10382 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10384 /* Look for the optional `::'. */
10385 cp_parser_global_scope_opt (parser,
10386 /*current_scope_valid_p=*/false);
10388 /* Look for the optional nested-name-specifier. */
10389 cp_parser_nested_name_specifier_opt (parser,
10390 /*typename_keyword_p=*/false,
10391 /*check_dependency_p=*/true,
10392 /*type_p=*/false,
10393 /*is_declaration=*/true);
10395 return cp_parser_namespace_name (parser);
10398 /* Parse a using-declaration.
10400 using-declaration:
10401 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10402 using :: unqualified-id ; */
10404 static void
10405 cp_parser_using_declaration (cp_parser* parser)
10407 cp_token *token;
10408 bool typename_p = false;
10409 bool global_scope_p;
10410 tree decl;
10411 tree identifier;
10412 tree qscope;
10414 /* Look for the `using' keyword. */
10415 cp_parser_require_keyword (parser, RID_USING, "`using'");
10417 /* Peek at the next token. */
10418 token = cp_lexer_peek_token (parser->lexer);
10419 /* See if it's `typename'. */
10420 if (token->keyword == RID_TYPENAME)
10422 /* Remember that we've seen it. */
10423 typename_p = true;
10424 /* Consume the `typename' token. */
10425 cp_lexer_consume_token (parser->lexer);
10428 /* Look for the optional global scope qualification. */
10429 global_scope_p
10430 = (cp_parser_global_scope_opt (parser,
10431 /*current_scope_valid_p=*/false)
10432 != NULL_TREE);
10434 /* If we saw `typename', or didn't see `::', then there must be a
10435 nested-name-specifier present. */
10436 if (typename_p || !global_scope_p)
10437 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10438 /*check_dependency_p=*/true,
10439 /*type_p=*/false,
10440 /*is_declaration=*/true);
10441 /* Otherwise, we could be in either of the two productions. In that
10442 case, treat the nested-name-specifier as optional. */
10443 else
10444 qscope = cp_parser_nested_name_specifier_opt (parser,
10445 /*typename_keyword_p=*/false,
10446 /*check_dependency_p=*/true,
10447 /*type_p=*/false,
10448 /*is_declaration=*/true);
10449 if (!qscope)
10450 qscope = global_namespace;
10452 /* Parse the unqualified-id. */
10453 identifier = cp_parser_unqualified_id (parser,
10454 /*template_keyword_p=*/false,
10455 /*check_dependency_p=*/true,
10456 /*declarator_p=*/true);
10458 /* The function we call to handle a using-declaration is different
10459 depending on what scope we are in. */
10460 if (identifier == error_mark_node)
10462 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10463 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10464 /* [namespace.udecl]
10466 A using declaration shall not name a template-id. */
10467 error ("a template-id may not appear in a using-declaration");
10468 else
10470 if (at_class_scope_p ())
10472 /* Create the USING_DECL. */
10473 decl = do_class_using_decl (parser->scope, identifier);
10474 /* Add it to the list of members in this class. */
10475 finish_member_declaration (decl);
10477 else
10479 decl = cp_parser_lookup_name_simple (parser, identifier);
10480 if (decl == error_mark_node)
10481 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10482 else if (!at_namespace_scope_p ())
10483 do_local_using_decl (decl, qscope, identifier);
10484 else
10485 do_toplevel_using_decl (decl, qscope, identifier);
10489 /* Look for the final `;'. */
10490 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10493 /* Parse a using-directive.
10495 using-directive:
10496 using namespace :: [opt] nested-name-specifier [opt]
10497 namespace-name ; */
10499 static void
10500 cp_parser_using_directive (cp_parser* parser)
10502 tree namespace_decl;
10503 tree attribs;
10505 /* Look for the `using' keyword. */
10506 cp_parser_require_keyword (parser, RID_USING, "`using'");
10507 /* And the `namespace' keyword. */
10508 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10509 /* Look for the optional `::' operator. */
10510 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10511 /* And the optional nested-name-specifier. */
10512 cp_parser_nested_name_specifier_opt (parser,
10513 /*typename_keyword_p=*/false,
10514 /*check_dependency_p=*/true,
10515 /*type_p=*/false,
10516 /*is_declaration=*/true);
10517 /* Get the namespace being used. */
10518 namespace_decl = cp_parser_namespace_name (parser);
10519 /* And any specified attributes. */
10520 attribs = cp_parser_attributes_opt (parser);
10521 /* Update the symbol table. */
10522 parse_using_directive (namespace_decl, attribs);
10523 /* Look for the final `;'. */
10524 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10527 /* Parse an asm-definition.
10529 asm-definition:
10530 asm ( string-literal ) ;
10532 GNU Extension:
10534 asm-definition:
10535 asm volatile [opt] ( string-literal ) ;
10536 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10537 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10538 : asm-operand-list [opt] ) ;
10539 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10540 : asm-operand-list [opt]
10541 : asm-operand-list [opt] ) ; */
10543 static void
10544 cp_parser_asm_definition (cp_parser* parser)
10546 tree string;
10547 tree outputs = NULL_TREE;
10548 tree inputs = NULL_TREE;
10549 tree clobbers = NULL_TREE;
10550 tree asm_stmt;
10551 bool volatile_p = false;
10552 bool extended_p = false;
10554 /* Look for the `asm' keyword. */
10555 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10556 /* See if the next token is `volatile'. */
10557 if (cp_parser_allow_gnu_extensions_p (parser)
10558 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10560 /* Remember that we saw the `volatile' keyword. */
10561 volatile_p = true;
10562 /* Consume the token. */
10563 cp_lexer_consume_token (parser->lexer);
10565 /* Look for the opening `('. */
10566 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10567 return;
10568 /* Look for the string. */
10569 string = cp_parser_string_literal (parser, false, false);
10570 if (string == error_mark_node)
10572 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10573 /*consume_paren=*/true);
10574 return;
10577 /* If we're allowing GNU extensions, check for the extended assembly
10578 syntax. Unfortunately, the `:' tokens need not be separated by
10579 a space in C, and so, for compatibility, we tolerate that here
10580 too. Doing that means that we have to treat the `::' operator as
10581 two `:' tokens. */
10582 if (cp_parser_allow_gnu_extensions_p (parser)
10583 && at_function_scope_p ()
10584 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10585 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10587 bool inputs_p = false;
10588 bool clobbers_p = false;
10590 /* The extended syntax was used. */
10591 extended_p = true;
10593 /* Look for outputs. */
10594 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10596 /* Consume the `:'. */
10597 cp_lexer_consume_token (parser->lexer);
10598 /* Parse the output-operands. */
10599 if (cp_lexer_next_token_is_not (parser->lexer,
10600 CPP_COLON)
10601 && cp_lexer_next_token_is_not (parser->lexer,
10602 CPP_SCOPE)
10603 && cp_lexer_next_token_is_not (parser->lexer,
10604 CPP_CLOSE_PAREN))
10605 outputs = cp_parser_asm_operand_list (parser);
10607 /* If the next token is `::', there are no outputs, and the
10608 next token is the beginning of the inputs. */
10609 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10610 /* The inputs are coming next. */
10611 inputs_p = true;
10613 /* Look for inputs. */
10614 if (inputs_p
10615 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10617 /* Consume the `:' or `::'. */
10618 cp_lexer_consume_token (parser->lexer);
10619 /* Parse the output-operands. */
10620 if (cp_lexer_next_token_is_not (parser->lexer,
10621 CPP_COLON)
10622 && cp_lexer_next_token_is_not (parser->lexer,
10623 CPP_CLOSE_PAREN))
10624 inputs = cp_parser_asm_operand_list (parser);
10626 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10627 /* The clobbers are coming next. */
10628 clobbers_p = true;
10630 /* Look for clobbers. */
10631 if (clobbers_p
10632 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10634 /* Consume the `:' or `::'. */
10635 cp_lexer_consume_token (parser->lexer);
10636 /* Parse the clobbers. */
10637 if (cp_lexer_next_token_is_not (parser->lexer,
10638 CPP_CLOSE_PAREN))
10639 clobbers = cp_parser_asm_clobber_list (parser);
10642 /* Look for the closing `)'. */
10643 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10644 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10645 /*consume_paren=*/true);
10646 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10648 /* Create the ASM_EXPR. */
10649 if (at_function_scope_p ())
10651 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10652 inputs, clobbers);
10653 /* If the extended syntax was not used, mark the ASM_EXPR. */
10654 if (!extended_p)
10656 tree temp = asm_stmt;
10657 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10658 temp = TREE_OPERAND (temp, 0);
10660 ASM_INPUT_P (temp) = 1;
10663 else
10664 assemble_asm (string);
10667 /* Declarators [gram.dcl.decl] */
10669 /* Parse an init-declarator.
10671 init-declarator:
10672 declarator initializer [opt]
10674 GNU Extension:
10676 init-declarator:
10677 declarator asm-specification [opt] attributes [opt] initializer [opt]
10679 function-definition:
10680 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10681 function-body
10682 decl-specifier-seq [opt] declarator function-try-block
10684 GNU Extension:
10686 function-definition:
10687 __extension__ function-definition
10689 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10690 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10691 then this declarator appears in a class scope. The new DECL created
10692 by this declarator is returned.
10694 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10695 for a function-definition here as well. If the declarator is a
10696 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10697 be TRUE upon return. By that point, the function-definition will
10698 have been completely parsed.
10700 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10701 is FALSE. */
10703 static tree
10704 cp_parser_init_declarator (cp_parser* parser,
10705 cp_decl_specifier_seq *decl_specifiers,
10706 bool function_definition_allowed_p,
10707 bool member_p,
10708 int declares_class_or_enum,
10709 bool* function_definition_p)
10711 cp_token *token;
10712 cp_declarator *declarator;
10713 tree prefix_attributes;
10714 tree attributes;
10715 tree asm_specification;
10716 tree initializer;
10717 tree decl = NULL_TREE;
10718 tree scope;
10719 bool is_initialized;
10720 bool is_parenthesized_init;
10721 bool is_non_constant_init;
10722 int ctor_dtor_or_conv_p;
10723 bool friend_p;
10724 tree pushed_scope = NULL;
10726 /* Gather the attributes that were provided with the
10727 decl-specifiers. */
10728 prefix_attributes = decl_specifiers->attributes;
10730 /* Assume that this is not the declarator for a function
10731 definition. */
10732 if (function_definition_p)
10733 *function_definition_p = false;
10735 /* Defer access checks while parsing the declarator; we cannot know
10736 what names are accessible until we know what is being
10737 declared. */
10738 resume_deferring_access_checks ();
10740 /* Parse the declarator. */
10741 declarator
10742 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10743 &ctor_dtor_or_conv_p,
10744 /*parenthesized_p=*/NULL,
10745 /*member_p=*/false);
10746 /* Gather up the deferred checks. */
10747 stop_deferring_access_checks ();
10749 /* If the DECLARATOR was erroneous, there's no need to go
10750 further. */
10751 if (declarator == cp_error_declarator)
10752 return error_mark_node;
10754 if (declares_class_or_enum & 2)
10755 cp_parser_check_for_definition_in_return_type (declarator,
10756 decl_specifiers->type);
10758 /* Figure out what scope the entity declared by the DECLARATOR is
10759 located in. `grokdeclarator' sometimes changes the scope, so
10760 we compute it now. */
10761 scope = get_scope_of_declarator (declarator);
10763 /* If we're allowing GNU extensions, look for an asm-specification
10764 and attributes. */
10765 if (cp_parser_allow_gnu_extensions_p (parser))
10767 /* Look for an asm-specification. */
10768 asm_specification = cp_parser_asm_specification_opt (parser);
10769 /* And attributes. */
10770 attributes = cp_parser_attributes_opt (parser);
10772 else
10774 asm_specification = NULL_TREE;
10775 attributes = NULL_TREE;
10778 /* Peek at the next token. */
10779 token = cp_lexer_peek_token (parser->lexer);
10780 /* Check to see if the token indicates the start of a
10781 function-definition. */
10782 if (cp_parser_token_starts_function_definition_p (token))
10784 if (!function_definition_allowed_p)
10786 /* If a function-definition should not appear here, issue an
10787 error message. */
10788 cp_parser_error (parser,
10789 "a function-definition is not allowed here");
10790 return error_mark_node;
10792 else
10794 /* Neither attributes nor an asm-specification are allowed
10795 on a function-definition. */
10796 if (asm_specification)
10797 error ("an asm-specification is not allowed on a function-definition");
10798 if (attributes)
10799 error ("attributes are not allowed on a function-definition");
10800 /* This is a function-definition. */
10801 *function_definition_p = true;
10803 /* Parse the function definition. */
10804 if (member_p)
10805 decl = cp_parser_save_member_function_body (parser,
10806 decl_specifiers,
10807 declarator,
10808 prefix_attributes);
10809 else
10810 decl
10811 = (cp_parser_function_definition_from_specifiers_and_declarator
10812 (parser, decl_specifiers, prefix_attributes, declarator));
10814 return decl;
10818 /* [dcl.dcl]
10820 Only in function declarations for constructors, destructors, and
10821 type conversions can the decl-specifier-seq be omitted.
10823 We explicitly postpone this check past the point where we handle
10824 function-definitions because we tolerate function-definitions
10825 that are missing their return types in some modes. */
10826 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10828 cp_parser_error (parser,
10829 "expected constructor, destructor, or type conversion");
10830 return error_mark_node;
10833 /* An `=' or an `(' indicates an initializer. */
10834 is_initialized = (token->type == CPP_EQ
10835 || token->type == CPP_OPEN_PAREN);
10836 /* If the init-declarator isn't initialized and isn't followed by a
10837 `,' or `;', it's not a valid init-declarator. */
10838 if (!is_initialized
10839 && token->type != CPP_COMMA
10840 && token->type != CPP_SEMICOLON)
10842 cp_parser_error (parser, "expected initializer");
10843 return error_mark_node;
10846 /* Because start_decl has side-effects, we should only call it if we
10847 know we're going ahead. By this point, we know that we cannot
10848 possibly be looking at any other construct. */
10849 cp_parser_commit_to_tentative_parse (parser);
10851 /* If the decl specifiers were bad, issue an error now that we're
10852 sure this was intended to be a declarator. Then continue
10853 declaring the variable(s), as int, to try to cut down on further
10854 errors. */
10855 if (decl_specifiers->any_specifiers_p
10856 && decl_specifiers->type == error_mark_node)
10858 cp_parser_error (parser, "invalid type in declaration");
10859 decl_specifiers->type = integer_type_node;
10862 /* Check to see whether or not this declaration is a friend. */
10863 friend_p = cp_parser_friend_p (decl_specifiers);
10865 /* Check that the number of template-parameter-lists is OK. */
10866 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10867 return error_mark_node;
10869 /* Enter the newly declared entry in the symbol table. If we're
10870 processing a declaration in a class-specifier, we wait until
10871 after processing the initializer. */
10872 if (!member_p)
10874 if (parser->in_unbraced_linkage_specification_p)
10876 decl_specifiers->storage_class = sc_extern;
10877 have_extern_spec = false;
10879 decl = start_decl (declarator, decl_specifiers,
10880 is_initialized, attributes, prefix_attributes,
10881 &pushed_scope);
10883 else if (scope)
10884 /* Enter the SCOPE. That way unqualified names appearing in the
10885 initializer will be looked up in SCOPE. */
10886 pushed_scope = push_scope (scope);
10888 /* Perform deferred access control checks, now that we know in which
10889 SCOPE the declared entity resides. */
10890 if (!member_p && decl)
10892 tree saved_current_function_decl = NULL_TREE;
10894 /* If the entity being declared is a function, pretend that we
10895 are in its scope. If it is a `friend', it may have access to
10896 things that would not otherwise be accessible. */
10897 if (TREE_CODE (decl) == FUNCTION_DECL)
10899 saved_current_function_decl = current_function_decl;
10900 current_function_decl = decl;
10903 /* Perform the access control checks for the declarator and the
10904 the decl-specifiers. */
10905 perform_deferred_access_checks ();
10907 /* Restore the saved value. */
10908 if (TREE_CODE (decl) == FUNCTION_DECL)
10909 current_function_decl = saved_current_function_decl;
10912 /* Parse the initializer. */
10913 if (is_initialized)
10914 initializer = cp_parser_initializer (parser,
10915 &is_parenthesized_init,
10916 &is_non_constant_init);
10917 else
10919 initializer = NULL_TREE;
10920 is_parenthesized_init = false;
10921 is_non_constant_init = true;
10924 /* The old parser allows attributes to appear after a parenthesized
10925 initializer. Mark Mitchell proposed removing this functionality
10926 on the GCC mailing lists on 2002-08-13. This parser accepts the
10927 attributes -- but ignores them. */
10928 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10929 if (cp_parser_attributes_opt (parser))
10930 warning (OPT_Wattributes,
10931 "attributes after parenthesized initializer ignored");
10933 /* For an in-class declaration, use `grokfield' to create the
10934 declaration. */
10935 if (member_p)
10937 if (pushed_scope)
10939 pop_scope (pushed_scope);
10940 pushed_scope = false;
10942 decl = grokfield (declarator, decl_specifiers,
10943 initializer, /*asmspec=*/NULL_TREE,
10944 /*attributes=*/NULL_TREE);
10945 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10946 cp_parser_save_default_args (parser, decl);
10949 /* Finish processing the declaration. But, skip friend
10950 declarations. */
10951 if (!friend_p && decl && decl != error_mark_node)
10953 cp_finish_decl (decl,
10954 initializer,
10955 asm_specification,
10956 /* If the initializer is in parentheses, then this is
10957 a direct-initialization, which means that an
10958 `explicit' constructor is OK. Otherwise, an
10959 `explicit' constructor cannot be used. */
10960 ((is_parenthesized_init || !is_initialized)
10961 ? 0 : LOOKUP_ONLYCONVERTING));
10963 if (!friend_p && pushed_scope)
10964 pop_scope (pushed_scope);
10966 /* Remember whether or not variables were initialized by
10967 constant-expressions. */
10968 if (decl && TREE_CODE (decl) == VAR_DECL
10969 && is_initialized && !is_non_constant_init)
10970 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10972 return decl;
10975 /* Parse a declarator.
10977 declarator:
10978 direct-declarator
10979 ptr-operator declarator
10981 abstract-declarator:
10982 ptr-operator abstract-declarator [opt]
10983 direct-abstract-declarator
10985 GNU Extensions:
10987 declarator:
10988 attributes [opt] direct-declarator
10989 attributes [opt] ptr-operator declarator
10991 abstract-declarator:
10992 attributes [opt] ptr-operator abstract-declarator [opt]
10993 attributes [opt] direct-abstract-declarator
10995 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10996 detect constructor, destructor or conversion operators. It is set
10997 to -1 if the declarator is a name, and +1 if it is a
10998 function. Otherwise it is set to zero. Usually you just want to
10999 test for >0, but internally the negative value is used.
11001 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11002 a decl-specifier-seq unless it declares a constructor, destructor,
11003 or conversion. It might seem that we could check this condition in
11004 semantic analysis, rather than parsing, but that makes it difficult
11005 to handle something like `f()'. We want to notice that there are
11006 no decl-specifiers, and therefore realize that this is an
11007 expression, not a declaration.)
11009 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11010 the declarator is a direct-declarator of the form "(...)".
11012 MEMBER_P is true iff this declarator is a member-declarator. */
11014 static cp_declarator *
11015 cp_parser_declarator (cp_parser* parser,
11016 cp_parser_declarator_kind dcl_kind,
11017 int* ctor_dtor_or_conv_p,
11018 bool* parenthesized_p,
11019 bool member_p)
11021 cp_token *token;
11022 cp_declarator *declarator;
11023 enum tree_code code;
11024 cp_cv_quals cv_quals;
11025 tree class_type;
11026 tree attributes = NULL_TREE;
11028 /* Assume this is not a constructor, destructor, or type-conversion
11029 operator. */
11030 if (ctor_dtor_or_conv_p)
11031 *ctor_dtor_or_conv_p = 0;
11033 if (cp_parser_allow_gnu_extensions_p (parser))
11034 attributes = cp_parser_attributes_opt (parser);
11036 /* Peek at the next token. */
11037 token = cp_lexer_peek_token (parser->lexer);
11039 /* Check for the ptr-operator production. */
11040 cp_parser_parse_tentatively (parser);
11041 /* Parse the ptr-operator. */
11042 code = cp_parser_ptr_operator (parser,
11043 &class_type,
11044 &cv_quals);
11045 /* If that worked, then we have a ptr-operator. */
11046 if (cp_parser_parse_definitely (parser))
11048 /* If a ptr-operator was found, then this declarator was not
11049 parenthesized. */
11050 if (parenthesized_p)
11051 *parenthesized_p = true;
11052 /* The dependent declarator is optional if we are parsing an
11053 abstract-declarator. */
11054 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11055 cp_parser_parse_tentatively (parser);
11057 /* Parse the dependent declarator. */
11058 declarator = cp_parser_declarator (parser, dcl_kind,
11059 /*ctor_dtor_or_conv_p=*/NULL,
11060 /*parenthesized_p=*/NULL,
11061 /*member_p=*/false);
11063 /* If we are parsing an abstract-declarator, we must handle the
11064 case where the dependent declarator is absent. */
11065 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11066 && !cp_parser_parse_definitely (parser))
11067 declarator = NULL;
11069 /* Build the representation of the ptr-operator. */
11070 if (class_type)
11071 declarator = make_ptrmem_declarator (cv_quals,
11072 class_type,
11073 declarator);
11074 else if (code == INDIRECT_REF)
11075 declarator = make_pointer_declarator (cv_quals, declarator);
11076 else
11077 declarator = make_reference_declarator (cv_quals, declarator);
11079 /* Everything else is a direct-declarator. */
11080 else
11082 if (parenthesized_p)
11083 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11084 CPP_OPEN_PAREN);
11085 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11086 ctor_dtor_or_conv_p,
11087 member_p);
11090 if (attributes && declarator != cp_error_declarator)
11091 declarator->attributes = attributes;
11093 return declarator;
11096 /* Parse a direct-declarator or direct-abstract-declarator.
11098 direct-declarator:
11099 declarator-id
11100 direct-declarator ( parameter-declaration-clause )
11101 cv-qualifier-seq [opt]
11102 exception-specification [opt]
11103 direct-declarator [ constant-expression [opt] ]
11104 ( declarator )
11106 direct-abstract-declarator:
11107 direct-abstract-declarator [opt]
11108 ( parameter-declaration-clause )
11109 cv-qualifier-seq [opt]
11110 exception-specification [opt]
11111 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11112 ( abstract-declarator )
11114 Returns a representation of the declarator. DCL_KIND is
11115 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11116 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11117 we are parsing a direct-declarator. It is
11118 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11119 of ambiguity we prefer an abstract declarator, as per
11120 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11121 cp_parser_declarator. */
11123 static cp_declarator *
11124 cp_parser_direct_declarator (cp_parser* parser,
11125 cp_parser_declarator_kind dcl_kind,
11126 int* ctor_dtor_or_conv_p,
11127 bool member_p)
11129 cp_token *token;
11130 cp_declarator *declarator = NULL;
11131 tree scope = NULL_TREE;
11132 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11133 bool saved_in_declarator_p = parser->in_declarator_p;
11134 bool first = true;
11135 tree pushed_scope = NULL_TREE;
11137 while (true)
11139 /* Peek at the next token. */
11140 token = cp_lexer_peek_token (parser->lexer);
11141 if (token->type == CPP_OPEN_PAREN)
11143 /* This is either a parameter-declaration-clause, or a
11144 parenthesized declarator. When we know we are parsing a
11145 named declarator, it must be a parenthesized declarator
11146 if FIRST is true. For instance, `(int)' is a
11147 parameter-declaration-clause, with an omitted
11148 direct-abstract-declarator. But `((*))', is a
11149 parenthesized abstract declarator. Finally, when T is a
11150 template parameter `(T)' is a
11151 parameter-declaration-clause, and not a parenthesized
11152 named declarator.
11154 We first try and parse a parameter-declaration-clause,
11155 and then try a nested declarator (if FIRST is true).
11157 It is not an error for it not to be a
11158 parameter-declaration-clause, even when FIRST is
11159 false. Consider,
11161 int i (int);
11162 int i (3);
11164 The first is the declaration of a function while the
11165 second is a the definition of a variable, including its
11166 initializer.
11168 Having seen only the parenthesis, we cannot know which of
11169 these two alternatives should be selected. Even more
11170 complex are examples like:
11172 int i (int (a));
11173 int i (int (3));
11175 The former is a function-declaration; the latter is a
11176 variable initialization.
11178 Thus again, we try a parameter-declaration-clause, and if
11179 that fails, we back out and return. */
11181 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11183 cp_parameter_declarator *params;
11184 unsigned saved_num_template_parameter_lists;
11186 /* In a member-declarator, the only valid interpretation
11187 of a parenthesis is the start of a
11188 parameter-declaration-clause. (It is invalid to
11189 initialize a static data member with a parenthesized
11190 initializer; only the "=" form of initialization is
11191 permitted.) */
11192 if (!member_p)
11193 cp_parser_parse_tentatively (parser);
11195 /* Consume the `('. */
11196 cp_lexer_consume_token (parser->lexer);
11197 if (first)
11199 /* If this is going to be an abstract declarator, we're
11200 in a declarator and we can't have default args. */
11201 parser->default_arg_ok_p = false;
11202 parser->in_declarator_p = true;
11205 /* Inside the function parameter list, surrounding
11206 template-parameter-lists do not apply. */
11207 saved_num_template_parameter_lists
11208 = parser->num_template_parameter_lists;
11209 parser->num_template_parameter_lists = 0;
11211 /* Parse the parameter-declaration-clause. */
11212 params = cp_parser_parameter_declaration_clause (parser);
11214 parser->num_template_parameter_lists
11215 = saved_num_template_parameter_lists;
11217 /* If all went well, parse the cv-qualifier-seq and the
11218 exception-specification. */
11219 if (member_p || cp_parser_parse_definitely (parser))
11221 cp_cv_quals cv_quals;
11222 tree exception_specification;
11224 if (ctor_dtor_or_conv_p)
11225 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11226 first = false;
11227 /* Consume the `)'. */
11228 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11230 /* Parse the cv-qualifier-seq. */
11231 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11232 /* And the exception-specification. */
11233 exception_specification
11234 = cp_parser_exception_specification_opt (parser);
11236 /* Create the function-declarator. */
11237 declarator = make_call_declarator (declarator,
11238 params,
11239 cv_quals,
11240 exception_specification);
11241 /* Any subsequent parameter lists are to do with
11242 return type, so are not those of the declared
11243 function. */
11244 parser->default_arg_ok_p = false;
11246 /* Repeat the main loop. */
11247 continue;
11251 /* If this is the first, we can try a parenthesized
11252 declarator. */
11253 if (first)
11255 bool saved_in_type_id_in_expr_p;
11257 parser->default_arg_ok_p = saved_default_arg_ok_p;
11258 parser->in_declarator_p = saved_in_declarator_p;
11260 /* Consume the `('. */
11261 cp_lexer_consume_token (parser->lexer);
11262 /* Parse the nested declarator. */
11263 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11264 parser->in_type_id_in_expr_p = true;
11265 declarator
11266 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11267 /*parenthesized_p=*/NULL,
11268 member_p);
11269 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11270 first = false;
11271 /* Expect a `)'. */
11272 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11273 declarator = cp_error_declarator;
11274 if (declarator == cp_error_declarator)
11275 break;
11277 goto handle_declarator;
11279 /* Otherwise, we must be done. */
11280 else
11281 break;
11283 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11284 && token->type == CPP_OPEN_SQUARE)
11286 /* Parse an array-declarator. */
11287 tree bounds;
11289 if (ctor_dtor_or_conv_p)
11290 *ctor_dtor_or_conv_p = 0;
11292 first = false;
11293 parser->default_arg_ok_p = false;
11294 parser->in_declarator_p = true;
11295 /* Consume the `['. */
11296 cp_lexer_consume_token (parser->lexer);
11297 /* Peek at the next token. */
11298 token = cp_lexer_peek_token (parser->lexer);
11299 /* If the next token is `]', then there is no
11300 constant-expression. */
11301 if (token->type != CPP_CLOSE_SQUARE)
11303 bool non_constant_p;
11305 bounds
11306 = cp_parser_constant_expression (parser,
11307 /*allow_non_constant=*/true,
11308 &non_constant_p);
11309 if (!non_constant_p)
11310 bounds = fold_non_dependent_expr (bounds);
11311 /* Normally, the array bound must be an integral constant
11312 expression. However, as an extension, we allow VLAs
11313 in function scopes. */
11314 else if (!at_function_scope_p ())
11316 error ("array bound is not an integer constant");
11317 bounds = error_mark_node;
11320 else
11321 bounds = NULL_TREE;
11322 /* Look for the closing `]'. */
11323 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11325 declarator = cp_error_declarator;
11326 break;
11329 declarator = make_array_declarator (declarator, bounds);
11331 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11333 tree qualifying_scope;
11334 tree unqualified_name;
11336 /* Parse a declarator-id */
11337 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11338 cp_parser_parse_tentatively (parser);
11339 unqualified_name = cp_parser_declarator_id (parser);
11340 qualifying_scope = parser->scope;
11341 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11343 if (!cp_parser_parse_definitely (parser))
11344 unqualified_name = error_mark_node;
11345 else if (qualifying_scope
11346 || (TREE_CODE (unqualified_name)
11347 != IDENTIFIER_NODE))
11349 cp_parser_error (parser, "expected unqualified-id");
11350 unqualified_name = error_mark_node;
11354 if (unqualified_name == error_mark_node)
11356 declarator = cp_error_declarator;
11357 break;
11360 if (qualifying_scope && at_namespace_scope_p ()
11361 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11363 /* In the declaration of a member of a template class
11364 outside of the class itself, the SCOPE will sometimes
11365 be a TYPENAME_TYPE. For example, given:
11367 template <typename T>
11368 int S<T>::R::i = 3;
11370 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11371 this context, we must resolve S<T>::R to an ordinary
11372 type, rather than a typename type.
11374 The reason we normally avoid resolving TYPENAME_TYPEs
11375 is that a specialization of `S' might render
11376 `S<T>::R' not a type. However, if `S' is
11377 specialized, then this `i' will not be used, so there
11378 is no harm in resolving the types here. */
11379 tree type;
11381 /* Resolve the TYPENAME_TYPE. */
11382 type = resolve_typename_type (qualifying_scope,
11383 /*only_current_p=*/false);
11384 /* If that failed, the declarator is invalid. */
11385 if (type == error_mark_node)
11386 error ("%<%T::%D%> is not a type",
11387 TYPE_CONTEXT (qualifying_scope),
11388 TYPE_IDENTIFIER (qualifying_scope));
11389 qualifying_scope = type;
11392 declarator = make_id_declarator (qualifying_scope,
11393 unqualified_name);
11394 declarator->id_loc = token->location;
11395 if (unqualified_name)
11397 tree class_type;
11399 if (qualifying_scope
11400 && CLASS_TYPE_P (qualifying_scope))
11401 class_type = qualifying_scope;
11402 else
11403 class_type = current_class_type;
11405 if (class_type)
11407 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11408 declarator->u.id.sfk = sfk_destructor;
11409 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11410 declarator->u.id.sfk = sfk_conversion;
11411 else if (/* There's no way to declare a constructor
11412 for an anonymous type, even if the type
11413 got a name for linkage purposes. */
11414 !TYPE_WAS_ANONYMOUS (class_type)
11415 && (constructor_name_p (unqualified_name,
11416 class_type)
11417 || (TREE_CODE (unqualified_name) == TYPE_DECL
11418 && (same_type_p
11419 (TREE_TYPE (unqualified_name),
11420 class_type)))))
11421 declarator->u.id.sfk = sfk_constructor;
11423 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11424 *ctor_dtor_or_conv_p = -1;
11425 if (qualifying_scope
11426 && TREE_CODE (unqualified_name) == TYPE_DECL
11427 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11429 error ("invalid use of constructor as a template");
11430 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11431 "the constructor in a qualified name",
11432 class_type,
11433 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11434 class_type, class_type);
11439 handle_declarator:;
11440 scope = get_scope_of_declarator (declarator);
11441 if (scope)
11442 /* Any names that appear after the declarator-id for a
11443 member are looked up in the containing scope. */
11444 pushed_scope = push_scope (scope);
11445 parser->in_declarator_p = true;
11446 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11447 || (declarator && declarator->kind == cdk_id))
11448 /* Default args are only allowed on function
11449 declarations. */
11450 parser->default_arg_ok_p = saved_default_arg_ok_p;
11451 else
11452 parser->default_arg_ok_p = false;
11454 first = false;
11456 /* We're done. */
11457 else
11458 break;
11461 /* For an abstract declarator, we might wind up with nothing at this
11462 point. That's an error; the declarator is not optional. */
11463 if (!declarator)
11464 cp_parser_error (parser, "expected declarator");
11466 /* If we entered a scope, we must exit it now. */
11467 if (pushed_scope)
11468 pop_scope (pushed_scope);
11470 parser->default_arg_ok_p = saved_default_arg_ok_p;
11471 parser->in_declarator_p = saved_in_declarator_p;
11473 return declarator;
11476 /* Parse a ptr-operator.
11478 ptr-operator:
11479 * cv-qualifier-seq [opt]
11481 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11483 GNU Extension:
11485 ptr-operator:
11486 & cv-qualifier-seq [opt]
11488 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11489 Returns ADDR_EXPR if a reference was used. In the case of a
11490 pointer-to-member, *TYPE is filled in with the TYPE containing the
11491 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11492 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11493 ERROR_MARK if an error occurred. */
11495 static enum tree_code
11496 cp_parser_ptr_operator (cp_parser* parser,
11497 tree* type,
11498 cp_cv_quals *cv_quals)
11500 enum tree_code code = ERROR_MARK;
11501 cp_token *token;
11503 /* Assume that it's not a pointer-to-member. */
11504 *type = NULL_TREE;
11505 /* And that there are no cv-qualifiers. */
11506 *cv_quals = TYPE_UNQUALIFIED;
11508 /* Peek at the next token. */
11509 token = cp_lexer_peek_token (parser->lexer);
11510 /* If it's a `*' or `&' we have a pointer or reference. */
11511 if (token->type == CPP_MULT || token->type == CPP_AND)
11513 /* Remember which ptr-operator we were processing. */
11514 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11516 /* Consume the `*' or `&'. */
11517 cp_lexer_consume_token (parser->lexer);
11519 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11520 `&', if we are allowing GNU extensions. (The only qualifier
11521 that can legally appear after `&' is `restrict', but that is
11522 enforced during semantic analysis. */
11523 if (code == INDIRECT_REF
11524 || cp_parser_allow_gnu_extensions_p (parser))
11525 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11527 else
11529 /* Try the pointer-to-member case. */
11530 cp_parser_parse_tentatively (parser);
11531 /* Look for the optional `::' operator. */
11532 cp_parser_global_scope_opt (parser,
11533 /*current_scope_valid_p=*/false);
11534 /* Look for the nested-name specifier. */
11535 cp_parser_nested_name_specifier (parser,
11536 /*typename_keyword_p=*/false,
11537 /*check_dependency_p=*/true,
11538 /*type_p=*/false,
11539 /*is_declaration=*/false);
11540 /* If we found it, and the next token is a `*', then we are
11541 indeed looking at a pointer-to-member operator. */
11542 if (!cp_parser_error_occurred (parser)
11543 && cp_parser_require (parser, CPP_MULT, "`*'"))
11545 /* The type of which the member is a member is given by the
11546 current SCOPE. */
11547 *type = parser->scope;
11548 /* The next name will not be qualified. */
11549 parser->scope = NULL_TREE;
11550 parser->qualifying_scope = NULL_TREE;
11551 parser->object_scope = NULL_TREE;
11552 /* Indicate that the `*' operator was used. */
11553 code = INDIRECT_REF;
11554 /* Look for the optional cv-qualifier-seq. */
11555 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11557 /* If that didn't work we don't have a ptr-operator. */
11558 if (!cp_parser_parse_definitely (parser))
11559 cp_parser_error (parser, "expected ptr-operator");
11562 return code;
11565 /* Parse an (optional) cv-qualifier-seq.
11567 cv-qualifier-seq:
11568 cv-qualifier cv-qualifier-seq [opt]
11570 cv-qualifier:
11571 const
11572 volatile
11574 GNU Extension:
11576 cv-qualifier:
11577 __restrict__
11579 Returns a bitmask representing the cv-qualifiers. */
11581 static cp_cv_quals
11582 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11584 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11586 while (true)
11588 cp_token *token;
11589 cp_cv_quals cv_qualifier;
11591 /* Peek at the next token. */
11592 token = cp_lexer_peek_token (parser->lexer);
11593 /* See if it's a cv-qualifier. */
11594 switch (token->keyword)
11596 case RID_CONST:
11597 cv_qualifier = TYPE_QUAL_CONST;
11598 break;
11600 case RID_VOLATILE:
11601 cv_qualifier = TYPE_QUAL_VOLATILE;
11602 break;
11604 case RID_RESTRICT:
11605 cv_qualifier = TYPE_QUAL_RESTRICT;
11606 break;
11608 default:
11609 cv_qualifier = TYPE_UNQUALIFIED;
11610 break;
11613 if (!cv_qualifier)
11614 break;
11616 if (cv_quals & cv_qualifier)
11618 error ("duplicate cv-qualifier");
11619 cp_lexer_purge_token (parser->lexer);
11621 else
11623 cp_lexer_consume_token (parser->lexer);
11624 cv_quals |= cv_qualifier;
11628 return cv_quals;
11631 /* Parse a declarator-id.
11633 declarator-id:
11634 id-expression
11635 :: [opt] nested-name-specifier [opt] type-name
11637 In the `id-expression' case, the value returned is as for
11638 cp_parser_id_expression if the id-expression was an unqualified-id.
11639 If the id-expression was a qualified-id, then a SCOPE_REF is
11640 returned. The first operand is the scope (either a NAMESPACE_DECL
11641 or TREE_TYPE), but the second is still just a representation of an
11642 unqualified-id. */
11644 static tree
11645 cp_parser_declarator_id (cp_parser* parser)
11647 /* The expression must be an id-expression. Assume that qualified
11648 names are the names of types so that:
11650 template <class T>
11651 int S<T>::R::i = 3;
11653 will work; we must treat `S<T>::R' as the name of a type.
11654 Similarly, assume that qualified names are templates, where
11655 required, so that:
11657 template <class T>
11658 int S<T>::R<T>::i = 3;
11660 will work, too. */
11661 return cp_parser_id_expression (parser,
11662 /*template_keyword_p=*/false,
11663 /*check_dependency_p=*/false,
11664 /*template_p=*/NULL,
11665 /*declarator_p=*/true);
11668 /* Parse a type-id.
11670 type-id:
11671 type-specifier-seq abstract-declarator [opt]
11673 Returns the TYPE specified. */
11675 static tree
11676 cp_parser_type_id (cp_parser* parser)
11678 cp_decl_specifier_seq type_specifier_seq;
11679 cp_declarator *abstract_declarator;
11681 /* Parse the type-specifier-seq. */
11682 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11683 &type_specifier_seq);
11684 if (type_specifier_seq.type == error_mark_node)
11685 return error_mark_node;
11687 /* There might or might not be an abstract declarator. */
11688 cp_parser_parse_tentatively (parser);
11689 /* Look for the declarator. */
11690 abstract_declarator
11691 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11692 /*parenthesized_p=*/NULL,
11693 /*member_p=*/false);
11694 /* Check to see if there really was a declarator. */
11695 if (!cp_parser_parse_definitely (parser))
11696 abstract_declarator = NULL;
11698 return groktypename (&type_specifier_seq, abstract_declarator);
11701 /* Parse a type-specifier-seq.
11703 type-specifier-seq:
11704 type-specifier type-specifier-seq [opt]
11706 GNU extension:
11708 type-specifier-seq:
11709 attributes type-specifier-seq [opt]
11711 If IS_CONDITION is true, we are at the start of a "condition",
11712 e.g., we've just seen "if (".
11714 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11716 static void
11717 cp_parser_type_specifier_seq (cp_parser* parser,
11718 bool is_condition,
11719 cp_decl_specifier_seq *type_specifier_seq)
11721 bool seen_type_specifier = false;
11722 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11724 /* Clear the TYPE_SPECIFIER_SEQ. */
11725 clear_decl_specs (type_specifier_seq);
11727 /* Parse the type-specifiers and attributes. */
11728 while (true)
11730 tree type_specifier;
11731 bool is_cv_qualifier;
11733 /* Check for attributes first. */
11734 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11736 type_specifier_seq->attributes =
11737 chainon (type_specifier_seq->attributes,
11738 cp_parser_attributes_opt (parser));
11739 continue;
11742 /* Look for the type-specifier. */
11743 type_specifier = cp_parser_type_specifier (parser,
11744 flags,
11745 type_specifier_seq,
11746 /*is_declaration=*/false,
11747 NULL,
11748 &is_cv_qualifier);
11749 if (!type_specifier)
11751 /* If the first type-specifier could not be found, this is not a
11752 type-specifier-seq at all. */
11753 if (!seen_type_specifier)
11755 cp_parser_error (parser, "expected type-specifier");
11756 type_specifier_seq->type = error_mark_node;
11757 return;
11759 /* If subsequent type-specifiers could not be found, the
11760 type-specifier-seq is complete. */
11761 break;
11764 seen_type_specifier = true;
11765 /* The standard says that a condition can be:
11767 type-specifier-seq declarator = assignment-expression
11769 However, given:
11771 struct S {};
11772 if (int S = ...)
11774 we should treat the "S" as a declarator, not as a
11775 type-specifier. The standard doesn't say that explicitly for
11776 type-specifier-seq, but it does say that for
11777 decl-specifier-seq in an ordinary declaration. Perhaps it
11778 would be clearer just to allow a decl-specifier-seq here, and
11779 then add a semantic restriction that if any decl-specifiers
11780 that are not type-specifiers appear, the program is invalid. */
11781 if (is_condition && !is_cv_qualifier)
11782 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11785 return;
11788 /* Parse a parameter-declaration-clause.
11790 parameter-declaration-clause:
11791 parameter-declaration-list [opt] ... [opt]
11792 parameter-declaration-list , ...
11794 Returns a representation for the parameter declarations. A return
11795 value of NULL indicates a parameter-declaration-clause consisting
11796 only of an ellipsis. */
11798 static cp_parameter_declarator *
11799 cp_parser_parameter_declaration_clause (cp_parser* parser)
11801 cp_parameter_declarator *parameters;
11802 cp_token *token;
11803 bool ellipsis_p;
11804 bool is_error;
11806 /* Peek at the next token. */
11807 token = cp_lexer_peek_token (parser->lexer);
11808 /* Check for trivial parameter-declaration-clauses. */
11809 if (token->type == CPP_ELLIPSIS)
11811 /* Consume the `...' token. */
11812 cp_lexer_consume_token (parser->lexer);
11813 return NULL;
11815 else if (token->type == CPP_CLOSE_PAREN)
11816 /* There are no parameters. */
11818 #ifndef NO_IMPLICIT_EXTERN_C
11819 if (in_system_header && current_class_type == NULL
11820 && current_lang_name == lang_name_c)
11821 return NULL;
11822 else
11823 #endif
11824 return no_parameters;
11826 /* Check for `(void)', too, which is a special case. */
11827 else if (token->keyword == RID_VOID
11828 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11829 == CPP_CLOSE_PAREN))
11831 /* Consume the `void' token. */
11832 cp_lexer_consume_token (parser->lexer);
11833 /* There are no parameters. */
11834 return no_parameters;
11837 /* Parse the parameter-declaration-list. */
11838 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11839 /* If a parse error occurred while parsing the
11840 parameter-declaration-list, then the entire
11841 parameter-declaration-clause is erroneous. */
11842 if (is_error)
11843 return NULL;
11845 /* Peek at the next token. */
11846 token = cp_lexer_peek_token (parser->lexer);
11847 /* If it's a `,', the clause should terminate with an ellipsis. */
11848 if (token->type == CPP_COMMA)
11850 /* Consume the `,'. */
11851 cp_lexer_consume_token (parser->lexer);
11852 /* Expect an ellipsis. */
11853 ellipsis_p
11854 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11856 /* It might also be `...' if the optional trailing `,' was
11857 omitted. */
11858 else if (token->type == CPP_ELLIPSIS)
11860 /* Consume the `...' token. */
11861 cp_lexer_consume_token (parser->lexer);
11862 /* And remember that we saw it. */
11863 ellipsis_p = true;
11865 else
11866 ellipsis_p = false;
11868 /* Finish the parameter list. */
11869 if (parameters && ellipsis_p)
11870 parameters->ellipsis_p = true;
11872 return parameters;
11875 /* Parse a parameter-declaration-list.
11877 parameter-declaration-list:
11878 parameter-declaration
11879 parameter-declaration-list , parameter-declaration
11881 Returns a representation of the parameter-declaration-list, as for
11882 cp_parser_parameter_declaration_clause. However, the
11883 `void_list_node' is never appended to the list. Upon return,
11884 *IS_ERROR will be true iff an error occurred. */
11886 static cp_parameter_declarator *
11887 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11889 cp_parameter_declarator *parameters = NULL;
11890 cp_parameter_declarator **tail = &parameters;
11892 /* Assume all will go well. */
11893 *is_error = false;
11895 /* Look for more parameters. */
11896 while (true)
11898 cp_parameter_declarator *parameter;
11899 bool parenthesized_p;
11900 /* Parse the parameter. */
11901 parameter
11902 = cp_parser_parameter_declaration (parser,
11903 /*template_parm_p=*/false,
11904 &parenthesized_p);
11906 /* If a parse error occurred parsing the parameter declaration,
11907 then the entire parameter-declaration-list is erroneous. */
11908 if (!parameter)
11910 *is_error = true;
11911 parameters = NULL;
11912 break;
11914 /* Add the new parameter to the list. */
11915 *tail = parameter;
11916 tail = &parameter->next;
11918 /* Peek at the next token. */
11919 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11920 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11921 /* These are for Objective-C++ */
11922 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11923 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11924 /* The parameter-declaration-list is complete. */
11925 break;
11926 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11928 cp_token *token;
11930 /* Peek at the next token. */
11931 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11932 /* If it's an ellipsis, then the list is complete. */
11933 if (token->type == CPP_ELLIPSIS)
11934 break;
11935 /* Otherwise, there must be more parameters. Consume the
11936 `,'. */
11937 cp_lexer_consume_token (parser->lexer);
11938 /* When parsing something like:
11940 int i(float f, double d)
11942 we can tell after seeing the declaration for "f" that we
11943 are not looking at an initialization of a variable "i",
11944 but rather at the declaration of a function "i".
11946 Due to the fact that the parsing of template arguments
11947 (as specified to a template-id) requires backtracking we
11948 cannot use this technique when inside a template argument
11949 list. */
11950 if (!parser->in_template_argument_list_p
11951 && !parser->in_type_id_in_expr_p
11952 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11953 /* However, a parameter-declaration of the form
11954 "foat(f)" (which is a valid declaration of a
11955 parameter "f") can also be interpreted as an
11956 expression (the conversion of "f" to "float"). */
11957 && !parenthesized_p)
11958 cp_parser_commit_to_tentative_parse (parser);
11960 else
11962 cp_parser_error (parser, "expected %<,%> or %<...%>");
11963 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11964 cp_parser_skip_to_closing_parenthesis (parser,
11965 /*recovering=*/true,
11966 /*or_comma=*/false,
11967 /*consume_paren=*/false);
11968 break;
11972 return parameters;
11975 /* Parse a parameter declaration.
11977 parameter-declaration:
11978 decl-specifier-seq declarator
11979 decl-specifier-seq declarator = assignment-expression
11980 decl-specifier-seq abstract-declarator [opt]
11981 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11983 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11984 declares a template parameter. (In that case, a non-nested `>'
11985 token encountered during the parsing of the assignment-expression
11986 is not interpreted as a greater-than operator.)
11988 Returns a representation of the parameter, or NULL if an error
11989 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11990 true iff the declarator is of the form "(p)". */
11992 static cp_parameter_declarator *
11993 cp_parser_parameter_declaration (cp_parser *parser,
11994 bool template_parm_p,
11995 bool *parenthesized_p)
11997 int declares_class_or_enum;
11998 bool greater_than_is_operator_p;
11999 cp_decl_specifier_seq decl_specifiers;
12000 cp_declarator *declarator;
12001 tree default_argument;
12002 cp_token *token;
12003 const char *saved_message;
12005 /* In a template parameter, `>' is not an operator.
12007 [temp.param]
12009 When parsing a default template-argument for a non-type
12010 template-parameter, the first non-nested `>' is taken as the end
12011 of the template parameter-list rather than a greater-than
12012 operator. */
12013 greater_than_is_operator_p = !template_parm_p;
12015 /* Type definitions may not appear in parameter types. */
12016 saved_message = parser->type_definition_forbidden_message;
12017 parser->type_definition_forbidden_message
12018 = "types may not be defined in parameter types";
12020 /* Parse the declaration-specifiers. */
12021 cp_parser_decl_specifier_seq (parser,
12022 CP_PARSER_FLAGS_NONE,
12023 &decl_specifiers,
12024 &declares_class_or_enum);
12025 /* If an error occurred, there's no reason to attempt to parse the
12026 rest of the declaration. */
12027 if (cp_parser_error_occurred (parser))
12029 parser->type_definition_forbidden_message = saved_message;
12030 return NULL;
12033 /* Peek at the next token. */
12034 token = cp_lexer_peek_token (parser->lexer);
12035 /* If the next token is a `)', `,', `=', `>', or `...', then there
12036 is no declarator. */
12037 if (token->type == CPP_CLOSE_PAREN
12038 || token->type == CPP_COMMA
12039 || token->type == CPP_EQ
12040 || token->type == CPP_ELLIPSIS
12041 || token->type == CPP_GREATER)
12043 declarator = NULL;
12044 if (parenthesized_p)
12045 *parenthesized_p = false;
12047 /* Otherwise, there should be a declarator. */
12048 else
12050 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12051 parser->default_arg_ok_p = false;
12053 /* After seeing a decl-specifier-seq, if the next token is not a
12054 "(", there is no possibility that the code is a valid
12055 expression. Therefore, if parsing tentatively, we commit at
12056 this point. */
12057 if (!parser->in_template_argument_list_p
12058 /* In an expression context, having seen:
12060 (int((char ...
12062 we cannot be sure whether we are looking at a
12063 function-type (taking a "char" as a parameter) or a cast
12064 of some object of type "char" to "int". */
12065 && !parser->in_type_id_in_expr_p
12066 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12067 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12068 cp_parser_commit_to_tentative_parse (parser);
12069 /* Parse the declarator. */
12070 declarator = cp_parser_declarator (parser,
12071 CP_PARSER_DECLARATOR_EITHER,
12072 /*ctor_dtor_or_conv_p=*/NULL,
12073 parenthesized_p,
12074 /*member_p=*/false);
12075 parser->default_arg_ok_p = saved_default_arg_ok_p;
12076 /* After the declarator, allow more attributes. */
12077 decl_specifiers.attributes
12078 = chainon (decl_specifiers.attributes,
12079 cp_parser_attributes_opt (parser));
12082 /* The restriction on defining new types applies only to the type
12083 of the parameter, not to the default argument. */
12084 parser->type_definition_forbidden_message = saved_message;
12086 /* If the next token is `=', then process a default argument. */
12087 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12089 bool saved_greater_than_is_operator_p;
12090 /* Consume the `='. */
12091 cp_lexer_consume_token (parser->lexer);
12093 /* If we are defining a class, then the tokens that make up the
12094 default argument must be saved and processed later. */
12095 if (!template_parm_p && at_class_scope_p ()
12096 && TYPE_BEING_DEFINED (current_class_type))
12098 unsigned depth = 0;
12099 cp_token *first_token;
12100 cp_token *token;
12102 /* Add tokens until we have processed the entire default
12103 argument. We add the range [first_token, token). */
12104 first_token = cp_lexer_peek_token (parser->lexer);
12105 while (true)
12107 bool done = false;
12109 /* Peek at the next token. */
12110 token = cp_lexer_peek_token (parser->lexer);
12111 /* What we do depends on what token we have. */
12112 switch (token->type)
12114 /* In valid code, a default argument must be
12115 immediately followed by a `,' `)', or `...'. */
12116 case CPP_COMMA:
12117 case CPP_CLOSE_PAREN:
12118 case CPP_ELLIPSIS:
12119 /* If we run into a non-nested `;', `}', or `]',
12120 then the code is invalid -- but the default
12121 argument is certainly over. */
12122 case CPP_SEMICOLON:
12123 case CPP_CLOSE_BRACE:
12124 case CPP_CLOSE_SQUARE:
12125 if (depth == 0)
12126 done = true;
12127 /* Update DEPTH, if necessary. */
12128 else if (token->type == CPP_CLOSE_PAREN
12129 || token->type == CPP_CLOSE_BRACE
12130 || token->type == CPP_CLOSE_SQUARE)
12131 --depth;
12132 break;
12134 case CPP_OPEN_PAREN:
12135 case CPP_OPEN_SQUARE:
12136 case CPP_OPEN_BRACE:
12137 ++depth;
12138 break;
12140 case CPP_GREATER:
12141 /* If we see a non-nested `>', and `>' is not an
12142 operator, then it marks the end of the default
12143 argument. */
12144 if (!depth && !greater_than_is_operator_p)
12145 done = true;
12146 break;
12148 /* If we run out of tokens, issue an error message. */
12149 case CPP_EOF:
12150 error ("file ends in default argument");
12151 done = true;
12152 break;
12154 case CPP_NAME:
12155 case CPP_SCOPE:
12156 /* In these cases, we should look for template-ids.
12157 For example, if the default argument is
12158 `X<int, double>()', we need to do name lookup to
12159 figure out whether or not `X' is a template; if
12160 so, the `,' does not end the default argument.
12162 That is not yet done. */
12163 break;
12165 default:
12166 break;
12169 /* If we've reached the end, stop. */
12170 if (done)
12171 break;
12173 /* Add the token to the token block. */
12174 token = cp_lexer_consume_token (parser->lexer);
12177 /* Create a DEFAULT_ARG to represented the unparsed default
12178 argument. */
12179 default_argument = make_node (DEFAULT_ARG);
12180 DEFARG_TOKENS (default_argument)
12181 = cp_token_cache_new (first_token, token);
12182 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12184 /* Outside of a class definition, we can just parse the
12185 assignment-expression. */
12186 else
12188 bool saved_local_variables_forbidden_p;
12190 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12191 set correctly. */
12192 saved_greater_than_is_operator_p
12193 = parser->greater_than_is_operator_p;
12194 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12195 /* Local variable names (and the `this' keyword) may not
12196 appear in a default argument. */
12197 saved_local_variables_forbidden_p
12198 = parser->local_variables_forbidden_p;
12199 parser->local_variables_forbidden_p = true;
12200 /* Parse the assignment-expression. */
12201 default_argument
12202 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12203 /* Restore saved state. */
12204 parser->greater_than_is_operator_p
12205 = saved_greater_than_is_operator_p;
12206 parser->local_variables_forbidden_p
12207 = saved_local_variables_forbidden_p;
12209 if (!parser->default_arg_ok_p)
12211 if (!flag_pedantic_errors)
12212 warning (0, "deprecated use of default argument for parameter of non-function");
12213 else
12215 error ("default arguments are only permitted for function parameters");
12216 default_argument = NULL_TREE;
12220 else
12221 default_argument = NULL_TREE;
12223 return make_parameter_declarator (&decl_specifiers,
12224 declarator,
12225 default_argument);
12228 /* Parse a function-body.
12230 function-body:
12231 compound_statement */
12233 static void
12234 cp_parser_function_body (cp_parser *parser)
12236 cp_parser_compound_statement (parser, NULL, false);
12239 /* Parse a ctor-initializer-opt followed by a function-body. Return
12240 true if a ctor-initializer was present. */
12242 static bool
12243 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12245 tree body;
12246 bool ctor_initializer_p;
12248 /* Begin the function body. */
12249 body = begin_function_body ();
12250 /* Parse the optional ctor-initializer. */
12251 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12252 /* Parse the function-body. */
12253 cp_parser_function_body (parser);
12254 /* Finish the function body. */
12255 finish_function_body (body);
12257 return ctor_initializer_p;
12260 /* Parse an initializer.
12262 initializer:
12263 = initializer-clause
12264 ( expression-list )
12266 Returns an expression representing the initializer. If no
12267 initializer is present, NULL_TREE is returned.
12269 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12270 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12271 set to FALSE if there is no initializer present. If there is an
12272 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12273 is set to true; otherwise it is set to false. */
12275 static tree
12276 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12277 bool* non_constant_p)
12279 cp_token *token;
12280 tree init;
12282 /* Peek at the next token. */
12283 token = cp_lexer_peek_token (parser->lexer);
12285 /* Let our caller know whether or not this initializer was
12286 parenthesized. */
12287 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12288 /* Assume that the initializer is constant. */
12289 *non_constant_p = false;
12291 if (token->type == CPP_EQ)
12293 /* Consume the `='. */
12294 cp_lexer_consume_token (parser->lexer);
12295 /* Parse the initializer-clause. */
12296 init = cp_parser_initializer_clause (parser, non_constant_p);
12298 else if (token->type == CPP_OPEN_PAREN)
12299 init = cp_parser_parenthesized_expression_list (parser, false,
12300 /*cast_p=*/false,
12301 non_constant_p);
12302 else
12304 /* Anything else is an error. */
12305 cp_parser_error (parser, "expected initializer");
12306 init = error_mark_node;
12309 return init;
12312 /* Parse an initializer-clause.
12314 initializer-clause:
12315 assignment-expression
12316 { initializer-list , [opt] }
12319 Returns an expression representing the initializer.
12321 If the `assignment-expression' production is used the value
12322 returned is simply a representation for the expression.
12324 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12325 the elements of the initializer-list (or NULL, if the last
12326 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12327 NULL_TREE. There is no way to detect whether or not the optional
12328 trailing `,' was provided. NON_CONSTANT_P is as for
12329 cp_parser_initializer. */
12331 static tree
12332 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12334 tree initializer;
12336 /* Assume the expression is constant. */
12337 *non_constant_p = false;
12339 /* If it is not a `{', then we are looking at an
12340 assignment-expression. */
12341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12343 initializer
12344 = cp_parser_constant_expression (parser,
12345 /*allow_non_constant_p=*/true,
12346 non_constant_p);
12347 if (!*non_constant_p)
12348 initializer = fold_non_dependent_expr (initializer);
12350 else
12352 /* Consume the `{' token. */
12353 cp_lexer_consume_token (parser->lexer);
12354 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12355 initializer = make_node (CONSTRUCTOR);
12356 /* If it's not a `}', then there is a non-trivial initializer. */
12357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12359 /* Parse the initializer list. */
12360 CONSTRUCTOR_ELTS (initializer)
12361 = cp_parser_initializer_list (parser, non_constant_p);
12362 /* A trailing `,' token is allowed. */
12363 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12364 cp_lexer_consume_token (parser->lexer);
12366 /* Now, there should be a trailing `}'. */
12367 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12370 return initializer;
12373 /* Parse an initializer-list.
12375 initializer-list:
12376 initializer-clause
12377 initializer-list , initializer-clause
12379 GNU Extension:
12381 initializer-list:
12382 identifier : initializer-clause
12383 initializer-list, identifier : initializer-clause
12385 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12386 for the initializer. If the INDEX of the elt is non-NULL, it is the
12387 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12388 as for cp_parser_initializer. */
12390 static VEC(constructor_elt,gc) *
12391 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12393 VEC(constructor_elt,gc) *v = NULL;
12395 /* Assume all of the expressions are constant. */
12396 *non_constant_p = false;
12398 /* Parse the rest of the list. */
12399 while (true)
12401 cp_token *token;
12402 tree identifier;
12403 tree initializer;
12404 bool clause_non_constant_p;
12406 /* If the next token is an identifier and the following one is a
12407 colon, we are looking at the GNU designated-initializer
12408 syntax. */
12409 if (cp_parser_allow_gnu_extensions_p (parser)
12410 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12411 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12413 /* Consume the identifier. */
12414 identifier = cp_lexer_consume_token (parser->lexer)->value;
12415 /* Consume the `:'. */
12416 cp_lexer_consume_token (parser->lexer);
12418 else
12419 identifier = NULL_TREE;
12421 /* Parse the initializer. */
12422 initializer = cp_parser_initializer_clause (parser,
12423 &clause_non_constant_p);
12424 /* If any clause is non-constant, so is the entire initializer. */
12425 if (clause_non_constant_p)
12426 *non_constant_p = true;
12428 /* Add it to the vector. */
12429 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12431 /* If the next token is not a comma, we have reached the end of
12432 the list. */
12433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12434 break;
12436 /* Peek at the next token. */
12437 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12438 /* If the next token is a `}', then we're still done. An
12439 initializer-clause can have a trailing `,' after the
12440 initializer-list and before the closing `}'. */
12441 if (token->type == CPP_CLOSE_BRACE)
12442 break;
12444 /* Consume the `,' token. */
12445 cp_lexer_consume_token (parser->lexer);
12448 return v;
12451 /* Classes [gram.class] */
12453 /* Parse a class-name.
12455 class-name:
12456 identifier
12457 template-id
12459 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12460 to indicate that names looked up in dependent types should be
12461 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12462 keyword has been used to indicate that the name that appears next
12463 is a template. TAG_TYPE indicates the explicit tag given before
12464 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12465 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12466 is the class being defined in a class-head.
12468 Returns the TYPE_DECL representing the class. */
12470 static tree
12471 cp_parser_class_name (cp_parser *parser,
12472 bool typename_keyword_p,
12473 bool template_keyword_p,
12474 enum tag_types tag_type,
12475 bool check_dependency_p,
12476 bool class_head_p,
12477 bool is_declaration)
12479 tree decl;
12480 tree scope;
12481 bool typename_p;
12482 cp_token *token;
12484 /* All class-names start with an identifier. */
12485 token = cp_lexer_peek_token (parser->lexer);
12486 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12488 cp_parser_error (parser, "expected class-name");
12489 return error_mark_node;
12492 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12493 to a template-id, so we save it here. */
12494 scope = parser->scope;
12495 if (scope == error_mark_node)
12496 return error_mark_node;
12498 /* Any name names a type if we're following the `typename' keyword
12499 in a qualified name where the enclosing scope is type-dependent. */
12500 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12501 && dependent_type_p (scope));
12502 /* Handle the common case (an identifier, but not a template-id)
12503 efficiently. */
12504 if (token->type == CPP_NAME
12505 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12507 tree identifier;
12509 /* Look for the identifier. */
12510 identifier = cp_parser_identifier (parser);
12511 /* If the next token isn't an identifier, we are certainly not
12512 looking at a class-name. */
12513 if (identifier == error_mark_node)
12514 decl = error_mark_node;
12515 /* If we know this is a type-name, there's no need to look it
12516 up. */
12517 else if (typename_p)
12518 decl = identifier;
12519 else
12521 /* If the next token is a `::', then the name must be a type
12522 name.
12524 [basic.lookup.qual]
12526 During the lookup for a name preceding the :: scope
12527 resolution operator, object, function, and enumerator
12528 names are ignored. */
12529 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12530 tag_type = typename_type;
12531 /* Look up the name. */
12532 decl = cp_parser_lookup_name (parser, identifier,
12533 tag_type,
12534 /*is_template=*/false,
12535 /*is_namespace=*/false,
12536 check_dependency_p,
12537 /*ambiguous_p=*/NULL);
12540 else
12542 /* Try a template-id. */
12543 decl = cp_parser_template_id (parser, template_keyword_p,
12544 check_dependency_p,
12545 is_declaration);
12546 if (decl == error_mark_node)
12547 return error_mark_node;
12550 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12552 /* If this is a typename, create a TYPENAME_TYPE. */
12553 if (typename_p && decl != error_mark_node)
12555 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12556 if (decl != error_mark_node)
12557 decl = TYPE_NAME (decl);
12560 /* Check to see that it is really the name of a class. */
12561 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12562 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12563 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12564 /* Situations like this:
12566 template <typename T> struct A {
12567 typename T::template X<int>::I i;
12570 are problematic. Is `T::template X<int>' a class-name? The
12571 standard does not seem to be definitive, but there is no other
12572 valid interpretation of the following `::'. Therefore, those
12573 names are considered class-names. */
12574 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12575 else if (decl == error_mark_node
12576 || TREE_CODE (decl) != TYPE_DECL
12577 || TREE_TYPE (decl) == error_mark_node
12578 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12580 cp_parser_error (parser, "expected class-name");
12581 return error_mark_node;
12584 return decl;
12587 /* Parse a class-specifier.
12589 class-specifier:
12590 class-head { member-specification [opt] }
12592 Returns the TREE_TYPE representing the class. */
12594 static tree
12595 cp_parser_class_specifier (cp_parser* parser)
12597 cp_token *token;
12598 tree type;
12599 tree attributes = NULL_TREE;
12600 int has_trailing_semicolon;
12601 bool nested_name_specifier_p;
12602 unsigned saved_num_template_parameter_lists;
12603 tree old_scope = NULL_TREE;
12604 tree scope = NULL_TREE;
12606 push_deferring_access_checks (dk_no_deferred);
12608 /* Parse the class-head. */
12609 type = cp_parser_class_head (parser,
12610 &nested_name_specifier_p,
12611 &attributes);
12612 /* If the class-head was a semantic disaster, skip the entire body
12613 of the class. */
12614 if (!type)
12616 cp_parser_skip_to_end_of_block_or_statement (parser);
12617 pop_deferring_access_checks ();
12618 return error_mark_node;
12621 /* Look for the `{'. */
12622 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12624 pop_deferring_access_checks ();
12625 return error_mark_node;
12628 /* Issue an error message if type-definitions are forbidden here. */
12629 cp_parser_check_type_definition (parser);
12630 /* Remember that we are defining one more class. */
12631 ++parser->num_classes_being_defined;
12632 /* Inside the class, surrounding template-parameter-lists do not
12633 apply. */
12634 saved_num_template_parameter_lists
12635 = parser->num_template_parameter_lists;
12636 parser->num_template_parameter_lists = 0;
12638 /* Start the class. */
12639 if (nested_name_specifier_p)
12641 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12642 old_scope = push_inner_scope (scope);
12644 type = begin_class_definition (type);
12646 if (type == error_mark_node)
12647 /* If the type is erroneous, skip the entire body of the class. */
12648 cp_parser_skip_to_closing_brace (parser);
12649 else
12650 /* Parse the member-specification. */
12651 cp_parser_member_specification_opt (parser);
12653 /* Look for the trailing `}'. */
12654 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12655 /* We get better error messages by noticing a common problem: a
12656 missing trailing `;'. */
12657 token = cp_lexer_peek_token (parser->lexer);
12658 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12659 /* Look for trailing attributes to apply to this class. */
12660 if (cp_parser_allow_gnu_extensions_p (parser))
12662 tree sub_attr = cp_parser_attributes_opt (parser);
12663 attributes = chainon (attributes, sub_attr);
12665 if (type != error_mark_node)
12666 type = finish_struct (type, attributes);
12667 if (nested_name_specifier_p)
12668 pop_inner_scope (old_scope, scope);
12669 /* If this class is not itself within the scope of another class,
12670 then we need to parse the bodies of all of the queued function
12671 definitions. Note that the queued functions defined in a class
12672 are not always processed immediately following the
12673 class-specifier for that class. Consider:
12675 struct A {
12676 struct B { void f() { sizeof (A); } };
12679 If `f' were processed before the processing of `A' were
12680 completed, there would be no way to compute the size of `A'.
12681 Note that the nesting we are interested in here is lexical --
12682 not the semantic nesting given by TYPE_CONTEXT. In particular,
12683 for:
12685 struct A { struct B; };
12686 struct A::B { void f() { } };
12688 there is no need to delay the parsing of `A::B::f'. */
12689 if (--parser->num_classes_being_defined == 0)
12691 tree queue_entry;
12692 tree fn;
12693 tree class_type = NULL_TREE;
12694 tree pushed_scope = NULL_TREE;
12696 /* In a first pass, parse default arguments to the functions.
12697 Then, in a second pass, parse the bodies of the functions.
12698 This two-phased approach handles cases like:
12700 struct S {
12701 void f() { g(); }
12702 void g(int i = 3);
12706 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12707 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12708 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12709 TREE_PURPOSE (parser->unparsed_functions_queues)
12710 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12712 fn = TREE_VALUE (queue_entry);
12713 /* If there are default arguments that have not yet been processed,
12714 take care of them now. */
12715 if (class_type != TREE_PURPOSE (queue_entry))
12717 if (pushed_scope)
12718 pop_scope (pushed_scope);
12719 class_type = TREE_PURPOSE (queue_entry);
12720 pushed_scope = push_scope (class_type);
12722 /* Make sure that any template parameters are in scope. */
12723 maybe_begin_member_template_processing (fn);
12724 /* Parse the default argument expressions. */
12725 cp_parser_late_parsing_default_args (parser, fn);
12726 /* Remove any template parameters from the symbol table. */
12727 maybe_end_member_template_processing ();
12729 if (pushed_scope)
12730 pop_scope (pushed_scope);
12731 /* Now parse the body of the functions. */
12732 for (TREE_VALUE (parser->unparsed_functions_queues)
12733 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12734 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12735 TREE_VALUE (parser->unparsed_functions_queues)
12736 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12738 /* Figure out which function we need to process. */
12739 fn = TREE_VALUE (queue_entry);
12740 /* Parse the function. */
12741 cp_parser_late_parsing_for_member (parser, fn);
12745 /* Put back any saved access checks. */
12746 pop_deferring_access_checks ();
12748 /* Restore the count of active template-parameter-lists. */
12749 parser->num_template_parameter_lists
12750 = saved_num_template_parameter_lists;
12752 return type;
12755 /* Parse a class-head.
12757 class-head:
12758 class-key identifier [opt] base-clause [opt]
12759 class-key nested-name-specifier identifier base-clause [opt]
12760 class-key nested-name-specifier [opt] template-id
12761 base-clause [opt]
12763 GNU Extensions:
12764 class-key attributes identifier [opt] base-clause [opt]
12765 class-key attributes nested-name-specifier identifier base-clause [opt]
12766 class-key attributes nested-name-specifier [opt] template-id
12767 base-clause [opt]
12769 Returns the TYPE of the indicated class. Sets
12770 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12771 involving a nested-name-specifier was used, and FALSE otherwise.
12773 Returns error_mark_node if this is not a class-head.
12775 Returns NULL_TREE if the class-head is syntactically valid, but
12776 semantically invalid in a way that means we should skip the entire
12777 body of the class. */
12779 static tree
12780 cp_parser_class_head (cp_parser* parser,
12781 bool* nested_name_specifier_p,
12782 tree *attributes_p)
12784 tree nested_name_specifier;
12785 enum tag_types class_key;
12786 tree id = NULL_TREE;
12787 tree type = NULL_TREE;
12788 tree attributes;
12789 bool template_id_p = false;
12790 bool qualified_p = false;
12791 bool invalid_nested_name_p = false;
12792 bool invalid_explicit_specialization_p = false;
12793 tree pushed_scope = NULL_TREE;
12794 unsigned num_templates;
12795 tree bases;
12797 /* Assume no nested-name-specifier will be present. */
12798 *nested_name_specifier_p = false;
12799 /* Assume no template parameter lists will be used in defining the
12800 type. */
12801 num_templates = 0;
12803 /* Look for the class-key. */
12804 class_key = cp_parser_class_key (parser);
12805 if (class_key == none_type)
12806 return error_mark_node;
12808 /* Parse the attributes. */
12809 attributes = cp_parser_attributes_opt (parser);
12811 /* If the next token is `::', that is invalid -- but sometimes
12812 people do try to write:
12814 struct ::S {};
12816 Handle this gracefully by accepting the extra qualifier, and then
12817 issuing an error about it later if this really is a
12818 class-head. If it turns out just to be an elaborated type
12819 specifier, remain silent. */
12820 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12821 qualified_p = true;
12823 push_deferring_access_checks (dk_no_check);
12825 /* Determine the name of the class. Begin by looking for an
12826 optional nested-name-specifier. */
12827 nested_name_specifier
12828 = cp_parser_nested_name_specifier_opt (parser,
12829 /*typename_keyword_p=*/false,
12830 /*check_dependency_p=*/false,
12831 /*type_p=*/false,
12832 /*is_declaration=*/false);
12833 /* If there was a nested-name-specifier, then there *must* be an
12834 identifier. */
12835 if (nested_name_specifier)
12837 /* Although the grammar says `identifier', it really means
12838 `class-name' or `template-name'. You are only allowed to
12839 define a class that has already been declared with this
12840 syntax.
12842 The proposed resolution for Core Issue 180 says that whever
12843 you see `class T::X' you should treat `X' as a type-name.
12845 It is OK to define an inaccessible class; for example:
12847 class A { class B; };
12848 class A::B {};
12850 We do not know if we will see a class-name, or a
12851 template-name. We look for a class-name first, in case the
12852 class-name is a template-id; if we looked for the
12853 template-name first we would stop after the template-name. */
12854 cp_parser_parse_tentatively (parser);
12855 type = cp_parser_class_name (parser,
12856 /*typename_keyword_p=*/false,
12857 /*template_keyword_p=*/false,
12858 class_type,
12859 /*check_dependency_p=*/false,
12860 /*class_head_p=*/true,
12861 /*is_declaration=*/false);
12862 /* If that didn't work, ignore the nested-name-specifier. */
12863 if (!cp_parser_parse_definitely (parser))
12865 invalid_nested_name_p = true;
12866 id = cp_parser_identifier (parser);
12867 if (id == error_mark_node)
12868 id = NULL_TREE;
12870 /* If we could not find a corresponding TYPE, treat this
12871 declaration like an unqualified declaration. */
12872 if (type == error_mark_node)
12873 nested_name_specifier = NULL_TREE;
12874 /* Otherwise, count the number of templates used in TYPE and its
12875 containing scopes. */
12876 else
12878 tree scope;
12880 for (scope = TREE_TYPE (type);
12881 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12882 scope = (TYPE_P (scope)
12883 ? TYPE_CONTEXT (scope)
12884 : DECL_CONTEXT (scope)))
12885 if (TYPE_P (scope)
12886 && CLASS_TYPE_P (scope)
12887 && CLASSTYPE_TEMPLATE_INFO (scope)
12888 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12889 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12890 ++num_templates;
12893 /* Otherwise, the identifier is optional. */
12894 else
12896 /* We don't know whether what comes next is a template-id,
12897 an identifier, or nothing at all. */
12898 cp_parser_parse_tentatively (parser);
12899 /* Check for a template-id. */
12900 id = cp_parser_template_id (parser,
12901 /*template_keyword_p=*/false,
12902 /*check_dependency_p=*/true,
12903 /*is_declaration=*/true);
12904 /* If that didn't work, it could still be an identifier. */
12905 if (!cp_parser_parse_definitely (parser))
12907 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12908 id = cp_parser_identifier (parser);
12909 else
12910 id = NULL_TREE;
12912 else
12914 template_id_p = true;
12915 ++num_templates;
12919 pop_deferring_access_checks ();
12921 if (id)
12922 cp_parser_check_for_invalid_template_id (parser, id);
12924 /* If it's not a `:' or a `{' then we can't really be looking at a
12925 class-head, since a class-head only appears as part of a
12926 class-specifier. We have to detect this situation before calling
12927 xref_tag, since that has irreversible side-effects. */
12928 if (!cp_parser_next_token_starts_class_definition_p (parser))
12930 cp_parser_error (parser, "expected %<{%> or %<:%>");
12931 return error_mark_node;
12934 /* At this point, we're going ahead with the class-specifier, even
12935 if some other problem occurs. */
12936 cp_parser_commit_to_tentative_parse (parser);
12937 /* Issue the error about the overly-qualified name now. */
12938 if (qualified_p)
12939 cp_parser_error (parser,
12940 "global qualification of class name is invalid");
12941 else if (invalid_nested_name_p)
12942 cp_parser_error (parser,
12943 "qualified name does not name a class");
12944 else if (nested_name_specifier)
12946 tree scope;
12948 /* Reject typedef-names in class heads. */
12949 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12951 error ("invalid class name in declaration of %qD", type);
12952 type = NULL_TREE;
12953 goto done;
12956 /* Figure out in what scope the declaration is being placed. */
12957 scope = current_scope ();
12958 /* If that scope does not contain the scope in which the
12959 class was originally declared, the program is invalid. */
12960 if (scope && !is_ancestor (scope, nested_name_specifier))
12962 error ("declaration of %qD in %qD which does not enclose %qD",
12963 type, scope, nested_name_specifier);
12964 type = NULL_TREE;
12965 goto done;
12967 /* [dcl.meaning]
12969 A declarator-id shall not be qualified exception of the
12970 definition of a ... nested class outside of its class
12971 ... [or] a the definition or explicit instantiation of a
12972 class member of a namespace outside of its namespace. */
12973 if (scope == nested_name_specifier)
12975 pedwarn ("extra qualification ignored");
12976 nested_name_specifier = NULL_TREE;
12977 num_templates = 0;
12980 /* An explicit-specialization must be preceded by "template <>". If
12981 it is not, try to recover gracefully. */
12982 if (at_namespace_scope_p ()
12983 && parser->num_template_parameter_lists == 0
12984 && template_id_p)
12986 error ("an explicit specialization must be preceded by %<template <>%>");
12987 invalid_explicit_specialization_p = true;
12988 /* Take the same action that would have been taken by
12989 cp_parser_explicit_specialization. */
12990 ++parser->num_template_parameter_lists;
12991 begin_specialization ();
12993 /* There must be no "return" statements between this point and the
12994 end of this function; set "type "to the correct return value and
12995 use "goto done;" to return. */
12996 /* Make sure that the right number of template parameters were
12997 present. */
12998 if (!cp_parser_check_template_parameters (parser, num_templates))
13000 /* If something went wrong, there is no point in even trying to
13001 process the class-definition. */
13002 type = NULL_TREE;
13003 goto done;
13006 /* Look up the type. */
13007 if (template_id_p)
13009 type = TREE_TYPE (id);
13010 maybe_process_partial_specialization (type);
13011 if (nested_name_specifier)
13012 pushed_scope = push_scope (nested_name_specifier);
13014 else if (nested_name_specifier)
13016 tree class_type;
13018 /* Given:
13020 template <typename T> struct S { struct T };
13021 template <typename T> struct S<T>::T { };
13023 we will get a TYPENAME_TYPE when processing the definition of
13024 `S::T'. We need to resolve it to the actual type before we
13025 try to define it. */
13026 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13028 class_type = resolve_typename_type (TREE_TYPE (type),
13029 /*only_current_p=*/false);
13030 if (class_type != error_mark_node)
13031 type = TYPE_NAME (class_type);
13032 else
13034 cp_parser_error (parser, "could not resolve typename type");
13035 type = error_mark_node;
13039 maybe_process_partial_specialization (TREE_TYPE (type));
13040 class_type = current_class_type;
13041 /* Enter the scope indicated by the nested-name-specifier. */
13042 pushed_scope = push_scope (nested_name_specifier);
13043 /* Get the canonical version of this type. */
13044 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13045 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13046 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13048 type = push_template_decl (type);
13049 if (type == error_mark_node)
13051 type = NULL_TREE;
13052 goto done;
13056 type = TREE_TYPE (type);
13057 *nested_name_specifier_p = true;
13059 else /* The name is not a nested name. */
13061 /* If the class was unnamed, create a dummy name. */
13062 if (!id)
13063 id = make_anon_name ();
13064 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13065 parser->num_template_parameter_lists);
13068 /* Indicate whether this class was declared as a `class' or as a
13069 `struct'. */
13070 if (TREE_CODE (type) == RECORD_TYPE)
13071 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13072 cp_parser_check_class_key (class_key, type);
13074 /* If this type was already complete, and we see another definition,
13075 that's an error. */
13076 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13078 error ("redefinition of %q#T", type);
13079 error ("previous definition of %q+#T", type);
13080 type = NULL_TREE;
13081 goto done;
13084 /* We will have entered the scope containing the class; the names of
13085 base classes should be looked up in that context. For example:
13087 struct A { struct B {}; struct C; };
13088 struct A::C : B {};
13090 is valid. */
13091 bases = NULL_TREE;
13093 /* Get the list of base-classes, if there is one. */
13094 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13095 bases = cp_parser_base_clause (parser);
13097 /* Process the base classes. */
13098 xref_basetypes (type, bases);
13100 done:
13101 /* Leave the scope given by the nested-name-specifier. We will
13102 enter the class scope itself while processing the members. */
13103 if (pushed_scope)
13104 pop_scope (pushed_scope);
13106 if (invalid_explicit_specialization_p)
13108 end_specialization ();
13109 --parser->num_template_parameter_lists;
13111 *attributes_p = attributes;
13112 return type;
13115 /* Parse a class-key.
13117 class-key:
13118 class
13119 struct
13120 union
13122 Returns the kind of class-key specified, or none_type to indicate
13123 error. */
13125 static enum tag_types
13126 cp_parser_class_key (cp_parser* parser)
13128 cp_token *token;
13129 enum tag_types tag_type;
13131 /* Look for the class-key. */
13132 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13133 if (!token)
13134 return none_type;
13136 /* Check to see if the TOKEN is a class-key. */
13137 tag_type = cp_parser_token_is_class_key (token);
13138 if (!tag_type)
13139 cp_parser_error (parser, "expected class-key");
13140 return tag_type;
13143 /* Parse an (optional) member-specification.
13145 member-specification:
13146 member-declaration member-specification [opt]
13147 access-specifier : member-specification [opt] */
13149 static void
13150 cp_parser_member_specification_opt (cp_parser* parser)
13152 while (true)
13154 cp_token *token;
13155 enum rid keyword;
13157 /* Peek at the next token. */
13158 token = cp_lexer_peek_token (parser->lexer);
13159 /* If it's a `}', or EOF then we've seen all the members. */
13160 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13161 break;
13163 /* See if this token is a keyword. */
13164 keyword = token->keyword;
13165 switch (keyword)
13167 case RID_PUBLIC:
13168 case RID_PROTECTED:
13169 case RID_PRIVATE:
13170 /* Consume the access-specifier. */
13171 cp_lexer_consume_token (parser->lexer);
13172 /* Remember which access-specifier is active. */
13173 current_access_specifier = token->value;
13174 /* Look for the `:'. */
13175 cp_parser_require (parser, CPP_COLON, "`:'");
13176 break;
13178 default:
13179 /* Accept #pragmas at class scope. */
13180 if (token->type == CPP_PRAGMA)
13182 cp_lexer_handle_pragma (parser->lexer);
13183 break;
13186 /* Otherwise, the next construction must be a
13187 member-declaration. */
13188 cp_parser_member_declaration (parser);
13193 /* Parse a member-declaration.
13195 member-declaration:
13196 decl-specifier-seq [opt] member-declarator-list [opt] ;
13197 function-definition ; [opt]
13198 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13199 using-declaration
13200 template-declaration
13202 member-declarator-list:
13203 member-declarator
13204 member-declarator-list , member-declarator
13206 member-declarator:
13207 declarator pure-specifier [opt]
13208 declarator constant-initializer [opt]
13209 identifier [opt] : constant-expression
13211 GNU Extensions:
13213 member-declaration:
13214 __extension__ member-declaration
13216 member-declarator:
13217 declarator attributes [opt] pure-specifier [opt]
13218 declarator attributes [opt] constant-initializer [opt]
13219 identifier [opt] attributes [opt] : constant-expression */
13221 static void
13222 cp_parser_member_declaration (cp_parser* parser)
13224 cp_decl_specifier_seq decl_specifiers;
13225 tree prefix_attributes;
13226 tree decl;
13227 int declares_class_or_enum;
13228 bool friend_p;
13229 cp_token *token;
13230 int saved_pedantic;
13232 /* Check for the `__extension__' keyword. */
13233 if (cp_parser_extension_opt (parser, &saved_pedantic))
13235 /* Recurse. */
13236 cp_parser_member_declaration (parser);
13237 /* Restore the old value of the PEDANTIC flag. */
13238 pedantic = saved_pedantic;
13240 return;
13243 /* Check for a template-declaration. */
13244 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13246 /* Parse the template-declaration. */
13247 cp_parser_template_declaration (parser, /*member_p=*/true);
13249 return;
13252 /* Check for a using-declaration. */
13253 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13255 /* Parse the using-declaration. */
13256 cp_parser_using_declaration (parser);
13258 return;
13261 /* Check for @defs. */
13262 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13264 tree ivar, member;
13265 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13266 ivar = ivar_chains;
13267 while (ivar)
13269 member = ivar;
13270 ivar = TREE_CHAIN (member);
13271 TREE_CHAIN (member) = NULL_TREE;
13272 finish_member_declaration (member);
13274 return;
13277 /* Parse the decl-specifier-seq. */
13278 cp_parser_decl_specifier_seq (parser,
13279 CP_PARSER_FLAGS_OPTIONAL,
13280 &decl_specifiers,
13281 &declares_class_or_enum);
13282 prefix_attributes = decl_specifiers.attributes;
13283 decl_specifiers.attributes = NULL_TREE;
13284 /* Check for an invalid type-name. */
13285 if (!decl_specifiers.type
13286 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13287 return;
13288 /* If there is no declarator, then the decl-specifier-seq should
13289 specify a type. */
13290 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13292 /* If there was no decl-specifier-seq, and the next token is a
13293 `;', then we have something like:
13295 struct S { ; };
13297 [class.mem]
13299 Each member-declaration shall declare at least one member
13300 name of the class. */
13301 if (!decl_specifiers.any_specifiers_p)
13303 cp_token *token = cp_lexer_peek_token (parser->lexer);
13304 if (pedantic && !token->in_system_header)
13305 pedwarn ("%Hextra %<;%>", &token->location);
13307 else
13309 tree type;
13311 /* See if this declaration is a friend. */
13312 friend_p = cp_parser_friend_p (&decl_specifiers);
13313 /* If there were decl-specifiers, check to see if there was
13314 a class-declaration. */
13315 type = check_tag_decl (&decl_specifiers);
13316 /* Nested classes have already been added to the class, but
13317 a `friend' needs to be explicitly registered. */
13318 if (friend_p)
13320 /* If the `friend' keyword was present, the friend must
13321 be introduced with a class-key. */
13322 if (!declares_class_or_enum)
13323 error ("a class-key must be used when declaring a friend");
13324 /* In this case:
13326 template <typename T> struct A {
13327 friend struct A<T>::B;
13330 A<T>::B will be represented by a TYPENAME_TYPE, and
13331 therefore not recognized by check_tag_decl. */
13332 if (!type
13333 && decl_specifiers.type
13334 && TYPE_P (decl_specifiers.type))
13335 type = decl_specifiers.type;
13336 if (!type || !TYPE_P (type))
13337 error ("friend declaration does not name a class or "
13338 "function");
13339 else
13340 make_friend_class (current_class_type, type,
13341 /*complain=*/true);
13343 /* If there is no TYPE, an error message will already have
13344 been issued. */
13345 else if (!type || type == error_mark_node)
13347 /* An anonymous aggregate has to be handled specially; such
13348 a declaration really declares a data member (with a
13349 particular type), as opposed to a nested class. */
13350 else if (ANON_AGGR_TYPE_P (type))
13352 /* Remove constructors and such from TYPE, now that we
13353 know it is an anonymous aggregate. */
13354 fixup_anonymous_aggr (type);
13355 /* And make the corresponding data member. */
13356 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13357 /* Add it to the class. */
13358 finish_member_declaration (decl);
13360 else
13361 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13364 else
13366 /* See if these declarations will be friends. */
13367 friend_p = cp_parser_friend_p (&decl_specifiers);
13369 /* Keep going until we hit the `;' at the end of the
13370 declaration. */
13371 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13373 tree attributes = NULL_TREE;
13374 tree first_attribute;
13376 /* Peek at the next token. */
13377 token = cp_lexer_peek_token (parser->lexer);
13379 /* Check for a bitfield declaration. */
13380 if (token->type == CPP_COLON
13381 || (token->type == CPP_NAME
13382 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13383 == CPP_COLON))
13385 tree identifier;
13386 tree width;
13388 /* Get the name of the bitfield. Note that we cannot just
13389 check TOKEN here because it may have been invalidated by
13390 the call to cp_lexer_peek_nth_token above. */
13391 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13392 identifier = cp_parser_identifier (parser);
13393 else
13394 identifier = NULL_TREE;
13396 /* Consume the `:' token. */
13397 cp_lexer_consume_token (parser->lexer);
13398 /* Get the width of the bitfield. */
13399 width
13400 = cp_parser_constant_expression (parser,
13401 /*allow_non_constant=*/false,
13402 NULL);
13404 /* Look for attributes that apply to the bitfield. */
13405 attributes = cp_parser_attributes_opt (parser);
13406 /* Remember which attributes are prefix attributes and
13407 which are not. */
13408 first_attribute = attributes;
13409 /* Combine the attributes. */
13410 attributes = chainon (prefix_attributes, attributes);
13412 /* Create the bitfield declaration. */
13413 decl = grokbitfield (identifier
13414 ? make_id_declarator (NULL_TREE,
13415 identifier)
13416 : NULL,
13417 &decl_specifiers,
13418 width);
13419 /* Apply the attributes. */
13420 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13422 else
13424 cp_declarator *declarator;
13425 tree initializer;
13426 tree asm_specification;
13427 int ctor_dtor_or_conv_p;
13429 /* Parse the declarator. */
13430 declarator
13431 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13432 &ctor_dtor_or_conv_p,
13433 /*parenthesized_p=*/NULL,
13434 /*member_p=*/true);
13436 /* If something went wrong parsing the declarator, make sure
13437 that we at least consume some tokens. */
13438 if (declarator == cp_error_declarator)
13440 /* Skip to the end of the statement. */
13441 cp_parser_skip_to_end_of_statement (parser);
13442 /* If the next token is not a semicolon, that is
13443 probably because we just skipped over the body of
13444 a function. So, we consume a semicolon if
13445 present, but do not issue an error message if it
13446 is not present. */
13447 if (cp_lexer_next_token_is (parser->lexer,
13448 CPP_SEMICOLON))
13449 cp_lexer_consume_token (parser->lexer);
13450 return;
13453 if (declares_class_or_enum & 2)
13454 cp_parser_check_for_definition_in_return_type
13455 (declarator, decl_specifiers.type);
13457 /* Look for an asm-specification. */
13458 asm_specification = cp_parser_asm_specification_opt (parser);
13459 /* Look for attributes that apply to the declaration. */
13460 attributes = cp_parser_attributes_opt (parser);
13461 /* Remember which attributes are prefix attributes and
13462 which are not. */
13463 first_attribute = attributes;
13464 /* Combine the attributes. */
13465 attributes = chainon (prefix_attributes, attributes);
13467 /* If it's an `=', then we have a constant-initializer or a
13468 pure-specifier. It is not correct to parse the
13469 initializer before registering the member declaration
13470 since the member declaration should be in scope while
13471 its initializer is processed. However, the rest of the
13472 front end does not yet provide an interface that allows
13473 us to handle this correctly. */
13474 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13476 /* In [class.mem]:
13478 A pure-specifier shall be used only in the declaration of
13479 a virtual function.
13481 A member-declarator can contain a constant-initializer
13482 only if it declares a static member of integral or
13483 enumeration type.
13485 Therefore, if the DECLARATOR is for a function, we look
13486 for a pure-specifier; otherwise, we look for a
13487 constant-initializer. When we call `grokfield', it will
13488 perform more stringent semantics checks. */
13489 if (declarator->kind == cdk_function)
13490 initializer = cp_parser_pure_specifier (parser);
13491 else
13492 /* Parse the initializer. */
13493 initializer = cp_parser_constant_initializer (parser);
13495 /* Otherwise, there is no initializer. */
13496 else
13497 initializer = NULL_TREE;
13499 /* See if we are probably looking at a function
13500 definition. We are certainly not looking at a
13501 member-declarator. Calling `grokfield' has
13502 side-effects, so we must not do it unless we are sure
13503 that we are looking at a member-declarator. */
13504 if (cp_parser_token_starts_function_definition_p
13505 (cp_lexer_peek_token (parser->lexer)))
13507 /* The grammar does not allow a pure-specifier to be
13508 used when a member function is defined. (It is
13509 possible that this fact is an oversight in the
13510 standard, since a pure function may be defined
13511 outside of the class-specifier. */
13512 if (initializer)
13513 error ("pure-specifier on function-definition");
13514 decl = cp_parser_save_member_function_body (parser,
13515 &decl_specifiers,
13516 declarator,
13517 attributes);
13518 /* If the member was not a friend, declare it here. */
13519 if (!friend_p)
13520 finish_member_declaration (decl);
13521 /* Peek at the next token. */
13522 token = cp_lexer_peek_token (parser->lexer);
13523 /* If the next token is a semicolon, consume it. */
13524 if (token->type == CPP_SEMICOLON)
13525 cp_lexer_consume_token (parser->lexer);
13526 return;
13528 else
13530 /* Create the declaration. */
13531 decl = grokfield (declarator, &decl_specifiers,
13532 initializer, asm_specification,
13533 attributes);
13534 /* Any initialization must have been from a
13535 constant-expression. */
13536 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13537 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13541 /* Reset PREFIX_ATTRIBUTES. */
13542 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13543 attributes = TREE_CHAIN (attributes);
13544 if (attributes)
13545 TREE_CHAIN (attributes) = NULL_TREE;
13547 /* If there is any qualification still in effect, clear it
13548 now; we will be starting fresh with the next declarator. */
13549 parser->scope = NULL_TREE;
13550 parser->qualifying_scope = NULL_TREE;
13551 parser->object_scope = NULL_TREE;
13552 /* If it's a `,', then there are more declarators. */
13553 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13554 cp_lexer_consume_token (parser->lexer);
13555 /* If the next token isn't a `;', then we have a parse error. */
13556 else if (cp_lexer_next_token_is_not (parser->lexer,
13557 CPP_SEMICOLON))
13559 cp_parser_error (parser, "expected %<;%>");
13560 /* Skip tokens until we find a `;'. */
13561 cp_parser_skip_to_end_of_statement (parser);
13563 break;
13566 if (decl)
13568 /* Add DECL to the list of members. */
13569 if (!friend_p)
13570 finish_member_declaration (decl);
13572 if (TREE_CODE (decl) == FUNCTION_DECL)
13573 cp_parser_save_default_args (parser, decl);
13578 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13581 /* Parse a pure-specifier.
13583 pure-specifier:
13586 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13587 Otherwise, ERROR_MARK_NODE is returned. */
13589 static tree
13590 cp_parser_pure_specifier (cp_parser* parser)
13592 cp_token *token;
13594 /* Look for the `=' token. */
13595 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13596 return error_mark_node;
13597 /* Look for the `0' token. */
13598 token = cp_lexer_consume_token (parser->lexer);
13599 if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13601 cp_parser_error (parser,
13602 "invalid pure specifier (only `= 0' is allowed)");
13603 cp_parser_skip_to_end_of_statement (parser);
13604 return error_mark_node;
13607 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13608 We need to get information from the lexer about how the number
13609 was spelled in order to fix this problem. */
13610 return integer_zero_node;
13613 /* Parse a constant-initializer.
13615 constant-initializer:
13616 = constant-expression
13618 Returns a representation of the constant-expression. */
13620 static tree
13621 cp_parser_constant_initializer (cp_parser* parser)
13623 /* Look for the `=' token. */
13624 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13625 return error_mark_node;
13627 /* It is invalid to write:
13629 struct S { static const int i = { 7 }; };
13632 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13634 cp_parser_error (parser,
13635 "a brace-enclosed initializer is not allowed here");
13636 /* Consume the opening brace. */
13637 cp_lexer_consume_token (parser->lexer);
13638 /* Skip the initializer. */
13639 cp_parser_skip_to_closing_brace (parser);
13640 /* Look for the trailing `}'. */
13641 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13643 return error_mark_node;
13646 return cp_parser_constant_expression (parser,
13647 /*allow_non_constant=*/false,
13648 NULL);
13651 /* Derived classes [gram.class.derived] */
13653 /* Parse a base-clause.
13655 base-clause:
13656 : base-specifier-list
13658 base-specifier-list:
13659 base-specifier
13660 base-specifier-list , base-specifier
13662 Returns a TREE_LIST representing the base-classes, in the order in
13663 which they were declared. The representation of each node is as
13664 described by cp_parser_base_specifier.
13666 In the case that no bases are specified, this function will return
13667 NULL_TREE, not ERROR_MARK_NODE. */
13669 static tree
13670 cp_parser_base_clause (cp_parser* parser)
13672 tree bases = NULL_TREE;
13674 /* Look for the `:' that begins the list. */
13675 cp_parser_require (parser, CPP_COLON, "`:'");
13677 /* Scan the base-specifier-list. */
13678 while (true)
13680 cp_token *token;
13681 tree base;
13683 /* Look for the base-specifier. */
13684 base = cp_parser_base_specifier (parser);
13685 /* Add BASE to the front of the list. */
13686 if (base != error_mark_node)
13688 TREE_CHAIN (base) = bases;
13689 bases = base;
13691 /* Peek at the next token. */
13692 token = cp_lexer_peek_token (parser->lexer);
13693 /* If it's not a comma, then the list is complete. */
13694 if (token->type != CPP_COMMA)
13695 break;
13696 /* Consume the `,'. */
13697 cp_lexer_consume_token (parser->lexer);
13700 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13701 base class had a qualified name. However, the next name that
13702 appears is certainly not qualified. */
13703 parser->scope = NULL_TREE;
13704 parser->qualifying_scope = NULL_TREE;
13705 parser->object_scope = NULL_TREE;
13707 return nreverse (bases);
13710 /* Parse a base-specifier.
13712 base-specifier:
13713 :: [opt] nested-name-specifier [opt] class-name
13714 virtual access-specifier [opt] :: [opt] nested-name-specifier
13715 [opt] class-name
13716 access-specifier virtual [opt] :: [opt] nested-name-specifier
13717 [opt] class-name
13719 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13720 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13721 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13722 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13724 static tree
13725 cp_parser_base_specifier (cp_parser* parser)
13727 cp_token *token;
13728 bool done = false;
13729 bool virtual_p = false;
13730 bool duplicate_virtual_error_issued_p = false;
13731 bool duplicate_access_error_issued_p = false;
13732 bool class_scope_p, template_p;
13733 tree access = access_default_node;
13734 tree type;
13736 /* Process the optional `virtual' and `access-specifier'. */
13737 while (!done)
13739 /* Peek at the next token. */
13740 token = cp_lexer_peek_token (parser->lexer);
13741 /* Process `virtual'. */
13742 switch (token->keyword)
13744 case RID_VIRTUAL:
13745 /* If `virtual' appears more than once, issue an error. */
13746 if (virtual_p && !duplicate_virtual_error_issued_p)
13748 cp_parser_error (parser,
13749 "%<virtual%> specified more than once in base-specified");
13750 duplicate_virtual_error_issued_p = true;
13753 virtual_p = true;
13755 /* Consume the `virtual' token. */
13756 cp_lexer_consume_token (parser->lexer);
13758 break;
13760 case RID_PUBLIC:
13761 case RID_PROTECTED:
13762 case RID_PRIVATE:
13763 /* If more than one access specifier appears, issue an
13764 error. */
13765 if (access != access_default_node
13766 && !duplicate_access_error_issued_p)
13768 cp_parser_error (parser,
13769 "more than one access specifier in base-specified");
13770 duplicate_access_error_issued_p = true;
13773 access = ridpointers[(int) token->keyword];
13775 /* Consume the access-specifier. */
13776 cp_lexer_consume_token (parser->lexer);
13778 break;
13780 default:
13781 done = true;
13782 break;
13785 /* It is not uncommon to see programs mechanically, erroneously, use
13786 the 'typename' keyword to denote (dependent) qualified types
13787 as base classes. */
13788 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13790 if (!processing_template_decl)
13791 error ("keyword %<typename%> not allowed outside of templates");
13792 else
13793 error ("keyword %<typename%> not allowed in this context "
13794 "(the base class is implicitly a type)");
13795 cp_lexer_consume_token (parser->lexer);
13798 /* Look for the optional `::' operator. */
13799 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13800 /* Look for the nested-name-specifier. The simplest way to
13801 implement:
13803 [temp.res]
13805 The keyword `typename' is not permitted in a base-specifier or
13806 mem-initializer; in these contexts a qualified name that
13807 depends on a template-parameter is implicitly assumed to be a
13808 type name.
13810 is to pretend that we have seen the `typename' keyword at this
13811 point. */
13812 cp_parser_nested_name_specifier_opt (parser,
13813 /*typename_keyword_p=*/true,
13814 /*check_dependency_p=*/true,
13815 typename_type,
13816 /*is_declaration=*/true);
13817 /* If the base class is given by a qualified name, assume that names
13818 we see are type names or templates, as appropriate. */
13819 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13820 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13822 /* Finally, look for the class-name. */
13823 type = cp_parser_class_name (parser,
13824 class_scope_p,
13825 template_p,
13826 typename_type,
13827 /*check_dependency_p=*/true,
13828 /*class_head_p=*/false,
13829 /*is_declaration=*/true);
13831 if (type == error_mark_node)
13832 return error_mark_node;
13834 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13837 /* Exception handling [gram.exception] */
13839 /* Parse an (optional) exception-specification.
13841 exception-specification:
13842 throw ( type-id-list [opt] )
13844 Returns a TREE_LIST representing the exception-specification. The
13845 TREE_VALUE of each node is a type. */
13847 static tree
13848 cp_parser_exception_specification_opt (cp_parser* parser)
13850 cp_token *token;
13851 tree type_id_list;
13853 /* Peek at the next token. */
13854 token = cp_lexer_peek_token (parser->lexer);
13855 /* If it's not `throw', then there's no exception-specification. */
13856 if (!cp_parser_is_keyword (token, RID_THROW))
13857 return NULL_TREE;
13859 /* Consume the `throw'. */
13860 cp_lexer_consume_token (parser->lexer);
13862 /* Look for the `('. */
13863 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13865 /* Peek at the next token. */
13866 token = cp_lexer_peek_token (parser->lexer);
13867 /* If it's not a `)', then there is a type-id-list. */
13868 if (token->type != CPP_CLOSE_PAREN)
13870 const char *saved_message;
13872 /* Types may not be defined in an exception-specification. */
13873 saved_message = parser->type_definition_forbidden_message;
13874 parser->type_definition_forbidden_message
13875 = "types may not be defined in an exception-specification";
13876 /* Parse the type-id-list. */
13877 type_id_list = cp_parser_type_id_list (parser);
13878 /* Restore the saved message. */
13879 parser->type_definition_forbidden_message = saved_message;
13881 else
13882 type_id_list = empty_except_spec;
13884 /* Look for the `)'. */
13885 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13887 return type_id_list;
13890 /* Parse an (optional) type-id-list.
13892 type-id-list:
13893 type-id
13894 type-id-list , type-id
13896 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13897 in the order that the types were presented. */
13899 static tree
13900 cp_parser_type_id_list (cp_parser* parser)
13902 tree types = NULL_TREE;
13904 while (true)
13906 cp_token *token;
13907 tree type;
13909 /* Get the next type-id. */
13910 type = cp_parser_type_id (parser);
13911 /* Add it to the list. */
13912 types = add_exception_specifier (types, type, /*complain=*/1);
13913 /* Peek at the next token. */
13914 token = cp_lexer_peek_token (parser->lexer);
13915 /* If it is not a `,', we are done. */
13916 if (token->type != CPP_COMMA)
13917 break;
13918 /* Consume the `,'. */
13919 cp_lexer_consume_token (parser->lexer);
13922 return nreverse (types);
13925 /* Parse a try-block.
13927 try-block:
13928 try compound-statement handler-seq */
13930 static tree
13931 cp_parser_try_block (cp_parser* parser)
13933 tree try_block;
13935 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13936 try_block = begin_try_block ();
13937 cp_parser_compound_statement (parser, NULL, true);
13938 finish_try_block (try_block);
13939 cp_parser_handler_seq (parser);
13940 finish_handler_sequence (try_block);
13942 return try_block;
13945 /* Parse a function-try-block.
13947 function-try-block:
13948 try ctor-initializer [opt] function-body handler-seq */
13950 static bool
13951 cp_parser_function_try_block (cp_parser* parser)
13953 tree try_block;
13954 bool ctor_initializer_p;
13956 /* Look for the `try' keyword. */
13957 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13958 return false;
13959 /* Let the rest of the front-end know where we are. */
13960 try_block = begin_function_try_block ();
13961 /* Parse the function-body. */
13962 ctor_initializer_p
13963 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13964 /* We're done with the `try' part. */
13965 finish_function_try_block (try_block);
13966 /* Parse the handlers. */
13967 cp_parser_handler_seq (parser);
13968 /* We're done with the handlers. */
13969 finish_function_handler_sequence (try_block);
13971 return ctor_initializer_p;
13974 /* Parse a handler-seq.
13976 handler-seq:
13977 handler handler-seq [opt] */
13979 static void
13980 cp_parser_handler_seq (cp_parser* parser)
13982 while (true)
13984 cp_token *token;
13986 /* Parse the handler. */
13987 cp_parser_handler (parser);
13988 /* Peek at the next token. */
13989 token = cp_lexer_peek_token (parser->lexer);
13990 /* If it's not `catch' then there are no more handlers. */
13991 if (!cp_parser_is_keyword (token, RID_CATCH))
13992 break;
13996 /* Parse a handler.
13998 handler:
13999 catch ( exception-declaration ) compound-statement */
14001 static void
14002 cp_parser_handler (cp_parser* parser)
14004 tree handler;
14005 tree declaration;
14007 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14008 handler = begin_handler ();
14009 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14010 declaration = cp_parser_exception_declaration (parser);
14011 finish_handler_parms (declaration, handler);
14012 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14013 cp_parser_compound_statement (parser, NULL, false);
14014 finish_handler (handler);
14017 /* Parse an exception-declaration.
14019 exception-declaration:
14020 type-specifier-seq declarator
14021 type-specifier-seq abstract-declarator
14022 type-specifier-seq
14025 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14026 ellipsis variant is used. */
14028 static tree
14029 cp_parser_exception_declaration (cp_parser* parser)
14031 tree decl;
14032 cp_decl_specifier_seq type_specifiers;
14033 cp_declarator *declarator;
14034 const char *saved_message;
14036 /* If it's an ellipsis, it's easy to handle. */
14037 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14039 /* Consume the `...' token. */
14040 cp_lexer_consume_token (parser->lexer);
14041 return NULL_TREE;
14044 /* Types may not be defined in exception-declarations. */
14045 saved_message = parser->type_definition_forbidden_message;
14046 parser->type_definition_forbidden_message
14047 = "types may not be defined in exception-declarations";
14049 /* Parse the type-specifier-seq. */
14050 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14051 &type_specifiers);
14052 /* If it's a `)', then there is no declarator. */
14053 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14054 declarator = NULL;
14055 else
14056 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14057 /*ctor_dtor_or_conv_p=*/NULL,
14058 /*parenthesized_p=*/NULL,
14059 /*member_p=*/false);
14061 /* Restore the saved message. */
14062 parser->type_definition_forbidden_message = saved_message;
14064 if (type_specifiers.any_specifiers_p)
14066 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14067 if (decl == NULL_TREE)
14068 error ("invalid catch parameter");
14070 else
14071 decl = NULL_TREE;
14073 return decl;
14076 /* Parse a throw-expression.
14078 throw-expression:
14079 throw assignment-expression [opt]
14081 Returns a THROW_EXPR representing the throw-expression. */
14083 static tree
14084 cp_parser_throw_expression (cp_parser* parser)
14086 tree expression;
14087 cp_token* token;
14089 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14090 token = cp_lexer_peek_token (parser->lexer);
14091 /* Figure out whether or not there is an assignment-expression
14092 following the "throw" keyword. */
14093 if (token->type == CPP_COMMA
14094 || token->type == CPP_SEMICOLON
14095 || token->type == CPP_CLOSE_PAREN
14096 || token->type == CPP_CLOSE_SQUARE
14097 || token->type == CPP_CLOSE_BRACE
14098 || token->type == CPP_COLON)
14099 expression = NULL_TREE;
14100 else
14101 expression = cp_parser_assignment_expression (parser,
14102 /*cast_p=*/false);
14104 return build_throw (expression);
14107 /* GNU Extensions */
14109 /* Parse an (optional) asm-specification.
14111 asm-specification:
14112 asm ( string-literal )
14114 If the asm-specification is present, returns a STRING_CST
14115 corresponding to the string-literal. Otherwise, returns
14116 NULL_TREE. */
14118 static tree
14119 cp_parser_asm_specification_opt (cp_parser* parser)
14121 cp_token *token;
14122 tree asm_specification;
14124 /* Peek at the next token. */
14125 token = cp_lexer_peek_token (parser->lexer);
14126 /* If the next token isn't the `asm' keyword, then there's no
14127 asm-specification. */
14128 if (!cp_parser_is_keyword (token, RID_ASM))
14129 return NULL_TREE;
14131 /* Consume the `asm' token. */
14132 cp_lexer_consume_token (parser->lexer);
14133 /* Look for the `('. */
14134 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14136 /* Look for the string-literal. */
14137 asm_specification = cp_parser_string_literal (parser, false, false);
14139 /* Look for the `)'. */
14140 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14142 return asm_specification;
14145 /* Parse an asm-operand-list.
14147 asm-operand-list:
14148 asm-operand
14149 asm-operand-list , asm-operand
14151 asm-operand:
14152 string-literal ( expression )
14153 [ string-literal ] string-literal ( expression )
14155 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14156 each node is the expression. The TREE_PURPOSE is itself a
14157 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14158 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14159 is a STRING_CST for the string literal before the parenthesis. */
14161 static tree
14162 cp_parser_asm_operand_list (cp_parser* parser)
14164 tree asm_operands = NULL_TREE;
14166 while (true)
14168 tree string_literal;
14169 tree expression;
14170 tree name;
14172 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14174 /* Consume the `[' token. */
14175 cp_lexer_consume_token (parser->lexer);
14176 /* Read the operand name. */
14177 name = cp_parser_identifier (parser);
14178 if (name != error_mark_node)
14179 name = build_string (IDENTIFIER_LENGTH (name),
14180 IDENTIFIER_POINTER (name));
14181 /* Look for the closing `]'. */
14182 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14184 else
14185 name = NULL_TREE;
14186 /* Look for the string-literal. */
14187 string_literal = cp_parser_string_literal (parser, false, false);
14189 /* Look for the `('. */
14190 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14191 /* Parse the expression. */
14192 expression = cp_parser_expression (parser, /*cast_p=*/false);
14193 /* Look for the `)'. */
14194 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14196 /* Add this operand to the list. */
14197 asm_operands = tree_cons (build_tree_list (name, string_literal),
14198 expression,
14199 asm_operands);
14200 /* If the next token is not a `,', there are no more
14201 operands. */
14202 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14203 break;
14204 /* Consume the `,'. */
14205 cp_lexer_consume_token (parser->lexer);
14208 return nreverse (asm_operands);
14211 /* Parse an asm-clobber-list.
14213 asm-clobber-list:
14214 string-literal
14215 asm-clobber-list , string-literal
14217 Returns a TREE_LIST, indicating the clobbers in the order that they
14218 appeared. The TREE_VALUE of each node is a STRING_CST. */
14220 static tree
14221 cp_parser_asm_clobber_list (cp_parser* parser)
14223 tree clobbers = NULL_TREE;
14225 while (true)
14227 tree string_literal;
14229 /* Look for the string literal. */
14230 string_literal = cp_parser_string_literal (parser, false, false);
14231 /* Add it to the list. */
14232 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14233 /* If the next token is not a `,', then the list is
14234 complete. */
14235 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14236 break;
14237 /* Consume the `,' token. */
14238 cp_lexer_consume_token (parser->lexer);
14241 return clobbers;
14244 /* Parse an (optional) series of attributes.
14246 attributes:
14247 attributes attribute
14249 attribute:
14250 __attribute__ (( attribute-list [opt] ))
14252 The return value is as for cp_parser_attribute_list. */
14254 static tree
14255 cp_parser_attributes_opt (cp_parser* parser)
14257 tree attributes = NULL_TREE;
14259 while (true)
14261 cp_token *token;
14262 tree attribute_list;
14264 /* Peek at the next token. */
14265 token = cp_lexer_peek_token (parser->lexer);
14266 /* If it's not `__attribute__', then we're done. */
14267 if (token->keyword != RID_ATTRIBUTE)
14268 break;
14270 /* Consume the `__attribute__' keyword. */
14271 cp_lexer_consume_token (parser->lexer);
14272 /* Look for the two `(' tokens. */
14273 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14274 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14276 /* Peek at the next token. */
14277 token = cp_lexer_peek_token (parser->lexer);
14278 if (token->type != CPP_CLOSE_PAREN)
14279 /* Parse the attribute-list. */
14280 attribute_list = cp_parser_attribute_list (parser);
14281 else
14282 /* If the next token is a `)', then there is no attribute
14283 list. */
14284 attribute_list = NULL;
14286 /* Look for the two `)' tokens. */
14287 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14288 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14290 /* Add these new attributes to the list. */
14291 attributes = chainon (attributes, attribute_list);
14294 return attributes;
14297 /* Parse an attribute-list.
14299 attribute-list:
14300 attribute
14301 attribute-list , attribute
14303 attribute:
14304 identifier
14305 identifier ( identifier )
14306 identifier ( identifier , expression-list )
14307 identifier ( expression-list )
14309 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14310 to an attribute. The TREE_PURPOSE of each node is the identifier
14311 indicating which attribute is in use. The TREE_VALUE represents
14312 the arguments, if any. */
14314 static tree
14315 cp_parser_attribute_list (cp_parser* parser)
14317 tree attribute_list = NULL_TREE;
14318 bool save_translate_strings_p = parser->translate_strings_p;
14320 parser->translate_strings_p = false;
14321 while (true)
14323 cp_token *token;
14324 tree identifier;
14325 tree attribute;
14327 /* Look for the identifier. We also allow keywords here; for
14328 example `__attribute__ ((const))' is legal. */
14329 token = cp_lexer_peek_token (parser->lexer);
14330 if (token->type == CPP_NAME
14331 || token->type == CPP_KEYWORD)
14333 /* Consume the token. */
14334 token = cp_lexer_consume_token (parser->lexer);
14336 /* Save away the identifier that indicates which attribute
14337 this is. */
14338 identifier = token->value;
14339 attribute = build_tree_list (identifier, NULL_TREE);
14341 /* Peek at the next token. */
14342 token = cp_lexer_peek_token (parser->lexer);
14343 /* If it's an `(', then parse the attribute arguments. */
14344 if (token->type == CPP_OPEN_PAREN)
14346 tree arguments;
14348 arguments = (cp_parser_parenthesized_expression_list
14349 (parser, true, /*cast_p=*/false,
14350 /*non_constant_p=*/NULL));
14351 /* Save the identifier and arguments away. */
14352 TREE_VALUE (attribute) = arguments;
14355 /* Add this attribute to the list. */
14356 TREE_CHAIN (attribute) = attribute_list;
14357 attribute_list = attribute;
14359 token = cp_lexer_peek_token (parser->lexer);
14361 /* Now, look for more attributes. If the next token isn't a
14362 `,', we're done. */
14363 if (token->type != CPP_COMMA)
14364 break;
14366 /* Consume the comma and keep going. */
14367 cp_lexer_consume_token (parser->lexer);
14369 parser->translate_strings_p = save_translate_strings_p;
14371 /* We built up the list in reverse order. */
14372 return nreverse (attribute_list);
14375 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14376 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14377 current value of the PEDANTIC flag, regardless of whether or not
14378 the `__extension__' keyword is present. The caller is responsible
14379 for restoring the value of the PEDANTIC flag. */
14381 static bool
14382 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14384 /* Save the old value of the PEDANTIC flag. */
14385 *saved_pedantic = pedantic;
14387 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14389 /* Consume the `__extension__' token. */
14390 cp_lexer_consume_token (parser->lexer);
14391 /* We're not being pedantic while the `__extension__' keyword is
14392 in effect. */
14393 pedantic = 0;
14395 return true;
14398 return false;
14401 /* Parse a label declaration.
14403 label-declaration:
14404 __label__ label-declarator-seq ;
14406 label-declarator-seq:
14407 identifier , label-declarator-seq
14408 identifier */
14410 static void
14411 cp_parser_label_declaration (cp_parser* parser)
14413 /* Look for the `__label__' keyword. */
14414 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14416 while (true)
14418 tree identifier;
14420 /* Look for an identifier. */
14421 identifier = cp_parser_identifier (parser);
14422 /* If we failed, stop. */
14423 if (identifier == error_mark_node)
14424 break;
14425 /* Declare it as a label. */
14426 finish_label_decl (identifier);
14427 /* If the next token is a `;', stop. */
14428 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14429 break;
14430 /* Look for the `,' separating the label declarations. */
14431 cp_parser_require (parser, CPP_COMMA, "`,'");
14434 /* Look for the final `;'. */
14435 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14438 /* Support Functions */
14440 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14441 NAME should have one of the representations used for an
14442 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14443 is returned. If PARSER->SCOPE is a dependent type, then a
14444 SCOPE_REF is returned.
14446 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14447 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14448 was formed. Abstractly, such entities should not be passed to this
14449 function, because they do not need to be looked up, but it is
14450 simpler to check for this special case here, rather than at the
14451 call-sites.
14453 In cases not explicitly covered above, this function returns a
14454 DECL, OVERLOAD, or baselink representing the result of the lookup.
14455 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14456 is returned.
14458 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14459 (e.g., "struct") that was used. In that case bindings that do not
14460 refer to types are ignored.
14462 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14463 ignored.
14465 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14466 are ignored.
14468 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14469 types.
14471 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14472 results in an ambiguity, and false otherwise. */
14474 static tree
14475 cp_parser_lookup_name (cp_parser *parser, tree name,
14476 enum tag_types tag_type,
14477 bool is_template, bool is_namespace,
14478 bool check_dependency,
14479 bool *ambiguous_p)
14481 int flags = 0;
14482 tree decl;
14483 tree object_type = parser->context->object_type;
14485 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14486 flags |= LOOKUP_COMPLAIN;
14488 /* Assume that the lookup will be unambiguous. */
14489 if (ambiguous_p)
14490 *ambiguous_p = false;
14492 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14493 no longer valid. Note that if we are parsing tentatively, and
14494 the parse fails, OBJECT_TYPE will be automatically restored. */
14495 parser->context->object_type = NULL_TREE;
14497 if (name == error_mark_node)
14498 return error_mark_node;
14500 /* A template-id has already been resolved; there is no lookup to
14501 do. */
14502 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14503 return name;
14504 if (BASELINK_P (name))
14506 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14507 == TEMPLATE_ID_EXPR);
14508 return name;
14511 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14512 it should already have been checked to make sure that the name
14513 used matches the type being destroyed. */
14514 if (TREE_CODE (name) == BIT_NOT_EXPR)
14516 tree type;
14518 /* Figure out to which type this destructor applies. */
14519 if (parser->scope)
14520 type = parser->scope;
14521 else if (object_type)
14522 type = object_type;
14523 else
14524 type = current_class_type;
14525 /* If that's not a class type, there is no destructor. */
14526 if (!type || !CLASS_TYPE_P (type))
14527 return error_mark_node;
14528 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14529 lazily_declare_fn (sfk_destructor, type);
14530 if (!CLASSTYPE_DESTRUCTORS (type))
14531 return error_mark_node;
14532 /* If it was a class type, return the destructor. */
14533 return CLASSTYPE_DESTRUCTORS (type);
14536 /* By this point, the NAME should be an ordinary identifier. If
14537 the id-expression was a qualified name, the qualifying scope is
14538 stored in PARSER->SCOPE at this point. */
14539 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14541 /* Perform the lookup. */
14542 if (parser->scope)
14544 bool dependent_p;
14546 if (parser->scope == error_mark_node)
14547 return error_mark_node;
14549 /* If the SCOPE is dependent, the lookup must be deferred until
14550 the template is instantiated -- unless we are explicitly
14551 looking up names in uninstantiated templates. Even then, we
14552 cannot look up the name if the scope is not a class type; it
14553 might, for example, be a template type parameter. */
14554 dependent_p = (TYPE_P (parser->scope)
14555 && !(parser->in_declarator_p
14556 && currently_open_class (parser->scope))
14557 && dependent_type_p (parser->scope));
14558 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14559 && dependent_p)
14561 if (tag_type)
14563 tree type;
14565 /* The resolution to Core Issue 180 says that `struct
14566 A::B' should be considered a type-name, even if `A'
14567 is dependent. */
14568 type = make_typename_type (parser->scope, name, tag_type,
14569 /*complain=*/1);
14570 decl = TYPE_NAME (type);
14572 else if (is_template)
14573 decl = make_unbound_class_template (parser->scope,
14574 name, NULL_TREE,
14575 /*complain=*/1);
14576 else
14577 decl = build_nt (SCOPE_REF, parser->scope, name);
14579 else
14581 tree pushed_scope = NULL_TREE;
14583 /* If PARSER->SCOPE is a dependent type, then it must be a
14584 class type, and we must not be checking dependencies;
14585 otherwise, we would have processed this lookup above. So
14586 that PARSER->SCOPE is not considered a dependent base by
14587 lookup_member, we must enter the scope here. */
14588 if (dependent_p)
14589 pushed_scope = push_scope (parser->scope);
14590 /* If the PARSER->SCOPE is a template specialization, it
14591 may be instantiated during name lookup. In that case,
14592 errors may be issued. Even if we rollback the current
14593 tentative parse, those errors are valid. */
14594 decl = lookup_qualified_name (parser->scope, name,
14595 tag_type != none_type,
14596 /*complain=*/true);
14597 if (pushed_scope)
14598 pop_scope (pushed_scope);
14600 parser->qualifying_scope = parser->scope;
14601 parser->object_scope = NULL_TREE;
14603 else if (object_type)
14605 tree object_decl = NULL_TREE;
14606 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14607 OBJECT_TYPE is not a class. */
14608 if (CLASS_TYPE_P (object_type))
14609 /* If the OBJECT_TYPE is a template specialization, it may
14610 be instantiated during name lookup. In that case, errors
14611 may be issued. Even if we rollback the current tentative
14612 parse, those errors are valid. */
14613 object_decl = lookup_member (object_type,
14614 name,
14615 /*protect=*/0,
14616 tag_type != none_type);
14617 /* Look it up in the enclosing context, too. */
14618 decl = lookup_name_real (name, tag_type != none_type,
14619 /*nonclass=*/0,
14620 /*block_p=*/true, is_namespace, flags);
14621 parser->object_scope = object_type;
14622 parser->qualifying_scope = NULL_TREE;
14623 if (object_decl)
14624 decl = object_decl;
14626 else
14628 decl = lookup_name_real (name, tag_type != none_type,
14629 /*nonclass=*/0,
14630 /*block_p=*/true, is_namespace, flags);
14631 parser->qualifying_scope = NULL_TREE;
14632 parser->object_scope = NULL_TREE;
14635 /* If the lookup failed, let our caller know. */
14636 if (!decl || decl == error_mark_node)
14637 return error_mark_node;
14639 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14640 if (TREE_CODE (decl) == TREE_LIST)
14642 if (ambiguous_p)
14643 *ambiguous_p = true;
14644 /* The error message we have to print is too complicated for
14645 cp_parser_error, so we incorporate its actions directly. */
14646 if (!cp_parser_simulate_error (parser))
14648 error ("reference to %qD is ambiguous", name);
14649 print_candidates (decl);
14651 return error_mark_node;
14654 gcc_assert (DECL_P (decl)
14655 || TREE_CODE (decl) == OVERLOAD
14656 || TREE_CODE (decl) == SCOPE_REF
14657 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14658 || BASELINK_P (decl));
14660 /* If we have resolved the name of a member declaration, check to
14661 see if the declaration is accessible. When the name resolves to
14662 set of overloaded functions, accessibility is checked when
14663 overload resolution is done.
14665 During an explicit instantiation, access is not checked at all,
14666 as per [temp.explicit]. */
14667 if (DECL_P (decl))
14668 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14670 return decl;
14673 /* Like cp_parser_lookup_name, but for use in the typical case where
14674 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14675 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14677 static tree
14678 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14680 return cp_parser_lookup_name (parser, name,
14681 none_type,
14682 /*is_template=*/false,
14683 /*is_namespace=*/false,
14684 /*check_dependency=*/true,
14685 /*ambiguous_p=*/NULL);
14688 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14689 the current context, return the TYPE_DECL. If TAG_NAME_P is
14690 true, the DECL indicates the class being defined in a class-head,
14691 or declared in an elaborated-type-specifier.
14693 Otherwise, return DECL. */
14695 static tree
14696 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14698 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14699 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14701 struct A {
14702 template <typename T> struct B;
14705 template <typename T> struct A::B {};
14707 Similarly, in an elaborated-type-specifier:
14709 namespace N { struct X{}; }
14711 struct A {
14712 template <typename T> friend struct N::X;
14715 However, if the DECL refers to a class type, and we are in
14716 the scope of the class, then the name lookup automatically
14717 finds the TYPE_DECL created by build_self_reference rather
14718 than a TEMPLATE_DECL. For example, in:
14720 template <class T> struct S {
14721 S s;
14724 there is no need to handle such case. */
14726 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14727 return DECL_TEMPLATE_RESULT (decl);
14729 return decl;
14732 /* If too many, or too few, template-parameter lists apply to the
14733 declarator, issue an error message. Returns TRUE if all went well,
14734 and FALSE otherwise. */
14736 static bool
14737 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14738 cp_declarator *declarator)
14740 unsigned num_templates;
14742 /* We haven't seen any classes that involve template parameters yet. */
14743 num_templates = 0;
14745 switch (declarator->kind)
14747 case cdk_id:
14748 if (declarator->u.id.qualifying_scope)
14750 tree scope;
14751 tree member;
14753 scope = declarator->u.id.qualifying_scope;
14754 member = declarator->u.id.unqualified_name;
14756 while (scope && CLASS_TYPE_P (scope))
14758 /* You're supposed to have one `template <...>'
14759 for every template class, but you don't need one
14760 for a full specialization. For example:
14762 template <class T> struct S{};
14763 template <> struct S<int> { void f(); };
14764 void S<int>::f () {}
14766 is correct; there shouldn't be a `template <>' for
14767 the definition of `S<int>::f'. */
14768 if (CLASSTYPE_TEMPLATE_INFO (scope)
14769 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14770 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14771 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14772 ++num_templates;
14774 scope = TYPE_CONTEXT (scope);
14777 else if (TREE_CODE (declarator->u.id.unqualified_name)
14778 == TEMPLATE_ID_EXPR)
14779 /* If the DECLARATOR has the form `X<y>' then it uses one
14780 additional level of template parameters. */
14781 ++num_templates;
14783 return cp_parser_check_template_parameters (parser,
14784 num_templates);
14786 case cdk_function:
14787 case cdk_array:
14788 case cdk_pointer:
14789 case cdk_reference:
14790 case cdk_ptrmem:
14791 return (cp_parser_check_declarator_template_parameters
14792 (parser, declarator->declarator));
14794 case cdk_error:
14795 return true;
14797 default:
14798 gcc_unreachable ();
14800 return false;
14803 /* NUM_TEMPLATES were used in the current declaration. If that is
14804 invalid, return FALSE and issue an error messages. Otherwise,
14805 return TRUE. */
14807 static bool
14808 cp_parser_check_template_parameters (cp_parser* parser,
14809 unsigned num_templates)
14811 /* If there are more template classes than parameter lists, we have
14812 something like:
14814 template <class T> void S<T>::R<T>::f (); */
14815 if (parser->num_template_parameter_lists < num_templates)
14817 error ("too few template-parameter-lists");
14818 return false;
14820 /* If there are the same number of template classes and parameter
14821 lists, that's OK. */
14822 if (parser->num_template_parameter_lists == num_templates)
14823 return true;
14824 /* If there are more, but only one more, then we are referring to a
14825 member template. That's OK too. */
14826 if (parser->num_template_parameter_lists == num_templates + 1)
14827 return true;
14828 /* Otherwise, there are too many template parameter lists. We have
14829 something like:
14831 template <class T> template <class U> void S::f(); */
14832 error ("too many template-parameter-lists");
14833 return false;
14836 /* Parse an optional `::' token indicating that the following name is
14837 from the global namespace. If so, PARSER->SCOPE is set to the
14838 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14839 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14840 Returns the new value of PARSER->SCOPE, if the `::' token is
14841 present, and NULL_TREE otherwise. */
14843 static tree
14844 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14846 cp_token *token;
14848 /* Peek at the next token. */
14849 token = cp_lexer_peek_token (parser->lexer);
14850 /* If we're looking at a `::' token then we're starting from the
14851 global namespace, not our current location. */
14852 if (token->type == CPP_SCOPE)
14854 /* Consume the `::' token. */
14855 cp_lexer_consume_token (parser->lexer);
14856 /* Set the SCOPE so that we know where to start the lookup. */
14857 parser->scope = global_namespace;
14858 parser->qualifying_scope = global_namespace;
14859 parser->object_scope = NULL_TREE;
14861 return parser->scope;
14863 else if (!current_scope_valid_p)
14865 parser->scope = NULL_TREE;
14866 parser->qualifying_scope = NULL_TREE;
14867 parser->object_scope = NULL_TREE;
14870 return NULL_TREE;
14873 /* Returns TRUE if the upcoming token sequence is the start of a
14874 constructor declarator. If FRIEND_P is true, the declarator is
14875 preceded by the `friend' specifier. */
14877 static bool
14878 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14880 bool constructor_p;
14881 tree type_decl = NULL_TREE;
14882 bool nested_name_p;
14883 cp_token *next_token;
14885 /* The common case is that this is not a constructor declarator, so
14886 try to avoid doing lots of work if at all possible. It's not
14887 valid declare a constructor at function scope. */
14888 if (at_function_scope_p ())
14889 return false;
14890 /* And only certain tokens can begin a constructor declarator. */
14891 next_token = cp_lexer_peek_token (parser->lexer);
14892 if (next_token->type != CPP_NAME
14893 && next_token->type != CPP_SCOPE
14894 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14895 && next_token->type != CPP_TEMPLATE_ID)
14896 return false;
14898 /* Parse tentatively; we are going to roll back all of the tokens
14899 consumed here. */
14900 cp_parser_parse_tentatively (parser);
14901 /* Assume that we are looking at a constructor declarator. */
14902 constructor_p = true;
14904 /* Look for the optional `::' operator. */
14905 cp_parser_global_scope_opt (parser,
14906 /*current_scope_valid_p=*/false);
14907 /* Look for the nested-name-specifier. */
14908 nested_name_p
14909 = (cp_parser_nested_name_specifier_opt (parser,
14910 /*typename_keyword_p=*/false,
14911 /*check_dependency_p=*/false,
14912 /*type_p=*/false,
14913 /*is_declaration=*/false)
14914 != NULL_TREE);
14915 /* Outside of a class-specifier, there must be a
14916 nested-name-specifier. */
14917 if (!nested_name_p &&
14918 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14919 || friend_p))
14920 constructor_p = false;
14921 /* If we still think that this might be a constructor-declarator,
14922 look for a class-name. */
14923 if (constructor_p)
14925 /* If we have:
14927 template <typename T> struct S { S(); };
14928 template <typename T> S<T>::S ();
14930 we must recognize that the nested `S' names a class.
14931 Similarly, for:
14933 template <typename T> S<T>::S<T> ();
14935 we must recognize that the nested `S' names a template. */
14936 type_decl = cp_parser_class_name (parser,
14937 /*typename_keyword_p=*/false,
14938 /*template_keyword_p=*/false,
14939 none_type,
14940 /*check_dependency_p=*/false,
14941 /*class_head_p=*/false,
14942 /*is_declaration=*/false);
14943 /* If there was no class-name, then this is not a constructor. */
14944 constructor_p = !cp_parser_error_occurred (parser);
14947 /* If we're still considering a constructor, we have to see a `(',
14948 to begin the parameter-declaration-clause, followed by either a
14949 `)', an `...', or a decl-specifier. We need to check for a
14950 type-specifier to avoid being fooled into thinking that:
14952 S::S (f) (int);
14954 is a constructor. (It is actually a function named `f' that
14955 takes one parameter (of type `int') and returns a value of type
14956 `S::S'. */
14957 if (constructor_p
14958 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14961 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14962 /* A parameter declaration begins with a decl-specifier,
14963 which is either the "attribute" keyword, a storage class
14964 specifier, or (usually) a type-specifier. */
14965 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14966 && !cp_parser_storage_class_specifier_opt (parser))
14968 tree type;
14969 tree pushed_scope = NULL_TREE;
14970 unsigned saved_num_template_parameter_lists;
14972 /* Names appearing in the type-specifier should be looked up
14973 in the scope of the class. */
14974 if (current_class_type)
14975 type = NULL_TREE;
14976 else
14978 type = TREE_TYPE (type_decl);
14979 if (TREE_CODE (type) == TYPENAME_TYPE)
14981 type = resolve_typename_type (type,
14982 /*only_current_p=*/false);
14983 if (type == error_mark_node)
14985 cp_parser_abort_tentative_parse (parser);
14986 return false;
14989 pushed_scope = push_scope (type);
14992 /* Inside the constructor parameter list, surrounding
14993 template-parameter-lists do not apply. */
14994 saved_num_template_parameter_lists
14995 = parser->num_template_parameter_lists;
14996 parser->num_template_parameter_lists = 0;
14998 /* Look for the type-specifier. */
14999 cp_parser_type_specifier (parser,
15000 CP_PARSER_FLAGS_NONE,
15001 /*decl_specs=*/NULL,
15002 /*is_declarator=*/true,
15003 /*declares_class_or_enum=*/NULL,
15004 /*is_cv_qualifier=*/NULL);
15006 parser->num_template_parameter_lists
15007 = saved_num_template_parameter_lists;
15009 /* Leave the scope of the class. */
15010 if (pushed_scope)
15011 pop_scope (pushed_scope);
15013 constructor_p = !cp_parser_error_occurred (parser);
15016 else
15017 constructor_p = false;
15018 /* We did not really want to consume any tokens. */
15019 cp_parser_abort_tentative_parse (parser);
15021 return constructor_p;
15024 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15025 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15026 they must be performed once we are in the scope of the function.
15028 Returns the function defined. */
15030 static tree
15031 cp_parser_function_definition_from_specifiers_and_declarator
15032 (cp_parser* parser,
15033 cp_decl_specifier_seq *decl_specifiers,
15034 tree attributes,
15035 const cp_declarator *declarator)
15037 tree fn;
15038 bool success_p;
15040 /* Begin the function-definition. */
15041 success_p = start_function (decl_specifiers, declarator, attributes);
15043 /* The things we're about to see are not directly qualified by any
15044 template headers we've seen thus far. */
15045 reset_specialization ();
15047 /* If there were names looked up in the decl-specifier-seq that we
15048 did not check, check them now. We must wait until we are in the
15049 scope of the function to perform the checks, since the function
15050 might be a friend. */
15051 perform_deferred_access_checks ();
15053 if (!success_p)
15055 /* Skip the entire function. */
15056 error ("invalid function declaration");
15057 cp_parser_skip_to_end_of_block_or_statement (parser);
15058 fn = error_mark_node;
15060 else
15061 fn = cp_parser_function_definition_after_declarator (parser,
15062 /*inline_p=*/false);
15064 return fn;
15067 /* Parse the part of a function-definition that follows the
15068 declarator. INLINE_P is TRUE iff this function is an inline
15069 function defined with a class-specifier.
15071 Returns the function defined. */
15073 static tree
15074 cp_parser_function_definition_after_declarator (cp_parser* parser,
15075 bool inline_p)
15077 tree fn;
15078 bool ctor_initializer_p = false;
15079 bool saved_in_unbraced_linkage_specification_p;
15080 unsigned saved_num_template_parameter_lists;
15082 /* If the next token is `return', then the code may be trying to
15083 make use of the "named return value" extension that G++ used to
15084 support. */
15085 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15087 /* Consume the `return' keyword. */
15088 cp_lexer_consume_token (parser->lexer);
15089 /* Look for the identifier that indicates what value is to be
15090 returned. */
15091 cp_parser_identifier (parser);
15092 /* Issue an error message. */
15093 error ("named return values are no longer supported");
15094 /* Skip tokens until we reach the start of the function body. */
15095 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15096 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15097 cp_lexer_consume_token (parser->lexer);
15099 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15100 anything declared inside `f'. */
15101 saved_in_unbraced_linkage_specification_p
15102 = parser->in_unbraced_linkage_specification_p;
15103 parser->in_unbraced_linkage_specification_p = false;
15104 /* Inside the function, surrounding template-parameter-lists do not
15105 apply. */
15106 saved_num_template_parameter_lists
15107 = parser->num_template_parameter_lists;
15108 parser->num_template_parameter_lists = 0;
15109 /* If the next token is `try', then we are looking at a
15110 function-try-block. */
15111 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15112 ctor_initializer_p = cp_parser_function_try_block (parser);
15113 /* A function-try-block includes the function-body, so we only do
15114 this next part if we're not processing a function-try-block. */
15115 else
15116 ctor_initializer_p
15117 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15119 /* Finish the function. */
15120 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15121 (inline_p ? 2 : 0));
15122 /* Generate code for it, if necessary. */
15123 expand_or_defer_fn (fn);
15124 /* Restore the saved values. */
15125 parser->in_unbraced_linkage_specification_p
15126 = saved_in_unbraced_linkage_specification_p;
15127 parser->num_template_parameter_lists
15128 = saved_num_template_parameter_lists;
15130 return fn;
15133 /* Parse a template-declaration, assuming that the `export' (and
15134 `extern') keywords, if present, has already been scanned. MEMBER_P
15135 is as for cp_parser_template_declaration. */
15137 static void
15138 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15140 tree decl = NULL_TREE;
15141 tree parameter_list;
15142 bool friend_p = false;
15144 /* Look for the `template' keyword. */
15145 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15146 return;
15148 /* And the `<'. */
15149 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15150 return;
15152 /* If the next token is `>', then we have an invalid
15153 specialization. Rather than complain about an invalid template
15154 parameter, issue an error message here. */
15155 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15157 cp_parser_error (parser, "invalid explicit specialization");
15158 begin_specialization ();
15159 parameter_list = NULL_TREE;
15161 else
15163 /* Parse the template parameters. */
15164 begin_template_parm_list ();
15165 parameter_list = cp_parser_template_parameter_list (parser);
15166 parameter_list = end_template_parm_list (parameter_list);
15169 /* Look for the `>'. */
15170 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15171 /* We just processed one more parameter list. */
15172 ++parser->num_template_parameter_lists;
15173 /* If the next token is `template', there are more template
15174 parameters. */
15175 if (cp_lexer_next_token_is_keyword (parser->lexer,
15176 RID_TEMPLATE))
15177 cp_parser_template_declaration_after_export (parser, member_p);
15178 else
15180 /* There are no access checks when parsing a template, as we do not
15181 know if a specialization will be a friend. */
15182 push_deferring_access_checks (dk_no_check);
15184 decl = cp_parser_single_declaration (parser,
15185 member_p,
15186 &friend_p);
15188 pop_deferring_access_checks ();
15190 /* If this is a member template declaration, let the front
15191 end know. */
15192 if (member_p && !friend_p && decl)
15194 if (TREE_CODE (decl) == TYPE_DECL)
15195 cp_parser_check_access_in_redeclaration (decl);
15197 decl = finish_member_template_decl (decl);
15199 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15200 make_friend_class (current_class_type, TREE_TYPE (decl),
15201 /*complain=*/true);
15203 /* We are done with the current parameter list. */
15204 --parser->num_template_parameter_lists;
15206 /* Finish up. */
15207 finish_template_decl (parameter_list);
15209 /* Register member declarations. */
15210 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15211 finish_member_declaration (decl);
15213 /* If DECL is a function template, we must return to parse it later.
15214 (Even though there is no definition, there might be default
15215 arguments that need handling.) */
15216 if (member_p && decl
15217 && (TREE_CODE (decl) == FUNCTION_DECL
15218 || DECL_FUNCTION_TEMPLATE_P (decl)))
15219 TREE_VALUE (parser->unparsed_functions_queues)
15220 = tree_cons (NULL_TREE, decl,
15221 TREE_VALUE (parser->unparsed_functions_queues));
15224 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15225 `function-definition' sequence. MEMBER_P is true, this declaration
15226 appears in a class scope.
15228 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15229 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15231 static tree
15232 cp_parser_single_declaration (cp_parser* parser,
15233 bool member_p,
15234 bool* friend_p)
15236 int declares_class_or_enum;
15237 tree decl = NULL_TREE;
15238 cp_decl_specifier_seq decl_specifiers;
15239 bool function_definition_p = false;
15241 /* This function is only used when processing a template
15242 declaration. */
15243 gcc_assert (innermost_scope_kind () == sk_template_parms
15244 || innermost_scope_kind () == sk_template_spec);
15246 /* Defer access checks until we know what is being declared. */
15247 push_deferring_access_checks (dk_deferred);
15249 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15250 alternative. */
15251 cp_parser_decl_specifier_seq (parser,
15252 CP_PARSER_FLAGS_OPTIONAL,
15253 &decl_specifiers,
15254 &declares_class_or_enum);
15255 if (friend_p)
15256 *friend_p = cp_parser_friend_p (&decl_specifiers);
15258 /* There are no template typedefs. */
15259 if (decl_specifiers.specs[(int) ds_typedef])
15261 error ("template declaration of %qs", "typedef");
15262 decl = error_mark_node;
15265 /* Gather up the access checks that occurred the
15266 decl-specifier-seq. */
15267 stop_deferring_access_checks ();
15269 /* Check for the declaration of a template class. */
15270 if (declares_class_or_enum)
15272 if (cp_parser_declares_only_class_p (parser))
15274 decl = shadow_tag (&decl_specifiers);
15276 /* In this case:
15278 struct C {
15279 friend template <typename T> struct A<T>::B;
15282 A<T>::B will be represented by a TYPENAME_TYPE, and
15283 therefore not recognized by shadow_tag. */
15284 if (friend_p && *friend_p
15285 && !decl
15286 && decl_specifiers.type
15287 && TYPE_P (decl_specifiers.type))
15288 decl = decl_specifiers.type;
15290 if (decl && decl != error_mark_node)
15291 decl = TYPE_NAME (decl);
15292 else
15293 decl = error_mark_node;
15296 /* If it's not a template class, try for a template function. If
15297 the next token is a `;', then this declaration does not declare
15298 anything. But, if there were errors in the decl-specifiers, then
15299 the error might well have come from an attempted class-specifier.
15300 In that case, there's no need to warn about a missing declarator. */
15301 if (!decl
15302 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15303 || decl_specifiers.type != error_mark_node))
15304 decl = cp_parser_init_declarator (parser,
15305 &decl_specifiers,
15306 /*function_definition_allowed_p=*/true,
15307 member_p,
15308 declares_class_or_enum,
15309 &function_definition_p);
15311 pop_deferring_access_checks ();
15313 /* Clear any current qualification; whatever comes next is the start
15314 of something new. */
15315 parser->scope = NULL_TREE;
15316 parser->qualifying_scope = NULL_TREE;
15317 parser->object_scope = NULL_TREE;
15318 /* Look for a trailing `;' after the declaration. */
15319 if (!function_definition_p
15320 && (decl == error_mark_node
15321 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15322 cp_parser_skip_to_end_of_block_or_statement (parser);
15324 return decl;
15327 /* Parse a cast-expression that is not the operand of a unary "&". */
15329 static tree
15330 cp_parser_simple_cast_expression (cp_parser *parser)
15332 return cp_parser_cast_expression (parser, /*address_p=*/false,
15333 /*cast_p=*/false);
15336 /* Parse a functional cast to TYPE. Returns an expression
15337 representing the cast. */
15339 static tree
15340 cp_parser_functional_cast (cp_parser* parser, tree type)
15342 tree expression_list;
15343 tree cast;
15345 expression_list
15346 = cp_parser_parenthesized_expression_list (parser, false,
15347 /*cast_p=*/true,
15348 /*non_constant_p=*/NULL);
15350 cast = build_functional_cast (type, expression_list);
15351 /* [expr.const]/1: In an integral constant expression "only type
15352 conversions to integral or enumeration type can be used". */
15353 if (cast != error_mark_node && !type_dependent_expression_p (type)
15354 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15356 if (cp_parser_non_integral_constant_expression
15357 (parser, "a call to a constructor"))
15358 return error_mark_node;
15360 return cast;
15363 /* Save the tokens that make up the body of a member function defined
15364 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15365 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15366 specifiers applied to the declaration. Returns the FUNCTION_DECL
15367 for the member function. */
15369 static tree
15370 cp_parser_save_member_function_body (cp_parser* parser,
15371 cp_decl_specifier_seq *decl_specifiers,
15372 cp_declarator *declarator,
15373 tree attributes)
15375 cp_token *first;
15376 cp_token *last;
15377 tree fn;
15379 /* Create the function-declaration. */
15380 fn = start_method (decl_specifiers, declarator, attributes);
15381 /* If something went badly wrong, bail out now. */
15382 if (fn == error_mark_node)
15384 /* If there's a function-body, skip it. */
15385 if (cp_parser_token_starts_function_definition_p
15386 (cp_lexer_peek_token (parser->lexer)))
15387 cp_parser_skip_to_end_of_block_or_statement (parser);
15388 return error_mark_node;
15391 /* Remember it, if there default args to post process. */
15392 cp_parser_save_default_args (parser, fn);
15394 /* Save away the tokens that make up the body of the
15395 function. */
15396 first = parser->lexer->next_token;
15397 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15398 /* Handle function try blocks. */
15399 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15400 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15401 last = parser->lexer->next_token;
15403 /* Save away the inline definition; we will process it when the
15404 class is complete. */
15405 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15406 DECL_PENDING_INLINE_P (fn) = 1;
15408 /* We need to know that this was defined in the class, so that
15409 friend templates are handled correctly. */
15410 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15412 /* We're done with the inline definition. */
15413 finish_method (fn);
15415 /* Add FN to the queue of functions to be parsed later. */
15416 TREE_VALUE (parser->unparsed_functions_queues)
15417 = tree_cons (NULL_TREE, fn,
15418 TREE_VALUE (parser->unparsed_functions_queues));
15420 return fn;
15423 /* Parse a template-argument-list, as well as the trailing ">" (but
15424 not the opening ">"). See cp_parser_template_argument_list for the
15425 return value. */
15427 static tree
15428 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15430 tree arguments;
15431 tree saved_scope;
15432 tree saved_qualifying_scope;
15433 tree saved_object_scope;
15434 bool saved_greater_than_is_operator_p;
15435 bool saved_skip_evaluation;
15437 /* [temp.names]
15439 When parsing a template-id, the first non-nested `>' is taken as
15440 the end of the template-argument-list rather than a greater-than
15441 operator. */
15442 saved_greater_than_is_operator_p
15443 = parser->greater_than_is_operator_p;
15444 parser->greater_than_is_operator_p = false;
15445 /* Parsing the argument list may modify SCOPE, so we save it
15446 here. */
15447 saved_scope = parser->scope;
15448 saved_qualifying_scope = parser->qualifying_scope;
15449 saved_object_scope = parser->object_scope;
15450 /* We need to evaluate the template arguments, even though this
15451 template-id may be nested within a "sizeof". */
15452 saved_skip_evaluation = skip_evaluation;
15453 skip_evaluation = false;
15454 /* Parse the template-argument-list itself. */
15455 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15456 arguments = NULL_TREE;
15457 else
15458 arguments = cp_parser_template_argument_list (parser);
15459 /* Look for the `>' that ends the template-argument-list. If we find
15460 a '>>' instead, it's probably just a typo. */
15461 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15463 if (!saved_greater_than_is_operator_p)
15465 /* If we're in a nested template argument list, the '>>' has
15466 to be a typo for '> >'. We emit the error message, but we
15467 continue parsing and we push a '>' as next token, so that
15468 the argument list will be parsed correctly. Note that the
15469 global source location is still on the token before the
15470 '>>', so we need to say explicitly where we want it. */
15471 cp_token *token = cp_lexer_peek_token (parser->lexer);
15472 error ("%H%<>>%> should be %<> >%> "
15473 "within a nested template argument list",
15474 &token->location);
15476 /* ??? Proper recovery should terminate two levels of
15477 template argument list here. */
15478 token->type = CPP_GREATER;
15480 else
15482 /* If this is not a nested template argument list, the '>>'
15483 is a typo for '>'. Emit an error message and continue.
15484 Same deal about the token location, but here we can get it
15485 right by consuming the '>>' before issuing the diagnostic. */
15486 cp_lexer_consume_token (parser->lexer);
15487 error ("spurious %<>>%>, use %<>%> to terminate "
15488 "a template argument list");
15491 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15492 error ("missing %<>%> to terminate the template argument list");
15493 else
15494 /* It's what we want, a '>'; consume it. */
15495 cp_lexer_consume_token (parser->lexer);
15496 /* The `>' token might be a greater-than operator again now. */
15497 parser->greater_than_is_operator_p
15498 = saved_greater_than_is_operator_p;
15499 /* Restore the SAVED_SCOPE. */
15500 parser->scope = saved_scope;
15501 parser->qualifying_scope = saved_qualifying_scope;
15502 parser->object_scope = saved_object_scope;
15503 skip_evaluation = saved_skip_evaluation;
15505 return arguments;
15508 /* MEMBER_FUNCTION is a member function, or a friend. If default
15509 arguments, or the body of the function have not yet been parsed,
15510 parse them now. */
15512 static void
15513 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15515 /* If this member is a template, get the underlying
15516 FUNCTION_DECL. */
15517 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15518 member_function = DECL_TEMPLATE_RESULT (member_function);
15520 /* There should not be any class definitions in progress at this
15521 point; the bodies of members are only parsed outside of all class
15522 definitions. */
15523 gcc_assert (parser->num_classes_being_defined == 0);
15524 /* While we're parsing the member functions we might encounter more
15525 classes. We want to handle them right away, but we don't want
15526 them getting mixed up with functions that are currently in the
15527 queue. */
15528 parser->unparsed_functions_queues
15529 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15531 /* Make sure that any template parameters are in scope. */
15532 maybe_begin_member_template_processing (member_function);
15534 /* If the body of the function has not yet been parsed, parse it
15535 now. */
15536 if (DECL_PENDING_INLINE_P (member_function))
15538 tree function_scope;
15539 cp_token_cache *tokens;
15541 /* The function is no longer pending; we are processing it. */
15542 tokens = DECL_PENDING_INLINE_INFO (member_function);
15543 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15544 DECL_PENDING_INLINE_P (member_function) = 0;
15546 /* If this is a local class, enter the scope of the containing
15547 function. */
15548 function_scope = current_function_decl;
15549 if (function_scope)
15550 push_function_context_to (function_scope);
15553 /* Push the body of the function onto the lexer stack. */
15554 cp_parser_push_lexer_for_tokens (parser, tokens);
15556 /* Let the front end know that we going to be defining this
15557 function. */
15558 start_preparsed_function (member_function, NULL_TREE,
15559 SF_PRE_PARSED | SF_INCLASS_INLINE);
15561 /* Don't do access checking if it is a templated function. */
15562 if (processing_template_decl)
15563 push_deferring_access_checks (dk_no_check);
15565 /* Now, parse the body of the function. */
15566 cp_parser_function_definition_after_declarator (parser,
15567 /*inline_p=*/true);
15569 if (processing_template_decl)
15570 pop_deferring_access_checks ();
15572 /* Leave the scope of the containing function. */
15573 if (function_scope)
15574 pop_function_context_from (function_scope);
15575 cp_parser_pop_lexer (parser);
15578 /* Remove any template parameters from the symbol table. */
15579 maybe_end_member_template_processing ();
15581 /* Restore the queue. */
15582 parser->unparsed_functions_queues
15583 = TREE_CHAIN (parser->unparsed_functions_queues);
15586 /* If DECL contains any default args, remember it on the unparsed
15587 functions queue. */
15589 static void
15590 cp_parser_save_default_args (cp_parser* parser, tree decl)
15592 tree probe;
15594 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15595 probe;
15596 probe = TREE_CHAIN (probe))
15597 if (TREE_PURPOSE (probe))
15599 TREE_PURPOSE (parser->unparsed_functions_queues)
15600 = tree_cons (current_class_type, decl,
15601 TREE_PURPOSE (parser->unparsed_functions_queues));
15602 break;
15604 return;
15607 /* FN is a FUNCTION_DECL which may contains a parameter with an
15608 unparsed DEFAULT_ARG. Parse the default args now. This function
15609 assumes that the current scope is the scope in which the default
15610 argument should be processed. */
15612 static void
15613 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15615 bool saved_local_variables_forbidden_p;
15616 tree parm;
15618 /* While we're parsing the default args, we might (due to the
15619 statement expression extension) encounter more classes. We want
15620 to handle them right away, but we don't want them getting mixed
15621 up with default args that are currently in the queue. */
15622 parser->unparsed_functions_queues
15623 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15625 /* Local variable names (and the `this' keyword) may not appear
15626 in a default argument. */
15627 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15628 parser->local_variables_forbidden_p = true;
15630 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15631 parm;
15632 parm = TREE_CHAIN (parm))
15634 cp_token_cache *tokens;
15635 tree default_arg = TREE_PURPOSE (parm);
15636 tree parsed_arg;
15637 VEC(tree,gc) *insts;
15638 tree copy;
15639 unsigned ix;
15641 if (!default_arg)
15642 continue;
15644 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15645 /* This can happen for a friend declaration for a function
15646 already declared with default arguments. */
15647 continue;
15649 /* Push the saved tokens for the default argument onto the parser's
15650 lexer stack. */
15651 tokens = DEFARG_TOKENS (default_arg);
15652 cp_parser_push_lexer_for_tokens (parser, tokens);
15654 /* Parse the assignment-expression. */
15655 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15657 TREE_PURPOSE (parm) = parsed_arg;
15659 /* Update any instantiations we've already created. */
15660 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15661 VEC_iterate (tree, insts, ix, copy); ix++)
15662 TREE_PURPOSE (copy) = parsed_arg;
15664 /* If the token stream has not been completely used up, then
15665 there was extra junk after the end of the default
15666 argument. */
15667 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15668 cp_parser_error (parser, "expected %<,%>");
15670 /* Revert to the main lexer. */
15671 cp_parser_pop_lexer (parser);
15674 /* Restore the state of local_variables_forbidden_p. */
15675 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15677 /* Restore the queue. */
15678 parser->unparsed_functions_queues
15679 = TREE_CHAIN (parser->unparsed_functions_queues);
15682 /* Parse the operand of `sizeof' (or a similar operator). Returns
15683 either a TYPE or an expression, depending on the form of the
15684 input. The KEYWORD indicates which kind of expression we have
15685 encountered. */
15687 static tree
15688 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15690 static const char *format;
15691 tree expr = NULL_TREE;
15692 const char *saved_message;
15693 bool saved_integral_constant_expression_p;
15694 bool saved_non_integral_constant_expression_p;
15696 /* Initialize FORMAT the first time we get here. */
15697 if (!format)
15698 format = "types may not be defined in '%s' expressions";
15700 /* Types cannot be defined in a `sizeof' expression. Save away the
15701 old message. */
15702 saved_message = parser->type_definition_forbidden_message;
15703 /* And create the new one. */
15704 parser->type_definition_forbidden_message
15705 = xmalloc (strlen (format)
15706 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15707 + 1 /* `\0' */);
15708 sprintf ((char *) parser->type_definition_forbidden_message,
15709 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15711 /* The restrictions on constant-expressions do not apply inside
15712 sizeof expressions. */
15713 saved_integral_constant_expression_p
15714 = parser->integral_constant_expression_p;
15715 saved_non_integral_constant_expression_p
15716 = parser->non_integral_constant_expression_p;
15717 parser->integral_constant_expression_p = false;
15719 /* Do not actually evaluate the expression. */
15720 ++skip_evaluation;
15721 /* If it's a `(', then we might be looking at the type-id
15722 construction. */
15723 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15725 tree type;
15726 bool saved_in_type_id_in_expr_p;
15728 /* We can't be sure yet whether we're looking at a type-id or an
15729 expression. */
15730 cp_parser_parse_tentatively (parser);
15731 /* Consume the `('. */
15732 cp_lexer_consume_token (parser->lexer);
15733 /* Parse the type-id. */
15734 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15735 parser->in_type_id_in_expr_p = true;
15736 type = cp_parser_type_id (parser);
15737 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15738 /* Now, look for the trailing `)'. */
15739 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15740 /* If all went well, then we're done. */
15741 if (cp_parser_parse_definitely (parser))
15743 cp_decl_specifier_seq decl_specs;
15745 /* Build a trivial decl-specifier-seq. */
15746 clear_decl_specs (&decl_specs);
15747 decl_specs.type = type;
15749 /* Call grokdeclarator to figure out what type this is. */
15750 expr = grokdeclarator (NULL,
15751 &decl_specs,
15752 TYPENAME,
15753 /*initialized=*/0,
15754 /*attrlist=*/NULL);
15758 /* If the type-id production did not work out, then we must be
15759 looking at the unary-expression production. */
15760 if (!expr)
15761 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15762 /*cast_p=*/false);
15763 /* Go back to evaluating expressions. */
15764 --skip_evaluation;
15766 /* Free the message we created. */
15767 free ((char *) parser->type_definition_forbidden_message);
15768 /* And restore the old one. */
15769 parser->type_definition_forbidden_message = saved_message;
15770 parser->integral_constant_expression_p
15771 = saved_integral_constant_expression_p;
15772 parser->non_integral_constant_expression_p
15773 = saved_non_integral_constant_expression_p;
15775 return expr;
15778 /* If the current declaration has no declarator, return true. */
15780 static bool
15781 cp_parser_declares_only_class_p (cp_parser *parser)
15783 /* If the next token is a `;' or a `,' then there is no
15784 declarator. */
15785 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15786 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15789 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15791 static void
15792 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15793 cp_storage_class storage_class)
15795 if (decl_specs->storage_class != sc_none)
15796 decl_specs->multiple_storage_classes_p = true;
15797 else
15798 decl_specs->storage_class = storage_class;
15801 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15802 is true, the type is a user-defined type; otherwise it is a
15803 built-in type specified by a keyword. */
15805 static void
15806 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15807 tree type_spec,
15808 bool user_defined_p)
15810 decl_specs->any_specifiers_p = true;
15812 /* If the user tries to redeclare bool or wchar_t (with, for
15813 example, in "typedef int wchar_t;") we remember that this is what
15814 happened. In system headers, we ignore these declarations so
15815 that G++ can work with system headers that are not C++-safe. */
15816 if (decl_specs->specs[(int) ds_typedef]
15817 && !user_defined_p
15818 && (type_spec == boolean_type_node
15819 || type_spec == wchar_type_node)
15820 && (decl_specs->type
15821 || decl_specs->specs[(int) ds_long]
15822 || decl_specs->specs[(int) ds_short]
15823 || decl_specs->specs[(int) ds_unsigned]
15824 || decl_specs->specs[(int) ds_signed]))
15826 decl_specs->redefined_builtin_type = type_spec;
15827 if (!decl_specs->type)
15829 decl_specs->type = type_spec;
15830 decl_specs->user_defined_type_p = false;
15833 else if (decl_specs->type)
15834 decl_specs->multiple_types_p = true;
15835 else
15837 decl_specs->type = type_spec;
15838 decl_specs->user_defined_type_p = user_defined_p;
15839 decl_specs->redefined_builtin_type = NULL_TREE;
15843 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15844 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15846 static bool
15847 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15849 return decl_specifiers->specs[(int) ds_friend] != 0;
15852 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15853 issue an error message indicating that TOKEN_DESC was expected.
15855 Returns the token consumed, if the token had the appropriate type.
15856 Otherwise, returns NULL. */
15858 static cp_token *
15859 cp_parser_require (cp_parser* parser,
15860 enum cpp_ttype type,
15861 const char* token_desc)
15863 if (cp_lexer_next_token_is (parser->lexer, type))
15864 return cp_lexer_consume_token (parser->lexer);
15865 else
15867 /* Output the MESSAGE -- unless we're parsing tentatively. */
15868 if (!cp_parser_simulate_error (parser))
15870 char *message = concat ("expected ", token_desc, NULL);
15871 cp_parser_error (parser, message);
15872 free (message);
15874 return NULL;
15878 /* Like cp_parser_require, except that tokens will be skipped until
15879 the desired token is found. An error message is still produced if
15880 the next token is not as expected. */
15882 static void
15883 cp_parser_skip_until_found (cp_parser* parser,
15884 enum cpp_ttype type,
15885 const char* token_desc)
15887 cp_token *token;
15888 unsigned nesting_depth = 0;
15890 if (cp_parser_require (parser, type, token_desc))
15891 return;
15893 /* Skip tokens until the desired token is found. */
15894 while (true)
15896 /* Peek at the next token. */
15897 token = cp_lexer_peek_token (parser->lexer);
15898 /* If we've reached the token we want, consume it and
15899 stop. */
15900 if (token->type == type && !nesting_depth)
15902 cp_lexer_consume_token (parser->lexer);
15903 return;
15905 /* If we've run out of tokens, stop. */
15906 if (token->type == CPP_EOF)
15907 return;
15908 if (token->type == CPP_OPEN_BRACE
15909 || token->type == CPP_OPEN_PAREN
15910 || token->type == CPP_OPEN_SQUARE)
15911 ++nesting_depth;
15912 else if (token->type == CPP_CLOSE_BRACE
15913 || token->type == CPP_CLOSE_PAREN
15914 || token->type == CPP_CLOSE_SQUARE)
15916 if (nesting_depth-- == 0)
15917 return;
15919 /* Consume this token. */
15920 cp_lexer_consume_token (parser->lexer);
15924 /* If the next token is the indicated keyword, consume it. Otherwise,
15925 issue an error message indicating that TOKEN_DESC was expected.
15927 Returns the token consumed, if the token had the appropriate type.
15928 Otherwise, returns NULL. */
15930 static cp_token *
15931 cp_parser_require_keyword (cp_parser* parser,
15932 enum rid keyword,
15933 const char* token_desc)
15935 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15937 if (token && token->keyword != keyword)
15939 dyn_string_t error_msg;
15941 /* Format the error message. */
15942 error_msg = dyn_string_new (0);
15943 dyn_string_append_cstr (error_msg, "expected ");
15944 dyn_string_append_cstr (error_msg, token_desc);
15945 cp_parser_error (parser, error_msg->s);
15946 dyn_string_delete (error_msg);
15947 return NULL;
15950 return token;
15953 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15954 function-definition. */
15956 static bool
15957 cp_parser_token_starts_function_definition_p (cp_token* token)
15959 return (/* An ordinary function-body begins with an `{'. */
15960 token->type == CPP_OPEN_BRACE
15961 /* A ctor-initializer begins with a `:'. */
15962 || token->type == CPP_COLON
15963 /* A function-try-block begins with `try'. */
15964 || token->keyword == RID_TRY
15965 /* The named return value extension begins with `return'. */
15966 || token->keyword == RID_RETURN);
15969 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15970 definition. */
15972 static bool
15973 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15975 cp_token *token;
15977 token = cp_lexer_peek_token (parser->lexer);
15978 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15981 /* Returns TRUE iff the next token is the "," or ">" ending a
15982 template-argument. */
15984 static bool
15985 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15987 cp_token *token;
15989 token = cp_lexer_peek_token (parser->lexer);
15990 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15993 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
15994 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15996 static bool
15997 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15998 size_t n)
16000 cp_token *token;
16002 token = cp_lexer_peek_nth_token (parser->lexer, n);
16003 if (token->type == CPP_LESS)
16004 return true;
16005 /* Check for the sequence `<::' in the original code. It would be lexed as
16006 `[:', where `[' is a digraph, and there is no whitespace before
16007 `:'. */
16008 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16010 cp_token *token2;
16011 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16012 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16013 return true;
16015 return false;
16018 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16019 or none_type otherwise. */
16021 static enum tag_types
16022 cp_parser_token_is_class_key (cp_token* token)
16024 switch (token->keyword)
16026 case RID_CLASS:
16027 return class_type;
16028 case RID_STRUCT:
16029 return record_type;
16030 case RID_UNION:
16031 return union_type;
16033 default:
16034 return none_type;
16038 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16040 static void
16041 cp_parser_check_class_key (enum tag_types class_key, tree type)
16043 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16044 pedwarn ("%qs tag used in naming %q#T",
16045 class_key == union_type ? "union"
16046 : class_key == record_type ? "struct" : "class",
16047 type);
16050 /* Issue an error message if DECL is redeclared with different
16051 access than its original declaration [class.access.spec/3].
16052 This applies to nested classes and nested class templates.
16053 [class.mem/1]. */
16055 static void
16056 cp_parser_check_access_in_redeclaration (tree decl)
16058 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16059 return;
16061 if ((TREE_PRIVATE (decl)
16062 != (current_access_specifier == access_private_node))
16063 || (TREE_PROTECTED (decl)
16064 != (current_access_specifier == access_protected_node)))
16065 error ("%qD redeclared with different access", decl);
16068 /* Look for the `template' keyword, as a syntactic disambiguator.
16069 Return TRUE iff it is present, in which case it will be
16070 consumed. */
16072 static bool
16073 cp_parser_optional_template_keyword (cp_parser *parser)
16075 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16077 /* The `template' keyword can only be used within templates;
16078 outside templates the parser can always figure out what is a
16079 template and what is not. */
16080 if (!processing_template_decl)
16082 error ("%<template%> (as a disambiguator) is only allowed "
16083 "within templates");
16084 /* If this part of the token stream is rescanned, the same
16085 error message would be generated. So, we purge the token
16086 from the stream. */
16087 cp_lexer_purge_token (parser->lexer);
16088 return false;
16090 else
16092 /* Consume the `template' keyword. */
16093 cp_lexer_consume_token (parser->lexer);
16094 return true;
16098 return false;
16101 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16102 set PARSER->SCOPE, and perform other related actions. */
16104 static void
16105 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16107 tree value;
16108 tree check;
16110 /* Get the stored value. */
16111 value = cp_lexer_consume_token (parser->lexer)->value;
16112 /* Perform any access checks that were deferred. */
16113 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16114 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16115 /* Set the scope from the stored value. */
16116 parser->scope = TREE_VALUE (value);
16117 parser->qualifying_scope = TREE_TYPE (value);
16118 parser->object_scope = NULL_TREE;
16121 /* Consume tokens up through a non-nested END token. */
16123 static void
16124 cp_parser_cache_group (cp_parser *parser,
16125 enum cpp_ttype end,
16126 unsigned depth)
16128 while (true)
16130 cp_token *token;
16132 /* Abort a parenthesized expression if we encounter a brace. */
16133 if ((end == CPP_CLOSE_PAREN || depth == 0)
16134 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16135 return;
16136 /* If we've reached the end of the file, stop. */
16137 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16138 return;
16139 /* Consume the next token. */
16140 token = cp_lexer_consume_token (parser->lexer);
16141 /* See if it starts a new group. */
16142 if (token->type == CPP_OPEN_BRACE)
16144 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16145 if (depth == 0)
16146 return;
16148 else if (token->type == CPP_OPEN_PAREN)
16149 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16150 else if (token->type == end)
16151 return;
16155 /* Begin parsing tentatively. We always save tokens while parsing
16156 tentatively so that if the tentative parsing fails we can restore the
16157 tokens. */
16159 static void
16160 cp_parser_parse_tentatively (cp_parser* parser)
16162 /* Enter a new parsing context. */
16163 parser->context = cp_parser_context_new (parser->context);
16164 /* Begin saving tokens. */
16165 cp_lexer_save_tokens (parser->lexer);
16166 /* In order to avoid repetitive access control error messages,
16167 access checks are queued up until we are no longer parsing
16168 tentatively. */
16169 push_deferring_access_checks (dk_deferred);
16172 /* Commit to the currently active tentative parse. */
16174 static void
16175 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16177 cp_parser_context *context;
16178 cp_lexer *lexer;
16180 /* Mark all of the levels as committed. */
16181 lexer = parser->lexer;
16182 for (context = parser->context; context->next; context = context->next)
16184 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16185 break;
16186 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16187 while (!cp_lexer_saving_tokens (lexer))
16188 lexer = lexer->next;
16189 cp_lexer_commit_tokens (lexer);
16193 /* Abort the currently active tentative parse. All consumed tokens
16194 will be rolled back, and no diagnostics will be issued. */
16196 static void
16197 cp_parser_abort_tentative_parse (cp_parser* parser)
16199 cp_parser_simulate_error (parser);
16200 /* Now, pretend that we want to see if the construct was
16201 successfully parsed. */
16202 cp_parser_parse_definitely (parser);
16205 /* Stop parsing tentatively. If a parse error has occurred, restore the
16206 token stream. Otherwise, commit to the tokens we have consumed.
16207 Returns true if no error occurred; false otherwise. */
16209 static bool
16210 cp_parser_parse_definitely (cp_parser* parser)
16212 bool error_occurred;
16213 cp_parser_context *context;
16215 /* Remember whether or not an error occurred, since we are about to
16216 destroy that information. */
16217 error_occurred = cp_parser_error_occurred (parser);
16218 /* Remove the topmost context from the stack. */
16219 context = parser->context;
16220 parser->context = context->next;
16221 /* If no parse errors occurred, commit to the tentative parse. */
16222 if (!error_occurred)
16224 /* Commit to the tokens read tentatively, unless that was
16225 already done. */
16226 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16227 cp_lexer_commit_tokens (parser->lexer);
16229 pop_to_parent_deferring_access_checks ();
16231 /* Otherwise, if errors occurred, roll back our state so that things
16232 are just as they were before we began the tentative parse. */
16233 else
16235 cp_lexer_rollback_tokens (parser->lexer);
16236 pop_deferring_access_checks ();
16238 /* Add the context to the front of the free list. */
16239 context->next = cp_parser_context_free_list;
16240 cp_parser_context_free_list = context;
16242 return !error_occurred;
16245 /* Returns true if we are parsing tentatively and are not committed to
16246 this tentative parse. */
16248 static bool
16249 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16251 return (cp_parser_parsing_tentatively (parser)
16252 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16255 /* Returns nonzero iff an error has occurred during the most recent
16256 tentative parse. */
16258 static bool
16259 cp_parser_error_occurred (cp_parser* parser)
16261 return (cp_parser_parsing_tentatively (parser)
16262 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16265 /* Returns nonzero if GNU extensions are allowed. */
16267 static bool
16268 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16270 return parser->allow_gnu_extensions_p;
16273 /* Objective-C++ Productions */
16276 /* Parse an Objective-C expression, which feeds into a primary-expression
16277 above.
16279 objc-expression:
16280 objc-message-expression
16281 objc-string-literal
16282 objc-encode-expression
16283 objc-protocol-expression
16284 objc-selector-expression
16286 Returns a tree representation of the expression. */
16288 static tree
16289 cp_parser_objc_expression (cp_parser* parser)
16291 /* Try to figure out what kind of declaration is present. */
16292 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16294 switch (kwd->type)
16296 case CPP_OPEN_SQUARE:
16297 return cp_parser_objc_message_expression (parser);
16299 case CPP_OBJC_STRING:
16300 kwd = cp_lexer_consume_token (parser->lexer);
16301 return objc_build_string_object (kwd->value);
16303 case CPP_KEYWORD:
16304 switch (kwd->keyword)
16306 case RID_AT_ENCODE:
16307 return cp_parser_objc_encode_expression (parser);
16309 case RID_AT_PROTOCOL:
16310 return cp_parser_objc_protocol_expression (parser);
16312 case RID_AT_SELECTOR:
16313 return cp_parser_objc_selector_expression (parser);
16315 default:
16316 break;
16318 default:
16319 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16320 cp_parser_skip_to_end_of_block_or_statement (parser);
16323 return error_mark_node;
16326 /* Parse an Objective-C message expression.
16328 objc-message-expression:
16329 [ objc-message-receiver objc-message-args ]
16331 Returns a representation of an Objective-C message. */
16333 static tree
16334 cp_parser_objc_message_expression (cp_parser* parser)
16336 tree receiver, messageargs;
16338 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16339 receiver = cp_parser_objc_message_receiver (parser);
16340 messageargs = cp_parser_objc_message_args (parser);
16341 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16343 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16346 /* Parse an objc-message-receiver.
16348 objc-message-receiver:
16349 expression
16350 simple-type-specifier
16352 Returns a representation of the type or expression. */
16354 static tree
16355 cp_parser_objc_message_receiver (cp_parser* parser)
16357 tree rcv;
16359 /* An Objective-C message receiver may be either (1) a type
16360 or (2) an expression. */
16361 cp_parser_parse_tentatively (parser);
16362 rcv = cp_parser_expression (parser, false);
16364 if (cp_parser_parse_definitely (parser))
16365 return rcv;
16367 rcv = cp_parser_simple_type_specifier (parser,
16368 /*decl_specs=*/NULL,
16369 CP_PARSER_FLAGS_NONE);
16371 return objc_get_class_reference (rcv);
16374 /* Parse the arguments and selectors comprising an Objective-C message.
16376 objc-message-args:
16377 objc-selector
16378 objc-selector-args
16379 objc-selector-args , objc-comma-args
16381 objc-selector-args:
16382 objc-selector [opt] : assignment-expression
16383 objc-selector-args objc-selector [opt] : assignment-expression
16385 objc-comma-args:
16386 assignment-expression
16387 objc-comma-args , assignment-expression
16389 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16390 selector arguments and TREE_VALUE containing a list of comma
16391 arguments. */
16393 static tree
16394 cp_parser_objc_message_args (cp_parser* parser)
16396 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16397 bool maybe_unary_selector_p = true;
16398 cp_token *token = cp_lexer_peek_token (parser->lexer);
16400 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16402 tree selector = NULL_TREE, arg;
16404 if (token->type != CPP_COLON)
16405 selector = cp_parser_objc_selector (parser);
16407 /* Detect if we have a unary selector. */
16408 if (maybe_unary_selector_p
16409 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16410 return build_tree_list (selector, NULL_TREE);
16412 maybe_unary_selector_p = false;
16413 cp_parser_require (parser, CPP_COLON, "`:'");
16414 arg = cp_parser_assignment_expression (parser, false);
16416 sel_args
16417 = chainon (sel_args,
16418 build_tree_list (selector, arg));
16420 token = cp_lexer_peek_token (parser->lexer);
16423 /* Handle non-selector arguments, if any. */
16424 while (token->type == CPP_COMMA)
16426 tree arg;
16428 cp_lexer_consume_token (parser->lexer);
16429 arg = cp_parser_assignment_expression (parser, false);
16431 addl_args
16432 = chainon (addl_args,
16433 build_tree_list (NULL_TREE, arg));
16435 token = cp_lexer_peek_token (parser->lexer);
16438 return build_tree_list (sel_args, addl_args);
16441 /* Parse an Objective-C encode expression.
16443 objc-encode-expression:
16444 @encode objc-typename
16446 Returns an encoded representation of the type argument. */
16448 static tree
16449 cp_parser_objc_encode_expression (cp_parser* parser)
16451 tree type;
16453 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16454 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16455 type = complete_type (cp_parser_type_id (parser));
16456 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16458 if (!type)
16460 error ("%<@encode%> must specify a type as an argument");
16461 return error_mark_node;
16464 return objc_build_encode_expr (type);
16467 /* Parse an Objective-C @defs expression. */
16469 static tree
16470 cp_parser_objc_defs_expression (cp_parser *parser)
16472 tree name;
16474 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16475 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16476 name = cp_parser_identifier (parser);
16477 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16479 return objc_get_class_ivars (name);
16482 /* Parse an Objective-C protocol expression.
16484 objc-protocol-expression:
16485 @protocol ( identifier )
16487 Returns a representation of the protocol expression. */
16489 static tree
16490 cp_parser_objc_protocol_expression (cp_parser* parser)
16492 tree proto;
16494 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16495 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16496 proto = cp_parser_identifier (parser);
16497 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16499 return objc_build_protocol_expr (proto);
16502 /* Parse an Objective-C selector expression.
16504 objc-selector-expression:
16505 @selector ( objc-method-signature )
16507 objc-method-signature:
16508 objc-selector
16509 objc-selector-seq
16511 objc-selector-seq:
16512 objc-selector :
16513 objc-selector-seq objc-selector :
16515 Returns a representation of the method selector. */
16517 static tree
16518 cp_parser_objc_selector_expression (cp_parser* parser)
16520 tree sel_seq = NULL_TREE;
16521 bool maybe_unary_selector_p = true;
16522 cp_token *token;
16524 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16525 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16526 token = cp_lexer_peek_token (parser->lexer);
16528 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16529 || token->type == CPP_SCOPE)
16531 tree selector = NULL_TREE;
16533 if (token->type != CPP_COLON
16534 || token->type == CPP_SCOPE)
16535 selector = cp_parser_objc_selector (parser);
16537 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16538 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16540 /* Detect if we have a unary selector. */
16541 if (maybe_unary_selector_p)
16543 sel_seq = selector;
16544 goto finish_selector;
16546 else
16548 cp_parser_error (parser, "expected %<:%>");
16551 maybe_unary_selector_p = false;
16552 token = cp_lexer_consume_token (parser->lexer);
16554 if (token->type == CPP_SCOPE)
16556 sel_seq
16557 = chainon (sel_seq,
16558 build_tree_list (selector, NULL_TREE));
16559 sel_seq
16560 = chainon (sel_seq,
16561 build_tree_list (NULL_TREE, NULL_TREE));
16563 else
16564 sel_seq
16565 = chainon (sel_seq,
16566 build_tree_list (selector, NULL_TREE));
16568 token = cp_lexer_peek_token (parser->lexer);
16571 finish_selector:
16572 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16574 return objc_build_selector_expr (sel_seq);
16577 /* Parse a list of identifiers.
16579 objc-identifier-list:
16580 identifier
16581 objc-identifier-list , identifier
16583 Returns a TREE_LIST of identifier nodes. */
16585 static tree
16586 cp_parser_objc_identifier_list (cp_parser* parser)
16588 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16589 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16591 while (sep->type == CPP_COMMA)
16593 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16594 list = chainon (list,
16595 build_tree_list (NULL_TREE,
16596 cp_parser_identifier (parser)));
16597 sep = cp_lexer_peek_token (parser->lexer);
16600 return list;
16603 /* Parse an Objective-C alias declaration.
16605 objc-alias-declaration:
16606 @compatibility_alias identifier identifier ;
16608 This function registers the alias mapping with the Objective-C front-end.
16609 It returns nothing. */
16611 static void
16612 cp_parser_objc_alias_declaration (cp_parser* parser)
16614 tree alias, orig;
16616 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
16617 alias = cp_parser_identifier (parser);
16618 orig = cp_parser_identifier (parser);
16619 objc_declare_alias (alias, orig);
16620 cp_parser_consume_semicolon_at_end_of_statement (parser);
16623 /* Parse an Objective-C class forward-declaration.
16625 objc-class-declaration:
16626 @class objc-identifier-list ;
16628 The function registers the forward declarations with the Objective-C
16629 front-end. It returns nothing. */
16631 static void
16632 cp_parser_objc_class_declaration (cp_parser* parser)
16634 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
16635 objc_declare_class (cp_parser_objc_identifier_list (parser));
16636 cp_parser_consume_semicolon_at_end_of_statement (parser);
16639 /* Parse a list of Objective-C protocol references.
16641 objc-protocol-refs-opt:
16642 objc-protocol-refs [opt]
16644 objc-protocol-refs:
16645 < objc-identifier-list >
16647 Returns a TREE_LIST of identifiers, if any. */
16649 static tree
16650 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16652 tree protorefs = NULL_TREE;
16654 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16656 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
16657 protorefs = cp_parser_objc_identifier_list (parser);
16658 cp_parser_require (parser, CPP_GREATER, "`>'");
16661 return protorefs;
16664 /* Parse a Objective-C visibility specification. */
16666 static void
16667 cp_parser_objc_visibility_spec (cp_parser* parser)
16669 cp_token *vis = cp_lexer_peek_token (parser->lexer);
16671 switch (vis->keyword)
16673 case RID_AT_PRIVATE:
16674 objc_set_visibility (2);
16675 break;
16676 case RID_AT_PROTECTED:
16677 objc_set_visibility (0);
16678 break;
16679 case RID_AT_PUBLIC:
16680 objc_set_visibility (1);
16681 break;
16682 default:
16683 return;
16686 /* Eat '@private'/'@protected'/'@public'. */
16687 cp_lexer_consume_token (parser->lexer);
16690 /* Parse an Objective-C method type. */
16692 static void
16693 cp_parser_objc_method_type (cp_parser* parser)
16695 objc_set_method_type
16696 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16697 ? PLUS_EXPR
16698 : MINUS_EXPR);
16701 /* Parse an Objective-C protocol qualifier. */
16703 static tree
16704 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16706 tree quals = NULL_TREE, node;
16707 cp_token *token = cp_lexer_peek_token (parser->lexer);
16709 node = token->value;
16711 while (node && TREE_CODE (node) == IDENTIFIER_NODE
16712 && (node == ridpointers [(int) RID_IN]
16713 || node == ridpointers [(int) RID_OUT]
16714 || node == ridpointers [(int) RID_INOUT]
16715 || node == ridpointers [(int) RID_BYCOPY]
16716 || node == ridpointers [(int) RID_BYREF]
16717 || node == ridpointers [(int) RID_ONEWAY]))
16719 quals = tree_cons (NULL_TREE, node, quals);
16720 cp_lexer_consume_token (parser->lexer);
16721 token = cp_lexer_peek_token (parser->lexer);
16722 node = token->value;
16725 return quals;
16728 /* Parse an Objective-C typename. */
16730 static tree
16731 cp_parser_objc_typename (cp_parser* parser)
16733 tree typename = NULL_TREE;
16735 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16737 tree proto_quals, cp_type = NULL_TREE;
16739 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
16740 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16742 /* An ObjC type name may consist of just protocol qualifiers, in which
16743 case the type shall default to 'id'. */
16744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16745 cp_type = cp_parser_type_id (parser);
16747 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16748 typename = build_tree_list (proto_quals, cp_type);
16751 return typename;
16754 /* Check to see if TYPE refers to an Objective-C selector name. */
16756 static bool
16757 cp_parser_objc_selector_p (enum cpp_ttype type)
16759 return (type == CPP_NAME || type == CPP_KEYWORD
16760 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16761 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16762 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16763 || type == CPP_XOR || type == CPP_XOR_EQ);
16766 /* Parse an Objective-C selector. */
16768 static tree
16769 cp_parser_objc_selector (cp_parser* parser)
16771 cp_token *token = cp_lexer_consume_token (parser->lexer);
16773 if (!cp_parser_objc_selector_p (token->type))
16775 error ("invalid Objective-C++ selector name");
16776 return error_mark_node;
16779 /* C++ operator names are allowed to appear in ObjC selectors. */
16780 switch (token->type)
16782 case CPP_AND_AND: return get_identifier ("and");
16783 case CPP_AND_EQ: return get_identifier ("and_eq");
16784 case CPP_AND: return get_identifier ("bitand");
16785 case CPP_OR: return get_identifier ("bitor");
16786 case CPP_COMPL: return get_identifier ("compl");
16787 case CPP_NOT: return get_identifier ("not");
16788 case CPP_NOT_EQ: return get_identifier ("not_eq");
16789 case CPP_OR_OR: return get_identifier ("or");
16790 case CPP_OR_EQ: return get_identifier ("or_eq");
16791 case CPP_XOR: return get_identifier ("xor");
16792 case CPP_XOR_EQ: return get_identifier ("xor_eq");
16793 default: return token->value;
16797 /* Parse an Objective-C params list. */
16799 static tree
16800 cp_parser_objc_method_keyword_params (cp_parser* parser)
16802 tree params = NULL_TREE;
16803 bool maybe_unary_selector_p = true;
16804 cp_token *token = cp_lexer_peek_token (parser->lexer);
16806 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16808 tree selector = NULL_TREE, typename, identifier;
16810 if (token->type != CPP_COLON)
16811 selector = cp_parser_objc_selector (parser);
16813 /* Detect if we have a unary selector. */
16814 if (maybe_unary_selector_p
16815 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16816 return selector;
16818 maybe_unary_selector_p = false;
16819 cp_parser_require (parser, CPP_COLON, "`:'");
16820 typename = cp_parser_objc_typename (parser);
16821 identifier = cp_parser_identifier (parser);
16823 params
16824 = chainon (params,
16825 objc_build_keyword_decl (selector,
16826 typename,
16827 identifier));
16829 token = cp_lexer_peek_token (parser->lexer);
16832 return params;
16835 /* Parse the non-keyword Objective-C params. */
16837 static tree
16838 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
16840 tree params = make_node (TREE_LIST);
16841 cp_token *token = cp_lexer_peek_token (parser->lexer);
16842 *ellipsisp = false; /* Initially, assume no ellipsis. */
16844 while (token->type == CPP_COMMA)
16846 cp_parameter_declarator *parmdecl;
16847 tree parm;
16849 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16850 token = cp_lexer_peek_token (parser->lexer);
16852 if (token->type == CPP_ELLIPSIS)
16854 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
16855 *ellipsisp = true;
16856 break;
16859 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
16860 parm = grokdeclarator (parmdecl->declarator,
16861 &parmdecl->decl_specifiers,
16862 PARM, /*initialized=*/0,
16863 /*attrlist=*/NULL);
16865 chainon (params, build_tree_list (NULL_TREE, parm));
16866 token = cp_lexer_peek_token (parser->lexer);
16869 return params;
16872 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
16874 static void
16875 cp_parser_objc_interstitial_code (cp_parser* parser)
16877 cp_token *token = cp_lexer_peek_token (parser->lexer);
16879 /* If the next token is `extern' and the following token is a string
16880 literal, then we have a linkage specification. */
16881 if (token->keyword == RID_EXTERN
16882 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
16883 cp_parser_linkage_specification (parser);
16884 /* Handle #pragma, if any. */
16885 else if (token->type == CPP_PRAGMA)
16886 cp_lexer_handle_pragma (parser->lexer);
16887 /* Allow stray semicolons. */
16888 else if (token->type == CPP_SEMICOLON)
16889 cp_lexer_consume_token (parser->lexer);
16890 /* Finally, try to parse a block-declaration, or a function-definition. */
16891 else
16892 cp_parser_block_declaration (parser, /*statement_p=*/false);
16895 /* Parse a method signature. */
16897 static tree
16898 cp_parser_objc_method_signature (cp_parser* parser)
16900 tree rettype, kwdparms, optparms;
16901 bool ellipsis = false;
16903 cp_parser_objc_method_type (parser);
16904 rettype = cp_parser_objc_typename (parser);
16905 kwdparms = cp_parser_objc_method_keyword_params (parser);
16906 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
16908 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
16911 /* Pars an Objective-C method prototype list. */
16913 static void
16914 cp_parser_objc_method_prototype_list (cp_parser* parser)
16916 cp_token *token = cp_lexer_peek_token (parser->lexer);
16918 while (token->keyword != RID_AT_END)
16920 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16922 objc_add_method_declaration
16923 (cp_parser_objc_method_signature (parser));
16924 cp_parser_consume_semicolon_at_end_of_statement (parser);
16926 else
16927 /* Allow for interspersed non-ObjC++ code. */
16928 cp_parser_objc_interstitial_code (parser);
16930 token = cp_lexer_peek_token (parser->lexer);
16933 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
16934 objc_finish_interface ();
16937 /* Parse an Objective-C method definition list. */
16939 static void
16940 cp_parser_objc_method_definition_list (cp_parser* parser)
16942 cp_token *token = cp_lexer_peek_token (parser->lexer);
16944 while (token->keyword != RID_AT_END)
16946 tree meth;
16948 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16950 push_deferring_access_checks (dk_deferred);
16951 objc_start_method_definition
16952 (cp_parser_objc_method_signature (parser));
16954 /* For historical reasons, we accept an optional semicolon. */
16955 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16956 cp_lexer_consume_token (parser->lexer);
16958 perform_deferred_access_checks ();
16959 stop_deferring_access_checks ();
16960 meth = cp_parser_function_definition_after_declarator (parser,
16961 false);
16962 pop_deferring_access_checks ();
16963 objc_finish_method_definition (meth);
16965 else
16966 /* Allow for interspersed non-ObjC++ code. */
16967 cp_parser_objc_interstitial_code (parser);
16969 token = cp_lexer_peek_token (parser->lexer);
16972 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
16973 objc_finish_implementation ();
16976 /* Parse Objective-C ivars. */
16978 static void
16979 cp_parser_objc_class_ivars (cp_parser* parser)
16981 cp_token *token = cp_lexer_peek_token (parser->lexer);
16983 if (token->type != CPP_OPEN_BRACE)
16984 return; /* No ivars specified. */
16986 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
16987 token = cp_lexer_peek_token (parser->lexer);
16989 while (token->type != CPP_CLOSE_BRACE)
16991 cp_decl_specifier_seq declspecs;
16992 int decl_class_or_enum_p;
16993 tree prefix_attributes;
16995 cp_parser_objc_visibility_spec (parser);
16997 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16998 break;
17000 cp_parser_decl_specifier_seq (parser,
17001 CP_PARSER_FLAGS_OPTIONAL,
17002 &declspecs,
17003 &decl_class_or_enum_p);
17004 prefix_attributes = declspecs.attributes;
17005 declspecs.attributes = NULL_TREE;
17007 /* Keep going until we hit the `;' at the end of the
17008 declaration. */
17009 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17011 tree width = NULL_TREE, attributes, first_attribute, decl;
17012 cp_declarator *declarator = NULL;
17013 int ctor_dtor_or_conv_p;
17015 /* Check for a (possibly unnamed) bitfield declaration. */
17016 token = cp_lexer_peek_token (parser->lexer);
17017 if (token->type == CPP_COLON)
17018 goto eat_colon;
17020 if (token->type == CPP_NAME
17021 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17022 == CPP_COLON))
17024 /* Get the name of the bitfield. */
17025 declarator = make_id_declarator (NULL_TREE,
17026 cp_parser_identifier (parser));
17028 eat_colon:
17029 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17030 /* Get the width of the bitfield. */
17031 width
17032 = cp_parser_constant_expression (parser,
17033 /*allow_non_constant=*/false,
17034 NULL);
17036 else
17038 /* Parse the declarator. */
17039 declarator
17040 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17041 &ctor_dtor_or_conv_p,
17042 /*parenthesized_p=*/NULL,
17043 /*member_p=*/false);
17046 /* Look for attributes that apply to the ivar. */
17047 attributes = cp_parser_attributes_opt (parser);
17048 /* Remember which attributes are prefix attributes and
17049 which are not. */
17050 first_attribute = attributes;
17051 /* Combine the attributes. */
17052 attributes = chainon (prefix_attributes, attributes);
17054 if (width)
17056 /* Create the bitfield declaration. */
17057 decl = grokbitfield (declarator, &declspecs, width);
17058 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17060 else
17061 decl = grokfield (declarator, &declspecs, NULL_TREE,
17062 NULL_TREE, attributes);
17064 /* Add the instance variable. */
17065 objc_add_instance_variable (decl);
17067 /* Reset PREFIX_ATTRIBUTES. */
17068 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17069 attributes = TREE_CHAIN (attributes);
17070 if (attributes)
17071 TREE_CHAIN (attributes) = NULL_TREE;
17073 token = cp_lexer_peek_token (parser->lexer);
17075 if (token->type == CPP_COMMA)
17077 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17078 continue;
17080 break;
17083 cp_parser_consume_semicolon_at_end_of_statement (parser);
17084 token = cp_lexer_peek_token (parser->lexer);
17087 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17088 /* For historical reasons, we accept an optional semicolon. */
17089 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17090 cp_lexer_consume_token (parser->lexer);
17093 /* Parse an Objective-C protocol declaration. */
17095 static void
17096 cp_parser_objc_protocol_declaration (cp_parser* parser)
17098 tree proto, protorefs;
17099 cp_token *tok;
17101 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17104 error ("identifier expected after %<@protocol%>");
17105 goto finish;
17108 /* See if we have a forward declaration or a definition. */
17109 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17111 /* Try a forward declaration first. */
17112 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17114 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17115 finish:
17116 cp_parser_consume_semicolon_at_end_of_statement (parser);
17119 /* Ok, we got a full-fledged definition (or at least should). */
17120 else
17122 proto = cp_parser_identifier (parser);
17123 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17124 objc_start_protocol (proto, protorefs);
17125 cp_parser_objc_method_prototype_list (parser);
17129 /* Parse an Objective-C superclass or category. */
17131 static void
17132 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17133 tree *categ)
17135 cp_token *next = cp_lexer_peek_token (parser->lexer);
17137 *super = *categ = NULL_TREE;
17138 if (next->type == CPP_COLON)
17140 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17141 *super = cp_parser_identifier (parser);
17143 else if (next->type == CPP_OPEN_PAREN)
17145 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17146 *categ = cp_parser_identifier (parser);
17147 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17151 /* Parse an Objective-C class interface. */
17153 static void
17154 cp_parser_objc_class_interface (cp_parser* parser)
17156 tree name, super, categ, protos;
17158 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17159 name = cp_parser_identifier (parser);
17160 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17161 protos = cp_parser_objc_protocol_refs_opt (parser);
17163 /* We have either a class or a category on our hands. */
17164 if (categ)
17165 objc_start_category_interface (name, categ, protos);
17166 else
17168 objc_start_class_interface (name, super, protos);
17169 /* Handle instance variable declarations, if any. */
17170 cp_parser_objc_class_ivars (parser);
17171 objc_continue_interface ();
17174 cp_parser_objc_method_prototype_list (parser);
17177 /* Parse an Objective-C class implementation. */
17179 static void
17180 cp_parser_objc_class_implementation (cp_parser* parser)
17182 tree name, super, categ;
17184 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17185 name = cp_parser_identifier (parser);
17186 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17188 /* We have either a class or a category on our hands. */
17189 if (categ)
17190 objc_start_category_implementation (name, categ);
17191 else
17193 objc_start_class_implementation (name, super);
17194 /* Handle instance variable declarations, if any. */
17195 cp_parser_objc_class_ivars (parser);
17196 objc_continue_implementation ();
17199 cp_parser_objc_method_definition_list (parser);
17202 /* Consume the @end token and finish off the implementation. */
17204 static void
17205 cp_parser_objc_end_implementation (cp_parser* parser)
17207 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17208 objc_finish_implementation ();
17211 /* Parse an Objective-C declaration. */
17213 static void
17214 cp_parser_objc_declaration (cp_parser* parser)
17216 /* Try to figure out what kind of declaration is present. */
17217 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17219 switch (kwd->keyword)
17221 case RID_AT_ALIAS:
17222 cp_parser_objc_alias_declaration (parser);
17223 break;
17224 case RID_AT_CLASS:
17225 cp_parser_objc_class_declaration (parser);
17226 break;
17227 case RID_AT_PROTOCOL:
17228 cp_parser_objc_protocol_declaration (parser);
17229 break;
17230 case RID_AT_INTERFACE:
17231 cp_parser_objc_class_interface (parser);
17232 break;
17233 case RID_AT_IMPLEMENTATION:
17234 cp_parser_objc_class_implementation (parser);
17235 break;
17236 case RID_AT_END:
17237 cp_parser_objc_end_implementation (parser);
17238 break;
17239 default:
17240 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17241 cp_parser_skip_to_end_of_block_or_statement (parser);
17245 /* Parse an Objective-C try-catch-finally statement.
17247 objc-try-catch-finally-stmt:
17248 @try compound-statement objc-catch-clause-seq [opt]
17249 objc-finally-clause [opt]
17251 objc-catch-clause-seq:
17252 objc-catch-clause objc-catch-clause-seq [opt]
17254 objc-catch-clause:
17255 @catch ( exception-declaration ) compound-statement
17257 objc-finally-clause
17258 @finally compound-statement
17260 Returns NULL_TREE. */
17262 static tree
17263 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17264 location_t location;
17265 tree stmt;
17267 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17268 location = cp_lexer_peek_token (parser->lexer)->location;
17269 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17270 node, lest it get absorbed into the surrounding block. */
17271 stmt = push_stmt_list ();
17272 cp_parser_compound_statement (parser, NULL, false);
17273 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17275 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17277 cp_parameter_declarator *parmdecl;
17278 tree parm;
17280 cp_lexer_consume_token (parser->lexer);
17281 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17282 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17283 parm = grokdeclarator (parmdecl->declarator,
17284 &parmdecl->decl_specifiers,
17285 PARM, /*initialized=*/0,
17286 /*attrlist=*/NULL);
17287 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17288 objc_begin_catch_clause (parm);
17289 cp_parser_compound_statement (parser, NULL, false);
17290 objc_finish_catch_clause ();
17293 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17295 cp_lexer_consume_token (parser->lexer);
17296 location = cp_lexer_peek_token (parser->lexer)->location;
17297 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17298 node, lest it get absorbed into the surrounding block. */
17299 stmt = push_stmt_list ();
17300 cp_parser_compound_statement (parser, NULL, false);
17301 objc_build_finally_clause (location, pop_stmt_list (stmt));
17304 return objc_finish_try_stmt ();
17307 /* Parse an Objective-C synchronized statement.
17309 objc-synchronized-stmt:
17310 @synchronized ( expression ) compound-statement
17312 Returns NULL_TREE. */
17314 static tree
17315 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17316 location_t location;
17317 tree lock, stmt;
17319 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17321 location = cp_lexer_peek_token (parser->lexer)->location;
17322 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17323 lock = cp_parser_expression (parser, false);
17324 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17326 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17327 node, lest it get absorbed into the surrounding block. */
17328 stmt = push_stmt_list ();
17329 cp_parser_compound_statement (parser, NULL, false);
17331 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17334 /* Parse an Objective-C throw statement.
17336 objc-throw-stmt:
17337 @throw assignment-expression [opt] ;
17339 Returns a constructed '@throw' statement. */
17341 static tree
17342 cp_parser_objc_throw_statement (cp_parser *parser) {
17343 tree expr = NULL_TREE;
17345 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17348 expr = cp_parser_assignment_expression (parser, false);
17350 cp_parser_consume_semicolon_at_end_of_statement (parser);
17352 return objc_build_throw_stmt (expr);
17355 /* Parse an Objective-C statement. */
17357 static tree
17358 cp_parser_objc_statement (cp_parser * parser) {
17359 /* Try to figure out what kind of declaration is present. */
17360 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17362 switch (kwd->keyword)
17364 case RID_AT_TRY:
17365 return cp_parser_objc_try_catch_finally_statement (parser);
17366 case RID_AT_SYNCHRONIZED:
17367 return cp_parser_objc_synchronized_statement (parser);
17368 case RID_AT_THROW:
17369 return cp_parser_objc_throw_statement (parser);
17370 default:
17371 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17372 cp_parser_skip_to_end_of_block_or_statement (parser);
17375 return error_mark_node;
17378 /* The parser. */
17380 static GTY (()) cp_parser *the_parser;
17382 /* External interface. */
17384 /* Parse one entire translation unit. */
17386 void
17387 c_parse_file (void)
17389 bool error_occurred;
17390 static bool already_called = false;
17392 if (already_called)
17394 sorry ("inter-module optimizations not implemented for C++");
17395 return;
17397 already_called = true;
17399 the_parser = cp_parser_new ();
17400 push_deferring_access_checks (flag_access_control
17401 ? dk_no_deferred : dk_no_check);
17402 error_occurred = cp_parser_translation_unit (the_parser);
17403 the_parser = NULL;
17406 /* This variable must be provided by every front end. */
17408 int yydebug;
17410 #include "gt-cp-parser.h"