* expr.c (force_operand): Use convert_to_mode for conversions.
[official-gcc.git] / gcc / cp / parser.c
blob8eb4fbfcf290f7de7013ac56dfdd9db9383e6e55
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 /* True for a CPP_NAME token that is not a keyword (i.e., for which
63 KEYWORD is RID_MAX) iff this name was looked up and found to be
64 ambiguous. An error has already been reported. */
65 BOOL_BITFIELD ambiguous_p : 1;
66 /* The value associated with this token, if any. */
67 tree value;
68 /* The location at which this token was found. */
69 location_t location;
70 } cp_token;
72 /* We use a stack of token pointer for saving token sets. */
73 typedef struct cp_token *cp_token_position;
74 DEF_VEC_P (cp_token_position);
75 DEF_VEC_ALLOC_P (cp_token_position,heap);
77 static const cp_token eof_token =
79 CPP_EOF, RID_MAX, 0, 0, 0, false, NULL_TREE,
80 #if USE_MAPPED_LOCATION
82 #else
83 {0, 0}
84 #endif
87 /* The cp_lexer structure represents the C++ lexer. It is responsible
88 for managing the token stream from the preprocessor and supplying
89 it to the parser. Tokens are never added to the cp_lexer after
90 it is created. */
92 typedef struct cp_lexer GTY (())
94 /* The memory allocated for the buffer. NULL if this lexer does not
95 own the token buffer. */
96 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
97 /* If the lexer owns the buffer, this is the number of tokens in the
98 buffer. */
99 size_t buffer_length;
101 /* A pointer just past the last available token. The tokens
102 in this lexer are [buffer, last_token). */
103 cp_token_position GTY ((skip)) last_token;
105 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
106 no more available tokens. */
107 cp_token_position GTY ((skip)) next_token;
109 /* A stack indicating positions at which cp_lexer_save_tokens was
110 called. The top entry is the most recent position at which we
111 began saving tokens. If the stack is non-empty, we are saving
112 tokens. */
113 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
115 /* True if we should output debugging information. */
116 bool debugging_p;
118 /* The next lexer in a linked list of lexers. */
119 struct cp_lexer *next;
120 } cp_lexer;
122 /* cp_token_cache is a range of tokens. There is no need to represent
123 allocate heap memory for it, since tokens are never removed from the
124 lexer's array. There is also no need for the GC to walk through
125 a cp_token_cache, since everything in here is referenced through
126 a lexer. */
128 typedef struct cp_token_cache GTY(())
130 /* The beginning of the token range. */
131 cp_token * GTY((skip)) first;
133 /* Points immediately after the last token in the range. */
134 cp_token * GTY ((skip)) last;
135 } cp_token_cache;
137 /* Prototypes. */
139 static cp_lexer *cp_lexer_new_main
140 (void);
141 static cp_lexer *cp_lexer_new_from_tokens
142 (cp_token_cache *tokens);
143 static void cp_lexer_destroy
144 (cp_lexer *);
145 static int cp_lexer_saving_tokens
146 (const cp_lexer *);
147 static cp_token_position cp_lexer_token_position
148 (cp_lexer *, bool);
149 static cp_token *cp_lexer_token_at
150 (cp_lexer *, cp_token_position);
151 static void cp_lexer_get_preprocessor_token
152 (cp_lexer *, cp_token *);
153 static inline cp_token *cp_lexer_peek_token
154 (cp_lexer *);
155 static cp_token *cp_lexer_peek_nth_token
156 (cp_lexer *, size_t);
157 static inline bool cp_lexer_next_token_is
158 (cp_lexer *, enum cpp_ttype);
159 static bool cp_lexer_next_token_is_not
160 (cp_lexer *, enum cpp_ttype);
161 static bool cp_lexer_next_token_is_keyword
162 (cp_lexer *, enum rid);
163 static cp_token *cp_lexer_consume_token
164 (cp_lexer *);
165 static void cp_lexer_purge_token
166 (cp_lexer *);
167 static void cp_lexer_purge_tokens_after
168 (cp_lexer *, cp_token_position);
169 static void cp_lexer_handle_pragma
170 (cp_lexer *);
171 static void cp_lexer_save_tokens
172 (cp_lexer *);
173 static void cp_lexer_commit_tokens
174 (cp_lexer *);
175 static void cp_lexer_rollback_tokens
176 (cp_lexer *);
177 #ifdef ENABLE_CHECKING
178 static void cp_lexer_print_token
179 (FILE *, cp_token *);
180 static inline bool cp_lexer_debugging_p
181 (cp_lexer *);
182 static void cp_lexer_start_debugging
183 (cp_lexer *) ATTRIBUTE_UNUSED;
184 static void cp_lexer_stop_debugging
185 (cp_lexer *) ATTRIBUTE_UNUSED;
186 #else
187 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
188 about passing NULL to functions that require non-NULL arguments
189 (fputs, fprintf). It will never be used, so all we need is a value
190 of the right type that's guaranteed not to be NULL. */
191 #define cp_lexer_debug_stream stdout
192 #define cp_lexer_print_token(str, tok) (void) 0
193 #define cp_lexer_debugging_p(lexer) 0
194 #endif /* ENABLE_CHECKING */
196 static cp_token_cache *cp_token_cache_new
197 (cp_token *, cp_token *);
199 /* Manifest constants. */
200 #define CP_LEXER_BUFFER_SIZE 10000
201 #define CP_SAVED_TOKEN_STACK 5
203 /* A token type for keywords, as opposed to ordinary identifiers. */
204 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
206 /* A token type for template-ids. If a template-id is processed while
207 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
208 the value of the CPP_TEMPLATE_ID is whatever was returned by
209 cp_parser_template_id. */
210 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
212 /* A token type for nested-name-specifiers. If a
213 nested-name-specifier is processed while parsing tentatively, it is
214 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
215 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
216 cp_parser_nested_name_specifier_opt. */
217 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
219 /* A token type for tokens that are not tokens at all; these are used
220 to represent slots in the array where there used to be a token
221 that has now been deleted. */
222 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
224 /* The number of token types, including C++-specific ones. */
225 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
227 /* Variables. */
229 #ifdef ENABLE_CHECKING
230 /* The stream to which debugging output should be written. */
231 static FILE *cp_lexer_debug_stream;
232 #endif /* ENABLE_CHECKING */
234 /* Create a new main C++ lexer, the lexer that gets tokens from the
235 preprocessor. */
237 static cp_lexer *
238 cp_lexer_new_main (void)
240 cp_token first_token;
241 cp_lexer *lexer;
242 cp_token *pos;
243 size_t alloc;
244 size_t space;
245 cp_token *buffer;
247 /* It's possible that lexing the first token will load a PCH file,
248 which is a GC collection point. So we have to grab the first
249 token before allocating any memory. Pragmas must not be deferred
250 as -fpch-preprocess can generate a pragma to load the PCH file in
251 the preprocessed output used by -save-temps. */
252 cp_lexer_get_preprocessor_token (NULL, &first_token);
254 /* Tell cpplib we want CPP_PRAGMA tokens. */
255 cpp_get_options (parse_in)->defer_pragmas = true;
257 /* Tell pragma_lex not to merge string constants. */
258 c_lex_return_raw_strings = true;
260 c_common_no_more_pch ();
262 /* Allocate the memory. */
263 lexer = GGC_CNEW (cp_lexer);
265 #ifdef ENABLE_CHECKING
266 /* Initially we are not debugging. */
267 lexer->debugging_p = false;
268 #endif /* ENABLE_CHECKING */
269 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
270 CP_SAVED_TOKEN_STACK);
272 /* Create the buffer. */
273 alloc = CP_LEXER_BUFFER_SIZE;
274 buffer = GGC_NEWVEC (cp_token, alloc);
276 /* Put the first token in the buffer. */
277 space = alloc;
278 pos = buffer;
279 *pos = first_token;
281 /* Get the remaining tokens from the preprocessor. */
282 while (pos->type != CPP_EOF)
284 pos++;
285 if (!--space)
287 space = alloc;
288 alloc *= 2;
289 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
290 pos = buffer + space;
292 cp_lexer_get_preprocessor_token (lexer, pos);
294 lexer->buffer = buffer;
295 lexer->buffer_length = alloc - space;
296 lexer->last_token = pos;
297 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
299 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
300 direct calls to pragma_lex. Those callers all expect pragma_lex
301 to do string constant concatenation. */
302 c_lex_return_raw_strings = false;
304 /* Subsequent preprocessor diagnostics should use compiler
305 diagnostic functions to get the compiler source location. */
306 cpp_get_options (parse_in)->client_diagnostic = true;
307 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
309 gcc_assert (lexer->next_token->type != CPP_PURGED);
310 return lexer;
313 /* Create a new lexer whose token stream is primed with the tokens in
314 CACHE. When these tokens are exhausted, no new tokens will be read. */
316 static cp_lexer *
317 cp_lexer_new_from_tokens (cp_token_cache *cache)
319 cp_token *first = cache->first;
320 cp_token *last = cache->last;
321 cp_lexer *lexer = GGC_CNEW (cp_lexer);
323 /* We do not own the buffer. */
324 lexer->buffer = NULL;
325 lexer->buffer_length = 0;
326 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327 lexer->last_token = last;
329 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330 CP_SAVED_TOKEN_STACK);
332 #ifdef ENABLE_CHECKING
333 /* Initially we are not debugging. */
334 lexer->debugging_p = false;
335 #endif
337 gcc_assert (lexer->next_token->type != CPP_PURGED);
338 return lexer;
341 /* Frees all resources associated with LEXER. */
343 static void
344 cp_lexer_destroy (cp_lexer *lexer)
346 if (lexer->buffer)
347 ggc_free (lexer->buffer);
348 VEC_free (cp_token_position, heap, lexer->saved_tokens);
349 ggc_free (lexer);
352 /* Returns nonzero if debugging information should be output. */
354 #ifdef ENABLE_CHECKING
356 static inline bool
357 cp_lexer_debugging_p (cp_lexer *lexer)
359 return lexer->debugging_p;
362 #endif /* ENABLE_CHECKING */
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
367 gcc_assert (!previous_p || lexer->next_token != &eof_token);
369 return lexer->next_token - previous_p;
372 static inline cp_token *
373 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
375 return pos;
378 /* nonzero if we are presently saving tokens. */
380 static inline int
381 cp_lexer_saving_tokens (const cp_lexer* lexer)
383 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
386 /* Store the next token from the preprocessor in *TOKEN. Return true
387 if we reach EOF. */
389 static void
390 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391 cp_token *token)
393 static int is_extern_c = 0;
395 /* Get a new token from the preprocessor. */
396 token->type
397 = c_lex_with_flags (&token->value, &token->location, &token->flags);
398 token->in_system_header = in_system_header;
400 /* On some systems, some header files are surrounded by an
401 implicit extern "C" block. Set a flag in the token if it
402 comes from such a header. */
403 is_extern_c += pending_lang_change;
404 pending_lang_change = 0;
405 token->implicit_extern_c = is_extern_c > 0;
407 /* Check to see if this token is a keyword. */
408 if (token->type == CPP_NAME)
410 if (C_IS_RESERVED_WORD (token->value))
412 /* Mark this token as a keyword. */
413 token->type = CPP_KEYWORD;
414 /* Record which keyword. */
415 token->keyword = C_RID_CODE (token->value);
416 /* Update the value. Some keywords are mapped to particular
417 entities, rather than simply having the value of the
418 corresponding IDENTIFIER_NODE. For example, `__const' is
419 mapped to `const'. */
420 token->value = ridpointers[token->keyword];
422 else
424 token->ambiguous_p = false;
425 token->keyword = RID_MAX;
428 /* Handle Objective-C++ keywords. */
429 else if (token->type == CPP_AT_NAME)
431 token->type = CPP_KEYWORD;
432 switch (C_RID_CODE (token->value))
434 /* Map 'class' to '@class', 'private' to '@private', etc. */
435 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439 case RID_THROW: token->keyword = RID_AT_THROW; break;
440 case RID_TRY: token->keyword = RID_AT_TRY; break;
441 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442 default: token->keyword = C_RID_CODE (token->value);
445 else
446 token->keyword = RID_MAX;
449 /* Update the globals input_location and in_system_header from TOKEN. */
450 static inline void
451 cp_lexer_set_source_position_from_token (cp_token *token)
453 if (token->type != CPP_EOF)
455 input_location = token->location;
456 in_system_header = token->in_system_header;
460 /* Return a pointer to the next token in the token stream, but do not
461 consume it. */
463 static inline cp_token *
464 cp_lexer_peek_token (cp_lexer *lexer)
466 if (cp_lexer_debugging_p (lexer))
468 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
469 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
470 putc ('\n', cp_lexer_debug_stream);
472 return lexer->next_token;
475 /* Return true if the next token has the indicated TYPE. */
477 static inline bool
478 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
480 return cp_lexer_peek_token (lexer)->type == type;
483 /* Return true if the next token does not have the indicated TYPE. */
485 static inline bool
486 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
488 return !cp_lexer_next_token_is (lexer, type);
491 /* Return true if the next token is the indicated KEYWORD. */
493 static inline bool
494 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
496 cp_token *token;
498 /* Peek at the next token. */
499 token = cp_lexer_peek_token (lexer);
500 /* Check to see if it is the indicated keyword. */
501 return token->keyword == keyword;
504 /* Return a pointer to the Nth token in the token stream. If N is 1,
505 then this is precisely equivalent to cp_lexer_peek_token (except
506 that it is not inline). One would like to disallow that case, but
507 there is one case (cp_parser_nth_token_starts_template_id) where
508 the caller passes a variable for N and it might be 1. */
510 static cp_token *
511 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
513 cp_token *token;
515 /* N is 1-based, not zero-based. */
516 gcc_assert (n > 0);
518 if (cp_lexer_debugging_p (lexer))
519 fprintf (cp_lexer_debug_stream,
520 "cp_lexer: peeking ahead %ld at token: ", (long)n);
522 --n;
523 token = lexer->next_token;
524 gcc_assert (!n || token != &eof_token);
525 while (n != 0)
527 ++token;
528 if (token == lexer->last_token)
530 token = (cp_token *)&eof_token;
531 break;
534 if (token->type != CPP_PURGED)
535 --n;
538 if (cp_lexer_debugging_p (lexer))
540 cp_lexer_print_token (cp_lexer_debug_stream, token);
541 putc ('\n', cp_lexer_debug_stream);
544 return token;
547 /* Return the next token, and advance the lexer's next_token pointer
548 to point to the next non-purged token. */
550 static cp_token *
551 cp_lexer_consume_token (cp_lexer* lexer)
553 cp_token *token = lexer->next_token;
555 gcc_assert (token != &eof_token);
559 lexer->next_token++;
560 if (lexer->next_token == lexer->last_token)
562 lexer->next_token = (cp_token *)&eof_token;
563 break;
567 while (lexer->next_token->type == CPP_PURGED);
569 cp_lexer_set_source_position_from_token (token);
571 /* Provide debugging output. */
572 if (cp_lexer_debugging_p (lexer))
574 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
575 cp_lexer_print_token (cp_lexer_debug_stream, token);
576 putc ('\n', cp_lexer_debug_stream);
579 return token;
582 /* Permanently remove the next token from the token stream, and
583 advance the next_token pointer to refer to the next non-purged
584 token. */
586 static void
587 cp_lexer_purge_token (cp_lexer *lexer)
589 cp_token *tok = lexer->next_token;
591 gcc_assert (tok != &eof_token);
592 tok->type = CPP_PURGED;
593 tok->location = UNKNOWN_LOCATION;
594 tok->value = NULL_TREE;
595 tok->keyword = RID_MAX;
599 tok++;
600 if (tok == lexer->last_token)
602 tok = (cp_token *)&eof_token;
603 break;
606 while (tok->type == CPP_PURGED);
607 lexer->next_token = tok;
610 /* Permanently remove all tokens after TOK, up to, but not
611 including, the token that will be returned next by
612 cp_lexer_peek_token. */
614 static void
615 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
617 cp_token *peek = lexer->next_token;
619 if (peek == &eof_token)
620 peek = lexer->last_token;
622 gcc_assert (tok < peek);
624 for ( tok += 1; tok != peek; tok += 1)
626 tok->type = CPP_PURGED;
627 tok->location = UNKNOWN_LOCATION;
628 tok->value = NULL_TREE;
629 tok->keyword = RID_MAX;
633 /* Consume and handle a pragma token. */
634 static void
635 cp_lexer_handle_pragma (cp_lexer *lexer)
637 cpp_string s;
638 cp_token *token = cp_lexer_consume_token (lexer);
639 gcc_assert (token->type == CPP_PRAGMA);
640 gcc_assert (token->value);
642 s.len = TREE_STRING_LENGTH (token->value);
643 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
645 cpp_handle_deferred_pragma (parse_in, &s);
647 /* Clearing token->value here means that we will get an ICE if we
648 try to process this #pragma again (which should be impossible). */
649 token->value = NULL;
652 /* Begin saving tokens. All tokens consumed after this point will be
653 preserved. */
655 static void
656 cp_lexer_save_tokens (cp_lexer* lexer)
658 /* Provide debugging output. */
659 if (cp_lexer_debugging_p (lexer))
660 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
662 VEC_safe_push (cp_token_position, heap,
663 lexer->saved_tokens, lexer->next_token);
666 /* Commit to the portion of the token stream most recently saved. */
668 static void
669 cp_lexer_commit_tokens (cp_lexer* lexer)
671 /* Provide debugging output. */
672 if (cp_lexer_debugging_p (lexer))
673 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
675 VEC_pop (cp_token_position, lexer->saved_tokens);
678 /* Return all tokens saved since the last call to cp_lexer_save_tokens
679 to the token stream. Stop saving tokens. */
681 static void
682 cp_lexer_rollback_tokens (cp_lexer* lexer)
684 /* Provide debugging output. */
685 if (cp_lexer_debugging_p (lexer))
686 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
688 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
691 /* Print a representation of the TOKEN on the STREAM. */
693 #ifdef ENABLE_CHECKING
695 static void
696 cp_lexer_print_token (FILE * stream, cp_token *token)
698 /* We don't use cpp_type2name here because the parser defines
699 a few tokens of its own. */
700 static const char *const token_names[] = {
701 /* cpplib-defined token types */
702 #define OP(e, s) #e,
703 #define TK(e, s) #e,
704 TTYPE_TABLE
705 #undef OP
706 #undef TK
707 /* C++ parser token types - see "Manifest constants", above. */
708 "KEYWORD",
709 "TEMPLATE_ID",
710 "NESTED_NAME_SPECIFIER",
711 "PURGED"
714 /* If we have a name for the token, print it out. Otherwise, we
715 simply give the numeric code. */
716 gcc_assert (token->type < ARRAY_SIZE(token_names));
717 fputs (token_names[token->type], stream);
719 /* For some tokens, print the associated data. */
720 switch (token->type)
722 case CPP_KEYWORD:
723 /* Some keywords have a value that is not an IDENTIFIER_NODE.
724 For example, `struct' is mapped to an INTEGER_CST. */
725 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
726 break;
727 /* else fall through */
728 case CPP_NAME:
729 fputs (IDENTIFIER_POINTER (token->value), stream);
730 break;
732 case CPP_STRING:
733 case CPP_WSTRING:
734 case CPP_PRAGMA:
735 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
736 break;
738 default:
739 break;
743 /* Start emitting debugging information. */
745 static void
746 cp_lexer_start_debugging (cp_lexer* lexer)
748 lexer->debugging_p = true;
751 /* Stop emitting debugging information. */
753 static void
754 cp_lexer_stop_debugging (cp_lexer* lexer)
756 lexer->debugging_p = false;
759 #endif /* ENABLE_CHECKING */
761 /* Create a new cp_token_cache, representing a range of tokens. */
763 static cp_token_cache *
764 cp_token_cache_new (cp_token *first, cp_token *last)
766 cp_token_cache *cache = GGC_NEW (cp_token_cache);
767 cache->first = first;
768 cache->last = last;
769 return cache;
773 /* Decl-specifiers. */
775 static void clear_decl_specs
776 (cp_decl_specifier_seq *);
778 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
780 static void
781 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
783 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
786 /* Declarators. */
788 /* Nothing other than the parser should be creating declarators;
789 declarators are a semi-syntactic representation of C++ entities.
790 Other parts of the front end that need to create entities (like
791 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
793 static cp_declarator *make_call_declarator
794 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
795 static cp_declarator *make_array_declarator
796 (cp_declarator *, tree);
797 static cp_declarator *make_pointer_declarator
798 (cp_cv_quals, cp_declarator *);
799 static cp_declarator *make_reference_declarator
800 (cp_cv_quals, cp_declarator *);
801 static cp_parameter_declarator *make_parameter_declarator
802 (cp_decl_specifier_seq *, cp_declarator *, tree);
803 static cp_declarator *make_ptrmem_declarator
804 (cp_cv_quals, tree, cp_declarator *);
806 cp_declarator *cp_error_declarator;
808 /* The obstack on which declarators and related data structures are
809 allocated. */
810 static struct obstack declarator_obstack;
812 /* Alloc BYTES from the declarator memory pool. */
814 static inline void *
815 alloc_declarator (size_t bytes)
817 return obstack_alloc (&declarator_obstack, bytes);
820 /* Allocate a declarator of the indicated KIND. Clear fields that are
821 common to all declarators. */
823 static cp_declarator *
824 make_declarator (cp_declarator_kind kind)
826 cp_declarator *declarator;
828 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
829 declarator->kind = kind;
830 declarator->attributes = NULL_TREE;
831 declarator->declarator = NULL;
833 return declarator;
836 /* Make a declarator for a generalized identifier. If non-NULL, the
837 identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
838 just UNQUALIFIED_NAME. */
840 static cp_declarator *
841 make_id_declarator (tree qualifying_scope, tree unqualified_name)
843 cp_declarator *declarator;
845 /* It is valid to write:
847 class C { void f(); };
848 typedef C D;
849 void D::f();
851 The standard is not clear about whether `typedef const C D' is
852 legal; as of 2002-09-15 the committee is considering that
853 question. EDG 3.0 allows that syntax. Therefore, we do as
854 well. */
855 if (qualifying_scope && TYPE_P (qualifying_scope))
856 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
858 declarator = make_declarator (cdk_id);
859 declarator->u.id.qualifying_scope = qualifying_scope;
860 declarator->u.id.unqualified_name = unqualified_name;
861 declarator->u.id.sfk = sfk_none;
863 return declarator;
866 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
867 of modifiers such as const or volatile to apply to the pointer
868 type, represented as identifiers. */
870 cp_declarator *
871 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
873 cp_declarator *declarator;
875 declarator = make_declarator (cdk_pointer);
876 declarator->declarator = target;
877 declarator->u.pointer.qualifiers = cv_qualifiers;
878 declarator->u.pointer.class_type = NULL_TREE;
880 return declarator;
883 /* Like make_pointer_declarator -- but for references. */
885 cp_declarator *
886 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
888 cp_declarator *declarator;
890 declarator = make_declarator (cdk_reference);
891 declarator->declarator = target;
892 declarator->u.pointer.qualifiers = cv_qualifiers;
893 declarator->u.pointer.class_type = NULL_TREE;
895 return declarator;
898 /* Like make_pointer_declarator -- but for a pointer to a non-static
899 member of CLASS_TYPE. */
901 cp_declarator *
902 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
903 cp_declarator *pointee)
905 cp_declarator *declarator;
907 declarator = make_declarator (cdk_ptrmem);
908 declarator->declarator = pointee;
909 declarator->u.pointer.qualifiers = cv_qualifiers;
910 declarator->u.pointer.class_type = class_type;
912 return declarator;
915 /* Make a declarator for the function given by TARGET, with the
916 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
917 "const"-qualified member function. The EXCEPTION_SPECIFICATION
918 indicates what exceptions can be thrown. */
920 cp_declarator *
921 make_call_declarator (cp_declarator *target,
922 cp_parameter_declarator *parms,
923 cp_cv_quals cv_qualifiers,
924 tree exception_specification)
926 cp_declarator *declarator;
928 declarator = make_declarator (cdk_function);
929 declarator->declarator = target;
930 declarator->u.function.parameters = parms;
931 declarator->u.function.qualifiers = cv_qualifiers;
932 declarator->u.function.exception_specification = exception_specification;
934 return declarator;
937 /* Make a declarator for an array of BOUNDS elements, each of which is
938 defined by ELEMENT. */
940 cp_declarator *
941 make_array_declarator (cp_declarator *element, tree bounds)
943 cp_declarator *declarator;
945 declarator = make_declarator (cdk_array);
946 declarator->declarator = element;
947 declarator->u.array.bounds = bounds;
949 return declarator;
952 cp_parameter_declarator *no_parameters;
954 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
955 DECLARATOR and DEFAULT_ARGUMENT. */
957 cp_parameter_declarator *
958 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
959 cp_declarator *declarator,
960 tree default_argument)
962 cp_parameter_declarator *parameter;
964 parameter = ((cp_parameter_declarator *)
965 alloc_declarator (sizeof (cp_parameter_declarator)));
966 parameter->next = NULL;
967 if (decl_specifiers)
968 parameter->decl_specifiers = *decl_specifiers;
969 else
970 clear_decl_specs (&parameter->decl_specifiers);
971 parameter->declarator = declarator;
972 parameter->default_argument = default_argument;
973 parameter->ellipsis_p = false;
975 return parameter;
978 /* The parser. */
980 /* Overview
981 --------
983 A cp_parser parses the token stream as specified by the C++
984 grammar. Its job is purely parsing, not semantic analysis. For
985 example, the parser breaks the token stream into declarators,
986 expressions, statements, and other similar syntactic constructs.
987 It does not check that the types of the expressions on either side
988 of an assignment-statement are compatible, or that a function is
989 not declared with a parameter of type `void'.
991 The parser invokes routines elsewhere in the compiler to perform
992 semantic analysis and to build up the abstract syntax tree for the
993 code processed.
995 The parser (and the template instantiation code, which is, in a
996 way, a close relative of parsing) are the only parts of the
997 compiler that should be calling push_scope and pop_scope, or
998 related functions. The parser (and template instantiation code)
999 keeps track of what scope is presently active; everything else
1000 should simply honor that. (The code that generates static
1001 initializers may also need to set the scope, in order to check
1002 access control correctly when emitting the initializers.)
1004 Methodology
1005 -----------
1007 The parser is of the standard recursive-descent variety. Upcoming
1008 tokens in the token stream are examined in order to determine which
1009 production to use when parsing a non-terminal. Some C++ constructs
1010 require arbitrary look ahead to disambiguate. For example, it is
1011 impossible, in the general case, to tell whether a statement is an
1012 expression or declaration without scanning the entire statement.
1013 Therefore, the parser is capable of "parsing tentatively." When the
1014 parser is not sure what construct comes next, it enters this mode.
1015 Then, while we attempt to parse the construct, the parser queues up
1016 error messages, rather than issuing them immediately, and saves the
1017 tokens it consumes. If the construct is parsed successfully, the
1018 parser "commits", i.e., it issues any queued error messages and
1019 the tokens that were being preserved are permanently discarded.
1020 If, however, the construct is not parsed successfully, the parser
1021 rolls back its state completely so that it can resume parsing using
1022 a different alternative.
1024 Future Improvements
1025 -------------------
1027 The performance of the parser could probably be improved substantially.
1028 We could often eliminate the need to parse tentatively by looking ahead
1029 a little bit. In some places, this approach might not entirely eliminate
1030 the need to parse tentatively, but it might still speed up the average
1031 case. */
1033 /* Flags that are passed to some parsing functions. These values can
1034 be bitwise-ored together. */
1036 typedef enum cp_parser_flags
1038 /* No flags. */
1039 CP_PARSER_FLAGS_NONE = 0x0,
1040 /* The construct is optional. If it is not present, then no error
1041 should be issued. */
1042 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1043 /* When parsing a type-specifier, do not allow user-defined types. */
1044 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1045 } cp_parser_flags;
1047 /* The different kinds of declarators we want to parse. */
1049 typedef enum cp_parser_declarator_kind
1051 /* We want an abstract declarator. */
1052 CP_PARSER_DECLARATOR_ABSTRACT,
1053 /* We want a named declarator. */
1054 CP_PARSER_DECLARATOR_NAMED,
1055 /* We don't mind, but the name must be an unqualified-id. */
1056 CP_PARSER_DECLARATOR_EITHER
1057 } cp_parser_declarator_kind;
1059 /* The precedence values used to parse binary expressions. The minimum value
1060 of PREC must be 1, because zero is reserved to quickly discriminate
1061 binary operators from other tokens. */
1063 enum cp_parser_prec
1065 PREC_NOT_OPERATOR,
1066 PREC_LOGICAL_OR_EXPRESSION,
1067 PREC_LOGICAL_AND_EXPRESSION,
1068 PREC_INCLUSIVE_OR_EXPRESSION,
1069 PREC_EXCLUSIVE_OR_EXPRESSION,
1070 PREC_AND_EXPRESSION,
1071 PREC_EQUALITY_EXPRESSION,
1072 PREC_RELATIONAL_EXPRESSION,
1073 PREC_SHIFT_EXPRESSION,
1074 PREC_ADDITIVE_EXPRESSION,
1075 PREC_MULTIPLICATIVE_EXPRESSION,
1076 PREC_PM_EXPRESSION,
1077 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1080 /* A mapping from a token type to a corresponding tree node type, with a
1081 precedence value. */
1083 typedef struct cp_parser_binary_operations_map_node
1085 /* The token type. */
1086 enum cpp_ttype token_type;
1087 /* The corresponding tree code. */
1088 enum tree_code tree_type;
1089 /* The precedence of this operator. */
1090 enum cp_parser_prec prec;
1091 } cp_parser_binary_operations_map_node;
1093 /* The status of a tentative parse. */
1095 typedef enum cp_parser_status_kind
1097 /* No errors have occurred. */
1098 CP_PARSER_STATUS_KIND_NO_ERROR,
1099 /* An error has occurred. */
1100 CP_PARSER_STATUS_KIND_ERROR,
1101 /* We are committed to this tentative parse, whether or not an error
1102 has occurred. */
1103 CP_PARSER_STATUS_KIND_COMMITTED
1104 } cp_parser_status_kind;
1106 typedef struct cp_parser_expression_stack_entry
1108 tree lhs;
1109 enum tree_code tree_type;
1110 int prec;
1111 } cp_parser_expression_stack_entry;
1113 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1114 entries because precedence levels on the stack are monotonically
1115 increasing. */
1116 typedef struct cp_parser_expression_stack_entry
1117 cp_parser_expression_stack[NUM_PREC_VALUES];
1119 /* Context that is saved and restored when parsing tentatively. */
1120 typedef struct cp_parser_context GTY (())
1122 /* If this is a tentative parsing context, the status of the
1123 tentative parse. */
1124 enum cp_parser_status_kind status;
1125 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1126 that are looked up in this context must be looked up both in the
1127 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1128 the context of the containing expression. */
1129 tree object_type;
1131 /* The next parsing context in the stack. */
1132 struct cp_parser_context *next;
1133 } cp_parser_context;
1135 /* Prototypes. */
1137 /* Constructors and destructors. */
1139 static cp_parser_context *cp_parser_context_new
1140 (cp_parser_context *);
1142 /* Class variables. */
1144 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1146 /* The operator-precedence table used by cp_parser_binary_expression.
1147 Transformed into an associative array (binops_by_token) by
1148 cp_parser_new. */
1150 static const cp_parser_binary_operations_map_node binops[] = {
1151 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1152 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1154 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1155 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1156 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1158 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1159 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1161 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1162 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1164 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1165 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1166 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1167 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1168 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1169 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1171 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1172 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1174 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1176 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1178 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1180 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1182 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1185 /* The same as binops, but initialized by cp_parser_new so that
1186 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1187 for speed. */
1188 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1190 /* Constructors and destructors. */
1192 /* Construct a new context. The context below this one on the stack
1193 is given by NEXT. */
1195 static cp_parser_context *
1196 cp_parser_context_new (cp_parser_context* next)
1198 cp_parser_context *context;
1200 /* Allocate the storage. */
1201 if (cp_parser_context_free_list != NULL)
1203 /* Pull the first entry from the free list. */
1204 context = cp_parser_context_free_list;
1205 cp_parser_context_free_list = context->next;
1206 memset (context, 0, sizeof (*context));
1208 else
1209 context = GGC_CNEW (cp_parser_context);
1211 /* No errors have occurred yet in this context. */
1212 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1213 /* If this is not the bottomost context, copy information that we
1214 need from the previous context. */
1215 if (next)
1217 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1218 expression, then we are parsing one in this context, too. */
1219 context->object_type = next->object_type;
1220 /* Thread the stack. */
1221 context->next = next;
1224 return context;
1227 /* The cp_parser structure represents the C++ parser. */
1229 typedef struct cp_parser GTY(())
1231 /* The lexer from which we are obtaining tokens. */
1232 cp_lexer *lexer;
1234 /* The scope in which names should be looked up. If NULL_TREE, then
1235 we look up names in the scope that is currently open in the
1236 source program. If non-NULL, this is either a TYPE or
1237 NAMESPACE_DECL for the scope in which we should look. It can
1238 also be ERROR_MARK, when we've parsed a bogus scope.
1240 This value is not cleared automatically after a name is looked
1241 up, so we must be careful to clear it before starting a new look
1242 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1243 will look up `Z' in the scope of `X', rather than the current
1244 scope.) Unfortunately, it is difficult to tell when name lookup
1245 is complete, because we sometimes peek at a token, look it up,
1246 and then decide not to consume it. */
1247 tree scope;
1249 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1250 last lookup took place. OBJECT_SCOPE is used if an expression
1251 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1252 respectively. QUALIFYING_SCOPE is used for an expression of the
1253 form "X::Y"; it refers to X. */
1254 tree object_scope;
1255 tree qualifying_scope;
1257 /* A stack of parsing contexts. All but the bottom entry on the
1258 stack will be tentative contexts.
1260 We parse tentatively in order to determine which construct is in
1261 use in some situations. For example, in order to determine
1262 whether a statement is an expression-statement or a
1263 declaration-statement we parse it tentatively as a
1264 declaration-statement. If that fails, we then reparse the same
1265 token stream as an expression-statement. */
1266 cp_parser_context *context;
1268 /* True if we are parsing GNU C++. If this flag is not set, then
1269 GNU extensions are not recognized. */
1270 bool allow_gnu_extensions_p;
1272 /* TRUE if the `>' token should be interpreted as the greater-than
1273 operator. FALSE if it is the end of a template-id or
1274 template-parameter-list. */
1275 bool greater_than_is_operator_p;
1277 /* TRUE if default arguments are allowed within a parameter list
1278 that starts at this point. FALSE if only a gnu extension makes
1279 them permissible. */
1280 bool default_arg_ok_p;
1282 /* TRUE if we are parsing an integral constant-expression. See
1283 [expr.const] for a precise definition. */
1284 bool integral_constant_expression_p;
1286 /* TRUE if we are parsing an integral constant-expression -- but a
1287 non-constant expression should be permitted as well. This flag
1288 is used when parsing an array bound so that GNU variable-length
1289 arrays are tolerated. */
1290 bool allow_non_integral_constant_expression_p;
1292 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1293 been seen that makes the expression non-constant. */
1294 bool non_integral_constant_expression_p;
1296 /* TRUE if local variable names and `this' are forbidden in the
1297 current context. */
1298 bool local_variables_forbidden_p;
1300 /* TRUE if the declaration we are parsing is part of a
1301 linkage-specification of the form `extern string-literal
1302 declaration'. */
1303 bool in_unbraced_linkage_specification_p;
1305 /* TRUE if we are presently parsing a declarator, after the
1306 direct-declarator. */
1307 bool in_declarator_p;
1309 /* TRUE if we are presently parsing a template-argument-list. */
1310 bool in_template_argument_list_p;
1312 /* TRUE if we are presently parsing the body of an
1313 iteration-statement. */
1314 bool in_iteration_statement_p;
1316 /* TRUE if we are presently parsing the body of a switch
1317 statement. */
1318 bool in_switch_statement_p;
1320 /* TRUE if we are parsing a type-id in an expression context. In
1321 such a situation, both "type (expr)" and "type (type)" are valid
1322 alternatives. */
1323 bool in_type_id_in_expr_p;
1325 /* TRUE if we are currently in a header file where declarations are
1326 implicitly extern "C". */
1327 bool implicit_extern_c;
1329 /* TRUE if strings in expressions should be translated to the execution
1330 character set. */
1331 bool translate_strings_p;
1333 /* If non-NULL, then we are parsing a construct where new type
1334 definitions are not permitted. The string stored here will be
1335 issued as an error message if a type is defined. */
1336 const char *type_definition_forbidden_message;
1338 /* A list of lists. The outer list is a stack, used for member
1339 functions of local classes. At each level there are two sub-list,
1340 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1341 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1342 TREE_VALUE's. The functions are chained in reverse declaration
1343 order.
1345 The TREE_PURPOSE sublist contains those functions with default
1346 arguments that need post processing, and the TREE_VALUE sublist
1347 contains those functions with definitions that need post
1348 processing.
1350 These lists can only be processed once the outermost class being
1351 defined is complete. */
1352 tree unparsed_functions_queues;
1354 /* The number of classes whose definitions are currently in
1355 progress. */
1356 unsigned num_classes_being_defined;
1358 /* The number of template parameter lists that apply directly to the
1359 current declaration. */
1360 unsigned num_template_parameter_lists;
1361 } cp_parser;
1363 /* The type of a function that parses some kind of expression. */
1364 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1366 /* Prototypes. */
1368 /* Constructors and destructors. */
1370 static cp_parser *cp_parser_new
1371 (void);
1373 /* Routines to parse various constructs.
1375 Those that return `tree' will return the error_mark_node (rather
1376 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1377 Sometimes, they will return an ordinary node if error-recovery was
1378 attempted, even though a parse error occurred. So, to check
1379 whether or not a parse error occurred, you should always use
1380 cp_parser_error_occurred. If the construct is optional (indicated
1381 either by an `_opt' in the name of the function that does the
1382 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1383 the construct is not present. */
1385 /* Lexical conventions [gram.lex] */
1387 static tree cp_parser_identifier
1388 (cp_parser *);
1389 static tree cp_parser_string_literal
1390 (cp_parser *, bool, bool);
1392 /* Basic concepts [gram.basic] */
1394 static bool cp_parser_translation_unit
1395 (cp_parser *);
1397 /* Expressions [gram.expr] */
1399 static tree cp_parser_primary_expression
1400 (cp_parser *, bool, bool, bool, cp_id_kind *);
1401 static tree cp_parser_id_expression
1402 (cp_parser *, bool, bool, bool *, bool);
1403 static tree cp_parser_unqualified_id
1404 (cp_parser *, bool, bool, bool);
1405 static tree cp_parser_nested_name_specifier_opt
1406 (cp_parser *, bool, bool, bool, bool);
1407 static tree cp_parser_nested_name_specifier
1408 (cp_parser *, bool, bool, bool, bool);
1409 static tree cp_parser_class_or_namespace_name
1410 (cp_parser *, bool, bool, bool, bool, bool);
1411 static tree cp_parser_postfix_expression
1412 (cp_parser *, bool, bool);
1413 static tree cp_parser_postfix_open_square_expression
1414 (cp_parser *, tree, bool);
1415 static tree cp_parser_postfix_dot_deref_expression
1416 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1417 static tree cp_parser_parenthesized_expression_list
1418 (cp_parser *, bool, bool, bool *);
1419 static void cp_parser_pseudo_destructor_name
1420 (cp_parser *, tree *, tree *);
1421 static tree cp_parser_unary_expression
1422 (cp_parser *, bool, bool);
1423 static enum tree_code cp_parser_unary_operator
1424 (cp_token *);
1425 static tree cp_parser_new_expression
1426 (cp_parser *);
1427 static tree cp_parser_new_placement
1428 (cp_parser *);
1429 static tree cp_parser_new_type_id
1430 (cp_parser *, tree *);
1431 static cp_declarator *cp_parser_new_declarator_opt
1432 (cp_parser *);
1433 static cp_declarator *cp_parser_direct_new_declarator
1434 (cp_parser *);
1435 static tree cp_parser_new_initializer
1436 (cp_parser *);
1437 static tree cp_parser_delete_expression
1438 (cp_parser *);
1439 static tree cp_parser_cast_expression
1440 (cp_parser *, bool, bool);
1441 static tree cp_parser_binary_expression
1442 (cp_parser *, bool);
1443 static tree cp_parser_question_colon_clause
1444 (cp_parser *, tree);
1445 static tree cp_parser_assignment_expression
1446 (cp_parser *, bool);
1447 static enum tree_code cp_parser_assignment_operator_opt
1448 (cp_parser *);
1449 static tree cp_parser_expression
1450 (cp_parser *, bool);
1451 static tree cp_parser_constant_expression
1452 (cp_parser *, bool, bool *);
1453 static tree cp_parser_builtin_offsetof
1454 (cp_parser *);
1456 /* Statements [gram.stmt.stmt] */
1458 static void cp_parser_statement
1459 (cp_parser *, tree);
1460 static tree cp_parser_labeled_statement
1461 (cp_parser *, tree);
1462 static tree cp_parser_expression_statement
1463 (cp_parser *, tree);
1464 static tree cp_parser_compound_statement
1465 (cp_parser *, tree, bool);
1466 static void cp_parser_statement_seq_opt
1467 (cp_parser *, tree);
1468 static tree cp_parser_selection_statement
1469 (cp_parser *);
1470 static tree cp_parser_condition
1471 (cp_parser *);
1472 static tree cp_parser_iteration_statement
1473 (cp_parser *);
1474 static void cp_parser_for_init_statement
1475 (cp_parser *);
1476 static tree cp_parser_jump_statement
1477 (cp_parser *);
1478 static void cp_parser_declaration_statement
1479 (cp_parser *);
1481 static tree cp_parser_implicitly_scoped_statement
1482 (cp_parser *);
1483 static void cp_parser_already_scoped_statement
1484 (cp_parser *);
1486 /* Declarations [gram.dcl.dcl] */
1488 static void cp_parser_declaration_seq_opt
1489 (cp_parser *);
1490 static void cp_parser_declaration
1491 (cp_parser *);
1492 static void cp_parser_block_declaration
1493 (cp_parser *, bool);
1494 static void cp_parser_simple_declaration
1495 (cp_parser *, bool);
1496 static void cp_parser_decl_specifier_seq
1497 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1498 static tree cp_parser_storage_class_specifier_opt
1499 (cp_parser *);
1500 static tree cp_parser_function_specifier_opt
1501 (cp_parser *, cp_decl_specifier_seq *);
1502 static tree cp_parser_type_specifier
1503 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1504 int *, bool *);
1505 static tree cp_parser_simple_type_specifier
1506 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1507 static tree cp_parser_type_name
1508 (cp_parser *);
1509 static tree cp_parser_elaborated_type_specifier
1510 (cp_parser *, bool, bool);
1511 static tree cp_parser_enum_specifier
1512 (cp_parser *);
1513 static void cp_parser_enumerator_list
1514 (cp_parser *, tree);
1515 static void cp_parser_enumerator_definition
1516 (cp_parser *, tree);
1517 static tree cp_parser_namespace_name
1518 (cp_parser *);
1519 static void cp_parser_namespace_definition
1520 (cp_parser *);
1521 static void cp_parser_namespace_body
1522 (cp_parser *);
1523 static tree cp_parser_qualified_namespace_specifier
1524 (cp_parser *);
1525 static void cp_parser_namespace_alias_definition
1526 (cp_parser *);
1527 static void cp_parser_using_declaration
1528 (cp_parser *);
1529 static void cp_parser_using_directive
1530 (cp_parser *);
1531 static void cp_parser_asm_definition
1532 (cp_parser *);
1533 static void cp_parser_linkage_specification
1534 (cp_parser *);
1536 /* Declarators [gram.dcl.decl] */
1538 static tree cp_parser_init_declarator
1539 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1540 static cp_declarator *cp_parser_declarator
1541 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1542 static cp_declarator *cp_parser_direct_declarator
1543 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1544 static enum tree_code cp_parser_ptr_operator
1545 (cp_parser *, tree *, cp_cv_quals *);
1546 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1547 (cp_parser *);
1548 static tree cp_parser_declarator_id
1549 (cp_parser *);
1550 static tree cp_parser_type_id
1551 (cp_parser *);
1552 static void cp_parser_type_specifier_seq
1553 (cp_parser *, bool, cp_decl_specifier_seq *);
1554 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1555 (cp_parser *);
1556 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1557 (cp_parser *, bool *);
1558 static cp_parameter_declarator *cp_parser_parameter_declaration
1559 (cp_parser *, bool, bool *);
1560 static void cp_parser_function_body
1561 (cp_parser *);
1562 static tree cp_parser_initializer
1563 (cp_parser *, bool *, bool *);
1564 static tree cp_parser_initializer_clause
1565 (cp_parser *, bool *);
1566 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1567 (cp_parser *, bool *);
1569 static bool cp_parser_ctor_initializer_opt_and_function_body
1570 (cp_parser *);
1572 /* Classes [gram.class] */
1574 static tree cp_parser_class_name
1575 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1576 static tree cp_parser_class_specifier
1577 (cp_parser *);
1578 static tree cp_parser_class_head
1579 (cp_parser *, bool *, tree *);
1580 static enum tag_types cp_parser_class_key
1581 (cp_parser *);
1582 static void cp_parser_member_specification_opt
1583 (cp_parser *);
1584 static void cp_parser_member_declaration
1585 (cp_parser *);
1586 static tree cp_parser_pure_specifier
1587 (cp_parser *);
1588 static tree cp_parser_constant_initializer
1589 (cp_parser *);
1591 /* Derived classes [gram.class.derived] */
1593 static tree cp_parser_base_clause
1594 (cp_parser *);
1595 static tree cp_parser_base_specifier
1596 (cp_parser *);
1598 /* Special member functions [gram.special] */
1600 static tree cp_parser_conversion_function_id
1601 (cp_parser *);
1602 static tree cp_parser_conversion_type_id
1603 (cp_parser *);
1604 static cp_declarator *cp_parser_conversion_declarator_opt
1605 (cp_parser *);
1606 static bool cp_parser_ctor_initializer_opt
1607 (cp_parser *);
1608 static void cp_parser_mem_initializer_list
1609 (cp_parser *);
1610 static tree cp_parser_mem_initializer
1611 (cp_parser *);
1612 static tree cp_parser_mem_initializer_id
1613 (cp_parser *);
1615 /* Overloading [gram.over] */
1617 static tree cp_parser_operator_function_id
1618 (cp_parser *);
1619 static tree cp_parser_operator
1620 (cp_parser *);
1622 /* Templates [gram.temp] */
1624 static void cp_parser_template_declaration
1625 (cp_parser *, bool);
1626 static tree cp_parser_template_parameter_list
1627 (cp_parser *);
1628 static tree cp_parser_template_parameter
1629 (cp_parser *, bool *);
1630 static tree cp_parser_type_parameter
1631 (cp_parser *);
1632 static tree cp_parser_template_id
1633 (cp_parser *, bool, bool, bool);
1634 static tree cp_parser_template_name
1635 (cp_parser *, bool, bool, bool, bool *);
1636 static tree cp_parser_template_argument_list
1637 (cp_parser *);
1638 static tree cp_parser_template_argument
1639 (cp_parser *);
1640 static void cp_parser_explicit_instantiation
1641 (cp_parser *);
1642 static void cp_parser_explicit_specialization
1643 (cp_parser *);
1645 /* Exception handling [gram.exception] */
1647 static tree cp_parser_try_block
1648 (cp_parser *);
1649 static bool cp_parser_function_try_block
1650 (cp_parser *);
1651 static void cp_parser_handler_seq
1652 (cp_parser *);
1653 static void cp_parser_handler
1654 (cp_parser *);
1655 static tree cp_parser_exception_declaration
1656 (cp_parser *);
1657 static tree cp_parser_throw_expression
1658 (cp_parser *);
1659 static tree cp_parser_exception_specification_opt
1660 (cp_parser *);
1661 static tree cp_parser_type_id_list
1662 (cp_parser *);
1664 /* GNU Extensions */
1666 static tree cp_parser_asm_specification_opt
1667 (cp_parser *);
1668 static tree cp_parser_asm_operand_list
1669 (cp_parser *);
1670 static tree cp_parser_asm_clobber_list
1671 (cp_parser *);
1672 static tree cp_parser_attributes_opt
1673 (cp_parser *);
1674 static tree cp_parser_attribute_list
1675 (cp_parser *);
1676 static bool cp_parser_extension_opt
1677 (cp_parser *, int *);
1678 static void cp_parser_label_declaration
1679 (cp_parser *);
1681 /* Objective-C++ Productions */
1683 static tree cp_parser_objc_message_receiver
1684 (cp_parser *);
1685 static tree cp_parser_objc_message_args
1686 (cp_parser *);
1687 static tree cp_parser_objc_message_expression
1688 (cp_parser *);
1689 static tree cp_parser_objc_encode_expression
1690 (cp_parser *);
1691 static tree cp_parser_objc_defs_expression
1692 (cp_parser *);
1693 static tree cp_parser_objc_protocol_expression
1694 (cp_parser *);
1695 static tree cp_parser_objc_selector_expression
1696 (cp_parser *);
1697 static tree cp_parser_objc_expression
1698 (cp_parser *);
1699 static bool cp_parser_objc_selector_p
1700 (enum cpp_ttype);
1701 static tree cp_parser_objc_selector
1702 (cp_parser *);
1703 static tree cp_parser_objc_protocol_refs_opt
1704 (cp_parser *);
1705 static void cp_parser_objc_declaration
1706 (cp_parser *);
1707 static tree cp_parser_objc_statement
1708 (cp_parser *);
1710 /* Utility Routines */
1712 static tree cp_parser_lookup_name
1713 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1714 static tree cp_parser_lookup_name_simple
1715 (cp_parser *, tree);
1716 static tree cp_parser_maybe_treat_template_as_class
1717 (tree, bool);
1718 static bool cp_parser_check_declarator_template_parameters
1719 (cp_parser *, cp_declarator *);
1720 static bool cp_parser_check_template_parameters
1721 (cp_parser *, unsigned);
1722 static tree cp_parser_simple_cast_expression
1723 (cp_parser *);
1724 static tree cp_parser_global_scope_opt
1725 (cp_parser *, bool);
1726 static bool cp_parser_constructor_declarator_p
1727 (cp_parser *, bool);
1728 static tree cp_parser_function_definition_from_specifiers_and_declarator
1729 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1730 static tree cp_parser_function_definition_after_declarator
1731 (cp_parser *, bool);
1732 static void cp_parser_template_declaration_after_export
1733 (cp_parser *, bool);
1734 static tree cp_parser_single_declaration
1735 (cp_parser *, bool, bool *);
1736 static tree cp_parser_functional_cast
1737 (cp_parser *, tree);
1738 static tree cp_parser_save_member_function_body
1739 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1740 static tree cp_parser_enclosed_template_argument_list
1741 (cp_parser *);
1742 static void cp_parser_save_default_args
1743 (cp_parser *, tree);
1744 static void cp_parser_late_parsing_for_member
1745 (cp_parser *, tree);
1746 static void cp_parser_late_parsing_default_args
1747 (cp_parser *, tree);
1748 static tree cp_parser_sizeof_operand
1749 (cp_parser *, enum rid);
1750 static bool cp_parser_declares_only_class_p
1751 (cp_parser *);
1752 static void cp_parser_set_storage_class
1753 (cp_decl_specifier_seq *, cp_storage_class);
1754 static void cp_parser_set_decl_spec_type
1755 (cp_decl_specifier_seq *, tree, bool);
1756 static bool cp_parser_friend_p
1757 (const cp_decl_specifier_seq *);
1758 static cp_token *cp_parser_require
1759 (cp_parser *, enum cpp_ttype, const char *);
1760 static cp_token *cp_parser_require_keyword
1761 (cp_parser *, enum rid, const char *);
1762 static bool cp_parser_token_starts_function_definition_p
1763 (cp_token *);
1764 static bool cp_parser_next_token_starts_class_definition_p
1765 (cp_parser *);
1766 static bool cp_parser_next_token_ends_template_argument_p
1767 (cp_parser *);
1768 static bool cp_parser_nth_token_starts_template_argument_list_p
1769 (cp_parser *, size_t);
1770 static enum tag_types cp_parser_token_is_class_key
1771 (cp_token *);
1772 static void cp_parser_check_class_key
1773 (enum tag_types, tree type);
1774 static void cp_parser_check_access_in_redeclaration
1775 (tree type);
1776 static bool cp_parser_optional_template_keyword
1777 (cp_parser *);
1778 static void cp_parser_pre_parsed_nested_name_specifier
1779 (cp_parser *);
1780 static void cp_parser_cache_group
1781 (cp_parser *, enum cpp_ttype, unsigned);
1782 static void cp_parser_parse_tentatively
1783 (cp_parser *);
1784 static void cp_parser_commit_to_tentative_parse
1785 (cp_parser *);
1786 static void cp_parser_abort_tentative_parse
1787 (cp_parser *);
1788 static bool cp_parser_parse_definitely
1789 (cp_parser *);
1790 static inline bool cp_parser_parsing_tentatively
1791 (cp_parser *);
1792 static bool cp_parser_uncommitted_to_tentative_parse_p
1793 (cp_parser *);
1794 static void cp_parser_error
1795 (cp_parser *, const char *);
1796 static void cp_parser_name_lookup_error
1797 (cp_parser *, tree, tree, const char *);
1798 static bool cp_parser_simulate_error
1799 (cp_parser *);
1800 static void cp_parser_check_type_definition
1801 (cp_parser *);
1802 static void cp_parser_check_for_definition_in_return_type
1803 (cp_declarator *, tree);
1804 static void cp_parser_check_for_invalid_template_id
1805 (cp_parser *, tree);
1806 static bool cp_parser_non_integral_constant_expression
1807 (cp_parser *, const char *);
1808 static void cp_parser_diagnose_invalid_type_name
1809 (cp_parser *, tree, tree);
1810 static bool cp_parser_parse_and_diagnose_invalid_type_name
1811 (cp_parser *);
1812 static int cp_parser_skip_to_closing_parenthesis
1813 (cp_parser *, bool, bool, bool);
1814 static void cp_parser_skip_to_end_of_statement
1815 (cp_parser *);
1816 static void cp_parser_consume_semicolon_at_end_of_statement
1817 (cp_parser *);
1818 static void cp_parser_skip_to_end_of_block_or_statement
1819 (cp_parser *);
1820 static void cp_parser_skip_to_closing_brace
1821 (cp_parser *);
1822 static void cp_parser_skip_until_found
1823 (cp_parser *, enum cpp_ttype, const char *);
1824 static bool cp_parser_error_occurred
1825 (cp_parser *);
1826 static bool cp_parser_allow_gnu_extensions_p
1827 (cp_parser *);
1828 static bool cp_parser_is_string_literal
1829 (cp_token *);
1830 static bool cp_parser_is_keyword
1831 (cp_token *, enum rid);
1832 static tree cp_parser_make_typename_type
1833 (cp_parser *, tree, tree);
1835 /* Returns nonzero if we are parsing tentatively. */
1837 static inline bool
1838 cp_parser_parsing_tentatively (cp_parser* parser)
1840 return parser->context->next != NULL;
1843 /* Returns nonzero if TOKEN is a string literal. */
1845 static bool
1846 cp_parser_is_string_literal (cp_token* token)
1848 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1851 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1853 static bool
1854 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1856 return token->keyword == keyword;
1859 /* A minimum or maximum operator has been seen. As these are
1860 deprecated, issue a warning. */
1862 static inline void
1863 cp_parser_warn_min_max (void)
1865 if (warn_deprecated && !in_system_header)
1866 warning (0, "minimum/maximum operators are deprecated");
1869 /* If not parsing tentatively, issue a diagnostic of the form
1870 FILE:LINE: MESSAGE before TOKEN
1871 where TOKEN is the next token in the input stream. MESSAGE
1872 (specified by the caller) is usually of the form "expected
1873 OTHER-TOKEN". */
1875 static void
1876 cp_parser_error (cp_parser* parser, const char* message)
1878 if (!cp_parser_simulate_error (parser))
1880 cp_token *token = cp_lexer_peek_token (parser->lexer);
1881 /* This diagnostic makes more sense if it is tagged to the line
1882 of the token we just peeked at. */
1883 cp_lexer_set_source_position_from_token (token);
1884 if (token->type == CPP_PRAGMA)
1886 error ("%<#pragma%> is not allowed here");
1887 cp_lexer_purge_token (parser->lexer);
1888 return;
1890 c_parse_error (message,
1891 /* Because c_parser_error does not understand
1892 CPP_KEYWORD, keywords are treated like
1893 identifiers. */
1894 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1895 token->value);
1899 /* Issue an error about name-lookup failing. NAME is the
1900 IDENTIFIER_NODE DECL is the result of
1901 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1902 the thing that we hoped to find. */
1904 static void
1905 cp_parser_name_lookup_error (cp_parser* parser,
1906 tree name,
1907 tree decl,
1908 const char* desired)
1910 /* If name lookup completely failed, tell the user that NAME was not
1911 declared. */
1912 if (decl == error_mark_node)
1914 if (parser->scope && parser->scope != global_namespace)
1915 error ("%<%D::%D%> has not been declared",
1916 parser->scope, name);
1917 else if (parser->scope == global_namespace)
1918 error ("%<::%D%> has not been declared", name);
1919 else if (parser->object_scope
1920 && !CLASS_TYPE_P (parser->object_scope))
1921 error ("request for member %qD in non-class type %qT",
1922 name, parser->object_scope);
1923 else if (parser->object_scope)
1924 error ("%<%T::%D%> has not been declared",
1925 parser->object_scope, name);
1926 else
1927 error ("%qD has not been declared", name);
1929 else if (parser->scope && parser->scope != global_namespace)
1930 error ("%<%D::%D%> %s", parser->scope, name, desired);
1931 else if (parser->scope == global_namespace)
1932 error ("%<::%D%> %s", name, desired);
1933 else
1934 error ("%qD %s", name, desired);
1937 /* If we are parsing tentatively, remember that an error has occurred
1938 during this tentative parse. Returns true if the error was
1939 simulated; false if a message should be issued by the caller. */
1941 static bool
1942 cp_parser_simulate_error (cp_parser* parser)
1944 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1946 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1947 return true;
1949 return false;
1952 /* This function is called when a type is defined. If type
1953 definitions are forbidden at this point, an error message is
1954 issued. */
1956 static void
1957 cp_parser_check_type_definition (cp_parser* parser)
1959 /* If types are forbidden here, issue a message. */
1960 if (parser->type_definition_forbidden_message)
1961 /* Use `%s' to print the string in case there are any escape
1962 characters in the message. */
1963 error ("%s", parser->type_definition_forbidden_message);
1966 /* This function is called when the DECLARATOR is processed. The TYPE
1967 was a type defined in the decl-specifiers. If it is invalid to
1968 define a type in the decl-specifiers for DECLARATOR, an error is
1969 issued. */
1971 static void
1972 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1973 tree type)
1975 /* [dcl.fct] forbids type definitions in return types.
1976 Unfortunately, it's not easy to know whether or not we are
1977 processing a return type until after the fact. */
1978 while (declarator
1979 && (declarator->kind == cdk_pointer
1980 || declarator->kind == cdk_reference
1981 || declarator->kind == cdk_ptrmem))
1982 declarator = declarator->declarator;
1983 if (declarator
1984 && declarator->kind == cdk_function)
1986 error ("new types may not be defined in a return type");
1987 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1988 type);
1992 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1993 "<" in any valid C++ program. If the next token is indeed "<",
1994 issue a message warning the user about what appears to be an
1995 invalid attempt to form a template-id. */
1997 static void
1998 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1999 tree type)
2001 cp_token_position start = 0;
2003 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2005 if (TYPE_P (type))
2006 error ("%qT is not a template", type);
2007 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2008 error ("%qE is not a template", type);
2009 else
2010 error ("invalid template-id");
2011 /* Remember the location of the invalid "<". */
2012 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2013 start = cp_lexer_token_position (parser->lexer, true);
2014 /* Consume the "<". */
2015 cp_lexer_consume_token (parser->lexer);
2016 /* Parse the template arguments. */
2017 cp_parser_enclosed_template_argument_list (parser);
2018 /* Permanently remove the invalid template arguments so that
2019 this error message is not issued again. */
2020 if (start)
2021 cp_lexer_purge_tokens_after (parser->lexer, start);
2025 /* If parsing an integral constant-expression, issue an error message
2026 about the fact that THING appeared and return true. Otherwise,
2027 return false. In either case, set
2028 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2030 static bool
2031 cp_parser_non_integral_constant_expression (cp_parser *parser,
2032 const char *thing)
2034 parser->non_integral_constant_expression_p = true;
2035 if (parser->integral_constant_expression_p)
2037 if (!parser->allow_non_integral_constant_expression_p)
2039 error ("%s cannot appear in a constant-expression", thing);
2040 return true;
2043 return false;
2046 /* Emit a diagnostic for an invalid type name. SCOPE is the
2047 qualifying scope (or NULL, if none) for ID. This function commits
2048 to the current active tentative parse, if any. (Otherwise, the
2049 problematic construct might be encountered again later, resulting
2050 in duplicate error messages.) */
2052 static void
2053 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2055 tree decl, old_scope;
2056 /* Try to lookup the identifier. */
2057 old_scope = parser->scope;
2058 parser->scope = scope;
2059 decl = cp_parser_lookup_name_simple (parser, id);
2060 parser->scope = old_scope;
2061 /* If the lookup found a template-name, it means that the user forgot
2062 to specify an argument list. Emit a useful error message. */
2063 if (TREE_CODE (decl) == TEMPLATE_DECL)
2064 error ("invalid use of template-name %qE without an argument list",
2065 decl);
2066 else if (!parser->scope)
2068 /* Issue an error message. */
2069 error ("%qE does not name a type", id);
2070 /* If we're in a template class, it's possible that the user was
2071 referring to a type from a base class. For example:
2073 template <typename T> struct A { typedef T X; };
2074 template <typename T> struct B : public A<T> { X x; };
2076 The user should have said "typename A<T>::X". */
2077 if (processing_template_decl && current_class_type
2078 && TYPE_BINFO (current_class_type))
2080 tree b;
2082 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2084 b = TREE_CHAIN (b))
2086 tree base_type = BINFO_TYPE (b);
2087 if (CLASS_TYPE_P (base_type)
2088 && dependent_type_p (base_type))
2090 tree field;
2091 /* Go from a particular instantiation of the
2092 template (which will have an empty TYPE_FIELDs),
2093 to the main version. */
2094 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2095 for (field = TYPE_FIELDS (base_type);
2096 field;
2097 field = TREE_CHAIN (field))
2098 if (TREE_CODE (field) == TYPE_DECL
2099 && DECL_NAME (field) == id)
2101 inform ("(perhaps %<typename %T::%E%> was intended)",
2102 BINFO_TYPE (b), id);
2103 break;
2105 if (field)
2106 break;
2111 /* Here we diagnose qualified-ids where the scope is actually correct,
2112 but the identifier does not resolve to a valid type name. */
2113 else if (parser->scope != error_mark_node)
2115 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2116 error ("%qE in namespace %qE does not name a type",
2117 id, parser->scope);
2118 else if (TYPE_P (parser->scope))
2119 error ("%qE in class %qT does not name a type", id, parser->scope);
2120 else
2121 gcc_unreachable ();
2123 cp_parser_commit_to_tentative_parse (parser);
2126 /* Check for a common situation where a type-name should be present,
2127 but is not, and issue a sensible error message. Returns true if an
2128 invalid type-name was detected.
2130 The situation handled by this function are variable declarations of the
2131 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2132 Usually, `ID' should name a type, but if we got here it means that it
2133 does not. We try to emit the best possible error message depending on
2134 how exactly the id-expression looks like.
2137 static bool
2138 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2140 tree id;
2142 cp_parser_parse_tentatively (parser);
2143 id = cp_parser_id_expression (parser,
2144 /*template_keyword_p=*/false,
2145 /*check_dependency_p=*/true,
2146 /*template_p=*/NULL,
2147 /*declarator_p=*/true);
2148 /* After the id-expression, there should be a plain identifier,
2149 otherwise this is not a simple variable declaration. Also, if
2150 the scope is dependent, we cannot do much. */
2151 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2152 || (parser->scope && TYPE_P (parser->scope)
2153 && dependent_type_p (parser->scope)))
2155 cp_parser_abort_tentative_parse (parser);
2156 return false;
2158 if (!cp_parser_parse_definitely (parser)
2159 || TREE_CODE (id) != IDENTIFIER_NODE)
2160 return false;
2162 /* Emit a diagnostic for the invalid type. */
2163 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2164 /* Skip to the end of the declaration; there's no point in
2165 trying to process it. */
2166 cp_parser_skip_to_end_of_block_or_statement (parser);
2167 return true;
2170 /* Consume tokens up to, and including, the next non-nested closing `)'.
2171 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2172 are doing error recovery. Returns -1 if OR_COMMA is true and we
2173 found an unnested comma. */
2175 static int
2176 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2177 bool recovering,
2178 bool or_comma,
2179 bool consume_paren)
2181 unsigned paren_depth = 0;
2182 unsigned brace_depth = 0;
2183 int result;
2185 if (recovering && !or_comma
2186 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2187 return 0;
2189 while (true)
2191 cp_token *token;
2193 /* If we've run out of tokens, then there is no closing `)'. */
2194 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2196 result = 0;
2197 break;
2200 token = cp_lexer_peek_token (parser->lexer);
2202 /* This matches the processing in skip_to_end_of_statement. */
2203 if (token->type == CPP_SEMICOLON && !brace_depth)
2205 result = 0;
2206 break;
2208 if (token->type == CPP_OPEN_BRACE)
2209 ++brace_depth;
2210 if (token->type == CPP_CLOSE_BRACE)
2212 if (!brace_depth--)
2214 result = 0;
2215 break;
2218 if (recovering && or_comma && token->type == CPP_COMMA
2219 && !brace_depth && !paren_depth)
2221 result = -1;
2222 break;
2225 if (!brace_depth)
2227 /* If it is an `(', we have entered another level of nesting. */
2228 if (token->type == CPP_OPEN_PAREN)
2229 ++paren_depth;
2230 /* If it is a `)', then we might be done. */
2231 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2233 if (consume_paren)
2234 cp_lexer_consume_token (parser->lexer);
2236 result = 1;
2237 break;
2242 /* Consume the token. */
2243 cp_lexer_consume_token (parser->lexer);
2246 return result;
2249 /* Consume tokens until we reach the end of the current statement.
2250 Normally, that will be just before consuming a `;'. However, if a
2251 non-nested `}' comes first, then we stop before consuming that. */
2253 static void
2254 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2256 unsigned nesting_depth = 0;
2258 while (true)
2260 cp_token *token;
2262 /* Peek at the next token. */
2263 token = cp_lexer_peek_token (parser->lexer);
2264 /* If we've run out of tokens, stop. */
2265 if (token->type == CPP_EOF)
2266 break;
2267 /* If the next token is a `;', we have reached the end of the
2268 statement. */
2269 if (token->type == CPP_SEMICOLON && !nesting_depth)
2270 break;
2271 /* If the next token is a non-nested `}', then we have reached
2272 the end of the current block. */
2273 if (token->type == CPP_CLOSE_BRACE)
2275 /* If this is a non-nested `}', stop before consuming it.
2276 That way, when confronted with something like:
2278 { 3 + }
2280 we stop before consuming the closing `}', even though we
2281 have not yet reached a `;'. */
2282 if (nesting_depth == 0)
2283 break;
2284 /* If it is the closing `}' for a block that we have
2285 scanned, stop -- but only after consuming the token.
2286 That way given:
2288 void f g () { ... }
2289 typedef int I;
2291 we will stop after the body of the erroneously declared
2292 function, but before consuming the following `typedef'
2293 declaration. */
2294 if (--nesting_depth == 0)
2296 cp_lexer_consume_token (parser->lexer);
2297 break;
2300 /* If it the next token is a `{', then we are entering a new
2301 block. Consume the entire block. */
2302 else if (token->type == CPP_OPEN_BRACE)
2303 ++nesting_depth;
2304 /* Consume the token. */
2305 cp_lexer_consume_token (parser->lexer);
2309 /* This function is called at the end of a statement or declaration.
2310 If the next token is a semicolon, it is consumed; otherwise, error
2311 recovery is attempted. */
2313 static void
2314 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2316 /* Look for the trailing `;'. */
2317 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2319 /* If there is additional (erroneous) input, skip to the end of
2320 the statement. */
2321 cp_parser_skip_to_end_of_statement (parser);
2322 /* If the next token is now a `;', consume it. */
2323 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2324 cp_lexer_consume_token (parser->lexer);
2328 /* Skip tokens until we have consumed an entire block, or until we
2329 have consumed a non-nested `;'. */
2331 static void
2332 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2334 int nesting_depth = 0;
2336 while (nesting_depth >= 0)
2338 cp_token *token = cp_lexer_peek_token (parser->lexer);
2340 if (token->type == CPP_EOF)
2341 break;
2343 switch (token->type)
2345 case CPP_EOF:
2346 /* If we've run out of tokens, stop. */
2347 nesting_depth = -1;
2348 continue;
2350 case CPP_SEMICOLON:
2351 /* Stop if this is an unnested ';'. */
2352 if (!nesting_depth)
2353 nesting_depth = -1;
2354 break;
2356 case CPP_CLOSE_BRACE:
2357 /* Stop if this is an unnested '}', or closes the outermost
2358 nesting level. */
2359 nesting_depth--;
2360 if (!nesting_depth)
2361 nesting_depth = -1;
2362 break;
2364 case CPP_OPEN_BRACE:
2365 /* Nest. */
2366 nesting_depth++;
2367 break;
2369 default:
2370 break;
2373 /* Consume the token. */
2374 cp_lexer_consume_token (parser->lexer);
2379 /* Skip tokens until a non-nested closing curly brace is the next
2380 token. */
2382 static void
2383 cp_parser_skip_to_closing_brace (cp_parser *parser)
2385 unsigned nesting_depth = 0;
2387 while (true)
2389 cp_token *token;
2391 /* Peek at the next token. */
2392 token = cp_lexer_peek_token (parser->lexer);
2393 /* If we've run out of tokens, stop. */
2394 if (token->type == CPP_EOF)
2395 break;
2396 /* If the next token is a non-nested `}', then we have reached
2397 the end of the current block. */
2398 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2399 break;
2400 /* If it the next token is a `{', then we are entering a new
2401 block. Consume the entire block. */
2402 else if (token->type == CPP_OPEN_BRACE)
2403 ++nesting_depth;
2404 /* Consume the token. */
2405 cp_lexer_consume_token (parser->lexer);
2409 /* This is a simple wrapper around make_typename_type. When the id is
2410 an unresolved identifier node, we can provide a superior diagnostic
2411 using cp_parser_diagnose_invalid_type_name. */
2413 static tree
2414 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2416 tree result;
2417 if (TREE_CODE (id) == IDENTIFIER_NODE)
2419 result = make_typename_type (scope, id, typename_type,
2420 /*complain=*/tf_none);
2421 if (result == error_mark_node)
2422 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2423 return result;
2425 return make_typename_type (scope, id, typename_type, tf_error);
2429 /* Create a new C++ parser. */
2431 static cp_parser *
2432 cp_parser_new (void)
2434 cp_parser *parser;
2435 cp_lexer *lexer;
2436 unsigned i;
2438 /* cp_lexer_new_main is called before calling ggc_alloc because
2439 cp_lexer_new_main might load a PCH file. */
2440 lexer = cp_lexer_new_main ();
2442 /* Initialize the binops_by_token so that we can get the tree
2443 directly from the token. */
2444 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2445 binops_by_token[binops[i].token_type] = binops[i];
2447 parser = GGC_CNEW (cp_parser);
2448 parser->lexer = lexer;
2449 parser->context = cp_parser_context_new (NULL);
2451 /* For now, we always accept GNU extensions. */
2452 parser->allow_gnu_extensions_p = 1;
2454 /* The `>' token is a greater-than operator, not the end of a
2455 template-id. */
2456 parser->greater_than_is_operator_p = true;
2458 parser->default_arg_ok_p = true;
2460 /* We are not parsing a constant-expression. */
2461 parser->integral_constant_expression_p = false;
2462 parser->allow_non_integral_constant_expression_p = false;
2463 parser->non_integral_constant_expression_p = false;
2465 /* Local variable names are not forbidden. */
2466 parser->local_variables_forbidden_p = false;
2468 /* We are not processing an `extern "C"' declaration. */
2469 parser->in_unbraced_linkage_specification_p = false;
2471 /* We are not processing a declarator. */
2472 parser->in_declarator_p = false;
2474 /* We are not processing a template-argument-list. */
2475 parser->in_template_argument_list_p = false;
2477 /* We are not in an iteration statement. */
2478 parser->in_iteration_statement_p = false;
2480 /* We are not in a switch statement. */
2481 parser->in_switch_statement_p = false;
2483 /* We are not parsing a type-id inside an expression. */
2484 parser->in_type_id_in_expr_p = false;
2486 /* Declarations aren't implicitly extern "C". */
2487 parser->implicit_extern_c = false;
2489 /* String literals should be translated to the execution character set. */
2490 parser->translate_strings_p = true;
2492 /* The unparsed function queue is empty. */
2493 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2495 /* There are no classes being defined. */
2496 parser->num_classes_being_defined = 0;
2498 /* No template parameters apply. */
2499 parser->num_template_parameter_lists = 0;
2501 return parser;
2504 /* Create a cp_lexer structure which will emit the tokens in CACHE
2505 and push it onto the parser's lexer stack. This is used for delayed
2506 parsing of in-class method bodies and default arguments, and should
2507 not be confused with tentative parsing. */
2508 static void
2509 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2511 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2512 lexer->next = parser->lexer;
2513 parser->lexer = lexer;
2515 /* Move the current source position to that of the first token in the
2516 new lexer. */
2517 cp_lexer_set_source_position_from_token (lexer->next_token);
2520 /* Pop the top lexer off the parser stack. This is never used for the
2521 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2522 static void
2523 cp_parser_pop_lexer (cp_parser *parser)
2525 cp_lexer *lexer = parser->lexer;
2526 parser->lexer = lexer->next;
2527 cp_lexer_destroy (lexer);
2529 /* Put the current source position back where it was before this
2530 lexer was pushed. */
2531 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2534 /* Lexical conventions [gram.lex] */
2536 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2537 identifier. */
2539 static tree
2540 cp_parser_identifier (cp_parser* parser)
2542 cp_token *token;
2544 /* Look for the identifier. */
2545 token = cp_parser_require (parser, CPP_NAME, "identifier");
2546 /* Return the value. */
2547 return token ? token->value : error_mark_node;
2550 /* Parse a sequence of adjacent string constants. Returns a
2551 TREE_STRING representing the combined, nul-terminated string
2552 constant. If TRANSLATE is true, translate the string to the
2553 execution character set. If WIDE_OK is true, a wide string is
2554 invalid here.
2556 C++98 [lex.string] says that if a narrow string literal token is
2557 adjacent to a wide string literal token, the behavior is undefined.
2558 However, C99 6.4.5p4 says that this results in a wide string literal.
2559 We follow C99 here, for consistency with the C front end.
2561 This code is largely lifted from lex_string() in c-lex.c.
2563 FUTURE: ObjC++ will need to handle @-strings here. */
2564 static tree
2565 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2567 tree value;
2568 bool wide = false;
2569 size_t count;
2570 struct obstack str_ob;
2571 cpp_string str, istr, *strs;
2572 cp_token *tok;
2574 tok = cp_lexer_peek_token (parser->lexer);
2575 if (!cp_parser_is_string_literal (tok))
2577 cp_parser_error (parser, "expected string-literal");
2578 return error_mark_node;
2581 /* Try to avoid the overhead of creating and destroying an obstack
2582 for the common case of just one string. */
2583 if (!cp_parser_is_string_literal
2584 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2586 cp_lexer_consume_token (parser->lexer);
2588 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2589 str.len = TREE_STRING_LENGTH (tok->value);
2590 count = 1;
2591 if (tok->type == CPP_WSTRING)
2592 wide = true;
2594 strs = &str;
2596 else
2598 gcc_obstack_init (&str_ob);
2599 count = 0;
2603 cp_lexer_consume_token (parser->lexer);
2604 count++;
2605 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2606 str.len = TREE_STRING_LENGTH (tok->value);
2607 if (tok->type == CPP_WSTRING)
2608 wide = true;
2610 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2612 tok = cp_lexer_peek_token (parser->lexer);
2614 while (cp_parser_is_string_literal (tok));
2616 strs = (cpp_string *) obstack_finish (&str_ob);
2619 if (wide && !wide_ok)
2621 cp_parser_error (parser, "a wide string is invalid in this context");
2622 wide = false;
2625 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2626 (parse_in, strs, count, &istr, wide))
2628 value = build_string (istr.len, (char *)istr.text);
2629 free ((void *)istr.text);
2631 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2632 value = fix_string_type (value);
2634 else
2635 /* cpp_interpret_string has issued an error. */
2636 value = error_mark_node;
2638 if (count > 1)
2639 obstack_free (&str_ob, 0);
2641 return value;
2645 /* Basic concepts [gram.basic] */
2647 /* Parse a translation-unit.
2649 translation-unit:
2650 declaration-seq [opt]
2652 Returns TRUE if all went well. */
2654 static bool
2655 cp_parser_translation_unit (cp_parser* parser)
2657 /* The address of the first non-permanent object on the declarator
2658 obstack. */
2659 static void *declarator_obstack_base;
2661 bool success;
2663 /* Create the declarator obstack, if necessary. */
2664 if (!cp_error_declarator)
2666 gcc_obstack_init (&declarator_obstack);
2667 /* Create the error declarator. */
2668 cp_error_declarator = make_declarator (cdk_error);
2669 /* Create the empty parameter list. */
2670 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2671 /* Remember where the base of the declarator obstack lies. */
2672 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2675 cp_parser_declaration_seq_opt (parser);
2677 /* If there are no tokens left then all went well. */
2678 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2680 /* Get rid of the token array; we don't need it any more. */
2681 cp_lexer_destroy (parser->lexer);
2682 parser->lexer = NULL;
2684 /* This file might have been a context that's implicitly extern
2685 "C". If so, pop the lang context. (Only relevant for PCH.) */
2686 if (parser->implicit_extern_c)
2688 pop_lang_context ();
2689 parser->implicit_extern_c = false;
2692 /* Finish up. */
2693 finish_translation_unit ();
2695 success = true;
2697 else
2699 cp_parser_error (parser, "expected declaration");
2700 success = false;
2703 /* Make sure the declarator obstack was fully cleaned up. */
2704 gcc_assert (obstack_next_free (&declarator_obstack)
2705 == declarator_obstack_base);
2707 /* All went well. */
2708 return success;
2711 /* Expressions [gram.expr] */
2713 /* Parse a primary-expression.
2715 primary-expression:
2716 literal
2717 this
2718 ( expression )
2719 id-expression
2721 GNU Extensions:
2723 primary-expression:
2724 ( compound-statement )
2725 __builtin_va_arg ( assignment-expression , type-id )
2727 Objective-C++ Extension:
2729 primary-expression:
2730 objc-expression
2732 literal:
2733 __null
2735 ADDRESS_P is true iff this expression was immediately preceded by
2736 "&" and therefore might denote a pointer-to-member. CAST_P is true
2737 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2738 true iff this expression is a tempalte argument.
2740 Returns a representation of the expression. Upon return, *IDK
2741 indicates what kind of id-expression (if any) was present. */
2743 static tree
2744 cp_parser_primary_expression (cp_parser *parser,
2745 bool address_p,
2746 bool cast_p,
2747 bool template_arg_p,
2748 cp_id_kind *idk)
2750 cp_token *token;
2752 /* Assume the primary expression is not an id-expression. */
2753 *idk = CP_ID_KIND_NONE;
2755 /* Peek at the next token. */
2756 token = cp_lexer_peek_token (parser->lexer);
2757 switch (token->type)
2759 /* literal:
2760 integer-literal
2761 character-literal
2762 floating-literal
2763 string-literal
2764 boolean-literal */
2765 case CPP_CHAR:
2766 case CPP_WCHAR:
2767 case CPP_NUMBER:
2768 token = cp_lexer_consume_token (parser->lexer);
2769 /* Floating-point literals are only allowed in an integral
2770 constant expression if they are cast to an integral or
2771 enumeration type. */
2772 if (TREE_CODE (token->value) == REAL_CST
2773 && parser->integral_constant_expression_p
2774 && pedantic)
2776 /* CAST_P will be set even in invalid code like "int(2.7 +
2777 ...)". Therefore, we have to check that the next token
2778 is sure to end the cast. */
2779 if (cast_p)
2781 cp_token *next_token;
2783 next_token = cp_lexer_peek_token (parser->lexer);
2784 if (/* The comma at the end of an
2785 enumerator-definition. */
2786 next_token->type != CPP_COMMA
2787 /* The curly brace at the end of an enum-specifier. */
2788 && next_token->type != CPP_CLOSE_BRACE
2789 /* The end of a statement. */
2790 && next_token->type != CPP_SEMICOLON
2791 /* The end of the cast-expression. */
2792 && next_token->type != CPP_CLOSE_PAREN
2793 /* The end of an array bound. */
2794 && next_token->type != CPP_CLOSE_SQUARE
2795 /* The closing ">" in a template-argument-list. */
2796 && (next_token->type != CPP_GREATER
2797 || parser->greater_than_is_operator_p))
2798 cast_p = false;
2801 /* If we are within a cast, then the constraint that the
2802 cast is to an integral or enumeration type will be
2803 checked at that point. If we are not within a cast, then
2804 this code is invalid. */
2805 if (!cast_p)
2806 cp_parser_non_integral_constant_expression
2807 (parser, "floating-point literal");
2809 return token->value;
2811 case CPP_STRING:
2812 case CPP_WSTRING:
2813 /* ??? Should wide strings be allowed when parser->translate_strings_p
2814 is false (i.e. in attributes)? If not, we can kill the third
2815 argument to cp_parser_string_literal. */
2816 return cp_parser_string_literal (parser,
2817 parser->translate_strings_p,
2818 true);
2820 case CPP_OPEN_PAREN:
2822 tree expr;
2823 bool saved_greater_than_is_operator_p;
2825 /* Consume the `('. */
2826 cp_lexer_consume_token (parser->lexer);
2827 /* Within a parenthesized expression, a `>' token is always
2828 the greater-than operator. */
2829 saved_greater_than_is_operator_p
2830 = parser->greater_than_is_operator_p;
2831 parser->greater_than_is_operator_p = true;
2832 /* If we see `( { ' then we are looking at the beginning of
2833 a GNU statement-expression. */
2834 if (cp_parser_allow_gnu_extensions_p (parser)
2835 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2837 /* Statement-expressions are not allowed by the standard. */
2838 if (pedantic)
2839 pedwarn ("ISO C++ forbids braced-groups within expressions");
2841 /* And they're not allowed outside of a function-body; you
2842 cannot, for example, write:
2844 int i = ({ int j = 3; j + 1; });
2846 at class or namespace scope. */
2847 if (!at_function_scope_p ())
2848 error ("statement-expressions are allowed only inside functions");
2849 /* Start the statement-expression. */
2850 expr = begin_stmt_expr ();
2851 /* Parse the compound-statement. */
2852 cp_parser_compound_statement (parser, expr, false);
2853 /* Finish up. */
2854 expr = finish_stmt_expr (expr, false);
2856 else
2858 /* Parse the parenthesized expression. */
2859 expr = cp_parser_expression (parser, cast_p);
2860 /* Let the front end know that this expression was
2861 enclosed in parentheses. This matters in case, for
2862 example, the expression is of the form `A::B', since
2863 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2864 not. */
2865 finish_parenthesized_expr (expr);
2867 /* The `>' token might be the end of a template-id or
2868 template-parameter-list now. */
2869 parser->greater_than_is_operator_p
2870 = saved_greater_than_is_operator_p;
2871 /* Consume the `)'. */
2872 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2873 cp_parser_skip_to_end_of_statement (parser);
2875 return expr;
2878 case CPP_KEYWORD:
2879 switch (token->keyword)
2881 /* These two are the boolean literals. */
2882 case RID_TRUE:
2883 cp_lexer_consume_token (parser->lexer);
2884 return boolean_true_node;
2885 case RID_FALSE:
2886 cp_lexer_consume_token (parser->lexer);
2887 return boolean_false_node;
2889 /* The `__null' literal. */
2890 case RID_NULL:
2891 cp_lexer_consume_token (parser->lexer);
2892 return null_node;
2894 /* Recognize the `this' keyword. */
2895 case RID_THIS:
2896 cp_lexer_consume_token (parser->lexer);
2897 if (parser->local_variables_forbidden_p)
2899 error ("%<this%> may not be used in this context");
2900 return error_mark_node;
2902 /* Pointers cannot appear in constant-expressions. */
2903 if (cp_parser_non_integral_constant_expression (parser,
2904 "`this'"))
2905 return error_mark_node;
2906 return finish_this_expr ();
2908 /* The `operator' keyword can be the beginning of an
2909 id-expression. */
2910 case RID_OPERATOR:
2911 goto id_expression;
2913 case RID_FUNCTION_NAME:
2914 case RID_PRETTY_FUNCTION_NAME:
2915 case RID_C99_FUNCTION_NAME:
2916 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2917 __func__ are the names of variables -- but they are
2918 treated specially. Therefore, they are handled here,
2919 rather than relying on the generic id-expression logic
2920 below. Grammatically, these names are id-expressions.
2922 Consume the token. */
2923 token = cp_lexer_consume_token (parser->lexer);
2924 /* Look up the name. */
2925 return finish_fname (token->value);
2927 case RID_VA_ARG:
2929 tree expression;
2930 tree type;
2932 /* The `__builtin_va_arg' construct is used to handle
2933 `va_arg'. Consume the `__builtin_va_arg' token. */
2934 cp_lexer_consume_token (parser->lexer);
2935 /* Look for the opening `('. */
2936 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2937 /* Now, parse the assignment-expression. */
2938 expression = cp_parser_assignment_expression (parser,
2939 /*cast_p=*/false);
2940 /* Look for the `,'. */
2941 cp_parser_require (parser, CPP_COMMA, "`,'");
2942 /* Parse the type-id. */
2943 type = cp_parser_type_id (parser);
2944 /* Look for the closing `)'. */
2945 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2946 /* Using `va_arg' in a constant-expression is not
2947 allowed. */
2948 if (cp_parser_non_integral_constant_expression (parser,
2949 "`va_arg'"))
2950 return error_mark_node;
2951 return build_x_va_arg (expression, type);
2954 case RID_OFFSETOF:
2955 return cp_parser_builtin_offsetof (parser);
2957 /* Objective-C++ expressions. */
2958 case RID_AT_ENCODE:
2959 case RID_AT_PROTOCOL:
2960 case RID_AT_SELECTOR:
2961 return cp_parser_objc_expression (parser);
2963 default:
2964 cp_parser_error (parser, "expected primary-expression");
2965 return error_mark_node;
2968 /* An id-expression can start with either an identifier, a
2969 `::' as the beginning of a qualified-id, or the "operator"
2970 keyword. */
2971 case CPP_NAME:
2972 case CPP_SCOPE:
2973 case CPP_TEMPLATE_ID:
2974 case CPP_NESTED_NAME_SPECIFIER:
2976 tree id_expression;
2977 tree decl;
2978 const char *error_msg;
2979 bool template_p;
2980 bool done;
2982 id_expression:
2983 /* Parse the id-expression. */
2984 id_expression
2985 = cp_parser_id_expression (parser,
2986 /*template_keyword_p=*/false,
2987 /*check_dependency_p=*/true,
2988 &template_p,
2989 /*declarator_p=*/false);
2990 if (id_expression == error_mark_node)
2991 return error_mark_node;
2992 token = cp_lexer_peek_token (parser->lexer);
2993 done = (token->type != CPP_OPEN_SQUARE
2994 && token->type != CPP_OPEN_PAREN
2995 && token->type != CPP_DOT
2996 && token->type != CPP_DEREF
2997 && token->type != CPP_PLUS_PLUS
2998 && token->type != CPP_MINUS_MINUS);
2999 /* If we have a template-id, then no further lookup is
3000 required. If the template-id was for a template-class, we
3001 will sometimes have a TYPE_DECL at this point. */
3002 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3003 || TREE_CODE (id_expression) == TYPE_DECL)
3004 decl = id_expression;
3005 /* Look up the name. */
3006 else
3008 tree ambiguous_decls;
3010 decl = cp_parser_lookup_name (parser, id_expression,
3011 none_type,
3012 template_p,
3013 /*is_namespace=*/false,
3014 /*check_dependency=*/true,
3015 &ambiguous_decls);
3016 /* If the lookup was ambiguous, an error will already have
3017 been issued. */
3018 if (ambiguous_decls)
3019 return error_mark_node;
3021 /* In Objective-C++, an instance variable (ivar) may be preferred
3022 to whatever cp_parser_lookup_name() found. */
3023 decl = objc_lookup_ivar (decl, id_expression);
3025 /* If name lookup gives us a SCOPE_REF, then the
3026 qualifying scope was dependent. */
3027 if (TREE_CODE (decl) == SCOPE_REF)
3028 return decl;
3029 /* Check to see if DECL is a local variable in a context
3030 where that is forbidden. */
3031 if (parser->local_variables_forbidden_p
3032 && local_variable_p (decl))
3034 /* It might be that we only found DECL because we are
3035 trying to be generous with pre-ISO scoping rules.
3036 For example, consider:
3038 int i;
3039 void g() {
3040 for (int i = 0; i < 10; ++i) {}
3041 extern void f(int j = i);
3044 Here, name look up will originally find the out
3045 of scope `i'. We need to issue a warning message,
3046 but then use the global `i'. */
3047 decl = check_for_out_of_scope_variable (decl);
3048 if (local_variable_p (decl))
3050 error ("local variable %qD may not appear in this context",
3051 decl);
3052 return error_mark_node;
3057 decl = (finish_id_expression
3058 (id_expression, decl, parser->scope,
3059 idk,
3060 parser->integral_constant_expression_p,
3061 parser->allow_non_integral_constant_expression_p,
3062 &parser->non_integral_constant_expression_p,
3063 template_p, done, address_p,
3064 template_arg_p,
3065 &error_msg));
3066 if (error_msg)
3067 cp_parser_error (parser, error_msg);
3068 return decl;
3071 /* Anything else is an error. */
3072 default:
3073 /* ...unless we have an Objective-C++ message or string literal, that is. */
3074 if (c_dialect_objc ()
3075 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3076 return cp_parser_objc_expression (parser);
3078 cp_parser_error (parser, "expected primary-expression");
3079 return error_mark_node;
3083 /* Parse an id-expression.
3085 id-expression:
3086 unqualified-id
3087 qualified-id
3089 qualified-id:
3090 :: [opt] nested-name-specifier template [opt] unqualified-id
3091 :: identifier
3092 :: operator-function-id
3093 :: template-id
3095 Return a representation of the unqualified portion of the
3096 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3097 a `::' or nested-name-specifier.
3099 Often, if the id-expression was a qualified-id, the caller will
3100 want to make a SCOPE_REF to represent the qualified-id. This
3101 function does not do this in order to avoid wastefully creating
3102 SCOPE_REFs when they are not required.
3104 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3105 `template' keyword.
3107 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3108 uninstantiated templates.
3110 If *TEMPLATE_P is non-NULL, it is set to true iff the
3111 `template' keyword is used to explicitly indicate that the entity
3112 named is a template.
3114 If DECLARATOR_P is true, the id-expression is appearing as part of
3115 a declarator, rather than as part of an expression. */
3117 static tree
3118 cp_parser_id_expression (cp_parser *parser,
3119 bool template_keyword_p,
3120 bool check_dependency_p,
3121 bool *template_p,
3122 bool declarator_p)
3124 bool global_scope_p;
3125 bool nested_name_specifier_p;
3127 /* Assume the `template' keyword was not used. */
3128 if (template_p)
3129 *template_p = template_keyword_p;
3131 /* Look for the optional `::' operator. */
3132 global_scope_p
3133 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3134 != NULL_TREE);
3135 /* Look for the optional nested-name-specifier. */
3136 nested_name_specifier_p
3137 = (cp_parser_nested_name_specifier_opt (parser,
3138 /*typename_keyword_p=*/false,
3139 check_dependency_p,
3140 /*type_p=*/false,
3141 declarator_p)
3142 != NULL_TREE);
3143 /* If there is a nested-name-specifier, then we are looking at
3144 the first qualified-id production. */
3145 if (nested_name_specifier_p)
3147 tree saved_scope;
3148 tree saved_object_scope;
3149 tree saved_qualifying_scope;
3150 tree unqualified_id;
3151 bool is_template;
3153 /* See if the next token is the `template' keyword. */
3154 if (!template_p)
3155 template_p = &is_template;
3156 *template_p = cp_parser_optional_template_keyword (parser);
3157 /* Name lookup we do during the processing of the
3158 unqualified-id might obliterate SCOPE. */
3159 saved_scope = parser->scope;
3160 saved_object_scope = parser->object_scope;
3161 saved_qualifying_scope = parser->qualifying_scope;
3162 /* Process the final unqualified-id. */
3163 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3164 check_dependency_p,
3165 declarator_p);
3166 /* Restore the SAVED_SCOPE for our caller. */
3167 parser->scope = saved_scope;
3168 parser->object_scope = saved_object_scope;
3169 parser->qualifying_scope = saved_qualifying_scope;
3171 return unqualified_id;
3173 /* Otherwise, if we are in global scope, then we are looking at one
3174 of the other qualified-id productions. */
3175 else if (global_scope_p)
3177 cp_token *token;
3178 tree id;
3180 /* Peek at the next token. */
3181 token = cp_lexer_peek_token (parser->lexer);
3183 /* If it's an identifier, and the next token is not a "<", then
3184 we can avoid the template-id case. This is an optimization
3185 for this common case. */
3186 if (token->type == CPP_NAME
3187 && !cp_parser_nth_token_starts_template_argument_list_p
3188 (parser, 2))
3189 return cp_parser_identifier (parser);
3191 cp_parser_parse_tentatively (parser);
3192 /* Try a template-id. */
3193 id = cp_parser_template_id (parser,
3194 /*template_keyword_p=*/false,
3195 /*check_dependency_p=*/true,
3196 declarator_p);
3197 /* If that worked, we're done. */
3198 if (cp_parser_parse_definitely (parser))
3199 return id;
3201 /* Peek at the next token. (Changes in the token buffer may
3202 have invalidated the pointer obtained above.) */
3203 token = cp_lexer_peek_token (parser->lexer);
3205 switch (token->type)
3207 case CPP_NAME:
3208 return cp_parser_identifier (parser);
3210 case CPP_KEYWORD:
3211 if (token->keyword == RID_OPERATOR)
3212 return cp_parser_operator_function_id (parser);
3213 /* Fall through. */
3215 default:
3216 cp_parser_error (parser, "expected id-expression");
3217 return error_mark_node;
3220 else
3221 return cp_parser_unqualified_id (parser, template_keyword_p,
3222 /*check_dependency_p=*/true,
3223 declarator_p);
3226 /* Parse an unqualified-id.
3228 unqualified-id:
3229 identifier
3230 operator-function-id
3231 conversion-function-id
3232 ~ class-name
3233 template-id
3235 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3236 keyword, in a construct like `A::template ...'.
3238 Returns a representation of unqualified-id. For the `identifier'
3239 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3240 production a BIT_NOT_EXPR is returned; the operand of the
3241 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3242 other productions, see the documentation accompanying the
3243 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3244 names are looked up in uninstantiated templates. If DECLARATOR_P
3245 is true, the unqualified-id is appearing as part of a declarator,
3246 rather than as part of an expression. */
3248 static tree
3249 cp_parser_unqualified_id (cp_parser* parser,
3250 bool template_keyword_p,
3251 bool check_dependency_p,
3252 bool declarator_p)
3254 cp_token *token;
3256 /* Peek at the next token. */
3257 token = cp_lexer_peek_token (parser->lexer);
3259 switch (token->type)
3261 case CPP_NAME:
3263 tree id;
3265 /* We don't know yet whether or not this will be a
3266 template-id. */
3267 cp_parser_parse_tentatively (parser);
3268 /* Try a template-id. */
3269 id = cp_parser_template_id (parser, template_keyword_p,
3270 check_dependency_p,
3271 declarator_p);
3272 /* If it worked, we're done. */
3273 if (cp_parser_parse_definitely (parser))
3274 return id;
3275 /* Otherwise, it's an ordinary identifier. */
3276 return cp_parser_identifier (parser);
3279 case CPP_TEMPLATE_ID:
3280 return cp_parser_template_id (parser, template_keyword_p,
3281 check_dependency_p,
3282 declarator_p);
3284 case CPP_COMPL:
3286 tree type_decl;
3287 tree qualifying_scope;
3288 tree object_scope;
3289 tree scope;
3290 bool done;
3292 /* Consume the `~' token. */
3293 cp_lexer_consume_token (parser->lexer);
3294 /* Parse the class-name. The standard, as written, seems to
3295 say that:
3297 template <typename T> struct S { ~S (); };
3298 template <typename T> S<T>::~S() {}
3300 is invalid, since `~' must be followed by a class-name, but
3301 `S<T>' is dependent, and so not known to be a class.
3302 That's not right; we need to look in uninstantiated
3303 templates. A further complication arises from:
3305 template <typename T> void f(T t) {
3306 t.T::~T();
3309 Here, it is not possible to look up `T' in the scope of `T'
3310 itself. We must look in both the current scope, and the
3311 scope of the containing complete expression.
3313 Yet another issue is:
3315 struct S {
3316 int S;
3317 ~S();
3320 S::~S() {}
3322 The standard does not seem to say that the `S' in `~S'
3323 should refer to the type `S' and not the data member
3324 `S::S'. */
3326 /* DR 244 says that we look up the name after the "~" in the
3327 same scope as we looked up the qualifying name. That idea
3328 isn't fully worked out; it's more complicated than that. */
3329 scope = parser->scope;
3330 object_scope = parser->object_scope;
3331 qualifying_scope = parser->qualifying_scope;
3333 /* If the name is of the form "X::~X" it's OK. */
3334 if (scope && TYPE_P (scope)
3335 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3336 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3337 == CPP_OPEN_PAREN)
3338 && (cp_lexer_peek_token (parser->lexer)->value
3339 == TYPE_IDENTIFIER (scope)))
3341 cp_lexer_consume_token (parser->lexer);
3342 return build_nt (BIT_NOT_EXPR, scope);
3345 /* If there was an explicit qualification (S::~T), first look
3346 in the scope given by the qualification (i.e., S). */
3347 done = false;
3348 type_decl = NULL_TREE;
3349 if (scope)
3351 cp_parser_parse_tentatively (parser);
3352 type_decl = cp_parser_class_name (parser,
3353 /*typename_keyword_p=*/false,
3354 /*template_keyword_p=*/false,
3355 none_type,
3356 /*check_dependency=*/false,
3357 /*class_head_p=*/false,
3358 declarator_p);
3359 if (cp_parser_parse_definitely (parser))
3360 done = true;
3362 /* In "N::S::~S", look in "N" as well. */
3363 if (!done && scope && qualifying_scope)
3365 cp_parser_parse_tentatively (parser);
3366 parser->scope = qualifying_scope;
3367 parser->object_scope = NULL_TREE;
3368 parser->qualifying_scope = NULL_TREE;
3369 type_decl
3370 = cp_parser_class_name (parser,
3371 /*typename_keyword_p=*/false,
3372 /*template_keyword_p=*/false,
3373 none_type,
3374 /*check_dependency=*/false,
3375 /*class_head_p=*/false,
3376 declarator_p);
3377 if (cp_parser_parse_definitely (parser))
3378 done = true;
3380 /* In "p->S::~T", look in the scope given by "*p" as well. */
3381 else if (!done && object_scope)
3383 cp_parser_parse_tentatively (parser);
3384 parser->scope = object_scope;
3385 parser->object_scope = NULL_TREE;
3386 parser->qualifying_scope = NULL_TREE;
3387 type_decl
3388 = cp_parser_class_name (parser,
3389 /*typename_keyword_p=*/false,
3390 /*template_keyword_p=*/false,
3391 none_type,
3392 /*check_dependency=*/false,
3393 /*class_head_p=*/false,
3394 declarator_p);
3395 if (cp_parser_parse_definitely (parser))
3396 done = true;
3398 /* Look in the surrounding context. */
3399 if (!done)
3401 parser->scope = NULL_TREE;
3402 parser->object_scope = NULL_TREE;
3403 parser->qualifying_scope = NULL_TREE;
3404 type_decl
3405 = cp_parser_class_name (parser,
3406 /*typename_keyword_p=*/false,
3407 /*template_keyword_p=*/false,
3408 none_type,
3409 /*check_dependency=*/false,
3410 /*class_head_p=*/false,
3411 declarator_p);
3413 /* If an error occurred, assume that the name of the
3414 destructor is the same as the name of the qualifying
3415 class. That allows us to keep parsing after running
3416 into ill-formed destructor names. */
3417 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3418 return build_nt (BIT_NOT_EXPR, scope);
3419 else if (type_decl == error_mark_node)
3420 return error_mark_node;
3422 /* [class.dtor]
3424 A typedef-name that names a class shall not be used as the
3425 identifier in the declarator for a destructor declaration. */
3426 if (declarator_p
3427 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3428 && !DECL_SELF_REFERENCE_P (type_decl)
3429 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3430 error ("typedef-name %qD used as destructor declarator",
3431 type_decl);
3433 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3436 case CPP_KEYWORD:
3437 if (token->keyword == RID_OPERATOR)
3439 tree id;
3441 /* This could be a template-id, so we try that first. */
3442 cp_parser_parse_tentatively (parser);
3443 /* Try a template-id. */
3444 id = cp_parser_template_id (parser, template_keyword_p,
3445 /*check_dependency_p=*/true,
3446 declarator_p);
3447 /* If that worked, we're done. */
3448 if (cp_parser_parse_definitely (parser))
3449 return id;
3450 /* We still don't know whether we're looking at an
3451 operator-function-id or a conversion-function-id. */
3452 cp_parser_parse_tentatively (parser);
3453 /* Try an operator-function-id. */
3454 id = cp_parser_operator_function_id (parser);
3455 /* If that didn't work, try a conversion-function-id. */
3456 if (!cp_parser_parse_definitely (parser))
3457 id = cp_parser_conversion_function_id (parser);
3459 return id;
3461 /* Fall through. */
3463 default:
3464 cp_parser_error (parser, "expected unqualified-id");
3465 return error_mark_node;
3469 /* Parse an (optional) nested-name-specifier.
3471 nested-name-specifier:
3472 class-or-namespace-name :: nested-name-specifier [opt]
3473 class-or-namespace-name :: template nested-name-specifier [opt]
3475 PARSER->SCOPE should be set appropriately before this function is
3476 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3477 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3478 in name lookups.
3480 Sets PARSER->SCOPE to the class (TYPE) or namespace
3481 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3482 it unchanged if there is no nested-name-specifier. Returns the new
3483 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3485 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3486 part of a declaration and/or decl-specifier. */
3488 static tree
3489 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3490 bool typename_keyword_p,
3491 bool check_dependency_p,
3492 bool type_p,
3493 bool is_declaration)
3495 bool success = false;
3496 tree access_check = NULL_TREE;
3497 cp_token_position start = 0;
3498 cp_token *token;
3500 /* If the next token corresponds to a nested name specifier, there
3501 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3502 false, it may have been true before, in which case something
3503 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3504 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3505 CHECK_DEPENDENCY_P is false, we have to fall through into the
3506 main loop. */
3507 if (check_dependency_p
3508 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3510 cp_parser_pre_parsed_nested_name_specifier (parser);
3511 return parser->scope;
3514 /* Remember where the nested-name-specifier starts. */
3515 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3516 start = cp_lexer_token_position (parser->lexer, false);
3518 push_deferring_access_checks (dk_deferred);
3520 while (true)
3522 tree new_scope;
3523 tree old_scope;
3524 tree saved_qualifying_scope;
3525 bool template_keyword_p;
3527 /* Spot cases that cannot be the beginning of a
3528 nested-name-specifier. */
3529 token = cp_lexer_peek_token (parser->lexer);
3531 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3532 the already parsed nested-name-specifier. */
3533 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3535 /* Grab the nested-name-specifier and continue the loop. */
3536 cp_parser_pre_parsed_nested_name_specifier (parser);
3537 success = true;
3538 continue;
3541 /* Spot cases that cannot be the beginning of a
3542 nested-name-specifier. On the second and subsequent times
3543 through the loop, we look for the `template' keyword. */
3544 if (success && token->keyword == RID_TEMPLATE)
3546 /* A template-id can start a nested-name-specifier. */
3547 else if (token->type == CPP_TEMPLATE_ID)
3549 else
3551 /* If the next token is not an identifier, then it is
3552 definitely not a class-or-namespace-name. */
3553 if (token->type != CPP_NAME)
3554 break;
3555 /* If the following token is neither a `<' (to begin a
3556 template-id), nor a `::', then we are not looking at a
3557 nested-name-specifier. */
3558 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3559 if (token->type != CPP_SCOPE
3560 && !cp_parser_nth_token_starts_template_argument_list_p
3561 (parser, 2))
3562 break;
3565 /* The nested-name-specifier is optional, so we parse
3566 tentatively. */
3567 cp_parser_parse_tentatively (parser);
3569 /* Look for the optional `template' keyword, if this isn't the
3570 first time through the loop. */
3571 if (success)
3572 template_keyword_p = cp_parser_optional_template_keyword (parser);
3573 else
3574 template_keyword_p = false;
3576 /* Save the old scope since the name lookup we are about to do
3577 might destroy it. */
3578 old_scope = parser->scope;
3579 saved_qualifying_scope = parser->qualifying_scope;
3580 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3581 look up names in "X<T>::I" in order to determine that "Y" is
3582 a template. So, if we have a typename at this point, we make
3583 an effort to look through it. */
3584 if (is_declaration
3585 && !typename_keyword_p
3586 && parser->scope
3587 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3588 parser->scope = resolve_typename_type (parser->scope,
3589 /*only_current_p=*/false);
3590 /* Parse the qualifying entity. */
3591 new_scope
3592 = cp_parser_class_or_namespace_name (parser,
3593 typename_keyword_p,
3594 template_keyword_p,
3595 check_dependency_p,
3596 type_p,
3597 is_declaration);
3598 /* Look for the `::' token. */
3599 cp_parser_require (parser, CPP_SCOPE, "`::'");
3601 /* If we found what we wanted, we keep going; otherwise, we're
3602 done. */
3603 if (!cp_parser_parse_definitely (parser))
3605 bool error_p = false;
3607 /* Restore the OLD_SCOPE since it was valid before the
3608 failed attempt at finding the last
3609 class-or-namespace-name. */
3610 parser->scope = old_scope;
3611 parser->qualifying_scope = saved_qualifying_scope;
3612 /* If the next token is an identifier, and the one after
3613 that is a `::', then any valid interpretation would have
3614 found a class-or-namespace-name. */
3615 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3616 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3617 == CPP_SCOPE)
3618 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3619 != CPP_COMPL))
3621 token = cp_lexer_consume_token (parser->lexer);
3622 if (!error_p)
3624 if (!token->ambiguous_p)
3626 tree decl;
3627 tree ambiguous_decls;
3629 decl = cp_parser_lookup_name (parser, token->value,
3630 none_type,
3631 /*is_template=*/false,
3632 /*is_namespace=*/false,
3633 /*check_dependency=*/true,
3634 &ambiguous_decls);
3635 if (TREE_CODE (decl) == TEMPLATE_DECL)
3636 error ("%qD used without template parameters", decl);
3637 else if (ambiguous_decls)
3639 error ("reference to %qD is ambiguous",
3640 token->value);
3641 print_candidates (ambiguous_decls);
3642 decl = error_mark_node;
3644 else
3645 cp_parser_name_lookup_error
3646 (parser, token->value, decl,
3647 "is not a class or namespace");
3649 parser->scope = error_mark_node;
3650 error_p = true;
3651 /* Treat this as a successful nested-name-specifier
3652 due to:
3654 [basic.lookup.qual]
3656 If the name found is not a class-name (clause
3657 _class_) or namespace-name (_namespace.def_), the
3658 program is ill-formed. */
3659 success = true;
3661 cp_lexer_consume_token (parser->lexer);
3663 break;
3665 /* We've found one valid nested-name-specifier. */
3666 success = true;
3667 /* Name lookup always gives us a DECL. */
3668 if (TREE_CODE (new_scope) == TYPE_DECL)
3669 new_scope = TREE_TYPE (new_scope);
3670 /* Uses of "template" must be followed by actual templates. */
3671 if (template_keyword_p
3672 && !(CLASS_TYPE_P (new_scope)
3673 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3674 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3675 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3676 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3677 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3678 == TEMPLATE_ID_EXPR)))
3679 pedwarn (TYPE_P (new_scope)
3680 ? "%qT is not a template"
3681 : "%qD is not a template",
3682 new_scope);
3683 /* If it is a class scope, try to complete it; we are about to
3684 be looking up names inside the class. */
3685 if (TYPE_P (new_scope)
3686 /* Since checking types for dependency can be expensive,
3687 avoid doing it if the type is already complete. */
3688 && !COMPLETE_TYPE_P (new_scope)
3689 /* Do not try to complete dependent types. */
3690 && !dependent_type_p (new_scope))
3691 new_scope = complete_type (new_scope);
3692 /* Make sure we look in the right scope the next time through
3693 the loop. */
3694 parser->scope = new_scope;
3697 /* Retrieve any deferred checks. Do not pop this access checks yet
3698 so the memory will not be reclaimed during token replacing below. */
3699 access_check = get_deferred_access_checks ();
3701 /* If parsing tentatively, replace the sequence of tokens that makes
3702 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3703 token. That way, should we re-parse the token stream, we will
3704 not have to repeat the effort required to do the parse, nor will
3705 we issue duplicate error messages. */
3706 if (success && start)
3708 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3710 /* Reset the contents of the START token. */
3711 token->type = CPP_NESTED_NAME_SPECIFIER;
3712 token->value = build_tree_list (access_check, parser->scope);
3713 TREE_TYPE (token->value) = parser->qualifying_scope;
3714 token->keyword = RID_MAX;
3716 /* Purge all subsequent tokens. */
3717 cp_lexer_purge_tokens_after (parser->lexer, start);
3720 pop_deferring_access_checks ();
3721 return success ? parser->scope : NULL_TREE;
3724 /* Parse a nested-name-specifier. See
3725 cp_parser_nested_name_specifier_opt for details. This function
3726 behaves identically, except that it will an issue an error if no
3727 nested-name-specifier is present. */
3729 static tree
3730 cp_parser_nested_name_specifier (cp_parser *parser,
3731 bool typename_keyword_p,
3732 bool check_dependency_p,
3733 bool type_p,
3734 bool is_declaration)
3736 tree scope;
3738 /* Look for the nested-name-specifier. */
3739 scope = cp_parser_nested_name_specifier_opt (parser,
3740 typename_keyword_p,
3741 check_dependency_p,
3742 type_p,
3743 is_declaration);
3744 /* If it was not present, issue an error message. */
3745 if (!scope)
3747 cp_parser_error (parser, "expected nested-name-specifier");
3748 parser->scope = NULL_TREE;
3751 return scope;
3754 /* Parse a class-or-namespace-name.
3756 class-or-namespace-name:
3757 class-name
3758 namespace-name
3760 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3761 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3762 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3763 TYPE_P is TRUE iff the next name should be taken as a class-name,
3764 even the same name is declared to be another entity in the same
3765 scope.
3767 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3768 specified by the class-or-namespace-name. If neither is found the
3769 ERROR_MARK_NODE is returned. */
3771 static tree
3772 cp_parser_class_or_namespace_name (cp_parser *parser,
3773 bool typename_keyword_p,
3774 bool template_keyword_p,
3775 bool check_dependency_p,
3776 bool type_p,
3777 bool is_declaration)
3779 tree saved_scope;
3780 tree saved_qualifying_scope;
3781 tree saved_object_scope;
3782 tree scope;
3783 bool only_class_p;
3785 /* Before we try to parse the class-name, we must save away the
3786 current PARSER->SCOPE since cp_parser_class_name will destroy
3787 it. */
3788 saved_scope = parser->scope;
3789 saved_qualifying_scope = parser->qualifying_scope;
3790 saved_object_scope = parser->object_scope;
3791 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3792 there is no need to look for a namespace-name. */
3793 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3794 if (!only_class_p)
3795 cp_parser_parse_tentatively (parser);
3796 scope = cp_parser_class_name (parser,
3797 typename_keyword_p,
3798 template_keyword_p,
3799 type_p ? class_type : none_type,
3800 check_dependency_p,
3801 /*class_head_p=*/false,
3802 is_declaration);
3803 /* If that didn't work, try for a namespace-name. */
3804 if (!only_class_p && !cp_parser_parse_definitely (parser))
3806 /* Restore the saved scope. */
3807 parser->scope = saved_scope;
3808 parser->qualifying_scope = saved_qualifying_scope;
3809 parser->object_scope = saved_object_scope;
3810 /* If we are not looking at an identifier followed by the scope
3811 resolution operator, then this is not part of a
3812 nested-name-specifier. (Note that this function is only used
3813 to parse the components of a nested-name-specifier.) */
3814 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3815 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3816 return error_mark_node;
3817 scope = cp_parser_namespace_name (parser);
3820 return scope;
3823 /* Parse a postfix-expression.
3825 postfix-expression:
3826 primary-expression
3827 postfix-expression [ expression ]
3828 postfix-expression ( expression-list [opt] )
3829 simple-type-specifier ( expression-list [opt] )
3830 typename :: [opt] nested-name-specifier identifier
3831 ( expression-list [opt] )
3832 typename :: [opt] nested-name-specifier template [opt] template-id
3833 ( expression-list [opt] )
3834 postfix-expression . template [opt] id-expression
3835 postfix-expression -> template [opt] id-expression
3836 postfix-expression . pseudo-destructor-name
3837 postfix-expression -> pseudo-destructor-name
3838 postfix-expression ++
3839 postfix-expression --
3840 dynamic_cast < type-id > ( expression )
3841 static_cast < type-id > ( expression )
3842 reinterpret_cast < type-id > ( expression )
3843 const_cast < type-id > ( expression )
3844 typeid ( expression )
3845 typeid ( type-id )
3847 GNU Extension:
3849 postfix-expression:
3850 ( type-id ) { initializer-list , [opt] }
3852 This extension is a GNU version of the C99 compound-literal
3853 construct. (The C99 grammar uses `type-name' instead of `type-id',
3854 but they are essentially the same concept.)
3856 If ADDRESS_P is true, the postfix expression is the operand of the
3857 `&' operator. CAST_P is true if this expression is the target of a
3858 cast.
3860 Returns a representation of the expression. */
3862 static tree
3863 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3865 cp_token *token;
3866 enum rid keyword;
3867 cp_id_kind idk = CP_ID_KIND_NONE;
3868 tree postfix_expression = NULL_TREE;
3870 /* Peek at the next token. */
3871 token = cp_lexer_peek_token (parser->lexer);
3872 /* Some of the productions are determined by keywords. */
3873 keyword = token->keyword;
3874 switch (keyword)
3876 case RID_DYNCAST:
3877 case RID_STATCAST:
3878 case RID_REINTCAST:
3879 case RID_CONSTCAST:
3881 tree type;
3882 tree expression;
3883 const char *saved_message;
3885 /* All of these can be handled in the same way from the point
3886 of view of parsing. Begin by consuming the token
3887 identifying the cast. */
3888 cp_lexer_consume_token (parser->lexer);
3890 /* New types cannot be defined in the cast. */
3891 saved_message = parser->type_definition_forbidden_message;
3892 parser->type_definition_forbidden_message
3893 = "types may not be defined in casts";
3895 /* Look for the opening `<'. */
3896 cp_parser_require (parser, CPP_LESS, "`<'");
3897 /* Parse the type to which we are casting. */
3898 type = cp_parser_type_id (parser);
3899 /* Look for the closing `>'. */
3900 cp_parser_require (parser, CPP_GREATER, "`>'");
3901 /* Restore the old message. */
3902 parser->type_definition_forbidden_message = saved_message;
3904 /* And the expression which is being cast. */
3905 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3906 expression = cp_parser_expression (parser, /*cast_p=*/true);
3907 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3909 /* Only type conversions to integral or enumeration types
3910 can be used in constant-expressions. */
3911 if (parser->integral_constant_expression_p
3912 && !dependent_type_p (type)
3913 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3914 && (cp_parser_non_integral_constant_expression
3915 (parser,
3916 "a cast to a type other than an integral or "
3917 "enumeration type")))
3918 return error_mark_node;
3920 switch (keyword)
3922 case RID_DYNCAST:
3923 postfix_expression
3924 = build_dynamic_cast (type, expression);
3925 break;
3926 case RID_STATCAST:
3927 postfix_expression
3928 = build_static_cast (type, expression);
3929 break;
3930 case RID_REINTCAST:
3931 postfix_expression
3932 = build_reinterpret_cast (type, expression);
3933 break;
3934 case RID_CONSTCAST:
3935 postfix_expression
3936 = build_const_cast (type, expression);
3937 break;
3938 default:
3939 gcc_unreachable ();
3942 break;
3944 case RID_TYPEID:
3946 tree type;
3947 const char *saved_message;
3948 bool saved_in_type_id_in_expr_p;
3950 /* Consume the `typeid' token. */
3951 cp_lexer_consume_token (parser->lexer);
3952 /* Look for the `(' token. */
3953 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3954 /* Types cannot be defined in a `typeid' expression. */
3955 saved_message = parser->type_definition_forbidden_message;
3956 parser->type_definition_forbidden_message
3957 = "types may not be defined in a `typeid\' expression";
3958 /* We can't be sure yet whether we're looking at a type-id or an
3959 expression. */
3960 cp_parser_parse_tentatively (parser);
3961 /* Try a type-id first. */
3962 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3963 parser->in_type_id_in_expr_p = true;
3964 type = cp_parser_type_id (parser);
3965 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3966 /* Look for the `)' token. Otherwise, we can't be sure that
3967 we're not looking at an expression: consider `typeid (int
3968 (3))', for example. */
3969 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3970 /* If all went well, simply lookup the type-id. */
3971 if (cp_parser_parse_definitely (parser))
3972 postfix_expression = get_typeid (type);
3973 /* Otherwise, fall back to the expression variant. */
3974 else
3976 tree expression;
3978 /* Look for an expression. */
3979 expression = cp_parser_expression (parser, /*cast_p=*/false);
3980 /* Compute its typeid. */
3981 postfix_expression = build_typeid (expression);
3982 /* Look for the `)' token. */
3983 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3985 /* `typeid' may not appear in an integral constant expression. */
3986 if (cp_parser_non_integral_constant_expression(parser,
3987 "`typeid' operator"))
3988 return error_mark_node;
3989 /* Restore the saved message. */
3990 parser->type_definition_forbidden_message = saved_message;
3992 break;
3994 case RID_TYPENAME:
3996 tree type;
3997 /* The syntax permitted here is the same permitted for an
3998 elaborated-type-specifier. */
3999 type = cp_parser_elaborated_type_specifier (parser,
4000 /*is_friend=*/false,
4001 /*is_declaration=*/false);
4002 postfix_expression = cp_parser_functional_cast (parser, type);
4004 break;
4006 default:
4008 tree type;
4010 /* If the next thing is a simple-type-specifier, we may be
4011 looking at a functional cast. We could also be looking at
4012 an id-expression. So, we try the functional cast, and if
4013 that doesn't work we fall back to the primary-expression. */
4014 cp_parser_parse_tentatively (parser);
4015 /* Look for the simple-type-specifier. */
4016 type = cp_parser_simple_type_specifier (parser,
4017 /*decl_specs=*/NULL,
4018 CP_PARSER_FLAGS_NONE);
4019 /* Parse the cast itself. */
4020 if (!cp_parser_error_occurred (parser))
4021 postfix_expression
4022 = cp_parser_functional_cast (parser, type);
4023 /* If that worked, we're done. */
4024 if (cp_parser_parse_definitely (parser))
4025 break;
4027 /* If the functional-cast didn't work out, try a
4028 compound-literal. */
4029 if (cp_parser_allow_gnu_extensions_p (parser)
4030 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4032 VEC(constructor_elt,gc) *initializer_list = NULL;
4033 bool saved_in_type_id_in_expr_p;
4035 cp_parser_parse_tentatively (parser);
4036 /* Consume the `('. */
4037 cp_lexer_consume_token (parser->lexer);
4038 /* Parse the type. */
4039 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4040 parser->in_type_id_in_expr_p = true;
4041 type = cp_parser_type_id (parser);
4042 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4043 /* Look for the `)'. */
4044 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4045 /* Look for the `{'. */
4046 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4047 /* If things aren't going well, there's no need to
4048 keep going. */
4049 if (!cp_parser_error_occurred (parser))
4051 bool non_constant_p;
4052 /* Parse the initializer-list. */
4053 initializer_list
4054 = cp_parser_initializer_list (parser, &non_constant_p);
4055 /* Allow a trailing `,'. */
4056 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4057 cp_lexer_consume_token (parser->lexer);
4058 /* Look for the final `}'. */
4059 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4061 /* If that worked, we're definitely looking at a
4062 compound-literal expression. */
4063 if (cp_parser_parse_definitely (parser))
4065 /* Warn the user that a compound literal is not
4066 allowed in standard C++. */
4067 if (pedantic)
4068 pedwarn ("ISO C++ forbids compound-literals");
4069 /* Form the representation of the compound-literal. */
4070 postfix_expression
4071 = finish_compound_literal (type, initializer_list);
4072 break;
4076 /* It must be a primary-expression. */
4077 postfix_expression
4078 = cp_parser_primary_expression (parser, address_p, cast_p,
4079 /*template_arg_p=*/false,
4080 &idk);
4082 break;
4085 /* Keep looping until the postfix-expression is complete. */
4086 while (true)
4088 if (idk == CP_ID_KIND_UNQUALIFIED
4089 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4090 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4091 /* It is not a Koenig lookup function call. */
4092 postfix_expression
4093 = unqualified_name_lookup_error (postfix_expression);
4095 /* Peek at the next token. */
4096 token = cp_lexer_peek_token (parser->lexer);
4098 switch (token->type)
4100 case CPP_OPEN_SQUARE:
4101 postfix_expression
4102 = cp_parser_postfix_open_square_expression (parser,
4103 postfix_expression,
4104 false);
4105 idk = CP_ID_KIND_NONE;
4106 break;
4108 case CPP_OPEN_PAREN:
4109 /* postfix-expression ( expression-list [opt] ) */
4111 bool koenig_p;
4112 bool is_builtin_constant_p;
4113 bool saved_integral_constant_expression_p = false;
4114 bool saved_non_integral_constant_expression_p = false;
4115 tree args;
4117 is_builtin_constant_p
4118 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4119 if (is_builtin_constant_p)
4121 /* The whole point of __builtin_constant_p is to allow
4122 non-constant expressions to appear as arguments. */
4123 saved_integral_constant_expression_p
4124 = parser->integral_constant_expression_p;
4125 saved_non_integral_constant_expression_p
4126 = parser->non_integral_constant_expression_p;
4127 parser->integral_constant_expression_p = false;
4129 args = (cp_parser_parenthesized_expression_list
4130 (parser, /*is_attribute_list=*/false,
4131 /*cast_p=*/false,
4132 /*non_constant_p=*/NULL));
4133 if (is_builtin_constant_p)
4135 parser->integral_constant_expression_p
4136 = saved_integral_constant_expression_p;
4137 parser->non_integral_constant_expression_p
4138 = saved_non_integral_constant_expression_p;
4141 if (args == error_mark_node)
4143 postfix_expression = error_mark_node;
4144 break;
4147 /* Function calls are not permitted in
4148 constant-expressions. */
4149 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4150 && cp_parser_non_integral_constant_expression (parser,
4151 "a function call"))
4153 postfix_expression = error_mark_node;
4154 break;
4157 koenig_p = false;
4158 if (idk == CP_ID_KIND_UNQUALIFIED)
4160 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4162 if (args)
4164 koenig_p = true;
4165 postfix_expression
4166 = perform_koenig_lookup (postfix_expression, args);
4168 else
4169 postfix_expression
4170 = unqualified_fn_lookup_error (postfix_expression);
4172 /* We do not perform argument-dependent lookup if
4173 normal lookup finds a non-function, in accordance
4174 with the expected resolution of DR 218. */
4175 else if (args && is_overloaded_fn (postfix_expression))
4177 tree fn = get_first_fn (postfix_expression);
4179 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4180 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4182 /* Only do argument dependent lookup if regular
4183 lookup does not find a set of member functions.
4184 [basic.lookup.koenig]/2a */
4185 if (!DECL_FUNCTION_MEMBER_P (fn))
4187 koenig_p = true;
4188 postfix_expression
4189 = perform_koenig_lookup (postfix_expression, args);
4194 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4196 tree instance = TREE_OPERAND (postfix_expression, 0);
4197 tree fn = TREE_OPERAND (postfix_expression, 1);
4199 if (processing_template_decl
4200 && (type_dependent_expression_p (instance)
4201 || (!BASELINK_P (fn)
4202 && TREE_CODE (fn) != FIELD_DECL)
4203 || type_dependent_expression_p (fn)
4204 || any_type_dependent_arguments_p (args)))
4206 postfix_expression
4207 = build_min_nt (CALL_EXPR, postfix_expression,
4208 args, NULL_TREE);
4209 break;
4212 if (BASELINK_P (fn))
4213 postfix_expression
4214 = (build_new_method_call
4215 (instance, fn, args, NULL_TREE,
4216 (idk == CP_ID_KIND_QUALIFIED
4217 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4218 else
4219 postfix_expression
4220 = finish_call_expr (postfix_expression, args,
4221 /*disallow_virtual=*/false,
4222 /*koenig_p=*/false);
4224 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4225 || TREE_CODE (postfix_expression) == MEMBER_REF
4226 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4227 postfix_expression = (build_offset_ref_call_from_tree
4228 (postfix_expression, args));
4229 else if (idk == CP_ID_KIND_QUALIFIED)
4230 /* A call to a static class member, or a namespace-scope
4231 function. */
4232 postfix_expression
4233 = finish_call_expr (postfix_expression, args,
4234 /*disallow_virtual=*/true,
4235 koenig_p);
4236 else
4237 /* All other function calls. */
4238 postfix_expression
4239 = finish_call_expr (postfix_expression, args,
4240 /*disallow_virtual=*/false,
4241 koenig_p);
4243 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4244 idk = CP_ID_KIND_NONE;
4246 break;
4248 case CPP_DOT:
4249 case CPP_DEREF:
4250 /* postfix-expression . template [opt] id-expression
4251 postfix-expression . pseudo-destructor-name
4252 postfix-expression -> template [opt] id-expression
4253 postfix-expression -> pseudo-destructor-name */
4255 /* Consume the `.' or `->' operator. */
4256 cp_lexer_consume_token (parser->lexer);
4258 postfix_expression
4259 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4260 postfix_expression,
4261 false, &idk);
4262 break;
4264 case CPP_PLUS_PLUS:
4265 /* postfix-expression ++ */
4266 /* Consume the `++' token. */
4267 cp_lexer_consume_token (parser->lexer);
4268 /* Generate a representation for the complete expression. */
4269 postfix_expression
4270 = finish_increment_expr (postfix_expression,
4271 POSTINCREMENT_EXPR);
4272 /* Increments may not appear in constant-expressions. */
4273 if (cp_parser_non_integral_constant_expression (parser,
4274 "an increment"))
4275 postfix_expression = error_mark_node;
4276 idk = CP_ID_KIND_NONE;
4277 break;
4279 case CPP_MINUS_MINUS:
4280 /* postfix-expression -- */
4281 /* Consume the `--' token. */
4282 cp_lexer_consume_token (parser->lexer);
4283 /* Generate a representation for the complete expression. */
4284 postfix_expression
4285 = finish_increment_expr (postfix_expression,
4286 POSTDECREMENT_EXPR);
4287 /* Decrements may not appear in constant-expressions. */
4288 if (cp_parser_non_integral_constant_expression (parser,
4289 "a decrement"))
4290 postfix_expression = error_mark_node;
4291 idk = CP_ID_KIND_NONE;
4292 break;
4294 default:
4295 return postfix_expression;
4299 /* We should never get here. */
4300 gcc_unreachable ();
4301 return error_mark_node;
4304 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4305 by cp_parser_builtin_offsetof. We're looking for
4307 postfix-expression [ expression ]
4309 FOR_OFFSETOF is set if we're being called in that context, which
4310 changes how we deal with integer constant expressions. */
4312 static tree
4313 cp_parser_postfix_open_square_expression (cp_parser *parser,
4314 tree postfix_expression,
4315 bool for_offsetof)
4317 tree index;
4319 /* Consume the `[' token. */
4320 cp_lexer_consume_token (parser->lexer);
4322 /* Parse the index expression. */
4323 /* ??? For offsetof, there is a question of what to allow here. If
4324 offsetof is not being used in an integral constant expression context,
4325 then we *could* get the right answer by computing the value at runtime.
4326 If we are in an integral constant expression context, then we might
4327 could accept any constant expression; hard to say without analysis.
4328 Rather than open the barn door too wide right away, allow only integer
4329 constant expressions here. */
4330 if (for_offsetof)
4331 index = cp_parser_constant_expression (parser, false, NULL);
4332 else
4333 index = cp_parser_expression (parser, /*cast_p=*/false);
4335 /* Look for the closing `]'. */
4336 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4338 /* Build the ARRAY_REF. */
4339 postfix_expression = grok_array_decl (postfix_expression, index);
4341 /* When not doing offsetof, array references are not permitted in
4342 constant-expressions. */
4343 if (!for_offsetof
4344 && (cp_parser_non_integral_constant_expression
4345 (parser, "an array reference")))
4346 postfix_expression = error_mark_node;
4348 return postfix_expression;
4351 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4352 by cp_parser_builtin_offsetof. We're looking for
4354 postfix-expression . template [opt] id-expression
4355 postfix-expression . pseudo-destructor-name
4356 postfix-expression -> template [opt] id-expression
4357 postfix-expression -> pseudo-destructor-name
4359 FOR_OFFSETOF is set if we're being called in that context. That sorta
4360 limits what of the above we'll actually accept, but nevermind.
4361 TOKEN_TYPE is the "." or "->" token, which will already have been
4362 removed from the stream. */
4364 static tree
4365 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4366 enum cpp_ttype token_type,
4367 tree postfix_expression,
4368 bool for_offsetof, cp_id_kind *idk)
4370 tree name;
4371 bool dependent_p;
4372 bool pseudo_destructor_p;
4373 tree scope = NULL_TREE;
4375 /* If this is a `->' operator, dereference the pointer. */
4376 if (token_type == CPP_DEREF)
4377 postfix_expression = build_x_arrow (postfix_expression);
4378 /* Check to see whether or not the expression is type-dependent. */
4379 dependent_p = type_dependent_expression_p (postfix_expression);
4380 /* The identifier following the `->' or `.' is not qualified. */
4381 parser->scope = NULL_TREE;
4382 parser->qualifying_scope = NULL_TREE;
4383 parser->object_scope = NULL_TREE;
4384 *idk = CP_ID_KIND_NONE;
4385 /* Enter the scope corresponding to the type of the object
4386 given by the POSTFIX_EXPRESSION. */
4387 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4389 scope = TREE_TYPE (postfix_expression);
4390 /* According to the standard, no expression should ever have
4391 reference type. Unfortunately, we do not currently match
4392 the standard in this respect in that our internal representation
4393 of an expression may have reference type even when the standard
4394 says it does not. Therefore, we have to manually obtain the
4395 underlying type here. */
4396 scope = non_reference (scope);
4397 /* The type of the POSTFIX_EXPRESSION must be complete. */
4398 if (scope == unknown_type_node)
4400 error ("%qE does not have class type", postfix_expression);
4401 scope = NULL_TREE;
4403 else
4404 scope = complete_type_or_else (scope, NULL_TREE);
4405 /* Let the name lookup machinery know that we are processing a
4406 class member access expression. */
4407 parser->context->object_type = scope;
4408 /* If something went wrong, we want to be able to discern that case,
4409 as opposed to the case where there was no SCOPE due to the type
4410 of expression being dependent. */
4411 if (!scope)
4412 scope = error_mark_node;
4413 /* If the SCOPE was erroneous, make the various semantic analysis
4414 functions exit quickly -- and without issuing additional error
4415 messages. */
4416 if (scope == error_mark_node)
4417 postfix_expression = error_mark_node;
4420 /* Assume this expression is not a pseudo-destructor access. */
4421 pseudo_destructor_p = false;
4423 /* If the SCOPE is a scalar type, then, if this is a valid program,
4424 we must be looking at a pseudo-destructor-name. */
4425 if (scope && SCALAR_TYPE_P (scope))
4427 tree s;
4428 tree type;
4430 cp_parser_parse_tentatively (parser);
4431 /* Parse the pseudo-destructor-name. */
4432 s = NULL_TREE;
4433 cp_parser_pseudo_destructor_name (parser, &s, &type);
4434 if (cp_parser_parse_definitely (parser))
4436 pseudo_destructor_p = true;
4437 postfix_expression
4438 = finish_pseudo_destructor_expr (postfix_expression,
4439 s, TREE_TYPE (type));
4443 if (!pseudo_destructor_p)
4445 /* If the SCOPE is not a scalar type, we are looking at an
4446 ordinary class member access expression, rather than a
4447 pseudo-destructor-name. */
4448 bool template_p;
4449 /* Parse the id-expression. */
4450 name = (cp_parser_id_expression
4451 (parser,
4452 cp_parser_optional_template_keyword (parser),
4453 /*check_dependency_p=*/true,
4454 &template_p,
4455 /*declarator_p=*/false));
4456 /* In general, build a SCOPE_REF if the member name is qualified.
4457 However, if the name was not dependent and has already been
4458 resolved; there is no need to build the SCOPE_REF. For example;
4460 struct X { void f(); };
4461 template <typename T> void f(T* t) { t->X::f(); }
4463 Even though "t" is dependent, "X::f" is not and has been resolved
4464 to a BASELINK; there is no need to include scope information. */
4466 /* But we do need to remember that there was an explicit scope for
4467 virtual function calls. */
4468 if (parser->scope)
4469 *idk = CP_ID_KIND_QUALIFIED;
4471 /* If the name is a template-id that names a type, we will get a
4472 TYPE_DECL here. That is invalid code. */
4473 if (TREE_CODE (name) == TYPE_DECL)
4475 error ("invalid use of %qD", name);
4476 postfix_expression = error_mark_node;
4478 else
4480 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4482 name = build_qualified_name (/*type=*/NULL_TREE,
4483 parser->scope,
4484 name,
4485 template_p);
4486 parser->scope = NULL_TREE;
4487 parser->qualifying_scope = NULL_TREE;
4488 parser->object_scope = NULL_TREE;
4490 if (scope && name && BASELINK_P (name))
4491 adjust_result_of_qualified_name_lookup
4492 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4493 postfix_expression
4494 = finish_class_member_access_expr (postfix_expression, name,
4495 template_p);
4499 /* We no longer need to look up names in the scope of the object on
4500 the left-hand side of the `.' or `->' operator. */
4501 parser->context->object_type = NULL_TREE;
4503 /* Outside of offsetof, these operators may not appear in
4504 constant-expressions. */
4505 if (!for_offsetof
4506 && (cp_parser_non_integral_constant_expression
4507 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4508 postfix_expression = error_mark_node;
4510 return postfix_expression;
4513 /* Parse a parenthesized expression-list.
4515 expression-list:
4516 assignment-expression
4517 expression-list, assignment-expression
4519 attribute-list:
4520 expression-list
4521 identifier
4522 identifier, expression-list
4524 CAST_P is true if this expression is the target of a cast.
4526 Returns a TREE_LIST. The TREE_VALUE of each node is a
4527 representation of an assignment-expression. Note that a TREE_LIST
4528 is returned even if there is only a single expression in the list.
4529 error_mark_node is returned if the ( and or ) are
4530 missing. NULL_TREE is returned on no expressions. The parentheses
4531 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4532 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4533 indicates whether or not all of the expressions in the list were
4534 constant. */
4536 static tree
4537 cp_parser_parenthesized_expression_list (cp_parser* parser,
4538 bool is_attribute_list,
4539 bool cast_p,
4540 bool *non_constant_p)
4542 tree expression_list = NULL_TREE;
4543 bool fold_expr_p = is_attribute_list;
4544 tree identifier = NULL_TREE;
4546 /* Assume all the expressions will be constant. */
4547 if (non_constant_p)
4548 *non_constant_p = false;
4550 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4551 return error_mark_node;
4553 /* Consume expressions until there are no more. */
4554 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4555 while (true)
4557 tree expr;
4559 /* At the beginning of attribute lists, check to see if the
4560 next token is an identifier. */
4561 if (is_attribute_list
4562 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4564 cp_token *token;
4566 /* Consume the identifier. */
4567 token = cp_lexer_consume_token (parser->lexer);
4568 /* Save the identifier. */
4569 identifier = token->value;
4571 else
4573 /* Parse the next assignment-expression. */
4574 if (non_constant_p)
4576 bool expr_non_constant_p;
4577 expr = (cp_parser_constant_expression
4578 (parser, /*allow_non_constant_p=*/true,
4579 &expr_non_constant_p));
4580 if (expr_non_constant_p)
4581 *non_constant_p = true;
4583 else
4584 expr = cp_parser_assignment_expression (parser, cast_p);
4586 if (fold_expr_p)
4587 expr = fold_non_dependent_expr (expr);
4589 /* Add it to the list. We add error_mark_node
4590 expressions to the list, so that we can still tell if
4591 the correct form for a parenthesized expression-list
4592 is found. That gives better errors. */
4593 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4595 if (expr == error_mark_node)
4596 goto skip_comma;
4599 /* After the first item, attribute lists look the same as
4600 expression lists. */
4601 is_attribute_list = false;
4603 get_comma:;
4604 /* If the next token isn't a `,', then we are done. */
4605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4606 break;
4608 /* Otherwise, consume the `,' and keep going. */
4609 cp_lexer_consume_token (parser->lexer);
4612 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4614 int ending;
4616 skip_comma:;
4617 /* We try and resync to an unnested comma, as that will give the
4618 user better diagnostics. */
4619 ending = cp_parser_skip_to_closing_parenthesis (parser,
4620 /*recovering=*/true,
4621 /*or_comma=*/true,
4622 /*consume_paren=*/true);
4623 if (ending < 0)
4624 goto get_comma;
4625 if (!ending)
4626 return error_mark_node;
4629 /* We built up the list in reverse order so we must reverse it now. */
4630 expression_list = nreverse (expression_list);
4631 if (identifier)
4632 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4634 return expression_list;
4637 /* Parse a pseudo-destructor-name.
4639 pseudo-destructor-name:
4640 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4641 :: [opt] nested-name-specifier template template-id :: ~ type-name
4642 :: [opt] nested-name-specifier [opt] ~ type-name
4644 If either of the first two productions is used, sets *SCOPE to the
4645 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4646 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4647 or ERROR_MARK_NODE if the parse fails. */
4649 static void
4650 cp_parser_pseudo_destructor_name (cp_parser* parser,
4651 tree* scope,
4652 tree* type)
4654 bool nested_name_specifier_p;
4656 /* Assume that things will not work out. */
4657 *type = error_mark_node;
4659 /* Look for the optional `::' operator. */
4660 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4661 /* Look for the optional nested-name-specifier. */
4662 nested_name_specifier_p
4663 = (cp_parser_nested_name_specifier_opt (parser,
4664 /*typename_keyword_p=*/false,
4665 /*check_dependency_p=*/true,
4666 /*type_p=*/false,
4667 /*is_declaration=*/true)
4668 != NULL_TREE);
4669 /* Now, if we saw a nested-name-specifier, we might be doing the
4670 second production. */
4671 if (nested_name_specifier_p
4672 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4674 /* Consume the `template' keyword. */
4675 cp_lexer_consume_token (parser->lexer);
4676 /* Parse the template-id. */
4677 cp_parser_template_id (parser,
4678 /*template_keyword_p=*/true,
4679 /*check_dependency_p=*/false,
4680 /*is_declaration=*/true);
4681 /* Look for the `::' token. */
4682 cp_parser_require (parser, CPP_SCOPE, "`::'");
4684 /* If the next token is not a `~', then there might be some
4685 additional qualification. */
4686 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4688 /* Look for the type-name. */
4689 *scope = TREE_TYPE (cp_parser_type_name (parser));
4691 if (*scope == error_mark_node)
4692 return;
4694 /* If we don't have ::~, then something has gone wrong. Since
4695 the only caller of this function is looking for something
4696 after `.' or `->' after a scalar type, most likely the
4697 program is trying to get a member of a non-aggregate
4698 type. */
4699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4700 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4702 cp_parser_error (parser, "request for member of non-aggregate type");
4703 return;
4706 /* Look for the `::' token. */
4707 cp_parser_require (parser, CPP_SCOPE, "`::'");
4709 else
4710 *scope = NULL_TREE;
4712 /* Look for the `~'. */
4713 cp_parser_require (parser, CPP_COMPL, "`~'");
4714 /* Look for the type-name again. We are not responsible for
4715 checking that it matches the first type-name. */
4716 *type = cp_parser_type_name (parser);
4719 /* Parse a unary-expression.
4721 unary-expression:
4722 postfix-expression
4723 ++ cast-expression
4724 -- cast-expression
4725 unary-operator cast-expression
4726 sizeof unary-expression
4727 sizeof ( type-id )
4728 new-expression
4729 delete-expression
4731 GNU Extensions:
4733 unary-expression:
4734 __extension__ cast-expression
4735 __alignof__ unary-expression
4736 __alignof__ ( type-id )
4737 __real__ cast-expression
4738 __imag__ cast-expression
4739 && identifier
4741 ADDRESS_P is true iff the unary-expression is appearing as the
4742 operand of the `&' operator. CAST_P is true if this expression is
4743 the target of a cast.
4745 Returns a representation of the expression. */
4747 static tree
4748 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4750 cp_token *token;
4751 enum tree_code unary_operator;
4753 /* Peek at the next token. */
4754 token = cp_lexer_peek_token (parser->lexer);
4755 /* Some keywords give away the kind of expression. */
4756 if (token->type == CPP_KEYWORD)
4758 enum rid keyword = token->keyword;
4760 switch (keyword)
4762 case RID_ALIGNOF:
4763 case RID_SIZEOF:
4765 tree operand;
4766 enum tree_code op;
4768 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4769 /* Consume the token. */
4770 cp_lexer_consume_token (parser->lexer);
4771 /* Parse the operand. */
4772 operand = cp_parser_sizeof_operand (parser, keyword);
4774 if (TYPE_P (operand))
4775 return cxx_sizeof_or_alignof_type (operand, op, true);
4776 else
4777 return cxx_sizeof_or_alignof_expr (operand, op);
4780 case RID_NEW:
4781 return cp_parser_new_expression (parser);
4783 case RID_DELETE:
4784 return cp_parser_delete_expression (parser);
4786 case RID_EXTENSION:
4788 /* The saved value of the PEDANTIC flag. */
4789 int saved_pedantic;
4790 tree expr;
4792 /* Save away the PEDANTIC flag. */
4793 cp_parser_extension_opt (parser, &saved_pedantic);
4794 /* Parse the cast-expression. */
4795 expr = cp_parser_simple_cast_expression (parser);
4796 /* Restore the PEDANTIC flag. */
4797 pedantic = saved_pedantic;
4799 return expr;
4802 case RID_REALPART:
4803 case RID_IMAGPART:
4805 tree expression;
4807 /* Consume the `__real__' or `__imag__' token. */
4808 cp_lexer_consume_token (parser->lexer);
4809 /* Parse the cast-expression. */
4810 expression = cp_parser_simple_cast_expression (parser);
4811 /* Create the complete representation. */
4812 return build_x_unary_op ((keyword == RID_REALPART
4813 ? REALPART_EXPR : IMAGPART_EXPR),
4814 expression);
4816 break;
4818 default:
4819 break;
4823 /* Look for the `:: new' and `:: delete', which also signal the
4824 beginning of a new-expression, or delete-expression,
4825 respectively. If the next token is `::', then it might be one of
4826 these. */
4827 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4829 enum rid keyword;
4831 /* See if the token after the `::' is one of the keywords in
4832 which we're interested. */
4833 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4834 /* If it's `new', we have a new-expression. */
4835 if (keyword == RID_NEW)
4836 return cp_parser_new_expression (parser);
4837 /* Similarly, for `delete'. */
4838 else if (keyword == RID_DELETE)
4839 return cp_parser_delete_expression (parser);
4842 /* Look for a unary operator. */
4843 unary_operator = cp_parser_unary_operator (token);
4844 /* The `++' and `--' operators can be handled similarly, even though
4845 they are not technically unary-operators in the grammar. */
4846 if (unary_operator == ERROR_MARK)
4848 if (token->type == CPP_PLUS_PLUS)
4849 unary_operator = PREINCREMENT_EXPR;
4850 else if (token->type == CPP_MINUS_MINUS)
4851 unary_operator = PREDECREMENT_EXPR;
4852 /* Handle the GNU address-of-label extension. */
4853 else if (cp_parser_allow_gnu_extensions_p (parser)
4854 && token->type == CPP_AND_AND)
4856 tree identifier;
4858 /* Consume the '&&' token. */
4859 cp_lexer_consume_token (parser->lexer);
4860 /* Look for the identifier. */
4861 identifier = cp_parser_identifier (parser);
4862 /* Create an expression representing the address. */
4863 return finish_label_address_expr (identifier);
4866 if (unary_operator != ERROR_MARK)
4868 tree cast_expression;
4869 tree expression = error_mark_node;
4870 const char *non_constant_p = NULL;
4872 /* Consume the operator token. */
4873 token = cp_lexer_consume_token (parser->lexer);
4874 /* Parse the cast-expression. */
4875 cast_expression
4876 = cp_parser_cast_expression (parser,
4877 unary_operator == ADDR_EXPR,
4878 /*cast_p=*/false);
4879 /* Now, build an appropriate representation. */
4880 switch (unary_operator)
4882 case INDIRECT_REF:
4883 non_constant_p = "`*'";
4884 expression = build_x_indirect_ref (cast_expression, "unary *");
4885 break;
4887 case ADDR_EXPR:
4888 non_constant_p = "`&'";
4889 /* Fall through. */
4890 case BIT_NOT_EXPR:
4891 expression = build_x_unary_op (unary_operator, cast_expression);
4892 break;
4894 case PREINCREMENT_EXPR:
4895 case PREDECREMENT_EXPR:
4896 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4897 ? "`++'" : "`--'");
4898 /* Fall through. */
4899 case UNARY_PLUS_EXPR:
4900 case NEGATE_EXPR:
4901 case TRUTH_NOT_EXPR:
4902 expression = finish_unary_op_expr (unary_operator, cast_expression);
4903 break;
4905 default:
4906 gcc_unreachable ();
4909 if (non_constant_p
4910 && cp_parser_non_integral_constant_expression (parser,
4911 non_constant_p))
4912 expression = error_mark_node;
4914 return expression;
4917 return cp_parser_postfix_expression (parser, address_p, cast_p);
4920 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4921 unary-operator, the corresponding tree code is returned. */
4923 static enum tree_code
4924 cp_parser_unary_operator (cp_token* token)
4926 switch (token->type)
4928 case CPP_MULT:
4929 return INDIRECT_REF;
4931 case CPP_AND:
4932 return ADDR_EXPR;
4934 case CPP_PLUS:
4935 return UNARY_PLUS_EXPR;
4937 case CPP_MINUS:
4938 return NEGATE_EXPR;
4940 case CPP_NOT:
4941 return TRUTH_NOT_EXPR;
4943 case CPP_COMPL:
4944 return BIT_NOT_EXPR;
4946 default:
4947 return ERROR_MARK;
4951 /* Parse a new-expression.
4953 new-expression:
4954 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4955 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4957 Returns a representation of the expression. */
4959 static tree
4960 cp_parser_new_expression (cp_parser* parser)
4962 bool global_scope_p;
4963 tree placement;
4964 tree type;
4965 tree initializer;
4966 tree nelts;
4968 /* Look for the optional `::' operator. */
4969 global_scope_p
4970 = (cp_parser_global_scope_opt (parser,
4971 /*current_scope_valid_p=*/false)
4972 != NULL_TREE);
4973 /* Look for the `new' operator. */
4974 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4975 /* There's no easy way to tell a new-placement from the
4976 `( type-id )' construct. */
4977 cp_parser_parse_tentatively (parser);
4978 /* Look for a new-placement. */
4979 placement = cp_parser_new_placement (parser);
4980 /* If that didn't work out, there's no new-placement. */
4981 if (!cp_parser_parse_definitely (parser))
4982 placement = NULL_TREE;
4984 /* If the next token is a `(', then we have a parenthesized
4985 type-id. */
4986 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4988 /* Consume the `('. */
4989 cp_lexer_consume_token (parser->lexer);
4990 /* Parse the type-id. */
4991 type = cp_parser_type_id (parser);
4992 /* Look for the closing `)'. */
4993 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4994 /* There should not be a direct-new-declarator in this production,
4995 but GCC used to allowed this, so we check and emit a sensible error
4996 message for this case. */
4997 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4999 error ("array bound forbidden after parenthesized type-id");
5000 inform ("try removing the parentheses around the type-id");
5001 cp_parser_direct_new_declarator (parser);
5003 nelts = NULL_TREE;
5005 /* Otherwise, there must be a new-type-id. */
5006 else
5007 type = cp_parser_new_type_id (parser, &nelts);
5009 /* If the next token is a `(', then we have a new-initializer. */
5010 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5011 initializer = cp_parser_new_initializer (parser);
5012 else
5013 initializer = NULL_TREE;
5015 /* A new-expression may not appear in an integral constant
5016 expression. */
5017 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5018 return error_mark_node;
5020 /* Create a representation of the new-expression. */
5021 return build_new (placement, type, nelts, initializer, global_scope_p);
5024 /* Parse a new-placement.
5026 new-placement:
5027 ( expression-list )
5029 Returns the same representation as for an expression-list. */
5031 static tree
5032 cp_parser_new_placement (cp_parser* parser)
5034 tree expression_list;
5036 /* Parse the expression-list. */
5037 expression_list = (cp_parser_parenthesized_expression_list
5038 (parser, false, /*cast_p=*/false,
5039 /*non_constant_p=*/NULL));
5041 return expression_list;
5044 /* Parse a new-type-id.
5046 new-type-id:
5047 type-specifier-seq new-declarator [opt]
5049 Returns the TYPE allocated. If the new-type-id indicates an array
5050 type, *NELTS is set to the number of elements in the last array
5051 bound; the TYPE will not include the last array bound. */
5053 static tree
5054 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5056 cp_decl_specifier_seq type_specifier_seq;
5057 cp_declarator *new_declarator;
5058 cp_declarator *declarator;
5059 cp_declarator *outer_declarator;
5060 const char *saved_message;
5061 tree type;
5063 /* The type-specifier sequence must not contain type definitions.
5064 (It cannot contain declarations of new types either, but if they
5065 are not definitions we will catch that because they are not
5066 complete.) */
5067 saved_message = parser->type_definition_forbidden_message;
5068 parser->type_definition_forbidden_message
5069 = "types may not be defined in a new-type-id";
5070 /* Parse the type-specifier-seq. */
5071 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5072 &type_specifier_seq);
5073 /* Restore the old message. */
5074 parser->type_definition_forbidden_message = saved_message;
5075 /* Parse the new-declarator. */
5076 new_declarator = cp_parser_new_declarator_opt (parser);
5078 /* Determine the number of elements in the last array dimension, if
5079 any. */
5080 *nelts = NULL_TREE;
5081 /* Skip down to the last array dimension. */
5082 declarator = new_declarator;
5083 outer_declarator = NULL;
5084 while (declarator && (declarator->kind == cdk_pointer
5085 || declarator->kind == cdk_ptrmem))
5087 outer_declarator = declarator;
5088 declarator = declarator->declarator;
5090 while (declarator
5091 && declarator->kind == cdk_array
5092 && declarator->declarator
5093 && declarator->declarator->kind == cdk_array)
5095 outer_declarator = declarator;
5096 declarator = declarator->declarator;
5099 if (declarator && declarator->kind == cdk_array)
5101 *nelts = declarator->u.array.bounds;
5102 if (*nelts == error_mark_node)
5103 *nelts = integer_one_node;
5105 if (outer_declarator)
5106 outer_declarator->declarator = declarator->declarator;
5107 else
5108 new_declarator = NULL;
5111 type = groktypename (&type_specifier_seq, new_declarator);
5112 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5114 *nelts = array_type_nelts_top (type);
5115 type = TREE_TYPE (type);
5117 return type;
5120 /* Parse an (optional) new-declarator.
5122 new-declarator:
5123 ptr-operator new-declarator [opt]
5124 direct-new-declarator
5126 Returns the declarator. */
5128 static cp_declarator *
5129 cp_parser_new_declarator_opt (cp_parser* parser)
5131 enum tree_code code;
5132 tree type;
5133 cp_cv_quals cv_quals;
5135 /* We don't know if there's a ptr-operator next, or not. */
5136 cp_parser_parse_tentatively (parser);
5137 /* Look for a ptr-operator. */
5138 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5139 /* If that worked, look for more new-declarators. */
5140 if (cp_parser_parse_definitely (parser))
5142 cp_declarator *declarator;
5144 /* Parse another optional declarator. */
5145 declarator = cp_parser_new_declarator_opt (parser);
5147 /* Create the representation of the declarator. */
5148 if (type)
5149 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5150 else if (code == INDIRECT_REF)
5151 declarator = make_pointer_declarator (cv_quals, declarator);
5152 else
5153 declarator = make_reference_declarator (cv_quals, declarator);
5155 return declarator;
5158 /* If the next token is a `[', there is a direct-new-declarator. */
5159 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5160 return cp_parser_direct_new_declarator (parser);
5162 return NULL;
5165 /* Parse a direct-new-declarator.
5167 direct-new-declarator:
5168 [ expression ]
5169 direct-new-declarator [constant-expression]
5173 static cp_declarator *
5174 cp_parser_direct_new_declarator (cp_parser* parser)
5176 cp_declarator *declarator = NULL;
5178 while (true)
5180 tree expression;
5182 /* Look for the opening `['. */
5183 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5184 /* The first expression is not required to be constant. */
5185 if (!declarator)
5187 expression = cp_parser_expression (parser, /*cast_p=*/false);
5188 /* The standard requires that the expression have integral
5189 type. DR 74 adds enumeration types. We believe that the
5190 real intent is that these expressions be handled like the
5191 expression in a `switch' condition, which also allows
5192 classes with a single conversion to integral or
5193 enumeration type. */
5194 if (!processing_template_decl)
5196 expression
5197 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5198 expression,
5199 /*complain=*/true);
5200 if (!expression)
5202 error ("expression in new-declarator must have integral "
5203 "or enumeration type");
5204 expression = error_mark_node;
5208 /* But all the other expressions must be. */
5209 else
5210 expression
5211 = cp_parser_constant_expression (parser,
5212 /*allow_non_constant=*/false,
5213 NULL);
5214 /* Look for the closing `]'. */
5215 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5217 /* Add this bound to the declarator. */
5218 declarator = make_array_declarator (declarator, expression);
5220 /* If the next token is not a `[', then there are no more
5221 bounds. */
5222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5223 break;
5226 return declarator;
5229 /* Parse a new-initializer.
5231 new-initializer:
5232 ( expression-list [opt] )
5234 Returns a representation of the expression-list. If there is no
5235 expression-list, VOID_ZERO_NODE is returned. */
5237 static tree
5238 cp_parser_new_initializer (cp_parser* parser)
5240 tree expression_list;
5242 expression_list = (cp_parser_parenthesized_expression_list
5243 (parser, false, /*cast_p=*/false,
5244 /*non_constant_p=*/NULL));
5245 if (!expression_list)
5246 expression_list = void_zero_node;
5248 return expression_list;
5251 /* Parse a delete-expression.
5253 delete-expression:
5254 :: [opt] delete cast-expression
5255 :: [opt] delete [ ] cast-expression
5257 Returns a representation of the expression. */
5259 static tree
5260 cp_parser_delete_expression (cp_parser* parser)
5262 bool global_scope_p;
5263 bool array_p;
5264 tree expression;
5266 /* Look for the optional `::' operator. */
5267 global_scope_p
5268 = (cp_parser_global_scope_opt (parser,
5269 /*current_scope_valid_p=*/false)
5270 != NULL_TREE);
5271 /* Look for the `delete' keyword. */
5272 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5273 /* See if the array syntax is in use. */
5274 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5276 /* Consume the `[' token. */
5277 cp_lexer_consume_token (parser->lexer);
5278 /* Look for the `]' token. */
5279 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5280 /* Remember that this is the `[]' construct. */
5281 array_p = true;
5283 else
5284 array_p = false;
5286 /* Parse the cast-expression. */
5287 expression = cp_parser_simple_cast_expression (parser);
5289 /* A delete-expression may not appear in an integral constant
5290 expression. */
5291 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5292 return error_mark_node;
5294 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5297 /* Parse a cast-expression.
5299 cast-expression:
5300 unary-expression
5301 ( type-id ) cast-expression
5303 ADDRESS_P is true iff the unary-expression is appearing as the
5304 operand of the `&' operator. CAST_P is true if this expression is
5305 the target of a cast.
5307 Returns a representation of the expression. */
5309 static tree
5310 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5312 /* If it's a `(', then we might be looking at a cast. */
5313 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5315 tree type = NULL_TREE;
5316 tree expr = NULL_TREE;
5317 bool compound_literal_p;
5318 const char *saved_message;
5320 /* There's no way to know yet whether or not this is a cast.
5321 For example, `(int (3))' is a unary-expression, while `(int)
5322 3' is a cast. So, we resort to parsing tentatively. */
5323 cp_parser_parse_tentatively (parser);
5324 /* Types may not be defined in a cast. */
5325 saved_message = parser->type_definition_forbidden_message;
5326 parser->type_definition_forbidden_message
5327 = "types may not be defined in casts";
5328 /* Consume the `('. */
5329 cp_lexer_consume_token (parser->lexer);
5330 /* A very tricky bit is that `(struct S) { 3 }' is a
5331 compound-literal (which we permit in C++ as an extension).
5332 But, that construct is not a cast-expression -- it is a
5333 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5334 is legal; if the compound-literal were a cast-expression,
5335 you'd need an extra set of parentheses.) But, if we parse
5336 the type-id, and it happens to be a class-specifier, then we
5337 will commit to the parse at that point, because we cannot
5338 undo the action that is done when creating a new class. So,
5339 then we cannot back up and do a postfix-expression.
5341 Therefore, we scan ahead to the closing `)', and check to see
5342 if the token after the `)' is a `{'. If so, we are not
5343 looking at a cast-expression.
5345 Save tokens so that we can put them back. */
5346 cp_lexer_save_tokens (parser->lexer);
5347 /* Skip tokens until the next token is a closing parenthesis.
5348 If we find the closing `)', and the next token is a `{', then
5349 we are looking at a compound-literal. */
5350 compound_literal_p
5351 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5352 /*consume_paren=*/true)
5353 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5354 /* Roll back the tokens we skipped. */
5355 cp_lexer_rollback_tokens (parser->lexer);
5356 /* If we were looking at a compound-literal, simulate an error
5357 so that the call to cp_parser_parse_definitely below will
5358 fail. */
5359 if (compound_literal_p)
5360 cp_parser_simulate_error (parser);
5361 else
5363 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5364 parser->in_type_id_in_expr_p = true;
5365 /* Look for the type-id. */
5366 type = cp_parser_type_id (parser);
5367 /* Look for the closing `)'. */
5368 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5369 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5372 /* Restore the saved message. */
5373 parser->type_definition_forbidden_message = saved_message;
5375 /* If ok so far, parse the dependent expression. We cannot be
5376 sure it is a cast. Consider `(T ())'. It is a parenthesized
5377 ctor of T, but looks like a cast to function returning T
5378 without a dependent expression. */
5379 if (!cp_parser_error_occurred (parser))
5380 expr = cp_parser_cast_expression (parser,
5381 /*address_p=*/false,
5382 /*cast_p=*/true);
5384 if (cp_parser_parse_definitely (parser))
5386 /* Warn about old-style casts, if so requested. */
5387 if (warn_old_style_cast
5388 && !in_system_header
5389 && !VOID_TYPE_P (type)
5390 && current_lang_name != lang_name_c)
5391 warning (0, "use of old-style cast");
5393 /* Only type conversions to integral or enumeration types
5394 can be used in constant-expressions. */
5395 if (parser->integral_constant_expression_p
5396 && !dependent_type_p (type)
5397 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5398 && (cp_parser_non_integral_constant_expression
5399 (parser,
5400 "a cast to a type other than an integral or "
5401 "enumeration type")))
5402 return error_mark_node;
5404 /* Perform the cast. */
5405 expr = build_c_cast (type, expr);
5406 return expr;
5410 /* If we get here, then it's not a cast, so it must be a
5411 unary-expression. */
5412 return cp_parser_unary_expression (parser, address_p, cast_p);
5415 /* Parse a binary expression of the general form:
5417 pm-expression:
5418 cast-expression
5419 pm-expression .* cast-expression
5420 pm-expression ->* cast-expression
5422 multiplicative-expression:
5423 pm-expression
5424 multiplicative-expression * pm-expression
5425 multiplicative-expression / pm-expression
5426 multiplicative-expression % pm-expression
5428 additive-expression:
5429 multiplicative-expression
5430 additive-expression + multiplicative-expression
5431 additive-expression - multiplicative-expression
5433 shift-expression:
5434 additive-expression
5435 shift-expression << additive-expression
5436 shift-expression >> additive-expression
5438 relational-expression:
5439 shift-expression
5440 relational-expression < shift-expression
5441 relational-expression > shift-expression
5442 relational-expression <= shift-expression
5443 relational-expression >= shift-expression
5445 GNU Extension:
5447 relational-expression:
5448 relational-expression <? shift-expression
5449 relational-expression >? shift-expression
5451 equality-expression:
5452 relational-expression
5453 equality-expression == relational-expression
5454 equality-expression != relational-expression
5456 and-expression:
5457 equality-expression
5458 and-expression & equality-expression
5460 exclusive-or-expression:
5461 and-expression
5462 exclusive-or-expression ^ and-expression
5464 inclusive-or-expression:
5465 exclusive-or-expression
5466 inclusive-or-expression | exclusive-or-expression
5468 logical-and-expression:
5469 inclusive-or-expression
5470 logical-and-expression && inclusive-or-expression
5472 logical-or-expression:
5473 logical-and-expression
5474 logical-or-expression || logical-and-expression
5476 All these are implemented with a single function like:
5478 binary-expression:
5479 simple-cast-expression
5480 binary-expression <token> binary-expression
5482 CAST_P is true if this expression is the target of a cast.
5484 The binops_by_token map is used to get the tree codes for each <token> type.
5485 binary-expressions are associated according to a precedence table. */
5487 #define TOKEN_PRECEDENCE(token) \
5488 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5489 ? PREC_NOT_OPERATOR \
5490 : binops_by_token[token->type].prec)
5492 static tree
5493 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5495 cp_parser_expression_stack stack;
5496 cp_parser_expression_stack_entry *sp = &stack[0];
5497 tree lhs, rhs;
5498 cp_token *token;
5499 enum tree_code tree_type;
5500 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5501 bool overloaded_p;
5503 /* Parse the first expression. */
5504 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5506 for (;;)
5508 /* Get an operator token. */
5509 token = cp_lexer_peek_token (parser->lexer);
5510 if (token->type == CPP_MIN || token->type == CPP_MAX)
5511 cp_parser_warn_min_max ();
5513 new_prec = TOKEN_PRECEDENCE (token);
5515 /* Popping an entry off the stack means we completed a subexpression:
5516 - either we found a token which is not an operator (`>' where it is not
5517 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5518 will happen repeatedly;
5519 - or, we found an operator which has lower priority. This is the case
5520 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5521 parsing `3 * 4'. */
5522 if (new_prec <= prec)
5524 if (sp == stack)
5525 break;
5526 else
5527 goto pop;
5530 get_rhs:
5531 tree_type = binops_by_token[token->type].tree_type;
5533 /* We used the operator token. */
5534 cp_lexer_consume_token (parser->lexer);
5536 /* Extract another operand. It may be the RHS of this expression
5537 or the LHS of a new, higher priority expression. */
5538 rhs = cp_parser_simple_cast_expression (parser);
5540 /* Get another operator token. Look up its precedence to avoid
5541 building a useless (immediately popped) stack entry for common
5542 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5543 token = cp_lexer_peek_token (parser->lexer);
5544 lookahead_prec = TOKEN_PRECEDENCE (token);
5545 if (lookahead_prec > new_prec)
5547 /* ... and prepare to parse the RHS of the new, higher priority
5548 expression. Since precedence levels on the stack are
5549 monotonically increasing, we do not have to care about
5550 stack overflows. */
5551 sp->prec = prec;
5552 sp->tree_type = tree_type;
5553 sp->lhs = lhs;
5554 sp++;
5555 lhs = rhs;
5556 prec = new_prec;
5557 new_prec = lookahead_prec;
5558 goto get_rhs;
5560 pop:
5561 /* If the stack is not empty, we have parsed into LHS the right side
5562 (`4' in the example above) of an expression we had suspended.
5563 We can use the information on the stack to recover the LHS (`3')
5564 from the stack together with the tree code (`MULT_EXPR'), and
5565 the precedence of the higher level subexpression
5566 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5567 which will be used to actually build the additive expression. */
5568 --sp;
5569 prec = sp->prec;
5570 tree_type = sp->tree_type;
5571 rhs = lhs;
5572 lhs = sp->lhs;
5575 overloaded_p = false;
5576 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5578 /* If the binary operator required the use of an overloaded operator,
5579 then this expression cannot be an integral constant-expression.
5580 An overloaded operator can be used even if both operands are
5581 otherwise permissible in an integral constant-expression if at
5582 least one of the operands is of enumeration type. */
5584 if (overloaded_p
5585 && (cp_parser_non_integral_constant_expression
5586 (parser, "calls to overloaded operators")))
5587 return error_mark_node;
5590 return lhs;
5594 /* Parse the `? expression : assignment-expression' part of a
5595 conditional-expression. The LOGICAL_OR_EXPR is the
5596 logical-or-expression that started the conditional-expression.
5597 Returns a representation of the entire conditional-expression.
5599 This routine is used by cp_parser_assignment_expression.
5601 ? expression : assignment-expression
5603 GNU Extensions:
5605 ? : assignment-expression */
5607 static tree
5608 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5610 tree expr;
5611 tree assignment_expr;
5613 /* Consume the `?' token. */
5614 cp_lexer_consume_token (parser->lexer);
5615 if (cp_parser_allow_gnu_extensions_p (parser)
5616 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5617 /* Implicit true clause. */
5618 expr = NULL_TREE;
5619 else
5620 /* Parse the expression. */
5621 expr = cp_parser_expression (parser, /*cast_p=*/false);
5623 /* The next token should be a `:'. */
5624 cp_parser_require (parser, CPP_COLON, "`:'");
5625 /* Parse the assignment-expression. */
5626 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5628 /* Build the conditional-expression. */
5629 return build_x_conditional_expr (logical_or_expr,
5630 expr,
5631 assignment_expr);
5634 /* Parse an assignment-expression.
5636 assignment-expression:
5637 conditional-expression
5638 logical-or-expression assignment-operator assignment_expression
5639 throw-expression
5641 CAST_P is true if this expression is the target of a cast.
5643 Returns a representation for the expression. */
5645 static tree
5646 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5648 tree expr;
5650 /* If the next token is the `throw' keyword, then we're looking at
5651 a throw-expression. */
5652 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5653 expr = cp_parser_throw_expression (parser);
5654 /* Otherwise, it must be that we are looking at a
5655 logical-or-expression. */
5656 else
5658 /* Parse the binary expressions (logical-or-expression). */
5659 expr = cp_parser_binary_expression (parser, cast_p);
5660 /* If the next token is a `?' then we're actually looking at a
5661 conditional-expression. */
5662 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5663 return cp_parser_question_colon_clause (parser, expr);
5664 else
5666 enum tree_code assignment_operator;
5668 /* If it's an assignment-operator, we're using the second
5669 production. */
5670 assignment_operator
5671 = cp_parser_assignment_operator_opt (parser);
5672 if (assignment_operator != ERROR_MARK)
5674 tree rhs;
5676 /* Parse the right-hand side of the assignment. */
5677 rhs = cp_parser_assignment_expression (parser, cast_p);
5678 /* An assignment may not appear in a
5679 constant-expression. */
5680 if (cp_parser_non_integral_constant_expression (parser,
5681 "an assignment"))
5682 return error_mark_node;
5683 /* Build the assignment expression. */
5684 expr = build_x_modify_expr (expr,
5685 assignment_operator,
5686 rhs);
5691 return expr;
5694 /* Parse an (optional) assignment-operator.
5696 assignment-operator: one of
5697 = *= /= %= += -= >>= <<= &= ^= |=
5699 GNU Extension:
5701 assignment-operator: one of
5702 <?= >?=
5704 If the next token is an assignment operator, the corresponding tree
5705 code is returned, and the token is consumed. For example, for
5706 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5707 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5708 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5709 operator, ERROR_MARK is returned. */
5711 static enum tree_code
5712 cp_parser_assignment_operator_opt (cp_parser* parser)
5714 enum tree_code op;
5715 cp_token *token;
5717 /* Peek at the next toen. */
5718 token = cp_lexer_peek_token (parser->lexer);
5720 switch (token->type)
5722 case CPP_EQ:
5723 op = NOP_EXPR;
5724 break;
5726 case CPP_MULT_EQ:
5727 op = MULT_EXPR;
5728 break;
5730 case CPP_DIV_EQ:
5731 op = TRUNC_DIV_EXPR;
5732 break;
5734 case CPP_MOD_EQ:
5735 op = TRUNC_MOD_EXPR;
5736 break;
5738 case CPP_PLUS_EQ:
5739 op = PLUS_EXPR;
5740 break;
5742 case CPP_MINUS_EQ:
5743 op = MINUS_EXPR;
5744 break;
5746 case CPP_RSHIFT_EQ:
5747 op = RSHIFT_EXPR;
5748 break;
5750 case CPP_LSHIFT_EQ:
5751 op = LSHIFT_EXPR;
5752 break;
5754 case CPP_AND_EQ:
5755 op = BIT_AND_EXPR;
5756 break;
5758 case CPP_XOR_EQ:
5759 op = BIT_XOR_EXPR;
5760 break;
5762 case CPP_OR_EQ:
5763 op = BIT_IOR_EXPR;
5764 break;
5766 case CPP_MIN_EQ:
5767 op = MIN_EXPR;
5768 cp_parser_warn_min_max ();
5769 break;
5771 case CPP_MAX_EQ:
5772 op = MAX_EXPR;
5773 cp_parser_warn_min_max ();
5774 break;
5776 default:
5777 /* Nothing else is an assignment operator. */
5778 op = ERROR_MARK;
5781 /* If it was an assignment operator, consume it. */
5782 if (op != ERROR_MARK)
5783 cp_lexer_consume_token (parser->lexer);
5785 return op;
5788 /* Parse an expression.
5790 expression:
5791 assignment-expression
5792 expression , assignment-expression
5794 CAST_P is true if this expression is the target of a cast.
5796 Returns a representation of the expression. */
5798 static tree
5799 cp_parser_expression (cp_parser* parser, bool cast_p)
5801 tree expression = NULL_TREE;
5803 while (true)
5805 tree assignment_expression;
5807 /* Parse the next assignment-expression. */
5808 assignment_expression
5809 = cp_parser_assignment_expression (parser, cast_p);
5810 /* If this is the first assignment-expression, we can just
5811 save it away. */
5812 if (!expression)
5813 expression = assignment_expression;
5814 else
5815 expression = build_x_compound_expr (expression,
5816 assignment_expression);
5817 /* If the next token is not a comma, then we are done with the
5818 expression. */
5819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5820 break;
5821 /* Consume the `,'. */
5822 cp_lexer_consume_token (parser->lexer);
5823 /* A comma operator cannot appear in a constant-expression. */
5824 if (cp_parser_non_integral_constant_expression (parser,
5825 "a comma operator"))
5826 expression = error_mark_node;
5829 return expression;
5832 /* Parse a constant-expression.
5834 constant-expression:
5835 conditional-expression
5837 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5838 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5839 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5840 is false, NON_CONSTANT_P should be NULL. */
5842 static tree
5843 cp_parser_constant_expression (cp_parser* parser,
5844 bool allow_non_constant_p,
5845 bool *non_constant_p)
5847 bool saved_integral_constant_expression_p;
5848 bool saved_allow_non_integral_constant_expression_p;
5849 bool saved_non_integral_constant_expression_p;
5850 tree expression;
5852 /* It might seem that we could simply parse the
5853 conditional-expression, and then check to see if it were
5854 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5855 one that the compiler can figure out is constant, possibly after
5856 doing some simplifications or optimizations. The standard has a
5857 precise definition of constant-expression, and we must honor
5858 that, even though it is somewhat more restrictive.
5860 For example:
5862 int i[(2, 3)];
5864 is not a legal declaration, because `(2, 3)' is not a
5865 constant-expression. The `,' operator is forbidden in a
5866 constant-expression. However, GCC's constant-folding machinery
5867 will fold this operation to an INTEGER_CST for `3'. */
5869 /* Save the old settings. */
5870 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5871 saved_allow_non_integral_constant_expression_p
5872 = parser->allow_non_integral_constant_expression_p;
5873 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5874 /* We are now parsing a constant-expression. */
5875 parser->integral_constant_expression_p = true;
5876 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5877 parser->non_integral_constant_expression_p = false;
5878 /* Although the grammar says "conditional-expression", we parse an
5879 "assignment-expression", which also permits "throw-expression"
5880 and the use of assignment operators. In the case that
5881 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5882 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5883 actually essential that we look for an assignment-expression.
5884 For example, cp_parser_initializer_clauses uses this function to
5885 determine whether a particular assignment-expression is in fact
5886 constant. */
5887 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5888 /* Restore the old settings. */
5889 parser->integral_constant_expression_p
5890 = saved_integral_constant_expression_p;
5891 parser->allow_non_integral_constant_expression_p
5892 = saved_allow_non_integral_constant_expression_p;
5893 if (allow_non_constant_p)
5894 *non_constant_p = parser->non_integral_constant_expression_p;
5895 else if (parser->non_integral_constant_expression_p)
5896 expression = error_mark_node;
5897 parser->non_integral_constant_expression_p
5898 = saved_non_integral_constant_expression_p;
5900 return expression;
5903 /* Parse __builtin_offsetof.
5905 offsetof-expression:
5906 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5908 offsetof-member-designator:
5909 id-expression
5910 | offsetof-member-designator "." id-expression
5911 | offsetof-member-designator "[" expression "]"
5914 static tree
5915 cp_parser_builtin_offsetof (cp_parser *parser)
5917 int save_ice_p, save_non_ice_p;
5918 tree type, expr;
5919 cp_id_kind dummy;
5921 /* We're about to accept non-integral-constant things, but will
5922 definitely yield an integral constant expression. Save and
5923 restore these values around our local parsing. */
5924 save_ice_p = parser->integral_constant_expression_p;
5925 save_non_ice_p = parser->non_integral_constant_expression_p;
5927 /* Consume the "__builtin_offsetof" token. */
5928 cp_lexer_consume_token (parser->lexer);
5929 /* Consume the opening `('. */
5930 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5931 /* Parse the type-id. */
5932 type = cp_parser_type_id (parser);
5933 /* Look for the `,'. */
5934 cp_parser_require (parser, CPP_COMMA, "`,'");
5936 /* Build the (type *)null that begins the traditional offsetof macro. */
5937 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5939 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5940 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5941 true, &dummy);
5942 while (true)
5944 cp_token *token = cp_lexer_peek_token (parser->lexer);
5945 switch (token->type)
5947 case CPP_OPEN_SQUARE:
5948 /* offsetof-member-designator "[" expression "]" */
5949 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5950 break;
5952 case CPP_DOT:
5953 /* offsetof-member-designator "." identifier */
5954 cp_lexer_consume_token (parser->lexer);
5955 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5956 true, &dummy);
5957 break;
5959 case CPP_CLOSE_PAREN:
5960 /* Consume the ")" token. */
5961 cp_lexer_consume_token (parser->lexer);
5962 goto success;
5964 default:
5965 /* Error. We know the following require will fail, but
5966 that gives the proper error message. */
5967 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5968 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5969 expr = error_mark_node;
5970 goto failure;
5974 success:
5975 /* If we're processing a template, we can't finish the semantics yet.
5976 Otherwise we can fold the entire expression now. */
5977 if (processing_template_decl)
5978 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5979 else
5980 expr = fold_offsetof (expr);
5982 failure:
5983 parser->integral_constant_expression_p = save_ice_p;
5984 parser->non_integral_constant_expression_p = save_non_ice_p;
5986 return expr;
5989 /* Statements [gram.stmt.stmt] */
5991 /* Parse a statement.
5993 statement:
5994 labeled-statement
5995 expression-statement
5996 compound-statement
5997 selection-statement
5998 iteration-statement
5999 jump-statement
6000 declaration-statement
6001 try-block */
6003 static void
6004 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
6006 tree statement;
6007 cp_token *token;
6008 location_t statement_location;
6010 /* There is no statement yet. */
6011 statement = NULL_TREE;
6012 /* Peek at the next token. */
6013 token = cp_lexer_peek_token (parser->lexer);
6014 /* Remember the location of the first token in the statement. */
6015 statement_location = token->location;
6016 /* If this is a keyword, then that will often determine what kind of
6017 statement we have. */
6018 if (token->type == CPP_KEYWORD)
6020 enum rid keyword = token->keyword;
6022 switch (keyword)
6024 case RID_CASE:
6025 case RID_DEFAULT:
6026 statement = cp_parser_labeled_statement (parser,
6027 in_statement_expr);
6028 break;
6030 case RID_IF:
6031 case RID_SWITCH:
6032 statement = cp_parser_selection_statement (parser);
6033 break;
6035 case RID_WHILE:
6036 case RID_DO:
6037 case RID_FOR:
6038 statement = cp_parser_iteration_statement (parser);
6039 break;
6041 case RID_BREAK:
6042 case RID_CONTINUE:
6043 case RID_RETURN:
6044 case RID_GOTO:
6045 statement = cp_parser_jump_statement (parser);
6046 break;
6048 /* Objective-C++ exception-handling constructs. */
6049 case RID_AT_TRY:
6050 case RID_AT_CATCH:
6051 case RID_AT_FINALLY:
6052 case RID_AT_SYNCHRONIZED:
6053 case RID_AT_THROW:
6054 statement = cp_parser_objc_statement (parser);
6055 break;
6057 case RID_TRY:
6058 statement = cp_parser_try_block (parser);
6059 break;
6061 default:
6062 /* It might be a keyword like `int' that can start a
6063 declaration-statement. */
6064 break;
6067 else if (token->type == CPP_NAME)
6069 /* If the next token is a `:', then we are looking at a
6070 labeled-statement. */
6071 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6072 if (token->type == CPP_COLON)
6073 statement = cp_parser_labeled_statement (parser, in_statement_expr);
6075 /* Anything that starts with a `{' must be a compound-statement. */
6076 else if (token->type == CPP_OPEN_BRACE)
6077 statement = cp_parser_compound_statement (parser, NULL, false);
6078 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6079 a statement all its own. */
6080 else if (token->type == CPP_PRAGMA)
6082 cp_lexer_handle_pragma (parser->lexer);
6083 return;
6085 else if (token->type == CPP_EOF)
6087 cp_parser_error (parser, "expected statement");
6088 return;
6091 /* Everything else must be a declaration-statement or an
6092 expression-statement. Try for the declaration-statement
6093 first, unless we are looking at a `;', in which case we know that
6094 we have an expression-statement. */
6095 if (!statement)
6097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6099 cp_parser_parse_tentatively (parser);
6100 /* Try to parse the declaration-statement. */
6101 cp_parser_declaration_statement (parser);
6102 /* If that worked, we're done. */
6103 if (cp_parser_parse_definitely (parser))
6104 return;
6106 /* Look for an expression-statement instead. */
6107 statement = cp_parser_expression_statement (parser, in_statement_expr);
6110 /* Set the line number for the statement. */
6111 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6112 SET_EXPR_LOCATION (statement, statement_location);
6115 /* Parse a labeled-statement.
6117 labeled-statement:
6118 identifier : statement
6119 case constant-expression : statement
6120 default : statement
6122 GNU Extension:
6124 labeled-statement:
6125 case constant-expression ... constant-expression : statement
6127 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6128 For an ordinary label, returns a LABEL_EXPR. */
6130 static tree
6131 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6133 cp_token *token;
6134 tree statement = error_mark_node;
6136 /* The next token should be an identifier. */
6137 token = cp_lexer_peek_token (parser->lexer);
6138 if (token->type != CPP_NAME
6139 && token->type != CPP_KEYWORD)
6141 cp_parser_error (parser, "expected labeled-statement");
6142 return error_mark_node;
6145 switch (token->keyword)
6147 case RID_CASE:
6149 tree expr, expr_hi;
6150 cp_token *ellipsis;
6152 /* Consume the `case' token. */
6153 cp_lexer_consume_token (parser->lexer);
6154 /* Parse the constant-expression. */
6155 expr = cp_parser_constant_expression (parser,
6156 /*allow_non_constant_p=*/false,
6157 NULL);
6159 ellipsis = cp_lexer_peek_token (parser->lexer);
6160 if (ellipsis->type == CPP_ELLIPSIS)
6162 /* Consume the `...' token. */
6163 cp_lexer_consume_token (parser->lexer);
6164 expr_hi =
6165 cp_parser_constant_expression (parser,
6166 /*allow_non_constant_p=*/false,
6167 NULL);
6168 /* We don't need to emit warnings here, as the common code
6169 will do this for us. */
6171 else
6172 expr_hi = NULL_TREE;
6174 if (!parser->in_switch_statement_p)
6175 error ("case label %qE not within a switch statement", expr);
6176 else
6177 statement = finish_case_label (expr, expr_hi);
6179 break;
6181 case RID_DEFAULT:
6182 /* Consume the `default' token. */
6183 cp_lexer_consume_token (parser->lexer);
6184 if (!parser->in_switch_statement_p)
6185 error ("case label not within a switch statement");
6186 else
6187 statement = finish_case_label (NULL_TREE, NULL_TREE);
6188 break;
6190 default:
6191 /* Anything else must be an ordinary label. */
6192 statement = finish_label_stmt (cp_parser_identifier (parser));
6193 break;
6196 /* Require the `:' token. */
6197 cp_parser_require (parser, CPP_COLON, "`:'");
6198 /* Parse the labeled statement. */
6199 cp_parser_statement (parser, in_statement_expr);
6201 /* Return the label, in the case of a `case' or `default' label. */
6202 return statement;
6205 /* Parse an expression-statement.
6207 expression-statement:
6208 expression [opt] ;
6210 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6211 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6212 indicates whether this expression-statement is part of an
6213 expression statement. */
6215 static tree
6216 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6218 tree statement = NULL_TREE;
6220 /* If the next token is a ';', then there is no expression
6221 statement. */
6222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6223 statement = cp_parser_expression (parser, /*cast_p=*/false);
6225 /* Consume the final `;'. */
6226 cp_parser_consume_semicolon_at_end_of_statement (parser);
6228 if (in_statement_expr
6229 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6230 /* This is the final expression statement of a statement
6231 expression. */
6232 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6233 else if (statement)
6234 statement = finish_expr_stmt (statement);
6235 else
6236 finish_stmt ();
6238 return statement;
6241 /* Parse a compound-statement.
6243 compound-statement:
6244 { statement-seq [opt] }
6246 Returns a tree representing the statement. */
6248 static tree
6249 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6250 bool in_try)
6252 tree compound_stmt;
6254 /* Consume the `{'. */
6255 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6256 return error_mark_node;
6257 /* Begin the compound-statement. */
6258 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6259 /* Parse an (optional) statement-seq. */
6260 cp_parser_statement_seq_opt (parser, in_statement_expr);
6261 /* Finish the compound-statement. */
6262 finish_compound_stmt (compound_stmt);
6263 /* Consume the `}'. */
6264 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6266 return compound_stmt;
6269 /* Parse an (optional) statement-seq.
6271 statement-seq:
6272 statement
6273 statement-seq [opt] statement */
6275 static void
6276 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6278 /* Scan statements until there aren't any more. */
6279 while (true)
6281 /* If we're looking at a `}', then we've run out of statements. */
6282 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6283 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6284 break;
6286 /* Parse the statement. */
6287 cp_parser_statement (parser, in_statement_expr);
6291 /* Parse a selection-statement.
6293 selection-statement:
6294 if ( condition ) statement
6295 if ( condition ) statement else statement
6296 switch ( condition ) statement
6298 Returns the new IF_STMT or SWITCH_STMT. */
6300 static tree
6301 cp_parser_selection_statement (cp_parser* parser)
6303 cp_token *token;
6304 enum rid keyword;
6306 /* Peek at the next token. */
6307 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6309 /* See what kind of keyword it is. */
6310 keyword = token->keyword;
6311 switch (keyword)
6313 case RID_IF:
6314 case RID_SWITCH:
6316 tree statement;
6317 tree condition;
6319 /* Look for the `('. */
6320 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6322 cp_parser_skip_to_end_of_statement (parser);
6323 return error_mark_node;
6326 /* Begin the selection-statement. */
6327 if (keyword == RID_IF)
6328 statement = begin_if_stmt ();
6329 else
6330 statement = begin_switch_stmt ();
6332 /* Parse the condition. */
6333 condition = cp_parser_condition (parser);
6334 /* Look for the `)'. */
6335 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6336 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6337 /*consume_paren=*/true);
6339 if (keyword == RID_IF)
6341 /* Add the condition. */
6342 finish_if_stmt_cond (condition, statement);
6344 /* Parse the then-clause. */
6345 cp_parser_implicitly_scoped_statement (parser);
6346 finish_then_clause (statement);
6348 /* If the next token is `else', parse the else-clause. */
6349 if (cp_lexer_next_token_is_keyword (parser->lexer,
6350 RID_ELSE))
6352 /* Consume the `else' keyword. */
6353 cp_lexer_consume_token (parser->lexer);
6354 begin_else_clause (statement);
6355 /* Parse the else-clause. */
6356 cp_parser_implicitly_scoped_statement (parser);
6357 finish_else_clause (statement);
6360 /* Now we're all done with the if-statement. */
6361 finish_if_stmt (statement);
6363 else
6365 bool in_switch_statement_p;
6367 /* Add the condition. */
6368 finish_switch_cond (condition, statement);
6370 /* Parse the body of the switch-statement. */
6371 in_switch_statement_p = parser->in_switch_statement_p;
6372 parser->in_switch_statement_p = true;
6373 cp_parser_implicitly_scoped_statement (parser);
6374 parser->in_switch_statement_p = in_switch_statement_p;
6376 /* Now we're all done with the switch-statement. */
6377 finish_switch_stmt (statement);
6380 return statement;
6382 break;
6384 default:
6385 cp_parser_error (parser, "expected selection-statement");
6386 return error_mark_node;
6390 /* Parse a condition.
6392 condition:
6393 expression
6394 type-specifier-seq declarator = assignment-expression
6396 GNU Extension:
6398 condition:
6399 type-specifier-seq declarator asm-specification [opt]
6400 attributes [opt] = assignment-expression
6402 Returns the expression that should be tested. */
6404 static tree
6405 cp_parser_condition (cp_parser* parser)
6407 cp_decl_specifier_seq type_specifiers;
6408 const char *saved_message;
6410 /* Try the declaration first. */
6411 cp_parser_parse_tentatively (parser);
6412 /* New types are not allowed in the type-specifier-seq for a
6413 condition. */
6414 saved_message = parser->type_definition_forbidden_message;
6415 parser->type_definition_forbidden_message
6416 = "types may not be defined in conditions";
6417 /* Parse the type-specifier-seq. */
6418 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6419 &type_specifiers);
6420 /* Restore the saved message. */
6421 parser->type_definition_forbidden_message = saved_message;
6422 /* If all is well, we might be looking at a declaration. */
6423 if (!cp_parser_error_occurred (parser))
6425 tree decl;
6426 tree asm_specification;
6427 tree attributes;
6428 cp_declarator *declarator;
6429 tree initializer = NULL_TREE;
6431 /* Parse the declarator. */
6432 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6433 /*ctor_dtor_or_conv_p=*/NULL,
6434 /*parenthesized_p=*/NULL,
6435 /*member_p=*/false);
6436 /* Parse the attributes. */
6437 attributes = cp_parser_attributes_opt (parser);
6438 /* Parse the asm-specification. */
6439 asm_specification = cp_parser_asm_specification_opt (parser);
6440 /* If the next token is not an `=', then we might still be
6441 looking at an expression. For example:
6443 if (A(a).x)
6445 looks like a decl-specifier-seq and a declarator -- but then
6446 there is no `=', so this is an expression. */
6447 cp_parser_require (parser, CPP_EQ, "`='");
6448 /* If we did see an `=', then we are looking at a declaration
6449 for sure. */
6450 if (cp_parser_parse_definitely (parser))
6452 tree pushed_scope;
6454 /* Create the declaration. */
6455 decl = start_decl (declarator, &type_specifiers,
6456 /*initialized_p=*/true,
6457 attributes, /*prefix_attributes=*/NULL_TREE,
6458 &pushed_scope);
6459 /* Parse the assignment-expression. */
6460 initializer = cp_parser_assignment_expression (parser,
6461 /*cast_p=*/false);
6463 /* Process the initializer. */
6464 cp_finish_decl (decl,
6465 initializer,
6466 asm_specification,
6467 LOOKUP_ONLYCONVERTING);
6469 if (pushed_scope)
6470 pop_scope (pushed_scope);
6472 return convert_from_reference (decl);
6475 /* If we didn't even get past the declarator successfully, we are
6476 definitely not looking at a declaration. */
6477 else
6478 cp_parser_abort_tentative_parse (parser);
6480 /* Otherwise, we are looking at an expression. */
6481 return cp_parser_expression (parser, /*cast_p=*/false);
6484 /* Parse an iteration-statement.
6486 iteration-statement:
6487 while ( condition ) statement
6488 do statement while ( expression ) ;
6489 for ( for-init-statement condition [opt] ; expression [opt] )
6490 statement
6492 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6494 static tree
6495 cp_parser_iteration_statement (cp_parser* parser)
6497 cp_token *token;
6498 enum rid keyword;
6499 tree statement;
6500 bool in_iteration_statement_p;
6503 /* Peek at the next token. */
6504 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6505 if (!token)
6506 return error_mark_node;
6508 /* Remember whether or not we are already within an iteration
6509 statement. */
6510 in_iteration_statement_p = parser->in_iteration_statement_p;
6512 /* See what kind of keyword it is. */
6513 keyword = token->keyword;
6514 switch (keyword)
6516 case RID_WHILE:
6518 tree condition;
6520 /* Begin the while-statement. */
6521 statement = begin_while_stmt ();
6522 /* Look for the `('. */
6523 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6524 /* Parse the condition. */
6525 condition = cp_parser_condition (parser);
6526 finish_while_stmt_cond (condition, statement);
6527 /* Look for the `)'. */
6528 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6529 /* Parse the dependent statement. */
6530 parser->in_iteration_statement_p = true;
6531 cp_parser_already_scoped_statement (parser);
6532 parser->in_iteration_statement_p = in_iteration_statement_p;
6533 /* We're done with the while-statement. */
6534 finish_while_stmt (statement);
6536 break;
6538 case RID_DO:
6540 tree expression;
6542 /* Begin the do-statement. */
6543 statement = begin_do_stmt ();
6544 /* Parse the body of the do-statement. */
6545 parser->in_iteration_statement_p = true;
6546 cp_parser_implicitly_scoped_statement (parser);
6547 parser->in_iteration_statement_p = in_iteration_statement_p;
6548 finish_do_body (statement);
6549 /* Look for the `while' keyword. */
6550 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6551 /* Look for the `('. */
6552 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6553 /* Parse the expression. */
6554 expression = cp_parser_expression (parser, /*cast_p=*/false);
6555 /* We're done with the do-statement. */
6556 finish_do_stmt (expression, statement);
6557 /* Look for the `)'. */
6558 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6559 /* Look for the `;'. */
6560 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6562 break;
6564 case RID_FOR:
6566 tree condition = NULL_TREE;
6567 tree expression = NULL_TREE;
6569 /* Begin the for-statement. */
6570 statement = begin_for_stmt ();
6571 /* Look for the `('. */
6572 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6573 /* Parse the initialization. */
6574 cp_parser_for_init_statement (parser);
6575 finish_for_init_stmt (statement);
6577 /* If there's a condition, process it. */
6578 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6579 condition = cp_parser_condition (parser);
6580 finish_for_cond (condition, statement);
6581 /* Look for the `;'. */
6582 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6584 /* If there's an expression, process it. */
6585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6586 expression = cp_parser_expression (parser, /*cast_p=*/false);
6587 finish_for_expr (expression, statement);
6588 /* Look for the `)'. */
6589 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6591 /* Parse the body of the for-statement. */
6592 parser->in_iteration_statement_p = true;
6593 cp_parser_already_scoped_statement (parser);
6594 parser->in_iteration_statement_p = in_iteration_statement_p;
6596 /* We're done with the for-statement. */
6597 finish_for_stmt (statement);
6599 break;
6601 default:
6602 cp_parser_error (parser, "expected iteration-statement");
6603 statement = error_mark_node;
6604 break;
6607 return statement;
6610 /* Parse a for-init-statement.
6612 for-init-statement:
6613 expression-statement
6614 simple-declaration */
6616 static void
6617 cp_parser_for_init_statement (cp_parser* parser)
6619 /* If the next token is a `;', then we have an empty
6620 expression-statement. Grammatically, this is also a
6621 simple-declaration, but an invalid one, because it does not
6622 declare anything. Therefore, if we did not handle this case
6623 specially, we would issue an error message about an invalid
6624 declaration. */
6625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6627 /* We're going to speculatively look for a declaration, falling back
6628 to an expression, if necessary. */
6629 cp_parser_parse_tentatively (parser);
6630 /* Parse the declaration. */
6631 cp_parser_simple_declaration (parser,
6632 /*function_definition_allowed_p=*/false);
6633 /* If the tentative parse failed, then we shall need to look for an
6634 expression-statement. */
6635 if (cp_parser_parse_definitely (parser))
6636 return;
6639 cp_parser_expression_statement (parser, false);
6642 /* Parse a jump-statement.
6644 jump-statement:
6645 break ;
6646 continue ;
6647 return expression [opt] ;
6648 goto identifier ;
6650 GNU extension:
6652 jump-statement:
6653 goto * expression ;
6655 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6657 static tree
6658 cp_parser_jump_statement (cp_parser* parser)
6660 tree statement = error_mark_node;
6661 cp_token *token;
6662 enum rid keyword;
6664 /* Peek at the next token. */
6665 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6666 if (!token)
6667 return error_mark_node;
6669 /* See what kind of keyword it is. */
6670 keyword = token->keyword;
6671 switch (keyword)
6673 case RID_BREAK:
6674 if (!parser->in_switch_statement_p
6675 && !parser->in_iteration_statement_p)
6677 error ("break statement not within loop or switch");
6678 statement = error_mark_node;
6680 else
6681 statement = finish_break_stmt ();
6682 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6683 break;
6685 case RID_CONTINUE:
6686 if (!parser->in_iteration_statement_p)
6688 error ("continue statement not within a loop");
6689 statement = error_mark_node;
6691 else
6692 statement = finish_continue_stmt ();
6693 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6694 break;
6696 case RID_RETURN:
6698 tree expr;
6700 /* If the next token is a `;', then there is no
6701 expression. */
6702 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6703 expr = cp_parser_expression (parser, /*cast_p=*/false);
6704 else
6705 expr = NULL_TREE;
6706 /* Build the return-statement. */
6707 statement = finish_return_stmt (expr);
6708 /* Look for the final `;'. */
6709 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6711 break;
6713 case RID_GOTO:
6714 /* Create the goto-statement. */
6715 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6717 /* Issue a warning about this use of a GNU extension. */
6718 if (pedantic)
6719 pedwarn ("ISO C++ forbids computed gotos");
6720 /* Consume the '*' token. */
6721 cp_lexer_consume_token (parser->lexer);
6722 /* Parse the dependent expression. */
6723 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6725 else
6726 finish_goto_stmt (cp_parser_identifier (parser));
6727 /* Look for the final `;'. */
6728 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6729 break;
6731 default:
6732 cp_parser_error (parser, "expected jump-statement");
6733 break;
6736 return statement;
6739 /* Parse a declaration-statement.
6741 declaration-statement:
6742 block-declaration */
6744 static void
6745 cp_parser_declaration_statement (cp_parser* parser)
6747 void *p;
6749 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6750 p = obstack_alloc (&declarator_obstack, 0);
6752 /* Parse the block-declaration. */
6753 cp_parser_block_declaration (parser, /*statement_p=*/true);
6755 /* Free any declarators allocated. */
6756 obstack_free (&declarator_obstack, p);
6758 /* Finish off the statement. */
6759 finish_stmt ();
6762 /* Some dependent statements (like `if (cond) statement'), are
6763 implicitly in their own scope. In other words, if the statement is
6764 a single statement (as opposed to a compound-statement), it is
6765 none-the-less treated as if it were enclosed in braces. Any
6766 declarations appearing in the dependent statement are out of scope
6767 after control passes that point. This function parses a statement,
6768 but ensures that is in its own scope, even if it is not a
6769 compound-statement.
6771 Returns the new statement. */
6773 static tree
6774 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6776 tree statement;
6778 /* If the token is not a `{', then we must take special action. */
6779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6781 /* Create a compound-statement. */
6782 statement = begin_compound_stmt (0);
6783 /* Parse the dependent-statement. */
6784 cp_parser_statement (parser, false);
6785 /* Finish the dummy compound-statement. */
6786 finish_compound_stmt (statement);
6788 /* Otherwise, we simply parse the statement directly. */
6789 else
6790 statement = cp_parser_compound_statement (parser, NULL, false);
6792 /* Return the statement. */
6793 return statement;
6796 /* For some dependent statements (like `while (cond) statement'), we
6797 have already created a scope. Therefore, even if the dependent
6798 statement is a compound-statement, we do not want to create another
6799 scope. */
6801 static void
6802 cp_parser_already_scoped_statement (cp_parser* parser)
6804 /* If the token is a `{', then we must take special action. */
6805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6806 cp_parser_statement (parser, false);
6807 else
6809 /* Avoid calling cp_parser_compound_statement, so that we
6810 don't create a new scope. Do everything else by hand. */
6811 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6812 cp_parser_statement_seq_opt (parser, false);
6813 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6817 /* Declarations [gram.dcl.dcl] */
6819 /* Parse an optional declaration-sequence.
6821 declaration-seq:
6822 declaration
6823 declaration-seq declaration */
6825 static void
6826 cp_parser_declaration_seq_opt (cp_parser* parser)
6828 while (true)
6830 cp_token *token;
6832 token = cp_lexer_peek_token (parser->lexer);
6834 if (token->type == CPP_CLOSE_BRACE
6835 || token->type == CPP_EOF)
6836 break;
6838 if (token->type == CPP_SEMICOLON)
6840 /* A declaration consisting of a single semicolon is
6841 invalid. Allow it unless we're being pedantic. */
6842 cp_lexer_consume_token (parser->lexer);
6843 if (pedantic && !in_system_header)
6844 pedwarn ("extra %<;%>");
6845 continue;
6848 /* If we're entering or exiting a region that's implicitly
6849 extern "C", modify the lang context appropriately. */
6850 if (!parser->implicit_extern_c && token->implicit_extern_c)
6852 push_lang_context (lang_name_c);
6853 parser->implicit_extern_c = true;
6855 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6857 pop_lang_context ();
6858 parser->implicit_extern_c = false;
6861 if (token->type == CPP_PRAGMA)
6863 /* A top-level declaration can consist solely of a #pragma.
6864 A nested declaration cannot, so this is done here and not
6865 in cp_parser_declaration. (A #pragma at block scope is
6866 handled in cp_parser_statement.) */
6867 cp_lexer_handle_pragma (parser->lexer);
6868 continue;
6871 /* Parse the declaration itself. */
6872 cp_parser_declaration (parser);
6876 /* Parse a declaration.
6878 declaration:
6879 block-declaration
6880 function-definition
6881 template-declaration
6882 explicit-instantiation
6883 explicit-specialization
6884 linkage-specification
6885 namespace-definition
6887 GNU extension:
6889 declaration:
6890 __extension__ declaration */
6892 static void
6893 cp_parser_declaration (cp_parser* parser)
6895 cp_token token1;
6896 cp_token token2;
6897 int saved_pedantic;
6898 void *p;
6900 /* Check for the `__extension__' keyword. */
6901 if (cp_parser_extension_opt (parser, &saved_pedantic))
6903 /* Parse the qualified declaration. */
6904 cp_parser_declaration (parser);
6905 /* Restore the PEDANTIC flag. */
6906 pedantic = saved_pedantic;
6908 return;
6911 /* Try to figure out what kind of declaration is present. */
6912 token1 = *cp_lexer_peek_token (parser->lexer);
6914 if (token1.type != CPP_EOF)
6915 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6916 else
6918 token2.type = CPP_EOF;
6919 token2.keyword = RID_MAX;
6922 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6923 p = obstack_alloc (&declarator_obstack, 0);
6925 /* If the next token is `extern' and the following token is a string
6926 literal, then we have a linkage specification. */
6927 if (token1.keyword == RID_EXTERN
6928 && cp_parser_is_string_literal (&token2))
6929 cp_parser_linkage_specification (parser);
6930 /* If the next token is `template', then we have either a template
6931 declaration, an explicit instantiation, or an explicit
6932 specialization. */
6933 else if (token1.keyword == RID_TEMPLATE)
6935 /* `template <>' indicates a template specialization. */
6936 if (token2.type == CPP_LESS
6937 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6938 cp_parser_explicit_specialization (parser);
6939 /* `template <' indicates a template declaration. */
6940 else if (token2.type == CPP_LESS)
6941 cp_parser_template_declaration (parser, /*member_p=*/false);
6942 /* Anything else must be an explicit instantiation. */
6943 else
6944 cp_parser_explicit_instantiation (parser);
6946 /* If the next token is `export', then we have a template
6947 declaration. */
6948 else if (token1.keyword == RID_EXPORT)
6949 cp_parser_template_declaration (parser, /*member_p=*/false);
6950 /* If the next token is `extern', 'static' or 'inline' and the one
6951 after that is `template', we have a GNU extended explicit
6952 instantiation directive. */
6953 else if (cp_parser_allow_gnu_extensions_p (parser)
6954 && (token1.keyword == RID_EXTERN
6955 || token1.keyword == RID_STATIC
6956 || token1.keyword == RID_INLINE)
6957 && token2.keyword == RID_TEMPLATE)
6958 cp_parser_explicit_instantiation (parser);
6959 /* If the next token is `namespace', check for a named or unnamed
6960 namespace definition. */
6961 else if (token1.keyword == RID_NAMESPACE
6962 && (/* A named namespace definition. */
6963 (token2.type == CPP_NAME
6964 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6965 == CPP_OPEN_BRACE))
6966 /* An unnamed namespace definition. */
6967 || token2.type == CPP_OPEN_BRACE))
6968 cp_parser_namespace_definition (parser);
6969 /* Objective-C++ declaration/definition. */
6970 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
6971 cp_parser_objc_declaration (parser);
6972 /* We must have either a block declaration or a function
6973 definition. */
6974 else
6975 /* Try to parse a block-declaration, or a function-definition. */
6976 cp_parser_block_declaration (parser, /*statement_p=*/false);
6978 /* Free any declarators allocated. */
6979 obstack_free (&declarator_obstack, p);
6982 /* Parse a block-declaration.
6984 block-declaration:
6985 simple-declaration
6986 asm-definition
6987 namespace-alias-definition
6988 using-declaration
6989 using-directive
6991 GNU Extension:
6993 block-declaration:
6994 __extension__ block-declaration
6995 label-declaration
6997 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6998 part of a declaration-statement. */
7000 static void
7001 cp_parser_block_declaration (cp_parser *parser,
7002 bool statement_p)
7004 cp_token *token1;
7005 int saved_pedantic;
7007 /* Check for the `__extension__' keyword. */
7008 if (cp_parser_extension_opt (parser, &saved_pedantic))
7010 /* Parse the qualified declaration. */
7011 cp_parser_block_declaration (parser, statement_p);
7012 /* Restore the PEDANTIC flag. */
7013 pedantic = saved_pedantic;
7015 return;
7018 /* Peek at the next token to figure out which kind of declaration is
7019 present. */
7020 token1 = cp_lexer_peek_token (parser->lexer);
7022 /* If the next keyword is `asm', we have an asm-definition. */
7023 if (token1->keyword == RID_ASM)
7025 if (statement_p)
7026 cp_parser_commit_to_tentative_parse (parser);
7027 cp_parser_asm_definition (parser);
7029 /* If the next keyword is `namespace', we have a
7030 namespace-alias-definition. */
7031 else if (token1->keyword == RID_NAMESPACE)
7032 cp_parser_namespace_alias_definition (parser);
7033 /* If the next keyword is `using', we have either a
7034 using-declaration or a using-directive. */
7035 else if (token1->keyword == RID_USING)
7037 cp_token *token2;
7039 if (statement_p)
7040 cp_parser_commit_to_tentative_parse (parser);
7041 /* If the token after `using' is `namespace', then we have a
7042 using-directive. */
7043 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7044 if (token2->keyword == RID_NAMESPACE)
7045 cp_parser_using_directive (parser);
7046 /* Otherwise, it's a using-declaration. */
7047 else
7048 cp_parser_using_declaration (parser);
7050 /* If the next keyword is `__label__' we have a label declaration. */
7051 else if (token1->keyword == RID_LABEL)
7053 if (statement_p)
7054 cp_parser_commit_to_tentative_parse (parser);
7055 cp_parser_label_declaration (parser);
7057 /* Anything else must be a simple-declaration. */
7058 else
7059 cp_parser_simple_declaration (parser, !statement_p);
7062 /* Parse a simple-declaration.
7064 simple-declaration:
7065 decl-specifier-seq [opt] init-declarator-list [opt] ;
7067 init-declarator-list:
7068 init-declarator
7069 init-declarator-list , init-declarator
7071 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7072 function-definition as a simple-declaration. */
7074 static void
7075 cp_parser_simple_declaration (cp_parser* parser,
7076 bool function_definition_allowed_p)
7078 cp_decl_specifier_seq decl_specifiers;
7079 int declares_class_or_enum;
7080 bool saw_declarator;
7082 /* Defer access checks until we know what is being declared; the
7083 checks for names appearing in the decl-specifier-seq should be
7084 done as if we were in the scope of the thing being declared. */
7085 push_deferring_access_checks (dk_deferred);
7087 /* Parse the decl-specifier-seq. We have to keep track of whether
7088 or not the decl-specifier-seq declares a named class or
7089 enumeration type, since that is the only case in which the
7090 init-declarator-list is allowed to be empty.
7092 [dcl.dcl]
7094 In a simple-declaration, the optional init-declarator-list can be
7095 omitted only when declaring a class or enumeration, that is when
7096 the decl-specifier-seq contains either a class-specifier, an
7097 elaborated-type-specifier, or an enum-specifier. */
7098 cp_parser_decl_specifier_seq (parser,
7099 CP_PARSER_FLAGS_OPTIONAL,
7100 &decl_specifiers,
7101 &declares_class_or_enum);
7102 /* We no longer need to defer access checks. */
7103 stop_deferring_access_checks ();
7105 /* In a block scope, a valid declaration must always have a
7106 decl-specifier-seq. By not trying to parse declarators, we can
7107 resolve the declaration/expression ambiguity more quickly. */
7108 if (!function_definition_allowed_p
7109 && !decl_specifiers.any_specifiers_p)
7111 cp_parser_error (parser, "expected declaration");
7112 goto done;
7115 /* If the next two tokens are both identifiers, the code is
7116 erroneous. The usual cause of this situation is code like:
7118 T t;
7120 where "T" should name a type -- but does not. */
7121 if (!decl_specifiers.type
7122 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7124 /* If parsing tentatively, we should commit; we really are
7125 looking at a declaration. */
7126 cp_parser_commit_to_tentative_parse (parser);
7127 /* Give up. */
7128 goto done;
7131 /* If we have seen at least one decl-specifier, and the next token
7132 is not a parenthesis, then we must be looking at a declaration.
7133 (After "int (" we might be looking at a functional cast.) */
7134 if (decl_specifiers.any_specifiers_p
7135 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7136 cp_parser_commit_to_tentative_parse (parser);
7138 /* Keep going until we hit the `;' at the end of the simple
7139 declaration. */
7140 saw_declarator = false;
7141 while (cp_lexer_next_token_is_not (parser->lexer,
7142 CPP_SEMICOLON))
7144 cp_token *token;
7145 bool function_definition_p;
7146 tree decl;
7148 saw_declarator = true;
7149 /* Parse the init-declarator. */
7150 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7151 function_definition_allowed_p,
7152 /*member_p=*/false,
7153 declares_class_or_enum,
7154 &function_definition_p);
7155 /* If an error occurred while parsing tentatively, exit quickly.
7156 (That usually happens when in the body of a function; each
7157 statement is treated as a declaration-statement until proven
7158 otherwise.) */
7159 if (cp_parser_error_occurred (parser))
7160 goto done;
7161 /* Handle function definitions specially. */
7162 if (function_definition_p)
7164 /* If the next token is a `,', then we are probably
7165 processing something like:
7167 void f() {}, *p;
7169 which is erroneous. */
7170 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7171 error ("mixing declarations and function-definitions is forbidden");
7172 /* Otherwise, we're done with the list of declarators. */
7173 else
7175 pop_deferring_access_checks ();
7176 return;
7179 /* The next token should be either a `,' or a `;'. */
7180 token = cp_lexer_peek_token (parser->lexer);
7181 /* If it's a `,', there are more declarators to come. */
7182 if (token->type == CPP_COMMA)
7183 cp_lexer_consume_token (parser->lexer);
7184 /* If it's a `;', we are done. */
7185 else if (token->type == CPP_SEMICOLON)
7186 break;
7187 /* Anything else is an error. */
7188 else
7190 /* If we have already issued an error message we don't need
7191 to issue another one. */
7192 if (decl != error_mark_node
7193 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7194 cp_parser_error (parser, "expected %<,%> or %<;%>");
7195 /* Skip tokens until we reach the end of the statement. */
7196 cp_parser_skip_to_end_of_statement (parser);
7197 /* If the next token is now a `;', consume it. */
7198 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7199 cp_lexer_consume_token (parser->lexer);
7200 goto done;
7202 /* After the first time around, a function-definition is not
7203 allowed -- even if it was OK at first. For example:
7205 int i, f() {}
7207 is not valid. */
7208 function_definition_allowed_p = false;
7211 /* Issue an error message if no declarators are present, and the
7212 decl-specifier-seq does not itself declare a class or
7213 enumeration. */
7214 if (!saw_declarator)
7216 if (cp_parser_declares_only_class_p (parser))
7217 shadow_tag (&decl_specifiers);
7218 /* Perform any deferred access checks. */
7219 perform_deferred_access_checks ();
7222 /* Consume the `;'. */
7223 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7225 done:
7226 pop_deferring_access_checks ();
7229 /* Parse a decl-specifier-seq.
7231 decl-specifier-seq:
7232 decl-specifier-seq [opt] decl-specifier
7234 decl-specifier:
7235 storage-class-specifier
7236 type-specifier
7237 function-specifier
7238 friend
7239 typedef
7241 GNU Extension:
7243 decl-specifier:
7244 attributes
7246 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7248 The parser flags FLAGS is used to control type-specifier parsing.
7250 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7251 flags:
7253 1: one of the decl-specifiers is an elaborated-type-specifier
7254 (i.e., a type declaration)
7255 2: one of the decl-specifiers is an enum-specifier or a
7256 class-specifier (i.e., a type definition)
7260 static void
7261 cp_parser_decl_specifier_seq (cp_parser* parser,
7262 cp_parser_flags flags,
7263 cp_decl_specifier_seq *decl_specs,
7264 int* declares_class_or_enum)
7266 bool constructor_possible_p = !parser->in_declarator_p;
7268 /* Clear DECL_SPECS. */
7269 clear_decl_specs (decl_specs);
7271 /* Assume no class or enumeration type is declared. */
7272 *declares_class_or_enum = 0;
7274 /* Keep reading specifiers until there are no more to read. */
7275 while (true)
7277 bool constructor_p;
7278 bool found_decl_spec;
7279 cp_token *token;
7281 /* Peek at the next token. */
7282 token = cp_lexer_peek_token (parser->lexer);
7283 /* Handle attributes. */
7284 if (token->keyword == RID_ATTRIBUTE)
7286 /* Parse the attributes. */
7287 decl_specs->attributes
7288 = chainon (decl_specs->attributes,
7289 cp_parser_attributes_opt (parser));
7290 continue;
7292 /* Assume we will find a decl-specifier keyword. */
7293 found_decl_spec = true;
7294 /* If the next token is an appropriate keyword, we can simply
7295 add it to the list. */
7296 switch (token->keyword)
7298 /* decl-specifier:
7299 friend */
7300 case RID_FRIEND:
7301 if (decl_specs->specs[(int) ds_friend]++)
7302 error ("duplicate %<friend%>");
7303 /* Consume the token. */
7304 cp_lexer_consume_token (parser->lexer);
7305 break;
7307 /* function-specifier:
7308 inline
7309 virtual
7310 explicit */
7311 case RID_INLINE:
7312 case RID_VIRTUAL:
7313 case RID_EXPLICIT:
7314 cp_parser_function_specifier_opt (parser, decl_specs);
7315 break;
7317 /* decl-specifier:
7318 typedef */
7319 case RID_TYPEDEF:
7320 ++decl_specs->specs[(int) ds_typedef];
7321 /* Consume the token. */
7322 cp_lexer_consume_token (parser->lexer);
7323 /* A constructor declarator cannot appear in a typedef. */
7324 constructor_possible_p = false;
7325 /* The "typedef" keyword can only occur in a declaration; we
7326 may as well commit at this point. */
7327 cp_parser_commit_to_tentative_parse (parser);
7328 break;
7330 /* storage-class-specifier:
7331 auto
7332 register
7333 static
7334 extern
7335 mutable
7337 GNU Extension:
7338 thread */
7339 case RID_AUTO:
7340 /* Consume the token. */
7341 cp_lexer_consume_token (parser->lexer);
7342 cp_parser_set_storage_class (decl_specs, sc_auto);
7343 break;
7344 case RID_REGISTER:
7345 /* Consume the token. */
7346 cp_lexer_consume_token (parser->lexer);
7347 cp_parser_set_storage_class (decl_specs, sc_register);
7348 break;
7349 case RID_STATIC:
7350 /* Consume the token. */
7351 cp_lexer_consume_token (parser->lexer);
7352 if (decl_specs->specs[(int) ds_thread])
7354 error ("%<__thread%> before %<static%>");
7355 decl_specs->specs[(int) ds_thread] = 0;
7357 cp_parser_set_storage_class (decl_specs, sc_static);
7358 break;
7359 case RID_EXTERN:
7360 /* Consume the token. */
7361 cp_lexer_consume_token (parser->lexer);
7362 if (decl_specs->specs[(int) ds_thread])
7364 error ("%<__thread%> before %<extern%>");
7365 decl_specs->specs[(int) ds_thread] = 0;
7367 cp_parser_set_storage_class (decl_specs, sc_extern);
7368 break;
7369 case RID_MUTABLE:
7370 /* Consume the token. */
7371 cp_lexer_consume_token (parser->lexer);
7372 cp_parser_set_storage_class (decl_specs, sc_mutable);
7373 break;
7374 case RID_THREAD:
7375 /* Consume the token. */
7376 cp_lexer_consume_token (parser->lexer);
7377 ++decl_specs->specs[(int) ds_thread];
7378 break;
7380 default:
7381 /* We did not yet find a decl-specifier yet. */
7382 found_decl_spec = false;
7383 break;
7386 /* Constructors are a special case. The `S' in `S()' is not a
7387 decl-specifier; it is the beginning of the declarator. */
7388 constructor_p
7389 = (!found_decl_spec
7390 && constructor_possible_p
7391 && (cp_parser_constructor_declarator_p
7392 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7394 /* If we don't have a DECL_SPEC yet, then we must be looking at
7395 a type-specifier. */
7396 if (!found_decl_spec && !constructor_p)
7398 int decl_spec_declares_class_or_enum;
7399 bool is_cv_qualifier;
7400 tree type_spec;
7402 type_spec
7403 = cp_parser_type_specifier (parser, flags,
7404 decl_specs,
7405 /*is_declaration=*/true,
7406 &decl_spec_declares_class_or_enum,
7407 &is_cv_qualifier);
7409 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7411 /* If this type-specifier referenced a user-defined type
7412 (a typedef, class-name, etc.), then we can't allow any
7413 more such type-specifiers henceforth.
7415 [dcl.spec]
7417 The longest sequence of decl-specifiers that could
7418 possibly be a type name is taken as the
7419 decl-specifier-seq of a declaration. The sequence shall
7420 be self-consistent as described below.
7422 [dcl.type]
7424 As a general rule, at most one type-specifier is allowed
7425 in the complete decl-specifier-seq of a declaration. The
7426 only exceptions are the following:
7428 -- const or volatile can be combined with any other
7429 type-specifier.
7431 -- signed or unsigned can be combined with char, long,
7432 short, or int.
7434 -- ..
7436 Example:
7438 typedef char* Pc;
7439 void g (const int Pc);
7441 Here, Pc is *not* part of the decl-specifier seq; it's
7442 the declarator. Therefore, once we see a type-specifier
7443 (other than a cv-qualifier), we forbid any additional
7444 user-defined types. We *do* still allow things like `int
7445 int' to be considered a decl-specifier-seq, and issue the
7446 error message later. */
7447 if (type_spec && !is_cv_qualifier)
7448 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7449 /* A constructor declarator cannot follow a type-specifier. */
7450 if (type_spec)
7452 constructor_possible_p = false;
7453 found_decl_spec = true;
7457 /* If we still do not have a DECL_SPEC, then there are no more
7458 decl-specifiers. */
7459 if (!found_decl_spec)
7460 break;
7462 decl_specs->any_specifiers_p = true;
7463 /* After we see one decl-specifier, further decl-specifiers are
7464 always optional. */
7465 flags |= CP_PARSER_FLAGS_OPTIONAL;
7468 /* Don't allow a friend specifier with a class definition. */
7469 if (decl_specs->specs[(int) ds_friend] != 0
7470 && (*declares_class_or_enum & 2))
7471 error ("class definition may not be declared a friend");
7474 /* Parse an (optional) storage-class-specifier.
7476 storage-class-specifier:
7477 auto
7478 register
7479 static
7480 extern
7481 mutable
7483 GNU Extension:
7485 storage-class-specifier:
7486 thread
7488 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7490 static tree
7491 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7493 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7495 case RID_AUTO:
7496 case RID_REGISTER:
7497 case RID_STATIC:
7498 case RID_EXTERN:
7499 case RID_MUTABLE:
7500 case RID_THREAD:
7501 /* Consume the token. */
7502 return cp_lexer_consume_token (parser->lexer)->value;
7504 default:
7505 return NULL_TREE;
7509 /* Parse an (optional) function-specifier.
7511 function-specifier:
7512 inline
7513 virtual
7514 explicit
7516 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7517 Updates DECL_SPECS, if it is non-NULL. */
7519 static tree
7520 cp_parser_function_specifier_opt (cp_parser* parser,
7521 cp_decl_specifier_seq *decl_specs)
7523 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7525 case RID_INLINE:
7526 if (decl_specs)
7527 ++decl_specs->specs[(int) ds_inline];
7528 break;
7530 case RID_VIRTUAL:
7531 if (decl_specs)
7532 ++decl_specs->specs[(int) ds_virtual];
7533 break;
7535 case RID_EXPLICIT:
7536 if (decl_specs)
7537 ++decl_specs->specs[(int) ds_explicit];
7538 break;
7540 default:
7541 return NULL_TREE;
7544 /* Consume the token. */
7545 return cp_lexer_consume_token (parser->lexer)->value;
7548 /* Parse a linkage-specification.
7550 linkage-specification:
7551 extern string-literal { declaration-seq [opt] }
7552 extern string-literal declaration */
7554 static void
7555 cp_parser_linkage_specification (cp_parser* parser)
7557 tree linkage;
7559 /* Look for the `extern' keyword. */
7560 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7562 /* Look for the string-literal. */
7563 linkage = cp_parser_string_literal (parser, false, false);
7565 /* Transform the literal into an identifier. If the literal is a
7566 wide-character string, or contains embedded NULs, then we can't
7567 handle it as the user wants. */
7568 if (strlen (TREE_STRING_POINTER (linkage))
7569 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7571 cp_parser_error (parser, "invalid linkage-specification");
7572 /* Assume C++ linkage. */
7573 linkage = lang_name_cplusplus;
7575 else
7576 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7578 /* We're now using the new linkage. */
7579 push_lang_context (linkage);
7581 /* If the next token is a `{', then we're using the first
7582 production. */
7583 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7585 /* Consume the `{' token. */
7586 cp_lexer_consume_token (parser->lexer);
7587 /* Parse the declarations. */
7588 cp_parser_declaration_seq_opt (parser);
7589 /* Look for the closing `}'. */
7590 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7592 /* Otherwise, there's just one declaration. */
7593 else
7595 bool saved_in_unbraced_linkage_specification_p;
7597 saved_in_unbraced_linkage_specification_p
7598 = parser->in_unbraced_linkage_specification_p;
7599 parser->in_unbraced_linkage_specification_p = true;
7600 have_extern_spec = true;
7601 cp_parser_declaration (parser);
7602 have_extern_spec = false;
7603 parser->in_unbraced_linkage_specification_p
7604 = saved_in_unbraced_linkage_specification_p;
7607 /* We're done with the linkage-specification. */
7608 pop_lang_context ();
7611 /* Special member functions [gram.special] */
7613 /* Parse a conversion-function-id.
7615 conversion-function-id:
7616 operator conversion-type-id
7618 Returns an IDENTIFIER_NODE representing the operator. */
7620 static tree
7621 cp_parser_conversion_function_id (cp_parser* parser)
7623 tree type;
7624 tree saved_scope;
7625 tree saved_qualifying_scope;
7626 tree saved_object_scope;
7627 tree pushed_scope = NULL_TREE;
7629 /* Look for the `operator' token. */
7630 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7631 return error_mark_node;
7632 /* When we parse the conversion-type-id, the current scope will be
7633 reset. However, we need that information in able to look up the
7634 conversion function later, so we save it here. */
7635 saved_scope = parser->scope;
7636 saved_qualifying_scope = parser->qualifying_scope;
7637 saved_object_scope = parser->object_scope;
7638 /* We must enter the scope of the class so that the names of
7639 entities declared within the class are available in the
7640 conversion-type-id. For example, consider:
7642 struct S {
7643 typedef int I;
7644 operator I();
7647 S::operator I() { ... }
7649 In order to see that `I' is a type-name in the definition, we
7650 must be in the scope of `S'. */
7651 if (saved_scope)
7652 pushed_scope = push_scope (saved_scope);
7653 /* Parse the conversion-type-id. */
7654 type = cp_parser_conversion_type_id (parser);
7655 /* Leave the scope of the class, if any. */
7656 if (pushed_scope)
7657 pop_scope (pushed_scope);
7658 /* Restore the saved scope. */
7659 parser->scope = saved_scope;
7660 parser->qualifying_scope = saved_qualifying_scope;
7661 parser->object_scope = saved_object_scope;
7662 /* If the TYPE is invalid, indicate failure. */
7663 if (type == error_mark_node)
7664 return error_mark_node;
7665 return mangle_conv_op_name_for_type (type);
7668 /* Parse a conversion-type-id:
7670 conversion-type-id:
7671 type-specifier-seq conversion-declarator [opt]
7673 Returns the TYPE specified. */
7675 static tree
7676 cp_parser_conversion_type_id (cp_parser* parser)
7678 tree attributes;
7679 cp_decl_specifier_seq type_specifiers;
7680 cp_declarator *declarator;
7681 tree type_specified;
7683 /* Parse the attributes. */
7684 attributes = cp_parser_attributes_opt (parser);
7685 /* Parse the type-specifiers. */
7686 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7687 &type_specifiers);
7688 /* If that didn't work, stop. */
7689 if (type_specifiers.type == error_mark_node)
7690 return error_mark_node;
7691 /* Parse the conversion-declarator. */
7692 declarator = cp_parser_conversion_declarator_opt (parser);
7694 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7695 /*initialized=*/0, &attributes);
7696 if (attributes)
7697 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7698 return type_specified;
7701 /* Parse an (optional) conversion-declarator.
7703 conversion-declarator:
7704 ptr-operator conversion-declarator [opt]
7708 static cp_declarator *
7709 cp_parser_conversion_declarator_opt (cp_parser* parser)
7711 enum tree_code code;
7712 tree class_type;
7713 cp_cv_quals cv_quals;
7715 /* We don't know if there's a ptr-operator next, or not. */
7716 cp_parser_parse_tentatively (parser);
7717 /* Try the ptr-operator. */
7718 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7719 /* If it worked, look for more conversion-declarators. */
7720 if (cp_parser_parse_definitely (parser))
7722 cp_declarator *declarator;
7724 /* Parse another optional declarator. */
7725 declarator = cp_parser_conversion_declarator_opt (parser);
7727 /* Create the representation of the declarator. */
7728 if (class_type)
7729 declarator = make_ptrmem_declarator (cv_quals, class_type,
7730 declarator);
7731 else if (code == INDIRECT_REF)
7732 declarator = make_pointer_declarator (cv_quals, declarator);
7733 else
7734 declarator = make_reference_declarator (cv_quals, declarator);
7736 return declarator;
7739 return NULL;
7742 /* Parse an (optional) ctor-initializer.
7744 ctor-initializer:
7745 : mem-initializer-list
7747 Returns TRUE iff the ctor-initializer was actually present. */
7749 static bool
7750 cp_parser_ctor_initializer_opt (cp_parser* parser)
7752 /* If the next token is not a `:', then there is no
7753 ctor-initializer. */
7754 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7756 /* Do default initialization of any bases and members. */
7757 if (DECL_CONSTRUCTOR_P (current_function_decl))
7758 finish_mem_initializers (NULL_TREE);
7760 return false;
7763 /* Consume the `:' token. */
7764 cp_lexer_consume_token (parser->lexer);
7765 /* And the mem-initializer-list. */
7766 cp_parser_mem_initializer_list (parser);
7768 return true;
7771 /* Parse a mem-initializer-list.
7773 mem-initializer-list:
7774 mem-initializer
7775 mem-initializer , mem-initializer-list */
7777 static void
7778 cp_parser_mem_initializer_list (cp_parser* parser)
7780 tree mem_initializer_list = NULL_TREE;
7782 /* Let the semantic analysis code know that we are starting the
7783 mem-initializer-list. */
7784 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7785 error ("only constructors take base initializers");
7787 /* Loop through the list. */
7788 while (true)
7790 tree mem_initializer;
7792 /* Parse the mem-initializer. */
7793 mem_initializer = cp_parser_mem_initializer (parser);
7794 /* Add it to the list, unless it was erroneous. */
7795 if (mem_initializer)
7797 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7798 mem_initializer_list = mem_initializer;
7800 /* If the next token is not a `,', we're done. */
7801 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7802 break;
7803 /* Consume the `,' token. */
7804 cp_lexer_consume_token (parser->lexer);
7807 /* Perform semantic analysis. */
7808 if (DECL_CONSTRUCTOR_P (current_function_decl))
7809 finish_mem_initializers (mem_initializer_list);
7812 /* Parse a mem-initializer.
7814 mem-initializer:
7815 mem-initializer-id ( expression-list [opt] )
7817 GNU extension:
7819 mem-initializer:
7820 ( expression-list [opt] )
7822 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7823 class) or FIELD_DECL (for a non-static data member) to initialize;
7824 the TREE_VALUE is the expression-list. */
7826 static tree
7827 cp_parser_mem_initializer (cp_parser* parser)
7829 tree mem_initializer_id;
7830 tree expression_list;
7831 tree member;
7833 /* Find out what is being initialized. */
7834 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7836 pedwarn ("anachronistic old-style base class initializer");
7837 mem_initializer_id = NULL_TREE;
7839 else
7840 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7841 member = expand_member_init (mem_initializer_id);
7842 if (member && !DECL_P (member))
7843 in_base_initializer = 1;
7845 expression_list
7846 = cp_parser_parenthesized_expression_list (parser, false,
7847 /*cast_p=*/false,
7848 /*non_constant_p=*/NULL);
7849 if (!expression_list)
7850 expression_list = void_type_node;
7852 in_base_initializer = 0;
7854 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7857 /* Parse a mem-initializer-id.
7859 mem-initializer-id:
7860 :: [opt] nested-name-specifier [opt] class-name
7861 identifier
7863 Returns a TYPE indicating the class to be initializer for the first
7864 production. Returns an IDENTIFIER_NODE indicating the data member
7865 to be initialized for the second production. */
7867 static tree
7868 cp_parser_mem_initializer_id (cp_parser* parser)
7870 bool global_scope_p;
7871 bool nested_name_specifier_p;
7872 bool template_p = false;
7873 tree id;
7875 /* `typename' is not allowed in this context ([temp.res]). */
7876 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7878 error ("keyword %<typename%> not allowed in this context (a qualified "
7879 "member initializer is implicitly a type)");
7880 cp_lexer_consume_token (parser->lexer);
7882 /* Look for the optional `::' operator. */
7883 global_scope_p
7884 = (cp_parser_global_scope_opt (parser,
7885 /*current_scope_valid_p=*/false)
7886 != NULL_TREE);
7887 /* Look for the optional nested-name-specifier. The simplest way to
7888 implement:
7890 [temp.res]
7892 The keyword `typename' is not permitted in a base-specifier or
7893 mem-initializer; in these contexts a qualified name that
7894 depends on a template-parameter is implicitly assumed to be a
7895 type name.
7897 is to assume that we have seen the `typename' keyword at this
7898 point. */
7899 nested_name_specifier_p
7900 = (cp_parser_nested_name_specifier_opt (parser,
7901 /*typename_keyword_p=*/true,
7902 /*check_dependency_p=*/true,
7903 /*type_p=*/true,
7904 /*is_declaration=*/true)
7905 != NULL_TREE);
7906 if (nested_name_specifier_p)
7907 template_p = cp_parser_optional_template_keyword (parser);
7908 /* If there is a `::' operator or a nested-name-specifier, then we
7909 are definitely looking for a class-name. */
7910 if (global_scope_p || nested_name_specifier_p)
7911 return cp_parser_class_name (parser,
7912 /*typename_keyword_p=*/true,
7913 /*template_keyword_p=*/template_p,
7914 none_type,
7915 /*check_dependency_p=*/true,
7916 /*class_head_p=*/false,
7917 /*is_declaration=*/true);
7918 /* Otherwise, we could also be looking for an ordinary identifier. */
7919 cp_parser_parse_tentatively (parser);
7920 /* Try a class-name. */
7921 id = cp_parser_class_name (parser,
7922 /*typename_keyword_p=*/true,
7923 /*template_keyword_p=*/false,
7924 none_type,
7925 /*check_dependency_p=*/true,
7926 /*class_head_p=*/false,
7927 /*is_declaration=*/true);
7928 /* If we found one, we're done. */
7929 if (cp_parser_parse_definitely (parser))
7930 return id;
7931 /* Otherwise, look for an ordinary identifier. */
7932 return cp_parser_identifier (parser);
7935 /* Overloading [gram.over] */
7937 /* Parse an operator-function-id.
7939 operator-function-id:
7940 operator operator
7942 Returns an IDENTIFIER_NODE for the operator which is a
7943 human-readable spelling of the identifier, e.g., `operator +'. */
7945 static tree
7946 cp_parser_operator_function_id (cp_parser* parser)
7948 /* Look for the `operator' keyword. */
7949 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7950 return error_mark_node;
7951 /* And then the name of the operator itself. */
7952 return cp_parser_operator (parser);
7955 /* Parse an operator.
7957 operator:
7958 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7959 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7960 || ++ -- , ->* -> () []
7962 GNU Extensions:
7964 operator:
7965 <? >? <?= >?=
7967 Returns an IDENTIFIER_NODE for the operator which is a
7968 human-readable spelling of the identifier, e.g., `operator +'. */
7970 static tree
7971 cp_parser_operator (cp_parser* parser)
7973 tree id = NULL_TREE;
7974 cp_token *token;
7976 /* Peek at the next token. */
7977 token = cp_lexer_peek_token (parser->lexer);
7978 /* Figure out which operator we have. */
7979 switch (token->type)
7981 case CPP_KEYWORD:
7983 enum tree_code op;
7985 /* The keyword should be either `new' or `delete'. */
7986 if (token->keyword == RID_NEW)
7987 op = NEW_EXPR;
7988 else if (token->keyword == RID_DELETE)
7989 op = DELETE_EXPR;
7990 else
7991 break;
7993 /* Consume the `new' or `delete' token. */
7994 cp_lexer_consume_token (parser->lexer);
7996 /* Peek at the next token. */
7997 token = cp_lexer_peek_token (parser->lexer);
7998 /* If it's a `[' token then this is the array variant of the
7999 operator. */
8000 if (token->type == CPP_OPEN_SQUARE)
8002 /* Consume the `[' token. */
8003 cp_lexer_consume_token (parser->lexer);
8004 /* Look for the `]' token. */
8005 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8006 id = ansi_opname (op == NEW_EXPR
8007 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8009 /* Otherwise, we have the non-array variant. */
8010 else
8011 id = ansi_opname (op);
8013 return id;
8016 case CPP_PLUS:
8017 id = ansi_opname (PLUS_EXPR);
8018 break;
8020 case CPP_MINUS:
8021 id = ansi_opname (MINUS_EXPR);
8022 break;
8024 case CPP_MULT:
8025 id = ansi_opname (MULT_EXPR);
8026 break;
8028 case CPP_DIV:
8029 id = ansi_opname (TRUNC_DIV_EXPR);
8030 break;
8032 case CPP_MOD:
8033 id = ansi_opname (TRUNC_MOD_EXPR);
8034 break;
8036 case CPP_XOR:
8037 id = ansi_opname (BIT_XOR_EXPR);
8038 break;
8040 case CPP_AND:
8041 id = ansi_opname (BIT_AND_EXPR);
8042 break;
8044 case CPP_OR:
8045 id = ansi_opname (BIT_IOR_EXPR);
8046 break;
8048 case CPP_COMPL:
8049 id = ansi_opname (BIT_NOT_EXPR);
8050 break;
8052 case CPP_NOT:
8053 id = ansi_opname (TRUTH_NOT_EXPR);
8054 break;
8056 case CPP_EQ:
8057 id = ansi_assopname (NOP_EXPR);
8058 break;
8060 case CPP_LESS:
8061 id = ansi_opname (LT_EXPR);
8062 break;
8064 case CPP_GREATER:
8065 id = ansi_opname (GT_EXPR);
8066 break;
8068 case CPP_PLUS_EQ:
8069 id = ansi_assopname (PLUS_EXPR);
8070 break;
8072 case CPP_MINUS_EQ:
8073 id = ansi_assopname (MINUS_EXPR);
8074 break;
8076 case CPP_MULT_EQ:
8077 id = ansi_assopname (MULT_EXPR);
8078 break;
8080 case CPP_DIV_EQ:
8081 id = ansi_assopname (TRUNC_DIV_EXPR);
8082 break;
8084 case CPP_MOD_EQ:
8085 id = ansi_assopname (TRUNC_MOD_EXPR);
8086 break;
8088 case CPP_XOR_EQ:
8089 id = ansi_assopname (BIT_XOR_EXPR);
8090 break;
8092 case CPP_AND_EQ:
8093 id = ansi_assopname (BIT_AND_EXPR);
8094 break;
8096 case CPP_OR_EQ:
8097 id = ansi_assopname (BIT_IOR_EXPR);
8098 break;
8100 case CPP_LSHIFT:
8101 id = ansi_opname (LSHIFT_EXPR);
8102 break;
8104 case CPP_RSHIFT:
8105 id = ansi_opname (RSHIFT_EXPR);
8106 break;
8108 case CPP_LSHIFT_EQ:
8109 id = ansi_assopname (LSHIFT_EXPR);
8110 break;
8112 case CPP_RSHIFT_EQ:
8113 id = ansi_assopname (RSHIFT_EXPR);
8114 break;
8116 case CPP_EQ_EQ:
8117 id = ansi_opname (EQ_EXPR);
8118 break;
8120 case CPP_NOT_EQ:
8121 id = ansi_opname (NE_EXPR);
8122 break;
8124 case CPP_LESS_EQ:
8125 id = ansi_opname (LE_EXPR);
8126 break;
8128 case CPP_GREATER_EQ:
8129 id = ansi_opname (GE_EXPR);
8130 break;
8132 case CPP_AND_AND:
8133 id = ansi_opname (TRUTH_ANDIF_EXPR);
8134 break;
8136 case CPP_OR_OR:
8137 id = ansi_opname (TRUTH_ORIF_EXPR);
8138 break;
8140 case CPP_PLUS_PLUS:
8141 id = ansi_opname (POSTINCREMENT_EXPR);
8142 break;
8144 case CPP_MINUS_MINUS:
8145 id = ansi_opname (PREDECREMENT_EXPR);
8146 break;
8148 case CPP_COMMA:
8149 id = ansi_opname (COMPOUND_EXPR);
8150 break;
8152 case CPP_DEREF_STAR:
8153 id = ansi_opname (MEMBER_REF);
8154 break;
8156 case CPP_DEREF:
8157 id = ansi_opname (COMPONENT_REF);
8158 break;
8160 case CPP_OPEN_PAREN:
8161 /* Consume the `('. */
8162 cp_lexer_consume_token (parser->lexer);
8163 /* Look for the matching `)'. */
8164 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8165 return ansi_opname (CALL_EXPR);
8167 case CPP_OPEN_SQUARE:
8168 /* Consume the `['. */
8169 cp_lexer_consume_token (parser->lexer);
8170 /* Look for the matching `]'. */
8171 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8172 return ansi_opname (ARRAY_REF);
8174 /* Extensions. */
8175 case CPP_MIN:
8176 id = ansi_opname (MIN_EXPR);
8177 cp_parser_warn_min_max ();
8178 break;
8180 case CPP_MAX:
8181 id = ansi_opname (MAX_EXPR);
8182 cp_parser_warn_min_max ();
8183 break;
8185 case CPP_MIN_EQ:
8186 id = ansi_assopname (MIN_EXPR);
8187 cp_parser_warn_min_max ();
8188 break;
8190 case CPP_MAX_EQ:
8191 id = ansi_assopname (MAX_EXPR);
8192 cp_parser_warn_min_max ();
8193 break;
8195 default:
8196 /* Anything else is an error. */
8197 break;
8200 /* If we have selected an identifier, we need to consume the
8201 operator token. */
8202 if (id)
8203 cp_lexer_consume_token (parser->lexer);
8204 /* Otherwise, no valid operator name was present. */
8205 else
8207 cp_parser_error (parser, "expected operator");
8208 id = error_mark_node;
8211 return id;
8214 /* Parse a template-declaration.
8216 template-declaration:
8217 export [opt] template < template-parameter-list > declaration
8219 If MEMBER_P is TRUE, this template-declaration occurs within a
8220 class-specifier.
8222 The grammar rule given by the standard isn't correct. What
8223 is really meant is:
8225 template-declaration:
8226 export [opt] template-parameter-list-seq
8227 decl-specifier-seq [opt] init-declarator [opt] ;
8228 export [opt] template-parameter-list-seq
8229 function-definition
8231 template-parameter-list-seq:
8232 template-parameter-list-seq [opt]
8233 template < template-parameter-list > */
8235 static void
8236 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8238 /* Check for `export'. */
8239 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8241 /* Consume the `export' token. */
8242 cp_lexer_consume_token (parser->lexer);
8243 /* Warn that we do not support `export'. */
8244 warning (0, "keyword %<export%> not implemented, and will be ignored");
8247 cp_parser_template_declaration_after_export (parser, member_p);
8250 /* Parse a template-parameter-list.
8252 template-parameter-list:
8253 template-parameter
8254 template-parameter-list , template-parameter
8256 Returns a TREE_LIST. Each node represents a template parameter.
8257 The nodes are connected via their TREE_CHAINs. */
8259 static tree
8260 cp_parser_template_parameter_list (cp_parser* parser)
8262 tree parameter_list = NULL_TREE;
8264 while (true)
8266 tree parameter;
8267 cp_token *token;
8268 bool is_non_type;
8270 /* Parse the template-parameter. */
8271 parameter = cp_parser_template_parameter (parser, &is_non_type);
8272 /* Add it to the list. */
8273 if (parameter != error_mark_node)
8274 parameter_list = process_template_parm (parameter_list,
8275 parameter,
8276 is_non_type);
8277 /* Peek at the next token. */
8278 token = cp_lexer_peek_token (parser->lexer);
8279 /* If it's not a `,', we're done. */
8280 if (token->type != CPP_COMMA)
8281 break;
8282 /* Otherwise, consume the `,' token. */
8283 cp_lexer_consume_token (parser->lexer);
8286 return parameter_list;
8289 /* Parse a template-parameter.
8291 template-parameter:
8292 type-parameter
8293 parameter-declaration
8295 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8296 the parameter. The TREE_PURPOSE is the default value, if any.
8297 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8298 iff this parameter is a non-type parameter. */
8300 static tree
8301 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8303 cp_token *token;
8304 cp_parameter_declarator *parameter_declarator;
8305 tree parm;
8307 /* Assume it is a type parameter or a template parameter. */
8308 *is_non_type = false;
8309 /* Peek at the next token. */
8310 token = cp_lexer_peek_token (parser->lexer);
8311 /* If it is `class' or `template', we have a type-parameter. */
8312 if (token->keyword == RID_TEMPLATE)
8313 return cp_parser_type_parameter (parser);
8314 /* If it is `class' or `typename' we do not know yet whether it is a
8315 type parameter or a non-type parameter. Consider:
8317 template <typename T, typename T::X X> ...
8321 template <class C, class D*> ...
8323 Here, the first parameter is a type parameter, and the second is
8324 a non-type parameter. We can tell by looking at the token after
8325 the identifier -- if it is a `,', `=', or `>' then we have a type
8326 parameter. */
8327 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8329 /* Peek at the token after `class' or `typename'. */
8330 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8331 /* If it's an identifier, skip it. */
8332 if (token->type == CPP_NAME)
8333 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8334 /* Now, see if the token looks like the end of a template
8335 parameter. */
8336 if (token->type == CPP_COMMA
8337 || token->type == CPP_EQ
8338 || token->type == CPP_GREATER)
8339 return cp_parser_type_parameter (parser);
8342 /* Otherwise, it is a non-type parameter.
8344 [temp.param]
8346 When parsing a default template-argument for a non-type
8347 template-parameter, the first non-nested `>' is taken as the end
8348 of the template parameter-list rather than a greater-than
8349 operator. */
8350 *is_non_type = true;
8351 parameter_declarator
8352 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8353 /*parenthesized_p=*/NULL);
8354 parm = grokdeclarator (parameter_declarator->declarator,
8355 &parameter_declarator->decl_specifiers,
8356 PARM, /*initialized=*/0,
8357 /*attrlist=*/NULL);
8358 if (parm == error_mark_node)
8359 return error_mark_node;
8360 return build_tree_list (parameter_declarator->default_argument, parm);
8363 /* Parse a type-parameter.
8365 type-parameter:
8366 class identifier [opt]
8367 class identifier [opt] = type-id
8368 typename identifier [opt]
8369 typename identifier [opt] = type-id
8370 template < template-parameter-list > class identifier [opt]
8371 template < template-parameter-list > class identifier [opt]
8372 = id-expression
8374 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8375 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8376 the declaration of the parameter. */
8378 static tree
8379 cp_parser_type_parameter (cp_parser* parser)
8381 cp_token *token;
8382 tree parameter;
8384 /* Look for a keyword to tell us what kind of parameter this is. */
8385 token = cp_parser_require (parser, CPP_KEYWORD,
8386 "`class', `typename', or `template'");
8387 if (!token)
8388 return error_mark_node;
8390 switch (token->keyword)
8392 case RID_CLASS:
8393 case RID_TYPENAME:
8395 tree identifier;
8396 tree default_argument;
8398 /* If the next token is an identifier, then it names the
8399 parameter. */
8400 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8401 identifier = cp_parser_identifier (parser);
8402 else
8403 identifier = NULL_TREE;
8405 /* Create the parameter. */
8406 parameter = finish_template_type_parm (class_type_node, identifier);
8408 /* If the next token is an `=', we have a default argument. */
8409 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8411 /* Consume the `=' token. */
8412 cp_lexer_consume_token (parser->lexer);
8413 /* Parse the default-argument. */
8414 default_argument = cp_parser_type_id (parser);
8416 else
8417 default_argument = NULL_TREE;
8419 /* Create the combined representation of the parameter and the
8420 default argument. */
8421 parameter = build_tree_list (default_argument, parameter);
8423 break;
8425 case RID_TEMPLATE:
8427 tree parameter_list;
8428 tree identifier;
8429 tree default_argument;
8431 /* Look for the `<'. */
8432 cp_parser_require (parser, CPP_LESS, "`<'");
8433 /* Parse the template-parameter-list. */
8434 begin_template_parm_list ();
8435 parameter_list
8436 = cp_parser_template_parameter_list (parser);
8437 parameter_list = end_template_parm_list (parameter_list);
8438 /* Look for the `>'. */
8439 cp_parser_require (parser, CPP_GREATER, "`>'");
8440 /* Look for the `class' keyword. */
8441 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8442 /* If the next token is an `=', then there is a
8443 default-argument. If the next token is a `>', we are at
8444 the end of the parameter-list. If the next token is a `,',
8445 then we are at the end of this parameter. */
8446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8447 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8448 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8450 identifier = cp_parser_identifier (parser);
8451 /* Treat invalid names as if the parameter were nameless. */
8452 if (identifier == error_mark_node)
8453 identifier = NULL_TREE;
8455 else
8456 identifier = NULL_TREE;
8458 /* Create the template parameter. */
8459 parameter = finish_template_template_parm (class_type_node,
8460 identifier);
8462 /* If the next token is an `=', then there is a
8463 default-argument. */
8464 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8466 bool is_template;
8468 /* Consume the `='. */
8469 cp_lexer_consume_token (parser->lexer);
8470 /* Parse the id-expression. */
8471 default_argument
8472 = cp_parser_id_expression (parser,
8473 /*template_keyword_p=*/false,
8474 /*check_dependency_p=*/true,
8475 /*template_p=*/&is_template,
8476 /*declarator_p=*/false);
8477 if (TREE_CODE (default_argument) == TYPE_DECL)
8478 /* If the id-expression was a template-id that refers to
8479 a template-class, we already have the declaration here,
8480 so no further lookup is needed. */
8482 else
8483 /* Look up the name. */
8484 default_argument
8485 = cp_parser_lookup_name (parser, default_argument,
8486 none_type,
8487 /*is_template=*/is_template,
8488 /*is_namespace=*/false,
8489 /*check_dependency=*/true,
8490 /*ambiguous_decls=*/NULL);
8491 /* See if the default argument is valid. */
8492 default_argument
8493 = check_template_template_default_arg (default_argument);
8495 else
8496 default_argument = NULL_TREE;
8498 /* Create the combined representation of the parameter and the
8499 default argument. */
8500 parameter = build_tree_list (default_argument, parameter);
8502 break;
8504 default:
8505 gcc_unreachable ();
8506 break;
8509 return parameter;
8512 /* Parse a template-id.
8514 template-id:
8515 template-name < template-argument-list [opt] >
8517 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8518 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8519 returned. Otherwise, if the template-name names a function, or set
8520 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8521 names a class, returns a TYPE_DECL for the specialization.
8523 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8524 uninstantiated templates. */
8526 static tree
8527 cp_parser_template_id (cp_parser *parser,
8528 bool template_keyword_p,
8529 bool check_dependency_p,
8530 bool is_declaration)
8532 tree template;
8533 tree arguments;
8534 tree template_id;
8535 cp_token_position start_of_id = 0;
8536 tree access_check = NULL_TREE;
8537 cp_token *next_token, *next_token_2;
8538 bool is_identifier;
8540 /* If the next token corresponds to a template-id, there is no need
8541 to reparse it. */
8542 next_token = cp_lexer_peek_token (parser->lexer);
8543 if (next_token->type == CPP_TEMPLATE_ID)
8545 tree value;
8546 tree check;
8548 /* Get the stored value. */
8549 value = cp_lexer_consume_token (parser->lexer)->value;
8550 /* Perform any access checks that were deferred. */
8551 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8552 perform_or_defer_access_check (TREE_PURPOSE (check),
8553 TREE_VALUE (check));
8554 /* Return the stored value. */
8555 return TREE_VALUE (value);
8558 /* Avoid performing name lookup if there is no possibility of
8559 finding a template-id. */
8560 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8561 || (next_token->type == CPP_NAME
8562 && !cp_parser_nth_token_starts_template_argument_list_p
8563 (parser, 2)))
8565 cp_parser_error (parser, "expected template-id");
8566 return error_mark_node;
8569 /* Remember where the template-id starts. */
8570 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8571 start_of_id = cp_lexer_token_position (parser->lexer, false);
8573 push_deferring_access_checks (dk_deferred);
8575 /* Parse the template-name. */
8576 is_identifier = false;
8577 template = cp_parser_template_name (parser, template_keyword_p,
8578 check_dependency_p,
8579 is_declaration,
8580 &is_identifier);
8581 if (template == error_mark_node || is_identifier)
8583 pop_deferring_access_checks ();
8584 return template;
8587 /* If we find the sequence `[:' after a template-name, it's probably
8588 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8589 parse correctly the argument list. */
8590 next_token = cp_lexer_peek_token (parser->lexer);
8591 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8592 if (next_token->type == CPP_OPEN_SQUARE
8593 && next_token->flags & DIGRAPH
8594 && next_token_2->type == CPP_COLON
8595 && !(next_token_2->flags & PREV_WHITE))
8597 cp_parser_parse_tentatively (parser);
8598 /* Change `:' into `::'. */
8599 next_token_2->type = CPP_SCOPE;
8600 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8601 CPP_LESS. */
8602 cp_lexer_consume_token (parser->lexer);
8603 /* Parse the arguments. */
8604 arguments = cp_parser_enclosed_template_argument_list (parser);
8605 if (!cp_parser_parse_definitely (parser))
8607 /* If we couldn't parse an argument list, then we revert our changes
8608 and return simply an error. Maybe this is not a template-id
8609 after all. */
8610 next_token_2->type = CPP_COLON;
8611 cp_parser_error (parser, "expected %<<%>");
8612 pop_deferring_access_checks ();
8613 return error_mark_node;
8615 /* Otherwise, emit an error about the invalid digraph, but continue
8616 parsing because we got our argument list. */
8617 pedwarn ("%<<::%> cannot begin a template-argument list");
8618 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8619 "between %<<%> and %<::%>");
8620 if (!flag_permissive)
8622 static bool hint;
8623 if (!hint)
8625 inform ("(if you use -fpermissive G++ will accept your code)");
8626 hint = true;
8630 else
8632 /* Look for the `<' that starts the template-argument-list. */
8633 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8635 pop_deferring_access_checks ();
8636 return error_mark_node;
8638 /* Parse the arguments. */
8639 arguments = cp_parser_enclosed_template_argument_list (parser);
8642 /* Build a representation of the specialization. */
8643 if (TREE_CODE (template) == IDENTIFIER_NODE)
8644 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8645 else if (DECL_CLASS_TEMPLATE_P (template)
8646 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8647 template_id
8648 = finish_template_type (template, arguments,
8649 cp_lexer_next_token_is (parser->lexer,
8650 CPP_SCOPE));
8651 else
8653 /* If it's not a class-template or a template-template, it should be
8654 a function-template. */
8655 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8656 || TREE_CODE (template) == OVERLOAD
8657 || BASELINK_P (template)));
8659 template_id = lookup_template_function (template, arguments);
8662 /* Retrieve any deferred checks. Do not pop this access checks yet
8663 so the memory will not be reclaimed during token replacing below. */
8664 access_check = get_deferred_access_checks ();
8666 /* If parsing tentatively, replace the sequence of tokens that makes
8667 up the template-id with a CPP_TEMPLATE_ID token. That way,
8668 should we re-parse the token stream, we will not have to repeat
8669 the effort required to do the parse, nor will we issue duplicate
8670 error messages about problems during instantiation of the
8671 template. */
8672 if (start_of_id)
8674 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8676 /* Reset the contents of the START_OF_ID token. */
8677 token->type = CPP_TEMPLATE_ID;
8678 token->value = build_tree_list (access_check, template_id);
8679 token->keyword = RID_MAX;
8681 /* Purge all subsequent tokens. */
8682 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8684 /* ??? Can we actually assume that, if template_id ==
8685 error_mark_node, we will have issued a diagnostic to the
8686 user, as opposed to simply marking the tentative parse as
8687 failed? */
8688 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8689 error ("parse error in template argument list");
8692 pop_deferring_access_checks ();
8693 return template_id;
8696 /* Parse a template-name.
8698 template-name:
8699 identifier
8701 The standard should actually say:
8703 template-name:
8704 identifier
8705 operator-function-id
8707 A defect report has been filed about this issue.
8709 A conversion-function-id cannot be a template name because they cannot
8710 be part of a template-id. In fact, looking at this code:
8712 a.operator K<int>()
8714 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8715 It is impossible to call a templated conversion-function-id with an
8716 explicit argument list, since the only allowed template parameter is
8717 the type to which it is converting.
8719 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8720 `template' keyword, in a construction like:
8722 T::template f<3>()
8724 In that case `f' is taken to be a template-name, even though there
8725 is no way of knowing for sure.
8727 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8728 name refers to a set of overloaded functions, at least one of which
8729 is a template, or an IDENTIFIER_NODE with the name of the template,
8730 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8731 names are looked up inside uninstantiated templates. */
8733 static tree
8734 cp_parser_template_name (cp_parser* parser,
8735 bool template_keyword_p,
8736 bool check_dependency_p,
8737 bool is_declaration,
8738 bool *is_identifier)
8740 tree identifier;
8741 tree decl;
8742 tree fns;
8744 /* If the next token is `operator', then we have either an
8745 operator-function-id or a conversion-function-id. */
8746 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8748 /* We don't know whether we're looking at an
8749 operator-function-id or a conversion-function-id. */
8750 cp_parser_parse_tentatively (parser);
8751 /* Try an operator-function-id. */
8752 identifier = cp_parser_operator_function_id (parser);
8753 /* If that didn't work, try a conversion-function-id. */
8754 if (!cp_parser_parse_definitely (parser))
8756 cp_parser_error (parser, "expected template-name");
8757 return error_mark_node;
8760 /* Look for the identifier. */
8761 else
8762 identifier = cp_parser_identifier (parser);
8764 /* If we didn't find an identifier, we don't have a template-id. */
8765 if (identifier == error_mark_node)
8766 return error_mark_node;
8768 /* If the name immediately followed the `template' keyword, then it
8769 is a template-name. However, if the next token is not `<', then
8770 we do not treat it as a template-name, since it is not being used
8771 as part of a template-id. This enables us to handle constructs
8772 like:
8774 template <typename T> struct S { S(); };
8775 template <typename T> S<T>::S();
8777 correctly. We would treat `S' as a template -- if it were `S<T>'
8778 -- but we do not if there is no `<'. */
8780 if (processing_template_decl
8781 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8783 /* In a declaration, in a dependent context, we pretend that the
8784 "template" keyword was present in order to improve error
8785 recovery. For example, given:
8787 template <typename T> void f(T::X<int>);
8789 we want to treat "X<int>" as a template-id. */
8790 if (is_declaration
8791 && !template_keyword_p
8792 && parser->scope && TYPE_P (parser->scope)
8793 && check_dependency_p
8794 && dependent_type_p (parser->scope)
8795 /* Do not do this for dtors (or ctors), since they never
8796 need the template keyword before their name. */
8797 && !constructor_name_p (identifier, parser->scope))
8799 cp_token_position start = 0;
8801 /* Explain what went wrong. */
8802 error ("non-template %qD used as template", identifier);
8803 inform ("use %<%T::template %D%> to indicate that it is a template",
8804 parser->scope, identifier);
8805 /* If parsing tentatively, find the location of the "<" token. */
8806 if (cp_parser_simulate_error (parser))
8807 start = cp_lexer_token_position (parser->lexer, true);
8808 /* Parse the template arguments so that we can issue error
8809 messages about them. */
8810 cp_lexer_consume_token (parser->lexer);
8811 cp_parser_enclosed_template_argument_list (parser);
8812 /* Skip tokens until we find a good place from which to
8813 continue parsing. */
8814 cp_parser_skip_to_closing_parenthesis (parser,
8815 /*recovering=*/true,
8816 /*or_comma=*/true,
8817 /*consume_paren=*/false);
8818 /* If parsing tentatively, permanently remove the
8819 template argument list. That will prevent duplicate
8820 error messages from being issued about the missing
8821 "template" keyword. */
8822 if (start)
8823 cp_lexer_purge_tokens_after (parser->lexer, start);
8824 if (is_identifier)
8825 *is_identifier = true;
8826 return identifier;
8829 /* If the "template" keyword is present, then there is generally
8830 no point in doing name-lookup, so we just return IDENTIFIER.
8831 But, if the qualifying scope is non-dependent then we can
8832 (and must) do name-lookup normally. */
8833 if (template_keyword_p
8834 && (!parser->scope
8835 || (TYPE_P (parser->scope)
8836 && dependent_type_p (parser->scope))))
8837 return identifier;
8840 /* Look up the name. */
8841 decl = cp_parser_lookup_name (parser, identifier,
8842 none_type,
8843 /*is_template=*/false,
8844 /*is_namespace=*/false,
8845 check_dependency_p,
8846 /*ambiguous_decls=*/NULL);
8847 decl = maybe_get_template_decl_from_type_decl (decl);
8849 /* If DECL is a template, then the name was a template-name. */
8850 if (TREE_CODE (decl) == TEMPLATE_DECL)
8852 else
8854 tree fn = NULL_TREE;
8856 /* The standard does not explicitly indicate whether a name that
8857 names a set of overloaded declarations, some of which are
8858 templates, is a template-name. However, such a name should
8859 be a template-name; otherwise, there is no way to form a
8860 template-id for the overloaded templates. */
8861 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8862 if (TREE_CODE (fns) == OVERLOAD)
8863 for (fn = fns; fn; fn = OVL_NEXT (fn))
8864 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8865 break;
8867 if (!fn)
8869 /* The name does not name a template. */
8870 cp_parser_error (parser, "expected template-name");
8871 return error_mark_node;
8875 /* If DECL is dependent, and refers to a function, then just return
8876 its name; we will look it up again during template instantiation. */
8877 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8879 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8880 if (TYPE_P (scope) && dependent_type_p (scope))
8881 return identifier;
8884 return decl;
8887 /* Parse a template-argument-list.
8889 template-argument-list:
8890 template-argument
8891 template-argument-list , template-argument
8893 Returns a TREE_VEC containing the arguments. */
8895 static tree
8896 cp_parser_template_argument_list (cp_parser* parser)
8898 tree fixed_args[10];
8899 unsigned n_args = 0;
8900 unsigned alloced = 10;
8901 tree *arg_ary = fixed_args;
8902 tree vec;
8903 bool saved_in_template_argument_list_p;
8904 bool saved_ice_p;
8905 bool saved_non_ice_p;
8907 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8908 parser->in_template_argument_list_p = true;
8909 /* Even if the template-id appears in an integral
8910 constant-expression, the contents of the argument list do
8911 not. */
8912 saved_ice_p = parser->integral_constant_expression_p;
8913 parser->integral_constant_expression_p = false;
8914 saved_non_ice_p = parser->non_integral_constant_expression_p;
8915 parser->non_integral_constant_expression_p = false;
8916 /* Parse the arguments. */
8919 tree argument;
8921 if (n_args)
8922 /* Consume the comma. */
8923 cp_lexer_consume_token (parser->lexer);
8925 /* Parse the template-argument. */
8926 argument = cp_parser_template_argument (parser);
8927 if (n_args == alloced)
8929 alloced *= 2;
8931 if (arg_ary == fixed_args)
8933 arg_ary = XNEWVEC (tree, alloced);
8934 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8936 else
8937 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
8939 arg_ary[n_args++] = argument;
8941 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8943 vec = make_tree_vec (n_args);
8945 while (n_args--)
8946 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8948 if (arg_ary != fixed_args)
8949 free (arg_ary);
8950 parser->non_integral_constant_expression_p = saved_non_ice_p;
8951 parser->integral_constant_expression_p = saved_ice_p;
8952 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8953 return vec;
8956 /* Parse a template-argument.
8958 template-argument:
8959 assignment-expression
8960 type-id
8961 id-expression
8963 The representation is that of an assignment-expression, type-id, or
8964 id-expression -- except that the qualified id-expression is
8965 evaluated, so that the value returned is either a DECL or an
8966 OVERLOAD.
8968 Although the standard says "assignment-expression", it forbids
8969 throw-expressions or assignments in the template argument.
8970 Therefore, we use "conditional-expression" instead. */
8972 static tree
8973 cp_parser_template_argument (cp_parser* parser)
8975 tree argument;
8976 bool template_p;
8977 bool address_p;
8978 bool maybe_type_id = false;
8979 cp_token *token;
8980 cp_id_kind idk;
8982 /* There's really no way to know what we're looking at, so we just
8983 try each alternative in order.
8985 [temp.arg]
8987 In a template-argument, an ambiguity between a type-id and an
8988 expression is resolved to a type-id, regardless of the form of
8989 the corresponding template-parameter.
8991 Therefore, we try a type-id first. */
8992 cp_parser_parse_tentatively (parser);
8993 argument = cp_parser_type_id (parser);
8994 /* If there was no error parsing the type-id but the next token is a '>>',
8995 we probably found a typo for '> >'. But there are type-id which are
8996 also valid expressions. For instance:
8998 struct X { int operator >> (int); };
8999 template <int V> struct Foo {};
9000 Foo<X () >> 5> r;
9002 Here 'X()' is a valid type-id of a function type, but the user just
9003 wanted to write the expression "X() >> 5". Thus, we remember that we
9004 found a valid type-id, but we still try to parse the argument as an
9005 expression to see what happens. */
9006 if (!cp_parser_error_occurred (parser)
9007 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9009 maybe_type_id = true;
9010 cp_parser_abort_tentative_parse (parser);
9012 else
9014 /* If the next token isn't a `,' or a `>', then this argument wasn't
9015 really finished. This means that the argument is not a valid
9016 type-id. */
9017 if (!cp_parser_next_token_ends_template_argument_p (parser))
9018 cp_parser_error (parser, "expected template-argument");
9019 /* If that worked, we're done. */
9020 if (cp_parser_parse_definitely (parser))
9021 return argument;
9023 /* We're still not sure what the argument will be. */
9024 cp_parser_parse_tentatively (parser);
9025 /* Try a template. */
9026 argument = cp_parser_id_expression (parser,
9027 /*template_keyword_p=*/false,
9028 /*check_dependency_p=*/true,
9029 &template_p,
9030 /*declarator_p=*/false);
9031 /* If the next token isn't a `,' or a `>', then this argument wasn't
9032 really finished. */
9033 if (!cp_parser_next_token_ends_template_argument_p (parser))
9034 cp_parser_error (parser, "expected template-argument");
9035 if (!cp_parser_error_occurred (parser))
9037 /* Figure out what is being referred to. If the id-expression
9038 was for a class template specialization, then we will have a
9039 TYPE_DECL at this point. There is no need to do name lookup
9040 at this point in that case. */
9041 if (TREE_CODE (argument) != TYPE_DECL)
9042 argument = cp_parser_lookup_name (parser, argument,
9043 none_type,
9044 /*is_template=*/template_p,
9045 /*is_namespace=*/false,
9046 /*check_dependency=*/true,
9047 /*ambiguous_decls=*/NULL);
9048 if (TREE_CODE (argument) != TEMPLATE_DECL
9049 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9050 cp_parser_error (parser, "expected template-name");
9052 if (cp_parser_parse_definitely (parser))
9053 return argument;
9054 /* It must be a non-type argument. There permitted cases are given
9055 in [temp.arg.nontype]:
9057 -- an integral constant-expression of integral or enumeration
9058 type; or
9060 -- the name of a non-type template-parameter; or
9062 -- the name of an object or function with external linkage...
9064 -- the address of an object or function with external linkage...
9066 -- a pointer to member... */
9067 /* Look for a non-type template parameter. */
9068 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9070 cp_parser_parse_tentatively (parser);
9071 argument = cp_parser_primary_expression (parser,
9072 /*adress_p=*/false,
9073 /*cast_p=*/false,
9074 /*template_arg_p=*/true,
9075 &idk);
9076 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9077 || !cp_parser_next_token_ends_template_argument_p (parser))
9078 cp_parser_simulate_error (parser);
9079 if (cp_parser_parse_definitely (parser))
9080 return argument;
9083 /* If the next token is "&", the argument must be the address of an
9084 object or function with external linkage. */
9085 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9086 if (address_p)
9087 cp_lexer_consume_token (parser->lexer);
9088 /* See if we might have an id-expression. */
9089 token = cp_lexer_peek_token (parser->lexer);
9090 if (token->type == CPP_NAME
9091 || token->keyword == RID_OPERATOR
9092 || token->type == CPP_SCOPE
9093 || token->type == CPP_TEMPLATE_ID
9094 || token->type == CPP_NESTED_NAME_SPECIFIER)
9096 cp_parser_parse_tentatively (parser);
9097 argument = cp_parser_primary_expression (parser,
9098 address_p,
9099 /*cast_p=*/false,
9100 /*template_arg_p=*/true,
9101 &idk);
9102 if (cp_parser_error_occurred (parser)
9103 || !cp_parser_next_token_ends_template_argument_p (parser))
9104 cp_parser_abort_tentative_parse (parser);
9105 else
9107 if (TREE_CODE (argument) == INDIRECT_REF)
9109 gcc_assert (REFERENCE_REF_P (argument));
9110 argument = TREE_OPERAND (argument, 0);
9113 if (TREE_CODE (argument) == BASELINK)
9114 /* We don't need the information about what class was used
9115 to name the overloaded functions. */
9116 argument = BASELINK_FUNCTIONS (argument);
9118 if (TREE_CODE (argument) == VAR_DECL)
9120 /* A variable without external linkage might still be a
9121 valid constant-expression, so no error is issued here
9122 if the external-linkage check fails. */
9123 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9124 cp_parser_simulate_error (parser);
9126 else if (is_overloaded_fn (argument))
9127 /* All overloaded functions are allowed; if the external
9128 linkage test does not pass, an error will be issued
9129 later. */
9131 else if (address_p
9132 && (TREE_CODE (argument) == OFFSET_REF
9133 || TREE_CODE (argument) == SCOPE_REF))
9134 /* A pointer-to-member. */
9136 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9138 else
9139 cp_parser_simulate_error (parser);
9141 if (cp_parser_parse_definitely (parser))
9143 if (address_p)
9144 argument = build_x_unary_op (ADDR_EXPR, argument);
9145 return argument;
9149 /* If the argument started with "&", there are no other valid
9150 alternatives at this point. */
9151 if (address_p)
9153 cp_parser_error (parser, "invalid non-type template argument");
9154 return error_mark_node;
9157 /* If the argument wasn't successfully parsed as a type-id followed
9158 by '>>', the argument can only be a constant expression now.
9159 Otherwise, we try parsing the constant-expression tentatively,
9160 because the argument could really be a type-id. */
9161 if (maybe_type_id)
9162 cp_parser_parse_tentatively (parser);
9163 argument = cp_parser_constant_expression (parser,
9164 /*allow_non_constant_p=*/false,
9165 /*non_constant_p=*/NULL);
9166 argument = fold_non_dependent_expr (argument);
9167 if (!maybe_type_id)
9168 return argument;
9169 if (!cp_parser_next_token_ends_template_argument_p (parser))
9170 cp_parser_error (parser, "expected template-argument");
9171 if (cp_parser_parse_definitely (parser))
9172 return argument;
9173 /* We did our best to parse the argument as a non type-id, but that
9174 was the only alternative that matched (albeit with a '>' after
9175 it). We can assume it's just a typo from the user, and a
9176 diagnostic will then be issued. */
9177 return cp_parser_type_id (parser);
9180 /* Parse an explicit-instantiation.
9182 explicit-instantiation:
9183 template declaration
9185 Although the standard says `declaration', what it really means is:
9187 explicit-instantiation:
9188 template decl-specifier-seq [opt] declarator [opt] ;
9190 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9191 supposed to be allowed. A defect report has been filed about this
9192 issue.
9194 GNU Extension:
9196 explicit-instantiation:
9197 storage-class-specifier template
9198 decl-specifier-seq [opt] declarator [opt] ;
9199 function-specifier template
9200 decl-specifier-seq [opt] declarator [opt] ; */
9202 static void
9203 cp_parser_explicit_instantiation (cp_parser* parser)
9205 int declares_class_or_enum;
9206 cp_decl_specifier_seq decl_specifiers;
9207 tree extension_specifier = NULL_TREE;
9209 /* Look for an (optional) storage-class-specifier or
9210 function-specifier. */
9211 if (cp_parser_allow_gnu_extensions_p (parser))
9213 extension_specifier
9214 = cp_parser_storage_class_specifier_opt (parser);
9215 if (!extension_specifier)
9216 extension_specifier
9217 = cp_parser_function_specifier_opt (parser,
9218 /*decl_specs=*/NULL);
9221 /* Look for the `template' keyword. */
9222 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9223 /* Let the front end know that we are processing an explicit
9224 instantiation. */
9225 begin_explicit_instantiation ();
9226 /* [temp.explicit] says that we are supposed to ignore access
9227 control while processing explicit instantiation directives. */
9228 push_deferring_access_checks (dk_no_check);
9229 /* Parse a decl-specifier-seq. */
9230 cp_parser_decl_specifier_seq (parser,
9231 CP_PARSER_FLAGS_OPTIONAL,
9232 &decl_specifiers,
9233 &declares_class_or_enum);
9234 /* If there was exactly one decl-specifier, and it declared a class,
9235 and there's no declarator, then we have an explicit type
9236 instantiation. */
9237 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9239 tree type;
9241 type = check_tag_decl (&decl_specifiers);
9242 /* Turn access control back on for names used during
9243 template instantiation. */
9244 pop_deferring_access_checks ();
9245 if (type)
9246 do_type_instantiation (type, extension_specifier,
9247 /*complain=*/tf_error);
9249 else
9251 cp_declarator *declarator;
9252 tree decl;
9254 /* Parse the declarator. */
9255 declarator
9256 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9257 /*ctor_dtor_or_conv_p=*/NULL,
9258 /*parenthesized_p=*/NULL,
9259 /*member_p=*/false);
9260 if (declares_class_or_enum & 2)
9261 cp_parser_check_for_definition_in_return_type (declarator,
9262 decl_specifiers.type);
9263 if (declarator != cp_error_declarator)
9265 decl = grokdeclarator (declarator, &decl_specifiers,
9266 NORMAL, 0, NULL);
9267 /* Turn access control back on for names used during
9268 template instantiation. */
9269 pop_deferring_access_checks ();
9270 /* Do the explicit instantiation. */
9271 do_decl_instantiation (decl, extension_specifier);
9273 else
9275 pop_deferring_access_checks ();
9276 /* Skip the body of the explicit instantiation. */
9277 cp_parser_skip_to_end_of_statement (parser);
9280 /* We're done with the instantiation. */
9281 end_explicit_instantiation ();
9283 cp_parser_consume_semicolon_at_end_of_statement (parser);
9286 /* Parse an explicit-specialization.
9288 explicit-specialization:
9289 template < > declaration
9291 Although the standard says `declaration', what it really means is:
9293 explicit-specialization:
9294 template <> decl-specifier [opt] init-declarator [opt] ;
9295 template <> function-definition
9296 template <> explicit-specialization
9297 template <> template-declaration */
9299 static void
9300 cp_parser_explicit_specialization (cp_parser* parser)
9302 bool need_lang_pop;
9303 /* Look for the `template' keyword. */
9304 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9305 /* Look for the `<'. */
9306 cp_parser_require (parser, CPP_LESS, "`<'");
9307 /* Look for the `>'. */
9308 cp_parser_require (parser, CPP_GREATER, "`>'");
9309 /* We have processed another parameter list. */
9310 ++parser->num_template_parameter_lists;
9311 /* [temp]
9313 A template ... explicit specialization ... shall not have C
9314 linkage. */
9315 if (current_lang_name == lang_name_c)
9317 error ("template specialization with C linkage");
9318 /* Give it C++ linkage to avoid confusing other parts of the
9319 front end. */
9320 push_lang_context (lang_name_cplusplus);
9321 need_lang_pop = true;
9323 else
9324 need_lang_pop = false;
9325 /* Let the front end know that we are beginning a specialization. */
9326 begin_specialization ();
9327 /* If the next keyword is `template', we need to figure out whether
9328 or not we're looking a template-declaration. */
9329 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9331 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9332 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9333 cp_parser_template_declaration_after_export (parser,
9334 /*member_p=*/false);
9335 else
9336 cp_parser_explicit_specialization (parser);
9338 else
9339 /* Parse the dependent declaration. */
9340 cp_parser_single_declaration (parser,
9341 /*member_p=*/false,
9342 /*friend_p=*/NULL);
9343 /* We're done with the specialization. */
9344 end_specialization ();
9345 /* For the erroneous case of a template with C linkage, we pushed an
9346 implicit C++ linkage scope; exit that scope now. */
9347 if (need_lang_pop)
9348 pop_lang_context ();
9349 /* We're done with this parameter list. */
9350 --parser->num_template_parameter_lists;
9353 /* Parse a type-specifier.
9355 type-specifier:
9356 simple-type-specifier
9357 class-specifier
9358 enum-specifier
9359 elaborated-type-specifier
9360 cv-qualifier
9362 GNU Extension:
9364 type-specifier:
9365 __complex__
9367 Returns a representation of the type-specifier. For a
9368 class-specifier, enum-specifier, or elaborated-type-specifier, a
9369 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9371 The parser flags FLAGS is used to control type-specifier parsing.
9373 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9374 in a decl-specifier-seq.
9376 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9377 class-specifier, enum-specifier, or elaborated-type-specifier, then
9378 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9379 if a type is declared; 2 if it is defined. Otherwise, it is set to
9380 zero.
9382 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9383 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9384 is set to FALSE. */
9386 static tree
9387 cp_parser_type_specifier (cp_parser* parser,
9388 cp_parser_flags flags,
9389 cp_decl_specifier_seq *decl_specs,
9390 bool is_declaration,
9391 int* declares_class_or_enum,
9392 bool* is_cv_qualifier)
9394 tree type_spec = NULL_TREE;
9395 cp_token *token;
9396 enum rid keyword;
9397 cp_decl_spec ds = ds_last;
9399 /* Assume this type-specifier does not declare a new type. */
9400 if (declares_class_or_enum)
9401 *declares_class_or_enum = 0;
9402 /* And that it does not specify a cv-qualifier. */
9403 if (is_cv_qualifier)
9404 *is_cv_qualifier = false;
9405 /* Peek at the next token. */
9406 token = cp_lexer_peek_token (parser->lexer);
9408 /* If we're looking at a keyword, we can use that to guide the
9409 production we choose. */
9410 keyword = token->keyword;
9411 switch (keyword)
9413 case RID_ENUM:
9414 /* 'enum' [identifier] '{' introduces an enum-specifier;
9415 'enum' <anything else> introduces an elaborated-type-specifier. */
9416 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9417 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9418 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9419 == CPP_OPEN_BRACE))
9421 if (parser->num_template_parameter_lists)
9423 error ("template declaration of %qs", "enum");
9424 cp_parser_skip_to_end_of_block_or_statement (parser);
9425 type_spec = error_mark_node;
9427 else
9428 type_spec = cp_parser_enum_specifier (parser);
9430 if (declares_class_or_enum)
9431 *declares_class_or_enum = 2;
9432 if (decl_specs)
9433 cp_parser_set_decl_spec_type (decl_specs,
9434 type_spec,
9435 /*user_defined_p=*/true);
9436 return type_spec;
9438 else
9439 goto elaborated_type_specifier;
9441 /* Any of these indicate either a class-specifier, or an
9442 elaborated-type-specifier. */
9443 case RID_CLASS:
9444 case RID_STRUCT:
9445 case RID_UNION:
9446 /* Parse tentatively so that we can back up if we don't find a
9447 class-specifier. */
9448 cp_parser_parse_tentatively (parser);
9449 /* Look for the class-specifier. */
9450 type_spec = cp_parser_class_specifier (parser);
9451 /* If that worked, we're done. */
9452 if (cp_parser_parse_definitely (parser))
9454 if (declares_class_or_enum)
9455 *declares_class_or_enum = 2;
9456 if (decl_specs)
9457 cp_parser_set_decl_spec_type (decl_specs,
9458 type_spec,
9459 /*user_defined_p=*/true);
9460 return type_spec;
9463 /* Fall through. */
9464 elaborated_type_specifier:
9465 /* We're declaring (not defining) a class or enum. */
9466 if (declares_class_or_enum)
9467 *declares_class_or_enum = 1;
9469 /* Fall through. */
9470 case RID_TYPENAME:
9471 /* Look for an elaborated-type-specifier. */
9472 type_spec
9473 = (cp_parser_elaborated_type_specifier
9474 (parser,
9475 decl_specs && decl_specs->specs[(int) ds_friend],
9476 is_declaration));
9477 if (decl_specs)
9478 cp_parser_set_decl_spec_type (decl_specs,
9479 type_spec,
9480 /*user_defined_p=*/true);
9481 return type_spec;
9483 case RID_CONST:
9484 ds = ds_const;
9485 if (is_cv_qualifier)
9486 *is_cv_qualifier = true;
9487 break;
9489 case RID_VOLATILE:
9490 ds = ds_volatile;
9491 if (is_cv_qualifier)
9492 *is_cv_qualifier = true;
9493 break;
9495 case RID_RESTRICT:
9496 ds = ds_restrict;
9497 if (is_cv_qualifier)
9498 *is_cv_qualifier = true;
9499 break;
9501 case RID_COMPLEX:
9502 /* The `__complex__' keyword is a GNU extension. */
9503 ds = ds_complex;
9504 break;
9506 default:
9507 break;
9510 /* Handle simple keywords. */
9511 if (ds != ds_last)
9513 if (decl_specs)
9515 ++decl_specs->specs[(int)ds];
9516 decl_specs->any_specifiers_p = true;
9518 return cp_lexer_consume_token (parser->lexer)->value;
9521 /* If we do not already have a type-specifier, assume we are looking
9522 at a simple-type-specifier. */
9523 type_spec = cp_parser_simple_type_specifier (parser,
9524 decl_specs,
9525 flags);
9527 /* If we didn't find a type-specifier, and a type-specifier was not
9528 optional in this context, issue an error message. */
9529 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9531 cp_parser_error (parser, "expected type specifier");
9532 return error_mark_node;
9535 return type_spec;
9538 /* Parse a simple-type-specifier.
9540 simple-type-specifier:
9541 :: [opt] nested-name-specifier [opt] type-name
9542 :: [opt] nested-name-specifier template template-id
9543 char
9544 wchar_t
9545 bool
9546 short
9548 long
9549 signed
9550 unsigned
9551 float
9552 double
9553 void
9555 GNU Extension:
9557 simple-type-specifier:
9558 __typeof__ unary-expression
9559 __typeof__ ( type-id )
9561 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9562 appropriately updated. */
9564 static tree
9565 cp_parser_simple_type_specifier (cp_parser* parser,
9566 cp_decl_specifier_seq *decl_specs,
9567 cp_parser_flags flags)
9569 tree type = NULL_TREE;
9570 cp_token *token;
9572 /* Peek at the next token. */
9573 token = cp_lexer_peek_token (parser->lexer);
9575 /* If we're looking at a keyword, things are easy. */
9576 switch (token->keyword)
9578 case RID_CHAR:
9579 if (decl_specs)
9580 decl_specs->explicit_char_p = true;
9581 type = char_type_node;
9582 break;
9583 case RID_WCHAR:
9584 type = wchar_type_node;
9585 break;
9586 case RID_BOOL:
9587 type = boolean_type_node;
9588 break;
9589 case RID_SHORT:
9590 if (decl_specs)
9591 ++decl_specs->specs[(int) ds_short];
9592 type = short_integer_type_node;
9593 break;
9594 case RID_INT:
9595 if (decl_specs)
9596 decl_specs->explicit_int_p = true;
9597 type = integer_type_node;
9598 break;
9599 case RID_LONG:
9600 if (decl_specs)
9601 ++decl_specs->specs[(int) ds_long];
9602 type = long_integer_type_node;
9603 break;
9604 case RID_SIGNED:
9605 if (decl_specs)
9606 ++decl_specs->specs[(int) ds_signed];
9607 type = integer_type_node;
9608 break;
9609 case RID_UNSIGNED:
9610 if (decl_specs)
9611 ++decl_specs->specs[(int) ds_unsigned];
9612 type = unsigned_type_node;
9613 break;
9614 case RID_FLOAT:
9615 type = float_type_node;
9616 break;
9617 case RID_DOUBLE:
9618 type = double_type_node;
9619 break;
9620 case RID_VOID:
9621 type = void_type_node;
9622 break;
9624 case RID_TYPEOF:
9625 /* Consume the `typeof' token. */
9626 cp_lexer_consume_token (parser->lexer);
9627 /* Parse the operand to `typeof'. */
9628 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9629 /* If it is not already a TYPE, take its type. */
9630 if (!TYPE_P (type))
9631 type = finish_typeof (type);
9633 if (decl_specs)
9634 cp_parser_set_decl_spec_type (decl_specs, type,
9635 /*user_defined_p=*/true);
9637 return type;
9639 default:
9640 break;
9643 /* If the type-specifier was for a built-in type, we're done. */
9644 if (type)
9646 tree id;
9648 /* Record the type. */
9649 if (decl_specs
9650 && (token->keyword != RID_SIGNED
9651 && token->keyword != RID_UNSIGNED
9652 && token->keyword != RID_SHORT
9653 && token->keyword != RID_LONG))
9654 cp_parser_set_decl_spec_type (decl_specs,
9655 type,
9656 /*user_defined=*/false);
9657 if (decl_specs)
9658 decl_specs->any_specifiers_p = true;
9660 /* Consume the token. */
9661 id = cp_lexer_consume_token (parser->lexer)->value;
9663 /* There is no valid C++ program where a non-template type is
9664 followed by a "<". That usually indicates that the user thought
9665 that the type was a template. */
9666 cp_parser_check_for_invalid_template_id (parser, type);
9668 return TYPE_NAME (type);
9671 /* The type-specifier must be a user-defined type. */
9672 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9674 bool qualified_p;
9675 bool global_p;
9677 /* Don't gobble tokens or issue error messages if this is an
9678 optional type-specifier. */
9679 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9680 cp_parser_parse_tentatively (parser);
9682 /* Look for the optional `::' operator. */
9683 global_p
9684 = (cp_parser_global_scope_opt (parser,
9685 /*current_scope_valid_p=*/false)
9686 != NULL_TREE);
9687 /* Look for the nested-name specifier. */
9688 qualified_p
9689 = (cp_parser_nested_name_specifier_opt (parser,
9690 /*typename_keyword_p=*/false,
9691 /*check_dependency_p=*/true,
9692 /*type_p=*/false,
9693 /*is_declaration=*/false)
9694 != NULL_TREE);
9695 /* If we have seen a nested-name-specifier, and the next token
9696 is `template', then we are using the template-id production. */
9697 if (parser->scope
9698 && cp_parser_optional_template_keyword (parser))
9700 /* Look for the template-id. */
9701 type = cp_parser_template_id (parser,
9702 /*template_keyword_p=*/true,
9703 /*check_dependency_p=*/true,
9704 /*is_declaration=*/false);
9705 /* If the template-id did not name a type, we are out of
9706 luck. */
9707 if (TREE_CODE (type) != TYPE_DECL)
9709 cp_parser_error (parser, "expected template-id for type");
9710 type = NULL_TREE;
9713 /* Otherwise, look for a type-name. */
9714 else
9715 type = cp_parser_type_name (parser);
9716 /* Keep track of all name-lookups performed in class scopes. */
9717 if (type
9718 && !global_p
9719 && !qualified_p
9720 && TREE_CODE (type) == TYPE_DECL
9721 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9722 maybe_note_name_used_in_class (DECL_NAME (type), type);
9723 /* If it didn't work out, we don't have a TYPE. */
9724 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9725 && !cp_parser_parse_definitely (parser))
9726 type = NULL_TREE;
9727 if (type && decl_specs)
9728 cp_parser_set_decl_spec_type (decl_specs, type,
9729 /*user_defined=*/true);
9732 /* If we didn't get a type-name, issue an error message. */
9733 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9735 cp_parser_error (parser, "expected type-name");
9736 return error_mark_node;
9739 /* There is no valid C++ program where a non-template type is
9740 followed by a "<". That usually indicates that the user thought
9741 that the type was a template. */
9742 if (type && type != error_mark_node)
9744 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9745 If it is, then the '<'...'>' enclose protocol names rather than
9746 template arguments, and so everything is fine. */
9747 if (c_dialect_objc ()
9748 && (objc_is_id (type) || objc_is_class_name (type)))
9750 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9751 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9753 /* Clobber the "unqualified" type previously entered into
9754 DECL_SPECS with the new, improved protocol-qualified version. */
9755 if (decl_specs)
9756 decl_specs->type = qual_type;
9758 return qual_type;
9761 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9764 return type;
9767 /* Parse a type-name.
9769 type-name:
9770 class-name
9771 enum-name
9772 typedef-name
9774 enum-name:
9775 identifier
9777 typedef-name:
9778 identifier
9780 Returns a TYPE_DECL for the type. */
9782 static tree
9783 cp_parser_type_name (cp_parser* parser)
9785 tree type_decl;
9786 tree identifier;
9788 /* We can't know yet whether it is a class-name or not. */
9789 cp_parser_parse_tentatively (parser);
9790 /* Try a class-name. */
9791 type_decl = cp_parser_class_name (parser,
9792 /*typename_keyword_p=*/false,
9793 /*template_keyword_p=*/false,
9794 none_type,
9795 /*check_dependency_p=*/true,
9796 /*class_head_p=*/false,
9797 /*is_declaration=*/false);
9798 /* If it's not a class-name, keep looking. */
9799 if (!cp_parser_parse_definitely (parser))
9801 /* It must be a typedef-name or an enum-name. */
9802 identifier = cp_parser_identifier (parser);
9803 if (identifier == error_mark_node)
9804 return error_mark_node;
9806 /* Look up the type-name. */
9807 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9809 if (TREE_CODE (type_decl) != TYPE_DECL
9810 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9812 /* See if this is an Objective-C type. */
9813 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9814 tree type = objc_get_protocol_qualified_type (identifier, protos);
9815 if (type)
9816 type_decl = TYPE_NAME (type);
9819 /* Issue an error if we did not find a type-name. */
9820 if (TREE_CODE (type_decl) != TYPE_DECL)
9822 if (!cp_parser_simulate_error (parser))
9823 cp_parser_name_lookup_error (parser, identifier, type_decl,
9824 "is not a type");
9825 type_decl = error_mark_node;
9827 /* Remember that the name was used in the definition of the
9828 current class so that we can check later to see if the
9829 meaning would have been different after the class was
9830 entirely defined. */
9831 else if (type_decl != error_mark_node
9832 && !parser->scope)
9833 maybe_note_name_used_in_class (identifier, type_decl);
9836 return type_decl;
9840 /* Parse an elaborated-type-specifier. Note that the grammar given
9841 here incorporates the resolution to DR68.
9843 elaborated-type-specifier:
9844 class-key :: [opt] nested-name-specifier [opt] identifier
9845 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9846 enum :: [opt] nested-name-specifier [opt] identifier
9847 typename :: [opt] nested-name-specifier identifier
9848 typename :: [opt] nested-name-specifier template [opt]
9849 template-id
9851 GNU extension:
9853 elaborated-type-specifier:
9854 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9855 class-key attributes :: [opt] nested-name-specifier [opt]
9856 template [opt] template-id
9857 enum attributes :: [opt] nested-name-specifier [opt] identifier
9859 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9860 declared `friend'. If IS_DECLARATION is TRUE, then this
9861 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9862 something is being declared.
9864 Returns the TYPE specified. */
9866 static tree
9867 cp_parser_elaborated_type_specifier (cp_parser* parser,
9868 bool is_friend,
9869 bool is_declaration)
9871 enum tag_types tag_type;
9872 tree identifier;
9873 tree type = NULL_TREE;
9874 tree attributes = NULL_TREE;
9876 /* See if we're looking at the `enum' keyword. */
9877 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9879 /* Consume the `enum' token. */
9880 cp_lexer_consume_token (parser->lexer);
9881 /* Remember that it's an enumeration type. */
9882 tag_type = enum_type;
9883 /* Parse the attributes. */
9884 attributes = cp_parser_attributes_opt (parser);
9886 /* Or, it might be `typename'. */
9887 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9888 RID_TYPENAME))
9890 /* Consume the `typename' token. */
9891 cp_lexer_consume_token (parser->lexer);
9892 /* Remember that it's a `typename' type. */
9893 tag_type = typename_type;
9894 /* The `typename' keyword is only allowed in templates. */
9895 if (!processing_template_decl)
9896 pedwarn ("using %<typename%> outside of template");
9898 /* Otherwise it must be a class-key. */
9899 else
9901 tag_type = cp_parser_class_key (parser);
9902 if (tag_type == none_type)
9903 return error_mark_node;
9904 /* Parse the attributes. */
9905 attributes = cp_parser_attributes_opt (parser);
9908 /* Look for the `::' operator. */
9909 cp_parser_global_scope_opt (parser,
9910 /*current_scope_valid_p=*/false);
9911 /* Look for the nested-name-specifier. */
9912 if (tag_type == typename_type)
9914 if (!cp_parser_nested_name_specifier (parser,
9915 /*typename_keyword_p=*/true,
9916 /*check_dependency_p=*/true,
9917 /*type_p=*/true,
9918 is_declaration))
9919 return error_mark_node;
9921 else
9922 /* Even though `typename' is not present, the proposed resolution
9923 to Core Issue 180 says that in `class A<T>::B', `B' should be
9924 considered a type-name, even if `A<T>' is dependent. */
9925 cp_parser_nested_name_specifier_opt (parser,
9926 /*typename_keyword_p=*/true,
9927 /*check_dependency_p=*/true,
9928 /*type_p=*/true,
9929 is_declaration);
9930 /* For everything but enumeration types, consider a template-id. */
9931 if (tag_type != enum_type)
9933 bool template_p = false;
9934 tree decl;
9936 /* Allow the `template' keyword. */
9937 template_p = cp_parser_optional_template_keyword (parser);
9938 /* If we didn't see `template', we don't know if there's a
9939 template-id or not. */
9940 if (!template_p)
9941 cp_parser_parse_tentatively (parser);
9942 /* Parse the template-id. */
9943 decl = cp_parser_template_id (parser, template_p,
9944 /*check_dependency_p=*/true,
9945 is_declaration);
9946 /* If we didn't find a template-id, look for an ordinary
9947 identifier. */
9948 if (!template_p && !cp_parser_parse_definitely (parser))
9950 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9951 in effect, then we must assume that, upon instantiation, the
9952 template will correspond to a class. */
9953 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9954 && tag_type == typename_type)
9955 type = make_typename_type (parser->scope, decl,
9956 typename_type,
9957 /*complain=*/tf_error);
9958 else
9959 type = TREE_TYPE (decl);
9962 /* For an enumeration type, consider only a plain identifier. */
9963 if (!type)
9965 identifier = cp_parser_identifier (parser);
9967 if (identifier == error_mark_node)
9969 parser->scope = NULL_TREE;
9970 return error_mark_node;
9973 /* For a `typename', we needn't call xref_tag. */
9974 if (tag_type == typename_type
9975 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9976 return cp_parser_make_typename_type (parser, parser->scope,
9977 identifier);
9978 /* Look up a qualified name in the usual way. */
9979 if (parser->scope)
9981 tree decl;
9983 decl = cp_parser_lookup_name (parser, identifier,
9984 tag_type,
9985 /*is_template=*/false,
9986 /*is_namespace=*/false,
9987 /*check_dependency=*/true,
9988 /*ambiguous_decls=*/NULL);
9990 /* If we are parsing friend declaration, DECL may be a
9991 TEMPLATE_DECL tree node here. However, we need to check
9992 whether this TEMPLATE_DECL results in valid code. Consider
9993 the following example:
9995 namespace N {
9996 template <class T> class C {};
9998 class X {
9999 template <class T> friend class N::C; // #1, valid code
10001 template <class T> class Y {
10002 friend class N::C; // #2, invalid code
10005 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10006 name lookup of `N::C'. We see that friend declaration must
10007 be template for the code to be valid. Note that
10008 processing_template_decl does not work here since it is
10009 always 1 for the above two cases. */
10011 decl = (cp_parser_maybe_treat_template_as_class
10012 (decl, /*tag_name_p=*/is_friend
10013 && parser->num_template_parameter_lists));
10015 if (TREE_CODE (decl) != TYPE_DECL)
10017 cp_parser_diagnose_invalid_type_name (parser,
10018 parser->scope,
10019 identifier);
10020 return error_mark_node;
10023 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10024 check_elaborated_type_specifier
10025 (tag_type, decl,
10026 (parser->num_template_parameter_lists
10027 || DECL_SELF_REFERENCE_P (decl)));
10029 type = TREE_TYPE (decl);
10031 else
10033 /* An elaborated-type-specifier sometimes introduces a new type and
10034 sometimes names an existing type. Normally, the rule is that it
10035 introduces a new type only if there is not an existing type of
10036 the same name already in scope. For example, given:
10038 struct S {};
10039 void f() { struct S s; }
10041 the `struct S' in the body of `f' is the same `struct S' as in
10042 the global scope; the existing definition is used. However, if
10043 there were no global declaration, this would introduce a new
10044 local class named `S'.
10046 An exception to this rule applies to the following code:
10048 namespace N { struct S; }
10050 Here, the elaborated-type-specifier names a new type
10051 unconditionally; even if there is already an `S' in the
10052 containing scope this declaration names a new type.
10053 This exception only applies if the elaborated-type-specifier
10054 forms the complete declaration:
10056 [class.name]
10058 A declaration consisting solely of `class-key identifier ;' is
10059 either a redeclaration of the name in the current scope or a
10060 forward declaration of the identifier as a class name. It
10061 introduces the name into the current scope.
10063 We are in this situation precisely when the next token is a `;'.
10065 An exception to the exception is that a `friend' declaration does
10066 *not* name a new type; i.e., given:
10068 struct S { friend struct T; };
10070 `T' is not a new type in the scope of `S'.
10072 Also, `new struct S' or `sizeof (struct S)' never results in the
10073 definition of a new type; a new type can only be declared in a
10074 declaration context. */
10076 tag_scope ts;
10077 bool template_p;
10079 if (is_friend)
10080 /* Friends have special name lookup rules. */
10081 ts = ts_within_enclosing_non_class;
10082 else if (is_declaration
10083 && cp_lexer_next_token_is (parser->lexer,
10084 CPP_SEMICOLON))
10085 /* This is a `class-key identifier ;' */
10086 ts = ts_current;
10087 else
10088 ts = ts_global;
10090 /* Warn about attributes. They are ignored. */
10091 if (attributes)
10092 warning (OPT_Wattributes,
10093 "type attributes are honored only at type definition");
10095 template_p =
10096 (parser->num_template_parameter_lists
10097 && (cp_parser_next_token_starts_class_definition_p (parser)
10098 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10099 type = xref_tag (tag_type, identifier, ts, template_p);
10102 if (tag_type != enum_type)
10103 cp_parser_check_class_key (tag_type, type);
10105 /* A "<" cannot follow an elaborated type specifier. If that
10106 happens, the user was probably trying to form a template-id. */
10107 cp_parser_check_for_invalid_template_id (parser, type);
10109 return type;
10112 /* Parse an enum-specifier.
10114 enum-specifier:
10115 enum identifier [opt] { enumerator-list [opt] }
10117 GNU Extensions:
10118 enum identifier [opt] { enumerator-list [opt] } attributes
10120 Returns an ENUM_TYPE representing the enumeration. */
10122 static tree
10123 cp_parser_enum_specifier (cp_parser* parser)
10125 tree identifier;
10126 tree type;
10128 /* Caller guarantees that the current token is 'enum', an identifier
10129 possibly follows, and the token after that is an opening brace.
10130 If we don't have an identifier, fabricate an anonymous name for
10131 the enumeration being defined. */
10132 cp_lexer_consume_token (parser->lexer);
10134 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10135 identifier = cp_parser_identifier (parser);
10136 else
10137 identifier = make_anon_name ();
10139 /* Issue an error message if type-definitions are forbidden here. */
10140 cp_parser_check_type_definition (parser);
10142 /* Create the new type. We do this before consuming the opening brace
10143 so the enum will be recorded as being on the line of its tag (or the
10144 'enum' keyword, if there is no tag). */
10145 type = start_enum (identifier);
10147 /* Consume the opening brace. */
10148 cp_lexer_consume_token (parser->lexer);
10150 /* If the next token is not '}', then there are some enumerators. */
10151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10152 cp_parser_enumerator_list (parser, type);
10154 /* Consume the final '}'. */
10155 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10157 /* Look for trailing attributes to apply to this enumeration, and
10158 apply them if appropriate. */
10159 if (cp_parser_allow_gnu_extensions_p (parser))
10161 tree trailing_attr = cp_parser_attributes_opt (parser);
10162 cplus_decl_attributes (&type,
10163 trailing_attr,
10164 (int) ATTR_FLAG_TYPE_IN_PLACE);
10167 /* Finish up the enumeration. */
10168 finish_enum (type);
10170 return type;
10173 /* Parse an enumerator-list. The enumerators all have the indicated
10174 TYPE.
10176 enumerator-list:
10177 enumerator-definition
10178 enumerator-list , enumerator-definition */
10180 static void
10181 cp_parser_enumerator_list (cp_parser* parser, tree type)
10183 while (true)
10185 /* Parse an enumerator-definition. */
10186 cp_parser_enumerator_definition (parser, type);
10188 /* If the next token is not a ',', we've reached the end of
10189 the list. */
10190 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10191 break;
10192 /* Otherwise, consume the `,' and keep going. */
10193 cp_lexer_consume_token (parser->lexer);
10194 /* If the next token is a `}', there is a trailing comma. */
10195 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10197 if (pedantic && !in_system_header)
10198 pedwarn ("comma at end of enumerator list");
10199 break;
10204 /* Parse an enumerator-definition. The enumerator has the indicated
10205 TYPE.
10207 enumerator-definition:
10208 enumerator
10209 enumerator = constant-expression
10211 enumerator:
10212 identifier */
10214 static void
10215 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10217 tree identifier;
10218 tree value;
10220 /* Look for the identifier. */
10221 identifier = cp_parser_identifier (parser);
10222 if (identifier == error_mark_node)
10223 return;
10225 /* If the next token is an '=', then there is an explicit value. */
10226 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10228 /* Consume the `=' token. */
10229 cp_lexer_consume_token (parser->lexer);
10230 /* Parse the value. */
10231 value = cp_parser_constant_expression (parser,
10232 /*allow_non_constant_p=*/false,
10233 NULL);
10235 else
10236 value = NULL_TREE;
10238 /* Create the enumerator. */
10239 build_enumerator (identifier, value, type);
10242 /* Parse a namespace-name.
10244 namespace-name:
10245 original-namespace-name
10246 namespace-alias
10248 Returns the NAMESPACE_DECL for the namespace. */
10250 static tree
10251 cp_parser_namespace_name (cp_parser* parser)
10253 tree identifier;
10254 tree namespace_decl;
10256 /* Get the name of the namespace. */
10257 identifier = cp_parser_identifier (parser);
10258 if (identifier == error_mark_node)
10259 return error_mark_node;
10261 /* Look up the identifier in the currently active scope. Look only
10262 for namespaces, due to:
10264 [basic.lookup.udir]
10266 When looking up a namespace-name in a using-directive or alias
10267 definition, only namespace names are considered.
10269 And:
10271 [basic.lookup.qual]
10273 During the lookup of a name preceding the :: scope resolution
10274 operator, object, function, and enumerator names are ignored.
10276 (Note that cp_parser_class_or_namespace_name only calls this
10277 function if the token after the name is the scope resolution
10278 operator.) */
10279 namespace_decl = cp_parser_lookup_name (parser, identifier,
10280 none_type,
10281 /*is_template=*/false,
10282 /*is_namespace=*/true,
10283 /*check_dependency=*/true,
10284 /*ambiguous_decls=*/NULL);
10285 /* If it's not a namespace, issue an error. */
10286 if (namespace_decl == error_mark_node
10287 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10289 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10290 error ("%qD is not a namespace-name", identifier);
10291 cp_parser_error (parser, "expected namespace-name");
10292 namespace_decl = error_mark_node;
10295 return namespace_decl;
10298 /* Parse a namespace-definition.
10300 namespace-definition:
10301 named-namespace-definition
10302 unnamed-namespace-definition
10304 named-namespace-definition:
10305 original-namespace-definition
10306 extension-namespace-definition
10308 original-namespace-definition:
10309 namespace identifier { namespace-body }
10311 extension-namespace-definition:
10312 namespace original-namespace-name { namespace-body }
10314 unnamed-namespace-definition:
10315 namespace { namespace-body } */
10317 static void
10318 cp_parser_namespace_definition (cp_parser* parser)
10320 tree identifier;
10322 /* Look for the `namespace' keyword. */
10323 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10325 /* Get the name of the namespace. We do not attempt to distinguish
10326 between an original-namespace-definition and an
10327 extension-namespace-definition at this point. The semantic
10328 analysis routines are responsible for that. */
10329 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10330 identifier = cp_parser_identifier (parser);
10331 else
10332 identifier = NULL_TREE;
10334 /* Look for the `{' to start the namespace. */
10335 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10336 /* Start the namespace. */
10337 push_namespace (identifier);
10338 /* Parse the body of the namespace. */
10339 cp_parser_namespace_body (parser);
10340 /* Finish the namespace. */
10341 pop_namespace ();
10342 /* Look for the final `}'. */
10343 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10346 /* Parse a namespace-body.
10348 namespace-body:
10349 declaration-seq [opt] */
10351 static void
10352 cp_parser_namespace_body (cp_parser* parser)
10354 cp_parser_declaration_seq_opt (parser);
10357 /* Parse a namespace-alias-definition.
10359 namespace-alias-definition:
10360 namespace identifier = qualified-namespace-specifier ; */
10362 static void
10363 cp_parser_namespace_alias_definition (cp_parser* parser)
10365 tree identifier;
10366 tree namespace_specifier;
10368 /* Look for the `namespace' keyword. */
10369 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10370 /* Look for the identifier. */
10371 identifier = cp_parser_identifier (parser);
10372 if (identifier == error_mark_node)
10373 return;
10374 /* Look for the `=' token. */
10375 cp_parser_require (parser, CPP_EQ, "`='");
10376 /* Look for the qualified-namespace-specifier. */
10377 namespace_specifier
10378 = cp_parser_qualified_namespace_specifier (parser);
10379 /* Look for the `;' token. */
10380 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10382 /* Register the alias in the symbol table. */
10383 do_namespace_alias (identifier, namespace_specifier);
10386 /* Parse a qualified-namespace-specifier.
10388 qualified-namespace-specifier:
10389 :: [opt] nested-name-specifier [opt] namespace-name
10391 Returns a NAMESPACE_DECL corresponding to the specified
10392 namespace. */
10394 static tree
10395 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10397 /* Look for the optional `::'. */
10398 cp_parser_global_scope_opt (parser,
10399 /*current_scope_valid_p=*/false);
10401 /* Look for the optional nested-name-specifier. */
10402 cp_parser_nested_name_specifier_opt (parser,
10403 /*typename_keyword_p=*/false,
10404 /*check_dependency_p=*/true,
10405 /*type_p=*/false,
10406 /*is_declaration=*/true);
10408 return cp_parser_namespace_name (parser);
10411 /* Parse a using-declaration.
10413 using-declaration:
10414 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10415 using :: unqualified-id ; */
10417 static void
10418 cp_parser_using_declaration (cp_parser* parser)
10420 cp_token *token;
10421 bool typename_p = false;
10422 bool global_scope_p;
10423 tree decl;
10424 tree identifier;
10425 tree qscope;
10427 /* Look for the `using' keyword. */
10428 cp_parser_require_keyword (parser, RID_USING, "`using'");
10430 /* Peek at the next token. */
10431 token = cp_lexer_peek_token (parser->lexer);
10432 /* See if it's `typename'. */
10433 if (token->keyword == RID_TYPENAME)
10435 /* Remember that we've seen it. */
10436 typename_p = true;
10437 /* Consume the `typename' token. */
10438 cp_lexer_consume_token (parser->lexer);
10441 /* Look for the optional global scope qualification. */
10442 global_scope_p
10443 = (cp_parser_global_scope_opt (parser,
10444 /*current_scope_valid_p=*/false)
10445 != NULL_TREE);
10447 /* If we saw `typename', or didn't see `::', then there must be a
10448 nested-name-specifier present. */
10449 if (typename_p || !global_scope_p)
10450 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10451 /*check_dependency_p=*/true,
10452 /*type_p=*/false,
10453 /*is_declaration=*/true);
10454 /* Otherwise, we could be in either of the two productions. In that
10455 case, treat the nested-name-specifier as optional. */
10456 else
10457 qscope = cp_parser_nested_name_specifier_opt (parser,
10458 /*typename_keyword_p=*/false,
10459 /*check_dependency_p=*/true,
10460 /*type_p=*/false,
10461 /*is_declaration=*/true);
10462 if (!qscope)
10463 qscope = global_namespace;
10465 /* Parse the unqualified-id. */
10466 identifier = cp_parser_unqualified_id (parser,
10467 /*template_keyword_p=*/false,
10468 /*check_dependency_p=*/true,
10469 /*declarator_p=*/true);
10471 /* The function we call to handle a using-declaration is different
10472 depending on what scope we are in. */
10473 if (identifier == error_mark_node)
10475 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10476 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10477 /* [namespace.udecl]
10479 A using declaration shall not name a template-id. */
10480 error ("a template-id may not appear in a using-declaration");
10481 else
10483 if (at_class_scope_p ())
10485 /* Create the USING_DECL. */
10486 decl = do_class_using_decl (parser->scope, identifier);
10487 /* Add it to the list of members in this class. */
10488 finish_member_declaration (decl);
10490 else
10492 decl = cp_parser_lookup_name_simple (parser, identifier);
10493 if (decl == error_mark_node)
10494 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10495 else if (!at_namespace_scope_p ())
10496 do_local_using_decl (decl, qscope, identifier);
10497 else
10498 do_toplevel_using_decl (decl, qscope, identifier);
10502 /* Look for the final `;'. */
10503 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10506 /* Parse a using-directive.
10508 using-directive:
10509 using namespace :: [opt] nested-name-specifier [opt]
10510 namespace-name ; */
10512 static void
10513 cp_parser_using_directive (cp_parser* parser)
10515 tree namespace_decl;
10516 tree attribs;
10518 /* Look for the `using' keyword. */
10519 cp_parser_require_keyword (parser, RID_USING, "`using'");
10520 /* And the `namespace' keyword. */
10521 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10522 /* Look for the optional `::' operator. */
10523 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10524 /* And the optional nested-name-specifier. */
10525 cp_parser_nested_name_specifier_opt (parser,
10526 /*typename_keyword_p=*/false,
10527 /*check_dependency_p=*/true,
10528 /*type_p=*/false,
10529 /*is_declaration=*/true);
10530 /* Get the namespace being used. */
10531 namespace_decl = cp_parser_namespace_name (parser);
10532 /* And any specified attributes. */
10533 attribs = cp_parser_attributes_opt (parser);
10534 /* Update the symbol table. */
10535 parse_using_directive (namespace_decl, attribs);
10536 /* Look for the final `;'. */
10537 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10540 /* Parse an asm-definition.
10542 asm-definition:
10543 asm ( string-literal ) ;
10545 GNU Extension:
10547 asm-definition:
10548 asm volatile [opt] ( string-literal ) ;
10549 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10550 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10551 : asm-operand-list [opt] ) ;
10552 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10553 : asm-operand-list [opt]
10554 : asm-operand-list [opt] ) ; */
10556 static void
10557 cp_parser_asm_definition (cp_parser* parser)
10559 tree string;
10560 tree outputs = NULL_TREE;
10561 tree inputs = NULL_TREE;
10562 tree clobbers = NULL_TREE;
10563 tree asm_stmt;
10564 bool volatile_p = false;
10565 bool extended_p = false;
10567 /* Look for the `asm' keyword. */
10568 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10569 /* See if the next token is `volatile'. */
10570 if (cp_parser_allow_gnu_extensions_p (parser)
10571 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10573 /* Remember that we saw the `volatile' keyword. */
10574 volatile_p = true;
10575 /* Consume the token. */
10576 cp_lexer_consume_token (parser->lexer);
10578 /* Look for the opening `('. */
10579 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10580 return;
10581 /* Look for the string. */
10582 string = cp_parser_string_literal (parser, false, false);
10583 if (string == error_mark_node)
10585 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10586 /*consume_paren=*/true);
10587 return;
10590 /* If we're allowing GNU extensions, check for the extended assembly
10591 syntax. Unfortunately, the `:' tokens need not be separated by
10592 a space in C, and so, for compatibility, we tolerate that here
10593 too. Doing that means that we have to treat the `::' operator as
10594 two `:' tokens. */
10595 if (cp_parser_allow_gnu_extensions_p (parser)
10596 && at_function_scope_p ()
10597 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10598 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10600 bool inputs_p = false;
10601 bool clobbers_p = false;
10603 /* The extended syntax was used. */
10604 extended_p = true;
10606 /* Look for outputs. */
10607 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10609 /* Consume the `:'. */
10610 cp_lexer_consume_token (parser->lexer);
10611 /* Parse the output-operands. */
10612 if (cp_lexer_next_token_is_not (parser->lexer,
10613 CPP_COLON)
10614 && cp_lexer_next_token_is_not (parser->lexer,
10615 CPP_SCOPE)
10616 && cp_lexer_next_token_is_not (parser->lexer,
10617 CPP_CLOSE_PAREN))
10618 outputs = cp_parser_asm_operand_list (parser);
10620 /* If the next token is `::', there are no outputs, and the
10621 next token is the beginning of the inputs. */
10622 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10623 /* The inputs are coming next. */
10624 inputs_p = true;
10626 /* Look for inputs. */
10627 if (inputs_p
10628 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10630 /* Consume the `:' or `::'. */
10631 cp_lexer_consume_token (parser->lexer);
10632 /* Parse the output-operands. */
10633 if (cp_lexer_next_token_is_not (parser->lexer,
10634 CPP_COLON)
10635 && cp_lexer_next_token_is_not (parser->lexer,
10636 CPP_CLOSE_PAREN))
10637 inputs = cp_parser_asm_operand_list (parser);
10639 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10640 /* The clobbers are coming next. */
10641 clobbers_p = true;
10643 /* Look for clobbers. */
10644 if (clobbers_p
10645 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10647 /* Consume the `:' or `::'. */
10648 cp_lexer_consume_token (parser->lexer);
10649 /* Parse the clobbers. */
10650 if (cp_lexer_next_token_is_not (parser->lexer,
10651 CPP_CLOSE_PAREN))
10652 clobbers = cp_parser_asm_clobber_list (parser);
10655 /* Look for the closing `)'. */
10656 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10657 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10658 /*consume_paren=*/true);
10659 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10661 /* Create the ASM_EXPR. */
10662 if (at_function_scope_p ())
10664 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10665 inputs, clobbers);
10666 /* If the extended syntax was not used, mark the ASM_EXPR. */
10667 if (!extended_p)
10669 tree temp = asm_stmt;
10670 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10671 temp = TREE_OPERAND (temp, 0);
10673 ASM_INPUT_P (temp) = 1;
10676 else
10677 assemble_asm (string);
10680 /* Declarators [gram.dcl.decl] */
10682 /* Parse an init-declarator.
10684 init-declarator:
10685 declarator initializer [opt]
10687 GNU Extension:
10689 init-declarator:
10690 declarator asm-specification [opt] attributes [opt] initializer [opt]
10692 function-definition:
10693 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10694 function-body
10695 decl-specifier-seq [opt] declarator function-try-block
10697 GNU Extension:
10699 function-definition:
10700 __extension__ function-definition
10702 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10703 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10704 then this declarator appears in a class scope. The new DECL created
10705 by this declarator is returned.
10707 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10708 for a function-definition here as well. If the declarator is a
10709 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10710 be TRUE upon return. By that point, the function-definition will
10711 have been completely parsed.
10713 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10714 is FALSE. */
10716 static tree
10717 cp_parser_init_declarator (cp_parser* parser,
10718 cp_decl_specifier_seq *decl_specifiers,
10719 bool function_definition_allowed_p,
10720 bool member_p,
10721 int declares_class_or_enum,
10722 bool* function_definition_p)
10724 cp_token *token;
10725 cp_declarator *declarator;
10726 tree prefix_attributes;
10727 tree attributes;
10728 tree asm_specification;
10729 tree initializer;
10730 tree decl = NULL_TREE;
10731 tree scope;
10732 bool is_initialized;
10733 bool is_parenthesized_init;
10734 bool is_non_constant_init;
10735 int ctor_dtor_or_conv_p;
10736 bool friend_p;
10737 tree pushed_scope = NULL;
10739 /* Gather the attributes that were provided with the
10740 decl-specifiers. */
10741 prefix_attributes = decl_specifiers->attributes;
10743 /* Assume that this is not the declarator for a function
10744 definition. */
10745 if (function_definition_p)
10746 *function_definition_p = false;
10748 /* Defer access checks while parsing the declarator; we cannot know
10749 what names are accessible until we know what is being
10750 declared. */
10751 resume_deferring_access_checks ();
10753 /* Parse the declarator. */
10754 declarator
10755 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10756 &ctor_dtor_or_conv_p,
10757 /*parenthesized_p=*/NULL,
10758 /*member_p=*/false);
10759 /* Gather up the deferred checks. */
10760 stop_deferring_access_checks ();
10762 /* If the DECLARATOR was erroneous, there's no need to go
10763 further. */
10764 if (declarator == cp_error_declarator)
10765 return error_mark_node;
10767 if (declares_class_or_enum & 2)
10768 cp_parser_check_for_definition_in_return_type (declarator,
10769 decl_specifiers->type);
10771 /* Figure out what scope the entity declared by the DECLARATOR is
10772 located in. `grokdeclarator' sometimes changes the scope, so
10773 we compute it now. */
10774 scope = get_scope_of_declarator (declarator);
10776 /* If we're allowing GNU extensions, look for an asm-specification
10777 and attributes. */
10778 if (cp_parser_allow_gnu_extensions_p (parser))
10780 /* Look for an asm-specification. */
10781 asm_specification = cp_parser_asm_specification_opt (parser);
10782 /* And attributes. */
10783 attributes = cp_parser_attributes_opt (parser);
10785 else
10787 asm_specification = NULL_TREE;
10788 attributes = NULL_TREE;
10791 /* Peek at the next token. */
10792 token = cp_lexer_peek_token (parser->lexer);
10793 /* Check to see if the token indicates the start of a
10794 function-definition. */
10795 if (cp_parser_token_starts_function_definition_p (token))
10797 if (!function_definition_allowed_p)
10799 /* If a function-definition should not appear here, issue an
10800 error message. */
10801 cp_parser_error (parser,
10802 "a function-definition is not allowed here");
10803 return error_mark_node;
10805 else
10807 /* Neither attributes nor an asm-specification are allowed
10808 on a function-definition. */
10809 if (asm_specification)
10810 error ("an asm-specification is not allowed on a function-definition");
10811 if (attributes)
10812 error ("attributes are not allowed on a function-definition");
10813 /* This is a function-definition. */
10814 *function_definition_p = true;
10816 /* Parse the function definition. */
10817 if (member_p)
10818 decl = cp_parser_save_member_function_body (parser,
10819 decl_specifiers,
10820 declarator,
10821 prefix_attributes);
10822 else
10823 decl
10824 = (cp_parser_function_definition_from_specifiers_and_declarator
10825 (parser, decl_specifiers, prefix_attributes, declarator));
10827 return decl;
10831 /* [dcl.dcl]
10833 Only in function declarations for constructors, destructors, and
10834 type conversions can the decl-specifier-seq be omitted.
10836 We explicitly postpone this check past the point where we handle
10837 function-definitions because we tolerate function-definitions
10838 that are missing their return types in some modes. */
10839 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10841 cp_parser_error (parser,
10842 "expected constructor, destructor, or type conversion");
10843 return error_mark_node;
10846 /* An `=' or an `(' indicates an initializer. */
10847 is_initialized = (token->type == CPP_EQ
10848 || token->type == CPP_OPEN_PAREN);
10849 /* If the init-declarator isn't initialized and isn't followed by a
10850 `,' or `;', it's not a valid init-declarator. */
10851 if (!is_initialized
10852 && token->type != CPP_COMMA
10853 && token->type != CPP_SEMICOLON)
10855 cp_parser_error (parser, "expected initializer");
10856 return error_mark_node;
10859 /* Because start_decl has side-effects, we should only call it if we
10860 know we're going ahead. By this point, we know that we cannot
10861 possibly be looking at any other construct. */
10862 cp_parser_commit_to_tentative_parse (parser);
10864 /* If the decl specifiers were bad, issue an error now that we're
10865 sure this was intended to be a declarator. Then continue
10866 declaring the variable(s), as int, to try to cut down on further
10867 errors. */
10868 if (decl_specifiers->any_specifiers_p
10869 && decl_specifiers->type == error_mark_node)
10871 cp_parser_error (parser, "invalid type in declaration");
10872 decl_specifiers->type = integer_type_node;
10875 /* Check to see whether or not this declaration is a friend. */
10876 friend_p = cp_parser_friend_p (decl_specifiers);
10878 /* Check that the number of template-parameter-lists is OK. */
10879 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10880 return error_mark_node;
10882 /* Enter the newly declared entry in the symbol table. If we're
10883 processing a declaration in a class-specifier, we wait until
10884 after processing the initializer. */
10885 if (!member_p)
10887 if (parser->in_unbraced_linkage_specification_p)
10889 decl_specifiers->storage_class = sc_extern;
10890 have_extern_spec = false;
10892 decl = start_decl (declarator, decl_specifiers,
10893 is_initialized, attributes, prefix_attributes,
10894 &pushed_scope);
10896 else if (scope)
10897 /* Enter the SCOPE. That way unqualified names appearing in the
10898 initializer will be looked up in SCOPE. */
10899 pushed_scope = push_scope (scope);
10901 /* Perform deferred access control checks, now that we know in which
10902 SCOPE the declared entity resides. */
10903 if (!member_p && decl)
10905 tree saved_current_function_decl = NULL_TREE;
10907 /* If the entity being declared is a function, pretend that we
10908 are in its scope. If it is a `friend', it may have access to
10909 things that would not otherwise be accessible. */
10910 if (TREE_CODE (decl) == FUNCTION_DECL)
10912 saved_current_function_decl = current_function_decl;
10913 current_function_decl = decl;
10916 /* Perform the access control checks for the declarator and the
10917 the decl-specifiers. */
10918 perform_deferred_access_checks ();
10920 /* Restore the saved value. */
10921 if (TREE_CODE (decl) == FUNCTION_DECL)
10922 current_function_decl = saved_current_function_decl;
10925 /* Parse the initializer. */
10926 if (is_initialized)
10927 initializer = cp_parser_initializer (parser,
10928 &is_parenthesized_init,
10929 &is_non_constant_init);
10930 else
10932 initializer = NULL_TREE;
10933 is_parenthesized_init = false;
10934 is_non_constant_init = true;
10937 /* The old parser allows attributes to appear after a parenthesized
10938 initializer. Mark Mitchell proposed removing this functionality
10939 on the GCC mailing lists on 2002-08-13. This parser accepts the
10940 attributes -- but ignores them. */
10941 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10942 if (cp_parser_attributes_opt (parser))
10943 warning (OPT_Wattributes,
10944 "attributes after parenthesized initializer ignored");
10946 /* For an in-class declaration, use `grokfield' to create the
10947 declaration. */
10948 if (member_p)
10950 if (pushed_scope)
10952 pop_scope (pushed_scope);
10953 pushed_scope = false;
10955 decl = grokfield (declarator, decl_specifiers,
10956 initializer, /*asmspec=*/NULL_TREE,
10957 prefix_attributes);
10958 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10959 cp_parser_save_default_args (parser, decl);
10962 /* Finish processing the declaration. But, skip friend
10963 declarations. */
10964 if (!friend_p && decl && decl != error_mark_node)
10966 cp_finish_decl (decl,
10967 initializer,
10968 asm_specification,
10969 /* If the initializer is in parentheses, then this is
10970 a direct-initialization, which means that an
10971 `explicit' constructor is OK. Otherwise, an
10972 `explicit' constructor cannot be used. */
10973 ((is_parenthesized_init || !is_initialized)
10974 ? 0 : LOOKUP_ONLYCONVERTING));
10976 if (!friend_p && pushed_scope)
10977 pop_scope (pushed_scope);
10979 /* Remember whether or not variables were initialized by
10980 constant-expressions. */
10981 if (decl && TREE_CODE (decl) == VAR_DECL
10982 && is_initialized && !is_non_constant_init)
10983 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10985 return decl;
10988 /* Parse a declarator.
10990 declarator:
10991 direct-declarator
10992 ptr-operator declarator
10994 abstract-declarator:
10995 ptr-operator abstract-declarator [opt]
10996 direct-abstract-declarator
10998 GNU Extensions:
11000 declarator:
11001 attributes [opt] direct-declarator
11002 attributes [opt] ptr-operator declarator
11004 abstract-declarator:
11005 attributes [opt] ptr-operator abstract-declarator [opt]
11006 attributes [opt] direct-abstract-declarator
11008 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11009 detect constructor, destructor or conversion operators. It is set
11010 to -1 if the declarator is a name, and +1 if it is a
11011 function. Otherwise it is set to zero. Usually you just want to
11012 test for >0, but internally the negative value is used.
11014 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11015 a decl-specifier-seq unless it declares a constructor, destructor,
11016 or conversion. It might seem that we could check this condition in
11017 semantic analysis, rather than parsing, but that makes it difficult
11018 to handle something like `f()'. We want to notice that there are
11019 no decl-specifiers, and therefore realize that this is an
11020 expression, not a declaration.)
11022 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11023 the declarator is a direct-declarator of the form "(...)".
11025 MEMBER_P is true iff this declarator is a member-declarator. */
11027 static cp_declarator *
11028 cp_parser_declarator (cp_parser* parser,
11029 cp_parser_declarator_kind dcl_kind,
11030 int* ctor_dtor_or_conv_p,
11031 bool* parenthesized_p,
11032 bool member_p)
11034 cp_token *token;
11035 cp_declarator *declarator;
11036 enum tree_code code;
11037 cp_cv_quals cv_quals;
11038 tree class_type;
11039 tree attributes = NULL_TREE;
11041 /* Assume this is not a constructor, destructor, or type-conversion
11042 operator. */
11043 if (ctor_dtor_or_conv_p)
11044 *ctor_dtor_or_conv_p = 0;
11046 if (cp_parser_allow_gnu_extensions_p (parser))
11047 attributes = cp_parser_attributes_opt (parser);
11049 /* Peek at the next token. */
11050 token = cp_lexer_peek_token (parser->lexer);
11052 /* Check for the ptr-operator production. */
11053 cp_parser_parse_tentatively (parser);
11054 /* Parse the ptr-operator. */
11055 code = cp_parser_ptr_operator (parser,
11056 &class_type,
11057 &cv_quals);
11058 /* If that worked, then we have a ptr-operator. */
11059 if (cp_parser_parse_definitely (parser))
11061 /* If a ptr-operator was found, then this declarator was not
11062 parenthesized. */
11063 if (parenthesized_p)
11064 *parenthesized_p = true;
11065 /* The dependent declarator is optional if we are parsing an
11066 abstract-declarator. */
11067 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11068 cp_parser_parse_tentatively (parser);
11070 /* Parse the dependent declarator. */
11071 declarator = cp_parser_declarator (parser, dcl_kind,
11072 /*ctor_dtor_or_conv_p=*/NULL,
11073 /*parenthesized_p=*/NULL,
11074 /*member_p=*/false);
11076 /* If we are parsing an abstract-declarator, we must handle the
11077 case where the dependent declarator is absent. */
11078 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11079 && !cp_parser_parse_definitely (parser))
11080 declarator = NULL;
11082 /* Build the representation of the ptr-operator. */
11083 if (class_type)
11084 declarator = make_ptrmem_declarator (cv_quals,
11085 class_type,
11086 declarator);
11087 else if (code == INDIRECT_REF)
11088 declarator = make_pointer_declarator (cv_quals, declarator);
11089 else
11090 declarator = make_reference_declarator (cv_quals, declarator);
11092 /* Everything else is a direct-declarator. */
11093 else
11095 if (parenthesized_p)
11096 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11097 CPP_OPEN_PAREN);
11098 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11099 ctor_dtor_or_conv_p,
11100 member_p);
11103 if (attributes && declarator != cp_error_declarator)
11104 declarator->attributes = attributes;
11106 return declarator;
11109 /* Parse a direct-declarator or direct-abstract-declarator.
11111 direct-declarator:
11112 declarator-id
11113 direct-declarator ( parameter-declaration-clause )
11114 cv-qualifier-seq [opt]
11115 exception-specification [opt]
11116 direct-declarator [ constant-expression [opt] ]
11117 ( declarator )
11119 direct-abstract-declarator:
11120 direct-abstract-declarator [opt]
11121 ( parameter-declaration-clause )
11122 cv-qualifier-seq [opt]
11123 exception-specification [opt]
11124 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11125 ( abstract-declarator )
11127 Returns a representation of the declarator. DCL_KIND is
11128 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11129 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11130 we are parsing a direct-declarator. It is
11131 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11132 of ambiguity we prefer an abstract declarator, as per
11133 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11134 cp_parser_declarator. */
11136 static cp_declarator *
11137 cp_parser_direct_declarator (cp_parser* parser,
11138 cp_parser_declarator_kind dcl_kind,
11139 int* ctor_dtor_or_conv_p,
11140 bool member_p)
11142 cp_token *token;
11143 cp_declarator *declarator = NULL;
11144 tree scope = NULL_TREE;
11145 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11146 bool saved_in_declarator_p = parser->in_declarator_p;
11147 bool first = true;
11148 tree pushed_scope = NULL_TREE;
11150 while (true)
11152 /* Peek at the next token. */
11153 token = cp_lexer_peek_token (parser->lexer);
11154 if (token->type == CPP_OPEN_PAREN)
11156 /* This is either a parameter-declaration-clause, or a
11157 parenthesized declarator. When we know we are parsing a
11158 named declarator, it must be a parenthesized declarator
11159 if FIRST is true. For instance, `(int)' is a
11160 parameter-declaration-clause, with an omitted
11161 direct-abstract-declarator. But `((*))', is a
11162 parenthesized abstract declarator. Finally, when T is a
11163 template parameter `(T)' is a
11164 parameter-declaration-clause, and not a parenthesized
11165 named declarator.
11167 We first try and parse a parameter-declaration-clause,
11168 and then try a nested declarator (if FIRST is true).
11170 It is not an error for it not to be a
11171 parameter-declaration-clause, even when FIRST is
11172 false. Consider,
11174 int i (int);
11175 int i (3);
11177 The first is the declaration of a function while the
11178 second is a the definition of a variable, including its
11179 initializer.
11181 Having seen only the parenthesis, we cannot know which of
11182 these two alternatives should be selected. Even more
11183 complex are examples like:
11185 int i (int (a));
11186 int i (int (3));
11188 The former is a function-declaration; the latter is a
11189 variable initialization.
11191 Thus again, we try a parameter-declaration-clause, and if
11192 that fails, we back out and return. */
11194 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11196 cp_parameter_declarator *params;
11197 unsigned saved_num_template_parameter_lists;
11199 /* In a member-declarator, the only valid interpretation
11200 of a parenthesis is the start of a
11201 parameter-declaration-clause. (It is invalid to
11202 initialize a static data member with a parenthesized
11203 initializer; only the "=" form of initialization is
11204 permitted.) */
11205 if (!member_p)
11206 cp_parser_parse_tentatively (parser);
11208 /* Consume the `('. */
11209 cp_lexer_consume_token (parser->lexer);
11210 if (first)
11212 /* If this is going to be an abstract declarator, we're
11213 in a declarator and we can't have default args. */
11214 parser->default_arg_ok_p = false;
11215 parser->in_declarator_p = true;
11218 /* Inside the function parameter list, surrounding
11219 template-parameter-lists do not apply. */
11220 saved_num_template_parameter_lists
11221 = parser->num_template_parameter_lists;
11222 parser->num_template_parameter_lists = 0;
11224 /* Parse the parameter-declaration-clause. */
11225 params = cp_parser_parameter_declaration_clause (parser);
11227 parser->num_template_parameter_lists
11228 = saved_num_template_parameter_lists;
11230 /* If all went well, parse the cv-qualifier-seq and the
11231 exception-specification. */
11232 if (member_p || cp_parser_parse_definitely (parser))
11234 cp_cv_quals cv_quals;
11235 tree exception_specification;
11237 if (ctor_dtor_or_conv_p)
11238 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11239 first = false;
11240 /* Consume the `)'. */
11241 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11243 /* Parse the cv-qualifier-seq. */
11244 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11245 /* And the exception-specification. */
11246 exception_specification
11247 = cp_parser_exception_specification_opt (parser);
11249 /* Create the function-declarator. */
11250 declarator = make_call_declarator (declarator,
11251 params,
11252 cv_quals,
11253 exception_specification);
11254 /* Any subsequent parameter lists are to do with
11255 return type, so are not those of the declared
11256 function. */
11257 parser->default_arg_ok_p = false;
11259 /* Repeat the main loop. */
11260 continue;
11264 /* If this is the first, we can try a parenthesized
11265 declarator. */
11266 if (first)
11268 bool saved_in_type_id_in_expr_p;
11270 parser->default_arg_ok_p = saved_default_arg_ok_p;
11271 parser->in_declarator_p = saved_in_declarator_p;
11273 /* Consume the `('. */
11274 cp_lexer_consume_token (parser->lexer);
11275 /* Parse the nested declarator. */
11276 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11277 parser->in_type_id_in_expr_p = true;
11278 declarator
11279 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11280 /*parenthesized_p=*/NULL,
11281 member_p);
11282 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11283 first = false;
11284 /* Expect a `)'. */
11285 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11286 declarator = cp_error_declarator;
11287 if (declarator == cp_error_declarator)
11288 break;
11290 goto handle_declarator;
11292 /* Otherwise, we must be done. */
11293 else
11294 break;
11296 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11297 && token->type == CPP_OPEN_SQUARE)
11299 /* Parse an array-declarator. */
11300 tree bounds;
11302 if (ctor_dtor_or_conv_p)
11303 *ctor_dtor_or_conv_p = 0;
11305 first = false;
11306 parser->default_arg_ok_p = false;
11307 parser->in_declarator_p = true;
11308 /* Consume the `['. */
11309 cp_lexer_consume_token (parser->lexer);
11310 /* Peek at the next token. */
11311 token = cp_lexer_peek_token (parser->lexer);
11312 /* If the next token is `]', then there is no
11313 constant-expression. */
11314 if (token->type != CPP_CLOSE_SQUARE)
11316 bool non_constant_p;
11318 bounds
11319 = cp_parser_constant_expression (parser,
11320 /*allow_non_constant=*/true,
11321 &non_constant_p);
11322 if (!non_constant_p)
11323 bounds = fold_non_dependent_expr (bounds);
11324 /* Normally, the array bound must be an integral constant
11325 expression. However, as an extension, we allow VLAs
11326 in function scopes. */
11327 else if (!at_function_scope_p ())
11329 error ("array bound is not an integer constant");
11330 bounds = error_mark_node;
11333 else
11334 bounds = NULL_TREE;
11335 /* Look for the closing `]'. */
11336 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11338 declarator = cp_error_declarator;
11339 break;
11342 declarator = make_array_declarator (declarator, bounds);
11344 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11346 tree qualifying_scope;
11347 tree unqualified_name;
11349 /* Parse a declarator-id */
11350 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11351 cp_parser_parse_tentatively (parser);
11352 unqualified_name = cp_parser_declarator_id (parser);
11353 qualifying_scope = parser->scope;
11354 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11356 if (!cp_parser_parse_definitely (parser))
11357 unqualified_name = error_mark_node;
11358 else if (qualifying_scope
11359 || (TREE_CODE (unqualified_name)
11360 != IDENTIFIER_NODE))
11362 cp_parser_error (parser, "expected unqualified-id");
11363 unqualified_name = error_mark_node;
11367 if (unqualified_name == error_mark_node)
11369 declarator = cp_error_declarator;
11370 break;
11373 if (qualifying_scope && at_namespace_scope_p ()
11374 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11376 /* In the declaration of a member of a template class
11377 outside of the class itself, the SCOPE will sometimes
11378 be a TYPENAME_TYPE. For example, given:
11380 template <typename T>
11381 int S<T>::R::i = 3;
11383 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11384 this context, we must resolve S<T>::R to an ordinary
11385 type, rather than a typename type.
11387 The reason we normally avoid resolving TYPENAME_TYPEs
11388 is that a specialization of `S' might render
11389 `S<T>::R' not a type. However, if `S' is
11390 specialized, then this `i' will not be used, so there
11391 is no harm in resolving the types here. */
11392 tree type;
11394 /* Resolve the TYPENAME_TYPE. */
11395 type = resolve_typename_type (qualifying_scope,
11396 /*only_current_p=*/false);
11397 /* If that failed, the declarator is invalid. */
11398 if (type == error_mark_node)
11399 error ("%<%T::%D%> is not a type",
11400 TYPE_CONTEXT (qualifying_scope),
11401 TYPE_IDENTIFIER (qualifying_scope));
11402 qualifying_scope = type;
11405 declarator = make_id_declarator (qualifying_scope,
11406 unqualified_name);
11407 declarator->id_loc = token->location;
11408 if (unqualified_name)
11410 tree class_type;
11412 if (qualifying_scope
11413 && CLASS_TYPE_P (qualifying_scope))
11414 class_type = qualifying_scope;
11415 else
11416 class_type = current_class_type;
11418 if (class_type)
11420 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11421 declarator->u.id.sfk = sfk_destructor;
11422 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11423 declarator->u.id.sfk = sfk_conversion;
11424 else if (/* There's no way to declare a constructor
11425 for an anonymous type, even if the type
11426 got a name for linkage purposes. */
11427 !TYPE_WAS_ANONYMOUS (class_type)
11428 && (constructor_name_p (unqualified_name,
11429 class_type)
11430 || (TREE_CODE (unqualified_name) == TYPE_DECL
11431 && (same_type_p
11432 (TREE_TYPE (unqualified_name),
11433 class_type)))))
11434 declarator->u.id.sfk = sfk_constructor;
11436 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11437 *ctor_dtor_or_conv_p = -1;
11438 if (qualifying_scope
11439 && TREE_CODE (unqualified_name) == TYPE_DECL
11440 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11442 error ("invalid use of constructor as a template");
11443 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11444 "the constructor in a qualified name",
11445 class_type,
11446 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11447 class_type, class_type);
11452 handle_declarator:;
11453 scope = get_scope_of_declarator (declarator);
11454 if (scope)
11455 /* Any names that appear after the declarator-id for a
11456 member are looked up in the containing scope. */
11457 pushed_scope = push_scope (scope);
11458 parser->in_declarator_p = true;
11459 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11460 || (declarator && declarator->kind == cdk_id))
11461 /* Default args are only allowed on function
11462 declarations. */
11463 parser->default_arg_ok_p = saved_default_arg_ok_p;
11464 else
11465 parser->default_arg_ok_p = false;
11467 first = false;
11469 /* We're done. */
11470 else
11471 break;
11474 /* For an abstract declarator, we might wind up with nothing at this
11475 point. That's an error; the declarator is not optional. */
11476 if (!declarator)
11477 cp_parser_error (parser, "expected declarator");
11479 /* If we entered a scope, we must exit it now. */
11480 if (pushed_scope)
11481 pop_scope (pushed_scope);
11483 parser->default_arg_ok_p = saved_default_arg_ok_p;
11484 parser->in_declarator_p = saved_in_declarator_p;
11486 return declarator;
11489 /* Parse a ptr-operator.
11491 ptr-operator:
11492 * cv-qualifier-seq [opt]
11494 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11496 GNU Extension:
11498 ptr-operator:
11499 & cv-qualifier-seq [opt]
11501 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11502 Returns ADDR_EXPR if a reference was used. In the case of a
11503 pointer-to-member, *TYPE is filled in with the TYPE containing the
11504 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11505 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11506 ERROR_MARK if an error occurred. */
11508 static enum tree_code
11509 cp_parser_ptr_operator (cp_parser* parser,
11510 tree* type,
11511 cp_cv_quals *cv_quals)
11513 enum tree_code code = ERROR_MARK;
11514 cp_token *token;
11516 /* Assume that it's not a pointer-to-member. */
11517 *type = NULL_TREE;
11518 /* And that there are no cv-qualifiers. */
11519 *cv_quals = TYPE_UNQUALIFIED;
11521 /* Peek at the next token. */
11522 token = cp_lexer_peek_token (parser->lexer);
11523 /* If it's a `*' or `&' we have a pointer or reference. */
11524 if (token->type == CPP_MULT || token->type == CPP_AND)
11526 /* Remember which ptr-operator we were processing. */
11527 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11529 /* Consume the `*' or `&'. */
11530 cp_lexer_consume_token (parser->lexer);
11532 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11533 `&', if we are allowing GNU extensions. (The only qualifier
11534 that can legally appear after `&' is `restrict', but that is
11535 enforced during semantic analysis. */
11536 if (code == INDIRECT_REF
11537 || cp_parser_allow_gnu_extensions_p (parser))
11538 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11540 else
11542 /* Try the pointer-to-member case. */
11543 cp_parser_parse_tentatively (parser);
11544 /* Look for the optional `::' operator. */
11545 cp_parser_global_scope_opt (parser,
11546 /*current_scope_valid_p=*/false);
11547 /* Look for the nested-name specifier. */
11548 cp_parser_nested_name_specifier (parser,
11549 /*typename_keyword_p=*/false,
11550 /*check_dependency_p=*/true,
11551 /*type_p=*/false,
11552 /*is_declaration=*/false);
11553 /* If we found it, and the next token is a `*', then we are
11554 indeed looking at a pointer-to-member operator. */
11555 if (!cp_parser_error_occurred (parser)
11556 && cp_parser_require (parser, CPP_MULT, "`*'"))
11558 /* The type of which the member is a member is given by the
11559 current SCOPE. */
11560 *type = parser->scope;
11561 /* The next name will not be qualified. */
11562 parser->scope = NULL_TREE;
11563 parser->qualifying_scope = NULL_TREE;
11564 parser->object_scope = NULL_TREE;
11565 /* Indicate that the `*' operator was used. */
11566 code = INDIRECT_REF;
11567 /* Look for the optional cv-qualifier-seq. */
11568 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11570 /* If that didn't work we don't have a ptr-operator. */
11571 if (!cp_parser_parse_definitely (parser))
11572 cp_parser_error (parser, "expected ptr-operator");
11575 return code;
11578 /* Parse an (optional) cv-qualifier-seq.
11580 cv-qualifier-seq:
11581 cv-qualifier cv-qualifier-seq [opt]
11583 cv-qualifier:
11584 const
11585 volatile
11587 GNU Extension:
11589 cv-qualifier:
11590 __restrict__
11592 Returns a bitmask representing the cv-qualifiers. */
11594 static cp_cv_quals
11595 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11597 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11599 while (true)
11601 cp_token *token;
11602 cp_cv_quals cv_qualifier;
11604 /* Peek at the next token. */
11605 token = cp_lexer_peek_token (parser->lexer);
11606 /* See if it's a cv-qualifier. */
11607 switch (token->keyword)
11609 case RID_CONST:
11610 cv_qualifier = TYPE_QUAL_CONST;
11611 break;
11613 case RID_VOLATILE:
11614 cv_qualifier = TYPE_QUAL_VOLATILE;
11615 break;
11617 case RID_RESTRICT:
11618 cv_qualifier = TYPE_QUAL_RESTRICT;
11619 break;
11621 default:
11622 cv_qualifier = TYPE_UNQUALIFIED;
11623 break;
11626 if (!cv_qualifier)
11627 break;
11629 if (cv_quals & cv_qualifier)
11631 error ("duplicate cv-qualifier");
11632 cp_lexer_purge_token (parser->lexer);
11634 else
11636 cp_lexer_consume_token (parser->lexer);
11637 cv_quals |= cv_qualifier;
11641 return cv_quals;
11644 /* Parse a declarator-id.
11646 declarator-id:
11647 id-expression
11648 :: [opt] nested-name-specifier [opt] type-name
11650 In the `id-expression' case, the value returned is as for
11651 cp_parser_id_expression if the id-expression was an unqualified-id.
11652 If the id-expression was a qualified-id, then a SCOPE_REF is
11653 returned. The first operand is the scope (either a NAMESPACE_DECL
11654 or TREE_TYPE), but the second is still just a representation of an
11655 unqualified-id. */
11657 static tree
11658 cp_parser_declarator_id (cp_parser* parser)
11660 /* The expression must be an id-expression. Assume that qualified
11661 names are the names of types so that:
11663 template <class T>
11664 int S<T>::R::i = 3;
11666 will work; we must treat `S<T>::R' as the name of a type.
11667 Similarly, assume that qualified names are templates, where
11668 required, so that:
11670 template <class T>
11671 int S<T>::R<T>::i = 3;
11673 will work, too. */
11674 return cp_parser_id_expression (parser,
11675 /*template_keyword_p=*/false,
11676 /*check_dependency_p=*/false,
11677 /*template_p=*/NULL,
11678 /*declarator_p=*/true);
11681 /* Parse a type-id.
11683 type-id:
11684 type-specifier-seq abstract-declarator [opt]
11686 Returns the TYPE specified. */
11688 static tree
11689 cp_parser_type_id (cp_parser* parser)
11691 cp_decl_specifier_seq type_specifier_seq;
11692 cp_declarator *abstract_declarator;
11694 /* Parse the type-specifier-seq. */
11695 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11696 &type_specifier_seq);
11697 if (type_specifier_seq.type == error_mark_node)
11698 return error_mark_node;
11700 /* There might or might not be an abstract declarator. */
11701 cp_parser_parse_tentatively (parser);
11702 /* Look for the declarator. */
11703 abstract_declarator
11704 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11705 /*parenthesized_p=*/NULL,
11706 /*member_p=*/false);
11707 /* Check to see if there really was a declarator. */
11708 if (!cp_parser_parse_definitely (parser))
11709 abstract_declarator = NULL;
11711 return groktypename (&type_specifier_seq, abstract_declarator);
11714 /* Parse a type-specifier-seq.
11716 type-specifier-seq:
11717 type-specifier type-specifier-seq [opt]
11719 GNU extension:
11721 type-specifier-seq:
11722 attributes type-specifier-seq [opt]
11724 If IS_CONDITION is true, we are at the start of a "condition",
11725 e.g., we've just seen "if (".
11727 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11729 static void
11730 cp_parser_type_specifier_seq (cp_parser* parser,
11731 bool is_condition,
11732 cp_decl_specifier_seq *type_specifier_seq)
11734 bool seen_type_specifier = false;
11735 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11737 /* Clear the TYPE_SPECIFIER_SEQ. */
11738 clear_decl_specs (type_specifier_seq);
11740 /* Parse the type-specifiers and attributes. */
11741 while (true)
11743 tree type_specifier;
11744 bool is_cv_qualifier;
11746 /* Check for attributes first. */
11747 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11749 type_specifier_seq->attributes =
11750 chainon (type_specifier_seq->attributes,
11751 cp_parser_attributes_opt (parser));
11752 continue;
11755 /* Look for the type-specifier. */
11756 type_specifier = cp_parser_type_specifier (parser,
11757 flags,
11758 type_specifier_seq,
11759 /*is_declaration=*/false,
11760 NULL,
11761 &is_cv_qualifier);
11762 if (!type_specifier)
11764 /* If the first type-specifier could not be found, this is not a
11765 type-specifier-seq at all. */
11766 if (!seen_type_specifier)
11768 cp_parser_error (parser, "expected type-specifier");
11769 type_specifier_seq->type = error_mark_node;
11770 return;
11772 /* If subsequent type-specifiers could not be found, the
11773 type-specifier-seq is complete. */
11774 break;
11777 seen_type_specifier = true;
11778 /* The standard says that a condition can be:
11780 type-specifier-seq declarator = assignment-expression
11782 However, given:
11784 struct S {};
11785 if (int S = ...)
11787 we should treat the "S" as a declarator, not as a
11788 type-specifier. The standard doesn't say that explicitly for
11789 type-specifier-seq, but it does say that for
11790 decl-specifier-seq in an ordinary declaration. Perhaps it
11791 would be clearer just to allow a decl-specifier-seq here, and
11792 then add a semantic restriction that if any decl-specifiers
11793 that are not type-specifiers appear, the program is invalid. */
11794 if (is_condition && !is_cv_qualifier)
11795 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11798 return;
11801 /* Parse a parameter-declaration-clause.
11803 parameter-declaration-clause:
11804 parameter-declaration-list [opt] ... [opt]
11805 parameter-declaration-list , ...
11807 Returns a representation for the parameter declarations. A return
11808 value of NULL indicates a parameter-declaration-clause consisting
11809 only of an ellipsis. */
11811 static cp_parameter_declarator *
11812 cp_parser_parameter_declaration_clause (cp_parser* parser)
11814 cp_parameter_declarator *parameters;
11815 cp_token *token;
11816 bool ellipsis_p;
11817 bool is_error;
11819 /* Peek at the next token. */
11820 token = cp_lexer_peek_token (parser->lexer);
11821 /* Check for trivial parameter-declaration-clauses. */
11822 if (token->type == CPP_ELLIPSIS)
11824 /* Consume the `...' token. */
11825 cp_lexer_consume_token (parser->lexer);
11826 return NULL;
11828 else if (token->type == CPP_CLOSE_PAREN)
11829 /* There are no parameters. */
11831 #ifndef NO_IMPLICIT_EXTERN_C
11832 if (in_system_header && current_class_type == NULL
11833 && current_lang_name == lang_name_c)
11834 return NULL;
11835 else
11836 #endif
11837 return no_parameters;
11839 /* Check for `(void)', too, which is a special case. */
11840 else if (token->keyword == RID_VOID
11841 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11842 == CPP_CLOSE_PAREN))
11844 /* Consume the `void' token. */
11845 cp_lexer_consume_token (parser->lexer);
11846 /* There are no parameters. */
11847 return no_parameters;
11850 /* Parse the parameter-declaration-list. */
11851 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11852 /* If a parse error occurred while parsing the
11853 parameter-declaration-list, then the entire
11854 parameter-declaration-clause is erroneous. */
11855 if (is_error)
11856 return NULL;
11858 /* Peek at the next token. */
11859 token = cp_lexer_peek_token (parser->lexer);
11860 /* If it's a `,', the clause should terminate with an ellipsis. */
11861 if (token->type == CPP_COMMA)
11863 /* Consume the `,'. */
11864 cp_lexer_consume_token (parser->lexer);
11865 /* Expect an ellipsis. */
11866 ellipsis_p
11867 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11869 /* It might also be `...' if the optional trailing `,' was
11870 omitted. */
11871 else if (token->type == CPP_ELLIPSIS)
11873 /* Consume the `...' token. */
11874 cp_lexer_consume_token (parser->lexer);
11875 /* And remember that we saw it. */
11876 ellipsis_p = true;
11878 else
11879 ellipsis_p = false;
11881 /* Finish the parameter list. */
11882 if (parameters && ellipsis_p)
11883 parameters->ellipsis_p = true;
11885 return parameters;
11888 /* Parse a parameter-declaration-list.
11890 parameter-declaration-list:
11891 parameter-declaration
11892 parameter-declaration-list , parameter-declaration
11894 Returns a representation of the parameter-declaration-list, as for
11895 cp_parser_parameter_declaration_clause. However, the
11896 `void_list_node' is never appended to the list. Upon return,
11897 *IS_ERROR will be true iff an error occurred. */
11899 static cp_parameter_declarator *
11900 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11902 cp_parameter_declarator *parameters = NULL;
11903 cp_parameter_declarator **tail = &parameters;
11905 /* Assume all will go well. */
11906 *is_error = false;
11908 /* Look for more parameters. */
11909 while (true)
11911 cp_parameter_declarator *parameter;
11912 bool parenthesized_p;
11913 /* Parse the parameter. */
11914 parameter
11915 = cp_parser_parameter_declaration (parser,
11916 /*template_parm_p=*/false,
11917 &parenthesized_p);
11919 /* If a parse error occurred parsing the parameter declaration,
11920 then the entire parameter-declaration-list is erroneous. */
11921 if (!parameter)
11923 *is_error = true;
11924 parameters = NULL;
11925 break;
11927 /* Add the new parameter to the list. */
11928 *tail = parameter;
11929 tail = &parameter->next;
11931 /* Peek at the next token. */
11932 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11933 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11934 /* These are for Objective-C++ */
11935 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11936 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11937 /* The parameter-declaration-list is complete. */
11938 break;
11939 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11941 cp_token *token;
11943 /* Peek at the next token. */
11944 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11945 /* If it's an ellipsis, then the list is complete. */
11946 if (token->type == CPP_ELLIPSIS)
11947 break;
11948 /* Otherwise, there must be more parameters. Consume the
11949 `,'. */
11950 cp_lexer_consume_token (parser->lexer);
11951 /* When parsing something like:
11953 int i(float f, double d)
11955 we can tell after seeing the declaration for "f" that we
11956 are not looking at an initialization of a variable "i",
11957 but rather at the declaration of a function "i".
11959 Due to the fact that the parsing of template arguments
11960 (as specified to a template-id) requires backtracking we
11961 cannot use this technique when inside a template argument
11962 list. */
11963 if (!parser->in_template_argument_list_p
11964 && !parser->in_type_id_in_expr_p
11965 && cp_parser_uncommitted_to_tentative_parse_p (parser)
11966 /* However, a parameter-declaration of the form
11967 "foat(f)" (which is a valid declaration of a
11968 parameter "f") can also be interpreted as an
11969 expression (the conversion of "f" to "float"). */
11970 && !parenthesized_p)
11971 cp_parser_commit_to_tentative_parse (parser);
11973 else
11975 cp_parser_error (parser, "expected %<,%> or %<...%>");
11976 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11977 cp_parser_skip_to_closing_parenthesis (parser,
11978 /*recovering=*/true,
11979 /*or_comma=*/false,
11980 /*consume_paren=*/false);
11981 break;
11985 return parameters;
11988 /* Parse a parameter declaration.
11990 parameter-declaration:
11991 decl-specifier-seq declarator
11992 decl-specifier-seq declarator = assignment-expression
11993 decl-specifier-seq abstract-declarator [opt]
11994 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11996 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11997 declares a template parameter. (In that case, a non-nested `>'
11998 token encountered during the parsing of the assignment-expression
11999 is not interpreted as a greater-than operator.)
12001 Returns a representation of the parameter, or NULL if an error
12002 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12003 true iff the declarator is of the form "(p)". */
12005 static cp_parameter_declarator *
12006 cp_parser_parameter_declaration (cp_parser *parser,
12007 bool template_parm_p,
12008 bool *parenthesized_p)
12010 int declares_class_or_enum;
12011 bool greater_than_is_operator_p;
12012 cp_decl_specifier_seq decl_specifiers;
12013 cp_declarator *declarator;
12014 tree default_argument;
12015 cp_token *token;
12016 const char *saved_message;
12018 /* In a template parameter, `>' is not an operator.
12020 [temp.param]
12022 When parsing a default template-argument for a non-type
12023 template-parameter, the first non-nested `>' is taken as the end
12024 of the template parameter-list rather than a greater-than
12025 operator. */
12026 greater_than_is_operator_p = !template_parm_p;
12028 /* Type definitions may not appear in parameter types. */
12029 saved_message = parser->type_definition_forbidden_message;
12030 parser->type_definition_forbidden_message
12031 = "types may not be defined in parameter types";
12033 /* Parse the declaration-specifiers. */
12034 cp_parser_decl_specifier_seq (parser,
12035 CP_PARSER_FLAGS_NONE,
12036 &decl_specifiers,
12037 &declares_class_or_enum);
12038 /* If an error occurred, there's no reason to attempt to parse the
12039 rest of the declaration. */
12040 if (cp_parser_error_occurred (parser))
12042 parser->type_definition_forbidden_message = saved_message;
12043 return NULL;
12046 /* Peek at the next token. */
12047 token = cp_lexer_peek_token (parser->lexer);
12048 /* If the next token is a `)', `,', `=', `>', or `...', then there
12049 is no declarator. */
12050 if (token->type == CPP_CLOSE_PAREN
12051 || token->type == CPP_COMMA
12052 || token->type == CPP_EQ
12053 || token->type == CPP_ELLIPSIS
12054 || token->type == CPP_GREATER)
12056 declarator = NULL;
12057 if (parenthesized_p)
12058 *parenthesized_p = false;
12060 /* Otherwise, there should be a declarator. */
12061 else
12063 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12064 parser->default_arg_ok_p = false;
12066 /* After seeing a decl-specifier-seq, if the next token is not a
12067 "(", there is no possibility that the code is a valid
12068 expression. Therefore, if parsing tentatively, we commit at
12069 this point. */
12070 if (!parser->in_template_argument_list_p
12071 /* In an expression context, having seen:
12073 (int((char ...
12075 we cannot be sure whether we are looking at a
12076 function-type (taking a "char" as a parameter) or a cast
12077 of some object of type "char" to "int". */
12078 && !parser->in_type_id_in_expr_p
12079 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12080 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12081 cp_parser_commit_to_tentative_parse (parser);
12082 /* Parse the declarator. */
12083 declarator = cp_parser_declarator (parser,
12084 CP_PARSER_DECLARATOR_EITHER,
12085 /*ctor_dtor_or_conv_p=*/NULL,
12086 parenthesized_p,
12087 /*member_p=*/false);
12088 parser->default_arg_ok_p = saved_default_arg_ok_p;
12089 /* After the declarator, allow more attributes. */
12090 decl_specifiers.attributes
12091 = chainon (decl_specifiers.attributes,
12092 cp_parser_attributes_opt (parser));
12095 /* The restriction on defining new types applies only to the type
12096 of the parameter, not to the default argument. */
12097 parser->type_definition_forbidden_message = saved_message;
12099 /* If the next token is `=', then process a default argument. */
12100 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12102 bool saved_greater_than_is_operator_p;
12103 /* Consume the `='. */
12104 cp_lexer_consume_token (parser->lexer);
12106 /* If we are defining a class, then the tokens that make up the
12107 default argument must be saved and processed later. */
12108 if (!template_parm_p && at_class_scope_p ()
12109 && TYPE_BEING_DEFINED (current_class_type))
12111 unsigned depth = 0;
12112 cp_token *first_token;
12113 cp_token *token;
12115 /* Add tokens until we have processed the entire default
12116 argument. We add the range [first_token, token). */
12117 first_token = cp_lexer_peek_token (parser->lexer);
12118 while (true)
12120 bool done = false;
12122 /* Peek at the next token. */
12123 token = cp_lexer_peek_token (parser->lexer);
12124 /* What we do depends on what token we have. */
12125 switch (token->type)
12127 /* In valid code, a default argument must be
12128 immediately followed by a `,' `)', or `...'. */
12129 case CPP_COMMA:
12130 case CPP_CLOSE_PAREN:
12131 case CPP_ELLIPSIS:
12132 /* If we run into a non-nested `;', `}', or `]',
12133 then the code is invalid -- but the default
12134 argument is certainly over. */
12135 case CPP_SEMICOLON:
12136 case CPP_CLOSE_BRACE:
12137 case CPP_CLOSE_SQUARE:
12138 if (depth == 0)
12139 done = true;
12140 /* Update DEPTH, if necessary. */
12141 else if (token->type == CPP_CLOSE_PAREN
12142 || token->type == CPP_CLOSE_BRACE
12143 || token->type == CPP_CLOSE_SQUARE)
12144 --depth;
12145 break;
12147 case CPP_OPEN_PAREN:
12148 case CPP_OPEN_SQUARE:
12149 case CPP_OPEN_BRACE:
12150 ++depth;
12151 break;
12153 case CPP_GREATER:
12154 /* If we see a non-nested `>', and `>' is not an
12155 operator, then it marks the end of the default
12156 argument. */
12157 if (!depth && !greater_than_is_operator_p)
12158 done = true;
12159 break;
12161 /* If we run out of tokens, issue an error message. */
12162 case CPP_EOF:
12163 error ("file ends in default argument");
12164 done = true;
12165 break;
12167 case CPP_NAME:
12168 case CPP_SCOPE:
12169 /* In these cases, we should look for template-ids.
12170 For example, if the default argument is
12171 `X<int, double>()', we need to do name lookup to
12172 figure out whether or not `X' is a template; if
12173 so, the `,' does not end the default argument.
12175 That is not yet done. */
12176 break;
12178 default:
12179 break;
12182 /* If we've reached the end, stop. */
12183 if (done)
12184 break;
12186 /* Add the token to the token block. */
12187 token = cp_lexer_consume_token (parser->lexer);
12190 /* Create a DEFAULT_ARG to represented the unparsed default
12191 argument. */
12192 default_argument = make_node (DEFAULT_ARG);
12193 DEFARG_TOKENS (default_argument)
12194 = cp_token_cache_new (first_token, token);
12195 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12197 /* Outside of a class definition, we can just parse the
12198 assignment-expression. */
12199 else
12201 bool saved_local_variables_forbidden_p;
12203 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12204 set correctly. */
12205 saved_greater_than_is_operator_p
12206 = parser->greater_than_is_operator_p;
12207 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12208 /* Local variable names (and the `this' keyword) may not
12209 appear in a default argument. */
12210 saved_local_variables_forbidden_p
12211 = parser->local_variables_forbidden_p;
12212 parser->local_variables_forbidden_p = true;
12213 /* Parse the assignment-expression. */
12214 default_argument
12215 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12216 /* Restore saved state. */
12217 parser->greater_than_is_operator_p
12218 = saved_greater_than_is_operator_p;
12219 parser->local_variables_forbidden_p
12220 = saved_local_variables_forbidden_p;
12222 if (!parser->default_arg_ok_p)
12224 if (!flag_pedantic_errors)
12225 warning (0, "deprecated use of default argument for parameter of non-function");
12226 else
12228 error ("default arguments are only permitted for function parameters");
12229 default_argument = NULL_TREE;
12233 else
12234 default_argument = NULL_TREE;
12236 return make_parameter_declarator (&decl_specifiers,
12237 declarator,
12238 default_argument);
12241 /* Parse a function-body.
12243 function-body:
12244 compound_statement */
12246 static void
12247 cp_parser_function_body (cp_parser *parser)
12249 cp_parser_compound_statement (parser, NULL, false);
12252 /* Parse a ctor-initializer-opt followed by a function-body. Return
12253 true if a ctor-initializer was present. */
12255 static bool
12256 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12258 tree body;
12259 bool ctor_initializer_p;
12261 /* Begin the function body. */
12262 body = begin_function_body ();
12263 /* Parse the optional ctor-initializer. */
12264 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12265 /* Parse the function-body. */
12266 cp_parser_function_body (parser);
12267 /* Finish the function body. */
12268 finish_function_body (body);
12270 return ctor_initializer_p;
12273 /* Parse an initializer.
12275 initializer:
12276 = initializer-clause
12277 ( expression-list )
12279 Returns an expression representing the initializer. If no
12280 initializer is present, NULL_TREE is returned.
12282 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12283 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12284 set to FALSE if there is no initializer present. If there is an
12285 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12286 is set to true; otherwise it is set to false. */
12288 static tree
12289 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12290 bool* non_constant_p)
12292 cp_token *token;
12293 tree init;
12295 /* Peek at the next token. */
12296 token = cp_lexer_peek_token (parser->lexer);
12298 /* Let our caller know whether or not this initializer was
12299 parenthesized. */
12300 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12301 /* Assume that the initializer is constant. */
12302 *non_constant_p = false;
12304 if (token->type == CPP_EQ)
12306 /* Consume the `='. */
12307 cp_lexer_consume_token (parser->lexer);
12308 /* Parse the initializer-clause. */
12309 init = cp_parser_initializer_clause (parser, non_constant_p);
12311 else if (token->type == CPP_OPEN_PAREN)
12312 init = cp_parser_parenthesized_expression_list (parser, false,
12313 /*cast_p=*/false,
12314 non_constant_p);
12315 else
12317 /* Anything else is an error. */
12318 cp_parser_error (parser, "expected initializer");
12319 init = error_mark_node;
12322 return init;
12325 /* Parse an initializer-clause.
12327 initializer-clause:
12328 assignment-expression
12329 { initializer-list , [opt] }
12332 Returns an expression representing the initializer.
12334 If the `assignment-expression' production is used the value
12335 returned is simply a representation for the expression.
12337 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12338 the elements of the initializer-list (or NULL, if the last
12339 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12340 NULL_TREE. There is no way to detect whether or not the optional
12341 trailing `,' was provided. NON_CONSTANT_P is as for
12342 cp_parser_initializer. */
12344 static tree
12345 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12347 tree initializer;
12349 /* Assume the expression is constant. */
12350 *non_constant_p = false;
12352 /* If it is not a `{', then we are looking at an
12353 assignment-expression. */
12354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12356 initializer
12357 = cp_parser_constant_expression (parser,
12358 /*allow_non_constant_p=*/true,
12359 non_constant_p);
12360 if (!*non_constant_p)
12361 initializer = fold_non_dependent_expr (initializer);
12363 else
12365 /* Consume the `{' token. */
12366 cp_lexer_consume_token (parser->lexer);
12367 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12368 initializer = make_node (CONSTRUCTOR);
12369 /* If it's not a `}', then there is a non-trivial initializer. */
12370 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12372 /* Parse the initializer list. */
12373 CONSTRUCTOR_ELTS (initializer)
12374 = cp_parser_initializer_list (parser, non_constant_p);
12375 /* A trailing `,' token is allowed. */
12376 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12377 cp_lexer_consume_token (parser->lexer);
12379 /* Now, there should be a trailing `}'. */
12380 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12383 return initializer;
12386 /* Parse an initializer-list.
12388 initializer-list:
12389 initializer-clause
12390 initializer-list , initializer-clause
12392 GNU Extension:
12394 initializer-list:
12395 identifier : initializer-clause
12396 initializer-list, identifier : initializer-clause
12398 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12399 for the initializer. If the INDEX of the elt is non-NULL, it is the
12400 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12401 as for cp_parser_initializer. */
12403 static VEC(constructor_elt,gc) *
12404 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12406 VEC(constructor_elt,gc) *v = NULL;
12408 /* Assume all of the expressions are constant. */
12409 *non_constant_p = false;
12411 /* Parse the rest of the list. */
12412 while (true)
12414 cp_token *token;
12415 tree identifier;
12416 tree initializer;
12417 bool clause_non_constant_p;
12419 /* If the next token is an identifier and the following one is a
12420 colon, we are looking at the GNU designated-initializer
12421 syntax. */
12422 if (cp_parser_allow_gnu_extensions_p (parser)
12423 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12424 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12426 /* Consume the identifier. */
12427 identifier = cp_lexer_consume_token (parser->lexer)->value;
12428 /* Consume the `:'. */
12429 cp_lexer_consume_token (parser->lexer);
12431 else
12432 identifier = NULL_TREE;
12434 /* Parse the initializer. */
12435 initializer = cp_parser_initializer_clause (parser,
12436 &clause_non_constant_p);
12437 /* If any clause is non-constant, so is the entire initializer. */
12438 if (clause_non_constant_p)
12439 *non_constant_p = true;
12441 /* Add it to the vector. */
12442 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12444 /* If the next token is not a comma, we have reached the end of
12445 the list. */
12446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12447 break;
12449 /* Peek at the next token. */
12450 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12451 /* If the next token is a `}', then we're still done. An
12452 initializer-clause can have a trailing `,' after the
12453 initializer-list and before the closing `}'. */
12454 if (token->type == CPP_CLOSE_BRACE)
12455 break;
12457 /* Consume the `,' token. */
12458 cp_lexer_consume_token (parser->lexer);
12461 return v;
12464 /* Classes [gram.class] */
12466 /* Parse a class-name.
12468 class-name:
12469 identifier
12470 template-id
12472 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12473 to indicate that names looked up in dependent types should be
12474 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12475 keyword has been used to indicate that the name that appears next
12476 is a template. TAG_TYPE indicates the explicit tag given before
12477 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12478 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12479 is the class being defined in a class-head.
12481 Returns the TYPE_DECL representing the class. */
12483 static tree
12484 cp_parser_class_name (cp_parser *parser,
12485 bool typename_keyword_p,
12486 bool template_keyword_p,
12487 enum tag_types tag_type,
12488 bool check_dependency_p,
12489 bool class_head_p,
12490 bool is_declaration)
12492 tree decl;
12493 tree scope;
12494 bool typename_p;
12495 cp_token *token;
12497 /* All class-names start with an identifier. */
12498 token = cp_lexer_peek_token (parser->lexer);
12499 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12501 cp_parser_error (parser, "expected class-name");
12502 return error_mark_node;
12505 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12506 to a template-id, so we save it here. */
12507 scope = parser->scope;
12508 if (scope == error_mark_node)
12509 return error_mark_node;
12511 /* Any name names a type if we're following the `typename' keyword
12512 in a qualified name where the enclosing scope is type-dependent. */
12513 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12514 && dependent_type_p (scope));
12515 /* Handle the common case (an identifier, but not a template-id)
12516 efficiently. */
12517 if (token->type == CPP_NAME
12518 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12520 cp_token *identifier_token;
12521 tree identifier;
12522 bool ambiguous_p;
12524 /* Look for the identifier. */
12525 identifier_token = cp_lexer_peek_token (parser->lexer);
12526 ambiguous_p = identifier_token->ambiguous_p;
12527 identifier = cp_parser_identifier (parser);
12528 /* If the next token isn't an identifier, we are certainly not
12529 looking at a class-name. */
12530 if (identifier == error_mark_node)
12531 decl = error_mark_node;
12532 /* If we know this is a type-name, there's no need to look it
12533 up. */
12534 else if (typename_p)
12535 decl = identifier;
12536 else
12538 tree ambiguous_decls;
12539 /* If we already know that this lookup is ambiguous, then
12540 we've already issued an error message; there's no reason
12541 to check again. */
12542 if (ambiguous_p)
12544 cp_parser_simulate_error (parser);
12545 return error_mark_node;
12547 /* If the next token is a `::', then the name must be a type
12548 name.
12550 [basic.lookup.qual]
12552 During the lookup for a name preceding the :: scope
12553 resolution operator, object, function, and enumerator
12554 names are ignored. */
12555 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12556 tag_type = typename_type;
12557 /* Look up the name. */
12558 decl = cp_parser_lookup_name (parser, identifier,
12559 tag_type,
12560 /*is_template=*/false,
12561 /*is_namespace=*/false,
12562 check_dependency_p,
12563 &ambiguous_decls);
12564 if (ambiguous_decls)
12566 error ("reference to %qD is ambiguous", identifier);
12567 print_candidates (ambiguous_decls);
12568 if (cp_parser_parsing_tentatively (parser))
12570 identifier_token->ambiguous_p = true;
12571 cp_parser_simulate_error (parser);
12573 return error_mark_node;
12577 else
12579 /* Try a template-id. */
12580 decl = cp_parser_template_id (parser, template_keyword_p,
12581 check_dependency_p,
12582 is_declaration);
12583 if (decl == error_mark_node)
12584 return error_mark_node;
12587 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12589 /* If this is a typename, create a TYPENAME_TYPE. */
12590 if (typename_p && decl != error_mark_node)
12592 decl = make_typename_type (scope, decl, typename_type,
12593 /*complain=*/tf_error);
12594 if (decl != error_mark_node)
12595 decl = TYPE_NAME (decl);
12598 /* Check to see that it is really the name of a class. */
12599 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12600 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12601 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12602 /* Situations like this:
12604 template <typename T> struct A {
12605 typename T::template X<int>::I i;
12608 are problematic. Is `T::template X<int>' a class-name? The
12609 standard does not seem to be definitive, but there is no other
12610 valid interpretation of the following `::'. Therefore, those
12611 names are considered class-names. */
12612 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12613 else if (decl == error_mark_node
12614 || TREE_CODE (decl) != TYPE_DECL
12615 || TREE_TYPE (decl) == error_mark_node
12616 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12618 cp_parser_error (parser, "expected class-name");
12619 return error_mark_node;
12622 return decl;
12625 /* Parse a class-specifier.
12627 class-specifier:
12628 class-head { member-specification [opt] }
12630 Returns the TREE_TYPE representing the class. */
12632 static tree
12633 cp_parser_class_specifier (cp_parser* parser)
12635 cp_token *token;
12636 tree type;
12637 tree attributes = NULL_TREE;
12638 int has_trailing_semicolon;
12639 bool nested_name_specifier_p;
12640 unsigned saved_num_template_parameter_lists;
12641 tree old_scope = NULL_TREE;
12642 tree scope = NULL_TREE;
12644 push_deferring_access_checks (dk_no_deferred);
12646 /* Parse the class-head. */
12647 type = cp_parser_class_head (parser,
12648 &nested_name_specifier_p,
12649 &attributes);
12650 /* If the class-head was a semantic disaster, skip the entire body
12651 of the class. */
12652 if (!type)
12654 cp_parser_skip_to_end_of_block_or_statement (parser);
12655 pop_deferring_access_checks ();
12656 return error_mark_node;
12659 /* Look for the `{'. */
12660 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12662 pop_deferring_access_checks ();
12663 return error_mark_node;
12666 /* Issue an error message if type-definitions are forbidden here. */
12667 cp_parser_check_type_definition (parser);
12668 /* Remember that we are defining one more class. */
12669 ++parser->num_classes_being_defined;
12670 /* Inside the class, surrounding template-parameter-lists do not
12671 apply. */
12672 saved_num_template_parameter_lists
12673 = parser->num_template_parameter_lists;
12674 parser->num_template_parameter_lists = 0;
12676 /* Start the class. */
12677 if (nested_name_specifier_p)
12679 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12680 old_scope = push_inner_scope (scope);
12682 type = begin_class_definition (type);
12684 if (type == error_mark_node)
12685 /* If the type is erroneous, skip the entire body of the class. */
12686 cp_parser_skip_to_closing_brace (parser);
12687 else
12688 /* Parse the member-specification. */
12689 cp_parser_member_specification_opt (parser);
12691 /* Look for the trailing `}'. */
12692 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12693 /* We get better error messages by noticing a common problem: a
12694 missing trailing `;'. */
12695 token = cp_lexer_peek_token (parser->lexer);
12696 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12697 /* Look for trailing attributes to apply to this class. */
12698 if (cp_parser_allow_gnu_extensions_p (parser))
12700 tree sub_attr = cp_parser_attributes_opt (parser);
12701 attributes = chainon (attributes, sub_attr);
12703 if (type != error_mark_node)
12704 type = finish_struct (type, attributes);
12705 if (nested_name_specifier_p)
12706 pop_inner_scope (old_scope, scope);
12707 /* If this class is not itself within the scope of another class,
12708 then we need to parse the bodies of all of the queued function
12709 definitions. Note that the queued functions defined in a class
12710 are not always processed immediately following the
12711 class-specifier for that class. Consider:
12713 struct A {
12714 struct B { void f() { sizeof (A); } };
12717 If `f' were processed before the processing of `A' were
12718 completed, there would be no way to compute the size of `A'.
12719 Note that the nesting we are interested in here is lexical --
12720 not the semantic nesting given by TYPE_CONTEXT. In particular,
12721 for:
12723 struct A { struct B; };
12724 struct A::B { void f() { } };
12726 there is no need to delay the parsing of `A::B::f'. */
12727 if (--parser->num_classes_being_defined == 0)
12729 tree queue_entry;
12730 tree fn;
12731 tree class_type = NULL_TREE;
12732 tree pushed_scope = NULL_TREE;
12734 /* In a first pass, parse default arguments to the functions.
12735 Then, in a second pass, parse the bodies of the functions.
12736 This two-phased approach handles cases like:
12738 struct S {
12739 void f() { g(); }
12740 void g(int i = 3);
12744 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12745 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12746 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12747 TREE_PURPOSE (parser->unparsed_functions_queues)
12748 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12750 fn = TREE_VALUE (queue_entry);
12751 /* If there are default arguments that have not yet been processed,
12752 take care of them now. */
12753 if (class_type != TREE_PURPOSE (queue_entry))
12755 if (pushed_scope)
12756 pop_scope (pushed_scope);
12757 class_type = TREE_PURPOSE (queue_entry);
12758 pushed_scope = push_scope (class_type);
12760 /* Make sure that any template parameters are in scope. */
12761 maybe_begin_member_template_processing (fn);
12762 /* Parse the default argument expressions. */
12763 cp_parser_late_parsing_default_args (parser, fn);
12764 /* Remove any template parameters from the symbol table. */
12765 maybe_end_member_template_processing ();
12767 if (pushed_scope)
12768 pop_scope (pushed_scope);
12769 /* Now parse the body of the functions. */
12770 for (TREE_VALUE (parser->unparsed_functions_queues)
12771 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12772 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12773 TREE_VALUE (parser->unparsed_functions_queues)
12774 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12776 /* Figure out which function we need to process. */
12777 fn = TREE_VALUE (queue_entry);
12778 /* Parse the function. */
12779 cp_parser_late_parsing_for_member (parser, fn);
12783 /* Put back any saved access checks. */
12784 pop_deferring_access_checks ();
12786 /* Restore the count of active template-parameter-lists. */
12787 parser->num_template_parameter_lists
12788 = saved_num_template_parameter_lists;
12790 return type;
12793 /* Parse a class-head.
12795 class-head:
12796 class-key identifier [opt] base-clause [opt]
12797 class-key nested-name-specifier identifier base-clause [opt]
12798 class-key nested-name-specifier [opt] template-id
12799 base-clause [opt]
12801 GNU Extensions:
12802 class-key attributes identifier [opt] base-clause [opt]
12803 class-key attributes nested-name-specifier identifier base-clause [opt]
12804 class-key attributes nested-name-specifier [opt] template-id
12805 base-clause [opt]
12807 Returns the TYPE of the indicated class. Sets
12808 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12809 involving a nested-name-specifier was used, and FALSE otherwise.
12811 Returns error_mark_node if this is not a class-head.
12813 Returns NULL_TREE if the class-head is syntactically valid, but
12814 semantically invalid in a way that means we should skip the entire
12815 body of the class. */
12817 static tree
12818 cp_parser_class_head (cp_parser* parser,
12819 bool* nested_name_specifier_p,
12820 tree *attributes_p)
12822 tree nested_name_specifier;
12823 enum tag_types class_key;
12824 tree id = NULL_TREE;
12825 tree type = NULL_TREE;
12826 tree attributes;
12827 bool template_id_p = false;
12828 bool qualified_p = false;
12829 bool invalid_nested_name_p = false;
12830 bool invalid_explicit_specialization_p = false;
12831 tree pushed_scope = NULL_TREE;
12832 unsigned num_templates;
12833 tree bases;
12835 /* Assume no nested-name-specifier will be present. */
12836 *nested_name_specifier_p = false;
12837 /* Assume no template parameter lists will be used in defining the
12838 type. */
12839 num_templates = 0;
12841 /* Look for the class-key. */
12842 class_key = cp_parser_class_key (parser);
12843 if (class_key == none_type)
12844 return error_mark_node;
12846 /* Parse the attributes. */
12847 attributes = cp_parser_attributes_opt (parser);
12849 /* If the next token is `::', that is invalid -- but sometimes
12850 people do try to write:
12852 struct ::S {};
12854 Handle this gracefully by accepting the extra qualifier, and then
12855 issuing an error about it later if this really is a
12856 class-head. If it turns out just to be an elaborated type
12857 specifier, remain silent. */
12858 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12859 qualified_p = true;
12861 push_deferring_access_checks (dk_no_check);
12863 /* Determine the name of the class. Begin by looking for an
12864 optional nested-name-specifier. */
12865 nested_name_specifier
12866 = cp_parser_nested_name_specifier_opt (parser,
12867 /*typename_keyword_p=*/false,
12868 /*check_dependency_p=*/false,
12869 /*type_p=*/false,
12870 /*is_declaration=*/false);
12871 /* If there was a nested-name-specifier, then there *must* be an
12872 identifier. */
12873 if (nested_name_specifier)
12875 /* Although the grammar says `identifier', it really means
12876 `class-name' or `template-name'. You are only allowed to
12877 define a class that has already been declared with this
12878 syntax.
12880 The proposed resolution for Core Issue 180 says that whever
12881 you see `class T::X' you should treat `X' as a type-name.
12883 It is OK to define an inaccessible class; for example:
12885 class A { class B; };
12886 class A::B {};
12888 We do not know if we will see a class-name, or a
12889 template-name. We look for a class-name first, in case the
12890 class-name is a template-id; if we looked for the
12891 template-name first we would stop after the template-name. */
12892 cp_parser_parse_tentatively (parser);
12893 type = cp_parser_class_name (parser,
12894 /*typename_keyword_p=*/false,
12895 /*template_keyword_p=*/false,
12896 class_type,
12897 /*check_dependency_p=*/false,
12898 /*class_head_p=*/true,
12899 /*is_declaration=*/false);
12900 /* If that didn't work, ignore the nested-name-specifier. */
12901 if (!cp_parser_parse_definitely (parser))
12903 invalid_nested_name_p = true;
12904 id = cp_parser_identifier (parser);
12905 if (id == error_mark_node)
12906 id = NULL_TREE;
12908 /* If we could not find a corresponding TYPE, treat this
12909 declaration like an unqualified declaration. */
12910 if (type == error_mark_node)
12911 nested_name_specifier = NULL_TREE;
12912 /* Otherwise, count the number of templates used in TYPE and its
12913 containing scopes. */
12914 else
12916 tree scope;
12918 for (scope = TREE_TYPE (type);
12919 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12920 scope = (TYPE_P (scope)
12921 ? TYPE_CONTEXT (scope)
12922 : DECL_CONTEXT (scope)))
12923 if (TYPE_P (scope)
12924 && CLASS_TYPE_P (scope)
12925 && CLASSTYPE_TEMPLATE_INFO (scope)
12926 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12927 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12928 ++num_templates;
12931 /* Otherwise, the identifier is optional. */
12932 else
12934 /* We don't know whether what comes next is a template-id,
12935 an identifier, or nothing at all. */
12936 cp_parser_parse_tentatively (parser);
12937 /* Check for a template-id. */
12938 id = cp_parser_template_id (parser,
12939 /*template_keyword_p=*/false,
12940 /*check_dependency_p=*/true,
12941 /*is_declaration=*/true);
12942 /* If that didn't work, it could still be an identifier. */
12943 if (!cp_parser_parse_definitely (parser))
12945 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12946 id = cp_parser_identifier (parser);
12947 else
12948 id = NULL_TREE;
12950 else
12952 template_id_p = true;
12953 ++num_templates;
12957 pop_deferring_access_checks ();
12959 if (id)
12960 cp_parser_check_for_invalid_template_id (parser, id);
12962 /* If it's not a `:' or a `{' then we can't really be looking at a
12963 class-head, since a class-head only appears as part of a
12964 class-specifier. We have to detect this situation before calling
12965 xref_tag, since that has irreversible side-effects. */
12966 if (!cp_parser_next_token_starts_class_definition_p (parser))
12968 cp_parser_error (parser, "expected %<{%> or %<:%>");
12969 return error_mark_node;
12972 /* At this point, we're going ahead with the class-specifier, even
12973 if some other problem occurs. */
12974 cp_parser_commit_to_tentative_parse (parser);
12975 /* Issue the error about the overly-qualified name now. */
12976 if (qualified_p)
12977 cp_parser_error (parser,
12978 "global qualification of class name is invalid");
12979 else if (invalid_nested_name_p)
12980 cp_parser_error (parser,
12981 "qualified name does not name a class");
12982 else if (nested_name_specifier)
12984 tree scope;
12986 /* Reject typedef-names in class heads. */
12987 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12989 error ("invalid class name in declaration of %qD", type);
12990 type = NULL_TREE;
12991 goto done;
12994 /* Figure out in what scope the declaration is being placed. */
12995 scope = current_scope ();
12996 /* If that scope does not contain the scope in which the
12997 class was originally declared, the program is invalid. */
12998 if (scope && !is_ancestor (scope, nested_name_specifier))
13000 error ("declaration of %qD in %qD which does not enclose %qD",
13001 type, scope, nested_name_specifier);
13002 type = NULL_TREE;
13003 goto done;
13005 /* [dcl.meaning]
13007 A declarator-id shall not be qualified exception of the
13008 definition of a ... nested class outside of its class
13009 ... [or] a the definition or explicit instantiation of a
13010 class member of a namespace outside of its namespace. */
13011 if (scope == nested_name_specifier)
13013 pedwarn ("extra qualification ignored");
13014 nested_name_specifier = NULL_TREE;
13015 num_templates = 0;
13018 /* An explicit-specialization must be preceded by "template <>". If
13019 it is not, try to recover gracefully. */
13020 if (at_namespace_scope_p ()
13021 && parser->num_template_parameter_lists == 0
13022 && template_id_p)
13024 error ("an explicit specialization must be preceded by %<template <>%>");
13025 invalid_explicit_specialization_p = true;
13026 /* Take the same action that would have been taken by
13027 cp_parser_explicit_specialization. */
13028 ++parser->num_template_parameter_lists;
13029 begin_specialization ();
13031 /* There must be no "return" statements between this point and the
13032 end of this function; set "type "to the correct return value and
13033 use "goto done;" to return. */
13034 /* Make sure that the right number of template parameters were
13035 present. */
13036 if (!cp_parser_check_template_parameters (parser, num_templates))
13038 /* If something went wrong, there is no point in even trying to
13039 process the class-definition. */
13040 type = NULL_TREE;
13041 goto done;
13044 /* Look up the type. */
13045 if (template_id_p)
13047 type = TREE_TYPE (id);
13048 maybe_process_partial_specialization (type);
13049 if (nested_name_specifier)
13050 pushed_scope = push_scope (nested_name_specifier);
13052 else if (nested_name_specifier)
13054 tree class_type;
13056 /* Given:
13058 template <typename T> struct S { struct T };
13059 template <typename T> struct S<T>::T { };
13061 we will get a TYPENAME_TYPE when processing the definition of
13062 `S::T'. We need to resolve it to the actual type before we
13063 try to define it. */
13064 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13066 class_type = resolve_typename_type (TREE_TYPE (type),
13067 /*only_current_p=*/false);
13068 if (class_type != error_mark_node)
13069 type = TYPE_NAME (class_type);
13070 else
13072 cp_parser_error (parser, "could not resolve typename type");
13073 type = error_mark_node;
13077 maybe_process_partial_specialization (TREE_TYPE (type));
13078 class_type = current_class_type;
13079 /* Enter the scope indicated by the nested-name-specifier. */
13080 pushed_scope = push_scope (nested_name_specifier);
13081 /* Get the canonical version of this type. */
13082 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13083 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13084 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13086 type = push_template_decl (type);
13087 if (type == error_mark_node)
13089 type = NULL_TREE;
13090 goto done;
13094 type = TREE_TYPE (type);
13095 *nested_name_specifier_p = true;
13097 else /* The name is not a nested name. */
13099 /* If the class was unnamed, create a dummy name. */
13100 if (!id)
13101 id = make_anon_name ();
13102 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13103 parser->num_template_parameter_lists);
13106 /* Indicate whether this class was declared as a `class' or as a
13107 `struct'. */
13108 if (TREE_CODE (type) == RECORD_TYPE)
13109 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13110 cp_parser_check_class_key (class_key, type);
13112 /* If this type was already complete, and we see another definition,
13113 that's an error. */
13114 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13116 error ("redefinition of %q#T", type);
13117 error ("previous definition of %q+#T", type);
13118 type = NULL_TREE;
13119 goto done;
13122 /* We will have entered the scope containing the class; the names of
13123 base classes should be looked up in that context. For example:
13125 struct A { struct B {}; struct C; };
13126 struct A::C : B {};
13128 is valid. */
13129 bases = NULL_TREE;
13131 /* Get the list of base-classes, if there is one. */
13132 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13133 bases = cp_parser_base_clause (parser);
13135 /* Process the base classes. */
13136 xref_basetypes (type, bases);
13138 done:
13139 /* Leave the scope given by the nested-name-specifier. We will
13140 enter the class scope itself while processing the members. */
13141 if (pushed_scope)
13142 pop_scope (pushed_scope);
13144 if (invalid_explicit_specialization_p)
13146 end_specialization ();
13147 --parser->num_template_parameter_lists;
13149 *attributes_p = attributes;
13150 return type;
13153 /* Parse a class-key.
13155 class-key:
13156 class
13157 struct
13158 union
13160 Returns the kind of class-key specified, or none_type to indicate
13161 error. */
13163 static enum tag_types
13164 cp_parser_class_key (cp_parser* parser)
13166 cp_token *token;
13167 enum tag_types tag_type;
13169 /* Look for the class-key. */
13170 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13171 if (!token)
13172 return none_type;
13174 /* Check to see if the TOKEN is a class-key. */
13175 tag_type = cp_parser_token_is_class_key (token);
13176 if (!tag_type)
13177 cp_parser_error (parser, "expected class-key");
13178 return tag_type;
13181 /* Parse an (optional) member-specification.
13183 member-specification:
13184 member-declaration member-specification [opt]
13185 access-specifier : member-specification [opt] */
13187 static void
13188 cp_parser_member_specification_opt (cp_parser* parser)
13190 while (true)
13192 cp_token *token;
13193 enum rid keyword;
13195 /* Peek at the next token. */
13196 token = cp_lexer_peek_token (parser->lexer);
13197 /* If it's a `}', or EOF then we've seen all the members. */
13198 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13199 break;
13201 /* See if this token is a keyword. */
13202 keyword = token->keyword;
13203 switch (keyword)
13205 case RID_PUBLIC:
13206 case RID_PROTECTED:
13207 case RID_PRIVATE:
13208 /* Consume the access-specifier. */
13209 cp_lexer_consume_token (parser->lexer);
13210 /* Remember which access-specifier is active. */
13211 current_access_specifier = token->value;
13212 /* Look for the `:'. */
13213 cp_parser_require (parser, CPP_COLON, "`:'");
13214 break;
13216 default:
13217 /* Accept #pragmas at class scope. */
13218 if (token->type == CPP_PRAGMA)
13220 cp_lexer_handle_pragma (parser->lexer);
13221 break;
13224 /* Otherwise, the next construction must be a
13225 member-declaration. */
13226 cp_parser_member_declaration (parser);
13231 /* Parse a member-declaration.
13233 member-declaration:
13234 decl-specifier-seq [opt] member-declarator-list [opt] ;
13235 function-definition ; [opt]
13236 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13237 using-declaration
13238 template-declaration
13240 member-declarator-list:
13241 member-declarator
13242 member-declarator-list , member-declarator
13244 member-declarator:
13245 declarator pure-specifier [opt]
13246 declarator constant-initializer [opt]
13247 identifier [opt] : constant-expression
13249 GNU Extensions:
13251 member-declaration:
13252 __extension__ member-declaration
13254 member-declarator:
13255 declarator attributes [opt] pure-specifier [opt]
13256 declarator attributes [opt] constant-initializer [opt]
13257 identifier [opt] attributes [opt] : constant-expression */
13259 static void
13260 cp_parser_member_declaration (cp_parser* parser)
13262 cp_decl_specifier_seq decl_specifiers;
13263 tree prefix_attributes;
13264 tree decl;
13265 int declares_class_or_enum;
13266 bool friend_p;
13267 cp_token *token;
13268 int saved_pedantic;
13270 /* Check for the `__extension__' keyword. */
13271 if (cp_parser_extension_opt (parser, &saved_pedantic))
13273 /* Recurse. */
13274 cp_parser_member_declaration (parser);
13275 /* Restore the old value of the PEDANTIC flag. */
13276 pedantic = saved_pedantic;
13278 return;
13281 /* Check for a template-declaration. */
13282 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13284 /* An explicit specialization here is an error condition, and we
13285 expect the specialization handler to detect and report this. */
13286 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13287 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13288 cp_parser_explicit_specialization (parser);
13289 else
13290 cp_parser_template_declaration (parser, /*member_p=*/true);
13292 return;
13295 /* Check for a using-declaration. */
13296 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13298 /* Parse the using-declaration. */
13299 cp_parser_using_declaration (parser);
13301 return;
13304 /* Check for @defs. */
13305 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13307 tree ivar, member;
13308 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13309 ivar = ivar_chains;
13310 while (ivar)
13312 member = ivar;
13313 ivar = TREE_CHAIN (member);
13314 TREE_CHAIN (member) = NULL_TREE;
13315 finish_member_declaration (member);
13317 return;
13320 /* Parse the decl-specifier-seq. */
13321 cp_parser_decl_specifier_seq (parser,
13322 CP_PARSER_FLAGS_OPTIONAL,
13323 &decl_specifiers,
13324 &declares_class_or_enum);
13325 prefix_attributes = decl_specifiers.attributes;
13326 decl_specifiers.attributes = NULL_TREE;
13327 /* Check for an invalid type-name. */
13328 if (!decl_specifiers.type
13329 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13330 return;
13331 /* If there is no declarator, then the decl-specifier-seq should
13332 specify a type. */
13333 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13335 /* If there was no decl-specifier-seq, and the next token is a
13336 `;', then we have something like:
13338 struct S { ; };
13340 [class.mem]
13342 Each member-declaration shall declare at least one member
13343 name of the class. */
13344 if (!decl_specifiers.any_specifiers_p)
13346 cp_token *token = cp_lexer_peek_token (parser->lexer);
13347 if (pedantic && !token->in_system_header)
13348 pedwarn ("%Hextra %<;%>", &token->location);
13350 else
13352 tree type;
13354 /* See if this declaration is a friend. */
13355 friend_p = cp_parser_friend_p (&decl_specifiers);
13356 /* If there were decl-specifiers, check to see if there was
13357 a class-declaration. */
13358 type = check_tag_decl (&decl_specifiers);
13359 /* Nested classes have already been added to the class, but
13360 a `friend' needs to be explicitly registered. */
13361 if (friend_p)
13363 /* If the `friend' keyword was present, the friend must
13364 be introduced with a class-key. */
13365 if (!declares_class_or_enum)
13366 error ("a class-key must be used when declaring a friend");
13367 /* In this case:
13369 template <typename T> struct A {
13370 friend struct A<T>::B;
13373 A<T>::B will be represented by a TYPENAME_TYPE, and
13374 therefore not recognized by check_tag_decl. */
13375 if (!type
13376 && decl_specifiers.type
13377 && TYPE_P (decl_specifiers.type))
13378 type = decl_specifiers.type;
13379 if (!type || !TYPE_P (type))
13380 error ("friend declaration does not name a class or "
13381 "function");
13382 else
13383 make_friend_class (current_class_type, type,
13384 /*complain=*/true);
13386 /* If there is no TYPE, an error message will already have
13387 been issued. */
13388 else if (!type || type == error_mark_node)
13390 /* An anonymous aggregate has to be handled specially; such
13391 a declaration really declares a data member (with a
13392 particular type), as opposed to a nested class. */
13393 else if (ANON_AGGR_TYPE_P (type))
13395 /* Remove constructors and such from TYPE, now that we
13396 know it is an anonymous aggregate. */
13397 fixup_anonymous_aggr (type);
13398 /* And make the corresponding data member. */
13399 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13400 /* Add it to the class. */
13401 finish_member_declaration (decl);
13403 else
13404 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13407 else
13409 /* See if these declarations will be friends. */
13410 friend_p = cp_parser_friend_p (&decl_specifiers);
13412 /* Keep going until we hit the `;' at the end of the
13413 declaration. */
13414 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13416 tree attributes = NULL_TREE;
13417 tree first_attribute;
13419 /* Peek at the next token. */
13420 token = cp_lexer_peek_token (parser->lexer);
13422 /* Check for a bitfield declaration. */
13423 if (token->type == CPP_COLON
13424 || (token->type == CPP_NAME
13425 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13426 == CPP_COLON))
13428 tree identifier;
13429 tree width;
13431 /* Get the name of the bitfield. Note that we cannot just
13432 check TOKEN here because it may have been invalidated by
13433 the call to cp_lexer_peek_nth_token above. */
13434 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13435 identifier = cp_parser_identifier (parser);
13436 else
13437 identifier = NULL_TREE;
13439 /* Consume the `:' token. */
13440 cp_lexer_consume_token (parser->lexer);
13441 /* Get the width of the bitfield. */
13442 width
13443 = cp_parser_constant_expression (parser,
13444 /*allow_non_constant=*/false,
13445 NULL);
13447 /* Look for attributes that apply to the bitfield. */
13448 attributes = cp_parser_attributes_opt (parser);
13449 /* Remember which attributes are prefix attributes and
13450 which are not. */
13451 first_attribute = attributes;
13452 /* Combine the attributes. */
13453 attributes = chainon (prefix_attributes, attributes);
13455 /* Create the bitfield declaration. */
13456 decl = grokbitfield (identifier
13457 ? make_id_declarator (NULL_TREE,
13458 identifier)
13459 : NULL,
13460 &decl_specifiers,
13461 width);
13462 /* Apply the attributes. */
13463 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13465 else
13467 cp_declarator *declarator;
13468 tree initializer;
13469 tree asm_specification;
13470 int ctor_dtor_or_conv_p;
13472 /* Parse the declarator. */
13473 declarator
13474 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13475 &ctor_dtor_or_conv_p,
13476 /*parenthesized_p=*/NULL,
13477 /*member_p=*/true);
13479 /* If something went wrong parsing the declarator, make sure
13480 that we at least consume some tokens. */
13481 if (declarator == cp_error_declarator)
13483 /* Skip to the end of the statement. */
13484 cp_parser_skip_to_end_of_statement (parser);
13485 /* If the next token is not a semicolon, that is
13486 probably because we just skipped over the body of
13487 a function. So, we consume a semicolon if
13488 present, but do not issue an error message if it
13489 is not present. */
13490 if (cp_lexer_next_token_is (parser->lexer,
13491 CPP_SEMICOLON))
13492 cp_lexer_consume_token (parser->lexer);
13493 return;
13496 if (declares_class_or_enum & 2)
13497 cp_parser_check_for_definition_in_return_type
13498 (declarator, decl_specifiers.type);
13500 /* Look for an asm-specification. */
13501 asm_specification = cp_parser_asm_specification_opt (parser);
13502 /* Look for attributes that apply to the declaration. */
13503 attributes = cp_parser_attributes_opt (parser);
13504 /* Remember which attributes are prefix attributes and
13505 which are not. */
13506 first_attribute = attributes;
13507 /* Combine the attributes. */
13508 attributes = chainon (prefix_attributes, attributes);
13510 /* If it's an `=', then we have a constant-initializer or a
13511 pure-specifier. It is not correct to parse the
13512 initializer before registering the member declaration
13513 since the member declaration should be in scope while
13514 its initializer is processed. However, the rest of the
13515 front end does not yet provide an interface that allows
13516 us to handle this correctly. */
13517 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13519 /* In [class.mem]:
13521 A pure-specifier shall be used only in the declaration of
13522 a virtual function.
13524 A member-declarator can contain a constant-initializer
13525 only if it declares a static member of integral or
13526 enumeration type.
13528 Therefore, if the DECLARATOR is for a function, we look
13529 for a pure-specifier; otherwise, we look for a
13530 constant-initializer. When we call `grokfield', it will
13531 perform more stringent semantics checks. */
13532 if (declarator->kind == cdk_function)
13533 initializer = cp_parser_pure_specifier (parser);
13534 else
13535 /* Parse the initializer. */
13536 initializer = cp_parser_constant_initializer (parser);
13538 /* Otherwise, there is no initializer. */
13539 else
13540 initializer = NULL_TREE;
13542 /* See if we are probably looking at a function
13543 definition. We are certainly not looking at a
13544 member-declarator. Calling `grokfield' has
13545 side-effects, so we must not do it unless we are sure
13546 that we are looking at a member-declarator. */
13547 if (cp_parser_token_starts_function_definition_p
13548 (cp_lexer_peek_token (parser->lexer)))
13550 /* The grammar does not allow a pure-specifier to be
13551 used when a member function is defined. (It is
13552 possible that this fact is an oversight in the
13553 standard, since a pure function may be defined
13554 outside of the class-specifier. */
13555 if (initializer)
13556 error ("pure-specifier on function-definition");
13557 decl = cp_parser_save_member_function_body (parser,
13558 &decl_specifiers,
13559 declarator,
13560 attributes);
13561 /* If the member was not a friend, declare it here. */
13562 if (!friend_p)
13563 finish_member_declaration (decl);
13564 /* Peek at the next token. */
13565 token = cp_lexer_peek_token (parser->lexer);
13566 /* If the next token is a semicolon, consume it. */
13567 if (token->type == CPP_SEMICOLON)
13568 cp_lexer_consume_token (parser->lexer);
13569 return;
13571 else
13573 /* Create the declaration. */
13574 decl = grokfield (declarator, &decl_specifiers,
13575 initializer, asm_specification,
13576 attributes);
13577 /* Any initialization must have been from a
13578 constant-expression. */
13579 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13580 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13584 /* Reset PREFIX_ATTRIBUTES. */
13585 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13586 attributes = TREE_CHAIN (attributes);
13587 if (attributes)
13588 TREE_CHAIN (attributes) = NULL_TREE;
13590 /* If there is any qualification still in effect, clear it
13591 now; we will be starting fresh with the next declarator. */
13592 parser->scope = NULL_TREE;
13593 parser->qualifying_scope = NULL_TREE;
13594 parser->object_scope = NULL_TREE;
13595 /* If it's a `,', then there are more declarators. */
13596 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13597 cp_lexer_consume_token (parser->lexer);
13598 /* If the next token isn't a `;', then we have a parse error. */
13599 else if (cp_lexer_next_token_is_not (parser->lexer,
13600 CPP_SEMICOLON))
13602 cp_parser_error (parser, "expected %<;%>");
13603 /* Skip tokens until we find a `;'. */
13604 cp_parser_skip_to_end_of_statement (parser);
13606 break;
13609 if (decl)
13611 /* Add DECL to the list of members. */
13612 if (!friend_p)
13613 finish_member_declaration (decl);
13615 if (TREE_CODE (decl) == FUNCTION_DECL)
13616 cp_parser_save_default_args (parser, decl);
13621 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13624 /* Parse a pure-specifier.
13626 pure-specifier:
13629 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13630 Otherwise, ERROR_MARK_NODE is returned. */
13632 static tree
13633 cp_parser_pure_specifier (cp_parser* parser)
13635 cp_token *token;
13637 /* Look for the `=' token. */
13638 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13639 return error_mark_node;
13640 /* Look for the `0' token. */
13641 token = cp_lexer_consume_token (parser->lexer);
13642 if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13644 cp_parser_error (parser,
13645 "invalid pure specifier (only `= 0' is allowed)");
13646 cp_parser_skip_to_end_of_statement (parser);
13647 return error_mark_node;
13650 /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13651 We need to get information from the lexer about how the number
13652 was spelled in order to fix this problem. */
13653 return integer_zero_node;
13656 /* Parse a constant-initializer.
13658 constant-initializer:
13659 = constant-expression
13661 Returns a representation of the constant-expression. */
13663 static tree
13664 cp_parser_constant_initializer (cp_parser* parser)
13666 /* Look for the `=' token. */
13667 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13668 return error_mark_node;
13670 /* It is invalid to write:
13672 struct S { static const int i = { 7 }; };
13675 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13677 cp_parser_error (parser,
13678 "a brace-enclosed initializer is not allowed here");
13679 /* Consume the opening brace. */
13680 cp_lexer_consume_token (parser->lexer);
13681 /* Skip the initializer. */
13682 cp_parser_skip_to_closing_brace (parser);
13683 /* Look for the trailing `}'. */
13684 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13686 return error_mark_node;
13689 return cp_parser_constant_expression (parser,
13690 /*allow_non_constant=*/false,
13691 NULL);
13694 /* Derived classes [gram.class.derived] */
13696 /* Parse a base-clause.
13698 base-clause:
13699 : base-specifier-list
13701 base-specifier-list:
13702 base-specifier
13703 base-specifier-list , base-specifier
13705 Returns a TREE_LIST representing the base-classes, in the order in
13706 which they were declared. The representation of each node is as
13707 described by cp_parser_base_specifier.
13709 In the case that no bases are specified, this function will return
13710 NULL_TREE, not ERROR_MARK_NODE. */
13712 static tree
13713 cp_parser_base_clause (cp_parser* parser)
13715 tree bases = NULL_TREE;
13717 /* Look for the `:' that begins the list. */
13718 cp_parser_require (parser, CPP_COLON, "`:'");
13720 /* Scan the base-specifier-list. */
13721 while (true)
13723 cp_token *token;
13724 tree base;
13726 /* Look for the base-specifier. */
13727 base = cp_parser_base_specifier (parser);
13728 /* Add BASE to the front of the list. */
13729 if (base != error_mark_node)
13731 TREE_CHAIN (base) = bases;
13732 bases = base;
13734 /* Peek at the next token. */
13735 token = cp_lexer_peek_token (parser->lexer);
13736 /* If it's not a comma, then the list is complete. */
13737 if (token->type != CPP_COMMA)
13738 break;
13739 /* Consume the `,'. */
13740 cp_lexer_consume_token (parser->lexer);
13743 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13744 base class had a qualified name. However, the next name that
13745 appears is certainly not qualified. */
13746 parser->scope = NULL_TREE;
13747 parser->qualifying_scope = NULL_TREE;
13748 parser->object_scope = NULL_TREE;
13750 return nreverse (bases);
13753 /* Parse a base-specifier.
13755 base-specifier:
13756 :: [opt] nested-name-specifier [opt] class-name
13757 virtual access-specifier [opt] :: [opt] nested-name-specifier
13758 [opt] class-name
13759 access-specifier virtual [opt] :: [opt] nested-name-specifier
13760 [opt] class-name
13762 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13763 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13764 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13765 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13767 static tree
13768 cp_parser_base_specifier (cp_parser* parser)
13770 cp_token *token;
13771 bool done = false;
13772 bool virtual_p = false;
13773 bool duplicate_virtual_error_issued_p = false;
13774 bool duplicate_access_error_issued_p = false;
13775 bool class_scope_p, template_p;
13776 tree access = access_default_node;
13777 tree type;
13779 /* Process the optional `virtual' and `access-specifier'. */
13780 while (!done)
13782 /* Peek at the next token. */
13783 token = cp_lexer_peek_token (parser->lexer);
13784 /* Process `virtual'. */
13785 switch (token->keyword)
13787 case RID_VIRTUAL:
13788 /* If `virtual' appears more than once, issue an error. */
13789 if (virtual_p && !duplicate_virtual_error_issued_p)
13791 cp_parser_error (parser,
13792 "%<virtual%> specified more than once in base-specified");
13793 duplicate_virtual_error_issued_p = true;
13796 virtual_p = true;
13798 /* Consume the `virtual' token. */
13799 cp_lexer_consume_token (parser->lexer);
13801 break;
13803 case RID_PUBLIC:
13804 case RID_PROTECTED:
13805 case RID_PRIVATE:
13806 /* If more than one access specifier appears, issue an
13807 error. */
13808 if (access != access_default_node
13809 && !duplicate_access_error_issued_p)
13811 cp_parser_error (parser,
13812 "more than one access specifier in base-specified");
13813 duplicate_access_error_issued_p = true;
13816 access = ridpointers[(int) token->keyword];
13818 /* Consume the access-specifier. */
13819 cp_lexer_consume_token (parser->lexer);
13821 break;
13823 default:
13824 done = true;
13825 break;
13828 /* It is not uncommon to see programs mechanically, erroneously, use
13829 the 'typename' keyword to denote (dependent) qualified types
13830 as base classes. */
13831 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13833 if (!processing_template_decl)
13834 error ("keyword %<typename%> not allowed outside of templates");
13835 else
13836 error ("keyword %<typename%> not allowed in this context "
13837 "(the base class is implicitly a type)");
13838 cp_lexer_consume_token (parser->lexer);
13841 /* Look for the optional `::' operator. */
13842 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13843 /* Look for the nested-name-specifier. The simplest way to
13844 implement:
13846 [temp.res]
13848 The keyword `typename' is not permitted in a base-specifier or
13849 mem-initializer; in these contexts a qualified name that
13850 depends on a template-parameter is implicitly assumed to be a
13851 type name.
13853 is to pretend that we have seen the `typename' keyword at this
13854 point. */
13855 cp_parser_nested_name_specifier_opt (parser,
13856 /*typename_keyword_p=*/true,
13857 /*check_dependency_p=*/true,
13858 typename_type,
13859 /*is_declaration=*/true);
13860 /* If the base class is given by a qualified name, assume that names
13861 we see are type names or templates, as appropriate. */
13862 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13863 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13865 /* Finally, look for the class-name. */
13866 type = cp_parser_class_name (parser,
13867 class_scope_p,
13868 template_p,
13869 typename_type,
13870 /*check_dependency_p=*/true,
13871 /*class_head_p=*/false,
13872 /*is_declaration=*/true);
13874 if (type == error_mark_node)
13875 return error_mark_node;
13877 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13880 /* Exception handling [gram.exception] */
13882 /* Parse an (optional) exception-specification.
13884 exception-specification:
13885 throw ( type-id-list [opt] )
13887 Returns a TREE_LIST representing the exception-specification. The
13888 TREE_VALUE of each node is a type. */
13890 static tree
13891 cp_parser_exception_specification_opt (cp_parser* parser)
13893 cp_token *token;
13894 tree type_id_list;
13896 /* Peek at the next token. */
13897 token = cp_lexer_peek_token (parser->lexer);
13898 /* If it's not `throw', then there's no exception-specification. */
13899 if (!cp_parser_is_keyword (token, RID_THROW))
13900 return NULL_TREE;
13902 /* Consume the `throw'. */
13903 cp_lexer_consume_token (parser->lexer);
13905 /* Look for the `('. */
13906 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13908 /* Peek at the next token. */
13909 token = cp_lexer_peek_token (parser->lexer);
13910 /* If it's not a `)', then there is a type-id-list. */
13911 if (token->type != CPP_CLOSE_PAREN)
13913 const char *saved_message;
13915 /* Types may not be defined in an exception-specification. */
13916 saved_message = parser->type_definition_forbidden_message;
13917 parser->type_definition_forbidden_message
13918 = "types may not be defined in an exception-specification";
13919 /* Parse the type-id-list. */
13920 type_id_list = cp_parser_type_id_list (parser);
13921 /* Restore the saved message. */
13922 parser->type_definition_forbidden_message = saved_message;
13924 else
13925 type_id_list = empty_except_spec;
13927 /* Look for the `)'. */
13928 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13930 return type_id_list;
13933 /* Parse an (optional) type-id-list.
13935 type-id-list:
13936 type-id
13937 type-id-list , type-id
13939 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13940 in the order that the types were presented. */
13942 static tree
13943 cp_parser_type_id_list (cp_parser* parser)
13945 tree types = NULL_TREE;
13947 while (true)
13949 cp_token *token;
13950 tree type;
13952 /* Get the next type-id. */
13953 type = cp_parser_type_id (parser);
13954 /* Add it to the list. */
13955 types = add_exception_specifier (types, type, /*complain=*/1);
13956 /* Peek at the next token. */
13957 token = cp_lexer_peek_token (parser->lexer);
13958 /* If it is not a `,', we are done. */
13959 if (token->type != CPP_COMMA)
13960 break;
13961 /* Consume the `,'. */
13962 cp_lexer_consume_token (parser->lexer);
13965 return nreverse (types);
13968 /* Parse a try-block.
13970 try-block:
13971 try compound-statement handler-seq */
13973 static tree
13974 cp_parser_try_block (cp_parser* parser)
13976 tree try_block;
13978 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13979 try_block = begin_try_block ();
13980 cp_parser_compound_statement (parser, NULL, true);
13981 finish_try_block (try_block);
13982 cp_parser_handler_seq (parser);
13983 finish_handler_sequence (try_block);
13985 return try_block;
13988 /* Parse a function-try-block.
13990 function-try-block:
13991 try ctor-initializer [opt] function-body handler-seq */
13993 static bool
13994 cp_parser_function_try_block (cp_parser* parser)
13996 tree try_block;
13997 bool ctor_initializer_p;
13999 /* Look for the `try' keyword. */
14000 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14001 return false;
14002 /* Let the rest of the front-end know where we are. */
14003 try_block = begin_function_try_block ();
14004 /* Parse the function-body. */
14005 ctor_initializer_p
14006 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14007 /* We're done with the `try' part. */
14008 finish_function_try_block (try_block);
14009 /* Parse the handlers. */
14010 cp_parser_handler_seq (parser);
14011 /* We're done with the handlers. */
14012 finish_function_handler_sequence (try_block);
14014 return ctor_initializer_p;
14017 /* Parse a handler-seq.
14019 handler-seq:
14020 handler handler-seq [opt] */
14022 static void
14023 cp_parser_handler_seq (cp_parser* parser)
14025 while (true)
14027 cp_token *token;
14029 /* Parse the handler. */
14030 cp_parser_handler (parser);
14031 /* Peek at the next token. */
14032 token = cp_lexer_peek_token (parser->lexer);
14033 /* If it's not `catch' then there are no more handlers. */
14034 if (!cp_parser_is_keyword (token, RID_CATCH))
14035 break;
14039 /* Parse a handler.
14041 handler:
14042 catch ( exception-declaration ) compound-statement */
14044 static void
14045 cp_parser_handler (cp_parser* parser)
14047 tree handler;
14048 tree declaration;
14050 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14051 handler = begin_handler ();
14052 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14053 declaration = cp_parser_exception_declaration (parser);
14054 finish_handler_parms (declaration, handler);
14055 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14056 cp_parser_compound_statement (parser, NULL, false);
14057 finish_handler (handler);
14060 /* Parse an exception-declaration.
14062 exception-declaration:
14063 type-specifier-seq declarator
14064 type-specifier-seq abstract-declarator
14065 type-specifier-seq
14068 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14069 ellipsis variant is used. */
14071 static tree
14072 cp_parser_exception_declaration (cp_parser* parser)
14074 tree decl;
14075 cp_decl_specifier_seq type_specifiers;
14076 cp_declarator *declarator;
14077 const char *saved_message;
14079 /* If it's an ellipsis, it's easy to handle. */
14080 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14082 /* Consume the `...' token. */
14083 cp_lexer_consume_token (parser->lexer);
14084 return NULL_TREE;
14087 /* Types may not be defined in exception-declarations. */
14088 saved_message = parser->type_definition_forbidden_message;
14089 parser->type_definition_forbidden_message
14090 = "types may not be defined in exception-declarations";
14092 /* Parse the type-specifier-seq. */
14093 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14094 &type_specifiers);
14095 /* If it's a `)', then there is no declarator. */
14096 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14097 declarator = NULL;
14098 else
14099 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14100 /*ctor_dtor_or_conv_p=*/NULL,
14101 /*parenthesized_p=*/NULL,
14102 /*member_p=*/false);
14104 /* Restore the saved message. */
14105 parser->type_definition_forbidden_message = saved_message;
14107 if (type_specifiers.any_specifiers_p)
14109 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14110 if (decl == NULL_TREE)
14111 error ("invalid catch parameter");
14113 else
14114 decl = NULL_TREE;
14116 return decl;
14119 /* Parse a throw-expression.
14121 throw-expression:
14122 throw assignment-expression [opt]
14124 Returns a THROW_EXPR representing the throw-expression. */
14126 static tree
14127 cp_parser_throw_expression (cp_parser* parser)
14129 tree expression;
14130 cp_token* token;
14132 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14133 token = cp_lexer_peek_token (parser->lexer);
14134 /* Figure out whether or not there is an assignment-expression
14135 following the "throw" keyword. */
14136 if (token->type == CPP_COMMA
14137 || token->type == CPP_SEMICOLON
14138 || token->type == CPP_CLOSE_PAREN
14139 || token->type == CPP_CLOSE_SQUARE
14140 || token->type == CPP_CLOSE_BRACE
14141 || token->type == CPP_COLON)
14142 expression = NULL_TREE;
14143 else
14144 expression = cp_parser_assignment_expression (parser,
14145 /*cast_p=*/false);
14147 return build_throw (expression);
14150 /* GNU Extensions */
14152 /* Parse an (optional) asm-specification.
14154 asm-specification:
14155 asm ( string-literal )
14157 If the asm-specification is present, returns a STRING_CST
14158 corresponding to the string-literal. Otherwise, returns
14159 NULL_TREE. */
14161 static tree
14162 cp_parser_asm_specification_opt (cp_parser* parser)
14164 cp_token *token;
14165 tree asm_specification;
14167 /* Peek at the next token. */
14168 token = cp_lexer_peek_token (parser->lexer);
14169 /* If the next token isn't the `asm' keyword, then there's no
14170 asm-specification. */
14171 if (!cp_parser_is_keyword (token, RID_ASM))
14172 return NULL_TREE;
14174 /* Consume the `asm' token. */
14175 cp_lexer_consume_token (parser->lexer);
14176 /* Look for the `('. */
14177 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14179 /* Look for the string-literal. */
14180 asm_specification = cp_parser_string_literal (parser, false, false);
14182 /* Look for the `)'. */
14183 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14185 return asm_specification;
14188 /* Parse an asm-operand-list.
14190 asm-operand-list:
14191 asm-operand
14192 asm-operand-list , asm-operand
14194 asm-operand:
14195 string-literal ( expression )
14196 [ string-literal ] string-literal ( expression )
14198 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14199 each node is the expression. The TREE_PURPOSE is itself a
14200 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14201 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14202 is a STRING_CST for the string literal before the parenthesis. */
14204 static tree
14205 cp_parser_asm_operand_list (cp_parser* parser)
14207 tree asm_operands = NULL_TREE;
14209 while (true)
14211 tree string_literal;
14212 tree expression;
14213 tree name;
14215 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14217 /* Consume the `[' token. */
14218 cp_lexer_consume_token (parser->lexer);
14219 /* Read the operand name. */
14220 name = cp_parser_identifier (parser);
14221 if (name != error_mark_node)
14222 name = build_string (IDENTIFIER_LENGTH (name),
14223 IDENTIFIER_POINTER (name));
14224 /* Look for the closing `]'. */
14225 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14227 else
14228 name = NULL_TREE;
14229 /* Look for the string-literal. */
14230 string_literal = cp_parser_string_literal (parser, false, false);
14232 /* Look for the `('. */
14233 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14234 /* Parse the expression. */
14235 expression = cp_parser_expression (parser, /*cast_p=*/false);
14236 /* Look for the `)'. */
14237 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14239 /* Add this operand to the list. */
14240 asm_operands = tree_cons (build_tree_list (name, string_literal),
14241 expression,
14242 asm_operands);
14243 /* If the next token is not a `,', there are no more
14244 operands. */
14245 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14246 break;
14247 /* Consume the `,'. */
14248 cp_lexer_consume_token (parser->lexer);
14251 return nreverse (asm_operands);
14254 /* Parse an asm-clobber-list.
14256 asm-clobber-list:
14257 string-literal
14258 asm-clobber-list , string-literal
14260 Returns a TREE_LIST, indicating the clobbers in the order that they
14261 appeared. The TREE_VALUE of each node is a STRING_CST. */
14263 static tree
14264 cp_parser_asm_clobber_list (cp_parser* parser)
14266 tree clobbers = NULL_TREE;
14268 while (true)
14270 tree string_literal;
14272 /* Look for the string literal. */
14273 string_literal = cp_parser_string_literal (parser, false, false);
14274 /* Add it to the list. */
14275 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14276 /* If the next token is not a `,', then the list is
14277 complete. */
14278 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14279 break;
14280 /* Consume the `,' token. */
14281 cp_lexer_consume_token (parser->lexer);
14284 return clobbers;
14287 /* Parse an (optional) series of attributes.
14289 attributes:
14290 attributes attribute
14292 attribute:
14293 __attribute__ (( attribute-list [opt] ))
14295 The return value is as for cp_parser_attribute_list. */
14297 static tree
14298 cp_parser_attributes_opt (cp_parser* parser)
14300 tree attributes = NULL_TREE;
14302 while (true)
14304 cp_token *token;
14305 tree attribute_list;
14307 /* Peek at the next token. */
14308 token = cp_lexer_peek_token (parser->lexer);
14309 /* If it's not `__attribute__', then we're done. */
14310 if (token->keyword != RID_ATTRIBUTE)
14311 break;
14313 /* Consume the `__attribute__' keyword. */
14314 cp_lexer_consume_token (parser->lexer);
14315 /* Look for the two `(' tokens. */
14316 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14317 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14319 /* Peek at the next token. */
14320 token = cp_lexer_peek_token (parser->lexer);
14321 if (token->type != CPP_CLOSE_PAREN)
14322 /* Parse the attribute-list. */
14323 attribute_list = cp_parser_attribute_list (parser);
14324 else
14325 /* If the next token is a `)', then there is no attribute
14326 list. */
14327 attribute_list = NULL;
14329 /* Look for the two `)' tokens. */
14330 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14331 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14333 /* Add these new attributes to the list. */
14334 attributes = chainon (attributes, attribute_list);
14337 return attributes;
14340 /* Parse an attribute-list.
14342 attribute-list:
14343 attribute
14344 attribute-list , attribute
14346 attribute:
14347 identifier
14348 identifier ( identifier )
14349 identifier ( identifier , expression-list )
14350 identifier ( expression-list )
14352 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14353 to an attribute. The TREE_PURPOSE of each node is the identifier
14354 indicating which attribute is in use. The TREE_VALUE represents
14355 the arguments, if any. */
14357 static tree
14358 cp_parser_attribute_list (cp_parser* parser)
14360 tree attribute_list = NULL_TREE;
14361 bool save_translate_strings_p = parser->translate_strings_p;
14363 parser->translate_strings_p = false;
14364 while (true)
14366 cp_token *token;
14367 tree identifier;
14368 tree attribute;
14370 /* Look for the identifier. We also allow keywords here; for
14371 example `__attribute__ ((const))' is legal. */
14372 token = cp_lexer_peek_token (parser->lexer);
14373 if (token->type == CPP_NAME
14374 || token->type == CPP_KEYWORD)
14376 /* Consume the token. */
14377 token = cp_lexer_consume_token (parser->lexer);
14379 /* Save away the identifier that indicates which attribute
14380 this is. */
14381 identifier = token->value;
14382 attribute = build_tree_list (identifier, NULL_TREE);
14384 /* Peek at the next token. */
14385 token = cp_lexer_peek_token (parser->lexer);
14386 /* If it's an `(', then parse the attribute arguments. */
14387 if (token->type == CPP_OPEN_PAREN)
14389 tree arguments;
14391 arguments = (cp_parser_parenthesized_expression_list
14392 (parser, true, /*cast_p=*/false,
14393 /*non_constant_p=*/NULL));
14394 /* Save the identifier and arguments away. */
14395 TREE_VALUE (attribute) = arguments;
14398 /* Add this attribute to the list. */
14399 TREE_CHAIN (attribute) = attribute_list;
14400 attribute_list = attribute;
14402 token = cp_lexer_peek_token (parser->lexer);
14404 /* Now, look for more attributes. If the next token isn't a
14405 `,', we're done. */
14406 if (token->type != CPP_COMMA)
14407 break;
14409 /* Consume the comma and keep going. */
14410 cp_lexer_consume_token (parser->lexer);
14412 parser->translate_strings_p = save_translate_strings_p;
14414 /* We built up the list in reverse order. */
14415 return nreverse (attribute_list);
14418 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14419 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14420 current value of the PEDANTIC flag, regardless of whether or not
14421 the `__extension__' keyword is present. The caller is responsible
14422 for restoring the value of the PEDANTIC flag. */
14424 static bool
14425 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14427 /* Save the old value of the PEDANTIC flag. */
14428 *saved_pedantic = pedantic;
14430 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14432 /* Consume the `__extension__' token. */
14433 cp_lexer_consume_token (parser->lexer);
14434 /* We're not being pedantic while the `__extension__' keyword is
14435 in effect. */
14436 pedantic = 0;
14438 return true;
14441 return false;
14444 /* Parse a label declaration.
14446 label-declaration:
14447 __label__ label-declarator-seq ;
14449 label-declarator-seq:
14450 identifier , label-declarator-seq
14451 identifier */
14453 static void
14454 cp_parser_label_declaration (cp_parser* parser)
14456 /* Look for the `__label__' keyword. */
14457 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14459 while (true)
14461 tree identifier;
14463 /* Look for an identifier. */
14464 identifier = cp_parser_identifier (parser);
14465 /* If we failed, stop. */
14466 if (identifier == error_mark_node)
14467 break;
14468 /* Declare it as a label. */
14469 finish_label_decl (identifier);
14470 /* If the next token is a `;', stop. */
14471 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14472 break;
14473 /* Look for the `,' separating the label declarations. */
14474 cp_parser_require (parser, CPP_COMMA, "`,'");
14477 /* Look for the final `;'. */
14478 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14481 /* Support Functions */
14483 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14484 NAME should have one of the representations used for an
14485 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14486 is returned. If PARSER->SCOPE is a dependent type, then a
14487 SCOPE_REF is returned.
14489 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14490 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14491 was formed. Abstractly, such entities should not be passed to this
14492 function, because they do not need to be looked up, but it is
14493 simpler to check for this special case here, rather than at the
14494 call-sites.
14496 In cases not explicitly covered above, this function returns a
14497 DECL, OVERLOAD, or baselink representing the result of the lookup.
14498 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14499 is returned.
14501 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14502 (e.g., "struct") that was used. In that case bindings that do not
14503 refer to types are ignored.
14505 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14506 ignored.
14508 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14509 are ignored.
14511 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14512 types.
14514 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14515 TREE_LIST of candiates if name-lookup results in an ambiguity, and
14516 NULL_TREE otherwise. */
14518 static tree
14519 cp_parser_lookup_name (cp_parser *parser, tree name,
14520 enum tag_types tag_type,
14521 bool is_template,
14522 bool is_namespace,
14523 bool check_dependency,
14524 tree *ambiguous_decls)
14526 int flags = 0;
14527 tree decl;
14528 tree object_type = parser->context->object_type;
14530 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14531 flags |= LOOKUP_COMPLAIN;
14533 /* Assume that the lookup will be unambiguous. */
14534 if (ambiguous_decls)
14535 *ambiguous_decls = NULL_TREE;
14537 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14538 no longer valid. Note that if we are parsing tentatively, and
14539 the parse fails, OBJECT_TYPE will be automatically restored. */
14540 parser->context->object_type = NULL_TREE;
14542 if (name == error_mark_node)
14543 return error_mark_node;
14545 /* A template-id has already been resolved; there is no lookup to
14546 do. */
14547 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14548 return name;
14549 if (BASELINK_P (name))
14551 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14552 == TEMPLATE_ID_EXPR);
14553 return name;
14556 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14557 it should already have been checked to make sure that the name
14558 used matches the type being destroyed. */
14559 if (TREE_CODE (name) == BIT_NOT_EXPR)
14561 tree type;
14563 /* Figure out to which type this destructor applies. */
14564 if (parser->scope)
14565 type = parser->scope;
14566 else if (object_type)
14567 type = object_type;
14568 else
14569 type = current_class_type;
14570 /* If that's not a class type, there is no destructor. */
14571 if (!type || !CLASS_TYPE_P (type))
14572 return error_mark_node;
14573 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14574 lazily_declare_fn (sfk_destructor, type);
14575 if (!CLASSTYPE_DESTRUCTORS (type))
14576 return error_mark_node;
14577 /* If it was a class type, return the destructor. */
14578 return CLASSTYPE_DESTRUCTORS (type);
14581 /* By this point, the NAME should be an ordinary identifier. If
14582 the id-expression was a qualified name, the qualifying scope is
14583 stored in PARSER->SCOPE at this point. */
14584 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14586 /* Perform the lookup. */
14587 if (parser->scope)
14589 bool dependent_p;
14591 if (parser->scope == error_mark_node)
14592 return error_mark_node;
14594 /* If the SCOPE is dependent, the lookup must be deferred until
14595 the template is instantiated -- unless we are explicitly
14596 looking up names in uninstantiated templates. Even then, we
14597 cannot look up the name if the scope is not a class type; it
14598 might, for example, be a template type parameter. */
14599 dependent_p = (TYPE_P (parser->scope)
14600 && !(parser->in_declarator_p
14601 && currently_open_class (parser->scope))
14602 && dependent_type_p (parser->scope));
14603 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14604 && dependent_p)
14606 if (tag_type)
14608 tree type;
14610 /* The resolution to Core Issue 180 says that `struct
14611 A::B' should be considered a type-name, even if `A'
14612 is dependent. */
14613 type = make_typename_type (parser->scope, name, tag_type,
14614 /*complain=*/tf_error);
14615 decl = TYPE_NAME (type);
14617 else if (is_template
14618 && (cp_parser_next_token_ends_template_argument_p (parser)
14619 || cp_lexer_next_token_is (parser->lexer,
14620 CPP_CLOSE_PAREN)))
14621 decl = make_unbound_class_template (parser->scope,
14622 name, NULL_TREE,
14623 /*complain=*/tf_error);
14624 else
14625 decl = build_qualified_name (/*type=*/NULL_TREE,
14626 parser->scope, name,
14627 is_template);
14629 else
14631 tree pushed_scope = NULL_TREE;
14633 /* If PARSER->SCOPE is a dependent type, then it must be a
14634 class type, and we must not be checking dependencies;
14635 otherwise, we would have processed this lookup above. So
14636 that PARSER->SCOPE is not considered a dependent base by
14637 lookup_member, we must enter the scope here. */
14638 if (dependent_p)
14639 pushed_scope = push_scope (parser->scope);
14640 /* If the PARSER->SCOPE is a template specialization, it
14641 may be instantiated during name lookup. In that case,
14642 errors may be issued. Even if we rollback the current
14643 tentative parse, those errors are valid. */
14644 decl = lookup_qualified_name (parser->scope, name,
14645 tag_type != none_type,
14646 /*complain=*/true);
14647 if (pushed_scope)
14648 pop_scope (pushed_scope);
14650 parser->qualifying_scope = parser->scope;
14651 parser->object_scope = NULL_TREE;
14653 else if (object_type)
14655 tree object_decl = NULL_TREE;
14656 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14657 OBJECT_TYPE is not a class. */
14658 if (CLASS_TYPE_P (object_type))
14659 /* If the OBJECT_TYPE is a template specialization, it may
14660 be instantiated during name lookup. In that case, errors
14661 may be issued. Even if we rollback the current tentative
14662 parse, those errors are valid. */
14663 object_decl = lookup_member (object_type,
14664 name,
14665 /*protect=*/0,
14666 tag_type != none_type);
14667 /* Look it up in the enclosing context, too. */
14668 decl = lookup_name_real (name, tag_type != none_type,
14669 /*nonclass=*/0,
14670 /*block_p=*/true, is_namespace, flags);
14671 parser->object_scope = object_type;
14672 parser->qualifying_scope = NULL_TREE;
14673 if (object_decl)
14674 decl = object_decl;
14676 else
14678 decl = lookup_name_real (name, tag_type != none_type,
14679 /*nonclass=*/0,
14680 /*block_p=*/true, is_namespace, flags);
14681 parser->qualifying_scope = NULL_TREE;
14682 parser->object_scope = NULL_TREE;
14685 /* If the lookup failed, let our caller know. */
14686 if (!decl || decl == error_mark_node)
14687 return error_mark_node;
14689 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14690 if (TREE_CODE (decl) == TREE_LIST)
14692 if (ambiguous_decls)
14693 *ambiguous_decls = decl;
14694 /* The error message we have to print is too complicated for
14695 cp_parser_error, so we incorporate its actions directly. */
14696 if (!cp_parser_simulate_error (parser))
14698 error ("reference to %qD is ambiguous", name);
14699 print_candidates (decl);
14701 return error_mark_node;
14704 gcc_assert (DECL_P (decl)
14705 || TREE_CODE (decl) == OVERLOAD
14706 || TREE_CODE (decl) == SCOPE_REF
14707 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14708 || BASELINK_P (decl));
14710 /* If we have resolved the name of a member declaration, check to
14711 see if the declaration is accessible. When the name resolves to
14712 set of overloaded functions, accessibility is checked when
14713 overload resolution is done.
14715 During an explicit instantiation, access is not checked at all,
14716 as per [temp.explicit]. */
14717 if (DECL_P (decl))
14718 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14720 return decl;
14723 /* Like cp_parser_lookup_name, but for use in the typical case where
14724 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14725 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14727 static tree
14728 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14730 return cp_parser_lookup_name (parser, name,
14731 none_type,
14732 /*is_template=*/false,
14733 /*is_namespace=*/false,
14734 /*check_dependency=*/true,
14735 /*ambiguous_decls=*/NULL);
14738 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14739 the current context, return the TYPE_DECL. If TAG_NAME_P is
14740 true, the DECL indicates the class being defined in a class-head,
14741 or declared in an elaborated-type-specifier.
14743 Otherwise, return DECL. */
14745 static tree
14746 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14748 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14749 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14751 struct A {
14752 template <typename T> struct B;
14755 template <typename T> struct A::B {};
14757 Similarly, in an elaborated-type-specifier:
14759 namespace N { struct X{}; }
14761 struct A {
14762 template <typename T> friend struct N::X;
14765 However, if the DECL refers to a class type, and we are in
14766 the scope of the class, then the name lookup automatically
14767 finds the TYPE_DECL created by build_self_reference rather
14768 than a TEMPLATE_DECL. For example, in:
14770 template <class T> struct S {
14771 S s;
14774 there is no need to handle such case. */
14776 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14777 return DECL_TEMPLATE_RESULT (decl);
14779 return decl;
14782 /* If too many, or too few, template-parameter lists apply to the
14783 declarator, issue an error message. Returns TRUE if all went well,
14784 and FALSE otherwise. */
14786 static bool
14787 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14788 cp_declarator *declarator)
14790 unsigned num_templates;
14792 /* We haven't seen any classes that involve template parameters yet. */
14793 num_templates = 0;
14795 switch (declarator->kind)
14797 case cdk_id:
14798 if (declarator->u.id.qualifying_scope)
14800 tree scope;
14801 tree member;
14803 scope = declarator->u.id.qualifying_scope;
14804 member = declarator->u.id.unqualified_name;
14806 while (scope && CLASS_TYPE_P (scope))
14808 /* You're supposed to have one `template <...>'
14809 for every template class, but you don't need one
14810 for a full specialization. For example:
14812 template <class T> struct S{};
14813 template <> struct S<int> { void f(); };
14814 void S<int>::f () {}
14816 is correct; there shouldn't be a `template <>' for
14817 the definition of `S<int>::f'. */
14818 if (CLASSTYPE_TEMPLATE_INFO (scope)
14819 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14820 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14821 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14822 ++num_templates;
14824 scope = TYPE_CONTEXT (scope);
14827 else if (TREE_CODE (declarator->u.id.unqualified_name)
14828 == TEMPLATE_ID_EXPR)
14829 /* If the DECLARATOR has the form `X<y>' then it uses one
14830 additional level of template parameters. */
14831 ++num_templates;
14833 return cp_parser_check_template_parameters (parser,
14834 num_templates);
14836 case cdk_function:
14837 case cdk_array:
14838 case cdk_pointer:
14839 case cdk_reference:
14840 case cdk_ptrmem:
14841 return (cp_parser_check_declarator_template_parameters
14842 (parser, declarator->declarator));
14844 case cdk_error:
14845 return true;
14847 default:
14848 gcc_unreachable ();
14850 return false;
14853 /* NUM_TEMPLATES were used in the current declaration. If that is
14854 invalid, return FALSE and issue an error messages. Otherwise,
14855 return TRUE. */
14857 static bool
14858 cp_parser_check_template_parameters (cp_parser* parser,
14859 unsigned num_templates)
14861 /* If there are more template classes than parameter lists, we have
14862 something like:
14864 template <class T> void S<T>::R<T>::f (); */
14865 if (parser->num_template_parameter_lists < num_templates)
14867 error ("too few template-parameter-lists");
14868 return false;
14870 /* If there are the same number of template classes and parameter
14871 lists, that's OK. */
14872 if (parser->num_template_parameter_lists == num_templates)
14873 return true;
14874 /* If there are more, but only one more, then we are referring to a
14875 member template. That's OK too. */
14876 if (parser->num_template_parameter_lists == num_templates + 1)
14877 return true;
14878 /* Otherwise, there are too many template parameter lists. We have
14879 something like:
14881 template <class T> template <class U> void S::f(); */
14882 error ("too many template-parameter-lists");
14883 return false;
14886 /* Parse an optional `::' token indicating that the following name is
14887 from the global namespace. If so, PARSER->SCOPE is set to the
14888 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14889 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14890 Returns the new value of PARSER->SCOPE, if the `::' token is
14891 present, and NULL_TREE otherwise. */
14893 static tree
14894 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14896 cp_token *token;
14898 /* Peek at the next token. */
14899 token = cp_lexer_peek_token (parser->lexer);
14900 /* If we're looking at a `::' token then we're starting from the
14901 global namespace, not our current location. */
14902 if (token->type == CPP_SCOPE)
14904 /* Consume the `::' token. */
14905 cp_lexer_consume_token (parser->lexer);
14906 /* Set the SCOPE so that we know where to start the lookup. */
14907 parser->scope = global_namespace;
14908 parser->qualifying_scope = global_namespace;
14909 parser->object_scope = NULL_TREE;
14911 return parser->scope;
14913 else if (!current_scope_valid_p)
14915 parser->scope = NULL_TREE;
14916 parser->qualifying_scope = NULL_TREE;
14917 parser->object_scope = NULL_TREE;
14920 return NULL_TREE;
14923 /* Returns TRUE if the upcoming token sequence is the start of a
14924 constructor declarator. If FRIEND_P is true, the declarator is
14925 preceded by the `friend' specifier. */
14927 static bool
14928 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14930 bool constructor_p;
14931 tree type_decl = NULL_TREE;
14932 bool nested_name_p;
14933 cp_token *next_token;
14935 /* The common case is that this is not a constructor declarator, so
14936 try to avoid doing lots of work if at all possible. It's not
14937 valid declare a constructor at function scope. */
14938 if (at_function_scope_p ())
14939 return false;
14940 /* And only certain tokens can begin a constructor declarator. */
14941 next_token = cp_lexer_peek_token (parser->lexer);
14942 if (next_token->type != CPP_NAME
14943 && next_token->type != CPP_SCOPE
14944 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14945 && next_token->type != CPP_TEMPLATE_ID)
14946 return false;
14948 /* Parse tentatively; we are going to roll back all of the tokens
14949 consumed here. */
14950 cp_parser_parse_tentatively (parser);
14951 /* Assume that we are looking at a constructor declarator. */
14952 constructor_p = true;
14954 /* Look for the optional `::' operator. */
14955 cp_parser_global_scope_opt (parser,
14956 /*current_scope_valid_p=*/false);
14957 /* Look for the nested-name-specifier. */
14958 nested_name_p
14959 = (cp_parser_nested_name_specifier_opt (parser,
14960 /*typename_keyword_p=*/false,
14961 /*check_dependency_p=*/false,
14962 /*type_p=*/false,
14963 /*is_declaration=*/false)
14964 != NULL_TREE);
14965 /* Outside of a class-specifier, there must be a
14966 nested-name-specifier. */
14967 if (!nested_name_p &&
14968 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14969 || friend_p))
14970 constructor_p = false;
14971 /* If we still think that this might be a constructor-declarator,
14972 look for a class-name. */
14973 if (constructor_p)
14975 /* If we have:
14977 template <typename T> struct S { S(); };
14978 template <typename T> S<T>::S ();
14980 we must recognize that the nested `S' names a class.
14981 Similarly, for:
14983 template <typename T> S<T>::S<T> ();
14985 we must recognize that the nested `S' names a template. */
14986 type_decl = cp_parser_class_name (parser,
14987 /*typename_keyword_p=*/false,
14988 /*template_keyword_p=*/false,
14989 none_type,
14990 /*check_dependency_p=*/false,
14991 /*class_head_p=*/false,
14992 /*is_declaration=*/false);
14993 /* If there was no class-name, then this is not a constructor. */
14994 constructor_p = !cp_parser_error_occurred (parser);
14997 /* If we're still considering a constructor, we have to see a `(',
14998 to begin the parameter-declaration-clause, followed by either a
14999 `)', an `...', or a decl-specifier. We need to check for a
15000 type-specifier to avoid being fooled into thinking that:
15002 S::S (f) (int);
15004 is a constructor. (It is actually a function named `f' that
15005 takes one parameter (of type `int') and returns a value of type
15006 `S::S'. */
15007 if (constructor_p
15008 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15010 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15011 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15012 /* A parameter declaration begins with a decl-specifier,
15013 which is either the "attribute" keyword, a storage class
15014 specifier, or (usually) a type-specifier. */
15015 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15016 && !cp_parser_storage_class_specifier_opt (parser))
15018 tree type;
15019 tree pushed_scope = NULL_TREE;
15020 unsigned saved_num_template_parameter_lists;
15022 /* Names appearing in the type-specifier should be looked up
15023 in the scope of the class. */
15024 if (current_class_type)
15025 type = NULL_TREE;
15026 else
15028 type = TREE_TYPE (type_decl);
15029 if (TREE_CODE (type) == TYPENAME_TYPE)
15031 type = resolve_typename_type (type,
15032 /*only_current_p=*/false);
15033 if (type == error_mark_node)
15035 cp_parser_abort_tentative_parse (parser);
15036 return false;
15039 pushed_scope = push_scope (type);
15042 /* Inside the constructor parameter list, surrounding
15043 template-parameter-lists do not apply. */
15044 saved_num_template_parameter_lists
15045 = parser->num_template_parameter_lists;
15046 parser->num_template_parameter_lists = 0;
15048 /* Look for the type-specifier. */
15049 cp_parser_type_specifier (parser,
15050 CP_PARSER_FLAGS_NONE,
15051 /*decl_specs=*/NULL,
15052 /*is_declarator=*/true,
15053 /*declares_class_or_enum=*/NULL,
15054 /*is_cv_qualifier=*/NULL);
15056 parser->num_template_parameter_lists
15057 = saved_num_template_parameter_lists;
15059 /* Leave the scope of the class. */
15060 if (pushed_scope)
15061 pop_scope (pushed_scope);
15063 constructor_p = !cp_parser_error_occurred (parser);
15066 else
15067 constructor_p = false;
15068 /* We did not really want to consume any tokens. */
15069 cp_parser_abort_tentative_parse (parser);
15071 return constructor_p;
15074 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15075 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15076 they must be performed once we are in the scope of the function.
15078 Returns the function defined. */
15080 static tree
15081 cp_parser_function_definition_from_specifiers_and_declarator
15082 (cp_parser* parser,
15083 cp_decl_specifier_seq *decl_specifiers,
15084 tree attributes,
15085 const cp_declarator *declarator)
15087 tree fn;
15088 bool success_p;
15090 /* Begin the function-definition. */
15091 success_p = start_function (decl_specifiers, declarator, attributes);
15093 /* The things we're about to see are not directly qualified by any
15094 template headers we've seen thus far. */
15095 reset_specialization ();
15097 /* If there were names looked up in the decl-specifier-seq that we
15098 did not check, check them now. We must wait until we are in the
15099 scope of the function to perform the checks, since the function
15100 might be a friend. */
15101 perform_deferred_access_checks ();
15103 if (!success_p)
15105 /* Skip the entire function. */
15106 error ("invalid function declaration");
15107 cp_parser_skip_to_end_of_block_or_statement (parser);
15108 fn = error_mark_node;
15110 else
15111 fn = cp_parser_function_definition_after_declarator (parser,
15112 /*inline_p=*/false);
15114 return fn;
15117 /* Parse the part of a function-definition that follows the
15118 declarator. INLINE_P is TRUE iff this function is an inline
15119 function defined with a class-specifier.
15121 Returns the function defined. */
15123 static tree
15124 cp_parser_function_definition_after_declarator (cp_parser* parser,
15125 bool inline_p)
15127 tree fn;
15128 bool ctor_initializer_p = false;
15129 bool saved_in_unbraced_linkage_specification_p;
15130 unsigned saved_num_template_parameter_lists;
15132 /* If the next token is `return', then the code may be trying to
15133 make use of the "named return value" extension that G++ used to
15134 support. */
15135 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15137 /* Consume the `return' keyword. */
15138 cp_lexer_consume_token (parser->lexer);
15139 /* Look for the identifier that indicates what value is to be
15140 returned. */
15141 cp_parser_identifier (parser);
15142 /* Issue an error message. */
15143 error ("named return values are no longer supported");
15144 /* Skip tokens until we reach the start of the function body. */
15145 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15146 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15147 cp_lexer_consume_token (parser->lexer);
15149 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15150 anything declared inside `f'. */
15151 saved_in_unbraced_linkage_specification_p
15152 = parser->in_unbraced_linkage_specification_p;
15153 parser->in_unbraced_linkage_specification_p = false;
15154 /* Inside the function, surrounding template-parameter-lists do not
15155 apply. */
15156 saved_num_template_parameter_lists
15157 = parser->num_template_parameter_lists;
15158 parser->num_template_parameter_lists = 0;
15159 /* If the next token is `try', then we are looking at a
15160 function-try-block. */
15161 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15162 ctor_initializer_p = cp_parser_function_try_block (parser);
15163 /* A function-try-block includes the function-body, so we only do
15164 this next part if we're not processing a function-try-block. */
15165 else
15166 ctor_initializer_p
15167 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15169 /* Finish the function. */
15170 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15171 (inline_p ? 2 : 0));
15172 /* Generate code for it, if necessary. */
15173 expand_or_defer_fn (fn);
15174 /* Restore the saved values. */
15175 parser->in_unbraced_linkage_specification_p
15176 = saved_in_unbraced_linkage_specification_p;
15177 parser->num_template_parameter_lists
15178 = saved_num_template_parameter_lists;
15180 return fn;
15183 /* Parse a template-declaration, assuming that the `export' (and
15184 `extern') keywords, if present, has already been scanned. MEMBER_P
15185 is as for cp_parser_template_declaration. */
15187 static void
15188 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15190 tree decl = NULL_TREE;
15191 tree parameter_list;
15192 bool friend_p = false;
15193 bool need_lang_pop;
15195 /* Look for the `template' keyword. */
15196 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15197 return;
15199 /* And the `<'. */
15200 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15201 return;
15202 /* [temp]
15204 A template ... shall not have C linkage. */
15205 if (current_lang_name == lang_name_c)
15207 error ("template with C linkage");
15208 /* Give it C++ linkage to avoid confusing other parts of the
15209 front end. */
15210 push_lang_context (lang_name_cplusplus);
15211 need_lang_pop = true;
15213 else
15214 need_lang_pop = false;
15215 /* If the next token is `>', then we have an invalid
15216 specialization. Rather than complain about an invalid template
15217 parameter, issue an error message here. */
15218 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15220 cp_parser_error (parser, "invalid explicit specialization");
15221 begin_specialization ();
15222 parameter_list = NULL_TREE;
15224 else
15226 /* Parse the template parameters. */
15227 begin_template_parm_list ();
15228 parameter_list = cp_parser_template_parameter_list (parser);
15229 parameter_list = end_template_parm_list (parameter_list);
15232 /* Look for the `>'. */
15233 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15234 /* We just processed one more parameter list. */
15235 ++parser->num_template_parameter_lists;
15236 /* If the next token is `template', there are more template
15237 parameters. */
15238 if (cp_lexer_next_token_is_keyword (parser->lexer,
15239 RID_TEMPLATE))
15240 cp_parser_template_declaration_after_export (parser, member_p);
15241 else
15243 /* There are no access checks when parsing a template, as we do not
15244 know if a specialization will be a friend. */
15245 push_deferring_access_checks (dk_no_check);
15247 decl = cp_parser_single_declaration (parser,
15248 member_p,
15249 &friend_p);
15251 pop_deferring_access_checks ();
15253 /* If this is a member template declaration, let the front
15254 end know. */
15255 if (member_p && !friend_p && decl)
15257 if (TREE_CODE (decl) == TYPE_DECL)
15258 cp_parser_check_access_in_redeclaration (decl);
15260 decl = finish_member_template_decl (decl);
15262 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15263 make_friend_class (current_class_type, TREE_TYPE (decl),
15264 /*complain=*/true);
15266 /* We are done with the current parameter list. */
15267 --parser->num_template_parameter_lists;
15269 /* Finish up. */
15270 finish_template_decl (parameter_list);
15272 /* Register member declarations. */
15273 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15274 finish_member_declaration (decl);
15275 /* For the erroneous case of a template with C linkage, we pushed an
15276 implicit C++ linkage scope; exit that scope now. */
15277 if (need_lang_pop)
15278 pop_lang_context ();
15279 /* If DECL is a function template, we must return to parse it later.
15280 (Even though there is no definition, there might be default
15281 arguments that need handling.) */
15282 if (member_p && decl
15283 && (TREE_CODE (decl) == FUNCTION_DECL
15284 || DECL_FUNCTION_TEMPLATE_P (decl)))
15285 TREE_VALUE (parser->unparsed_functions_queues)
15286 = tree_cons (NULL_TREE, decl,
15287 TREE_VALUE (parser->unparsed_functions_queues));
15290 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15291 `function-definition' sequence. MEMBER_P is true, this declaration
15292 appears in a class scope.
15294 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15295 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15297 static tree
15298 cp_parser_single_declaration (cp_parser* parser,
15299 bool member_p,
15300 bool* friend_p)
15302 int declares_class_or_enum;
15303 tree decl = NULL_TREE;
15304 cp_decl_specifier_seq decl_specifiers;
15305 bool function_definition_p = false;
15307 /* This function is only used when processing a template
15308 declaration. */
15309 gcc_assert (innermost_scope_kind () == sk_template_parms
15310 || innermost_scope_kind () == sk_template_spec);
15312 /* Defer access checks until we know what is being declared. */
15313 push_deferring_access_checks (dk_deferred);
15315 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15316 alternative. */
15317 cp_parser_decl_specifier_seq (parser,
15318 CP_PARSER_FLAGS_OPTIONAL,
15319 &decl_specifiers,
15320 &declares_class_or_enum);
15321 if (friend_p)
15322 *friend_p = cp_parser_friend_p (&decl_specifiers);
15324 /* There are no template typedefs. */
15325 if (decl_specifiers.specs[(int) ds_typedef])
15327 error ("template declaration of %qs", "typedef");
15328 decl = error_mark_node;
15331 /* Gather up the access checks that occurred the
15332 decl-specifier-seq. */
15333 stop_deferring_access_checks ();
15335 /* Check for the declaration of a template class. */
15336 if (declares_class_or_enum)
15338 if (cp_parser_declares_only_class_p (parser))
15340 decl = shadow_tag (&decl_specifiers);
15342 /* In this case:
15344 struct C {
15345 friend template <typename T> struct A<T>::B;
15348 A<T>::B will be represented by a TYPENAME_TYPE, and
15349 therefore not recognized by shadow_tag. */
15350 if (friend_p && *friend_p
15351 && !decl
15352 && decl_specifiers.type
15353 && TYPE_P (decl_specifiers.type))
15354 decl = decl_specifiers.type;
15356 if (decl && decl != error_mark_node)
15357 decl = TYPE_NAME (decl);
15358 else
15359 decl = error_mark_node;
15362 /* If it's not a template class, try for a template function. If
15363 the next token is a `;', then this declaration does not declare
15364 anything. But, if there were errors in the decl-specifiers, then
15365 the error might well have come from an attempted class-specifier.
15366 In that case, there's no need to warn about a missing declarator. */
15367 if (!decl
15368 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15369 || decl_specifiers.type != error_mark_node))
15370 decl = cp_parser_init_declarator (parser,
15371 &decl_specifiers,
15372 /*function_definition_allowed_p=*/true,
15373 member_p,
15374 declares_class_or_enum,
15375 &function_definition_p);
15377 pop_deferring_access_checks ();
15379 /* Clear any current qualification; whatever comes next is the start
15380 of something new. */
15381 parser->scope = NULL_TREE;
15382 parser->qualifying_scope = NULL_TREE;
15383 parser->object_scope = NULL_TREE;
15384 /* Look for a trailing `;' after the declaration. */
15385 if (!function_definition_p
15386 && (decl == error_mark_node
15387 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15388 cp_parser_skip_to_end_of_block_or_statement (parser);
15390 return decl;
15393 /* Parse a cast-expression that is not the operand of a unary "&". */
15395 static tree
15396 cp_parser_simple_cast_expression (cp_parser *parser)
15398 return cp_parser_cast_expression (parser, /*address_p=*/false,
15399 /*cast_p=*/false);
15402 /* Parse a functional cast to TYPE. Returns an expression
15403 representing the cast. */
15405 static tree
15406 cp_parser_functional_cast (cp_parser* parser, tree type)
15408 tree expression_list;
15409 tree cast;
15411 expression_list
15412 = cp_parser_parenthesized_expression_list (parser, false,
15413 /*cast_p=*/true,
15414 /*non_constant_p=*/NULL);
15416 cast = build_functional_cast (type, expression_list);
15417 /* [expr.const]/1: In an integral constant expression "only type
15418 conversions to integral or enumeration type can be used". */
15419 if (TREE_CODE (type) == TYPE_DECL)
15420 type = TREE_TYPE (type);
15421 if (cast != error_mark_node && !dependent_type_p (type)
15422 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15424 if (cp_parser_non_integral_constant_expression
15425 (parser, "a call to a constructor"))
15426 return error_mark_node;
15428 return cast;
15431 /* Save the tokens that make up the body of a member function defined
15432 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15433 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15434 specifiers applied to the declaration. Returns the FUNCTION_DECL
15435 for the member function. */
15437 static tree
15438 cp_parser_save_member_function_body (cp_parser* parser,
15439 cp_decl_specifier_seq *decl_specifiers,
15440 cp_declarator *declarator,
15441 tree attributes)
15443 cp_token *first;
15444 cp_token *last;
15445 tree fn;
15447 /* Create the function-declaration. */
15448 fn = start_method (decl_specifiers, declarator, attributes);
15449 /* If something went badly wrong, bail out now. */
15450 if (fn == error_mark_node)
15452 /* If there's a function-body, skip it. */
15453 if (cp_parser_token_starts_function_definition_p
15454 (cp_lexer_peek_token (parser->lexer)))
15455 cp_parser_skip_to_end_of_block_or_statement (parser);
15456 return error_mark_node;
15459 /* Remember it, if there default args to post process. */
15460 cp_parser_save_default_args (parser, fn);
15462 /* Save away the tokens that make up the body of the
15463 function. */
15464 first = parser->lexer->next_token;
15465 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15466 /* Handle function try blocks. */
15467 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15468 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15469 last = parser->lexer->next_token;
15471 /* Save away the inline definition; we will process it when the
15472 class is complete. */
15473 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15474 DECL_PENDING_INLINE_P (fn) = 1;
15476 /* We need to know that this was defined in the class, so that
15477 friend templates are handled correctly. */
15478 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15480 /* We're done with the inline definition. */
15481 finish_method (fn);
15483 /* Add FN to the queue of functions to be parsed later. */
15484 TREE_VALUE (parser->unparsed_functions_queues)
15485 = tree_cons (NULL_TREE, fn,
15486 TREE_VALUE (parser->unparsed_functions_queues));
15488 return fn;
15491 /* Parse a template-argument-list, as well as the trailing ">" (but
15492 not the opening ">"). See cp_parser_template_argument_list for the
15493 return value. */
15495 static tree
15496 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15498 tree arguments;
15499 tree saved_scope;
15500 tree saved_qualifying_scope;
15501 tree saved_object_scope;
15502 bool saved_greater_than_is_operator_p;
15503 bool saved_skip_evaluation;
15505 /* [temp.names]
15507 When parsing a template-id, the first non-nested `>' is taken as
15508 the end of the template-argument-list rather than a greater-than
15509 operator. */
15510 saved_greater_than_is_operator_p
15511 = parser->greater_than_is_operator_p;
15512 parser->greater_than_is_operator_p = false;
15513 /* Parsing the argument list may modify SCOPE, so we save it
15514 here. */
15515 saved_scope = parser->scope;
15516 saved_qualifying_scope = parser->qualifying_scope;
15517 saved_object_scope = parser->object_scope;
15518 /* We need to evaluate the template arguments, even though this
15519 template-id may be nested within a "sizeof". */
15520 saved_skip_evaluation = skip_evaluation;
15521 skip_evaluation = false;
15522 /* Parse the template-argument-list itself. */
15523 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15524 arguments = NULL_TREE;
15525 else
15526 arguments = cp_parser_template_argument_list (parser);
15527 /* Look for the `>' that ends the template-argument-list. If we find
15528 a '>>' instead, it's probably just a typo. */
15529 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15531 if (!saved_greater_than_is_operator_p)
15533 /* If we're in a nested template argument list, the '>>' has
15534 to be a typo for '> >'. We emit the error message, but we
15535 continue parsing and we push a '>' as next token, so that
15536 the argument list will be parsed correctly. Note that the
15537 global source location is still on the token before the
15538 '>>', so we need to say explicitly where we want it. */
15539 cp_token *token = cp_lexer_peek_token (parser->lexer);
15540 error ("%H%<>>%> should be %<> >%> "
15541 "within a nested template argument list",
15542 &token->location);
15544 /* ??? Proper recovery should terminate two levels of
15545 template argument list here. */
15546 token->type = CPP_GREATER;
15548 else
15550 /* If this is not a nested template argument list, the '>>'
15551 is a typo for '>'. Emit an error message and continue.
15552 Same deal about the token location, but here we can get it
15553 right by consuming the '>>' before issuing the diagnostic. */
15554 cp_lexer_consume_token (parser->lexer);
15555 error ("spurious %<>>%>, use %<>%> to terminate "
15556 "a template argument list");
15559 else
15560 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15561 /* The `>' token might be a greater-than operator again now. */
15562 parser->greater_than_is_operator_p
15563 = saved_greater_than_is_operator_p;
15564 /* Restore the SAVED_SCOPE. */
15565 parser->scope = saved_scope;
15566 parser->qualifying_scope = saved_qualifying_scope;
15567 parser->object_scope = saved_object_scope;
15568 skip_evaluation = saved_skip_evaluation;
15570 return arguments;
15573 /* MEMBER_FUNCTION is a member function, or a friend. If default
15574 arguments, or the body of the function have not yet been parsed,
15575 parse them now. */
15577 static void
15578 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15580 /* If this member is a template, get the underlying
15581 FUNCTION_DECL. */
15582 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15583 member_function = DECL_TEMPLATE_RESULT (member_function);
15585 /* There should not be any class definitions in progress at this
15586 point; the bodies of members are only parsed outside of all class
15587 definitions. */
15588 gcc_assert (parser->num_classes_being_defined == 0);
15589 /* While we're parsing the member functions we might encounter more
15590 classes. We want to handle them right away, but we don't want
15591 them getting mixed up with functions that are currently in the
15592 queue. */
15593 parser->unparsed_functions_queues
15594 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15596 /* Make sure that any template parameters are in scope. */
15597 maybe_begin_member_template_processing (member_function);
15599 /* If the body of the function has not yet been parsed, parse it
15600 now. */
15601 if (DECL_PENDING_INLINE_P (member_function))
15603 tree function_scope;
15604 cp_token_cache *tokens;
15606 /* The function is no longer pending; we are processing it. */
15607 tokens = DECL_PENDING_INLINE_INFO (member_function);
15608 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15609 DECL_PENDING_INLINE_P (member_function) = 0;
15611 /* If this is a local class, enter the scope of the containing
15612 function. */
15613 function_scope = current_function_decl;
15614 if (function_scope)
15615 push_function_context_to (function_scope);
15618 /* Push the body of the function onto the lexer stack. */
15619 cp_parser_push_lexer_for_tokens (parser, tokens);
15621 /* Let the front end know that we going to be defining this
15622 function. */
15623 start_preparsed_function (member_function, NULL_TREE,
15624 SF_PRE_PARSED | SF_INCLASS_INLINE);
15626 /* Don't do access checking if it is a templated function. */
15627 if (processing_template_decl)
15628 push_deferring_access_checks (dk_no_check);
15630 /* Now, parse the body of the function. */
15631 cp_parser_function_definition_after_declarator (parser,
15632 /*inline_p=*/true);
15634 if (processing_template_decl)
15635 pop_deferring_access_checks ();
15637 /* Leave the scope of the containing function. */
15638 if (function_scope)
15639 pop_function_context_from (function_scope);
15640 cp_parser_pop_lexer (parser);
15643 /* Remove any template parameters from the symbol table. */
15644 maybe_end_member_template_processing ();
15646 /* Restore the queue. */
15647 parser->unparsed_functions_queues
15648 = TREE_CHAIN (parser->unparsed_functions_queues);
15651 /* If DECL contains any default args, remember it on the unparsed
15652 functions queue. */
15654 static void
15655 cp_parser_save_default_args (cp_parser* parser, tree decl)
15657 tree probe;
15659 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15660 probe;
15661 probe = TREE_CHAIN (probe))
15662 if (TREE_PURPOSE (probe))
15664 TREE_PURPOSE (parser->unparsed_functions_queues)
15665 = tree_cons (current_class_type, decl,
15666 TREE_PURPOSE (parser->unparsed_functions_queues));
15667 break;
15669 return;
15672 /* FN is a FUNCTION_DECL which may contains a parameter with an
15673 unparsed DEFAULT_ARG. Parse the default args now. This function
15674 assumes that the current scope is the scope in which the default
15675 argument should be processed. */
15677 static void
15678 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15680 bool saved_local_variables_forbidden_p;
15681 tree parm;
15683 /* While we're parsing the default args, we might (due to the
15684 statement expression extension) encounter more classes. We want
15685 to handle them right away, but we don't want them getting mixed
15686 up with default args that are currently in the queue. */
15687 parser->unparsed_functions_queues
15688 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15690 /* Local variable names (and the `this' keyword) may not appear
15691 in a default argument. */
15692 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15693 parser->local_variables_forbidden_p = true;
15695 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15696 parm;
15697 parm = TREE_CHAIN (parm))
15699 cp_token_cache *tokens;
15700 tree default_arg = TREE_PURPOSE (parm);
15701 tree parsed_arg;
15702 VEC(tree,gc) *insts;
15703 tree copy;
15704 unsigned ix;
15706 if (!default_arg)
15707 continue;
15709 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15710 /* This can happen for a friend declaration for a function
15711 already declared with default arguments. */
15712 continue;
15714 /* Push the saved tokens for the default argument onto the parser's
15715 lexer stack. */
15716 tokens = DEFARG_TOKENS (default_arg);
15717 cp_parser_push_lexer_for_tokens (parser, tokens);
15719 /* Parse the assignment-expression. */
15720 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15722 if (!processing_template_decl)
15723 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15725 TREE_PURPOSE (parm) = parsed_arg;
15727 /* Update any instantiations we've already created. */
15728 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15729 VEC_iterate (tree, insts, ix, copy); ix++)
15730 TREE_PURPOSE (copy) = parsed_arg;
15732 /* If the token stream has not been completely used up, then
15733 there was extra junk after the end of the default
15734 argument. */
15735 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15736 cp_parser_error (parser, "expected %<,%>");
15738 /* Revert to the main lexer. */
15739 cp_parser_pop_lexer (parser);
15742 /* Restore the state of local_variables_forbidden_p. */
15743 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15745 /* Restore the queue. */
15746 parser->unparsed_functions_queues
15747 = TREE_CHAIN (parser->unparsed_functions_queues);
15750 /* Parse the operand of `sizeof' (or a similar operator). Returns
15751 either a TYPE or an expression, depending on the form of the
15752 input. The KEYWORD indicates which kind of expression we have
15753 encountered. */
15755 static tree
15756 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15758 static const char *format;
15759 tree expr = NULL_TREE;
15760 const char *saved_message;
15761 bool saved_integral_constant_expression_p;
15762 bool saved_non_integral_constant_expression_p;
15764 /* Initialize FORMAT the first time we get here. */
15765 if (!format)
15766 format = "types may not be defined in '%s' expressions";
15768 /* Types cannot be defined in a `sizeof' expression. Save away the
15769 old message. */
15770 saved_message = parser->type_definition_forbidden_message;
15771 /* And create the new one. */
15772 parser->type_definition_forbidden_message
15773 = XNEWVEC (const char, strlen (format)
15774 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15775 + 1 /* `\0' */);
15776 sprintf ((char *) parser->type_definition_forbidden_message,
15777 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15779 /* The restrictions on constant-expressions do not apply inside
15780 sizeof expressions. */
15781 saved_integral_constant_expression_p
15782 = parser->integral_constant_expression_p;
15783 saved_non_integral_constant_expression_p
15784 = parser->non_integral_constant_expression_p;
15785 parser->integral_constant_expression_p = false;
15787 /* Do not actually evaluate the expression. */
15788 ++skip_evaluation;
15789 /* If it's a `(', then we might be looking at the type-id
15790 construction. */
15791 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15793 tree type;
15794 bool saved_in_type_id_in_expr_p;
15796 /* We can't be sure yet whether we're looking at a type-id or an
15797 expression. */
15798 cp_parser_parse_tentatively (parser);
15799 /* Consume the `('. */
15800 cp_lexer_consume_token (parser->lexer);
15801 /* Parse the type-id. */
15802 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15803 parser->in_type_id_in_expr_p = true;
15804 type = cp_parser_type_id (parser);
15805 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15806 /* Now, look for the trailing `)'. */
15807 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15808 /* If all went well, then we're done. */
15809 if (cp_parser_parse_definitely (parser))
15811 cp_decl_specifier_seq decl_specs;
15813 /* Build a trivial decl-specifier-seq. */
15814 clear_decl_specs (&decl_specs);
15815 decl_specs.type = type;
15817 /* Call grokdeclarator to figure out what type this is. */
15818 expr = grokdeclarator (NULL,
15819 &decl_specs,
15820 TYPENAME,
15821 /*initialized=*/0,
15822 /*attrlist=*/NULL);
15826 /* If the type-id production did not work out, then we must be
15827 looking at the unary-expression production. */
15828 if (!expr)
15829 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15830 /*cast_p=*/false);
15831 /* Go back to evaluating expressions. */
15832 --skip_evaluation;
15834 /* Free the message we created. */
15835 free ((char *) parser->type_definition_forbidden_message);
15836 /* And restore the old one. */
15837 parser->type_definition_forbidden_message = saved_message;
15838 parser->integral_constant_expression_p
15839 = saved_integral_constant_expression_p;
15840 parser->non_integral_constant_expression_p
15841 = saved_non_integral_constant_expression_p;
15843 return expr;
15846 /* If the current declaration has no declarator, return true. */
15848 static bool
15849 cp_parser_declares_only_class_p (cp_parser *parser)
15851 /* If the next token is a `;' or a `,' then there is no
15852 declarator. */
15853 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15854 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15857 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15859 static void
15860 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15861 cp_storage_class storage_class)
15863 if (decl_specs->storage_class != sc_none)
15864 decl_specs->multiple_storage_classes_p = true;
15865 else
15866 decl_specs->storage_class = storage_class;
15869 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15870 is true, the type is a user-defined type; otherwise it is a
15871 built-in type specified by a keyword. */
15873 static void
15874 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15875 tree type_spec,
15876 bool user_defined_p)
15878 decl_specs->any_specifiers_p = true;
15880 /* If the user tries to redeclare bool or wchar_t (with, for
15881 example, in "typedef int wchar_t;") we remember that this is what
15882 happened. In system headers, we ignore these declarations so
15883 that G++ can work with system headers that are not C++-safe. */
15884 if (decl_specs->specs[(int) ds_typedef]
15885 && !user_defined_p
15886 && (type_spec == boolean_type_node
15887 || type_spec == wchar_type_node)
15888 && (decl_specs->type
15889 || decl_specs->specs[(int) ds_long]
15890 || decl_specs->specs[(int) ds_short]
15891 || decl_specs->specs[(int) ds_unsigned]
15892 || decl_specs->specs[(int) ds_signed]))
15894 decl_specs->redefined_builtin_type = type_spec;
15895 if (!decl_specs->type)
15897 decl_specs->type = type_spec;
15898 decl_specs->user_defined_type_p = false;
15901 else if (decl_specs->type)
15902 decl_specs->multiple_types_p = true;
15903 else
15905 decl_specs->type = type_spec;
15906 decl_specs->user_defined_type_p = user_defined_p;
15907 decl_specs->redefined_builtin_type = NULL_TREE;
15911 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15912 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15914 static bool
15915 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15917 return decl_specifiers->specs[(int) ds_friend] != 0;
15920 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15921 issue an error message indicating that TOKEN_DESC was expected.
15923 Returns the token consumed, if the token had the appropriate type.
15924 Otherwise, returns NULL. */
15926 static cp_token *
15927 cp_parser_require (cp_parser* parser,
15928 enum cpp_ttype type,
15929 const char* token_desc)
15931 if (cp_lexer_next_token_is (parser->lexer, type))
15932 return cp_lexer_consume_token (parser->lexer);
15933 else
15935 /* Output the MESSAGE -- unless we're parsing tentatively. */
15936 if (!cp_parser_simulate_error (parser))
15938 char *message = concat ("expected ", token_desc, NULL);
15939 cp_parser_error (parser, message);
15940 free (message);
15942 return NULL;
15946 /* Like cp_parser_require, except that tokens will be skipped until
15947 the desired token is found. An error message is still produced if
15948 the next token is not as expected. */
15950 static void
15951 cp_parser_skip_until_found (cp_parser* parser,
15952 enum cpp_ttype type,
15953 const char* token_desc)
15955 cp_token *token;
15956 unsigned nesting_depth = 0;
15958 if (cp_parser_require (parser, type, token_desc))
15959 return;
15961 /* Skip tokens until the desired token is found. */
15962 while (true)
15964 /* Peek at the next token. */
15965 token = cp_lexer_peek_token (parser->lexer);
15966 /* If we've reached the token we want, consume it and
15967 stop. */
15968 if (token->type == type && !nesting_depth)
15970 cp_lexer_consume_token (parser->lexer);
15971 return;
15973 /* If we've run out of tokens, stop. */
15974 if (token->type == CPP_EOF)
15975 return;
15976 if (token->type == CPP_OPEN_BRACE
15977 || token->type == CPP_OPEN_PAREN
15978 || token->type == CPP_OPEN_SQUARE)
15979 ++nesting_depth;
15980 else if (token->type == CPP_CLOSE_BRACE
15981 || token->type == CPP_CLOSE_PAREN
15982 || token->type == CPP_CLOSE_SQUARE)
15984 if (nesting_depth-- == 0)
15985 return;
15987 /* Consume this token. */
15988 cp_lexer_consume_token (parser->lexer);
15992 /* If the next token is the indicated keyword, consume it. Otherwise,
15993 issue an error message indicating that TOKEN_DESC was expected.
15995 Returns the token consumed, if the token had the appropriate type.
15996 Otherwise, returns NULL. */
15998 static cp_token *
15999 cp_parser_require_keyword (cp_parser* parser,
16000 enum rid keyword,
16001 const char* token_desc)
16003 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16005 if (token && token->keyword != keyword)
16007 dyn_string_t error_msg;
16009 /* Format the error message. */
16010 error_msg = dyn_string_new (0);
16011 dyn_string_append_cstr (error_msg, "expected ");
16012 dyn_string_append_cstr (error_msg, token_desc);
16013 cp_parser_error (parser, error_msg->s);
16014 dyn_string_delete (error_msg);
16015 return NULL;
16018 return token;
16021 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16022 function-definition. */
16024 static bool
16025 cp_parser_token_starts_function_definition_p (cp_token* token)
16027 return (/* An ordinary function-body begins with an `{'. */
16028 token->type == CPP_OPEN_BRACE
16029 /* A ctor-initializer begins with a `:'. */
16030 || token->type == CPP_COLON
16031 /* A function-try-block begins with `try'. */
16032 || token->keyword == RID_TRY
16033 /* The named return value extension begins with `return'. */
16034 || token->keyword == RID_RETURN);
16037 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16038 definition. */
16040 static bool
16041 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16043 cp_token *token;
16045 token = cp_lexer_peek_token (parser->lexer);
16046 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16049 /* Returns TRUE iff the next token is the "," or ">" ending a
16050 template-argument. */
16052 static bool
16053 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16055 cp_token *token;
16057 token = cp_lexer_peek_token (parser->lexer);
16058 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16061 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16062 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16064 static bool
16065 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16066 size_t n)
16068 cp_token *token;
16070 token = cp_lexer_peek_nth_token (parser->lexer, n);
16071 if (token->type == CPP_LESS)
16072 return true;
16073 /* Check for the sequence `<::' in the original code. It would be lexed as
16074 `[:', where `[' is a digraph, and there is no whitespace before
16075 `:'. */
16076 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16078 cp_token *token2;
16079 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16080 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16081 return true;
16083 return false;
16086 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16087 or none_type otherwise. */
16089 static enum tag_types
16090 cp_parser_token_is_class_key (cp_token* token)
16092 switch (token->keyword)
16094 case RID_CLASS:
16095 return class_type;
16096 case RID_STRUCT:
16097 return record_type;
16098 case RID_UNION:
16099 return union_type;
16101 default:
16102 return none_type;
16106 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16108 static void
16109 cp_parser_check_class_key (enum tag_types class_key, tree type)
16111 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16112 pedwarn ("%qs tag used in naming %q#T",
16113 class_key == union_type ? "union"
16114 : class_key == record_type ? "struct" : "class",
16115 type);
16118 /* Issue an error message if DECL is redeclared with different
16119 access than its original declaration [class.access.spec/3].
16120 This applies to nested classes and nested class templates.
16121 [class.mem/1]. */
16123 static void
16124 cp_parser_check_access_in_redeclaration (tree decl)
16126 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16127 return;
16129 if ((TREE_PRIVATE (decl)
16130 != (current_access_specifier == access_private_node))
16131 || (TREE_PROTECTED (decl)
16132 != (current_access_specifier == access_protected_node)))
16133 error ("%qD redeclared with different access", decl);
16136 /* Look for the `template' keyword, as a syntactic disambiguator.
16137 Return TRUE iff it is present, in which case it will be
16138 consumed. */
16140 static bool
16141 cp_parser_optional_template_keyword (cp_parser *parser)
16143 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16145 /* The `template' keyword can only be used within templates;
16146 outside templates the parser can always figure out what is a
16147 template and what is not. */
16148 if (!processing_template_decl)
16150 error ("%<template%> (as a disambiguator) is only allowed "
16151 "within templates");
16152 /* If this part of the token stream is rescanned, the same
16153 error message would be generated. So, we purge the token
16154 from the stream. */
16155 cp_lexer_purge_token (parser->lexer);
16156 return false;
16158 else
16160 /* Consume the `template' keyword. */
16161 cp_lexer_consume_token (parser->lexer);
16162 return true;
16166 return false;
16169 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16170 set PARSER->SCOPE, and perform other related actions. */
16172 static void
16173 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16175 tree value;
16176 tree check;
16178 /* Get the stored value. */
16179 value = cp_lexer_consume_token (parser->lexer)->value;
16180 /* Perform any access checks that were deferred. */
16181 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16182 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16183 /* Set the scope from the stored value. */
16184 parser->scope = TREE_VALUE (value);
16185 parser->qualifying_scope = TREE_TYPE (value);
16186 parser->object_scope = NULL_TREE;
16189 /* Consume tokens up through a non-nested END token. */
16191 static void
16192 cp_parser_cache_group (cp_parser *parser,
16193 enum cpp_ttype end,
16194 unsigned depth)
16196 while (true)
16198 cp_token *token;
16200 /* Abort a parenthesized expression if we encounter a brace. */
16201 if ((end == CPP_CLOSE_PAREN || depth == 0)
16202 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16203 return;
16204 /* If we've reached the end of the file, stop. */
16205 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16206 return;
16207 /* Consume the next token. */
16208 token = cp_lexer_consume_token (parser->lexer);
16209 /* See if it starts a new group. */
16210 if (token->type == CPP_OPEN_BRACE)
16212 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16213 if (depth == 0)
16214 return;
16216 else if (token->type == CPP_OPEN_PAREN)
16217 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16218 else if (token->type == end)
16219 return;
16223 /* Begin parsing tentatively. We always save tokens while parsing
16224 tentatively so that if the tentative parsing fails we can restore the
16225 tokens. */
16227 static void
16228 cp_parser_parse_tentatively (cp_parser* parser)
16230 /* Enter a new parsing context. */
16231 parser->context = cp_parser_context_new (parser->context);
16232 /* Begin saving tokens. */
16233 cp_lexer_save_tokens (parser->lexer);
16234 /* In order to avoid repetitive access control error messages,
16235 access checks are queued up until we are no longer parsing
16236 tentatively. */
16237 push_deferring_access_checks (dk_deferred);
16240 /* Commit to the currently active tentative parse. */
16242 static void
16243 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16245 cp_parser_context *context;
16246 cp_lexer *lexer;
16248 /* Mark all of the levels as committed. */
16249 lexer = parser->lexer;
16250 for (context = parser->context; context->next; context = context->next)
16252 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16253 break;
16254 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16255 while (!cp_lexer_saving_tokens (lexer))
16256 lexer = lexer->next;
16257 cp_lexer_commit_tokens (lexer);
16261 /* Abort the currently active tentative parse. All consumed tokens
16262 will be rolled back, and no diagnostics will be issued. */
16264 static void
16265 cp_parser_abort_tentative_parse (cp_parser* parser)
16267 cp_parser_simulate_error (parser);
16268 /* Now, pretend that we want to see if the construct was
16269 successfully parsed. */
16270 cp_parser_parse_definitely (parser);
16273 /* Stop parsing tentatively. If a parse error has occurred, restore the
16274 token stream. Otherwise, commit to the tokens we have consumed.
16275 Returns true if no error occurred; false otherwise. */
16277 static bool
16278 cp_parser_parse_definitely (cp_parser* parser)
16280 bool error_occurred;
16281 cp_parser_context *context;
16283 /* Remember whether or not an error occurred, since we are about to
16284 destroy that information. */
16285 error_occurred = cp_parser_error_occurred (parser);
16286 /* Remove the topmost context from the stack. */
16287 context = parser->context;
16288 parser->context = context->next;
16289 /* If no parse errors occurred, commit to the tentative parse. */
16290 if (!error_occurred)
16292 /* Commit to the tokens read tentatively, unless that was
16293 already done. */
16294 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16295 cp_lexer_commit_tokens (parser->lexer);
16297 pop_to_parent_deferring_access_checks ();
16299 /* Otherwise, if errors occurred, roll back our state so that things
16300 are just as they were before we began the tentative parse. */
16301 else
16303 cp_lexer_rollback_tokens (parser->lexer);
16304 pop_deferring_access_checks ();
16306 /* Add the context to the front of the free list. */
16307 context->next = cp_parser_context_free_list;
16308 cp_parser_context_free_list = context;
16310 return !error_occurred;
16313 /* Returns true if we are parsing tentatively and are not committed to
16314 this tentative parse. */
16316 static bool
16317 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16319 return (cp_parser_parsing_tentatively (parser)
16320 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16323 /* Returns nonzero iff an error has occurred during the most recent
16324 tentative parse. */
16326 static bool
16327 cp_parser_error_occurred (cp_parser* parser)
16329 return (cp_parser_parsing_tentatively (parser)
16330 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16333 /* Returns nonzero if GNU extensions are allowed. */
16335 static bool
16336 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16338 return parser->allow_gnu_extensions_p;
16341 /* Objective-C++ Productions */
16344 /* Parse an Objective-C expression, which feeds into a primary-expression
16345 above.
16347 objc-expression:
16348 objc-message-expression
16349 objc-string-literal
16350 objc-encode-expression
16351 objc-protocol-expression
16352 objc-selector-expression
16354 Returns a tree representation of the expression. */
16356 static tree
16357 cp_parser_objc_expression (cp_parser* parser)
16359 /* Try to figure out what kind of declaration is present. */
16360 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16362 switch (kwd->type)
16364 case CPP_OPEN_SQUARE:
16365 return cp_parser_objc_message_expression (parser);
16367 case CPP_OBJC_STRING:
16368 kwd = cp_lexer_consume_token (parser->lexer);
16369 return objc_build_string_object (kwd->value);
16371 case CPP_KEYWORD:
16372 switch (kwd->keyword)
16374 case RID_AT_ENCODE:
16375 return cp_parser_objc_encode_expression (parser);
16377 case RID_AT_PROTOCOL:
16378 return cp_parser_objc_protocol_expression (parser);
16380 case RID_AT_SELECTOR:
16381 return cp_parser_objc_selector_expression (parser);
16383 default:
16384 break;
16386 default:
16387 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16388 cp_parser_skip_to_end_of_block_or_statement (parser);
16391 return error_mark_node;
16394 /* Parse an Objective-C message expression.
16396 objc-message-expression:
16397 [ objc-message-receiver objc-message-args ]
16399 Returns a representation of an Objective-C message. */
16401 static tree
16402 cp_parser_objc_message_expression (cp_parser* parser)
16404 tree receiver, messageargs;
16406 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16407 receiver = cp_parser_objc_message_receiver (parser);
16408 messageargs = cp_parser_objc_message_args (parser);
16409 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16411 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16414 /* Parse an objc-message-receiver.
16416 objc-message-receiver:
16417 expression
16418 simple-type-specifier
16420 Returns a representation of the type or expression. */
16422 static tree
16423 cp_parser_objc_message_receiver (cp_parser* parser)
16425 tree rcv;
16427 /* An Objective-C message receiver may be either (1) a type
16428 or (2) an expression. */
16429 cp_parser_parse_tentatively (parser);
16430 rcv = cp_parser_expression (parser, false);
16432 if (cp_parser_parse_definitely (parser))
16433 return rcv;
16435 rcv = cp_parser_simple_type_specifier (parser,
16436 /*decl_specs=*/NULL,
16437 CP_PARSER_FLAGS_NONE);
16439 return objc_get_class_reference (rcv);
16442 /* Parse the arguments and selectors comprising an Objective-C message.
16444 objc-message-args:
16445 objc-selector
16446 objc-selector-args
16447 objc-selector-args , objc-comma-args
16449 objc-selector-args:
16450 objc-selector [opt] : assignment-expression
16451 objc-selector-args objc-selector [opt] : assignment-expression
16453 objc-comma-args:
16454 assignment-expression
16455 objc-comma-args , assignment-expression
16457 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16458 selector arguments and TREE_VALUE containing a list of comma
16459 arguments. */
16461 static tree
16462 cp_parser_objc_message_args (cp_parser* parser)
16464 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16465 bool maybe_unary_selector_p = true;
16466 cp_token *token = cp_lexer_peek_token (parser->lexer);
16468 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16470 tree selector = NULL_TREE, arg;
16472 if (token->type != CPP_COLON)
16473 selector = cp_parser_objc_selector (parser);
16475 /* Detect if we have a unary selector. */
16476 if (maybe_unary_selector_p
16477 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16478 return build_tree_list (selector, NULL_TREE);
16480 maybe_unary_selector_p = false;
16481 cp_parser_require (parser, CPP_COLON, "`:'");
16482 arg = cp_parser_assignment_expression (parser, false);
16484 sel_args
16485 = chainon (sel_args,
16486 build_tree_list (selector, arg));
16488 token = cp_lexer_peek_token (parser->lexer);
16491 /* Handle non-selector arguments, if any. */
16492 while (token->type == CPP_COMMA)
16494 tree arg;
16496 cp_lexer_consume_token (parser->lexer);
16497 arg = cp_parser_assignment_expression (parser, false);
16499 addl_args
16500 = chainon (addl_args,
16501 build_tree_list (NULL_TREE, arg));
16503 token = cp_lexer_peek_token (parser->lexer);
16506 return build_tree_list (sel_args, addl_args);
16509 /* Parse an Objective-C encode expression.
16511 objc-encode-expression:
16512 @encode objc-typename
16514 Returns an encoded representation of the type argument. */
16516 static tree
16517 cp_parser_objc_encode_expression (cp_parser* parser)
16519 tree type;
16521 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16522 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16523 type = complete_type (cp_parser_type_id (parser));
16524 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16526 if (!type)
16528 error ("%<@encode%> must specify a type as an argument");
16529 return error_mark_node;
16532 return objc_build_encode_expr (type);
16535 /* Parse an Objective-C @defs expression. */
16537 static tree
16538 cp_parser_objc_defs_expression (cp_parser *parser)
16540 tree name;
16542 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16543 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16544 name = cp_parser_identifier (parser);
16545 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16547 return objc_get_class_ivars (name);
16550 /* Parse an Objective-C protocol expression.
16552 objc-protocol-expression:
16553 @protocol ( identifier )
16555 Returns a representation of the protocol expression. */
16557 static tree
16558 cp_parser_objc_protocol_expression (cp_parser* parser)
16560 tree proto;
16562 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16563 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16564 proto = cp_parser_identifier (parser);
16565 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16567 return objc_build_protocol_expr (proto);
16570 /* Parse an Objective-C selector expression.
16572 objc-selector-expression:
16573 @selector ( objc-method-signature )
16575 objc-method-signature:
16576 objc-selector
16577 objc-selector-seq
16579 objc-selector-seq:
16580 objc-selector :
16581 objc-selector-seq objc-selector :
16583 Returns a representation of the method selector. */
16585 static tree
16586 cp_parser_objc_selector_expression (cp_parser* parser)
16588 tree sel_seq = NULL_TREE;
16589 bool maybe_unary_selector_p = true;
16590 cp_token *token;
16592 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16593 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16594 token = cp_lexer_peek_token (parser->lexer);
16596 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16597 || token->type == CPP_SCOPE)
16599 tree selector = NULL_TREE;
16601 if (token->type != CPP_COLON
16602 || token->type == CPP_SCOPE)
16603 selector = cp_parser_objc_selector (parser);
16605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16606 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16608 /* Detect if we have a unary selector. */
16609 if (maybe_unary_selector_p)
16611 sel_seq = selector;
16612 goto finish_selector;
16614 else
16616 cp_parser_error (parser, "expected %<:%>");
16619 maybe_unary_selector_p = false;
16620 token = cp_lexer_consume_token (parser->lexer);
16622 if (token->type == CPP_SCOPE)
16624 sel_seq
16625 = chainon (sel_seq,
16626 build_tree_list (selector, NULL_TREE));
16627 sel_seq
16628 = chainon (sel_seq,
16629 build_tree_list (NULL_TREE, NULL_TREE));
16631 else
16632 sel_seq
16633 = chainon (sel_seq,
16634 build_tree_list (selector, NULL_TREE));
16636 token = cp_lexer_peek_token (parser->lexer);
16639 finish_selector:
16640 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16642 return objc_build_selector_expr (sel_seq);
16645 /* Parse a list of identifiers.
16647 objc-identifier-list:
16648 identifier
16649 objc-identifier-list , identifier
16651 Returns a TREE_LIST of identifier nodes. */
16653 static tree
16654 cp_parser_objc_identifier_list (cp_parser* parser)
16656 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16657 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16659 while (sep->type == CPP_COMMA)
16661 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16662 list = chainon (list,
16663 build_tree_list (NULL_TREE,
16664 cp_parser_identifier (parser)));
16665 sep = cp_lexer_peek_token (parser->lexer);
16668 return list;
16671 /* Parse an Objective-C alias declaration.
16673 objc-alias-declaration:
16674 @compatibility_alias identifier identifier ;
16676 This function registers the alias mapping with the Objective-C front-end.
16677 It returns nothing. */
16679 static void
16680 cp_parser_objc_alias_declaration (cp_parser* parser)
16682 tree alias, orig;
16684 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
16685 alias = cp_parser_identifier (parser);
16686 orig = cp_parser_identifier (parser);
16687 objc_declare_alias (alias, orig);
16688 cp_parser_consume_semicolon_at_end_of_statement (parser);
16691 /* Parse an Objective-C class forward-declaration.
16693 objc-class-declaration:
16694 @class objc-identifier-list ;
16696 The function registers the forward declarations with the Objective-C
16697 front-end. It returns nothing. */
16699 static void
16700 cp_parser_objc_class_declaration (cp_parser* parser)
16702 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
16703 objc_declare_class (cp_parser_objc_identifier_list (parser));
16704 cp_parser_consume_semicolon_at_end_of_statement (parser);
16707 /* Parse a list of Objective-C protocol references.
16709 objc-protocol-refs-opt:
16710 objc-protocol-refs [opt]
16712 objc-protocol-refs:
16713 < objc-identifier-list >
16715 Returns a TREE_LIST of identifiers, if any. */
16717 static tree
16718 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16720 tree protorefs = NULL_TREE;
16722 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16724 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
16725 protorefs = cp_parser_objc_identifier_list (parser);
16726 cp_parser_require (parser, CPP_GREATER, "`>'");
16729 return protorefs;
16732 /* Parse a Objective-C visibility specification. */
16734 static void
16735 cp_parser_objc_visibility_spec (cp_parser* parser)
16737 cp_token *vis = cp_lexer_peek_token (parser->lexer);
16739 switch (vis->keyword)
16741 case RID_AT_PRIVATE:
16742 objc_set_visibility (2);
16743 break;
16744 case RID_AT_PROTECTED:
16745 objc_set_visibility (0);
16746 break;
16747 case RID_AT_PUBLIC:
16748 objc_set_visibility (1);
16749 break;
16750 default:
16751 return;
16754 /* Eat '@private'/'@protected'/'@public'. */
16755 cp_lexer_consume_token (parser->lexer);
16758 /* Parse an Objective-C method type. */
16760 static void
16761 cp_parser_objc_method_type (cp_parser* parser)
16763 objc_set_method_type
16764 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16765 ? PLUS_EXPR
16766 : MINUS_EXPR);
16769 /* Parse an Objective-C protocol qualifier. */
16771 static tree
16772 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16774 tree quals = NULL_TREE, node;
16775 cp_token *token = cp_lexer_peek_token (parser->lexer);
16777 node = token->value;
16779 while (node && TREE_CODE (node) == IDENTIFIER_NODE
16780 && (node == ridpointers [(int) RID_IN]
16781 || node == ridpointers [(int) RID_OUT]
16782 || node == ridpointers [(int) RID_INOUT]
16783 || node == ridpointers [(int) RID_BYCOPY]
16784 || node == ridpointers [(int) RID_BYREF]
16785 || node == ridpointers [(int) RID_ONEWAY]))
16787 quals = tree_cons (NULL_TREE, node, quals);
16788 cp_lexer_consume_token (parser->lexer);
16789 token = cp_lexer_peek_token (parser->lexer);
16790 node = token->value;
16793 return quals;
16796 /* Parse an Objective-C typename. */
16798 static tree
16799 cp_parser_objc_typename (cp_parser* parser)
16801 tree typename = NULL_TREE;
16803 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16805 tree proto_quals, cp_type = NULL_TREE;
16807 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
16808 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16810 /* An ObjC type name may consist of just protocol qualifiers, in which
16811 case the type shall default to 'id'. */
16812 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16813 cp_type = cp_parser_type_id (parser);
16815 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16816 typename = build_tree_list (proto_quals, cp_type);
16819 return typename;
16822 /* Check to see if TYPE refers to an Objective-C selector name. */
16824 static bool
16825 cp_parser_objc_selector_p (enum cpp_ttype type)
16827 return (type == CPP_NAME || type == CPP_KEYWORD
16828 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16829 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16830 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16831 || type == CPP_XOR || type == CPP_XOR_EQ);
16834 /* Parse an Objective-C selector. */
16836 static tree
16837 cp_parser_objc_selector (cp_parser* parser)
16839 cp_token *token = cp_lexer_consume_token (parser->lexer);
16841 if (!cp_parser_objc_selector_p (token->type))
16843 error ("invalid Objective-C++ selector name");
16844 return error_mark_node;
16847 /* C++ operator names are allowed to appear in ObjC selectors. */
16848 switch (token->type)
16850 case CPP_AND_AND: return get_identifier ("and");
16851 case CPP_AND_EQ: return get_identifier ("and_eq");
16852 case CPP_AND: return get_identifier ("bitand");
16853 case CPP_OR: return get_identifier ("bitor");
16854 case CPP_COMPL: return get_identifier ("compl");
16855 case CPP_NOT: return get_identifier ("not");
16856 case CPP_NOT_EQ: return get_identifier ("not_eq");
16857 case CPP_OR_OR: return get_identifier ("or");
16858 case CPP_OR_EQ: return get_identifier ("or_eq");
16859 case CPP_XOR: return get_identifier ("xor");
16860 case CPP_XOR_EQ: return get_identifier ("xor_eq");
16861 default: return token->value;
16865 /* Parse an Objective-C params list. */
16867 static tree
16868 cp_parser_objc_method_keyword_params (cp_parser* parser)
16870 tree params = NULL_TREE;
16871 bool maybe_unary_selector_p = true;
16872 cp_token *token = cp_lexer_peek_token (parser->lexer);
16874 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16876 tree selector = NULL_TREE, typename, identifier;
16878 if (token->type != CPP_COLON)
16879 selector = cp_parser_objc_selector (parser);
16881 /* Detect if we have a unary selector. */
16882 if (maybe_unary_selector_p
16883 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16884 return selector;
16886 maybe_unary_selector_p = false;
16887 cp_parser_require (parser, CPP_COLON, "`:'");
16888 typename = cp_parser_objc_typename (parser);
16889 identifier = cp_parser_identifier (parser);
16891 params
16892 = chainon (params,
16893 objc_build_keyword_decl (selector,
16894 typename,
16895 identifier));
16897 token = cp_lexer_peek_token (parser->lexer);
16900 return params;
16903 /* Parse the non-keyword Objective-C params. */
16905 static tree
16906 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
16908 tree params = make_node (TREE_LIST);
16909 cp_token *token = cp_lexer_peek_token (parser->lexer);
16910 *ellipsisp = false; /* Initially, assume no ellipsis. */
16912 while (token->type == CPP_COMMA)
16914 cp_parameter_declarator *parmdecl;
16915 tree parm;
16917 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16918 token = cp_lexer_peek_token (parser->lexer);
16920 if (token->type == CPP_ELLIPSIS)
16922 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
16923 *ellipsisp = true;
16924 break;
16927 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
16928 parm = grokdeclarator (parmdecl->declarator,
16929 &parmdecl->decl_specifiers,
16930 PARM, /*initialized=*/0,
16931 /*attrlist=*/NULL);
16933 chainon (params, build_tree_list (NULL_TREE, parm));
16934 token = cp_lexer_peek_token (parser->lexer);
16937 return params;
16940 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
16942 static void
16943 cp_parser_objc_interstitial_code (cp_parser* parser)
16945 cp_token *token = cp_lexer_peek_token (parser->lexer);
16947 /* If the next token is `extern' and the following token is a string
16948 literal, then we have a linkage specification. */
16949 if (token->keyword == RID_EXTERN
16950 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
16951 cp_parser_linkage_specification (parser);
16952 /* Handle #pragma, if any. */
16953 else if (token->type == CPP_PRAGMA)
16954 cp_lexer_handle_pragma (parser->lexer);
16955 /* Allow stray semicolons. */
16956 else if (token->type == CPP_SEMICOLON)
16957 cp_lexer_consume_token (parser->lexer);
16958 /* Finally, try to parse a block-declaration, or a function-definition. */
16959 else
16960 cp_parser_block_declaration (parser, /*statement_p=*/false);
16963 /* Parse a method signature. */
16965 static tree
16966 cp_parser_objc_method_signature (cp_parser* parser)
16968 tree rettype, kwdparms, optparms;
16969 bool ellipsis = false;
16971 cp_parser_objc_method_type (parser);
16972 rettype = cp_parser_objc_typename (parser);
16973 kwdparms = cp_parser_objc_method_keyword_params (parser);
16974 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
16976 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
16979 /* Pars an Objective-C method prototype list. */
16981 static void
16982 cp_parser_objc_method_prototype_list (cp_parser* parser)
16984 cp_token *token = cp_lexer_peek_token (parser->lexer);
16986 while (token->keyword != RID_AT_END)
16988 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16990 objc_add_method_declaration
16991 (cp_parser_objc_method_signature (parser));
16992 cp_parser_consume_semicolon_at_end_of_statement (parser);
16994 else
16995 /* Allow for interspersed non-ObjC++ code. */
16996 cp_parser_objc_interstitial_code (parser);
16998 token = cp_lexer_peek_token (parser->lexer);
17001 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17002 objc_finish_interface ();
17005 /* Parse an Objective-C method definition list. */
17007 static void
17008 cp_parser_objc_method_definition_list (cp_parser* parser)
17010 cp_token *token = cp_lexer_peek_token (parser->lexer);
17012 while (token->keyword != RID_AT_END)
17014 tree meth;
17016 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17018 push_deferring_access_checks (dk_deferred);
17019 objc_start_method_definition
17020 (cp_parser_objc_method_signature (parser));
17022 /* For historical reasons, we accept an optional semicolon. */
17023 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17024 cp_lexer_consume_token (parser->lexer);
17026 perform_deferred_access_checks ();
17027 stop_deferring_access_checks ();
17028 meth = cp_parser_function_definition_after_declarator (parser,
17029 false);
17030 pop_deferring_access_checks ();
17031 objc_finish_method_definition (meth);
17033 else
17034 /* Allow for interspersed non-ObjC++ code. */
17035 cp_parser_objc_interstitial_code (parser);
17037 token = cp_lexer_peek_token (parser->lexer);
17040 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17041 objc_finish_implementation ();
17044 /* Parse Objective-C ivars. */
17046 static void
17047 cp_parser_objc_class_ivars (cp_parser* parser)
17049 cp_token *token = cp_lexer_peek_token (parser->lexer);
17051 if (token->type != CPP_OPEN_BRACE)
17052 return; /* No ivars specified. */
17054 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17055 token = cp_lexer_peek_token (parser->lexer);
17057 while (token->type != CPP_CLOSE_BRACE)
17059 cp_decl_specifier_seq declspecs;
17060 int decl_class_or_enum_p;
17061 tree prefix_attributes;
17063 cp_parser_objc_visibility_spec (parser);
17065 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17066 break;
17068 cp_parser_decl_specifier_seq (parser,
17069 CP_PARSER_FLAGS_OPTIONAL,
17070 &declspecs,
17071 &decl_class_or_enum_p);
17072 prefix_attributes = declspecs.attributes;
17073 declspecs.attributes = NULL_TREE;
17075 /* Keep going until we hit the `;' at the end of the
17076 declaration. */
17077 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17079 tree width = NULL_TREE, attributes, first_attribute, decl;
17080 cp_declarator *declarator = NULL;
17081 int ctor_dtor_or_conv_p;
17083 /* Check for a (possibly unnamed) bitfield declaration. */
17084 token = cp_lexer_peek_token (parser->lexer);
17085 if (token->type == CPP_COLON)
17086 goto eat_colon;
17088 if (token->type == CPP_NAME
17089 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17090 == CPP_COLON))
17092 /* Get the name of the bitfield. */
17093 declarator = make_id_declarator (NULL_TREE,
17094 cp_parser_identifier (parser));
17096 eat_colon:
17097 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17098 /* Get the width of the bitfield. */
17099 width
17100 = cp_parser_constant_expression (parser,
17101 /*allow_non_constant=*/false,
17102 NULL);
17104 else
17106 /* Parse the declarator. */
17107 declarator
17108 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17109 &ctor_dtor_or_conv_p,
17110 /*parenthesized_p=*/NULL,
17111 /*member_p=*/false);
17114 /* Look for attributes that apply to the ivar. */
17115 attributes = cp_parser_attributes_opt (parser);
17116 /* Remember which attributes are prefix attributes and
17117 which are not. */
17118 first_attribute = attributes;
17119 /* Combine the attributes. */
17120 attributes = chainon (prefix_attributes, attributes);
17122 if (width)
17124 /* Create the bitfield declaration. */
17125 decl = grokbitfield (declarator, &declspecs, width);
17126 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17128 else
17129 decl = grokfield (declarator, &declspecs, NULL_TREE,
17130 NULL_TREE, attributes);
17132 /* Add the instance variable. */
17133 objc_add_instance_variable (decl);
17135 /* Reset PREFIX_ATTRIBUTES. */
17136 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17137 attributes = TREE_CHAIN (attributes);
17138 if (attributes)
17139 TREE_CHAIN (attributes) = NULL_TREE;
17141 token = cp_lexer_peek_token (parser->lexer);
17143 if (token->type == CPP_COMMA)
17145 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17146 continue;
17148 break;
17151 cp_parser_consume_semicolon_at_end_of_statement (parser);
17152 token = cp_lexer_peek_token (parser->lexer);
17155 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17156 /* For historical reasons, we accept an optional semicolon. */
17157 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17158 cp_lexer_consume_token (parser->lexer);
17161 /* Parse an Objective-C protocol declaration. */
17163 static void
17164 cp_parser_objc_protocol_declaration (cp_parser* parser)
17166 tree proto, protorefs;
17167 cp_token *tok;
17169 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17170 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17172 error ("identifier expected after %<@protocol%>");
17173 goto finish;
17176 /* See if we have a forward declaration or a definition. */
17177 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17179 /* Try a forward declaration first. */
17180 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17182 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17183 finish:
17184 cp_parser_consume_semicolon_at_end_of_statement (parser);
17187 /* Ok, we got a full-fledged definition (or at least should). */
17188 else
17190 proto = cp_parser_identifier (parser);
17191 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17192 objc_start_protocol (proto, protorefs);
17193 cp_parser_objc_method_prototype_list (parser);
17197 /* Parse an Objective-C superclass or category. */
17199 static void
17200 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17201 tree *categ)
17203 cp_token *next = cp_lexer_peek_token (parser->lexer);
17205 *super = *categ = NULL_TREE;
17206 if (next->type == CPP_COLON)
17208 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17209 *super = cp_parser_identifier (parser);
17211 else if (next->type == CPP_OPEN_PAREN)
17213 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17214 *categ = cp_parser_identifier (parser);
17215 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17219 /* Parse an Objective-C class interface. */
17221 static void
17222 cp_parser_objc_class_interface (cp_parser* parser)
17224 tree name, super, categ, protos;
17226 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17227 name = cp_parser_identifier (parser);
17228 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17229 protos = cp_parser_objc_protocol_refs_opt (parser);
17231 /* We have either a class or a category on our hands. */
17232 if (categ)
17233 objc_start_category_interface (name, categ, protos);
17234 else
17236 objc_start_class_interface (name, super, protos);
17237 /* Handle instance variable declarations, if any. */
17238 cp_parser_objc_class_ivars (parser);
17239 objc_continue_interface ();
17242 cp_parser_objc_method_prototype_list (parser);
17245 /* Parse an Objective-C class implementation. */
17247 static void
17248 cp_parser_objc_class_implementation (cp_parser* parser)
17250 tree name, super, categ;
17252 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17253 name = cp_parser_identifier (parser);
17254 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17256 /* We have either a class or a category on our hands. */
17257 if (categ)
17258 objc_start_category_implementation (name, categ);
17259 else
17261 objc_start_class_implementation (name, super);
17262 /* Handle instance variable declarations, if any. */
17263 cp_parser_objc_class_ivars (parser);
17264 objc_continue_implementation ();
17267 cp_parser_objc_method_definition_list (parser);
17270 /* Consume the @end token and finish off the implementation. */
17272 static void
17273 cp_parser_objc_end_implementation (cp_parser* parser)
17275 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17276 objc_finish_implementation ();
17279 /* Parse an Objective-C declaration. */
17281 static void
17282 cp_parser_objc_declaration (cp_parser* parser)
17284 /* Try to figure out what kind of declaration is present. */
17285 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17287 switch (kwd->keyword)
17289 case RID_AT_ALIAS:
17290 cp_parser_objc_alias_declaration (parser);
17291 break;
17292 case RID_AT_CLASS:
17293 cp_parser_objc_class_declaration (parser);
17294 break;
17295 case RID_AT_PROTOCOL:
17296 cp_parser_objc_protocol_declaration (parser);
17297 break;
17298 case RID_AT_INTERFACE:
17299 cp_parser_objc_class_interface (parser);
17300 break;
17301 case RID_AT_IMPLEMENTATION:
17302 cp_parser_objc_class_implementation (parser);
17303 break;
17304 case RID_AT_END:
17305 cp_parser_objc_end_implementation (parser);
17306 break;
17307 default:
17308 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17309 cp_parser_skip_to_end_of_block_or_statement (parser);
17313 /* Parse an Objective-C try-catch-finally statement.
17315 objc-try-catch-finally-stmt:
17316 @try compound-statement objc-catch-clause-seq [opt]
17317 objc-finally-clause [opt]
17319 objc-catch-clause-seq:
17320 objc-catch-clause objc-catch-clause-seq [opt]
17322 objc-catch-clause:
17323 @catch ( exception-declaration ) compound-statement
17325 objc-finally-clause
17326 @finally compound-statement
17328 Returns NULL_TREE. */
17330 static tree
17331 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17332 location_t location;
17333 tree stmt;
17335 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17336 location = cp_lexer_peek_token (parser->lexer)->location;
17337 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17338 node, lest it get absorbed into the surrounding block. */
17339 stmt = push_stmt_list ();
17340 cp_parser_compound_statement (parser, NULL, false);
17341 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17343 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17345 cp_parameter_declarator *parmdecl;
17346 tree parm;
17348 cp_lexer_consume_token (parser->lexer);
17349 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17350 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17351 parm = grokdeclarator (parmdecl->declarator,
17352 &parmdecl->decl_specifiers,
17353 PARM, /*initialized=*/0,
17354 /*attrlist=*/NULL);
17355 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17356 objc_begin_catch_clause (parm);
17357 cp_parser_compound_statement (parser, NULL, false);
17358 objc_finish_catch_clause ();
17361 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17363 cp_lexer_consume_token (parser->lexer);
17364 location = cp_lexer_peek_token (parser->lexer)->location;
17365 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17366 node, lest it get absorbed into the surrounding block. */
17367 stmt = push_stmt_list ();
17368 cp_parser_compound_statement (parser, NULL, false);
17369 objc_build_finally_clause (location, pop_stmt_list (stmt));
17372 return objc_finish_try_stmt ();
17375 /* Parse an Objective-C synchronized statement.
17377 objc-synchronized-stmt:
17378 @synchronized ( expression ) compound-statement
17380 Returns NULL_TREE. */
17382 static tree
17383 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17384 location_t location;
17385 tree lock, stmt;
17387 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17389 location = cp_lexer_peek_token (parser->lexer)->location;
17390 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17391 lock = cp_parser_expression (parser, false);
17392 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17394 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17395 node, lest it get absorbed into the surrounding block. */
17396 stmt = push_stmt_list ();
17397 cp_parser_compound_statement (parser, NULL, false);
17399 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17402 /* Parse an Objective-C throw statement.
17404 objc-throw-stmt:
17405 @throw assignment-expression [opt] ;
17407 Returns a constructed '@throw' statement. */
17409 static tree
17410 cp_parser_objc_throw_statement (cp_parser *parser) {
17411 tree expr = NULL_TREE;
17413 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17415 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17416 expr = cp_parser_assignment_expression (parser, false);
17418 cp_parser_consume_semicolon_at_end_of_statement (parser);
17420 return objc_build_throw_stmt (expr);
17423 /* Parse an Objective-C statement. */
17425 static tree
17426 cp_parser_objc_statement (cp_parser * parser) {
17427 /* Try to figure out what kind of declaration is present. */
17428 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17430 switch (kwd->keyword)
17432 case RID_AT_TRY:
17433 return cp_parser_objc_try_catch_finally_statement (parser);
17434 case RID_AT_SYNCHRONIZED:
17435 return cp_parser_objc_synchronized_statement (parser);
17436 case RID_AT_THROW:
17437 return cp_parser_objc_throw_statement (parser);
17438 default:
17439 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17440 cp_parser_skip_to_end_of_block_or_statement (parser);
17443 return error_mark_node;
17446 /* The parser. */
17448 static GTY (()) cp_parser *the_parser;
17450 /* External interface. */
17452 /* Parse one entire translation unit. */
17454 void
17455 c_parse_file (void)
17457 bool error_occurred;
17458 static bool already_called = false;
17460 if (already_called)
17462 sorry ("inter-module optimizations not implemented for C++");
17463 return;
17465 already_called = true;
17467 the_parser = cp_parser_new ();
17468 push_deferring_access_checks (flag_access_control
17469 ? dk_no_deferred : dk_no_check);
17470 error_occurred = cp_parser_translation_unit (the_parser);
17471 the_parser = NULL;
17474 /* This variable must be provided by every front end. */
17476 int yydebug;
17478 #include "gt-cp-parser.h"