libcpp/ChangeLog:
[official-gcc.git] / gcc / cp / parser.c
blobf2acddbb255f9a4baab5d346634a1e40830fc114
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008 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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.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 token's value and its associated deferred access checks and
48 qualifying scope. */
50 struct tree_check GTY(())
52 /* The value associated with the token. */
53 tree value;
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
61 /* A C++ token. */
63 typedef struct cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
70 /* Token flags. */
71 unsigned char flags;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a system header. */
75 BOOL_BITFIELD in_system_header : 1;
76 /* True if this token is from a context where it is implicitly extern "C" */
77 BOOL_BITFIELD implicit_extern_c : 1;
78 /* True for a CPP_NAME token that is not a keyword (i.e., for which
79 KEYWORD is RID_MAX) iff this name was looked up and found to be
80 ambiguous. An error has already been reported. */
81 BOOL_BITFIELD ambiguous_p : 1;
82 /* The value associated with this token, if any. */
83 union cp_token_value {
84 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
85 struct tree_check* GTY((tag ("1"))) tree_check_value;
86 /* Use for all other tokens. */
87 tree GTY((tag ("0"))) value;
88 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 /* The location at which this token was found. */
90 location_t location;
91 } cp_token;
93 /* We use a stack of token pointer for saving token sets. */
94 typedef struct cp_token *cp_token_position;
95 DEF_VEC_P (cp_token_position);
96 DEF_VEC_ALLOC_P (cp_token_position,heap);
98 static cp_token eof_token =
100 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, false, 0, { NULL },
104 /* The cp_lexer structure represents the C++ lexer. It is responsible
105 for managing the token stream from the preprocessor and supplying
106 it to the parser. Tokens are never added to the cp_lexer after
107 it is created. */
109 typedef struct cp_lexer GTY (())
111 /* The memory allocated for the buffer. NULL if this lexer does not
112 own the token buffer. */
113 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
114 /* If the lexer owns the buffer, this is the number of tokens in the
115 buffer. */
116 size_t buffer_length;
118 /* A pointer just past the last available token. The tokens
119 in this lexer are [buffer, last_token). */
120 cp_token_position GTY ((skip)) last_token;
122 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
123 no more available tokens. */
124 cp_token_position GTY ((skip)) next_token;
126 /* A stack indicating positions at which cp_lexer_save_tokens was
127 called. The top entry is the most recent position at which we
128 began saving tokens. If the stack is non-empty, we are saving
129 tokens. */
130 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
132 /* The next lexer in a linked list of lexers. */
133 struct cp_lexer *next;
135 /* True if we should output debugging information. */
136 bool debugging_p;
138 /* True if we're in the context of parsing a pragma, and should not
139 increment past the end-of-line marker. */
140 bool in_pragma;
141 } cp_lexer;
143 /* cp_token_cache is a range of tokens. There is no need to represent
144 allocate heap memory for it, since tokens are never removed from the
145 lexer's array. There is also no need for the GC to walk through
146 a cp_token_cache, since everything in here is referenced through
147 a lexer. */
149 typedef struct cp_token_cache GTY(())
151 /* The beginning of the token range. */
152 cp_token * GTY((skip)) first;
154 /* Points immediately after the last token in the range. */
155 cp_token * GTY ((skip)) last;
156 } cp_token_cache;
158 /* Prototypes. */
160 static cp_lexer *cp_lexer_new_main
161 (void);
162 static cp_lexer *cp_lexer_new_from_tokens
163 (cp_token_cache *tokens);
164 static void cp_lexer_destroy
165 (cp_lexer *);
166 static int cp_lexer_saving_tokens
167 (const cp_lexer *);
168 static cp_token_position cp_lexer_token_position
169 (cp_lexer *, bool);
170 static cp_token *cp_lexer_token_at
171 (cp_lexer *, cp_token_position);
172 static void cp_lexer_get_preprocessor_token
173 (cp_lexer *, cp_token *);
174 static inline cp_token *cp_lexer_peek_token
175 (cp_lexer *);
176 static cp_token *cp_lexer_peek_nth_token
177 (cp_lexer *, size_t);
178 static inline bool cp_lexer_next_token_is
179 (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_not
181 (cp_lexer *, enum cpp_ttype);
182 static bool cp_lexer_next_token_is_keyword
183 (cp_lexer *, enum rid);
184 static cp_token *cp_lexer_consume_token
185 (cp_lexer *);
186 static void cp_lexer_purge_token
187 (cp_lexer *);
188 static void cp_lexer_purge_tokens_after
189 (cp_lexer *, cp_token_position);
190 static void cp_lexer_save_tokens
191 (cp_lexer *);
192 static void cp_lexer_commit_tokens
193 (cp_lexer *);
194 static void cp_lexer_rollback_tokens
195 (cp_lexer *);
196 #ifdef ENABLE_CHECKING
197 static void cp_lexer_print_token
198 (FILE *, cp_token *);
199 static inline bool cp_lexer_debugging_p
200 (cp_lexer *);
201 static void cp_lexer_start_debugging
202 (cp_lexer *) ATTRIBUTE_UNUSED;
203 static void cp_lexer_stop_debugging
204 (cp_lexer *) ATTRIBUTE_UNUSED;
205 #else
206 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
207 about passing NULL to functions that require non-NULL arguments
208 (fputs, fprintf). It will never be used, so all we need is a value
209 of the right type that's guaranteed not to be NULL. */
210 #define cp_lexer_debug_stream stdout
211 #define cp_lexer_print_token(str, tok) (void) 0
212 #define cp_lexer_debugging_p(lexer) 0
213 #endif /* ENABLE_CHECKING */
215 static cp_token_cache *cp_token_cache_new
216 (cp_token *, cp_token *);
218 static void cp_parser_initial_pragma
219 (cp_token *);
221 /* Manifest constants. */
222 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
223 #define CP_SAVED_TOKEN_STACK 5
225 /* A token type for keywords, as opposed to ordinary identifiers. */
226 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
228 /* A token type for template-ids. If a template-id is processed while
229 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
230 the value of the CPP_TEMPLATE_ID is whatever was returned by
231 cp_parser_template_id. */
232 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
234 /* A token type for nested-name-specifiers. If a
235 nested-name-specifier is processed while parsing tentatively, it is
236 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
237 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
238 cp_parser_nested_name_specifier_opt. */
239 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
241 /* A token type for tokens that are not tokens at all; these are used
242 to represent slots in the array where there used to be a token
243 that has now been deleted. */
244 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
246 /* The number of token types, including C++-specific ones. */
247 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
249 /* Variables. */
251 #ifdef ENABLE_CHECKING
252 /* The stream to which debugging output should be written. */
253 static FILE *cp_lexer_debug_stream;
254 #endif /* ENABLE_CHECKING */
256 /* Create a new main C++ lexer, the lexer that gets tokens from the
257 preprocessor. */
259 static cp_lexer *
260 cp_lexer_new_main (void)
262 cp_token first_token;
263 cp_lexer *lexer;
264 cp_token *pos;
265 size_t alloc;
266 size_t space;
267 cp_token *buffer;
269 /* It's possible that parsing the first pragma will load a PCH file,
270 which is a GC collection point. So we have to do that before
271 allocating any memory. */
272 cp_parser_initial_pragma (&first_token);
274 c_common_no_more_pch ();
276 /* Allocate the memory. */
277 lexer = GGC_CNEW (cp_lexer);
279 #ifdef ENABLE_CHECKING
280 /* Initially we are not debugging. */
281 lexer->debugging_p = false;
282 #endif /* ENABLE_CHECKING */
283 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
284 CP_SAVED_TOKEN_STACK);
286 /* Create the buffer. */
287 alloc = CP_LEXER_BUFFER_SIZE;
288 buffer = GGC_NEWVEC (cp_token, alloc);
290 /* Put the first token in the buffer. */
291 space = alloc;
292 pos = buffer;
293 *pos = first_token;
295 /* Get the remaining tokens from the preprocessor. */
296 while (pos->type != CPP_EOF)
298 pos++;
299 if (!--space)
301 space = alloc;
302 alloc *= 2;
303 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
304 pos = buffer + space;
306 cp_lexer_get_preprocessor_token (lexer, pos);
308 lexer->buffer = buffer;
309 lexer->buffer_length = alloc - space;
310 lexer->last_token = pos;
311 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
313 /* Subsequent preprocessor diagnostics should use compiler
314 diagnostic functions to get the compiler source location. */
315 cpp_get_options (parse_in)->client_diagnostic = true;
316 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
318 gcc_assert (lexer->next_token->type != CPP_PURGED);
319 return lexer;
322 /* Create a new lexer whose token stream is primed with the tokens in
323 CACHE. When these tokens are exhausted, no new tokens will be read. */
325 static cp_lexer *
326 cp_lexer_new_from_tokens (cp_token_cache *cache)
328 cp_token *first = cache->first;
329 cp_token *last = cache->last;
330 cp_lexer *lexer = GGC_CNEW (cp_lexer);
332 /* We do not own the buffer. */
333 lexer->buffer = NULL;
334 lexer->buffer_length = 0;
335 lexer->next_token = first == last ? &eof_token : first;
336 lexer->last_token = last;
338 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
339 CP_SAVED_TOKEN_STACK);
341 #ifdef ENABLE_CHECKING
342 /* Initially we are not debugging. */
343 lexer->debugging_p = false;
344 #endif
346 gcc_assert (lexer->next_token->type != CPP_PURGED);
347 return lexer;
350 /* Frees all resources associated with LEXER. */
352 static void
353 cp_lexer_destroy (cp_lexer *lexer)
355 if (lexer->buffer)
356 ggc_free (lexer->buffer);
357 VEC_free (cp_token_position, heap, lexer->saved_tokens);
358 ggc_free (lexer);
361 /* Returns nonzero if debugging information should be output. */
363 #ifdef ENABLE_CHECKING
365 static inline bool
366 cp_lexer_debugging_p (cp_lexer *lexer)
368 return lexer->debugging_p;
371 #endif /* ENABLE_CHECKING */
373 static inline cp_token_position
374 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
376 gcc_assert (!previous_p || lexer->next_token != &eof_token);
378 return lexer->next_token - previous_p;
381 static inline cp_token *
382 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
384 return pos;
387 /* nonzero if we are presently saving tokens. */
389 static inline int
390 cp_lexer_saving_tokens (const cp_lexer* lexer)
392 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
395 /* Store the next token from the preprocessor in *TOKEN. Return true
396 if we reach EOF. If LEXER is NULL, assume we are handling an
397 initial #pragma pch_preprocess, and thus want the lexer to return
398 processed strings. */
400 static void
401 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
403 static int is_extern_c = 0;
405 /* Get a new token from the preprocessor. */
406 token->type
407 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
408 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
409 token->keyword = RID_MAX;
410 token->pragma_kind = PRAGMA_NONE;
411 token->in_system_header = in_system_header;
413 /* On some systems, some header files are surrounded by an
414 implicit extern "C" block. Set a flag in the token if it
415 comes from such a header. */
416 is_extern_c += pending_lang_change;
417 pending_lang_change = 0;
418 token->implicit_extern_c = is_extern_c > 0;
420 /* Check to see if this token is a keyword. */
421 if (token->type == CPP_NAME)
423 if (C_IS_RESERVED_WORD (token->u.value))
425 /* Mark this token as a keyword. */
426 token->type = CPP_KEYWORD;
427 /* Record which keyword. */
428 token->keyword = C_RID_CODE (token->u.value);
429 /* Update the value. Some keywords are mapped to particular
430 entities, rather than simply having the value of the
431 corresponding IDENTIFIER_NODE. For example, `__const' is
432 mapped to `const'. */
433 token->u.value = ridpointers[token->keyword];
435 else
437 if (warn_cxx0x_compat
438 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
439 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
441 /* Warn about the C++0x keyword (but still treat it as
442 an identifier). */
443 warning (OPT_Wc__0x_compat,
444 "identifier %<%s%> will become a keyword in C++0x",
445 IDENTIFIER_POINTER (token->u.value));
447 /* Clear out the C_RID_CODE so we don't warn about this
448 particular identifier-turned-keyword again. */
449 C_RID_CODE (token->u.value) = RID_MAX;
452 token->ambiguous_p = false;
453 token->keyword = RID_MAX;
456 /* Handle Objective-C++ keywords. */
457 else if (token->type == CPP_AT_NAME)
459 token->type = CPP_KEYWORD;
460 switch (C_RID_CODE (token->u.value))
462 /* Map 'class' to '@class', 'private' to '@private', etc. */
463 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
464 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
465 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
466 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
467 case RID_THROW: token->keyword = RID_AT_THROW; break;
468 case RID_TRY: token->keyword = RID_AT_TRY; break;
469 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
470 default: token->keyword = C_RID_CODE (token->u.value);
473 else if (token->type == CPP_PRAGMA)
475 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
476 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
477 token->u.value = NULL_TREE;
481 /* Update the globals input_location and in_system_header and the
482 input file stack from TOKEN. */
483 static inline void
484 cp_lexer_set_source_position_from_token (cp_token *token)
486 if (token->type != CPP_EOF)
488 input_location = token->location;
489 in_system_header = token->in_system_header;
493 /* Return a pointer to the next token in the token stream, but do not
494 consume it. */
496 static inline cp_token *
497 cp_lexer_peek_token (cp_lexer *lexer)
499 if (cp_lexer_debugging_p (lexer))
501 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
502 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
503 putc ('\n', cp_lexer_debug_stream);
505 return lexer->next_token;
508 /* Return true if the next token has the indicated TYPE. */
510 static inline bool
511 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
513 return cp_lexer_peek_token (lexer)->type == type;
516 /* Return true if the next token does not have the indicated TYPE. */
518 static inline bool
519 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
521 return !cp_lexer_next_token_is (lexer, type);
524 /* Return true if the next token is the indicated KEYWORD. */
526 static inline bool
527 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
529 return cp_lexer_peek_token (lexer)->keyword == keyword;
532 /* Return true if the next token is a keyword for a decl-specifier. */
534 static bool
535 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
537 cp_token *token;
539 token = cp_lexer_peek_token (lexer);
540 switch (token->keyword)
542 /* auto specifier: storage-class-specifier in C++,
543 simple-type-specifier in C++0x. */
544 case RID_AUTO:
545 /* Storage classes. */
546 case RID_REGISTER:
547 case RID_STATIC:
548 case RID_EXTERN:
549 case RID_MUTABLE:
550 case RID_THREAD:
551 /* Elaborated type specifiers. */
552 case RID_ENUM:
553 case RID_CLASS:
554 case RID_STRUCT:
555 case RID_UNION:
556 case RID_TYPENAME:
557 /* Simple type specifiers. */
558 case RID_CHAR:
559 case RID_CHAR16:
560 case RID_CHAR32:
561 case RID_WCHAR:
562 case RID_BOOL:
563 case RID_SHORT:
564 case RID_INT:
565 case RID_LONG:
566 case RID_SIGNED:
567 case RID_UNSIGNED:
568 case RID_FLOAT:
569 case RID_DOUBLE:
570 case RID_VOID:
571 /* GNU extensions. */
572 case RID_ATTRIBUTE:
573 case RID_TYPEOF:
574 /* C++0x extensions. */
575 case RID_DECLTYPE:
576 return true;
578 default:
579 return false;
583 /* Return a pointer to the Nth token in the token stream. If N is 1,
584 then this is precisely equivalent to cp_lexer_peek_token (except
585 that it is not inline). One would like to disallow that case, but
586 there is one case (cp_parser_nth_token_starts_template_id) where
587 the caller passes a variable for N and it might be 1. */
589 static cp_token *
590 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
592 cp_token *token;
594 /* N is 1-based, not zero-based. */
595 gcc_assert (n > 0);
597 if (cp_lexer_debugging_p (lexer))
598 fprintf (cp_lexer_debug_stream,
599 "cp_lexer: peeking ahead %ld at token: ", (long)n);
601 --n;
602 token = lexer->next_token;
603 gcc_assert (!n || token != &eof_token);
604 while (n != 0)
606 ++token;
607 if (token == lexer->last_token)
609 token = &eof_token;
610 break;
613 if (token->type != CPP_PURGED)
614 --n;
617 if (cp_lexer_debugging_p (lexer))
619 cp_lexer_print_token (cp_lexer_debug_stream, token);
620 putc ('\n', cp_lexer_debug_stream);
623 return token;
626 /* Return the next token, and advance the lexer's next_token pointer
627 to point to the next non-purged token. */
629 static cp_token *
630 cp_lexer_consume_token (cp_lexer* lexer)
632 cp_token *token = lexer->next_token;
634 gcc_assert (token != &eof_token);
635 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
639 lexer->next_token++;
640 if (lexer->next_token == lexer->last_token)
642 lexer->next_token = &eof_token;
643 break;
647 while (lexer->next_token->type == CPP_PURGED);
649 cp_lexer_set_source_position_from_token (token);
651 /* Provide debugging output. */
652 if (cp_lexer_debugging_p (lexer))
654 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
655 cp_lexer_print_token (cp_lexer_debug_stream, token);
656 putc ('\n', cp_lexer_debug_stream);
659 return token;
662 /* Permanently remove the next token from the token stream, and
663 advance the next_token pointer to refer to the next non-purged
664 token. */
666 static void
667 cp_lexer_purge_token (cp_lexer *lexer)
669 cp_token *tok = lexer->next_token;
671 gcc_assert (tok != &eof_token);
672 tok->type = CPP_PURGED;
673 tok->location = UNKNOWN_LOCATION;
674 tok->u.value = NULL_TREE;
675 tok->keyword = RID_MAX;
679 tok++;
680 if (tok == lexer->last_token)
682 tok = &eof_token;
683 break;
686 while (tok->type == CPP_PURGED);
687 lexer->next_token = tok;
690 /* Permanently remove all tokens after TOK, up to, but not
691 including, the token that will be returned next by
692 cp_lexer_peek_token. */
694 static void
695 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
697 cp_token *peek = lexer->next_token;
699 if (peek == &eof_token)
700 peek = lexer->last_token;
702 gcc_assert (tok < peek);
704 for ( tok += 1; tok != peek; tok += 1)
706 tok->type = CPP_PURGED;
707 tok->location = UNKNOWN_LOCATION;
708 tok->u.value = NULL_TREE;
709 tok->keyword = RID_MAX;
713 /* Begin saving tokens. All tokens consumed after this point will be
714 preserved. */
716 static void
717 cp_lexer_save_tokens (cp_lexer* lexer)
719 /* Provide debugging output. */
720 if (cp_lexer_debugging_p (lexer))
721 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
723 VEC_safe_push (cp_token_position, heap,
724 lexer->saved_tokens, lexer->next_token);
727 /* Commit to the portion of the token stream most recently saved. */
729 static void
730 cp_lexer_commit_tokens (cp_lexer* lexer)
732 /* Provide debugging output. */
733 if (cp_lexer_debugging_p (lexer))
734 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
736 VEC_pop (cp_token_position, lexer->saved_tokens);
739 /* Return all tokens saved since the last call to cp_lexer_save_tokens
740 to the token stream. Stop saving tokens. */
742 static void
743 cp_lexer_rollback_tokens (cp_lexer* lexer)
745 /* Provide debugging output. */
746 if (cp_lexer_debugging_p (lexer))
747 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
749 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
752 /* Print a representation of the TOKEN on the STREAM. */
754 #ifdef ENABLE_CHECKING
756 static void
757 cp_lexer_print_token (FILE * stream, cp_token *token)
759 /* We don't use cpp_type2name here because the parser defines
760 a few tokens of its own. */
761 static const char *const token_names[] = {
762 /* cpplib-defined token types */
763 #define OP(e, s) #e,
764 #define TK(e, s) #e,
765 TTYPE_TABLE
766 #undef OP
767 #undef TK
768 /* C++ parser token types - see "Manifest constants", above. */
769 "KEYWORD",
770 "TEMPLATE_ID",
771 "NESTED_NAME_SPECIFIER",
772 "PURGED"
775 /* If we have a name for the token, print it out. Otherwise, we
776 simply give the numeric code. */
777 gcc_assert (token->type < ARRAY_SIZE(token_names));
778 fputs (token_names[token->type], stream);
780 /* For some tokens, print the associated data. */
781 switch (token->type)
783 case CPP_KEYWORD:
784 /* Some keywords have a value that is not an IDENTIFIER_NODE.
785 For example, `struct' is mapped to an INTEGER_CST. */
786 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
787 break;
788 /* else fall through */
789 case CPP_NAME:
790 fputs (IDENTIFIER_POINTER (token->u.value), stream);
791 break;
793 case CPP_STRING:
794 case CPP_STRING16:
795 case CPP_STRING32:
796 case CPP_WSTRING:
797 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
798 break;
800 default:
801 break;
805 /* Start emitting debugging information. */
807 static void
808 cp_lexer_start_debugging (cp_lexer* lexer)
810 lexer->debugging_p = true;
813 /* Stop emitting debugging information. */
815 static void
816 cp_lexer_stop_debugging (cp_lexer* lexer)
818 lexer->debugging_p = false;
821 #endif /* ENABLE_CHECKING */
823 /* Create a new cp_token_cache, representing a range of tokens. */
825 static cp_token_cache *
826 cp_token_cache_new (cp_token *first, cp_token *last)
828 cp_token_cache *cache = GGC_NEW (cp_token_cache);
829 cache->first = first;
830 cache->last = last;
831 return cache;
835 /* Decl-specifiers. */
837 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
839 static void
840 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
842 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
845 /* Declarators. */
847 /* Nothing other than the parser should be creating declarators;
848 declarators are a semi-syntactic representation of C++ entities.
849 Other parts of the front end that need to create entities (like
850 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
852 static cp_declarator *make_call_declarator
853 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
854 static cp_declarator *make_array_declarator
855 (cp_declarator *, tree);
856 static cp_declarator *make_pointer_declarator
857 (cp_cv_quals, cp_declarator *);
858 static cp_declarator *make_reference_declarator
859 (cp_cv_quals, cp_declarator *, bool);
860 static cp_parameter_declarator *make_parameter_declarator
861 (cp_decl_specifier_seq *, cp_declarator *, tree);
862 static cp_declarator *make_ptrmem_declarator
863 (cp_cv_quals, tree, cp_declarator *);
865 /* An erroneous declarator. */
866 static cp_declarator *cp_error_declarator;
868 /* The obstack on which declarators and related data structures are
869 allocated. */
870 static struct obstack declarator_obstack;
872 /* Alloc BYTES from the declarator memory pool. */
874 static inline void *
875 alloc_declarator (size_t bytes)
877 return obstack_alloc (&declarator_obstack, bytes);
880 /* Allocate a declarator of the indicated KIND. Clear fields that are
881 common to all declarators. */
883 static cp_declarator *
884 make_declarator (cp_declarator_kind kind)
886 cp_declarator *declarator;
888 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
889 declarator->kind = kind;
890 declarator->attributes = NULL_TREE;
891 declarator->declarator = NULL;
892 declarator->parameter_pack_p = false;
894 return declarator;
897 /* Make a declarator for a generalized identifier. If
898 QUALIFYING_SCOPE is non-NULL, the identifier is
899 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
900 UNQUALIFIED_NAME. SFK indicates the kind of special function this
901 is, if any. */
903 static cp_declarator *
904 make_id_declarator (tree qualifying_scope, tree unqualified_name,
905 special_function_kind sfk)
907 cp_declarator *declarator;
909 /* It is valid to write:
911 class C { void f(); };
912 typedef C D;
913 void D::f();
915 The standard is not clear about whether `typedef const C D' is
916 legal; as of 2002-09-15 the committee is considering that
917 question. EDG 3.0 allows that syntax. Therefore, we do as
918 well. */
919 if (qualifying_scope && TYPE_P (qualifying_scope))
920 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
922 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
923 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
924 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
926 declarator = make_declarator (cdk_id);
927 declarator->u.id.qualifying_scope = qualifying_scope;
928 declarator->u.id.unqualified_name = unqualified_name;
929 declarator->u.id.sfk = sfk;
931 return declarator;
934 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
935 of modifiers such as const or volatile to apply to the pointer
936 type, represented as identifiers. */
938 cp_declarator *
939 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941 cp_declarator *declarator;
943 declarator = make_declarator (cdk_pointer);
944 declarator->declarator = target;
945 declarator->u.pointer.qualifiers = cv_qualifiers;
946 declarator->u.pointer.class_type = NULL_TREE;
947 if (target)
949 declarator->parameter_pack_p = target->parameter_pack_p;
950 target->parameter_pack_p = false;
952 else
953 declarator->parameter_pack_p = false;
955 return declarator;
958 /* Like make_pointer_declarator -- but for references. */
960 cp_declarator *
961 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
962 bool rvalue_ref)
964 cp_declarator *declarator;
966 declarator = make_declarator (cdk_reference);
967 declarator->declarator = target;
968 declarator->u.reference.qualifiers = cv_qualifiers;
969 declarator->u.reference.rvalue_ref = rvalue_ref;
970 if (target)
972 declarator->parameter_pack_p = target->parameter_pack_p;
973 target->parameter_pack_p = false;
975 else
976 declarator->parameter_pack_p = false;
978 return declarator;
981 /* Like make_pointer_declarator -- but for a pointer to a non-static
982 member of CLASS_TYPE. */
984 cp_declarator *
985 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
986 cp_declarator *pointee)
988 cp_declarator *declarator;
990 declarator = make_declarator (cdk_ptrmem);
991 declarator->declarator = pointee;
992 declarator->u.pointer.qualifiers = cv_qualifiers;
993 declarator->u.pointer.class_type = class_type;
995 if (pointee)
997 declarator->parameter_pack_p = pointee->parameter_pack_p;
998 pointee->parameter_pack_p = false;
1000 else
1001 declarator->parameter_pack_p = false;
1003 return declarator;
1006 /* Make a declarator for the function given by TARGET, with the
1007 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1008 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1009 indicates what exceptions can be thrown. */
1011 cp_declarator *
1012 make_call_declarator (cp_declarator *target,
1013 cp_parameter_declarator *parms,
1014 cp_cv_quals cv_qualifiers,
1015 tree exception_specification)
1017 cp_declarator *declarator;
1019 declarator = make_declarator (cdk_function);
1020 declarator->declarator = target;
1021 declarator->u.function.parameters = parms;
1022 declarator->u.function.qualifiers = cv_qualifiers;
1023 declarator->u.function.exception_specification = exception_specification;
1024 if (target)
1026 declarator->parameter_pack_p = target->parameter_pack_p;
1027 target->parameter_pack_p = false;
1029 else
1030 declarator->parameter_pack_p = false;
1032 return declarator;
1035 /* Make a declarator for an array of BOUNDS elements, each of which is
1036 defined by ELEMENT. */
1038 cp_declarator *
1039 make_array_declarator (cp_declarator *element, tree bounds)
1041 cp_declarator *declarator;
1043 declarator = make_declarator (cdk_array);
1044 declarator->declarator = element;
1045 declarator->u.array.bounds = bounds;
1046 if (element)
1048 declarator->parameter_pack_p = element->parameter_pack_p;
1049 element->parameter_pack_p = false;
1051 else
1052 declarator->parameter_pack_p = false;
1054 return declarator;
1057 /* Determine whether the declarator we've seen so far can be a
1058 parameter pack, when followed by an ellipsis. */
1059 static bool
1060 declarator_can_be_parameter_pack (cp_declarator *declarator)
1062 /* Search for a declarator name, or any other declarator that goes
1063 after the point where the ellipsis could appear in a parameter
1064 pack. If we find any of these, then this declarator can not be
1065 made into a parameter pack. */
1066 bool found = false;
1067 while (declarator && !found)
1069 switch ((int)declarator->kind)
1071 case cdk_id:
1072 case cdk_array:
1073 found = true;
1074 break;
1076 case cdk_error:
1077 return true;
1079 default:
1080 declarator = declarator->declarator;
1081 break;
1085 return !found;
1088 cp_parameter_declarator *no_parameters;
1090 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1091 DECLARATOR and DEFAULT_ARGUMENT. */
1093 cp_parameter_declarator *
1094 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1095 cp_declarator *declarator,
1096 tree default_argument)
1098 cp_parameter_declarator *parameter;
1100 parameter = ((cp_parameter_declarator *)
1101 alloc_declarator (sizeof (cp_parameter_declarator)));
1102 parameter->next = NULL;
1103 if (decl_specifiers)
1104 parameter->decl_specifiers = *decl_specifiers;
1105 else
1106 clear_decl_specs (&parameter->decl_specifiers);
1107 parameter->declarator = declarator;
1108 parameter->default_argument = default_argument;
1109 parameter->ellipsis_p = false;
1111 return parameter;
1114 /* Returns true iff DECLARATOR is a declaration for a function. */
1116 static bool
1117 function_declarator_p (const cp_declarator *declarator)
1119 while (declarator)
1121 if (declarator->kind == cdk_function
1122 && declarator->declarator->kind == cdk_id)
1123 return true;
1124 if (declarator->kind == cdk_id
1125 || declarator->kind == cdk_error)
1126 return false;
1127 declarator = declarator->declarator;
1129 return false;
1132 /* The parser. */
1134 /* Overview
1135 --------
1137 A cp_parser parses the token stream as specified by the C++
1138 grammar. Its job is purely parsing, not semantic analysis. For
1139 example, the parser breaks the token stream into declarators,
1140 expressions, statements, and other similar syntactic constructs.
1141 It does not check that the types of the expressions on either side
1142 of an assignment-statement are compatible, or that a function is
1143 not declared with a parameter of type `void'.
1145 The parser invokes routines elsewhere in the compiler to perform
1146 semantic analysis and to build up the abstract syntax tree for the
1147 code processed.
1149 The parser (and the template instantiation code, which is, in a
1150 way, a close relative of parsing) are the only parts of the
1151 compiler that should be calling push_scope and pop_scope, or
1152 related functions. The parser (and template instantiation code)
1153 keeps track of what scope is presently active; everything else
1154 should simply honor that. (The code that generates static
1155 initializers may also need to set the scope, in order to check
1156 access control correctly when emitting the initializers.)
1158 Methodology
1159 -----------
1161 The parser is of the standard recursive-descent variety. Upcoming
1162 tokens in the token stream are examined in order to determine which
1163 production to use when parsing a non-terminal. Some C++ constructs
1164 require arbitrary look ahead to disambiguate. For example, it is
1165 impossible, in the general case, to tell whether a statement is an
1166 expression or declaration without scanning the entire statement.
1167 Therefore, the parser is capable of "parsing tentatively." When the
1168 parser is not sure what construct comes next, it enters this mode.
1169 Then, while we attempt to parse the construct, the parser queues up
1170 error messages, rather than issuing them immediately, and saves the
1171 tokens it consumes. If the construct is parsed successfully, the
1172 parser "commits", i.e., it issues any queued error messages and
1173 the tokens that were being preserved are permanently discarded.
1174 If, however, the construct is not parsed successfully, the parser
1175 rolls back its state completely so that it can resume parsing using
1176 a different alternative.
1178 Future Improvements
1179 -------------------
1181 The performance of the parser could probably be improved substantially.
1182 We could often eliminate the need to parse tentatively by looking ahead
1183 a little bit. In some places, this approach might not entirely eliminate
1184 the need to parse tentatively, but it might still speed up the average
1185 case. */
1187 /* Flags that are passed to some parsing functions. These values can
1188 be bitwise-ored together. */
1190 typedef enum cp_parser_flags
1192 /* No flags. */
1193 CP_PARSER_FLAGS_NONE = 0x0,
1194 /* The construct is optional. If it is not present, then no error
1195 should be issued. */
1196 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1197 /* When parsing a type-specifier, do not allow user-defined types. */
1198 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1199 } cp_parser_flags;
1201 /* The different kinds of declarators we want to parse. */
1203 typedef enum cp_parser_declarator_kind
1205 /* We want an abstract declarator. */
1206 CP_PARSER_DECLARATOR_ABSTRACT,
1207 /* We want a named declarator. */
1208 CP_PARSER_DECLARATOR_NAMED,
1209 /* We don't mind, but the name must be an unqualified-id. */
1210 CP_PARSER_DECLARATOR_EITHER
1211 } cp_parser_declarator_kind;
1213 /* The precedence values used to parse binary expressions. The minimum value
1214 of PREC must be 1, because zero is reserved to quickly discriminate
1215 binary operators from other tokens. */
1217 enum cp_parser_prec
1219 PREC_NOT_OPERATOR,
1220 PREC_LOGICAL_OR_EXPRESSION,
1221 PREC_LOGICAL_AND_EXPRESSION,
1222 PREC_INCLUSIVE_OR_EXPRESSION,
1223 PREC_EXCLUSIVE_OR_EXPRESSION,
1224 PREC_AND_EXPRESSION,
1225 PREC_EQUALITY_EXPRESSION,
1226 PREC_RELATIONAL_EXPRESSION,
1227 PREC_SHIFT_EXPRESSION,
1228 PREC_ADDITIVE_EXPRESSION,
1229 PREC_MULTIPLICATIVE_EXPRESSION,
1230 PREC_PM_EXPRESSION,
1231 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1234 /* A mapping from a token type to a corresponding tree node type, with a
1235 precedence value. */
1237 typedef struct cp_parser_binary_operations_map_node
1239 /* The token type. */
1240 enum cpp_ttype token_type;
1241 /* The corresponding tree code. */
1242 enum tree_code tree_type;
1243 /* The precedence of this operator. */
1244 enum cp_parser_prec prec;
1245 } cp_parser_binary_operations_map_node;
1247 /* The status of a tentative parse. */
1249 typedef enum cp_parser_status_kind
1251 /* No errors have occurred. */
1252 CP_PARSER_STATUS_KIND_NO_ERROR,
1253 /* An error has occurred. */
1254 CP_PARSER_STATUS_KIND_ERROR,
1255 /* We are committed to this tentative parse, whether or not an error
1256 has occurred. */
1257 CP_PARSER_STATUS_KIND_COMMITTED
1258 } cp_parser_status_kind;
1260 typedef struct cp_parser_expression_stack_entry
1262 /* Left hand side of the binary operation we are currently
1263 parsing. */
1264 tree lhs;
1265 /* Original tree code for left hand side, if it was a binary
1266 expression itself (used for -Wparentheses). */
1267 enum tree_code lhs_type;
1268 /* Tree code for the binary operation we are parsing. */
1269 enum tree_code tree_type;
1270 /* Precedence of the binary operation we are parsing. */
1271 int prec;
1272 } cp_parser_expression_stack_entry;
1274 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1275 entries because precedence levels on the stack are monotonically
1276 increasing. */
1277 typedef struct cp_parser_expression_stack_entry
1278 cp_parser_expression_stack[NUM_PREC_VALUES];
1280 /* Context that is saved and restored when parsing tentatively. */
1281 typedef struct cp_parser_context GTY (())
1283 /* If this is a tentative parsing context, the status of the
1284 tentative parse. */
1285 enum cp_parser_status_kind status;
1286 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1287 that are looked up in this context must be looked up both in the
1288 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1289 the context of the containing expression. */
1290 tree object_type;
1292 /* The next parsing context in the stack. */
1293 struct cp_parser_context *next;
1294 } cp_parser_context;
1296 /* Prototypes. */
1298 /* Constructors and destructors. */
1300 static cp_parser_context *cp_parser_context_new
1301 (cp_parser_context *);
1303 /* Class variables. */
1305 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1307 /* The operator-precedence table used by cp_parser_binary_expression.
1308 Transformed into an associative array (binops_by_token) by
1309 cp_parser_new. */
1311 static const cp_parser_binary_operations_map_node binops[] = {
1312 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1313 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1315 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1316 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1317 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1320 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1322 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1323 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1325 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1326 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1327 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1328 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1330 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1331 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1333 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1335 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1337 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1339 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1341 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1344 /* The same as binops, but initialized by cp_parser_new so that
1345 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1346 for speed. */
1347 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1349 /* Constructors and destructors. */
1351 /* Construct a new context. The context below this one on the stack
1352 is given by NEXT. */
1354 static cp_parser_context *
1355 cp_parser_context_new (cp_parser_context* next)
1357 cp_parser_context *context;
1359 /* Allocate the storage. */
1360 if (cp_parser_context_free_list != NULL)
1362 /* Pull the first entry from the free list. */
1363 context = cp_parser_context_free_list;
1364 cp_parser_context_free_list = context->next;
1365 memset (context, 0, sizeof (*context));
1367 else
1368 context = GGC_CNEW (cp_parser_context);
1370 /* No errors have occurred yet in this context. */
1371 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1372 /* If this is not the bottomost context, copy information that we
1373 need from the previous context. */
1374 if (next)
1376 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1377 expression, then we are parsing one in this context, too. */
1378 context->object_type = next->object_type;
1379 /* Thread the stack. */
1380 context->next = next;
1383 return context;
1386 /* The cp_parser structure represents the C++ parser. */
1388 typedef struct cp_parser GTY(())
1390 /* The lexer from which we are obtaining tokens. */
1391 cp_lexer *lexer;
1393 /* The scope in which names should be looked up. If NULL_TREE, then
1394 we look up names in the scope that is currently open in the
1395 source program. If non-NULL, this is either a TYPE or
1396 NAMESPACE_DECL for the scope in which we should look. It can
1397 also be ERROR_MARK, when we've parsed a bogus scope.
1399 This value is not cleared automatically after a name is looked
1400 up, so we must be careful to clear it before starting a new look
1401 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1402 will look up `Z' in the scope of `X', rather than the current
1403 scope.) Unfortunately, it is difficult to tell when name lookup
1404 is complete, because we sometimes peek at a token, look it up,
1405 and then decide not to consume it. */
1406 tree scope;
1408 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1409 last lookup took place. OBJECT_SCOPE is used if an expression
1410 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1411 respectively. QUALIFYING_SCOPE is used for an expression of the
1412 form "X::Y"; it refers to X. */
1413 tree object_scope;
1414 tree qualifying_scope;
1416 /* A stack of parsing contexts. All but the bottom entry on the
1417 stack will be tentative contexts.
1419 We parse tentatively in order to determine which construct is in
1420 use in some situations. For example, in order to determine
1421 whether a statement is an expression-statement or a
1422 declaration-statement we parse it tentatively as a
1423 declaration-statement. If that fails, we then reparse the same
1424 token stream as an expression-statement. */
1425 cp_parser_context *context;
1427 /* True if we are parsing GNU C++. If this flag is not set, then
1428 GNU extensions are not recognized. */
1429 bool allow_gnu_extensions_p;
1431 /* TRUE if the `>' token should be interpreted as the greater-than
1432 operator. FALSE if it is the end of a template-id or
1433 template-parameter-list. In C++0x mode, this flag also applies to
1434 `>>' tokens, which are viewed as two consecutive `>' tokens when
1435 this flag is FALSE. */
1436 bool greater_than_is_operator_p;
1438 /* TRUE if default arguments are allowed within a parameter list
1439 that starts at this point. FALSE if only a gnu extension makes
1440 them permissible. */
1441 bool default_arg_ok_p;
1443 /* TRUE if we are parsing an integral constant-expression. See
1444 [expr.const] for a precise definition. */
1445 bool integral_constant_expression_p;
1447 /* TRUE if we are parsing an integral constant-expression -- but a
1448 non-constant expression should be permitted as well. This flag
1449 is used when parsing an array bound so that GNU variable-length
1450 arrays are tolerated. */
1451 bool allow_non_integral_constant_expression_p;
1453 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1454 been seen that makes the expression non-constant. */
1455 bool non_integral_constant_expression_p;
1457 /* TRUE if local variable names and `this' are forbidden in the
1458 current context. */
1459 bool local_variables_forbidden_p;
1461 /* TRUE if the declaration we are parsing is part of a
1462 linkage-specification of the form `extern string-literal
1463 declaration'. */
1464 bool in_unbraced_linkage_specification_p;
1466 /* TRUE if we are presently parsing a declarator, after the
1467 direct-declarator. */
1468 bool in_declarator_p;
1470 /* TRUE if we are presently parsing a template-argument-list. */
1471 bool in_template_argument_list_p;
1473 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1474 to IN_OMP_BLOCK if parsing OpenMP structured block and
1475 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1476 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1477 iteration-statement, OpenMP block or loop within that switch. */
1478 #define IN_SWITCH_STMT 1
1479 #define IN_ITERATION_STMT 2
1480 #define IN_OMP_BLOCK 4
1481 #define IN_OMP_FOR 8
1482 #define IN_IF_STMT 16
1483 unsigned char in_statement;
1485 /* TRUE if we are presently parsing the body of a switch statement.
1486 Note that this doesn't quite overlap with in_statement above.
1487 The difference relates to giving the right sets of error messages:
1488 "case not in switch" vs "break statement used with OpenMP...". */
1489 bool in_switch_statement_p;
1491 /* TRUE if we are parsing a type-id in an expression context. In
1492 such a situation, both "type (expr)" and "type (type)" are valid
1493 alternatives. */
1494 bool in_type_id_in_expr_p;
1496 /* TRUE if we are currently in a header file where declarations are
1497 implicitly extern "C". */
1498 bool implicit_extern_c;
1500 /* TRUE if strings in expressions should be translated to the execution
1501 character set. */
1502 bool translate_strings_p;
1504 /* TRUE if we are presently parsing the body of a function, but not
1505 a local class. */
1506 bool in_function_body;
1508 /* If non-NULL, then we are parsing a construct where new type
1509 definitions are not permitted. The string stored here will be
1510 issued as an error message if a type is defined. */
1511 const char *type_definition_forbidden_message;
1513 /* A list of lists. The outer list is a stack, used for member
1514 functions of local classes. At each level there are two sub-list,
1515 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1516 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1517 TREE_VALUE's. The functions are chained in reverse declaration
1518 order.
1520 The TREE_PURPOSE sublist contains those functions with default
1521 arguments that need post processing, and the TREE_VALUE sublist
1522 contains those functions with definitions that need post
1523 processing.
1525 These lists can only be processed once the outermost class being
1526 defined is complete. */
1527 tree unparsed_functions_queues;
1529 /* The number of classes whose definitions are currently in
1530 progress. */
1531 unsigned num_classes_being_defined;
1533 /* The number of template parameter lists that apply directly to the
1534 current declaration. */
1535 unsigned num_template_parameter_lists;
1536 } cp_parser;
1538 /* Prototypes. */
1540 /* Constructors and destructors. */
1542 static cp_parser *cp_parser_new
1543 (void);
1545 /* Routines to parse various constructs.
1547 Those that return `tree' will return the error_mark_node (rather
1548 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1549 Sometimes, they will return an ordinary node if error-recovery was
1550 attempted, even though a parse error occurred. So, to check
1551 whether or not a parse error occurred, you should always use
1552 cp_parser_error_occurred. If the construct is optional (indicated
1553 either by an `_opt' in the name of the function that does the
1554 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1555 the construct is not present. */
1557 /* Lexical conventions [gram.lex] */
1559 static tree cp_parser_identifier
1560 (cp_parser *);
1561 static tree cp_parser_string_literal
1562 (cp_parser *, bool, bool);
1564 /* Basic concepts [gram.basic] */
1566 static bool cp_parser_translation_unit
1567 (cp_parser *);
1569 /* Expressions [gram.expr] */
1571 static tree cp_parser_primary_expression
1572 (cp_parser *, bool, bool, bool, cp_id_kind *);
1573 static tree cp_parser_id_expression
1574 (cp_parser *, bool, bool, bool *, bool, bool);
1575 static tree cp_parser_unqualified_id
1576 (cp_parser *, bool, bool, bool, bool);
1577 static tree cp_parser_nested_name_specifier_opt
1578 (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier
1580 (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_class_or_namespace_name
1582 (cp_parser *, bool, bool, bool, bool, bool);
1583 static tree cp_parser_postfix_expression
1584 (cp_parser *, bool, bool, bool);
1585 static tree cp_parser_postfix_open_square_expression
1586 (cp_parser *, tree, bool);
1587 static tree cp_parser_postfix_dot_deref_expression
1588 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1589 static tree cp_parser_parenthesized_expression_list
1590 (cp_parser *, bool, bool, bool, bool *);
1591 static void cp_parser_pseudo_destructor_name
1592 (cp_parser *, tree *, tree *);
1593 static tree cp_parser_unary_expression
1594 (cp_parser *, bool, bool);
1595 static enum tree_code cp_parser_unary_operator
1596 (cp_token *);
1597 static tree cp_parser_new_expression
1598 (cp_parser *);
1599 static tree cp_parser_new_placement
1600 (cp_parser *);
1601 static tree cp_parser_new_type_id
1602 (cp_parser *, tree *);
1603 static cp_declarator *cp_parser_new_declarator_opt
1604 (cp_parser *);
1605 static cp_declarator *cp_parser_direct_new_declarator
1606 (cp_parser *);
1607 static tree cp_parser_new_initializer
1608 (cp_parser *);
1609 static tree cp_parser_delete_expression
1610 (cp_parser *);
1611 static tree cp_parser_cast_expression
1612 (cp_parser *, bool, bool);
1613 static tree cp_parser_binary_expression
1614 (cp_parser *, bool);
1615 static tree cp_parser_question_colon_clause
1616 (cp_parser *, tree);
1617 static tree cp_parser_assignment_expression
1618 (cp_parser *, bool);
1619 static enum tree_code cp_parser_assignment_operator_opt
1620 (cp_parser *);
1621 static tree cp_parser_expression
1622 (cp_parser *, bool);
1623 static tree cp_parser_constant_expression
1624 (cp_parser *, bool, bool *);
1625 static tree cp_parser_builtin_offsetof
1626 (cp_parser *);
1628 /* Statements [gram.stmt.stmt] */
1630 static void cp_parser_statement
1631 (cp_parser *, tree, bool, bool *);
1632 static void cp_parser_label_for_labeled_statement
1633 (cp_parser *);
1634 static tree cp_parser_expression_statement
1635 (cp_parser *, tree);
1636 static tree cp_parser_compound_statement
1637 (cp_parser *, tree, bool);
1638 static void cp_parser_statement_seq_opt
1639 (cp_parser *, tree);
1640 static tree cp_parser_selection_statement
1641 (cp_parser *, bool *);
1642 static tree cp_parser_condition
1643 (cp_parser *);
1644 static tree cp_parser_iteration_statement
1645 (cp_parser *);
1646 static void cp_parser_for_init_statement
1647 (cp_parser *);
1648 static tree cp_parser_jump_statement
1649 (cp_parser *);
1650 static void cp_parser_declaration_statement
1651 (cp_parser *);
1653 static tree cp_parser_implicitly_scoped_statement
1654 (cp_parser *, bool *);
1655 static void cp_parser_already_scoped_statement
1656 (cp_parser *);
1658 /* Declarations [gram.dcl.dcl] */
1660 static void cp_parser_declaration_seq_opt
1661 (cp_parser *);
1662 static void cp_parser_declaration
1663 (cp_parser *);
1664 static void cp_parser_block_declaration
1665 (cp_parser *, bool);
1666 static void cp_parser_simple_declaration
1667 (cp_parser *, bool);
1668 static void cp_parser_decl_specifier_seq
1669 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1670 static tree cp_parser_storage_class_specifier_opt
1671 (cp_parser *);
1672 static tree cp_parser_function_specifier_opt
1673 (cp_parser *, cp_decl_specifier_seq *);
1674 static tree cp_parser_type_specifier
1675 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1676 int *, bool *);
1677 static tree cp_parser_simple_type_specifier
1678 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1679 static tree cp_parser_type_name
1680 (cp_parser *);
1681 static tree cp_parser_nonclass_name
1682 (cp_parser* parser);
1683 static tree cp_parser_elaborated_type_specifier
1684 (cp_parser *, bool, bool);
1685 static tree cp_parser_enum_specifier
1686 (cp_parser *);
1687 static void cp_parser_enumerator_list
1688 (cp_parser *, tree);
1689 static void cp_parser_enumerator_definition
1690 (cp_parser *, tree);
1691 static tree cp_parser_namespace_name
1692 (cp_parser *);
1693 static void cp_parser_namespace_definition
1694 (cp_parser *);
1695 static void cp_parser_namespace_body
1696 (cp_parser *);
1697 static tree cp_parser_qualified_namespace_specifier
1698 (cp_parser *);
1699 static void cp_parser_namespace_alias_definition
1700 (cp_parser *);
1701 static bool cp_parser_using_declaration
1702 (cp_parser *, bool);
1703 static void cp_parser_using_directive
1704 (cp_parser *);
1705 static void cp_parser_asm_definition
1706 (cp_parser *);
1707 static void cp_parser_linkage_specification
1708 (cp_parser *);
1709 static void cp_parser_static_assert
1710 (cp_parser *, bool);
1711 static tree cp_parser_decltype
1712 (cp_parser *);
1714 /* Declarators [gram.dcl.decl] */
1716 static tree cp_parser_init_declarator
1717 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1718 static cp_declarator *cp_parser_declarator
1719 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1720 static cp_declarator *cp_parser_direct_declarator
1721 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1722 static enum tree_code cp_parser_ptr_operator
1723 (cp_parser *, tree *, cp_cv_quals *);
1724 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1725 (cp_parser *);
1726 static tree cp_parser_declarator_id
1727 (cp_parser *, bool);
1728 static tree cp_parser_type_id
1729 (cp_parser *);
1730 static void cp_parser_type_specifier_seq
1731 (cp_parser *, bool, cp_decl_specifier_seq *);
1732 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1733 (cp_parser *);
1734 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1735 (cp_parser *, bool *);
1736 static cp_parameter_declarator *cp_parser_parameter_declaration
1737 (cp_parser *, bool, bool *);
1738 static tree cp_parser_default_argument
1739 (cp_parser *, bool);
1740 static void cp_parser_function_body
1741 (cp_parser *);
1742 static tree cp_parser_initializer
1743 (cp_parser *, bool *, bool *);
1744 static tree cp_parser_initializer_clause
1745 (cp_parser *, bool *);
1746 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1747 (cp_parser *, bool *);
1749 static bool cp_parser_ctor_initializer_opt_and_function_body
1750 (cp_parser *);
1752 /* Classes [gram.class] */
1754 static tree cp_parser_class_name
1755 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1756 static tree cp_parser_class_specifier
1757 (cp_parser *);
1758 static tree cp_parser_class_head
1759 (cp_parser *, bool *, tree *, tree *);
1760 static enum tag_types cp_parser_class_key
1761 (cp_parser *);
1762 static void cp_parser_member_specification_opt
1763 (cp_parser *);
1764 static void cp_parser_member_declaration
1765 (cp_parser *);
1766 static tree cp_parser_pure_specifier
1767 (cp_parser *);
1768 static tree cp_parser_constant_initializer
1769 (cp_parser *);
1771 /* Derived classes [gram.class.derived] */
1773 static tree cp_parser_base_clause
1774 (cp_parser *);
1775 static tree cp_parser_base_specifier
1776 (cp_parser *);
1778 /* Special member functions [gram.special] */
1780 static tree cp_parser_conversion_function_id
1781 (cp_parser *);
1782 static tree cp_parser_conversion_type_id
1783 (cp_parser *);
1784 static cp_declarator *cp_parser_conversion_declarator_opt
1785 (cp_parser *);
1786 static bool cp_parser_ctor_initializer_opt
1787 (cp_parser *);
1788 static void cp_parser_mem_initializer_list
1789 (cp_parser *);
1790 static tree cp_parser_mem_initializer
1791 (cp_parser *);
1792 static tree cp_parser_mem_initializer_id
1793 (cp_parser *);
1795 /* Overloading [gram.over] */
1797 static tree cp_parser_operator_function_id
1798 (cp_parser *);
1799 static tree cp_parser_operator
1800 (cp_parser *);
1802 /* Templates [gram.temp] */
1804 static void cp_parser_template_declaration
1805 (cp_parser *, bool);
1806 static tree cp_parser_template_parameter_list
1807 (cp_parser *);
1808 static tree cp_parser_template_parameter
1809 (cp_parser *, bool *, bool *);
1810 static tree cp_parser_type_parameter
1811 (cp_parser *, bool *);
1812 static tree cp_parser_template_id
1813 (cp_parser *, bool, bool, bool);
1814 static tree cp_parser_template_name
1815 (cp_parser *, bool, bool, bool, bool *);
1816 static tree cp_parser_template_argument_list
1817 (cp_parser *);
1818 static tree cp_parser_template_argument
1819 (cp_parser *);
1820 static void cp_parser_explicit_instantiation
1821 (cp_parser *);
1822 static void cp_parser_explicit_specialization
1823 (cp_parser *);
1825 /* Exception handling [gram.exception] */
1827 static tree cp_parser_try_block
1828 (cp_parser *);
1829 static bool cp_parser_function_try_block
1830 (cp_parser *);
1831 static void cp_parser_handler_seq
1832 (cp_parser *);
1833 static void cp_parser_handler
1834 (cp_parser *);
1835 static tree cp_parser_exception_declaration
1836 (cp_parser *);
1837 static tree cp_parser_throw_expression
1838 (cp_parser *);
1839 static tree cp_parser_exception_specification_opt
1840 (cp_parser *);
1841 static tree cp_parser_type_id_list
1842 (cp_parser *);
1844 /* GNU Extensions */
1846 static tree cp_parser_asm_specification_opt
1847 (cp_parser *);
1848 static tree cp_parser_asm_operand_list
1849 (cp_parser *);
1850 static tree cp_parser_asm_clobber_list
1851 (cp_parser *);
1852 static tree cp_parser_attributes_opt
1853 (cp_parser *);
1854 static tree cp_parser_attribute_list
1855 (cp_parser *);
1856 static bool cp_parser_extension_opt
1857 (cp_parser *, int *);
1858 static void cp_parser_label_declaration
1859 (cp_parser *);
1861 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1862 static bool cp_parser_pragma
1863 (cp_parser *, enum pragma_context);
1865 /* Objective-C++ Productions */
1867 static tree cp_parser_objc_message_receiver
1868 (cp_parser *);
1869 static tree cp_parser_objc_message_args
1870 (cp_parser *);
1871 static tree cp_parser_objc_message_expression
1872 (cp_parser *);
1873 static tree cp_parser_objc_encode_expression
1874 (cp_parser *);
1875 static tree cp_parser_objc_defs_expression
1876 (cp_parser *);
1877 static tree cp_parser_objc_protocol_expression
1878 (cp_parser *);
1879 static tree cp_parser_objc_selector_expression
1880 (cp_parser *);
1881 static tree cp_parser_objc_expression
1882 (cp_parser *);
1883 static bool cp_parser_objc_selector_p
1884 (enum cpp_ttype);
1885 static tree cp_parser_objc_selector
1886 (cp_parser *);
1887 static tree cp_parser_objc_protocol_refs_opt
1888 (cp_parser *);
1889 static void cp_parser_objc_declaration
1890 (cp_parser *);
1891 static tree cp_parser_objc_statement
1892 (cp_parser *);
1894 /* Utility Routines */
1896 static tree cp_parser_lookup_name
1897 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1898 static tree cp_parser_lookup_name_simple
1899 (cp_parser *, tree);
1900 static tree cp_parser_maybe_treat_template_as_class
1901 (tree, bool);
1902 static bool cp_parser_check_declarator_template_parameters
1903 (cp_parser *, cp_declarator *);
1904 static bool cp_parser_check_template_parameters
1905 (cp_parser *, unsigned);
1906 static tree cp_parser_simple_cast_expression
1907 (cp_parser *);
1908 static tree cp_parser_global_scope_opt
1909 (cp_parser *, bool);
1910 static bool cp_parser_constructor_declarator_p
1911 (cp_parser *, bool);
1912 static tree cp_parser_function_definition_from_specifiers_and_declarator
1913 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1914 static tree cp_parser_function_definition_after_declarator
1915 (cp_parser *, bool);
1916 static void cp_parser_template_declaration_after_export
1917 (cp_parser *, bool);
1918 static void cp_parser_perform_template_parameter_access_checks
1919 (VEC (deferred_access_check,gc)*);
1920 static tree cp_parser_single_declaration
1921 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1922 static tree cp_parser_functional_cast
1923 (cp_parser *, tree);
1924 static tree cp_parser_save_member_function_body
1925 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1926 static tree cp_parser_enclosed_template_argument_list
1927 (cp_parser *);
1928 static void cp_parser_save_default_args
1929 (cp_parser *, tree);
1930 static void cp_parser_late_parsing_for_member
1931 (cp_parser *, tree);
1932 static void cp_parser_late_parsing_default_args
1933 (cp_parser *, tree);
1934 static tree cp_parser_sizeof_operand
1935 (cp_parser *, enum rid);
1936 static tree cp_parser_trait_expr
1937 (cp_parser *, enum rid);
1938 static bool cp_parser_declares_only_class_p
1939 (cp_parser *);
1940 static void cp_parser_set_storage_class
1941 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1942 static void cp_parser_set_decl_spec_type
1943 (cp_decl_specifier_seq *, tree, bool);
1944 static bool cp_parser_friend_p
1945 (const cp_decl_specifier_seq *);
1946 static cp_token *cp_parser_require
1947 (cp_parser *, enum cpp_ttype, const char *);
1948 static cp_token *cp_parser_require_keyword
1949 (cp_parser *, enum rid, const char *);
1950 static bool cp_parser_token_starts_function_definition_p
1951 (cp_token *);
1952 static bool cp_parser_next_token_starts_class_definition_p
1953 (cp_parser *);
1954 static bool cp_parser_next_token_ends_template_argument_p
1955 (cp_parser *);
1956 static bool cp_parser_nth_token_starts_template_argument_list_p
1957 (cp_parser *, size_t);
1958 static enum tag_types cp_parser_token_is_class_key
1959 (cp_token *);
1960 static void cp_parser_check_class_key
1961 (enum tag_types, tree type);
1962 static void cp_parser_check_access_in_redeclaration
1963 (tree type);
1964 static bool cp_parser_optional_template_keyword
1965 (cp_parser *);
1966 static void cp_parser_pre_parsed_nested_name_specifier
1967 (cp_parser *);
1968 static void cp_parser_cache_group
1969 (cp_parser *, enum cpp_ttype, unsigned);
1970 static void cp_parser_parse_tentatively
1971 (cp_parser *);
1972 static void cp_parser_commit_to_tentative_parse
1973 (cp_parser *);
1974 static void cp_parser_abort_tentative_parse
1975 (cp_parser *);
1976 static bool cp_parser_parse_definitely
1977 (cp_parser *);
1978 static inline bool cp_parser_parsing_tentatively
1979 (cp_parser *);
1980 static bool cp_parser_uncommitted_to_tentative_parse_p
1981 (cp_parser *);
1982 static void cp_parser_error
1983 (cp_parser *, const char *);
1984 static void cp_parser_name_lookup_error
1985 (cp_parser *, tree, tree, const char *);
1986 static bool cp_parser_simulate_error
1987 (cp_parser *);
1988 static bool cp_parser_check_type_definition
1989 (cp_parser *);
1990 static void cp_parser_check_for_definition_in_return_type
1991 (cp_declarator *, tree);
1992 static void cp_parser_check_for_invalid_template_id
1993 (cp_parser *, tree);
1994 static bool cp_parser_non_integral_constant_expression
1995 (cp_parser *, const char *);
1996 static void cp_parser_diagnose_invalid_type_name
1997 (cp_parser *, tree, tree);
1998 static bool cp_parser_parse_and_diagnose_invalid_type_name
1999 (cp_parser *);
2000 static int cp_parser_skip_to_closing_parenthesis
2001 (cp_parser *, bool, bool, bool);
2002 static void cp_parser_skip_to_end_of_statement
2003 (cp_parser *);
2004 static void cp_parser_consume_semicolon_at_end_of_statement
2005 (cp_parser *);
2006 static void cp_parser_skip_to_end_of_block_or_statement
2007 (cp_parser *);
2008 static bool cp_parser_skip_to_closing_brace
2009 (cp_parser *);
2010 static void cp_parser_skip_to_end_of_template_parameter_list
2011 (cp_parser *);
2012 static void cp_parser_skip_to_pragma_eol
2013 (cp_parser*, cp_token *);
2014 static bool cp_parser_error_occurred
2015 (cp_parser *);
2016 static bool cp_parser_allow_gnu_extensions_p
2017 (cp_parser *);
2018 static bool cp_parser_is_string_literal
2019 (cp_token *);
2020 static bool cp_parser_is_keyword
2021 (cp_token *, enum rid);
2022 static tree cp_parser_make_typename_type
2023 (cp_parser *, tree, tree);
2024 static cp_declarator * cp_parser_make_indirect_declarator
2025 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2027 /* Returns nonzero if we are parsing tentatively. */
2029 static inline bool
2030 cp_parser_parsing_tentatively (cp_parser* parser)
2032 return parser->context->next != NULL;
2035 /* Returns nonzero if TOKEN is a string literal. */
2037 static bool
2038 cp_parser_is_string_literal (cp_token* token)
2040 return (token->type == CPP_STRING ||
2041 token->type == CPP_STRING16 ||
2042 token->type == CPP_STRING32 ||
2043 token->type == CPP_WSTRING);
2046 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2048 static bool
2049 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2051 return token->keyword == keyword;
2054 /* If not parsing tentatively, issue a diagnostic of the form
2055 FILE:LINE: MESSAGE before TOKEN
2056 where TOKEN is the next token in the input stream. MESSAGE
2057 (specified by the caller) is usually of the form "expected
2058 OTHER-TOKEN". */
2060 static void
2061 cp_parser_error (cp_parser* parser, const char* message)
2063 if (!cp_parser_simulate_error (parser))
2065 cp_token *token = cp_lexer_peek_token (parser->lexer);
2066 /* This diagnostic makes more sense if it is tagged to the line
2067 of the token we just peeked at. */
2068 cp_lexer_set_source_position_from_token (token);
2070 if (token->type == CPP_PRAGMA)
2072 error ("%<#pragma%> is not allowed here");
2073 cp_parser_skip_to_pragma_eol (parser, token);
2074 return;
2077 c_parse_error (message,
2078 /* Because c_parser_error does not understand
2079 CPP_KEYWORD, keywords are treated like
2080 identifiers. */
2081 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2082 token->u.value);
2086 /* Issue an error about name-lookup failing. NAME is the
2087 IDENTIFIER_NODE DECL is the result of
2088 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2089 the thing that we hoped to find. */
2091 static void
2092 cp_parser_name_lookup_error (cp_parser* parser,
2093 tree name,
2094 tree decl,
2095 const char* desired)
2097 /* If name lookup completely failed, tell the user that NAME was not
2098 declared. */
2099 if (decl == error_mark_node)
2101 if (parser->scope && parser->scope != global_namespace)
2102 error ("%<%E::%E%> has not been declared",
2103 parser->scope, name);
2104 else if (parser->scope == global_namespace)
2105 error ("%<::%E%> has not been declared", name);
2106 else if (parser->object_scope
2107 && !CLASS_TYPE_P (parser->object_scope))
2108 error ("request for member %qE in non-class type %qT",
2109 name, parser->object_scope);
2110 else if (parser->object_scope)
2111 error ("%<%T::%E%> has not been declared",
2112 parser->object_scope, name);
2113 else
2114 error ("%qE has not been declared", name);
2116 else if (parser->scope && parser->scope != global_namespace)
2117 error ("%<%E::%E%> %s", parser->scope, name, desired);
2118 else if (parser->scope == global_namespace)
2119 error ("%<::%E%> %s", name, desired);
2120 else
2121 error ("%qE %s", name, desired);
2124 /* If we are parsing tentatively, remember that an error has occurred
2125 during this tentative parse. Returns true if the error was
2126 simulated; false if a message should be issued by the caller. */
2128 static bool
2129 cp_parser_simulate_error (cp_parser* parser)
2131 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2133 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2134 return true;
2136 return false;
2139 /* Check for repeated decl-specifiers. */
2141 static void
2142 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2144 cp_decl_spec ds;
2146 for (ds = ds_first; ds != ds_last; ++ds)
2148 unsigned count = decl_specs->specs[(int)ds];
2149 if (count < 2)
2150 continue;
2151 /* The "long" specifier is a special case because of "long long". */
2152 if (ds == ds_long)
2154 if (count > 2)
2155 error ("%<long long long%> is too long for GCC");
2156 else if (pedantic && !in_system_header && warn_long_long
2157 && cxx_dialect == cxx98)
2158 pedwarn ("ISO C++ 1998 does not support %<long long%>");
2160 else if (count > 1)
2162 static const char *const decl_spec_names[] = {
2163 "signed",
2164 "unsigned",
2165 "short",
2166 "long",
2167 "const",
2168 "volatile",
2169 "restrict",
2170 "inline",
2171 "virtual",
2172 "explicit",
2173 "friend",
2174 "typedef",
2175 "__complex",
2176 "__thread"
2178 error ("duplicate %qs", decl_spec_names[(int)ds]);
2183 /* This function is called when a type is defined. If type
2184 definitions are forbidden at this point, an error message is
2185 issued. */
2187 static bool
2188 cp_parser_check_type_definition (cp_parser* parser)
2190 /* If types are forbidden here, issue a message. */
2191 if (parser->type_definition_forbidden_message)
2193 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2194 in the message need to be interpreted. */
2195 error (parser->type_definition_forbidden_message);
2196 return false;
2198 return true;
2201 /* This function is called when the DECLARATOR is processed. The TYPE
2202 was a type defined in the decl-specifiers. If it is invalid to
2203 define a type in the decl-specifiers for DECLARATOR, an error is
2204 issued. */
2206 static void
2207 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2208 tree type)
2210 /* [dcl.fct] forbids type definitions in return types.
2211 Unfortunately, it's not easy to know whether or not we are
2212 processing a return type until after the fact. */
2213 while (declarator
2214 && (declarator->kind == cdk_pointer
2215 || declarator->kind == cdk_reference
2216 || declarator->kind == cdk_ptrmem))
2217 declarator = declarator->declarator;
2218 if (declarator
2219 && declarator->kind == cdk_function)
2221 error ("new types may not be defined in a return type");
2222 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2223 type);
2227 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2228 "<" in any valid C++ program. If the next token is indeed "<",
2229 issue a message warning the user about what appears to be an
2230 invalid attempt to form a template-id. */
2232 static void
2233 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2234 tree type)
2236 cp_token_position start = 0;
2238 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2240 if (TYPE_P (type))
2241 error ("%qT is not a template", type);
2242 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2243 error ("%qE is not a template", type);
2244 else
2245 error ("invalid template-id");
2246 /* Remember the location of the invalid "<". */
2247 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2248 start = cp_lexer_token_position (parser->lexer, true);
2249 /* Consume the "<". */
2250 cp_lexer_consume_token (parser->lexer);
2251 /* Parse the template arguments. */
2252 cp_parser_enclosed_template_argument_list (parser);
2253 /* Permanently remove the invalid template arguments so that
2254 this error message is not issued again. */
2255 if (start)
2256 cp_lexer_purge_tokens_after (parser->lexer, start);
2260 /* If parsing an integral constant-expression, issue an error message
2261 about the fact that THING appeared and return true. Otherwise,
2262 return false. In either case, set
2263 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2265 static bool
2266 cp_parser_non_integral_constant_expression (cp_parser *parser,
2267 const char *thing)
2269 parser->non_integral_constant_expression_p = true;
2270 if (parser->integral_constant_expression_p)
2272 if (!parser->allow_non_integral_constant_expression_p)
2274 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2275 in the message need to be interpreted. */
2276 char *message = concat (thing,
2277 " cannot appear in a constant-expression",
2278 NULL);
2279 error (message);
2280 free (message);
2281 return true;
2284 return false;
2287 /* Emit a diagnostic for an invalid type name. SCOPE is the
2288 qualifying scope (or NULL, if none) for ID. This function commits
2289 to the current active tentative parse, if any. (Otherwise, the
2290 problematic construct might be encountered again later, resulting
2291 in duplicate error messages.) */
2293 static void
2294 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2296 tree decl, old_scope;
2297 /* Try to lookup the identifier. */
2298 old_scope = parser->scope;
2299 parser->scope = scope;
2300 decl = cp_parser_lookup_name_simple (parser, id);
2301 parser->scope = old_scope;
2302 /* If the lookup found a template-name, it means that the user forgot
2303 to specify an argument list. Emit a useful error message. */
2304 if (TREE_CODE (decl) == TEMPLATE_DECL)
2305 error ("invalid use of template-name %qE without an argument list", decl);
2306 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2307 error ("invalid use of destructor %qD as a type", id);
2308 else if (TREE_CODE (decl) == TYPE_DECL)
2309 /* Something like 'unsigned A a;' */
2310 error ("invalid combination of multiple type-specifiers");
2311 else if (!parser->scope)
2313 /* Issue an error message. */
2314 error ("%qE does not name a type", id);
2315 /* If we're in a template class, it's possible that the user was
2316 referring to a type from a base class. For example:
2318 template <typename T> struct A { typedef T X; };
2319 template <typename T> struct B : public A<T> { X x; };
2321 The user should have said "typename A<T>::X". */
2322 if (processing_template_decl && current_class_type
2323 && TYPE_BINFO (current_class_type))
2325 tree b;
2327 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2329 b = TREE_CHAIN (b))
2331 tree base_type = BINFO_TYPE (b);
2332 if (CLASS_TYPE_P (base_type)
2333 && dependent_type_p (base_type))
2335 tree field;
2336 /* Go from a particular instantiation of the
2337 template (which will have an empty TYPE_FIELDs),
2338 to the main version. */
2339 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2340 for (field = TYPE_FIELDS (base_type);
2341 field;
2342 field = TREE_CHAIN (field))
2343 if (TREE_CODE (field) == TYPE_DECL
2344 && DECL_NAME (field) == id)
2346 inform ("(perhaps %<typename %T::%E%> was intended)",
2347 BINFO_TYPE (b), id);
2348 break;
2350 if (field)
2351 break;
2356 /* Here we diagnose qualified-ids where the scope is actually correct,
2357 but the identifier does not resolve to a valid type name. */
2358 else if (parser->scope != error_mark_node)
2360 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2361 error ("%qE in namespace %qE does not name a type",
2362 id, parser->scope);
2363 else if (TYPE_P (parser->scope))
2364 error ("%qE in class %qT does not name a type", id, parser->scope);
2365 else
2366 gcc_unreachable ();
2368 cp_parser_commit_to_tentative_parse (parser);
2371 /* Check for a common situation where a type-name should be present,
2372 but is not, and issue a sensible error message. Returns true if an
2373 invalid type-name was detected.
2375 The situation handled by this function are variable declarations of the
2376 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2377 Usually, `ID' should name a type, but if we got here it means that it
2378 does not. We try to emit the best possible error message depending on
2379 how exactly the id-expression looks like. */
2381 static bool
2382 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2384 tree id;
2386 cp_parser_parse_tentatively (parser);
2387 id = cp_parser_id_expression (parser,
2388 /*template_keyword_p=*/false,
2389 /*check_dependency_p=*/true,
2390 /*template_p=*/NULL,
2391 /*declarator_p=*/true,
2392 /*optional_p=*/false);
2393 /* After the id-expression, there should be a plain identifier,
2394 otherwise this is not a simple variable declaration. Also, if
2395 the scope is dependent, we cannot do much. */
2396 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2397 || (parser->scope && TYPE_P (parser->scope)
2398 && dependent_type_p (parser->scope))
2399 || TREE_CODE (id) == TYPE_DECL)
2401 cp_parser_abort_tentative_parse (parser);
2402 return false;
2404 if (!cp_parser_parse_definitely (parser))
2405 return false;
2407 /* Emit a diagnostic for the invalid type. */
2408 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2409 /* Skip to the end of the declaration; there's no point in
2410 trying to process it. */
2411 cp_parser_skip_to_end_of_block_or_statement (parser);
2412 return true;
2415 /* Consume tokens up to, and including, the next non-nested closing `)'.
2416 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2417 are doing error recovery. Returns -1 if OR_COMMA is true and we
2418 found an unnested comma. */
2420 static int
2421 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2422 bool recovering,
2423 bool or_comma,
2424 bool consume_paren)
2426 unsigned paren_depth = 0;
2427 unsigned brace_depth = 0;
2429 if (recovering && !or_comma
2430 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2431 return 0;
2433 while (true)
2435 cp_token * token = cp_lexer_peek_token (parser->lexer);
2437 switch (token->type)
2439 case CPP_EOF:
2440 case CPP_PRAGMA_EOL:
2441 /* If we've run out of tokens, then there is no closing `)'. */
2442 return 0;
2444 case CPP_SEMICOLON:
2445 /* This matches the processing in skip_to_end_of_statement. */
2446 if (!brace_depth)
2447 return 0;
2448 break;
2450 case CPP_OPEN_BRACE:
2451 ++brace_depth;
2452 break;
2453 case CPP_CLOSE_BRACE:
2454 if (!brace_depth--)
2455 return 0;
2456 break;
2458 case CPP_COMMA:
2459 if (recovering && or_comma && !brace_depth && !paren_depth)
2460 return -1;
2461 break;
2463 case CPP_OPEN_PAREN:
2464 if (!brace_depth)
2465 ++paren_depth;
2466 break;
2468 case CPP_CLOSE_PAREN:
2469 if (!brace_depth && !paren_depth--)
2471 if (consume_paren)
2472 cp_lexer_consume_token (parser->lexer);
2473 return 1;
2475 break;
2477 default:
2478 break;
2481 /* Consume the token. */
2482 cp_lexer_consume_token (parser->lexer);
2486 /* Consume tokens until we reach the end of the current statement.
2487 Normally, that will be just before consuming a `;'. However, if a
2488 non-nested `}' comes first, then we stop before consuming that. */
2490 static void
2491 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2493 unsigned nesting_depth = 0;
2495 while (true)
2497 cp_token *token = cp_lexer_peek_token (parser->lexer);
2499 switch (token->type)
2501 case CPP_EOF:
2502 case CPP_PRAGMA_EOL:
2503 /* If we've run out of tokens, stop. */
2504 return;
2506 case CPP_SEMICOLON:
2507 /* If the next token is a `;', we have reached the end of the
2508 statement. */
2509 if (!nesting_depth)
2510 return;
2511 break;
2513 case CPP_CLOSE_BRACE:
2514 /* If this is a non-nested '}', stop before consuming it.
2515 That way, when confronted with something like:
2517 { 3 + }
2519 we stop before consuming the closing '}', even though we
2520 have not yet reached a `;'. */
2521 if (nesting_depth == 0)
2522 return;
2524 /* If it is the closing '}' for a block that we have
2525 scanned, stop -- but only after consuming the token.
2526 That way given:
2528 void f g () { ... }
2529 typedef int I;
2531 we will stop after the body of the erroneously declared
2532 function, but before consuming the following `typedef'
2533 declaration. */
2534 if (--nesting_depth == 0)
2536 cp_lexer_consume_token (parser->lexer);
2537 return;
2540 case CPP_OPEN_BRACE:
2541 ++nesting_depth;
2542 break;
2544 default:
2545 break;
2548 /* Consume the token. */
2549 cp_lexer_consume_token (parser->lexer);
2553 /* This function is called at the end of a statement or declaration.
2554 If the next token is a semicolon, it is consumed; otherwise, error
2555 recovery is attempted. */
2557 static void
2558 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2560 /* Look for the trailing `;'. */
2561 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2563 /* If there is additional (erroneous) input, skip to the end of
2564 the statement. */
2565 cp_parser_skip_to_end_of_statement (parser);
2566 /* If the next token is now a `;', consume it. */
2567 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2568 cp_lexer_consume_token (parser->lexer);
2572 /* Skip tokens until we have consumed an entire block, or until we
2573 have consumed a non-nested `;'. */
2575 static void
2576 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2578 int nesting_depth = 0;
2580 while (nesting_depth >= 0)
2582 cp_token *token = cp_lexer_peek_token (parser->lexer);
2584 switch (token->type)
2586 case CPP_EOF:
2587 case CPP_PRAGMA_EOL:
2588 /* If we've run out of tokens, stop. */
2589 return;
2591 case CPP_SEMICOLON:
2592 /* Stop if this is an unnested ';'. */
2593 if (!nesting_depth)
2594 nesting_depth = -1;
2595 break;
2597 case CPP_CLOSE_BRACE:
2598 /* Stop if this is an unnested '}', or closes the outermost
2599 nesting level. */
2600 nesting_depth--;
2601 if (!nesting_depth)
2602 nesting_depth = -1;
2603 break;
2605 case CPP_OPEN_BRACE:
2606 /* Nest. */
2607 nesting_depth++;
2608 break;
2610 default:
2611 break;
2614 /* Consume the token. */
2615 cp_lexer_consume_token (parser->lexer);
2619 /* Skip tokens until a non-nested closing curly brace is the next
2620 token, or there are no more tokens. Return true in the first case,
2621 false otherwise. */
2623 static bool
2624 cp_parser_skip_to_closing_brace (cp_parser *parser)
2626 unsigned nesting_depth = 0;
2628 while (true)
2630 cp_token *token = cp_lexer_peek_token (parser->lexer);
2632 switch (token->type)
2634 case CPP_EOF:
2635 case CPP_PRAGMA_EOL:
2636 /* If we've run out of tokens, stop. */
2637 return false;
2639 case CPP_CLOSE_BRACE:
2640 /* If the next token is a non-nested `}', then we have reached
2641 the end of the current block. */
2642 if (nesting_depth-- == 0)
2643 return true;
2644 break;
2646 case CPP_OPEN_BRACE:
2647 /* If it the next token is a `{', then we are entering a new
2648 block. Consume the entire block. */
2649 ++nesting_depth;
2650 break;
2652 default:
2653 break;
2656 /* Consume the token. */
2657 cp_lexer_consume_token (parser->lexer);
2661 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2662 parameter is the PRAGMA token, allowing us to purge the entire pragma
2663 sequence. */
2665 static void
2666 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2668 cp_token *token;
2670 parser->lexer->in_pragma = false;
2673 token = cp_lexer_consume_token (parser->lexer);
2674 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2676 /* Ensure that the pragma is not parsed again. */
2677 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2680 /* Require pragma end of line, resyncing with it as necessary. The
2681 arguments are as for cp_parser_skip_to_pragma_eol. */
2683 static void
2684 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2686 parser->lexer->in_pragma = false;
2687 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2688 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2691 /* This is a simple wrapper around make_typename_type. When the id is
2692 an unresolved identifier node, we can provide a superior diagnostic
2693 using cp_parser_diagnose_invalid_type_name. */
2695 static tree
2696 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2698 tree result;
2699 if (TREE_CODE (id) == IDENTIFIER_NODE)
2701 result = make_typename_type (scope, id, typename_type,
2702 /*complain=*/tf_none);
2703 if (result == error_mark_node)
2704 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2705 return result;
2707 return make_typename_type (scope, id, typename_type, tf_error);
2710 /* This is a wrapper around the
2711 make_{pointer,ptrmem,reference}_declarator functions that decides
2712 which one to call based on the CODE and CLASS_TYPE arguments. The
2713 CODE argument should be one of the values returned by
2714 cp_parser_ptr_operator. */
2715 static cp_declarator *
2716 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2717 cp_cv_quals cv_qualifiers,
2718 cp_declarator *target)
2720 if (code == ERROR_MARK)
2721 return cp_error_declarator;
2723 if (code == INDIRECT_REF)
2724 if (class_type == NULL_TREE)
2725 return make_pointer_declarator (cv_qualifiers, target);
2726 else
2727 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2728 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2729 return make_reference_declarator (cv_qualifiers, target, false);
2730 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2731 return make_reference_declarator (cv_qualifiers, target, true);
2732 gcc_unreachable ();
2735 /* Create a new C++ parser. */
2737 static cp_parser *
2738 cp_parser_new (void)
2740 cp_parser *parser;
2741 cp_lexer *lexer;
2742 unsigned i;
2744 /* cp_lexer_new_main is called before calling ggc_alloc because
2745 cp_lexer_new_main might load a PCH file. */
2746 lexer = cp_lexer_new_main ();
2748 /* Initialize the binops_by_token so that we can get the tree
2749 directly from the token. */
2750 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2751 binops_by_token[binops[i].token_type] = binops[i];
2753 parser = GGC_CNEW (cp_parser);
2754 parser->lexer = lexer;
2755 parser->context = cp_parser_context_new (NULL);
2757 /* For now, we always accept GNU extensions. */
2758 parser->allow_gnu_extensions_p = 1;
2760 /* The `>' token is a greater-than operator, not the end of a
2761 template-id. */
2762 parser->greater_than_is_operator_p = true;
2764 parser->default_arg_ok_p = true;
2766 /* We are not parsing a constant-expression. */
2767 parser->integral_constant_expression_p = false;
2768 parser->allow_non_integral_constant_expression_p = false;
2769 parser->non_integral_constant_expression_p = false;
2771 /* Local variable names are not forbidden. */
2772 parser->local_variables_forbidden_p = false;
2774 /* We are not processing an `extern "C"' declaration. */
2775 parser->in_unbraced_linkage_specification_p = false;
2777 /* We are not processing a declarator. */
2778 parser->in_declarator_p = false;
2780 /* We are not processing a template-argument-list. */
2781 parser->in_template_argument_list_p = false;
2783 /* We are not in an iteration statement. */
2784 parser->in_statement = 0;
2786 /* We are not in a switch statement. */
2787 parser->in_switch_statement_p = false;
2789 /* We are not parsing a type-id inside an expression. */
2790 parser->in_type_id_in_expr_p = false;
2792 /* Declarations aren't implicitly extern "C". */
2793 parser->implicit_extern_c = false;
2795 /* String literals should be translated to the execution character set. */
2796 parser->translate_strings_p = true;
2798 /* We are not parsing a function body. */
2799 parser->in_function_body = false;
2801 /* The unparsed function queue is empty. */
2802 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2804 /* There are no classes being defined. */
2805 parser->num_classes_being_defined = 0;
2807 /* No template parameters apply. */
2808 parser->num_template_parameter_lists = 0;
2810 return parser;
2813 /* Create a cp_lexer structure which will emit the tokens in CACHE
2814 and push it onto the parser's lexer stack. This is used for delayed
2815 parsing of in-class method bodies and default arguments, and should
2816 not be confused with tentative parsing. */
2817 static void
2818 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2820 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2821 lexer->next = parser->lexer;
2822 parser->lexer = lexer;
2824 /* Move the current source position to that of the first token in the
2825 new lexer. */
2826 cp_lexer_set_source_position_from_token (lexer->next_token);
2829 /* Pop the top lexer off the parser stack. This is never used for the
2830 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2831 static void
2832 cp_parser_pop_lexer (cp_parser *parser)
2834 cp_lexer *lexer = parser->lexer;
2835 parser->lexer = lexer->next;
2836 cp_lexer_destroy (lexer);
2838 /* Put the current source position back where it was before this
2839 lexer was pushed. */
2840 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2843 /* Lexical conventions [gram.lex] */
2845 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2846 identifier. */
2848 static tree
2849 cp_parser_identifier (cp_parser* parser)
2851 cp_token *token;
2853 /* Look for the identifier. */
2854 token = cp_parser_require (parser, CPP_NAME, "identifier");
2855 /* Return the value. */
2856 return token ? token->u.value : error_mark_node;
2859 /* Parse a sequence of adjacent string constants. Returns a
2860 TREE_STRING representing the combined, nul-terminated string
2861 constant. If TRANSLATE is true, translate the string to the
2862 execution character set. If WIDE_OK is true, a wide string is
2863 invalid here.
2865 C++98 [lex.string] says that if a narrow string literal token is
2866 adjacent to a wide string literal token, the behavior is undefined.
2867 However, C99 6.4.5p4 says that this results in a wide string literal.
2868 We follow C99 here, for consistency with the C front end.
2870 This code is largely lifted from lex_string() in c-lex.c.
2872 FUTURE: ObjC++ will need to handle @-strings here. */
2873 static tree
2874 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2876 tree value;
2877 size_t count;
2878 struct obstack str_ob;
2879 cpp_string str, istr, *strs;
2880 cp_token *tok;
2881 enum cpp_ttype type;
2883 tok = cp_lexer_peek_token (parser->lexer);
2884 if (!cp_parser_is_string_literal (tok))
2886 cp_parser_error (parser, "expected string-literal");
2887 return error_mark_node;
2890 type = tok->type;
2892 /* Try to avoid the overhead of creating and destroying an obstack
2893 for the common case of just one string. */
2894 if (!cp_parser_is_string_literal
2895 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2897 cp_lexer_consume_token (parser->lexer);
2899 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2900 str.len = TREE_STRING_LENGTH (tok->u.value);
2901 count = 1;
2903 strs = &str;
2905 else
2907 gcc_obstack_init (&str_ob);
2908 count = 0;
2912 cp_lexer_consume_token (parser->lexer);
2913 count++;
2914 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2915 str.len = TREE_STRING_LENGTH (tok->u.value);
2917 if (type != tok->type)
2919 if (type == CPP_STRING)
2920 type = tok->type;
2921 else if (tok->type != CPP_STRING)
2922 error ("unsupported non-standard concatenation of string literals");
2925 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2927 tok = cp_lexer_peek_token (parser->lexer);
2929 while (cp_parser_is_string_literal (tok));
2931 strs = (cpp_string *) obstack_finish (&str_ob);
2934 if (type != CPP_STRING && !wide_ok)
2936 cp_parser_error (parser, "a wide string is invalid in this context");
2937 type = CPP_STRING;
2940 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2941 (parse_in, strs, count, &istr, type))
2943 value = build_string (istr.len, (const char *)istr.text);
2944 free (CONST_CAST (unsigned char *, istr.text));
2946 switch (type)
2948 default:
2949 case CPP_STRING:
2950 TREE_TYPE (value) = char_array_type_node;
2951 break;
2952 case CPP_STRING16:
2953 TREE_TYPE (value) = char16_array_type_node;
2954 break;
2955 case CPP_STRING32:
2956 TREE_TYPE (value) = char32_array_type_node;
2957 break;
2958 case CPP_WSTRING:
2959 TREE_TYPE (value) = wchar_array_type_node;
2960 break;
2963 value = fix_string_type (value);
2965 else
2966 /* cpp_interpret_string has issued an error. */
2967 value = error_mark_node;
2969 if (count > 1)
2970 obstack_free (&str_ob, 0);
2972 return value;
2976 /* Basic concepts [gram.basic] */
2978 /* Parse a translation-unit.
2980 translation-unit:
2981 declaration-seq [opt]
2983 Returns TRUE if all went well. */
2985 static bool
2986 cp_parser_translation_unit (cp_parser* parser)
2988 /* The address of the first non-permanent object on the declarator
2989 obstack. */
2990 static void *declarator_obstack_base;
2992 bool success;
2994 /* Create the declarator obstack, if necessary. */
2995 if (!cp_error_declarator)
2997 gcc_obstack_init (&declarator_obstack);
2998 /* Create the error declarator. */
2999 cp_error_declarator = make_declarator (cdk_error);
3000 /* Create the empty parameter list. */
3001 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3002 /* Remember where the base of the declarator obstack lies. */
3003 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3006 cp_parser_declaration_seq_opt (parser);
3008 /* If there are no tokens left then all went well. */
3009 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3011 /* Get rid of the token array; we don't need it any more. */
3012 cp_lexer_destroy (parser->lexer);
3013 parser->lexer = NULL;
3015 /* This file might have been a context that's implicitly extern
3016 "C". If so, pop the lang context. (Only relevant for PCH.) */
3017 if (parser->implicit_extern_c)
3019 pop_lang_context ();
3020 parser->implicit_extern_c = false;
3023 /* Finish up. */
3024 finish_translation_unit ();
3026 success = true;
3028 else
3030 cp_parser_error (parser, "expected declaration");
3031 success = false;
3034 /* Make sure the declarator obstack was fully cleaned up. */
3035 gcc_assert (obstack_next_free (&declarator_obstack)
3036 == declarator_obstack_base);
3038 /* All went well. */
3039 return success;
3042 /* Expressions [gram.expr] */
3044 /* Parse a primary-expression.
3046 primary-expression:
3047 literal
3048 this
3049 ( expression )
3050 id-expression
3052 GNU Extensions:
3054 primary-expression:
3055 ( compound-statement )
3056 __builtin_va_arg ( assignment-expression , type-id )
3057 __builtin_offsetof ( type-id , offsetof-expression )
3059 C++ Extensions:
3060 __has_nothrow_assign ( type-id )
3061 __has_nothrow_constructor ( type-id )
3062 __has_nothrow_copy ( type-id )
3063 __has_trivial_assign ( type-id )
3064 __has_trivial_constructor ( type-id )
3065 __has_trivial_copy ( type-id )
3066 __has_trivial_destructor ( type-id )
3067 __has_virtual_destructor ( type-id )
3068 __is_abstract ( type-id )
3069 __is_base_of ( type-id , type-id )
3070 __is_class ( type-id )
3071 __is_convertible_to ( type-id , type-id )
3072 __is_empty ( type-id )
3073 __is_enum ( type-id )
3074 __is_pod ( type-id )
3075 __is_polymorphic ( type-id )
3076 __is_union ( type-id )
3078 Objective-C++ Extension:
3080 primary-expression:
3081 objc-expression
3083 literal:
3084 __null
3086 ADDRESS_P is true iff this expression was immediately preceded by
3087 "&" and therefore might denote a pointer-to-member. CAST_P is true
3088 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3089 true iff this expression is a template argument.
3091 Returns a representation of the expression. Upon return, *IDK
3092 indicates what kind of id-expression (if any) was present. */
3094 static tree
3095 cp_parser_primary_expression (cp_parser *parser,
3096 bool address_p,
3097 bool cast_p,
3098 bool template_arg_p,
3099 cp_id_kind *idk)
3101 cp_token *token;
3103 /* Assume the primary expression is not an id-expression. */
3104 *idk = CP_ID_KIND_NONE;
3106 /* Peek at the next token. */
3107 token = cp_lexer_peek_token (parser->lexer);
3108 switch (token->type)
3110 /* literal:
3111 integer-literal
3112 character-literal
3113 floating-literal
3114 string-literal
3115 boolean-literal */
3116 case CPP_CHAR:
3117 case CPP_CHAR16:
3118 case CPP_CHAR32:
3119 case CPP_WCHAR:
3120 case CPP_NUMBER:
3121 token = cp_lexer_consume_token (parser->lexer);
3122 /* Floating-point literals are only allowed in an integral
3123 constant expression if they are cast to an integral or
3124 enumeration type. */
3125 if (TREE_CODE (token->u.value) == REAL_CST
3126 && parser->integral_constant_expression_p
3127 && pedantic)
3129 /* CAST_P will be set even in invalid code like "int(2.7 +
3130 ...)". Therefore, we have to check that the next token
3131 is sure to end the cast. */
3132 if (cast_p)
3134 cp_token *next_token;
3136 next_token = cp_lexer_peek_token (parser->lexer);
3137 if (/* The comma at the end of an
3138 enumerator-definition. */
3139 next_token->type != CPP_COMMA
3140 /* The curly brace at the end of an enum-specifier. */
3141 && next_token->type != CPP_CLOSE_BRACE
3142 /* The end of a statement. */
3143 && next_token->type != CPP_SEMICOLON
3144 /* The end of the cast-expression. */
3145 && next_token->type != CPP_CLOSE_PAREN
3146 /* The end of an array bound. */
3147 && next_token->type != CPP_CLOSE_SQUARE
3148 /* The closing ">" in a template-argument-list. */
3149 && (next_token->type != CPP_GREATER
3150 || parser->greater_than_is_operator_p)
3151 /* C++0x only: A ">>" treated like two ">" tokens,
3152 in a template-argument-list. */
3153 && (next_token->type != CPP_RSHIFT
3154 || (cxx_dialect == cxx98)
3155 || parser->greater_than_is_operator_p))
3156 cast_p = false;
3159 /* If we are within a cast, then the constraint that the
3160 cast is to an integral or enumeration type will be
3161 checked at that point. If we are not within a cast, then
3162 this code is invalid. */
3163 if (!cast_p)
3164 cp_parser_non_integral_constant_expression
3165 (parser, "floating-point literal");
3167 return token->u.value;
3169 case CPP_STRING:
3170 case CPP_STRING16:
3171 case CPP_STRING32:
3172 case CPP_WSTRING:
3173 /* ??? Should wide strings be allowed when parser->translate_strings_p
3174 is false (i.e. in attributes)? If not, we can kill the third
3175 argument to cp_parser_string_literal. */
3176 return cp_parser_string_literal (parser,
3177 parser->translate_strings_p,
3178 true);
3180 case CPP_OPEN_PAREN:
3182 tree expr;
3183 bool saved_greater_than_is_operator_p;
3185 /* Consume the `('. */
3186 cp_lexer_consume_token (parser->lexer);
3187 /* Within a parenthesized expression, a `>' token is always
3188 the greater-than operator. */
3189 saved_greater_than_is_operator_p
3190 = parser->greater_than_is_operator_p;
3191 parser->greater_than_is_operator_p = true;
3192 /* If we see `( { ' then we are looking at the beginning of
3193 a GNU statement-expression. */
3194 if (cp_parser_allow_gnu_extensions_p (parser)
3195 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3197 /* Statement-expressions are not allowed by the standard. */
3198 if (pedantic)
3199 pedwarn ("ISO C++ forbids braced-groups within expressions");
3201 /* And they're not allowed outside of a function-body; you
3202 cannot, for example, write:
3204 int i = ({ int j = 3; j + 1; });
3206 at class or namespace scope. */
3207 if (!parser->in_function_body
3208 || parser->in_template_argument_list_p)
3210 error ("statement-expressions are not allowed outside "
3211 "functions nor in template-argument lists");
3212 cp_parser_skip_to_end_of_block_or_statement (parser);
3213 expr = error_mark_node;
3215 else
3217 /* Start the statement-expression. */
3218 expr = begin_stmt_expr ();
3219 /* Parse the compound-statement. */
3220 cp_parser_compound_statement (parser, expr, false);
3221 /* Finish up. */
3222 expr = finish_stmt_expr (expr, false);
3225 else
3227 /* Parse the parenthesized expression. */
3228 expr = cp_parser_expression (parser, cast_p);
3229 /* Let the front end know that this expression was
3230 enclosed in parentheses. This matters in case, for
3231 example, the expression is of the form `A::B', since
3232 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3233 not. */
3234 finish_parenthesized_expr (expr);
3236 /* The `>' token might be the end of a template-id or
3237 template-parameter-list now. */
3238 parser->greater_than_is_operator_p
3239 = saved_greater_than_is_operator_p;
3240 /* Consume the `)'. */
3241 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3242 cp_parser_skip_to_end_of_statement (parser);
3244 return expr;
3247 case CPP_KEYWORD:
3248 switch (token->keyword)
3250 /* These two are the boolean literals. */
3251 case RID_TRUE:
3252 cp_lexer_consume_token (parser->lexer);
3253 return boolean_true_node;
3254 case RID_FALSE:
3255 cp_lexer_consume_token (parser->lexer);
3256 return boolean_false_node;
3258 /* The `__null' literal. */
3259 case RID_NULL:
3260 cp_lexer_consume_token (parser->lexer);
3261 return null_node;
3263 /* Recognize the `this' keyword. */
3264 case RID_THIS:
3265 cp_lexer_consume_token (parser->lexer);
3266 if (parser->local_variables_forbidden_p)
3268 error ("%<this%> may not be used in this context");
3269 return error_mark_node;
3271 /* Pointers cannot appear in constant-expressions. */
3272 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3273 return error_mark_node;
3274 return finish_this_expr ();
3276 /* The `operator' keyword can be the beginning of an
3277 id-expression. */
3278 case RID_OPERATOR:
3279 goto id_expression;
3281 case RID_FUNCTION_NAME:
3282 case RID_PRETTY_FUNCTION_NAME:
3283 case RID_C99_FUNCTION_NAME:
3284 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3285 __func__ are the names of variables -- but they are
3286 treated specially. Therefore, they are handled here,
3287 rather than relying on the generic id-expression logic
3288 below. Grammatically, these names are id-expressions.
3290 Consume the token. */
3291 token = cp_lexer_consume_token (parser->lexer);
3292 /* Look up the name. */
3293 return finish_fname (token->u.value);
3295 case RID_VA_ARG:
3297 tree expression;
3298 tree type;
3300 /* The `__builtin_va_arg' construct is used to handle
3301 `va_arg'. Consume the `__builtin_va_arg' token. */
3302 cp_lexer_consume_token (parser->lexer);
3303 /* Look for the opening `('. */
3304 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3305 /* Now, parse the assignment-expression. */
3306 expression = cp_parser_assignment_expression (parser,
3307 /*cast_p=*/false);
3308 /* Look for the `,'. */
3309 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3310 /* Parse the type-id. */
3311 type = cp_parser_type_id (parser);
3312 /* Look for the closing `)'. */
3313 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3314 /* Using `va_arg' in a constant-expression is not
3315 allowed. */
3316 if (cp_parser_non_integral_constant_expression (parser,
3317 "%<va_arg%>"))
3318 return error_mark_node;
3319 return build_x_va_arg (expression, type);
3322 case RID_OFFSETOF:
3323 return cp_parser_builtin_offsetof (parser);
3325 case RID_HAS_NOTHROW_ASSIGN:
3326 case RID_HAS_NOTHROW_CONSTRUCTOR:
3327 case RID_HAS_NOTHROW_COPY:
3328 case RID_HAS_TRIVIAL_ASSIGN:
3329 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3330 case RID_HAS_TRIVIAL_COPY:
3331 case RID_HAS_TRIVIAL_DESTRUCTOR:
3332 case RID_HAS_VIRTUAL_DESTRUCTOR:
3333 case RID_IS_ABSTRACT:
3334 case RID_IS_BASE_OF:
3335 case RID_IS_CLASS:
3336 case RID_IS_CONVERTIBLE_TO:
3337 case RID_IS_EMPTY:
3338 case RID_IS_ENUM:
3339 case RID_IS_POD:
3340 case RID_IS_POLYMORPHIC:
3341 case RID_IS_UNION:
3342 return cp_parser_trait_expr (parser, token->keyword);
3344 /* Objective-C++ expressions. */
3345 case RID_AT_ENCODE:
3346 case RID_AT_PROTOCOL:
3347 case RID_AT_SELECTOR:
3348 return cp_parser_objc_expression (parser);
3350 default:
3351 cp_parser_error (parser, "expected primary-expression");
3352 return error_mark_node;
3355 /* An id-expression can start with either an identifier, a
3356 `::' as the beginning of a qualified-id, or the "operator"
3357 keyword. */
3358 case CPP_NAME:
3359 case CPP_SCOPE:
3360 case CPP_TEMPLATE_ID:
3361 case CPP_NESTED_NAME_SPECIFIER:
3363 tree id_expression;
3364 tree decl;
3365 const char *error_msg;
3366 bool template_p;
3367 bool done;
3369 id_expression:
3370 /* Parse the id-expression. */
3371 id_expression
3372 = cp_parser_id_expression (parser,
3373 /*template_keyword_p=*/false,
3374 /*check_dependency_p=*/true,
3375 &template_p,
3376 /*declarator_p=*/false,
3377 /*optional_p=*/false);
3378 if (id_expression == error_mark_node)
3379 return error_mark_node;
3380 token = cp_lexer_peek_token (parser->lexer);
3381 done = (token->type != CPP_OPEN_SQUARE
3382 && token->type != CPP_OPEN_PAREN
3383 && token->type != CPP_DOT
3384 && token->type != CPP_DEREF
3385 && token->type != CPP_PLUS_PLUS
3386 && token->type != CPP_MINUS_MINUS);
3387 /* If we have a template-id, then no further lookup is
3388 required. If the template-id was for a template-class, we
3389 will sometimes have a TYPE_DECL at this point. */
3390 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3391 || TREE_CODE (id_expression) == TYPE_DECL)
3392 decl = id_expression;
3393 /* Look up the name. */
3394 else
3396 tree ambiguous_decls;
3398 decl = cp_parser_lookup_name (parser, id_expression,
3399 none_type,
3400 template_p,
3401 /*is_namespace=*/false,
3402 /*check_dependency=*/true,
3403 &ambiguous_decls);
3404 /* If the lookup was ambiguous, an error will already have
3405 been issued. */
3406 if (ambiguous_decls)
3407 return error_mark_node;
3409 /* In Objective-C++, an instance variable (ivar) may be preferred
3410 to whatever cp_parser_lookup_name() found. */
3411 decl = objc_lookup_ivar (decl, id_expression);
3413 /* If name lookup gives us a SCOPE_REF, then the
3414 qualifying scope was dependent. */
3415 if (TREE_CODE (decl) == SCOPE_REF)
3417 /* At this point, we do not know if DECL is a valid
3418 integral constant expression. We assume that it is
3419 in fact such an expression, so that code like:
3421 template <int N> struct A {
3422 int a[B<N>::i];
3425 is accepted. At template-instantiation time, we
3426 will check that B<N>::i is actually a constant. */
3427 return decl;
3429 /* Check to see if DECL is a local variable in a context
3430 where that is forbidden. */
3431 if (parser->local_variables_forbidden_p
3432 && local_variable_p (decl))
3434 /* It might be that we only found DECL because we are
3435 trying to be generous with pre-ISO scoping rules.
3436 For example, consider:
3438 int i;
3439 void g() {
3440 for (int i = 0; i < 10; ++i) {}
3441 extern void f(int j = i);
3444 Here, name look up will originally find the out
3445 of scope `i'. We need to issue a warning message,
3446 but then use the global `i'. */
3447 decl = check_for_out_of_scope_variable (decl);
3448 if (local_variable_p (decl))
3450 error ("local variable %qD may not appear in this context",
3451 decl);
3452 return error_mark_node;
3457 decl = (finish_id_expression
3458 (id_expression, decl, parser->scope,
3459 idk,
3460 parser->integral_constant_expression_p,
3461 parser->allow_non_integral_constant_expression_p,
3462 &parser->non_integral_constant_expression_p,
3463 template_p, done, address_p,
3464 template_arg_p,
3465 &error_msg));
3466 if (error_msg)
3467 cp_parser_error (parser, error_msg);
3468 return decl;
3471 /* Anything else is an error. */
3472 default:
3473 /* ...unless we have an Objective-C++ message or string literal,
3474 that is. */
3475 if (c_dialect_objc ()
3476 && (token->type == CPP_OPEN_SQUARE
3477 || token->type == CPP_OBJC_STRING))
3478 return cp_parser_objc_expression (parser);
3480 cp_parser_error (parser, "expected primary-expression");
3481 return error_mark_node;
3485 /* Parse an id-expression.
3487 id-expression:
3488 unqualified-id
3489 qualified-id
3491 qualified-id:
3492 :: [opt] nested-name-specifier template [opt] unqualified-id
3493 :: identifier
3494 :: operator-function-id
3495 :: template-id
3497 Return a representation of the unqualified portion of the
3498 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3499 a `::' or nested-name-specifier.
3501 Often, if the id-expression was a qualified-id, the caller will
3502 want to make a SCOPE_REF to represent the qualified-id. This
3503 function does not do this in order to avoid wastefully creating
3504 SCOPE_REFs when they are not required.
3506 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3507 `template' keyword.
3509 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3510 uninstantiated templates.
3512 If *TEMPLATE_P is non-NULL, it is set to true iff the
3513 `template' keyword is used to explicitly indicate that the entity
3514 named is a template.
3516 If DECLARATOR_P is true, the id-expression is appearing as part of
3517 a declarator, rather than as part of an expression. */
3519 static tree
3520 cp_parser_id_expression (cp_parser *parser,
3521 bool template_keyword_p,
3522 bool check_dependency_p,
3523 bool *template_p,
3524 bool declarator_p,
3525 bool optional_p)
3527 bool global_scope_p;
3528 bool nested_name_specifier_p;
3530 /* Assume the `template' keyword was not used. */
3531 if (template_p)
3532 *template_p = template_keyword_p;
3534 /* Look for the optional `::' operator. */
3535 global_scope_p
3536 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3537 != NULL_TREE);
3538 /* Look for the optional nested-name-specifier. */
3539 nested_name_specifier_p
3540 = (cp_parser_nested_name_specifier_opt (parser,
3541 /*typename_keyword_p=*/false,
3542 check_dependency_p,
3543 /*type_p=*/false,
3544 declarator_p)
3545 != NULL_TREE);
3546 /* If there is a nested-name-specifier, then we are looking at
3547 the first qualified-id production. */
3548 if (nested_name_specifier_p)
3550 tree saved_scope;
3551 tree saved_object_scope;
3552 tree saved_qualifying_scope;
3553 tree unqualified_id;
3554 bool is_template;
3556 /* See if the next token is the `template' keyword. */
3557 if (!template_p)
3558 template_p = &is_template;
3559 *template_p = cp_parser_optional_template_keyword (parser);
3560 /* Name lookup we do during the processing of the
3561 unqualified-id might obliterate SCOPE. */
3562 saved_scope = parser->scope;
3563 saved_object_scope = parser->object_scope;
3564 saved_qualifying_scope = parser->qualifying_scope;
3565 /* Process the final unqualified-id. */
3566 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3567 check_dependency_p,
3568 declarator_p,
3569 /*optional_p=*/false);
3570 /* Restore the SAVED_SCOPE for our caller. */
3571 parser->scope = saved_scope;
3572 parser->object_scope = saved_object_scope;
3573 parser->qualifying_scope = saved_qualifying_scope;
3575 return unqualified_id;
3577 /* Otherwise, if we are in global scope, then we are looking at one
3578 of the other qualified-id productions. */
3579 else if (global_scope_p)
3581 cp_token *token;
3582 tree id;
3584 /* Peek at the next token. */
3585 token = cp_lexer_peek_token (parser->lexer);
3587 /* If it's an identifier, and the next token is not a "<", then
3588 we can avoid the template-id case. This is an optimization
3589 for this common case. */
3590 if (token->type == CPP_NAME
3591 && !cp_parser_nth_token_starts_template_argument_list_p
3592 (parser, 2))
3593 return cp_parser_identifier (parser);
3595 cp_parser_parse_tentatively (parser);
3596 /* Try a template-id. */
3597 id = cp_parser_template_id (parser,
3598 /*template_keyword_p=*/false,
3599 /*check_dependency_p=*/true,
3600 declarator_p);
3601 /* If that worked, we're done. */
3602 if (cp_parser_parse_definitely (parser))
3603 return id;
3605 /* Peek at the next token. (Changes in the token buffer may
3606 have invalidated the pointer obtained above.) */
3607 token = cp_lexer_peek_token (parser->lexer);
3609 switch (token->type)
3611 case CPP_NAME:
3612 return cp_parser_identifier (parser);
3614 case CPP_KEYWORD:
3615 if (token->keyword == RID_OPERATOR)
3616 return cp_parser_operator_function_id (parser);
3617 /* Fall through. */
3619 default:
3620 cp_parser_error (parser, "expected id-expression");
3621 return error_mark_node;
3624 else
3625 return cp_parser_unqualified_id (parser, template_keyword_p,
3626 /*check_dependency_p=*/true,
3627 declarator_p,
3628 optional_p);
3631 /* Parse an unqualified-id.
3633 unqualified-id:
3634 identifier
3635 operator-function-id
3636 conversion-function-id
3637 ~ class-name
3638 template-id
3640 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3641 keyword, in a construct like `A::template ...'.
3643 Returns a representation of unqualified-id. For the `identifier'
3644 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3645 production a BIT_NOT_EXPR is returned; the operand of the
3646 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3647 other productions, see the documentation accompanying the
3648 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3649 names are looked up in uninstantiated templates. If DECLARATOR_P
3650 is true, the unqualified-id is appearing as part of a declarator,
3651 rather than as part of an expression. */
3653 static tree
3654 cp_parser_unqualified_id (cp_parser* parser,
3655 bool template_keyword_p,
3656 bool check_dependency_p,
3657 bool declarator_p,
3658 bool optional_p)
3660 cp_token *token;
3662 /* Peek at the next token. */
3663 token = cp_lexer_peek_token (parser->lexer);
3665 switch (token->type)
3667 case CPP_NAME:
3669 tree id;
3671 /* We don't know yet whether or not this will be a
3672 template-id. */
3673 cp_parser_parse_tentatively (parser);
3674 /* Try a template-id. */
3675 id = cp_parser_template_id (parser, template_keyword_p,
3676 check_dependency_p,
3677 declarator_p);
3678 /* If it worked, we're done. */
3679 if (cp_parser_parse_definitely (parser))
3680 return id;
3681 /* Otherwise, it's an ordinary identifier. */
3682 return cp_parser_identifier (parser);
3685 case CPP_TEMPLATE_ID:
3686 return cp_parser_template_id (parser, template_keyword_p,
3687 check_dependency_p,
3688 declarator_p);
3690 case CPP_COMPL:
3692 tree type_decl;
3693 tree qualifying_scope;
3694 tree object_scope;
3695 tree scope;
3696 bool done;
3698 /* Consume the `~' token. */
3699 cp_lexer_consume_token (parser->lexer);
3700 /* Parse the class-name. The standard, as written, seems to
3701 say that:
3703 template <typename T> struct S { ~S (); };
3704 template <typename T> S<T>::~S() {}
3706 is invalid, since `~' must be followed by a class-name, but
3707 `S<T>' is dependent, and so not known to be a class.
3708 That's not right; we need to look in uninstantiated
3709 templates. A further complication arises from:
3711 template <typename T> void f(T t) {
3712 t.T::~T();
3715 Here, it is not possible to look up `T' in the scope of `T'
3716 itself. We must look in both the current scope, and the
3717 scope of the containing complete expression.
3719 Yet another issue is:
3721 struct S {
3722 int S;
3723 ~S();
3726 S::~S() {}
3728 The standard does not seem to say that the `S' in `~S'
3729 should refer to the type `S' and not the data member
3730 `S::S'. */
3732 /* DR 244 says that we look up the name after the "~" in the
3733 same scope as we looked up the qualifying name. That idea
3734 isn't fully worked out; it's more complicated than that. */
3735 scope = parser->scope;
3736 object_scope = parser->object_scope;
3737 qualifying_scope = parser->qualifying_scope;
3739 /* Check for invalid scopes. */
3740 if (scope == error_mark_node)
3742 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3743 cp_lexer_consume_token (parser->lexer);
3744 return error_mark_node;
3746 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3748 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3749 error ("scope %qT before %<~%> is not a class-name", scope);
3750 cp_parser_simulate_error (parser);
3751 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3752 cp_lexer_consume_token (parser->lexer);
3753 return error_mark_node;
3755 gcc_assert (!scope || TYPE_P (scope));
3757 /* If the name is of the form "X::~X" it's OK. */
3758 token = cp_lexer_peek_token (parser->lexer);
3759 if (scope
3760 && token->type == CPP_NAME
3761 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3762 == CPP_OPEN_PAREN)
3763 && constructor_name_p (token->u.value, scope))
3765 cp_lexer_consume_token (parser->lexer);
3766 return build_nt (BIT_NOT_EXPR, scope);
3769 /* If there was an explicit qualification (S::~T), first look
3770 in the scope given by the qualification (i.e., S). */
3771 done = false;
3772 type_decl = NULL_TREE;
3773 if (scope)
3775 cp_parser_parse_tentatively (parser);
3776 type_decl = cp_parser_class_name (parser,
3777 /*typename_keyword_p=*/false,
3778 /*template_keyword_p=*/false,
3779 none_type,
3780 /*check_dependency=*/false,
3781 /*class_head_p=*/false,
3782 declarator_p);
3783 if (cp_parser_parse_definitely (parser))
3784 done = true;
3786 /* In "N::S::~S", look in "N" as well. */
3787 if (!done && scope && qualifying_scope)
3789 cp_parser_parse_tentatively (parser);
3790 parser->scope = qualifying_scope;
3791 parser->object_scope = NULL_TREE;
3792 parser->qualifying_scope = NULL_TREE;
3793 type_decl
3794 = cp_parser_class_name (parser,
3795 /*typename_keyword_p=*/false,
3796 /*template_keyword_p=*/false,
3797 none_type,
3798 /*check_dependency=*/false,
3799 /*class_head_p=*/false,
3800 declarator_p);
3801 if (cp_parser_parse_definitely (parser))
3802 done = true;
3804 /* In "p->S::~T", look in the scope given by "*p" as well. */
3805 else if (!done && object_scope)
3807 cp_parser_parse_tentatively (parser);
3808 parser->scope = object_scope;
3809 parser->object_scope = NULL_TREE;
3810 parser->qualifying_scope = NULL_TREE;
3811 type_decl
3812 = cp_parser_class_name (parser,
3813 /*typename_keyword_p=*/false,
3814 /*template_keyword_p=*/false,
3815 none_type,
3816 /*check_dependency=*/false,
3817 /*class_head_p=*/false,
3818 declarator_p);
3819 if (cp_parser_parse_definitely (parser))
3820 done = true;
3822 /* Look in the surrounding context. */
3823 if (!done)
3825 parser->scope = NULL_TREE;
3826 parser->object_scope = NULL_TREE;
3827 parser->qualifying_scope = NULL_TREE;
3828 type_decl
3829 = cp_parser_class_name (parser,
3830 /*typename_keyword_p=*/false,
3831 /*template_keyword_p=*/false,
3832 none_type,
3833 /*check_dependency=*/false,
3834 /*class_head_p=*/false,
3835 declarator_p);
3837 /* If an error occurred, assume that the name of the
3838 destructor is the same as the name of the qualifying
3839 class. That allows us to keep parsing after running
3840 into ill-formed destructor names. */
3841 if (type_decl == error_mark_node && scope)
3842 return build_nt (BIT_NOT_EXPR, scope);
3843 else if (type_decl == error_mark_node)
3844 return error_mark_node;
3846 /* Check that destructor name and scope match. */
3847 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3849 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3850 error ("declaration of %<~%T%> as member of %qT",
3851 type_decl, scope);
3852 cp_parser_simulate_error (parser);
3853 return error_mark_node;
3856 /* [class.dtor]
3858 A typedef-name that names a class shall not be used as the
3859 identifier in the declarator for a destructor declaration. */
3860 if (declarator_p
3861 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3862 && !DECL_SELF_REFERENCE_P (type_decl)
3863 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3864 error ("typedef-name %qD used as destructor declarator",
3865 type_decl);
3867 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3870 case CPP_KEYWORD:
3871 if (token->keyword == RID_OPERATOR)
3873 tree id;
3875 /* This could be a template-id, so we try that first. */
3876 cp_parser_parse_tentatively (parser);
3877 /* Try a template-id. */
3878 id = cp_parser_template_id (parser, template_keyword_p,
3879 /*check_dependency_p=*/true,
3880 declarator_p);
3881 /* If that worked, we're done. */
3882 if (cp_parser_parse_definitely (parser))
3883 return id;
3884 /* We still don't know whether we're looking at an
3885 operator-function-id or a conversion-function-id. */
3886 cp_parser_parse_tentatively (parser);
3887 /* Try an operator-function-id. */
3888 id = cp_parser_operator_function_id (parser);
3889 /* If that didn't work, try a conversion-function-id. */
3890 if (!cp_parser_parse_definitely (parser))
3891 id = cp_parser_conversion_function_id (parser);
3893 return id;
3895 /* Fall through. */
3897 default:
3898 if (optional_p)
3899 return NULL_TREE;
3900 cp_parser_error (parser, "expected unqualified-id");
3901 return error_mark_node;
3905 /* Parse an (optional) nested-name-specifier.
3907 nested-name-specifier:
3908 class-or-namespace-name :: nested-name-specifier [opt]
3909 class-or-namespace-name :: template nested-name-specifier [opt]
3911 PARSER->SCOPE should be set appropriately before this function is
3912 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3913 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3914 in name lookups.
3916 Sets PARSER->SCOPE to the class (TYPE) or namespace
3917 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3918 it unchanged if there is no nested-name-specifier. Returns the new
3919 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3921 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3922 part of a declaration and/or decl-specifier. */
3924 static tree
3925 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3926 bool typename_keyword_p,
3927 bool check_dependency_p,
3928 bool type_p,
3929 bool is_declaration)
3931 bool success = false;
3932 cp_token_position start = 0;
3933 cp_token *token;
3935 /* Remember where the nested-name-specifier starts. */
3936 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3938 start = cp_lexer_token_position (parser->lexer, false);
3939 push_deferring_access_checks (dk_deferred);
3942 while (true)
3944 tree new_scope;
3945 tree old_scope;
3946 tree saved_qualifying_scope;
3947 bool template_keyword_p;
3949 /* Spot cases that cannot be the beginning of a
3950 nested-name-specifier. */
3951 token = cp_lexer_peek_token (parser->lexer);
3953 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3954 the already parsed nested-name-specifier. */
3955 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3957 /* Grab the nested-name-specifier and continue the loop. */
3958 cp_parser_pre_parsed_nested_name_specifier (parser);
3959 /* If we originally encountered this nested-name-specifier
3960 with IS_DECLARATION set to false, we will not have
3961 resolved TYPENAME_TYPEs, so we must do so here. */
3962 if (is_declaration
3963 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3965 new_scope = resolve_typename_type (parser->scope,
3966 /*only_current_p=*/false);
3967 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3968 parser->scope = new_scope;
3970 success = true;
3971 continue;
3974 /* Spot cases that cannot be the beginning of a
3975 nested-name-specifier. On the second and subsequent times
3976 through the loop, we look for the `template' keyword. */
3977 if (success && token->keyword == RID_TEMPLATE)
3979 /* A template-id can start a nested-name-specifier. */
3980 else if (token->type == CPP_TEMPLATE_ID)
3982 else
3984 /* If the next token is not an identifier, then it is
3985 definitely not a class-or-namespace-name. */
3986 if (token->type != CPP_NAME)
3987 break;
3988 /* If the following token is neither a `<' (to begin a
3989 template-id), nor a `::', then we are not looking at a
3990 nested-name-specifier. */
3991 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3992 if (token->type != CPP_SCOPE
3993 && !cp_parser_nth_token_starts_template_argument_list_p
3994 (parser, 2))
3995 break;
3998 /* The nested-name-specifier is optional, so we parse
3999 tentatively. */
4000 cp_parser_parse_tentatively (parser);
4002 /* Look for the optional `template' keyword, if this isn't the
4003 first time through the loop. */
4004 if (success)
4005 template_keyword_p = cp_parser_optional_template_keyword (parser);
4006 else
4007 template_keyword_p = false;
4009 /* Save the old scope since the name lookup we are about to do
4010 might destroy it. */
4011 old_scope = parser->scope;
4012 saved_qualifying_scope = parser->qualifying_scope;
4013 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4014 look up names in "X<T>::I" in order to determine that "Y" is
4015 a template. So, if we have a typename at this point, we make
4016 an effort to look through it. */
4017 if (is_declaration
4018 && !typename_keyword_p
4019 && parser->scope
4020 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4021 parser->scope = resolve_typename_type (parser->scope,
4022 /*only_current_p=*/false);
4023 /* Parse the qualifying entity. */
4024 new_scope
4025 = cp_parser_class_or_namespace_name (parser,
4026 typename_keyword_p,
4027 template_keyword_p,
4028 check_dependency_p,
4029 type_p,
4030 is_declaration);
4031 /* Look for the `::' token. */
4032 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4034 /* If we found what we wanted, we keep going; otherwise, we're
4035 done. */
4036 if (!cp_parser_parse_definitely (parser))
4038 bool error_p = false;
4040 /* Restore the OLD_SCOPE since it was valid before the
4041 failed attempt at finding the last
4042 class-or-namespace-name. */
4043 parser->scope = old_scope;
4044 parser->qualifying_scope = saved_qualifying_scope;
4045 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4046 break;
4047 /* If the next token is an identifier, and the one after
4048 that is a `::', then any valid interpretation would have
4049 found a class-or-namespace-name. */
4050 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4051 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4052 == CPP_SCOPE)
4053 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4054 != CPP_COMPL))
4056 token = cp_lexer_consume_token (parser->lexer);
4057 if (!error_p)
4059 if (!token->ambiguous_p)
4061 tree decl;
4062 tree ambiguous_decls;
4064 decl = cp_parser_lookup_name (parser, token->u.value,
4065 none_type,
4066 /*is_template=*/false,
4067 /*is_namespace=*/false,
4068 /*check_dependency=*/true,
4069 &ambiguous_decls);
4070 if (TREE_CODE (decl) == TEMPLATE_DECL)
4071 error ("%qD used without template parameters", decl);
4072 else if (ambiguous_decls)
4074 error ("reference to %qD is ambiguous",
4075 token->u.value);
4076 print_candidates (ambiguous_decls);
4077 decl = error_mark_node;
4079 else
4080 cp_parser_name_lookup_error
4081 (parser, token->u.value, decl,
4082 "is not a class or namespace");
4084 parser->scope = error_mark_node;
4085 error_p = true;
4086 /* Treat this as a successful nested-name-specifier
4087 due to:
4089 [basic.lookup.qual]
4091 If the name found is not a class-name (clause
4092 _class_) or namespace-name (_namespace.def_), the
4093 program is ill-formed. */
4094 success = true;
4096 cp_lexer_consume_token (parser->lexer);
4098 break;
4100 /* We've found one valid nested-name-specifier. */
4101 success = true;
4102 /* Name lookup always gives us a DECL. */
4103 if (TREE_CODE (new_scope) == TYPE_DECL)
4104 new_scope = TREE_TYPE (new_scope);
4105 /* Uses of "template" must be followed by actual templates. */
4106 if (template_keyword_p
4107 && !(CLASS_TYPE_P (new_scope)
4108 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4109 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4110 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4111 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4112 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4113 == TEMPLATE_ID_EXPR)))
4114 pedwarn (TYPE_P (new_scope)
4115 ? "%qT is not a template"
4116 : "%qD is not a template",
4117 new_scope);
4118 /* If it is a class scope, try to complete it; we are about to
4119 be looking up names inside the class. */
4120 if (TYPE_P (new_scope)
4121 /* Since checking types for dependency can be expensive,
4122 avoid doing it if the type is already complete. */
4123 && !COMPLETE_TYPE_P (new_scope)
4124 /* Do not try to complete dependent types. */
4125 && !dependent_type_p (new_scope))
4127 new_scope = complete_type (new_scope);
4128 /* If it is a typedef to current class, use the current
4129 class instead, as the typedef won't have any names inside
4130 it yet. */
4131 if (!COMPLETE_TYPE_P (new_scope)
4132 && currently_open_class (new_scope))
4133 new_scope = TYPE_MAIN_VARIANT (new_scope);
4135 /* Make sure we look in the right scope the next time through
4136 the loop. */
4137 parser->scope = new_scope;
4140 /* If parsing tentatively, replace the sequence of tokens that makes
4141 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4142 token. That way, should we re-parse the token stream, we will
4143 not have to repeat the effort required to do the parse, nor will
4144 we issue duplicate error messages. */
4145 if (success && start)
4147 cp_token *token;
4149 token = cp_lexer_token_at (parser->lexer, start);
4150 /* Reset the contents of the START token. */
4151 token->type = CPP_NESTED_NAME_SPECIFIER;
4152 /* Retrieve any deferred checks. Do not pop this access checks yet
4153 so the memory will not be reclaimed during token replacing below. */
4154 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4155 token->u.tree_check_value->value = parser->scope;
4156 token->u.tree_check_value->checks = get_deferred_access_checks ();
4157 token->u.tree_check_value->qualifying_scope =
4158 parser->qualifying_scope;
4159 token->keyword = RID_MAX;
4161 /* Purge all subsequent tokens. */
4162 cp_lexer_purge_tokens_after (parser->lexer, start);
4165 if (start)
4166 pop_to_parent_deferring_access_checks ();
4168 return success ? parser->scope : NULL_TREE;
4171 /* Parse a nested-name-specifier. See
4172 cp_parser_nested_name_specifier_opt for details. This function
4173 behaves identically, except that it will an issue an error if no
4174 nested-name-specifier is present. */
4176 static tree
4177 cp_parser_nested_name_specifier (cp_parser *parser,
4178 bool typename_keyword_p,
4179 bool check_dependency_p,
4180 bool type_p,
4181 bool is_declaration)
4183 tree scope;
4185 /* Look for the nested-name-specifier. */
4186 scope = cp_parser_nested_name_specifier_opt (parser,
4187 typename_keyword_p,
4188 check_dependency_p,
4189 type_p,
4190 is_declaration);
4191 /* If it was not present, issue an error message. */
4192 if (!scope)
4194 cp_parser_error (parser, "expected nested-name-specifier");
4195 parser->scope = NULL_TREE;
4198 return scope;
4201 /* Parse a class-or-namespace-name.
4203 class-or-namespace-name:
4204 class-name
4205 namespace-name
4207 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4208 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4209 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4210 TYPE_P is TRUE iff the next name should be taken as a class-name,
4211 even the same name is declared to be another entity in the same
4212 scope.
4214 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4215 specified by the class-or-namespace-name. If neither is found the
4216 ERROR_MARK_NODE is returned. */
4218 static tree
4219 cp_parser_class_or_namespace_name (cp_parser *parser,
4220 bool typename_keyword_p,
4221 bool template_keyword_p,
4222 bool check_dependency_p,
4223 bool type_p,
4224 bool is_declaration)
4226 tree saved_scope;
4227 tree saved_qualifying_scope;
4228 tree saved_object_scope;
4229 tree scope;
4230 bool only_class_p;
4232 /* Before we try to parse the class-name, we must save away the
4233 current PARSER->SCOPE since cp_parser_class_name will destroy
4234 it. */
4235 saved_scope = parser->scope;
4236 saved_qualifying_scope = parser->qualifying_scope;
4237 saved_object_scope = parser->object_scope;
4238 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4239 there is no need to look for a namespace-name. */
4240 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4241 if (!only_class_p)
4242 cp_parser_parse_tentatively (parser);
4243 scope = cp_parser_class_name (parser,
4244 typename_keyword_p,
4245 template_keyword_p,
4246 type_p ? class_type : none_type,
4247 check_dependency_p,
4248 /*class_head_p=*/false,
4249 is_declaration);
4250 /* If that didn't work, try for a namespace-name. */
4251 if (!only_class_p && !cp_parser_parse_definitely (parser))
4253 /* Restore the saved scope. */
4254 parser->scope = saved_scope;
4255 parser->qualifying_scope = saved_qualifying_scope;
4256 parser->object_scope = saved_object_scope;
4257 /* If we are not looking at an identifier followed by the scope
4258 resolution operator, then this is not part of a
4259 nested-name-specifier. (Note that this function is only used
4260 to parse the components of a nested-name-specifier.) */
4261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4262 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4263 return error_mark_node;
4264 scope = cp_parser_namespace_name (parser);
4267 return scope;
4270 /* Parse a postfix-expression.
4272 postfix-expression:
4273 primary-expression
4274 postfix-expression [ expression ]
4275 postfix-expression ( expression-list [opt] )
4276 simple-type-specifier ( expression-list [opt] )
4277 typename :: [opt] nested-name-specifier identifier
4278 ( expression-list [opt] )
4279 typename :: [opt] nested-name-specifier template [opt] template-id
4280 ( expression-list [opt] )
4281 postfix-expression . template [opt] id-expression
4282 postfix-expression -> template [opt] id-expression
4283 postfix-expression . pseudo-destructor-name
4284 postfix-expression -> pseudo-destructor-name
4285 postfix-expression ++
4286 postfix-expression --
4287 dynamic_cast < type-id > ( expression )
4288 static_cast < type-id > ( expression )
4289 reinterpret_cast < type-id > ( expression )
4290 const_cast < type-id > ( expression )
4291 typeid ( expression )
4292 typeid ( type-id )
4294 GNU Extension:
4296 postfix-expression:
4297 ( type-id ) { initializer-list , [opt] }
4299 This extension is a GNU version of the C99 compound-literal
4300 construct. (The C99 grammar uses `type-name' instead of `type-id',
4301 but they are essentially the same concept.)
4303 If ADDRESS_P is true, the postfix expression is the operand of the
4304 `&' operator. CAST_P is true if this expression is the target of a
4305 cast.
4307 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4308 class member access expressions [expr.ref].
4310 Returns a representation of the expression. */
4312 static tree
4313 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4314 bool member_access_only_p)
4316 cp_token *token;
4317 enum rid keyword;
4318 cp_id_kind idk = CP_ID_KIND_NONE;
4319 tree postfix_expression = NULL_TREE;
4320 bool is_member_access = false;
4322 /* Peek at the next token. */
4323 token = cp_lexer_peek_token (parser->lexer);
4324 /* Some of the productions are determined by keywords. */
4325 keyword = token->keyword;
4326 switch (keyword)
4328 case RID_DYNCAST:
4329 case RID_STATCAST:
4330 case RID_REINTCAST:
4331 case RID_CONSTCAST:
4333 tree type;
4334 tree expression;
4335 const char *saved_message;
4337 /* All of these can be handled in the same way from the point
4338 of view of parsing. Begin by consuming the token
4339 identifying the cast. */
4340 cp_lexer_consume_token (parser->lexer);
4342 /* New types cannot be defined in the cast. */
4343 saved_message = parser->type_definition_forbidden_message;
4344 parser->type_definition_forbidden_message
4345 = "types may not be defined in casts";
4347 /* Look for the opening `<'. */
4348 cp_parser_require (parser, CPP_LESS, "%<<%>");
4349 /* Parse the type to which we are casting. */
4350 type = cp_parser_type_id (parser);
4351 /* Look for the closing `>'. */
4352 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4353 /* Restore the old message. */
4354 parser->type_definition_forbidden_message = saved_message;
4356 /* And the expression which is being cast. */
4357 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4358 expression = cp_parser_expression (parser, /*cast_p=*/true);
4359 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4361 /* Only type conversions to integral or enumeration types
4362 can be used in constant-expressions. */
4363 if (!cast_valid_in_integral_constant_expression_p (type)
4364 && (cp_parser_non_integral_constant_expression
4365 (parser,
4366 "a cast to a type other than an integral or "
4367 "enumeration type")))
4368 return error_mark_node;
4370 switch (keyword)
4372 case RID_DYNCAST:
4373 postfix_expression
4374 = build_dynamic_cast (type, expression, tf_warning_or_error);
4375 break;
4376 case RID_STATCAST:
4377 postfix_expression
4378 = build_static_cast (type, expression, tf_warning_or_error);
4379 break;
4380 case RID_REINTCAST:
4381 postfix_expression
4382 = build_reinterpret_cast (type, expression,
4383 tf_warning_or_error);
4384 break;
4385 case RID_CONSTCAST:
4386 postfix_expression
4387 = build_const_cast (type, expression, tf_warning_or_error);
4388 break;
4389 default:
4390 gcc_unreachable ();
4393 break;
4395 case RID_TYPEID:
4397 tree type;
4398 const char *saved_message;
4399 bool saved_in_type_id_in_expr_p;
4401 /* Consume the `typeid' token. */
4402 cp_lexer_consume_token (parser->lexer);
4403 /* Look for the `(' token. */
4404 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4405 /* Types cannot be defined in a `typeid' expression. */
4406 saved_message = parser->type_definition_forbidden_message;
4407 parser->type_definition_forbidden_message
4408 = "types may not be defined in a %<typeid%> expression";
4409 /* We can't be sure yet whether we're looking at a type-id or an
4410 expression. */
4411 cp_parser_parse_tentatively (parser);
4412 /* Try a type-id first. */
4413 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4414 parser->in_type_id_in_expr_p = true;
4415 type = cp_parser_type_id (parser);
4416 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4417 /* Look for the `)' token. Otherwise, we can't be sure that
4418 we're not looking at an expression: consider `typeid (int
4419 (3))', for example. */
4420 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4421 /* If all went well, simply lookup the type-id. */
4422 if (cp_parser_parse_definitely (parser))
4423 postfix_expression = get_typeid (type);
4424 /* Otherwise, fall back to the expression variant. */
4425 else
4427 tree expression;
4429 /* Look for an expression. */
4430 expression = cp_parser_expression (parser, /*cast_p=*/false);
4431 /* Compute its typeid. */
4432 postfix_expression = build_typeid (expression);
4433 /* Look for the `)' token. */
4434 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4436 /* Restore the saved message. */
4437 parser->type_definition_forbidden_message = saved_message;
4438 /* `typeid' may not appear in an integral constant expression. */
4439 if (cp_parser_non_integral_constant_expression(parser,
4440 "%<typeid%> operator"))
4441 return error_mark_node;
4443 break;
4445 case RID_TYPENAME:
4447 tree type;
4448 /* The syntax permitted here is the same permitted for an
4449 elaborated-type-specifier. */
4450 type = cp_parser_elaborated_type_specifier (parser,
4451 /*is_friend=*/false,
4452 /*is_declaration=*/false);
4453 postfix_expression = cp_parser_functional_cast (parser, type);
4455 break;
4457 default:
4459 tree type;
4461 /* If the next thing is a simple-type-specifier, we may be
4462 looking at a functional cast. We could also be looking at
4463 an id-expression. So, we try the functional cast, and if
4464 that doesn't work we fall back to the primary-expression. */
4465 cp_parser_parse_tentatively (parser);
4466 /* Look for the simple-type-specifier. */
4467 type = cp_parser_simple_type_specifier (parser,
4468 /*decl_specs=*/NULL,
4469 CP_PARSER_FLAGS_NONE);
4470 /* Parse the cast itself. */
4471 if (!cp_parser_error_occurred (parser))
4472 postfix_expression
4473 = cp_parser_functional_cast (parser, type);
4474 /* If that worked, we're done. */
4475 if (cp_parser_parse_definitely (parser))
4476 break;
4478 /* If the functional-cast didn't work out, try a
4479 compound-literal. */
4480 if (cp_parser_allow_gnu_extensions_p (parser)
4481 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4483 VEC(constructor_elt,gc) *initializer_list = NULL;
4484 bool saved_in_type_id_in_expr_p;
4486 cp_parser_parse_tentatively (parser);
4487 /* Consume the `('. */
4488 cp_lexer_consume_token (parser->lexer);
4489 /* Parse the type. */
4490 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4491 parser->in_type_id_in_expr_p = true;
4492 type = cp_parser_type_id (parser);
4493 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4494 /* Look for the `)'. */
4495 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4496 /* Look for the `{'. */
4497 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4498 /* If things aren't going well, there's no need to
4499 keep going. */
4500 if (!cp_parser_error_occurred (parser))
4502 bool non_constant_p;
4503 /* Parse the initializer-list. */
4504 initializer_list
4505 = cp_parser_initializer_list (parser, &non_constant_p);
4506 /* Allow a trailing `,'. */
4507 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4508 cp_lexer_consume_token (parser->lexer);
4509 /* Look for the final `}'. */
4510 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4512 /* If that worked, we're definitely looking at a
4513 compound-literal expression. */
4514 if (cp_parser_parse_definitely (parser))
4516 /* Warn the user that a compound literal is not
4517 allowed in standard C++. */
4518 if (pedantic)
4519 pedwarn ("ISO C++ forbids compound-literals");
4520 /* For simplicity, we disallow compound literals in
4521 constant-expressions. We could
4522 allow compound literals of integer type, whose
4523 initializer was a constant, in constant
4524 expressions. Permitting that usage, as a further
4525 extension, would not change the meaning of any
4526 currently accepted programs. (Of course, as
4527 compound literals are not part of ISO C++, the
4528 standard has nothing to say.) */
4529 if (cp_parser_non_integral_constant_expression
4530 (parser, "non-constant compound literals"))
4532 postfix_expression = error_mark_node;
4533 break;
4535 /* Form the representation of the compound-literal. */
4536 postfix_expression
4537 = finish_compound_literal (type, initializer_list);
4538 break;
4542 /* It must be a primary-expression. */
4543 postfix_expression
4544 = cp_parser_primary_expression (parser, address_p, cast_p,
4545 /*template_arg_p=*/false,
4546 &idk);
4548 break;
4551 /* Keep looping until the postfix-expression is complete. */
4552 while (true)
4554 if (idk == CP_ID_KIND_UNQUALIFIED
4555 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4556 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4557 /* It is not a Koenig lookup function call. */
4558 postfix_expression
4559 = unqualified_name_lookup_error (postfix_expression);
4561 /* Peek at the next token. */
4562 token = cp_lexer_peek_token (parser->lexer);
4564 switch (token->type)
4566 case CPP_OPEN_SQUARE:
4567 postfix_expression
4568 = cp_parser_postfix_open_square_expression (parser,
4569 postfix_expression,
4570 false);
4571 idk = CP_ID_KIND_NONE;
4572 is_member_access = false;
4573 break;
4575 case CPP_OPEN_PAREN:
4576 /* postfix-expression ( expression-list [opt] ) */
4578 bool koenig_p;
4579 bool is_builtin_constant_p;
4580 bool saved_integral_constant_expression_p = false;
4581 bool saved_non_integral_constant_expression_p = false;
4582 tree args;
4584 is_member_access = false;
4586 is_builtin_constant_p
4587 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4588 if (is_builtin_constant_p)
4590 /* The whole point of __builtin_constant_p is to allow
4591 non-constant expressions to appear as arguments. */
4592 saved_integral_constant_expression_p
4593 = parser->integral_constant_expression_p;
4594 saved_non_integral_constant_expression_p
4595 = parser->non_integral_constant_expression_p;
4596 parser->integral_constant_expression_p = false;
4598 args = (cp_parser_parenthesized_expression_list
4599 (parser, /*is_attribute_list=*/false,
4600 /*cast_p=*/false, /*allow_expansion_p=*/true,
4601 /*non_constant_p=*/NULL));
4602 if (is_builtin_constant_p)
4604 parser->integral_constant_expression_p
4605 = saved_integral_constant_expression_p;
4606 parser->non_integral_constant_expression_p
4607 = saved_non_integral_constant_expression_p;
4610 if (args == error_mark_node)
4612 postfix_expression = error_mark_node;
4613 break;
4616 /* Function calls are not permitted in
4617 constant-expressions. */
4618 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4619 && cp_parser_non_integral_constant_expression (parser,
4620 "a function call"))
4622 postfix_expression = error_mark_node;
4623 break;
4626 koenig_p = false;
4627 if (idk == CP_ID_KIND_UNQUALIFIED)
4629 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4631 if (args)
4633 koenig_p = true;
4634 postfix_expression
4635 = perform_koenig_lookup (postfix_expression, args);
4637 else
4638 postfix_expression
4639 = unqualified_fn_lookup_error (postfix_expression);
4641 /* We do not perform argument-dependent lookup if
4642 normal lookup finds a non-function, in accordance
4643 with the expected resolution of DR 218. */
4644 else if (args && is_overloaded_fn (postfix_expression))
4646 tree fn = get_first_fn (postfix_expression);
4648 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4649 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4651 /* Only do argument dependent lookup if regular
4652 lookup does not find a set of member functions.
4653 [basic.lookup.koenig]/2a */
4654 if (!DECL_FUNCTION_MEMBER_P (fn))
4656 koenig_p = true;
4657 postfix_expression
4658 = perform_koenig_lookup (postfix_expression, args);
4663 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4665 tree instance = TREE_OPERAND (postfix_expression, 0);
4666 tree fn = TREE_OPERAND (postfix_expression, 1);
4668 if (processing_template_decl
4669 && (type_dependent_expression_p (instance)
4670 || (!BASELINK_P (fn)
4671 && TREE_CODE (fn) != FIELD_DECL)
4672 || type_dependent_expression_p (fn)
4673 || any_type_dependent_arguments_p (args)))
4675 postfix_expression
4676 = build_nt_call_list (postfix_expression, args);
4677 break;
4680 if (BASELINK_P (fn))
4681 postfix_expression
4682 = (build_new_method_call
4683 (instance, fn, args, NULL_TREE,
4684 (idk == CP_ID_KIND_QUALIFIED
4685 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4686 /*fn_p=*/NULL,
4687 tf_warning_or_error));
4688 else
4689 postfix_expression
4690 = finish_call_expr (postfix_expression, args,
4691 /*disallow_virtual=*/false,
4692 /*koenig_p=*/false,
4693 tf_warning_or_error);
4695 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4696 || TREE_CODE (postfix_expression) == MEMBER_REF
4697 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4698 postfix_expression = (build_offset_ref_call_from_tree
4699 (postfix_expression, args));
4700 else if (idk == CP_ID_KIND_QUALIFIED)
4701 /* A call to a static class member, or a namespace-scope
4702 function. */
4703 postfix_expression
4704 = finish_call_expr (postfix_expression, args,
4705 /*disallow_virtual=*/true,
4706 koenig_p,
4707 tf_warning_or_error);
4708 else
4709 /* All other function calls. */
4710 postfix_expression
4711 = finish_call_expr (postfix_expression, args,
4712 /*disallow_virtual=*/false,
4713 koenig_p,
4714 tf_warning_or_error);
4716 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4717 idk = CP_ID_KIND_NONE;
4719 break;
4721 case CPP_DOT:
4722 case CPP_DEREF:
4723 /* postfix-expression . template [opt] id-expression
4724 postfix-expression . pseudo-destructor-name
4725 postfix-expression -> template [opt] id-expression
4726 postfix-expression -> pseudo-destructor-name */
4728 /* Consume the `.' or `->' operator. */
4729 cp_lexer_consume_token (parser->lexer);
4731 postfix_expression
4732 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4733 postfix_expression,
4734 false, &idk);
4736 is_member_access = true;
4737 break;
4739 case CPP_PLUS_PLUS:
4740 /* postfix-expression ++ */
4741 /* Consume the `++' token. */
4742 cp_lexer_consume_token (parser->lexer);
4743 /* Generate a representation for the complete expression. */
4744 postfix_expression
4745 = finish_increment_expr (postfix_expression,
4746 POSTINCREMENT_EXPR);
4747 /* Increments may not appear in constant-expressions. */
4748 if (cp_parser_non_integral_constant_expression (parser,
4749 "an increment"))
4750 postfix_expression = error_mark_node;
4751 idk = CP_ID_KIND_NONE;
4752 is_member_access = false;
4753 break;
4755 case CPP_MINUS_MINUS:
4756 /* postfix-expression -- */
4757 /* Consume the `--' token. */
4758 cp_lexer_consume_token (parser->lexer);
4759 /* Generate a representation for the complete expression. */
4760 postfix_expression
4761 = finish_increment_expr (postfix_expression,
4762 POSTDECREMENT_EXPR);
4763 /* Decrements may not appear in constant-expressions. */
4764 if (cp_parser_non_integral_constant_expression (parser,
4765 "a decrement"))
4766 postfix_expression = error_mark_node;
4767 idk = CP_ID_KIND_NONE;
4768 is_member_access = false;
4769 break;
4771 default:
4772 if (member_access_only_p)
4773 return is_member_access? postfix_expression : error_mark_node;
4774 else
4775 return postfix_expression;
4779 /* We should never get here. */
4780 gcc_unreachable ();
4781 return error_mark_node;
4784 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4785 by cp_parser_builtin_offsetof. We're looking for
4787 postfix-expression [ expression ]
4789 FOR_OFFSETOF is set if we're being called in that context, which
4790 changes how we deal with integer constant expressions. */
4792 static tree
4793 cp_parser_postfix_open_square_expression (cp_parser *parser,
4794 tree postfix_expression,
4795 bool for_offsetof)
4797 tree index;
4799 /* Consume the `[' token. */
4800 cp_lexer_consume_token (parser->lexer);
4802 /* Parse the index expression. */
4803 /* ??? For offsetof, there is a question of what to allow here. If
4804 offsetof is not being used in an integral constant expression context,
4805 then we *could* get the right answer by computing the value at runtime.
4806 If we are in an integral constant expression context, then we might
4807 could accept any constant expression; hard to say without analysis.
4808 Rather than open the barn door too wide right away, allow only integer
4809 constant expressions here. */
4810 if (for_offsetof)
4811 index = cp_parser_constant_expression (parser, false, NULL);
4812 else
4813 index = cp_parser_expression (parser, /*cast_p=*/false);
4815 /* Look for the closing `]'. */
4816 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4818 /* Build the ARRAY_REF. */
4819 postfix_expression = grok_array_decl (postfix_expression, index);
4821 /* When not doing offsetof, array references are not permitted in
4822 constant-expressions. */
4823 if (!for_offsetof
4824 && (cp_parser_non_integral_constant_expression
4825 (parser, "an array reference")))
4826 postfix_expression = error_mark_node;
4828 return postfix_expression;
4831 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4832 by cp_parser_builtin_offsetof. We're looking for
4834 postfix-expression . template [opt] id-expression
4835 postfix-expression . pseudo-destructor-name
4836 postfix-expression -> template [opt] id-expression
4837 postfix-expression -> pseudo-destructor-name
4839 FOR_OFFSETOF is set if we're being called in that context. That sorta
4840 limits what of the above we'll actually accept, but nevermind.
4841 TOKEN_TYPE is the "." or "->" token, which will already have been
4842 removed from the stream. */
4844 static tree
4845 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4846 enum cpp_ttype token_type,
4847 tree postfix_expression,
4848 bool for_offsetof, cp_id_kind *idk)
4850 tree name;
4851 bool dependent_p;
4852 bool pseudo_destructor_p;
4853 tree scope = NULL_TREE;
4855 /* If this is a `->' operator, dereference the pointer. */
4856 if (token_type == CPP_DEREF)
4857 postfix_expression = build_x_arrow (postfix_expression);
4858 /* Check to see whether or not the expression is type-dependent. */
4859 dependent_p = type_dependent_expression_p (postfix_expression);
4860 /* The identifier following the `->' or `.' is not qualified. */
4861 parser->scope = NULL_TREE;
4862 parser->qualifying_scope = NULL_TREE;
4863 parser->object_scope = NULL_TREE;
4864 *idk = CP_ID_KIND_NONE;
4865 /* Enter the scope corresponding to the type of the object
4866 given by the POSTFIX_EXPRESSION. */
4867 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4869 scope = TREE_TYPE (postfix_expression);
4870 /* According to the standard, no expression should ever have
4871 reference type. Unfortunately, we do not currently match
4872 the standard in this respect in that our internal representation
4873 of an expression may have reference type even when the standard
4874 says it does not. Therefore, we have to manually obtain the
4875 underlying type here. */
4876 scope = non_reference (scope);
4877 /* The type of the POSTFIX_EXPRESSION must be complete. */
4878 if (scope == unknown_type_node)
4880 error ("%qE does not have class type", postfix_expression);
4881 scope = NULL_TREE;
4883 else
4884 scope = complete_type_or_else (scope, NULL_TREE);
4885 /* Let the name lookup machinery know that we are processing a
4886 class member access expression. */
4887 parser->context->object_type = scope;
4888 /* If something went wrong, we want to be able to discern that case,
4889 as opposed to the case where there was no SCOPE due to the type
4890 of expression being dependent. */
4891 if (!scope)
4892 scope = error_mark_node;
4893 /* If the SCOPE was erroneous, make the various semantic analysis
4894 functions exit quickly -- and without issuing additional error
4895 messages. */
4896 if (scope == error_mark_node)
4897 postfix_expression = error_mark_node;
4900 /* Assume this expression is not a pseudo-destructor access. */
4901 pseudo_destructor_p = false;
4903 /* If the SCOPE is a scalar type, then, if this is a valid program,
4904 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4905 is type dependent, it can be pseudo-destructor-name or something else.
4906 Try to parse it as pseudo-destructor-name first. */
4907 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4909 tree s;
4910 tree type;
4912 cp_parser_parse_tentatively (parser);
4913 /* Parse the pseudo-destructor-name. */
4914 s = NULL_TREE;
4915 cp_parser_pseudo_destructor_name (parser, &s, &type);
4916 if (dependent_p
4917 && (cp_parser_error_occurred (parser)
4918 || TREE_CODE (type) != TYPE_DECL
4919 || !SCALAR_TYPE_P (TREE_TYPE (type))))
4920 cp_parser_abort_tentative_parse (parser);
4921 else if (cp_parser_parse_definitely (parser))
4923 pseudo_destructor_p = true;
4924 postfix_expression
4925 = finish_pseudo_destructor_expr (postfix_expression,
4926 s, TREE_TYPE (type));
4930 if (!pseudo_destructor_p)
4932 /* If the SCOPE is not a scalar type, we are looking at an
4933 ordinary class member access expression, rather than a
4934 pseudo-destructor-name. */
4935 bool template_p;
4936 /* Parse the id-expression. */
4937 name = (cp_parser_id_expression
4938 (parser,
4939 cp_parser_optional_template_keyword (parser),
4940 /*check_dependency_p=*/true,
4941 &template_p,
4942 /*declarator_p=*/false,
4943 /*optional_p=*/false));
4944 /* In general, build a SCOPE_REF if the member name is qualified.
4945 However, if the name was not dependent and has already been
4946 resolved; there is no need to build the SCOPE_REF. For example;
4948 struct X { void f(); };
4949 template <typename T> void f(T* t) { t->X::f(); }
4951 Even though "t" is dependent, "X::f" is not and has been resolved
4952 to a BASELINK; there is no need to include scope information. */
4954 /* But we do need to remember that there was an explicit scope for
4955 virtual function calls. */
4956 if (parser->scope)
4957 *idk = CP_ID_KIND_QUALIFIED;
4959 /* If the name is a template-id that names a type, we will get a
4960 TYPE_DECL here. That is invalid code. */
4961 if (TREE_CODE (name) == TYPE_DECL)
4963 error ("invalid use of %qD", name);
4964 postfix_expression = error_mark_node;
4966 else
4968 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4970 name = build_qualified_name (/*type=*/NULL_TREE,
4971 parser->scope,
4972 name,
4973 template_p);
4974 parser->scope = NULL_TREE;
4975 parser->qualifying_scope = NULL_TREE;
4976 parser->object_scope = NULL_TREE;
4978 if (scope && name && BASELINK_P (name))
4979 adjust_result_of_qualified_name_lookup
4980 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4981 postfix_expression
4982 = finish_class_member_access_expr (postfix_expression, name,
4983 template_p,
4984 tf_warning_or_error);
4988 /* We no longer need to look up names in the scope of the object on
4989 the left-hand side of the `.' or `->' operator. */
4990 parser->context->object_type = NULL_TREE;
4992 /* Outside of offsetof, these operators may not appear in
4993 constant-expressions. */
4994 if (!for_offsetof
4995 && (cp_parser_non_integral_constant_expression
4996 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
4997 postfix_expression = error_mark_node;
4999 return postfix_expression;
5002 /* Parse a parenthesized expression-list.
5004 expression-list:
5005 assignment-expression
5006 expression-list, assignment-expression
5008 attribute-list:
5009 expression-list
5010 identifier
5011 identifier, expression-list
5013 CAST_P is true if this expression is the target of a cast.
5015 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5016 argument pack.
5018 Returns a TREE_LIST. The TREE_VALUE of each node is a
5019 representation of an assignment-expression. Note that a TREE_LIST
5020 is returned even if there is only a single expression in the list.
5021 error_mark_node is returned if the ( and or ) are
5022 missing. NULL_TREE is returned on no expressions. The parentheses
5023 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5024 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5025 indicates whether or not all of the expressions in the list were
5026 constant. */
5028 static tree
5029 cp_parser_parenthesized_expression_list (cp_parser* parser,
5030 bool is_attribute_list,
5031 bool cast_p,
5032 bool allow_expansion_p,
5033 bool *non_constant_p)
5035 tree expression_list = NULL_TREE;
5036 bool fold_expr_p = is_attribute_list;
5037 tree identifier = NULL_TREE;
5038 bool saved_greater_than_is_operator_p;
5040 /* Assume all the expressions will be constant. */
5041 if (non_constant_p)
5042 *non_constant_p = false;
5044 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5045 return error_mark_node;
5047 /* Within a parenthesized expression, a `>' token is always
5048 the greater-than operator. */
5049 saved_greater_than_is_operator_p
5050 = parser->greater_than_is_operator_p;
5051 parser->greater_than_is_operator_p = true;
5053 /* Consume expressions until there are no more. */
5054 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5055 while (true)
5057 tree expr;
5059 /* At the beginning of attribute lists, check to see if the
5060 next token is an identifier. */
5061 if (is_attribute_list
5062 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5064 cp_token *token;
5066 /* Consume the identifier. */
5067 token = cp_lexer_consume_token (parser->lexer);
5068 /* Save the identifier. */
5069 identifier = token->u.value;
5071 else
5073 /* Parse the next assignment-expression. */
5074 if (non_constant_p)
5076 bool expr_non_constant_p;
5077 expr = (cp_parser_constant_expression
5078 (parser, /*allow_non_constant_p=*/true,
5079 &expr_non_constant_p));
5080 if (expr_non_constant_p)
5081 *non_constant_p = true;
5083 else
5084 expr = cp_parser_assignment_expression (parser, cast_p);
5086 if (fold_expr_p)
5087 expr = fold_non_dependent_expr (expr);
5089 /* If we have an ellipsis, then this is an expression
5090 expansion. */
5091 if (allow_expansion_p
5092 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5094 /* Consume the `...'. */
5095 cp_lexer_consume_token (parser->lexer);
5097 /* Build the argument pack. */
5098 expr = make_pack_expansion (expr);
5101 /* Add it to the list. We add error_mark_node
5102 expressions to the list, so that we can still tell if
5103 the correct form for a parenthesized expression-list
5104 is found. That gives better errors. */
5105 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5107 if (expr == error_mark_node)
5108 goto skip_comma;
5111 /* After the first item, attribute lists look the same as
5112 expression lists. */
5113 is_attribute_list = false;
5115 get_comma:;
5116 /* If the next token isn't a `,', then we are done. */
5117 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5118 break;
5120 /* Otherwise, consume the `,' and keep going. */
5121 cp_lexer_consume_token (parser->lexer);
5124 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5126 int ending;
5128 skip_comma:;
5129 /* We try and resync to an unnested comma, as that will give the
5130 user better diagnostics. */
5131 ending = cp_parser_skip_to_closing_parenthesis (parser,
5132 /*recovering=*/true,
5133 /*or_comma=*/true,
5134 /*consume_paren=*/true);
5135 if (ending < 0)
5136 goto get_comma;
5137 if (!ending)
5139 parser->greater_than_is_operator_p
5140 = saved_greater_than_is_operator_p;
5141 return error_mark_node;
5145 parser->greater_than_is_operator_p
5146 = saved_greater_than_is_operator_p;
5148 /* We built up the list in reverse order so we must reverse it now. */
5149 expression_list = nreverse (expression_list);
5150 if (identifier)
5151 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5153 return expression_list;
5156 /* Parse a pseudo-destructor-name.
5158 pseudo-destructor-name:
5159 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5160 :: [opt] nested-name-specifier template template-id :: ~ type-name
5161 :: [opt] nested-name-specifier [opt] ~ type-name
5163 If either of the first two productions is used, sets *SCOPE to the
5164 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5165 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5166 or ERROR_MARK_NODE if the parse fails. */
5168 static void
5169 cp_parser_pseudo_destructor_name (cp_parser* parser,
5170 tree* scope,
5171 tree* type)
5173 bool nested_name_specifier_p;
5175 /* Assume that things will not work out. */
5176 *type = error_mark_node;
5178 /* Look for the optional `::' operator. */
5179 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5180 /* Look for the optional nested-name-specifier. */
5181 nested_name_specifier_p
5182 = (cp_parser_nested_name_specifier_opt (parser,
5183 /*typename_keyword_p=*/false,
5184 /*check_dependency_p=*/true,
5185 /*type_p=*/false,
5186 /*is_declaration=*/true)
5187 != NULL_TREE);
5188 /* Now, if we saw a nested-name-specifier, we might be doing the
5189 second production. */
5190 if (nested_name_specifier_p
5191 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5193 /* Consume the `template' keyword. */
5194 cp_lexer_consume_token (parser->lexer);
5195 /* Parse the template-id. */
5196 cp_parser_template_id (parser,
5197 /*template_keyword_p=*/true,
5198 /*check_dependency_p=*/false,
5199 /*is_declaration=*/true);
5200 /* Look for the `::' token. */
5201 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5203 /* If the next token is not a `~', then there might be some
5204 additional qualification. */
5205 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5207 /* At this point, we're looking for "type-name :: ~". The type-name
5208 must not be a class-name, since this is a pseudo-destructor. So,
5209 it must be either an enum-name, or a typedef-name -- both of which
5210 are just identifiers. So, we peek ahead to check that the "::"
5211 and "~" tokens are present; if they are not, then we can avoid
5212 calling type_name. */
5213 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5214 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5215 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5217 cp_parser_error (parser, "non-scalar type");
5218 return;
5221 /* Look for the type-name. */
5222 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5223 if (*scope == error_mark_node)
5224 return;
5226 /* Look for the `::' token. */
5227 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5229 else
5230 *scope = NULL_TREE;
5232 /* Look for the `~'. */
5233 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5234 /* Look for the type-name again. We are not responsible for
5235 checking that it matches the first type-name. */
5236 *type = cp_parser_nonclass_name (parser);
5239 /* Parse a unary-expression.
5241 unary-expression:
5242 postfix-expression
5243 ++ cast-expression
5244 -- cast-expression
5245 unary-operator cast-expression
5246 sizeof unary-expression
5247 sizeof ( type-id )
5248 new-expression
5249 delete-expression
5251 GNU Extensions:
5253 unary-expression:
5254 __extension__ cast-expression
5255 __alignof__ unary-expression
5256 __alignof__ ( type-id )
5257 __real__ cast-expression
5258 __imag__ cast-expression
5259 && identifier
5261 ADDRESS_P is true iff the unary-expression is appearing as the
5262 operand of the `&' operator. CAST_P is true if this expression is
5263 the target of a cast.
5265 Returns a representation of the expression. */
5267 static tree
5268 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5270 cp_token *token;
5271 enum tree_code unary_operator;
5273 /* Peek at the next token. */
5274 token = cp_lexer_peek_token (parser->lexer);
5275 /* Some keywords give away the kind of expression. */
5276 if (token->type == CPP_KEYWORD)
5278 enum rid keyword = token->keyword;
5280 switch (keyword)
5282 case RID_ALIGNOF:
5283 case RID_SIZEOF:
5285 tree operand;
5286 enum tree_code op;
5288 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5289 /* Consume the token. */
5290 cp_lexer_consume_token (parser->lexer);
5291 /* Parse the operand. */
5292 operand = cp_parser_sizeof_operand (parser, keyword);
5294 if (TYPE_P (operand))
5295 return cxx_sizeof_or_alignof_type (operand, op, true);
5296 else
5297 return cxx_sizeof_or_alignof_expr (operand, op, true);
5300 case RID_NEW:
5301 return cp_parser_new_expression (parser);
5303 case RID_DELETE:
5304 return cp_parser_delete_expression (parser);
5306 case RID_EXTENSION:
5308 /* The saved value of the PEDANTIC flag. */
5309 int saved_pedantic;
5310 tree expr;
5312 /* Save away the PEDANTIC flag. */
5313 cp_parser_extension_opt (parser, &saved_pedantic);
5314 /* Parse the cast-expression. */
5315 expr = cp_parser_simple_cast_expression (parser);
5316 /* Restore the PEDANTIC flag. */
5317 pedantic = saved_pedantic;
5319 return expr;
5322 case RID_REALPART:
5323 case RID_IMAGPART:
5325 tree expression;
5327 /* Consume the `__real__' or `__imag__' token. */
5328 cp_lexer_consume_token (parser->lexer);
5329 /* Parse the cast-expression. */
5330 expression = cp_parser_simple_cast_expression (parser);
5331 /* Create the complete representation. */
5332 return build_x_unary_op ((keyword == RID_REALPART
5333 ? REALPART_EXPR : IMAGPART_EXPR),
5334 expression,
5335 tf_warning_or_error);
5337 break;
5339 default:
5340 break;
5344 /* Look for the `:: new' and `:: delete', which also signal the
5345 beginning of a new-expression, or delete-expression,
5346 respectively. If the next token is `::', then it might be one of
5347 these. */
5348 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5350 enum rid keyword;
5352 /* See if the token after the `::' is one of the keywords in
5353 which we're interested. */
5354 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5355 /* If it's `new', we have a new-expression. */
5356 if (keyword == RID_NEW)
5357 return cp_parser_new_expression (parser);
5358 /* Similarly, for `delete'. */
5359 else if (keyword == RID_DELETE)
5360 return cp_parser_delete_expression (parser);
5363 /* Look for a unary operator. */
5364 unary_operator = cp_parser_unary_operator (token);
5365 /* The `++' and `--' operators can be handled similarly, even though
5366 they are not technically unary-operators in the grammar. */
5367 if (unary_operator == ERROR_MARK)
5369 if (token->type == CPP_PLUS_PLUS)
5370 unary_operator = PREINCREMENT_EXPR;
5371 else if (token->type == CPP_MINUS_MINUS)
5372 unary_operator = PREDECREMENT_EXPR;
5373 /* Handle the GNU address-of-label extension. */
5374 else if (cp_parser_allow_gnu_extensions_p (parser)
5375 && token->type == CPP_AND_AND)
5377 tree identifier;
5378 tree expression;
5380 /* Consume the '&&' token. */
5381 cp_lexer_consume_token (parser->lexer);
5382 /* Look for the identifier. */
5383 identifier = cp_parser_identifier (parser);
5384 /* Create an expression representing the address. */
5385 expression = finish_label_address_expr (identifier);
5386 if (cp_parser_non_integral_constant_expression (parser,
5387 "the address of a label"))
5388 expression = error_mark_node;
5389 return expression;
5392 if (unary_operator != ERROR_MARK)
5394 tree cast_expression;
5395 tree expression = error_mark_node;
5396 const char *non_constant_p = NULL;
5398 /* Consume the operator token. */
5399 token = cp_lexer_consume_token (parser->lexer);
5400 /* Parse the cast-expression. */
5401 cast_expression
5402 = cp_parser_cast_expression (parser,
5403 unary_operator == ADDR_EXPR,
5404 /*cast_p=*/false);
5405 /* Now, build an appropriate representation. */
5406 switch (unary_operator)
5408 case INDIRECT_REF:
5409 non_constant_p = "%<*%>";
5410 expression = build_x_indirect_ref (cast_expression, "unary *",
5411 tf_warning_or_error);
5412 break;
5414 case ADDR_EXPR:
5415 non_constant_p = "%<&%>";
5416 /* Fall through. */
5417 case BIT_NOT_EXPR:
5418 expression = build_x_unary_op (unary_operator, cast_expression,
5419 tf_warning_or_error);
5420 break;
5422 case PREINCREMENT_EXPR:
5423 case PREDECREMENT_EXPR:
5424 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5425 ? "%<++%>" : "%<--%>");
5426 /* Fall through. */
5427 case UNARY_PLUS_EXPR:
5428 case NEGATE_EXPR:
5429 case TRUTH_NOT_EXPR:
5430 expression = finish_unary_op_expr (unary_operator, cast_expression);
5431 break;
5433 default:
5434 gcc_unreachable ();
5437 if (non_constant_p
5438 && cp_parser_non_integral_constant_expression (parser,
5439 non_constant_p))
5440 expression = error_mark_node;
5442 return expression;
5445 return cp_parser_postfix_expression (parser, address_p, cast_p,
5446 /*member_access_only_p=*/false);
5449 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5450 unary-operator, the corresponding tree code is returned. */
5452 static enum tree_code
5453 cp_parser_unary_operator (cp_token* token)
5455 switch (token->type)
5457 case CPP_MULT:
5458 return INDIRECT_REF;
5460 case CPP_AND:
5461 return ADDR_EXPR;
5463 case CPP_PLUS:
5464 return UNARY_PLUS_EXPR;
5466 case CPP_MINUS:
5467 return NEGATE_EXPR;
5469 case CPP_NOT:
5470 return TRUTH_NOT_EXPR;
5472 case CPP_COMPL:
5473 return BIT_NOT_EXPR;
5475 default:
5476 return ERROR_MARK;
5480 /* Parse a new-expression.
5482 new-expression:
5483 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5484 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5486 Returns a representation of the expression. */
5488 static tree
5489 cp_parser_new_expression (cp_parser* parser)
5491 bool global_scope_p;
5492 tree placement;
5493 tree type;
5494 tree initializer;
5495 tree nelts;
5497 /* Look for the optional `::' operator. */
5498 global_scope_p
5499 = (cp_parser_global_scope_opt (parser,
5500 /*current_scope_valid_p=*/false)
5501 != NULL_TREE);
5502 /* Look for the `new' operator. */
5503 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5504 /* There's no easy way to tell a new-placement from the
5505 `( type-id )' construct. */
5506 cp_parser_parse_tentatively (parser);
5507 /* Look for a new-placement. */
5508 placement = cp_parser_new_placement (parser);
5509 /* If that didn't work out, there's no new-placement. */
5510 if (!cp_parser_parse_definitely (parser))
5511 placement = NULL_TREE;
5513 /* If the next token is a `(', then we have a parenthesized
5514 type-id. */
5515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5517 /* Consume the `('. */
5518 cp_lexer_consume_token (parser->lexer);
5519 /* Parse the type-id. */
5520 type = cp_parser_type_id (parser);
5521 /* Look for the closing `)'. */
5522 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5523 /* There should not be a direct-new-declarator in this production,
5524 but GCC used to allowed this, so we check and emit a sensible error
5525 message for this case. */
5526 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5528 error ("array bound forbidden after parenthesized type-id");
5529 inform ("try removing the parentheses around the type-id");
5530 cp_parser_direct_new_declarator (parser);
5532 nelts = NULL_TREE;
5534 /* Otherwise, there must be a new-type-id. */
5535 else
5536 type = cp_parser_new_type_id (parser, &nelts);
5538 /* If the next token is a `(', then we have a new-initializer. */
5539 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5540 initializer = cp_parser_new_initializer (parser);
5541 else
5542 initializer = NULL_TREE;
5544 /* A new-expression may not appear in an integral constant
5545 expression. */
5546 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5547 return error_mark_node;
5549 /* Create a representation of the new-expression. */
5550 return build_new (placement, type, nelts, initializer, global_scope_p,
5551 tf_warning_or_error);
5554 /* Parse a new-placement.
5556 new-placement:
5557 ( expression-list )
5559 Returns the same representation as for an expression-list. */
5561 static tree
5562 cp_parser_new_placement (cp_parser* parser)
5564 tree expression_list;
5566 /* Parse the expression-list. */
5567 expression_list = (cp_parser_parenthesized_expression_list
5568 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5569 /*non_constant_p=*/NULL));
5571 return expression_list;
5574 /* Parse a new-type-id.
5576 new-type-id:
5577 type-specifier-seq new-declarator [opt]
5579 Returns the TYPE allocated. If the new-type-id indicates an array
5580 type, *NELTS is set to the number of elements in the last array
5581 bound; the TYPE will not include the last array bound. */
5583 static tree
5584 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5586 cp_decl_specifier_seq type_specifier_seq;
5587 cp_declarator *new_declarator;
5588 cp_declarator *declarator;
5589 cp_declarator *outer_declarator;
5590 const char *saved_message;
5591 tree type;
5593 /* The type-specifier sequence must not contain type definitions.
5594 (It cannot contain declarations of new types either, but if they
5595 are not definitions we will catch that because they are not
5596 complete.) */
5597 saved_message = parser->type_definition_forbidden_message;
5598 parser->type_definition_forbidden_message
5599 = "types may not be defined in a new-type-id";
5600 /* Parse the type-specifier-seq. */
5601 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5602 &type_specifier_seq);
5603 /* Restore the old message. */
5604 parser->type_definition_forbidden_message = saved_message;
5605 /* Parse the new-declarator. */
5606 new_declarator = cp_parser_new_declarator_opt (parser);
5608 /* Determine the number of elements in the last array dimension, if
5609 any. */
5610 *nelts = NULL_TREE;
5611 /* Skip down to the last array dimension. */
5612 declarator = new_declarator;
5613 outer_declarator = NULL;
5614 while (declarator && (declarator->kind == cdk_pointer
5615 || declarator->kind == cdk_ptrmem))
5617 outer_declarator = declarator;
5618 declarator = declarator->declarator;
5620 while (declarator
5621 && declarator->kind == cdk_array
5622 && declarator->declarator
5623 && declarator->declarator->kind == cdk_array)
5625 outer_declarator = declarator;
5626 declarator = declarator->declarator;
5629 if (declarator && declarator->kind == cdk_array)
5631 *nelts = declarator->u.array.bounds;
5632 if (*nelts == error_mark_node)
5633 *nelts = integer_one_node;
5635 if (outer_declarator)
5636 outer_declarator->declarator = declarator->declarator;
5637 else
5638 new_declarator = NULL;
5641 type = groktypename (&type_specifier_seq, new_declarator);
5642 return type;
5645 /* Parse an (optional) new-declarator.
5647 new-declarator:
5648 ptr-operator new-declarator [opt]
5649 direct-new-declarator
5651 Returns the declarator. */
5653 static cp_declarator *
5654 cp_parser_new_declarator_opt (cp_parser* parser)
5656 enum tree_code code;
5657 tree type;
5658 cp_cv_quals cv_quals;
5660 /* We don't know if there's a ptr-operator next, or not. */
5661 cp_parser_parse_tentatively (parser);
5662 /* Look for a ptr-operator. */
5663 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5664 /* If that worked, look for more new-declarators. */
5665 if (cp_parser_parse_definitely (parser))
5667 cp_declarator *declarator;
5669 /* Parse another optional declarator. */
5670 declarator = cp_parser_new_declarator_opt (parser);
5672 return cp_parser_make_indirect_declarator
5673 (code, type, cv_quals, declarator);
5676 /* If the next token is a `[', there is a direct-new-declarator. */
5677 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5678 return cp_parser_direct_new_declarator (parser);
5680 return NULL;
5683 /* Parse a direct-new-declarator.
5685 direct-new-declarator:
5686 [ expression ]
5687 direct-new-declarator [constant-expression]
5691 static cp_declarator *
5692 cp_parser_direct_new_declarator (cp_parser* parser)
5694 cp_declarator *declarator = NULL;
5696 while (true)
5698 tree expression;
5700 /* Look for the opening `['. */
5701 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5702 /* The first expression is not required to be constant. */
5703 if (!declarator)
5705 expression = cp_parser_expression (parser, /*cast_p=*/false);
5706 /* The standard requires that the expression have integral
5707 type. DR 74 adds enumeration types. We believe that the
5708 real intent is that these expressions be handled like the
5709 expression in a `switch' condition, which also allows
5710 classes with a single conversion to integral or
5711 enumeration type. */
5712 if (!processing_template_decl)
5714 expression
5715 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5716 expression,
5717 /*complain=*/true);
5718 if (!expression)
5720 error ("expression in new-declarator must have integral "
5721 "or enumeration type");
5722 expression = error_mark_node;
5726 /* But all the other expressions must be. */
5727 else
5728 expression
5729 = cp_parser_constant_expression (parser,
5730 /*allow_non_constant=*/false,
5731 NULL);
5732 /* Look for the closing `]'. */
5733 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5735 /* Add this bound to the declarator. */
5736 declarator = make_array_declarator (declarator, expression);
5738 /* If the next token is not a `[', then there are no more
5739 bounds. */
5740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5741 break;
5744 return declarator;
5747 /* Parse a new-initializer.
5749 new-initializer:
5750 ( expression-list [opt] )
5752 Returns a representation of the expression-list. If there is no
5753 expression-list, VOID_ZERO_NODE is returned. */
5755 static tree
5756 cp_parser_new_initializer (cp_parser* parser)
5758 tree expression_list;
5760 expression_list = (cp_parser_parenthesized_expression_list
5761 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5762 /*non_constant_p=*/NULL));
5763 if (!expression_list)
5764 expression_list = void_zero_node;
5766 return expression_list;
5769 /* Parse a delete-expression.
5771 delete-expression:
5772 :: [opt] delete cast-expression
5773 :: [opt] delete [ ] cast-expression
5775 Returns a representation of the expression. */
5777 static tree
5778 cp_parser_delete_expression (cp_parser* parser)
5780 bool global_scope_p;
5781 bool array_p;
5782 tree expression;
5784 /* Look for the optional `::' operator. */
5785 global_scope_p
5786 = (cp_parser_global_scope_opt (parser,
5787 /*current_scope_valid_p=*/false)
5788 != NULL_TREE);
5789 /* Look for the `delete' keyword. */
5790 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5791 /* See if the array syntax is in use. */
5792 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5794 /* Consume the `[' token. */
5795 cp_lexer_consume_token (parser->lexer);
5796 /* Look for the `]' token. */
5797 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5798 /* Remember that this is the `[]' construct. */
5799 array_p = true;
5801 else
5802 array_p = false;
5804 /* Parse the cast-expression. */
5805 expression = cp_parser_simple_cast_expression (parser);
5807 /* A delete-expression may not appear in an integral constant
5808 expression. */
5809 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5810 return error_mark_node;
5812 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5815 /* Parse a cast-expression.
5817 cast-expression:
5818 unary-expression
5819 ( type-id ) cast-expression
5821 ADDRESS_P is true iff the unary-expression is appearing as the
5822 operand of the `&' operator. CAST_P is true if this expression is
5823 the target of a cast.
5825 Returns a representation of the expression. */
5827 static tree
5828 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5830 /* If it's a `(', then we might be looking at a cast. */
5831 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5833 tree type = NULL_TREE;
5834 tree expr = NULL_TREE;
5835 bool compound_literal_p;
5836 const char *saved_message;
5838 /* There's no way to know yet whether or not this is a cast.
5839 For example, `(int (3))' is a unary-expression, while `(int)
5840 3' is a cast. So, we resort to parsing tentatively. */
5841 cp_parser_parse_tentatively (parser);
5842 /* Types may not be defined in a cast. */
5843 saved_message = parser->type_definition_forbidden_message;
5844 parser->type_definition_forbidden_message
5845 = "types may not be defined in casts";
5846 /* Consume the `('. */
5847 cp_lexer_consume_token (parser->lexer);
5848 /* A very tricky bit is that `(struct S) { 3 }' is a
5849 compound-literal (which we permit in C++ as an extension).
5850 But, that construct is not a cast-expression -- it is a
5851 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5852 is legal; if the compound-literal were a cast-expression,
5853 you'd need an extra set of parentheses.) But, if we parse
5854 the type-id, and it happens to be a class-specifier, then we
5855 will commit to the parse at that point, because we cannot
5856 undo the action that is done when creating a new class. So,
5857 then we cannot back up and do a postfix-expression.
5859 Therefore, we scan ahead to the closing `)', and check to see
5860 if the token after the `)' is a `{'. If so, we are not
5861 looking at a cast-expression.
5863 Save tokens so that we can put them back. */
5864 cp_lexer_save_tokens (parser->lexer);
5865 /* Skip tokens until the next token is a closing parenthesis.
5866 If we find the closing `)', and the next token is a `{', then
5867 we are looking at a compound-literal. */
5868 compound_literal_p
5869 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5870 /*consume_paren=*/true)
5871 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5872 /* Roll back the tokens we skipped. */
5873 cp_lexer_rollback_tokens (parser->lexer);
5874 /* If we were looking at a compound-literal, simulate an error
5875 so that the call to cp_parser_parse_definitely below will
5876 fail. */
5877 if (compound_literal_p)
5878 cp_parser_simulate_error (parser);
5879 else
5881 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5882 parser->in_type_id_in_expr_p = true;
5883 /* Look for the type-id. */
5884 type = cp_parser_type_id (parser);
5885 /* Look for the closing `)'. */
5886 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5887 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5890 /* Restore the saved message. */
5891 parser->type_definition_forbidden_message = saved_message;
5893 /* If ok so far, parse the dependent expression. We cannot be
5894 sure it is a cast. Consider `(T ())'. It is a parenthesized
5895 ctor of T, but looks like a cast to function returning T
5896 without a dependent expression. */
5897 if (!cp_parser_error_occurred (parser))
5898 expr = cp_parser_cast_expression (parser,
5899 /*address_p=*/false,
5900 /*cast_p=*/true);
5902 if (cp_parser_parse_definitely (parser))
5904 /* Warn about old-style casts, if so requested. */
5905 if (warn_old_style_cast
5906 && !in_system_header
5907 && !VOID_TYPE_P (type)
5908 && current_lang_name != lang_name_c)
5909 warning (OPT_Wold_style_cast, "use of old-style cast");
5911 /* Only type conversions to integral or enumeration types
5912 can be used in constant-expressions. */
5913 if (!cast_valid_in_integral_constant_expression_p (type)
5914 && (cp_parser_non_integral_constant_expression
5915 (parser,
5916 "a cast to a type other than an integral or "
5917 "enumeration type")))
5918 return error_mark_node;
5920 /* Perform the cast. */
5921 expr = build_c_cast (type, expr);
5922 return expr;
5926 /* If we get here, then it's not a cast, so it must be a
5927 unary-expression. */
5928 return cp_parser_unary_expression (parser, address_p, cast_p);
5931 /* Parse a binary expression of the general form:
5933 pm-expression:
5934 cast-expression
5935 pm-expression .* cast-expression
5936 pm-expression ->* cast-expression
5938 multiplicative-expression:
5939 pm-expression
5940 multiplicative-expression * pm-expression
5941 multiplicative-expression / pm-expression
5942 multiplicative-expression % pm-expression
5944 additive-expression:
5945 multiplicative-expression
5946 additive-expression + multiplicative-expression
5947 additive-expression - multiplicative-expression
5949 shift-expression:
5950 additive-expression
5951 shift-expression << additive-expression
5952 shift-expression >> additive-expression
5954 relational-expression:
5955 shift-expression
5956 relational-expression < shift-expression
5957 relational-expression > shift-expression
5958 relational-expression <= shift-expression
5959 relational-expression >= shift-expression
5961 GNU Extension:
5963 relational-expression:
5964 relational-expression <? shift-expression
5965 relational-expression >? shift-expression
5967 equality-expression:
5968 relational-expression
5969 equality-expression == relational-expression
5970 equality-expression != relational-expression
5972 and-expression:
5973 equality-expression
5974 and-expression & equality-expression
5976 exclusive-or-expression:
5977 and-expression
5978 exclusive-or-expression ^ and-expression
5980 inclusive-or-expression:
5981 exclusive-or-expression
5982 inclusive-or-expression | exclusive-or-expression
5984 logical-and-expression:
5985 inclusive-or-expression
5986 logical-and-expression && inclusive-or-expression
5988 logical-or-expression:
5989 logical-and-expression
5990 logical-or-expression || logical-and-expression
5992 All these are implemented with a single function like:
5994 binary-expression:
5995 simple-cast-expression
5996 binary-expression <token> binary-expression
5998 CAST_P is true if this expression is the target of a cast.
6000 The binops_by_token map is used to get the tree codes for each <token> type.
6001 binary-expressions are associated according to a precedence table. */
6003 #define TOKEN_PRECEDENCE(token) \
6004 (((token->type == CPP_GREATER \
6005 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6006 && !parser->greater_than_is_operator_p) \
6007 ? PREC_NOT_OPERATOR \
6008 : binops_by_token[token->type].prec)
6010 static tree
6011 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
6013 cp_parser_expression_stack stack;
6014 cp_parser_expression_stack_entry *sp = &stack[0];
6015 tree lhs, rhs;
6016 cp_token *token;
6017 enum tree_code tree_type, lhs_type, rhs_type;
6018 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
6019 bool overloaded_p;
6021 /* Parse the first expression. */
6022 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6023 lhs_type = ERROR_MARK;
6025 for (;;)
6027 /* Get an operator token. */
6028 token = cp_lexer_peek_token (parser->lexer);
6030 if (warn_cxx0x_compat
6031 && token->type == CPP_RSHIFT
6032 && !parser->greater_than_is_operator_p)
6034 warning (OPT_Wc__0x_compat,
6035 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6036 &token->location);
6037 warning (OPT_Wc__0x_compat,
6038 "suggest parentheses around %<>>%> expression");
6041 new_prec = TOKEN_PRECEDENCE (token);
6043 /* Popping an entry off the stack means we completed a subexpression:
6044 - either we found a token which is not an operator (`>' where it is not
6045 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6046 will happen repeatedly;
6047 - or, we found an operator which has lower priority. This is the case
6048 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6049 parsing `3 * 4'. */
6050 if (new_prec <= prec)
6052 if (sp == stack)
6053 break;
6054 else
6055 goto pop;
6058 get_rhs:
6059 tree_type = binops_by_token[token->type].tree_type;
6061 /* We used the operator token. */
6062 cp_lexer_consume_token (parser->lexer);
6064 /* Extract another operand. It may be the RHS of this expression
6065 or the LHS of a new, higher priority expression. */
6066 rhs = cp_parser_simple_cast_expression (parser);
6067 rhs_type = ERROR_MARK;
6069 /* Get another operator token. Look up its precedence to avoid
6070 building a useless (immediately popped) stack entry for common
6071 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6072 token = cp_lexer_peek_token (parser->lexer);
6073 lookahead_prec = TOKEN_PRECEDENCE (token);
6074 if (lookahead_prec > new_prec)
6076 /* ... and prepare to parse the RHS of the new, higher priority
6077 expression. Since precedence levels on the stack are
6078 monotonically increasing, we do not have to care about
6079 stack overflows. */
6080 sp->prec = prec;
6081 sp->tree_type = tree_type;
6082 sp->lhs = lhs;
6083 sp->lhs_type = lhs_type;
6084 sp++;
6085 lhs = rhs;
6086 lhs_type = rhs_type;
6087 prec = new_prec;
6088 new_prec = lookahead_prec;
6089 goto get_rhs;
6091 pop:
6092 /* If the stack is not empty, we have parsed into LHS the right side
6093 (`4' in the example above) of an expression we had suspended.
6094 We can use the information on the stack to recover the LHS (`3')
6095 from the stack together with the tree code (`MULT_EXPR'), and
6096 the precedence of the higher level subexpression
6097 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6098 which will be used to actually build the additive expression. */
6099 --sp;
6100 prec = sp->prec;
6101 tree_type = sp->tree_type;
6102 rhs = lhs;
6103 rhs_type = lhs_type;
6104 lhs = sp->lhs;
6105 lhs_type = sp->lhs_type;
6108 overloaded_p = false;
6109 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6110 &overloaded_p, tf_warning_or_error);
6111 lhs_type = tree_type;
6113 /* If the binary operator required the use of an overloaded operator,
6114 then this expression cannot be an integral constant-expression.
6115 An overloaded operator can be used even if both operands are
6116 otherwise permissible in an integral constant-expression if at
6117 least one of the operands is of enumeration type. */
6119 if (overloaded_p
6120 && (cp_parser_non_integral_constant_expression
6121 (parser, "calls to overloaded operators")))
6122 return error_mark_node;
6125 return lhs;
6129 /* Parse the `? expression : assignment-expression' part of a
6130 conditional-expression. The LOGICAL_OR_EXPR is the
6131 logical-or-expression that started the conditional-expression.
6132 Returns a representation of the entire conditional-expression.
6134 This routine is used by cp_parser_assignment_expression.
6136 ? expression : assignment-expression
6138 GNU Extensions:
6140 ? : assignment-expression */
6142 static tree
6143 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6145 tree expr;
6146 tree assignment_expr;
6148 /* Consume the `?' token. */
6149 cp_lexer_consume_token (parser->lexer);
6150 if (cp_parser_allow_gnu_extensions_p (parser)
6151 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6152 /* Implicit true clause. */
6153 expr = NULL_TREE;
6154 else
6155 /* Parse the expression. */
6156 expr = cp_parser_expression (parser, /*cast_p=*/false);
6158 /* The next token should be a `:'. */
6159 cp_parser_require (parser, CPP_COLON, "%<:%>");
6160 /* Parse the assignment-expression. */
6161 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6163 /* Build the conditional-expression. */
6164 return build_x_conditional_expr (logical_or_expr,
6165 expr,
6166 assignment_expr,
6167 tf_warning_or_error);
6170 /* Parse an assignment-expression.
6172 assignment-expression:
6173 conditional-expression
6174 logical-or-expression assignment-operator assignment_expression
6175 throw-expression
6177 CAST_P is true if this expression is the target of a cast.
6179 Returns a representation for the expression. */
6181 static tree
6182 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6184 tree expr;
6186 /* If the next token is the `throw' keyword, then we're looking at
6187 a throw-expression. */
6188 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6189 expr = cp_parser_throw_expression (parser);
6190 /* Otherwise, it must be that we are looking at a
6191 logical-or-expression. */
6192 else
6194 /* Parse the binary expressions (logical-or-expression). */
6195 expr = cp_parser_binary_expression (parser, cast_p);
6196 /* If the next token is a `?' then we're actually looking at a
6197 conditional-expression. */
6198 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6199 return cp_parser_question_colon_clause (parser, expr);
6200 else
6202 enum tree_code assignment_operator;
6204 /* If it's an assignment-operator, we're using the second
6205 production. */
6206 assignment_operator
6207 = cp_parser_assignment_operator_opt (parser);
6208 if (assignment_operator != ERROR_MARK)
6210 tree rhs;
6212 /* Parse the right-hand side of the assignment. */
6213 rhs = cp_parser_assignment_expression (parser, cast_p);
6214 /* An assignment may not appear in a
6215 constant-expression. */
6216 if (cp_parser_non_integral_constant_expression (parser,
6217 "an assignment"))
6218 return error_mark_node;
6219 /* Build the assignment expression. */
6220 expr = build_x_modify_expr (expr,
6221 assignment_operator,
6222 rhs,
6223 tf_warning_or_error);
6228 return expr;
6231 /* Parse an (optional) assignment-operator.
6233 assignment-operator: one of
6234 = *= /= %= += -= >>= <<= &= ^= |=
6236 GNU Extension:
6238 assignment-operator: one of
6239 <?= >?=
6241 If the next token is an assignment operator, the corresponding tree
6242 code is returned, and the token is consumed. For example, for
6243 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6244 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6245 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6246 operator, ERROR_MARK is returned. */
6248 static enum tree_code
6249 cp_parser_assignment_operator_opt (cp_parser* parser)
6251 enum tree_code op;
6252 cp_token *token;
6254 /* Peek at the next toen. */
6255 token = cp_lexer_peek_token (parser->lexer);
6257 switch (token->type)
6259 case CPP_EQ:
6260 op = NOP_EXPR;
6261 break;
6263 case CPP_MULT_EQ:
6264 op = MULT_EXPR;
6265 break;
6267 case CPP_DIV_EQ:
6268 op = TRUNC_DIV_EXPR;
6269 break;
6271 case CPP_MOD_EQ:
6272 op = TRUNC_MOD_EXPR;
6273 break;
6275 case CPP_PLUS_EQ:
6276 op = PLUS_EXPR;
6277 break;
6279 case CPP_MINUS_EQ:
6280 op = MINUS_EXPR;
6281 break;
6283 case CPP_RSHIFT_EQ:
6284 op = RSHIFT_EXPR;
6285 break;
6287 case CPP_LSHIFT_EQ:
6288 op = LSHIFT_EXPR;
6289 break;
6291 case CPP_AND_EQ:
6292 op = BIT_AND_EXPR;
6293 break;
6295 case CPP_XOR_EQ:
6296 op = BIT_XOR_EXPR;
6297 break;
6299 case CPP_OR_EQ:
6300 op = BIT_IOR_EXPR;
6301 break;
6303 default:
6304 /* Nothing else is an assignment operator. */
6305 op = ERROR_MARK;
6308 /* If it was an assignment operator, consume it. */
6309 if (op != ERROR_MARK)
6310 cp_lexer_consume_token (parser->lexer);
6312 return op;
6315 /* Parse an expression.
6317 expression:
6318 assignment-expression
6319 expression , assignment-expression
6321 CAST_P is true if this expression is the target of a cast.
6323 Returns a representation of the expression. */
6325 static tree
6326 cp_parser_expression (cp_parser* parser, bool cast_p)
6328 tree expression = NULL_TREE;
6330 while (true)
6332 tree assignment_expression;
6334 /* Parse the next assignment-expression. */
6335 assignment_expression
6336 = cp_parser_assignment_expression (parser, cast_p);
6337 /* If this is the first assignment-expression, we can just
6338 save it away. */
6339 if (!expression)
6340 expression = assignment_expression;
6341 else
6342 expression = build_x_compound_expr (expression,
6343 assignment_expression,
6344 tf_warning_or_error);
6345 /* If the next token is not a comma, then we are done with the
6346 expression. */
6347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6348 break;
6349 /* Consume the `,'. */
6350 cp_lexer_consume_token (parser->lexer);
6351 /* A comma operator cannot appear in a constant-expression. */
6352 if (cp_parser_non_integral_constant_expression (parser,
6353 "a comma operator"))
6354 expression = error_mark_node;
6357 return expression;
6360 /* Parse a constant-expression.
6362 constant-expression:
6363 conditional-expression
6365 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6366 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6367 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6368 is false, NON_CONSTANT_P should be NULL. */
6370 static tree
6371 cp_parser_constant_expression (cp_parser* parser,
6372 bool allow_non_constant_p,
6373 bool *non_constant_p)
6375 bool saved_integral_constant_expression_p;
6376 bool saved_allow_non_integral_constant_expression_p;
6377 bool saved_non_integral_constant_expression_p;
6378 tree expression;
6380 /* It might seem that we could simply parse the
6381 conditional-expression, and then check to see if it were
6382 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6383 one that the compiler can figure out is constant, possibly after
6384 doing some simplifications or optimizations. The standard has a
6385 precise definition of constant-expression, and we must honor
6386 that, even though it is somewhat more restrictive.
6388 For example:
6390 int i[(2, 3)];
6392 is not a legal declaration, because `(2, 3)' is not a
6393 constant-expression. The `,' operator is forbidden in a
6394 constant-expression. However, GCC's constant-folding machinery
6395 will fold this operation to an INTEGER_CST for `3'. */
6397 /* Save the old settings. */
6398 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6399 saved_allow_non_integral_constant_expression_p
6400 = parser->allow_non_integral_constant_expression_p;
6401 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6402 /* We are now parsing a constant-expression. */
6403 parser->integral_constant_expression_p = true;
6404 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6405 parser->non_integral_constant_expression_p = false;
6406 /* Although the grammar says "conditional-expression", we parse an
6407 "assignment-expression", which also permits "throw-expression"
6408 and the use of assignment operators. In the case that
6409 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6410 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6411 actually essential that we look for an assignment-expression.
6412 For example, cp_parser_initializer_clauses uses this function to
6413 determine whether a particular assignment-expression is in fact
6414 constant. */
6415 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6416 /* Restore the old settings. */
6417 parser->integral_constant_expression_p
6418 = saved_integral_constant_expression_p;
6419 parser->allow_non_integral_constant_expression_p
6420 = saved_allow_non_integral_constant_expression_p;
6421 if (allow_non_constant_p)
6422 *non_constant_p = parser->non_integral_constant_expression_p;
6423 else if (parser->non_integral_constant_expression_p)
6424 expression = error_mark_node;
6425 parser->non_integral_constant_expression_p
6426 = saved_non_integral_constant_expression_p;
6428 return expression;
6431 /* Parse __builtin_offsetof.
6433 offsetof-expression:
6434 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6436 offsetof-member-designator:
6437 id-expression
6438 | offsetof-member-designator "." id-expression
6439 | offsetof-member-designator "[" expression "]" */
6441 static tree
6442 cp_parser_builtin_offsetof (cp_parser *parser)
6444 int save_ice_p, save_non_ice_p;
6445 tree type, expr;
6446 cp_id_kind dummy;
6448 /* We're about to accept non-integral-constant things, but will
6449 definitely yield an integral constant expression. Save and
6450 restore these values around our local parsing. */
6451 save_ice_p = parser->integral_constant_expression_p;
6452 save_non_ice_p = parser->non_integral_constant_expression_p;
6454 /* Consume the "__builtin_offsetof" token. */
6455 cp_lexer_consume_token (parser->lexer);
6456 /* Consume the opening `('. */
6457 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6458 /* Parse the type-id. */
6459 type = cp_parser_type_id (parser);
6460 /* Look for the `,'. */
6461 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6463 /* Build the (type *)null that begins the traditional offsetof macro. */
6464 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6465 tf_warning_or_error);
6467 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6468 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6469 true, &dummy);
6470 while (true)
6472 cp_token *token = cp_lexer_peek_token (parser->lexer);
6473 switch (token->type)
6475 case CPP_OPEN_SQUARE:
6476 /* offsetof-member-designator "[" expression "]" */
6477 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6478 break;
6480 case CPP_DOT:
6481 /* offsetof-member-designator "." identifier */
6482 cp_lexer_consume_token (parser->lexer);
6483 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6484 true, &dummy);
6485 break;
6487 case CPP_CLOSE_PAREN:
6488 /* Consume the ")" token. */
6489 cp_lexer_consume_token (parser->lexer);
6490 goto success;
6492 default:
6493 /* Error. We know the following require will fail, but
6494 that gives the proper error message. */
6495 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6496 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6497 expr = error_mark_node;
6498 goto failure;
6502 success:
6503 /* If we're processing a template, we can't finish the semantics yet.
6504 Otherwise we can fold the entire expression now. */
6505 if (processing_template_decl)
6506 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6507 else
6508 expr = finish_offsetof (expr);
6510 failure:
6511 parser->integral_constant_expression_p = save_ice_p;
6512 parser->non_integral_constant_expression_p = save_non_ice_p;
6514 return expr;
6517 /* Parse a trait expression. */
6519 static tree
6520 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6522 cp_trait_kind kind;
6523 tree type1, type2 = NULL_TREE;
6524 bool binary = false;
6525 cp_decl_specifier_seq decl_specs;
6527 switch (keyword)
6529 case RID_HAS_NOTHROW_ASSIGN:
6530 kind = CPTK_HAS_NOTHROW_ASSIGN;
6531 break;
6532 case RID_HAS_NOTHROW_CONSTRUCTOR:
6533 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6534 break;
6535 case RID_HAS_NOTHROW_COPY:
6536 kind = CPTK_HAS_NOTHROW_COPY;
6537 break;
6538 case RID_HAS_TRIVIAL_ASSIGN:
6539 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6540 break;
6541 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6542 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6543 break;
6544 case RID_HAS_TRIVIAL_COPY:
6545 kind = CPTK_HAS_TRIVIAL_COPY;
6546 break;
6547 case RID_HAS_TRIVIAL_DESTRUCTOR:
6548 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6549 break;
6550 case RID_HAS_VIRTUAL_DESTRUCTOR:
6551 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6552 break;
6553 case RID_IS_ABSTRACT:
6554 kind = CPTK_IS_ABSTRACT;
6555 break;
6556 case RID_IS_BASE_OF:
6557 kind = CPTK_IS_BASE_OF;
6558 binary = true;
6559 break;
6560 case RID_IS_CLASS:
6561 kind = CPTK_IS_CLASS;
6562 break;
6563 case RID_IS_CONVERTIBLE_TO:
6564 kind = CPTK_IS_CONVERTIBLE_TO;
6565 binary = true;
6566 break;
6567 case RID_IS_EMPTY:
6568 kind = CPTK_IS_EMPTY;
6569 break;
6570 case RID_IS_ENUM:
6571 kind = CPTK_IS_ENUM;
6572 break;
6573 case RID_IS_POD:
6574 kind = CPTK_IS_POD;
6575 break;
6576 case RID_IS_POLYMORPHIC:
6577 kind = CPTK_IS_POLYMORPHIC;
6578 break;
6579 case RID_IS_UNION:
6580 kind = CPTK_IS_UNION;
6581 break;
6582 default:
6583 gcc_unreachable ();
6586 /* Consume the token. */
6587 cp_lexer_consume_token (parser->lexer);
6589 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6591 type1 = cp_parser_type_id (parser);
6593 if (type1 == error_mark_node)
6594 return error_mark_node;
6596 /* Build a trivial decl-specifier-seq. */
6597 clear_decl_specs (&decl_specs);
6598 decl_specs.type = type1;
6600 /* Call grokdeclarator to figure out what type this is. */
6601 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6602 /*initialized=*/0, /*attrlist=*/NULL);
6604 if (binary)
6606 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6608 type2 = cp_parser_type_id (parser);
6610 if (type2 == error_mark_node)
6611 return error_mark_node;
6613 /* Build a trivial decl-specifier-seq. */
6614 clear_decl_specs (&decl_specs);
6615 decl_specs.type = type2;
6617 /* Call grokdeclarator to figure out what type this is. */
6618 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6619 /*initialized=*/0, /*attrlist=*/NULL);
6622 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6624 /* Complete the trait expression, which may mean either processing
6625 the trait expr now or saving it for template instantiation. */
6626 return finish_trait_expr (kind, type1, type2);
6629 /* Statements [gram.stmt.stmt] */
6631 /* Parse a statement.
6633 statement:
6634 labeled-statement
6635 expression-statement
6636 compound-statement
6637 selection-statement
6638 iteration-statement
6639 jump-statement
6640 declaration-statement
6641 try-block
6643 IN_COMPOUND is true when the statement is nested inside a
6644 cp_parser_compound_statement; this matters for certain pragmas.
6646 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6647 is a (possibly labeled) if statement which is not enclosed in braces
6648 and has an else clause. This is used to implement -Wparentheses. */
6650 static void
6651 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6652 bool in_compound, bool *if_p)
6654 tree statement;
6655 cp_token *token;
6656 location_t statement_location;
6658 restart:
6659 if (if_p != NULL)
6660 *if_p = false;
6661 /* There is no statement yet. */
6662 statement = NULL_TREE;
6663 /* Peek at the next token. */
6664 token = cp_lexer_peek_token (parser->lexer);
6665 /* Remember the location of the first token in the statement. */
6666 statement_location = token->location;
6667 /* If this is a keyword, then that will often determine what kind of
6668 statement we have. */
6669 if (token->type == CPP_KEYWORD)
6671 enum rid keyword = token->keyword;
6673 switch (keyword)
6675 case RID_CASE:
6676 case RID_DEFAULT:
6677 /* Looks like a labeled-statement with a case label.
6678 Parse the label, and then use tail recursion to parse
6679 the statement. */
6680 cp_parser_label_for_labeled_statement (parser);
6681 goto restart;
6683 case RID_IF:
6684 case RID_SWITCH:
6685 statement = cp_parser_selection_statement (parser, if_p);
6686 break;
6688 case RID_WHILE:
6689 case RID_DO:
6690 case RID_FOR:
6691 statement = cp_parser_iteration_statement (parser);
6692 break;
6694 case RID_BREAK:
6695 case RID_CONTINUE:
6696 case RID_RETURN:
6697 case RID_GOTO:
6698 statement = cp_parser_jump_statement (parser);
6699 break;
6701 /* Objective-C++ exception-handling constructs. */
6702 case RID_AT_TRY:
6703 case RID_AT_CATCH:
6704 case RID_AT_FINALLY:
6705 case RID_AT_SYNCHRONIZED:
6706 case RID_AT_THROW:
6707 statement = cp_parser_objc_statement (parser);
6708 break;
6710 case RID_TRY:
6711 statement = cp_parser_try_block (parser);
6712 break;
6714 case RID_NAMESPACE:
6715 /* This must be a namespace alias definition. */
6716 cp_parser_declaration_statement (parser);
6717 return;
6719 default:
6720 /* It might be a keyword like `int' that can start a
6721 declaration-statement. */
6722 break;
6725 else if (token->type == CPP_NAME)
6727 /* If the next token is a `:', then we are looking at a
6728 labeled-statement. */
6729 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6730 if (token->type == CPP_COLON)
6732 /* Looks like a labeled-statement with an ordinary label.
6733 Parse the label, and then use tail recursion to parse
6734 the statement. */
6735 cp_parser_label_for_labeled_statement (parser);
6736 goto restart;
6739 /* Anything that starts with a `{' must be a compound-statement. */
6740 else if (token->type == CPP_OPEN_BRACE)
6741 statement = cp_parser_compound_statement (parser, NULL, false);
6742 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6743 a statement all its own. */
6744 else if (token->type == CPP_PRAGMA)
6746 /* Only certain OpenMP pragmas are attached to statements, and thus
6747 are considered statements themselves. All others are not. In
6748 the context of a compound, accept the pragma as a "statement" and
6749 return so that we can check for a close brace. Otherwise we
6750 require a real statement and must go back and read one. */
6751 if (in_compound)
6752 cp_parser_pragma (parser, pragma_compound);
6753 else if (!cp_parser_pragma (parser, pragma_stmt))
6754 goto restart;
6755 return;
6757 else if (token->type == CPP_EOF)
6759 cp_parser_error (parser, "expected statement");
6760 return;
6763 /* Everything else must be a declaration-statement or an
6764 expression-statement. Try for the declaration-statement
6765 first, unless we are looking at a `;', in which case we know that
6766 we have an expression-statement. */
6767 if (!statement)
6769 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6771 cp_parser_parse_tentatively (parser);
6772 /* Try to parse the declaration-statement. */
6773 cp_parser_declaration_statement (parser);
6774 /* If that worked, we're done. */
6775 if (cp_parser_parse_definitely (parser))
6776 return;
6778 /* Look for an expression-statement instead. */
6779 statement = cp_parser_expression_statement (parser, in_statement_expr);
6782 /* Set the line number for the statement. */
6783 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6784 SET_EXPR_LOCATION (statement, statement_location);
6787 /* Parse the label for a labeled-statement, i.e.
6789 identifier :
6790 case constant-expression :
6791 default :
6793 GNU Extension:
6794 case constant-expression ... constant-expression : statement
6796 When a label is parsed without errors, the label is added to the
6797 parse tree by the finish_* functions, so this function doesn't
6798 have to return the label. */
6800 static void
6801 cp_parser_label_for_labeled_statement (cp_parser* parser)
6803 cp_token *token;
6805 /* The next token should be an identifier. */
6806 token = cp_lexer_peek_token (parser->lexer);
6807 if (token->type != CPP_NAME
6808 && token->type != CPP_KEYWORD)
6810 cp_parser_error (parser, "expected labeled-statement");
6811 return;
6814 switch (token->keyword)
6816 case RID_CASE:
6818 tree expr, expr_hi;
6819 cp_token *ellipsis;
6821 /* Consume the `case' token. */
6822 cp_lexer_consume_token (parser->lexer);
6823 /* Parse the constant-expression. */
6824 expr = cp_parser_constant_expression (parser,
6825 /*allow_non_constant_p=*/false,
6826 NULL);
6828 ellipsis = cp_lexer_peek_token (parser->lexer);
6829 if (ellipsis->type == CPP_ELLIPSIS)
6831 /* Consume the `...' token. */
6832 cp_lexer_consume_token (parser->lexer);
6833 expr_hi =
6834 cp_parser_constant_expression (parser,
6835 /*allow_non_constant_p=*/false,
6836 NULL);
6837 /* We don't need to emit warnings here, as the common code
6838 will do this for us. */
6840 else
6841 expr_hi = NULL_TREE;
6843 if (parser->in_switch_statement_p)
6844 finish_case_label (expr, expr_hi);
6845 else
6846 error ("case label %qE not within a switch statement", expr);
6848 break;
6850 case RID_DEFAULT:
6851 /* Consume the `default' token. */
6852 cp_lexer_consume_token (parser->lexer);
6854 if (parser->in_switch_statement_p)
6855 finish_case_label (NULL_TREE, NULL_TREE);
6856 else
6857 error ("case label not within a switch statement");
6858 break;
6860 default:
6861 /* Anything else must be an ordinary label. */
6862 finish_label_stmt (cp_parser_identifier (parser));
6863 break;
6866 /* Require the `:' token. */
6867 cp_parser_require (parser, CPP_COLON, "%<:%>");
6870 /* Parse an expression-statement.
6872 expression-statement:
6873 expression [opt] ;
6875 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6876 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6877 indicates whether this expression-statement is part of an
6878 expression statement. */
6880 static tree
6881 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6883 tree statement = NULL_TREE;
6885 /* If the next token is a ';', then there is no expression
6886 statement. */
6887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6888 statement = cp_parser_expression (parser, /*cast_p=*/false);
6890 /* Consume the final `;'. */
6891 cp_parser_consume_semicolon_at_end_of_statement (parser);
6893 if (in_statement_expr
6894 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6895 /* This is the final expression statement of a statement
6896 expression. */
6897 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6898 else if (statement)
6899 statement = finish_expr_stmt (statement);
6900 else
6901 finish_stmt ();
6903 return statement;
6906 /* Parse a compound-statement.
6908 compound-statement:
6909 { statement-seq [opt] }
6911 GNU extension:
6913 compound-statement:
6914 { label-declaration-seq [opt] statement-seq [opt] }
6916 label-declaration-seq:
6917 label-declaration
6918 label-declaration-seq label-declaration
6920 Returns a tree representing the statement. */
6922 static tree
6923 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6924 bool in_try)
6926 tree compound_stmt;
6928 /* Consume the `{'. */
6929 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
6930 return error_mark_node;
6931 /* Begin the compound-statement. */
6932 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6933 /* If the next keyword is `__label__' we have a label declaration. */
6934 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6935 cp_parser_label_declaration (parser);
6936 /* Parse an (optional) statement-seq. */
6937 cp_parser_statement_seq_opt (parser, in_statement_expr);
6938 /* Finish the compound-statement. */
6939 finish_compound_stmt (compound_stmt);
6940 /* Consume the `}'. */
6941 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
6943 return compound_stmt;
6946 /* Parse an (optional) statement-seq.
6948 statement-seq:
6949 statement
6950 statement-seq [opt] statement */
6952 static void
6953 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6955 /* Scan statements until there aren't any more. */
6956 while (true)
6958 cp_token *token = cp_lexer_peek_token (parser->lexer);
6960 /* If we're looking at a `}', then we've run out of statements. */
6961 if (token->type == CPP_CLOSE_BRACE
6962 || token->type == CPP_EOF
6963 || token->type == CPP_PRAGMA_EOL)
6964 break;
6966 /* If we are in a compound statement and find 'else' then
6967 something went wrong. */
6968 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6970 if (parser->in_statement & IN_IF_STMT)
6971 break;
6972 else
6974 token = cp_lexer_consume_token (parser->lexer);
6975 error ("%<else%> without a previous %<if%>");
6979 /* Parse the statement. */
6980 cp_parser_statement (parser, in_statement_expr, true, NULL);
6984 /* Parse a selection-statement.
6986 selection-statement:
6987 if ( condition ) statement
6988 if ( condition ) statement else statement
6989 switch ( condition ) statement
6991 Returns the new IF_STMT or SWITCH_STMT.
6993 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6994 is a (possibly labeled) if statement which is not enclosed in
6995 braces and has an else clause. This is used to implement
6996 -Wparentheses. */
6998 static tree
6999 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7001 cp_token *token;
7002 enum rid keyword;
7004 if (if_p != NULL)
7005 *if_p = false;
7007 /* Peek at the next token. */
7008 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7010 /* See what kind of keyword it is. */
7011 keyword = token->keyword;
7012 switch (keyword)
7014 case RID_IF:
7015 case RID_SWITCH:
7017 tree statement;
7018 tree condition;
7020 /* Look for the `('. */
7021 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7023 cp_parser_skip_to_end_of_statement (parser);
7024 return error_mark_node;
7027 /* Begin the selection-statement. */
7028 if (keyword == RID_IF)
7029 statement = begin_if_stmt ();
7030 else
7031 statement = begin_switch_stmt ();
7033 /* Parse the condition. */
7034 condition = cp_parser_condition (parser);
7035 /* Look for the `)'. */
7036 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7037 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7038 /*consume_paren=*/true);
7040 if (keyword == RID_IF)
7042 bool nested_if;
7043 unsigned char in_statement;
7045 /* Add the condition. */
7046 finish_if_stmt_cond (condition, statement);
7048 /* Parse the then-clause. */
7049 in_statement = parser->in_statement;
7050 parser->in_statement |= IN_IF_STMT;
7051 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7052 parser->in_statement = in_statement;
7054 finish_then_clause (statement);
7056 /* If the next token is `else', parse the else-clause. */
7057 if (cp_lexer_next_token_is_keyword (parser->lexer,
7058 RID_ELSE))
7060 /* Consume the `else' keyword. */
7061 cp_lexer_consume_token (parser->lexer);
7062 begin_else_clause (statement);
7063 /* Parse the else-clause. */
7064 cp_parser_implicitly_scoped_statement (parser, NULL);
7065 finish_else_clause (statement);
7067 /* If we are currently parsing a then-clause, then
7068 IF_P will not be NULL. We set it to true to
7069 indicate that this if statement has an else clause.
7070 This may trigger the Wparentheses warning below
7071 when we get back up to the parent if statement. */
7072 if (if_p != NULL)
7073 *if_p = true;
7075 else
7077 /* This if statement does not have an else clause. If
7078 NESTED_IF is true, then the then-clause is an if
7079 statement which does have an else clause. We warn
7080 about the potential ambiguity. */
7081 if (nested_if)
7082 warning (OPT_Wparentheses,
7083 ("%Hsuggest explicit braces "
7084 "to avoid ambiguous %<else%>"),
7085 EXPR_LOCUS (statement));
7088 /* Now we're all done with the if-statement. */
7089 finish_if_stmt (statement);
7091 else
7093 bool in_switch_statement_p;
7094 unsigned char in_statement;
7096 /* Add the condition. */
7097 finish_switch_cond (condition, statement);
7099 /* Parse the body of the switch-statement. */
7100 in_switch_statement_p = parser->in_switch_statement_p;
7101 in_statement = parser->in_statement;
7102 parser->in_switch_statement_p = true;
7103 parser->in_statement |= IN_SWITCH_STMT;
7104 cp_parser_implicitly_scoped_statement (parser, NULL);
7105 parser->in_switch_statement_p = in_switch_statement_p;
7106 parser->in_statement = in_statement;
7108 /* Now we're all done with the switch-statement. */
7109 finish_switch_stmt (statement);
7112 return statement;
7114 break;
7116 default:
7117 cp_parser_error (parser, "expected selection-statement");
7118 return error_mark_node;
7122 /* Parse a condition.
7124 condition:
7125 expression
7126 type-specifier-seq declarator = assignment-expression
7128 GNU Extension:
7130 condition:
7131 type-specifier-seq declarator asm-specification [opt]
7132 attributes [opt] = assignment-expression
7134 Returns the expression that should be tested. */
7136 static tree
7137 cp_parser_condition (cp_parser* parser)
7139 cp_decl_specifier_seq type_specifiers;
7140 const char *saved_message;
7142 /* Try the declaration first. */
7143 cp_parser_parse_tentatively (parser);
7144 /* New types are not allowed in the type-specifier-seq for a
7145 condition. */
7146 saved_message = parser->type_definition_forbidden_message;
7147 parser->type_definition_forbidden_message
7148 = "types may not be defined in conditions";
7149 /* Parse the type-specifier-seq. */
7150 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7151 &type_specifiers);
7152 /* Restore the saved message. */
7153 parser->type_definition_forbidden_message = saved_message;
7154 /* If all is well, we might be looking at a declaration. */
7155 if (!cp_parser_error_occurred (parser))
7157 tree decl;
7158 tree asm_specification;
7159 tree attributes;
7160 cp_declarator *declarator;
7161 tree initializer = NULL_TREE;
7163 /* Parse the declarator. */
7164 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7165 /*ctor_dtor_or_conv_p=*/NULL,
7166 /*parenthesized_p=*/NULL,
7167 /*member_p=*/false);
7168 /* Parse the attributes. */
7169 attributes = cp_parser_attributes_opt (parser);
7170 /* Parse the asm-specification. */
7171 asm_specification = cp_parser_asm_specification_opt (parser);
7172 /* If the next token is not an `=', then we might still be
7173 looking at an expression. For example:
7175 if (A(a).x)
7177 looks like a decl-specifier-seq and a declarator -- but then
7178 there is no `=', so this is an expression. */
7179 cp_parser_require (parser, CPP_EQ, "%<=%>");
7180 /* If we did see an `=', then we are looking at a declaration
7181 for sure. */
7182 if (cp_parser_parse_definitely (parser))
7184 tree pushed_scope;
7185 bool non_constant_p;
7187 /* Create the declaration. */
7188 decl = start_decl (declarator, &type_specifiers,
7189 /*initialized_p=*/true,
7190 attributes, /*prefix_attributes=*/NULL_TREE,
7191 &pushed_scope);
7192 /* Parse the assignment-expression. */
7193 initializer
7194 = cp_parser_constant_expression (parser,
7195 /*allow_non_constant_p=*/true,
7196 &non_constant_p);
7197 if (!non_constant_p)
7198 initializer = fold_non_dependent_expr (initializer);
7200 /* Process the initializer. */
7201 cp_finish_decl (decl,
7202 initializer, !non_constant_p,
7203 asm_specification,
7204 LOOKUP_ONLYCONVERTING);
7206 if (pushed_scope)
7207 pop_scope (pushed_scope);
7209 return convert_from_reference (decl);
7212 /* If we didn't even get past the declarator successfully, we are
7213 definitely not looking at a declaration. */
7214 else
7215 cp_parser_abort_tentative_parse (parser);
7217 /* Otherwise, we are looking at an expression. */
7218 return cp_parser_expression (parser, /*cast_p=*/false);
7221 /* We check for a ) immediately followed by ; with no whitespacing
7222 between. This is used to issue a warning for:
7224 while (...);
7226 and:
7228 for (...);
7230 as the semicolon is probably extraneous.
7232 On parse errors, the next token might not be a ), so do nothing in
7233 that case. */
7235 static void
7236 check_empty_body (cp_parser* parser, const char* type)
7238 cp_token *token;
7239 cp_token *close_paren;
7240 expanded_location close_loc;
7241 expanded_location semi_loc;
7243 close_paren = cp_lexer_peek_token (parser->lexer);
7244 if (close_paren->type != CPP_CLOSE_PAREN)
7245 return;
7247 close_loc = expand_location (close_paren->location);
7248 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7250 if (token->type != CPP_SEMICOLON
7251 || (token->flags & PREV_WHITE))
7252 return;
7254 semi_loc = expand_location (token->location);
7255 if (close_loc.line == semi_loc.line
7256 && close_loc.column+1 == semi_loc.column)
7257 warning (OPT_Wempty_body,
7258 "suggest a space before %<;%> or explicit braces around empty "
7259 "body in %<%s%> statement",
7260 type);
7263 /* Parse an iteration-statement.
7265 iteration-statement:
7266 while ( condition ) statement
7267 do statement while ( expression ) ;
7268 for ( for-init-statement condition [opt] ; expression [opt] )
7269 statement
7271 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7273 static tree
7274 cp_parser_iteration_statement (cp_parser* parser)
7276 cp_token *token;
7277 enum rid keyword;
7278 tree statement;
7279 unsigned char in_statement;
7281 /* Peek at the next token. */
7282 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7283 if (!token)
7284 return error_mark_node;
7286 /* Remember whether or not we are already within an iteration
7287 statement. */
7288 in_statement = parser->in_statement;
7290 /* See what kind of keyword it is. */
7291 keyword = token->keyword;
7292 switch (keyword)
7294 case RID_WHILE:
7296 tree condition;
7298 /* Begin the while-statement. */
7299 statement = begin_while_stmt ();
7300 /* Look for the `('. */
7301 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7302 /* Parse the condition. */
7303 condition = cp_parser_condition (parser);
7304 finish_while_stmt_cond (condition, statement);
7305 check_empty_body (parser, "while");
7306 /* Look for the `)'. */
7307 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7308 /* Parse the dependent statement. */
7309 parser->in_statement = IN_ITERATION_STMT;
7310 cp_parser_already_scoped_statement (parser);
7311 parser->in_statement = in_statement;
7312 /* We're done with the while-statement. */
7313 finish_while_stmt (statement);
7315 break;
7317 case RID_DO:
7319 tree expression;
7321 /* Begin the do-statement. */
7322 statement = begin_do_stmt ();
7323 /* Parse the body of the do-statement. */
7324 parser->in_statement = IN_ITERATION_STMT;
7325 cp_parser_implicitly_scoped_statement (parser, NULL);
7326 parser->in_statement = in_statement;
7327 finish_do_body (statement);
7328 /* Look for the `while' keyword. */
7329 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7330 /* Look for the `('. */
7331 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7332 /* Parse the expression. */
7333 expression = cp_parser_expression (parser, /*cast_p=*/false);
7334 /* We're done with the do-statement. */
7335 finish_do_stmt (expression, statement);
7336 /* Look for the `)'. */
7337 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7338 /* Look for the `;'. */
7339 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7341 break;
7343 case RID_FOR:
7345 tree condition = NULL_TREE;
7346 tree expression = NULL_TREE;
7348 /* Begin the for-statement. */
7349 statement = begin_for_stmt ();
7350 /* Look for the `('. */
7351 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7352 /* Parse the initialization. */
7353 cp_parser_for_init_statement (parser);
7354 finish_for_init_stmt (statement);
7356 /* If there's a condition, process it. */
7357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7358 condition = cp_parser_condition (parser);
7359 finish_for_cond (condition, statement);
7360 /* Look for the `;'. */
7361 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7363 /* If there's an expression, process it. */
7364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7365 expression = cp_parser_expression (parser, /*cast_p=*/false);
7366 finish_for_expr (expression, statement);
7367 check_empty_body (parser, "for");
7368 /* Look for the `)'. */
7369 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7371 /* Parse the body of the for-statement. */
7372 parser->in_statement = IN_ITERATION_STMT;
7373 cp_parser_already_scoped_statement (parser);
7374 parser->in_statement = in_statement;
7376 /* We're done with the for-statement. */
7377 finish_for_stmt (statement);
7379 break;
7381 default:
7382 cp_parser_error (parser, "expected iteration-statement");
7383 statement = error_mark_node;
7384 break;
7387 return statement;
7390 /* Parse a for-init-statement.
7392 for-init-statement:
7393 expression-statement
7394 simple-declaration */
7396 static void
7397 cp_parser_for_init_statement (cp_parser* parser)
7399 /* If the next token is a `;', then we have an empty
7400 expression-statement. Grammatically, this is also a
7401 simple-declaration, but an invalid one, because it does not
7402 declare anything. Therefore, if we did not handle this case
7403 specially, we would issue an error message about an invalid
7404 declaration. */
7405 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7407 /* We're going to speculatively look for a declaration, falling back
7408 to an expression, if necessary. */
7409 cp_parser_parse_tentatively (parser);
7410 /* Parse the declaration. */
7411 cp_parser_simple_declaration (parser,
7412 /*function_definition_allowed_p=*/false);
7413 /* If the tentative parse failed, then we shall need to look for an
7414 expression-statement. */
7415 if (cp_parser_parse_definitely (parser))
7416 return;
7419 cp_parser_expression_statement (parser, false);
7422 /* Parse a jump-statement.
7424 jump-statement:
7425 break ;
7426 continue ;
7427 return expression [opt] ;
7428 goto identifier ;
7430 GNU extension:
7432 jump-statement:
7433 goto * expression ;
7435 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7437 static tree
7438 cp_parser_jump_statement (cp_parser* parser)
7440 tree statement = error_mark_node;
7441 cp_token *token;
7442 enum rid keyword;
7443 unsigned char in_statement;
7445 /* Peek at the next token. */
7446 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7447 if (!token)
7448 return error_mark_node;
7450 /* See what kind of keyword it is. */
7451 keyword = token->keyword;
7452 switch (keyword)
7454 case RID_BREAK:
7455 in_statement = parser->in_statement & ~IN_IF_STMT;
7456 switch (in_statement)
7458 case 0:
7459 error ("break statement not within loop or switch");
7460 break;
7461 default:
7462 gcc_assert ((in_statement & IN_SWITCH_STMT)
7463 || in_statement == IN_ITERATION_STMT);
7464 statement = finish_break_stmt ();
7465 break;
7466 case IN_OMP_BLOCK:
7467 error ("invalid exit from OpenMP structured block");
7468 break;
7469 case IN_OMP_FOR:
7470 error ("break statement used with OpenMP for loop");
7471 break;
7473 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7474 break;
7476 case RID_CONTINUE:
7477 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7479 case 0:
7480 error ("continue statement not within a loop");
7481 break;
7482 case IN_ITERATION_STMT:
7483 case IN_OMP_FOR:
7484 statement = finish_continue_stmt ();
7485 break;
7486 case IN_OMP_BLOCK:
7487 error ("invalid exit from OpenMP structured block");
7488 break;
7489 default:
7490 gcc_unreachable ();
7492 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7493 break;
7495 case RID_RETURN:
7497 tree expr;
7499 /* If the next token is a `;', then there is no
7500 expression. */
7501 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7502 expr = cp_parser_expression (parser, /*cast_p=*/false);
7503 else
7504 expr = NULL_TREE;
7505 /* Build the return-statement. */
7506 statement = finish_return_stmt (expr);
7507 /* Look for the final `;'. */
7508 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7510 break;
7512 case RID_GOTO:
7513 /* Create the goto-statement. */
7514 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7516 /* Issue a warning about this use of a GNU extension. */
7517 if (pedantic)
7518 pedwarn ("ISO C++ forbids computed gotos");
7519 /* Consume the '*' token. */
7520 cp_lexer_consume_token (parser->lexer);
7521 /* Parse the dependent expression. */
7522 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7524 else
7525 finish_goto_stmt (cp_parser_identifier (parser));
7526 /* Look for the final `;'. */
7527 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7528 break;
7530 default:
7531 cp_parser_error (parser, "expected jump-statement");
7532 break;
7535 return statement;
7538 /* Parse a declaration-statement.
7540 declaration-statement:
7541 block-declaration */
7543 static void
7544 cp_parser_declaration_statement (cp_parser* parser)
7546 void *p;
7548 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7549 p = obstack_alloc (&declarator_obstack, 0);
7551 /* Parse the block-declaration. */
7552 cp_parser_block_declaration (parser, /*statement_p=*/true);
7554 /* Free any declarators allocated. */
7555 obstack_free (&declarator_obstack, p);
7557 /* Finish off the statement. */
7558 finish_stmt ();
7561 /* Some dependent statements (like `if (cond) statement'), are
7562 implicitly in their own scope. In other words, if the statement is
7563 a single statement (as opposed to a compound-statement), it is
7564 none-the-less treated as if it were enclosed in braces. Any
7565 declarations appearing in the dependent statement are out of scope
7566 after control passes that point. This function parses a statement,
7567 but ensures that is in its own scope, even if it is not a
7568 compound-statement.
7570 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7571 is a (possibly labeled) if statement which is not enclosed in
7572 braces and has an else clause. This is used to implement
7573 -Wparentheses.
7575 Returns the new statement. */
7577 static tree
7578 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7580 tree statement;
7582 if (if_p != NULL)
7583 *if_p = false;
7585 /* Mark if () ; with a special NOP_EXPR. */
7586 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7588 cp_lexer_consume_token (parser->lexer);
7589 statement = add_stmt (build_empty_stmt ());
7591 /* if a compound is opened, we simply parse the statement directly. */
7592 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7593 statement = cp_parser_compound_statement (parser, NULL, false);
7594 /* If the token is not a `{', then we must take special action. */
7595 else
7597 /* Create a compound-statement. */
7598 statement = begin_compound_stmt (0);
7599 /* Parse the dependent-statement. */
7600 cp_parser_statement (parser, NULL_TREE, false, if_p);
7601 /* Finish the dummy compound-statement. */
7602 finish_compound_stmt (statement);
7605 /* Return the statement. */
7606 return statement;
7609 /* For some dependent statements (like `while (cond) statement'), we
7610 have already created a scope. Therefore, even if the dependent
7611 statement is a compound-statement, we do not want to create another
7612 scope. */
7614 static void
7615 cp_parser_already_scoped_statement (cp_parser* parser)
7617 /* If the token is a `{', then we must take special action. */
7618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7619 cp_parser_statement (parser, NULL_TREE, false, NULL);
7620 else
7622 /* Avoid calling cp_parser_compound_statement, so that we
7623 don't create a new scope. Do everything else by hand. */
7624 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7625 cp_parser_statement_seq_opt (parser, NULL_TREE);
7626 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7630 /* Declarations [gram.dcl.dcl] */
7632 /* Parse an optional declaration-sequence.
7634 declaration-seq:
7635 declaration
7636 declaration-seq declaration */
7638 static void
7639 cp_parser_declaration_seq_opt (cp_parser* parser)
7641 while (true)
7643 cp_token *token;
7645 token = cp_lexer_peek_token (parser->lexer);
7647 if (token->type == CPP_CLOSE_BRACE
7648 || token->type == CPP_EOF
7649 || token->type == CPP_PRAGMA_EOL)
7650 break;
7652 if (token->type == CPP_SEMICOLON)
7654 /* A declaration consisting of a single semicolon is
7655 invalid. Allow it unless we're being pedantic. */
7656 cp_lexer_consume_token (parser->lexer);
7657 if (pedantic && !in_system_header)
7658 pedwarn ("extra %<;%>");
7659 continue;
7662 /* If we're entering or exiting a region that's implicitly
7663 extern "C", modify the lang context appropriately. */
7664 if (!parser->implicit_extern_c && token->implicit_extern_c)
7666 push_lang_context (lang_name_c);
7667 parser->implicit_extern_c = true;
7669 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7671 pop_lang_context ();
7672 parser->implicit_extern_c = false;
7675 if (token->type == CPP_PRAGMA)
7677 /* A top-level declaration can consist solely of a #pragma.
7678 A nested declaration cannot, so this is done here and not
7679 in cp_parser_declaration. (A #pragma at block scope is
7680 handled in cp_parser_statement.) */
7681 cp_parser_pragma (parser, pragma_external);
7682 continue;
7685 /* Parse the declaration itself. */
7686 cp_parser_declaration (parser);
7690 /* Parse a declaration.
7692 declaration:
7693 block-declaration
7694 function-definition
7695 template-declaration
7696 explicit-instantiation
7697 explicit-specialization
7698 linkage-specification
7699 namespace-definition
7701 GNU extension:
7703 declaration:
7704 __extension__ declaration */
7706 static void
7707 cp_parser_declaration (cp_parser* parser)
7709 cp_token token1;
7710 cp_token token2;
7711 int saved_pedantic;
7712 void *p;
7714 /* Check for the `__extension__' keyword. */
7715 if (cp_parser_extension_opt (parser, &saved_pedantic))
7717 /* Parse the qualified declaration. */
7718 cp_parser_declaration (parser);
7719 /* Restore the PEDANTIC flag. */
7720 pedantic = saved_pedantic;
7722 return;
7725 /* Try to figure out what kind of declaration is present. */
7726 token1 = *cp_lexer_peek_token (parser->lexer);
7728 if (token1.type != CPP_EOF)
7729 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7730 else
7732 token2.type = CPP_EOF;
7733 token2.keyword = RID_MAX;
7736 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7737 p = obstack_alloc (&declarator_obstack, 0);
7739 /* If the next token is `extern' and the following token is a string
7740 literal, then we have a linkage specification. */
7741 if (token1.keyword == RID_EXTERN
7742 && cp_parser_is_string_literal (&token2))
7743 cp_parser_linkage_specification (parser);
7744 /* If the next token is `template', then we have either a template
7745 declaration, an explicit instantiation, or an explicit
7746 specialization. */
7747 else if (token1.keyword == RID_TEMPLATE)
7749 /* `template <>' indicates a template specialization. */
7750 if (token2.type == CPP_LESS
7751 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7752 cp_parser_explicit_specialization (parser);
7753 /* `template <' indicates a template declaration. */
7754 else if (token2.type == CPP_LESS)
7755 cp_parser_template_declaration (parser, /*member_p=*/false);
7756 /* Anything else must be an explicit instantiation. */
7757 else
7758 cp_parser_explicit_instantiation (parser);
7760 /* If the next token is `export', then we have a template
7761 declaration. */
7762 else if (token1.keyword == RID_EXPORT)
7763 cp_parser_template_declaration (parser, /*member_p=*/false);
7764 /* If the next token is `extern', 'static' or 'inline' and the one
7765 after that is `template', we have a GNU extended explicit
7766 instantiation directive. */
7767 else if (cp_parser_allow_gnu_extensions_p (parser)
7768 && (token1.keyword == RID_EXTERN
7769 || token1.keyword == RID_STATIC
7770 || token1.keyword == RID_INLINE)
7771 && token2.keyword == RID_TEMPLATE)
7772 cp_parser_explicit_instantiation (parser);
7773 /* If the next token is `namespace', check for a named or unnamed
7774 namespace definition. */
7775 else if (token1.keyword == RID_NAMESPACE
7776 && (/* A named namespace definition. */
7777 (token2.type == CPP_NAME
7778 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7779 != CPP_EQ))
7780 /* An unnamed namespace definition. */
7781 || token2.type == CPP_OPEN_BRACE
7782 || token2.keyword == RID_ATTRIBUTE))
7783 cp_parser_namespace_definition (parser);
7784 /* An inline (associated) namespace definition. */
7785 else if (token1.keyword == RID_INLINE
7786 && token2.keyword == RID_NAMESPACE)
7787 cp_parser_namespace_definition (parser);
7788 /* Objective-C++ declaration/definition. */
7789 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7790 cp_parser_objc_declaration (parser);
7791 /* We must have either a block declaration or a function
7792 definition. */
7793 else
7794 /* Try to parse a block-declaration, or a function-definition. */
7795 cp_parser_block_declaration (parser, /*statement_p=*/false);
7797 /* Free any declarators allocated. */
7798 obstack_free (&declarator_obstack, p);
7801 /* Parse a block-declaration.
7803 block-declaration:
7804 simple-declaration
7805 asm-definition
7806 namespace-alias-definition
7807 using-declaration
7808 using-directive
7810 GNU Extension:
7812 block-declaration:
7813 __extension__ block-declaration
7815 C++0x Extension:
7817 block-declaration:
7818 static_assert-declaration
7820 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7821 part of a declaration-statement. */
7823 static void
7824 cp_parser_block_declaration (cp_parser *parser,
7825 bool statement_p)
7827 cp_token *token1;
7828 int saved_pedantic;
7830 /* Check for the `__extension__' keyword. */
7831 if (cp_parser_extension_opt (parser, &saved_pedantic))
7833 /* Parse the qualified declaration. */
7834 cp_parser_block_declaration (parser, statement_p);
7835 /* Restore the PEDANTIC flag. */
7836 pedantic = saved_pedantic;
7838 return;
7841 /* Peek at the next token to figure out which kind of declaration is
7842 present. */
7843 token1 = cp_lexer_peek_token (parser->lexer);
7845 /* If the next keyword is `asm', we have an asm-definition. */
7846 if (token1->keyword == RID_ASM)
7848 if (statement_p)
7849 cp_parser_commit_to_tentative_parse (parser);
7850 cp_parser_asm_definition (parser);
7852 /* If the next keyword is `namespace', we have a
7853 namespace-alias-definition. */
7854 else if (token1->keyword == RID_NAMESPACE)
7855 cp_parser_namespace_alias_definition (parser);
7856 /* If the next keyword is `using', we have either a
7857 using-declaration or a using-directive. */
7858 else if (token1->keyword == RID_USING)
7860 cp_token *token2;
7862 if (statement_p)
7863 cp_parser_commit_to_tentative_parse (parser);
7864 /* If the token after `using' is `namespace', then we have a
7865 using-directive. */
7866 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7867 if (token2->keyword == RID_NAMESPACE)
7868 cp_parser_using_directive (parser);
7869 /* Otherwise, it's a using-declaration. */
7870 else
7871 cp_parser_using_declaration (parser,
7872 /*access_declaration_p=*/false);
7874 /* If the next keyword is `__label__' we have a misplaced label
7875 declaration. */
7876 else if (token1->keyword == RID_LABEL)
7878 cp_lexer_consume_token (parser->lexer);
7879 error ("%<__label__%> not at the beginning of a block");
7880 cp_parser_skip_to_end_of_statement (parser);
7881 /* If the next token is now a `;', consume it. */
7882 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7883 cp_lexer_consume_token (parser->lexer);
7885 /* If the next token is `static_assert' we have a static assertion. */
7886 else if (token1->keyword == RID_STATIC_ASSERT)
7887 cp_parser_static_assert (parser, /*member_p=*/false);
7888 /* Anything else must be a simple-declaration. */
7889 else
7890 cp_parser_simple_declaration (parser, !statement_p);
7893 /* Parse a simple-declaration.
7895 simple-declaration:
7896 decl-specifier-seq [opt] init-declarator-list [opt] ;
7898 init-declarator-list:
7899 init-declarator
7900 init-declarator-list , init-declarator
7902 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7903 function-definition as a simple-declaration. */
7905 static void
7906 cp_parser_simple_declaration (cp_parser* parser,
7907 bool function_definition_allowed_p)
7909 cp_decl_specifier_seq decl_specifiers;
7910 int declares_class_or_enum;
7911 bool saw_declarator;
7913 /* Defer access checks until we know what is being declared; the
7914 checks for names appearing in the decl-specifier-seq should be
7915 done as if we were in the scope of the thing being declared. */
7916 push_deferring_access_checks (dk_deferred);
7918 /* Parse the decl-specifier-seq. We have to keep track of whether
7919 or not the decl-specifier-seq declares a named class or
7920 enumeration type, since that is the only case in which the
7921 init-declarator-list is allowed to be empty.
7923 [dcl.dcl]
7925 In a simple-declaration, the optional init-declarator-list can be
7926 omitted only when declaring a class or enumeration, that is when
7927 the decl-specifier-seq contains either a class-specifier, an
7928 elaborated-type-specifier, or an enum-specifier. */
7929 cp_parser_decl_specifier_seq (parser,
7930 CP_PARSER_FLAGS_OPTIONAL,
7931 &decl_specifiers,
7932 &declares_class_or_enum);
7933 /* We no longer need to defer access checks. */
7934 stop_deferring_access_checks ();
7936 /* In a block scope, a valid declaration must always have a
7937 decl-specifier-seq. By not trying to parse declarators, we can
7938 resolve the declaration/expression ambiguity more quickly. */
7939 if (!function_definition_allowed_p
7940 && !decl_specifiers.any_specifiers_p)
7942 cp_parser_error (parser, "expected declaration");
7943 goto done;
7946 /* If the next two tokens are both identifiers, the code is
7947 erroneous. The usual cause of this situation is code like:
7949 T t;
7951 where "T" should name a type -- but does not. */
7952 if (!decl_specifiers.type
7953 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7955 /* If parsing tentatively, we should commit; we really are
7956 looking at a declaration. */
7957 cp_parser_commit_to_tentative_parse (parser);
7958 /* Give up. */
7959 goto done;
7962 /* If we have seen at least one decl-specifier, and the next token
7963 is not a parenthesis, then we must be looking at a declaration.
7964 (After "int (" we might be looking at a functional cast.) */
7965 if (decl_specifiers.any_specifiers_p
7966 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7967 cp_parser_commit_to_tentative_parse (parser);
7969 /* Keep going until we hit the `;' at the end of the simple
7970 declaration. */
7971 saw_declarator = false;
7972 while (cp_lexer_next_token_is_not (parser->lexer,
7973 CPP_SEMICOLON))
7975 cp_token *token;
7976 bool function_definition_p;
7977 tree decl;
7979 if (saw_declarator)
7981 /* If we are processing next declarator, coma is expected */
7982 token = cp_lexer_peek_token (parser->lexer);
7983 gcc_assert (token->type == CPP_COMMA);
7984 cp_lexer_consume_token (parser->lexer);
7986 else
7987 saw_declarator = true;
7989 /* Parse the init-declarator. */
7990 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7991 /*checks=*/NULL,
7992 function_definition_allowed_p,
7993 /*member_p=*/false,
7994 declares_class_or_enum,
7995 &function_definition_p);
7996 /* If an error occurred while parsing tentatively, exit quickly.
7997 (That usually happens when in the body of a function; each
7998 statement is treated as a declaration-statement until proven
7999 otherwise.) */
8000 if (cp_parser_error_occurred (parser))
8001 goto done;
8002 /* Handle function definitions specially. */
8003 if (function_definition_p)
8005 /* If the next token is a `,', then we are probably
8006 processing something like:
8008 void f() {}, *p;
8010 which is erroneous. */
8011 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8012 error ("mixing declarations and function-definitions is forbidden");
8013 /* Otherwise, we're done with the list of declarators. */
8014 else
8016 pop_deferring_access_checks ();
8017 return;
8020 /* The next token should be either a `,' or a `;'. */
8021 token = cp_lexer_peek_token (parser->lexer);
8022 /* If it's a `,', there are more declarators to come. */
8023 if (token->type == CPP_COMMA)
8024 /* will be consumed next time around */;
8025 /* If it's a `;', we are done. */
8026 else if (token->type == CPP_SEMICOLON)
8027 break;
8028 /* Anything else is an error. */
8029 else
8031 /* If we have already issued an error message we don't need
8032 to issue another one. */
8033 if (decl != error_mark_node
8034 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8035 cp_parser_error (parser, "expected %<,%> or %<;%>");
8036 /* Skip tokens until we reach the end of the statement. */
8037 cp_parser_skip_to_end_of_statement (parser);
8038 /* If the next token is now a `;', consume it. */
8039 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8040 cp_lexer_consume_token (parser->lexer);
8041 goto done;
8043 /* After the first time around, a function-definition is not
8044 allowed -- even if it was OK at first. For example:
8046 int i, f() {}
8048 is not valid. */
8049 function_definition_allowed_p = false;
8052 /* Issue an error message if no declarators are present, and the
8053 decl-specifier-seq does not itself declare a class or
8054 enumeration. */
8055 if (!saw_declarator)
8057 if (cp_parser_declares_only_class_p (parser))
8058 shadow_tag (&decl_specifiers);
8059 /* Perform any deferred access checks. */
8060 perform_deferred_access_checks ();
8063 /* Consume the `;'. */
8064 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8066 done:
8067 pop_deferring_access_checks ();
8070 /* Parse a decl-specifier-seq.
8072 decl-specifier-seq:
8073 decl-specifier-seq [opt] decl-specifier
8075 decl-specifier:
8076 storage-class-specifier
8077 type-specifier
8078 function-specifier
8079 friend
8080 typedef
8082 GNU Extension:
8084 decl-specifier:
8085 attributes
8087 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8089 The parser flags FLAGS is used to control type-specifier parsing.
8091 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8092 flags:
8094 1: one of the decl-specifiers is an elaborated-type-specifier
8095 (i.e., a type declaration)
8096 2: one of the decl-specifiers is an enum-specifier or a
8097 class-specifier (i.e., a type definition)
8101 static void
8102 cp_parser_decl_specifier_seq (cp_parser* parser,
8103 cp_parser_flags flags,
8104 cp_decl_specifier_seq *decl_specs,
8105 int* declares_class_or_enum)
8107 bool constructor_possible_p = !parser->in_declarator_p;
8109 /* Clear DECL_SPECS. */
8110 clear_decl_specs (decl_specs);
8112 /* Assume no class or enumeration type is declared. */
8113 *declares_class_or_enum = 0;
8115 /* Keep reading specifiers until there are no more to read. */
8116 while (true)
8118 bool constructor_p;
8119 bool found_decl_spec;
8120 cp_token *token;
8122 /* Peek at the next token. */
8123 token = cp_lexer_peek_token (parser->lexer);
8124 /* Handle attributes. */
8125 if (token->keyword == RID_ATTRIBUTE)
8127 /* Parse the attributes. */
8128 decl_specs->attributes
8129 = chainon (decl_specs->attributes,
8130 cp_parser_attributes_opt (parser));
8131 continue;
8133 /* Assume we will find a decl-specifier keyword. */
8134 found_decl_spec = true;
8135 /* If the next token is an appropriate keyword, we can simply
8136 add it to the list. */
8137 switch (token->keyword)
8139 /* decl-specifier:
8140 friend */
8141 case RID_FRIEND:
8142 if (!at_class_scope_p ())
8144 error ("%H%<friend%> used outside of class", &token->location);
8145 cp_lexer_purge_token (parser->lexer);
8147 else
8149 ++decl_specs->specs[(int) ds_friend];
8150 /* Consume the token. */
8151 cp_lexer_consume_token (parser->lexer);
8153 break;
8155 /* function-specifier:
8156 inline
8157 virtual
8158 explicit */
8159 case RID_INLINE:
8160 case RID_VIRTUAL:
8161 case RID_EXPLICIT:
8162 cp_parser_function_specifier_opt (parser, decl_specs);
8163 break;
8165 /* decl-specifier:
8166 typedef */
8167 case RID_TYPEDEF:
8168 ++decl_specs->specs[(int) ds_typedef];
8169 /* Consume the token. */
8170 cp_lexer_consume_token (parser->lexer);
8171 /* A constructor declarator cannot appear in a typedef. */
8172 constructor_possible_p = false;
8173 /* The "typedef" keyword can only occur in a declaration; we
8174 may as well commit at this point. */
8175 cp_parser_commit_to_tentative_parse (parser);
8177 if (decl_specs->storage_class != sc_none)
8178 decl_specs->conflicting_specifiers_p = true;
8179 break;
8181 /* storage-class-specifier:
8182 auto
8183 register
8184 static
8185 extern
8186 mutable
8188 GNU Extension:
8189 thread */
8190 case RID_AUTO:
8191 /* Consume the token. */
8192 cp_lexer_consume_token (parser->lexer);
8194 if (cxx_dialect == cxx98)
8196 /* Complain about `auto' as a storage specifier, if
8197 we're complaining about C++0x compatibility. */
8198 warning
8199 (OPT_Wc__0x_compat,
8200 "%<auto%> will change meaning in C++0x; please remove it");
8202 /* Set the storage class anyway. */
8203 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO);
8205 else
8206 /* We do not yet support the use of `auto' as a
8207 type-specifier. */
8208 error ("C++0x %<auto%> specifier not supported");
8209 break;
8211 case RID_REGISTER:
8212 case RID_STATIC:
8213 case RID_EXTERN:
8214 case RID_MUTABLE:
8215 /* Consume the token. */
8216 cp_lexer_consume_token (parser->lexer);
8217 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8218 break;
8219 case RID_THREAD:
8220 /* Consume the token. */
8221 cp_lexer_consume_token (parser->lexer);
8222 ++decl_specs->specs[(int) ds_thread];
8223 break;
8225 default:
8226 /* We did not yet find a decl-specifier yet. */
8227 found_decl_spec = false;
8228 break;
8231 /* Constructors are a special case. The `S' in `S()' is not a
8232 decl-specifier; it is the beginning of the declarator. */
8233 constructor_p
8234 = (!found_decl_spec
8235 && constructor_possible_p
8236 && (cp_parser_constructor_declarator_p
8237 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8239 /* If we don't have a DECL_SPEC yet, then we must be looking at
8240 a type-specifier. */
8241 if (!found_decl_spec && !constructor_p)
8243 int decl_spec_declares_class_or_enum;
8244 bool is_cv_qualifier;
8245 tree type_spec;
8247 type_spec
8248 = cp_parser_type_specifier (parser, flags,
8249 decl_specs,
8250 /*is_declaration=*/true,
8251 &decl_spec_declares_class_or_enum,
8252 &is_cv_qualifier);
8254 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8256 /* If this type-specifier referenced a user-defined type
8257 (a typedef, class-name, etc.), then we can't allow any
8258 more such type-specifiers henceforth.
8260 [dcl.spec]
8262 The longest sequence of decl-specifiers that could
8263 possibly be a type name is taken as the
8264 decl-specifier-seq of a declaration. The sequence shall
8265 be self-consistent as described below.
8267 [dcl.type]
8269 As a general rule, at most one type-specifier is allowed
8270 in the complete decl-specifier-seq of a declaration. The
8271 only exceptions are the following:
8273 -- const or volatile can be combined with any other
8274 type-specifier.
8276 -- signed or unsigned can be combined with char, long,
8277 short, or int.
8279 -- ..
8281 Example:
8283 typedef char* Pc;
8284 void g (const int Pc);
8286 Here, Pc is *not* part of the decl-specifier seq; it's
8287 the declarator. Therefore, once we see a type-specifier
8288 (other than a cv-qualifier), we forbid any additional
8289 user-defined types. We *do* still allow things like `int
8290 int' to be considered a decl-specifier-seq, and issue the
8291 error message later. */
8292 if (type_spec && !is_cv_qualifier)
8293 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8294 /* A constructor declarator cannot follow a type-specifier. */
8295 if (type_spec)
8297 constructor_possible_p = false;
8298 found_decl_spec = true;
8302 /* If we still do not have a DECL_SPEC, then there are no more
8303 decl-specifiers. */
8304 if (!found_decl_spec)
8305 break;
8307 decl_specs->any_specifiers_p = true;
8308 /* After we see one decl-specifier, further decl-specifiers are
8309 always optional. */
8310 flags |= CP_PARSER_FLAGS_OPTIONAL;
8313 cp_parser_check_decl_spec (decl_specs);
8315 /* Don't allow a friend specifier with a class definition. */
8316 if (decl_specs->specs[(int) ds_friend] != 0
8317 && (*declares_class_or_enum & 2))
8318 error ("class definition may not be declared a friend");
8321 /* Parse an (optional) storage-class-specifier.
8323 storage-class-specifier:
8324 auto
8325 register
8326 static
8327 extern
8328 mutable
8330 GNU Extension:
8332 storage-class-specifier:
8333 thread
8335 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8337 static tree
8338 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8340 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8342 case RID_AUTO:
8343 if (cxx_dialect != cxx98)
8344 return NULL_TREE;
8345 /* Fall through for C++98. */
8347 case RID_REGISTER:
8348 case RID_STATIC:
8349 case RID_EXTERN:
8350 case RID_MUTABLE:
8351 case RID_THREAD:
8352 /* Consume the token. */
8353 return cp_lexer_consume_token (parser->lexer)->u.value;
8355 default:
8356 return NULL_TREE;
8360 /* Parse an (optional) function-specifier.
8362 function-specifier:
8363 inline
8364 virtual
8365 explicit
8367 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8368 Updates DECL_SPECS, if it is non-NULL. */
8370 static tree
8371 cp_parser_function_specifier_opt (cp_parser* parser,
8372 cp_decl_specifier_seq *decl_specs)
8374 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8376 case RID_INLINE:
8377 if (decl_specs)
8378 ++decl_specs->specs[(int) ds_inline];
8379 break;
8381 case RID_VIRTUAL:
8382 /* 14.5.2.3 [temp.mem]
8384 A member function template shall not be virtual. */
8385 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8386 error ("templates may not be %<virtual%>");
8387 else if (decl_specs)
8388 ++decl_specs->specs[(int) ds_virtual];
8389 break;
8391 case RID_EXPLICIT:
8392 if (decl_specs)
8393 ++decl_specs->specs[(int) ds_explicit];
8394 break;
8396 default:
8397 return NULL_TREE;
8400 /* Consume the token. */
8401 return cp_lexer_consume_token (parser->lexer)->u.value;
8404 /* Parse a linkage-specification.
8406 linkage-specification:
8407 extern string-literal { declaration-seq [opt] }
8408 extern string-literal declaration */
8410 static void
8411 cp_parser_linkage_specification (cp_parser* parser)
8413 tree linkage;
8415 /* Look for the `extern' keyword. */
8416 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8418 /* Look for the string-literal. */
8419 linkage = cp_parser_string_literal (parser, false, false);
8421 /* Transform the literal into an identifier. If the literal is a
8422 wide-character string, or contains embedded NULs, then we can't
8423 handle it as the user wants. */
8424 if (strlen (TREE_STRING_POINTER (linkage))
8425 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8427 cp_parser_error (parser, "invalid linkage-specification");
8428 /* Assume C++ linkage. */
8429 linkage = lang_name_cplusplus;
8431 else
8432 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8434 /* We're now using the new linkage. */
8435 push_lang_context (linkage);
8437 /* If the next token is a `{', then we're using the first
8438 production. */
8439 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8441 /* Consume the `{' token. */
8442 cp_lexer_consume_token (parser->lexer);
8443 /* Parse the declarations. */
8444 cp_parser_declaration_seq_opt (parser);
8445 /* Look for the closing `}'. */
8446 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8448 /* Otherwise, there's just one declaration. */
8449 else
8451 bool saved_in_unbraced_linkage_specification_p;
8453 saved_in_unbraced_linkage_specification_p
8454 = parser->in_unbraced_linkage_specification_p;
8455 parser->in_unbraced_linkage_specification_p = true;
8456 cp_parser_declaration (parser);
8457 parser->in_unbraced_linkage_specification_p
8458 = saved_in_unbraced_linkage_specification_p;
8461 /* We're done with the linkage-specification. */
8462 pop_lang_context ();
8465 /* Parse a static_assert-declaration.
8467 static_assert-declaration:
8468 static_assert ( constant-expression , string-literal ) ;
8470 If MEMBER_P, this static_assert is a class member. */
8472 static void
8473 cp_parser_static_assert(cp_parser *parser, bool member_p)
8475 tree condition;
8476 tree message;
8477 cp_token *token;
8478 location_t saved_loc;
8480 /* Peek at the `static_assert' token so we can keep track of exactly
8481 where the static assertion started. */
8482 token = cp_lexer_peek_token (parser->lexer);
8483 saved_loc = token->location;
8485 /* Look for the `static_assert' keyword. */
8486 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8487 "%<static_assert%>"))
8488 return;
8490 /* We know we are in a static assertion; commit to any tentative
8491 parse. */
8492 if (cp_parser_parsing_tentatively (parser))
8493 cp_parser_commit_to_tentative_parse (parser);
8495 /* Parse the `(' starting the static assertion condition. */
8496 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8498 /* Parse the constant-expression. */
8499 condition =
8500 cp_parser_constant_expression (parser,
8501 /*allow_non_constant_p=*/false,
8502 /*non_constant_p=*/NULL);
8504 /* Parse the separating `,'. */
8505 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8507 /* Parse the string-literal message. */
8508 message = cp_parser_string_literal (parser,
8509 /*translate=*/false,
8510 /*wide_ok=*/true);
8512 /* A `)' completes the static assertion. */
8513 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8514 cp_parser_skip_to_closing_parenthesis (parser,
8515 /*recovering=*/true,
8516 /*or_comma=*/false,
8517 /*consume_paren=*/true);
8519 /* A semicolon terminates the declaration. */
8520 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8522 /* Complete the static assertion, which may mean either processing
8523 the static assert now or saving it for template instantiation. */
8524 finish_static_assert (condition, message, saved_loc, member_p);
8527 /* Parse a `decltype' type. Returns the type.
8529 simple-type-specifier:
8530 decltype ( expression ) */
8532 static tree
8533 cp_parser_decltype (cp_parser *parser)
8535 tree expr;
8536 bool id_expression_or_member_access_p = false;
8537 const char *saved_message;
8538 bool saved_integral_constant_expression_p;
8539 bool saved_non_integral_constant_expression_p;
8541 /* Look for the `decltype' token. */
8542 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8543 return error_mark_node;
8545 /* Types cannot be defined in a `decltype' expression. Save away the
8546 old message. */
8547 saved_message = parser->type_definition_forbidden_message;
8549 /* And create the new one. */
8550 parser->type_definition_forbidden_message
8551 = "types may not be defined in %<decltype%> expressions";
8553 /* The restrictions on constant-expressions do not apply inside
8554 decltype expressions. */
8555 saved_integral_constant_expression_p
8556 = parser->integral_constant_expression_p;
8557 saved_non_integral_constant_expression_p
8558 = parser->non_integral_constant_expression_p;
8559 parser->integral_constant_expression_p = false;
8561 /* Do not actually evaluate the expression. */
8562 ++skip_evaluation;
8564 /* Parse the opening `('. */
8565 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8566 return error_mark_node;
8568 /* First, try parsing an id-expression. */
8569 cp_parser_parse_tentatively (parser);
8570 expr = cp_parser_id_expression (parser,
8571 /*template_keyword_p=*/false,
8572 /*check_dependency_p=*/true,
8573 /*template_p=*/NULL,
8574 /*declarator_p=*/false,
8575 /*optional_p=*/false);
8577 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8579 bool non_integral_constant_expression_p = false;
8580 tree id_expression = expr;
8581 cp_id_kind idk;
8582 const char *error_msg;
8584 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8585 /* Lookup the name we got back from the id-expression. */
8586 expr = cp_parser_lookup_name (parser, expr,
8587 none_type,
8588 /*is_template=*/false,
8589 /*is_namespace=*/false,
8590 /*check_dependency=*/true,
8591 /*ambiguous_decls=*/NULL);
8593 if (expr
8594 && expr != error_mark_node
8595 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8596 && TREE_CODE (expr) != TYPE_DECL
8597 && (TREE_CODE (expr) != BIT_NOT_EXPR
8598 || !TYPE_P (TREE_OPERAND (expr, 0)))
8599 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8601 /* Complete lookup of the id-expression. */
8602 expr = (finish_id_expression
8603 (id_expression, expr, parser->scope, &idk,
8604 /*integral_constant_expression_p=*/false,
8605 /*allow_non_integral_constant_expression_p=*/true,
8606 &non_integral_constant_expression_p,
8607 /*template_p=*/false,
8608 /*done=*/true,
8609 /*address_p=*/false,
8610 /*template_arg_p=*/false,
8611 &error_msg));
8613 if (expr == error_mark_node)
8614 /* We found an id-expression, but it was something that we
8615 should not have found. This is an error, not something
8616 we can recover from, so note that we found an
8617 id-expression and we'll recover as gracefully as
8618 possible. */
8619 id_expression_or_member_access_p = true;
8622 if (expr
8623 && expr != error_mark_node
8624 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8625 /* We have an id-expression. */
8626 id_expression_or_member_access_p = true;
8629 if (!id_expression_or_member_access_p)
8631 /* Abort the id-expression parse. */
8632 cp_parser_abort_tentative_parse (parser);
8634 /* Parsing tentatively, again. */
8635 cp_parser_parse_tentatively (parser);
8637 /* Parse a class member access. */
8638 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8639 /*cast_p=*/false,
8640 /*member_access_only_p=*/true);
8642 if (expr
8643 && expr != error_mark_node
8644 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8645 /* We have an id-expression. */
8646 id_expression_or_member_access_p = true;
8649 if (id_expression_or_member_access_p)
8650 /* We have parsed the complete id-expression or member access. */
8651 cp_parser_parse_definitely (parser);
8652 else
8654 /* Abort our attempt to parse an id-expression or member access
8655 expression. */
8656 cp_parser_abort_tentative_parse (parser);
8658 /* Parse a full expression. */
8659 expr = cp_parser_expression (parser, /*cast_p=*/false);
8662 /* Go back to evaluating expressions. */
8663 --skip_evaluation;
8665 /* Restore the old message and the integral constant expression
8666 flags. */
8667 parser->type_definition_forbidden_message = saved_message;
8668 parser->integral_constant_expression_p
8669 = saved_integral_constant_expression_p;
8670 parser->non_integral_constant_expression_p
8671 = saved_non_integral_constant_expression_p;
8673 if (expr == error_mark_node)
8675 /* Skip everything up to the closing `)'. */
8676 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8677 /*consume_paren=*/true);
8678 return error_mark_node;
8681 /* Parse to the closing `)'. */
8682 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8684 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8685 /*consume_paren=*/true);
8686 return error_mark_node;
8689 return finish_decltype_type (expr, id_expression_or_member_access_p);
8692 /* Special member functions [gram.special] */
8694 /* Parse a conversion-function-id.
8696 conversion-function-id:
8697 operator conversion-type-id
8699 Returns an IDENTIFIER_NODE representing the operator. */
8701 static tree
8702 cp_parser_conversion_function_id (cp_parser* parser)
8704 tree type;
8705 tree saved_scope;
8706 tree saved_qualifying_scope;
8707 tree saved_object_scope;
8708 tree pushed_scope = NULL_TREE;
8710 /* Look for the `operator' token. */
8711 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8712 return error_mark_node;
8713 /* When we parse the conversion-type-id, the current scope will be
8714 reset. However, we need that information in able to look up the
8715 conversion function later, so we save it here. */
8716 saved_scope = parser->scope;
8717 saved_qualifying_scope = parser->qualifying_scope;
8718 saved_object_scope = parser->object_scope;
8719 /* We must enter the scope of the class so that the names of
8720 entities declared within the class are available in the
8721 conversion-type-id. For example, consider:
8723 struct S {
8724 typedef int I;
8725 operator I();
8728 S::operator I() { ... }
8730 In order to see that `I' is a type-name in the definition, we
8731 must be in the scope of `S'. */
8732 if (saved_scope)
8733 pushed_scope = push_scope (saved_scope);
8734 /* Parse the conversion-type-id. */
8735 type = cp_parser_conversion_type_id (parser);
8736 /* Leave the scope of the class, if any. */
8737 if (pushed_scope)
8738 pop_scope (pushed_scope);
8739 /* Restore the saved scope. */
8740 parser->scope = saved_scope;
8741 parser->qualifying_scope = saved_qualifying_scope;
8742 parser->object_scope = saved_object_scope;
8743 /* If the TYPE is invalid, indicate failure. */
8744 if (type == error_mark_node)
8745 return error_mark_node;
8746 return mangle_conv_op_name_for_type (type);
8749 /* Parse a conversion-type-id:
8751 conversion-type-id:
8752 type-specifier-seq conversion-declarator [opt]
8754 Returns the TYPE specified. */
8756 static tree
8757 cp_parser_conversion_type_id (cp_parser* parser)
8759 tree attributes;
8760 cp_decl_specifier_seq type_specifiers;
8761 cp_declarator *declarator;
8762 tree type_specified;
8764 /* Parse the attributes. */
8765 attributes = cp_parser_attributes_opt (parser);
8766 /* Parse the type-specifiers. */
8767 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8768 &type_specifiers);
8769 /* If that didn't work, stop. */
8770 if (type_specifiers.type == error_mark_node)
8771 return error_mark_node;
8772 /* Parse the conversion-declarator. */
8773 declarator = cp_parser_conversion_declarator_opt (parser);
8775 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8776 /*initialized=*/0, &attributes);
8777 if (attributes)
8778 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8779 return type_specified;
8782 /* Parse an (optional) conversion-declarator.
8784 conversion-declarator:
8785 ptr-operator conversion-declarator [opt]
8789 static cp_declarator *
8790 cp_parser_conversion_declarator_opt (cp_parser* parser)
8792 enum tree_code code;
8793 tree class_type;
8794 cp_cv_quals cv_quals;
8796 /* We don't know if there's a ptr-operator next, or not. */
8797 cp_parser_parse_tentatively (parser);
8798 /* Try the ptr-operator. */
8799 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8800 /* If it worked, look for more conversion-declarators. */
8801 if (cp_parser_parse_definitely (parser))
8803 cp_declarator *declarator;
8805 /* Parse another optional declarator. */
8806 declarator = cp_parser_conversion_declarator_opt (parser);
8808 return cp_parser_make_indirect_declarator
8809 (code, class_type, cv_quals, declarator);
8812 return NULL;
8815 /* Parse an (optional) ctor-initializer.
8817 ctor-initializer:
8818 : mem-initializer-list
8820 Returns TRUE iff the ctor-initializer was actually present. */
8822 static bool
8823 cp_parser_ctor_initializer_opt (cp_parser* parser)
8825 /* If the next token is not a `:', then there is no
8826 ctor-initializer. */
8827 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8829 /* Do default initialization of any bases and members. */
8830 if (DECL_CONSTRUCTOR_P (current_function_decl))
8831 finish_mem_initializers (NULL_TREE);
8833 return false;
8836 /* Consume the `:' token. */
8837 cp_lexer_consume_token (parser->lexer);
8838 /* And the mem-initializer-list. */
8839 cp_parser_mem_initializer_list (parser);
8841 return true;
8844 /* Parse a mem-initializer-list.
8846 mem-initializer-list:
8847 mem-initializer ... [opt]
8848 mem-initializer ... [opt] , mem-initializer-list */
8850 static void
8851 cp_parser_mem_initializer_list (cp_parser* parser)
8853 tree mem_initializer_list = NULL_TREE;
8855 /* Let the semantic analysis code know that we are starting the
8856 mem-initializer-list. */
8857 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8858 error ("only constructors take base initializers");
8860 /* Loop through the list. */
8861 while (true)
8863 tree mem_initializer;
8865 /* Parse the mem-initializer. */
8866 mem_initializer = cp_parser_mem_initializer (parser);
8867 /* If the next token is a `...', we're expanding member initializers. */
8868 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8870 /* Consume the `...'. */
8871 cp_lexer_consume_token (parser->lexer);
8873 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8874 can be expanded but members cannot. */
8875 if (mem_initializer != error_mark_node
8876 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8878 error ("cannot expand initializer for member %<%D%>",
8879 TREE_PURPOSE (mem_initializer));
8880 mem_initializer = error_mark_node;
8883 /* Construct the pack expansion type. */
8884 if (mem_initializer != error_mark_node)
8885 mem_initializer = make_pack_expansion (mem_initializer);
8887 /* Add it to the list, unless it was erroneous. */
8888 if (mem_initializer != error_mark_node)
8890 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8891 mem_initializer_list = mem_initializer;
8893 /* If the next token is not a `,', we're done. */
8894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8895 break;
8896 /* Consume the `,' token. */
8897 cp_lexer_consume_token (parser->lexer);
8900 /* Perform semantic analysis. */
8901 if (DECL_CONSTRUCTOR_P (current_function_decl))
8902 finish_mem_initializers (mem_initializer_list);
8905 /* Parse a mem-initializer.
8907 mem-initializer:
8908 mem-initializer-id ( expression-list [opt] )
8910 GNU extension:
8912 mem-initializer:
8913 ( expression-list [opt] )
8915 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8916 class) or FIELD_DECL (for a non-static data member) to initialize;
8917 the TREE_VALUE is the expression-list. An empty initialization
8918 list is represented by void_list_node. */
8920 static tree
8921 cp_parser_mem_initializer (cp_parser* parser)
8923 tree mem_initializer_id;
8924 tree expression_list;
8925 tree member;
8927 /* Find out what is being initialized. */
8928 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8930 pedwarn ("anachronistic old-style base class initializer");
8931 mem_initializer_id = NULL_TREE;
8933 else
8934 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8935 member = expand_member_init (mem_initializer_id);
8936 if (member && !DECL_P (member))
8937 in_base_initializer = 1;
8939 expression_list
8940 = cp_parser_parenthesized_expression_list (parser, false,
8941 /*cast_p=*/false,
8942 /*allow_expansion_p=*/true,
8943 /*non_constant_p=*/NULL);
8944 if (expression_list == error_mark_node)
8945 return error_mark_node;
8946 if (!expression_list)
8947 expression_list = void_type_node;
8949 in_base_initializer = 0;
8951 return member ? build_tree_list (member, expression_list) : error_mark_node;
8954 /* Parse a mem-initializer-id.
8956 mem-initializer-id:
8957 :: [opt] nested-name-specifier [opt] class-name
8958 identifier
8960 Returns a TYPE indicating the class to be initializer for the first
8961 production. Returns an IDENTIFIER_NODE indicating the data member
8962 to be initialized for the second production. */
8964 static tree
8965 cp_parser_mem_initializer_id (cp_parser* parser)
8967 bool global_scope_p;
8968 bool nested_name_specifier_p;
8969 bool template_p = false;
8970 tree id;
8972 /* `typename' is not allowed in this context ([temp.res]). */
8973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8975 error ("keyword %<typename%> not allowed in this context (a qualified "
8976 "member initializer is implicitly a type)");
8977 cp_lexer_consume_token (parser->lexer);
8979 /* Look for the optional `::' operator. */
8980 global_scope_p
8981 = (cp_parser_global_scope_opt (parser,
8982 /*current_scope_valid_p=*/false)
8983 != NULL_TREE);
8984 /* Look for the optional nested-name-specifier. The simplest way to
8985 implement:
8987 [temp.res]
8989 The keyword `typename' is not permitted in a base-specifier or
8990 mem-initializer; in these contexts a qualified name that
8991 depends on a template-parameter is implicitly assumed to be a
8992 type name.
8994 is to assume that we have seen the `typename' keyword at this
8995 point. */
8996 nested_name_specifier_p
8997 = (cp_parser_nested_name_specifier_opt (parser,
8998 /*typename_keyword_p=*/true,
8999 /*check_dependency_p=*/true,
9000 /*type_p=*/true,
9001 /*is_declaration=*/true)
9002 != NULL_TREE);
9003 if (nested_name_specifier_p)
9004 template_p = cp_parser_optional_template_keyword (parser);
9005 /* If there is a `::' operator or a nested-name-specifier, then we
9006 are definitely looking for a class-name. */
9007 if (global_scope_p || nested_name_specifier_p)
9008 return cp_parser_class_name (parser,
9009 /*typename_keyword_p=*/true,
9010 /*template_keyword_p=*/template_p,
9011 none_type,
9012 /*check_dependency_p=*/true,
9013 /*class_head_p=*/false,
9014 /*is_declaration=*/true);
9015 /* Otherwise, we could also be looking for an ordinary identifier. */
9016 cp_parser_parse_tentatively (parser);
9017 /* Try a class-name. */
9018 id = cp_parser_class_name (parser,
9019 /*typename_keyword_p=*/true,
9020 /*template_keyword_p=*/false,
9021 none_type,
9022 /*check_dependency_p=*/true,
9023 /*class_head_p=*/false,
9024 /*is_declaration=*/true);
9025 /* If we found one, we're done. */
9026 if (cp_parser_parse_definitely (parser))
9027 return id;
9028 /* Otherwise, look for an ordinary identifier. */
9029 return cp_parser_identifier (parser);
9032 /* Overloading [gram.over] */
9034 /* Parse an operator-function-id.
9036 operator-function-id:
9037 operator operator
9039 Returns an IDENTIFIER_NODE for the operator which is a
9040 human-readable spelling of the identifier, e.g., `operator +'. */
9042 static tree
9043 cp_parser_operator_function_id (cp_parser* parser)
9045 /* Look for the `operator' keyword. */
9046 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9047 return error_mark_node;
9048 /* And then the name of the operator itself. */
9049 return cp_parser_operator (parser);
9052 /* Parse an operator.
9054 operator:
9055 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9056 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9057 || ++ -- , ->* -> () []
9059 GNU Extensions:
9061 operator:
9062 <? >? <?= >?=
9064 Returns an IDENTIFIER_NODE for the operator which is a
9065 human-readable spelling of the identifier, e.g., `operator +'. */
9067 static tree
9068 cp_parser_operator (cp_parser* parser)
9070 tree id = NULL_TREE;
9071 cp_token *token;
9073 /* Peek at the next token. */
9074 token = cp_lexer_peek_token (parser->lexer);
9075 /* Figure out which operator we have. */
9076 switch (token->type)
9078 case CPP_KEYWORD:
9080 enum tree_code op;
9082 /* The keyword should be either `new' or `delete'. */
9083 if (token->keyword == RID_NEW)
9084 op = NEW_EXPR;
9085 else if (token->keyword == RID_DELETE)
9086 op = DELETE_EXPR;
9087 else
9088 break;
9090 /* Consume the `new' or `delete' token. */
9091 cp_lexer_consume_token (parser->lexer);
9093 /* Peek at the next token. */
9094 token = cp_lexer_peek_token (parser->lexer);
9095 /* If it's a `[' token then this is the array variant of the
9096 operator. */
9097 if (token->type == CPP_OPEN_SQUARE)
9099 /* Consume the `[' token. */
9100 cp_lexer_consume_token (parser->lexer);
9101 /* Look for the `]' token. */
9102 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9103 id = ansi_opname (op == NEW_EXPR
9104 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9106 /* Otherwise, we have the non-array variant. */
9107 else
9108 id = ansi_opname (op);
9110 return id;
9113 case CPP_PLUS:
9114 id = ansi_opname (PLUS_EXPR);
9115 break;
9117 case CPP_MINUS:
9118 id = ansi_opname (MINUS_EXPR);
9119 break;
9121 case CPP_MULT:
9122 id = ansi_opname (MULT_EXPR);
9123 break;
9125 case CPP_DIV:
9126 id = ansi_opname (TRUNC_DIV_EXPR);
9127 break;
9129 case CPP_MOD:
9130 id = ansi_opname (TRUNC_MOD_EXPR);
9131 break;
9133 case CPP_XOR:
9134 id = ansi_opname (BIT_XOR_EXPR);
9135 break;
9137 case CPP_AND:
9138 id = ansi_opname (BIT_AND_EXPR);
9139 break;
9141 case CPP_OR:
9142 id = ansi_opname (BIT_IOR_EXPR);
9143 break;
9145 case CPP_COMPL:
9146 id = ansi_opname (BIT_NOT_EXPR);
9147 break;
9149 case CPP_NOT:
9150 id = ansi_opname (TRUTH_NOT_EXPR);
9151 break;
9153 case CPP_EQ:
9154 id = ansi_assopname (NOP_EXPR);
9155 break;
9157 case CPP_LESS:
9158 id = ansi_opname (LT_EXPR);
9159 break;
9161 case CPP_GREATER:
9162 id = ansi_opname (GT_EXPR);
9163 break;
9165 case CPP_PLUS_EQ:
9166 id = ansi_assopname (PLUS_EXPR);
9167 break;
9169 case CPP_MINUS_EQ:
9170 id = ansi_assopname (MINUS_EXPR);
9171 break;
9173 case CPP_MULT_EQ:
9174 id = ansi_assopname (MULT_EXPR);
9175 break;
9177 case CPP_DIV_EQ:
9178 id = ansi_assopname (TRUNC_DIV_EXPR);
9179 break;
9181 case CPP_MOD_EQ:
9182 id = ansi_assopname (TRUNC_MOD_EXPR);
9183 break;
9185 case CPP_XOR_EQ:
9186 id = ansi_assopname (BIT_XOR_EXPR);
9187 break;
9189 case CPP_AND_EQ:
9190 id = ansi_assopname (BIT_AND_EXPR);
9191 break;
9193 case CPP_OR_EQ:
9194 id = ansi_assopname (BIT_IOR_EXPR);
9195 break;
9197 case CPP_LSHIFT:
9198 id = ansi_opname (LSHIFT_EXPR);
9199 break;
9201 case CPP_RSHIFT:
9202 id = ansi_opname (RSHIFT_EXPR);
9203 break;
9205 case CPP_LSHIFT_EQ:
9206 id = ansi_assopname (LSHIFT_EXPR);
9207 break;
9209 case CPP_RSHIFT_EQ:
9210 id = ansi_assopname (RSHIFT_EXPR);
9211 break;
9213 case CPP_EQ_EQ:
9214 id = ansi_opname (EQ_EXPR);
9215 break;
9217 case CPP_NOT_EQ:
9218 id = ansi_opname (NE_EXPR);
9219 break;
9221 case CPP_LESS_EQ:
9222 id = ansi_opname (LE_EXPR);
9223 break;
9225 case CPP_GREATER_EQ:
9226 id = ansi_opname (GE_EXPR);
9227 break;
9229 case CPP_AND_AND:
9230 id = ansi_opname (TRUTH_ANDIF_EXPR);
9231 break;
9233 case CPP_OR_OR:
9234 id = ansi_opname (TRUTH_ORIF_EXPR);
9235 break;
9237 case CPP_PLUS_PLUS:
9238 id = ansi_opname (POSTINCREMENT_EXPR);
9239 break;
9241 case CPP_MINUS_MINUS:
9242 id = ansi_opname (PREDECREMENT_EXPR);
9243 break;
9245 case CPP_COMMA:
9246 id = ansi_opname (COMPOUND_EXPR);
9247 break;
9249 case CPP_DEREF_STAR:
9250 id = ansi_opname (MEMBER_REF);
9251 break;
9253 case CPP_DEREF:
9254 id = ansi_opname (COMPONENT_REF);
9255 break;
9257 case CPP_OPEN_PAREN:
9258 /* Consume the `('. */
9259 cp_lexer_consume_token (parser->lexer);
9260 /* Look for the matching `)'. */
9261 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9262 return ansi_opname (CALL_EXPR);
9264 case CPP_OPEN_SQUARE:
9265 /* Consume the `['. */
9266 cp_lexer_consume_token (parser->lexer);
9267 /* Look for the matching `]'. */
9268 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9269 return ansi_opname (ARRAY_REF);
9271 default:
9272 /* Anything else is an error. */
9273 break;
9276 /* If we have selected an identifier, we need to consume the
9277 operator token. */
9278 if (id)
9279 cp_lexer_consume_token (parser->lexer);
9280 /* Otherwise, no valid operator name was present. */
9281 else
9283 cp_parser_error (parser, "expected operator");
9284 id = error_mark_node;
9287 return id;
9290 /* Parse a template-declaration.
9292 template-declaration:
9293 export [opt] template < template-parameter-list > declaration
9295 If MEMBER_P is TRUE, this template-declaration occurs within a
9296 class-specifier.
9298 The grammar rule given by the standard isn't correct. What
9299 is really meant is:
9301 template-declaration:
9302 export [opt] template-parameter-list-seq
9303 decl-specifier-seq [opt] init-declarator [opt] ;
9304 export [opt] template-parameter-list-seq
9305 function-definition
9307 template-parameter-list-seq:
9308 template-parameter-list-seq [opt]
9309 template < template-parameter-list > */
9311 static void
9312 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9314 /* Check for `export'. */
9315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9317 /* Consume the `export' token. */
9318 cp_lexer_consume_token (parser->lexer);
9319 /* Warn that we do not support `export'. */
9320 warning (0, "keyword %<export%> not implemented, and will be ignored");
9323 cp_parser_template_declaration_after_export (parser, member_p);
9326 /* Parse a template-parameter-list.
9328 template-parameter-list:
9329 template-parameter
9330 template-parameter-list , template-parameter
9332 Returns a TREE_LIST. Each node represents a template parameter.
9333 The nodes are connected via their TREE_CHAINs. */
9335 static tree
9336 cp_parser_template_parameter_list (cp_parser* parser)
9338 tree parameter_list = NULL_TREE;
9340 begin_template_parm_list ();
9341 while (true)
9343 tree parameter;
9344 bool is_non_type;
9345 bool is_parameter_pack;
9347 /* Parse the template-parameter. */
9348 parameter = cp_parser_template_parameter (parser,
9349 &is_non_type,
9350 &is_parameter_pack);
9351 /* Add it to the list. */
9352 if (parameter != error_mark_node)
9353 parameter_list = process_template_parm (parameter_list,
9354 parameter,
9355 is_non_type,
9356 is_parameter_pack);
9357 else
9359 tree err_parm = build_tree_list (parameter, parameter);
9360 TREE_VALUE (err_parm) = error_mark_node;
9361 parameter_list = chainon (parameter_list, err_parm);
9364 /* If the next token is not a `,', we're done. */
9365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9366 break;
9367 /* Otherwise, consume the `,' token. */
9368 cp_lexer_consume_token (parser->lexer);
9371 return end_template_parm_list (parameter_list);
9374 /* Parse a template-parameter.
9376 template-parameter:
9377 type-parameter
9378 parameter-declaration
9380 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9381 the parameter. The TREE_PURPOSE is the default value, if any.
9382 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9383 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9384 set to true iff this parameter is a parameter pack. */
9386 static tree
9387 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9388 bool *is_parameter_pack)
9390 cp_token *token;
9391 cp_parameter_declarator *parameter_declarator;
9392 cp_declarator *id_declarator;
9393 tree parm;
9395 /* Assume it is a type parameter or a template parameter. */
9396 *is_non_type = false;
9397 /* Assume it not a parameter pack. */
9398 *is_parameter_pack = false;
9399 /* Peek at the next token. */
9400 token = cp_lexer_peek_token (parser->lexer);
9401 /* If it is `class' or `template', we have a type-parameter. */
9402 if (token->keyword == RID_TEMPLATE)
9403 return cp_parser_type_parameter (parser, is_parameter_pack);
9404 /* If it is `class' or `typename' we do not know yet whether it is a
9405 type parameter or a non-type parameter. Consider:
9407 template <typename T, typename T::X X> ...
9411 template <class C, class D*> ...
9413 Here, the first parameter is a type parameter, and the second is
9414 a non-type parameter. We can tell by looking at the token after
9415 the identifier -- if it is a `,', `=', or `>' then we have a type
9416 parameter. */
9417 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9419 /* Peek at the token after `class' or `typename'. */
9420 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9421 /* If it's an ellipsis, we have a template type parameter
9422 pack. */
9423 if (token->type == CPP_ELLIPSIS)
9424 return cp_parser_type_parameter (parser, is_parameter_pack);
9425 /* If it's an identifier, skip it. */
9426 if (token->type == CPP_NAME)
9427 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9428 /* Now, see if the token looks like the end of a template
9429 parameter. */
9430 if (token->type == CPP_COMMA
9431 || token->type == CPP_EQ
9432 || token->type == CPP_GREATER)
9433 return cp_parser_type_parameter (parser, is_parameter_pack);
9436 /* Otherwise, it is a non-type parameter.
9438 [temp.param]
9440 When parsing a default template-argument for a non-type
9441 template-parameter, the first non-nested `>' is taken as the end
9442 of the template parameter-list rather than a greater-than
9443 operator. */
9444 *is_non_type = true;
9445 parameter_declarator
9446 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9447 /*parenthesized_p=*/NULL);
9449 /* If the parameter declaration is marked as a parameter pack, set
9450 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9451 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9452 grokdeclarator. */
9453 if (parameter_declarator
9454 && parameter_declarator->declarator
9455 && parameter_declarator->declarator->parameter_pack_p)
9457 *is_parameter_pack = true;
9458 parameter_declarator->declarator->parameter_pack_p = false;
9461 /* If the next token is an ellipsis, and we don't already have it
9462 marked as a parameter pack, then we have a parameter pack (that
9463 has no declarator). */
9464 if (!*is_parameter_pack
9465 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9466 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9468 /* Consume the `...'. */
9469 cp_lexer_consume_token (parser->lexer);
9470 maybe_warn_variadic_templates ();
9472 *is_parameter_pack = true;
9474 /* We might end up with a pack expansion as the type of the non-type
9475 template parameter, in which case this is a non-type template
9476 parameter pack. */
9477 else if (parameter_declarator
9478 && parameter_declarator->decl_specifiers.type
9479 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9481 *is_parameter_pack = true;
9482 parameter_declarator->decl_specifiers.type =
9483 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9486 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9488 /* Parameter packs cannot have default arguments. However, a
9489 user may try to do so, so we'll parse them and give an
9490 appropriate diagnostic here. */
9492 /* Consume the `='. */
9493 cp_lexer_consume_token (parser->lexer);
9495 /* Find the name of the parameter pack. */
9496 id_declarator = parameter_declarator->declarator;
9497 while (id_declarator && id_declarator->kind != cdk_id)
9498 id_declarator = id_declarator->declarator;
9500 if (id_declarator && id_declarator->kind == cdk_id)
9501 error ("template parameter pack %qD cannot have a default argument",
9502 id_declarator->u.id.unqualified_name);
9503 else
9504 error ("template parameter pack cannot have a default argument");
9506 /* Parse the default argument, but throw away the result. */
9507 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9510 parm = grokdeclarator (parameter_declarator->declarator,
9511 &parameter_declarator->decl_specifiers,
9512 PARM, /*initialized=*/0,
9513 /*attrlist=*/NULL);
9514 if (parm == error_mark_node)
9515 return error_mark_node;
9517 return build_tree_list (parameter_declarator->default_argument, parm);
9520 /* Parse a type-parameter.
9522 type-parameter:
9523 class identifier [opt]
9524 class identifier [opt] = type-id
9525 typename identifier [opt]
9526 typename identifier [opt] = type-id
9527 template < template-parameter-list > class identifier [opt]
9528 template < template-parameter-list > class identifier [opt]
9529 = id-expression
9531 GNU Extension (variadic templates):
9533 type-parameter:
9534 class ... identifier [opt]
9535 typename ... identifier [opt]
9537 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9538 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9539 the declaration of the parameter.
9541 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9543 static tree
9544 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9546 cp_token *token;
9547 tree parameter;
9549 /* Look for a keyword to tell us what kind of parameter this is. */
9550 token = cp_parser_require (parser, CPP_KEYWORD,
9551 "%<class%>, %<typename%>, or %<template%>");
9552 if (!token)
9553 return error_mark_node;
9555 switch (token->keyword)
9557 case RID_CLASS:
9558 case RID_TYPENAME:
9560 tree identifier;
9561 tree default_argument;
9563 /* If the next token is an ellipsis, we have a template
9564 argument pack. */
9565 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9567 /* Consume the `...' token. */
9568 cp_lexer_consume_token (parser->lexer);
9569 maybe_warn_variadic_templates ();
9571 *is_parameter_pack = true;
9574 /* If the next token is an identifier, then it names the
9575 parameter. */
9576 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9577 identifier = cp_parser_identifier (parser);
9578 else
9579 identifier = NULL_TREE;
9581 /* Create the parameter. */
9582 parameter = finish_template_type_parm (class_type_node, identifier);
9584 /* If the next token is an `=', we have a default argument. */
9585 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9587 /* Consume the `=' token. */
9588 cp_lexer_consume_token (parser->lexer);
9589 /* Parse the default-argument. */
9590 push_deferring_access_checks (dk_no_deferred);
9591 default_argument = cp_parser_type_id (parser);
9593 /* Template parameter packs cannot have default
9594 arguments. */
9595 if (*is_parameter_pack)
9597 if (identifier)
9598 error ("template parameter pack %qD cannot have a default argument",
9599 identifier);
9600 else
9601 error ("template parameter packs cannot have default arguments");
9602 default_argument = NULL_TREE;
9604 pop_deferring_access_checks ();
9606 else
9607 default_argument = NULL_TREE;
9609 /* Create the combined representation of the parameter and the
9610 default argument. */
9611 parameter = build_tree_list (default_argument, parameter);
9613 break;
9615 case RID_TEMPLATE:
9617 tree parameter_list;
9618 tree identifier;
9619 tree default_argument;
9621 /* Look for the `<'. */
9622 cp_parser_require (parser, CPP_LESS, "%<<%>");
9623 /* Parse the template-parameter-list. */
9624 parameter_list = cp_parser_template_parameter_list (parser);
9625 /* Look for the `>'. */
9626 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9627 /* Look for the `class' keyword. */
9628 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9629 /* If the next token is an ellipsis, we have a template
9630 argument pack. */
9631 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9633 /* Consume the `...' token. */
9634 cp_lexer_consume_token (parser->lexer);
9635 maybe_warn_variadic_templates ();
9637 *is_parameter_pack = true;
9639 /* If the next token is an `=', then there is a
9640 default-argument. If the next token is a `>', we are at
9641 the end of the parameter-list. If the next token is a `,',
9642 then we are at the end of this parameter. */
9643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9644 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9645 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9647 identifier = cp_parser_identifier (parser);
9648 /* Treat invalid names as if the parameter were nameless. */
9649 if (identifier == error_mark_node)
9650 identifier = NULL_TREE;
9652 else
9653 identifier = NULL_TREE;
9655 /* Create the template parameter. */
9656 parameter = finish_template_template_parm (class_type_node,
9657 identifier);
9659 /* If the next token is an `=', then there is a
9660 default-argument. */
9661 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9663 bool is_template;
9665 /* Consume the `='. */
9666 cp_lexer_consume_token (parser->lexer);
9667 /* Parse the id-expression. */
9668 push_deferring_access_checks (dk_no_deferred);
9669 default_argument
9670 = cp_parser_id_expression (parser,
9671 /*template_keyword_p=*/false,
9672 /*check_dependency_p=*/true,
9673 /*template_p=*/&is_template,
9674 /*declarator_p=*/false,
9675 /*optional_p=*/false);
9676 if (TREE_CODE (default_argument) == TYPE_DECL)
9677 /* If the id-expression was a template-id that refers to
9678 a template-class, we already have the declaration here,
9679 so no further lookup is needed. */
9681 else
9682 /* Look up the name. */
9683 default_argument
9684 = cp_parser_lookup_name (parser, default_argument,
9685 none_type,
9686 /*is_template=*/is_template,
9687 /*is_namespace=*/false,
9688 /*check_dependency=*/true,
9689 /*ambiguous_decls=*/NULL);
9690 /* See if the default argument is valid. */
9691 default_argument
9692 = check_template_template_default_arg (default_argument);
9694 /* Template parameter packs cannot have default
9695 arguments. */
9696 if (*is_parameter_pack)
9698 if (identifier)
9699 error ("template parameter pack %qD cannot have a default argument",
9700 identifier);
9701 else
9702 error ("template parameter packs cannot have default arguments");
9703 default_argument = NULL_TREE;
9705 pop_deferring_access_checks ();
9707 else
9708 default_argument = NULL_TREE;
9710 /* Create the combined representation of the parameter and the
9711 default argument. */
9712 parameter = build_tree_list (default_argument, parameter);
9714 break;
9716 default:
9717 gcc_unreachable ();
9718 break;
9721 return parameter;
9724 /* Parse a template-id.
9726 template-id:
9727 template-name < template-argument-list [opt] >
9729 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9730 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9731 returned. Otherwise, if the template-name names a function, or set
9732 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9733 names a class, returns a TYPE_DECL for the specialization.
9735 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9736 uninstantiated templates. */
9738 static tree
9739 cp_parser_template_id (cp_parser *parser,
9740 bool template_keyword_p,
9741 bool check_dependency_p,
9742 bool is_declaration)
9744 int i;
9745 tree template;
9746 tree arguments;
9747 tree template_id;
9748 cp_token_position start_of_id = 0;
9749 deferred_access_check *chk;
9750 VEC (deferred_access_check,gc) *access_check;
9751 cp_token *next_token, *next_token_2;
9752 bool is_identifier;
9754 /* If the next token corresponds to a template-id, there is no need
9755 to reparse it. */
9756 next_token = cp_lexer_peek_token (parser->lexer);
9757 if (next_token->type == CPP_TEMPLATE_ID)
9759 struct tree_check *check_value;
9761 /* Get the stored value. */
9762 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9763 /* Perform any access checks that were deferred. */
9764 access_check = check_value->checks;
9765 if (access_check)
9767 for (i = 0 ;
9768 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9769 ++i)
9771 perform_or_defer_access_check (chk->binfo,
9772 chk->decl,
9773 chk->diag_decl);
9776 /* Return the stored value. */
9777 return check_value->value;
9780 /* Avoid performing name lookup if there is no possibility of
9781 finding a template-id. */
9782 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9783 || (next_token->type == CPP_NAME
9784 && !cp_parser_nth_token_starts_template_argument_list_p
9785 (parser, 2)))
9787 cp_parser_error (parser, "expected template-id");
9788 return error_mark_node;
9791 /* Remember where the template-id starts. */
9792 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9793 start_of_id = cp_lexer_token_position (parser->lexer, false);
9795 push_deferring_access_checks (dk_deferred);
9797 /* Parse the template-name. */
9798 is_identifier = false;
9799 template = cp_parser_template_name (parser, template_keyword_p,
9800 check_dependency_p,
9801 is_declaration,
9802 &is_identifier);
9803 if (template == error_mark_node || is_identifier)
9805 pop_deferring_access_checks ();
9806 return template;
9809 /* If we find the sequence `[:' after a template-name, it's probably
9810 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9811 parse correctly the argument list. */
9812 next_token = cp_lexer_peek_token (parser->lexer);
9813 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9814 if (next_token->type == CPP_OPEN_SQUARE
9815 && next_token->flags & DIGRAPH
9816 && next_token_2->type == CPP_COLON
9817 && !(next_token_2->flags & PREV_WHITE))
9819 cp_parser_parse_tentatively (parser);
9820 /* Change `:' into `::'. */
9821 next_token_2->type = CPP_SCOPE;
9822 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9823 CPP_LESS. */
9824 cp_lexer_consume_token (parser->lexer);
9825 /* Parse the arguments. */
9826 arguments = cp_parser_enclosed_template_argument_list (parser);
9827 if (!cp_parser_parse_definitely (parser))
9829 /* If we couldn't parse an argument list, then we revert our changes
9830 and return simply an error. Maybe this is not a template-id
9831 after all. */
9832 next_token_2->type = CPP_COLON;
9833 cp_parser_error (parser, "expected %<<%>");
9834 pop_deferring_access_checks ();
9835 return error_mark_node;
9837 /* Otherwise, emit an error about the invalid digraph, but continue
9838 parsing because we got our argument list. */
9839 permerror ("%<<::%> cannot begin a template-argument list");
9840 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9841 "between %<<%> and %<::%>");
9842 if (!flag_permissive)
9844 static bool hint;
9845 if (!hint)
9847 inform ("(if you use -fpermissive G++ will accept your code)");
9848 hint = true;
9852 else
9854 /* Look for the `<' that starts the template-argument-list. */
9855 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
9857 pop_deferring_access_checks ();
9858 return error_mark_node;
9860 /* Parse the arguments. */
9861 arguments = cp_parser_enclosed_template_argument_list (parser);
9864 /* Build a representation of the specialization. */
9865 if (TREE_CODE (template) == IDENTIFIER_NODE)
9866 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9867 else if (DECL_CLASS_TEMPLATE_P (template)
9868 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9870 bool entering_scope;
9871 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9872 template (rather than some instantiation thereof) only if
9873 is not nested within some other construct. For example, in
9874 "template <typename T> void f(T) { A<T>::", A<T> is just an
9875 instantiation of A. */
9876 entering_scope = (template_parm_scope_p ()
9877 && cp_lexer_next_token_is (parser->lexer,
9878 CPP_SCOPE));
9879 template_id
9880 = finish_template_type (template, arguments, entering_scope);
9882 else
9884 /* If it's not a class-template or a template-template, it should be
9885 a function-template. */
9886 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9887 || TREE_CODE (template) == OVERLOAD
9888 || BASELINK_P (template)));
9890 template_id = lookup_template_function (template, arguments);
9893 /* If parsing tentatively, replace the sequence of tokens that makes
9894 up the template-id with a CPP_TEMPLATE_ID token. That way,
9895 should we re-parse the token stream, we will not have to repeat
9896 the effort required to do the parse, nor will we issue duplicate
9897 error messages about problems during instantiation of the
9898 template. */
9899 if (start_of_id)
9901 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9903 /* Reset the contents of the START_OF_ID token. */
9904 token->type = CPP_TEMPLATE_ID;
9905 /* Retrieve any deferred checks. Do not pop this access checks yet
9906 so the memory will not be reclaimed during token replacing below. */
9907 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9908 token->u.tree_check_value->value = template_id;
9909 token->u.tree_check_value->checks = get_deferred_access_checks ();
9910 token->keyword = RID_MAX;
9912 /* Purge all subsequent tokens. */
9913 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9915 /* ??? Can we actually assume that, if template_id ==
9916 error_mark_node, we will have issued a diagnostic to the
9917 user, as opposed to simply marking the tentative parse as
9918 failed? */
9919 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9920 error ("parse error in template argument list");
9923 pop_deferring_access_checks ();
9924 return template_id;
9927 /* Parse a template-name.
9929 template-name:
9930 identifier
9932 The standard should actually say:
9934 template-name:
9935 identifier
9936 operator-function-id
9938 A defect report has been filed about this issue.
9940 A conversion-function-id cannot be a template name because they cannot
9941 be part of a template-id. In fact, looking at this code:
9943 a.operator K<int>()
9945 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9946 It is impossible to call a templated conversion-function-id with an
9947 explicit argument list, since the only allowed template parameter is
9948 the type to which it is converting.
9950 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9951 `template' keyword, in a construction like:
9953 T::template f<3>()
9955 In that case `f' is taken to be a template-name, even though there
9956 is no way of knowing for sure.
9958 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9959 name refers to a set of overloaded functions, at least one of which
9960 is a template, or an IDENTIFIER_NODE with the name of the template,
9961 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9962 names are looked up inside uninstantiated templates. */
9964 static tree
9965 cp_parser_template_name (cp_parser* parser,
9966 bool template_keyword_p,
9967 bool check_dependency_p,
9968 bool is_declaration,
9969 bool *is_identifier)
9971 tree identifier;
9972 tree decl;
9973 tree fns;
9975 /* If the next token is `operator', then we have either an
9976 operator-function-id or a conversion-function-id. */
9977 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9979 /* We don't know whether we're looking at an
9980 operator-function-id or a conversion-function-id. */
9981 cp_parser_parse_tentatively (parser);
9982 /* Try an operator-function-id. */
9983 identifier = cp_parser_operator_function_id (parser);
9984 /* If that didn't work, try a conversion-function-id. */
9985 if (!cp_parser_parse_definitely (parser))
9987 cp_parser_error (parser, "expected template-name");
9988 return error_mark_node;
9991 /* Look for the identifier. */
9992 else
9993 identifier = cp_parser_identifier (parser);
9995 /* If we didn't find an identifier, we don't have a template-id. */
9996 if (identifier == error_mark_node)
9997 return error_mark_node;
9999 /* If the name immediately followed the `template' keyword, then it
10000 is a template-name. However, if the next token is not `<', then
10001 we do not treat it as a template-name, since it is not being used
10002 as part of a template-id. This enables us to handle constructs
10003 like:
10005 template <typename T> struct S { S(); };
10006 template <typename T> S<T>::S();
10008 correctly. We would treat `S' as a template -- if it were `S<T>'
10009 -- but we do not if there is no `<'. */
10011 if (processing_template_decl
10012 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10014 /* In a declaration, in a dependent context, we pretend that the
10015 "template" keyword was present in order to improve error
10016 recovery. For example, given:
10018 template <typename T> void f(T::X<int>);
10020 we want to treat "X<int>" as a template-id. */
10021 if (is_declaration
10022 && !template_keyword_p
10023 && parser->scope && TYPE_P (parser->scope)
10024 && check_dependency_p
10025 && dependent_type_p (parser->scope)
10026 /* Do not do this for dtors (or ctors), since they never
10027 need the template keyword before their name. */
10028 && !constructor_name_p (identifier, parser->scope))
10030 cp_token_position start = 0;
10032 /* Explain what went wrong. */
10033 error ("non-template %qD used as template", identifier);
10034 inform ("use %<%T::template %D%> to indicate that it is a template",
10035 parser->scope, identifier);
10036 /* If parsing tentatively, find the location of the "<" token. */
10037 if (cp_parser_simulate_error (parser))
10038 start = cp_lexer_token_position (parser->lexer, true);
10039 /* Parse the template arguments so that we can issue error
10040 messages about them. */
10041 cp_lexer_consume_token (parser->lexer);
10042 cp_parser_enclosed_template_argument_list (parser);
10043 /* Skip tokens until we find a good place from which to
10044 continue parsing. */
10045 cp_parser_skip_to_closing_parenthesis (parser,
10046 /*recovering=*/true,
10047 /*or_comma=*/true,
10048 /*consume_paren=*/false);
10049 /* If parsing tentatively, permanently remove the
10050 template argument list. That will prevent duplicate
10051 error messages from being issued about the missing
10052 "template" keyword. */
10053 if (start)
10054 cp_lexer_purge_tokens_after (parser->lexer, start);
10055 if (is_identifier)
10056 *is_identifier = true;
10057 return identifier;
10060 /* If the "template" keyword is present, then there is generally
10061 no point in doing name-lookup, so we just return IDENTIFIER.
10062 But, if the qualifying scope is non-dependent then we can
10063 (and must) do name-lookup normally. */
10064 if (template_keyword_p
10065 && (!parser->scope
10066 || (TYPE_P (parser->scope)
10067 && dependent_type_p (parser->scope))))
10068 return identifier;
10071 /* Look up the name. */
10072 decl = cp_parser_lookup_name (parser, identifier,
10073 none_type,
10074 /*is_template=*/false,
10075 /*is_namespace=*/false,
10076 check_dependency_p,
10077 /*ambiguous_decls=*/NULL);
10078 decl = maybe_get_template_decl_from_type_decl (decl);
10080 /* If DECL is a template, then the name was a template-name. */
10081 if (TREE_CODE (decl) == TEMPLATE_DECL)
10083 else
10085 tree fn = NULL_TREE;
10087 /* The standard does not explicitly indicate whether a name that
10088 names a set of overloaded declarations, some of which are
10089 templates, is a template-name. However, such a name should
10090 be a template-name; otherwise, there is no way to form a
10091 template-id for the overloaded templates. */
10092 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10093 if (TREE_CODE (fns) == OVERLOAD)
10094 for (fn = fns; fn; fn = OVL_NEXT (fn))
10095 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10096 break;
10098 if (!fn)
10100 /* The name does not name a template. */
10101 cp_parser_error (parser, "expected template-name");
10102 return error_mark_node;
10106 /* If DECL is dependent, and refers to a function, then just return
10107 its name; we will look it up again during template instantiation. */
10108 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10110 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10111 if (TYPE_P (scope) && dependent_type_p (scope))
10112 return identifier;
10115 return decl;
10118 /* Parse a template-argument-list.
10120 template-argument-list:
10121 template-argument ... [opt]
10122 template-argument-list , template-argument ... [opt]
10124 Returns a TREE_VEC containing the arguments. */
10126 static tree
10127 cp_parser_template_argument_list (cp_parser* parser)
10129 tree fixed_args[10];
10130 unsigned n_args = 0;
10131 unsigned alloced = 10;
10132 tree *arg_ary = fixed_args;
10133 tree vec;
10134 bool saved_in_template_argument_list_p;
10135 bool saved_ice_p;
10136 bool saved_non_ice_p;
10138 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10139 parser->in_template_argument_list_p = true;
10140 /* Even if the template-id appears in an integral
10141 constant-expression, the contents of the argument list do
10142 not. */
10143 saved_ice_p = parser->integral_constant_expression_p;
10144 parser->integral_constant_expression_p = false;
10145 saved_non_ice_p = parser->non_integral_constant_expression_p;
10146 parser->non_integral_constant_expression_p = false;
10147 /* Parse the arguments. */
10150 tree argument;
10152 if (n_args)
10153 /* Consume the comma. */
10154 cp_lexer_consume_token (parser->lexer);
10156 /* Parse the template-argument. */
10157 argument = cp_parser_template_argument (parser);
10159 /* If the next token is an ellipsis, we're expanding a template
10160 argument pack. */
10161 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10163 /* Consume the `...' token. */
10164 cp_lexer_consume_token (parser->lexer);
10166 /* Make the argument into a TYPE_PACK_EXPANSION or
10167 EXPR_PACK_EXPANSION. */
10168 argument = make_pack_expansion (argument);
10171 if (n_args == alloced)
10173 alloced *= 2;
10175 if (arg_ary == fixed_args)
10177 arg_ary = XNEWVEC (tree, alloced);
10178 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10180 else
10181 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10183 arg_ary[n_args++] = argument;
10185 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10187 vec = make_tree_vec (n_args);
10189 while (n_args--)
10190 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10192 if (arg_ary != fixed_args)
10193 free (arg_ary);
10194 parser->non_integral_constant_expression_p = saved_non_ice_p;
10195 parser->integral_constant_expression_p = saved_ice_p;
10196 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10197 return vec;
10200 /* Parse a template-argument.
10202 template-argument:
10203 assignment-expression
10204 type-id
10205 id-expression
10207 The representation is that of an assignment-expression, type-id, or
10208 id-expression -- except that the qualified id-expression is
10209 evaluated, so that the value returned is either a DECL or an
10210 OVERLOAD.
10212 Although the standard says "assignment-expression", it forbids
10213 throw-expressions or assignments in the template argument.
10214 Therefore, we use "conditional-expression" instead. */
10216 static tree
10217 cp_parser_template_argument (cp_parser* parser)
10219 tree argument;
10220 bool template_p;
10221 bool address_p;
10222 bool maybe_type_id = false;
10223 cp_token *token;
10224 cp_id_kind idk;
10226 /* There's really no way to know what we're looking at, so we just
10227 try each alternative in order.
10229 [temp.arg]
10231 In a template-argument, an ambiguity between a type-id and an
10232 expression is resolved to a type-id, regardless of the form of
10233 the corresponding template-parameter.
10235 Therefore, we try a type-id first. */
10236 cp_parser_parse_tentatively (parser);
10237 argument = cp_parser_type_id (parser);
10238 /* If there was no error parsing the type-id but the next token is a '>>',
10239 we probably found a typo for '> >'. But there are type-id which are
10240 also valid expressions. For instance:
10242 struct X { int operator >> (int); };
10243 template <int V> struct Foo {};
10244 Foo<X () >> 5> r;
10246 Here 'X()' is a valid type-id of a function type, but the user just
10247 wanted to write the expression "X() >> 5". Thus, we remember that we
10248 found a valid type-id, but we still try to parse the argument as an
10249 expression to see what happens. */
10250 if (!cp_parser_error_occurred (parser)
10251 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10253 maybe_type_id = true;
10254 cp_parser_abort_tentative_parse (parser);
10256 else
10258 /* If the next token isn't a `,' or a `>', then this argument wasn't
10259 really finished. This means that the argument is not a valid
10260 type-id. */
10261 if (!cp_parser_next_token_ends_template_argument_p (parser))
10262 cp_parser_error (parser, "expected template-argument");
10263 /* If that worked, we're done. */
10264 if (cp_parser_parse_definitely (parser))
10265 return argument;
10267 /* We're still not sure what the argument will be. */
10268 cp_parser_parse_tentatively (parser);
10269 /* Try a template. */
10270 argument = cp_parser_id_expression (parser,
10271 /*template_keyword_p=*/false,
10272 /*check_dependency_p=*/true,
10273 &template_p,
10274 /*declarator_p=*/false,
10275 /*optional_p=*/false);
10276 /* If the next token isn't a `,' or a `>', then this argument wasn't
10277 really finished. */
10278 if (!cp_parser_next_token_ends_template_argument_p (parser))
10279 cp_parser_error (parser, "expected template-argument");
10280 if (!cp_parser_error_occurred (parser))
10282 /* Figure out what is being referred to. If the id-expression
10283 was for a class template specialization, then we will have a
10284 TYPE_DECL at this point. There is no need to do name lookup
10285 at this point in that case. */
10286 if (TREE_CODE (argument) != TYPE_DECL)
10287 argument = cp_parser_lookup_name (parser, argument,
10288 none_type,
10289 /*is_template=*/template_p,
10290 /*is_namespace=*/false,
10291 /*check_dependency=*/true,
10292 /*ambiguous_decls=*/NULL);
10293 if (TREE_CODE (argument) != TEMPLATE_DECL
10294 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10295 cp_parser_error (parser, "expected template-name");
10297 if (cp_parser_parse_definitely (parser))
10298 return argument;
10299 /* It must be a non-type argument. There permitted cases are given
10300 in [temp.arg.nontype]:
10302 -- an integral constant-expression of integral or enumeration
10303 type; or
10305 -- the name of a non-type template-parameter; or
10307 -- the name of an object or function with external linkage...
10309 -- the address of an object or function with external linkage...
10311 -- a pointer to member... */
10312 /* Look for a non-type template parameter. */
10313 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10315 cp_parser_parse_tentatively (parser);
10316 argument = cp_parser_primary_expression (parser,
10317 /*adress_p=*/false,
10318 /*cast_p=*/false,
10319 /*template_arg_p=*/true,
10320 &idk);
10321 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10322 || !cp_parser_next_token_ends_template_argument_p (parser))
10323 cp_parser_simulate_error (parser);
10324 if (cp_parser_parse_definitely (parser))
10325 return argument;
10328 /* If the next token is "&", the argument must be the address of an
10329 object or function with external linkage. */
10330 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10331 if (address_p)
10332 cp_lexer_consume_token (parser->lexer);
10333 /* See if we might have an id-expression. */
10334 token = cp_lexer_peek_token (parser->lexer);
10335 if (token->type == CPP_NAME
10336 || token->keyword == RID_OPERATOR
10337 || token->type == CPP_SCOPE
10338 || token->type == CPP_TEMPLATE_ID
10339 || token->type == CPP_NESTED_NAME_SPECIFIER)
10341 cp_parser_parse_tentatively (parser);
10342 argument = cp_parser_primary_expression (parser,
10343 address_p,
10344 /*cast_p=*/false,
10345 /*template_arg_p=*/true,
10346 &idk);
10347 if (cp_parser_error_occurred (parser)
10348 || !cp_parser_next_token_ends_template_argument_p (parser))
10349 cp_parser_abort_tentative_parse (parser);
10350 else
10352 if (TREE_CODE (argument) == INDIRECT_REF)
10354 gcc_assert (REFERENCE_REF_P (argument));
10355 argument = TREE_OPERAND (argument, 0);
10358 if (TREE_CODE (argument) == VAR_DECL)
10360 /* A variable without external linkage might still be a
10361 valid constant-expression, so no error is issued here
10362 if the external-linkage check fails. */
10363 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10364 cp_parser_simulate_error (parser);
10366 else if (is_overloaded_fn (argument))
10367 /* All overloaded functions are allowed; if the external
10368 linkage test does not pass, an error will be issued
10369 later. */
10371 else if (address_p
10372 && (TREE_CODE (argument) == OFFSET_REF
10373 || TREE_CODE (argument) == SCOPE_REF))
10374 /* A pointer-to-member. */
10376 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10378 else
10379 cp_parser_simulate_error (parser);
10381 if (cp_parser_parse_definitely (parser))
10383 if (address_p)
10384 argument = build_x_unary_op (ADDR_EXPR, argument,
10385 tf_warning_or_error);
10386 return argument;
10390 /* If the argument started with "&", there are no other valid
10391 alternatives at this point. */
10392 if (address_p)
10394 cp_parser_error (parser, "invalid non-type template argument");
10395 return error_mark_node;
10398 /* If the argument wasn't successfully parsed as a type-id followed
10399 by '>>', the argument can only be a constant expression now.
10400 Otherwise, we try parsing the constant-expression tentatively,
10401 because the argument could really be a type-id. */
10402 if (maybe_type_id)
10403 cp_parser_parse_tentatively (parser);
10404 argument = cp_parser_constant_expression (parser,
10405 /*allow_non_constant_p=*/false,
10406 /*non_constant_p=*/NULL);
10407 argument = fold_non_dependent_expr (argument);
10408 if (!maybe_type_id)
10409 return argument;
10410 if (!cp_parser_next_token_ends_template_argument_p (parser))
10411 cp_parser_error (parser, "expected template-argument");
10412 if (cp_parser_parse_definitely (parser))
10413 return argument;
10414 /* We did our best to parse the argument as a non type-id, but that
10415 was the only alternative that matched (albeit with a '>' after
10416 it). We can assume it's just a typo from the user, and a
10417 diagnostic will then be issued. */
10418 return cp_parser_type_id (parser);
10421 /* Parse an explicit-instantiation.
10423 explicit-instantiation:
10424 template declaration
10426 Although the standard says `declaration', what it really means is:
10428 explicit-instantiation:
10429 template decl-specifier-seq [opt] declarator [opt] ;
10431 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10432 supposed to be allowed. A defect report has been filed about this
10433 issue.
10435 GNU Extension:
10437 explicit-instantiation:
10438 storage-class-specifier template
10439 decl-specifier-seq [opt] declarator [opt] ;
10440 function-specifier template
10441 decl-specifier-seq [opt] declarator [opt] ; */
10443 static void
10444 cp_parser_explicit_instantiation (cp_parser* parser)
10446 int declares_class_or_enum;
10447 cp_decl_specifier_seq decl_specifiers;
10448 tree extension_specifier = NULL_TREE;
10450 /* Look for an (optional) storage-class-specifier or
10451 function-specifier. */
10452 if (cp_parser_allow_gnu_extensions_p (parser))
10454 extension_specifier
10455 = cp_parser_storage_class_specifier_opt (parser);
10456 if (!extension_specifier)
10457 extension_specifier
10458 = cp_parser_function_specifier_opt (parser,
10459 /*decl_specs=*/NULL);
10462 /* Look for the `template' keyword. */
10463 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10464 /* Let the front end know that we are processing an explicit
10465 instantiation. */
10466 begin_explicit_instantiation ();
10467 /* [temp.explicit] says that we are supposed to ignore access
10468 control while processing explicit instantiation directives. */
10469 push_deferring_access_checks (dk_no_check);
10470 /* Parse a decl-specifier-seq. */
10471 cp_parser_decl_specifier_seq (parser,
10472 CP_PARSER_FLAGS_OPTIONAL,
10473 &decl_specifiers,
10474 &declares_class_or_enum);
10475 /* If there was exactly one decl-specifier, and it declared a class,
10476 and there's no declarator, then we have an explicit type
10477 instantiation. */
10478 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10480 tree type;
10482 type = check_tag_decl (&decl_specifiers);
10483 /* Turn access control back on for names used during
10484 template instantiation. */
10485 pop_deferring_access_checks ();
10486 if (type)
10487 do_type_instantiation (type, extension_specifier,
10488 /*complain=*/tf_error);
10490 else
10492 cp_declarator *declarator;
10493 tree decl;
10495 /* Parse the declarator. */
10496 declarator
10497 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10498 /*ctor_dtor_or_conv_p=*/NULL,
10499 /*parenthesized_p=*/NULL,
10500 /*member_p=*/false);
10501 if (declares_class_or_enum & 2)
10502 cp_parser_check_for_definition_in_return_type (declarator,
10503 decl_specifiers.type);
10504 if (declarator != cp_error_declarator)
10506 decl = grokdeclarator (declarator, &decl_specifiers,
10507 NORMAL, 0, &decl_specifiers.attributes);
10508 /* Turn access control back on for names used during
10509 template instantiation. */
10510 pop_deferring_access_checks ();
10511 /* Do the explicit instantiation. */
10512 do_decl_instantiation (decl, extension_specifier);
10514 else
10516 pop_deferring_access_checks ();
10517 /* Skip the body of the explicit instantiation. */
10518 cp_parser_skip_to_end_of_statement (parser);
10521 /* We're done with the instantiation. */
10522 end_explicit_instantiation ();
10524 cp_parser_consume_semicolon_at_end_of_statement (parser);
10527 /* Parse an explicit-specialization.
10529 explicit-specialization:
10530 template < > declaration
10532 Although the standard says `declaration', what it really means is:
10534 explicit-specialization:
10535 template <> decl-specifier [opt] init-declarator [opt] ;
10536 template <> function-definition
10537 template <> explicit-specialization
10538 template <> template-declaration */
10540 static void
10541 cp_parser_explicit_specialization (cp_parser* parser)
10543 bool need_lang_pop;
10544 /* Look for the `template' keyword. */
10545 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10546 /* Look for the `<'. */
10547 cp_parser_require (parser, CPP_LESS, "%<<%>");
10548 /* Look for the `>'. */
10549 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10550 /* We have processed another parameter list. */
10551 ++parser->num_template_parameter_lists;
10552 /* [temp]
10554 A template ... explicit specialization ... shall not have C
10555 linkage. */
10556 if (current_lang_name == lang_name_c)
10558 error ("template specialization with C linkage");
10559 /* Give it C++ linkage to avoid confusing other parts of the
10560 front end. */
10561 push_lang_context (lang_name_cplusplus);
10562 need_lang_pop = true;
10564 else
10565 need_lang_pop = false;
10566 /* Let the front end know that we are beginning a specialization. */
10567 if (!begin_specialization ())
10569 end_specialization ();
10570 cp_parser_skip_to_end_of_block_or_statement (parser);
10571 return;
10574 /* If the next keyword is `template', we need to figure out whether
10575 or not we're looking a template-declaration. */
10576 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10578 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10579 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10580 cp_parser_template_declaration_after_export (parser,
10581 /*member_p=*/false);
10582 else
10583 cp_parser_explicit_specialization (parser);
10585 else
10586 /* Parse the dependent declaration. */
10587 cp_parser_single_declaration (parser,
10588 /*checks=*/NULL,
10589 /*member_p=*/false,
10590 /*explicit_specialization_p=*/true,
10591 /*friend_p=*/NULL);
10592 /* We're done with the specialization. */
10593 end_specialization ();
10594 /* For the erroneous case of a template with C linkage, we pushed an
10595 implicit C++ linkage scope; exit that scope now. */
10596 if (need_lang_pop)
10597 pop_lang_context ();
10598 /* We're done with this parameter list. */
10599 --parser->num_template_parameter_lists;
10602 /* Parse a type-specifier.
10604 type-specifier:
10605 simple-type-specifier
10606 class-specifier
10607 enum-specifier
10608 elaborated-type-specifier
10609 cv-qualifier
10611 GNU Extension:
10613 type-specifier:
10614 __complex__
10616 Returns a representation of the type-specifier. For a
10617 class-specifier, enum-specifier, or elaborated-type-specifier, a
10618 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10620 The parser flags FLAGS is used to control type-specifier parsing.
10622 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10623 in a decl-specifier-seq.
10625 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10626 class-specifier, enum-specifier, or elaborated-type-specifier, then
10627 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10628 if a type is declared; 2 if it is defined. Otherwise, it is set to
10629 zero.
10631 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10632 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10633 is set to FALSE. */
10635 static tree
10636 cp_parser_type_specifier (cp_parser* parser,
10637 cp_parser_flags flags,
10638 cp_decl_specifier_seq *decl_specs,
10639 bool is_declaration,
10640 int* declares_class_or_enum,
10641 bool* is_cv_qualifier)
10643 tree type_spec = NULL_TREE;
10644 cp_token *token;
10645 enum rid keyword;
10646 cp_decl_spec ds = ds_last;
10648 /* Assume this type-specifier does not declare a new type. */
10649 if (declares_class_or_enum)
10650 *declares_class_or_enum = 0;
10651 /* And that it does not specify a cv-qualifier. */
10652 if (is_cv_qualifier)
10653 *is_cv_qualifier = false;
10654 /* Peek at the next token. */
10655 token = cp_lexer_peek_token (parser->lexer);
10657 /* If we're looking at a keyword, we can use that to guide the
10658 production we choose. */
10659 keyword = token->keyword;
10660 switch (keyword)
10662 case RID_ENUM:
10663 /* Look for the enum-specifier. */
10664 type_spec = cp_parser_enum_specifier (parser);
10665 /* If that worked, we're done. */
10666 if (type_spec)
10668 if (declares_class_or_enum)
10669 *declares_class_or_enum = 2;
10670 if (decl_specs)
10671 cp_parser_set_decl_spec_type (decl_specs,
10672 type_spec,
10673 /*user_defined_p=*/true);
10674 return type_spec;
10676 else
10677 goto elaborated_type_specifier;
10679 /* Any of these indicate either a class-specifier, or an
10680 elaborated-type-specifier. */
10681 case RID_CLASS:
10682 case RID_STRUCT:
10683 case RID_UNION:
10684 /* Parse tentatively so that we can back up if we don't find a
10685 class-specifier. */
10686 cp_parser_parse_tentatively (parser);
10687 /* Look for the class-specifier. */
10688 type_spec = cp_parser_class_specifier (parser);
10689 /* If that worked, we're done. */
10690 if (cp_parser_parse_definitely (parser))
10692 if (declares_class_or_enum)
10693 *declares_class_or_enum = 2;
10694 if (decl_specs)
10695 cp_parser_set_decl_spec_type (decl_specs,
10696 type_spec,
10697 /*user_defined_p=*/true);
10698 return type_spec;
10701 /* Fall through. */
10702 elaborated_type_specifier:
10703 /* We're declaring (not defining) a class or enum. */
10704 if (declares_class_or_enum)
10705 *declares_class_or_enum = 1;
10707 /* Fall through. */
10708 case RID_TYPENAME:
10709 /* Look for an elaborated-type-specifier. */
10710 type_spec
10711 = (cp_parser_elaborated_type_specifier
10712 (parser,
10713 decl_specs && decl_specs->specs[(int) ds_friend],
10714 is_declaration));
10715 if (decl_specs)
10716 cp_parser_set_decl_spec_type (decl_specs,
10717 type_spec,
10718 /*user_defined_p=*/true);
10719 return type_spec;
10721 case RID_CONST:
10722 ds = ds_const;
10723 if (is_cv_qualifier)
10724 *is_cv_qualifier = true;
10725 break;
10727 case RID_VOLATILE:
10728 ds = ds_volatile;
10729 if (is_cv_qualifier)
10730 *is_cv_qualifier = true;
10731 break;
10733 case RID_RESTRICT:
10734 ds = ds_restrict;
10735 if (is_cv_qualifier)
10736 *is_cv_qualifier = true;
10737 break;
10739 case RID_COMPLEX:
10740 /* The `__complex__' keyword is a GNU extension. */
10741 ds = ds_complex;
10742 break;
10744 default:
10745 break;
10748 /* Handle simple keywords. */
10749 if (ds != ds_last)
10751 if (decl_specs)
10753 ++decl_specs->specs[(int)ds];
10754 decl_specs->any_specifiers_p = true;
10756 return cp_lexer_consume_token (parser->lexer)->u.value;
10759 /* If we do not already have a type-specifier, assume we are looking
10760 at a simple-type-specifier. */
10761 type_spec = cp_parser_simple_type_specifier (parser,
10762 decl_specs,
10763 flags);
10765 /* If we didn't find a type-specifier, and a type-specifier was not
10766 optional in this context, issue an error message. */
10767 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10769 cp_parser_error (parser, "expected type specifier");
10770 return error_mark_node;
10773 return type_spec;
10776 /* Parse a simple-type-specifier.
10778 simple-type-specifier:
10779 :: [opt] nested-name-specifier [opt] type-name
10780 :: [opt] nested-name-specifier template template-id
10781 char
10782 wchar_t
10783 bool
10784 short
10786 long
10787 signed
10788 unsigned
10789 float
10790 double
10791 void
10793 C++0x Extension:
10795 simple-type-specifier:
10796 auto
10797 decltype ( expression )
10798 char16_t
10799 char32_t
10801 GNU Extension:
10803 simple-type-specifier:
10804 __typeof__ unary-expression
10805 __typeof__ ( type-id )
10807 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10808 appropriately updated. */
10810 static tree
10811 cp_parser_simple_type_specifier (cp_parser* parser,
10812 cp_decl_specifier_seq *decl_specs,
10813 cp_parser_flags flags)
10815 tree type = NULL_TREE;
10816 cp_token *token;
10818 /* Peek at the next token. */
10819 token = cp_lexer_peek_token (parser->lexer);
10821 /* If we're looking at a keyword, things are easy. */
10822 switch (token->keyword)
10824 case RID_CHAR:
10825 if (decl_specs)
10826 decl_specs->explicit_char_p = true;
10827 type = char_type_node;
10828 break;
10829 case RID_CHAR16:
10830 type = char16_type_node;
10831 break;
10832 case RID_CHAR32:
10833 type = char32_type_node;
10834 break;
10835 case RID_WCHAR:
10836 type = wchar_type_node;
10837 break;
10838 case RID_BOOL:
10839 type = boolean_type_node;
10840 break;
10841 case RID_SHORT:
10842 if (decl_specs)
10843 ++decl_specs->specs[(int) ds_short];
10844 type = short_integer_type_node;
10845 break;
10846 case RID_INT:
10847 if (decl_specs)
10848 decl_specs->explicit_int_p = true;
10849 type = integer_type_node;
10850 break;
10851 case RID_LONG:
10852 if (decl_specs)
10853 ++decl_specs->specs[(int) ds_long];
10854 type = long_integer_type_node;
10855 break;
10856 case RID_SIGNED:
10857 if (decl_specs)
10858 ++decl_specs->specs[(int) ds_signed];
10859 type = integer_type_node;
10860 break;
10861 case RID_UNSIGNED:
10862 if (decl_specs)
10863 ++decl_specs->specs[(int) ds_unsigned];
10864 type = unsigned_type_node;
10865 break;
10866 case RID_FLOAT:
10867 type = float_type_node;
10868 break;
10869 case RID_DOUBLE:
10870 type = double_type_node;
10871 break;
10872 case RID_VOID:
10873 type = void_type_node;
10874 break;
10876 case RID_AUTO:
10877 if (cxx_dialect != cxx98)
10879 /* Consume the token. */
10880 cp_lexer_consume_token (parser->lexer);
10881 /* We do not yet support the use of `auto' as a
10882 type-specifier. */
10883 error ("C++0x %<auto%> specifier not supported");
10885 break;
10887 case RID_DECLTYPE:
10888 /* Parse the `decltype' type. */
10889 type = cp_parser_decltype (parser);
10891 if (decl_specs)
10892 cp_parser_set_decl_spec_type (decl_specs, type,
10893 /*user_defined_p=*/true);
10895 return type;
10897 case RID_TYPEOF:
10898 /* Consume the `typeof' token. */
10899 cp_lexer_consume_token (parser->lexer);
10900 /* Parse the operand to `typeof'. */
10901 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10902 /* If it is not already a TYPE, take its type. */
10903 if (!TYPE_P (type))
10904 type = finish_typeof (type);
10906 if (decl_specs)
10907 cp_parser_set_decl_spec_type (decl_specs, type,
10908 /*user_defined_p=*/true);
10910 return type;
10912 default:
10913 break;
10916 /* If the type-specifier was for a built-in type, we're done. */
10917 if (type)
10919 tree id;
10921 /* Record the type. */
10922 if (decl_specs
10923 && (token->keyword != RID_SIGNED
10924 && token->keyword != RID_UNSIGNED
10925 && token->keyword != RID_SHORT
10926 && token->keyword != RID_LONG))
10927 cp_parser_set_decl_spec_type (decl_specs,
10928 type,
10929 /*user_defined=*/false);
10930 if (decl_specs)
10931 decl_specs->any_specifiers_p = true;
10933 /* Consume the token. */
10934 id = cp_lexer_consume_token (parser->lexer)->u.value;
10936 /* There is no valid C++ program where a non-template type is
10937 followed by a "<". That usually indicates that the user thought
10938 that the type was a template. */
10939 cp_parser_check_for_invalid_template_id (parser, type);
10941 return TYPE_NAME (type);
10944 /* The type-specifier must be a user-defined type. */
10945 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10947 bool qualified_p;
10948 bool global_p;
10950 /* Don't gobble tokens or issue error messages if this is an
10951 optional type-specifier. */
10952 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10953 cp_parser_parse_tentatively (parser);
10955 /* Look for the optional `::' operator. */
10956 global_p
10957 = (cp_parser_global_scope_opt (parser,
10958 /*current_scope_valid_p=*/false)
10959 != NULL_TREE);
10960 /* Look for the nested-name specifier. */
10961 qualified_p
10962 = (cp_parser_nested_name_specifier_opt (parser,
10963 /*typename_keyword_p=*/false,
10964 /*check_dependency_p=*/true,
10965 /*type_p=*/false,
10966 /*is_declaration=*/false)
10967 != NULL_TREE);
10968 /* If we have seen a nested-name-specifier, and the next token
10969 is `template', then we are using the template-id production. */
10970 if (parser->scope
10971 && cp_parser_optional_template_keyword (parser))
10973 /* Look for the template-id. */
10974 type = cp_parser_template_id (parser,
10975 /*template_keyword_p=*/true,
10976 /*check_dependency_p=*/true,
10977 /*is_declaration=*/false);
10978 /* If the template-id did not name a type, we are out of
10979 luck. */
10980 if (TREE_CODE (type) != TYPE_DECL)
10982 cp_parser_error (parser, "expected template-id for type");
10983 type = NULL_TREE;
10986 /* Otherwise, look for a type-name. */
10987 else
10988 type = cp_parser_type_name (parser);
10989 /* Keep track of all name-lookups performed in class scopes. */
10990 if (type
10991 && !global_p
10992 && !qualified_p
10993 && TREE_CODE (type) == TYPE_DECL
10994 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10995 maybe_note_name_used_in_class (DECL_NAME (type), type);
10996 /* If it didn't work out, we don't have a TYPE. */
10997 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10998 && !cp_parser_parse_definitely (parser))
10999 type = NULL_TREE;
11000 if (type && decl_specs)
11001 cp_parser_set_decl_spec_type (decl_specs, type,
11002 /*user_defined=*/true);
11005 /* If we didn't get a type-name, issue an error message. */
11006 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11008 cp_parser_error (parser, "expected type-name");
11009 return error_mark_node;
11012 /* There is no valid C++ program where a non-template type is
11013 followed by a "<". That usually indicates that the user thought
11014 that the type was a template. */
11015 if (type && type != error_mark_node)
11017 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11018 If it is, then the '<'...'>' enclose protocol names rather than
11019 template arguments, and so everything is fine. */
11020 if (c_dialect_objc ()
11021 && (objc_is_id (type) || objc_is_class_name (type)))
11023 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11024 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11026 /* Clobber the "unqualified" type previously entered into
11027 DECL_SPECS with the new, improved protocol-qualified version. */
11028 if (decl_specs)
11029 decl_specs->type = qual_type;
11031 return qual_type;
11034 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
11037 return type;
11040 /* Parse a type-name.
11042 type-name:
11043 class-name
11044 enum-name
11045 typedef-name
11047 enum-name:
11048 identifier
11050 typedef-name:
11051 identifier
11053 Returns a TYPE_DECL for the type. */
11055 static tree
11056 cp_parser_type_name (cp_parser* parser)
11058 tree type_decl;
11060 /* We can't know yet whether it is a class-name or not. */
11061 cp_parser_parse_tentatively (parser);
11062 /* Try a class-name. */
11063 type_decl = cp_parser_class_name (parser,
11064 /*typename_keyword_p=*/false,
11065 /*template_keyword_p=*/false,
11066 none_type,
11067 /*check_dependency_p=*/true,
11068 /*class_head_p=*/false,
11069 /*is_declaration=*/false);
11070 /* If it's not a class-name, keep looking. */
11071 if (!cp_parser_parse_definitely (parser))
11073 /* It must be a typedef-name or an enum-name. */
11074 return cp_parser_nonclass_name (parser);
11077 return type_decl;
11080 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11082 enum-name:
11083 identifier
11085 typedef-name:
11086 identifier
11088 Returns a TYPE_DECL for the type. */
11090 static tree
11091 cp_parser_nonclass_name (cp_parser* parser)
11093 tree type_decl;
11094 tree identifier;
11096 identifier = cp_parser_identifier (parser);
11097 if (identifier == error_mark_node)
11098 return error_mark_node;
11100 /* Look up the type-name. */
11101 type_decl = cp_parser_lookup_name_simple (parser, identifier);
11103 if (TREE_CODE (type_decl) != TYPE_DECL
11104 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11106 /* See if this is an Objective-C type. */
11107 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11108 tree type = objc_get_protocol_qualified_type (identifier, protos);
11109 if (type)
11110 type_decl = TYPE_NAME (type);
11113 /* Issue an error if we did not find a type-name. */
11114 if (TREE_CODE (type_decl) != TYPE_DECL)
11116 if (!cp_parser_simulate_error (parser))
11117 cp_parser_name_lookup_error (parser, identifier, type_decl,
11118 "is not a type");
11119 return error_mark_node;
11121 /* Remember that the name was used in the definition of the
11122 current class so that we can check later to see if the
11123 meaning would have been different after the class was
11124 entirely defined. */
11125 else if (type_decl != error_mark_node
11126 && !parser->scope)
11127 maybe_note_name_used_in_class (identifier, type_decl);
11129 return type_decl;
11132 /* Parse an elaborated-type-specifier. Note that the grammar given
11133 here incorporates the resolution to DR68.
11135 elaborated-type-specifier:
11136 class-key :: [opt] nested-name-specifier [opt] identifier
11137 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11138 enum :: [opt] nested-name-specifier [opt] identifier
11139 typename :: [opt] nested-name-specifier identifier
11140 typename :: [opt] nested-name-specifier template [opt]
11141 template-id
11143 GNU extension:
11145 elaborated-type-specifier:
11146 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11147 class-key attributes :: [opt] nested-name-specifier [opt]
11148 template [opt] template-id
11149 enum attributes :: [opt] nested-name-specifier [opt] identifier
11151 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11152 declared `friend'. If IS_DECLARATION is TRUE, then this
11153 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11154 something is being declared.
11156 Returns the TYPE specified. */
11158 static tree
11159 cp_parser_elaborated_type_specifier (cp_parser* parser,
11160 bool is_friend,
11161 bool is_declaration)
11163 enum tag_types tag_type;
11164 tree identifier;
11165 tree type = NULL_TREE;
11166 tree attributes = NULL_TREE;
11168 /* See if we're looking at the `enum' keyword. */
11169 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11171 /* Consume the `enum' token. */
11172 cp_lexer_consume_token (parser->lexer);
11173 /* Remember that it's an enumeration type. */
11174 tag_type = enum_type;
11175 /* Parse the attributes. */
11176 attributes = cp_parser_attributes_opt (parser);
11178 /* Or, it might be `typename'. */
11179 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11180 RID_TYPENAME))
11182 /* Consume the `typename' token. */
11183 cp_lexer_consume_token (parser->lexer);
11184 /* Remember that it's a `typename' type. */
11185 tag_type = typename_type;
11186 /* The `typename' keyword is only allowed in templates. */
11187 if (!processing_template_decl)
11188 pedwarn ("using %<typename%> outside of template");
11190 /* Otherwise it must be a class-key. */
11191 else
11193 tag_type = cp_parser_class_key (parser);
11194 if (tag_type == none_type)
11195 return error_mark_node;
11196 /* Parse the attributes. */
11197 attributes = cp_parser_attributes_opt (parser);
11200 /* Look for the `::' operator. */
11201 cp_parser_global_scope_opt (parser,
11202 /*current_scope_valid_p=*/false);
11203 /* Look for the nested-name-specifier. */
11204 if (tag_type == typename_type)
11206 if (!cp_parser_nested_name_specifier (parser,
11207 /*typename_keyword_p=*/true,
11208 /*check_dependency_p=*/true,
11209 /*type_p=*/true,
11210 is_declaration))
11211 return error_mark_node;
11213 else
11214 /* Even though `typename' is not present, the proposed resolution
11215 to Core Issue 180 says that in `class A<T>::B', `B' should be
11216 considered a type-name, even if `A<T>' is dependent. */
11217 cp_parser_nested_name_specifier_opt (parser,
11218 /*typename_keyword_p=*/true,
11219 /*check_dependency_p=*/true,
11220 /*type_p=*/true,
11221 is_declaration);
11222 /* For everything but enumeration types, consider a template-id.
11223 For an enumeration type, consider only a plain identifier. */
11224 if (tag_type != enum_type)
11226 bool template_p = false;
11227 tree decl;
11229 /* Allow the `template' keyword. */
11230 template_p = cp_parser_optional_template_keyword (parser);
11231 /* If we didn't see `template', we don't know if there's a
11232 template-id or not. */
11233 if (!template_p)
11234 cp_parser_parse_tentatively (parser);
11235 /* Parse the template-id. */
11236 decl = cp_parser_template_id (parser, template_p,
11237 /*check_dependency_p=*/true,
11238 is_declaration);
11239 /* If we didn't find a template-id, look for an ordinary
11240 identifier. */
11241 if (!template_p && !cp_parser_parse_definitely (parser))
11243 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11244 in effect, then we must assume that, upon instantiation, the
11245 template will correspond to a class. */
11246 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11247 && tag_type == typename_type)
11248 type = make_typename_type (parser->scope, decl,
11249 typename_type,
11250 /*complain=*/tf_error);
11251 else
11252 type = TREE_TYPE (decl);
11255 if (!type)
11257 identifier = cp_parser_identifier (parser);
11259 if (identifier == error_mark_node)
11261 parser->scope = NULL_TREE;
11262 return error_mark_node;
11265 /* For a `typename', we needn't call xref_tag. */
11266 if (tag_type == typename_type
11267 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11268 return cp_parser_make_typename_type (parser, parser->scope,
11269 identifier);
11270 /* Look up a qualified name in the usual way. */
11271 if (parser->scope)
11273 tree decl;
11274 tree ambiguous_decls;
11276 decl = cp_parser_lookup_name (parser, identifier,
11277 tag_type,
11278 /*is_template=*/false,
11279 /*is_namespace=*/false,
11280 /*check_dependency=*/true,
11281 &ambiguous_decls);
11283 /* If the lookup was ambiguous, an error will already have been
11284 issued. */
11285 if (ambiguous_decls)
11286 return error_mark_node;
11288 /* If we are parsing friend declaration, DECL may be a
11289 TEMPLATE_DECL tree node here. However, we need to check
11290 whether this TEMPLATE_DECL results in valid code. Consider
11291 the following example:
11293 namespace N {
11294 template <class T> class C {};
11296 class X {
11297 template <class T> friend class N::C; // #1, valid code
11299 template <class T> class Y {
11300 friend class N::C; // #2, invalid code
11303 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11304 name lookup of `N::C'. We see that friend declaration must
11305 be template for the code to be valid. Note that
11306 processing_template_decl does not work here since it is
11307 always 1 for the above two cases. */
11309 decl = (cp_parser_maybe_treat_template_as_class
11310 (decl, /*tag_name_p=*/is_friend
11311 && parser->num_template_parameter_lists));
11313 if (TREE_CODE (decl) != TYPE_DECL)
11315 cp_parser_diagnose_invalid_type_name (parser,
11316 parser->scope,
11317 identifier);
11318 return error_mark_node;
11321 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11323 bool allow_template = (parser->num_template_parameter_lists
11324 || DECL_SELF_REFERENCE_P (decl));
11325 type = check_elaborated_type_specifier (tag_type, decl,
11326 allow_template);
11328 if (type == error_mark_node)
11329 return error_mark_node;
11332 /* Forward declarations of nested types, such as
11334 class C1::C2;
11335 class C1::C2::C3;
11337 are invalid unless all components preceding the final '::'
11338 are complete. If all enclosing types are complete, these
11339 declarations become merely pointless.
11341 Invalid forward declarations of nested types are errors
11342 caught elsewhere in parsing. Those that are pointless arrive
11343 here. */
11345 if (cp_parser_declares_only_class_p (parser)
11346 && !is_friend && !processing_explicit_instantiation)
11347 warning (0, "declaration %qD does not declare anything", decl);
11349 type = TREE_TYPE (decl);
11351 else
11353 /* An elaborated-type-specifier sometimes introduces a new type and
11354 sometimes names an existing type. Normally, the rule is that it
11355 introduces a new type only if there is not an existing type of
11356 the same name already in scope. For example, given:
11358 struct S {};
11359 void f() { struct S s; }
11361 the `struct S' in the body of `f' is the same `struct S' as in
11362 the global scope; the existing definition is used. However, if
11363 there were no global declaration, this would introduce a new
11364 local class named `S'.
11366 An exception to this rule applies to the following code:
11368 namespace N { struct S; }
11370 Here, the elaborated-type-specifier names a new type
11371 unconditionally; even if there is already an `S' in the
11372 containing scope this declaration names a new type.
11373 This exception only applies if the elaborated-type-specifier
11374 forms the complete declaration:
11376 [class.name]
11378 A declaration consisting solely of `class-key identifier ;' is
11379 either a redeclaration of the name in the current scope or a
11380 forward declaration of the identifier as a class name. It
11381 introduces the name into the current scope.
11383 We are in this situation precisely when the next token is a `;'.
11385 An exception to the exception is that a `friend' declaration does
11386 *not* name a new type; i.e., given:
11388 struct S { friend struct T; };
11390 `T' is not a new type in the scope of `S'.
11392 Also, `new struct S' or `sizeof (struct S)' never results in the
11393 definition of a new type; a new type can only be declared in a
11394 declaration context. */
11396 tag_scope ts;
11397 bool template_p;
11399 if (is_friend)
11400 /* Friends have special name lookup rules. */
11401 ts = ts_within_enclosing_non_class;
11402 else if (is_declaration
11403 && cp_lexer_next_token_is (parser->lexer,
11404 CPP_SEMICOLON))
11405 /* This is a `class-key identifier ;' */
11406 ts = ts_current;
11407 else
11408 ts = ts_global;
11410 template_p =
11411 (parser->num_template_parameter_lists
11412 && (cp_parser_next_token_starts_class_definition_p (parser)
11413 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11414 /* An unqualified name was used to reference this type, so
11415 there were no qualifying templates. */
11416 if (!cp_parser_check_template_parameters (parser,
11417 /*num_templates=*/0))
11418 return error_mark_node;
11419 type = xref_tag (tag_type, identifier, ts, template_p);
11423 if (type == error_mark_node)
11424 return error_mark_node;
11426 /* Allow attributes on forward declarations of classes. */
11427 if (attributes)
11429 if (TREE_CODE (type) == TYPENAME_TYPE)
11430 warning (OPT_Wattributes,
11431 "attributes ignored on uninstantiated type");
11432 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11433 && ! processing_explicit_instantiation)
11434 warning (OPT_Wattributes,
11435 "attributes ignored on template instantiation");
11436 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11437 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11438 else
11439 warning (OPT_Wattributes,
11440 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11443 if (tag_type != enum_type)
11444 cp_parser_check_class_key (tag_type, type);
11446 /* A "<" cannot follow an elaborated type specifier. If that
11447 happens, the user was probably trying to form a template-id. */
11448 cp_parser_check_for_invalid_template_id (parser, type);
11450 return type;
11453 /* Parse an enum-specifier.
11455 enum-specifier:
11456 enum identifier [opt] { enumerator-list [opt] }
11458 GNU Extensions:
11459 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11460 attributes[opt]
11462 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11463 if the token stream isn't an enum-specifier after all. */
11465 static tree
11466 cp_parser_enum_specifier (cp_parser* parser)
11468 tree identifier;
11469 tree type;
11470 tree attributes;
11472 /* Parse tentatively so that we can back up if we don't find a
11473 enum-specifier. */
11474 cp_parser_parse_tentatively (parser);
11476 /* Caller guarantees that the current token is 'enum', an identifier
11477 possibly follows, and the token after that is an opening brace.
11478 If we don't have an identifier, fabricate an anonymous name for
11479 the enumeration being defined. */
11480 cp_lexer_consume_token (parser->lexer);
11482 attributes = cp_parser_attributes_opt (parser);
11484 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11485 identifier = cp_parser_identifier (parser);
11486 else
11487 identifier = make_anon_name ();
11489 /* Look for the `{' but don't consume it yet. */
11490 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11491 cp_parser_simulate_error (parser);
11493 if (!cp_parser_parse_definitely (parser))
11494 return NULL_TREE;
11496 /* Issue an error message if type-definitions are forbidden here. */
11497 if (!cp_parser_check_type_definition (parser))
11498 type = error_mark_node;
11499 else
11500 /* Create the new type. We do this before consuming the opening
11501 brace so the enum will be recorded as being on the line of its
11502 tag (or the 'enum' keyword, if there is no tag). */
11503 type = start_enum (identifier);
11505 /* Consume the opening brace. */
11506 cp_lexer_consume_token (parser->lexer);
11508 if (type == error_mark_node)
11510 cp_parser_skip_to_end_of_block_or_statement (parser);
11511 return error_mark_node;
11514 /* If the next token is not '}', then there are some enumerators. */
11515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11516 cp_parser_enumerator_list (parser, type);
11518 /* Consume the final '}'. */
11519 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11521 /* Look for trailing attributes to apply to this enumeration, and
11522 apply them if appropriate. */
11523 if (cp_parser_allow_gnu_extensions_p (parser))
11525 tree trailing_attr = cp_parser_attributes_opt (parser);
11526 cplus_decl_attributes (&type,
11527 trailing_attr,
11528 (int) ATTR_FLAG_TYPE_IN_PLACE);
11531 /* Finish up the enumeration. */
11532 finish_enum (type);
11534 return type;
11537 /* Parse an enumerator-list. The enumerators all have the indicated
11538 TYPE.
11540 enumerator-list:
11541 enumerator-definition
11542 enumerator-list , enumerator-definition */
11544 static void
11545 cp_parser_enumerator_list (cp_parser* parser, tree type)
11547 while (true)
11549 /* Parse an enumerator-definition. */
11550 cp_parser_enumerator_definition (parser, type);
11552 /* If the next token is not a ',', we've reached the end of
11553 the list. */
11554 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11555 break;
11556 /* Otherwise, consume the `,' and keep going. */
11557 cp_lexer_consume_token (parser->lexer);
11558 /* If the next token is a `}', there is a trailing comma. */
11559 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11561 if (pedantic && !in_system_header)
11562 pedwarn ("comma at end of enumerator list");
11563 break;
11568 /* Parse an enumerator-definition. The enumerator has the indicated
11569 TYPE.
11571 enumerator-definition:
11572 enumerator
11573 enumerator = constant-expression
11575 enumerator:
11576 identifier */
11578 static void
11579 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11581 tree identifier;
11582 tree value;
11584 /* Look for the identifier. */
11585 identifier = cp_parser_identifier (parser);
11586 if (identifier == error_mark_node)
11587 return;
11589 /* If the next token is an '=', then there is an explicit value. */
11590 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11592 /* Consume the `=' token. */
11593 cp_lexer_consume_token (parser->lexer);
11594 /* Parse the value. */
11595 value = cp_parser_constant_expression (parser,
11596 /*allow_non_constant_p=*/false,
11597 NULL);
11599 else
11600 value = NULL_TREE;
11602 /* Create the enumerator. */
11603 build_enumerator (identifier, value, type);
11606 /* Parse a namespace-name.
11608 namespace-name:
11609 original-namespace-name
11610 namespace-alias
11612 Returns the NAMESPACE_DECL for the namespace. */
11614 static tree
11615 cp_parser_namespace_name (cp_parser* parser)
11617 tree identifier;
11618 tree namespace_decl;
11620 /* Get the name of the namespace. */
11621 identifier = cp_parser_identifier (parser);
11622 if (identifier == error_mark_node)
11623 return error_mark_node;
11625 /* Look up the identifier in the currently active scope. Look only
11626 for namespaces, due to:
11628 [basic.lookup.udir]
11630 When looking up a namespace-name in a using-directive or alias
11631 definition, only namespace names are considered.
11633 And:
11635 [basic.lookup.qual]
11637 During the lookup of a name preceding the :: scope resolution
11638 operator, object, function, and enumerator names are ignored.
11640 (Note that cp_parser_class_or_namespace_name only calls this
11641 function if the token after the name is the scope resolution
11642 operator.) */
11643 namespace_decl = cp_parser_lookup_name (parser, identifier,
11644 none_type,
11645 /*is_template=*/false,
11646 /*is_namespace=*/true,
11647 /*check_dependency=*/true,
11648 /*ambiguous_decls=*/NULL);
11649 /* If it's not a namespace, issue an error. */
11650 if (namespace_decl == error_mark_node
11651 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11653 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11654 error ("%qD is not a namespace-name", identifier);
11655 cp_parser_error (parser, "expected namespace-name");
11656 namespace_decl = error_mark_node;
11659 return namespace_decl;
11662 /* Parse a namespace-definition.
11664 namespace-definition:
11665 named-namespace-definition
11666 unnamed-namespace-definition
11668 named-namespace-definition:
11669 original-namespace-definition
11670 extension-namespace-definition
11672 original-namespace-definition:
11673 namespace identifier { namespace-body }
11675 extension-namespace-definition:
11676 namespace original-namespace-name { namespace-body }
11678 unnamed-namespace-definition:
11679 namespace { namespace-body } */
11681 static void
11682 cp_parser_namespace_definition (cp_parser* parser)
11684 tree identifier, attribs;
11685 bool has_visibility;
11686 bool is_inline;
11688 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11690 is_inline = true;
11691 cp_lexer_consume_token (parser->lexer);
11693 else
11694 is_inline = false;
11696 /* Look for the `namespace' keyword. */
11697 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11699 /* Get the name of the namespace. We do not attempt to distinguish
11700 between an original-namespace-definition and an
11701 extension-namespace-definition at this point. The semantic
11702 analysis routines are responsible for that. */
11703 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11704 identifier = cp_parser_identifier (parser);
11705 else
11706 identifier = NULL_TREE;
11708 /* Parse any specified attributes. */
11709 attribs = cp_parser_attributes_opt (parser);
11711 /* Look for the `{' to start the namespace. */
11712 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11713 /* Start the namespace. */
11714 push_namespace (identifier);
11716 /* "inline namespace" is equivalent to a stub namespace definition
11717 followed by a strong using directive. */
11718 if (is_inline)
11720 tree namespace = current_namespace;
11721 /* Set up namespace association. */
11722 DECL_NAMESPACE_ASSOCIATIONS (namespace)
11723 = tree_cons (CP_DECL_CONTEXT (namespace), NULL_TREE,
11724 DECL_NAMESPACE_ASSOCIATIONS (namespace));
11725 /* Import the contents of the inline namespace. */
11726 pop_namespace ();
11727 do_using_directive (namespace);
11728 push_namespace (identifier);
11731 has_visibility = handle_namespace_attrs (current_namespace, attribs);
11733 /* Parse the body of the namespace. */
11734 cp_parser_namespace_body (parser);
11736 #ifdef HANDLE_PRAGMA_VISIBILITY
11737 if (has_visibility)
11738 pop_visibility ();
11739 #endif
11741 /* Finish the namespace. */
11742 pop_namespace ();
11743 /* Look for the final `}'. */
11744 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11747 /* Parse a namespace-body.
11749 namespace-body:
11750 declaration-seq [opt] */
11752 static void
11753 cp_parser_namespace_body (cp_parser* parser)
11755 cp_parser_declaration_seq_opt (parser);
11758 /* Parse a namespace-alias-definition.
11760 namespace-alias-definition:
11761 namespace identifier = qualified-namespace-specifier ; */
11763 static void
11764 cp_parser_namespace_alias_definition (cp_parser* parser)
11766 tree identifier;
11767 tree namespace_specifier;
11769 /* Look for the `namespace' keyword. */
11770 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11771 /* Look for the identifier. */
11772 identifier = cp_parser_identifier (parser);
11773 if (identifier == error_mark_node)
11774 return;
11775 /* Look for the `=' token. */
11776 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11777 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11779 error ("%<namespace%> definition is not allowed here");
11780 /* Skip the definition. */
11781 cp_lexer_consume_token (parser->lexer);
11782 if (cp_parser_skip_to_closing_brace (parser))
11783 cp_lexer_consume_token (parser->lexer);
11784 return;
11786 cp_parser_require (parser, CPP_EQ, "%<=%>");
11787 /* Look for the qualified-namespace-specifier. */
11788 namespace_specifier
11789 = cp_parser_qualified_namespace_specifier (parser);
11790 /* Look for the `;' token. */
11791 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11793 /* Register the alias in the symbol table. */
11794 do_namespace_alias (identifier, namespace_specifier);
11797 /* Parse a qualified-namespace-specifier.
11799 qualified-namespace-specifier:
11800 :: [opt] nested-name-specifier [opt] namespace-name
11802 Returns a NAMESPACE_DECL corresponding to the specified
11803 namespace. */
11805 static tree
11806 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11808 /* Look for the optional `::'. */
11809 cp_parser_global_scope_opt (parser,
11810 /*current_scope_valid_p=*/false);
11812 /* Look for the optional nested-name-specifier. */
11813 cp_parser_nested_name_specifier_opt (parser,
11814 /*typename_keyword_p=*/false,
11815 /*check_dependency_p=*/true,
11816 /*type_p=*/false,
11817 /*is_declaration=*/true);
11819 return cp_parser_namespace_name (parser);
11822 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11823 access declaration.
11825 using-declaration:
11826 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11827 using :: unqualified-id ;
11829 access-declaration:
11830 qualified-id ;
11834 static bool
11835 cp_parser_using_declaration (cp_parser* parser,
11836 bool access_declaration_p)
11838 cp_token *token;
11839 bool typename_p = false;
11840 bool global_scope_p;
11841 tree decl;
11842 tree identifier;
11843 tree qscope;
11845 if (access_declaration_p)
11846 cp_parser_parse_tentatively (parser);
11847 else
11849 /* Look for the `using' keyword. */
11850 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
11852 /* Peek at the next token. */
11853 token = cp_lexer_peek_token (parser->lexer);
11854 /* See if it's `typename'. */
11855 if (token->keyword == RID_TYPENAME)
11857 /* Remember that we've seen it. */
11858 typename_p = true;
11859 /* Consume the `typename' token. */
11860 cp_lexer_consume_token (parser->lexer);
11864 /* Look for the optional global scope qualification. */
11865 global_scope_p
11866 = (cp_parser_global_scope_opt (parser,
11867 /*current_scope_valid_p=*/false)
11868 != NULL_TREE);
11870 /* If we saw `typename', or didn't see `::', then there must be a
11871 nested-name-specifier present. */
11872 if (typename_p || !global_scope_p)
11873 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11874 /*check_dependency_p=*/true,
11875 /*type_p=*/false,
11876 /*is_declaration=*/true);
11877 /* Otherwise, we could be in either of the two productions. In that
11878 case, treat the nested-name-specifier as optional. */
11879 else
11880 qscope = cp_parser_nested_name_specifier_opt (parser,
11881 /*typename_keyword_p=*/false,
11882 /*check_dependency_p=*/true,
11883 /*type_p=*/false,
11884 /*is_declaration=*/true);
11885 if (!qscope)
11886 qscope = global_namespace;
11888 if (access_declaration_p && cp_parser_error_occurred (parser))
11889 /* Something has already gone wrong; there's no need to parse
11890 further. Since an error has occurred, the return value of
11891 cp_parser_parse_definitely will be false, as required. */
11892 return cp_parser_parse_definitely (parser);
11894 /* Parse the unqualified-id. */
11895 identifier = cp_parser_unqualified_id (parser,
11896 /*template_keyword_p=*/false,
11897 /*check_dependency_p=*/true,
11898 /*declarator_p=*/true,
11899 /*optional_p=*/false);
11901 if (access_declaration_p)
11903 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11904 cp_parser_simulate_error (parser);
11905 if (!cp_parser_parse_definitely (parser))
11906 return false;
11909 /* The function we call to handle a using-declaration is different
11910 depending on what scope we are in. */
11911 if (qscope == error_mark_node || identifier == error_mark_node)
11913 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11914 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11915 /* [namespace.udecl]
11917 A using declaration shall not name a template-id. */
11918 error ("a template-id may not appear in a using-declaration");
11919 else
11921 if (at_class_scope_p ())
11923 /* Create the USING_DECL. */
11924 decl = do_class_using_decl (parser->scope, identifier);
11926 if (check_for_bare_parameter_packs (decl))
11927 return false;
11928 else
11929 /* Add it to the list of members in this class. */
11930 finish_member_declaration (decl);
11932 else
11934 decl = cp_parser_lookup_name_simple (parser, identifier);
11935 if (decl == error_mark_node)
11936 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11937 else if (check_for_bare_parameter_packs (decl))
11938 return false;
11939 else if (!at_namespace_scope_p ())
11940 do_local_using_decl (decl, qscope, identifier);
11941 else
11942 do_toplevel_using_decl (decl, qscope, identifier);
11946 /* Look for the final `;'. */
11947 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11949 return true;
11952 /* Parse a using-directive.
11954 using-directive:
11955 using namespace :: [opt] nested-name-specifier [opt]
11956 namespace-name ; */
11958 static void
11959 cp_parser_using_directive (cp_parser* parser)
11961 tree namespace_decl;
11962 tree attribs;
11964 /* Look for the `using' keyword. */
11965 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
11966 /* And the `namespace' keyword. */
11967 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11968 /* Look for the optional `::' operator. */
11969 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11970 /* And the optional nested-name-specifier. */
11971 cp_parser_nested_name_specifier_opt (parser,
11972 /*typename_keyword_p=*/false,
11973 /*check_dependency_p=*/true,
11974 /*type_p=*/false,
11975 /*is_declaration=*/true);
11976 /* Get the namespace being used. */
11977 namespace_decl = cp_parser_namespace_name (parser);
11978 /* And any specified attributes. */
11979 attribs = cp_parser_attributes_opt (parser);
11980 /* Update the symbol table. */
11981 parse_using_directive (namespace_decl, attribs);
11982 /* Look for the final `;'. */
11983 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11986 /* Parse an asm-definition.
11988 asm-definition:
11989 asm ( string-literal ) ;
11991 GNU Extension:
11993 asm-definition:
11994 asm volatile [opt] ( string-literal ) ;
11995 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11996 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11997 : asm-operand-list [opt] ) ;
11998 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11999 : asm-operand-list [opt]
12000 : asm-operand-list [opt] ) ; */
12002 static void
12003 cp_parser_asm_definition (cp_parser* parser)
12005 tree string;
12006 tree outputs = NULL_TREE;
12007 tree inputs = NULL_TREE;
12008 tree clobbers = NULL_TREE;
12009 tree asm_stmt;
12010 bool volatile_p = false;
12011 bool extended_p = false;
12012 bool invalid_inputs_p = false;
12013 bool invalid_outputs_p = false;
12015 /* Look for the `asm' keyword. */
12016 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12017 /* See if the next token is `volatile'. */
12018 if (cp_parser_allow_gnu_extensions_p (parser)
12019 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12021 /* Remember that we saw the `volatile' keyword. */
12022 volatile_p = true;
12023 /* Consume the token. */
12024 cp_lexer_consume_token (parser->lexer);
12026 /* Look for the opening `('. */
12027 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12028 return;
12029 /* Look for the string. */
12030 string = cp_parser_string_literal (parser, false, false);
12031 if (string == error_mark_node)
12033 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12034 /*consume_paren=*/true);
12035 return;
12038 /* If we're allowing GNU extensions, check for the extended assembly
12039 syntax. Unfortunately, the `:' tokens need not be separated by
12040 a space in C, and so, for compatibility, we tolerate that here
12041 too. Doing that means that we have to treat the `::' operator as
12042 two `:' tokens. */
12043 if (cp_parser_allow_gnu_extensions_p (parser)
12044 && parser->in_function_body
12045 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12046 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12048 bool inputs_p = false;
12049 bool clobbers_p = false;
12051 /* The extended syntax was used. */
12052 extended_p = true;
12054 /* Look for outputs. */
12055 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12057 /* Consume the `:'. */
12058 cp_lexer_consume_token (parser->lexer);
12059 /* Parse the output-operands. */
12060 if (cp_lexer_next_token_is_not (parser->lexer,
12061 CPP_COLON)
12062 && cp_lexer_next_token_is_not (parser->lexer,
12063 CPP_SCOPE)
12064 && cp_lexer_next_token_is_not (parser->lexer,
12065 CPP_CLOSE_PAREN))
12066 outputs = cp_parser_asm_operand_list (parser);
12068 if (outputs == error_mark_node)
12069 invalid_outputs_p = true;
12071 /* If the next token is `::', there are no outputs, and the
12072 next token is the beginning of the inputs. */
12073 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12074 /* The inputs are coming next. */
12075 inputs_p = true;
12077 /* Look for inputs. */
12078 if (inputs_p
12079 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12081 /* Consume the `:' or `::'. */
12082 cp_lexer_consume_token (parser->lexer);
12083 /* Parse the output-operands. */
12084 if (cp_lexer_next_token_is_not (parser->lexer,
12085 CPP_COLON)
12086 && cp_lexer_next_token_is_not (parser->lexer,
12087 CPP_CLOSE_PAREN))
12088 inputs = cp_parser_asm_operand_list (parser);
12090 if (inputs == error_mark_node)
12091 invalid_inputs_p = true;
12093 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12094 /* The clobbers are coming next. */
12095 clobbers_p = true;
12097 /* Look for clobbers. */
12098 if (clobbers_p
12099 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12101 /* Consume the `:' or `::'. */
12102 cp_lexer_consume_token (parser->lexer);
12103 /* Parse the clobbers. */
12104 if (cp_lexer_next_token_is_not (parser->lexer,
12105 CPP_CLOSE_PAREN))
12106 clobbers = cp_parser_asm_clobber_list (parser);
12109 /* Look for the closing `)'. */
12110 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12111 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12112 /*consume_paren=*/true);
12113 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12115 if (!invalid_inputs_p && !invalid_outputs_p)
12117 /* Create the ASM_EXPR. */
12118 if (parser->in_function_body)
12120 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12121 inputs, clobbers);
12122 /* If the extended syntax was not used, mark the ASM_EXPR. */
12123 if (!extended_p)
12125 tree temp = asm_stmt;
12126 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12127 temp = TREE_OPERAND (temp, 0);
12129 ASM_INPUT_P (temp) = 1;
12132 else
12133 cgraph_add_asm_node (string);
12137 /* Declarators [gram.dcl.decl] */
12139 /* Parse an init-declarator.
12141 init-declarator:
12142 declarator initializer [opt]
12144 GNU Extension:
12146 init-declarator:
12147 declarator asm-specification [opt] attributes [opt] initializer [opt]
12149 function-definition:
12150 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12151 function-body
12152 decl-specifier-seq [opt] declarator function-try-block
12154 GNU Extension:
12156 function-definition:
12157 __extension__ function-definition
12159 The DECL_SPECIFIERS apply to this declarator. Returns a
12160 representation of the entity declared. If MEMBER_P is TRUE, then
12161 this declarator appears in a class scope. The new DECL created by
12162 this declarator is returned.
12164 The CHECKS are access checks that should be performed once we know
12165 what entity is being declared (and, therefore, what classes have
12166 befriended it).
12168 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12169 for a function-definition here as well. If the declarator is a
12170 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12171 be TRUE upon return. By that point, the function-definition will
12172 have been completely parsed.
12174 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12175 is FALSE. */
12177 static tree
12178 cp_parser_init_declarator (cp_parser* parser,
12179 cp_decl_specifier_seq *decl_specifiers,
12180 VEC (deferred_access_check,gc)* checks,
12181 bool function_definition_allowed_p,
12182 bool member_p,
12183 int declares_class_or_enum,
12184 bool* function_definition_p)
12186 cp_token *token;
12187 cp_declarator *declarator;
12188 tree prefix_attributes;
12189 tree attributes;
12190 tree asm_specification;
12191 tree initializer;
12192 tree decl = NULL_TREE;
12193 tree scope;
12194 bool is_initialized;
12195 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12196 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12197 "(...)". */
12198 enum cpp_ttype initialization_kind;
12199 bool is_parenthesized_init = false;
12200 bool is_non_constant_init;
12201 int ctor_dtor_or_conv_p;
12202 bool friend_p;
12203 tree pushed_scope = NULL;
12205 /* Gather the attributes that were provided with the
12206 decl-specifiers. */
12207 prefix_attributes = decl_specifiers->attributes;
12209 /* Assume that this is not the declarator for a function
12210 definition. */
12211 if (function_definition_p)
12212 *function_definition_p = false;
12214 /* Defer access checks while parsing the declarator; we cannot know
12215 what names are accessible until we know what is being
12216 declared. */
12217 resume_deferring_access_checks ();
12219 /* Parse the declarator. */
12220 declarator
12221 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12222 &ctor_dtor_or_conv_p,
12223 /*parenthesized_p=*/NULL,
12224 /*member_p=*/false);
12225 /* Gather up the deferred checks. */
12226 stop_deferring_access_checks ();
12228 /* If the DECLARATOR was erroneous, there's no need to go
12229 further. */
12230 if (declarator == cp_error_declarator)
12231 return error_mark_node;
12233 /* Check that the number of template-parameter-lists is OK. */
12234 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12235 return error_mark_node;
12237 if (declares_class_or_enum & 2)
12238 cp_parser_check_for_definition_in_return_type (declarator,
12239 decl_specifiers->type);
12241 /* Figure out what scope the entity declared by the DECLARATOR is
12242 located in. `grokdeclarator' sometimes changes the scope, so
12243 we compute it now. */
12244 scope = get_scope_of_declarator (declarator);
12246 /* If we're allowing GNU extensions, look for an asm-specification
12247 and attributes. */
12248 if (cp_parser_allow_gnu_extensions_p (parser))
12250 /* Look for an asm-specification. */
12251 asm_specification = cp_parser_asm_specification_opt (parser);
12252 /* And attributes. */
12253 attributes = cp_parser_attributes_opt (parser);
12255 else
12257 asm_specification = NULL_TREE;
12258 attributes = NULL_TREE;
12261 /* Peek at the next token. */
12262 token = cp_lexer_peek_token (parser->lexer);
12263 /* Check to see if the token indicates the start of a
12264 function-definition. */
12265 if (cp_parser_token_starts_function_definition_p (token))
12267 if (!function_definition_allowed_p)
12269 /* If a function-definition should not appear here, issue an
12270 error message. */
12271 cp_parser_error (parser,
12272 "a function-definition is not allowed here");
12273 return error_mark_node;
12275 else
12277 /* Neither attributes nor an asm-specification are allowed
12278 on a function-definition. */
12279 if (asm_specification)
12280 error ("an asm-specification is not allowed on a function-definition");
12281 if (attributes)
12282 error ("attributes are not allowed on a function-definition");
12283 /* This is a function-definition. */
12284 *function_definition_p = true;
12286 /* Parse the function definition. */
12287 if (member_p)
12288 decl = cp_parser_save_member_function_body (parser,
12289 decl_specifiers,
12290 declarator,
12291 prefix_attributes);
12292 else
12293 decl
12294 = (cp_parser_function_definition_from_specifiers_and_declarator
12295 (parser, decl_specifiers, prefix_attributes, declarator));
12297 return decl;
12301 /* [dcl.dcl]
12303 Only in function declarations for constructors, destructors, and
12304 type conversions can the decl-specifier-seq be omitted.
12306 We explicitly postpone this check past the point where we handle
12307 function-definitions because we tolerate function-definitions
12308 that are missing their return types in some modes. */
12309 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12311 cp_parser_error (parser,
12312 "expected constructor, destructor, or type conversion");
12313 return error_mark_node;
12316 /* An `=' or an `(' indicates an initializer. */
12317 if (token->type == CPP_EQ
12318 || token->type == CPP_OPEN_PAREN)
12320 is_initialized = true;
12321 initialization_kind = token->type;
12323 else
12325 /* If the init-declarator isn't initialized and isn't followed by a
12326 `,' or `;', it's not a valid init-declarator. */
12327 if (token->type != CPP_COMMA
12328 && token->type != CPP_SEMICOLON)
12330 cp_parser_error (parser, "expected initializer");
12331 return error_mark_node;
12333 is_initialized = false;
12334 initialization_kind = CPP_EOF;
12337 /* Because start_decl has side-effects, we should only call it if we
12338 know we're going ahead. By this point, we know that we cannot
12339 possibly be looking at any other construct. */
12340 cp_parser_commit_to_tentative_parse (parser);
12342 /* If the decl specifiers were bad, issue an error now that we're
12343 sure this was intended to be a declarator. Then continue
12344 declaring the variable(s), as int, to try to cut down on further
12345 errors. */
12346 if (decl_specifiers->any_specifiers_p
12347 && decl_specifiers->type == error_mark_node)
12349 cp_parser_error (parser, "invalid type in declaration");
12350 decl_specifiers->type = integer_type_node;
12353 /* Check to see whether or not this declaration is a friend. */
12354 friend_p = cp_parser_friend_p (decl_specifiers);
12356 /* Enter the newly declared entry in the symbol table. If we're
12357 processing a declaration in a class-specifier, we wait until
12358 after processing the initializer. */
12359 if (!member_p)
12361 if (parser->in_unbraced_linkage_specification_p)
12362 decl_specifiers->storage_class = sc_extern;
12363 decl = start_decl (declarator, decl_specifiers,
12364 is_initialized, attributes, prefix_attributes,
12365 &pushed_scope);
12367 else if (scope)
12368 /* Enter the SCOPE. That way unqualified names appearing in the
12369 initializer will be looked up in SCOPE. */
12370 pushed_scope = push_scope (scope);
12372 /* Perform deferred access control checks, now that we know in which
12373 SCOPE the declared entity resides. */
12374 if (!member_p && decl)
12376 tree saved_current_function_decl = NULL_TREE;
12378 /* If the entity being declared is a function, pretend that we
12379 are in its scope. If it is a `friend', it may have access to
12380 things that would not otherwise be accessible. */
12381 if (TREE_CODE (decl) == FUNCTION_DECL)
12383 saved_current_function_decl = current_function_decl;
12384 current_function_decl = decl;
12387 /* Perform access checks for template parameters. */
12388 cp_parser_perform_template_parameter_access_checks (checks);
12390 /* Perform the access control checks for the declarator and the
12391 the decl-specifiers. */
12392 perform_deferred_access_checks ();
12394 /* Restore the saved value. */
12395 if (TREE_CODE (decl) == FUNCTION_DECL)
12396 current_function_decl = saved_current_function_decl;
12399 /* Parse the initializer. */
12400 initializer = NULL_TREE;
12401 is_parenthesized_init = false;
12402 is_non_constant_init = true;
12403 if (is_initialized)
12405 if (function_declarator_p (declarator))
12407 if (initialization_kind == CPP_EQ)
12408 initializer = cp_parser_pure_specifier (parser);
12409 else
12411 /* If the declaration was erroneous, we don't really
12412 know what the user intended, so just silently
12413 consume the initializer. */
12414 if (decl != error_mark_node)
12415 error ("initializer provided for function");
12416 cp_parser_skip_to_closing_parenthesis (parser,
12417 /*recovering=*/true,
12418 /*or_comma=*/false,
12419 /*consume_paren=*/true);
12422 else
12423 initializer = cp_parser_initializer (parser,
12424 &is_parenthesized_init,
12425 &is_non_constant_init);
12428 /* The old parser allows attributes to appear after a parenthesized
12429 initializer. Mark Mitchell proposed removing this functionality
12430 on the GCC mailing lists on 2002-08-13. This parser accepts the
12431 attributes -- but ignores them. */
12432 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12433 if (cp_parser_attributes_opt (parser))
12434 warning (OPT_Wattributes,
12435 "attributes after parenthesized initializer ignored");
12437 /* For an in-class declaration, use `grokfield' to create the
12438 declaration. */
12439 if (member_p)
12441 if (pushed_scope)
12443 pop_scope (pushed_scope);
12444 pushed_scope = false;
12446 decl = grokfield (declarator, decl_specifiers,
12447 initializer, !is_non_constant_init,
12448 /*asmspec=*/NULL_TREE,
12449 prefix_attributes);
12450 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12451 cp_parser_save_default_args (parser, decl);
12454 /* Finish processing the declaration. But, skip friend
12455 declarations. */
12456 if (!friend_p && decl && decl != error_mark_node)
12458 cp_finish_decl (decl,
12459 initializer, !is_non_constant_init,
12460 asm_specification,
12461 /* If the initializer is in parentheses, then this is
12462 a direct-initialization, which means that an
12463 `explicit' constructor is OK. Otherwise, an
12464 `explicit' constructor cannot be used. */
12465 ((is_parenthesized_init || !is_initialized)
12466 ? 0 : LOOKUP_ONLYCONVERTING));
12468 else if ((cxx_dialect != cxx98) && friend_p
12469 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12470 /* Core issue #226 (C++0x only): A default template-argument
12471 shall not be specified in a friend class template
12472 declaration. */
12473 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12474 /*is_partial=*/0, /*is_friend_decl=*/1);
12476 if (!friend_p && pushed_scope)
12477 pop_scope (pushed_scope);
12479 return decl;
12482 /* Parse a declarator.
12484 declarator:
12485 direct-declarator
12486 ptr-operator declarator
12488 abstract-declarator:
12489 ptr-operator abstract-declarator [opt]
12490 direct-abstract-declarator
12492 GNU Extensions:
12494 declarator:
12495 attributes [opt] direct-declarator
12496 attributes [opt] ptr-operator declarator
12498 abstract-declarator:
12499 attributes [opt] ptr-operator abstract-declarator [opt]
12500 attributes [opt] direct-abstract-declarator
12502 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12503 detect constructor, destructor or conversion operators. It is set
12504 to -1 if the declarator is a name, and +1 if it is a
12505 function. Otherwise it is set to zero. Usually you just want to
12506 test for >0, but internally the negative value is used.
12508 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12509 a decl-specifier-seq unless it declares a constructor, destructor,
12510 or conversion. It might seem that we could check this condition in
12511 semantic analysis, rather than parsing, but that makes it difficult
12512 to handle something like `f()'. We want to notice that there are
12513 no decl-specifiers, and therefore realize that this is an
12514 expression, not a declaration.)
12516 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12517 the declarator is a direct-declarator of the form "(...)".
12519 MEMBER_P is true iff this declarator is a member-declarator. */
12521 static cp_declarator *
12522 cp_parser_declarator (cp_parser* parser,
12523 cp_parser_declarator_kind dcl_kind,
12524 int* ctor_dtor_or_conv_p,
12525 bool* parenthesized_p,
12526 bool member_p)
12528 cp_token *token;
12529 cp_declarator *declarator;
12530 enum tree_code code;
12531 cp_cv_quals cv_quals;
12532 tree class_type;
12533 tree attributes = NULL_TREE;
12535 /* Assume this is not a constructor, destructor, or type-conversion
12536 operator. */
12537 if (ctor_dtor_or_conv_p)
12538 *ctor_dtor_or_conv_p = 0;
12540 if (cp_parser_allow_gnu_extensions_p (parser))
12541 attributes = cp_parser_attributes_opt (parser);
12543 /* Peek at the next token. */
12544 token = cp_lexer_peek_token (parser->lexer);
12546 /* Check for the ptr-operator production. */
12547 cp_parser_parse_tentatively (parser);
12548 /* Parse the ptr-operator. */
12549 code = cp_parser_ptr_operator (parser,
12550 &class_type,
12551 &cv_quals);
12552 /* If that worked, then we have a ptr-operator. */
12553 if (cp_parser_parse_definitely (parser))
12555 /* If a ptr-operator was found, then this declarator was not
12556 parenthesized. */
12557 if (parenthesized_p)
12558 *parenthesized_p = true;
12559 /* The dependent declarator is optional if we are parsing an
12560 abstract-declarator. */
12561 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12562 cp_parser_parse_tentatively (parser);
12564 /* Parse the dependent declarator. */
12565 declarator = cp_parser_declarator (parser, dcl_kind,
12566 /*ctor_dtor_or_conv_p=*/NULL,
12567 /*parenthesized_p=*/NULL,
12568 /*member_p=*/false);
12570 /* If we are parsing an abstract-declarator, we must handle the
12571 case where the dependent declarator is absent. */
12572 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12573 && !cp_parser_parse_definitely (parser))
12574 declarator = NULL;
12576 declarator = cp_parser_make_indirect_declarator
12577 (code, class_type, cv_quals, declarator);
12579 /* Everything else is a direct-declarator. */
12580 else
12582 if (parenthesized_p)
12583 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12584 CPP_OPEN_PAREN);
12585 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12586 ctor_dtor_or_conv_p,
12587 member_p);
12590 if (attributes && declarator && declarator != cp_error_declarator)
12591 declarator->attributes = attributes;
12593 return declarator;
12596 /* Parse a direct-declarator or direct-abstract-declarator.
12598 direct-declarator:
12599 declarator-id
12600 direct-declarator ( parameter-declaration-clause )
12601 cv-qualifier-seq [opt]
12602 exception-specification [opt]
12603 direct-declarator [ constant-expression [opt] ]
12604 ( declarator )
12606 direct-abstract-declarator:
12607 direct-abstract-declarator [opt]
12608 ( parameter-declaration-clause )
12609 cv-qualifier-seq [opt]
12610 exception-specification [opt]
12611 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12612 ( abstract-declarator )
12614 Returns a representation of the declarator. DCL_KIND is
12615 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12616 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12617 we are parsing a direct-declarator. It is
12618 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12619 of ambiguity we prefer an abstract declarator, as per
12620 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12621 cp_parser_declarator. */
12623 static cp_declarator *
12624 cp_parser_direct_declarator (cp_parser* parser,
12625 cp_parser_declarator_kind dcl_kind,
12626 int* ctor_dtor_or_conv_p,
12627 bool member_p)
12629 cp_token *token;
12630 cp_declarator *declarator = NULL;
12631 tree scope = NULL_TREE;
12632 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12633 bool saved_in_declarator_p = parser->in_declarator_p;
12634 bool first = true;
12635 tree pushed_scope = NULL_TREE;
12637 while (true)
12639 /* Peek at the next token. */
12640 token = cp_lexer_peek_token (parser->lexer);
12641 if (token->type == CPP_OPEN_PAREN)
12643 /* This is either a parameter-declaration-clause, or a
12644 parenthesized declarator. When we know we are parsing a
12645 named declarator, it must be a parenthesized declarator
12646 if FIRST is true. For instance, `(int)' is a
12647 parameter-declaration-clause, with an omitted
12648 direct-abstract-declarator. But `((*))', is a
12649 parenthesized abstract declarator. Finally, when T is a
12650 template parameter `(T)' is a
12651 parameter-declaration-clause, and not a parenthesized
12652 named declarator.
12654 We first try and parse a parameter-declaration-clause,
12655 and then try a nested declarator (if FIRST is true).
12657 It is not an error for it not to be a
12658 parameter-declaration-clause, even when FIRST is
12659 false. Consider,
12661 int i (int);
12662 int i (3);
12664 The first is the declaration of a function while the
12665 second is a the definition of a variable, including its
12666 initializer.
12668 Having seen only the parenthesis, we cannot know which of
12669 these two alternatives should be selected. Even more
12670 complex are examples like:
12672 int i (int (a));
12673 int i (int (3));
12675 The former is a function-declaration; the latter is a
12676 variable initialization.
12678 Thus again, we try a parameter-declaration-clause, and if
12679 that fails, we back out and return. */
12681 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12683 cp_parameter_declarator *params;
12684 unsigned saved_num_template_parameter_lists;
12686 /* In a member-declarator, the only valid interpretation
12687 of a parenthesis is the start of a
12688 parameter-declaration-clause. (It is invalid to
12689 initialize a static data member with a parenthesized
12690 initializer; only the "=" form of initialization is
12691 permitted.) */
12692 if (!member_p)
12693 cp_parser_parse_tentatively (parser);
12695 /* Consume the `('. */
12696 cp_lexer_consume_token (parser->lexer);
12697 if (first)
12699 /* If this is going to be an abstract declarator, we're
12700 in a declarator and we can't have default args. */
12701 parser->default_arg_ok_p = false;
12702 parser->in_declarator_p = true;
12705 /* Inside the function parameter list, surrounding
12706 template-parameter-lists do not apply. */
12707 saved_num_template_parameter_lists
12708 = parser->num_template_parameter_lists;
12709 parser->num_template_parameter_lists = 0;
12711 /* Parse the parameter-declaration-clause. */
12712 params = cp_parser_parameter_declaration_clause (parser);
12714 parser->num_template_parameter_lists
12715 = saved_num_template_parameter_lists;
12717 /* If all went well, parse the cv-qualifier-seq and the
12718 exception-specification. */
12719 if (member_p || cp_parser_parse_definitely (parser))
12721 cp_cv_quals cv_quals;
12722 tree exception_specification;
12724 if (ctor_dtor_or_conv_p)
12725 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12726 first = false;
12727 /* Consume the `)'. */
12728 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
12730 /* Parse the cv-qualifier-seq. */
12731 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12732 /* And the exception-specification. */
12733 exception_specification
12734 = cp_parser_exception_specification_opt (parser);
12736 /* Create the function-declarator. */
12737 declarator = make_call_declarator (declarator,
12738 params,
12739 cv_quals,
12740 exception_specification);
12741 /* Any subsequent parameter lists are to do with
12742 return type, so are not those of the declared
12743 function. */
12744 parser->default_arg_ok_p = false;
12746 /* Repeat the main loop. */
12747 continue;
12751 /* If this is the first, we can try a parenthesized
12752 declarator. */
12753 if (first)
12755 bool saved_in_type_id_in_expr_p;
12757 parser->default_arg_ok_p = saved_default_arg_ok_p;
12758 parser->in_declarator_p = saved_in_declarator_p;
12760 /* Consume the `('. */
12761 cp_lexer_consume_token (parser->lexer);
12762 /* Parse the nested declarator. */
12763 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12764 parser->in_type_id_in_expr_p = true;
12765 declarator
12766 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12767 /*parenthesized_p=*/NULL,
12768 member_p);
12769 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12770 first = false;
12771 /* Expect a `)'. */
12772 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12773 declarator = cp_error_declarator;
12774 if (declarator == cp_error_declarator)
12775 break;
12777 goto handle_declarator;
12779 /* Otherwise, we must be done. */
12780 else
12781 break;
12783 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12784 && token->type == CPP_OPEN_SQUARE)
12786 /* Parse an array-declarator. */
12787 tree bounds;
12789 if (ctor_dtor_or_conv_p)
12790 *ctor_dtor_or_conv_p = 0;
12792 first = false;
12793 parser->default_arg_ok_p = false;
12794 parser->in_declarator_p = true;
12795 /* Consume the `['. */
12796 cp_lexer_consume_token (parser->lexer);
12797 /* Peek at the next token. */
12798 token = cp_lexer_peek_token (parser->lexer);
12799 /* If the next token is `]', then there is no
12800 constant-expression. */
12801 if (token->type != CPP_CLOSE_SQUARE)
12803 bool non_constant_p;
12805 bounds
12806 = cp_parser_constant_expression (parser,
12807 /*allow_non_constant=*/true,
12808 &non_constant_p);
12809 if (!non_constant_p)
12810 bounds = fold_non_dependent_expr (bounds);
12811 /* Normally, the array bound must be an integral constant
12812 expression. However, as an extension, we allow VLAs
12813 in function scopes. */
12814 else if (!parser->in_function_body)
12816 error ("array bound is not an integer constant");
12817 bounds = error_mark_node;
12820 else
12821 bounds = NULL_TREE;
12822 /* Look for the closing `]'. */
12823 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
12825 declarator = cp_error_declarator;
12826 break;
12829 declarator = make_array_declarator (declarator, bounds);
12831 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12833 tree qualifying_scope;
12834 tree unqualified_name;
12835 special_function_kind sfk;
12836 bool abstract_ok;
12837 bool pack_expansion_p = false;
12839 /* Parse a declarator-id */
12840 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12841 if (abstract_ok)
12843 cp_parser_parse_tentatively (parser);
12845 /* If we see an ellipsis, we should be looking at a
12846 parameter pack. */
12847 if (token->type == CPP_ELLIPSIS)
12849 /* Consume the `...' */
12850 cp_lexer_consume_token (parser->lexer);
12852 pack_expansion_p = true;
12856 unqualified_name
12857 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12858 qualifying_scope = parser->scope;
12859 if (abstract_ok)
12861 bool okay = false;
12863 if (!unqualified_name && pack_expansion_p)
12865 /* Check whether an error occurred. */
12866 okay = !cp_parser_error_occurred (parser);
12868 /* We already consumed the ellipsis to mark a
12869 parameter pack, but we have no way to report it,
12870 so abort the tentative parse. We will be exiting
12871 immediately anyway. */
12872 cp_parser_abort_tentative_parse (parser);
12874 else
12875 okay = cp_parser_parse_definitely (parser);
12877 if (!okay)
12878 unqualified_name = error_mark_node;
12879 else if (unqualified_name
12880 && (qualifying_scope
12881 || (TREE_CODE (unqualified_name)
12882 != IDENTIFIER_NODE)))
12884 cp_parser_error (parser, "expected unqualified-id");
12885 unqualified_name = error_mark_node;
12889 if (!unqualified_name)
12890 return NULL;
12891 if (unqualified_name == error_mark_node)
12893 declarator = cp_error_declarator;
12894 pack_expansion_p = false;
12895 declarator->parameter_pack_p = false;
12896 break;
12899 if (qualifying_scope && at_namespace_scope_p ()
12900 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12902 /* In the declaration of a member of a template class
12903 outside of the class itself, the SCOPE will sometimes
12904 be a TYPENAME_TYPE. For example, given:
12906 template <typename T>
12907 int S<T>::R::i = 3;
12909 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12910 this context, we must resolve S<T>::R to an ordinary
12911 type, rather than a typename type.
12913 The reason we normally avoid resolving TYPENAME_TYPEs
12914 is that a specialization of `S' might render
12915 `S<T>::R' not a type. However, if `S' is
12916 specialized, then this `i' will not be used, so there
12917 is no harm in resolving the types here. */
12918 tree type;
12920 /* Resolve the TYPENAME_TYPE. */
12921 type = resolve_typename_type (qualifying_scope,
12922 /*only_current_p=*/false);
12923 /* If that failed, the declarator is invalid. */
12924 if (TREE_CODE (type) == TYPENAME_TYPE)
12925 error ("%<%T::%E%> is not a type",
12926 TYPE_CONTEXT (qualifying_scope),
12927 TYPE_IDENTIFIER (qualifying_scope));
12928 qualifying_scope = type;
12931 sfk = sfk_none;
12933 if (unqualified_name)
12935 tree class_type;
12937 if (qualifying_scope
12938 && CLASS_TYPE_P (qualifying_scope))
12939 class_type = qualifying_scope;
12940 else
12941 class_type = current_class_type;
12943 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12945 tree name_type = TREE_TYPE (unqualified_name);
12946 if (class_type && same_type_p (name_type, class_type))
12948 if (qualifying_scope
12949 && CLASSTYPE_USE_TEMPLATE (name_type))
12951 error ("invalid use of constructor as a template");
12952 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12953 "name the constructor in a qualified name",
12954 class_type,
12955 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12956 class_type, name_type);
12957 declarator = cp_error_declarator;
12958 break;
12960 else
12961 unqualified_name = constructor_name (class_type);
12963 else
12965 /* We do not attempt to print the declarator
12966 here because we do not have enough
12967 information about its original syntactic
12968 form. */
12969 cp_parser_error (parser, "invalid declarator");
12970 declarator = cp_error_declarator;
12971 break;
12975 if (class_type)
12977 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12978 sfk = sfk_destructor;
12979 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12980 sfk = sfk_conversion;
12981 else if (/* There's no way to declare a constructor
12982 for an anonymous type, even if the type
12983 got a name for linkage purposes. */
12984 !TYPE_WAS_ANONYMOUS (class_type)
12985 && constructor_name_p (unqualified_name,
12986 class_type))
12988 unqualified_name = constructor_name (class_type);
12989 sfk = sfk_constructor;
12992 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12993 *ctor_dtor_or_conv_p = -1;
12996 declarator = make_id_declarator (qualifying_scope,
12997 unqualified_name,
12998 sfk);
12999 declarator->id_loc = token->location;
13000 declarator->parameter_pack_p = pack_expansion_p;
13002 if (pack_expansion_p)
13003 maybe_warn_variadic_templates ();
13005 handle_declarator:;
13006 scope = get_scope_of_declarator (declarator);
13007 if (scope)
13008 /* Any names that appear after the declarator-id for a
13009 member are looked up in the containing scope. */
13010 pushed_scope = push_scope (scope);
13011 parser->in_declarator_p = true;
13012 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13013 || (declarator && declarator->kind == cdk_id))
13014 /* Default args are only allowed on function
13015 declarations. */
13016 parser->default_arg_ok_p = saved_default_arg_ok_p;
13017 else
13018 parser->default_arg_ok_p = false;
13020 first = false;
13022 /* We're done. */
13023 else
13024 break;
13027 /* For an abstract declarator, we might wind up with nothing at this
13028 point. That's an error; the declarator is not optional. */
13029 if (!declarator)
13030 cp_parser_error (parser, "expected declarator");
13032 /* If we entered a scope, we must exit it now. */
13033 if (pushed_scope)
13034 pop_scope (pushed_scope);
13036 parser->default_arg_ok_p = saved_default_arg_ok_p;
13037 parser->in_declarator_p = saved_in_declarator_p;
13039 return declarator;
13042 /* Parse a ptr-operator.
13044 ptr-operator:
13045 * cv-qualifier-seq [opt]
13047 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13049 GNU Extension:
13051 ptr-operator:
13052 & cv-qualifier-seq [opt]
13054 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13055 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13056 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13057 filled in with the TYPE containing the member. *CV_QUALS is
13058 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13059 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13060 Note that the tree codes returned by this function have nothing
13061 to do with the types of trees that will be eventually be created
13062 to represent the pointer or reference type being parsed. They are
13063 just constants with suggestive names. */
13064 static enum tree_code
13065 cp_parser_ptr_operator (cp_parser* parser,
13066 tree* type,
13067 cp_cv_quals *cv_quals)
13069 enum tree_code code = ERROR_MARK;
13070 cp_token *token;
13072 /* Assume that it's not a pointer-to-member. */
13073 *type = NULL_TREE;
13074 /* And that there are no cv-qualifiers. */
13075 *cv_quals = TYPE_UNQUALIFIED;
13077 /* Peek at the next token. */
13078 token = cp_lexer_peek_token (parser->lexer);
13080 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13081 if (token->type == CPP_MULT)
13082 code = INDIRECT_REF;
13083 else if (token->type == CPP_AND)
13084 code = ADDR_EXPR;
13085 else if ((cxx_dialect != cxx98) &&
13086 token->type == CPP_AND_AND) /* C++0x only */
13087 code = NON_LVALUE_EXPR;
13089 if (code != ERROR_MARK)
13091 /* Consume the `*', `&' or `&&'. */
13092 cp_lexer_consume_token (parser->lexer);
13094 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13095 `&', if we are allowing GNU extensions. (The only qualifier
13096 that can legally appear after `&' is `restrict', but that is
13097 enforced during semantic analysis. */
13098 if (code == INDIRECT_REF
13099 || cp_parser_allow_gnu_extensions_p (parser))
13100 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13102 else
13104 /* Try the pointer-to-member case. */
13105 cp_parser_parse_tentatively (parser);
13106 /* Look for the optional `::' operator. */
13107 cp_parser_global_scope_opt (parser,
13108 /*current_scope_valid_p=*/false);
13109 /* Look for the nested-name specifier. */
13110 cp_parser_nested_name_specifier (parser,
13111 /*typename_keyword_p=*/false,
13112 /*check_dependency_p=*/true,
13113 /*type_p=*/false,
13114 /*is_declaration=*/false);
13115 /* If we found it, and the next token is a `*', then we are
13116 indeed looking at a pointer-to-member operator. */
13117 if (!cp_parser_error_occurred (parser)
13118 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13120 /* Indicate that the `*' operator was used. */
13121 code = INDIRECT_REF;
13123 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13124 error ("%qD is a namespace", parser->scope);
13125 else
13127 /* The type of which the member is a member is given by the
13128 current SCOPE. */
13129 *type = parser->scope;
13130 /* The next name will not be qualified. */
13131 parser->scope = NULL_TREE;
13132 parser->qualifying_scope = NULL_TREE;
13133 parser->object_scope = NULL_TREE;
13134 /* Look for the optional cv-qualifier-seq. */
13135 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13138 /* If that didn't work we don't have a ptr-operator. */
13139 if (!cp_parser_parse_definitely (parser))
13140 cp_parser_error (parser, "expected ptr-operator");
13143 return code;
13146 /* Parse an (optional) cv-qualifier-seq.
13148 cv-qualifier-seq:
13149 cv-qualifier cv-qualifier-seq [opt]
13151 cv-qualifier:
13152 const
13153 volatile
13155 GNU Extension:
13157 cv-qualifier:
13158 __restrict__
13160 Returns a bitmask representing the cv-qualifiers. */
13162 static cp_cv_quals
13163 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13165 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13167 while (true)
13169 cp_token *token;
13170 cp_cv_quals cv_qualifier;
13172 /* Peek at the next token. */
13173 token = cp_lexer_peek_token (parser->lexer);
13174 /* See if it's a cv-qualifier. */
13175 switch (token->keyword)
13177 case RID_CONST:
13178 cv_qualifier = TYPE_QUAL_CONST;
13179 break;
13181 case RID_VOLATILE:
13182 cv_qualifier = TYPE_QUAL_VOLATILE;
13183 break;
13185 case RID_RESTRICT:
13186 cv_qualifier = TYPE_QUAL_RESTRICT;
13187 break;
13189 default:
13190 cv_qualifier = TYPE_UNQUALIFIED;
13191 break;
13194 if (!cv_qualifier)
13195 break;
13197 if (cv_quals & cv_qualifier)
13199 error ("duplicate cv-qualifier");
13200 cp_lexer_purge_token (parser->lexer);
13202 else
13204 cp_lexer_consume_token (parser->lexer);
13205 cv_quals |= cv_qualifier;
13209 return cv_quals;
13212 /* Parse a declarator-id.
13214 declarator-id:
13215 id-expression
13216 :: [opt] nested-name-specifier [opt] type-name
13218 In the `id-expression' case, the value returned is as for
13219 cp_parser_id_expression if the id-expression was an unqualified-id.
13220 If the id-expression was a qualified-id, then a SCOPE_REF is
13221 returned. The first operand is the scope (either a NAMESPACE_DECL
13222 or TREE_TYPE), but the second is still just a representation of an
13223 unqualified-id. */
13225 static tree
13226 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13228 tree id;
13229 /* The expression must be an id-expression. Assume that qualified
13230 names are the names of types so that:
13232 template <class T>
13233 int S<T>::R::i = 3;
13235 will work; we must treat `S<T>::R' as the name of a type.
13236 Similarly, assume that qualified names are templates, where
13237 required, so that:
13239 template <class T>
13240 int S<T>::R<T>::i = 3;
13242 will work, too. */
13243 id = cp_parser_id_expression (parser,
13244 /*template_keyword_p=*/false,
13245 /*check_dependency_p=*/false,
13246 /*template_p=*/NULL,
13247 /*declarator_p=*/true,
13248 optional_p);
13249 if (id && BASELINK_P (id))
13250 id = BASELINK_FUNCTIONS (id);
13251 return id;
13254 /* Parse a type-id.
13256 type-id:
13257 type-specifier-seq abstract-declarator [opt]
13259 Returns the TYPE specified. */
13261 static tree
13262 cp_parser_type_id (cp_parser* parser)
13264 cp_decl_specifier_seq type_specifier_seq;
13265 cp_declarator *abstract_declarator;
13267 /* Parse the type-specifier-seq. */
13268 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13269 &type_specifier_seq);
13270 if (type_specifier_seq.type == error_mark_node)
13271 return error_mark_node;
13273 /* There might or might not be an abstract declarator. */
13274 cp_parser_parse_tentatively (parser);
13275 /* Look for the declarator. */
13276 abstract_declarator
13277 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13278 /*parenthesized_p=*/NULL,
13279 /*member_p=*/false);
13280 /* Check to see if there really was a declarator. */
13281 if (!cp_parser_parse_definitely (parser))
13282 abstract_declarator = NULL;
13284 return groktypename (&type_specifier_seq, abstract_declarator);
13287 /* Parse a type-specifier-seq.
13289 type-specifier-seq:
13290 type-specifier type-specifier-seq [opt]
13292 GNU extension:
13294 type-specifier-seq:
13295 attributes type-specifier-seq [opt]
13297 If IS_CONDITION is true, we are at the start of a "condition",
13298 e.g., we've just seen "if (".
13300 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13302 static void
13303 cp_parser_type_specifier_seq (cp_parser* parser,
13304 bool is_condition,
13305 cp_decl_specifier_seq *type_specifier_seq)
13307 bool seen_type_specifier = false;
13308 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13310 /* Clear the TYPE_SPECIFIER_SEQ. */
13311 clear_decl_specs (type_specifier_seq);
13313 /* Parse the type-specifiers and attributes. */
13314 while (true)
13316 tree type_specifier;
13317 bool is_cv_qualifier;
13319 /* Check for attributes first. */
13320 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13322 type_specifier_seq->attributes =
13323 chainon (type_specifier_seq->attributes,
13324 cp_parser_attributes_opt (parser));
13325 continue;
13328 /* Look for the type-specifier. */
13329 type_specifier = cp_parser_type_specifier (parser,
13330 flags,
13331 type_specifier_seq,
13332 /*is_declaration=*/false,
13333 NULL,
13334 &is_cv_qualifier);
13335 if (!type_specifier)
13337 /* If the first type-specifier could not be found, this is not a
13338 type-specifier-seq at all. */
13339 if (!seen_type_specifier)
13341 cp_parser_error (parser, "expected type-specifier");
13342 type_specifier_seq->type = error_mark_node;
13343 return;
13345 /* If subsequent type-specifiers could not be found, the
13346 type-specifier-seq is complete. */
13347 break;
13350 seen_type_specifier = true;
13351 /* The standard says that a condition can be:
13353 type-specifier-seq declarator = assignment-expression
13355 However, given:
13357 struct S {};
13358 if (int S = ...)
13360 we should treat the "S" as a declarator, not as a
13361 type-specifier. The standard doesn't say that explicitly for
13362 type-specifier-seq, but it does say that for
13363 decl-specifier-seq in an ordinary declaration. Perhaps it
13364 would be clearer just to allow a decl-specifier-seq here, and
13365 then add a semantic restriction that if any decl-specifiers
13366 that are not type-specifiers appear, the program is invalid. */
13367 if (is_condition && !is_cv_qualifier)
13368 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13371 cp_parser_check_decl_spec (type_specifier_seq);
13374 /* Parse a parameter-declaration-clause.
13376 parameter-declaration-clause:
13377 parameter-declaration-list [opt] ... [opt]
13378 parameter-declaration-list , ...
13380 Returns a representation for the parameter declarations. A return
13381 value of NULL indicates a parameter-declaration-clause consisting
13382 only of an ellipsis. */
13384 static cp_parameter_declarator *
13385 cp_parser_parameter_declaration_clause (cp_parser* parser)
13387 cp_parameter_declarator *parameters;
13388 cp_token *token;
13389 bool ellipsis_p;
13390 bool is_error;
13392 /* Peek at the next token. */
13393 token = cp_lexer_peek_token (parser->lexer);
13394 /* Check for trivial parameter-declaration-clauses. */
13395 if (token->type == CPP_ELLIPSIS)
13397 /* Consume the `...' token. */
13398 cp_lexer_consume_token (parser->lexer);
13399 return NULL;
13401 else if (token->type == CPP_CLOSE_PAREN)
13402 /* There are no parameters. */
13404 #ifndef NO_IMPLICIT_EXTERN_C
13405 if (in_system_header && current_class_type == NULL
13406 && current_lang_name == lang_name_c)
13407 return NULL;
13408 else
13409 #endif
13410 return no_parameters;
13412 /* Check for `(void)', too, which is a special case. */
13413 else if (token->keyword == RID_VOID
13414 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13415 == CPP_CLOSE_PAREN))
13417 /* Consume the `void' token. */
13418 cp_lexer_consume_token (parser->lexer);
13419 /* There are no parameters. */
13420 return no_parameters;
13423 /* Parse the parameter-declaration-list. */
13424 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13425 /* If a parse error occurred while parsing the
13426 parameter-declaration-list, then the entire
13427 parameter-declaration-clause is erroneous. */
13428 if (is_error)
13429 return NULL;
13431 /* Peek at the next token. */
13432 token = cp_lexer_peek_token (parser->lexer);
13433 /* If it's a `,', the clause should terminate with an ellipsis. */
13434 if (token->type == CPP_COMMA)
13436 /* Consume the `,'. */
13437 cp_lexer_consume_token (parser->lexer);
13438 /* Expect an ellipsis. */
13439 ellipsis_p
13440 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13442 /* It might also be `...' if the optional trailing `,' was
13443 omitted. */
13444 else if (token->type == CPP_ELLIPSIS)
13446 /* Consume the `...' token. */
13447 cp_lexer_consume_token (parser->lexer);
13448 /* And remember that we saw it. */
13449 ellipsis_p = true;
13451 else
13452 ellipsis_p = false;
13454 /* Finish the parameter list. */
13455 if (parameters && ellipsis_p)
13456 parameters->ellipsis_p = true;
13458 return parameters;
13461 /* Parse a parameter-declaration-list.
13463 parameter-declaration-list:
13464 parameter-declaration
13465 parameter-declaration-list , parameter-declaration
13467 Returns a representation of the parameter-declaration-list, as for
13468 cp_parser_parameter_declaration_clause. However, the
13469 `void_list_node' is never appended to the list. Upon return,
13470 *IS_ERROR will be true iff an error occurred. */
13472 static cp_parameter_declarator *
13473 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13475 cp_parameter_declarator *parameters = NULL;
13476 cp_parameter_declarator **tail = &parameters;
13477 bool saved_in_unbraced_linkage_specification_p;
13479 /* Assume all will go well. */
13480 *is_error = false;
13481 /* The special considerations that apply to a function within an
13482 unbraced linkage specifications do not apply to the parameters
13483 to the function. */
13484 saved_in_unbraced_linkage_specification_p
13485 = parser->in_unbraced_linkage_specification_p;
13486 parser->in_unbraced_linkage_specification_p = false;
13488 /* Look for more parameters. */
13489 while (true)
13491 cp_parameter_declarator *parameter;
13492 bool parenthesized_p;
13493 /* Parse the parameter. */
13494 parameter
13495 = cp_parser_parameter_declaration (parser,
13496 /*template_parm_p=*/false,
13497 &parenthesized_p);
13499 /* If a parse error occurred parsing the parameter declaration,
13500 then the entire parameter-declaration-list is erroneous. */
13501 if (!parameter)
13503 *is_error = true;
13504 parameters = NULL;
13505 break;
13507 /* Add the new parameter to the list. */
13508 *tail = parameter;
13509 tail = &parameter->next;
13511 /* Peek at the next token. */
13512 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13513 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13514 /* These are for Objective-C++ */
13515 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13516 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13517 /* The parameter-declaration-list is complete. */
13518 break;
13519 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13521 cp_token *token;
13523 /* Peek at the next token. */
13524 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13525 /* If it's an ellipsis, then the list is complete. */
13526 if (token->type == CPP_ELLIPSIS)
13527 break;
13528 /* Otherwise, there must be more parameters. Consume the
13529 `,'. */
13530 cp_lexer_consume_token (parser->lexer);
13531 /* When parsing something like:
13533 int i(float f, double d)
13535 we can tell after seeing the declaration for "f" that we
13536 are not looking at an initialization of a variable "i",
13537 but rather at the declaration of a function "i".
13539 Due to the fact that the parsing of template arguments
13540 (as specified to a template-id) requires backtracking we
13541 cannot use this technique when inside a template argument
13542 list. */
13543 if (!parser->in_template_argument_list_p
13544 && !parser->in_type_id_in_expr_p
13545 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13546 /* However, a parameter-declaration of the form
13547 "foat(f)" (which is a valid declaration of a
13548 parameter "f") can also be interpreted as an
13549 expression (the conversion of "f" to "float"). */
13550 && !parenthesized_p)
13551 cp_parser_commit_to_tentative_parse (parser);
13553 else
13555 cp_parser_error (parser, "expected %<,%> or %<...%>");
13556 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13557 cp_parser_skip_to_closing_parenthesis (parser,
13558 /*recovering=*/true,
13559 /*or_comma=*/false,
13560 /*consume_paren=*/false);
13561 break;
13565 parser->in_unbraced_linkage_specification_p
13566 = saved_in_unbraced_linkage_specification_p;
13568 return parameters;
13571 /* Parse a parameter declaration.
13573 parameter-declaration:
13574 decl-specifier-seq ... [opt] declarator
13575 decl-specifier-seq declarator = assignment-expression
13576 decl-specifier-seq ... [opt] abstract-declarator [opt]
13577 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13579 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13580 declares a template parameter. (In that case, a non-nested `>'
13581 token encountered during the parsing of the assignment-expression
13582 is not interpreted as a greater-than operator.)
13584 Returns a representation of the parameter, or NULL if an error
13585 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13586 true iff the declarator is of the form "(p)". */
13588 static cp_parameter_declarator *
13589 cp_parser_parameter_declaration (cp_parser *parser,
13590 bool template_parm_p,
13591 bool *parenthesized_p)
13593 int declares_class_or_enum;
13594 bool greater_than_is_operator_p;
13595 cp_decl_specifier_seq decl_specifiers;
13596 cp_declarator *declarator;
13597 tree default_argument;
13598 cp_token *token;
13599 const char *saved_message;
13601 /* In a template parameter, `>' is not an operator.
13603 [temp.param]
13605 When parsing a default template-argument for a non-type
13606 template-parameter, the first non-nested `>' is taken as the end
13607 of the template parameter-list rather than a greater-than
13608 operator. */
13609 greater_than_is_operator_p = !template_parm_p;
13611 /* Type definitions may not appear in parameter types. */
13612 saved_message = parser->type_definition_forbidden_message;
13613 parser->type_definition_forbidden_message
13614 = "types may not be defined in parameter types";
13616 /* Parse the declaration-specifiers. */
13617 cp_parser_decl_specifier_seq (parser,
13618 CP_PARSER_FLAGS_NONE,
13619 &decl_specifiers,
13620 &declares_class_or_enum);
13621 /* If an error occurred, there's no reason to attempt to parse the
13622 rest of the declaration. */
13623 if (cp_parser_error_occurred (parser))
13625 parser->type_definition_forbidden_message = saved_message;
13626 return NULL;
13629 /* Peek at the next token. */
13630 token = cp_lexer_peek_token (parser->lexer);
13632 /* If the next token is a `)', `,', `=', `>', or `...', then there
13633 is no declarator. However, when variadic templates are enabled,
13634 there may be a declarator following `...'. */
13635 if (token->type == CPP_CLOSE_PAREN
13636 || token->type == CPP_COMMA
13637 || token->type == CPP_EQ
13638 || token->type == CPP_GREATER)
13640 declarator = NULL;
13641 if (parenthesized_p)
13642 *parenthesized_p = false;
13644 /* Otherwise, there should be a declarator. */
13645 else
13647 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13648 parser->default_arg_ok_p = false;
13650 /* After seeing a decl-specifier-seq, if the next token is not a
13651 "(", there is no possibility that the code is a valid
13652 expression. Therefore, if parsing tentatively, we commit at
13653 this point. */
13654 if (!parser->in_template_argument_list_p
13655 /* In an expression context, having seen:
13657 (int((char ...
13659 we cannot be sure whether we are looking at a
13660 function-type (taking a "char" as a parameter) or a cast
13661 of some object of type "char" to "int". */
13662 && !parser->in_type_id_in_expr_p
13663 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13664 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13665 cp_parser_commit_to_tentative_parse (parser);
13666 /* Parse the declarator. */
13667 declarator = cp_parser_declarator (parser,
13668 CP_PARSER_DECLARATOR_EITHER,
13669 /*ctor_dtor_or_conv_p=*/NULL,
13670 parenthesized_p,
13671 /*member_p=*/false);
13672 parser->default_arg_ok_p = saved_default_arg_ok_p;
13673 /* After the declarator, allow more attributes. */
13674 decl_specifiers.attributes
13675 = chainon (decl_specifiers.attributes,
13676 cp_parser_attributes_opt (parser));
13679 /* If the next token is an ellipsis, and we have not seen a
13680 declarator name, and the type of the declarator contains parameter
13681 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13682 a parameter pack expansion expression. Otherwise, leave the
13683 ellipsis for a C-style variadic function. */
13684 token = cp_lexer_peek_token (parser->lexer);
13685 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13687 tree type = decl_specifiers.type;
13689 if (type && DECL_P (type))
13690 type = TREE_TYPE (type);
13692 if (type
13693 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13694 && declarator_can_be_parameter_pack (declarator)
13695 && (!declarator || !declarator->parameter_pack_p)
13696 && uses_parameter_packs (type))
13698 /* Consume the `...'. */
13699 cp_lexer_consume_token (parser->lexer);
13700 maybe_warn_variadic_templates ();
13702 /* Build a pack expansion type */
13703 if (declarator)
13704 declarator->parameter_pack_p = true;
13705 else
13706 decl_specifiers.type = make_pack_expansion (type);
13710 /* The restriction on defining new types applies only to the type
13711 of the parameter, not to the default argument. */
13712 parser->type_definition_forbidden_message = saved_message;
13714 /* If the next token is `=', then process a default argument. */
13715 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13717 /* Consume the `='. */
13718 cp_lexer_consume_token (parser->lexer);
13720 /* If we are defining a class, then the tokens that make up the
13721 default argument must be saved and processed later. */
13722 if (!template_parm_p && at_class_scope_p ()
13723 && TYPE_BEING_DEFINED (current_class_type))
13725 unsigned depth = 0;
13726 cp_token *first_token;
13727 cp_token *token;
13729 /* Add tokens until we have processed the entire default
13730 argument. We add the range [first_token, token). */
13731 first_token = cp_lexer_peek_token (parser->lexer);
13732 while (true)
13734 bool done = false;
13736 /* Peek at the next token. */
13737 token = cp_lexer_peek_token (parser->lexer);
13738 /* What we do depends on what token we have. */
13739 switch (token->type)
13741 /* In valid code, a default argument must be
13742 immediately followed by a `,' `)', or `...'. */
13743 case CPP_COMMA:
13744 case CPP_CLOSE_PAREN:
13745 case CPP_ELLIPSIS:
13746 /* If we run into a non-nested `;', `}', or `]',
13747 then the code is invalid -- but the default
13748 argument is certainly over. */
13749 case CPP_SEMICOLON:
13750 case CPP_CLOSE_BRACE:
13751 case CPP_CLOSE_SQUARE:
13752 if (depth == 0)
13753 done = true;
13754 /* Update DEPTH, if necessary. */
13755 else if (token->type == CPP_CLOSE_PAREN
13756 || token->type == CPP_CLOSE_BRACE
13757 || token->type == CPP_CLOSE_SQUARE)
13758 --depth;
13759 break;
13761 case CPP_OPEN_PAREN:
13762 case CPP_OPEN_SQUARE:
13763 case CPP_OPEN_BRACE:
13764 ++depth;
13765 break;
13767 case CPP_RSHIFT:
13768 if (cxx_dialect == cxx98)
13769 break;
13770 /* Fall through for C++0x, which treats the `>>'
13771 operator like two `>' tokens in certain
13772 cases. */
13774 case CPP_GREATER:
13775 /* If we see a non-nested `>', and `>' is not an
13776 operator, then it marks the end of the default
13777 argument. */
13778 if (!depth && !greater_than_is_operator_p)
13779 done = true;
13780 break;
13782 /* If we run out of tokens, issue an error message. */
13783 case CPP_EOF:
13784 case CPP_PRAGMA_EOL:
13785 error ("file ends in default argument");
13786 done = true;
13787 break;
13789 case CPP_NAME:
13790 case CPP_SCOPE:
13791 /* In these cases, we should look for template-ids.
13792 For example, if the default argument is
13793 `X<int, double>()', we need to do name lookup to
13794 figure out whether or not `X' is a template; if
13795 so, the `,' does not end the default argument.
13797 That is not yet done. */
13798 break;
13800 default:
13801 break;
13804 /* If we've reached the end, stop. */
13805 if (done)
13806 break;
13808 /* Add the token to the token block. */
13809 token = cp_lexer_consume_token (parser->lexer);
13812 /* Create a DEFAULT_ARG to represent the unparsed default
13813 argument. */
13814 default_argument = make_node (DEFAULT_ARG);
13815 DEFARG_TOKENS (default_argument)
13816 = cp_token_cache_new (first_token, token);
13817 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13819 /* Outside of a class definition, we can just parse the
13820 assignment-expression. */
13821 else
13822 default_argument
13823 = cp_parser_default_argument (parser, template_parm_p);
13825 if (!parser->default_arg_ok_p)
13827 if (!flag_pedantic_errors)
13828 warning (0, "deprecated use of default argument for parameter of non-function");
13829 else
13831 error ("default arguments are only permitted for function parameters");
13832 default_argument = NULL_TREE;
13835 else if ((declarator && declarator->parameter_pack_p)
13836 || (decl_specifiers.type
13837 && PACK_EXPANSION_P (decl_specifiers.type)))
13839 const char* kind = template_parm_p? "template " : "";
13841 /* Find the name of the parameter pack. */
13842 cp_declarator *id_declarator = declarator;
13843 while (id_declarator && id_declarator->kind != cdk_id)
13844 id_declarator = id_declarator->declarator;
13846 if (id_declarator && id_declarator->kind == cdk_id)
13847 error ("%sparameter pack %qD cannot have a default argument",
13848 kind, id_declarator->u.id.unqualified_name);
13849 else
13850 error ("%sparameter pack cannot have a default argument",
13851 kind);
13853 default_argument = NULL_TREE;
13856 else
13857 default_argument = NULL_TREE;
13859 return make_parameter_declarator (&decl_specifiers,
13860 declarator,
13861 default_argument);
13864 /* Parse a default argument and return it.
13866 TEMPLATE_PARM_P is true if this is a default argument for a
13867 non-type template parameter. */
13868 static tree
13869 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
13871 tree default_argument = NULL_TREE;
13872 bool saved_greater_than_is_operator_p;
13873 bool saved_local_variables_forbidden_p;
13875 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13876 set correctly. */
13877 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
13878 parser->greater_than_is_operator_p = !template_parm_p;
13879 /* Local variable names (and the `this' keyword) may not
13880 appear in a default argument. */
13881 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13882 parser->local_variables_forbidden_p = true;
13883 /* The default argument expression may cause implicitly
13884 defined member functions to be synthesized, which will
13885 result in garbage collection. We must treat this
13886 situation as if we were within the body of function so as
13887 to avoid collecting live data on the stack. */
13888 ++function_depth;
13889 /* Parse the assignment-expression. */
13890 if (template_parm_p)
13891 push_deferring_access_checks (dk_no_deferred);
13892 default_argument
13893 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13894 if (template_parm_p)
13895 pop_deferring_access_checks ();
13896 /* Restore saved state. */
13897 --function_depth;
13898 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
13899 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13901 return default_argument;
13904 /* Parse a function-body.
13906 function-body:
13907 compound_statement */
13909 static void
13910 cp_parser_function_body (cp_parser *parser)
13912 cp_parser_compound_statement (parser, NULL, false);
13915 /* Parse a ctor-initializer-opt followed by a function-body. Return
13916 true if a ctor-initializer was present. */
13918 static bool
13919 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13921 tree body;
13922 bool ctor_initializer_p;
13924 /* Begin the function body. */
13925 body = begin_function_body ();
13926 /* Parse the optional ctor-initializer. */
13927 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13928 /* Parse the function-body. */
13929 cp_parser_function_body (parser);
13930 /* Finish the function body. */
13931 finish_function_body (body);
13933 return ctor_initializer_p;
13936 /* Parse an initializer.
13938 initializer:
13939 = initializer-clause
13940 ( expression-list )
13942 Returns an expression representing the initializer. If no
13943 initializer is present, NULL_TREE is returned.
13945 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13946 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13947 set to FALSE if there is no initializer present. If there is an
13948 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13949 is set to true; otherwise it is set to false. */
13951 static tree
13952 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13953 bool* non_constant_p)
13955 cp_token *token;
13956 tree init;
13958 /* Peek at the next token. */
13959 token = cp_lexer_peek_token (parser->lexer);
13961 /* Let our caller know whether or not this initializer was
13962 parenthesized. */
13963 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13964 /* Assume that the initializer is constant. */
13965 *non_constant_p = false;
13967 if (token->type == CPP_EQ)
13969 /* Consume the `='. */
13970 cp_lexer_consume_token (parser->lexer);
13971 /* Parse the initializer-clause. */
13972 init = cp_parser_initializer_clause (parser, non_constant_p);
13974 else if (token->type == CPP_OPEN_PAREN)
13975 init = cp_parser_parenthesized_expression_list (parser, false,
13976 /*cast_p=*/false,
13977 /*allow_expansion_p=*/true,
13978 non_constant_p);
13979 else
13981 /* Anything else is an error. */
13982 cp_parser_error (parser, "expected initializer");
13983 init = error_mark_node;
13986 return init;
13989 /* Parse an initializer-clause.
13991 initializer-clause:
13992 assignment-expression
13993 { initializer-list , [opt] }
13996 Returns an expression representing the initializer.
13998 If the `assignment-expression' production is used the value
13999 returned is simply a representation for the expression.
14001 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
14002 the elements of the initializer-list (or NULL, if the last
14003 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14004 NULL_TREE. There is no way to detect whether or not the optional
14005 trailing `,' was provided. NON_CONSTANT_P is as for
14006 cp_parser_initializer. */
14008 static tree
14009 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14011 tree initializer;
14013 /* Assume the expression is constant. */
14014 *non_constant_p = false;
14016 /* If it is not a `{', then we are looking at an
14017 assignment-expression. */
14018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14020 initializer
14021 = cp_parser_constant_expression (parser,
14022 /*allow_non_constant_p=*/true,
14023 non_constant_p);
14024 if (!*non_constant_p)
14025 initializer = fold_non_dependent_expr (initializer);
14027 else
14029 /* Consume the `{' token. */
14030 cp_lexer_consume_token (parser->lexer);
14031 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14032 initializer = make_node (CONSTRUCTOR);
14033 /* If it's not a `}', then there is a non-trivial initializer. */
14034 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14036 /* Parse the initializer list. */
14037 CONSTRUCTOR_ELTS (initializer)
14038 = cp_parser_initializer_list (parser, non_constant_p);
14039 /* A trailing `,' token is allowed. */
14040 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14041 cp_lexer_consume_token (parser->lexer);
14043 /* Now, there should be a trailing `}'. */
14044 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14047 return initializer;
14050 /* Parse an initializer-list.
14052 initializer-list:
14053 initializer-clause ... [opt]
14054 initializer-list , initializer-clause ... [opt]
14056 GNU Extension:
14058 initializer-list:
14059 identifier : initializer-clause
14060 initializer-list, identifier : initializer-clause
14062 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14063 for the initializer. If the INDEX of the elt is non-NULL, it is the
14064 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14065 as for cp_parser_initializer. */
14067 static VEC(constructor_elt,gc) *
14068 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14070 VEC(constructor_elt,gc) *v = NULL;
14072 /* Assume all of the expressions are constant. */
14073 *non_constant_p = false;
14075 /* Parse the rest of the list. */
14076 while (true)
14078 cp_token *token;
14079 tree identifier;
14080 tree initializer;
14081 bool clause_non_constant_p;
14083 /* If the next token is an identifier and the following one is a
14084 colon, we are looking at the GNU designated-initializer
14085 syntax. */
14086 if (cp_parser_allow_gnu_extensions_p (parser)
14087 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14088 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14090 /* Warn the user that they are using an extension. */
14091 if (pedantic)
14092 pedwarn ("ISO C++ does not allow designated initializers");
14093 /* Consume the identifier. */
14094 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14095 /* Consume the `:'. */
14096 cp_lexer_consume_token (parser->lexer);
14098 else
14099 identifier = NULL_TREE;
14101 /* Parse the initializer. */
14102 initializer = cp_parser_initializer_clause (parser,
14103 &clause_non_constant_p);
14104 /* If any clause is non-constant, so is the entire initializer. */
14105 if (clause_non_constant_p)
14106 *non_constant_p = true;
14108 /* If we have an ellipsis, this is an initializer pack
14109 expansion. */
14110 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14112 /* Consume the `...'. */
14113 cp_lexer_consume_token (parser->lexer);
14115 /* Turn the initializer into an initializer expansion. */
14116 initializer = make_pack_expansion (initializer);
14119 /* Add it to the vector. */
14120 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14122 /* If the next token is not a comma, we have reached the end of
14123 the list. */
14124 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14125 break;
14127 /* Peek at the next token. */
14128 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14129 /* If the next token is a `}', then we're still done. An
14130 initializer-clause can have a trailing `,' after the
14131 initializer-list and before the closing `}'. */
14132 if (token->type == CPP_CLOSE_BRACE)
14133 break;
14135 /* Consume the `,' token. */
14136 cp_lexer_consume_token (parser->lexer);
14139 return v;
14142 /* Classes [gram.class] */
14144 /* Parse a class-name.
14146 class-name:
14147 identifier
14148 template-id
14150 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14151 to indicate that names looked up in dependent types should be
14152 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14153 keyword has been used to indicate that the name that appears next
14154 is a template. TAG_TYPE indicates the explicit tag given before
14155 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14156 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14157 is the class being defined in a class-head.
14159 Returns the TYPE_DECL representing the class. */
14161 static tree
14162 cp_parser_class_name (cp_parser *parser,
14163 bool typename_keyword_p,
14164 bool template_keyword_p,
14165 enum tag_types tag_type,
14166 bool check_dependency_p,
14167 bool class_head_p,
14168 bool is_declaration)
14170 tree decl;
14171 tree scope;
14172 bool typename_p;
14173 cp_token *token;
14175 /* All class-names start with an identifier. */
14176 token = cp_lexer_peek_token (parser->lexer);
14177 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14179 cp_parser_error (parser, "expected class-name");
14180 return error_mark_node;
14183 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14184 to a template-id, so we save it here. */
14185 scope = parser->scope;
14186 if (scope == error_mark_node)
14187 return error_mark_node;
14189 /* Any name names a type if we're following the `typename' keyword
14190 in a qualified name where the enclosing scope is type-dependent. */
14191 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14192 && dependent_type_p (scope));
14193 /* Handle the common case (an identifier, but not a template-id)
14194 efficiently. */
14195 if (token->type == CPP_NAME
14196 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14198 cp_token *identifier_token;
14199 tree identifier;
14200 bool ambiguous_p;
14202 /* Look for the identifier. */
14203 identifier_token = cp_lexer_peek_token (parser->lexer);
14204 ambiguous_p = identifier_token->ambiguous_p;
14205 identifier = cp_parser_identifier (parser);
14206 /* If the next token isn't an identifier, we are certainly not
14207 looking at a class-name. */
14208 if (identifier == error_mark_node)
14209 decl = error_mark_node;
14210 /* If we know this is a type-name, there's no need to look it
14211 up. */
14212 else if (typename_p)
14213 decl = identifier;
14214 else
14216 tree ambiguous_decls;
14217 /* If we already know that this lookup is ambiguous, then
14218 we've already issued an error message; there's no reason
14219 to check again. */
14220 if (ambiguous_p)
14222 cp_parser_simulate_error (parser);
14223 return error_mark_node;
14225 /* If the next token is a `::', then the name must be a type
14226 name.
14228 [basic.lookup.qual]
14230 During the lookup for a name preceding the :: scope
14231 resolution operator, object, function, and enumerator
14232 names are ignored. */
14233 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14234 tag_type = typename_type;
14235 /* Look up the name. */
14236 decl = cp_parser_lookup_name (parser, identifier,
14237 tag_type,
14238 /*is_template=*/false,
14239 /*is_namespace=*/false,
14240 check_dependency_p,
14241 &ambiguous_decls);
14242 if (ambiguous_decls)
14244 error ("reference to %qD is ambiguous", identifier);
14245 print_candidates (ambiguous_decls);
14246 if (cp_parser_parsing_tentatively (parser))
14248 identifier_token->ambiguous_p = true;
14249 cp_parser_simulate_error (parser);
14251 return error_mark_node;
14255 else
14257 /* Try a template-id. */
14258 decl = cp_parser_template_id (parser, template_keyword_p,
14259 check_dependency_p,
14260 is_declaration);
14261 if (decl == error_mark_node)
14262 return error_mark_node;
14265 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14267 /* If this is a typename, create a TYPENAME_TYPE. */
14268 if (typename_p && decl != error_mark_node)
14270 decl = make_typename_type (scope, decl, typename_type,
14271 /*complain=*/tf_error);
14272 if (decl != error_mark_node)
14273 decl = TYPE_NAME (decl);
14276 /* Check to see that it is really the name of a class. */
14277 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14278 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14279 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14280 /* Situations like this:
14282 template <typename T> struct A {
14283 typename T::template X<int>::I i;
14286 are problematic. Is `T::template X<int>' a class-name? The
14287 standard does not seem to be definitive, but there is no other
14288 valid interpretation of the following `::'. Therefore, those
14289 names are considered class-names. */
14291 decl = make_typename_type (scope, decl, tag_type, tf_error);
14292 if (decl != error_mark_node)
14293 decl = TYPE_NAME (decl);
14295 else if (TREE_CODE (decl) != TYPE_DECL
14296 || TREE_TYPE (decl) == error_mark_node
14297 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14298 decl = error_mark_node;
14300 if (decl == error_mark_node)
14301 cp_parser_error (parser, "expected class-name");
14303 return decl;
14306 /* Parse a class-specifier.
14308 class-specifier:
14309 class-head { member-specification [opt] }
14311 Returns the TREE_TYPE representing the class. */
14313 static tree
14314 cp_parser_class_specifier (cp_parser* parser)
14316 cp_token *token;
14317 tree type;
14318 tree attributes = NULL_TREE;
14319 int has_trailing_semicolon;
14320 bool nested_name_specifier_p;
14321 unsigned saved_num_template_parameter_lists;
14322 bool saved_in_function_body;
14323 tree old_scope = NULL_TREE;
14324 tree scope = NULL_TREE;
14325 tree bases;
14327 push_deferring_access_checks (dk_no_deferred);
14329 /* Parse the class-head. */
14330 type = cp_parser_class_head (parser,
14331 &nested_name_specifier_p,
14332 &attributes,
14333 &bases);
14334 /* If the class-head was a semantic disaster, skip the entire body
14335 of the class. */
14336 if (!type)
14338 cp_parser_skip_to_end_of_block_or_statement (parser);
14339 pop_deferring_access_checks ();
14340 return error_mark_node;
14343 /* Look for the `{'. */
14344 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14346 pop_deferring_access_checks ();
14347 return error_mark_node;
14350 /* Process the base classes. If they're invalid, skip the
14351 entire class body. */
14352 if (!xref_basetypes (type, bases))
14354 /* Consuming the closing brace yields better error messages
14355 later on. */
14356 if (cp_parser_skip_to_closing_brace (parser))
14357 cp_lexer_consume_token (parser->lexer);
14358 pop_deferring_access_checks ();
14359 return error_mark_node;
14362 /* Issue an error message if type-definitions are forbidden here. */
14363 cp_parser_check_type_definition (parser);
14364 /* Remember that we are defining one more class. */
14365 ++parser->num_classes_being_defined;
14366 /* Inside the class, surrounding template-parameter-lists do not
14367 apply. */
14368 saved_num_template_parameter_lists
14369 = parser->num_template_parameter_lists;
14370 parser->num_template_parameter_lists = 0;
14371 /* We are not in a function body. */
14372 saved_in_function_body = parser->in_function_body;
14373 parser->in_function_body = false;
14375 /* Start the class. */
14376 if (nested_name_specifier_p)
14378 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14379 old_scope = push_inner_scope (scope);
14381 type = begin_class_definition (type, attributes);
14383 if (type == error_mark_node)
14384 /* If the type is erroneous, skip the entire body of the class. */
14385 cp_parser_skip_to_closing_brace (parser);
14386 else
14387 /* Parse the member-specification. */
14388 cp_parser_member_specification_opt (parser);
14390 /* Look for the trailing `}'. */
14391 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14392 /* We get better error messages by noticing a common problem: a
14393 missing trailing `;'. */
14394 token = cp_lexer_peek_token (parser->lexer);
14395 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14396 /* Look for trailing attributes to apply to this class. */
14397 if (cp_parser_allow_gnu_extensions_p (parser))
14398 attributes = cp_parser_attributes_opt (parser);
14399 if (type != error_mark_node)
14400 type = finish_struct (type, attributes);
14401 if (nested_name_specifier_p)
14402 pop_inner_scope (old_scope, scope);
14403 /* If this class is not itself within the scope of another class,
14404 then we need to parse the bodies of all of the queued function
14405 definitions. Note that the queued functions defined in a class
14406 are not always processed immediately following the
14407 class-specifier for that class. Consider:
14409 struct A {
14410 struct B { void f() { sizeof (A); } };
14413 If `f' were processed before the processing of `A' were
14414 completed, there would be no way to compute the size of `A'.
14415 Note that the nesting we are interested in here is lexical --
14416 not the semantic nesting given by TYPE_CONTEXT. In particular,
14417 for:
14419 struct A { struct B; };
14420 struct A::B { void f() { } };
14422 there is no need to delay the parsing of `A::B::f'. */
14423 if (--parser->num_classes_being_defined == 0)
14425 tree queue_entry;
14426 tree fn;
14427 tree class_type = NULL_TREE;
14428 tree pushed_scope = NULL_TREE;
14430 /* In a first pass, parse default arguments to the functions.
14431 Then, in a second pass, parse the bodies of the functions.
14432 This two-phased approach handles cases like:
14434 struct S {
14435 void f() { g(); }
14436 void g(int i = 3);
14440 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14441 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14442 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14443 TREE_PURPOSE (parser->unparsed_functions_queues)
14444 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14446 fn = TREE_VALUE (queue_entry);
14447 /* If there are default arguments that have not yet been processed,
14448 take care of them now. */
14449 if (class_type != TREE_PURPOSE (queue_entry))
14451 if (pushed_scope)
14452 pop_scope (pushed_scope);
14453 class_type = TREE_PURPOSE (queue_entry);
14454 pushed_scope = push_scope (class_type);
14456 /* Make sure that any template parameters are in scope. */
14457 maybe_begin_member_template_processing (fn);
14458 /* Parse the default argument expressions. */
14459 cp_parser_late_parsing_default_args (parser, fn);
14460 /* Remove any template parameters from the symbol table. */
14461 maybe_end_member_template_processing ();
14463 if (pushed_scope)
14464 pop_scope (pushed_scope);
14465 /* Now parse the body of the functions. */
14466 for (TREE_VALUE (parser->unparsed_functions_queues)
14467 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14468 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14469 TREE_VALUE (parser->unparsed_functions_queues)
14470 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14472 /* Figure out which function we need to process. */
14473 fn = TREE_VALUE (queue_entry);
14474 /* Parse the function. */
14475 cp_parser_late_parsing_for_member (parser, fn);
14479 /* Put back any saved access checks. */
14480 pop_deferring_access_checks ();
14482 /* Restore saved state. */
14483 parser->in_function_body = saved_in_function_body;
14484 parser->num_template_parameter_lists
14485 = saved_num_template_parameter_lists;
14487 return type;
14490 /* Parse a class-head.
14492 class-head:
14493 class-key identifier [opt] base-clause [opt]
14494 class-key nested-name-specifier identifier base-clause [opt]
14495 class-key nested-name-specifier [opt] template-id
14496 base-clause [opt]
14498 GNU Extensions:
14499 class-key attributes identifier [opt] base-clause [opt]
14500 class-key attributes nested-name-specifier identifier base-clause [opt]
14501 class-key attributes nested-name-specifier [opt] template-id
14502 base-clause [opt]
14504 Upon return BASES is initialized to the list of base classes (or
14505 NULL, if there are none) in the same form returned by
14506 cp_parser_base_clause.
14508 Returns the TYPE of the indicated class. Sets
14509 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14510 involving a nested-name-specifier was used, and FALSE otherwise.
14512 Returns error_mark_node if this is not a class-head.
14514 Returns NULL_TREE if the class-head is syntactically valid, but
14515 semantically invalid in a way that means we should skip the entire
14516 body of the class. */
14518 static tree
14519 cp_parser_class_head (cp_parser* parser,
14520 bool* nested_name_specifier_p,
14521 tree *attributes_p,
14522 tree *bases)
14524 tree nested_name_specifier;
14525 enum tag_types class_key;
14526 tree id = NULL_TREE;
14527 tree type = NULL_TREE;
14528 tree attributes;
14529 bool template_id_p = false;
14530 bool qualified_p = false;
14531 bool invalid_nested_name_p = false;
14532 bool invalid_explicit_specialization_p = false;
14533 tree pushed_scope = NULL_TREE;
14534 unsigned num_templates;
14536 /* Assume no nested-name-specifier will be present. */
14537 *nested_name_specifier_p = false;
14538 /* Assume no template parameter lists will be used in defining the
14539 type. */
14540 num_templates = 0;
14542 *bases = NULL_TREE;
14544 /* Look for the class-key. */
14545 class_key = cp_parser_class_key (parser);
14546 if (class_key == none_type)
14547 return error_mark_node;
14549 /* Parse the attributes. */
14550 attributes = cp_parser_attributes_opt (parser);
14552 /* If the next token is `::', that is invalid -- but sometimes
14553 people do try to write:
14555 struct ::S {};
14557 Handle this gracefully by accepting the extra qualifier, and then
14558 issuing an error about it later if this really is a
14559 class-head. If it turns out just to be an elaborated type
14560 specifier, remain silent. */
14561 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14562 qualified_p = true;
14564 push_deferring_access_checks (dk_no_check);
14566 /* Determine the name of the class. Begin by looking for an
14567 optional nested-name-specifier. */
14568 nested_name_specifier
14569 = cp_parser_nested_name_specifier_opt (parser,
14570 /*typename_keyword_p=*/false,
14571 /*check_dependency_p=*/false,
14572 /*type_p=*/false,
14573 /*is_declaration=*/false);
14574 /* If there was a nested-name-specifier, then there *must* be an
14575 identifier. */
14576 if (nested_name_specifier)
14578 /* Although the grammar says `identifier', it really means
14579 `class-name' or `template-name'. You are only allowed to
14580 define a class that has already been declared with this
14581 syntax.
14583 The proposed resolution for Core Issue 180 says that wherever
14584 you see `class T::X' you should treat `X' as a type-name.
14586 It is OK to define an inaccessible class; for example:
14588 class A { class B; };
14589 class A::B {};
14591 We do not know if we will see a class-name, or a
14592 template-name. We look for a class-name first, in case the
14593 class-name is a template-id; if we looked for the
14594 template-name first we would stop after the template-name. */
14595 cp_parser_parse_tentatively (parser);
14596 type = cp_parser_class_name (parser,
14597 /*typename_keyword_p=*/false,
14598 /*template_keyword_p=*/false,
14599 class_type,
14600 /*check_dependency_p=*/false,
14601 /*class_head_p=*/true,
14602 /*is_declaration=*/false);
14603 /* If that didn't work, ignore the nested-name-specifier. */
14604 if (!cp_parser_parse_definitely (parser))
14606 invalid_nested_name_p = true;
14607 id = cp_parser_identifier (parser);
14608 if (id == error_mark_node)
14609 id = NULL_TREE;
14611 /* If we could not find a corresponding TYPE, treat this
14612 declaration like an unqualified declaration. */
14613 if (type == error_mark_node)
14614 nested_name_specifier = NULL_TREE;
14615 /* Otherwise, count the number of templates used in TYPE and its
14616 containing scopes. */
14617 else
14619 tree scope;
14621 for (scope = TREE_TYPE (type);
14622 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14623 scope = (TYPE_P (scope)
14624 ? TYPE_CONTEXT (scope)
14625 : DECL_CONTEXT (scope)))
14626 if (TYPE_P (scope)
14627 && CLASS_TYPE_P (scope)
14628 && CLASSTYPE_TEMPLATE_INFO (scope)
14629 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14630 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14631 ++num_templates;
14634 /* Otherwise, the identifier is optional. */
14635 else
14637 /* We don't know whether what comes next is a template-id,
14638 an identifier, or nothing at all. */
14639 cp_parser_parse_tentatively (parser);
14640 /* Check for a template-id. */
14641 id = cp_parser_template_id (parser,
14642 /*template_keyword_p=*/false,
14643 /*check_dependency_p=*/true,
14644 /*is_declaration=*/true);
14645 /* If that didn't work, it could still be an identifier. */
14646 if (!cp_parser_parse_definitely (parser))
14648 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14649 id = cp_parser_identifier (parser);
14650 else
14651 id = NULL_TREE;
14653 else
14655 template_id_p = true;
14656 ++num_templates;
14660 pop_deferring_access_checks ();
14662 if (id)
14663 cp_parser_check_for_invalid_template_id (parser, id);
14665 /* If it's not a `:' or a `{' then we can't really be looking at a
14666 class-head, since a class-head only appears as part of a
14667 class-specifier. We have to detect this situation before calling
14668 xref_tag, since that has irreversible side-effects. */
14669 if (!cp_parser_next_token_starts_class_definition_p (parser))
14671 cp_parser_error (parser, "expected %<{%> or %<:%>");
14672 return error_mark_node;
14675 /* At this point, we're going ahead with the class-specifier, even
14676 if some other problem occurs. */
14677 cp_parser_commit_to_tentative_parse (parser);
14678 /* Issue the error about the overly-qualified name now. */
14679 if (qualified_p)
14680 cp_parser_error (parser,
14681 "global qualification of class name is invalid");
14682 else if (invalid_nested_name_p)
14683 cp_parser_error (parser,
14684 "qualified name does not name a class");
14685 else if (nested_name_specifier)
14687 tree scope;
14689 /* Reject typedef-names in class heads. */
14690 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14692 error ("invalid class name in declaration of %qD", type);
14693 type = NULL_TREE;
14694 goto done;
14697 /* Figure out in what scope the declaration is being placed. */
14698 scope = current_scope ();
14699 /* If that scope does not contain the scope in which the
14700 class was originally declared, the program is invalid. */
14701 if (scope && !is_ancestor (scope, nested_name_specifier))
14703 if (at_namespace_scope_p ())
14704 error ("declaration of %qD in namespace %qD which does not "
14705 "enclose %qD", type, scope, nested_name_specifier);
14706 else
14707 error ("declaration of %qD in %qD which does not enclose %qD",
14708 type, scope, nested_name_specifier);
14709 type = NULL_TREE;
14710 goto done;
14712 /* [dcl.meaning]
14714 A declarator-id shall not be qualified exception of the
14715 definition of a ... nested class outside of its class
14716 ... [or] a the definition or explicit instantiation of a
14717 class member of a namespace outside of its namespace. */
14718 if (scope == nested_name_specifier)
14720 pedwarn ("extra qualification ignored");
14721 nested_name_specifier = NULL_TREE;
14722 num_templates = 0;
14725 /* An explicit-specialization must be preceded by "template <>". If
14726 it is not, try to recover gracefully. */
14727 if (at_namespace_scope_p ()
14728 && parser->num_template_parameter_lists == 0
14729 && template_id_p)
14731 error ("an explicit specialization must be preceded by %<template <>%>");
14732 invalid_explicit_specialization_p = true;
14733 /* Take the same action that would have been taken by
14734 cp_parser_explicit_specialization. */
14735 ++parser->num_template_parameter_lists;
14736 begin_specialization ();
14738 /* There must be no "return" statements between this point and the
14739 end of this function; set "type "to the correct return value and
14740 use "goto done;" to return. */
14741 /* Make sure that the right number of template parameters were
14742 present. */
14743 if (!cp_parser_check_template_parameters (parser, num_templates))
14745 /* If something went wrong, there is no point in even trying to
14746 process the class-definition. */
14747 type = NULL_TREE;
14748 goto done;
14751 /* Look up the type. */
14752 if (template_id_p)
14754 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
14755 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
14756 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
14758 error ("function template %qD redeclared as a class template", id);
14759 type = error_mark_node;
14761 else
14763 type = TREE_TYPE (id);
14764 type = maybe_process_partial_specialization (type);
14766 if (nested_name_specifier)
14767 pushed_scope = push_scope (nested_name_specifier);
14769 else if (nested_name_specifier)
14771 tree class_type;
14773 /* Given:
14775 template <typename T> struct S { struct T };
14776 template <typename T> struct S<T>::T { };
14778 we will get a TYPENAME_TYPE when processing the definition of
14779 `S::T'. We need to resolve it to the actual type before we
14780 try to define it. */
14781 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14783 class_type = resolve_typename_type (TREE_TYPE (type),
14784 /*only_current_p=*/false);
14785 if (TREE_CODE (class_type) != TYPENAME_TYPE)
14786 type = TYPE_NAME (class_type);
14787 else
14789 cp_parser_error (parser, "could not resolve typename type");
14790 type = error_mark_node;
14794 maybe_process_partial_specialization (TREE_TYPE (type));
14795 class_type = current_class_type;
14796 /* Enter the scope indicated by the nested-name-specifier. */
14797 pushed_scope = push_scope (nested_name_specifier);
14798 /* Get the canonical version of this type. */
14799 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14800 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14801 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14803 type = push_template_decl (type);
14804 if (type == error_mark_node)
14806 type = NULL_TREE;
14807 goto done;
14811 type = TREE_TYPE (type);
14812 *nested_name_specifier_p = true;
14814 else /* The name is not a nested name. */
14816 /* If the class was unnamed, create a dummy name. */
14817 if (!id)
14818 id = make_anon_name ();
14819 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14820 parser->num_template_parameter_lists);
14823 /* Indicate whether this class was declared as a `class' or as a
14824 `struct'. */
14825 if (TREE_CODE (type) == RECORD_TYPE)
14826 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14827 cp_parser_check_class_key (class_key, type);
14829 /* If this type was already complete, and we see another definition,
14830 that's an error. */
14831 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14833 error ("redefinition of %q#T", type);
14834 error ("previous definition of %q+#T", type);
14835 type = NULL_TREE;
14836 goto done;
14838 else if (type == error_mark_node)
14839 type = NULL_TREE;
14841 /* We will have entered the scope containing the class; the names of
14842 base classes should be looked up in that context. For example:
14844 struct A { struct B {}; struct C; };
14845 struct A::C : B {};
14847 is valid. */
14849 /* Get the list of base-classes, if there is one. */
14850 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14851 *bases = cp_parser_base_clause (parser);
14853 done:
14854 /* Leave the scope given by the nested-name-specifier. We will
14855 enter the class scope itself while processing the members. */
14856 if (pushed_scope)
14857 pop_scope (pushed_scope);
14859 if (invalid_explicit_specialization_p)
14861 end_specialization ();
14862 --parser->num_template_parameter_lists;
14864 *attributes_p = attributes;
14865 return type;
14868 /* Parse a class-key.
14870 class-key:
14871 class
14872 struct
14873 union
14875 Returns the kind of class-key specified, or none_type to indicate
14876 error. */
14878 static enum tag_types
14879 cp_parser_class_key (cp_parser* parser)
14881 cp_token *token;
14882 enum tag_types tag_type;
14884 /* Look for the class-key. */
14885 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14886 if (!token)
14887 return none_type;
14889 /* Check to see if the TOKEN is a class-key. */
14890 tag_type = cp_parser_token_is_class_key (token);
14891 if (!tag_type)
14892 cp_parser_error (parser, "expected class-key");
14893 return tag_type;
14896 /* Parse an (optional) member-specification.
14898 member-specification:
14899 member-declaration member-specification [opt]
14900 access-specifier : member-specification [opt] */
14902 static void
14903 cp_parser_member_specification_opt (cp_parser* parser)
14905 while (true)
14907 cp_token *token;
14908 enum rid keyword;
14910 /* Peek at the next token. */
14911 token = cp_lexer_peek_token (parser->lexer);
14912 /* If it's a `}', or EOF then we've seen all the members. */
14913 if (token->type == CPP_CLOSE_BRACE
14914 || token->type == CPP_EOF
14915 || token->type == CPP_PRAGMA_EOL)
14916 break;
14918 /* See if this token is a keyword. */
14919 keyword = token->keyword;
14920 switch (keyword)
14922 case RID_PUBLIC:
14923 case RID_PROTECTED:
14924 case RID_PRIVATE:
14925 /* Consume the access-specifier. */
14926 cp_lexer_consume_token (parser->lexer);
14927 /* Remember which access-specifier is active. */
14928 current_access_specifier = token->u.value;
14929 /* Look for the `:'. */
14930 cp_parser_require (parser, CPP_COLON, "%<:%>");
14931 break;
14933 default:
14934 /* Accept #pragmas at class scope. */
14935 if (token->type == CPP_PRAGMA)
14937 cp_parser_pragma (parser, pragma_external);
14938 break;
14941 /* Otherwise, the next construction must be a
14942 member-declaration. */
14943 cp_parser_member_declaration (parser);
14948 /* Parse a member-declaration.
14950 member-declaration:
14951 decl-specifier-seq [opt] member-declarator-list [opt] ;
14952 function-definition ; [opt]
14953 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14954 using-declaration
14955 template-declaration
14957 member-declarator-list:
14958 member-declarator
14959 member-declarator-list , member-declarator
14961 member-declarator:
14962 declarator pure-specifier [opt]
14963 declarator constant-initializer [opt]
14964 identifier [opt] : constant-expression
14966 GNU Extensions:
14968 member-declaration:
14969 __extension__ member-declaration
14971 member-declarator:
14972 declarator attributes [opt] pure-specifier [opt]
14973 declarator attributes [opt] constant-initializer [opt]
14974 identifier [opt] attributes [opt] : constant-expression
14976 C++0x Extensions:
14978 member-declaration:
14979 static_assert-declaration */
14981 static void
14982 cp_parser_member_declaration (cp_parser* parser)
14984 cp_decl_specifier_seq decl_specifiers;
14985 tree prefix_attributes;
14986 tree decl;
14987 int declares_class_or_enum;
14988 bool friend_p;
14989 cp_token *token;
14990 int saved_pedantic;
14992 /* Check for the `__extension__' keyword. */
14993 if (cp_parser_extension_opt (parser, &saved_pedantic))
14995 /* Recurse. */
14996 cp_parser_member_declaration (parser);
14997 /* Restore the old value of the PEDANTIC flag. */
14998 pedantic = saved_pedantic;
15000 return;
15003 /* Check for a template-declaration. */
15004 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15006 /* An explicit specialization here is an error condition, and we
15007 expect the specialization handler to detect and report this. */
15008 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15009 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15010 cp_parser_explicit_specialization (parser);
15011 else
15012 cp_parser_template_declaration (parser, /*member_p=*/true);
15014 return;
15017 /* Check for a using-declaration. */
15018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15020 /* Parse the using-declaration. */
15021 cp_parser_using_declaration (parser,
15022 /*access_declaration_p=*/false);
15023 return;
15026 /* Check for @defs. */
15027 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15029 tree ivar, member;
15030 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15031 ivar = ivar_chains;
15032 while (ivar)
15034 member = ivar;
15035 ivar = TREE_CHAIN (member);
15036 TREE_CHAIN (member) = NULL_TREE;
15037 finish_member_declaration (member);
15039 return;
15042 /* If the next token is `static_assert' we have a static assertion. */
15043 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15045 cp_parser_static_assert (parser, /*member_p=*/true);
15046 return;
15049 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15050 return;
15052 /* Parse the decl-specifier-seq. */
15053 cp_parser_decl_specifier_seq (parser,
15054 CP_PARSER_FLAGS_OPTIONAL,
15055 &decl_specifiers,
15056 &declares_class_or_enum);
15057 prefix_attributes = decl_specifiers.attributes;
15058 decl_specifiers.attributes = NULL_TREE;
15059 /* Check for an invalid type-name. */
15060 if (!decl_specifiers.type
15061 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15062 return;
15063 /* If there is no declarator, then the decl-specifier-seq should
15064 specify a type. */
15065 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15067 /* If there was no decl-specifier-seq, and the next token is a
15068 `;', then we have something like:
15070 struct S { ; };
15072 [class.mem]
15074 Each member-declaration shall declare at least one member
15075 name of the class. */
15076 if (!decl_specifiers.any_specifiers_p)
15078 cp_token *token = cp_lexer_peek_token (parser->lexer);
15079 if (pedantic && !token->in_system_header)
15080 pedwarn ("%Hextra %<;%>", &token->location);
15082 else
15084 tree type;
15086 /* See if this declaration is a friend. */
15087 friend_p = cp_parser_friend_p (&decl_specifiers);
15088 /* If there were decl-specifiers, check to see if there was
15089 a class-declaration. */
15090 type = check_tag_decl (&decl_specifiers);
15091 /* Nested classes have already been added to the class, but
15092 a `friend' needs to be explicitly registered. */
15093 if (friend_p)
15095 /* If the `friend' keyword was present, the friend must
15096 be introduced with a class-key. */
15097 if (!declares_class_or_enum)
15098 error ("a class-key must be used when declaring a friend");
15099 /* In this case:
15101 template <typename T> struct A {
15102 friend struct A<T>::B;
15105 A<T>::B will be represented by a TYPENAME_TYPE, and
15106 therefore not recognized by check_tag_decl. */
15107 if (!type
15108 && decl_specifiers.type
15109 && TYPE_P (decl_specifiers.type))
15110 type = decl_specifiers.type;
15111 if (!type || !TYPE_P (type))
15112 error ("friend declaration does not name a class or "
15113 "function");
15114 else
15115 make_friend_class (current_class_type, type,
15116 /*complain=*/true);
15118 /* If there is no TYPE, an error message will already have
15119 been issued. */
15120 else if (!type || type == error_mark_node)
15122 /* An anonymous aggregate has to be handled specially; such
15123 a declaration really declares a data member (with a
15124 particular type), as opposed to a nested class. */
15125 else if (ANON_AGGR_TYPE_P (type))
15127 /* Remove constructors and such from TYPE, now that we
15128 know it is an anonymous aggregate. */
15129 fixup_anonymous_aggr (type);
15130 /* And make the corresponding data member. */
15131 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15132 /* Add it to the class. */
15133 finish_member_declaration (decl);
15135 else
15136 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
15139 else
15141 /* See if these declarations will be friends. */
15142 friend_p = cp_parser_friend_p (&decl_specifiers);
15144 /* Keep going until we hit the `;' at the end of the
15145 declaration. */
15146 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15148 tree attributes = NULL_TREE;
15149 tree first_attribute;
15151 /* Peek at the next token. */
15152 token = cp_lexer_peek_token (parser->lexer);
15154 /* Check for a bitfield declaration. */
15155 if (token->type == CPP_COLON
15156 || (token->type == CPP_NAME
15157 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15158 == CPP_COLON))
15160 tree identifier;
15161 tree width;
15163 /* Get the name of the bitfield. Note that we cannot just
15164 check TOKEN here because it may have been invalidated by
15165 the call to cp_lexer_peek_nth_token above. */
15166 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15167 identifier = cp_parser_identifier (parser);
15168 else
15169 identifier = NULL_TREE;
15171 /* Consume the `:' token. */
15172 cp_lexer_consume_token (parser->lexer);
15173 /* Get the width of the bitfield. */
15174 width
15175 = cp_parser_constant_expression (parser,
15176 /*allow_non_constant=*/false,
15177 NULL);
15179 /* Look for attributes that apply to the bitfield. */
15180 attributes = cp_parser_attributes_opt (parser);
15181 /* Remember which attributes are prefix attributes and
15182 which are not. */
15183 first_attribute = attributes;
15184 /* Combine the attributes. */
15185 attributes = chainon (prefix_attributes, attributes);
15187 /* Create the bitfield declaration. */
15188 decl = grokbitfield (identifier
15189 ? make_id_declarator (NULL_TREE,
15190 identifier,
15191 sfk_none)
15192 : NULL,
15193 &decl_specifiers,
15194 width);
15195 /* Apply the attributes. */
15196 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
15198 else
15200 cp_declarator *declarator;
15201 tree initializer;
15202 tree asm_specification;
15203 int ctor_dtor_or_conv_p;
15205 /* Parse the declarator. */
15206 declarator
15207 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15208 &ctor_dtor_or_conv_p,
15209 /*parenthesized_p=*/NULL,
15210 /*member_p=*/true);
15212 /* If something went wrong parsing the declarator, make sure
15213 that we at least consume some tokens. */
15214 if (declarator == cp_error_declarator)
15216 /* Skip to the end of the statement. */
15217 cp_parser_skip_to_end_of_statement (parser);
15218 /* If the next token is not a semicolon, that is
15219 probably because we just skipped over the body of
15220 a function. So, we consume a semicolon if
15221 present, but do not issue an error message if it
15222 is not present. */
15223 if (cp_lexer_next_token_is (parser->lexer,
15224 CPP_SEMICOLON))
15225 cp_lexer_consume_token (parser->lexer);
15226 return;
15229 if (declares_class_or_enum & 2)
15230 cp_parser_check_for_definition_in_return_type
15231 (declarator, decl_specifiers.type);
15233 /* Look for an asm-specification. */
15234 asm_specification = cp_parser_asm_specification_opt (parser);
15235 /* Look for attributes that apply to the declaration. */
15236 attributes = cp_parser_attributes_opt (parser);
15237 /* Remember which attributes are prefix attributes and
15238 which are not. */
15239 first_attribute = attributes;
15240 /* Combine the attributes. */
15241 attributes = chainon (prefix_attributes, attributes);
15243 /* If it's an `=', then we have a constant-initializer or a
15244 pure-specifier. It is not correct to parse the
15245 initializer before registering the member declaration
15246 since the member declaration should be in scope while
15247 its initializer is processed. However, the rest of the
15248 front end does not yet provide an interface that allows
15249 us to handle this correctly. */
15250 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15252 /* In [class.mem]:
15254 A pure-specifier shall be used only in the declaration of
15255 a virtual function.
15257 A member-declarator can contain a constant-initializer
15258 only if it declares a static member of integral or
15259 enumeration type.
15261 Therefore, if the DECLARATOR is for a function, we look
15262 for a pure-specifier; otherwise, we look for a
15263 constant-initializer. When we call `grokfield', it will
15264 perform more stringent semantics checks. */
15265 if (function_declarator_p (declarator))
15266 initializer = cp_parser_pure_specifier (parser);
15267 else
15268 /* Parse the initializer. */
15269 initializer = cp_parser_constant_initializer (parser);
15271 /* Otherwise, there is no initializer. */
15272 else
15273 initializer = NULL_TREE;
15275 /* See if we are probably looking at a function
15276 definition. We are certainly not looking at a
15277 member-declarator. Calling `grokfield' has
15278 side-effects, so we must not do it unless we are sure
15279 that we are looking at a member-declarator. */
15280 if (cp_parser_token_starts_function_definition_p
15281 (cp_lexer_peek_token (parser->lexer)))
15283 /* The grammar does not allow a pure-specifier to be
15284 used when a member function is defined. (It is
15285 possible that this fact is an oversight in the
15286 standard, since a pure function may be defined
15287 outside of the class-specifier. */
15288 if (initializer)
15289 error ("pure-specifier on function-definition");
15290 decl = cp_parser_save_member_function_body (parser,
15291 &decl_specifiers,
15292 declarator,
15293 attributes);
15294 /* If the member was not a friend, declare it here. */
15295 if (!friend_p)
15296 finish_member_declaration (decl);
15297 /* Peek at the next token. */
15298 token = cp_lexer_peek_token (parser->lexer);
15299 /* If the next token is a semicolon, consume it. */
15300 if (token->type == CPP_SEMICOLON)
15301 cp_lexer_consume_token (parser->lexer);
15302 return;
15304 else
15305 /* Create the declaration. */
15306 decl = grokfield (declarator, &decl_specifiers,
15307 initializer, /*init_const_expr_p=*/true,
15308 asm_specification,
15309 attributes);
15312 /* Reset PREFIX_ATTRIBUTES. */
15313 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15314 attributes = TREE_CHAIN (attributes);
15315 if (attributes)
15316 TREE_CHAIN (attributes) = NULL_TREE;
15318 /* If there is any qualification still in effect, clear it
15319 now; we will be starting fresh with the next declarator. */
15320 parser->scope = NULL_TREE;
15321 parser->qualifying_scope = NULL_TREE;
15322 parser->object_scope = NULL_TREE;
15323 /* If it's a `,', then there are more declarators. */
15324 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15325 cp_lexer_consume_token (parser->lexer);
15326 /* If the next token isn't a `;', then we have a parse error. */
15327 else if (cp_lexer_next_token_is_not (parser->lexer,
15328 CPP_SEMICOLON))
15330 cp_parser_error (parser, "expected %<;%>");
15331 /* Skip tokens until we find a `;'. */
15332 cp_parser_skip_to_end_of_statement (parser);
15334 break;
15337 if (decl)
15339 /* Add DECL to the list of members. */
15340 if (!friend_p)
15341 finish_member_declaration (decl);
15343 if (TREE_CODE (decl) == FUNCTION_DECL)
15344 cp_parser_save_default_args (parser, decl);
15349 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15352 /* Parse a pure-specifier.
15354 pure-specifier:
15357 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15358 Otherwise, ERROR_MARK_NODE is returned. */
15360 static tree
15361 cp_parser_pure_specifier (cp_parser* parser)
15363 cp_token *token;
15365 /* Look for the `=' token. */
15366 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15367 return error_mark_node;
15368 /* Look for the `0' token. */
15369 token = cp_lexer_consume_token (parser->lexer);
15370 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15371 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15373 cp_parser_error (parser,
15374 "invalid pure specifier (only %<= 0%> is allowed)");
15375 cp_parser_skip_to_end_of_statement (parser);
15376 return error_mark_node;
15378 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15380 error ("templates may not be %<virtual%>");
15381 return error_mark_node;
15384 return integer_zero_node;
15387 /* Parse a constant-initializer.
15389 constant-initializer:
15390 = constant-expression
15392 Returns a representation of the constant-expression. */
15394 static tree
15395 cp_parser_constant_initializer (cp_parser* parser)
15397 /* Look for the `=' token. */
15398 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15399 return error_mark_node;
15401 /* It is invalid to write:
15403 struct S { static const int i = { 7 }; };
15406 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15408 cp_parser_error (parser,
15409 "a brace-enclosed initializer is not allowed here");
15410 /* Consume the opening brace. */
15411 cp_lexer_consume_token (parser->lexer);
15412 /* Skip the initializer. */
15413 cp_parser_skip_to_closing_brace (parser);
15414 /* Look for the trailing `}'. */
15415 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15417 return error_mark_node;
15420 return cp_parser_constant_expression (parser,
15421 /*allow_non_constant=*/false,
15422 NULL);
15425 /* Derived classes [gram.class.derived] */
15427 /* Parse a base-clause.
15429 base-clause:
15430 : base-specifier-list
15432 base-specifier-list:
15433 base-specifier ... [opt]
15434 base-specifier-list , base-specifier ... [opt]
15436 Returns a TREE_LIST representing the base-classes, in the order in
15437 which they were declared. The representation of each node is as
15438 described by cp_parser_base_specifier.
15440 In the case that no bases are specified, this function will return
15441 NULL_TREE, not ERROR_MARK_NODE. */
15443 static tree
15444 cp_parser_base_clause (cp_parser* parser)
15446 tree bases = NULL_TREE;
15448 /* Look for the `:' that begins the list. */
15449 cp_parser_require (parser, CPP_COLON, "%<:%>");
15451 /* Scan the base-specifier-list. */
15452 while (true)
15454 cp_token *token;
15455 tree base;
15456 bool pack_expansion_p = false;
15458 /* Look for the base-specifier. */
15459 base = cp_parser_base_specifier (parser);
15460 /* Look for the (optional) ellipsis. */
15461 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15463 /* Consume the `...'. */
15464 cp_lexer_consume_token (parser->lexer);
15466 pack_expansion_p = true;
15469 /* Add BASE to the front of the list. */
15470 if (base != error_mark_node)
15472 if (pack_expansion_p)
15473 /* Make this a pack expansion type. */
15474 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15477 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15479 TREE_CHAIN (base) = bases;
15480 bases = base;
15483 /* Peek at the next token. */
15484 token = cp_lexer_peek_token (parser->lexer);
15485 /* If it's not a comma, then the list is complete. */
15486 if (token->type != CPP_COMMA)
15487 break;
15488 /* Consume the `,'. */
15489 cp_lexer_consume_token (parser->lexer);
15492 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15493 base class had a qualified name. However, the next name that
15494 appears is certainly not qualified. */
15495 parser->scope = NULL_TREE;
15496 parser->qualifying_scope = NULL_TREE;
15497 parser->object_scope = NULL_TREE;
15499 return nreverse (bases);
15502 /* Parse a base-specifier.
15504 base-specifier:
15505 :: [opt] nested-name-specifier [opt] class-name
15506 virtual access-specifier [opt] :: [opt] nested-name-specifier
15507 [opt] class-name
15508 access-specifier virtual [opt] :: [opt] nested-name-specifier
15509 [opt] class-name
15511 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15512 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15513 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15514 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15516 static tree
15517 cp_parser_base_specifier (cp_parser* parser)
15519 cp_token *token;
15520 bool done = false;
15521 bool virtual_p = false;
15522 bool duplicate_virtual_error_issued_p = false;
15523 bool duplicate_access_error_issued_p = false;
15524 bool class_scope_p, template_p;
15525 tree access = access_default_node;
15526 tree type;
15528 /* Process the optional `virtual' and `access-specifier'. */
15529 while (!done)
15531 /* Peek at the next token. */
15532 token = cp_lexer_peek_token (parser->lexer);
15533 /* Process `virtual'. */
15534 switch (token->keyword)
15536 case RID_VIRTUAL:
15537 /* If `virtual' appears more than once, issue an error. */
15538 if (virtual_p && !duplicate_virtual_error_issued_p)
15540 cp_parser_error (parser,
15541 "%<virtual%> specified more than once in base-specified");
15542 duplicate_virtual_error_issued_p = true;
15545 virtual_p = true;
15547 /* Consume the `virtual' token. */
15548 cp_lexer_consume_token (parser->lexer);
15550 break;
15552 case RID_PUBLIC:
15553 case RID_PROTECTED:
15554 case RID_PRIVATE:
15555 /* If more than one access specifier appears, issue an
15556 error. */
15557 if (access != access_default_node
15558 && !duplicate_access_error_issued_p)
15560 cp_parser_error (parser,
15561 "more than one access specifier in base-specified");
15562 duplicate_access_error_issued_p = true;
15565 access = ridpointers[(int) token->keyword];
15567 /* Consume the access-specifier. */
15568 cp_lexer_consume_token (parser->lexer);
15570 break;
15572 default:
15573 done = true;
15574 break;
15577 /* It is not uncommon to see programs mechanically, erroneously, use
15578 the 'typename' keyword to denote (dependent) qualified types
15579 as base classes. */
15580 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15582 if (!processing_template_decl)
15583 error ("keyword %<typename%> not allowed outside of templates");
15584 else
15585 error ("keyword %<typename%> not allowed in this context "
15586 "(the base class is implicitly a type)");
15587 cp_lexer_consume_token (parser->lexer);
15590 /* Look for the optional `::' operator. */
15591 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15592 /* Look for the nested-name-specifier. The simplest way to
15593 implement:
15595 [temp.res]
15597 The keyword `typename' is not permitted in a base-specifier or
15598 mem-initializer; in these contexts a qualified name that
15599 depends on a template-parameter is implicitly assumed to be a
15600 type name.
15602 is to pretend that we have seen the `typename' keyword at this
15603 point. */
15604 cp_parser_nested_name_specifier_opt (parser,
15605 /*typename_keyword_p=*/true,
15606 /*check_dependency_p=*/true,
15607 typename_type,
15608 /*is_declaration=*/true);
15609 /* If the base class is given by a qualified name, assume that names
15610 we see are type names or templates, as appropriate. */
15611 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15612 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15614 /* Finally, look for the class-name. */
15615 type = cp_parser_class_name (parser,
15616 class_scope_p,
15617 template_p,
15618 typename_type,
15619 /*check_dependency_p=*/true,
15620 /*class_head_p=*/false,
15621 /*is_declaration=*/true);
15623 if (type == error_mark_node)
15624 return error_mark_node;
15626 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15629 /* Exception handling [gram.exception] */
15631 /* Parse an (optional) exception-specification.
15633 exception-specification:
15634 throw ( type-id-list [opt] )
15636 Returns a TREE_LIST representing the exception-specification. The
15637 TREE_VALUE of each node is a type. */
15639 static tree
15640 cp_parser_exception_specification_opt (cp_parser* parser)
15642 cp_token *token;
15643 tree type_id_list;
15645 /* Peek at the next token. */
15646 token = cp_lexer_peek_token (parser->lexer);
15647 /* If it's not `throw', then there's no exception-specification. */
15648 if (!cp_parser_is_keyword (token, RID_THROW))
15649 return NULL_TREE;
15651 /* Consume the `throw'. */
15652 cp_lexer_consume_token (parser->lexer);
15654 /* Look for the `('. */
15655 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15657 /* Peek at the next token. */
15658 token = cp_lexer_peek_token (parser->lexer);
15659 /* If it's not a `)', then there is a type-id-list. */
15660 if (token->type != CPP_CLOSE_PAREN)
15662 const char *saved_message;
15664 /* Types may not be defined in an exception-specification. */
15665 saved_message = parser->type_definition_forbidden_message;
15666 parser->type_definition_forbidden_message
15667 = "types may not be defined in an exception-specification";
15668 /* Parse the type-id-list. */
15669 type_id_list = cp_parser_type_id_list (parser);
15670 /* Restore the saved message. */
15671 parser->type_definition_forbidden_message = saved_message;
15673 else
15674 type_id_list = empty_except_spec;
15676 /* Look for the `)'. */
15677 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15679 return type_id_list;
15682 /* Parse an (optional) type-id-list.
15684 type-id-list:
15685 type-id ... [opt]
15686 type-id-list , type-id ... [opt]
15688 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15689 in the order that the types were presented. */
15691 static tree
15692 cp_parser_type_id_list (cp_parser* parser)
15694 tree types = NULL_TREE;
15696 while (true)
15698 cp_token *token;
15699 tree type;
15701 /* Get the next type-id. */
15702 type = cp_parser_type_id (parser);
15703 /* Parse the optional ellipsis. */
15704 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15706 /* Consume the `...'. */
15707 cp_lexer_consume_token (parser->lexer);
15709 /* Turn the type into a pack expansion expression. */
15710 type = make_pack_expansion (type);
15712 /* Add it to the list. */
15713 types = add_exception_specifier (types, type, /*complain=*/1);
15714 /* Peek at the next token. */
15715 token = cp_lexer_peek_token (parser->lexer);
15716 /* If it is not a `,', we are done. */
15717 if (token->type != CPP_COMMA)
15718 break;
15719 /* Consume the `,'. */
15720 cp_lexer_consume_token (parser->lexer);
15723 return nreverse (types);
15726 /* Parse a try-block.
15728 try-block:
15729 try compound-statement handler-seq */
15731 static tree
15732 cp_parser_try_block (cp_parser* parser)
15734 tree try_block;
15736 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
15737 try_block = begin_try_block ();
15738 cp_parser_compound_statement (parser, NULL, true);
15739 finish_try_block (try_block);
15740 cp_parser_handler_seq (parser);
15741 finish_handler_sequence (try_block);
15743 return try_block;
15746 /* Parse a function-try-block.
15748 function-try-block:
15749 try ctor-initializer [opt] function-body handler-seq */
15751 static bool
15752 cp_parser_function_try_block (cp_parser* parser)
15754 tree compound_stmt;
15755 tree try_block;
15756 bool ctor_initializer_p;
15758 /* Look for the `try' keyword. */
15759 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
15760 return false;
15761 /* Let the rest of the front end know where we are. */
15762 try_block = begin_function_try_block (&compound_stmt);
15763 /* Parse the function-body. */
15764 ctor_initializer_p
15765 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15766 /* We're done with the `try' part. */
15767 finish_function_try_block (try_block);
15768 /* Parse the handlers. */
15769 cp_parser_handler_seq (parser);
15770 /* We're done with the handlers. */
15771 finish_function_handler_sequence (try_block, compound_stmt);
15773 return ctor_initializer_p;
15776 /* Parse a handler-seq.
15778 handler-seq:
15779 handler handler-seq [opt] */
15781 static void
15782 cp_parser_handler_seq (cp_parser* parser)
15784 while (true)
15786 cp_token *token;
15788 /* Parse the handler. */
15789 cp_parser_handler (parser);
15790 /* Peek at the next token. */
15791 token = cp_lexer_peek_token (parser->lexer);
15792 /* If it's not `catch' then there are no more handlers. */
15793 if (!cp_parser_is_keyword (token, RID_CATCH))
15794 break;
15798 /* Parse a handler.
15800 handler:
15801 catch ( exception-declaration ) compound-statement */
15803 static void
15804 cp_parser_handler (cp_parser* parser)
15806 tree handler;
15807 tree declaration;
15809 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
15810 handler = begin_handler ();
15811 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15812 declaration = cp_parser_exception_declaration (parser);
15813 finish_handler_parms (declaration, handler);
15814 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15815 cp_parser_compound_statement (parser, NULL, false);
15816 finish_handler (handler);
15819 /* Parse an exception-declaration.
15821 exception-declaration:
15822 type-specifier-seq declarator
15823 type-specifier-seq abstract-declarator
15824 type-specifier-seq
15827 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15828 ellipsis variant is used. */
15830 static tree
15831 cp_parser_exception_declaration (cp_parser* parser)
15833 cp_decl_specifier_seq type_specifiers;
15834 cp_declarator *declarator;
15835 const char *saved_message;
15837 /* If it's an ellipsis, it's easy to handle. */
15838 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15840 /* Consume the `...' token. */
15841 cp_lexer_consume_token (parser->lexer);
15842 return NULL_TREE;
15845 /* Types may not be defined in exception-declarations. */
15846 saved_message = parser->type_definition_forbidden_message;
15847 parser->type_definition_forbidden_message
15848 = "types may not be defined in exception-declarations";
15850 /* Parse the type-specifier-seq. */
15851 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15852 &type_specifiers);
15853 /* If it's a `)', then there is no declarator. */
15854 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15855 declarator = NULL;
15856 else
15857 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15858 /*ctor_dtor_or_conv_p=*/NULL,
15859 /*parenthesized_p=*/NULL,
15860 /*member_p=*/false);
15862 /* Restore the saved message. */
15863 parser->type_definition_forbidden_message = saved_message;
15865 if (!type_specifiers.any_specifiers_p)
15866 return error_mark_node;
15868 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15871 /* Parse a throw-expression.
15873 throw-expression:
15874 throw assignment-expression [opt]
15876 Returns a THROW_EXPR representing the throw-expression. */
15878 static tree
15879 cp_parser_throw_expression (cp_parser* parser)
15881 tree expression;
15882 cp_token* token;
15884 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
15885 token = cp_lexer_peek_token (parser->lexer);
15886 /* Figure out whether or not there is an assignment-expression
15887 following the "throw" keyword. */
15888 if (token->type == CPP_COMMA
15889 || token->type == CPP_SEMICOLON
15890 || token->type == CPP_CLOSE_PAREN
15891 || token->type == CPP_CLOSE_SQUARE
15892 || token->type == CPP_CLOSE_BRACE
15893 || token->type == CPP_COLON)
15894 expression = NULL_TREE;
15895 else
15896 expression = cp_parser_assignment_expression (parser,
15897 /*cast_p=*/false);
15899 return build_throw (expression);
15902 /* GNU Extensions */
15904 /* Parse an (optional) asm-specification.
15906 asm-specification:
15907 asm ( string-literal )
15909 If the asm-specification is present, returns a STRING_CST
15910 corresponding to the string-literal. Otherwise, returns
15911 NULL_TREE. */
15913 static tree
15914 cp_parser_asm_specification_opt (cp_parser* parser)
15916 cp_token *token;
15917 tree asm_specification;
15919 /* Peek at the next token. */
15920 token = cp_lexer_peek_token (parser->lexer);
15921 /* If the next token isn't the `asm' keyword, then there's no
15922 asm-specification. */
15923 if (!cp_parser_is_keyword (token, RID_ASM))
15924 return NULL_TREE;
15926 /* Consume the `asm' token. */
15927 cp_lexer_consume_token (parser->lexer);
15928 /* Look for the `('. */
15929 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15931 /* Look for the string-literal. */
15932 asm_specification = cp_parser_string_literal (parser, false, false);
15934 /* Look for the `)'. */
15935 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15937 return asm_specification;
15940 /* Parse an asm-operand-list.
15942 asm-operand-list:
15943 asm-operand
15944 asm-operand-list , asm-operand
15946 asm-operand:
15947 string-literal ( expression )
15948 [ string-literal ] string-literal ( expression )
15950 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15951 each node is the expression. The TREE_PURPOSE is itself a
15952 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15953 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15954 is a STRING_CST for the string literal before the parenthesis. Returns
15955 ERROR_MARK_NODE if any of the operands are invalid. */
15957 static tree
15958 cp_parser_asm_operand_list (cp_parser* parser)
15960 tree asm_operands = NULL_TREE;
15961 bool invalid_operands = false;
15963 while (true)
15965 tree string_literal;
15966 tree expression;
15967 tree name;
15969 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15971 /* Consume the `[' token. */
15972 cp_lexer_consume_token (parser->lexer);
15973 /* Read the operand name. */
15974 name = cp_parser_identifier (parser);
15975 if (name != error_mark_node)
15976 name = build_string (IDENTIFIER_LENGTH (name),
15977 IDENTIFIER_POINTER (name));
15978 /* Look for the closing `]'. */
15979 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
15981 else
15982 name = NULL_TREE;
15983 /* Look for the string-literal. */
15984 string_literal = cp_parser_string_literal (parser, false, false);
15986 /* Look for the `('. */
15987 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15988 /* Parse the expression. */
15989 expression = cp_parser_expression (parser, /*cast_p=*/false);
15990 /* Look for the `)'. */
15991 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15993 if (name == error_mark_node
15994 || string_literal == error_mark_node
15995 || expression == error_mark_node)
15996 invalid_operands = true;
15998 /* Add this operand to the list. */
15999 asm_operands = tree_cons (build_tree_list (name, string_literal),
16000 expression,
16001 asm_operands);
16002 /* If the next token is not a `,', there are no more
16003 operands. */
16004 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16005 break;
16006 /* Consume the `,'. */
16007 cp_lexer_consume_token (parser->lexer);
16010 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16013 /* Parse an asm-clobber-list.
16015 asm-clobber-list:
16016 string-literal
16017 asm-clobber-list , string-literal
16019 Returns a TREE_LIST, indicating the clobbers in the order that they
16020 appeared. The TREE_VALUE of each node is a STRING_CST. */
16022 static tree
16023 cp_parser_asm_clobber_list (cp_parser* parser)
16025 tree clobbers = NULL_TREE;
16027 while (true)
16029 tree string_literal;
16031 /* Look for the string literal. */
16032 string_literal = cp_parser_string_literal (parser, false, false);
16033 /* Add it to the list. */
16034 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16035 /* If the next token is not a `,', then the list is
16036 complete. */
16037 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16038 break;
16039 /* Consume the `,' token. */
16040 cp_lexer_consume_token (parser->lexer);
16043 return clobbers;
16046 /* Parse an (optional) series of attributes.
16048 attributes:
16049 attributes attribute
16051 attribute:
16052 __attribute__ (( attribute-list [opt] ))
16054 The return value is as for cp_parser_attribute_list. */
16056 static tree
16057 cp_parser_attributes_opt (cp_parser* parser)
16059 tree attributes = NULL_TREE;
16061 while (true)
16063 cp_token *token;
16064 tree attribute_list;
16066 /* Peek at the next token. */
16067 token = cp_lexer_peek_token (parser->lexer);
16068 /* If it's not `__attribute__', then we're done. */
16069 if (token->keyword != RID_ATTRIBUTE)
16070 break;
16072 /* Consume the `__attribute__' keyword. */
16073 cp_lexer_consume_token (parser->lexer);
16074 /* Look for the two `(' tokens. */
16075 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16076 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16078 /* Peek at the next token. */
16079 token = cp_lexer_peek_token (parser->lexer);
16080 if (token->type != CPP_CLOSE_PAREN)
16081 /* Parse the attribute-list. */
16082 attribute_list = cp_parser_attribute_list (parser);
16083 else
16084 /* If the next token is a `)', then there is no attribute
16085 list. */
16086 attribute_list = NULL;
16088 /* Look for the two `)' tokens. */
16089 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16090 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16092 /* Add these new attributes to the list. */
16093 attributes = chainon (attributes, attribute_list);
16096 return attributes;
16099 /* Parse an attribute-list.
16101 attribute-list:
16102 attribute
16103 attribute-list , attribute
16105 attribute:
16106 identifier
16107 identifier ( identifier )
16108 identifier ( identifier , expression-list )
16109 identifier ( expression-list )
16111 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16112 to an attribute. The TREE_PURPOSE of each node is the identifier
16113 indicating which attribute is in use. The TREE_VALUE represents
16114 the arguments, if any. */
16116 static tree
16117 cp_parser_attribute_list (cp_parser* parser)
16119 tree attribute_list = NULL_TREE;
16120 bool save_translate_strings_p = parser->translate_strings_p;
16122 parser->translate_strings_p = false;
16123 while (true)
16125 cp_token *token;
16126 tree identifier;
16127 tree attribute;
16129 /* Look for the identifier. We also allow keywords here; for
16130 example `__attribute__ ((const))' is legal. */
16131 token = cp_lexer_peek_token (parser->lexer);
16132 if (token->type == CPP_NAME
16133 || token->type == CPP_KEYWORD)
16135 tree arguments = NULL_TREE;
16137 /* Consume the token. */
16138 token = cp_lexer_consume_token (parser->lexer);
16140 /* Save away the identifier that indicates which attribute
16141 this is. */
16142 identifier = token->u.value;
16143 attribute = build_tree_list (identifier, NULL_TREE);
16145 /* Peek at the next token. */
16146 token = cp_lexer_peek_token (parser->lexer);
16147 /* If it's an `(', then parse the attribute arguments. */
16148 if (token->type == CPP_OPEN_PAREN)
16150 arguments = cp_parser_parenthesized_expression_list
16151 (parser, true, /*cast_p=*/false,
16152 /*allow_expansion_p=*/false,
16153 /*non_constant_p=*/NULL);
16154 /* Save the arguments away. */
16155 TREE_VALUE (attribute) = arguments;
16158 if (arguments != error_mark_node)
16160 /* Add this attribute to the list. */
16161 TREE_CHAIN (attribute) = attribute_list;
16162 attribute_list = attribute;
16165 token = cp_lexer_peek_token (parser->lexer);
16167 /* Now, look for more attributes. If the next token isn't a
16168 `,', we're done. */
16169 if (token->type != CPP_COMMA)
16170 break;
16172 /* Consume the comma and keep going. */
16173 cp_lexer_consume_token (parser->lexer);
16175 parser->translate_strings_p = save_translate_strings_p;
16177 /* We built up the list in reverse order. */
16178 return nreverse (attribute_list);
16181 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16182 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16183 current value of the PEDANTIC flag, regardless of whether or not
16184 the `__extension__' keyword is present. The caller is responsible
16185 for restoring the value of the PEDANTIC flag. */
16187 static bool
16188 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16190 /* Save the old value of the PEDANTIC flag. */
16191 *saved_pedantic = pedantic;
16193 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16195 /* Consume the `__extension__' token. */
16196 cp_lexer_consume_token (parser->lexer);
16197 /* We're not being pedantic while the `__extension__' keyword is
16198 in effect. */
16199 pedantic = 0;
16201 return true;
16204 return false;
16207 /* Parse a label declaration.
16209 label-declaration:
16210 __label__ label-declarator-seq ;
16212 label-declarator-seq:
16213 identifier , label-declarator-seq
16214 identifier */
16216 static void
16217 cp_parser_label_declaration (cp_parser* parser)
16219 /* Look for the `__label__' keyword. */
16220 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16222 while (true)
16224 tree identifier;
16226 /* Look for an identifier. */
16227 identifier = cp_parser_identifier (parser);
16228 /* If we failed, stop. */
16229 if (identifier == error_mark_node)
16230 break;
16231 /* Declare it as a label. */
16232 finish_label_decl (identifier);
16233 /* If the next token is a `;', stop. */
16234 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16235 break;
16236 /* Look for the `,' separating the label declarations. */
16237 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16240 /* Look for the final `;'. */
16241 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16244 /* Support Functions */
16246 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16247 NAME should have one of the representations used for an
16248 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16249 is returned. If PARSER->SCOPE is a dependent type, then a
16250 SCOPE_REF is returned.
16252 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16253 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16254 was formed. Abstractly, such entities should not be passed to this
16255 function, because they do not need to be looked up, but it is
16256 simpler to check for this special case here, rather than at the
16257 call-sites.
16259 In cases not explicitly covered above, this function returns a
16260 DECL, OVERLOAD, or baselink representing the result of the lookup.
16261 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16262 is returned.
16264 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16265 (e.g., "struct") that was used. In that case bindings that do not
16266 refer to types are ignored.
16268 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16269 ignored.
16271 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16272 are ignored.
16274 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16275 types.
16277 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16278 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16279 NULL_TREE otherwise. */
16281 static tree
16282 cp_parser_lookup_name (cp_parser *parser, tree name,
16283 enum tag_types tag_type,
16284 bool is_template,
16285 bool is_namespace,
16286 bool check_dependency,
16287 tree *ambiguous_decls)
16289 int flags = 0;
16290 tree decl;
16291 tree object_type = parser->context->object_type;
16293 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16294 flags |= LOOKUP_COMPLAIN;
16296 /* Assume that the lookup will be unambiguous. */
16297 if (ambiguous_decls)
16298 *ambiguous_decls = NULL_TREE;
16300 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16301 no longer valid. Note that if we are parsing tentatively, and
16302 the parse fails, OBJECT_TYPE will be automatically restored. */
16303 parser->context->object_type = NULL_TREE;
16305 if (name == error_mark_node)
16306 return error_mark_node;
16308 /* A template-id has already been resolved; there is no lookup to
16309 do. */
16310 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16311 return name;
16312 if (BASELINK_P (name))
16314 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16315 == TEMPLATE_ID_EXPR);
16316 return name;
16319 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16320 it should already have been checked to make sure that the name
16321 used matches the type being destroyed. */
16322 if (TREE_CODE (name) == BIT_NOT_EXPR)
16324 tree type;
16326 /* Figure out to which type this destructor applies. */
16327 if (parser->scope)
16328 type = parser->scope;
16329 else if (object_type)
16330 type = object_type;
16331 else
16332 type = current_class_type;
16333 /* If that's not a class type, there is no destructor. */
16334 if (!type || !CLASS_TYPE_P (type))
16335 return error_mark_node;
16336 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16337 lazily_declare_fn (sfk_destructor, type);
16338 if (!CLASSTYPE_DESTRUCTORS (type))
16339 return error_mark_node;
16340 /* If it was a class type, return the destructor. */
16341 return CLASSTYPE_DESTRUCTORS (type);
16344 /* By this point, the NAME should be an ordinary identifier. If
16345 the id-expression was a qualified name, the qualifying scope is
16346 stored in PARSER->SCOPE at this point. */
16347 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16349 /* Perform the lookup. */
16350 if (parser->scope)
16352 bool dependent_p;
16354 if (parser->scope == error_mark_node)
16355 return error_mark_node;
16357 /* If the SCOPE is dependent, the lookup must be deferred until
16358 the template is instantiated -- unless we are explicitly
16359 looking up names in uninstantiated templates. Even then, we
16360 cannot look up the name if the scope is not a class type; it
16361 might, for example, be a template type parameter. */
16362 dependent_p = (TYPE_P (parser->scope)
16363 && !(parser->in_declarator_p
16364 && currently_open_class (parser->scope))
16365 && dependent_type_p (parser->scope));
16366 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16367 && dependent_p)
16369 if (tag_type)
16371 tree type;
16373 /* The resolution to Core Issue 180 says that `struct
16374 A::B' should be considered a type-name, even if `A'
16375 is dependent. */
16376 type = make_typename_type (parser->scope, name, tag_type,
16377 /*complain=*/tf_error);
16378 decl = TYPE_NAME (type);
16380 else if (is_template
16381 && (cp_parser_next_token_ends_template_argument_p (parser)
16382 || cp_lexer_next_token_is (parser->lexer,
16383 CPP_CLOSE_PAREN)))
16384 decl = make_unbound_class_template (parser->scope,
16385 name, NULL_TREE,
16386 /*complain=*/tf_error);
16387 else
16388 decl = build_qualified_name (/*type=*/NULL_TREE,
16389 parser->scope, name,
16390 is_template);
16392 else
16394 tree pushed_scope = NULL_TREE;
16396 /* If PARSER->SCOPE is a dependent type, then it must be a
16397 class type, and we must not be checking dependencies;
16398 otherwise, we would have processed this lookup above. So
16399 that PARSER->SCOPE is not considered a dependent base by
16400 lookup_member, we must enter the scope here. */
16401 if (dependent_p)
16402 pushed_scope = push_scope (parser->scope);
16403 /* If the PARSER->SCOPE is a template specialization, it
16404 may be instantiated during name lookup. In that case,
16405 errors may be issued. Even if we rollback the current
16406 tentative parse, those errors are valid. */
16407 decl = lookup_qualified_name (parser->scope, name,
16408 tag_type != none_type,
16409 /*complain=*/true);
16410 if (pushed_scope)
16411 pop_scope (pushed_scope);
16413 parser->qualifying_scope = parser->scope;
16414 parser->object_scope = NULL_TREE;
16416 else if (object_type)
16418 tree object_decl = NULL_TREE;
16419 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16420 OBJECT_TYPE is not a class. */
16421 if (CLASS_TYPE_P (object_type))
16422 /* If the OBJECT_TYPE is a template specialization, it may
16423 be instantiated during name lookup. In that case, errors
16424 may be issued. Even if we rollback the current tentative
16425 parse, those errors are valid. */
16426 object_decl = lookup_member (object_type,
16427 name,
16428 /*protect=*/0,
16429 tag_type != none_type);
16430 /* Look it up in the enclosing context, too. */
16431 decl = lookup_name_real (name, tag_type != none_type,
16432 /*nonclass=*/0,
16433 /*block_p=*/true, is_namespace, flags);
16434 parser->object_scope = object_type;
16435 parser->qualifying_scope = NULL_TREE;
16436 if (object_decl)
16437 decl = object_decl;
16439 else
16441 decl = lookup_name_real (name, tag_type != none_type,
16442 /*nonclass=*/0,
16443 /*block_p=*/true, is_namespace, flags);
16444 parser->qualifying_scope = NULL_TREE;
16445 parser->object_scope = NULL_TREE;
16448 /* If the lookup failed, let our caller know. */
16449 if (!decl || decl == error_mark_node)
16450 return error_mark_node;
16452 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16453 if (TREE_CODE (decl) == TREE_LIST)
16455 if (ambiguous_decls)
16456 *ambiguous_decls = decl;
16457 /* The error message we have to print is too complicated for
16458 cp_parser_error, so we incorporate its actions directly. */
16459 if (!cp_parser_simulate_error (parser))
16461 error ("reference to %qD is ambiguous", name);
16462 print_candidates (decl);
16464 return error_mark_node;
16467 gcc_assert (DECL_P (decl)
16468 || TREE_CODE (decl) == OVERLOAD
16469 || TREE_CODE (decl) == SCOPE_REF
16470 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16471 || BASELINK_P (decl));
16473 /* If we have resolved the name of a member declaration, check to
16474 see if the declaration is accessible. When the name resolves to
16475 set of overloaded functions, accessibility is checked when
16476 overload resolution is done.
16478 During an explicit instantiation, access is not checked at all,
16479 as per [temp.explicit]. */
16480 if (DECL_P (decl))
16481 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16483 return decl;
16486 /* Like cp_parser_lookup_name, but for use in the typical case where
16487 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16488 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16490 static tree
16491 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16493 return cp_parser_lookup_name (parser, name,
16494 none_type,
16495 /*is_template=*/false,
16496 /*is_namespace=*/false,
16497 /*check_dependency=*/true,
16498 /*ambiguous_decls=*/NULL);
16501 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16502 the current context, return the TYPE_DECL. If TAG_NAME_P is
16503 true, the DECL indicates the class being defined in a class-head,
16504 or declared in an elaborated-type-specifier.
16506 Otherwise, return DECL. */
16508 static tree
16509 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16511 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16512 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16514 struct A {
16515 template <typename T> struct B;
16518 template <typename T> struct A::B {};
16520 Similarly, in an elaborated-type-specifier:
16522 namespace N { struct X{}; }
16524 struct A {
16525 template <typename T> friend struct N::X;
16528 However, if the DECL refers to a class type, and we are in
16529 the scope of the class, then the name lookup automatically
16530 finds the TYPE_DECL created by build_self_reference rather
16531 than a TEMPLATE_DECL. For example, in:
16533 template <class T> struct S {
16534 S s;
16537 there is no need to handle such case. */
16539 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16540 return DECL_TEMPLATE_RESULT (decl);
16542 return decl;
16545 /* If too many, or too few, template-parameter lists apply to the
16546 declarator, issue an error message. Returns TRUE if all went well,
16547 and FALSE otherwise. */
16549 static bool
16550 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16551 cp_declarator *declarator)
16553 unsigned num_templates;
16555 /* We haven't seen any classes that involve template parameters yet. */
16556 num_templates = 0;
16558 switch (declarator->kind)
16560 case cdk_id:
16561 if (declarator->u.id.qualifying_scope)
16563 tree scope;
16564 tree member;
16566 scope = declarator->u.id.qualifying_scope;
16567 member = declarator->u.id.unqualified_name;
16569 while (scope && CLASS_TYPE_P (scope))
16571 /* You're supposed to have one `template <...>'
16572 for every template class, but you don't need one
16573 for a full specialization. For example:
16575 template <class T> struct S{};
16576 template <> struct S<int> { void f(); };
16577 void S<int>::f () {}
16579 is correct; there shouldn't be a `template <>' for
16580 the definition of `S<int>::f'. */
16581 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16582 /* If SCOPE does not have template information of any
16583 kind, then it is not a template, nor is it nested
16584 within a template. */
16585 break;
16586 if (explicit_class_specialization_p (scope))
16587 break;
16588 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16589 ++num_templates;
16591 scope = TYPE_CONTEXT (scope);
16594 else if (TREE_CODE (declarator->u.id.unqualified_name)
16595 == TEMPLATE_ID_EXPR)
16596 /* If the DECLARATOR has the form `X<y>' then it uses one
16597 additional level of template parameters. */
16598 ++num_templates;
16600 return cp_parser_check_template_parameters (parser,
16601 num_templates);
16603 case cdk_function:
16604 case cdk_array:
16605 case cdk_pointer:
16606 case cdk_reference:
16607 case cdk_ptrmem:
16608 return (cp_parser_check_declarator_template_parameters
16609 (parser, declarator->declarator));
16611 case cdk_error:
16612 return true;
16614 default:
16615 gcc_unreachable ();
16617 return false;
16620 /* NUM_TEMPLATES were used in the current declaration. If that is
16621 invalid, return FALSE and issue an error messages. Otherwise,
16622 return TRUE. */
16624 static bool
16625 cp_parser_check_template_parameters (cp_parser* parser,
16626 unsigned num_templates)
16628 /* If there are more template classes than parameter lists, we have
16629 something like:
16631 template <class T> void S<T>::R<T>::f (); */
16632 if (parser->num_template_parameter_lists < num_templates)
16634 error ("too few template-parameter-lists");
16635 return false;
16637 /* If there are the same number of template classes and parameter
16638 lists, that's OK. */
16639 if (parser->num_template_parameter_lists == num_templates)
16640 return true;
16641 /* If there are more, but only one more, then we are referring to a
16642 member template. That's OK too. */
16643 if (parser->num_template_parameter_lists == num_templates + 1)
16644 return true;
16645 /* Otherwise, there are too many template parameter lists. We have
16646 something like:
16648 template <class T> template <class U> void S::f(); */
16649 error ("too many template-parameter-lists");
16650 return false;
16653 /* Parse an optional `::' token indicating that the following name is
16654 from the global namespace. If so, PARSER->SCOPE is set to the
16655 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16656 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16657 Returns the new value of PARSER->SCOPE, if the `::' token is
16658 present, and NULL_TREE otherwise. */
16660 static tree
16661 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16663 cp_token *token;
16665 /* Peek at the next token. */
16666 token = cp_lexer_peek_token (parser->lexer);
16667 /* If we're looking at a `::' token then we're starting from the
16668 global namespace, not our current location. */
16669 if (token->type == CPP_SCOPE)
16671 /* Consume the `::' token. */
16672 cp_lexer_consume_token (parser->lexer);
16673 /* Set the SCOPE so that we know where to start the lookup. */
16674 parser->scope = global_namespace;
16675 parser->qualifying_scope = global_namespace;
16676 parser->object_scope = NULL_TREE;
16678 return parser->scope;
16680 else if (!current_scope_valid_p)
16682 parser->scope = NULL_TREE;
16683 parser->qualifying_scope = NULL_TREE;
16684 parser->object_scope = NULL_TREE;
16687 return NULL_TREE;
16690 /* Returns TRUE if the upcoming token sequence is the start of a
16691 constructor declarator. If FRIEND_P is true, the declarator is
16692 preceded by the `friend' specifier. */
16694 static bool
16695 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16697 bool constructor_p;
16698 tree type_decl = NULL_TREE;
16699 bool nested_name_p;
16700 cp_token *next_token;
16702 /* The common case is that this is not a constructor declarator, so
16703 try to avoid doing lots of work if at all possible. It's not
16704 valid declare a constructor at function scope. */
16705 if (parser->in_function_body)
16706 return false;
16707 /* And only certain tokens can begin a constructor declarator. */
16708 next_token = cp_lexer_peek_token (parser->lexer);
16709 if (next_token->type != CPP_NAME
16710 && next_token->type != CPP_SCOPE
16711 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16712 && next_token->type != CPP_TEMPLATE_ID)
16713 return false;
16715 /* Parse tentatively; we are going to roll back all of the tokens
16716 consumed here. */
16717 cp_parser_parse_tentatively (parser);
16718 /* Assume that we are looking at a constructor declarator. */
16719 constructor_p = true;
16721 /* Look for the optional `::' operator. */
16722 cp_parser_global_scope_opt (parser,
16723 /*current_scope_valid_p=*/false);
16724 /* Look for the nested-name-specifier. */
16725 nested_name_p
16726 = (cp_parser_nested_name_specifier_opt (parser,
16727 /*typename_keyword_p=*/false,
16728 /*check_dependency_p=*/false,
16729 /*type_p=*/false,
16730 /*is_declaration=*/false)
16731 != NULL_TREE);
16732 /* Outside of a class-specifier, there must be a
16733 nested-name-specifier. */
16734 if (!nested_name_p &&
16735 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16736 || friend_p))
16737 constructor_p = false;
16738 /* If we still think that this might be a constructor-declarator,
16739 look for a class-name. */
16740 if (constructor_p)
16742 /* If we have:
16744 template <typename T> struct S { S(); };
16745 template <typename T> S<T>::S ();
16747 we must recognize that the nested `S' names a class.
16748 Similarly, for:
16750 template <typename T> S<T>::S<T> ();
16752 we must recognize that the nested `S' names a template. */
16753 type_decl = cp_parser_class_name (parser,
16754 /*typename_keyword_p=*/false,
16755 /*template_keyword_p=*/false,
16756 none_type,
16757 /*check_dependency_p=*/false,
16758 /*class_head_p=*/false,
16759 /*is_declaration=*/false);
16760 /* If there was no class-name, then this is not a constructor. */
16761 constructor_p = !cp_parser_error_occurred (parser);
16764 /* If we're still considering a constructor, we have to see a `(',
16765 to begin the parameter-declaration-clause, followed by either a
16766 `)', an `...', or a decl-specifier. We need to check for a
16767 type-specifier to avoid being fooled into thinking that:
16769 S::S (f) (int);
16771 is a constructor. (It is actually a function named `f' that
16772 takes one parameter (of type `int') and returns a value of type
16773 `S::S'. */
16774 if (constructor_p
16775 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
16777 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16778 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16779 /* A parameter declaration begins with a decl-specifier,
16780 which is either the "attribute" keyword, a storage class
16781 specifier, or (usually) a type-specifier. */
16782 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16784 tree type;
16785 tree pushed_scope = NULL_TREE;
16786 unsigned saved_num_template_parameter_lists;
16788 /* Names appearing in the type-specifier should be looked up
16789 in the scope of the class. */
16790 if (current_class_type)
16791 type = NULL_TREE;
16792 else
16794 type = TREE_TYPE (type_decl);
16795 if (TREE_CODE (type) == TYPENAME_TYPE)
16797 type = resolve_typename_type (type,
16798 /*only_current_p=*/false);
16799 if (TREE_CODE (type) == TYPENAME_TYPE)
16801 cp_parser_abort_tentative_parse (parser);
16802 return false;
16805 pushed_scope = push_scope (type);
16808 /* Inside the constructor parameter list, surrounding
16809 template-parameter-lists do not apply. */
16810 saved_num_template_parameter_lists
16811 = parser->num_template_parameter_lists;
16812 parser->num_template_parameter_lists = 0;
16814 /* Look for the type-specifier. */
16815 cp_parser_type_specifier (parser,
16816 CP_PARSER_FLAGS_NONE,
16817 /*decl_specs=*/NULL,
16818 /*is_declarator=*/true,
16819 /*declares_class_or_enum=*/NULL,
16820 /*is_cv_qualifier=*/NULL);
16822 parser->num_template_parameter_lists
16823 = saved_num_template_parameter_lists;
16825 /* Leave the scope of the class. */
16826 if (pushed_scope)
16827 pop_scope (pushed_scope);
16829 constructor_p = !cp_parser_error_occurred (parser);
16832 else
16833 constructor_p = false;
16834 /* We did not really want to consume any tokens. */
16835 cp_parser_abort_tentative_parse (parser);
16837 return constructor_p;
16840 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16841 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16842 they must be performed once we are in the scope of the function.
16844 Returns the function defined. */
16846 static tree
16847 cp_parser_function_definition_from_specifiers_and_declarator
16848 (cp_parser* parser,
16849 cp_decl_specifier_seq *decl_specifiers,
16850 tree attributes,
16851 const cp_declarator *declarator)
16853 tree fn;
16854 bool success_p;
16856 /* Begin the function-definition. */
16857 success_p = start_function (decl_specifiers, declarator, attributes);
16859 /* The things we're about to see are not directly qualified by any
16860 template headers we've seen thus far. */
16861 reset_specialization ();
16863 /* If there were names looked up in the decl-specifier-seq that we
16864 did not check, check them now. We must wait until we are in the
16865 scope of the function to perform the checks, since the function
16866 might be a friend. */
16867 perform_deferred_access_checks ();
16869 if (!success_p)
16871 /* Skip the entire function. */
16872 cp_parser_skip_to_end_of_block_or_statement (parser);
16873 fn = error_mark_node;
16875 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16877 /* Seen already, skip it. An error message has already been output. */
16878 cp_parser_skip_to_end_of_block_or_statement (parser);
16879 fn = current_function_decl;
16880 current_function_decl = NULL_TREE;
16881 /* If this is a function from a class, pop the nested class. */
16882 if (current_class_name)
16883 pop_nested_class ();
16885 else
16886 fn = cp_parser_function_definition_after_declarator (parser,
16887 /*inline_p=*/false);
16889 return fn;
16892 /* Parse the part of a function-definition that follows the
16893 declarator. INLINE_P is TRUE iff this function is an inline
16894 function defined with a class-specifier.
16896 Returns the function defined. */
16898 static tree
16899 cp_parser_function_definition_after_declarator (cp_parser* parser,
16900 bool inline_p)
16902 tree fn;
16903 bool ctor_initializer_p = false;
16904 bool saved_in_unbraced_linkage_specification_p;
16905 bool saved_in_function_body;
16906 unsigned saved_num_template_parameter_lists;
16908 saved_in_function_body = parser->in_function_body;
16909 parser->in_function_body = true;
16910 /* If the next token is `return', then the code may be trying to
16911 make use of the "named return value" extension that G++ used to
16912 support. */
16913 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16915 /* Consume the `return' keyword. */
16916 cp_lexer_consume_token (parser->lexer);
16917 /* Look for the identifier that indicates what value is to be
16918 returned. */
16919 cp_parser_identifier (parser);
16920 /* Issue an error message. */
16921 error ("named return values are no longer supported");
16922 /* Skip tokens until we reach the start of the function body. */
16923 while (true)
16925 cp_token *token = cp_lexer_peek_token (parser->lexer);
16926 if (token->type == CPP_OPEN_BRACE
16927 || token->type == CPP_EOF
16928 || token->type == CPP_PRAGMA_EOL)
16929 break;
16930 cp_lexer_consume_token (parser->lexer);
16933 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16934 anything declared inside `f'. */
16935 saved_in_unbraced_linkage_specification_p
16936 = parser->in_unbraced_linkage_specification_p;
16937 parser->in_unbraced_linkage_specification_p = false;
16938 /* Inside the function, surrounding template-parameter-lists do not
16939 apply. */
16940 saved_num_template_parameter_lists
16941 = parser->num_template_parameter_lists;
16942 parser->num_template_parameter_lists = 0;
16943 /* If the next token is `try', then we are looking at a
16944 function-try-block. */
16945 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16946 ctor_initializer_p = cp_parser_function_try_block (parser);
16947 /* A function-try-block includes the function-body, so we only do
16948 this next part if we're not processing a function-try-block. */
16949 else
16950 ctor_initializer_p
16951 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16953 /* Finish the function. */
16954 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16955 (inline_p ? 2 : 0));
16956 /* Generate code for it, if necessary. */
16957 expand_or_defer_fn (fn);
16958 /* Restore the saved values. */
16959 parser->in_unbraced_linkage_specification_p
16960 = saved_in_unbraced_linkage_specification_p;
16961 parser->num_template_parameter_lists
16962 = saved_num_template_parameter_lists;
16963 parser->in_function_body = saved_in_function_body;
16965 return fn;
16968 /* Parse a template-declaration, assuming that the `export' (and
16969 `extern') keywords, if present, has already been scanned. MEMBER_P
16970 is as for cp_parser_template_declaration. */
16972 static void
16973 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16975 tree decl = NULL_TREE;
16976 VEC (deferred_access_check,gc) *checks;
16977 tree parameter_list;
16978 bool friend_p = false;
16979 bool need_lang_pop;
16981 /* Look for the `template' keyword. */
16982 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
16983 return;
16985 /* And the `<'. */
16986 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
16987 return;
16988 if (at_class_scope_p () && current_function_decl)
16990 /* 14.5.2.2 [temp.mem]
16992 A local class shall not have member templates. */
16993 error ("invalid declaration of member template in local class");
16994 cp_parser_skip_to_end_of_block_or_statement (parser);
16995 return;
16997 /* [temp]
16999 A template ... shall not have C linkage. */
17000 if (current_lang_name == lang_name_c)
17002 error ("template with C linkage");
17003 /* Give it C++ linkage to avoid confusing other parts of the
17004 front end. */
17005 push_lang_context (lang_name_cplusplus);
17006 need_lang_pop = true;
17008 else
17009 need_lang_pop = false;
17011 /* We cannot perform access checks on the template parameter
17012 declarations until we know what is being declared, just as we
17013 cannot check the decl-specifier list. */
17014 push_deferring_access_checks (dk_deferred);
17016 /* If the next token is `>', then we have an invalid
17017 specialization. Rather than complain about an invalid template
17018 parameter, issue an error message here. */
17019 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17021 cp_parser_error (parser, "invalid explicit specialization");
17022 begin_specialization ();
17023 parameter_list = NULL_TREE;
17025 else
17026 /* Parse the template parameters. */
17027 parameter_list = cp_parser_template_parameter_list (parser);
17029 /* Get the deferred access checks from the parameter list. These
17030 will be checked once we know what is being declared, as for a
17031 member template the checks must be performed in the scope of the
17032 class containing the member. */
17033 checks = get_deferred_access_checks ();
17035 /* Look for the `>'. */
17036 cp_parser_skip_to_end_of_template_parameter_list (parser);
17037 /* We just processed one more parameter list. */
17038 ++parser->num_template_parameter_lists;
17039 /* If the next token is `template', there are more template
17040 parameters. */
17041 if (cp_lexer_next_token_is_keyword (parser->lexer,
17042 RID_TEMPLATE))
17043 cp_parser_template_declaration_after_export (parser, member_p);
17044 else
17046 /* There are no access checks when parsing a template, as we do not
17047 know if a specialization will be a friend. */
17048 push_deferring_access_checks (dk_no_check);
17049 decl = cp_parser_single_declaration (parser,
17050 checks,
17051 member_p,
17052 /*explicit_specialization_p=*/false,
17053 &friend_p);
17054 pop_deferring_access_checks ();
17056 /* If this is a member template declaration, let the front
17057 end know. */
17058 if (member_p && !friend_p && decl)
17060 if (TREE_CODE (decl) == TYPE_DECL)
17061 cp_parser_check_access_in_redeclaration (decl);
17063 decl = finish_member_template_decl (decl);
17065 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17066 make_friend_class (current_class_type, TREE_TYPE (decl),
17067 /*complain=*/true);
17069 /* We are done with the current parameter list. */
17070 --parser->num_template_parameter_lists;
17072 pop_deferring_access_checks ();
17074 /* Finish up. */
17075 finish_template_decl (parameter_list);
17077 /* Register member declarations. */
17078 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17079 finish_member_declaration (decl);
17080 /* For the erroneous case of a template with C linkage, we pushed an
17081 implicit C++ linkage scope; exit that scope now. */
17082 if (need_lang_pop)
17083 pop_lang_context ();
17084 /* If DECL is a function template, we must return to parse it later.
17085 (Even though there is no definition, there might be default
17086 arguments that need handling.) */
17087 if (member_p && decl
17088 && (TREE_CODE (decl) == FUNCTION_DECL
17089 || DECL_FUNCTION_TEMPLATE_P (decl)))
17090 TREE_VALUE (parser->unparsed_functions_queues)
17091 = tree_cons (NULL_TREE, decl,
17092 TREE_VALUE (parser->unparsed_functions_queues));
17095 /* Perform the deferred access checks from a template-parameter-list.
17096 CHECKS is a TREE_LIST of access checks, as returned by
17097 get_deferred_access_checks. */
17099 static void
17100 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17102 ++processing_template_parmlist;
17103 perform_access_checks (checks);
17104 --processing_template_parmlist;
17107 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17108 `function-definition' sequence. MEMBER_P is true, this declaration
17109 appears in a class scope.
17111 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17112 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17114 static tree
17115 cp_parser_single_declaration (cp_parser* parser,
17116 VEC (deferred_access_check,gc)* checks,
17117 bool member_p,
17118 bool explicit_specialization_p,
17119 bool* friend_p)
17121 int declares_class_or_enum;
17122 tree decl = NULL_TREE;
17123 cp_decl_specifier_seq decl_specifiers;
17124 bool function_definition_p = false;
17126 /* This function is only used when processing a template
17127 declaration. */
17128 gcc_assert (innermost_scope_kind () == sk_template_parms
17129 || innermost_scope_kind () == sk_template_spec);
17131 /* Defer access checks until we know what is being declared. */
17132 push_deferring_access_checks (dk_deferred);
17134 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17135 alternative. */
17136 cp_parser_decl_specifier_seq (parser,
17137 CP_PARSER_FLAGS_OPTIONAL,
17138 &decl_specifiers,
17139 &declares_class_or_enum);
17140 if (friend_p)
17141 *friend_p = cp_parser_friend_p (&decl_specifiers);
17143 /* There are no template typedefs. */
17144 if (decl_specifiers.specs[(int) ds_typedef])
17146 error ("template declaration of %qs", "typedef");
17147 decl = error_mark_node;
17150 /* Gather up the access checks that occurred the
17151 decl-specifier-seq. */
17152 stop_deferring_access_checks ();
17154 /* Check for the declaration of a template class. */
17155 if (declares_class_or_enum)
17157 if (cp_parser_declares_only_class_p (parser))
17159 decl = shadow_tag (&decl_specifiers);
17161 /* In this case:
17163 struct C {
17164 friend template <typename T> struct A<T>::B;
17167 A<T>::B will be represented by a TYPENAME_TYPE, and
17168 therefore not recognized by shadow_tag. */
17169 if (friend_p && *friend_p
17170 && !decl
17171 && decl_specifiers.type
17172 && TYPE_P (decl_specifiers.type))
17173 decl = decl_specifiers.type;
17175 if (decl && decl != error_mark_node)
17176 decl = TYPE_NAME (decl);
17177 else
17178 decl = error_mark_node;
17180 /* Perform access checks for template parameters. */
17181 cp_parser_perform_template_parameter_access_checks (checks);
17184 /* If it's not a template class, try for a template function. If
17185 the next token is a `;', then this declaration does not declare
17186 anything. But, if there were errors in the decl-specifiers, then
17187 the error might well have come from an attempted class-specifier.
17188 In that case, there's no need to warn about a missing declarator. */
17189 if (!decl
17190 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17191 || decl_specifiers.type != error_mark_node))
17193 decl = cp_parser_init_declarator (parser,
17194 &decl_specifiers,
17195 checks,
17196 /*function_definition_allowed_p=*/true,
17197 member_p,
17198 declares_class_or_enum,
17199 &function_definition_p);
17201 /* 7.1.1-1 [dcl.stc]
17203 A storage-class-specifier shall not be specified in an explicit
17204 specialization... */
17205 if (decl
17206 && explicit_specialization_p
17207 && decl_specifiers.storage_class != sc_none)
17209 error ("explicit template specialization cannot have a storage class");
17210 decl = error_mark_node;
17214 pop_deferring_access_checks ();
17216 /* Clear any current qualification; whatever comes next is the start
17217 of something new. */
17218 parser->scope = NULL_TREE;
17219 parser->qualifying_scope = NULL_TREE;
17220 parser->object_scope = NULL_TREE;
17221 /* Look for a trailing `;' after the declaration. */
17222 if (!function_definition_p
17223 && (decl == error_mark_node
17224 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17225 cp_parser_skip_to_end_of_block_or_statement (parser);
17227 return decl;
17230 /* Parse a cast-expression that is not the operand of a unary "&". */
17232 static tree
17233 cp_parser_simple_cast_expression (cp_parser *parser)
17235 return cp_parser_cast_expression (parser, /*address_p=*/false,
17236 /*cast_p=*/false);
17239 /* Parse a functional cast to TYPE. Returns an expression
17240 representing the cast. */
17242 static tree
17243 cp_parser_functional_cast (cp_parser* parser, tree type)
17245 tree expression_list;
17246 tree cast;
17248 expression_list
17249 = cp_parser_parenthesized_expression_list (parser, false,
17250 /*cast_p=*/true,
17251 /*allow_expansion_p=*/true,
17252 /*non_constant_p=*/NULL);
17254 cast = build_functional_cast (type, expression_list,
17255 tf_warning_or_error);
17256 /* [expr.const]/1: In an integral constant expression "only type
17257 conversions to integral or enumeration type can be used". */
17258 if (TREE_CODE (type) == TYPE_DECL)
17259 type = TREE_TYPE (type);
17260 if (cast != error_mark_node
17261 && !cast_valid_in_integral_constant_expression_p (type)
17262 && (cp_parser_non_integral_constant_expression
17263 (parser, "a call to a constructor")))
17264 return error_mark_node;
17265 return cast;
17268 /* Save the tokens that make up the body of a member function defined
17269 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17270 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17271 specifiers applied to the declaration. Returns the FUNCTION_DECL
17272 for the member function. */
17274 static tree
17275 cp_parser_save_member_function_body (cp_parser* parser,
17276 cp_decl_specifier_seq *decl_specifiers,
17277 cp_declarator *declarator,
17278 tree attributes)
17280 cp_token *first;
17281 cp_token *last;
17282 tree fn;
17284 /* Create the function-declaration. */
17285 fn = start_method (decl_specifiers, declarator, attributes);
17286 /* If something went badly wrong, bail out now. */
17287 if (fn == error_mark_node)
17289 /* If there's a function-body, skip it. */
17290 if (cp_parser_token_starts_function_definition_p
17291 (cp_lexer_peek_token (parser->lexer)))
17292 cp_parser_skip_to_end_of_block_or_statement (parser);
17293 return error_mark_node;
17296 /* Remember it, if there default args to post process. */
17297 cp_parser_save_default_args (parser, fn);
17299 /* Save away the tokens that make up the body of the
17300 function. */
17301 first = parser->lexer->next_token;
17302 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17303 /* Handle function try blocks. */
17304 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17305 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17306 last = parser->lexer->next_token;
17308 /* Save away the inline definition; we will process it when the
17309 class is complete. */
17310 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17311 DECL_PENDING_INLINE_P (fn) = 1;
17313 /* We need to know that this was defined in the class, so that
17314 friend templates are handled correctly. */
17315 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17317 /* We're done with the inline definition. */
17318 finish_method (fn);
17320 /* Add FN to the queue of functions to be parsed later. */
17321 TREE_VALUE (parser->unparsed_functions_queues)
17322 = tree_cons (NULL_TREE, fn,
17323 TREE_VALUE (parser->unparsed_functions_queues));
17325 return fn;
17328 /* Parse a template-argument-list, as well as the trailing ">" (but
17329 not the opening ">"). See cp_parser_template_argument_list for the
17330 return value. */
17332 static tree
17333 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17335 tree arguments;
17336 tree saved_scope;
17337 tree saved_qualifying_scope;
17338 tree saved_object_scope;
17339 bool saved_greater_than_is_operator_p;
17340 bool saved_skip_evaluation;
17342 /* [temp.names]
17344 When parsing a template-id, the first non-nested `>' is taken as
17345 the end of the template-argument-list rather than a greater-than
17346 operator. */
17347 saved_greater_than_is_operator_p
17348 = parser->greater_than_is_operator_p;
17349 parser->greater_than_is_operator_p = false;
17350 /* Parsing the argument list may modify SCOPE, so we save it
17351 here. */
17352 saved_scope = parser->scope;
17353 saved_qualifying_scope = parser->qualifying_scope;
17354 saved_object_scope = parser->object_scope;
17355 /* We need to evaluate the template arguments, even though this
17356 template-id may be nested within a "sizeof". */
17357 saved_skip_evaluation = skip_evaluation;
17358 skip_evaluation = false;
17359 /* Parse the template-argument-list itself. */
17360 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17361 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17362 arguments = NULL_TREE;
17363 else
17364 arguments = cp_parser_template_argument_list (parser);
17365 /* Look for the `>' that ends the template-argument-list. If we find
17366 a '>>' instead, it's probably just a typo. */
17367 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17369 if (cxx_dialect != cxx98)
17371 /* In C++0x, a `>>' in a template argument list or cast
17372 expression is considered to be two separate `>'
17373 tokens. So, change the current token to a `>', but don't
17374 consume it: it will be consumed later when the outer
17375 template argument list (or cast expression) is parsed.
17376 Note that this replacement of `>' for `>>' is necessary
17377 even if we are parsing tentatively: in the tentative
17378 case, after calling
17379 cp_parser_enclosed_template_argument_list we will always
17380 throw away all of the template arguments and the first
17381 closing `>', either because the template argument list
17382 was erroneous or because we are replacing those tokens
17383 with a CPP_TEMPLATE_ID token. The second `>' (which will
17384 not have been thrown away) is needed either to close an
17385 outer template argument list or to complete a new-style
17386 cast. */
17387 cp_token *token = cp_lexer_peek_token (parser->lexer);
17388 token->type = CPP_GREATER;
17390 else if (!saved_greater_than_is_operator_p)
17392 /* If we're in a nested template argument list, the '>>' has
17393 to be a typo for '> >'. We emit the error message, but we
17394 continue parsing and we push a '>' as next token, so that
17395 the argument list will be parsed correctly. Note that the
17396 global source location is still on the token before the
17397 '>>', so we need to say explicitly where we want it. */
17398 cp_token *token = cp_lexer_peek_token (parser->lexer);
17399 error ("%H%<>>%> should be %<> >%> "
17400 "within a nested template argument list",
17401 &token->location);
17403 token->type = CPP_GREATER;
17405 else
17407 /* If this is not a nested template argument list, the '>>'
17408 is a typo for '>'. Emit an error message and continue.
17409 Same deal about the token location, but here we can get it
17410 right by consuming the '>>' before issuing the diagnostic. */
17411 cp_lexer_consume_token (parser->lexer);
17412 error ("spurious %<>>%>, use %<>%> to terminate "
17413 "a template argument list");
17416 else
17417 cp_parser_skip_to_end_of_template_parameter_list (parser);
17418 /* The `>' token might be a greater-than operator again now. */
17419 parser->greater_than_is_operator_p
17420 = saved_greater_than_is_operator_p;
17421 /* Restore the SAVED_SCOPE. */
17422 parser->scope = saved_scope;
17423 parser->qualifying_scope = saved_qualifying_scope;
17424 parser->object_scope = saved_object_scope;
17425 skip_evaluation = saved_skip_evaluation;
17427 return arguments;
17430 /* MEMBER_FUNCTION is a member function, or a friend. If default
17431 arguments, or the body of the function have not yet been parsed,
17432 parse them now. */
17434 static void
17435 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17437 /* If this member is a template, get the underlying
17438 FUNCTION_DECL. */
17439 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17440 member_function = DECL_TEMPLATE_RESULT (member_function);
17442 /* There should not be any class definitions in progress at this
17443 point; the bodies of members are only parsed outside of all class
17444 definitions. */
17445 gcc_assert (parser->num_classes_being_defined == 0);
17446 /* While we're parsing the member functions we might encounter more
17447 classes. We want to handle them right away, but we don't want
17448 them getting mixed up with functions that are currently in the
17449 queue. */
17450 parser->unparsed_functions_queues
17451 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17453 /* Make sure that any template parameters are in scope. */
17454 maybe_begin_member_template_processing (member_function);
17456 /* If the body of the function has not yet been parsed, parse it
17457 now. */
17458 if (DECL_PENDING_INLINE_P (member_function))
17460 tree function_scope;
17461 cp_token_cache *tokens;
17463 /* The function is no longer pending; we are processing it. */
17464 tokens = DECL_PENDING_INLINE_INFO (member_function);
17465 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17466 DECL_PENDING_INLINE_P (member_function) = 0;
17468 /* If this is a local class, enter the scope of the containing
17469 function. */
17470 function_scope = current_function_decl;
17471 if (function_scope)
17472 push_function_context ();
17474 /* Push the body of the function onto the lexer stack. */
17475 cp_parser_push_lexer_for_tokens (parser, tokens);
17477 /* Let the front end know that we going to be defining this
17478 function. */
17479 start_preparsed_function (member_function, NULL_TREE,
17480 SF_PRE_PARSED | SF_INCLASS_INLINE);
17482 /* Don't do access checking if it is a templated function. */
17483 if (processing_template_decl)
17484 push_deferring_access_checks (dk_no_check);
17486 /* Now, parse the body of the function. */
17487 cp_parser_function_definition_after_declarator (parser,
17488 /*inline_p=*/true);
17490 if (processing_template_decl)
17491 pop_deferring_access_checks ();
17493 /* Leave the scope of the containing function. */
17494 if (function_scope)
17495 pop_function_context ();
17496 cp_parser_pop_lexer (parser);
17499 /* Remove any template parameters from the symbol table. */
17500 maybe_end_member_template_processing ();
17502 /* Restore the queue. */
17503 parser->unparsed_functions_queues
17504 = TREE_CHAIN (parser->unparsed_functions_queues);
17507 /* If DECL contains any default args, remember it on the unparsed
17508 functions queue. */
17510 static void
17511 cp_parser_save_default_args (cp_parser* parser, tree decl)
17513 tree probe;
17515 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17516 probe;
17517 probe = TREE_CHAIN (probe))
17518 if (TREE_PURPOSE (probe))
17520 TREE_PURPOSE (parser->unparsed_functions_queues)
17521 = tree_cons (current_class_type, decl,
17522 TREE_PURPOSE (parser->unparsed_functions_queues));
17523 break;
17527 /* FN is a FUNCTION_DECL which may contains a parameter with an
17528 unparsed DEFAULT_ARG. Parse the default args now. This function
17529 assumes that the current scope is the scope in which the default
17530 argument should be processed. */
17532 static void
17533 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17535 bool saved_local_variables_forbidden_p;
17536 tree parm;
17538 /* While we're parsing the default args, we might (due to the
17539 statement expression extension) encounter more classes. We want
17540 to handle them right away, but we don't want them getting mixed
17541 up with default args that are currently in the queue. */
17542 parser->unparsed_functions_queues
17543 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17545 /* Local variable names (and the `this' keyword) may not appear
17546 in a default argument. */
17547 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17548 parser->local_variables_forbidden_p = true;
17550 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17551 parm;
17552 parm = TREE_CHAIN (parm))
17554 cp_token_cache *tokens;
17555 tree default_arg = TREE_PURPOSE (parm);
17556 tree parsed_arg;
17557 VEC(tree,gc) *insts;
17558 tree copy;
17559 unsigned ix;
17561 if (!default_arg)
17562 continue;
17564 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17565 /* This can happen for a friend declaration for a function
17566 already declared with default arguments. */
17567 continue;
17569 /* Push the saved tokens for the default argument onto the parser's
17570 lexer stack. */
17571 tokens = DEFARG_TOKENS (default_arg);
17572 cp_parser_push_lexer_for_tokens (parser, tokens);
17574 /* Parse the assignment-expression. */
17575 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17577 if (!processing_template_decl)
17578 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17580 TREE_PURPOSE (parm) = parsed_arg;
17582 /* Update any instantiations we've already created. */
17583 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17584 VEC_iterate (tree, insts, ix, copy); ix++)
17585 TREE_PURPOSE (copy) = parsed_arg;
17587 /* If the token stream has not been completely used up, then
17588 there was extra junk after the end of the default
17589 argument. */
17590 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17591 cp_parser_error (parser, "expected %<,%>");
17593 /* Revert to the main lexer. */
17594 cp_parser_pop_lexer (parser);
17597 /* Make sure no default arg is missing. */
17598 check_default_args (fn);
17600 /* Restore the state of local_variables_forbidden_p. */
17601 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17603 /* Restore the queue. */
17604 parser->unparsed_functions_queues
17605 = TREE_CHAIN (parser->unparsed_functions_queues);
17608 /* Parse the operand of `sizeof' (or a similar operator). Returns
17609 either a TYPE or an expression, depending on the form of the
17610 input. The KEYWORD indicates which kind of expression we have
17611 encountered. */
17613 static tree
17614 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17616 tree expr = NULL_TREE;
17617 const char *saved_message;
17618 char *tmp;
17619 bool saved_integral_constant_expression_p;
17620 bool saved_non_integral_constant_expression_p;
17621 bool pack_expansion_p = false;
17623 /* Types cannot be defined in a `sizeof' expression. Save away the
17624 old message. */
17625 saved_message = parser->type_definition_forbidden_message;
17626 /* And create the new one. */
17627 tmp = concat ("types may not be defined in %<",
17628 IDENTIFIER_POINTER (ridpointers[keyword]),
17629 "%> expressions", NULL);
17630 parser->type_definition_forbidden_message = tmp;
17632 /* The restrictions on constant-expressions do not apply inside
17633 sizeof expressions. */
17634 saved_integral_constant_expression_p
17635 = parser->integral_constant_expression_p;
17636 saved_non_integral_constant_expression_p
17637 = parser->non_integral_constant_expression_p;
17638 parser->integral_constant_expression_p = false;
17640 /* If it's a `...', then we are computing the length of a parameter
17641 pack. */
17642 if (keyword == RID_SIZEOF
17643 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17645 /* Consume the `...'. */
17646 cp_lexer_consume_token (parser->lexer);
17647 maybe_warn_variadic_templates ();
17649 /* Note that this is an expansion. */
17650 pack_expansion_p = true;
17653 /* Do not actually evaluate the expression. */
17654 ++skip_evaluation;
17655 /* If it's a `(', then we might be looking at the type-id
17656 construction. */
17657 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17659 tree type;
17660 bool saved_in_type_id_in_expr_p;
17662 /* We can't be sure yet whether we're looking at a type-id or an
17663 expression. */
17664 cp_parser_parse_tentatively (parser);
17665 /* Consume the `('. */
17666 cp_lexer_consume_token (parser->lexer);
17667 /* Parse the type-id. */
17668 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17669 parser->in_type_id_in_expr_p = true;
17670 type = cp_parser_type_id (parser);
17671 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17672 /* Now, look for the trailing `)'. */
17673 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17674 /* If all went well, then we're done. */
17675 if (cp_parser_parse_definitely (parser))
17677 cp_decl_specifier_seq decl_specs;
17679 /* Build a trivial decl-specifier-seq. */
17680 clear_decl_specs (&decl_specs);
17681 decl_specs.type = type;
17683 /* Call grokdeclarator to figure out what type this is. */
17684 expr = grokdeclarator (NULL,
17685 &decl_specs,
17686 TYPENAME,
17687 /*initialized=*/0,
17688 /*attrlist=*/NULL);
17692 /* If the type-id production did not work out, then we must be
17693 looking at the unary-expression production. */
17694 if (!expr)
17695 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17696 /*cast_p=*/false);
17698 if (pack_expansion_p)
17699 /* Build a pack expansion. */
17700 expr = make_pack_expansion (expr);
17702 /* Go back to evaluating expressions. */
17703 --skip_evaluation;
17705 /* Free the message we created. */
17706 free (tmp);
17707 /* And restore the old one. */
17708 parser->type_definition_forbidden_message = saved_message;
17709 parser->integral_constant_expression_p
17710 = saved_integral_constant_expression_p;
17711 parser->non_integral_constant_expression_p
17712 = saved_non_integral_constant_expression_p;
17714 return expr;
17717 /* If the current declaration has no declarator, return true. */
17719 static bool
17720 cp_parser_declares_only_class_p (cp_parser *parser)
17722 /* If the next token is a `;' or a `,' then there is no
17723 declarator. */
17724 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17725 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17728 /* Update the DECL_SPECS to reflect the storage class indicated by
17729 KEYWORD. */
17731 static void
17732 cp_parser_set_storage_class (cp_parser *parser,
17733 cp_decl_specifier_seq *decl_specs,
17734 enum rid keyword)
17736 cp_storage_class storage_class;
17738 if (parser->in_unbraced_linkage_specification_p)
17740 error ("invalid use of %qD in linkage specification",
17741 ridpointers[keyword]);
17742 return;
17744 else if (decl_specs->storage_class != sc_none)
17746 decl_specs->conflicting_specifiers_p = true;
17747 return;
17750 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17751 && decl_specs->specs[(int) ds_thread])
17753 error ("%<__thread%> before %qD", ridpointers[keyword]);
17754 decl_specs->specs[(int) ds_thread] = 0;
17757 switch (keyword)
17759 case RID_AUTO:
17760 storage_class = sc_auto;
17761 break;
17762 case RID_REGISTER:
17763 storage_class = sc_register;
17764 break;
17765 case RID_STATIC:
17766 storage_class = sc_static;
17767 break;
17768 case RID_EXTERN:
17769 storage_class = sc_extern;
17770 break;
17771 case RID_MUTABLE:
17772 storage_class = sc_mutable;
17773 break;
17774 default:
17775 gcc_unreachable ();
17777 decl_specs->storage_class = storage_class;
17779 /* A storage class specifier cannot be applied alongside a typedef
17780 specifier. If there is a typedef specifier present then set
17781 conflicting_specifiers_p which will trigger an error later
17782 on in grokdeclarator. */
17783 if (decl_specs->specs[(int)ds_typedef])
17784 decl_specs->conflicting_specifiers_p = true;
17787 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17788 is true, the type is a user-defined type; otherwise it is a
17789 built-in type specified by a keyword. */
17791 static void
17792 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17793 tree type_spec,
17794 bool user_defined_p)
17796 decl_specs->any_specifiers_p = true;
17798 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
17799 (with, for example, in "typedef int wchar_t;") we remember that
17800 this is what happened. In system headers, we ignore these
17801 declarations so that G++ can work with system headers that are not
17802 C++-safe. */
17803 if (decl_specs->specs[(int) ds_typedef]
17804 && !user_defined_p
17805 && (type_spec == boolean_type_node
17806 || type_spec == char16_type_node
17807 || type_spec == char32_type_node
17808 || type_spec == wchar_type_node)
17809 && (decl_specs->type
17810 || decl_specs->specs[(int) ds_long]
17811 || decl_specs->specs[(int) ds_short]
17812 || decl_specs->specs[(int) ds_unsigned]
17813 || decl_specs->specs[(int) ds_signed]))
17815 decl_specs->redefined_builtin_type = type_spec;
17816 if (!decl_specs->type)
17818 decl_specs->type = type_spec;
17819 decl_specs->user_defined_type_p = false;
17822 else if (decl_specs->type)
17823 decl_specs->multiple_types_p = true;
17824 else
17826 decl_specs->type = type_spec;
17827 decl_specs->user_defined_type_p = user_defined_p;
17828 decl_specs->redefined_builtin_type = NULL_TREE;
17832 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17833 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17835 static bool
17836 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17838 return decl_specifiers->specs[(int) ds_friend] != 0;
17841 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17842 issue an error message indicating that TOKEN_DESC was expected.
17844 Returns the token consumed, if the token had the appropriate type.
17845 Otherwise, returns NULL. */
17847 static cp_token *
17848 cp_parser_require (cp_parser* parser,
17849 enum cpp_ttype type,
17850 const char* token_desc)
17852 if (cp_lexer_next_token_is (parser->lexer, type))
17853 return cp_lexer_consume_token (parser->lexer);
17854 else
17856 /* Output the MESSAGE -- unless we're parsing tentatively. */
17857 if (!cp_parser_simulate_error (parser))
17859 char *message = concat ("expected ", token_desc, NULL);
17860 cp_parser_error (parser, message);
17861 free (message);
17863 return NULL;
17867 /* An error message is produced if the next token is not '>'.
17868 All further tokens are skipped until the desired token is
17869 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17871 static void
17872 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17874 /* Current level of '< ... >'. */
17875 unsigned level = 0;
17876 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17877 unsigned nesting_depth = 0;
17879 /* Are we ready, yet? If not, issue error message. */
17880 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17881 return;
17883 /* Skip tokens until the desired token is found. */
17884 while (true)
17886 /* Peek at the next token. */
17887 switch (cp_lexer_peek_token (parser->lexer)->type)
17889 case CPP_LESS:
17890 if (!nesting_depth)
17891 ++level;
17892 break;
17894 case CPP_RSHIFT:
17895 if (cxx_dialect == cxx98)
17896 /* C++0x views the `>>' operator as two `>' tokens, but
17897 C++98 does not. */
17898 break;
17899 else if (!nesting_depth && level-- == 0)
17901 /* We've hit a `>>' where the first `>' closes the
17902 template argument list, and the second `>' is
17903 spurious. Just consume the `>>' and stop; we've
17904 already produced at least one error. */
17905 cp_lexer_consume_token (parser->lexer);
17906 return;
17908 /* Fall through for C++0x, so we handle the second `>' in
17909 the `>>'. */
17911 case CPP_GREATER:
17912 if (!nesting_depth && level-- == 0)
17914 /* We've reached the token we want, consume it and stop. */
17915 cp_lexer_consume_token (parser->lexer);
17916 return;
17918 break;
17920 case CPP_OPEN_PAREN:
17921 case CPP_OPEN_SQUARE:
17922 ++nesting_depth;
17923 break;
17925 case CPP_CLOSE_PAREN:
17926 case CPP_CLOSE_SQUARE:
17927 if (nesting_depth-- == 0)
17928 return;
17929 break;
17931 case CPP_EOF:
17932 case CPP_PRAGMA_EOL:
17933 case CPP_SEMICOLON:
17934 case CPP_OPEN_BRACE:
17935 case CPP_CLOSE_BRACE:
17936 /* The '>' was probably forgotten, don't look further. */
17937 return;
17939 default:
17940 break;
17943 /* Consume this token. */
17944 cp_lexer_consume_token (parser->lexer);
17948 /* If the next token is the indicated keyword, consume it. Otherwise,
17949 issue an error message indicating that TOKEN_DESC was expected.
17951 Returns the token consumed, if the token had the appropriate type.
17952 Otherwise, returns NULL. */
17954 static cp_token *
17955 cp_parser_require_keyword (cp_parser* parser,
17956 enum rid keyword,
17957 const char* token_desc)
17959 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17961 if (token && token->keyword != keyword)
17963 dyn_string_t error_msg;
17965 /* Format the error message. */
17966 error_msg = dyn_string_new (0);
17967 dyn_string_append_cstr (error_msg, "expected ");
17968 dyn_string_append_cstr (error_msg, token_desc);
17969 cp_parser_error (parser, error_msg->s);
17970 dyn_string_delete (error_msg);
17971 return NULL;
17974 return token;
17977 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17978 function-definition. */
17980 static bool
17981 cp_parser_token_starts_function_definition_p (cp_token* token)
17983 return (/* An ordinary function-body begins with an `{'. */
17984 token->type == CPP_OPEN_BRACE
17985 /* A ctor-initializer begins with a `:'. */
17986 || token->type == CPP_COLON
17987 /* A function-try-block begins with `try'. */
17988 || token->keyword == RID_TRY
17989 /* The named return value extension begins with `return'. */
17990 || token->keyword == RID_RETURN);
17993 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17994 definition. */
17996 static bool
17997 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17999 cp_token *token;
18001 token = cp_lexer_peek_token (parser->lexer);
18002 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18005 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18006 C++0x) ending a template-argument. */
18008 static bool
18009 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18011 cp_token *token;
18013 token = cp_lexer_peek_token (parser->lexer);
18014 return (token->type == CPP_COMMA
18015 || token->type == CPP_GREATER
18016 || token->type == CPP_ELLIPSIS
18017 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18020 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18021 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18023 static bool
18024 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18025 size_t n)
18027 cp_token *token;
18029 token = cp_lexer_peek_nth_token (parser->lexer, n);
18030 if (token->type == CPP_LESS)
18031 return true;
18032 /* Check for the sequence `<::' in the original code. It would be lexed as
18033 `[:', where `[' is a digraph, and there is no whitespace before
18034 `:'. */
18035 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18037 cp_token *token2;
18038 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18039 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18040 return true;
18042 return false;
18045 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18046 or none_type otherwise. */
18048 static enum tag_types
18049 cp_parser_token_is_class_key (cp_token* token)
18051 switch (token->keyword)
18053 case RID_CLASS:
18054 return class_type;
18055 case RID_STRUCT:
18056 return record_type;
18057 case RID_UNION:
18058 return union_type;
18060 default:
18061 return none_type;
18065 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18067 static void
18068 cp_parser_check_class_key (enum tag_types class_key, tree type)
18070 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18071 pedwarn ("%qs tag used in naming %q#T",
18072 class_key == union_type ? "union"
18073 : class_key == record_type ? "struct" : "class",
18074 type);
18077 /* Issue an error message if DECL is redeclared with different
18078 access than its original declaration [class.access.spec/3].
18079 This applies to nested classes and nested class templates.
18080 [class.mem/1]. */
18082 static void
18083 cp_parser_check_access_in_redeclaration (tree decl)
18085 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18086 return;
18088 if ((TREE_PRIVATE (decl)
18089 != (current_access_specifier == access_private_node))
18090 || (TREE_PROTECTED (decl)
18091 != (current_access_specifier == access_protected_node)))
18092 error ("%qD redeclared with different access", decl);
18095 /* Look for the `template' keyword, as a syntactic disambiguator.
18096 Return TRUE iff it is present, in which case it will be
18097 consumed. */
18099 static bool
18100 cp_parser_optional_template_keyword (cp_parser *parser)
18102 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18104 /* The `template' keyword can only be used within templates;
18105 outside templates the parser can always figure out what is a
18106 template and what is not. */
18107 if (!processing_template_decl)
18109 error ("%<template%> (as a disambiguator) is only allowed "
18110 "within templates");
18111 /* If this part of the token stream is rescanned, the same
18112 error message would be generated. So, we purge the token
18113 from the stream. */
18114 cp_lexer_purge_token (parser->lexer);
18115 return false;
18117 else
18119 /* Consume the `template' keyword. */
18120 cp_lexer_consume_token (parser->lexer);
18121 return true;
18125 return false;
18128 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18129 set PARSER->SCOPE, and perform other related actions. */
18131 static void
18132 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18134 int i;
18135 struct tree_check *check_value;
18136 deferred_access_check *chk;
18137 VEC (deferred_access_check,gc) *checks;
18139 /* Get the stored value. */
18140 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18141 /* Perform any access checks that were deferred. */
18142 checks = check_value->checks;
18143 if (checks)
18145 for (i = 0 ;
18146 VEC_iterate (deferred_access_check, checks, i, chk) ;
18147 ++i)
18149 perform_or_defer_access_check (chk->binfo,
18150 chk->decl,
18151 chk->diag_decl);
18154 /* Set the scope from the stored value. */
18155 parser->scope = check_value->value;
18156 parser->qualifying_scope = check_value->qualifying_scope;
18157 parser->object_scope = NULL_TREE;
18160 /* Consume tokens up through a non-nested END token. */
18162 static void
18163 cp_parser_cache_group (cp_parser *parser,
18164 enum cpp_ttype end,
18165 unsigned depth)
18167 while (true)
18169 cp_token *token;
18171 /* Abort a parenthesized expression if we encounter a brace. */
18172 if ((end == CPP_CLOSE_PAREN || depth == 0)
18173 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18174 return;
18175 /* If we've reached the end of the file, stop. */
18176 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
18177 || (end != CPP_PRAGMA_EOL
18178 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
18179 return;
18180 /* Consume the next token. */
18181 token = cp_lexer_consume_token (parser->lexer);
18182 /* See if it starts a new group. */
18183 if (token->type == CPP_OPEN_BRACE)
18185 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18186 if (depth == 0)
18187 return;
18189 else if (token->type == CPP_OPEN_PAREN)
18190 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18191 else if (token->type == CPP_PRAGMA)
18192 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18193 else if (token->type == end)
18194 return;
18198 /* Begin parsing tentatively. We always save tokens while parsing
18199 tentatively so that if the tentative parsing fails we can restore the
18200 tokens. */
18202 static void
18203 cp_parser_parse_tentatively (cp_parser* parser)
18205 /* Enter a new parsing context. */
18206 parser->context = cp_parser_context_new (parser->context);
18207 /* Begin saving tokens. */
18208 cp_lexer_save_tokens (parser->lexer);
18209 /* In order to avoid repetitive access control error messages,
18210 access checks are queued up until we are no longer parsing
18211 tentatively. */
18212 push_deferring_access_checks (dk_deferred);
18215 /* Commit to the currently active tentative parse. */
18217 static void
18218 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18220 cp_parser_context *context;
18221 cp_lexer *lexer;
18223 /* Mark all of the levels as committed. */
18224 lexer = parser->lexer;
18225 for (context = parser->context; context->next; context = context->next)
18227 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18228 break;
18229 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18230 while (!cp_lexer_saving_tokens (lexer))
18231 lexer = lexer->next;
18232 cp_lexer_commit_tokens (lexer);
18236 /* Abort the currently active tentative parse. All consumed tokens
18237 will be rolled back, and no diagnostics will be issued. */
18239 static void
18240 cp_parser_abort_tentative_parse (cp_parser* parser)
18242 cp_parser_simulate_error (parser);
18243 /* Now, pretend that we want to see if the construct was
18244 successfully parsed. */
18245 cp_parser_parse_definitely (parser);
18248 /* Stop parsing tentatively. If a parse error has occurred, restore the
18249 token stream. Otherwise, commit to the tokens we have consumed.
18250 Returns true if no error occurred; false otherwise. */
18252 static bool
18253 cp_parser_parse_definitely (cp_parser* parser)
18255 bool error_occurred;
18256 cp_parser_context *context;
18258 /* Remember whether or not an error occurred, since we are about to
18259 destroy that information. */
18260 error_occurred = cp_parser_error_occurred (parser);
18261 /* Remove the topmost context from the stack. */
18262 context = parser->context;
18263 parser->context = context->next;
18264 /* If no parse errors occurred, commit to the tentative parse. */
18265 if (!error_occurred)
18267 /* Commit to the tokens read tentatively, unless that was
18268 already done. */
18269 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18270 cp_lexer_commit_tokens (parser->lexer);
18272 pop_to_parent_deferring_access_checks ();
18274 /* Otherwise, if errors occurred, roll back our state so that things
18275 are just as they were before we began the tentative parse. */
18276 else
18278 cp_lexer_rollback_tokens (parser->lexer);
18279 pop_deferring_access_checks ();
18281 /* Add the context to the front of the free list. */
18282 context->next = cp_parser_context_free_list;
18283 cp_parser_context_free_list = context;
18285 return !error_occurred;
18288 /* Returns true if we are parsing tentatively and are not committed to
18289 this tentative parse. */
18291 static bool
18292 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18294 return (cp_parser_parsing_tentatively (parser)
18295 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18298 /* Returns nonzero iff an error has occurred during the most recent
18299 tentative parse. */
18301 static bool
18302 cp_parser_error_occurred (cp_parser* parser)
18304 return (cp_parser_parsing_tentatively (parser)
18305 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18308 /* Returns nonzero if GNU extensions are allowed. */
18310 static bool
18311 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18313 return parser->allow_gnu_extensions_p;
18316 /* Objective-C++ Productions */
18319 /* Parse an Objective-C expression, which feeds into a primary-expression
18320 above.
18322 objc-expression:
18323 objc-message-expression
18324 objc-string-literal
18325 objc-encode-expression
18326 objc-protocol-expression
18327 objc-selector-expression
18329 Returns a tree representation of the expression. */
18331 static tree
18332 cp_parser_objc_expression (cp_parser* parser)
18334 /* Try to figure out what kind of declaration is present. */
18335 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18337 switch (kwd->type)
18339 case CPP_OPEN_SQUARE:
18340 return cp_parser_objc_message_expression (parser);
18342 case CPP_OBJC_STRING:
18343 kwd = cp_lexer_consume_token (parser->lexer);
18344 return objc_build_string_object (kwd->u.value);
18346 case CPP_KEYWORD:
18347 switch (kwd->keyword)
18349 case RID_AT_ENCODE:
18350 return cp_parser_objc_encode_expression (parser);
18352 case RID_AT_PROTOCOL:
18353 return cp_parser_objc_protocol_expression (parser);
18355 case RID_AT_SELECTOR:
18356 return cp_parser_objc_selector_expression (parser);
18358 default:
18359 break;
18361 default:
18362 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18363 cp_parser_skip_to_end_of_block_or_statement (parser);
18366 return error_mark_node;
18369 /* Parse an Objective-C message expression.
18371 objc-message-expression:
18372 [ objc-message-receiver objc-message-args ]
18374 Returns a representation of an Objective-C message. */
18376 static tree
18377 cp_parser_objc_message_expression (cp_parser* parser)
18379 tree receiver, messageargs;
18381 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18382 receiver = cp_parser_objc_message_receiver (parser);
18383 messageargs = cp_parser_objc_message_args (parser);
18384 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18386 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18389 /* Parse an objc-message-receiver.
18391 objc-message-receiver:
18392 expression
18393 simple-type-specifier
18395 Returns a representation of the type or expression. */
18397 static tree
18398 cp_parser_objc_message_receiver (cp_parser* parser)
18400 tree rcv;
18402 /* An Objective-C message receiver may be either (1) a type
18403 or (2) an expression. */
18404 cp_parser_parse_tentatively (parser);
18405 rcv = cp_parser_expression (parser, false);
18407 if (cp_parser_parse_definitely (parser))
18408 return rcv;
18410 rcv = cp_parser_simple_type_specifier (parser,
18411 /*decl_specs=*/NULL,
18412 CP_PARSER_FLAGS_NONE);
18414 return objc_get_class_reference (rcv);
18417 /* Parse the arguments and selectors comprising an Objective-C message.
18419 objc-message-args:
18420 objc-selector
18421 objc-selector-args
18422 objc-selector-args , objc-comma-args
18424 objc-selector-args:
18425 objc-selector [opt] : assignment-expression
18426 objc-selector-args objc-selector [opt] : assignment-expression
18428 objc-comma-args:
18429 assignment-expression
18430 objc-comma-args , assignment-expression
18432 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18433 selector arguments and TREE_VALUE containing a list of comma
18434 arguments. */
18436 static tree
18437 cp_parser_objc_message_args (cp_parser* parser)
18439 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18440 bool maybe_unary_selector_p = true;
18441 cp_token *token = cp_lexer_peek_token (parser->lexer);
18443 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18445 tree selector = NULL_TREE, arg;
18447 if (token->type != CPP_COLON)
18448 selector = cp_parser_objc_selector (parser);
18450 /* Detect if we have a unary selector. */
18451 if (maybe_unary_selector_p
18452 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18453 return build_tree_list (selector, NULL_TREE);
18455 maybe_unary_selector_p = false;
18456 cp_parser_require (parser, CPP_COLON, "%<:%>");
18457 arg = cp_parser_assignment_expression (parser, false);
18459 sel_args
18460 = chainon (sel_args,
18461 build_tree_list (selector, arg));
18463 token = cp_lexer_peek_token (parser->lexer);
18466 /* Handle non-selector arguments, if any. */
18467 while (token->type == CPP_COMMA)
18469 tree arg;
18471 cp_lexer_consume_token (parser->lexer);
18472 arg = cp_parser_assignment_expression (parser, false);
18474 addl_args
18475 = chainon (addl_args,
18476 build_tree_list (NULL_TREE, arg));
18478 token = cp_lexer_peek_token (parser->lexer);
18481 return build_tree_list (sel_args, addl_args);
18484 /* Parse an Objective-C encode expression.
18486 objc-encode-expression:
18487 @encode objc-typename
18489 Returns an encoded representation of the type argument. */
18491 static tree
18492 cp_parser_objc_encode_expression (cp_parser* parser)
18494 tree type;
18496 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
18497 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18498 type = complete_type (cp_parser_type_id (parser));
18499 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18501 if (!type)
18503 error ("%<@encode%> must specify a type as an argument");
18504 return error_mark_node;
18507 return objc_build_encode_expr (type);
18510 /* Parse an Objective-C @defs expression. */
18512 static tree
18513 cp_parser_objc_defs_expression (cp_parser *parser)
18515 tree name;
18517 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18518 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18519 name = cp_parser_identifier (parser);
18520 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18522 return objc_get_class_ivars (name);
18525 /* Parse an Objective-C protocol expression.
18527 objc-protocol-expression:
18528 @protocol ( identifier )
18530 Returns a representation of the protocol expression. */
18532 static tree
18533 cp_parser_objc_protocol_expression (cp_parser* parser)
18535 tree proto;
18537 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18538 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18539 proto = cp_parser_identifier (parser);
18540 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18542 return objc_build_protocol_expr (proto);
18545 /* Parse an Objective-C selector expression.
18547 objc-selector-expression:
18548 @selector ( objc-method-signature )
18550 objc-method-signature:
18551 objc-selector
18552 objc-selector-seq
18554 objc-selector-seq:
18555 objc-selector :
18556 objc-selector-seq objc-selector :
18558 Returns a representation of the method selector. */
18560 static tree
18561 cp_parser_objc_selector_expression (cp_parser* parser)
18563 tree sel_seq = NULL_TREE;
18564 bool maybe_unary_selector_p = true;
18565 cp_token *token;
18567 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18568 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18569 token = cp_lexer_peek_token (parser->lexer);
18571 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18572 || token->type == CPP_SCOPE)
18574 tree selector = NULL_TREE;
18576 if (token->type != CPP_COLON
18577 || token->type == CPP_SCOPE)
18578 selector = cp_parser_objc_selector (parser);
18580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18581 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18583 /* Detect if we have a unary selector. */
18584 if (maybe_unary_selector_p)
18586 sel_seq = selector;
18587 goto finish_selector;
18589 else
18591 cp_parser_error (parser, "expected %<:%>");
18594 maybe_unary_selector_p = false;
18595 token = cp_lexer_consume_token (parser->lexer);
18597 if (token->type == CPP_SCOPE)
18599 sel_seq
18600 = chainon (sel_seq,
18601 build_tree_list (selector, NULL_TREE));
18602 sel_seq
18603 = chainon (sel_seq,
18604 build_tree_list (NULL_TREE, NULL_TREE));
18606 else
18607 sel_seq
18608 = chainon (sel_seq,
18609 build_tree_list (selector, NULL_TREE));
18611 token = cp_lexer_peek_token (parser->lexer);
18614 finish_selector:
18615 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18617 return objc_build_selector_expr (sel_seq);
18620 /* Parse a list of identifiers.
18622 objc-identifier-list:
18623 identifier
18624 objc-identifier-list , identifier
18626 Returns a TREE_LIST of identifier nodes. */
18628 static tree
18629 cp_parser_objc_identifier_list (cp_parser* parser)
18631 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18632 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18634 while (sep->type == CPP_COMMA)
18636 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18637 list = chainon (list,
18638 build_tree_list (NULL_TREE,
18639 cp_parser_identifier (parser)));
18640 sep = cp_lexer_peek_token (parser->lexer);
18643 return list;
18646 /* Parse an Objective-C alias declaration.
18648 objc-alias-declaration:
18649 @compatibility_alias identifier identifier ;
18651 This function registers the alias mapping with the Objective-C front end.
18652 It returns nothing. */
18654 static void
18655 cp_parser_objc_alias_declaration (cp_parser* parser)
18657 tree alias, orig;
18659 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18660 alias = cp_parser_identifier (parser);
18661 orig = cp_parser_identifier (parser);
18662 objc_declare_alias (alias, orig);
18663 cp_parser_consume_semicolon_at_end_of_statement (parser);
18666 /* Parse an Objective-C class forward-declaration.
18668 objc-class-declaration:
18669 @class objc-identifier-list ;
18671 The function registers the forward declarations with the Objective-C
18672 front end. It returns nothing. */
18674 static void
18675 cp_parser_objc_class_declaration (cp_parser* parser)
18677 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18678 objc_declare_class (cp_parser_objc_identifier_list (parser));
18679 cp_parser_consume_semicolon_at_end_of_statement (parser);
18682 /* Parse a list of Objective-C protocol references.
18684 objc-protocol-refs-opt:
18685 objc-protocol-refs [opt]
18687 objc-protocol-refs:
18688 < objc-identifier-list >
18690 Returns a TREE_LIST of identifiers, if any. */
18692 static tree
18693 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18695 tree protorefs = NULL_TREE;
18697 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18699 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18700 protorefs = cp_parser_objc_identifier_list (parser);
18701 cp_parser_require (parser, CPP_GREATER, "%<>%>");
18704 return protorefs;
18707 /* Parse a Objective-C visibility specification. */
18709 static void
18710 cp_parser_objc_visibility_spec (cp_parser* parser)
18712 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18714 switch (vis->keyword)
18716 case RID_AT_PRIVATE:
18717 objc_set_visibility (2);
18718 break;
18719 case RID_AT_PROTECTED:
18720 objc_set_visibility (0);
18721 break;
18722 case RID_AT_PUBLIC:
18723 objc_set_visibility (1);
18724 break;
18725 default:
18726 return;
18729 /* Eat '@private'/'@protected'/'@public'. */
18730 cp_lexer_consume_token (parser->lexer);
18733 /* Parse an Objective-C method type. */
18735 static void
18736 cp_parser_objc_method_type (cp_parser* parser)
18738 objc_set_method_type
18739 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18740 ? PLUS_EXPR
18741 : MINUS_EXPR);
18744 /* Parse an Objective-C protocol qualifier. */
18746 static tree
18747 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18749 tree quals = NULL_TREE, node;
18750 cp_token *token = cp_lexer_peek_token (parser->lexer);
18752 node = token->u.value;
18754 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18755 && (node == ridpointers [(int) RID_IN]
18756 || node == ridpointers [(int) RID_OUT]
18757 || node == ridpointers [(int) RID_INOUT]
18758 || node == ridpointers [(int) RID_BYCOPY]
18759 || node == ridpointers [(int) RID_BYREF]
18760 || node == ridpointers [(int) RID_ONEWAY]))
18762 quals = tree_cons (NULL_TREE, node, quals);
18763 cp_lexer_consume_token (parser->lexer);
18764 token = cp_lexer_peek_token (parser->lexer);
18765 node = token->u.value;
18768 return quals;
18771 /* Parse an Objective-C typename. */
18773 static tree
18774 cp_parser_objc_typename (cp_parser* parser)
18776 tree typename = NULL_TREE;
18778 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18780 tree proto_quals, cp_type = NULL_TREE;
18782 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18783 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18785 /* An ObjC type name may consist of just protocol qualifiers, in which
18786 case the type shall default to 'id'. */
18787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18788 cp_type = cp_parser_type_id (parser);
18790 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18791 typename = build_tree_list (proto_quals, cp_type);
18794 return typename;
18797 /* Check to see if TYPE refers to an Objective-C selector name. */
18799 static bool
18800 cp_parser_objc_selector_p (enum cpp_ttype type)
18802 return (type == CPP_NAME || type == CPP_KEYWORD
18803 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18804 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18805 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18806 || type == CPP_XOR || type == CPP_XOR_EQ);
18809 /* Parse an Objective-C selector. */
18811 static tree
18812 cp_parser_objc_selector (cp_parser* parser)
18814 cp_token *token = cp_lexer_consume_token (parser->lexer);
18816 if (!cp_parser_objc_selector_p (token->type))
18818 error ("invalid Objective-C++ selector name");
18819 return error_mark_node;
18822 /* C++ operator names are allowed to appear in ObjC selectors. */
18823 switch (token->type)
18825 case CPP_AND_AND: return get_identifier ("and");
18826 case CPP_AND_EQ: return get_identifier ("and_eq");
18827 case CPP_AND: return get_identifier ("bitand");
18828 case CPP_OR: return get_identifier ("bitor");
18829 case CPP_COMPL: return get_identifier ("compl");
18830 case CPP_NOT: return get_identifier ("not");
18831 case CPP_NOT_EQ: return get_identifier ("not_eq");
18832 case CPP_OR_OR: return get_identifier ("or");
18833 case CPP_OR_EQ: return get_identifier ("or_eq");
18834 case CPP_XOR: return get_identifier ("xor");
18835 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18836 default: return token->u.value;
18840 /* Parse an Objective-C params list. */
18842 static tree
18843 cp_parser_objc_method_keyword_params (cp_parser* parser)
18845 tree params = NULL_TREE;
18846 bool maybe_unary_selector_p = true;
18847 cp_token *token = cp_lexer_peek_token (parser->lexer);
18849 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18851 tree selector = NULL_TREE, typename, identifier;
18853 if (token->type != CPP_COLON)
18854 selector = cp_parser_objc_selector (parser);
18856 /* Detect if we have a unary selector. */
18857 if (maybe_unary_selector_p
18858 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18859 return selector;
18861 maybe_unary_selector_p = false;
18862 cp_parser_require (parser, CPP_COLON, "%<:%>");
18863 typename = cp_parser_objc_typename (parser);
18864 identifier = cp_parser_identifier (parser);
18866 params
18867 = chainon (params,
18868 objc_build_keyword_decl (selector,
18869 typename,
18870 identifier));
18872 token = cp_lexer_peek_token (parser->lexer);
18875 return params;
18878 /* Parse the non-keyword Objective-C params. */
18880 static tree
18881 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18883 tree params = make_node (TREE_LIST);
18884 cp_token *token = cp_lexer_peek_token (parser->lexer);
18885 *ellipsisp = false; /* Initially, assume no ellipsis. */
18887 while (token->type == CPP_COMMA)
18889 cp_parameter_declarator *parmdecl;
18890 tree parm;
18892 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18893 token = cp_lexer_peek_token (parser->lexer);
18895 if (token->type == CPP_ELLIPSIS)
18897 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18898 *ellipsisp = true;
18899 break;
18902 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18903 parm = grokdeclarator (parmdecl->declarator,
18904 &parmdecl->decl_specifiers,
18905 PARM, /*initialized=*/0,
18906 /*attrlist=*/NULL);
18908 chainon (params, build_tree_list (NULL_TREE, parm));
18909 token = cp_lexer_peek_token (parser->lexer);
18912 return params;
18915 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18917 static void
18918 cp_parser_objc_interstitial_code (cp_parser* parser)
18920 cp_token *token = cp_lexer_peek_token (parser->lexer);
18922 /* If the next token is `extern' and the following token is a string
18923 literal, then we have a linkage specification. */
18924 if (token->keyword == RID_EXTERN
18925 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18926 cp_parser_linkage_specification (parser);
18927 /* Handle #pragma, if any. */
18928 else if (token->type == CPP_PRAGMA)
18929 cp_parser_pragma (parser, pragma_external);
18930 /* Allow stray semicolons. */
18931 else if (token->type == CPP_SEMICOLON)
18932 cp_lexer_consume_token (parser->lexer);
18933 /* Finally, try to parse a block-declaration, or a function-definition. */
18934 else
18935 cp_parser_block_declaration (parser, /*statement_p=*/false);
18938 /* Parse a method signature. */
18940 static tree
18941 cp_parser_objc_method_signature (cp_parser* parser)
18943 tree rettype, kwdparms, optparms;
18944 bool ellipsis = false;
18946 cp_parser_objc_method_type (parser);
18947 rettype = cp_parser_objc_typename (parser);
18948 kwdparms = cp_parser_objc_method_keyword_params (parser);
18949 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18951 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18954 /* Pars an Objective-C method prototype list. */
18956 static void
18957 cp_parser_objc_method_prototype_list (cp_parser* parser)
18959 cp_token *token = cp_lexer_peek_token (parser->lexer);
18961 while (token->keyword != RID_AT_END)
18963 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18965 objc_add_method_declaration
18966 (cp_parser_objc_method_signature (parser));
18967 cp_parser_consume_semicolon_at_end_of_statement (parser);
18969 else
18970 /* Allow for interspersed non-ObjC++ code. */
18971 cp_parser_objc_interstitial_code (parser);
18973 token = cp_lexer_peek_token (parser->lexer);
18976 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18977 objc_finish_interface ();
18980 /* Parse an Objective-C method definition list. */
18982 static void
18983 cp_parser_objc_method_definition_list (cp_parser* parser)
18985 cp_token *token = cp_lexer_peek_token (parser->lexer);
18987 while (token->keyword != RID_AT_END)
18989 tree meth;
18991 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18993 push_deferring_access_checks (dk_deferred);
18994 objc_start_method_definition
18995 (cp_parser_objc_method_signature (parser));
18997 /* For historical reasons, we accept an optional semicolon. */
18998 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18999 cp_lexer_consume_token (parser->lexer);
19001 perform_deferred_access_checks ();
19002 stop_deferring_access_checks ();
19003 meth = cp_parser_function_definition_after_declarator (parser,
19004 false);
19005 pop_deferring_access_checks ();
19006 objc_finish_method_definition (meth);
19008 else
19009 /* Allow for interspersed non-ObjC++ code. */
19010 cp_parser_objc_interstitial_code (parser);
19012 token = cp_lexer_peek_token (parser->lexer);
19015 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19016 objc_finish_implementation ();
19019 /* Parse Objective-C ivars. */
19021 static void
19022 cp_parser_objc_class_ivars (cp_parser* parser)
19024 cp_token *token = cp_lexer_peek_token (parser->lexer);
19026 if (token->type != CPP_OPEN_BRACE)
19027 return; /* No ivars specified. */
19029 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19030 token = cp_lexer_peek_token (parser->lexer);
19032 while (token->type != CPP_CLOSE_BRACE)
19034 cp_decl_specifier_seq declspecs;
19035 int decl_class_or_enum_p;
19036 tree prefix_attributes;
19038 cp_parser_objc_visibility_spec (parser);
19040 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19041 break;
19043 cp_parser_decl_specifier_seq (parser,
19044 CP_PARSER_FLAGS_OPTIONAL,
19045 &declspecs,
19046 &decl_class_or_enum_p);
19047 prefix_attributes = declspecs.attributes;
19048 declspecs.attributes = NULL_TREE;
19050 /* Keep going until we hit the `;' at the end of the
19051 declaration. */
19052 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19054 tree width = NULL_TREE, attributes, first_attribute, decl;
19055 cp_declarator *declarator = NULL;
19056 int ctor_dtor_or_conv_p;
19058 /* Check for a (possibly unnamed) bitfield declaration. */
19059 token = cp_lexer_peek_token (parser->lexer);
19060 if (token->type == CPP_COLON)
19061 goto eat_colon;
19063 if (token->type == CPP_NAME
19064 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19065 == CPP_COLON))
19067 /* Get the name of the bitfield. */
19068 declarator = make_id_declarator (NULL_TREE,
19069 cp_parser_identifier (parser),
19070 sfk_none);
19072 eat_colon:
19073 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19074 /* Get the width of the bitfield. */
19075 width
19076 = cp_parser_constant_expression (parser,
19077 /*allow_non_constant=*/false,
19078 NULL);
19080 else
19082 /* Parse the declarator. */
19083 declarator
19084 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19085 &ctor_dtor_or_conv_p,
19086 /*parenthesized_p=*/NULL,
19087 /*member_p=*/false);
19090 /* Look for attributes that apply to the ivar. */
19091 attributes = cp_parser_attributes_opt (parser);
19092 /* Remember which attributes are prefix attributes and
19093 which are not. */
19094 first_attribute = attributes;
19095 /* Combine the attributes. */
19096 attributes = chainon (prefix_attributes, attributes);
19098 if (width)
19100 /* Create the bitfield declaration. */
19101 decl = grokbitfield (declarator, &declspecs, width);
19102 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
19104 else
19105 decl = grokfield (declarator, &declspecs,
19106 NULL_TREE, /*init_const_expr_p=*/false,
19107 NULL_TREE, attributes);
19109 /* Add the instance variable. */
19110 objc_add_instance_variable (decl);
19112 /* Reset PREFIX_ATTRIBUTES. */
19113 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19114 attributes = TREE_CHAIN (attributes);
19115 if (attributes)
19116 TREE_CHAIN (attributes) = NULL_TREE;
19118 token = cp_lexer_peek_token (parser->lexer);
19120 if (token->type == CPP_COMMA)
19122 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19123 continue;
19125 break;
19128 cp_parser_consume_semicolon_at_end_of_statement (parser);
19129 token = cp_lexer_peek_token (parser->lexer);
19132 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19133 /* For historical reasons, we accept an optional semicolon. */
19134 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19135 cp_lexer_consume_token (parser->lexer);
19138 /* Parse an Objective-C protocol declaration. */
19140 static void
19141 cp_parser_objc_protocol_declaration (cp_parser* parser)
19143 tree proto, protorefs;
19144 cp_token *tok;
19146 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19147 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19149 error ("identifier expected after %<@protocol%>");
19150 goto finish;
19153 /* See if we have a forward declaration or a definition. */
19154 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19156 /* Try a forward declaration first. */
19157 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19159 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19160 finish:
19161 cp_parser_consume_semicolon_at_end_of_statement (parser);
19164 /* Ok, we got a full-fledged definition (or at least should). */
19165 else
19167 proto = cp_parser_identifier (parser);
19168 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19169 objc_start_protocol (proto, protorefs);
19170 cp_parser_objc_method_prototype_list (parser);
19174 /* Parse an Objective-C superclass or category. */
19176 static void
19177 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19178 tree *categ)
19180 cp_token *next = cp_lexer_peek_token (parser->lexer);
19182 *super = *categ = NULL_TREE;
19183 if (next->type == CPP_COLON)
19185 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19186 *super = cp_parser_identifier (parser);
19188 else if (next->type == CPP_OPEN_PAREN)
19190 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19191 *categ = cp_parser_identifier (parser);
19192 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19196 /* Parse an Objective-C class interface. */
19198 static void
19199 cp_parser_objc_class_interface (cp_parser* parser)
19201 tree name, super, categ, protos;
19203 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19204 name = cp_parser_identifier (parser);
19205 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19206 protos = cp_parser_objc_protocol_refs_opt (parser);
19208 /* We have either a class or a category on our hands. */
19209 if (categ)
19210 objc_start_category_interface (name, categ, protos);
19211 else
19213 objc_start_class_interface (name, super, protos);
19214 /* Handle instance variable declarations, if any. */
19215 cp_parser_objc_class_ivars (parser);
19216 objc_continue_interface ();
19219 cp_parser_objc_method_prototype_list (parser);
19222 /* Parse an Objective-C class implementation. */
19224 static void
19225 cp_parser_objc_class_implementation (cp_parser* parser)
19227 tree name, super, categ;
19229 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19230 name = cp_parser_identifier (parser);
19231 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19233 /* We have either a class or a category on our hands. */
19234 if (categ)
19235 objc_start_category_implementation (name, categ);
19236 else
19238 objc_start_class_implementation (name, super);
19239 /* Handle instance variable declarations, if any. */
19240 cp_parser_objc_class_ivars (parser);
19241 objc_continue_implementation ();
19244 cp_parser_objc_method_definition_list (parser);
19247 /* Consume the @end token and finish off the implementation. */
19249 static void
19250 cp_parser_objc_end_implementation (cp_parser* parser)
19252 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19253 objc_finish_implementation ();
19256 /* Parse an Objective-C declaration. */
19258 static void
19259 cp_parser_objc_declaration (cp_parser* parser)
19261 /* Try to figure out what kind of declaration is present. */
19262 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19264 switch (kwd->keyword)
19266 case RID_AT_ALIAS:
19267 cp_parser_objc_alias_declaration (parser);
19268 break;
19269 case RID_AT_CLASS:
19270 cp_parser_objc_class_declaration (parser);
19271 break;
19272 case RID_AT_PROTOCOL:
19273 cp_parser_objc_protocol_declaration (parser);
19274 break;
19275 case RID_AT_INTERFACE:
19276 cp_parser_objc_class_interface (parser);
19277 break;
19278 case RID_AT_IMPLEMENTATION:
19279 cp_parser_objc_class_implementation (parser);
19280 break;
19281 case RID_AT_END:
19282 cp_parser_objc_end_implementation (parser);
19283 break;
19284 default:
19285 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19286 cp_parser_skip_to_end_of_block_or_statement (parser);
19290 /* Parse an Objective-C try-catch-finally statement.
19292 objc-try-catch-finally-stmt:
19293 @try compound-statement objc-catch-clause-seq [opt]
19294 objc-finally-clause [opt]
19296 objc-catch-clause-seq:
19297 objc-catch-clause objc-catch-clause-seq [opt]
19299 objc-catch-clause:
19300 @catch ( exception-declaration ) compound-statement
19302 objc-finally-clause
19303 @finally compound-statement
19305 Returns NULL_TREE. */
19307 static tree
19308 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19309 location_t location;
19310 tree stmt;
19312 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19313 location = cp_lexer_peek_token (parser->lexer)->location;
19314 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19315 node, lest it get absorbed into the surrounding block. */
19316 stmt = push_stmt_list ();
19317 cp_parser_compound_statement (parser, NULL, false);
19318 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19320 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19322 cp_parameter_declarator *parmdecl;
19323 tree parm;
19325 cp_lexer_consume_token (parser->lexer);
19326 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19327 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19328 parm = grokdeclarator (parmdecl->declarator,
19329 &parmdecl->decl_specifiers,
19330 PARM, /*initialized=*/0,
19331 /*attrlist=*/NULL);
19332 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19333 objc_begin_catch_clause (parm);
19334 cp_parser_compound_statement (parser, NULL, false);
19335 objc_finish_catch_clause ();
19338 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19340 cp_lexer_consume_token (parser->lexer);
19341 location = cp_lexer_peek_token (parser->lexer)->location;
19342 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19343 node, lest it get absorbed into the surrounding block. */
19344 stmt = push_stmt_list ();
19345 cp_parser_compound_statement (parser, NULL, false);
19346 objc_build_finally_clause (location, pop_stmt_list (stmt));
19349 return objc_finish_try_stmt ();
19352 /* Parse an Objective-C synchronized statement.
19354 objc-synchronized-stmt:
19355 @synchronized ( expression ) compound-statement
19357 Returns NULL_TREE. */
19359 static tree
19360 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19361 location_t location;
19362 tree lock, stmt;
19364 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19366 location = cp_lexer_peek_token (parser->lexer)->location;
19367 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19368 lock = cp_parser_expression (parser, false);
19369 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19371 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19372 node, lest it get absorbed into the surrounding block. */
19373 stmt = push_stmt_list ();
19374 cp_parser_compound_statement (parser, NULL, false);
19376 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19379 /* Parse an Objective-C throw statement.
19381 objc-throw-stmt:
19382 @throw assignment-expression [opt] ;
19384 Returns a constructed '@throw' statement. */
19386 static tree
19387 cp_parser_objc_throw_statement (cp_parser *parser) {
19388 tree expr = NULL_TREE;
19390 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19393 expr = cp_parser_assignment_expression (parser, false);
19395 cp_parser_consume_semicolon_at_end_of_statement (parser);
19397 return objc_build_throw_stmt (expr);
19400 /* Parse an Objective-C statement. */
19402 static tree
19403 cp_parser_objc_statement (cp_parser * parser) {
19404 /* Try to figure out what kind of declaration is present. */
19405 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19407 switch (kwd->keyword)
19409 case RID_AT_TRY:
19410 return cp_parser_objc_try_catch_finally_statement (parser);
19411 case RID_AT_SYNCHRONIZED:
19412 return cp_parser_objc_synchronized_statement (parser);
19413 case RID_AT_THROW:
19414 return cp_parser_objc_throw_statement (parser);
19415 default:
19416 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19417 cp_parser_skip_to_end_of_block_or_statement (parser);
19420 return error_mark_node;
19423 /* OpenMP 2.5 parsing routines. */
19425 /* Returns name of the next clause.
19426 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19427 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19428 returned and the token is consumed. */
19430 static pragma_omp_clause
19431 cp_parser_omp_clause_name (cp_parser *parser)
19433 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19435 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19436 result = PRAGMA_OMP_CLAUSE_IF;
19437 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19438 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19439 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19440 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19441 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19443 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19444 const char *p = IDENTIFIER_POINTER (id);
19446 switch (p[0])
19448 case 'c':
19449 if (!strcmp ("copyin", p))
19450 result = PRAGMA_OMP_CLAUSE_COPYIN;
19451 else if (!strcmp ("copyprivate", p))
19452 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19453 break;
19454 case 'f':
19455 if (!strcmp ("firstprivate", p))
19456 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19457 break;
19458 case 'l':
19459 if (!strcmp ("lastprivate", p))
19460 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19461 break;
19462 case 'n':
19463 if (!strcmp ("nowait", p))
19464 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19465 else if (!strcmp ("num_threads", p))
19466 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19467 break;
19468 case 'o':
19469 if (!strcmp ("ordered", p))
19470 result = PRAGMA_OMP_CLAUSE_ORDERED;
19471 break;
19472 case 'r':
19473 if (!strcmp ("reduction", p))
19474 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19475 break;
19476 case 's':
19477 if (!strcmp ("schedule", p))
19478 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19479 else if (!strcmp ("shared", p))
19480 result = PRAGMA_OMP_CLAUSE_SHARED;
19481 break;
19485 if (result != PRAGMA_OMP_CLAUSE_NONE)
19486 cp_lexer_consume_token (parser->lexer);
19488 return result;
19491 /* Validate that a clause of the given type does not already exist. */
19493 static void
19494 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19496 tree c;
19498 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19499 if (OMP_CLAUSE_CODE (c) == code)
19501 error ("too many %qs clauses", name);
19502 break;
19506 /* OpenMP 2.5:
19507 variable-list:
19508 identifier
19509 variable-list , identifier
19511 In addition, we match a closing parenthesis. An opening parenthesis
19512 will have been consumed by the caller.
19514 If KIND is nonzero, create the appropriate node and install the decl
19515 in OMP_CLAUSE_DECL and add the node to the head of the list.
19517 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19518 return the list created. */
19520 static tree
19521 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19522 tree list)
19524 while (1)
19526 tree name, decl;
19528 name = cp_parser_id_expression (parser, /*template_p=*/false,
19529 /*check_dependency_p=*/true,
19530 /*template_p=*/NULL,
19531 /*declarator_p=*/false,
19532 /*optional_p=*/false);
19533 if (name == error_mark_node)
19534 goto skip_comma;
19536 decl = cp_parser_lookup_name_simple (parser, name);
19537 if (decl == error_mark_node)
19538 cp_parser_name_lookup_error (parser, name, decl, NULL);
19539 else if (kind != 0)
19541 tree u = build_omp_clause (kind);
19542 OMP_CLAUSE_DECL (u) = decl;
19543 OMP_CLAUSE_CHAIN (u) = list;
19544 list = u;
19546 else
19547 list = tree_cons (decl, NULL_TREE, list);
19549 get_comma:
19550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19551 break;
19552 cp_lexer_consume_token (parser->lexer);
19555 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19557 int ending;
19559 /* Try to resync to an unnested comma. Copied from
19560 cp_parser_parenthesized_expression_list. */
19561 skip_comma:
19562 ending = cp_parser_skip_to_closing_parenthesis (parser,
19563 /*recovering=*/true,
19564 /*or_comma=*/true,
19565 /*consume_paren=*/true);
19566 if (ending < 0)
19567 goto get_comma;
19570 return list;
19573 /* Similarly, but expect leading and trailing parenthesis. This is a very
19574 common case for omp clauses. */
19576 static tree
19577 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19579 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19580 return cp_parser_omp_var_list_no_open (parser, kind, list);
19581 return list;
19584 /* OpenMP 2.5:
19585 default ( shared | none ) */
19587 static tree
19588 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19590 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19591 tree c;
19593 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19594 return list;
19595 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19597 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19598 const char *p = IDENTIFIER_POINTER (id);
19600 switch (p[0])
19602 case 'n':
19603 if (strcmp ("none", p) != 0)
19604 goto invalid_kind;
19605 kind = OMP_CLAUSE_DEFAULT_NONE;
19606 break;
19608 case 's':
19609 if (strcmp ("shared", p) != 0)
19610 goto invalid_kind;
19611 kind = OMP_CLAUSE_DEFAULT_SHARED;
19612 break;
19614 default:
19615 goto invalid_kind;
19618 cp_lexer_consume_token (parser->lexer);
19620 else
19622 invalid_kind:
19623 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19626 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19627 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19628 /*or_comma=*/false,
19629 /*consume_paren=*/true);
19631 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19632 return list;
19634 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19635 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19636 OMP_CLAUSE_CHAIN (c) = list;
19637 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19639 return c;
19642 /* OpenMP 2.5:
19643 if ( expression ) */
19645 static tree
19646 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19648 tree t, c;
19650 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19651 return list;
19653 t = cp_parser_condition (parser);
19655 if (t == error_mark_node
19656 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19657 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19658 /*or_comma=*/false,
19659 /*consume_paren=*/true);
19661 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19663 c = build_omp_clause (OMP_CLAUSE_IF);
19664 OMP_CLAUSE_IF_EXPR (c) = t;
19665 OMP_CLAUSE_CHAIN (c) = list;
19667 return c;
19670 /* OpenMP 2.5:
19671 nowait */
19673 static tree
19674 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19676 tree c;
19678 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19680 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19681 OMP_CLAUSE_CHAIN (c) = list;
19682 return c;
19685 /* OpenMP 2.5:
19686 num_threads ( expression ) */
19688 static tree
19689 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19691 tree t, c;
19693 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19694 return list;
19696 t = cp_parser_expression (parser, false);
19698 if (t == error_mark_node
19699 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19700 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19701 /*or_comma=*/false,
19702 /*consume_paren=*/true);
19704 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19706 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19707 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19708 OMP_CLAUSE_CHAIN (c) = list;
19710 return c;
19713 /* OpenMP 2.5:
19714 ordered */
19716 static tree
19717 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19719 tree c;
19721 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19723 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19724 OMP_CLAUSE_CHAIN (c) = list;
19725 return c;
19728 /* OpenMP 2.5:
19729 reduction ( reduction-operator : variable-list )
19731 reduction-operator:
19732 One of: + * - & ^ | && || */
19734 static tree
19735 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19737 enum tree_code code;
19738 tree nlist, c;
19740 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19741 return list;
19743 switch (cp_lexer_peek_token (parser->lexer)->type)
19745 case CPP_PLUS:
19746 code = PLUS_EXPR;
19747 break;
19748 case CPP_MULT:
19749 code = MULT_EXPR;
19750 break;
19751 case CPP_MINUS:
19752 code = MINUS_EXPR;
19753 break;
19754 case CPP_AND:
19755 code = BIT_AND_EXPR;
19756 break;
19757 case CPP_XOR:
19758 code = BIT_XOR_EXPR;
19759 break;
19760 case CPP_OR:
19761 code = BIT_IOR_EXPR;
19762 break;
19763 case CPP_AND_AND:
19764 code = TRUTH_ANDIF_EXPR;
19765 break;
19766 case CPP_OR_OR:
19767 code = TRUTH_ORIF_EXPR;
19768 break;
19769 default:
19770 cp_parser_error (parser, "%<+%>, %<*%>, %<-%>, %<&%>, %<^%>, %<|%>, "
19771 "%<&&%>, or %<||%>");
19772 resync_fail:
19773 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19774 /*or_comma=*/false,
19775 /*consume_paren=*/true);
19776 return list;
19778 cp_lexer_consume_token (parser->lexer);
19780 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
19781 goto resync_fail;
19783 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19784 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19785 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19787 return nlist;
19790 /* OpenMP 2.5:
19791 schedule ( schedule-kind )
19792 schedule ( schedule-kind , expression )
19794 schedule-kind:
19795 static | dynamic | guided | runtime */
19797 static tree
19798 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19800 tree c, t;
19802 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19803 return list;
19805 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19807 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19809 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19810 const char *p = IDENTIFIER_POINTER (id);
19812 switch (p[0])
19814 case 'd':
19815 if (strcmp ("dynamic", p) != 0)
19816 goto invalid_kind;
19817 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19818 break;
19820 case 'g':
19821 if (strcmp ("guided", p) != 0)
19822 goto invalid_kind;
19823 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19824 break;
19826 case 'r':
19827 if (strcmp ("runtime", p) != 0)
19828 goto invalid_kind;
19829 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19830 break;
19832 default:
19833 goto invalid_kind;
19836 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19837 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19838 else
19839 goto invalid_kind;
19840 cp_lexer_consume_token (parser->lexer);
19842 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19844 cp_lexer_consume_token (parser->lexer);
19846 t = cp_parser_assignment_expression (parser, false);
19848 if (t == error_mark_node)
19849 goto resync_fail;
19850 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19851 error ("schedule %<runtime%> does not take "
19852 "a %<chunk_size%> parameter");
19853 else
19854 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19856 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19857 goto resync_fail;
19859 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
19860 goto resync_fail;
19862 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19863 OMP_CLAUSE_CHAIN (c) = list;
19864 return c;
19866 invalid_kind:
19867 cp_parser_error (parser, "invalid schedule kind");
19868 resync_fail:
19869 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19870 /*or_comma=*/false,
19871 /*consume_paren=*/true);
19872 return list;
19875 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19876 is a bitmask in MASK. Return the list of clauses found; the result
19877 of clause default goes in *pdefault. */
19879 static tree
19880 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19881 const char *where, cp_token *pragma_tok)
19883 tree clauses = NULL;
19884 bool first = true;
19886 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19888 pragma_omp_clause c_kind;
19889 const char *c_name;
19890 tree prev = clauses;
19892 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19893 cp_lexer_consume_token (parser->lexer);
19895 c_kind = cp_parser_omp_clause_name (parser);
19896 first = false;
19898 switch (c_kind)
19900 case PRAGMA_OMP_CLAUSE_COPYIN:
19901 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19902 c_name = "copyin";
19903 break;
19904 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19905 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19906 clauses);
19907 c_name = "copyprivate";
19908 break;
19909 case PRAGMA_OMP_CLAUSE_DEFAULT:
19910 clauses = cp_parser_omp_clause_default (parser, clauses);
19911 c_name = "default";
19912 break;
19913 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19914 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19915 clauses);
19916 c_name = "firstprivate";
19917 break;
19918 case PRAGMA_OMP_CLAUSE_IF:
19919 clauses = cp_parser_omp_clause_if (parser, clauses);
19920 c_name = "if";
19921 break;
19922 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19923 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19924 clauses);
19925 c_name = "lastprivate";
19926 break;
19927 case PRAGMA_OMP_CLAUSE_NOWAIT:
19928 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19929 c_name = "nowait";
19930 break;
19931 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19932 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19933 c_name = "num_threads";
19934 break;
19935 case PRAGMA_OMP_CLAUSE_ORDERED:
19936 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19937 c_name = "ordered";
19938 break;
19939 case PRAGMA_OMP_CLAUSE_PRIVATE:
19940 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19941 clauses);
19942 c_name = "private";
19943 break;
19944 case PRAGMA_OMP_CLAUSE_REDUCTION:
19945 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19946 c_name = "reduction";
19947 break;
19948 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19949 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19950 c_name = "schedule";
19951 break;
19952 case PRAGMA_OMP_CLAUSE_SHARED:
19953 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19954 clauses);
19955 c_name = "shared";
19956 break;
19957 default:
19958 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19959 goto saw_error;
19962 if (((mask >> c_kind) & 1) == 0)
19964 /* Remove the invalid clause(s) from the list to avoid
19965 confusing the rest of the compiler. */
19966 clauses = prev;
19967 error ("%qs is not valid for %qs", c_name, where);
19970 saw_error:
19971 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19972 return finish_omp_clauses (clauses);
19975 /* OpenMP 2.5:
19976 structured-block:
19977 statement
19979 In practice, we're also interested in adding the statement to an
19980 outer node. So it is convenient if we work around the fact that
19981 cp_parser_statement calls add_stmt. */
19983 static unsigned
19984 cp_parser_begin_omp_structured_block (cp_parser *parser)
19986 unsigned save = parser->in_statement;
19988 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19989 This preserves the "not within loop or switch" style error messages
19990 for nonsense cases like
19991 void foo() {
19992 #pragma omp single
19993 break;
19996 if (parser->in_statement)
19997 parser->in_statement = IN_OMP_BLOCK;
19999 return save;
20002 static void
20003 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20005 parser->in_statement = save;
20008 static tree
20009 cp_parser_omp_structured_block (cp_parser *parser)
20011 tree stmt = begin_omp_structured_block ();
20012 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20014 cp_parser_statement (parser, NULL_TREE, false, NULL);
20016 cp_parser_end_omp_structured_block (parser, save);
20017 return finish_omp_structured_block (stmt);
20020 /* OpenMP 2.5:
20021 # pragma omp atomic new-line
20022 expression-stmt
20024 expression-stmt:
20025 x binop= expr | x++ | ++x | x-- | --x
20026 binop:
20027 +, *, -, /, &, ^, |, <<, >>
20029 where x is an lvalue expression with scalar type. */
20031 static void
20032 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20034 tree lhs, rhs;
20035 enum tree_code code;
20037 cp_parser_require_pragma_eol (parser, pragma_tok);
20039 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20040 /*cast_p=*/false);
20041 switch (TREE_CODE (lhs))
20043 case ERROR_MARK:
20044 goto saw_error;
20046 case PREINCREMENT_EXPR:
20047 case POSTINCREMENT_EXPR:
20048 lhs = TREE_OPERAND (lhs, 0);
20049 code = PLUS_EXPR;
20050 rhs = integer_one_node;
20051 break;
20053 case PREDECREMENT_EXPR:
20054 case POSTDECREMENT_EXPR:
20055 lhs = TREE_OPERAND (lhs, 0);
20056 code = MINUS_EXPR;
20057 rhs = integer_one_node;
20058 break;
20060 default:
20061 switch (cp_lexer_peek_token (parser->lexer)->type)
20063 case CPP_MULT_EQ:
20064 code = MULT_EXPR;
20065 break;
20066 case CPP_DIV_EQ:
20067 code = TRUNC_DIV_EXPR;
20068 break;
20069 case CPP_PLUS_EQ:
20070 code = PLUS_EXPR;
20071 break;
20072 case CPP_MINUS_EQ:
20073 code = MINUS_EXPR;
20074 break;
20075 case CPP_LSHIFT_EQ:
20076 code = LSHIFT_EXPR;
20077 break;
20078 case CPP_RSHIFT_EQ:
20079 code = RSHIFT_EXPR;
20080 break;
20081 case CPP_AND_EQ:
20082 code = BIT_AND_EXPR;
20083 break;
20084 case CPP_OR_EQ:
20085 code = BIT_IOR_EXPR;
20086 break;
20087 case CPP_XOR_EQ:
20088 code = BIT_XOR_EXPR;
20089 break;
20090 default:
20091 cp_parser_error (parser,
20092 "invalid operator for %<#pragma omp atomic%>");
20093 goto saw_error;
20095 cp_lexer_consume_token (parser->lexer);
20097 rhs = cp_parser_expression (parser, false);
20098 if (rhs == error_mark_node)
20099 goto saw_error;
20100 break;
20102 finish_omp_atomic (code, lhs, rhs);
20103 cp_parser_consume_semicolon_at_end_of_statement (parser);
20104 return;
20106 saw_error:
20107 cp_parser_skip_to_end_of_block_or_statement (parser);
20111 /* OpenMP 2.5:
20112 # pragma omp barrier new-line */
20114 static void
20115 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20117 cp_parser_require_pragma_eol (parser, pragma_tok);
20118 finish_omp_barrier ();
20121 /* OpenMP 2.5:
20122 # pragma omp critical [(name)] new-line
20123 structured-block */
20125 static tree
20126 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20128 tree stmt, name = NULL;
20130 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20132 cp_lexer_consume_token (parser->lexer);
20134 name = cp_parser_identifier (parser);
20136 if (name == error_mark_node
20137 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20138 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20139 /*or_comma=*/false,
20140 /*consume_paren=*/true);
20141 if (name == error_mark_node)
20142 name = NULL;
20144 cp_parser_require_pragma_eol (parser, pragma_tok);
20146 stmt = cp_parser_omp_structured_block (parser);
20147 return c_finish_omp_critical (stmt, name);
20150 /* OpenMP 2.5:
20151 # pragma omp flush flush-vars[opt] new-line
20153 flush-vars:
20154 ( variable-list ) */
20156 static void
20157 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20159 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20160 (void) cp_parser_omp_var_list (parser, 0, NULL);
20161 cp_parser_require_pragma_eol (parser, pragma_tok);
20163 finish_omp_flush ();
20166 /* Parse the restricted form of the for statment allowed by OpenMP. */
20168 static tree
20169 cp_parser_omp_for_loop (cp_parser *parser)
20171 tree init, cond, incr, body, decl, pre_body;
20172 location_t loc;
20174 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20176 cp_parser_error (parser, "for statement expected");
20177 return NULL;
20179 loc = cp_lexer_consume_token (parser->lexer)->location;
20180 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20181 return NULL;
20183 init = decl = NULL;
20184 pre_body = push_stmt_list ();
20185 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20187 cp_decl_specifier_seq type_specifiers;
20189 /* First, try to parse as an initialized declaration. See
20190 cp_parser_condition, from whence the bulk of this is copied. */
20192 cp_parser_parse_tentatively (parser);
20193 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20194 &type_specifiers);
20195 if (!cp_parser_error_occurred (parser))
20197 tree asm_specification, attributes;
20198 cp_declarator *declarator;
20200 declarator = cp_parser_declarator (parser,
20201 CP_PARSER_DECLARATOR_NAMED,
20202 /*ctor_dtor_or_conv_p=*/NULL,
20203 /*parenthesized_p=*/NULL,
20204 /*member_p=*/false);
20205 attributes = cp_parser_attributes_opt (parser);
20206 asm_specification = cp_parser_asm_specification_opt (parser);
20208 cp_parser_require (parser, CPP_EQ, "%<=%>");
20209 if (cp_parser_parse_definitely (parser))
20211 tree pushed_scope;
20213 decl = start_decl (declarator, &type_specifiers,
20214 /*initialized_p=*/false, attributes,
20215 /*prefix_attributes=*/NULL_TREE,
20216 &pushed_scope);
20218 init = cp_parser_assignment_expression (parser, false);
20220 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20221 init = error_mark_node;
20222 else
20223 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
20224 asm_specification, LOOKUP_ONLYCONVERTING);
20226 if (pushed_scope)
20227 pop_scope (pushed_scope);
20230 else
20231 cp_parser_abort_tentative_parse (parser);
20233 /* If parsing as an initialized declaration failed, try again as
20234 a simple expression. */
20235 if (decl == NULL)
20236 init = cp_parser_expression (parser, false);
20238 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
20239 pre_body = pop_stmt_list (pre_body);
20241 cond = NULL;
20242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20243 cond = cp_parser_condition (parser);
20244 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
20246 incr = NULL;
20247 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20248 incr = cp_parser_expression (parser, false);
20250 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20251 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20252 /*or_comma=*/false,
20253 /*consume_paren=*/true);
20255 /* Note that we saved the original contents of this flag when we entered
20256 the structured block, and so we don't need to re-save it here. */
20257 parser->in_statement = IN_OMP_FOR;
20259 /* Note that the grammar doesn't call for a structured block here,
20260 though the loop as a whole is a structured block. */
20261 body = push_stmt_list ();
20262 cp_parser_statement (parser, NULL_TREE, false, NULL);
20263 body = pop_stmt_list (body);
20265 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
20268 /* OpenMP 2.5:
20269 #pragma omp for for-clause[optseq] new-line
20270 for-loop */
20272 #define OMP_FOR_CLAUSE_MASK \
20273 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20274 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20275 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20276 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20277 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
20278 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
20279 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20281 static tree
20282 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20284 tree clauses, sb, ret;
20285 unsigned int save;
20287 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20288 "#pragma omp for", pragma_tok);
20290 sb = begin_omp_structured_block ();
20291 save = cp_parser_begin_omp_structured_block (parser);
20293 ret = cp_parser_omp_for_loop (parser);
20294 if (ret)
20295 OMP_FOR_CLAUSES (ret) = clauses;
20297 cp_parser_end_omp_structured_block (parser, save);
20298 add_stmt (finish_omp_structured_block (sb));
20300 return ret;
20303 /* OpenMP 2.5:
20304 # pragma omp master new-line
20305 structured-block */
20307 static tree
20308 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20310 cp_parser_require_pragma_eol (parser, pragma_tok);
20311 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20314 /* OpenMP 2.5:
20315 # pragma omp ordered new-line
20316 structured-block */
20318 static tree
20319 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20321 cp_parser_require_pragma_eol (parser, pragma_tok);
20322 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20325 /* OpenMP 2.5:
20327 section-scope:
20328 { section-sequence }
20330 section-sequence:
20331 section-directive[opt] structured-block
20332 section-sequence section-directive structured-block */
20334 static tree
20335 cp_parser_omp_sections_scope (cp_parser *parser)
20337 tree stmt, substmt;
20338 bool error_suppress = false;
20339 cp_token *tok;
20341 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
20342 return NULL_TREE;
20344 stmt = push_stmt_list ();
20346 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20348 unsigned save;
20350 substmt = begin_omp_structured_block ();
20351 save = cp_parser_begin_omp_structured_block (parser);
20353 while (1)
20355 cp_parser_statement (parser, NULL_TREE, false, NULL);
20357 tok = cp_lexer_peek_token (parser->lexer);
20358 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20359 break;
20360 if (tok->type == CPP_CLOSE_BRACE)
20361 break;
20362 if (tok->type == CPP_EOF)
20363 break;
20366 cp_parser_end_omp_structured_block (parser, save);
20367 substmt = finish_omp_structured_block (substmt);
20368 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20369 add_stmt (substmt);
20372 while (1)
20374 tok = cp_lexer_peek_token (parser->lexer);
20375 if (tok->type == CPP_CLOSE_BRACE)
20376 break;
20377 if (tok->type == CPP_EOF)
20378 break;
20380 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20382 cp_lexer_consume_token (parser->lexer);
20383 cp_parser_require_pragma_eol (parser, tok);
20384 error_suppress = false;
20386 else if (!error_suppress)
20388 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20389 error_suppress = true;
20392 substmt = cp_parser_omp_structured_block (parser);
20393 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20394 add_stmt (substmt);
20396 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
20398 substmt = pop_stmt_list (stmt);
20400 stmt = make_node (OMP_SECTIONS);
20401 TREE_TYPE (stmt) = void_type_node;
20402 OMP_SECTIONS_BODY (stmt) = substmt;
20404 add_stmt (stmt);
20405 return stmt;
20408 /* OpenMP 2.5:
20409 # pragma omp sections sections-clause[optseq] newline
20410 sections-scope */
20412 #define OMP_SECTIONS_CLAUSE_MASK \
20413 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20414 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20415 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20416 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20417 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20419 static tree
20420 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20422 tree clauses, ret;
20424 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20425 "#pragma omp sections", pragma_tok);
20427 ret = cp_parser_omp_sections_scope (parser);
20428 if (ret)
20429 OMP_SECTIONS_CLAUSES (ret) = clauses;
20431 return ret;
20434 /* OpenMP 2.5:
20435 # pragma parallel parallel-clause new-line
20436 # pragma parallel for parallel-for-clause new-line
20437 # pragma parallel sections parallel-sections-clause new-line */
20439 #define OMP_PARALLEL_CLAUSE_MASK \
20440 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
20441 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20442 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20443 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
20444 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
20445 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
20446 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20447 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20449 static tree
20450 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20452 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20453 const char *p_name = "#pragma omp parallel";
20454 tree stmt, clauses, par_clause, ws_clause, block;
20455 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20456 unsigned int save;
20458 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20460 cp_lexer_consume_token (parser->lexer);
20461 p_kind = PRAGMA_OMP_PARALLEL_FOR;
20462 p_name = "#pragma omp parallel for";
20463 mask |= OMP_FOR_CLAUSE_MASK;
20464 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20466 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20468 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20469 const char *p = IDENTIFIER_POINTER (id);
20470 if (strcmp (p, "sections") == 0)
20472 cp_lexer_consume_token (parser->lexer);
20473 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20474 p_name = "#pragma omp parallel sections";
20475 mask |= OMP_SECTIONS_CLAUSE_MASK;
20476 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20480 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20481 block = begin_omp_parallel ();
20482 save = cp_parser_begin_omp_structured_block (parser);
20484 switch (p_kind)
20486 case PRAGMA_OMP_PARALLEL:
20487 cp_parser_statement (parser, NULL_TREE, false, NULL);
20488 par_clause = clauses;
20489 break;
20491 case PRAGMA_OMP_PARALLEL_FOR:
20492 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20493 stmt = cp_parser_omp_for_loop (parser);
20494 if (stmt)
20495 OMP_FOR_CLAUSES (stmt) = ws_clause;
20496 break;
20498 case PRAGMA_OMP_PARALLEL_SECTIONS:
20499 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20500 stmt = cp_parser_omp_sections_scope (parser);
20501 if (stmt)
20502 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20503 break;
20505 default:
20506 gcc_unreachable ();
20509 cp_parser_end_omp_structured_block (parser, save);
20510 stmt = finish_omp_parallel (par_clause, block);
20511 if (p_kind != PRAGMA_OMP_PARALLEL)
20512 OMP_PARALLEL_COMBINED (stmt) = 1;
20513 return stmt;
20516 /* OpenMP 2.5:
20517 # pragma omp single single-clause[optseq] new-line
20518 structured-block */
20520 #define OMP_SINGLE_CLAUSE_MASK \
20521 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20522 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20523 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20524 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20526 static tree
20527 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20529 tree stmt = make_node (OMP_SINGLE);
20530 TREE_TYPE (stmt) = void_type_node;
20532 OMP_SINGLE_CLAUSES (stmt)
20533 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20534 "#pragma omp single", pragma_tok);
20535 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20537 return add_stmt (stmt);
20540 /* OpenMP 2.5:
20541 # pragma omp threadprivate (variable-list) */
20543 static void
20544 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20546 tree vars;
20548 vars = cp_parser_omp_var_list (parser, 0, NULL);
20549 cp_parser_require_pragma_eol (parser, pragma_tok);
20551 finish_omp_threadprivate (vars);
20554 /* Main entry point to OpenMP statement pragmas. */
20556 static void
20557 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20559 tree stmt;
20561 switch (pragma_tok->pragma_kind)
20563 case PRAGMA_OMP_ATOMIC:
20564 cp_parser_omp_atomic (parser, pragma_tok);
20565 return;
20566 case PRAGMA_OMP_CRITICAL:
20567 stmt = cp_parser_omp_critical (parser, pragma_tok);
20568 break;
20569 case PRAGMA_OMP_FOR:
20570 stmt = cp_parser_omp_for (parser, pragma_tok);
20571 break;
20572 case PRAGMA_OMP_MASTER:
20573 stmt = cp_parser_omp_master (parser, pragma_tok);
20574 break;
20575 case PRAGMA_OMP_ORDERED:
20576 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20577 break;
20578 case PRAGMA_OMP_PARALLEL:
20579 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20580 break;
20581 case PRAGMA_OMP_SECTIONS:
20582 stmt = cp_parser_omp_sections (parser, pragma_tok);
20583 break;
20584 case PRAGMA_OMP_SINGLE:
20585 stmt = cp_parser_omp_single (parser, pragma_tok);
20586 break;
20587 default:
20588 gcc_unreachable ();
20591 if (stmt)
20592 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20595 /* The parser. */
20597 static GTY (()) cp_parser *the_parser;
20600 /* Special handling for the first token or line in the file. The first
20601 thing in the file might be #pragma GCC pch_preprocess, which loads a
20602 PCH file, which is a GC collection point. So we need to handle this
20603 first pragma without benefit of an existing lexer structure.
20605 Always returns one token to the caller in *FIRST_TOKEN. This is
20606 either the true first token of the file, or the first token after
20607 the initial pragma. */
20609 static void
20610 cp_parser_initial_pragma (cp_token *first_token)
20612 tree name = NULL;
20614 cp_lexer_get_preprocessor_token (NULL, first_token);
20615 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20616 return;
20618 cp_lexer_get_preprocessor_token (NULL, first_token);
20619 if (first_token->type == CPP_STRING)
20621 name = first_token->u.value;
20623 cp_lexer_get_preprocessor_token (NULL, first_token);
20624 if (first_token->type != CPP_PRAGMA_EOL)
20625 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20627 else
20628 error ("expected string literal");
20630 /* Skip to the end of the pragma. */
20631 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20632 cp_lexer_get_preprocessor_token (NULL, first_token);
20634 /* Now actually load the PCH file. */
20635 if (name)
20636 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20638 /* Read one more token to return to our caller. We have to do this
20639 after reading the PCH file in, since its pointers have to be
20640 live. */
20641 cp_lexer_get_preprocessor_token (NULL, first_token);
20644 /* Normal parsing of a pragma token. Here we can (and must) use the
20645 regular lexer. */
20647 static bool
20648 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20650 cp_token *pragma_tok;
20651 unsigned int id;
20653 pragma_tok = cp_lexer_consume_token (parser->lexer);
20654 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20655 parser->lexer->in_pragma = true;
20657 id = pragma_tok->pragma_kind;
20658 switch (id)
20660 case PRAGMA_GCC_PCH_PREPROCESS:
20661 error ("%<#pragma GCC pch_preprocess%> must be first");
20662 break;
20664 case PRAGMA_OMP_BARRIER:
20665 switch (context)
20667 case pragma_compound:
20668 cp_parser_omp_barrier (parser, pragma_tok);
20669 return false;
20670 case pragma_stmt:
20671 error ("%<#pragma omp barrier%> may only be "
20672 "used in compound statements");
20673 break;
20674 default:
20675 goto bad_stmt;
20677 break;
20679 case PRAGMA_OMP_FLUSH:
20680 switch (context)
20682 case pragma_compound:
20683 cp_parser_omp_flush (parser, pragma_tok);
20684 return false;
20685 case pragma_stmt:
20686 error ("%<#pragma omp flush%> may only be "
20687 "used in compound statements");
20688 break;
20689 default:
20690 goto bad_stmt;
20692 break;
20694 case PRAGMA_OMP_THREADPRIVATE:
20695 cp_parser_omp_threadprivate (parser, pragma_tok);
20696 return false;
20698 case PRAGMA_OMP_ATOMIC:
20699 case PRAGMA_OMP_CRITICAL:
20700 case PRAGMA_OMP_FOR:
20701 case PRAGMA_OMP_MASTER:
20702 case PRAGMA_OMP_ORDERED:
20703 case PRAGMA_OMP_PARALLEL:
20704 case PRAGMA_OMP_SECTIONS:
20705 case PRAGMA_OMP_SINGLE:
20706 if (context == pragma_external)
20707 goto bad_stmt;
20708 cp_parser_omp_construct (parser, pragma_tok);
20709 return true;
20711 case PRAGMA_OMP_SECTION:
20712 error ("%<#pragma omp section%> may only be used in "
20713 "%<#pragma omp sections%> construct");
20714 break;
20716 default:
20717 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20718 c_invoke_pragma_handler (id);
20719 break;
20721 bad_stmt:
20722 cp_parser_error (parser, "expected declaration specifiers");
20723 break;
20726 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20727 return false;
20730 /* The interface the pragma parsers have to the lexer. */
20732 enum cpp_ttype
20733 pragma_lex (tree *value)
20735 cp_token *tok;
20736 enum cpp_ttype ret;
20738 tok = cp_lexer_peek_token (the_parser->lexer);
20740 ret = tok->type;
20741 *value = tok->u.value;
20743 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20744 ret = CPP_EOF;
20745 else if (ret == CPP_STRING)
20746 *value = cp_parser_string_literal (the_parser, false, false);
20747 else
20749 cp_lexer_consume_token (the_parser->lexer);
20750 if (ret == CPP_KEYWORD)
20751 ret = CPP_NAME;
20754 return ret;
20758 /* External interface. */
20760 /* Parse one entire translation unit. */
20762 void
20763 c_parse_file (void)
20765 bool error_occurred;
20766 static bool already_called = false;
20768 if (already_called)
20770 sorry ("inter-module optimizations not implemented for C++");
20771 return;
20773 already_called = true;
20775 the_parser = cp_parser_new ();
20776 push_deferring_access_checks (flag_access_control
20777 ? dk_no_deferred : dk_no_check);
20778 error_occurred = cp_parser_translation_unit (the_parser);
20779 the_parser = NULL;
20782 #include "gt-cp-parser.h"