2007-05-04 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / cp / parser.c
blob63f7fec0169b63540fac06732a895c61f55feb80
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
43 /* The lexer. */
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
49 qualifying scope. */
51 struct tree_check GTY(())
53 /* The value associated with the token. */
54 tree value;
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
62 /* A C++ token. */
64 typedef struct cp_token GTY (())
66 /* The kind of token. */
67 ENUM_BITFIELD (cpp_ttype) type : 8;
68 /* If this token is a keyword, this value indicates which keyword.
69 Otherwise, this value is RID_MAX. */
70 ENUM_BITFIELD (rid) keyword : 8;
71 /* Token flags. */
72 unsigned char flags;
73 /* Identifier for the pragma. */
74 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
75 /* True if this token is from a system header. */
76 BOOL_BITFIELD in_system_header : 1;
77 /* True if this token is from a context where it is implicitly extern "C" */
78 BOOL_BITFIELD implicit_extern_c : 1;
79 /* True for a CPP_NAME token that is not a keyword (i.e., for which
80 KEYWORD is RID_MAX) iff this name was looked up and found to be
81 ambiguous. An error has already been reported. */
82 BOOL_BITFIELD ambiguous_p : 1;
83 /* The input file stack index at which this token was found. */
84 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
85 /* The value associated with this token, if any. */
86 union cp_token_value {
87 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
88 struct tree_check* GTY((tag ("1"))) tree_check_value;
89 /* Use for all other tokens. */
90 tree GTY((tag ("0"))) value;
91 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
92 /* The location at which this token was found. */
93 location_t location;
94 } cp_token;
96 /* We use a stack of token pointer for saving token sets. */
97 typedef struct cp_token *cp_token_position;
98 DEF_VEC_P (cp_token_position);
99 DEF_VEC_ALLOC_P (cp_token_position,heap);
101 static const cp_token eof_token =
103 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
104 #if USE_MAPPED_LOCATION
106 #else
107 {0, 0}
108 #endif
111 /* The cp_lexer structure represents the C++ lexer. It is responsible
112 for managing the token stream from the preprocessor and supplying
113 it to the parser. Tokens are never added to the cp_lexer after
114 it is created. */
116 typedef struct cp_lexer GTY (())
118 /* The memory allocated for the buffer. NULL if this lexer does not
119 own the token buffer. */
120 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
121 /* If the lexer owns the buffer, this is the number of tokens in the
122 buffer. */
123 size_t buffer_length;
125 /* A pointer just past the last available token. The tokens
126 in this lexer are [buffer, last_token). */
127 cp_token_position GTY ((skip)) last_token;
129 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
130 no more available tokens. */
131 cp_token_position GTY ((skip)) next_token;
133 /* A stack indicating positions at which cp_lexer_save_tokens was
134 called. The top entry is the most recent position at which we
135 began saving tokens. If the stack is non-empty, we are saving
136 tokens. */
137 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
139 /* The next lexer in a linked list of lexers. */
140 struct cp_lexer *next;
142 /* True if we should output debugging information. */
143 bool debugging_p;
145 /* True if we're in the context of parsing a pragma, and should not
146 increment past the end-of-line marker. */
147 bool in_pragma;
148 } cp_lexer;
150 /* cp_token_cache is a range of tokens. There is no need to represent
151 allocate heap memory for it, since tokens are never removed from the
152 lexer's array. There is also no need for the GC to walk through
153 a cp_token_cache, since everything in here is referenced through
154 a lexer. */
156 typedef struct cp_token_cache GTY(())
158 /* The beginning of the token range. */
159 cp_token * GTY((skip)) first;
161 /* Points immediately after the last token in the range. */
162 cp_token * GTY ((skip)) last;
163 } cp_token_cache;
165 /* Prototypes. */
167 static cp_lexer *cp_lexer_new_main
168 (void);
169 static cp_lexer *cp_lexer_new_from_tokens
170 (cp_token_cache *tokens);
171 static void cp_lexer_destroy
172 (cp_lexer *);
173 static int cp_lexer_saving_tokens
174 (const cp_lexer *);
175 static cp_token_position cp_lexer_token_position
176 (cp_lexer *, bool);
177 static cp_token *cp_lexer_token_at
178 (cp_lexer *, cp_token_position);
179 static void cp_lexer_get_preprocessor_token
180 (cp_lexer *, cp_token *);
181 static inline cp_token *cp_lexer_peek_token
182 (cp_lexer *);
183 static cp_token *cp_lexer_peek_nth_token
184 (cp_lexer *, size_t);
185 static inline bool cp_lexer_next_token_is
186 (cp_lexer *, enum cpp_ttype);
187 static bool cp_lexer_next_token_is_not
188 (cp_lexer *, enum cpp_ttype);
189 static bool cp_lexer_next_token_is_keyword
190 (cp_lexer *, enum rid);
191 static cp_token *cp_lexer_consume_token
192 (cp_lexer *);
193 static void cp_lexer_purge_token
194 (cp_lexer *);
195 static void cp_lexer_purge_tokens_after
196 (cp_lexer *, cp_token_position);
197 static void cp_lexer_save_tokens
198 (cp_lexer *);
199 static void cp_lexer_commit_tokens
200 (cp_lexer *);
201 static void cp_lexer_rollback_tokens
202 (cp_lexer *);
203 #ifdef ENABLE_CHECKING
204 static void cp_lexer_print_token
205 (FILE *, cp_token *);
206 static inline bool cp_lexer_debugging_p
207 (cp_lexer *);
208 static void cp_lexer_start_debugging
209 (cp_lexer *) ATTRIBUTE_UNUSED;
210 static void cp_lexer_stop_debugging
211 (cp_lexer *) ATTRIBUTE_UNUSED;
212 #else
213 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
214 about passing NULL to functions that require non-NULL arguments
215 (fputs, fprintf). It will never be used, so all we need is a value
216 of the right type that's guaranteed not to be NULL. */
217 #define cp_lexer_debug_stream stdout
218 #define cp_lexer_print_token(str, tok) (void) 0
219 #define cp_lexer_debugging_p(lexer) 0
220 #endif /* ENABLE_CHECKING */
222 static cp_token_cache *cp_token_cache_new
223 (cp_token *, cp_token *);
225 static void cp_parser_initial_pragma
226 (cp_token *);
228 /* Manifest constants. */
229 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
230 #define CP_SAVED_TOKEN_STACK 5
232 /* A token type for keywords, as opposed to ordinary identifiers. */
233 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
235 /* A token type for template-ids. If a template-id is processed while
236 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
237 the value of the CPP_TEMPLATE_ID is whatever was returned by
238 cp_parser_template_id. */
239 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
241 /* A token type for nested-name-specifiers. If a
242 nested-name-specifier is processed while parsing tentatively, it is
243 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
244 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
245 cp_parser_nested_name_specifier_opt. */
246 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
248 /* A token type for tokens that are not tokens at all; these are used
249 to represent slots in the array where there used to be a token
250 that has now been deleted. */
251 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
253 /* The number of token types, including C++-specific ones. */
254 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
256 /* Variables. */
258 #ifdef ENABLE_CHECKING
259 /* The stream to which debugging output should be written. */
260 static FILE *cp_lexer_debug_stream;
261 #endif /* ENABLE_CHECKING */
263 /* Create a new main C++ lexer, the lexer that gets tokens from the
264 preprocessor. */
266 static cp_lexer *
267 cp_lexer_new_main (void)
269 cp_token first_token;
270 cp_lexer *lexer;
271 cp_token *pos;
272 size_t alloc;
273 size_t space;
274 cp_token *buffer;
276 /* It's possible that parsing the first pragma will load a PCH file,
277 which is a GC collection point. So we have to do that before
278 allocating any memory. */
279 cp_parser_initial_pragma (&first_token);
281 /* Tell c_lex_with_flags not to merge string constants. */
282 c_lex_return_raw_strings = true;
284 c_common_no_more_pch ();
286 /* Allocate the memory. */
287 lexer = GGC_CNEW (cp_lexer);
289 #ifdef ENABLE_CHECKING
290 /* Initially we are not debugging. */
291 lexer->debugging_p = false;
292 #endif /* ENABLE_CHECKING */
293 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
294 CP_SAVED_TOKEN_STACK);
296 /* Create the buffer. */
297 alloc = CP_LEXER_BUFFER_SIZE;
298 buffer = GGC_NEWVEC (cp_token, alloc);
300 /* Put the first token in the buffer. */
301 space = alloc;
302 pos = buffer;
303 *pos = first_token;
305 /* Get the remaining tokens from the preprocessor. */
306 while (pos->type != CPP_EOF)
308 pos++;
309 if (!--space)
311 space = alloc;
312 alloc *= 2;
313 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
314 pos = buffer + space;
316 cp_lexer_get_preprocessor_token (lexer, pos);
318 lexer->buffer = buffer;
319 lexer->buffer_length = alloc - space;
320 lexer->last_token = pos;
321 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
323 /* Subsequent preprocessor diagnostics should use compiler
324 diagnostic functions to get the compiler source location. */
325 cpp_get_options (parse_in)->client_diagnostic = true;
326 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
328 gcc_assert (lexer->next_token->type != CPP_PURGED);
329 return lexer;
332 /* Create a new lexer whose token stream is primed with the tokens in
333 CACHE. When these tokens are exhausted, no new tokens will be read. */
335 static cp_lexer *
336 cp_lexer_new_from_tokens (cp_token_cache *cache)
338 cp_token *first = cache->first;
339 cp_token *last = cache->last;
340 cp_lexer *lexer = GGC_CNEW (cp_lexer);
342 /* We do not own the buffer. */
343 lexer->buffer = NULL;
344 lexer->buffer_length = 0;
345 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
346 lexer->last_token = last;
348 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
349 CP_SAVED_TOKEN_STACK);
351 #ifdef ENABLE_CHECKING
352 /* Initially we are not debugging. */
353 lexer->debugging_p = false;
354 #endif
356 gcc_assert (lexer->next_token->type != CPP_PURGED);
357 return lexer;
360 /* Frees all resources associated with LEXER. */
362 static void
363 cp_lexer_destroy (cp_lexer *lexer)
365 if (lexer->buffer)
366 ggc_free (lexer->buffer);
367 VEC_free (cp_token_position, heap, lexer->saved_tokens);
368 ggc_free (lexer);
371 /* Returns nonzero if debugging information should be output. */
373 #ifdef ENABLE_CHECKING
375 static inline bool
376 cp_lexer_debugging_p (cp_lexer *lexer)
378 return lexer->debugging_p;
381 #endif /* ENABLE_CHECKING */
383 static inline cp_token_position
384 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
386 gcc_assert (!previous_p || lexer->next_token != &eof_token);
388 return lexer->next_token - previous_p;
391 static inline cp_token *
392 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
394 return pos;
397 /* nonzero if we are presently saving tokens. */
399 static inline int
400 cp_lexer_saving_tokens (const cp_lexer* lexer)
402 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
405 /* Store the next token from the preprocessor in *TOKEN. Return true
406 if we reach EOF. */
408 static void
409 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
410 cp_token *token)
412 static int is_extern_c = 0;
414 /* Get a new token from the preprocessor. */
415 token->type
416 = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
417 token->input_file_stack_index = input_file_stack_tick;
418 token->keyword = RID_MAX;
419 token->pragma_kind = PRAGMA_NONE;
420 token->in_system_header = in_system_header;
422 /* On some systems, some header files are surrounded by an
423 implicit extern "C" block. Set a flag in the token if it
424 comes from such a header. */
425 is_extern_c += pending_lang_change;
426 pending_lang_change = 0;
427 token->implicit_extern_c = is_extern_c > 0;
429 /* Check to see if this token is a keyword. */
430 if (token->type == CPP_NAME)
432 if (C_IS_RESERVED_WORD (token->u.value))
434 /* Mark this token as a keyword. */
435 token->type = CPP_KEYWORD;
436 /* Record which keyword. */
437 token->keyword = C_RID_CODE (token->u.value);
438 /* Update the value. Some keywords are mapped to particular
439 entities, rather than simply having the value of the
440 corresponding IDENTIFIER_NODE. For example, `__const' is
441 mapped to `const'. */
442 token->u.value = ridpointers[token->keyword];
444 else
446 if (warn_cxx0x_compat
447 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
448 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
450 /* Warn about the C++0x keyword (but still treat it as
451 an identifier). */
452 warning (OPT_Wc__0x_compat,
453 "identifier %<%s%> will become a keyword in C++0x",
454 IDENTIFIER_POINTER (token->u.value));
456 /* Clear out the C_RID_CODE so we don't warn about this
457 particular identifier-turned-keyword again. */
458 C_RID_CODE (token->u.value) = RID_MAX;
461 token->ambiguous_p = false;
462 token->keyword = RID_MAX;
465 /* Handle Objective-C++ keywords. */
466 else if (token->type == CPP_AT_NAME)
468 token->type = CPP_KEYWORD;
469 switch (C_RID_CODE (token->u.value))
471 /* Map 'class' to '@class', 'private' to '@private', etc. */
472 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
473 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
474 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
475 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
476 case RID_THROW: token->keyword = RID_AT_THROW; break;
477 case RID_TRY: token->keyword = RID_AT_TRY; break;
478 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
479 default: token->keyword = C_RID_CODE (token->u.value);
482 else if (token->type == CPP_PRAGMA)
484 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
485 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
486 token->u.value = NULL_TREE;
490 /* Update the globals input_location and in_system_header and the
491 input file stack from TOKEN. */
492 static inline void
493 cp_lexer_set_source_position_from_token (cp_token *token)
495 if (token->type != CPP_EOF)
497 input_location = token->location;
498 in_system_header = token->in_system_header;
499 restore_input_file_stack (token->input_file_stack_index);
503 /* Return a pointer to the next token in the token stream, but do not
504 consume it. */
506 static inline cp_token *
507 cp_lexer_peek_token (cp_lexer *lexer)
509 if (cp_lexer_debugging_p (lexer))
511 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
512 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
513 putc ('\n', cp_lexer_debug_stream);
515 return lexer->next_token;
518 /* Return true if the next token has the indicated TYPE. */
520 static inline bool
521 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
523 return cp_lexer_peek_token (lexer)->type == type;
526 /* Return true if the next token does not have the indicated TYPE. */
528 static inline bool
529 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
531 return !cp_lexer_next_token_is (lexer, type);
534 /* Return true if the next token is the indicated KEYWORD. */
536 static inline bool
537 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
539 return cp_lexer_peek_token (lexer)->keyword == keyword;
542 /* Return true if the next token is a keyword for a decl-specifier. */
544 static bool
545 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
547 cp_token *token;
549 token = cp_lexer_peek_token (lexer);
550 switch (token->keyword)
552 /* Storage classes. */
553 case RID_AUTO:
554 case RID_REGISTER:
555 case RID_STATIC:
556 case RID_EXTERN:
557 case RID_MUTABLE:
558 case RID_THREAD:
559 /* Elaborated type specifiers. */
560 case RID_ENUM:
561 case RID_CLASS:
562 case RID_STRUCT:
563 case RID_UNION:
564 case RID_TYPENAME:
565 /* Simple type specifiers. */
566 case RID_CHAR:
567 case RID_WCHAR:
568 case RID_BOOL:
569 case RID_SHORT:
570 case RID_INT:
571 case RID_LONG:
572 case RID_SIGNED:
573 case RID_UNSIGNED:
574 case RID_FLOAT:
575 case RID_DOUBLE:
576 case RID_VOID:
577 /* GNU extensions. */
578 case RID_ATTRIBUTE:
579 case RID_TYPEOF:
580 return true;
582 default:
583 return false;
587 /* Return a pointer to the Nth token in the token stream. If N is 1,
588 then this is precisely equivalent to cp_lexer_peek_token (except
589 that it is not inline). One would like to disallow that case, but
590 there is one case (cp_parser_nth_token_starts_template_id) where
591 the caller passes a variable for N and it might be 1. */
593 static cp_token *
594 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
596 cp_token *token;
598 /* N is 1-based, not zero-based. */
599 gcc_assert (n > 0);
601 if (cp_lexer_debugging_p (lexer))
602 fprintf (cp_lexer_debug_stream,
603 "cp_lexer: peeking ahead %ld at token: ", (long)n);
605 --n;
606 token = lexer->next_token;
607 gcc_assert (!n || token != &eof_token);
608 while (n != 0)
610 ++token;
611 if (token == lexer->last_token)
613 token = (cp_token *)&eof_token;
614 break;
617 if (token->type != CPP_PURGED)
618 --n;
621 if (cp_lexer_debugging_p (lexer))
623 cp_lexer_print_token (cp_lexer_debug_stream, token);
624 putc ('\n', cp_lexer_debug_stream);
627 return token;
630 /* Return the next token, and advance the lexer's next_token pointer
631 to point to the next non-purged token. */
633 static cp_token *
634 cp_lexer_consume_token (cp_lexer* lexer)
636 cp_token *token = lexer->next_token;
638 gcc_assert (token != &eof_token);
639 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
643 lexer->next_token++;
644 if (lexer->next_token == lexer->last_token)
646 lexer->next_token = (cp_token *)&eof_token;
647 break;
651 while (lexer->next_token->type == CPP_PURGED);
653 cp_lexer_set_source_position_from_token (token);
655 /* Provide debugging output. */
656 if (cp_lexer_debugging_p (lexer))
658 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
659 cp_lexer_print_token (cp_lexer_debug_stream, token);
660 putc ('\n', cp_lexer_debug_stream);
663 return token;
666 /* Permanently remove the next token from the token stream, and
667 advance the next_token pointer to refer to the next non-purged
668 token. */
670 static void
671 cp_lexer_purge_token (cp_lexer *lexer)
673 cp_token *tok = lexer->next_token;
675 gcc_assert (tok != &eof_token);
676 tok->type = CPP_PURGED;
677 tok->location = UNKNOWN_LOCATION;
678 tok->u.value = NULL_TREE;
679 tok->keyword = RID_MAX;
683 tok++;
684 if (tok == lexer->last_token)
686 tok = (cp_token *)&eof_token;
687 break;
690 while (tok->type == CPP_PURGED);
691 lexer->next_token = tok;
694 /* Permanently remove all tokens after TOK, up to, but not
695 including, the token that will be returned next by
696 cp_lexer_peek_token. */
698 static void
699 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
701 cp_token *peek = lexer->next_token;
703 if (peek == &eof_token)
704 peek = lexer->last_token;
706 gcc_assert (tok < peek);
708 for ( tok += 1; tok != peek; tok += 1)
710 tok->type = CPP_PURGED;
711 tok->location = UNKNOWN_LOCATION;
712 tok->u.value = NULL_TREE;
713 tok->keyword = RID_MAX;
717 /* Begin saving tokens. All tokens consumed after this point will be
718 preserved. */
720 static void
721 cp_lexer_save_tokens (cp_lexer* lexer)
723 /* Provide debugging output. */
724 if (cp_lexer_debugging_p (lexer))
725 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
727 VEC_safe_push (cp_token_position, heap,
728 lexer->saved_tokens, lexer->next_token);
731 /* Commit to the portion of the token stream most recently saved. */
733 static void
734 cp_lexer_commit_tokens (cp_lexer* lexer)
736 /* Provide debugging output. */
737 if (cp_lexer_debugging_p (lexer))
738 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
740 VEC_pop (cp_token_position, lexer->saved_tokens);
743 /* Return all tokens saved since the last call to cp_lexer_save_tokens
744 to the token stream. Stop saving tokens. */
746 static void
747 cp_lexer_rollback_tokens (cp_lexer* lexer)
749 /* Provide debugging output. */
750 if (cp_lexer_debugging_p (lexer))
751 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
753 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
756 /* Print a representation of the TOKEN on the STREAM. */
758 #ifdef ENABLE_CHECKING
760 static void
761 cp_lexer_print_token (FILE * stream, cp_token *token)
763 /* We don't use cpp_type2name here because the parser defines
764 a few tokens of its own. */
765 static const char *const token_names[] = {
766 /* cpplib-defined token types */
767 #define OP(e, s) #e,
768 #define TK(e, s) #e,
769 TTYPE_TABLE
770 #undef OP
771 #undef TK
772 /* C++ parser token types - see "Manifest constants", above. */
773 "KEYWORD",
774 "TEMPLATE_ID",
775 "NESTED_NAME_SPECIFIER",
776 "PURGED"
779 /* If we have a name for the token, print it out. Otherwise, we
780 simply give the numeric code. */
781 gcc_assert (token->type < ARRAY_SIZE(token_names));
782 fputs (token_names[token->type], stream);
784 /* For some tokens, print the associated data. */
785 switch (token->type)
787 case CPP_KEYWORD:
788 /* Some keywords have a value that is not an IDENTIFIER_NODE.
789 For example, `struct' is mapped to an INTEGER_CST. */
790 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
791 break;
792 /* else fall through */
793 case CPP_NAME:
794 fputs (IDENTIFIER_POINTER (token->u.value), stream);
795 break;
797 case CPP_STRING:
798 case CPP_WSTRING:
799 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
800 break;
802 default:
803 break;
807 /* Start emitting debugging information. */
809 static void
810 cp_lexer_start_debugging (cp_lexer* lexer)
812 lexer->debugging_p = true;
815 /* Stop emitting debugging information. */
817 static void
818 cp_lexer_stop_debugging (cp_lexer* lexer)
820 lexer->debugging_p = false;
823 #endif /* ENABLE_CHECKING */
825 /* Create a new cp_token_cache, representing a range of tokens. */
827 static cp_token_cache *
828 cp_token_cache_new (cp_token *first, cp_token *last)
830 cp_token_cache *cache = GGC_NEW (cp_token_cache);
831 cache->first = first;
832 cache->last = last;
833 return cache;
837 /* Decl-specifiers. */
839 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
841 static void
842 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
847 /* Declarators. */
849 /* Nothing other than the parser should be creating declarators;
850 declarators are a semi-syntactic representation of C++ entities.
851 Other parts of the front end that need to create entities (like
852 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
854 static cp_declarator *make_call_declarator
855 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
856 static cp_declarator *make_array_declarator
857 (cp_declarator *, tree);
858 static cp_declarator *make_pointer_declarator
859 (cp_cv_quals, cp_declarator *);
860 static cp_declarator *make_reference_declarator
861 (cp_cv_quals, cp_declarator *);
862 static cp_parameter_declarator *make_parameter_declarator
863 (cp_decl_specifier_seq *, cp_declarator *, tree);
864 static cp_declarator *make_ptrmem_declarator
865 (cp_cv_quals, tree, cp_declarator *);
867 /* An erroneous declarator. */
868 static cp_declarator *cp_error_declarator;
870 /* The obstack on which declarators and related data structures are
871 allocated. */
872 static struct obstack declarator_obstack;
874 /* Alloc BYTES from the declarator memory pool. */
876 static inline void *
877 alloc_declarator (size_t bytes)
879 return obstack_alloc (&declarator_obstack, bytes);
882 /* Allocate a declarator of the indicated KIND. Clear fields that are
883 common to all declarators. */
885 static cp_declarator *
886 make_declarator (cp_declarator_kind kind)
888 cp_declarator *declarator;
890 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
891 declarator->kind = kind;
892 declarator->attributes = NULL_TREE;
893 declarator->declarator = NULL;
894 declarator->parameter_pack_p = false;
896 return declarator;
899 /* Make a declarator for a generalized identifier. If
900 QUALIFYING_SCOPE is non-NULL, the identifier is
901 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
902 UNQUALIFIED_NAME. SFK indicates the kind of special function this
903 is, if any. */
905 static cp_declarator *
906 make_id_declarator (tree qualifying_scope, tree unqualified_name,
907 special_function_kind sfk)
909 cp_declarator *declarator;
911 /* It is valid to write:
913 class C { void f(); };
914 typedef C D;
915 void D::f();
917 The standard is not clear about whether `typedef const C D' is
918 legal; as of 2002-09-15 the committee is considering that
919 question. EDG 3.0 allows that syntax. Therefore, we do as
920 well. */
921 if (qualifying_scope && TYPE_P (qualifying_scope))
922 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
924 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
925 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
926 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
928 declarator = make_declarator (cdk_id);
929 declarator->u.id.qualifying_scope = qualifying_scope;
930 declarator->u.id.unqualified_name = unqualified_name;
931 declarator->u.id.sfk = sfk;
933 return declarator;
936 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
937 of modifiers such as const or volatile to apply to the pointer
938 type, represented as identifiers. */
940 cp_declarator *
941 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 cp_declarator *declarator;
945 declarator = make_declarator (cdk_pointer);
946 declarator->declarator = target;
947 declarator->u.pointer.qualifiers = cv_qualifiers;
948 declarator->u.pointer.class_type = NULL_TREE;
949 if (target)
951 declarator->parameter_pack_p = target->parameter_pack_p;
952 target->parameter_pack_p = false;
954 else
955 declarator->parameter_pack_p = false;
957 return declarator;
960 /* Like make_pointer_declarator -- but for references. */
962 cp_declarator *
963 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
965 cp_declarator *declarator;
967 declarator = make_declarator (cdk_reference);
968 declarator->declarator = target;
969 declarator->u.pointer.qualifiers = cv_qualifiers;
970 declarator->u.pointer.class_type = NULL_TREE;
971 if (target)
973 declarator->parameter_pack_p = target->parameter_pack_p;
974 target->parameter_pack_p = false;
976 else
977 declarator->parameter_pack_p = false;
979 return declarator;
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983 member of CLASS_TYPE. */
985 cp_declarator *
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987 cp_declarator *pointee)
989 cp_declarator *declarator;
991 declarator = make_declarator (cdk_ptrmem);
992 declarator->declarator = pointee;
993 declarator->u.pointer.qualifiers = cv_qualifiers;
994 declarator->u.pointer.class_type = class_type;
996 if (pointee)
998 declarator->parameter_pack_p = pointee->parameter_pack_p;
999 pointee->parameter_pack_p = false;
1001 else
1002 declarator->parameter_pack_p = false;
1004 return declarator;
1007 /* Make a declarator for the function given by TARGET, with the
1008 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1009 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1010 indicates what exceptions can be thrown. */
1012 cp_declarator *
1013 make_call_declarator (cp_declarator *target,
1014 cp_parameter_declarator *parms,
1015 cp_cv_quals cv_qualifiers,
1016 tree exception_specification)
1018 cp_declarator *declarator;
1020 declarator = make_declarator (cdk_function);
1021 declarator->declarator = target;
1022 declarator->u.function.parameters = parms;
1023 declarator->u.function.qualifiers = cv_qualifiers;
1024 declarator->u.function.exception_specification = exception_specification;
1025 if (target)
1027 declarator->parameter_pack_p = target->parameter_pack_p;
1028 target->parameter_pack_p = false;
1030 else
1031 declarator->parameter_pack_p = false;
1033 return declarator;
1036 /* Make a declarator for an array of BOUNDS elements, each of which is
1037 defined by ELEMENT. */
1039 cp_declarator *
1040 make_array_declarator (cp_declarator *element, tree bounds)
1042 cp_declarator *declarator;
1044 declarator = make_declarator (cdk_array);
1045 declarator->declarator = element;
1046 declarator->u.array.bounds = bounds;
1047 if (element)
1049 declarator->parameter_pack_p = element->parameter_pack_p;
1050 element->parameter_pack_p = false;
1052 else
1053 declarator->parameter_pack_p = false;
1055 return declarator;
1058 /* Determine whether the declarator we've seen so far can be a
1059 parameter pack, when followed by an ellipsis. */
1060 static bool
1061 declarator_can_be_parameter_pack (cp_declarator *declarator)
1063 /* Search for a declarator name, or any other declarator that goes
1064 after the point where the ellipsis could appear in a parameter
1065 pack. If we find any of these, then this declarator can not be
1066 made into a parameter pack. */
1067 bool found = false;
1068 while (declarator && !found)
1070 switch ((int)declarator->kind)
1072 case cdk_id:
1073 case cdk_error:
1074 case cdk_array:
1075 case cdk_ptrmem:
1076 found = true;
1077 break;
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);
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_elaborated_type_specifier
1682 (cp_parser *, bool, bool);
1683 static tree cp_parser_enum_specifier
1684 (cp_parser *);
1685 static void cp_parser_enumerator_list
1686 (cp_parser *, tree);
1687 static void cp_parser_enumerator_definition
1688 (cp_parser *, tree);
1689 static tree cp_parser_namespace_name
1690 (cp_parser *);
1691 static void cp_parser_namespace_definition
1692 (cp_parser *);
1693 static void cp_parser_namespace_body
1694 (cp_parser *);
1695 static tree cp_parser_qualified_namespace_specifier
1696 (cp_parser *);
1697 static void cp_parser_namespace_alias_definition
1698 (cp_parser *);
1699 static bool cp_parser_using_declaration
1700 (cp_parser *, bool);
1701 static void cp_parser_using_directive
1702 (cp_parser *);
1703 static void cp_parser_asm_definition
1704 (cp_parser *);
1705 static void cp_parser_linkage_specification
1706 (cp_parser *);
1707 static void cp_parser_static_assert
1708 (cp_parser *, bool);
1710 /* Declarators [gram.dcl.decl] */
1712 static tree cp_parser_init_declarator
1713 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1714 static cp_declarator *cp_parser_declarator
1715 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1716 static cp_declarator *cp_parser_direct_declarator
1717 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1718 static enum tree_code cp_parser_ptr_operator
1719 (cp_parser *, tree *, cp_cv_quals *);
1720 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1721 (cp_parser *);
1722 static tree cp_parser_declarator_id
1723 (cp_parser *, bool);
1724 static tree cp_parser_type_id
1725 (cp_parser *);
1726 static void cp_parser_type_specifier_seq
1727 (cp_parser *, bool, cp_decl_specifier_seq *);
1728 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1729 (cp_parser *);
1730 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1731 (cp_parser *, bool *);
1732 static cp_parameter_declarator *cp_parser_parameter_declaration
1733 (cp_parser *, bool, bool *);
1734 static void cp_parser_function_body
1735 (cp_parser *);
1736 static tree cp_parser_initializer
1737 (cp_parser *, bool *, bool *);
1738 static tree cp_parser_initializer_clause
1739 (cp_parser *, bool *);
1740 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1741 (cp_parser *, bool *);
1743 static bool cp_parser_ctor_initializer_opt_and_function_body
1744 (cp_parser *);
1746 /* Classes [gram.class] */
1748 static tree cp_parser_class_name
1749 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1750 static tree cp_parser_class_specifier
1751 (cp_parser *);
1752 static tree cp_parser_class_head
1753 (cp_parser *, bool *, tree *, tree *);
1754 static enum tag_types cp_parser_class_key
1755 (cp_parser *);
1756 static void cp_parser_member_specification_opt
1757 (cp_parser *);
1758 static void cp_parser_member_declaration
1759 (cp_parser *);
1760 static tree cp_parser_pure_specifier
1761 (cp_parser *);
1762 static tree cp_parser_constant_initializer
1763 (cp_parser *);
1765 /* Derived classes [gram.class.derived] */
1767 static tree cp_parser_base_clause
1768 (cp_parser *);
1769 static tree cp_parser_base_specifier
1770 (cp_parser *);
1772 /* Special member functions [gram.special] */
1774 static tree cp_parser_conversion_function_id
1775 (cp_parser *);
1776 static tree cp_parser_conversion_type_id
1777 (cp_parser *);
1778 static cp_declarator *cp_parser_conversion_declarator_opt
1779 (cp_parser *);
1780 static bool cp_parser_ctor_initializer_opt
1781 (cp_parser *);
1782 static void cp_parser_mem_initializer_list
1783 (cp_parser *);
1784 static tree cp_parser_mem_initializer
1785 (cp_parser *);
1786 static tree cp_parser_mem_initializer_id
1787 (cp_parser *);
1789 /* Overloading [gram.over] */
1791 static tree cp_parser_operator_function_id
1792 (cp_parser *);
1793 static tree cp_parser_operator
1794 (cp_parser *);
1796 /* Templates [gram.temp] */
1798 static void cp_parser_template_declaration
1799 (cp_parser *, bool);
1800 static tree cp_parser_template_parameter_list
1801 (cp_parser *);
1802 static tree cp_parser_template_parameter
1803 (cp_parser *, bool *, bool *);
1804 static tree cp_parser_type_parameter
1805 (cp_parser *, bool *);
1806 static tree cp_parser_template_id
1807 (cp_parser *, bool, bool, bool);
1808 static tree cp_parser_template_name
1809 (cp_parser *, bool, bool, bool, bool *);
1810 static tree cp_parser_template_argument_list
1811 (cp_parser *);
1812 static tree cp_parser_template_argument
1813 (cp_parser *);
1814 static void cp_parser_explicit_instantiation
1815 (cp_parser *);
1816 static void cp_parser_explicit_specialization
1817 (cp_parser *);
1819 /* Exception handling [gram.exception] */
1821 static tree cp_parser_try_block
1822 (cp_parser *);
1823 static bool cp_parser_function_try_block
1824 (cp_parser *);
1825 static void cp_parser_handler_seq
1826 (cp_parser *);
1827 static void cp_parser_handler
1828 (cp_parser *);
1829 static tree cp_parser_exception_declaration
1830 (cp_parser *);
1831 static tree cp_parser_throw_expression
1832 (cp_parser *);
1833 static tree cp_parser_exception_specification_opt
1834 (cp_parser *);
1835 static tree cp_parser_type_id_list
1836 (cp_parser *);
1838 /* GNU Extensions */
1840 static tree cp_parser_asm_specification_opt
1841 (cp_parser *);
1842 static tree cp_parser_asm_operand_list
1843 (cp_parser *);
1844 static tree cp_parser_asm_clobber_list
1845 (cp_parser *);
1846 static tree cp_parser_attributes_opt
1847 (cp_parser *);
1848 static tree cp_parser_attribute_list
1849 (cp_parser *);
1850 static bool cp_parser_extension_opt
1851 (cp_parser *, int *);
1852 static void cp_parser_label_declaration
1853 (cp_parser *);
1855 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1856 static bool cp_parser_pragma
1857 (cp_parser *, enum pragma_context);
1859 /* Objective-C++ Productions */
1861 static tree cp_parser_objc_message_receiver
1862 (cp_parser *);
1863 static tree cp_parser_objc_message_args
1864 (cp_parser *);
1865 static tree cp_parser_objc_message_expression
1866 (cp_parser *);
1867 static tree cp_parser_objc_encode_expression
1868 (cp_parser *);
1869 static tree cp_parser_objc_defs_expression
1870 (cp_parser *);
1871 static tree cp_parser_objc_protocol_expression
1872 (cp_parser *);
1873 static tree cp_parser_objc_selector_expression
1874 (cp_parser *);
1875 static tree cp_parser_objc_expression
1876 (cp_parser *);
1877 static bool cp_parser_objc_selector_p
1878 (enum cpp_ttype);
1879 static tree cp_parser_objc_selector
1880 (cp_parser *);
1881 static tree cp_parser_objc_protocol_refs_opt
1882 (cp_parser *);
1883 static void cp_parser_objc_declaration
1884 (cp_parser *);
1885 static tree cp_parser_objc_statement
1886 (cp_parser *);
1888 /* Utility Routines */
1890 static tree cp_parser_lookup_name
1891 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1892 static tree cp_parser_lookup_name_simple
1893 (cp_parser *, tree);
1894 static tree cp_parser_maybe_treat_template_as_class
1895 (tree, bool);
1896 static bool cp_parser_check_declarator_template_parameters
1897 (cp_parser *, cp_declarator *);
1898 static bool cp_parser_check_template_parameters
1899 (cp_parser *, unsigned);
1900 static tree cp_parser_simple_cast_expression
1901 (cp_parser *);
1902 static tree cp_parser_global_scope_opt
1903 (cp_parser *, bool);
1904 static bool cp_parser_constructor_declarator_p
1905 (cp_parser *, bool);
1906 static tree cp_parser_function_definition_from_specifiers_and_declarator
1907 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1908 static tree cp_parser_function_definition_after_declarator
1909 (cp_parser *, bool);
1910 static void cp_parser_template_declaration_after_export
1911 (cp_parser *, bool);
1912 static void cp_parser_perform_template_parameter_access_checks
1913 (VEC (deferred_access_check,gc)*);
1914 static tree cp_parser_single_declaration
1915 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1916 static tree cp_parser_functional_cast
1917 (cp_parser *, tree);
1918 static tree cp_parser_save_member_function_body
1919 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1920 static tree cp_parser_enclosed_template_argument_list
1921 (cp_parser *);
1922 static void cp_parser_save_default_args
1923 (cp_parser *, tree);
1924 static void cp_parser_late_parsing_for_member
1925 (cp_parser *, tree);
1926 static void cp_parser_late_parsing_default_args
1927 (cp_parser *, tree);
1928 static tree cp_parser_sizeof_operand
1929 (cp_parser *, enum rid);
1930 static tree cp_parser_trait_expr
1931 (cp_parser *, enum rid);
1932 static bool cp_parser_declares_only_class_p
1933 (cp_parser *);
1934 static void cp_parser_set_storage_class
1935 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1936 static void cp_parser_set_decl_spec_type
1937 (cp_decl_specifier_seq *, tree, bool);
1938 static bool cp_parser_friend_p
1939 (const cp_decl_specifier_seq *);
1940 static cp_token *cp_parser_require
1941 (cp_parser *, enum cpp_ttype, const char *);
1942 static cp_token *cp_parser_require_keyword
1943 (cp_parser *, enum rid, const char *);
1944 static bool cp_parser_token_starts_function_definition_p
1945 (cp_token *);
1946 static bool cp_parser_next_token_starts_class_definition_p
1947 (cp_parser *);
1948 static bool cp_parser_next_token_ends_template_argument_p
1949 (cp_parser *);
1950 static bool cp_parser_nth_token_starts_template_argument_list_p
1951 (cp_parser *, size_t);
1952 static enum tag_types cp_parser_token_is_class_key
1953 (cp_token *);
1954 static void cp_parser_check_class_key
1955 (enum tag_types, tree type);
1956 static void cp_parser_check_access_in_redeclaration
1957 (tree type);
1958 static bool cp_parser_optional_template_keyword
1959 (cp_parser *);
1960 static void cp_parser_pre_parsed_nested_name_specifier
1961 (cp_parser *);
1962 static void cp_parser_cache_group
1963 (cp_parser *, enum cpp_ttype, unsigned);
1964 static void cp_parser_parse_tentatively
1965 (cp_parser *);
1966 static void cp_parser_commit_to_tentative_parse
1967 (cp_parser *);
1968 static void cp_parser_abort_tentative_parse
1969 (cp_parser *);
1970 static bool cp_parser_parse_definitely
1971 (cp_parser *);
1972 static inline bool cp_parser_parsing_tentatively
1973 (cp_parser *);
1974 static bool cp_parser_uncommitted_to_tentative_parse_p
1975 (cp_parser *);
1976 static void cp_parser_error
1977 (cp_parser *, const char *);
1978 static void cp_parser_name_lookup_error
1979 (cp_parser *, tree, tree, const char *);
1980 static bool cp_parser_simulate_error
1981 (cp_parser *);
1982 static bool cp_parser_check_type_definition
1983 (cp_parser *);
1984 static void cp_parser_check_for_definition_in_return_type
1985 (cp_declarator *, tree);
1986 static void cp_parser_check_for_invalid_template_id
1987 (cp_parser *, tree);
1988 static bool cp_parser_non_integral_constant_expression
1989 (cp_parser *, const char *);
1990 static void cp_parser_diagnose_invalid_type_name
1991 (cp_parser *, tree, tree);
1992 static bool cp_parser_parse_and_diagnose_invalid_type_name
1993 (cp_parser *);
1994 static int cp_parser_skip_to_closing_parenthesis
1995 (cp_parser *, bool, bool, bool);
1996 static void cp_parser_skip_to_end_of_statement
1997 (cp_parser *);
1998 static void cp_parser_consume_semicolon_at_end_of_statement
1999 (cp_parser *);
2000 static void cp_parser_skip_to_end_of_block_or_statement
2001 (cp_parser *);
2002 static void cp_parser_skip_to_closing_brace
2003 (cp_parser *);
2004 static void cp_parser_skip_to_end_of_template_parameter_list
2005 (cp_parser *);
2006 static void cp_parser_skip_to_pragma_eol
2007 (cp_parser*, cp_token *);
2008 static bool cp_parser_error_occurred
2009 (cp_parser *);
2010 static bool cp_parser_allow_gnu_extensions_p
2011 (cp_parser *);
2012 static bool cp_parser_is_string_literal
2013 (cp_token *);
2014 static bool cp_parser_is_keyword
2015 (cp_token *, enum rid);
2016 static tree cp_parser_make_typename_type
2017 (cp_parser *, tree, tree);
2019 /* Returns nonzero if we are parsing tentatively. */
2021 static inline bool
2022 cp_parser_parsing_tentatively (cp_parser* parser)
2024 return parser->context->next != NULL;
2027 /* Returns nonzero if TOKEN is a string literal. */
2029 static bool
2030 cp_parser_is_string_literal (cp_token* token)
2032 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2035 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2037 static bool
2038 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2040 return token->keyword == keyword;
2043 /* If not parsing tentatively, issue a diagnostic of the form
2044 FILE:LINE: MESSAGE before TOKEN
2045 where TOKEN is the next token in the input stream. MESSAGE
2046 (specified by the caller) is usually of the form "expected
2047 OTHER-TOKEN". */
2049 static void
2050 cp_parser_error (cp_parser* parser, const char* message)
2052 if (!cp_parser_simulate_error (parser))
2054 cp_token *token = cp_lexer_peek_token (parser->lexer);
2055 /* This diagnostic makes more sense if it is tagged to the line
2056 of the token we just peeked at. */
2057 cp_lexer_set_source_position_from_token (token);
2059 if (token->type == CPP_PRAGMA)
2061 error ("%<#pragma%> is not allowed here");
2062 cp_parser_skip_to_pragma_eol (parser, token);
2063 return;
2066 c_parse_error (message,
2067 /* Because c_parser_error does not understand
2068 CPP_KEYWORD, keywords are treated like
2069 identifiers. */
2070 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2071 token->u.value);
2075 /* Issue an error about name-lookup failing. NAME is the
2076 IDENTIFIER_NODE DECL is the result of
2077 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2078 the thing that we hoped to find. */
2080 static void
2081 cp_parser_name_lookup_error (cp_parser* parser,
2082 tree name,
2083 tree decl,
2084 const char* desired)
2086 /* If name lookup completely failed, tell the user that NAME was not
2087 declared. */
2088 if (decl == error_mark_node)
2090 if (parser->scope && parser->scope != global_namespace)
2091 error ("%<%E::%E%> has not been declared",
2092 parser->scope, name);
2093 else if (parser->scope == global_namespace)
2094 error ("%<::%E%> has not been declared", name);
2095 else if (parser->object_scope
2096 && !CLASS_TYPE_P (parser->object_scope))
2097 error ("request for member %qE in non-class type %qT",
2098 name, parser->object_scope);
2099 else if (parser->object_scope)
2100 error ("%<%T::%E%> has not been declared",
2101 parser->object_scope, name);
2102 else
2103 error ("%qE has not been declared", name);
2105 else if (parser->scope && parser->scope != global_namespace)
2106 error ("%<%E::%E%> %s", parser->scope, name, desired);
2107 else if (parser->scope == global_namespace)
2108 error ("%<::%E%> %s", name, desired);
2109 else
2110 error ("%qE %s", name, desired);
2113 /* If we are parsing tentatively, remember that an error has occurred
2114 during this tentative parse. Returns true if the error was
2115 simulated; false if a message should be issued by the caller. */
2117 static bool
2118 cp_parser_simulate_error (cp_parser* parser)
2120 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2122 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2123 return true;
2125 return false;
2128 /* Check for repeated decl-specifiers. */
2130 static void
2131 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2133 cp_decl_spec ds;
2135 for (ds = ds_first; ds != ds_last; ++ds)
2137 unsigned count = decl_specs->specs[(int)ds];
2138 if (count < 2)
2139 continue;
2140 /* The "long" specifier is a special case because of "long long". */
2141 if (ds == ds_long)
2143 if (count > 2)
2144 error ("%<long long long%> is too long for GCC");
2145 else if (pedantic && !in_system_header && warn_long_long)
2146 pedwarn ("ISO C++ does not support %<long long%>");
2148 else if (count > 1)
2150 static const char *const decl_spec_names[] = {
2151 "signed",
2152 "unsigned",
2153 "short",
2154 "long",
2155 "const",
2156 "volatile",
2157 "restrict",
2158 "inline",
2159 "virtual",
2160 "explicit",
2161 "friend",
2162 "typedef",
2163 "__complex",
2164 "__thread"
2166 error ("duplicate %qs", decl_spec_names[(int)ds]);
2171 /* This function is called when a type is defined. If type
2172 definitions are forbidden at this point, an error message is
2173 issued. */
2175 static bool
2176 cp_parser_check_type_definition (cp_parser* parser)
2178 /* If types are forbidden here, issue a message. */
2179 if (parser->type_definition_forbidden_message)
2181 /* Use `%s' to print the string in case there are any escape
2182 characters in the message. */
2183 error ("%s", parser->type_definition_forbidden_message);
2184 return false;
2186 return true;
2189 /* This function is called when the DECLARATOR is processed. The TYPE
2190 was a type defined in the decl-specifiers. If it is invalid to
2191 define a type in the decl-specifiers for DECLARATOR, an error is
2192 issued. */
2194 static void
2195 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2196 tree type)
2198 /* [dcl.fct] forbids type definitions in return types.
2199 Unfortunately, it's not easy to know whether or not we are
2200 processing a return type until after the fact. */
2201 while (declarator
2202 && (declarator->kind == cdk_pointer
2203 || declarator->kind == cdk_reference
2204 || declarator->kind == cdk_ptrmem))
2205 declarator = declarator->declarator;
2206 if (declarator
2207 && declarator->kind == cdk_function)
2209 error ("new types may not be defined in a return type");
2210 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2211 type);
2215 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2216 "<" in any valid C++ program. If the next token is indeed "<",
2217 issue a message warning the user about what appears to be an
2218 invalid attempt to form a template-id. */
2220 static void
2221 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2222 tree type)
2224 cp_token_position start = 0;
2226 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2228 if (TYPE_P (type))
2229 error ("%qT is not a template", type);
2230 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2231 error ("%qE is not a template", type);
2232 else
2233 error ("invalid template-id");
2234 /* Remember the location of the invalid "<". */
2235 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2236 start = cp_lexer_token_position (parser->lexer, true);
2237 /* Consume the "<". */
2238 cp_lexer_consume_token (parser->lexer);
2239 /* Parse the template arguments. */
2240 cp_parser_enclosed_template_argument_list (parser);
2241 /* Permanently remove the invalid template arguments so that
2242 this error message is not issued again. */
2243 if (start)
2244 cp_lexer_purge_tokens_after (parser->lexer, start);
2248 /* If parsing an integral constant-expression, issue an error message
2249 about the fact that THING appeared and return true. Otherwise,
2250 return false. In either case, set
2251 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2253 static bool
2254 cp_parser_non_integral_constant_expression (cp_parser *parser,
2255 const char *thing)
2257 parser->non_integral_constant_expression_p = true;
2258 if (parser->integral_constant_expression_p)
2260 if (!parser->allow_non_integral_constant_expression_p)
2262 error ("%s cannot appear in a constant-expression", thing);
2263 return true;
2266 return false;
2269 /* Emit a diagnostic for an invalid type name. SCOPE is the
2270 qualifying scope (or NULL, if none) for ID. This function commits
2271 to the current active tentative parse, if any. (Otherwise, the
2272 problematic construct might be encountered again later, resulting
2273 in duplicate error messages.) */
2275 static void
2276 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2278 tree decl, old_scope;
2279 /* Try to lookup the identifier. */
2280 old_scope = parser->scope;
2281 parser->scope = scope;
2282 decl = cp_parser_lookup_name_simple (parser, id);
2283 parser->scope = old_scope;
2284 /* If the lookup found a template-name, it means that the user forgot
2285 to specify an argument list. Emit a useful error message. */
2286 if (TREE_CODE (decl) == TEMPLATE_DECL)
2287 error ("invalid use of template-name %qE without an argument list", decl);
2288 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2289 error ("invalid use of destructor %qD as a type", id);
2290 else if (TREE_CODE (decl) == TYPE_DECL)
2291 /* Something like 'unsigned A a;' */
2292 error ("invalid combination of multiple type-specifiers");
2293 else if (!parser->scope)
2295 /* Issue an error message. */
2296 error ("%qE does not name a type", id);
2297 /* If we're in a template class, it's possible that the user was
2298 referring to a type from a base class. For example:
2300 template <typename T> struct A { typedef T X; };
2301 template <typename T> struct B : public A<T> { X x; };
2303 The user should have said "typename A<T>::X". */
2304 if (processing_template_decl && current_class_type
2305 && TYPE_BINFO (current_class_type))
2307 tree b;
2309 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2311 b = TREE_CHAIN (b))
2313 tree base_type = BINFO_TYPE (b);
2314 if (CLASS_TYPE_P (base_type)
2315 && dependent_type_p (base_type))
2317 tree field;
2318 /* Go from a particular instantiation of the
2319 template (which will have an empty TYPE_FIELDs),
2320 to the main version. */
2321 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2322 for (field = TYPE_FIELDS (base_type);
2323 field;
2324 field = TREE_CHAIN (field))
2325 if (TREE_CODE (field) == TYPE_DECL
2326 && DECL_NAME (field) == id)
2328 inform ("(perhaps %<typename %T::%E%> was intended)",
2329 BINFO_TYPE (b), id);
2330 break;
2332 if (field)
2333 break;
2338 /* Here we diagnose qualified-ids where the scope is actually correct,
2339 but the identifier does not resolve to a valid type name. */
2340 else if (parser->scope != error_mark_node)
2342 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2343 error ("%qE in namespace %qE does not name a type",
2344 id, parser->scope);
2345 else if (TYPE_P (parser->scope))
2346 error ("%qE in class %qT does not name a type", id, parser->scope);
2347 else
2348 gcc_unreachable ();
2350 cp_parser_commit_to_tentative_parse (parser);
2353 /* Check for a common situation where a type-name should be present,
2354 but is not, and issue a sensible error message. Returns true if an
2355 invalid type-name was detected.
2357 The situation handled by this function are variable declarations of the
2358 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2359 Usually, `ID' should name a type, but if we got here it means that it
2360 does not. We try to emit the best possible error message depending on
2361 how exactly the id-expression looks like. */
2363 static bool
2364 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2366 tree id;
2368 cp_parser_parse_tentatively (parser);
2369 id = cp_parser_id_expression (parser,
2370 /*template_keyword_p=*/false,
2371 /*check_dependency_p=*/true,
2372 /*template_p=*/NULL,
2373 /*declarator_p=*/true,
2374 /*optional_p=*/false);
2375 /* After the id-expression, there should be a plain identifier,
2376 otherwise this is not a simple variable declaration. Also, if
2377 the scope is dependent, we cannot do much. */
2378 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2379 || (parser->scope && TYPE_P (parser->scope)
2380 && dependent_type_p (parser->scope))
2381 || TREE_CODE (id) == TYPE_DECL)
2383 cp_parser_abort_tentative_parse (parser);
2384 return false;
2386 if (!cp_parser_parse_definitely (parser))
2387 return false;
2389 /* Emit a diagnostic for the invalid type. */
2390 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2391 /* Skip to the end of the declaration; there's no point in
2392 trying to process it. */
2393 cp_parser_skip_to_end_of_block_or_statement (parser);
2394 return true;
2397 /* Consume tokens up to, and including, the next non-nested closing `)'.
2398 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2399 are doing error recovery. Returns -1 if OR_COMMA is true and we
2400 found an unnested comma. */
2402 static int
2403 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2404 bool recovering,
2405 bool or_comma,
2406 bool consume_paren)
2408 unsigned paren_depth = 0;
2409 unsigned brace_depth = 0;
2411 if (recovering && !or_comma
2412 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2413 return 0;
2415 while (true)
2417 cp_token * token = cp_lexer_peek_token (parser->lexer);
2419 switch (token->type)
2421 case CPP_EOF:
2422 case CPP_PRAGMA_EOL:
2423 /* If we've run out of tokens, then there is no closing `)'. */
2424 return 0;
2426 case CPP_SEMICOLON:
2427 /* This matches the processing in skip_to_end_of_statement. */
2428 if (!brace_depth)
2429 return 0;
2430 break;
2432 case CPP_OPEN_BRACE:
2433 ++brace_depth;
2434 break;
2435 case CPP_CLOSE_BRACE:
2436 if (!brace_depth--)
2437 return 0;
2438 break;
2440 case CPP_COMMA:
2441 if (recovering && or_comma && !brace_depth && !paren_depth)
2442 return -1;
2443 break;
2445 case CPP_OPEN_PAREN:
2446 if (!brace_depth)
2447 ++paren_depth;
2448 break;
2450 case CPP_CLOSE_PAREN:
2451 if (!brace_depth && !paren_depth--)
2453 if (consume_paren)
2454 cp_lexer_consume_token (parser->lexer);
2455 return 1;
2457 break;
2459 default:
2460 break;
2463 /* Consume the token. */
2464 cp_lexer_consume_token (parser->lexer);
2468 /* Consume tokens until we reach the end of the current statement.
2469 Normally, that will be just before consuming a `;'. However, if a
2470 non-nested `}' comes first, then we stop before consuming that. */
2472 static void
2473 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2475 unsigned nesting_depth = 0;
2477 while (true)
2479 cp_token *token = cp_lexer_peek_token (parser->lexer);
2481 switch (token->type)
2483 case CPP_EOF:
2484 case CPP_PRAGMA_EOL:
2485 /* If we've run out of tokens, stop. */
2486 return;
2488 case CPP_SEMICOLON:
2489 /* If the next token is a `;', we have reached the end of the
2490 statement. */
2491 if (!nesting_depth)
2492 return;
2493 break;
2495 case CPP_CLOSE_BRACE:
2496 /* If this is a non-nested '}', stop before consuming it.
2497 That way, when confronted with something like:
2499 { 3 + }
2501 we stop before consuming the closing '}', even though we
2502 have not yet reached a `;'. */
2503 if (nesting_depth == 0)
2504 return;
2506 /* If it is the closing '}' for a block that we have
2507 scanned, stop -- but only after consuming the token.
2508 That way given:
2510 void f g () { ... }
2511 typedef int I;
2513 we will stop after the body of the erroneously declared
2514 function, but before consuming the following `typedef'
2515 declaration. */
2516 if (--nesting_depth == 0)
2518 cp_lexer_consume_token (parser->lexer);
2519 return;
2522 case CPP_OPEN_BRACE:
2523 ++nesting_depth;
2524 break;
2526 default:
2527 break;
2530 /* Consume the token. */
2531 cp_lexer_consume_token (parser->lexer);
2535 /* This function is called at the end of a statement or declaration.
2536 If the next token is a semicolon, it is consumed; otherwise, error
2537 recovery is attempted. */
2539 static void
2540 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2542 /* Look for the trailing `;'. */
2543 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2545 /* If there is additional (erroneous) input, skip to the end of
2546 the statement. */
2547 cp_parser_skip_to_end_of_statement (parser);
2548 /* If the next token is now a `;', consume it. */
2549 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2550 cp_lexer_consume_token (parser->lexer);
2554 /* Skip tokens until we have consumed an entire block, or until we
2555 have consumed a non-nested `;'. */
2557 static void
2558 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2560 int nesting_depth = 0;
2562 while (nesting_depth >= 0)
2564 cp_token *token = cp_lexer_peek_token (parser->lexer);
2566 switch (token->type)
2568 case CPP_EOF:
2569 case CPP_PRAGMA_EOL:
2570 /* If we've run out of tokens, stop. */
2571 return;
2573 case CPP_SEMICOLON:
2574 /* Stop if this is an unnested ';'. */
2575 if (!nesting_depth)
2576 nesting_depth = -1;
2577 break;
2579 case CPP_CLOSE_BRACE:
2580 /* Stop if this is an unnested '}', or closes the outermost
2581 nesting level. */
2582 nesting_depth--;
2583 if (!nesting_depth)
2584 nesting_depth = -1;
2585 break;
2587 case CPP_OPEN_BRACE:
2588 /* Nest. */
2589 nesting_depth++;
2590 break;
2592 default:
2593 break;
2596 /* Consume the token. */
2597 cp_lexer_consume_token (parser->lexer);
2601 /* Skip tokens until a non-nested closing curly brace is the next
2602 token. */
2604 static void
2605 cp_parser_skip_to_closing_brace (cp_parser *parser)
2607 unsigned nesting_depth = 0;
2609 while (true)
2611 cp_token *token = cp_lexer_peek_token (parser->lexer);
2613 switch (token->type)
2615 case CPP_EOF:
2616 case CPP_PRAGMA_EOL:
2617 /* If we've run out of tokens, stop. */
2618 return;
2620 case CPP_CLOSE_BRACE:
2621 /* If the next token is a non-nested `}', then we have reached
2622 the end of the current block. */
2623 if (nesting_depth-- == 0)
2624 return;
2625 break;
2627 case CPP_OPEN_BRACE:
2628 /* If it the next token is a `{', then we are entering a new
2629 block. Consume the entire block. */
2630 ++nesting_depth;
2631 break;
2633 default:
2634 break;
2637 /* Consume the token. */
2638 cp_lexer_consume_token (parser->lexer);
2642 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2643 parameter is the PRAGMA token, allowing us to purge the entire pragma
2644 sequence. */
2646 static void
2647 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2649 cp_token *token;
2651 parser->lexer->in_pragma = false;
2654 token = cp_lexer_consume_token (parser->lexer);
2655 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2657 /* Ensure that the pragma is not parsed again. */
2658 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2661 /* Require pragma end of line, resyncing with it as necessary. The
2662 arguments are as for cp_parser_skip_to_pragma_eol. */
2664 static void
2665 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2667 parser->lexer->in_pragma = false;
2668 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2669 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2672 /* This is a simple wrapper around make_typename_type. When the id is
2673 an unresolved identifier node, we can provide a superior diagnostic
2674 using cp_parser_diagnose_invalid_type_name. */
2676 static tree
2677 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2679 tree result;
2680 if (TREE_CODE (id) == IDENTIFIER_NODE)
2682 result = make_typename_type (scope, id, typename_type,
2683 /*complain=*/tf_none);
2684 if (result == error_mark_node)
2685 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2686 return result;
2688 return make_typename_type (scope, id, typename_type, tf_error);
2692 /* Create a new C++ parser. */
2694 static cp_parser *
2695 cp_parser_new (void)
2697 cp_parser *parser;
2698 cp_lexer *lexer;
2699 unsigned i;
2701 /* cp_lexer_new_main is called before calling ggc_alloc because
2702 cp_lexer_new_main might load a PCH file. */
2703 lexer = cp_lexer_new_main ();
2705 /* Initialize the binops_by_token so that we can get the tree
2706 directly from the token. */
2707 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2708 binops_by_token[binops[i].token_type] = binops[i];
2710 parser = GGC_CNEW (cp_parser);
2711 parser->lexer = lexer;
2712 parser->context = cp_parser_context_new (NULL);
2714 /* For now, we always accept GNU extensions. */
2715 parser->allow_gnu_extensions_p = 1;
2717 /* The `>' token is a greater-than operator, not the end of a
2718 template-id. */
2719 parser->greater_than_is_operator_p = true;
2721 parser->default_arg_ok_p = true;
2723 /* We are not parsing a constant-expression. */
2724 parser->integral_constant_expression_p = false;
2725 parser->allow_non_integral_constant_expression_p = false;
2726 parser->non_integral_constant_expression_p = false;
2728 /* Local variable names are not forbidden. */
2729 parser->local_variables_forbidden_p = false;
2731 /* We are not processing an `extern "C"' declaration. */
2732 parser->in_unbraced_linkage_specification_p = false;
2734 /* We are not processing a declarator. */
2735 parser->in_declarator_p = false;
2737 /* We are not processing a template-argument-list. */
2738 parser->in_template_argument_list_p = false;
2740 /* We are not in an iteration statement. */
2741 parser->in_statement = 0;
2743 /* We are not in a switch statement. */
2744 parser->in_switch_statement_p = false;
2746 /* We are not parsing a type-id inside an expression. */
2747 parser->in_type_id_in_expr_p = false;
2749 /* Declarations aren't implicitly extern "C". */
2750 parser->implicit_extern_c = false;
2752 /* String literals should be translated to the execution character set. */
2753 parser->translate_strings_p = true;
2755 /* We are not parsing a function body. */
2756 parser->in_function_body = false;
2758 /* The unparsed function queue is empty. */
2759 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2761 /* There are no classes being defined. */
2762 parser->num_classes_being_defined = 0;
2764 /* No template parameters apply. */
2765 parser->num_template_parameter_lists = 0;
2767 return parser;
2770 /* Create a cp_lexer structure which will emit the tokens in CACHE
2771 and push it onto the parser's lexer stack. This is used for delayed
2772 parsing of in-class method bodies and default arguments, and should
2773 not be confused with tentative parsing. */
2774 static void
2775 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2777 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2778 lexer->next = parser->lexer;
2779 parser->lexer = lexer;
2781 /* Move the current source position to that of the first token in the
2782 new lexer. */
2783 cp_lexer_set_source_position_from_token (lexer->next_token);
2786 /* Pop the top lexer off the parser stack. This is never used for the
2787 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2788 static void
2789 cp_parser_pop_lexer (cp_parser *parser)
2791 cp_lexer *lexer = parser->lexer;
2792 parser->lexer = lexer->next;
2793 cp_lexer_destroy (lexer);
2795 /* Put the current source position back where it was before this
2796 lexer was pushed. */
2797 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2800 /* Lexical conventions [gram.lex] */
2802 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2803 identifier. */
2805 static tree
2806 cp_parser_identifier (cp_parser* parser)
2808 cp_token *token;
2810 /* Look for the identifier. */
2811 token = cp_parser_require (parser, CPP_NAME, "identifier");
2812 /* Return the value. */
2813 return token ? token->u.value : error_mark_node;
2816 /* Parse a sequence of adjacent string constants. Returns a
2817 TREE_STRING representing the combined, nul-terminated string
2818 constant. If TRANSLATE is true, translate the string to the
2819 execution character set. If WIDE_OK is true, a wide string is
2820 invalid here.
2822 C++98 [lex.string] says that if a narrow string literal token is
2823 adjacent to a wide string literal token, the behavior is undefined.
2824 However, C99 6.4.5p4 says that this results in a wide string literal.
2825 We follow C99 here, for consistency with the C front end.
2827 This code is largely lifted from lex_string() in c-lex.c.
2829 FUTURE: ObjC++ will need to handle @-strings here. */
2830 static tree
2831 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2833 tree value;
2834 bool wide = false;
2835 size_t count;
2836 struct obstack str_ob;
2837 cpp_string str, istr, *strs;
2838 cp_token *tok;
2840 tok = cp_lexer_peek_token (parser->lexer);
2841 if (!cp_parser_is_string_literal (tok))
2843 cp_parser_error (parser, "expected string-literal");
2844 return error_mark_node;
2847 /* Try to avoid the overhead of creating and destroying an obstack
2848 for the common case of just one string. */
2849 if (!cp_parser_is_string_literal
2850 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2852 cp_lexer_consume_token (parser->lexer);
2854 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2855 str.len = TREE_STRING_LENGTH (tok->u.value);
2856 count = 1;
2857 if (tok->type == CPP_WSTRING)
2858 wide = true;
2860 strs = &str;
2862 else
2864 gcc_obstack_init (&str_ob);
2865 count = 0;
2869 cp_lexer_consume_token (parser->lexer);
2870 count++;
2871 str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2872 str.len = TREE_STRING_LENGTH (tok->u.value);
2873 if (tok->type == CPP_WSTRING)
2874 wide = true;
2876 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2878 tok = cp_lexer_peek_token (parser->lexer);
2880 while (cp_parser_is_string_literal (tok));
2882 strs = (cpp_string *) obstack_finish (&str_ob);
2885 if (wide && !wide_ok)
2887 cp_parser_error (parser, "a wide string is invalid in this context");
2888 wide = false;
2891 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2892 (parse_in, strs, count, &istr, wide))
2894 value = build_string (istr.len, (char *)istr.text);
2895 free ((void *)istr.text);
2897 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2898 value = fix_string_type (value);
2900 else
2901 /* cpp_interpret_string has issued an error. */
2902 value = error_mark_node;
2904 if (count > 1)
2905 obstack_free (&str_ob, 0);
2907 return value;
2911 /* Basic concepts [gram.basic] */
2913 /* Parse a translation-unit.
2915 translation-unit:
2916 declaration-seq [opt]
2918 Returns TRUE if all went well. */
2920 static bool
2921 cp_parser_translation_unit (cp_parser* parser)
2923 /* The address of the first non-permanent object on the declarator
2924 obstack. */
2925 static void *declarator_obstack_base;
2927 bool success;
2929 /* Create the declarator obstack, if necessary. */
2930 if (!cp_error_declarator)
2932 gcc_obstack_init (&declarator_obstack);
2933 /* Create the error declarator. */
2934 cp_error_declarator = make_declarator (cdk_error);
2935 /* Create the empty parameter list. */
2936 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2937 /* Remember where the base of the declarator obstack lies. */
2938 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2941 cp_parser_declaration_seq_opt (parser);
2943 /* If there are no tokens left then all went well. */
2944 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2946 /* Get rid of the token array; we don't need it any more. */
2947 cp_lexer_destroy (parser->lexer);
2948 parser->lexer = NULL;
2950 /* This file might have been a context that's implicitly extern
2951 "C". If so, pop the lang context. (Only relevant for PCH.) */
2952 if (parser->implicit_extern_c)
2954 pop_lang_context ();
2955 parser->implicit_extern_c = false;
2958 /* Finish up. */
2959 finish_translation_unit ();
2961 success = true;
2963 else
2965 cp_parser_error (parser, "expected declaration");
2966 success = false;
2969 /* Make sure the declarator obstack was fully cleaned up. */
2970 gcc_assert (obstack_next_free (&declarator_obstack)
2971 == declarator_obstack_base);
2973 /* All went well. */
2974 return success;
2977 /* Expressions [gram.expr] */
2979 /* Parse a primary-expression.
2981 primary-expression:
2982 literal
2983 this
2984 ( expression )
2985 id-expression
2987 GNU Extensions:
2989 primary-expression:
2990 ( compound-statement )
2991 __builtin_va_arg ( assignment-expression , type-id )
2992 __builtin_offsetof ( type-id , offsetof-expression )
2994 C++ Extensions:
2995 __has_nothrow_assign ( type-id )
2996 __has_nothrow_constructor ( type-id )
2997 __has_nothrow_copy ( type-id )
2998 __has_trivial_assign ( type-id )
2999 __has_trivial_constructor ( type-id )
3000 __has_trivial_copy ( type-id )
3001 __has_trivial_destructor ( type-id )
3002 __has_virtual_destructor ( type-id )
3003 __is_abstract ( type-id )
3004 __is_base_of ( type-id , type-id )
3005 __is_class ( type-id )
3006 __is_convertible_to ( type-id , type-id )
3007 __is_empty ( type-id )
3008 __is_enum ( type-id )
3009 __is_pod ( type-id )
3010 __is_polymorphic ( type-id )
3011 __is_union ( type-id )
3013 Objective-C++ Extension:
3015 primary-expression:
3016 objc-expression
3018 literal:
3019 __null
3021 ADDRESS_P is true iff this expression was immediately preceded by
3022 "&" and therefore might denote a pointer-to-member. CAST_P is true
3023 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3024 true iff this expression is a template argument.
3026 Returns a representation of the expression. Upon return, *IDK
3027 indicates what kind of id-expression (if any) was present. */
3029 static tree
3030 cp_parser_primary_expression (cp_parser *parser,
3031 bool address_p,
3032 bool cast_p,
3033 bool template_arg_p,
3034 cp_id_kind *idk)
3036 cp_token *token;
3038 /* Assume the primary expression is not an id-expression. */
3039 *idk = CP_ID_KIND_NONE;
3041 /* Peek at the next token. */
3042 token = cp_lexer_peek_token (parser->lexer);
3043 switch (token->type)
3045 /* literal:
3046 integer-literal
3047 character-literal
3048 floating-literal
3049 string-literal
3050 boolean-literal */
3051 case CPP_CHAR:
3052 case CPP_WCHAR:
3053 case CPP_NUMBER:
3054 token = cp_lexer_consume_token (parser->lexer);
3055 /* Floating-point literals are only allowed in an integral
3056 constant expression if they are cast to an integral or
3057 enumeration type. */
3058 if (TREE_CODE (token->u.value) == REAL_CST
3059 && parser->integral_constant_expression_p
3060 && pedantic)
3062 /* CAST_P will be set even in invalid code like "int(2.7 +
3063 ...)". Therefore, we have to check that the next token
3064 is sure to end the cast. */
3065 if (cast_p)
3067 cp_token *next_token;
3069 next_token = cp_lexer_peek_token (parser->lexer);
3070 if (/* The comma at the end of an
3071 enumerator-definition. */
3072 next_token->type != CPP_COMMA
3073 /* The curly brace at the end of an enum-specifier. */
3074 && next_token->type != CPP_CLOSE_BRACE
3075 /* The end of a statement. */
3076 && next_token->type != CPP_SEMICOLON
3077 /* The end of the cast-expression. */
3078 && next_token->type != CPP_CLOSE_PAREN
3079 /* The end of an array bound. */
3080 && next_token->type != CPP_CLOSE_SQUARE
3081 /* The closing ">" in a template-argument-list. */
3082 && (next_token->type != CPP_GREATER
3083 || parser->greater_than_is_operator_p)
3084 /* C++0x only: A ">>" treated like two ">" tokens,
3085 in a template-argument-list. */
3086 && (next_token->type != CPP_RSHIFT
3087 || !flag_cpp0x
3088 || parser->greater_than_is_operator_p))
3089 cast_p = false;
3092 /* If we are within a cast, then the constraint that the
3093 cast is to an integral or enumeration type will be
3094 checked at that point. If we are not within a cast, then
3095 this code is invalid. */
3096 if (!cast_p)
3097 cp_parser_non_integral_constant_expression
3098 (parser, "floating-point literal");
3100 return token->u.value;
3102 case CPP_STRING:
3103 case CPP_WSTRING:
3104 /* ??? Should wide strings be allowed when parser->translate_strings_p
3105 is false (i.e. in attributes)? If not, we can kill the third
3106 argument to cp_parser_string_literal. */
3107 return cp_parser_string_literal (parser,
3108 parser->translate_strings_p,
3109 true);
3111 case CPP_OPEN_PAREN:
3113 tree expr;
3114 bool saved_greater_than_is_operator_p;
3116 /* Consume the `('. */
3117 cp_lexer_consume_token (parser->lexer);
3118 /* Within a parenthesized expression, a `>' token is always
3119 the greater-than operator. */
3120 saved_greater_than_is_operator_p
3121 = parser->greater_than_is_operator_p;
3122 parser->greater_than_is_operator_p = true;
3123 /* If we see `( { ' then we are looking at the beginning of
3124 a GNU statement-expression. */
3125 if (cp_parser_allow_gnu_extensions_p (parser)
3126 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3128 /* Statement-expressions are not allowed by the standard. */
3129 if (pedantic)
3130 pedwarn ("ISO C++ forbids braced-groups within expressions");
3132 /* And they're not allowed outside of a function-body; you
3133 cannot, for example, write:
3135 int i = ({ int j = 3; j + 1; });
3137 at class or namespace scope. */
3138 if (!parser->in_function_body)
3140 error ("statement-expressions are allowed only inside functions");
3141 cp_parser_skip_to_end_of_block_or_statement (parser);
3142 expr = error_mark_node;
3144 else
3146 /* Start the statement-expression. */
3147 expr = begin_stmt_expr ();
3148 /* Parse the compound-statement. */
3149 cp_parser_compound_statement (parser, expr, false);
3150 /* Finish up. */
3151 expr = finish_stmt_expr (expr, false);
3154 else
3156 /* Parse the parenthesized expression. */
3157 expr = cp_parser_expression (parser, cast_p);
3158 /* Let the front end know that this expression was
3159 enclosed in parentheses. This matters in case, for
3160 example, the expression is of the form `A::B', since
3161 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3162 not. */
3163 finish_parenthesized_expr (expr);
3165 /* The `>' token might be the end of a template-id or
3166 template-parameter-list now. */
3167 parser->greater_than_is_operator_p
3168 = saved_greater_than_is_operator_p;
3169 /* Consume the `)'. */
3170 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3171 cp_parser_skip_to_end_of_statement (parser);
3173 return expr;
3176 case CPP_KEYWORD:
3177 switch (token->keyword)
3179 /* These two are the boolean literals. */
3180 case RID_TRUE:
3181 cp_lexer_consume_token (parser->lexer);
3182 return boolean_true_node;
3183 case RID_FALSE:
3184 cp_lexer_consume_token (parser->lexer);
3185 return boolean_false_node;
3187 /* The `__null' literal. */
3188 case RID_NULL:
3189 cp_lexer_consume_token (parser->lexer);
3190 return null_node;
3192 /* Recognize the `this' keyword. */
3193 case RID_THIS:
3194 cp_lexer_consume_token (parser->lexer);
3195 if (parser->local_variables_forbidden_p)
3197 error ("%<this%> may not be used in this context");
3198 return error_mark_node;
3200 /* Pointers cannot appear in constant-expressions. */
3201 if (cp_parser_non_integral_constant_expression (parser,
3202 "`this'"))
3203 return error_mark_node;
3204 return finish_this_expr ();
3206 /* The `operator' keyword can be the beginning of an
3207 id-expression. */
3208 case RID_OPERATOR:
3209 goto id_expression;
3211 case RID_FUNCTION_NAME:
3212 case RID_PRETTY_FUNCTION_NAME:
3213 case RID_C99_FUNCTION_NAME:
3214 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3215 __func__ are the names of variables -- but they are
3216 treated specially. Therefore, they are handled here,
3217 rather than relying on the generic id-expression logic
3218 below. Grammatically, these names are id-expressions.
3220 Consume the token. */
3221 token = cp_lexer_consume_token (parser->lexer);
3222 /* Look up the name. */
3223 return finish_fname (token->u.value);
3225 case RID_VA_ARG:
3227 tree expression;
3228 tree type;
3230 /* The `__builtin_va_arg' construct is used to handle
3231 `va_arg'. Consume the `__builtin_va_arg' token. */
3232 cp_lexer_consume_token (parser->lexer);
3233 /* Look for the opening `('. */
3234 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3235 /* Now, parse the assignment-expression. */
3236 expression = cp_parser_assignment_expression (parser,
3237 /*cast_p=*/false);
3238 /* Look for the `,'. */
3239 cp_parser_require (parser, CPP_COMMA, "`,'");
3240 /* Parse the type-id. */
3241 type = cp_parser_type_id (parser);
3242 /* Look for the closing `)'. */
3243 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3244 /* Using `va_arg' in a constant-expression is not
3245 allowed. */
3246 if (cp_parser_non_integral_constant_expression (parser,
3247 "`va_arg'"))
3248 return error_mark_node;
3249 return build_x_va_arg (expression, type);
3252 case RID_OFFSETOF:
3253 return cp_parser_builtin_offsetof (parser);
3255 case RID_HAS_NOTHROW_ASSIGN:
3256 case RID_HAS_NOTHROW_CONSTRUCTOR:
3257 case RID_HAS_NOTHROW_COPY:
3258 case RID_HAS_TRIVIAL_ASSIGN:
3259 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3260 case RID_HAS_TRIVIAL_COPY:
3261 case RID_HAS_TRIVIAL_DESTRUCTOR:
3262 case RID_HAS_VIRTUAL_DESTRUCTOR:
3263 case RID_IS_ABSTRACT:
3264 case RID_IS_BASE_OF:
3265 case RID_IS_CLASS:
3266 case RID_IS_CONVERTIBLE_TO:
3267 case RID_IS_EMPTY:
3268 case RID_IS_ENUM:
3269 case RID_IS_POD:
3270 case RID_IS_POLYMORPHIC:
3271 case RID_IS_UNION:
3272 return cp_parser_trait_expr (parser, token->keyword);
3274 /* Objective-C++ expressions. */
3275 case RID_AT_ENCODE:
3276 case RID_AT_PROTOCOL:
3277 case RID_AT_SELECTOR:
3278 return cp_parser_objc_expression (parser);
3280 default:
3281 cp_parser_error (parser, "expected primary-expression");
3282 return error_mark_node;
3285 /* An id-expression can start with either an identifier, a
3286 `::' as the beginning of a qualified-id, or the "operator"
3287 keyword. */
3288 case CPP_NAME:
3289 case CPP_SCOPE:
3290 case CPP_TEMPLATE_ID:
3291 case CPP_NESTED_NAME_SPECIFIER:
3293 tree id_expression;
3294 tree decl;
3295 const char *error_msg;
3296 bool template_p;
3297 bool done;
3299 id_expression:
3300 /* Parse the id-expression. */
3301 id_expression
3302 = cp_parser_id_expression (parser,
3303 /*template_keyword_p=*/false,
3304 /*check_dependency_p=*/true,
3305 &template_p,
3306 /*declarator_p=*/false,
3307 /*optional_p=*/false);
3308 if (id_expression == error_mark_node)
3309 return error_mark_node;
3310 token = cp_lexer_peek_token (parser->lexer);
3311 done = (token->type != CPP_OPEN_SQUARE
3312 && token->type != CPP_OPEN_PAREN
3313 && token->type != CPP_DOT
3314 && token->type != CPP_DEREF
3315 && token->type != CPP_PLUS_PLUS
3316 && token->type != CPP_MINUS_MINUS);
3317 /* If we have a template-id, then no further lookup is
3318 required. If the template-id was for a template-class, we
3319 will sometimes have a TYPE_DECL at this point. */
3320 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3321 || TREE_CODE (id_expression) == TYPE_DECL)
3322 decl = id_expression;
3323 /* Look up the name. */
3324 else
3326 tree ambiguous_decls;
3328 decl = cp_parser_lookup_name (parser, id_expression,
3329 none_type,
3330 template_p,
3331 /*is_namespace=*/false,
3332 /*check_dependency=*/true,
3333 &ambiguous_decls);
3334 /* If the lookup was ambiguous, an error will already have
3335 been issued. */
3336 if (ambiguous_decls)
3337 return error_mark_node;
3339 /* In Objective-C++, an instance variable (ivar) may be preferred
3340 to whatever cp_parser_lookup_name() found. */
3341 decl = objc_lookup_ivar (decl, id_expression);
3343 /* If name lookup gives us a SCOPE_REF, then the
3344 qualifying scope was dependent. */
3345 if (TREE_CODE (decl) == SCOPE_REF)
3346 return decl;
3347 /* Check to see if DECL is a local variable in a context
3348 where that is forbidden. */
3349 if (parser->local_variables_forbidden_p
3350 && local_variable_p (decl))
3352 /* It might be that we only found DECL because we are
3353 trying to be generous with pre-ISO scoping rules.
3354 For example, consider:
3356 int i;
3357 void g() {
3358 for (int i = 0; i < 10; ++i) {}
3359 extern void f(int j = i);
3362 Here, name look up will originally find the out
3363 of scope `i'. We need to issue a warning message,
3364 but then use the global `i'. */
3365 decl = check_for_out_of_scope_variable (decl);
3366 if (local_variable_p (decl))
3368 error ("local variable %qD may not appear in this context",
3369 decl);
3370 return error_mark_node;
3375 decl = (finish_id_expression
3376 (id_expression, decl, parser->scope,
3377 idk,
3378 parser->integral_constant_expression_p,
3379 parser->allow_non_integral_constant_expression_p,
3380 &parser->non_integral_constant_expression_p,
3381 template_p, done, address_p,
3382 template_arg_p,
3383 &error_msg));
3384 if (error_msg)
3385 cp_parser_error (parser, error_msg);
3386 return decl;
3389 /* Anything else is an error. */
3390 default:
3391 /* ...unless we have an Objective-C++ message or string literal,
3392 that is. */
3393 if (c_dialect_objc ()
3394 && (token->type == CPP_OPEN_SQUARE
3395 || token->type == CPP_OBJC_STRING))
3396 return cp_parser_objc_expression (parser);
3398 cp_parser_error (parser, "expected primary-expression");
3399 return error_mark_node;
3403 /* Parse an id-expression.
3405 id-expression:
3406 unqualified-id
3407 qualified-id
3409 qualified-id:
3410 :: [opt] nested-name-specifier template [opt] unqualified-id
3411 :: identifier
3412 :: operator-function-id
3413 :: template-id
3415 Return a representation of the unqualified portion of the
3416 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3417 a `::' or nested-name-specifier.
3419 Often, if the id-expression was a qualified-id, the caller will
3420 want to make a SCOPE_REF to represent the qualified-id. This
3421 function does not do this in order to avoid wastefully creating
3422 SCOPE_REFs when they are not required.
3424 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3425 `template' keyword.
3427 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3428 uninstantiated templates.
3430 If *TEMPLATE_P is non-NULL, it is set to true iff the
3431 `template' keyword is used to explicitly indicate that the entity
3432 named is a template.
3434 If DECLARATOR_P is true, the id-expression is appearing as part of
3435 a declarator, rather than as part of an expression. */
3437 static tree
3438 cp_parser_id_expression (cp_parser *parser,
3439 bool template_keyword_p,
3440 bool check_dependency_p,
3441 bool *template_p,
3442 bool declarator_p,
3443 bool optional_p)
3445 bool global_scope_p;
3446 bool nested_name_specifier_p;
3448 /* Assume the `template' keyword was not used. */
3449 if (template_p)
3450 *template_p = template_keyword_p;
3452 /* Look for the optional `::' operator. */
3453 global_scope_p
3454 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3455 != NULL_TREE);
3456 /* Look for the optional nested-name-specifier. */
3457 nested_name_specifier_p
3458 = (cp_parser_nested_name_specifier_opt (parser,
3459 /*typename_keyword_p=*/false,
3460 check_dependency_p,
3461 /*type_p=*/false,
3462 declarator_p)
3463 != NULL_TREE);
3464 /* If there is a nested-name-specifier, then we are looking at
3465 the first qualified-id production. */
3466 if (nested_name_specifier_p)
3468 tree saved_scope;
3469 tree saved_object_scope;
3470 tree saved_qualifying_scope;
3471 tree unqualified_id;
3472 bool is_template;
3474 /* See if the next token is the `template' keyword. */
3475 if (!template_p)
3476 template_p = &is_template;
3477 *template_p = cp_parser_optional_template_keyword (parser);
3478 /* Name lookup we do during the processing of the
3479 unqualified-id might obliterate SCOPE. */
3480 saved_scope = parser->scope;
3481 saved_object_scope = parser->object_scope;
3482 saved_qualifying_scope = parser->qualifying_scope;
3483 /* Process the final unqualified-id. */
3484 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3485 check_dependency_p,
3486 declarator_p,
3487 /*optional_p=*/false);
3488 /* Restore the SAVED_SCOPE for our caller. */
3489 parser->scope = saved_scope;
3490 parser->object_scope = saved_object_scope;
3491 parser->qualifying_scope = saved_qualifying_scope;
3493 return unqualified_id;
3495 /* Otherwise, if we are in global scope, then we are looking at one
3496 of the other qualified-id productions. */
3497 else if (global_scope_p)
3499 cp_token *token;
3500 tree id;
3502 /* Peek at the next token. */
3503 token = cp_lexer_peek_token (parser->lexer);
3505 /* If it's an identifier, and the next token is not a "<", then
3506 we can avoid the template-id case. This is an optimization
3507 for this common case. */
3508 if (token->type == CPP_NAME
3509 && !cp_parser_nth_token_starts_template_argument_list_p
3510 (parser, 2))
3511 return cp_parser_identifier (parser);
3513 cp_parser_parse_tentatively (parser);
3514 /* Try a template-id. */
3515 id = cp_parser_template_id (parser,
3516 /*template_keyword_p=*/false,
3517 /*check_dependency_p=*/true,
3518 declarator_p);
3519 /* If that worked, we're done. */
3520 if (cp_parser_parse_definitely (parser))
3521 return id;
3523 /* Peek at the next token. (Changes in the token buffer may
3524 have invalidated the pointer obtained above.) */
3525 token = cp_lexer_peek_token (parser->lexer);
3527 switch (token->type)
3529 case CPP_NAME:
3530 return cp_parser_identifier (parser);
3532 case CPP_KEYWORD:
3533 if (token->keyword == RID_OPERATOR)
3534 return cp_parser_operator_function_id (parser);
3535 /* Fall through. */
3537 default:
3538 cp_parser_error (parser, "expected id-expression");
3539 return error_mark_node;
3542 else
3543 return cp_parser_unqualified_id (parser, template_keyword_p,
3544 /*check_dependency_p=*/true,
3545 declarator_p,
3546 optional_p);
3549 /* Parse an unqualified-id.
3551 unqualified-id:
3552 identifier
3553 operator-function-id
3554 conversion-function-id
3555 ~ class-name
3556 template-id
3558 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3559 keyword, in a construct like `A::template ...'.
3561 Returns a representation of unqualified-id. For the `identifier'
3562 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3563 production a BIT_NOT_EXPR is returned; the operand of the
3564 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3565 other productions, see the documentation accompanying the
3566 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3567 names are looked up in uninstantiated templates. If DECLARATOR_P
3568 is true, the unqualified-id is appearing as part of a declarator,
3569 rather than as part of an expression. */
3571 static tree
3572 cp_parser_unqualified_id (cp_parser* parser,
3573 bool template_keyword_p,
3574 bool check_dependency_p,
3575 bool declarator_p,
3576 bool optional_p)
3578 cp_token *token;
3580 /* Peek at the next token. */
3581 token = cp_lexer_peek_token (parser->lexer);
3583 switch (token->type)
3585 case CPP_NAME:
3587 tree id;
3589 /* We don't know yet whether or not this will be a
3590 template-id. */
3591 cp_parser_parse_tentatively (parser);
3592 /* Try a template-id. */
3593 id = cp_parser_template_id (parser, template_keyword_p,
3594 check_dependency_p,
3595 declarator_p);
3596 /* If it worked, we're done. */
3597 if (cp_parser_parse_definitely (parser))
3598 return id;
3599 /* Otherwise, it's an ordinary identifier. */
3600 return cp_parser_identifier (parser);
3603 case CPP_TEMPLATE_ID:
3604 return cp_parser_template_id (parser, template_keyword_p,
3605 check_dependency_p,
3606 declarator_p);
3608 case CPP_COMPL:
3610 tree type_decl;
3611 tree qualifying_scope;
3612 tree object_scope;
3613 tree scope;
3614 bool done;
3616 /* Consume the `~' token. */
3617 cp_lexer_consume_token (parser->lexer);
3618 /* Parse the class-name. The standard, as written, seems to
3619 say that:
3621 template <typename T> struct S { ~S (); };
3622 template <typename T> S<T>::~S() {}
3624 is invalid, since `~' must be followed by a class-name, but
3625 `S<T>' is dependent, and so not known to be a class.
3626 That's not right; we need to look in uninstantiated
3627 templates. A further complication arises from:
3629 template <typename T> void f(T t) {
3630 t.T::~T();
3633 Here, it is not possible to look up `T' in the scope of `T'
3634 itself. We must look in both the current scope, and the
3635 scope of the containing complete expression.
3637 Yet another issue is:
3639 struct S {
3640 int S;
3641 ~S();
3644 S::~S() {}
3646 The standard does not seem to say that the `S' in `~S'
3647 should refer to the type `S' and not the data member
3648 `S::S'. */
3650 /* DR 244 says that we look up the name after the "~" in the
3651 same scope as we looked up the qualifying name. That idea
3652 isn't fully worked out; it's more complicated than that. */
3653 scope = parser->scope;
3654 object_scope = parser->object_scope;
3655 qualifying_scope = parser->qualifying_scope;
3657 /* Check for invalid scopes. */
3658 if (scope == error_mark_node)
3660 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3661 cp_lexer_consume_token (parser->lexer);
3662 return error_mark_node;
3664 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3666 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3667 error ("scope %qT before %<~%> is not a class-name", scope);
3668 cp_parser_simulate_error (parser);
3669 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3670 cp_lexer_consume_token (parser->lexer);
3671 return error_mark_node;
3673 gcc_assert (!scope || TYPE_P (scope));
3675 /* If the name is of the form "X::~X" it's OK. */
3676 token = cp_lexer_peek_token (parser->lexer);
3677 if (scope
3678 && token->type == CPP_NAME
3679 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3680 == CPP_OPEN_PAREN)
3681 && constructor_name_p (token->u.value, scope))
3683 cp_lexer_consume_token (parser->lexer);
3684 return build_nt (BIT_NOT_EXPR, scope);
3687 /* If there was an explicit qualification (S::~T), first look
3688 in the scope given by the qualification (i.e., S). */
3689 done = false;
3690 type_decl = NULL_TREE;
3691 if (scope)
3693 cp_parser_parse_tentatively (parser);
3694 type_decl = cp_parser_class_name (parser,
3695 /*typename_keyword_p=*/false,
3696 /*template_keyword_p=*/false,
3697 none_type,
3698 /*check_dependency=*/false,
3699 /*class_head_p=*/false,
3700 declarator_p);
3701 if (cp_parser_parse_definitely (parser))
3702 done = true;
3704 /* In "N::S::~S", look in "N" as well. */
3705 if (!done && scope && qualifying_scope)
3707 cp_parser_parse_tentatively (parser);
3708 parser->scope = qualifying_scope;
3709 parser->object_scope = NULL_TREE;
3710 parser->qualifying_scope = NULL_TREE;
3711 type_decl
3712 = cp_parser_class_name (parser,
3713 /*typename_keyword_p=*/false,
3714 /*template_keyword_p=*/false,
3715 none_type,
3716 /*check_dependency=*/false,
3717 /*class_head_p=*/false,
3718 declarator_p);
3719 if (cp_parser_parse_definitely (parser))
3720 done = true;
3722 /* In "p->S::~T", look in the scope given by "*p" as well. */
3723 else if (!done && object_scope)
3725 cp_parser_parse_tentatively (parser);
3726 parser->scope = object_scope;
3727 parser->object_scope = NULL_TREE;
3728 parser->qualifying_scope = NULL_TREE;
3729 type_decl
3730 = cp_parser_class_name (parser,
3731 /*typename_keyword_p=*/false,
3732 /*template_keyword_p=*/false,
3733 none_type,
3734 /*check_dependency=*/false,
3735 /*class_head_p=*/false,
3736 declarator_p);
3737 if (cp_parser_parse_definitely (parser))
3738 done = true;
3740 /* Look in the surrounding context. */
3741 if (!done)
3743 parser->scope = NULL_TREE;
3744 parser->object_scope = NULL_TREE;
3745 parser->qualifying_scope = NULL_TREE;
3746 type_decl
3747 = cp_parser_class_name (parser,
3748 /*typename_keyword_p=*/false,
3749 /*template_keyword_p=*/false,
3750 none_type,
3751 /*check_dependency=*/false,
3752 /*class_head_p=*/false,
3753 declarator_p);
3755 /* If an error occurred, assume that the name of the
3756 destructor is the same as the name of the qualifying
3757 class. That allows us to keep parsing after running
3758 into ill-formed destructor names. */
3759 if (type_decl == error_mark_node && scope)
3760 return build_nt (BIT_NOT_EXPR, scope);
3761 else if (type_decl == error_mark_node)
3762 return error_mark_node;
3764 /* Check that destructor name and scope match. */
3765 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3767 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3768 error ("declaration of %<~%T%> as member of %qT",
3769 type_decl, scope);
3770 cp_parser_simulate_error (parser);
3771 return error_mark_node;
3774 /* [class.dtor]
3776 A typedef-name that names a class shall not be used as the
3777 identifier in the declarator for a destructor declaration. */
3778 if (declarator_p
3779 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3780 && !DECL_SELF_REFERENCE_P (type_decl)
3781 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3782 error ("typedef-name %qD used as destructor declarator",
3783 type_decl);
3785 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3788 case CPP_KEYWORD:
3789 if (token->keyword == RID_OPERATOR)
3791 tree id;
3793 /* This could be a template-id, so we try that first. */
3794 cp_parser_parse_tentatively (parser);
3795 /* Try a template-id. */
3796 id = cp_parser_template_id (parser, template_keyword_p,
3797 /*check_dependency_p=*/true,
3798 declarator_p);
3799 /* If that worked, we're done. */
3800 if (cp_parser_parse_definitely (parser))
3801 return id;
3802 /* We still don't know whether we're looking at an
3803 operator-function-id or a conversion-function-id. */
3804 cp_parser_parse_tentatively (parser);
3805 /* Try an operator-function-id. */
3806 id = cp_parser_operator_function_id (parser);
3807 /* If that didn't work, try a conversion-function-id. */
3808 if (!cp_parser_parse_definitely (parser))
3809 id = cp_parser_conversion_function_id (parser);
3811 return id;
3813 /* Fall through. */
3815 default:
3816 if (optional_p)
3817 return NULL_TREE;
3818 cp_parser_error (parser, "expected unqualified-id");
3819 return error_mark_node;
3823 /* Parse an (optional) nested-name-specifier.
3825 nested-name-specifier:
3826 class-or-namespace-name :: nested-name-specifier [opt]
3827 class-or-namespace-name :: template nested-name-specifier [opt]
3829 PARSER->SCOPE should be set appropriately before this function is
3830 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3831 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3832 in name lookups.
3834 Sets PARSER->SCOPE to the class (TYPE) or namespace
3835 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3836 it unchanged if there is no nested-name-specifier. Returns the new
3837 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3839 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3840 part of a declaration and/or decl-specifier. */
3842 static tree
3843 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3844 bool typename_keyword_p,
3845 bool check_dependency_p,
3846 bool type_p,
3847 bool is_declaration)
3849 bool success = false;
3850 cp_token_position start = 0;
3851 cp_token *token;
3853 /* Remember where the nested-name-specifier starts. */
3854 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3856 start = cp_lexer_token_position (parser->lexer, false);
3857 push_deferring_access_checks (dk_deferred);
3860 while (true)
3862 tree new_scope;
3863 tree old_scope;
3864 tree saved_qualifying_scope;
3865 bool template_keyword_p;
3867 /* Spot cases that cannot be the beginning of a
3868 nested-name-specifier. */
3869 token = cp_lexer_peek_token (parser->lexer);
3871 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3872 the already parsed nested-name-specifier. */
3873 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3875 /* Grab the nested-name-specifier and continue the loop. */
3876 cp_parser_pre_parsed_nested_name_specifier (parser);
3877 /* If we originally encountered this nested-name-specifier
3878 with IS_DECLARATION set to false, we will not have
3879 resolved TYPENAME_TYPEs, so we must do so here. */
3880 if (is_declaration
3881 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3883 new_scope = resolve_typename_type (parser->scope,
3884 /*only_current_p=*/false);
3885 if (new_scope != error_mark_node)
3886 parser->scope = new_scope;
3888 success = true;
3889 continue;
3892 /* Spot cases that cannot be the beginning of a
3893 nested-name-specifier. On the second and subsequent times
3894 through the loop, we look for the `template' keyword. */
3895 if (success && token->keyword == RID_TEMPLATE)
3897 /* A template-id can start a nested-name-specifier. */
3898 else if (token->type == CPP_TEMPLATE_ID)
3900 else
3902 /* If the next token is not an identifier, then it is
3903 definitely not a class-or-namespace-name. */
3904 if (token->type != CPP_NAME)
3905 break;
3906 /* If the following token is neither a `<' (to begin a
3907 template-id), nor a `::', then we are not looking at a
3908 nested-name-specifier. */
3909 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3910 if (token->type != CPP_SCOPE
3911 && !cp_parser_nth_token_starts_template_argument_list_p
3912 (parser, 2))
3913 break;
3916 /* The nested-name-specifier is optional, so we parse
3917 tentatively. */
3918 cp_parser_parse_tentatively (parser);
3920 /* Look for the optional `template' keyword, if this isn't the
3921 first time through the loop. */
3922 if (success)
3923 template_keyword_p = cp_parser_optional_template_keyword (parser);
3924 else
3925 template_keyword_p = false;
3927 /* Save the old scope since the name lookup we are about to do
3928 might destroy it. */
3929 old_scope = parser->scope;
3930 saved_qualifying_scope = parser->qualifying_scope;
3931 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3932 look up names in "X<T>::I" in order to determine that "Y" is
3933 a template. So, if we have a typename at this point, we make
3934 an effort to look through it. */
3935 if (is_declaration
3936 && !typename_keyword_p
3937 && parser->scope
3938 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3939 parser->scope = resolve_typename_type (parser->scope,
3940 /*only_current_p=*/false);
3941 /* Parse the qualifying entity. */
3942 new_scope
3943 = cp_parser_class_or_namespace_name (parser,
3944 typename_keyword_p,
3945 template_keyword_p,
3946 check_dependency_p,
3947 type_p,
3948 is_declaration);
3949 /* Look for the `::' token. */
3950 cp_parser_require (parser, CPP_SCOPE, "`::'");
3952 /* If we found what we wanted, we keep going; otherwise, we're
3953 done. */
3954 if (!cp_parser_parse_definitely (parser))
3956 bool error_p = false;
3958 /* Restore the OLD_SCOPE since it was valid before the
3959 failed attempt at finding the last
3960 class-or-namespace-name. */
3961 parser->scope = old_scope;
3962 parser->qualifying_scope = saved_qualifying_scope;
3963 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3964 break;
3965 /* If the next token is an identifier, and the one after
3966 that is a `::', then any valid interpretation would have
3967 found a class-or-namespace-name. */
3968 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3969 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3970 == CPP_SCOPE)
3971 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3972 != CPP_COMPL))
3974 token = cp_lexer_consume_token (parser->lexer);
3975 if (!error_p)
3977 if (!token->ambiguous_p)
3979 tree decl;
3980 tree ambiguous_decls;
3982 decl = cp_parser_lookup_name (parser, token->u.value,
3983 none_type,
3984 /*is_template=*/false,
3985 /*is_namespace=*/false,
3986 /*check_dependency=*/true,
3987 &ambiguous_decls);
3988 if (TREE_CODE (decl) == TEMPLATE_DECL)
3989 error ("%qD used without template parameters", decl);
3990 else if (ambiguous_decls)
3992 error ("reference to %qD is ambiguous",
3993 token->u.value);
3994 print_candidates (ambiguous_decls);
3995 decl = error_mark_node;
3997 else
3998 cp_parser_name_lookup_error
3999 (parser, token->u.value, decl,
4000 "is not a class or namespace");
4002 parser->scope = error_mark_node;
4003 error_p = true;
4004 /* Treat this as a successful nested-name-specifier
4005 due to:
4007 [basic.lookup.qual]
4009 If the name found is not a class-name (clause
4010 _class_) or namespace-name (_namespace.def_), the
4011 program is ill-formed. */
4012 success = true;
4014 cp_lexer_consume_token (parser->lexer);
4016 break;
4018 /* We've found one valid nested-name-specifier. */
4019 success = true;
4020 /* Name lookup always gives us a DECL. */
4021 if (TREE_CODE (new_scope) == TYPE_DECL)
4022 new_scope = TREE_TYPE (new_scope);
4023 /* Uses of "template" must be followed by actual templates. */
4024 if (template_keyword_p
4025 && !(CLASS_TYPE_P (new_scope)
4026 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4027 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4028 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4029 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4030 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4031 == TEMPLATE_ID_EXPR)))
4032 pedwarn (TYPE_P (new_scope)
4033 ? "%qT is not a template"
4034 : "%qD is not a template",
4035 new_scope);
4036 /* If it is a class scope, try to complete it; we are about to
4037 be looking up names inside the class. */
4038 if (TYPE_P (new_scope)
4039 /* Since checking types for dependency can be expensive,
4040 avoid doing it if the type is already complete. */
4041 && !COMPLETE_TYPE_P (new_scope)
4042 /* Do not try to complete dependent types. */
4043 && !dependent_type_p (new_scope))
4044 new_scope = complete_type (new_scope);
4045 /* Make sure we look in the right scope the next time through
4046 the loop. */
4047 parser->scope = new_scope;
4050 /* If parsing tentatively, replace the sequence of tokens that makes
4051 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4052 token. That way, should we re-parse the token stream, we will
4053 not have to repeat the effort required to do the parse, nor will
4054 we issue duplicate error messages. */
4055 if (success && start)
4057 cp_token *token;
4059 token = cp_lexer_token_at (parser->lexer, start);
4060 /* Reset the contents of the START token. */
4061 token->type = CPP_NESTED_NAME_SPECIFIER;
4062 /* Retrieve any deferred checks. Do not pop this access checks yet
4063 so the memory will not be reclaimed during token replacing below. */
4064 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4065 token->u.tree_check_value->value = parser->scope;
4066 token->u.tree_check_value->checks = get_deferred_access_checks ();
4067 token->u.tree_check_value->qualifying_scope =
4068 parser->qualifying_scope;
4069 token->keyword = RID_MAX;
4071 /* Purge all subsequent tokens. */
4072 cp_lexer_purge_tokens_after (parser->lexer, start);
4075 if (start)
4076 pop_to_parent_deferring_access_checks ();
4078 return success ? parser->scope : NULL_TREE;
4081 /* Parse a nested-name-specifier. See
4082 cp_parser_nested_name_specifier_opt for details. This function
4083 behaves identically, except that it will an issue an error if no
4084 nested-name-specifier is present. */
4086 static tree
4087 cp_parser_nested_name_specifier (cp_parser *parser,
4088 bool typename_keyword_p,
4089 bool check_dependency_p,
4090 bool type_p,
4091 bool is_declaration)
4093 tree scope;
4095 /* Look for the nested-name-specifier. */
4096 scope = cp_parser_nested_name_specifier_opt (parser,
4097 typename_keyword_p,
4098 check_dependency_p,
4099 type_p,
4100 is_declaration);
4101 /* If it was not present, issue an error message. */
4102 if (!scope)
4104 cp_parser_error (parser, "expected nested-name-specifier");
4105 parser->scope = NULL_TREE;
4108 return scope;
4111 /* Parse a class-or-namespace-name.
4113 class-or-namespace-name:
4114 class-name
4115 namespace-name
4117 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4118 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4119 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4120 TYPE_P is TRUE iff the next name should be taken as a class-name,
4121 even the same name is declared to be another entity in the same
4122 scope.
4124 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4125 specified by the class-or-namespace-name. If neither is found the
4126 ERROR_MARK_NODE is returned. */
4128 static tree
4129 cp_parser_class_or_namespace_name (cp_parser *parser,
4130 bool typename_keyword_p,
4131 bool template_keyword_p,
4132 bool check_dependency_p,
4133 bool type_p,
4134 bool is_declaration)
4136 tree saved_scope;
4137 tree saved_qualifying_scope;
4138 tree saved_object_scope;
4139 tree scope;
4140 bool only_class_p;
4142 /* Before we try to parse the class-name, we must save away the
4143 current PARSER->SCOPE since cp_parser_class_name will destroy
4144 it. */
4145 saved_scope = parser->scope;
4146 saved_qualifying_scope = parser->qualifying_scope;
4147 saved_object_scope = parser->object_scope;
4148 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4149 there is no need to look for a namespace-name. */
4150 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4151 if (!only_class_p)
4152 cp_parser_parse_tentatively (parser);
4153 scope = cp_parser_class_name (parser,
4154 typename_keyword_p,
4155 template_keyword_p,
4156 type_p ? class_type : none_type,
4157 check_dependency_p,
4158 /*class_head_p=*/false,
4159 is_declaration);
4160 /* If that didn't work, try for a namespace-name. */
4161 if (!only_class_p && !cp_parser_parse_definitely (parser))
4163 /* Restore the saved scope. */
4164 parser->scope = saved_scope;
4165 parser->qualifying_scope = saved_qualifying_scope;
4166 parser->object_scope = saved_object_scope;
4167 /* If we are not looking at an identifier followed by the scope
4168 resolution operator, then this is not part of a
4169 nested-name-specifier. (Note that this function is only used
4170 to parse the components of a nested-name-specifier.) */
4171 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4172 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4173 return error_mark_node;
4174 scope = cp_parser_namespace_name (parser);
4177 return scope;
4180 /* Parse a postfix-expression.
4182 postfix-expression:
4183 primary-expression
4184 postfix-expression [ expression ]
4185 postfix-expression ( expression-list [opt] )
4186 simple-type-specifier ( expression-list [opt] )
4187 typename :: [opt] nested-name-specifier identifier
4188 ( expression-list [opt] )
4189 typename :: [opt] nested-name-specifier template [opt] template-id
4190 ( expression-list [opt] )
4191 postfix-expression . template [opt] id-expression
4192 postfix-expression -> template [opt] id-expression
4193 postfix-expression . pseudo-destructor-name
4194 postfix-expression -> pseudo-destructor-name
4195 postfix-expression ++
4196 postfix-expression --
4197 dynamic_cast < type-id > ( expression )
4198 static_cast < type-id > ( expression )
4199 reinterpret_cast < type-id > ( expression )
4200 const_cast < type-id > ( expression )
4201 typeid ( expression )
4202 typeid ( type-id )
4204 GNU Extension:
4206 postfix-expression:
4207 ( type-id ) { initializer-list , [opt] }
4209 This extension is a GNU version of the C99 compound-literal
4210 construct. (The C99 grammar uses `type-name' instead of `type-id',
4211 but they are essentially the same concept.)
4213 If ADDRESS_P is true, the postfix expression is the operand of the
4214 `&' operator. CAST_P is true if this expression is the target of a
4215 cast.
4217 Returns a representation of the expression. */
4219 static tree
4220 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4222 cp_token *token;
4223 enum rid keyword;
4224 cp_id_kind idk = CP_ID_KIND_NONE;
4225 tree postfix_expression = NULL_TREE;
4227 /* Peek at the next token. */
4228 token = cp_lexer_peek_token (parser->lexer);
4229 /* Some of the productions are determined by keywords. */
4230 keyword = token->keyword;
4231 switch (keyword)
4233 case RID_DYNCAST:
4234 case RID_STATCAST:
4235 case RID_REINTCAST:
4236 case RID_CONSTCAST:
4238 tree type;
4239 tree expression;
4240 const char *saved_message;
4242 /* All of these can be handled in the same way from the point
4243 of view of parsing. Begin by consuming the token
4244 identifying the cast. */
4245 cp_lexer_consume_token (parser->lexer);
4247 /* New types cannot be defined in the cast. */
4248 saved_message = parser->type_definition_forbidden_message;
4249 parser->type_definition_forbidden_message
4250 = "types may not be defined in casts";
4252 /* Look for the opening `<'. */
4253 cp_parser_require (parser, CPP_LESS, "`<'");
4254 /* Parse the type to which we are casting. */
4255 type = cp_parser_type_id (parser);
4256 /* Look for the closing `>'. */
4257 cp_parser_require (parser, CPP_GREATER, "`>'");
4258 /* Restore the old message. */
4259 parser->type_definition_forbidden_message = saved_message;
4261 /* And the expression which is being cast. */
4262 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4263 expression = cp_parser_expression (parser, /*cast_p=*/true);
4264 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4266 /* Only type conversions to integral or enumeration types
4267 can be used in constant-expressions. */
4268 if (!cast_valid_in_integral_constant_expression_p (type)
4269 && (cp_parser_non_integral_constant_expression
4270 (parser,
4271 "a cast to a type other than an integral or "
4272 "enumeration type")))
4273 return error_mark_node;
4275 switch (keyword)
4277 case RID_DYNCAST:
4278 postfix_expression
4279 = build_dynamic_cast (type, expression);
4280 break;
4281 case RID_STATCAST:
4282 postfix_expression
4283 = build_static_cast (type, expression);
4284 break;
4285 case RID_REINTCAST:
4286 postfix_expression
4287 = build_reinterpret_cast (type, expression);
4288 break;
4289 case RID_CONSTCAST:
4290 postfix_expression
4291 = build_const_cast (type, expression);
4292 break;
4293 default:
4294 gcc_unreachable ();
4297 break;
4299 case RID_TYPEID:
4301 tree type;
4302 const char *saved_message;
4303 bool saved_in_type_id_in_expr_p;
4305 /* Consume the `typeid' token. */
4306 cp_lexer_consume_token (parser->lexer);
4307 /* Look for the `(' token. */
4308 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4309 /* Types cannot be defined in a `typeid' expression. */
4310 saved_message = parser->type_definition_forbidden_message;
4311 parser->type_definition_forbidden_message
4312 = "types may not be defined in a `typeid\' expression";
4313 /* We can't be sure yet whether we're looking at a type-id or an
4314 expression. */
4315 cp_parser_parse_tentatively (parser);
4316 /* Try a type-id first. */
4317 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4318 parser->in_type_id_in_expr_p = true;
4319 type = cp_parser_type_id (parser);
4320 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4321 /* Look for the `)' token. Otherwise, we can't be sure that
4322 we're not looking at an expression: consider `typeid (int
4323 (3))', for example. */
4324 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4325 /* If all went well, simply lookup the type-id. */
4326 if (cp_parser_parse_definitely (parser))
4327 postfix_expression = get_typeid (type);
4328 /* Otherwise, fall back to the expression variant. */
4329 else
4331 tree expression;
4333 /* Look for an expression. */
4334 expression = cp_parser_expression (parser, /*cast_p=*/false);
4335 /* Compute its typeid. */
4336 postfix_expression = build_typeid (expression);
4337 /* Look for the `)' token. */
4338 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4340 /* Restore the saved message. */
4341 parser->type_definition_forbidden_message = saved_message;
4342 /* `typeid' may not appear in an integral constant expression. */
4343 if (cp_parser_non_integral_constant_expression(parser,
4344 "`typeid' operator"))
4345 return error_mark_node;
4347 break;
4349 case RID_TYPENAME:
4351 tree type;
4352 /* The syntax permitted here is the same permitted for an
4353 elaborated-type-specifier. */
4354 type = cp_parser_elaborated_type_specifier (parser,
4355 /*is_friend=*/false,
4356 /*is_declaration=*/false);
4357 postfix_expression = cp_parser_functional_cast (parser, type);
4359 break;
4361 default:
4363 tree type;
4365 /* If the next thing is a simple-type-specifier, we may be
4366 looking at a functional cast. We could also be looking at
4367 an id-expression. So, we try the functional cast, and if
4368 that doesn't work we fall back to the primary-expression. */
4369 cp_parser_parse_tentatively (parser);
4370 /* Look for the simple-type-specifier. */
4371 type = cp_parser_simple_type_specifier (parser,
4372 /*decl_specs=*/NULL,
4373 CP_PARSER_FLAGS_NONE);
4374 /* Parse the cast itself. */
4375 if (!cp_parser_error_occurred (parser))
4376 postfix_expression
4377 = cp_parser_functional_cast (parser, type);
4378 /* If that worked, we're done. */
4379 if (cp_parser_parse_definitely (parser))
4380 break;
4382 /* If the functional-cast didn't work out, try a
4383 compound-literal. */
4384 if (cp_parser_allow_gnu_extensions_p (parser)
4385 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4387 VEC(constructor_elt,gc) *initializer_list = NULL;
4388 bool saved_in_type_id_in_expr_p;
4390 cp_parser_parse_tentatively (parser);
4391 /* Consume the `('. */
4392 cp_lexer_consume_token (parser->lexer);
4393 /* Parse the type. */
4394 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4395 parser->in_type_id_in_expr_p = true;
4396 type = cp_parser_type_id (parser);
4397 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4398 /* Look for the `)'. */
4399 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4400 /* Look for the `{'. */
4401 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4402 /* If things aren't going well, there's no need to
4403 keep going. */
4404 if (!cp_parser_error_occurred (parser))
4406 bool non_constant_p;
4407 /* Parse the initializer-list. */
4408 initializer_list
4409 = cp_parser_initializer_list (parser, &non_constant_p);
4410 /* Allow a trailing `,'. */
4411 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4412 cp_lexer_consume_token (parser->lexer);
4413 /* Look for the final `}'. */
4414 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4416 /* If that worked, we're definitely looking at a
4417 compound-literal expression. */
4418 if (cp_parser_parse_definitely (parser))
4420 /* Warn the user that a compound literal is not
4421 allowed in standard C++. */
4422 if (pedantic)
4423 pedwarn ("ISO C++ forbids compound-literals");
4424 /* For simplicity, we disallow compound literals in
4425 constant-expressions. We could
4426 allow compound literals of integer type, whose
4427 initializer was a constant, in constant
4428 expressions. Permitting that usage, as a further
4429 extension, would not change the meaning of any
4430 currently accepted programs. (Of course, as
4431 compound literals are not part of ISO C++, the
4432 standard has nothing to say.) */
4433 if (cp_parser_non_integral_constant_expression
4434 (parser, "non-constant compound literals"))
4436 postfix_expression = error_mark_node;
4437 break;
4439 /* Form the representation of the compound-literal. */
4440 postfix_expression
4441 = finish_compound_literal (type, initializer_list);
4442 break;
4446 /* It must be a primary-expression. */
4447 postfix_expression
4448 = cp_parser_primary_expression (parser, address_p, cast_p,
4449 /*template_arg_p=*/false,
4450 &idk);
4452 break;
4455 /* Keep looping until the postfix-expression is complete. */
4456 while (true)
4458 if (idk == CP_ID_KIND_UNQUALIFIED
4459 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4460 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4461 /* It is not a Koenig lookup function call. */
4462 postfix_expression
4463 = unqualified_name_lookup_error (postfix_expression);
4465 /* Peek at the next token. */
4466 token = cp_lexer_peek_token (parser->lexer);
4468 switch (token->type)
4470 case CPP_OPEN_SQUARE:
4471 postfix_expression
4472 = cp_parser_postfix_open_square_expression (parser,
4473 postfix_expression,
4474 false);
4475 idk = CP_ID_KIND_NONE;
4476 break;
4478 case CPP_OPEN_PAREN:
4479 /* postfix-expression ( expression-list [opt] ) */
4481 bool koenig_p;
4482 bool is_builtin_constant_p;
4483 bool saved_integral_constant_expression_p = false;
4484 bool saved_non_integral_constant_expression_p = false;
4485 tree args;
4487 is_builtin_constant_p
4488 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4489 if (is_builtin_constant_p)
4491 /* The whole point of __builtin_constant_p is to allow
4492 non-constant expressions to appear as arguments. */
4493 saved_integral_constant_expression_p
4494 = parser->integral_constant_expression_p;
4495 saved_non_integral_constant_expression_p
4496 = parser->non_integral_constant_expression_p;
4497 parser->integral_constant_expression_p = false;
4499 args = (cp_parser_parenthesized_expression_list
4500 (parser, /*is_attribute_list=*/false,
4501 /*cast_p=*/false, /*allow_expansion_p=*/true,
4502 /*non_constant_p=*/NULL));
4503 if (is_builtin_constant_p)
4505 parser->integral_constant_expression_p
4506 = saved_integral_constant_expression_p;
4507 parser->non_integral_constant_expression_p
4508 = saved_non_integral_constant_expression_p;
4511 if (args == error_mark_node)
4513 postfix_expression = error_mark_node;
4514 break;
4517 /* Function calls are not permitted in
4518 constant-expressions. */
4519 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4520 && cp_parser_non_integral_constant_expression (parser,
4521 "a function call"))
4523 postfix_expression = error_mark_node;
4524 break;
4527 koenig_p = false;
4528 if (idk == CP_ID_KIND_UNQUALIFIED)
4530 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4532 if (args)
4534 koenig_p = true;
4535 postfix_expression
4536 = perform_koenig_lookup (postfix_expression, args);
4538 else
4539 postfix_expression
4540 = unqualified_fn_lookup_error (postfix_expression);
4542 /* We do not perform argument-dependent lookup if
4543 normal lookup finds a non-function, in accordance
4544 with the expected resolution of DR 218. */
4545 else if (args && is_overloaded_fn (postfix_expression))
4547 tree fn = get_first_fn (postfix_expression);
4549 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4550 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4552 /* Only do argument dependent lookup if regular
4553 lookup does not find a set of member functions.
4554 [basic.lookup.koenig]/2a */
4555 if (!DECL_FUNCTION_MEMBER_P (fn))
4557 koenig_p = true;
4558 postfix_expression
4559 = perform_koenig_lookup (postfix_expression, args);
4564 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4566 tree instance = TREE_OPERAND (postfix_expression, 0);
4567 tree fn = TREE_OPERAND (postfix_expression, 1);
4569 if (processing_template_decl
4570 && (type_dependent_expression_p (instance)
4571 || (!BASELINK_P (fn)
4572 && TREE_CODE (fn) != FIELD_DECL)
4573 || type_dependent_expression_p (fn)
4574 || any_type_dependent_arguments_p (args)))
4576 postfix_expression
4577 = build_nt_call_list (postfix_expression, args);
4578 break;
4581 if (BASELINK_P (fn))
4582 postfix_expression
4583 = (build_new_method_call
4584 (instance, fn, args, NULL_TREE,
4585 (idk == CP_ID_KIND_QUALIFIED
4586 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4587 /*fn_p=*/NULL));
4588 else
4589 postfix_expression
4590 = finish_call_expr (postfix_expression, args,
4591 /*disallow_virtual=*/false,
4592 /*koenig_p=*/false);
4594 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4595 || TREE_CODE (postfix_expression) == MEMBER_REF
4596 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4597 postfix_expression = (build_offset_ref_call_from_tree
4598 (postfix_expression, args));
4599 else if (idk == CP_ID_KIND_QUALIFIED)
4600 /* A call to a static class member, or a namespace-scope
4601 function. */
4602 postfix_expression
4603 = finish_call_expr (postfix_expression, args,
4604 /*disallow_virtual=*/true,
4605 koenig_p);
4606 else
4607 /* All other function calls. */
4608 postfix_expression
4609 = finish_call_expr (postfix_expression, args,
4610 /*disallow_virtual=*/false,
4611 koenig_p);
4613 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4614 idk = CP_ID_KIND_NONE;
4616 break;
4618 case CPP_DOT:
4619 case CPP_DEREF:
4620 /* postfix-expression . template [opt] id-expression
4621 postfix-expression . pseudo-destructor-name
4622 postfix-expression -> template [opt] id-expression
4623 postfix-expression -> pseudo-destructor-name */
4625 /* Consume the `.' or `->' operator. */
4626 cp_lexer_consume_token (parser->lexer);
4628 postfix_expression
4629 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4630 postfix_expression,
4631 false, &idk);
4632 break;
4634 case CPP_PLUS_PLUS:
4635 /* postfix-expression ++ */
4636 /* Consume the `++' token. */
4637 cp_lexer_consume_token (parser->lexer);
4638 /* Generate a representation for the complete expression. */
4639 postfix_expression
4640 = finish_increment_expr (postfix_expression,
4641 POSTINCREMENT_EXPR);
4642 /* Increments may not appear in constant-expressions. */
4643 if (cp_parser_non_integral_constant_expression (parser,
4644 "an increment"))
4645 postfix_expression = error_mark_node;
4646 idk = CP_ID_KIND_NONE;
4647 break;
4649 case CPP_MINUS_MINUS:
4650 /* postfix-expression -- */
4651 /* Consume the `--' token. */
4652 cp_lexer_consume_token (parser->lexer);
4653 /* Generate a representation for the complete expression. */
4654 postfix_expression
4655 = finish_increment_expr (postfix_expression,
4656 POSTDECREMENT_EXPR);
4657 /* Decrements may not appear in constant-expressions. */
4658 if (cp_parser_non_integral_constant_expression (parser,
4659 "a decrement"))
4660 postfix_expression = error_mark_node;
4661 idk = CP_ID_KIND_NONE;
4662 break;
4664 default:
4665 return postfix_expression;
4669 /* We should never get here. */
4670 gcc_unreachable ();
4671 return error_mark_node;
4674 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4675 by cp_parser_builtin_offsetof. We're looking for
4677 postfix-expression [ expression ]
4679 FOR_OFFSETOF is set if we're being called in that context, which
4680 changes how we deal with integer constant expressions. */
4682 static tree
4683 cp_parser_postfix_open_square_expression (cp_parser *parser,
4684 tree postfix_expression,
4685 bool for_offsetof)
4687 tree index;
4689 /* Consume the `[' token. */
4690 cp_lexer_consume_token (parser->lexer);
4692 /* Parse the index expression. */
4693 /* ??? For offsetof, there is a question of what to allow here. If
4694 offsetof is not being used in an integral constant expression context,
4695 then we *could* get the right answer by computing the value at runtime.
4696 If we are in an integral constant expression context, then we might
4697 could accept any constant expression; hard to say without analysis.
4698 Rather than open the barn door too wide right away, allow only integer
4699 constant expressions here. */
4700 if (for_offsetof)
4701 index = cp_parser_constant_expression (parser, false, NULL);
4702 else
4703 index = cp_parser_expression (parser, /*cast_p=*/false);
4705 /* Look for the closing `]'. */
4706 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4708 /* Build the ARRAY_REF. */
4709 postfix_expression = grok_array_decl (postfix_expression, index);
4711 /* When not doing offsetof, array references are not permitted in
4712 constant-expressions. */
4713 if (!for_offsetof
4714 && (cp_parser_non_integral_constant_expression
4715 (parser, "an array reference")))
4716 postfix_expression = error_mark_node;
4718 return postfix_expression;
4721 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4722 by cp_parser_builtin_offsetof. We're looking for
4724 postfix-expression . template [opt] id-expression
4725 postfix-expression . pseudo-destructor-name
4726 postfix-expression -> template [opt] id-expression
4727 postfix-expression -> pseudo-destructor-name
4729 FOR_OFFSETOF is set if we're being called in that context. That sorta
4730 limits what of the above we'll actually accept, but nevermind.
4731 TOKEN_TYPE is the "." or "->" token, which will already have been
4732 removed from the stream. */
4734 static tree
4735 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4736 enum cpp_ttype token_type,
4737 tree postfix_expression,
4738 bool for_offsetof, cp_id_kind *idk)
4740 tree name;
4741 bool dependent_p;
4742 bool pseudo_destructor_p;
4743 tree scope = NULL_TREE;
4745 /* If this is a `->' operator, dereference the pointer. */
4746 if (token_type == CPP_DEREF)
4747 postfix_expression = build_x_arrow (postfix_expression);
4748 /* Check to see whether or not the expression is type-dependent. */
4749 dependent_p = type_dependent_expression_p (postfix_expression);
4750 /* The identifier following the `->' or `.' is not qualified. */
4751 parser->scope = NULL_TREE;
4752 parser->qualifying_scope = NULL_TREE;
4753 parser->object_scope = NULL_TREE;
4754 *idk = CP_ID_KIND_NONE;
4755 /* Enter the scope corresponding to the type of the object
4756 given by the POSTFIX_EXPRESSION. */
4757 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4759 scope = TREE_TYPE (postfix_expression);
4760 /* According to the standard, no expression should ever have
4761 reference type. Unfortunately, we do not currently match
4762 the standard in this respect in that our internal representation
4763 of an expression may have reference type even when the standard
4764 says it does not. Therefore, we have to manually obtain the
4765 underlying type here. */
4766 scope = non_reference (scope);
4767 /* The type of the POSTFIX_EXPRESSION must be complete. */
4768 if (scope == unknown_type_node)
4770 error ("%qE does not have class type", postfix_expression);
4771 scope = NULL_TREE;
4773 else
4774 scope = complete_type_or_else (scope, NULL_TREE);
4775 /* Let the name lookup machinery know that we are processing a
4776 class member access expression. */
4777 parser->context->object_type = scope;
4778 /* If something went wrong, we want to be able to discern that case,
4779 as opposed to the case where there was no SCOPE due to the type
4780 of expression being dependent. */
4781 if (!scope)
4782 scope = error_mark_node;
4783 /* If the SCOPE was erroneous, make the various semantic analysis
4784 functions exit quickly -- and without issuing additional error
4785 messages. */
4786 if (scope == error_mark_node)
4787 postfix_expression = error_mark_node;
4790 /* Assume this expression is not a pseudo-destructor access. */
4791 pseudo_destructor_p = false;
4793 /* If the SCOPE is a scalar type, then, if this is a valid program,
4794 we must be looking at a pseudo-destructor-name. */
4795 if (scope && SCALAR_TYPE_P (scope))
4797 tree s;
4798 tree type;
4800 cp_parser_parse_tentatively (parser);
4801 /* Parse the pseudo-destructor-name. */
4802 s = NULL_TREE;
4803 cp_parser_pseudo_destructor_name (parser, &s, &type);
4804 if (cp_parser_parse_definitely (parser))
4806 pseudo_destructor_p = true;
4807 postfix_expression
4808 = finish_pseudo_destructor_expr (postfix_expression,
4809 s, TREE_TYPE (type));
4813 if (!pseudo_destructor_p)
4815 /* If the SCOPE is not a scalar type, we are looking at an
4816 ordinary class member access expression, rather than a
4817 pseudo-destructor-name. */
4818 bool template_p;
4819 /* Parse the id-expression. */
4820 name = (cp_parser_id_expression
4821 (parser,
4822 cp_parser_optional_template_keyword (parser),
4823 /*check_dependency_p=*/true,
4824 &template_p,
4825 /*declarator_p=*/false,
4826 /*optional_p=*/false));
4827 /* In general, build a SCOPE_REF if the member name is qualified.
4828 However, if the name was not dependent and has already been
4829 resolved; there is no need to build the SCOPE_REF. For example;
4831 struct X { void f(); };
4832 template <typename T> void f(T* t) { t->X::f(); }
4834 Even though "t" is dependent, "X::f" is not and has been resolved
4835 to a BASELINK; there is no need to include scope information. */
4837 /* But we do need to remember that there was an explicit scope for
4838 virtual function calls. */
4839 if (parser->scope)
4840 *idk = CP_ID_KIND_QUALIFIED;
4842 /* If the name is a template-id that names a type, we will get a
4843 TYPE_DECL here. That is invalid code. */
4844 if (TREE_CODE (name) == TYPE_DECL)
4846 error ("invalid use of %qD", name);
4847 postfix_expression = error_mark_node;
4849 else
4851 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4853 name = build_qualified_name (/*type=*/NULL_TREE,
4854 parser->scope,
4855 name,
4856 template_p);
4857 parser->scope = NULL_TREE;
4858 parser->qualifying_scope = NULL_TREE;
4859 parser->object_scope = NULL_TREE;
4861 if (scope && name && BASELINK_P (name))
4862 adjust_result_of_qualified_name_lookup
4863 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4864 postfix_expression
4865 = finish_class_member_access_expr (postfix_expression, name,
4866 template_p);
4870 /* We no longer need to look up names in the scope of the object on
4871 the left-hand side of the `.' or `->' operator. */
4872 parser->context->object_type = NULL_TREE;
4874 /* Outside of offsetof, these operators may not appear in
4875 constant-expressions. */
4876 if (!for_offsetof
4877 && (cp_parser_non_integral_constant_expression
4878 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4879 postfix_expression = error_mark_node;
4881 return postfix_expression;
4884 /* Parse a parenthesized expression-list.
4886 expression-list:
4887 assignment-expression
4888 expression-list, assignment-expression
4890 attribute-list:
4891 expression-list
4892 identifier
4893 identifier, expression-list
4895 CAST_P is true if this expression is the target of a cast.
4897 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4898 argument pack.
4900 Returns a TREE_LIST. The TREE_VALUE of each node is a
4901 representation of an assignment-expression. Note that a TREE_LIST
4902 is returned even if there is only a single expression in the list.
4903 error_mark_node is returned if the ( and or ) are
4904 missing. NULL_TREE is returned on no expressions. The parentheses
4905 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4906 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4907 indicates whether or not all of the expressions in the list were
4908 constant. */
4910 static tree
4911 cp_parser_parenthesized_expression_list (cp_parser* parser,
4912 bool is_attribute_list,
4913 bool cast_p,
4914 bool allow_expansion_p,
4915 bool *non_constant_p)
4917 tree expression_list = NULL_TREE;
4918 bool fold_expr_p = is_attribute_list;
4919 tree identifier = NULL_TREE;
4921 /* Assume all the expressions will be constant. */
4922 if (non_constant_p)
4923 *non_constant_p = false;
4925 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4926 return error_mark_node;
4928 /* Consume expressions until there are no more. */
4929 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4930 while (true)
4932 tree expr;
4934 /* At the beginning of attribute lists, check to see if the
4935 next token is an identifier. */
4936 if (is_attribute_list
4937 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4939 cp_token *token;
4941 /* Consume the identifier. */
4942 token = cp_lexer_consume_token (parser->lexer);
4943 /* Save the identifier. */
4944 identifier = token->u.value;
4946 else
4948 /* Parse the next assignment-expression. */
4949 if (non_constant_p)
4951 bool expr_non_constant_p;
4952 expr = (cp_parser_constant_expression
4953 (parser, /*allow_non_constant_p=*/true,
4954 &expr_non_constant_p));
4955 if (expr_non_constant_p)
4956 *non_constant_p = true;
4958 else
4959 expr = cp_parser_assignment_expression (parser, cast_p);
4961 if (fold_expr_p)
4962 expr = fold_non_dependent_expr (expr);
4964 /* If we have an ellipsis, then this is an expression
4965 expansion. */
4966 if (allow_expansion_p
4967 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4969 /* Consume the `...'. */
4970 cp_lexer_consume_token (parser->lexer);
4972 /* Build the argument pack. */
4973 expr = make_pack_expansion (expr);
4976 /* Add it to the list. We add error_mark_node
4977 expressions to the list, so that we can still tell if
4978 the correct form for a parenthesized expression-list
4979 is found. That gives better errors. */
4980 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4982 if (expr == error_mark_node)
4983 goto skip_comma;
4986 /* After the first item, attribute lists look the same as
4987 expression lists. */
4988 is_attribute_list = false;
4990 get_comma:;
4991 /* If the next token isn't a `,', then we are done. */
4992 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4993 break;
4995 /* Otherwise, consume the `,' and keep going. */
4996 cp_lexer_consume_token (parser->lexer);
4999 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5001 int ending;
5003 skip_comma:;
5004 /* We try and resync to an unnested comma, as that will give the
5005 user better diagnostics. */
5006 ending = cp_parser_skip_to_closing_parenthesis (parser,
5007 /*recovering=*/true,
5008 /*or_comma=*/true,
5009 /*consume_paren=*/true);
5010 if (ending < 0)
5011 goto get_comma;
5012 if (!ending)
5013 return error_mark_node;
5016 /* We built up the list in reverse order so we must reverse it now. */
5017 expression_list = nreverse (expression_list);
5018 if (identifier)
5019 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5021 return expression_list;
5024 /* Parse a pseudo-destructor-name.
5026 pseudo-destructor-name:
5027 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5028 :: [opt] nested-name-specifier template template-id :: ~ type-name
5029 :: [opt] nested-name-specifier [opt] ~ type-name
5031 If either of the first two productions is used, sets *SCOPE to the
5032 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5033 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5034 or ERROR_MARK_NODE if the parse fails. */
5036 static void
5037 cp_parser_pseudo_destructor_name (cp_parser* parser,
5038 tree* scope,
5039 tree* type)
5041 bool nested_name_specifier_p;
5043 /* Assume that things will not work out. */
5044 *type = error_mark_node;
5046 /* Look for the optional `::' operator. */
5047 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5048 /* Look for the optional nested-name-specifier. */
5049 nested_name_specifier_p
5050 = (cp_parser_nested_name_specifier_opt (parser,
5051 /*typename_keyword_p=*/false,
5052 /*check_dependency_p=*/true,
5053 /*type_p=*/false,
5054 /*is_declaration=*/true)
5055 != NULL_TREE);
5056 /* Now, if we saw a nested-name-specifier, we might be doing the
5057 second production. */
5058 if (nested_name_specifier_p
5059 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5061 /* Consume the `template' keyword. */
5062 cp_lexer_consume_token (parser->lexer);
5063 /* Parse the template-id. */
5064 cp_parser_template_id (parser,
5065 /*template_keyword_p=*/true,
5066 /*check_dependency_p=*/false,
5067 /*is_declaration=*/true);
5068 /* Look for the `::' token. */
5069 cp_parser_require (parser, CPP_SCOPE, "`::'");
5071 /* If the next token is not a `~', then there might be some
5072 additional qualification. */
5073 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5075 /* Look for the type-name. */
5076 *scope = TREE_TYPE (cp_parser_type_name (parser));
5078 if (*scope == error_mark_node)
5079 return;
5081 /* If we don't have ::~, then something has gone wrong. Since
5082 the only caller of this function is looking for something
5083 after `.' or `->' after a scalar type, most likely the
5084 program is trying to get a member of a non-aggregate
5085 type. */
5086 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5087 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5089 cp_parser_error (parser, "request for member of non-aggregate type");
5090 return;
5093 /* Look for the `::' token. */
5094 cp_parser_require (parser, CPP_SCOPE, "`::'");
5096 else
5097 *scope = NULL_TREE;
5099 /* Look for the `~'. */
5100 cp_parser_require (parser, CPP_COMPL, "`~'");
5101 /* Look for the type-name again. We are not responsible for
5102 checking that it matches the first type-name. */
5103 *type = cp_parser_type_name (parser);
5106 /* Parse a unary-expression.
5108 unary-expression:
5109 postfix-expression
5110 ++ cast-expression
5111 -- cast-expression
5112 unary-operator cast-expression
5113 sizeof unary-expression
5114 sizeof ( type-id )
5115 new-expression
5116 delete-expression
5118 GNU Extensions:
5120 unary-expression:
5121 __extension__ cast-expression
5122 __alignof__ unary-expression
5123 __alignof__ ( type-id )
5124 __real__ cast-expression
5125 __imag__ cast-expression
5126 && identifier
5128 ADDRESS_P is true iff the unary-expression is appearing as the
5129 operand of the `&' operator. CAST_P is true if this expression is
5130 the target of a cast.
5132 Returns a representation of the expression. */
5134 static tree
5135 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5137 cp_token *token;
5138 enum tree_code unary_operator;
5140 /* Peek at the next token. */
5141 token = cp_lexer_peek_token (parser->lexer);
5142 /* Some keywords give away the kind of expression. */
5143 if (token->type == CPP_KEYWORD)
5145 enum rid keyword = token->keyword;
5147 switch (keyword)
5149 case RID_ALIGNOF:
5150 case RID_SIZEOF:
5152 tree operand;
5153 enum tree_code op;
5155 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5156 /* Consume the token. */
5157 cp_lexer_consume_token (parser->lexer);
5158 /* Parse the operand. */
5159 operand = cp_parser_sizeof_operand (parser, keyword);
5161 if (TYPE_P (operand))
5162 return cxx_sizeof_or_alignof_type (operand, op, true);
5163 else
5164 return cxx_sizeof_or_alignof_expr (operand, op);
5167 case RID_NEW:
5168 return cp_parser_new_expression (parser);
5170 case RID_DELETE:
5171 return cp_parser_delete_expression (parser);
5173 case RID_EXTENSION:
5175 /* The saved value of the PEDANTIC flag. */
5176 int saved_pedantic;
5177 tree expr;
5179 /* Save away the PEDANTIC flag. */
5180 cp_parser_extension_opt (parser, &saved_pedantic);
5181 /* Parse the cast-expression. */
5182 expr = cp_parser_simple_cast_expression (parser);
5183 /* Restore the PEDANTIC flag. */
5184 pedantic = saved_pedantic;
5186 return expr;
5189 case RID_REALPART:
5190 case RID_IMAGPART:
5192 tree expression;
5194 /* Consume the `__real__' or `__imag__' token. */
5195 cp_lexer_consume_token (parser->lexer);
5196 /* Parse the cast-expression. */
5197 expression = cp_parser_simple_cast_expression (parser);
5198 /* Create the complete representation. */
5199 return build_x_unary_op ((keyword == RID_REALPART
5200 ? REALPART_EXPR : IMAGPART_EXPR),
5201 expression);
5203 break;
5205 default:
5206 break;
5210 /* Look for the `:: new' and `:: delete', which also signal the
5211 beginning of a new-expression, or delete-expression,
5212 respectively. If the next token is `::', then it might be one of
5213 these. */
5214 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5216 enum rid keyword;
5218 /* See if the token after the `::' is one of the keywords in
5219 which we're interested. */
5220 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5221 /* If it's `new', we have a new-expression. */
5222 if (keyword == RID_NEW)
5223 return cp_parser_new_expression (parser);
5224 /* Similarly, for `delete'. */
5225 else if (keyword == RID_DELETE)
5226 return cp_parser_delete_expression (parser);
5229 /* Look for a unary operator. */
5230 unary_operator = cp_parser_unary_operator (token);
5231 /* The `++' and `--' operators can be handled similarly, even though
5232 they are not technically unary-operators in the grammar. */
5233 if (unary_operator == ERROR_MARK)
5235 if (token->type == CPP_PLUS_PLUS)
5236 unary_operator = PREINCREMENT_EXPR;
5237 else if (token->type == CPP_MINUS_MINUS)
5238 unary_operator = PREDECREMENT_EXPR;
5239 /* Handle the GNU address-of-label extension. */
5240 else if (cp_parser_allow_gnu_extensions_p (parser)
5241 && token->type == CPP_AND_AND)
5243 tree identifier;
5245 /* Consume the '&&' token. */
5246 cp_lexer_consume_token (parser->lexer);
5247 /* Look for the identifier. */
5248 identifier = cp_parser_identifier (parser);
5249 /* Create an expression representing the address. */
5250 return finish_label_address_expr (identifier);
5253 if (unary_operator != ERROR_MARK)
5255 tree cast_expression;
5256 tree expression = error_mark_node;
5257 const char *non_constant_p = NULL;
5259 /* Consume the operator token. */
5260 token = cp_lexer_consume_token (parser->lexer);
5261 /* Parse the cast-expression. */
5262 cast_expression
5263 = cp_parser_cast_expression (parser,
5264 unary_operator == ADDR_EXPR,
5265 /*cast_p=*/false);
5266 /* Now, build an appropriate representation. */
5267 switch (unary_operator)
5269 case INDIRECT_REF:
5270 non_constant_p = "`*'";
5271 expression = build_x_indirect_ref (cast_expression, "unary *");
5272 break;
5274 case ADDR_EXPR:
5275 non_constant_p = "`&'";
5276 /* Fall through. */
5277 case BIT_NOT_EXPR:
5278 expression = build_x_unary_op (unary_operator, cast_expression);
5279 break;
5281 case PREINCREMENT_EXPR:
5282 case PREDECREMENT_EXPR:
5283 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5284 ? "`++'" : "`--'");
5285 /* Fall through. */
5286 case UNARY_PLUS_EXPR:
5287 case NEGATE_EXPR:
5288 case TRUTH_NOT_EXPR:
5289 expression = finish_unary_op_expr (unary_operator, cast_expression);
5290 break;
5292 default:
5293 gcc_unreachable ();
5296 if (non_constant_p
5297 && cp_parser_non_integral_constant_expression (parser,
5298 non_constant_p))
5299 expression = error_mark_node;
5301 return expression;
5304 return cp_parser_postfix_expression (parser, address_p, cast_p);
5307 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5308 unary-operator, the corresponding tree code is returned. */
5310 static enum tree_code
5311 cp_parser_unary_operator (cp_token* token)
5313 switch (token->type)
5315 case CPP_MULT:
5316 return INDIRECT_REF;
5318 case CPP_AND:
5319 return ADDR_EXPR;
5321 case CPP_PLUS:
5322 return UNARY_PLUS_EXPR;
5324 case CPP_MINUS:
5325 return NEGATE_EXPR;
5327 case CPP_NOT:
5328 return TRUTH_NOT_EXPR;
5330 case CPP_COMPL:
5331 return BIT_NOT_EXPR;
5333 default:
5334 return ERROR_MARK;
5338 /* Parse a new-expression.
5340 new-expression:
5341 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5342 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5344 Returns a representation of the expression. */
5346 static tree
5347 cp_parser_new_expression (cp_parser* parser)
5349 bool global_scope_p;
5350 tree placement;
5351 tree type;
5352 tree initializer;
5353 tree nelts;
5355 /* Look for the optional `::' operator. */
5356 global_scope_p
5357 = (cp_parser_global_scope_opt (parser,
5358 /*current_scope_valid_p=*/false)
5359 != NULL_TREE);
5360 /* Look for the `new' operator. */
5361 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5362 /* There's no easy way to tell a new-placement from the
5363 `( type-id )' construct. */
5364 cp_parser_parse_tentatively (parser);
5365 /* Look for a new-placement. */
5366 placement = cp_parser_new_placement (parser);
5367 /* If that didn't work out, there's no new-placement. */
5368 if (!cp_parser_parse_definitely (parser))
5369 placement = NULL_TREE;
5371 /* If the next token is a `(', then we have a parenthesized
5372 type-id. */
5373 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5375 /* Consume the `('. */
5376 cp_lexer_consume_token (parser->lexer);
5377 /* Parse the type-id. */
5378 type = cp_parser_type_id (parser);
5379 /* Look for the closing `)'. */
5380 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5381 /* There should not be a direct-new-declarator in this production,
5382 but GCC used to allowed this, so we check and emit a sensible error
5383 message for this case. */
5384 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5386 error ("array bound forbidden after parenthesized type-id");
5387 inform ("try removing the parentheses around the type-id");
5388 cp_parser_direct_new_declarator (parser);
5390 nelts = NULL_TREE;
5392 /* Otherwise, there must be a new-type-id. */
5393 else
5394 type = cp_parser_new_type_id (parser, &nelts);
5396 /* If the next token is a `(', then we have a new-initializer. */
5397 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5398 initializer = cp_parser_new_initializer (parser);
5399 else
5400 initializer = NULL_TREE;
5402 /* A new-expression may not appear in an integral constant
5403 expression. */
5404 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5405 return error_mark_node;
5407 /* Create a representation of the new-expression. */
5408 return build_new (placement, type, nelts, initializer, global_scope_p);
5411 /* Parse a new-placement.
5413 new-placement:
5414 ( expression-list )
5416 Returns the same representation as for an expression-list. */
5418 static tree
5419 cp_parser_new_placement (cp_parser* parser)
5421 tree expression_list;
5423 /* Parse the expression-list. */
5424 expression_list = (cp_parser_parenthesized_expression_list
5425 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5426 /*non_constant_p=*/NULL));
5428 return expression_list;
5431 /* Parse a new-type-id.
5433 new-type-id:
5434 type-specifier-seq new-declarator [opt]
5436 Returns the TYPE allocated. If the new-type-id indicates an array
5437 type, *NELTS is set to the number of elements in the last array
5438 bound; the TYPE will not include the last array bound. */
5440 static tree
5441 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5443 cp_decl_specifier_seq type_specifier_seq;
5444 cp_declarator *new_declarator;
5445 cp_declarator *declarator;
5446 cp_declarator *outer_declarator;
5447 const char *saved_message;
5448 tree type;
5450 /* The type-specifier sequence must not contain type definitions.
5451 (It cannot contain declarations of new types either, but if they
5452 are not definitions we will catch that because they are not
5453 complete.) */
5454 saved_message = parser->type_definition_forbidden_message;
5455 parser->type_definition_forbidden_message
5456 = "types may not be defined in a new-type-id";
5457 /* Parse the type-specifier-seq. */
5458 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5459 &type_specifier_seq);
5460 /* Restore the old message. */
5461 parser->type_definition_forbidden_message = saved_message;
5462 /* Parse the new-declarator. */
5463 new_declarator = cp_parser_new_declarator_opt (parser);
5465 /* Determine the number of elements in the last array dimension, if
5466 any. */
5467 *nelts = NULL_TREE;
5468 /* Skip down to the last array dimension. */
5469 declarator = new_declarator;
5470 outer_declarator = NULL;
5471 while (declarator && (declarator->kind == cdk_pointer
5472 || declarator->kind == cdk_ptrmem))
5474 outer_declarator = declarator;
5475 declarator = declarator->declarator;
5477 while (declarator
5478 && declarator->kind == cdk_array
5479 && declarator->declarator
5480 && declarator->declarator->kind == cdk_array)
5482 outer_declarator = declarator;
5483 declarator = declarator->declarator;
5486 if (declarator && declarator->kind == cdk_array)
5488 *nelts = declarator->u.array.bounds;
5489 if (*nelts == error_mark_node)
5490 *nelts = integer_one_node;
5492 if (outer_declarator)
5493 outer_declarator->declarator = declarator->declarator;
5494 else
5495 new_declarator = NULL;
5498 type = groktypename (&type_specifier_seq, new_declarator);
5499 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5501 *nelts = array_type_nelts_top (type);
5502 type = TREE_TYPE (type);
5504 return type;
5507 /* Parse an (optional) new-declarator.
5509 new-declarator:
5510 ptr-operator new-declarator [opt]
5511 direct-new-declarator
5513 Returns the declarator. */
5515 static cp_declarator *
5516 cp_parser_new_declarator_opt (cp_parser* parser)
5518 enum tree_code code;
5519 tree type;
5520 cp_cv_quals cv_quals;
5522 /* We don't know if there's a ptr-operator next, or not. */
5523 cp_parser_parse_tentatively (parser);
5524 /* Look for a ptr-operator. */
5525 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5526 /* If that worked, look for more new-declarators. */
5527 if (cp_parser_parse_definitely (parser))
5529 cp_declarator *declarator;
5531 /* Parse another optional declarator. */
5532 declarator = cp_parser_new_declarator_opt (parser);
5534 /* Create the representation of the declarator. */
5535 if (type)
5536 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5537 else if (code == INDIRECT_REF)
5538 declarator = make_pointer_declarator (cv_quals, declarator);
5539 else
5540 declarator = make_reference_declarator (cv_quals, declarator);
5542 return declarator;
5545 /* If the next token is a `[', there is a direct-new-declarator. */
5546 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5547 return cp_parser_direct_new_declarator (parser);
5549 return NULL;
5552 /* Parse a direct-new-declarator.
5554 direct-new-declarator:
5555 [ expression ]
5556 direct-new-declarator [constant-expression]
5560 static cp_declarator *
5561 cp_parser_direct_new_declarator (cp_parser* parser)
5563 cp_declarator *declarator = NULL;
5565 while (true)
5567 tree expression;
5569 /* Look for the opening `['. */
5570 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5571 /* The first expression is not required to be constant. */
5572 if (!declarator)
5574 expression = cp_parser_expression (parser, /*cast_p=*/false);
5575 /* The standard requires that the expression have integral
5576 type. DR 74 adds enumeration types. We believe that the
5577 real intent is that these expressions be handled like the
5578 expression in a `switch' condition, which also allows
5579 classes with a single conversion to integral or
5580 enumeration type. */
5581 if (!processing_template_decl)
5583 expression
5584 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5585 expression,
5586 /*complain=*/true);
5587 if (!expression)
5589 error ("expression in new-declarator must have integral "
5590 "or enumeration type");
5591 expression = error_mark_node;
5595 /* But all the other expressions must be. */
5596 else
5597 expression
5598 = cp_parser_constant_expression (parser,
5599 /*allow_non_constant=*/false,
5600 NULL);
5601 /* Look for the closing `]'. */
5602 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5604 /* Add this bound to the declarator. */
5605 declarator = make_array_declarator (declarator, expression);
5607 /* If the next token is not a `[', then there are no more
5608 bounds. */
5609 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5610 break;
5613 return declarator;
5616 /* Parse a new-initializer.
5618 new-initializer:
5619 ( expression-list [opt] )
5621 Returns a representation of the expression-list. If there is no
5622 expression-list, VOID_ZERO_NODE is returned. */
5624 static tree
5625 cp_parser_new_initializer (cp_parser* parser)
5627 tree expression_list;
5629 expression_list = (cp_parser_parenthesized_expression_list
5630 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5631 /*non_constant_p=*/NULL));
5632 if (!expression_list)
5633 expression_list = void_zero_node;
5635 return expression_list;
5638 /* Parse a delete-expression.
5640 delete-expression:
5641 :: [opt] delete cast-expression
5642 :: [opt] delete [ ] cast-expression
5644 Returns a representation of the expression. */
5646 static tree
5647 cp_parser_delete_expression (cp_parser* parser)
5649 bool global_scope_p;
5650 bool array_p;
5651 tree expression;
5653 /* Look for the optional `::' operator. */
5654 global_scope_p
5655 = (cp_parser_global_scope_opt (parser,
5656 /*current_scope_valid_p=*/false)
5657 != NULL_TREE);
5658 /* Look for the `delete' keyword. */
5659 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5660 /* See if the array syntax is in use. */
5661 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5663 /* Consume the `[' token. */
5664 cp_lexer_consume_token (parser->lexer);
5665 /* Look for the `]' token. */
5666 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5667 /* Remember that this is the `[]' construct. */
5668 array_p = true;
5670 else
5671 array_p = false;
5673 /* Parse the cast-expression. */
5674 expression = cp_parser_simple_cast_expression (parser);
5676 /* A delete-expression may not appear in an integral constant
5677 expression. */
5678 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5679 return error_mark_node;
5681 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5684 /* Parse a cast-expression.
5686 cast-expression:
5687 unary-expression
5688 ( type-id ) cast-expression
5690 ADDRESS_P is true iff the unary-expression is appearing as the
5691 operand of the `&' operator. CAST_P is true if this expression is
5692 the target of a cast.
5694 Returns a representation of the expression. */
5696 static tree
5697 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5699 /* If it's a `(', then we might be looking at a cast. */
5700 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5702 tree type = NULL_TREE;
5703 tree expr = NULL_TREE;
5704 bool compound_literal_p;
5705 const char *saved_message;
5707 /* There's no way to know yet whether or not this is a cast.
5708 For example, `(int (3))' is a unary-expression, while `(int)
5709 3' is a cast. So, we resort to parsing tentatively. */
5710 cp_parser_parse_tentatively (parser);
5711 /* Types may not be defined in a cast. */
5712 saved_message = parser->type_definition_forbidden_message;
5713 parser->type_definition_forbidden_message
5714 = "types may not be defined in casts";
5715 /* Consume the `('. */
5716 cp_lexer_consume_token (parser->lexer);
5717 /* A very tricky bit is that `(struct S) { 3 }' is a
5718 compound-literal (which we permit in C++ as an extension).
5719 But, that construct is not a cast-expression -- it is a
5720 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5721 is legal; if the compound-literal were a cast-expression,
5722 you'd need an extra set of parentheses.) But, if we parse
5723 the type-id, and it happens to be a class-specifier, then we
5724 will commit to the parse at that point, because we cannot
5725 undo the action that is done when creating a new class. So,
5726 then we cannot back up and do a postfix-expression.
5728 Therefore, we scan ahead to the closing `)', and check to see
5729 if the token after the `)' is a `{'. If so, we are not
5730 looking at a cast-expression.
5732 Save tokens so that we can put them back. */
5733 cp_lexer_save_tokens (parser->lexer);
5734 /* Skip tokens until the next token is a closing parenthesis.
5735 If we find the closing `)', and the next token is a `{', then
5736 we are looking at a compound-literal. */
5737 compound_literal_p
5738 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5739 /*consume_paren=*/true)
5740 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5741 /* Roll back the tokens we skipped. */
5742 cp_lexer_rollback_tokens (parser->lexer);
5743 /* If we were looking at a compound-literal, simulate an error
5744 so that the call to cp_parser_parse_definitely below will
5745 fail. */
5746 if (compound_literal_p)
5747 cp_parser_simulate_error (parser);
5748 else
5750 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5751 parser->in_type_id_in_expr_p = true;
5752 /* Look for the type-id. */
5753 type = cp_parser_type_id (parser);
5754 /* Look for the closing `)'. */
5755 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5756 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5759 /* Restore the saved message. */
5760 parser->type_definition_forbidden_message = saved_message;
5762 /* If ok so far, parse the dependent expression. We cannot be
5763 sure it is a cast. Consider `(T ())'. It is a parenthesized
5764 ctor of T, but looks like a cast to function returning T
5765 without a dependent expression. */
5766 if (!cp_parser_error_occurred (parser))
5767 expr = cp_parser_cast_expression (parser,
5768 /*address_p=*/false,
5769 /*cast_p=*/true);
5771 if (cp_parser_parse_definitely (parser))
5773 /* Warn about old-style casts, if so requested. */
5774 if (warn_old_style_cast
5775 && !in_system_header
5776 && !VOID_TYPE_P (type)
5777 && current_lang_name != lang_name_c)
5778 warning (OPT_Wold_style_cast, "use of old-style cast");
5780 /* Only type conversions to integral or enumeration types
5781 can be used in constant-expressions. */
5782 if (!cast_valid_in_integral_constant_expression_p (type)
5783 && (cp_parser_non_integral_constant_expression
5784 (parser,
5785 "a cast to a type other than an integral or "
5786 "enumeration type")))
5787 return error_mark_node;
5789 /* Perform the cast. */
5790 expr = build_c_cast (type, expr);
5791 return expr;
5795 /* If we get here, then it's not a cast, so it must be a
5796 unary-expression. */
5797 return cp_parser_unary_expression (parser, address_p, cast_p);
5800 /* Parse a binary expression of the general form:
5802 pm-expression:
5803 cast-expression
5804 pm-expression .* cast-expression
5805 pm-expression ->* cast-expression
5807 multiplicative-expression:
5808 pm-expression
5809 multiplicative-expression * pm-expression
5810 multiplicative-expression / pm-expression
5811 multiplicative-expression % pm-expression
5813 additive-expression:
5814 multiplicative-expression
5815 additive-expression + multiplicative-expression
5816 additive-expression - multiplicative-expression
5818 shift-expression:
5819 additive-expression
5820 shift-expression << additive-expression
5821 shift-expression >> additive-expression
5823 relational-expression:
5824 shift-expression
5825 relational-expression < shift-expression
5826 relational-expression > shift-expression
5827 relational-expression <= shift-expression
5828 relational-expression >= shift-expression
5830 GNU Extension:
5832 relational-expression:
5833 relational-expression <? shift-expression
5834 relational-expression >? shift-expression
5836 equality-expression:
5837 relational-expression
5838 equality-expression == relational-expression
5839 equality-expression != relational-expression
5841 and-expression:
5842 equality-expression
5843 and-expression & equality-expression
5845 exclusive-or-expression:
5846 and-expression
5847 exclusive-or-expression ^ and-expression
5849 inclusive-or-expression:
5850 exclusive-or-expression
5851 inclusive-or-expression | exclusive-or-expression
5853 logical-and-expression:
5854 inclusive-or-expression
5855 logical-and-expression && inclusive-or-expression
5857 logical-or-expression:
5858 logical-and-expression
5859 logical-or-expression || logical-and-expression
5861 All these are implemented with a single function like:
5863 binary-expression:
5864 simple-cast-expression
5865 binary-expression <token> binary-expression
5867 CAST_P is true if this expression is the target of a cast.
5869 The binops_by_token map is used to get the tree codes for each <token> type.
5870 binary-expressions are associated according to a precedence table. */
5872 #define TOKEN_PRECEDENCE(token) \
5873 (((token->type == CPP_GREATER \
5874 || (flag_cpp0x && token->type == CPP_RSHIFT)) \
5875 && !parser->greater_than_is_operator_p) \
5876 ? PREC_NOT_OPERATOR \
5877 : binops_by_token[token->type].prec)
5879 static tree
5880 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5882 cp_parser_expression_stack stack;
5883 cp_parser_expression_stack_entry *sp = &stack[0];
5884 tree lhs, rhs;
5885 cp_token *token;
5886 enum tree_code tree_type, lhs_type, rhs_type;
5887 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5888 bool overloaded_p;
5890 /* Parse the first expression. */
5891 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5892 lhs_type = ERROR_MARK;
5894 for (;;)
5896 /* Get an operator token. */
5897 token = cp_lexer_peek_token (parser->lexer);
5899 if (warn_cxx0x_compat
5900 && token->type == CPP_RSHIFT
5901 && !parser->greater_than_is_operator_p)
5903 warning (OPT_Wc__0x_compat,
5904 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5905 &token->location);
5906 warning (OPT_Wc__0x_compat,
5907 "suggest parentheses around %<>>%> expression");
5910 new_prec = TOKEN_PRECEDENCE (token);
5912 /* Popping an entry off the stack means we completed a subexpression:
5913 - either we found a token which is not an operator (`>' where it is not
5914 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5915 will happen repeatedly;
5916 - or, we found an operator which has lower priority. This is the case
5917 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5918 parsing `3 * 4'. */
5919 if (new_prec <= prec)
5921 if (sp == stack)
5922 break;
5923 else
5924 goto pop;
5927 get_rhs:
5928 tree_type = binops_by_token[token->type].tree_type;
5930 /* We used the operator token. */
5931 cp_lexer_consume_token (parser->lexer);
5933 /* Extract another operand. It may be the RHS of this expression
5934 or the LHS of a new, higher priority expression. */
5935 rhs = cp_parser_simple_cast_expression (parser);
5936 rhs_type = ERROR_MARK;
5938 /* Get another operator token. Look up its precedence to avoid
5939 building a useless (immediately popped) stack entry for common
5940 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5941 token = cp_lexer_peek_token (parser->lexer);
5942 lookahead_prec = TOKEN_PRECEDENCE (token);
5943 if (lookahead_prec > new_prec)
5945 /* ... and prepare to parse the RHS of the new, higher priority
5946 expression. Since precedence levels on the stack are
5947 monotonically increasing, we do not have to care about
5948 stack overflows. */
5949 sp->prec = prec;
5950 sp->tree_type = tree_type;
5951 sp->lhs = lhs;
5952 sp->lhs_type = lhs_type;
5953 sp++;
5954 lhs = rhs;
5955 lhs_type = rhs_type;
5956 prec = new_prec;
5957 new_prec = lookahead_prec;
5958 goto get_rhs;
5960 pop:
5961 /* If the stack is not empty, we have parsed into LHS the right side
5962 (`4' in the example above) of an expression we had suspended.
5963 We can use the information on the stack to recover the LHS (`3')
5964 from the stack together with the tree code (`MULT_EXPR'), and
5965 the precedence of the higher level subexpression
5966 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5967 which will be used to actually build the additive expression. */
5968 --sp;
5969 prec = sp->prec;
5970 tree_type = sp->tree_type;
5971 rhs = lhs;
5972 rhs_type = lhs_type;
5973 lhs = sp->lhs;
5974 lhs_type = sp->lhs_type;
5977 overloaded_p = false;
5978 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5979 &overloaded_p);
5980 lhs_type = tree_type;
5982 /* If the binary operator required the use of an overloaded operator,
5983 then this expression cannot be an integral constant-expression.
5984 An overloaded operator can be used even if both operands are
5985 otherwise permissible in an integral constant-expression if at
5986 least one of the operands is of enumeration type. */
5988 if (overloaded_p
5989 && (cp_parser_non_integral_constant_expression
5990 (parser, "calls to overloaded operators")))
5991 return error_mark_node;
5994 return lhs;
5998 /* Parse the `? expression : assignment-expression' part of a
5999 conditional-expression. The LOGICAL_OR_EXPR is the
6000 logical-or-expression that started the conditional-expression.
6001 Returns a representation of the entire conditional-expression.
6003 This routine is used by cp_parser_assignment_expression.
6005 ? expression : assignment-expression
6007 GNU Extensions:
6009 ? : assignment-expression */
6011 static tree
6012 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6014 tree expr;
6015 tree assignment_expr;
6017 /* Consume the `?' token. */
6018 cp_lexer_consume_token (parser->lexer);
6019 if (cp_parser_allow_gnu_extensions_p (parser)
6020 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6021 /* Implicit true clause. */
6022 expr = NULL_TREE;
6023 else
6024 /* Parse the expression. */
6025 expr = cp_parser_expression (parser, /*cast_p=*/false);
6027 /* The next token should be a `:'. */
6028 cp_parser_require (parser, CPP_COLON, "`:'");
6029 /* Parse the assignment-expression. */
6030 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6032 /* Build the conditional-expression. */
6033 return build_x_conditional_expr (logical_or_expr,
6034 expr,
6035 assignment_expr);
6038 /* Parse an assignment-expression.
6040 assignment-expression:
6041 conditional-expression
6042 logical-or-expression assignment-operator assignment_expression
6043 throw-expression
6045 CAST_P is true if this expression is the target of a cast.
6047 Returns a representation for the expression. */
6049 static tree
6050 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6052 tree expr;
6054 /* If the next token is the `throw' keyword, then we're looking at
6055 a throw-expression. */
6056 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6057 expr = cp_parser_throw_expression (parser);
6058 /* Otherwise, it must be that we are looking at a
6059 logical-or-expression. */
6060 else
6062 /* Parse the binary expressions (logical-or-expression). */
6063 expr = cp_parser_binary_expression (parser, cast_p);
6064 /* If the next token is a `?' then we're actually looking at a
6065 conditional-expression. */
6066 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6067 return cp_parser_question_colon_clause (parser, expr);
6068 else
6070 enum tree_code assignment_operator;
6072 /* If it's an assignment-operator, we're using the second
6073 production. */
6074 assignment_operator
6075 = cp_parser_assignment_operator_opt (parser);
6076 if (assignment_operator != ERROR_MARK)
6078 tree rhs;
6080 /* Parse the right-hand side of the assignment. */
6081 rhs = cp_parser_assignment_expression (parser, cast_p);
6082 /* An assignment may not appear in a
6083 constant-expression. */
6084 if (cp_parser_non_integral_constant_expression (parser,
6085 "an assignment"))
6086 return error_mark_node;
6087 /* Build the assignment expression. */
6088 expr = build_x_modify_expr (expr,
6089 assignment_operator,
6090 rhs);
6095 return expr;
6098 /* Parse an (optional) assignment-operator.
6100 assignment-operator: one of
6101 = *= /= %= += -= >>= <<= &= ^= |=
6103 GNU Extension:
6105 assignment-operator: one of
6106 <?= >?=
6108 If the next token is an assignment operator, the corresponding tree
6109 code is returned, and the token is consumed. For example, for
6110 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6111 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6112 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6113 operator, ERROR_MARK is returned. */
6115 static enum tree_code
6116 cp_parser_assignment_operator_opt (cp_parser* parser)
6118 enum tree_code op;
6119 cp_token *token;
6121 /* Peek at the next toen. */
6122 token = cp_lexer_peek_token (parser->lexer);
6124 switch (token->type)
6126 case CPP_EQ:
6127 op = NOP_EXPR;
6128 break;
6130 case CPP_MULT_EQ:
6131 op = MULT_EXPR;
6132 break;
6134 case CPP_DIV_EQ:
6135 op = TRUNC_DIV_EXPR;
6136 break;
6138 case CPP_MOD_EQ:
6139 op = TRUNC_MOD_EXPR;
6140 break;
6142 case CPP_PLUS_EQ:
6143 op = PLUS_EXPR;
6144 break;
6146 case CPP_MINUS_EQ:
6147 op = MINUS_EXPR;
6148 break;
6150 case CPP_RSHIFT_EQ:
6151 op = RSHIFT_EXPR;
6152 break;
6154 case CPP_LSHIFT_EQ:
6155 op = LSHIFT_EXPR;
6156 break;
6158 case CPP_AND_EQ:
6159 op = BIT_AND_EXPR;
6160 break;
6162 case CPP_XOR_EQ:
6163 op = BIT_XOR_EXPR;
6164 break;
6166 case CPP_OR_EQ:
6167 op = BIT_IOR_EXPR;
6168 break;
6170 default:
6171 /* Nothing else is an assignment operator. */
6172 op = ERROR_MARK;
6175 /* If it was an assignment operator, consume it. */
6176 if (op != ERROR_MARK)
6177 cp_lexer_consume_token (parser->lexer);
6179 return op;
6182 /* Parse an expression.
6184 expression:
6185 assignment-expression
6186 expression , assignment-expression
6188 CAST_P is true if this expression is the target of a cast.
6190 Returns a representation of the expression. */
6192 static tree
6193 cp_parser_expression (cp_parser* parser, bool cast_p)
6195 tree expression = NULL_TREE;
6197 while (true)
6199 tree assignment_expression;
6201 /* Parse the next assignment-expression. */
6202 assignment_expression
6203 = cp_parser_assignment_expression (parser, cast_p);
6204 /* If this is the first assignment-expression, we can just
6205 save it away. */
6206 if (!expression)
6207 expression = assignment_expression;
6208 else
6209 expression = build_x_compound_expr (expression,
6210 assignment_expression);
6211 /* If the next token is not a comma, then we are done with the
6212 expression. */
6213 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6214 break;
6215 /* Consume the `,'. */
6216 cp_lexer_consume_token (parser->lexer);
6217 /* A comma operator cannot appear in a constant-expression. */
6218 if (cp_parser_non_integral_constant_expression (parser,
6219 "a comma operator"))
6220 expression = error_mark_node;
6223 return expression;
6226 /* Parse a constant-expression.
6228 constant-expression:
6229 conditional-expression
6231 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6232 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6233 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6234 is false, NON_CONSTANT_P should be NULL. */
6236 static tree
6237 cp_parser_constant_expression (cp_parser* parser,
6238 bool allow_non_constant_p,
6239 bool *non_constant_p)
6241 bool saved_integral_constant_expression_p;
6242 bool saved_allow_non_integral_constant_expression_p;
6243 bool saved_non_integral_constant_expression_p;
6244 tree expression;
6246 /* It might seem that we could simply parse the
6247 conditional-expression, and then check to see if it were
6248 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6249 one that the compiler can figure out is constant, possibly after
6250 doing some simplifications or optimizations. The standard has a
6251 precise definition of constant-expression, and we must honor
6252 that, even though it is somewhat more restrictive.
6254 For example:
6256 int i[(2, 3)];
6258 is not a legal declaration, because `(2, 3)' is not a
6259 constant-expression. The `,' operator is forbidden in a
6260 constant-expression. However, GCC's constant-folding machinery
6261 will fold this operation to an INTEGER_CST for `3'. */
6263 /* Save the old settings. */
6264 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6265 saved_allow_non_integral_constant_expression_p
6266 = parser->allow_non_integral_constant_expression_p;
6267 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6268 /* We are now parsing a constant-expression. */
6269 parser->integral_constant_expression_p = true;
6270 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6271 parser->non_integral_constant_expression_p = false;
6272 /* Although the grammar says "conditional-expression", we parse an
6273 "assignment-expression", which also permits "throw-expression"
6274 and the use of assignment operators. In the case that
6275 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6276 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6277 actually essential that we look for an assignment-expression.
6278 For example, cp_parser_initializer_clauses uses this function to
6279 determine whether a particular assignment-expression is in fact
6280 constant. */
6281 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6282 /* Restore the old settings. */
6283 parser->integral_constant_expression_p
6284 = saved_integral_constant_expression_p;
6285 parser->allow_non_integral_constant_expression_p
6286 = saved_allow_non_integral_constant_expression_p;
6287 if (allow_non_constant_p)
6288 *non_constant_p = parser->non_integral_constant_expression_p;
6289 else if (parser->non_integral_constant_expression_p)
6290 expression = error_mark_node;
6291 parser->non_integral_constant_expression_p
6292 = saved_non_integral_constant_expression_p;
6294 return expression;
6297 /* Parse __builtin_offsetof.
6299 offsetof-expression:
6300 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6302 offsetof-member-designator:
6303 id-expression
6304 | offsetof-member-designator "." id-expression
6305 | offsetof-member-designator "[" expression "]" */
6307 static tree
6308 cp_parser_builtin_offsetof (cp_parser *parser)
6310 int save_ice_p, save_non_ice_p;
6311 tree type, expr;
6312 cp_id_kind dummy;
6314 /* We're about to accept non-integral-constant things, but will
6315 definitely yield an integral constant expression. Save and
6316 restore these values around our local parsing. */
6317 save_ice_p = parser->integral_constant_expression_p;
6318 save_non_ice_p = parser->non_integral_constant_expression_p;
6320 /* Consume the "__builtin_offsetof" token. */
6321 cp_lexer_consume_token (parser->lexer);
6322 /* Consume the opening `('. */
6323 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6324 /* Parse the type-id. */
6325 type = cp_parser_type_id (parser);
6326 /* Look for the `,'. */
6327 cp_parser_require (parser, CPP_COMMA, "`,'");
6329 /* Build the (type *)null that begins the traditional offsetof macro. */
6330 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6332 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6333 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6334 true, &dummy);
6335 while (true)
6337 cp_token *token = cp_lexer_peek_token (parser->lexer);
6338 switch (token->type)
6340 case CPP_OPEN_SQUARE:
6341 /* offsetof-member-designator "[" expression "]" */
6342 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6343 break;
6345 case CPP_DOT:
6346 /* offsetof-member-designator "." identifier */
6347 cp_lexer_consume_token (parser->lexer);
6348 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6349 true, &dummy);
6350 break;
6352 case CPP_CLOSE_PAREN:
6353 /* Consume the ")" token. */
6354 cp_lexer_consume_token (parser->lexer);
6355 goto success;
6357 default:
6358 /* Error. We know the following require will fail, but
6359 that gives the proper error message. */
6360 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6361 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6362 expr = error_mark_node;
6363 goto failure;
6367 success:
6368 /* If we're processing a template, we can't finish the semantics yet.
6369 Otherwise we can fold the entire expression now. */
6370 if (processing_template_decl)
6371 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6372 else
6373 expr = finish_offsetof (expr);
6375 failure:
6376 parser->integral_constant_expression_p = save_ice_p;
6377 parser->non_integral_constant_expression_p = save_non_ice_p;
6379 return expr;
6382 /* Parse a trait expression. */
6384 static tree
6385 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6387 cp_trait_kind kind;
6388 tree type1, type2 = NULL_TREE;
6389 bool binary = false;
6390 cp_decl_specifier_seq decl_specs;
6392 switch (keyword)
6394 case RID_HAS_NOTHROW_ASSIGN:
6395 kind = CPTK_HAS_NOTHROW_ASSIGN;
6396 break;
6397 case RID_HAS_NOTHROW_CONSTRUCTOR:
6398 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6399 break;
6400 case RID_HAS_NOTHROW_COPY:
6401 kind = CPTK_HAS_NOTHROW_COPY;
6402 break;
6403 case RID_HAS_TRIVIAL_ASSIGN:
6404 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6405 break;
6406 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6407 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6408 break;
6409 case RID_HAS_TRIVIAL_COPY:
6410 kind = CPTK_HAS_TRIVIAL_COPY;
6411 break;
6412 case RID_HAS_TRIVIAL_DESTRUCTOR:
6413 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6414 break;
6415 case RID_HAS_VIRTUAL_DESTRUCTOR:
6416 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6417 break;
6418 case RID_IS_ABSTRACT:
6419 kind = CPTK_IS_ABSTRACT;
6420 break;
6421 case RID_IS_BASE_OF:
6422 kind = CPTK_IS_BASE_OF;
6423 binary = true;
6424 break;
6425 case RID_IS_CLASS:
6426 kind = CPTK_IS_CLASS;
6427 break;
6428 case RID_IS_CONVERTIBLE_TO:
6429 kind = CPTK_IS_CONVERTIBLE_TO;
6430 binary = true;
6431 break;
6432 case RID_IS_EMPTY:
6433 kind = CPTK_IS_EMPTY;
6434 break;
6435 case RID_IS_ENUM:
6436 kind = CPTK_IS_ENUM;
6437 break;
6438 case RID_IS_POD:
6439 kind = CPTK_IS_POD;
6440 break;
6441 case RID_IS_POLYMORPHIC:
6442 kind = CPTK_IS_POLYMORPHIC;
6443 break;
6444 case RID_IS_UNION:
6445 kind = CPTK_IS_UNION;
6446 break;
6447 default:
6448 gcc_unreachable ();
6451 /* Consume the token. */
6452 cp_lexer_consume_token (parser->lexer);
6454 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6456 type1 = cp_parser_type_id (parser);
6458 /* Build a trivial decl-specifier-seq. */
6459 clear_decl_specs (&decl_specs);
6460 decl_specs.type = type1;
6462 /* Call grokdeclarator to figure out what type this is. */
6463 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6464 /*initialized=*/0, /*attrlist=*/NULL);
6466 if (binary)
6468 cp_parser_require (parser, CPP_COMMA, "`,'");
6470 type2 = cp_parser_type_id (parser);
6472 /* Build a trivial decl-specifier-seq. */
6473 clear_decl_specs (&decl_specs);
6474 decl_specs.type = type2;
6476 /* Call grokdeclarator to figure out what type this is. */
6477 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6478 /*initialized=*/0, /*attrlist=*/NULL);
6481 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6483 /* Complete the trait expr, which may mean either processing the
6484 static assert now or saving it for template instantiation. */
6485 return finish_trait_expr (kind, type1, type2);
6488 /* Statements [gram.stmt.stmt] */
6490 /* Parse a statement.
6492 statement:
6493 labeled-statement
6494 expression-statement
6495 compound-statement
6496 selection-statement
6497 iteration-statement
6498 jump-statement
6499 declaration-statement
6500 try-block
6502 IN_COMPOUND is true when the statement is nested inside a
6503 cp_parser_compound_statement; this matters for certain pragmas.
6505 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6506 is a (possibly labeled) if statement which is not enclosed in braces
6507 and has an else clause. This is used to implement -Wparentheses. */
6509 static void
6510 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6511 bool in_compound, bool *if_p)
6513 tree statement;
6514 cp_token *token;
6515 location_t statement_location;
6517 restart:
6518 if (if_p != NULL)
6519 *if_p = false;
6520 /* There is no statement yet. */
6521 statement = NULL_TREE;
6522 /* Peek at the next token. */
6523 token = cp_lexer_peek_token (parser->lexer);
6524 /* Remember the location of the first token in the statement. */
6525 statement_location = token->location;
6526 /* If this is a keyword, then that will often determine what kind of
6527 statement we have. */
6528 if (token->type == CPP_KEYWORD)
6530 enum rid keyword = token->keyword;
6532 switch (keyword)
6534 case RID_CASE:
6535 case RID_DEFAULT:
6536 /* Looks like a labeled-statement with a case label.
6537 Parse the label, and then use tail recursion to parse
6538 the statement. */
6539 cp_parser_label_for_labeled_statement (parser);
6540 goto restart;
6542 case RID_IF:
6543 case RID_SWITCH:
6544 statement = cp_parser_selection_statement (parser, if_p);
6545 break;
6547 case RID_WHILE:
6548 case RID_DO:
6549 case RID_FOR:
6550 statement = cp_parser_iteration_statement (parser);
6551 break;
6553 case RID_BREAK:
6554 case RID_CONTINUE:
6555 case RID_RETURN:
6556 case RID_GOTO:
6557 statement = cp_parser_jump_statement (parser);
6558 break;
6560 /* Objective-C++ exception-handling constructs. */
6561 case RID_AT_TRY:
6562 case RID_AT_CATCH:
6563 case RID_AT_FINALLY:
6564 case RID_AT_SYNCHRONIZED:
6565 case RID_AT_THROW:
6566 statement = cp_parser_objc_statement (parser);
6567 break;
6569 case RID_TRY:
6570 statement = cp_parser_try_block (parser);
6571 break;
6573 case RID_NAMESPACE:
6574 /* This must be a namespace alias definition. */
6575 cp_parser_declaration_statement (parser);
6576 return;
6578 default:
6579 /* It might be a keyword like `int' that can start a
6580 declaration-statement. */
6581 break;
6584 else if (token->type == CPP_NAME)
6586 /* If the next token is a `:', then we are looking at a
6587 labeled-statement. */
6588 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6589 if (token->type == CPP_COLON)
6591 /* Looks like a labeled-statement with an ordinary label.
6592 Parse the label, and then use tail recursion to parse
6593 the statement. */
6594 cp_parser_label_for_labeled_statement (parser);
6595 goto restart;
6598 /* Anything that starts with a `{' must be a compound-statement. */
6599 else if (token->type == CPP_OPEN_BRACE)
6600 statement = cp_parser_compound_statement (parser, NULL, false);
6601 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6602 a statement all its own. */
6603 else if (token->type == CPP_PRAGMA)
6605 /* Only certain OpenMP pragmas are attached to statements, and thus
6606 are considered statements themselves. All others are not. In
6607 the context of a compound, accept the pragma as a "statement" and
6608 return so that we can check for a close brace. Otherwise we
6609 require a real statement and must go back and read one. */
6610 if (in_compound)
6611 cp_parser_pragma (parser, pragma_compound);
6612 else if (!cp_parser_pragma (parser, pragma_stmt))
6613 goto restart;
6614 return;
6616 else if (token->type == CPP_EOF)
6618 cp_parser_error (parser, "expected statement");
6619 return;
6622 /* Everything else must be a declaration-statement or an
6623 expression-statement. Try for the declaration-statement
6624 first, unless we are looking at a `;', in which case we know that
6625 we have an expression-statement. */
6626 if (!statement)
6628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6630 cp_parser_parse_tentatively (parser);
6631 /* Try to parse the declaration-statement. */
6632 cp_parser_declaration_statement (parser);
6633 /* If that worked, we're done. */
6634 if (cp_parser_parse_definitely (parser))
6635 return;
6637 /* Look for an expression-statement instead. */
6638 statement = cp_parser_expression_statement (parser, in_statement_expr);
6641 /* Set the line number for the statement. */
6642 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6643 SET_EXPR_LOCATION (statement, statement_location);
6646 /* Parse the label for a labeled-statement, i.e.
6648 identifier :
6649 case constant-expression :
6650 default :
6652 GNU Extension:
6653 case constant-expression ... constant-expression : statement
6655 When a label is parsed without errors, the label is added to the
6656 parse tree by the finish_* functions, so this function doesn't
6657 have to return the label. */
6659 static void
6660 cp_parser_label_for_labeled_statement (cp_parser* parser)
6662 cp_token *token;
6664 /* The next token should be an identifier. */
6665 token = cp_lexer_peek_token (parser->lexer);
6666 if (token->type != CPP_NAME
6667 && token->type != CPP_KEYWORD)
6669 cp_parser_error (parser, "expected labeled-statement");
6670 return;
6673 switch (token->keyword)
6675 case RID_CASE:
6677 tree expr, expr_hi;
6678 cp_token *ellipsis;
6680 /* Consume the `case' token. */
6681 cp_lexer_consume_token (parser->lexer);
6682 /* Parse the constant-expression. */
6683 expr = cp_parser_constant_expression (parser,
6684 /*allow_non_constant_p=*/false,
6685 NULL);
6687 ellipsis = cp_lexer_peek_token (parser->lexer);
6688 if (ellipsis->type == CPP_ELLIPSIS)
6690 /* Consume the `...' token. */
6691 cp_lexer_consume_token (parser->lexer);
6692 expr_hi =
6693 cp_parser_constant_expression (parser,
6694 /*allow_non_constant_p=*/false,
6695 NULL);
6696 /* We don't need to emit warnings here, as the common code
6697 will do this for us. */
6699 else
6700 expr_hi = NULL_TREE;
6702 if (parser->in_switch_statement_p)
6703 finish_case_label (expr, expr_hi);
6704 else
6705 error ("case label %qE not within a switch statement", expr);
6707 break;
6709 case RID_DEFAULT:
6710 /* Consume the `default' token. */
6711 cp_lexer_consume_token (parser->lexer);
6713 if (parser->in_switch_statement_p)
6714 finish_case_label (NULL_TREE, NULL_TREE);
6715 else
6716 error ("case label not within a switch statement");
6717 break;
6719 default:
6720 /* Anything else must be an ordinary label. */
6721 finish_label_stmt (cp_parser_identifier (parser));
6722 break;
6725 /* Require the `:' token. */
6726 cp_parser_require (parser, CPP_COLON, "`:'");
6729 /* Parse an expression-statement.
6731 expression-statement:
6732 expression [opt] ;
6734 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6735 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6736 indicates whether this expression-statement is part of an
6737 expression statement. */
6739 static tree
6740 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6742 tree statement = NULL_TREE;
6744 /* If the next token is a ';', then there is no expression
6745 statement. */
6746 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6747 statement = cp_parser_expression (parser, /*cast_p=*/false);
6749 /* Consume the final `;'. */
6750 cp_parser_consume_semicolon_at_end_of_statement (parser);
6752 if (in_statement_expr
6753 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6754 /* This is the final expression statement of a statement
6755 expression. */
6756 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6757 else if (statement)
6758 statement = finish_expr_stmt (statement);
6759 else
6760 finish_stmt ();
6762 return statement;
6765 /* Parse a compound-statement.
6767 compound-statement:
6768 { statement-seq [opt] }
6770 Returns a tree representing the statement. */
6772 static tree
6773 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6774 bool in_try)
6776 tree compound_stmt;
6778 /* Consume the `{'. */
6779 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6780 return error_mark_node;
6781 /* Begin the compound-statement. */
6782 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6783 /* Parse an (optional) statement-seq. */
6784 cp_parser_statement_seq_opt (parser, in_statement_expr);
6785 /* Finish the compound-statement. */
6786 finish_compound_stmt (compound_stmt);
6787 /* Consume the `}'. */
6788 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6790 return compound_stmt;
6793 /* Parse an (optional) statement-seq.
6795 statement-seq:
6796 statement
6797 statement-seq [opt] statement */
6799 static void
6800 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6802 /* Scan statements until there aren't any more. */
6803 while (true)
6805 cp_token *token = cp_lexer_peek_token (parser->lexer);
6807 /* If we're looking at a `}', then we've run out of statements. */
6808 if (token->type == CPP_CLOSE_BRACE
6809 || token->type == CPP_EOF
6810 || token->type == CPP_PRAGMA_EOL)
6811 break;
6813 /* If we are in a compound statement and find 'else' then
6814 something went wrong. */
6815 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6817 if (parser->in_statement & IN_IF_STMT)
6818 break;
6819 else
6821 token = cp_lexer_consume_token (parser->lexer);
6822 error ("%<else%> without a previous %<if%>");
6826 /* Parse the statement. */
6827 cp_parser_statement (parser, in_statement_expr, true, NULL);
6831 /* Parse a selection-statement.
6833 selection-statement:
6834 if ( condition ) statement
6835 if ( condition ) statement else statement
6836 switch ( condition ) statement
6838 Returns the new IF_STMT or SWITCH_STMT.
6840 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6841 is a (possibly labeled) if statement which is not enclosed in
6842 braces and has an else clause. This is used to implement
6843 -Wparentheses. */
6845 static tree
6846 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6848 cp_token *token;
6849 enum rid keyword;
6851 if (if_p != NULL)
6852 *if_p = false;
6854 /* Peek at the next token. */
6855 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6857 /* See what kind of keyword it is. */
6858 keyword = token->keyword;
6859 switch (keyword)
6861 case RID_IF:
6862 case RID_SWITCH:
6864 tree statement;
6865 tree condition;
6867 /* Look for the `('. */
6868 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6870 cp_parser_skip_to_end_of_statement (parser);
6871 return error_mark_node;
6874 /* Begin the selection-statement. */
6875 if (keyword == RID_IF)
6876 statement = begin_if_stmt ();
6877 else
6878 statement = begin_switch_stmt ();
6880 /* Parse the condition. */
6881 condition = cp_parser_condition (parser);
6882 /* Look for the `)'. */
6883 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6884 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6885 /*consume_paren=*/true);
6887 if (keyword == RID_IF)
6889 bool nested_if;
6890 unsigned char in_statement;
6892 /* Add the condition. */
6893 finish_if_stmt_cond (condition, statement);
6895 /* Parse the then-clause. */
6896 in_statement = parser->in_statement;
6897 parser->in_statement |= IN_IF_STMT;
6898 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6899 parser->in_statement = in_statement;
6901 finish_then_clause (statement);
6903 /* If the next token is `else', parse the else-clause. */
6904 if (cp_lexer_next_token_is_keyword (parser->lexer,
6905 RID_ELSE))
6907 /* Consume the `else' keyword. */
6908 cp_lexer_consume_token (parser->lexer);
6909 begin_else_clause (statement);
6910 /* Parse the else-clause. */
6911 cp_parser_implicitly_scoped_statement (parser, NULL);
6912 finish_else_clause (statement);
6914 /* If we are currently parsing a then-clause, then
6915 IF_P will not be NULL. We set it to true to
6916 indicate that this if statement has an else clause.
6917 This may trigger the Wparentheses warning below
6918 when we get back up to the parent if statement. */
6919 if (if_p != NULL)
6920 *if_p = true;
6922 else
6924 /* This if statement does not have an else clause. If
6925 NESTED_IF is true, then the then-clause is an if
6926 statement which does have an else clause. We warn
6927 about the potential ambiguity. */
6928 if (nested_if)
6929 warning (OPT_Wparentheses,
6930 ("%Hsuggest explicit braces "
6931 "to avoid ambiguous %<else%>"),
6932 EXPR_LOCUS (statement));
6935 /* Now we're all done with the if-statement. */
6936 finish_if_stmt (statement);
6938 else
6940 bool in_switch_statement_p;
6941 unsigned char in_statement;
6943 /* Add the condition. */
6944 finish_switch_cond (condition, statement);
6946 /* Parse the body of the switch-statement. */
6947 in_switch_statement_p = parser->in_switch_statement_p;
6948 in_statement = parser->in_statement;
6949 parser->in_switch_statement_p = true;
6950 parser->in_statement |= IN_SWITCH_STMT;
6951 cp_parser_implicitly_scoped_statement (parser, NULL);
6952 parser->in_switch_statement_p = in_switch_statement_p;
6953 parser->in_statement = in_statement;
6955 /* Now we're all done with the switch-statement. */
6956 finish_switch_stmt (statement);
6959 return statement;
6961 break;
6963 default:
6964 cp_parser_error (parser, "expected selection-statement");
6965 return error_mark_node;
6969 /* Parse a condition.
6971 condition:
6972 expression
6973 type-specifier-seq declarator = assignment-expression
6975 GNU Extension:
6977 condition:
6978 type-specifier-seq declarator asm-specification [opt]
6979 attributes [opt] = assignment-expression
6981 Returns the expression that should be tested. */
6983 static tree
6984 cp_parser_condition (cp_parser* parser)
6986 cp_decl_specifier_seq type_specifiers;
6987 const char *saved_message;
6989 /* Try the declaration first. */
6990 cp_parser_parse_tentatively (parser);
6991 /* New types are not allowed in the type-specifier-seq for a
6992 condition. */
6993 saved_message = parser->type_definition_forbidden_message;
6994 parser->type_definition_forbidden_message
6995 = "types may not be defined in conditions";
6996 /* Parse the type-specifier-seq. */
6997 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6998 &type_specifiers);
6999 /* Restore the saved message. */
7000 parser->type_definition_forbidden_message = saved_message;
7001 /* If all is well, we might be looking at a declaration. */
7002 if (!cp_parser_error_occurred (parser))
7004 tree decl;
7005 tree asm_specification;
7006 tree attributes;
7007 cp_declarator *declarator;
7008 tree initializer = NULL_TREE;
7010 /* Parse the declarator. */
7011 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7012 /*ctor_dtor_or_conv_p=*/NULL,
7013 /*parenthesized_p=*/NULL,
7014 /*member_p=*/false);
7015 /* Parse the attributes. */
7016 attributes = cp_parser_attributes_opt (parser);
7017 /* Parse the asm-specification. */
7018 asm_specification = cp_parser_asm_specification_opt (parser);
7019 /* If the next token is not an `=', then we might still be
7020 looking at an expression. For example:
7022 if (A(a).x)
7024 looks like a decl-specifier-seq and a declarator -- but then
7025 there is no `=', so this is an expression. */
7026 cp_parser_require (parser, CPP_EQ, "`='");
7027 /* If we did see an `=', then we are looking at a declaration
7028 for sure. */
7029 if (cp_parser_parse_definitely (parser))
7031 tree pushed_scope;
7032 bool non_constant_p;
7034 /* Create the declaration. */
7035 decl = start_decl (declarator, &type_specifiers,
7036 /*initialized_p=*/true,
7037 attributes, /*prefix_attributes=*/NULL_TREE,
7038 &pushed_scope);
7039 /* Parse the assignment-expression. */
7040 initializer
7041 = cp_parser_constant_expression (parser,
7042 /*allow_non_constant_p=*/true,
7043 &non_constant_p);
7044 if (!non_constant_p)
7045 initializer = fold_non_dependent_expr (initializer);
7047 /* Process the initializer. */
7048 cp_finish_decl (decl,
7049 initializer, !non_constant_p,
7050 asm_specification,
7051 LOOKUP_ONLYCONVERTING);
7053 if (pushed_scope)
7054 pop_scope (pushed_scope);
7056 return convert_from_reference (decl);
7059 /* If we didn't even get past the declarator successfully, we are
7060 definitely not looking at a declaration. */
7061 else
7062 cp_parser_abort_tentative_parse (parser);
7064 /* Otherwise, we are looking at an expression. */
7065 return cp_parser_expression (parser, /*cast_p=*/false);
7068 /* Parse an iteration-statement.
7070 iteration-statement:
7071 while ( condition ) statement
7072 do statement while ( expression ) ;
7073 for ( for-init-statement condition [opt] ; expression [opt] )
7074 statement
7076 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7078 static tree
7079 cp_parser_iteration_statement (cp_parser* parser)
7081 cp_token *token;
7082 enum rid keyword;
7083 tree statement;
7084 unsigned char in_statement;
7086 /* Peek at the next token. */
7087 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7088 if (!token)
7089 return error_mark_node;
7091 /* Remember whether or not we are already within an iteration
7092 statement. */
7093 in_statement = parser->in_statement;
7095 /* See what kind of keyword it is. */
7096 keyword = token->keyword;
7097 switch (keyword)
7099 case RID_WHILE:
7101 tree condition;
7103 /* Begin the while-statement. */
7104 statement = begin_while_stmt ();
7105 /* Look for the `('. */
7106 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7107 /* Parse the condition. */
7108 condition = cp_parser_condition (parser);
7109 finish_while_stmt_cond (condition, statement);
7110 /* Look for the `)'. */
7111 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7112 /* Parse the dependent statement. */
7113 parser->in_statement = IN_ITERATION_STMT;
7114 cp_parser_already_scoped_statement (parser);
7115 parser->in_statement = in_statement;
7116 /* We're done with the while-statement. */
7117 finish_while_stmt (statement);
7119 break;
7121 case RID_DO:
7123 tree expression;
7125 /* Begin the do-statement. */
7126 statement = begin_do_stmt ();
7127 /* Parse the body of the do-statement. */
7128 parser->in_statement = IN_ITERATION_STMT;
7129 cp_parser_implicitly_scoped_statement (parser, NULL);
7130 parser->in_statement = in_statement;
7131 finish_do_body (statement);
7132 /* Look for the `while' keyword. */
7133 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7134 /* Look for the `('. */
7135 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7136 /* Parse the expression. */
7137 expression = cp_parser_expression (parser, /*cast_p=*/false);
7138 /* We're done with the do-statement. */
7139 finish_do_stmt (expression, statement);
7140 /* Look for the `)'. */
7141 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7142 /* Look for the `;'. */
7143 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7145 break;
7147 case RID_FOR:
7149 tree condition = NULL_TREE;
7150 tree expression = NULL_TREE;
7152 /* Begin the for-statement. */
7153 statement = begin_for_stmt ();
7154 /* Look for the `('. */
7155 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7156 /* Parse the initialization. */
7157 cp_parser_for_init_statement (parser);
7158 finish_for_init_stmt (statement);
7160 /* If there's a condition, process it. */
7161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7162 condition = cp_parser_condition (parser);
7163 finish_for_cond (condition, statement);
7164 /* Look for the `;'. */
7165 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7167 /* If there's an expression, process it. */
7168 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7169 expression = cp_parser_expression (parser, /*cast_p=*/false);
7170 finish_for_expr (expression, statement);
7171 /* Look for the `)'. */
7172 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7174 /* Parse the body of the for-statement. */
7175 parser->in_statement = IN_ITERATION_STMT;
7176 cp_parser_already_scoped_statement (parser);
7177 parser->in_statement = in_statement;
7179 /* We're done with the for-statement. */
7180 finish_for_stmt (statement);
7182 break;
7184 default:
7185 cp_parser_error (parser, "expected iteration-statement");
7186 statement = error_mark_node;
7187 break;
7190 return statement;
7193 /* Parse a for-init-statement.
7195 for-init-statement:
7196 expression-statement
7197 simple-declaration */
7199 static void
7200 cp_parser_for_init_statement (cp_parser* parser)
7202 /* If the next token is a `;', then we have an empty
7203 expression-statement. Grammatically, this is also a
7204 simple-declaration, but an invalid one, because it does not
7205 declare anything. Therefore, if we did not handle this case
7206 specially, we would issue an error message about an invalid
7207 declaration. */
7208 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7210 /* We're going to speculatively look for a declaration, falling back
7211 to an expression, if necessary. */
7212 cp_parser_parse_tentatively (parser);
7213 /* Parse the declaration. */
7214 cp_parser_simple_declaration (parser,
7215 /*function_definition_allowed_p=*/false);
7216 /* If the tentative parse failed, then we shall need to look for an
7217 expression-statement. */
7218 if (cp_parser_parse_definitely (parser))
7219 return;
7222 cp_parser_expression_statement (parser, false);
7225 /* Parse a jump-statement.
7227 jump-statement:
7228 break ;
7229 continue ;
7230 return expression [opt] ;
7231 goto identifier ;
7233 GNU extension:
7235 jump-statement:
7236 goto * expression ;
7238 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7240 static tree
7241 cp_parser_jump_statement (cp_parser* parser)
7243 tree statement = error_mark_node;
7244 cp_token *token;
7245 enum rid keyword;
7246 unsigned char in_statement;
7248 /* Peek at the next token. */
7249 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7250 if (!token)
7251 return error_mark_node;
7253 /* See what kind of keyword it is. */
7254 keyword = token->keyword;
7255 switch (keyword)
7257 case RID_BREAK:
7258 in_statement = parser->in_statement & ~IN_IF_STMT;
7259 switch (in_statement)
7261 case 0:
7262 error ("break statement not within loop or switch");
7263 break;
7264 default:
7265 gcc_assert ((in_statement & IN_SWITCH_STMT)
7266 || in_statement == IN_ITERATION_STMT);
7267 statement = finish_break_stmt ();
7268 break;
7269 case IN_OMP_BLOCK:
7270 error ("invalid exit from OpenMP structured block");
7271 break;
7272 case IN_OMP_FOR:
7273 error ("break statement used with OpenMP for loop");
7274 break;
7276 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7277 break;
7279 case RID_CONTINUE:
7280 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7282 case 0:
7283 error ("continue statement not within a loop");
7284 break;
7285 case IN_ITERATION_STMT:
7286 case IN_OMP_FOR:
7287 statement = finish_continue_stmt ();
7288 break;
7289 case IN_OMP_BLOCK:
7290 error ("invalid exit from OpenMP structured block");
7291 break;
7292 default:
7293 gcc_unreachable ();
7295 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7296 break;
7298 case RID_RETURN:
7300 tree expr;
7302 /* If the next token is a `;', then there is no
7303 expression. */
7304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7305 expr = cp_parser_expression (parser, /*cast_p=*/false);
7306 else
7307 expr = NULL_TREE;
7308 /* Build the return-statement. */
7309 statement = finish_return_stmt (expr);
7310 /* Look for the final `;'. */
7311 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7313 break;
7315 case RID_GOTO:
7316 /* Create the goto-statement. */
7317 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7319 /* Issue a warning about this use of a GNU extension. */
7320 if (pedantic)
7321 pedwarn ("ISO C++ forbids computed gotos");
7322 /* Consume the '*' token. */
7323 cp_lexer_consume_token (parser->lexer);
7324 /* Parse the dependent expression. */
7325 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7327 else
7328 finish_goto_stmt (cp_parser_identifier (parser));
7329 /* Look for the final `;'. */
7330 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7331 break;
7333 default:
7334 cp_parser_error (parser, "expected jump-statement");
7335 break;
7338 return statement;
7341 /* Parse a declaration-statement.
7343 declaration-statement:
7344 block-declaration */
7346 static void
7347 cp_parser_declaration_statement (cp_parser* parser)
7349 void *p;
7351 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7352 p = obstack_alloc (&declarator_obstack, 0);
7354 /* Parse the block-declaration. */
7355 cp_parser_block_declaration (parser, /*statement_p=*/true);
7357 /* Free any declarators allocated. */
7358 obstack_free (&declarator_obstack, p);
7360 /* Finish off the statement. */
7361 finish_stmt ();
7364 /* Some dependent statements (like `if (cond) statement'), are
7365 implicitly in their own scope. In other words, if the statement is
7366 a single statement (as opposed to a compound-statement), it is
7367 none-the-less treated as if it were enclosed in braces. Any
7368 declarations appearing in the dependent statement are out of scope
7369 after control passes that point. This function parses a statement,
7370 but ensures that is in its own scope, even if it is not a
7371 compound-statement.
7373 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7374 is a (possibly labeled) if statement which is not enclosed in
7375 braces and has an else clause. This is used to implement
7376 -Wparentheses.
7378 Returns the new statement. */
7380 static tree
7381 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7383 tree statement;
7385 if (if_p != NULL)
7386 *if_p = false;
7388 /* Mark if () ; with a special NOP_EXPR. */
7389 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7391 cp_lexer_consume_token (parser->lexer);
7392 statement = add_stmt (build_empty_stmt ());
7394 /* if a compound is opened, we simply parse the statement directly. */
7395 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7396 statement = cp_parser_compound_statement (parser, NULL, false);
7397 /* If the token is not a `{', then we must take special action. */
7398 else
7400 /* Create a compound-statement. */
7401 statement = begin_compound_stmt (0);
7402 /* Parse the dependent-statement. */
7403 cp_parser_statement (parser, NULL_TREE, false, if_p);
7404 /* Finish the dummy compound-statement. */
7405 finish_compound_stmt (statement);
7408 /* Return the statement. */
7409 return statement;
7412 /* For some dependent statements (like `while (cond) statement'), we
7413 have already created a scope. Therefore, even if the dependent
7414 statement is a compound-statement, we do not want to create another
7415 scope. */
7417 static void
7418 cp_parser_already_scoped_statement (cp_parser* parser)
7420 /* If the token is a `{', then we must take special action. */
7421 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7422 cp_parser_statement (parser, NULL_TREE, false, NULL);
7423 else
7425 /* Avoid calling cp_parser_compound_statement, so that we
7426 don't create a new scope. Do everything else by hand. */
7427 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7428 cp_parser_statement_seq_opt (parser, NULL_TREE);
7429 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7433 /* Declarations [gram.dcl.dcl] */
7435 /* Parse an optional declaration-sequence.
7437 declaration-seq:
7438 declaration
7439 declaration-seq declaration */
7441 static void
7442 cp_parser_declaration_seq_opt (cp_parser* parser)
7444 while (true)
7446 cp_token *token;
7448 token = cp_lexer_peek_token (parser->lexer);
7450 if (token->type == CPP_CLOSE_BRACE
7451 || token->type == CPP_EOF
7452 || token->type == CPP_PRAGMA_EOL)
7453 break;
7455 if (token->type == CPP_SEMICOLON)
7457 /* A declaration consisting of a single semicolon is
7458 invalid. Allow it unless we're being pedantic. */
7459 cp_lexer_consume_token (parser->lexer);
7460 if (pedantic && !in_system_header)
7461 pedwarn ("extra %<;%>");
7462 continue;
7465 /* If we're entering or exiting a region that's implicitly
7466 extern "C", modify the lang context appropriately. */
7467 if (!parser->implicit_extern_c && token->implicit_extern_c)
7469 push_lang_context (lang_name_c);
7470 parser->implicit_extern_c = true;
7472 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7474 pop_lang_context ();
7475 parser->implicit_extern_c = false;
7478 if (token->type == CPP_PRAGMA)
7480 /* A top-level declaration can consist solely of a #pragma.
7481 A nested declaration cannot, so this is done here and not
7482 in cp_parser_declaration. (A #pragma at block scope is
7483 handled in cp_parser_statement.) */
7484 cp_parser_pragma (parser, pragma_external);
7485 continue;
7488 /* Parse the declaration itself. */
7489 cp_parser_declaration (parser);
7493 /* Parse a declaration.
7495 declaration:
7496 block-declaration
7497 function-definition
7498 template-declaration
7499 explicit-instantiation
7500 explicit-specialization
7501 linkage-specification
7502 namespace-definition
7504 GNU extension:
7506 declaration:
7507 __extension__ declaration */
7509 static void
7510 cp_parser_declaration (cp_parser* parser)
7512 cp_token token1;
7513 cp_token token2;
7514 int saved_pedantic;
7515 void *p;
7517 /* Check for the `__extension__' keyword. */
7518 if (cp_parser_extension_opt (parser, &saved_pedantic))
7520 /* Parse the qualified declaration. */
7521 cp_parser_declaration (parser);
7522 /* Restore the PEDANTIC flag. */
7523 pedantic = saved_pedantic;
7525 return;
7528 /* Try to figure out what kind of declaration is present. */
7529 token1 = *cp_lexer_peek_token (parser->lexer);
7531 if (token1.type != CPP_EOF)
7532 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7533 else
7535 token2.type = CPP_EOF;
7536 token2.keyword = RID_MAX;
7539 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7540 p = obstack_alloc (&declarator_obstack, 0);
7542 /* If the next token is `extern' and the following token is a string
7543 literal, then we have a linkage specification. */
7544 if (token1.keyword == RID_EXTERN
7545 && cp_parser_is_string_literal (&token2))
7546 cp_parser_linkage_specification (parser);
7547 /* If the next token is `template', then we have either a template
7548 declaration, an explicit instantiation, or an explicit
7549 specialization. */
7550 else if (token1.keyword == RID_TEMPLATE)
7552 /* `template <>' indicates a template specialization. */
7553 if (token2.type == CPP_LESS
7554 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7555 cp_parser_explicit_specialization (parser);
7556 /* `template <' indicates a template declaration. */
7557 else if (token2.type == CPP_LESS)
7558 cp_parser_template_declaration (parser, /*member_p=*/false);
7559 /* Anything else must be an explicit instantiation. */
7560 else
7561 cp_parser_explicit_instantiation (parser);
7563 /* If the next token is `export', then we have a template
7564 declaration. */
7565 else if (token1.keyword == RID_EXPORT)
7566 cp_parser_template_declaration (parser, /*member_p=*/false);
7567 /* If the next token is `extern', 'static' or 'inline' and the one
7568 after that is `template', we have a GNU extended explicit
7569 instantiation directive. */
7570 else if (cp_parser_allow_gnu_extensions_p (parser)
7571 && (token1.keyword == RID_EXTERN
7572 || token1.keyword == RID_STATIC
7573 || token1.keyword == RID_INLINE)
7574 && token2.keyword == RID_TEMPLATE)
7575 cp_parser_explicit_instantiation (parser);
7576 /* If the next token is `namespace', check for a named or unnamed
7577 namespace definition. */
7578 else if (token1.keyword == RID_NAMESPACE
7579 && (/* A named namespace definition. */
7580 (token2.type == CPP_NAME
7581 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7582 != CPP_EQ))
7583 /* An unnamed namespace definition. */
7584 || token2.type == CPP_OPEN_BRACE
7585 || token2.keyword == RID_ATTRIBUTE))
7586 cp_parser_namespace_definition (parser);
7587 /* Objective-C++ declaration/definition. */
7588 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7589 cp_parser_objc_declaration (parser);
7590 /* We must have either a block declaration or a function
7591 definition. */
7592 else
7593 /* Try to parse a block-declaration, or a function-definition. */
7594 cp_parser_block_declaration (parser, /*statement_p=*/false);
7596 /* Free any declarators allocated. */
7597 obstack_free (&declarator_obstack, p);
7600 /* Parse a block-declaration.
7602 block-declaration:
7603 simple-declaration
7604 asm-definition
7605 namespace-alias-definition
7606 using-declaration
7607 using-directive
7609 GNU Extension:
7611 block-declaration:
7612 __extension__ block-declaration
7613 label-declaration
7615 C++0x Extension:
7617 block-declaration:
7618 static_assert-declaration
7620 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7621 part of a declaration-statement. */
7623 static void
7624 cp_parser_block_declaration (cp_parser *parser,
7625 bool statement_p)
7627 cp_token *token1;
7628 int saved_pedantic;
7630 /* Check for the `__extension__' keyword. */
7631 if (cp_parser_extension_opt (parser, &saved_pedantic))
7633 /* Parse the qualified declaration. */
7634 cp_parser_block_declaration (parser, statement_p);
7635 /* Restore the PEDANTIC flag. */
7636 pedantic = saved_pedantic;
7638 return;
7641 /* Peek at the next token to figure out which kind of declaration is
7642 present. */
7643 token1 = cp_lexer_peek_token (parser->lexer);
7645 /* If the next keyword is `asm', we have an asm-definition. */
7646 if (token1->keyword == RID_ASM)
7648 if (statement_p)
7649 cp_parser_commit_to_tentative_parse (parser);
7650 cp_parser_asm_definition (parser);
7652 /* If the next keyword is `namespace', we have a
7653 namespace-alias-definition. */
7654 else if (token1->keyword == RID_NAMESPACE)
7655 cp_parser_namespace_alias_definition (parser);
7656 /* If the next keyword is `using', we have either a
7657 using-declaration or a using-directive. */
7658 else if (token1->keyword == RID_USING)
7660 cp_token *token2;
7662 if (statement_p)
7663 cp_parser_commit_to_tentative_parse (parser);
7664 /* If the token after `using' is `namespace', then we have a
7665 using-directive. */
7666 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7667 if (token2->keyword == RID_NAMESPACE)
7668 cp_parser_using_directive (parser);
7669 /* Otherwise, it's a using-declaration. */
7670 else
7671 cp_parser_using_declaration (parser,
7672 /*access_declaration_p=*/false);
7674 /* If the next keyword is `__label__' we have a label declaration. */
7675 else if (token1->keyword == RID_LABEL)
7677 if (statement_p)
7678 cp_parser_commit_to_tentative_parse (parser);
7679 cp_parser_label_declaration (parser);
7681 /* If the next token is `static_assert' we have a static assertion. */
7682 else if (token1->keyword == RID_STATIC_ASSERT)
7683 cp_parser_static_assert (parser, /*member_p=*/false);
7684 /* Anything else must be a simple-declaration. */
7685 else
7686 cp_parser_simple_declaration (parser, !statement_p);
7689 /* Parse a simple-declaration.
7691 simple-declaration:
7692 decl-specifier-seq [opt] init-declarator-list [opt] ;
7694 init-declarator-list:
7695 init-declarator
7696 init-declarator-list , init-declarator
7698 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7699 function-definition as a simple-declaration. */
7701 static void
7702 cp_parser_simple_declaration (cp_parser* parser,
7703 bool function_definition_allowed_p)
7705 cp_decl_specifier_seq decl_specifiers;
7706 int declares_class_or_enum;
7707 bool saw_declarator;
7709 /* Defer access checks until we know what is being declared; the
7710 checks for names appearing in the decl-specifier-seq should be
7711 done as if we were in the scope of the thing being declared. */
7712 push_deferring_access_checks (dk_deferred);
7714 /* Parse the decl-specifier-seq. We have to keep track of whether
7715 or not the decl-specifier-seq declares a named class or
7716 enumeration type, since that is the only case in which the
7717 init-declarator-list is allowed to be empty.
7719 [dcl.dcl]
7721 In a simple-declaration, the optional init-declarator-list can be
7722 omitted only when declaring a class or enumeration, that is when
7723 the decl-specifier-seq contains either a class-specifier, an
7724 elaborated-type-specifier, or an enum-specifier. */
7725 cp_parser_decl_specifier_seq (parser,
7726 CP_PARSER_FLAGS_OPTIONAL,
7727 &decl_specifiers,
7728 &declares_class_or_enum);
7729 /* We no longer need to defer access checks. */
7730 stop_deferring_access_checks ();
7732 /* In a block scope, a valid declaration must always have a
7733 decl-specifier-seq. By not trying to parse declarators, we can
7734 resolve the declaration/expression ambiguity more quickly. */
7735 if (!function_definition_allowed_p
7736 && !decl_specifiers.any_specifiers_p)
7738 cp_parser_error (parser, "expected declaration");
7739 goto done;
7742 /* If the next two tokens are both identifiers, the code is
7743 erroneous. The usual cause of this situation is code like:
7745 T t;
7747 where "T" should name a type -- but does not. */
7748 if (!decl_specifiers.type
7749 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7751 /* If parsing tentatively, we should commit; we really are
7752 looking at a declaration. */
7753 cp_parser_commit_to_tentative_parse (parser);
7754 /* Give up. */
7755 goto done;
7758 /* If we have seen at least one decl-specifier, and the next token
7759 is not a parenthesis, then we must be looking at a declaration.
7760 (After "int (" we might be looking at a functional cast.) */
7761 if (decl_specifiers.any_specifiers_p
7762 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7763 cp_parser_commit_to_tentative_parse (parser);
7765 /* Keep going until we hit the `;' at the end of the simple
7766 declaration. */
7767 saw_declarator = false;
7768 while (cp_lexer_next_token_is_not (parser->lexer,
7769 CPP_SEMICOLON))
7771 cp_token *token;
7772 bool function_definition_p;
7773 tree decl;
7775 if (saw_declarator)
7777 /* If we are processing next declarator, coma is expected */
7778 token = cp_lexer_peek_token (parser->lexer);
7779 gcc_assert (token->type == CPP_COMMA);
7780 cp_lexer_consume_token (parser->lexer);
7782 else
7783 saw_declarator = true;
7785 /* Parse the init-declarator. */
7786 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7787 /*checks=*/NULL,
7788 function_definition_allowed_p,
7789 /*member_p=*/false,
7790 declares_class_or_enum,
7791 &function_definition_p);
7792 /* If an error occurred while parsing tentatively, exit quickly.
7793 (That usually happens when in the body of a function; each
7794 statement is treated as a declaration-statement until proven
7795 otherwise.) */
7796 if (cp_parser_error_occurred (parser))
7797 goto done;
7798 /* Handle function definitions specially. */
7799 if (function_definition_p)
7801 /* If the next token is a `,', then we are probably
7802 processing something like:
7804 void f() {}, *p;
7806 which is erroneous. */
7807 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7808 error ("mixing declarations and function-definitions is forbidden");
7809 /* Otherwise, we're done with the list of declarators. */
7810 else
7812 pop_deferring_access_checks ();
7813 return;
7816 /* The next token should be either a `,' or a `;'. */
7817 token = cp_lexer_peek_token (parser->lexer);
7818 /* If it's a `,', there are more declarators to come. */
7819 if (token->type == CPP_COMMA)
7820 /* will be consumed next time around */;
7821 /* If it's a `;', we are done. */
7822 else if (token->type == CPP_SEMICOLON)
7823 break;
7824 /* Anything else is an error. */
7825 else
7827 /* If we have already issued an error message we don't need
7828 to issue another one. */
7829 if (decl != error_mark_node
7830 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7831 cp_parser_error (parser, "expected %<,%> or %<;%>");
7832 /* Skip tokens until we reach the end of the statement. */
7833 cp_parser_skip_to_end_of_statement (parser);
7834 /* If the next token is now a `;', consume it. */
7835 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7836 cp_lexer_consume_token (parser->lexer);
7837 goto done;
7839 /* After the first time around, a function-definition is not
7840 allowed -- even if it was OK at first. For example:
7842 int i, f() {}
7844 is not valid. */
7845 function_definition_allowed_p = false;
7848 /* Issue an error message if no declarators are present, and the
7849 decl-specifier-seq does not itself declare a class or
7850 enumeration. */
7851 if (!saw_declarator)
7853 if (cp_parser_declares_only_class_p (parser))
7854 shadow_tag (&decl_specifiers);
7855 /* Perform any deferred access checks. */
7856 perform_deferred_access_checks ();
7859 /* Consume the `;'. */
7860 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7862 done:
7863 pop_deferring_access_checks ();
7866 /* Parse a decl-specifier-seq.
7868 decl-specifier-seq:
7869 decl-specifier-seq [opt] decl-specifier
7871 decl-specifier:
7872 storage-class-specifier
7873 type-specifier
7874 function-specifier
7875 friend
7876 typedef
7878 GNU Extension:
7880 decl-specifier:
7881 attributes
7883 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7885 The parser flags FLAGS is used to control type-specifier parsing.
7887 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7888 flags:
7890 1: one of the decl-specifiers is an elaborated-type-specifier
7891 (i.e., a type declaration)
7892 2: one of the decl-specifiers is an enum-specifier or a
7893 class-specifier (i.e., a type definition)
7897 static void
7898 cp_parser_decl_specifier_seq (cp_parser* parser,
7899 cp_parser_flags flags,
7900 cp_decl_specifier_seq *decl_specs,
7901 int* declares_class_or_enum)
7903 bool constructor_possible_p = !parser->in_declarator_p;
7905 /* Clear DECL_SPECS. */
7906 clear_decl_specs (decl_specs);
7908 /* Assume no class or enumeration type is declared. */
7909 *declares_class_or_enum = 0;
7911 /* Keep reading specifiers until there are no more to read. */
7912 while (true)
7914 bool constructor_p;
7915 bool found_decl_spec;
7916 cp_token *token;
7918 /* Peek at the next token. */
7919 token = cp_lexer_peek_token (parser->lexer);
7920 /* Handle attributes. */
7921 if (token->keyword == RID_ATTRIBUTE)
7923 /* Parse the attributes. */
7924 decl_specs->attributes
7925 = chainon (decl_specs->attributes,
7926 cp_parser_attributes_opt (parser));
7927 continue;
7929 /* Assume we will find a decl-specifier keyword. */
7930 found_decl_spec = true;
7931 /* If the next token is an appropriate keyword, we can simply
7932 add it to the list. */
7933 switch (token->keyword)
7935 /* decl-specifier:
7936 friend */
7937 case RID_FRIEND:
7938 if (!at_class_scope_p ())
7940 error ("%<friend%> used outside of class");
7941 cp_lexer_purge_token (parser->lexer);
7943 else
7945 ++decl_specs->specs[(int) ds_friend];
7946 /* Consume the token. */
7947 cp_lexer_consume_token (parser->lexer);
7949 break;
7951 /* function-specifier:
7952 inline
7953 virtual
7954 explicit */
7955 case RID_INLINE:
7956 case RID_VIRTUAL:
7957 case RID_EXPLICIT:
7958 cp_parser_function_specifier_opt (parser, decl_specs);
7959 break;
7961 /* decl-specifier:
7962 typedef */
7963 case RID_TYPEDEF:
7964 ++decl_specs->specs[(int) ds_typedef];
7965 /* Consume the token. */
7966 cp_lexer_consume_token (parser->lexer);
7967 /* A constructor declarator cannot appear in a typedef. */
7968 constructor_possible_p = false;
7969 /* The "typedef" keyword can only occur in a declaration; we
7970 may as well commit at this point. */
7971 cp_parser_commit_to_tentative_parse (parser);
7973 if (decl_specs->storage_class != sc_none)
7974 decl_specs->conflicting_specifiers_p = true;
7975 break;
7977 /* storage-class-specifier:
7978 auto
7979 register
7980 static
7981 extern
7982 mutable
7984 GNU Extension:
7985 thread */
7986 case RID_AUTO:
7987 case RID_REGISTER:
7988 case RID_STATIC:
7989 case RID_EXTERN:
7990 case RID_MUTABLE:
7991 /* Consume the token. */
7992 cp_lexer_consume_token (parser->lexer);
7993 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7994 break;
7995 case RID_THREAD:
7996 /* Consume the token. */
7997 cp_lexer_consume_token (parser->lexer);
7998 ++decl_specs->specs[(int) ds_thread];
7999 break;
8001 default:
8002 /* We did not yet find a decl-specifier yet. */
8003 found_decl_spec = false;
8004 break;
8007 /* Constructors are a special case. The `S' in `S()' is not a
8008 decl-specifier; it is the beginning of the declarator. */
8009 constructor_p
8010 = (!found_decl_spec
8011 && constructor_possible_p
8012 && (cp_parser_constructor_declarator_p
8013 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8015 /* If we don't have a DECL_SPEC yet, then we must be looking at
8016 a type-specifier. */
8017 if (!found_decl_spec && !constructor_p)
8019 int decl_spec_declares_class_or_enum;
8020 bool is_cv_qualifier;
8021 tree type_spec;
8023 type_spec
8024 = cp_parser_type_specifier (parser, flags,
8025 decl_specs,
8026 /*is_declaration=*/true,
8027 &decl_spec_declares_class_or_enum,
8028 &is_cv_qualifier);
8030 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8032 /* If this type-specifier referenced a user-defined type
8033 (a typedef, class-name, etc.), then we can't allow any
8034 more such type-specifiers henceforth.
8036 [dcl.spec]
8038 The longest sequence of decl-specifiers that could
8039 possibly be a type name is taken as the
8040 decl-specifier-seq of a declaration. The sequence shall
8041 be self-consistent as described below.
8043 [dcl.type]
8045 As a general rule, at most one type-specifier is allowed
8046 in the complete decl-specifier-seq of a declaration. The
8047 only exceptions are the following:
8049 -- const or volatile can be combined with any other
8050 type-specifier.
8052 -- signed or unsigned can be combined with char, long,
8053 short, or int.
8055 -- ..
8057 Example:
8059 typedef char* Pc;
8060 void g (const int Pc);
8062 Here, Pc is *not* part of the decl-specifier seq; it's
8063 the declarator. Therefore, once we see a type-specifier
8064 (other than a cv-qualifier), we forbid any additional
8065 user-defined types. We *do* still allow things like `int
8066 int' to be considered a decl-specifier-seq, and issue the
8067 error message later. */
8068 if (type_spec && !is_cv_qualifier)
8069 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8070 /* A constructor declarator cannot follow a type-specifier. */
8071 if (type_spec)
8073 constructor_possible_p = false;
8074 found_decl_spec = true;
8078 /* If we still do not have a DECL_SPEC, then there are no more
8079 decl-specifiers. */
8080 if (!found_decl_spec)
8081 break;
8083 decl_specs->any_specifiers_p = true;
8084 /* After we see one decl-specifier, further decl-specifiers are
8085 always optional. */
8086 flags |= CP_PARSER_FLAGS_OPTIONAL;
8089 cp_parser_check_decl_spec (decl_specs);
8091 /* Don't allow a friend specifier with a class definition. */
8092 if (decl_specs->specs[(int) ds_friend] != 0
8093 && (*declares_class_or_enum & 2))
8094 error ("class definition may not be declared a friend");
8097 /* Parse an (optional) storage-class-specifier.
8099 storage-class-specifier:
8100 auto
8101 register
8102 static
8103 extern
8104 mutable
8106 GNU Extension:
8108 storage-class-specifier:
8109 thread
8111 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8113 static tree
8114 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8116 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8118 case RID_AUTO:
8119 case RID_REGISTER:
8120 case RID_STATIC:
8121 case RID_EXTERN:
8122 case RID_MUTABLE:
8123 case RID_THREAD:
8124 /* Consume the token. */
8125 return cp_lexer_consume_token (parser->lexer)->u.value;
8127 default:
8128 return NULL_TREE;
8132 /* Parse an (optional) function-specifier.
8134 function-specifier:
8135 inline
8136 virtual
8137 explicit
8139 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8140 Updates DECL_SPECS, if it is non-NULL. */
8142 static tree
8143 cp_parser_function_specifier_opt (cp_parser* parser,
8144 cp_decl_specifier_seq *decl_specs)
8146 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8148 case RID_INLINE:
8149 if (decl_specs)
8150 ++decl_specs->specs[(int) ds_inline];
8151 break;
8153 case RID_VIRTUAL:
8154 /* 14.5.2.3 [temp.mem]
8156 A member function template shall not be virtual. */
8157 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8158 error ("templates may not be %<virtual%>");
8159 else if (decl_specs)
8160 ++decl_specs->specs[(int) ds_virtual];
8161 break;
8163 case RID_EXPLICIT:
8164 if (decl_specs)
8165 ++decl_specs->specs[(int) ds_explicit];
8166 break;
8168 default:
8169 return NULL_TREE;
8172 /* Consume the token. */
8173 return cp_lexer_consume_token (parser->lexer)->u.value;
8176 /* Parse a linkage-specification.
8178 linkage-specification:
8179 extern string-literal { declaration-seq [opt] }
8180 extern string-literal declaration */
8182 static void
8183 cp_parser_linkage_specification (cp_parser* parser)
8185 tree linkage;
8187 /* Look for the `extern' keyword. */
8188 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8190 /* Look for the string-literal. */
8191 linkage = cp_parser_string_literal (parser, false, false);
8193 /* Transform the literal into an identifier. If the literal is a
8194 wide-character string, or contains embedded NULs, then we can't
8195 handle it as the user wants. */
8196 if (strlen (TREE_STRING_POINTER (linkage))
8197 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8199 cp_parser_error (parser, "invalid linkage-specification");
8200 /* Assume C++ linkage. */
8201 linkage = lang_name_cplusplus;
8203 else
8204 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8206 /* We're now using the new linkage. */
8207 push_lang_context (linkage);
8209 /* If the next token is a `{', then we're using the first
8210 production. */
8211 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8213 /* Consume the `{' token. */
8214 cp_lexer_consume_token (parser->lexer);
8215 /* Parse the declarations. */
8216 cp_parser_declaration_seq_opt (parser);
8217 /* Look for the closing `}'. */
8218 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8220 /* Otherwise, there's just one declaration. */
8221 else
8223 bool saved_in_unbraced_linkage_specification_p;
8225 saved_in_unbraced_linkage_specification_p
8226 = parser->in_unbraced_linkage_specification_p;
8227 parser->in_unbraced_linkage_specification_p = true;
8228 cp_parser_declaration (parser);
8229 parser->in_unbraced_linkage_specification_p
8230 = saved_in_unbraced_linkage_specification_p;
8233 /* We're done with the linkage-specification. */
8234 pop_lang_context ();
8237 /* Parse a static_assert-declaration.
8239 static_assert-declaration:
8240 static_assert ( constant-expression , string-literal ) ;
8242 If MEMBER_P, this static_assert is a class member. */
8244 static void
8245 cp_parser_static_assert(cp_parser *parser, bool member_p)
8247 tree condition;
8248 tree message;
8249 cp_token *token;
8250 location_t saved_loc;
8252 /* Peek at the `static_assert' token so we can keep track of exactly
8253 where the static assertion started. */
8254 token = cp_lexer_peek_token (parser->lexer);
8255 saved_loc = token->location;
8257 /* Look for the `static_assert' keyword. */
8258 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8259 "`static_assert'"))
8260 return;
8262 /* We know we are in a static assertion; commit to any tentative
8263 parse. */
8264 if (cp_parser_parsing_tentatively (parser))
8265 cp_parser_commit_to_tentative_parse (parser);
8267 /* Parse the `(' starting the static assertion condition. */
8268 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8270 /* Parse the constant-expression. */
8271 condition =
8272 cp_parser_constant_expression (parser,
8273 /*allow_non_constant_p=*/false,
8274 /*non_constant_p=*/NULL);
8276 /* Parse the separating `,'. */
8277 cp_parser_require (parser, CPP_COMMA, "`,'");
8279 /* Parse the string-literal message. */
8280 message = cp_parser_string_literal (parser,
8281 /*translate=*/false,
8282 /*wide_ok=*/true);
8284 /* A `)' completes the static assertion. */
8285 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8286 cp_parser_skip_to_closing_parenthesis (parser,
8287 /*recovering=*/true,
8288 /*or_comma=*/false,
8289 /*consume_paren=*/true);
8291 /* A semicolon terminates the declaration. */
8292 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8294 /* Complete the static assertion, which may mean either processing
8295 the static assert now or saving it for template instantiation. */
8296 finish_static_assert (condition, message, saved_loc, member_p);
8299 /* Special member functions [gram.special] */
8301 /* Parse a conversion-function-id.
8303 conversion-function-id:
8304 operator conversion-type-id
8306 Returns an IDENTIFIER_NODE representing the operator. */
8308 static tree
8309 cp_parser_conversion_function_id (cp_parser* parser)
8311 tree type;
8312 tree saved_scope;
8313 tree saved_qualifying_scope;
8314 tree saved_object_scope;
8315 tree pushed_scope = NULL_TREE;
8317 /* Look for the `operator' token. */
8318 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8319 return error_mark_node;
8320 /* When we parse the conversion-type-id, the current scope will be
8321 reset. However, we need that information in able to look up the
8322 conversion function later, so we save it here. */
8323 saved_scope = parser->scope;
8324 saved_qualifying_scope = parser->qualifying_scope;
8325 saved_object_scope = parser->object_scope;
8326 /* We must enter the scope of the class so that the names of
8327 entities declared within the class are available in the
8328 conversion-type-id. For example, consider:
8330 struct S {
8331 typedef int I;
8332 operator I();
8335 S::operator I() { ... }
8337 In order to see that `I' is a type-name in the definition, we
8338 must be in the scope of `S'. */
8339 if (saved_scope)
8340 pushed_scope = push_scope (saved_scope);
8341 /* Parse the conversion-type-id. */
8342 type = cp_parser_conversion_type_id (parser);
8343 /* Leave the scope of the class, if any. */
8344 if (pushed_scope)
8345 pop_scope (pushed_scope);
8346 /* Restore the saved scope. */
8347 parser->scope = saved_scope;
8348 parser->qualifying_scope = saved_qualifying_scope;
8349 parser->object_scope = saved_object_scope;
8350 /* If the TYPE is invalid, indicate failure. */
8351 if (type == error_mark_node)
8352 return error_mark_node;
8353 return mangle_conv_op_name_for_type (type);
8356 /* Parse a conversion-type-id:
8358 conversion-type-id:
8359 type-specifier-seq conversion-declarator [opt]
8361 Returns the TYPE specified. */
8363 static tree
8364 cp_parser_conversion_type_id (cp_parser* parser)
8366 tree attributes;
8367 cp_decl_specifier_seq type_specifiers;
8368 cp_declarator *declarator;
8369 tree type_specified;
8371 /* Parse the attributes. */
8372 attributes = cp_parser_attributes_opt (parser);
8373 /* Parse the type-specifiers. */
8374 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8375 &type_specifiers);
8376 /* If that didn't work, stop. */
8377 if (type_specifiers.type == error_mark_node)
8378 return error_mark_node;
8379 /* Parse the conversion-declarator. */
8380 declarator = cp_parser_conversion_declarator_opt (parser);
8382 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8383 /*initialized=*/0, &attributes);
8384 if (attributes)
8385 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8386 return type_specified;
8389 /* Parse an (optional) conversion-declarator.
8391 conversion-declarator:
8392 ptr-operator conversion-declarator [opt]
8396 static cp_declarator *
8397 cp_parser_conversion_declarator_opt (cp_parser* parser)
8399 enum tree_code code;
8400 tree class_type;
8401 cp_cv_quals cv_quals;
8403 /* We don't know if there's a ptr-operator next, or not. */
8404 cp_parser_parse_tentatively (parser);
8405 /* Try the ptr-operator. */
8406 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8407 /* If it worked, look for more conversion-declarators. */
8408 if (cp_parser_parse_definitely (parser))
8410 cp_declarator *declarator;
8412 /* Parse another optional declarator. */
8413 declarator = cp_parser_conversion_declarator_opt (parser);
8415 /* Create the representation of the declarator. */
8416 if (class_type)
8417 declarator = make_ptrmem_declarator (cv_quals, class_type,
8418 declarator);
8419 else if (code == INDIRECT_REF)
8420 declarator = make_pointer_declarator (cv_quals, declarator);
8421 else
8422 declarator = make_reference_declarator (cv_quals, declarator);
8424 return declarator;
8427 return NULL;
8430 /* Parse an (optional) ctor-initializer.
8432 ctor-initializer:
8433 : mem-initializer-list
8435 Returns TRUE iff the ctor-initializer was actually present. */
8437 static bool
8438 cp_parser_ctor_initializer_opt (cp_parser* parser)
8440 /* If the next token is not a `:', then there is no
8441 ctor-initializer. */
8442 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8444 /* Do default initialization of any bases and members. */
8445 if (DECL_CONSTRUCTOR_P (current_function_decl))
8446 finish_mem_initializers (NULL_TREE);
8448 return false;
8451 /* Consume the `:' token. */
8452 cp_lexer_consume_token (parser->lexer);
8453 /* And the mem-initializer-list. */
8454 cp_parser_mem_initializer_list (parser);
8456 return true;
8459 /* Parse a mem-initializer-list.
8461 mem-initializer-list:
8462 mem-initializer ... [opt]
8463 mem-initializer ... [opt] , mem-initializer-list */
8465 static void
8466 cp_parser_mem_initializer_list (cp_parser* parser)
8468 tree mem_initializer_list = NULL_TREE;
8470 /* Let the semantic analysis code know that we are starting the
8471 mem-initializer-list. */
8472 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8473 error ("only constructors take base initializers");
8475 /* Loop through the list. */
8476 while (true)
8478 tree mem_initializer;
8480 /* Parse the mem-initializer. */
8481 mem_initializer = cp_parser_mem_initializer (parser);
8482 /* If the next token is a `...', we're expanding member initializers. */
8483 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8485 /* Consume the `...'. */
8486 cp_lexer_consume_token (parser->lexer);
8488 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8489 can be expanded but members cannot. */
8490 if (mem_initializer != error_mark_node
8491 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8493 error ("cannot expand initializer for member %<%D%>",
8494 TREE_PURPOSE (mem_initializer));
8495 mem_initializer = error_mark_node;
8498 /* Construct the pack expansion type. */
8499 if (mem_initializer != error_mark_node)
8500 mem_initializer = make_pack_expansion (mem_initializer);
8502 /* Add it to the list, unless it was erroneous. */
8503 if (mem_initializer != error_mark_node)
8505 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8506 mem_initializer_list = mem_initializer;
8508 /* If the next token is not a `,', we're done. */
8509 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8510 break;
8511 /* Consume the `,' token. */
8512 cp_lexer_consume_token (parser->lexer);
8515 /* Perform semantic analysis. */
8516 if (DECL_CONSTRUCTOR_P (current_function_decl))
8517 finish_mem_initializers (mem_initializer_list);
8520 /* Parse a mem-initializer.
8522 mem-initializer:
8523 mem-initializer-id ( expression-list [opt] )
8525 GNU extension:
8527 mem-initializer:
8528 ( expression-list [opt] )
8530 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8531 class) or FIELD_DECL (for a non-static data member) to initialize;
8532 the TREE_VALUE is the expression-list. An empty initialization
8533 list is represented by void_list_node. */
8535 static tree
8536 cp_parser_mem_initializer (cp_parser* parser)
8538 tree mem_initializer_id;
8539 tree expression_list;
8540 tree member;
8542 /* Find out what is being initialized. */
8543 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8545 pedwarn ("anachronistic old-style base class initializer");
8546 mem_initializer_id = NULL_TREE;
8548 else
8549 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8550 member = expand_member_init (mem_initializer_id);
8551 if (member && !DECL_P (member))
8552 in_base_initializer = 1;
8554 expression_list
8555 = cp_parser_parenthesized_expression_list (parser, false,
8556 /*cast_p=*/false,
8557 /*allow_expansion_p=*/true,
8558 /*non_constant_p=*/NULL);
8559 if (expression_list == error_mark_node)
8560 return error_mark_node;
8561 if (!expression_list)
8562 expression_list = void_type_node;
8564 in_base_initializer = 0;
8566 return member ? build_tree_list (member, expression_list) : error_mark_node;
8569 /* Parse a mem-initializer-id.
8571 mem-initializer-id:
8572 :: [opt] nested-name-specifier [opt] class-name
8573 identifier
8575 Returns a TYPE indicating the class to be initializer for the first
8576 production. Returns an IDENTIFIER_NODE indicating the data member
8577 to be initialized for the second production. */
8579 static tree
8580 cp_parser_mem_initializer_id (cp_parser* parser)
8582 bool global_scope_p;
8583 bool nested_name_specifier_p;
8584 bool template_p = false;
8585 tree id;
8587 /* `typename' is not allowed in this context ([temp.res]). */
8588 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8590 error ("keyword %<typename%> not allowed in this context (a qualified "
8591 "member initializer is implicitly a type)");
8592 cp_lexer_consume_token (parser->lexer);
8594 /* Look for the optional `::' operator. */
8595 global_scope_p
8596 = (cp_parser_global_scope_opt (parser,
8597 /*current_scope_valid_p=*/false)
8598 != NULL_TREE);
8599 /* Look for the optional nested-name-specifier. The simplest way to
8600 implement:
8602 [temp.res]
8604 The keyword `typename' is not permitted in a base-specifier or
8605 mem-initializer; in these contexts a qualified name that
8606 depends on a template-parameter is implicitly assumed to be a
8607 type name.
8609 is to assume that we have seen the `typename' keyword at this
8610 point. */
8611 nested_name_specifier_p
8612 = (cp_parser_nested_name_specifier_opt (parser,
8613 /*typename_keyword_p=*/true,
8614 /*check_dependency_p=*/true,
8615 /*type_p=*/true,
8616 /*is_declaration=*/true)
8617 != NULL_TREE);
8618 if (nested_name_specifier_p)
8619 template_p = cp_parser_optional_template_keyword (parser);
8620 /* If there is a `::' operator or a nested-name-specifier, then we
8621 are definitely looking for a class-name. */
8622 if (global_scope_p || nested_name_specifier_p)
8623 return cp_parser_class_name (parser,
8624 /*typename_keyword_p=*/true,
8625 /*template_keyword_p=*/template_p,
8626 none_type,
8627 /*check_dependency_p=*/true,
8628 /*class_head_p=*/false,
8629 /*is_declaration=*/true);
8630 /* Otherwise, we could also be looking for an ordinary identifier. */
8631 cp_parser_parse_tentatively (parser);
8632 /* Try a class-name. */
8633 id = cp_parser_class_name (parser,
8634 /*typename_keyword_p=*/true,
8635 /*template_keyword_p=*/false,
8636 none_type,
8637 /*check_dependency_p=*/true,
8638 /*class_head_p=*/false,
8639 /*is_declaration=*/true);
8640 /* If we found one, we're done. */
8641 if (cp_parser_parse_definitely (parser))
8642 return id;
8643 /* Otherwise, look for an ordinary identifier. */
8644 return cp_parser_identifier (parser);
8647 /* Overloading [gram.over] */
8649 /* Parse an operator-function-id.
8651 operator-function-id:
8652 operator operator
8654 Returns an IDENTIFIER_NODE for the operator which is a
8655 human-readable spelling of the identifier, e.g., `operator +'. */
8657 static tree
8658 cp_parser_operator_function_id (cp_parser* parser)
8660 /* Look for the `operator' keyword. */
8661 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8662 return error_mark_node;
8663 /* And then the name of the operator itself. */
8664 return cp_parser_operator (parser);
8667 /* Parse an operator.
8669 operator:
8670 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8671 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8672 || ++ -- , ->* -> () []
8674 GNU Extensions:
8676 operator:
8677 <? >? <?= >?=
8679 Returns an IDENTIFIER_NODE for the operator which is a
8680 human-readable spelling of the identifier, e.g., `operator +'. */
8682 static tree
8683 cp_parser_operator (cp_parser* parser)
8685 tree id = NULL_TREE;
8686 cp_token *token;
8688 /* Peek at the next token. */
8689 token = cp_lexer_peek_token (parser->lexer);
8690 /* Figure out which operator we have. */
8691 switch (token->type)
8693 case CPP_KEYWORD:
8695 enum tree_code op;
8697 /* The keyword should be either `new' or `delete'. */
8698 if (token->keyword == RID_NEW)
8699 op = NEW_EXPR;
8700 else if (token->keyword == RID_DELETE)
8701 op = DELETE_EXPR;
8702 else
8703 break;
8705 /* Consume the `new' or `delete' token. */
8706 cp_lexer_consume_token (parser->lexer);
8708 /* Peek at the next token. */
8709 token = cp_lexer_peek_token (parser->lexer);
8710 /* If it's a `[' token then this is the array variant of the
8711 operator. */
8712 if (token->type == CPP_OPEN_SQUARE)
8714 /* Consume the `[' token. */
8715 cp_lexer_consume_token (parser->lexer);
8716 /* Look for the `]' token. */
8717 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8718 id = ansi_opname (op == NEW_EXPR
8719 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8721 /* Otherwise, we have the non-array variant. */
8722 else
8723 id = ansi_opname (op);
8725 return id;
8728 case CPP_PLUS:
8729 id = ansi_opname (PLUS_EXPR);
8730 break;
8732 case CPP_MINUS:
8733 id = ansi_opname (MINUS_EXPR);
8734 break;
8736 case CPP_MULT:
8737 id = ansi_opname (MULT_EXPR);
8738 break;
8740 case CPP_DIV:
8741 id = ansi_opname (TRUNC_DIV_EXPR);
8742 break;
8744 case CPP_MOD:
8745 id = ansi_opname (TRUNC_MOD_EXPR);
8746 break;
8748 case CPP_XOR:
8749 id = ansi_opname (BIT_XOR_EXPR);
8750 break;
8752 case CPP_AND:
8753 id = ansi_opname (BIT_AND_EXPR);
8754 break;
8756 case CPP_OR:
8757 id = ansi_opname (BIT_IOR_EXPR);
8758 break;
8760 case CPP_COMPL:
8761 id = ansi_opname (BIT_NOT_EXPR);
8762 break;
8764 case CPP_NOT:
8765 id = ansi_opname (TRUTH_NOT_EXPR);
8766 break;
8768 case CPP_EQ:
8769 id = ansi_assopname (NOP_EXPR);
8770 break;
8772 case CPP_LESS:
8773 id = ansi_opname (LT_EXPR);
8774 break;
8776 case CPP_GREATER:
8777 id = ansi_opname (GT_EXPR);
8778 break;
8780 case CPP_PLUS_EQ:
8781 id = ansi_assopname (PLUS_EXPR);
8782 break;
8784 case CPP_MINUS_EQ:
8785 id = ansi_assopname (MINUS_EXPR);
8786 break;
8788 case CPP_MULT_EQ:
8789 id = ansi_assopname (MULT_EXPR);
8790 break;
8792 case CPP_DIV_EQ:
8793 id = ansi_assopname (TRUNC_DIV_EXPR);
8794 break;
8796 case CPP_MOD_EQ:
8797 id = ansi_assopname (TRUNC_MOD_EXPR);
8798 break;
8800 case CPP_XOR_EQ:
8801 id = ansi_assopname (BIT_XOR_EXPR);
8802 break;
8804 case CPP_AND_EQ:
8805 id = ansi_assopname (BIT_AND_EXPR);
8806 break;
8808 case CPP_OR_EQ:
8809 id = ansi_assopname (BIT_IOR_EXPR);
8810 break;
8812 case CPP_LSHIFT:
8813 id = ansi_opname (LSHIFT_EXPR);
8814 break;
8816 case CPP_RSHIFT:
8817 id = ansi_opname (RSHIFT_EXPR);
8818 break;
8820 case CPP_LSHIFT_EQ:
8821 id = ansi_assopname (LSHIFT_EXPR);
8822 break;
8824 case CPP_RSHIFT_EQ:
8825 id = ansi_assopname (RSHIFT_EXPR);
8826 break;
8828 case CPP_EQ_EQ:
8829 id = ansi_opname (EQ_EXPR);
8830 break;
8832 case CPP_NOT_EQ:
8833 id = ansi_opname (NE_EXPR);
8834 break;
8836 case CPP_LESS_EQ:
8837 id = ansi_opname (LE_EXPR);
8838 break;
8840 case CPP_GREATER_EQ:
8841 id = ansi_opname (GE_EXPR);
8842 break;
8844 case CPP_AND_AND:
8845 id = ansi_opname (TRUTH_ANDIF_EXPR);
8846 break;
8848 case CPP_OR_OR:
8849 id = ansi_opname (TRUTH_ORIF_EXPR);
8850 break;
8852 case CPP_PLUS_PLUS:
8853 id = ansi_opname (POSTINCREMENT_EXPR);
8854 break;
8856 case CPP_MINUS_MINUS:
8857 id = ansi_opname (PREDECREMENT_EXPR);
8858 break;
8860 case CPP_COMMA:
8861 id = ansi_opname (COMPOUND_EXPR);
8862 break;
8864 case CPP_DEREF_STAR:
8865 id = ansi_opname (MEMBER_REF);
8866 break;
8868 case CPP_DEREF:
8869 id = ansi_opname (COMPONENT_REF);
8870 break;
8872 case CPP_OPEN_PAREN:
8873 /* Consume the `('. */
8874 cp_lexer_consume_token (parser->lexer);
8875 /* Look for the matching `)'. */
8876 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8877 return ansi_opname (CALL_EXPR);
8879 case CPP_OPEN_SQUARE:
8880 /* Consume the `['. */
8881 cp_lexer_consume_token (parser->lexer);
8882 /* Look for the matching `]'. */
8883 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8884 return ansi_opname (ARRAY_REF);
8886 default:
8887 /* Anything else is an error. */
8888 break;
8891 /* If we have selected an identifier, we need to consume the
8892 operator token. */
8893 if (id)
8894 cp_lexer_consume_token (parser->lexer);
8895 /* Otherwise, no valid operator name was present. */
8896 else
8898 cp_parser_error (parser, "expected operator");
8899 id = error_mark_node;
8902 return id;
8905 /* Parse a template-declaration.
8907 template-declaration:
8908 export [opt] template < template-parameter-list > declaration
8910 If MEMBER_P is TRUE, this template-declaration occurs within a
8911 class-specifier.
8913 The grammar rule given by the standard isn't correct. What
8914 is really meant is:
8916 template-declaration:
8917 export [opt] template-parameter-list-seq
8918 decl-specifier-seq [opt] init-declarator [opt] ;
8919 export [opt] template-parameter-list-seq
8920 function-definition
8922 template-parameter-list-seq:
8923 template-parameter-list-seq [opt]
8924 template < template-parameter-list > */
8926 static void
8927 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8929 /* Check for `export'. */
8930 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8932 /* Consume the `export' token. */
8933 cp_lexer_consume_token (parser->lexer);
8934 /* Warn that we do not support `export'. */
8935 warning (0, "keyword %<export%> not implemented, and will be ignored");
8938 cp_parser_template_declaration_after_export (parser, member_p);
8941 /* Parse a template-parameter-list.
8943 template-parameter-list:
8944 template-parameter
8945 template-parameter-list , template-parameter
8947 Returns a TREE_LIST. Each node represents a template parameter.
8948 The nodes are connected via their TREE_CHAINs. */
8950 static tree
8951 cp_parser_template_parameter_list (cp_parser* parser)
8953 tree parameter_list = NULL_TREE;
8955 begin_template_parm_list ();
8956 while (true)
8958 tree parameter;
8959 cp_token *token;
8960 bool is_non_type;
8961 bool is_parameter_pack;
8963 /* Parse the template-parameter. */
8964 parameter = cp_parser_template_parameter (parser,
8965 &is_non_type,
8966 &is_parameter_pack);
8967 /* Add it to the list. */
8968 if (parameter != error_mark_node)
8969 parameter_list = process_template_parm (parameter_list,
8970 parameter,
8971 is_non_type,
8972 is_parameter_pack);
8973 else
8975 tree err_parm = build_tree_list (parameter, parameter);
8976 TREE_VALUE (err_parm) = error_mark_node;
8977 parameter_list = chainon (parameter_list, err_parm);
8980 /* Peek at the next token. */
8981 token = cp_lexer_peek_token (parser->lexer);
8982 /* If it's not a `,', we're done. */
8983 if (token->type != CPP_COMMA)
8984 break;
8985 /* Otherwise, consume the `,' token. */
8986 cp_lexer_consume_token (parser->lexer);
8989 return end_template_parm_list (parameter_list);
8992 /* Parse a template-parameter.
8994 template-parameter:
8995 type-parameter
8996 parameter-declaration
8998 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8999 the parameter. The TREE_PURPOSE is the default value, if any.
9000 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9001 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9002 set to true iff this parameter is a parameter pack. */
9004 static tree
9005 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9006 bool *is_parameter_pack)
9008 cp_token *token;
9009 cp_parameter_declarator *parameter_declarator;
9010 tree parm;
9012 /* Assume it is a type parameter or a template parameter. */
9013 *is_non_type = false;
9014 /* Assume it not a parameter pack. */
9015 *is_parameter_pack = false;
9016 /* Peek at the next token. */
9017 token = cp_lexer_peek_token (parser->lexer);
9018 /* If it is `class' or `template', we have a type-parameter. */
9019 if (token->keyword == RID_TEMPLATE)
9020 return cp_parser_type_parameter (parser, is_parameter_pack);
9021 /* If it is `class' or `typename' we do not know yet whether it is a
9022 type parameter or a non-type parameter. Consider:
9024 template <typename T, typename T::X X> ...
9028 template <class C, class D*> ...
9030 Here, the first parameter is a type parameter, and the second is
9031 a non-type parameter. We can tell by looking at the token after
9032 the identifier -- if it is a `,', `=', or `>' then we have a type
9033 parameter. */
9034 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9036 /* Peek at the token after `class' or `typename'. */
9037 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9038 /* If it's an ellipsis, we have a template type parameter
9039 pack. */
9040 if (token->type == CPP_ELLIPSIS)
9041 return cp_parser_type_parameter (parser, is_parameter_pack);
9042 /* If it's an identifier, skip it. */
9043 if (token->type == CPP_NAME)
9044 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9045 /* Now, see if the token looks like the end of a template
9046 parameter. */
9047 if (token->type == CPP_COMMA
9048 || token->type == CPP_EQ
9049 || token->type == CPP_GREATER)
9050 return cp_parser_type_parameter (parser, is_parameter_pack);
9053 /* Otherwise, it is a non-type parameter.
9055 [temp.param]
9057 When parsing a default template-argument for a non-type
9058 template-parameter, the first non-nested `>' is taken as the end
9059 of the template parameter-list rather than a greater-than
9060 operator. */
9061 *is_non_type = true;
9062 parameter_declarator
9063 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9064 /*parenthesized_p=*/NULL);
9066 /* If the parameter declaration is marked as a parameter pack, set
9067 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9068 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9069 grokdeclarator. */
9070 if (parameter_declarator
9071 && parameter_declarator->declarator
9072 && parameter_declarator->declarator->parameter_pack_p)
9074 *is_parameter_pack = true;
9075 parameter_declarator->declarator->parameter_pack_p = false;
9078 /* If the next token is an ellipsis, and we don't already have it
9079 marked as a parameter pack, then we have a parameter pack (that
9080 has no declarator); */
9081 if (!*is_parameter_pack
9082 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9083 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9085 /* Consume the `...'. */
9086 cp_lexer_consume_token (parser->lexer);
9087 maybe_warn_variadic_templates ();
9089 *is_parameter_pack = true;
9092 parm = grokdeclarator (parameter_declarator->declarator,
9093 &parameter_declarator->decl_specifiers,
9094 PARM, /*initialized=*/0,
9095 /*attrlist=*/NULL);
9096 if (parm == error_mark_node)
9097 return error_mark_node;
9099 return build_tree_list (parameter_declarator->default_argument, parm);
9102 /* Parse a type-parameter.
9104 type-parameter:
9105 class identifier [opt]
9106 class identifier [opt] = type-id
9107 typename identifier [opt]
9108 typename identifier [opt] = type-id
9109 template < template-parameter-list > class identifier [opt]
9110 template < template-parameter-list > class identifier [opt]
9111 = id-expression
9113 GNU Extension (variadic templates):
9115 type-parameter:
9116 class ... identifier [opt]
9117 typename ... identifier [opt]
9119 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9120 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9121 the declaration of the parameter.
9123 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9125 static tree
9126 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9128 cp_token *token;
9129 tree parameter;
9131 /* Look for a keyword to tell us what kind of parameter this is. */
9132 token = cp_parser_require (parser, CPP_KEYWORD,
9133 "`class', `typename', or `template'");
9134 if (!token)
9135 return error_mark_node;
9137 switch (token->keyword)
9139 case RID_CLASS:
9140 case RID_TYPENAME:
9142 tree identifier;
9143 tree default_argument;
9145 /* If the next token is an ellipsis, we have a template
9146 argument pack. */
9147 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9149 /* Consume the `...' token. */
9150 cp_lexer_consume_token (parser->lexer);
9151 maybe_warn_variadic_templates ();
9153 *is_parameter_pack = true;
9156 /* If the next token is an identifier, then it names the
9157 parameter. */
9158 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9159 identifier = cp_parser_identifier (parser);
9160 else
9161 identifier = NULL_TREE;
9163 /* Create the parameter. */
9164 parameter = finish_template_type_parm (class_type_node, identifier);
9166 /* If the next token is an `=', we have a default argument. */
9167 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9169 /* Consume the `=' token. */
9170 cp_lexer_consume_token (parser->lexer);
9171 /* Parse the default-argument. */
9172 push_deferring_access_checks (dk_no_deferred);
9173 default_argument = cp_parser_type_id (parser);
9175 /* Template parameter packs cannot have default
9176 arguments. */
9177 if (*is_parameter_pack)
9179 if (identifier)
9180 error ("template parameter pack %qD cannot have a default argument",
9181 identifier);
9182 else
9183 error ("template parameter packs cannot have default arguments");
9184 default_argument = NULL_TREE;
9186 pop_deferring_access_checks ();
9188 else
9189 default_argument = NULL_TREE;
9191 /* Create the combined representation of the parameter and the
9192 default argument. */
9193 parameter = build_tree_list (default_argument, parameter);
9195 break;
9197 case RID_TEMPLATE:
9199 tree parameter_list;
9200 tree identifier;
9201 tree default_argument;
9203 /* Look for the `<'. */
9204 cp_parser_require (parser, CPP_LESS, "`<'");
9205 /* Parse the template-parameter-list. */
9206 parameter_list = cp_parser_template_parameter_list (parser);
9207 /* Look for the `>'. */
9208 cp_parser_require (parser, CPP_GREATER, "`>'");
9209 /* Look for the `class' keyword. */
9210 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9211 /* If the next token is an ellipsis, we have a template
9212 argument pack. */
9213 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9215 /* Consume the `...' token. */
9216 cp_lexer_consume_token (parser->lexer);
9217 maybe_warn_variadic_templates ();
9219 *is_parameter_pack = true;
9221 /* If the next token is an `=', then there is a
9222 default-argument. If the next token is a `>', we are at
9223 the end of the parameter-list. If the next token is a `,',
9224 then we are at the end of this parameter. */
9225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9226 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9227 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9229 identifier = cp_parser_identifier (parser);
9230 /* Treat invalid names as if the parameter were nameless. */
9231 if (identifier == error_mark_node)
9232 identifier = NULL_TREE;
9234 else
9235 identifier = NULL_TREE;
9237 /* Create the template parameter. */
9238 parameter = finish_template_template_parm (class_type_node,
9239 identifier);
9241 /* If the next token is an `=', then there is a
9242 default-argument. */
9243 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9245 bool is_template;
9247 /* Consume the `='. */
9248 cp_lexer_consume_token (parser->lexer);
9249 /* Parse the id-expression. */
9250 push_deferring_access_checks (dk_no_deferred);
9251 default_argument
9252 = cp_parser_id_expression (parser,
9253 /*template_keyword_p=*/false,
9254 /*check_dependency_p=*/true,
9255 /*template_p=*/&is_template,
9256 /*declarator_p=*/false,
9257 /*optional_p=*/false);
9258 if (TREE_CODE (default_argument) == TYPE_DECL)
9259 /* If the id-expression was a template-id that refers to
9260 a template-class, we already have the declaration here,
9261 so no further lookup is needed. */
9263 else
9264 /* Look up the name. */
9265 default_argument
9266 = cp_parser_lookup_name (parser, default_argument,
9267 none_type,
9268 /*is_template=*/is_template,
9269 /*is_namespace=*/false,
9270 /*check_dependency=*/true,
9271 /*ambiguous_decls=*/NULL);
9272 /* See if the default argument is valid. */
9273 default_argument
9274 = check_template_template_default_arg (default_argument);
9276 /* Template parameter packs cannot have default
9277 arguments. */
9278 if (*is_parameter_pack)
9280 if (identifier)
9281 error ("template parameter pack %qD cannot have a default argument",
9282 identifier);
9283 else
9284 error ("template parameter packs cannot have default arguments");
9285 default_argument = NULL_TREE;
9287 pop_deferring_access_checks ();
9289 else
9290 default_argument = NULL_TREE;
9292 /* Create the combined representation of the parameter and the
9293 default argument. */
9294 parameter = build_tree_list (default_argument, parameter);
9296 break;
9298 default:
9299 gcc_unreachable ();
9300 break;
9303 return parameter;
9306 /* Parse a template-id.
9308 template-id:
9309 template-name < template-argument-list [opt] >
9311 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9312 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9313 returned. Otherwise, if the template-name names a function, or set
9314 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9315 names a class, returns a TYPE_DECL for the specialization.
9317 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9318 uninstantiated templates. */
9320 static tree
9321 cp_parser_template_id (cp_parser *parser,
9322 bool template_keyword_p,
9323 bool check_dependency_p,
9324 bool is_declaration)
9326 int i;
9327 tree template;
9328 tree arguments;
9329 tree template_id;
9330 cp_token_position start_of_id = 0;
9331 deferred_access_check *chk;
9332 VEC (deferred_access_check,gc) *access_check;
9333 cp_token *next_token, *next_token_2;
9334 bool is_identifier;
9336 /* If the next token corresponds to a template-id, there is no need
9337 to reparse it. */
9338 next_token = cp_lexer_peek_token (parser->lexer);
9339 if (next_token->type == CPP_TEMPLATE_ID)
9341 struct tree_check *check_value;
9343 /* Get the stored value. */
9344 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9345 /* Perform any access checks that were deferred. */
9346 access_check = check_value->checks;
9347 if (access_check)
9349 for (i = 0 ;
9350 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9351 ++i)
9353 perform_or_defer_access_check (chk->binfo,
9354 chk->decl,
9355 chk->diag_decl);
9358 /* Return the stored value. */
9359 return check_value->value;
9362 /* Avoid performing name lookup if there is no possibility of
9363 finding a template-id. */
9364 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9365 || (next_token->type == CPP_NAME
9366 && !cp_parser_nth_token_starts_template_argument_list_p
9367 (parser, 2)))
9369 cp_parser_error (parser, "expected template-id");
9370 return error_mark_node;
9373 /* Remember where the template-id starts. */
9374 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9375 start_of_id = cp_lexer_token_position (parser->lexer, false);
9377 push_deferring_access_checks (dk_deferred);
9379 /* Parse the template-name. */
9380 is_identifier = false;
9381 template = cp_parser_template_name (parser, template_keyword_p,
9382 check_dependency_p,
9383 is_declaration,
9384 &is_identifier);
9385 if (template == error_mark_node || is_identifier)
9387 pop_deferring_access_checks ();
9388 return template;
9391 /* If we find the sequence `[:' after a template-name, it's probably
9392 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9393 parse correctly the argument list. */
9394 next_token = cp_lexer_peek_token (parser->lexer);
9395 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9396 if (next_token->type == CPP_OPEN_SQUARE
9397 && next_token->flags & DIGRAPH
9398 && next_token_2->type == CPP_COLON
9399 && !(next_token_2->flags & PREV_WHITE))
9401 cp_parser_parse_tentatively (parser);
9402 /* Change `:' into `::'. */
9403 next_token_2->type = CPP_SCOPE;
9404 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9405 CPP_LESS. */
9406 cp_lexer_consume_token (parser->lexer);
9407 /* Parse the arguments. */
9408 arguments = cp_parser_enclosed_template_argument_list (parser);
9409 if (!cp_parser_parse_definitely (parser))
9411 /* If we couldn't parse an argument list, then we revert our changes
9412 and return simply an error. Maybe this is not a template-id
9413 after all. */
9414 next_token_2->type = CPP_COLON;
9415 cp_parser_error (parser, "expected %<<%>");
9416 pop_deferring_access_checks ();
9417 return error_mark_node;
9419 /* Otherwise, emit an error about the invalid digraph, but continue
9420 parsing because we got our argument list. */
9421 pedwarn ("%<<::%> cannot begin a template-argument list");
9422 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9423 "between %<<%> and %<::%>");
9424 if (!flag_permissive)
9426 static bool hint;
9427 if (!hint)
9429 inform ("(if you use -fpermissive G++ will accept your code)");
9430 hint = true;
9434 else
9436 /* Look for the `<' that starts the template-argument-list. */
9437 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9439 pop_deferring_access_checks ();
9440 return error_mark_node;
9442 /* Parse the arguments. */
9443 arguments = cp_parser_enclosed_template_argument_list (parser);
9446 /* Build a representation of the specialization. */
9447 if (TREE_CODE (template) == IDENTIFIER_NODE)
9448 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9449 else if (DECL_CLASS_TEMPLATE_P (template)
9450 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9452 bool entering_scope;
9453 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9454 template (rather than some instantiation thereof) only if
9455 is not nested within some other construct. For example, in
9456 "template <typename T> void f(T) { A<T>::", A<T> is just an
9457 instantiation of A. */
9458 entering_scope = (template_parm_scope_p ()
9459 && cp_lexer_next_token_is (parser->lexer,
9460 CPP_SCOPE));
9461 template_id
9462 = finish_template_type (template, arguments, entering_scope);
9464 else
9466 /* If it's not a class-template or a template-template, it should be
9467 a function-template. */
9468 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9469 || TREE_CODE (template) == OVERLOAD
9470 || BASELINK_P (template)));
9472 template_id = lookup_template_function (template, arguments);
9475 /* If parsing tentatively, replace the sequence of tokens that makes
9476 up the template-id with a CPP_TEMPLATE_ID token. That way,
9477 should we re-parse the token stream, we will not have to repeat
9478 the effort required to do the parse, nor will we issue duplicate
9479 error messages about problems during instantiation of the
9480 template. */
9481 if (start_of_id)
9483 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9485 /* Reset the contents of the START_OF_ID token. */
9486 token->type = CPP_TEMPLATE_ID;
9487 /* Retrieve any deferred checks. Do not pop this access checks yet
9488 so the memory will not be reclaimed during token replacing below. */
9489 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9490 token->u.tree_check_value->value = template_id;
9491 token->u.tree_check_value->checks = get_deferred_access_checks ();
9492 token->keyword = RID_MAX;
9494 /* Purge all subsequent tokens. */
9495 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9497 /* ??? Can we actually assume that, if template_id ==
9498 error_mark_node, we will have issued a diagnostic to the
9499 user, as opposed to simply marking the tentative parse as
9500 failed? */
9501 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9502 error ("parse error in template argument list");
9505 pop_deferring_access_checks ();
9506 return template_id;
9509 /* Parse a template-name.
9511 template-name:
9512 identifier
9514 The standard should actually say:
9516 template-name:
9517 identifier
9518 operator-function-id
9520 A defect report has been filed about this issue.
9522 A conversion-function-id cannot be a template name because they cannot
9523 be part of a template-id. In fact, looking at this code:
9525 a.operator K<int>()
9527 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9528 It is impossible to call a templated conversion-function-id with an
9529 explicit argument list, since the only allowed template parameter is
9530 the type to which it is converting.
9532 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9533 `template' keyword, in a construction like:
9535 T::template f<3>()
9537 In that case `f' is taken to be a template-name, even though there
9538 is no way of knowing for sure.
9540 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9541 name refers to a set of overloaded functions, at least one of which
9542 is a template, or an IDENTIFIER_NODE with the name of the template,
9543 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9544 names are looked up inside uninstantiated templates. */
9546 static tree
9547 cp_parser_template_name (cp_parser* parser,
9548 bool template_keyword_p,
9549 bool check_dependency_p,
9550 bool is_declaration,
9551 bool *is_identifier)
9553 tree identifier;
9554 tree decl;
9555 tree fns;
9557 /* If the next token is `operator', then we have either an
9558 operator-function-id or a conversion-function-id. */
9559 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9561 /* We don't know whether we're looking at an
9562 operator-function-id or a conversion-function-id. */
9563 cp_parser_parse_tentatively (parser);
9564 /* Try an operator-function-id. */
9565 identifier = cp_parser_operator_function_id (parser);
9566 /* If that didn't work, try a conversion-function-id. */
9567 if (!cp_parser_parse_definitely (parser))
9569 cp_parser_error (parser, "expected template-name");
9570 return error_mark_node;
9573 /* Look for the identifier. */
9574 else
9575 identifier = cp_parser_identifier (parser);
9577 /* If we didn't find an identifier, we don't have a template-id. */
9578 if (identifier == error_mark_node)
9579 return error_mark_node;
9581 /* If the name immediately followed the `template' keyword, then it
9582 is a template-name. However, if the next token is not `<', then
9583 we do not treat it as a template-name, since it is not being used
9584 as part of a template-id. This enables us to handle constructs
9585 like:
9587 template <typename T> struct S { S(); };
9588 template <typename T> S<T>::S();
9590 correctly. We would treat `S' as a template -- if it were `S<T>'
9591 -- but we do not if there is no `<'. */
9593 if (processing_template_decl
9594 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9596 /* In a declaration, in a dependent context, we pretend that the
9597 "template" keyword was present in order to improve error
9598 recovery. For example, given:
9600 template <typename T> void f(T::X<int>);
9602 we want to treat "X<int>" as a template-id. */
9603 if (is_declaration
9604 && !template_keyword_p
9605 && parser->scope && TYPE_P (parser->scope)
9606 && check_dependency_p
9607 && dependent_type_p (parser->scope)
9608 /* Do not do this for dtors (or ctors), since they never
9609 need the template keyword before their name. */
9610 && !constructor_name_p (identifier, parser->scope))
9612 cp_token_position start = 0;
9614 /* Explain what went wrong. */
9615 error ("non-template %qD used as template", identifier);
9616 inform ("use %<%T::template %D%> to indicate that it is a template",
9617 parser->scope, identifier);
9618 /* If parsing tentatively, find the location of the "<" token. */
9619 if (cp_parser_simulate_error (parser))
9620 start = cp_lexer_token_position (parser->lexer, true);
9621 /* Parse the template arguments so that we can issue error
9622 messages about them. */
9623 cp_lexer_consume_token (parser->lexer);
9624 cp_parser_enclosed_template_argument_list (parser);
9625 /* Skip tokens until we find a good place from which to
9626 continue parsing. */
9627 cp_parser_skip_to_closing_parenthesis (parser,
9628 /*recovering=*/true,
9629 /*or_comma=*/true,
9630 /*consume_paren=*/false);
9631 /* If parsing tentatively, permanently remove the
9632 template argument list. That will prevent duplicate
9633 error messages from being issued about the missing
9634 "template" keyword. */
9635 if (start)
9636 cp_lexer_purge_tokens_after (parser->lexer, start);
9637 if (is_identifier)
9638 *is_identifier = true;
9639 return identifier;
9642 /* If the "template" keyword is present, then there is generally
9643 no point in doing name-lookup, so we just return IDENTIFIER.
9644 But, if the qualifying scope is non-dependent then we can
9645 (and must) do name-lookup normally. */
9646 if (template_keyword_p
9647 && (!parser->scope
9648 || (TYPE_P (parser->scope)
9649 && dependent_type_p (parser->scope))))
9650 return identifier;
9653 /* Look up the name. */
9654 decl = cp_parser_lookup_name (parser, identifier,
9655 none_type,
9656 /*is_template=*/false,
9657 /*is_namespace=*/false,
9658 check_dependency_p,
9659 /*ambiguous_decls=*/NULL);
9660 decl = maybe_get_template_decl_from_type_decl (decl);
9662 /* If DECL is a template, then the name was a template-name. */
9663 if (TREE_CODE (decl) == TEMPLATE_DECL)
9665 else
9667 tree fn = NULL_TREE;
9669 /* The standard does not explicitly indicate whether a name that
9670 names a set of overloaded declarations, some of which are
9671 templates, is a template-name. However, such a name should
9672 be a template-name; otherwise, there is no way to form a
9673 template-id for the overloaded templates. */
9674 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9675 if (TREE_CODE (fns) == OVERLOAD)
9676 for (fn = fns; fn; fn = OVL_NEXT (fn))
9677 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9678 break;
9680 if (!fn)
9682 /* The name does not name a template. */
9683 cp_parser_error (parser, "expected template-name");
9684 return error_mark_node;
9688 /* If DECL is dependent, and refers to a function, then just return
9689 its name; we will look it up again during template instantiation. */
9690 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9692 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9693 if (TYPE_P (scope) && dependent_type_p (scope))
9694 return identifier;
9697 return decl;
9700 /* Parse a template-argument-list.
9702 template-argument-list:
9703 template-argument ... [opt]
9704 template-argument-list , template-argument ... [opt]
9706 Returns a TREE_VEC containing the arguments. */
9708 static tree
9709 cp_parser_template_argument_list (cp_parser* parser)
9711 tree fixed_args[10];
9712 unsigned n_args = 0;
9713 unsigned alloced = 10;
9714 tree *arg_ary = fixed_args;
9715 tree vec;
9716 bool saved_in_template_argument_list_p;
9717 bool saved_ice_p;
9718 bool saved_non_ice_p;
9720 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9721 parser->in_template_argument_list_p = true;
9722 /* Even if the template-id appears in an integral
9723 constant-expression, the contents of the argument list do
9724 not. */
9725 saved_ice_p = parser->integral_constant_expression_p;
9726 parser->integral_constant_expression_p = false;
9727 saved_non_ice_p = parser->non_integral_constant_expression_p;
9728 parser->non_integral_constant_expression_p = false;
9729 /* Parse the arguments. */
9732 tree argument;
9734 if (n_args)
9735 /* Consume the comma. */
9736 cp_lexer_consume_token (parser->lexer);
9738 /* Parse the template-argument. */
9739 argument = cp_parser_template_argument (parser);
9741 /* If the next token is an ellipsis, we're expanding a template
9742 argument pack. */
9743 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9745 /* Consume the `...' token. */
9746 cp_lexer_consume_token (parser->lexer);
9748 /* Make the argument into a TYPE_PACK_EXPANSION or
9749 EXPR_PACK_EXPANSION. */
9750 argument = make_pack_expansion (argument);
9753 if (n_args == alloced)
9755 alloced *= 2;
9757 if (arg_ary == fixed_args)
9759 arg_ary = XNEWVEC (tree, alloced);
9760 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9762 else
9763 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9765 arg_ary[n_args++] = argument;
9767 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9769 vec = make_tree_vec (n_args);
9771 while (n_args--)
9772 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9774 if (arg_ary != fixed_args)
9775 free (arg_ary);
9776 parser->non_integral_constant_expression_p = saved_non_ice_p;
9777 parser->integral_constant_expression_p = saved_ice_p;
9778 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9779 return vec;
9782 /* Parse a template-argument.
9784 template-argument:
9785 assignment-expression
9786 type-id
9787 id-expression
9789 The representation is that of an assignment-expression, type-id, or
9790 id-expression -- except that the qualified id-expression is
9791 evaluated, so that the value returned is either a DECL or an
9792 OVERLOAD.
9794 Although the standard says "assignment-expression", it forbids
9795 throw-expressions or assignments in the template argument.
9796 Therefore, we use "conditional-expression" instead. */
9798 static tree
9799 cp_parser_template_argument (cp_parser* parser)
9801 tree argument;
9802 bool template_p;
9803 bool address_p;
9804 bool maybe_type_id = false;
9805 cp_token *token;
9806 cp_id_kind idk;
9808 /* There's really no way to know what we're looking at, so we just
9809 try each alternative in order.
9811 [temp.arg]
9813 In a template-argument, an ambiguity between a type-id and an
9814 expression is resolved to a type-id, regardless of the form of
9815 the corresponding template-parameter.
9817 Therefore, we try a type-id first. */
9818 cp_parser_parse_tentatively (parser);
9819 argument = cp_parser_type_id (parser);
9820 /* If there was no error parsing the type-id but the next token is a '>>',
9821 we probably found a typo for '> >'. But there are type-id which are
9822 also valid expressions. For instance:
9824 struct X { int operator >> (int); };
9825 template <int V> struct Foo {};
9826 Foo<X () >> 5> r;
9828 Here 'X()' is a valid type-id of a function type, but the user just
9829 wanted to write the expression "X() >> 5". Thus, we remember that we
9830 found a valid type-id, but we still try to parse the argument as an
9831 expression to see what happens. */
9832 if (!cp_parser_error_occurred (parser)
9833 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9835 maybe_type_id = true;
9836 cp_parser_abort_tentative_parse (parser);
9838 else
9840 /* If the next token isn't a `,' or a `>', then this argument wasn't
9841 really finished. This means that the argument is not a valid
9842 type-id. */
9843 if (!cp_parser_next_token_ends_template_argument_p (parser))
9844 cp_parser_error (parser, "expected template-argument");
9845 /* If that worked, we're done. */
9846 if (cp_parser_parse_definitely (parser))
9847 return argument;
9849 /* We're still not sure what the argument will be. */
9850 cp_parser_parse_tentatively (parser);
9851 /* Try a template. */
9852 argument = cp_parser_id_expression (parser,
9853 /*template_keyword_p=*/false,
9854 /*check_dependency_p=*/true,
9855 &template_p,
9856 /*declarator_p=*/false,
9857 /*optional_p=*/false);
9858 /* If the next token isn't a `,' or a `>', then this argument wasn't
9859 really finished. */
9860 if (!cp_parser_next_token_ends_template_argument_p (parser))
9861 cp_parser_error (parser, "expected template-argument");
9862 if (!cp_parser_error_occurred (parser))
9864 /* Figure out what is being referred to. If the id-expression
9865 was for a class template specialization, then we will have a
9866 TYPE_DECL at this point. There is no need to do name lookup
9867 at this point in that case. */
9868 if (TREE_CODE (argument) != TYPE_DECL)
9869 argument = cp_parser_lookup_name (parser, argument,
9870 none_type,
9871 /*is_template=*/template_p,
9872 /*is_namespace=*/false,
9873 /*check_dependency=*/true,
9874 /*ambiguous_decls=*/NULL);
9875 if (TREE_CODE (argument) != TEMPLATE_DECL
9876 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9877 cp_parser_error (parser, "expected template-name");
9879 if (cp_parser_parse_definitely (parser))
9880 return argument;
9881 /* It must be a non-type argument. There permitted cases are given
9882 in [temp.arg.nontype]:
9884 -- an integral constant-expression of integral or enumeration
9885 type; or
9887 -- the name of a non-type template-parameter; or
9889 -- the name of an object or function with external linkage...
9891 -- the address of an object or function with external linkage...
9893 -- a pointer to member... */
9894 /* Look for a non-type template parameter. */
9895 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9897 cp_parser_parse_tentatively (parser);
9898 argument = cp_parser_primary_expression (parser,
9899 /*adress_p=*/false,
9900 /*cast_p=*/false,
9901 /*template_arg_p=*/true,
9902 &idk);
9903 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9904 || !cp_parser_next_token_ends_template_argument_p (parser))
9905 cp_parser_simulate_error (parser);
9906 if (cp_parser_parse_definitely (parser))
9907 return argument;
9910 /* If the next token is "&", the argument must be the address of an
9911 object or function with external linkage. */
9912 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9913 if (address_p)
9914 cp_lexer_consume_token (parser->lexer);
9915 /* See if we might have an id-expression. */
9916 token = cp_lexer_peek_token (parser->lexer);
9917 if (token->type == CPP_NAME
9918 || token->keyword == RID_OPERATOR
9919 || token->type == CPP_SCOPE
9920 || token->type == CPP_TEMPLATE_ID
9921 || token->type == CPP_NESTED_NAME_SPECIFIER)
9923 cp_parser_parse_tentatively (parser);
9924 argument = cp_parser_primary_expression (parser,
9925 address_p,
9926 /*cast_p=*/false,
9927 /*template_arg_p=*/true,
9928 &idk);
9929 if (cp_parser_error_occurred (parser)
9930 || !cp_parser_next_token_ends_template_argument_p (parser))
9931 cp_parser_abort_tentative_parse (parser);
9932 else
9934 if (TREE_CODE (argument) == INDIRECT_REF)
9936 gcc_assert (REFERENCE_REF_P (argument));
9937 argument = TREE_OPERAND (argument, 0);
9940 if (TREE_CODE (argument) == VAR_DECL)
9942 /* A variable without external linkage might still be a
9943 valid constant-expression, so no error is issued here
9944 if the external-linkage check fails. */
9945 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9946 cp_parser_simulate_error (parser);
9948 else if (is_overloaded_fn (argument))
9949 /* All overloaded functions are allowed; if the external
9950 linkage test does not pass, an error will be issued
9951 later. */
9953 else if (address_p
9954 && (TREE_CODE (argument) == OFFSET_REF
9955 || TREE_CODE (argument) == SCOPE_REF))
9956 /* A pointer-to-member. */
9958 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9960 else
9961 cp_parser_simulate_error (parser);
9963 if (cp_parser_parse_definitely (parser))
9965 if (address_p)
9966 argument = build_x_unary_op (ADDR_EXPR, argument);
9967 return argument;
9971 /* If the argument started with "&", there are no other valid
9972 alternatives at this point. */
9973 if (address_p)
9975 cp_parser_error (parser, "invalid non-type template argument");
9976 return error_mark_node;
9979 /* If the argument wasn't successfully parsed as a type-id followed
9980 by '>>', the argument can only be a constant expression now.
9981 Otherwise, we try parsing the constant-expression tentatively,
9982 because the argument could really be a type-id. */
9983 if (maybe_type_id)
9984 cp_parser_parse_tentatively (parser);
9985 argument = cp_parser_constant_expression (parser,
9986 /*allow_non_constant_p=*/false,
9987 /*non_constant_p=*/NULL);
9988 argument = fold_non_dependent_expr (argument);
9989 if (!maybe_type_id)
9990 return argument;
9991 if (!cp_parser_next_token_ends_template_argument_p (parser))
9992 cp_parser_error (parser, "expected template-argument");
9993 if (cp_parser_parse_definitely (parser))
9994 return argument;
9995 /* We did our best to parse the argument as a non type-id, but that
9996 was the only alternative that matched (albeit with a '>' after
9997 it). We can assume it's just a typo from the user, and a
9998 diagnostic will then be issued. */
9999 return cp_parser_type_id (parser);
10002 /* Parse an explicit-instantiation.
10004 explicit-instantiation:
10005 template declaration
10007 Although the standard says `declaration', what it really means is:
10009 explicit-instantiation:
10010 template decl-specifier-seq [opt] declarator [opt] ;
10012 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10013 supposed to be allowed. A defect report has been filed about this
10014 issue.
10016 GNU Extension:
10018 explicit-instantiation:
10019 storage-class-specifier template
10020 decl-specifier-seq [opt] declarator [opt] ;
10021 function-specifier template
10022 decl-specifier-seq [opt] declarator [opt] ; */
10024 static void
10025 cp_parser_explicit_instantiation (cp_parser* parser)
10027 int declares_class_or_enum;
10028 cp_decl_specifier_seq decl_specifiers;
10029 tree extension_specifier = NULL_TREE;
10031 /* Look for an (optional) storage-class-specifier or
10032 function-specifier. */
10033 if (cp_parser_allow_gnu_extensions_p (parser))
10035 extension_specifier
10036 = cp_parser_storage_class_specifier_opt (parser);
10037 if (!extension_specifier)
10038 extension_specifier
10039 = cp_parser_function_specifier_opt (parser,
10040 /*decl_specs=*/NULL);
10043 /* Look for the `template' keyword. */
10044 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10045 /* Let the front end know that we are processing an explicit
10046 instantiation. */
10047 begin_explicit_instantiation ();
10048 /* [temp.explicit] says that we are supposed to ignore access
10049 control while processing explicit instantiation directives. */
10050 push_deferring_access_checks (dk_no_check);
10051 /* Parse a decl-specifier-seq. */
10052 cp_parser_decl_specifier_seq (parser,
10053 CP_PARSER_FLAGS_OPTIONAL,
10054 &decl_specifiers,
10055 &declares_class_or_enum);
10056 /* If there was exactly one decl-specifier, and it declared a class,
10057 and there's no declarator, then we have an explicit type
10058 instantiation. */
10059 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10061 tree type;
10063 type = check_tag_decl (&decl_specifiers);
10064 /* Turn access control back on for names used during
10065 template instantiation. */
10066 pop_deferring_access_checks ();
10067 if (type)
10068 do_type_instantiation (type, extension_specifier,
10069 /*complain=*/tf_error);
10071 else
10073 cp_declarator *declarator;
10074 tree decl;
10076 /* Parse the declarator. */
10077 declarator
10078 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10079 /*ctor_dtor_or_conv_p=*/NULL,
10080 /*parenthesized_p=*/NULL,
10081 /*member_p=*/false);
10082 if (declares_class_or_enum & 2)
10083 cp_parser_check_for_definition_in_return_type (declarator,
10084 decl_specifiers.type);
10085 if (declarator != cp_error_declarator)
10087 decl = grokdeclarator (declarator, &decl_specifiers,
10088 NORMAL, 0, &decl_specifiers.attributes);
10089 /* Turn access control back on for names used during
10090 template instantiation. */
10091 pop_deferring_access_checks ();
10092 /* Do the explicit instantiation. */
10093 do_decl_instantiation (decl, extension_specifier);
10095 else
10097 pop_deferring_access_checks ();
10098 /* Skip the body of the explicit instantiation. */
10099 cp_parser_skip_to_end_of_statement (parser);
10102 /* We're done with the instantiation. */
10103 end_explicit_instantiation ();
10105 cp_parser_consume_semicolon_at_end_of_statement (parser);
10108 /* Parse an explicit-specialization.
10110 explicit-specialization:
10111 template < > declaration
10113 Although the standard says `declaration', what it really means is:
10115 explicit-specialization:
10116 template <> decl-specifier [opt] init-declarator [opt] ;
10117 template <> function-definition
10118 template <> explicit-specialization
10119 template <> template-declaration */
10121 static void
10122 cp_parser_explicit_specialization (cp_parser* parser)
10124 bool need_lang_pop;
10125 /* Look for the `template' keyword. */
10126 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10127 /* Look for the `<'. */
10128 cp_parser_require (parser, CPP_LESS, "`<'");
10129 /* Look for the `>'. */
10130 cp_parser_require (parser, CPP_GREATER, "`>'");
10131 /* We have processed another parameter list. */
10132 ++parser->num_template_parameter_lists;
10133 /* [temp]
10135 A template ... explicit specialization ... shall not have C
10136 linkage. */
10137 if (current_lang_name == lang_name_c)
10139 error ("template specialization with C linkage");
10140 /* Give it C++ linkage to avoid confusing other parts of the
10141 front end. */
10142 push_lang_context (lang_name_cplusplus);
10143 need_lang_pop = true;
10145 else
10146 need_lang_pop = false;
10147 /* Let the front end know that we are beginning a specialization. */
10148 if (!begin_specialization ())
10150 end_specialization ();
10151 cp_parser_skip_to_end_of_block_or_statement (parser);
10152 return;
10155 /* If the next keyword is `template', we need to figure out whether
10156 or not we're looking a template-declaration. */
10157 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10159 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10160 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10161 cp_parser_template_declaration_after_export (parser,
10162 /*member_p=*/false);
10163 else
10164 cp_parser_explicit_specialization (parser);
10166 else
10167 /* Parse the dependent declaration. */
10168 cp_parser_single_declaration (parser,
10169 /*checks=*/NULL,
10170 /*member_p=*/false,
10171 /*friend_p=*/NULL);
10172 /* We're done with the specialization. */
10173 end_specialization ();
10174 /* For the erroneous case of a template with C linkage, we pushed an
10175 implicit C++ linkage scope; exit that scope now. */
10176 if (need_lang_pop)
10177 pop_lang_context ();
10178 /* We're done with this parameter list. */
10179 --parser->num_template_parameter_lists;
10182 /* Parse a type-specifier.
10184 type-specifier:
10185 simple-type-specifier
10186 class-specifier
10187 enum-specifier
10188 elaborated-type-specifier
10189 cv-qualifier
10191 GNU Extension:
10193 type-specifier:
10194 __complex__
10196 Returns a representation of the type-specifier. For a
10197 class-specifier, enum-specifier, or elaborated-type-specifier, a
10198 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10200 The parser flags FLAGS is used to control type-specifier parsing.
10202 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10203 in a decl-specifier-seq.
10205 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10206 class-specifier, enum-specifier, or elaborated-type-specifier, then
10207 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10208 if a type is declared; 2 if it is defined. Otherwise, it is set to
10209 zero.
10211 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10212 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10213 is set to FALSE. */
10215 static tree
10216 cp_parser_type_specifier (cp_parser* parser,
10217 cp_parser_flags flags,
10218 cp_decl_specifier_seq *decl_specs,
10219 bool is_declaration,
10220 int* declares_class_or_enum,
10221 bool* is_cv_qualifier)
10223 tree type_spec = NULL_TREE;
10224 cp_token *token;
10225 enum rid keyword;
10226 cp_decl_spec ds = ds_last;
10228 /* Assume this type-specifier does not declare a new type. */
10229 if (declares_class_or_enum)
10230 *declares_class_or_enum = 0;
10231 /* And that it does not specify a cv-qualifier. */
10232 if (is_cv_qualifier)
10233 *is_cv_qualifier = false;
10234 /* Peek at the next token. */
10235 token = cp_lexer_peek_token (parser->lexer);
10237 /* If we're looking at a keyword, we can use that to guide the
10238 production we choose. */
10239 keyword = token->keyword;
10240 switch (keyword)
10242 case RID_ENUM:
10243 /* Look for the enum-specifier. */
10244 type_spec = cp_parser_enum_specifier (parser);
10245 /* If that worked, we're done. */
10246 if (type_spec)
10248 if (declares_class_or_enum)
10249 *declares_class_or_enum = 2;
10250 if (decl_specs)
10251 cp_parser_set_decl_spec_type (decl_specs,
10252 type_spec,
10253 /*user_defined_p=*/true);
10254 return type_spec;
10256 else
10257 goto elaborated_type_specifier;
10259 /* Any of these indicate either a class-specifier, or an
10260 elaborated-type-specifier. */
10261 case RID_CLASS:
10262 case RID_STRUCT:
10263 case RID_UNION:
10264 /* Parse tentatively so that we can back up if we don't find a
10265 class-specifier. */
10266 cp_parser_parse_tentatively (parser);
10267 /* Look for the class-specifier. */
10268 type_spec = cp_parser_class_specifier (parser);
10269 /* If that worked, we're done. */
10270 if (cp_parser_parse_definitely (parser))
10272 if (declares_class_or_enum)
10273 *declares_class_or_enum = 2;
10274 if (decl_specs)
10275 cp_parser_set_decl_spec_type (decl_specs,
10276 type_spec,
10277 /*user_defined_p=*/true);
10278 return type_spec;
10281 /* Fall through. */
10282 elaborated_type_specifier:
10283 /* We're declaring (not defining) a class or enum. */
10284 if (declares_class_or_enum)
10285 *declares_class_or_enum = 1;
10287 /* Fall through. */
10288 case RID_TYPENAME:
10289 /* Look for an elaborated-type-specifier. */
10290 type_spec
10291 = (cp_parser_elaborated_type_specifier
10292 (parser,
10293 decl_specs && decl_specs->specs[(int) ds_friend],
10294 is_declaration));
10295 if (decl_specs)
10296 cp_parser_set_decl_spec_type (decl_specs,
10297 type_spec,
10298 /*user_defined_p=*/true);
10299 return type_spec;
10301 case RID_CONST:
10302 ds = ds_const;
10303 if (is_cv_qualifier)
10304 *is_cv_qualifier = true;
10305 break;
10307 case RID_VOLATILE:
10308 ds = ds_volatile;
10309 if (is_cv_qualifier)
10310 *is_cv_qualifier = true;
10311 break;
10313 case RID_RESTRICT:
10314 ds = ds_restrict;
10315 if (is_cv_qualifier)
10316 *is_cv_qualifier = true;
10317 break;
10319 case RID_COMPLEX:
10320 /* The `__complex__' keyword is a GNU extension. */
10321 ds = ds_complex;
10322 break;
10324 default:
10325 break;
10328 /* Handle simple keywords. */
10329 if (ds != ds_last)
10331 if (decl_specs)
10333 ++decl_specs->specs[(int)ds];
10334 decl_specs->any_specifiers_p = true;
10336 return cp_lexer_consume_token (parser->lexer)->u.value;
10339 /* If we do not already have a type-specifier, assume we are looking
10340 at a simple-type-specifier. */
10341 type_spec = cp_parser_simple_type_specifier (parser,
10342 decl_specs,
10343 flags);
10345 /* If we didn't find a type-specifier, and a type-specifier was not
10346 optional in this context, issue an error message. */
10347 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10349 cp_parser_error (parser, "expected type specifier");
10350 return error_mark_node;
10353 return type_spec;
10356 /* Parse a simple-type-specifier.
10358 simple-type-specifier:
10359 :: [opt] nested-name-specifier [opt] type-name
10360 :: [opt] nested-name-specifier template template-id
10361 char
10362 wchar_t
10363 bool
10364 short
10366 long
10367 signed
10368 unsigned
10369 float
10370 double
10371 void
10373 GNU Extension:
10375 simple-type-specifier:
10376 __typeof__ unary-expression
10377 __typeof__ ( type-id )
10379 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10380 appropriately updated. */
10382 static tree
10383 cp_parser_simple_type_specifier (cp_parser* parser,
10384 cp_decl_specifier_seq *decl_specs,
10385 cp_parser_flags flags)
10387 tree type = NULL_TREE;
10388 cp_token *token;
10390 /* Peek at the next token. */
10391 token = cp_lexer_peek_token (parser->lexer);
10393 /* If we're looking at a keyword, things are easy. */
10394 switch (token->keyword)
10396 case RID_CHAR:
10397 if (decl_specs)
10398 decl_specs->explicit_char_p = true;
10399 type = char_type_node;
10400 break;
10401 case RID_WCHAR:
10402 type = wchar_type_node;
10403 break;
10404 case RID_BOOL:
10405 type = boolean_type_node;
10406 break;
10407 case RID_SHORT:
10408 if (decl_specs)
10409 ++decl_specs->specs[(int) ds_short];
10410 type = short_integer_type_node;
10411 break;
10412 case RID_INT:
10413 if (decl_specs)
10414 decl_specs->explicit_int_p = true;
10415 type = integer_type_node;
10416 break;
10417 case RID_LONG:
10418 if (decl_specs)
10419 ++decl_specs->specs[(int) ds_long];
10420 type = long_integer_type_node;
10421 break;
10422 case RID_SIGNED:
10423 if (decl_specs)
10424 ++decl_specs->specs[(int) ds_signed];
10425 type = integer_type_node;
10426 break;
10427 case RID_UNSIGNED:
10428 if (decl_specs)
10429 ++decl_specs->specs[(int) ds_unsigned];
10430 type = unsigned_type_node;
10431 break;
10432 case RID_FLOAT:
10433 type = float_type_node;
10434 break;
10435 case RID_DOUBLE:
10436 type = double_type_node;
10437 break;
10438 case RID_VOID:
10439 type = void_type_node;
10440 break;
10442 case RID_TYPEOF:
10443 /* Consume the `typeof' token. */
10444 cp_lexer_consume_token (parser->lexer);
10445 /* Parse the operand to `typeof'. */
10446 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10447 /* If it is not already a TYPE, take its type. */
10448 if (!TYPE_P (type))
10449 type = finish_typeof (type);
10451 if (decl_specs)
10452 cp_parser_set_decl_spec_type (decl_specs, type,
10453 /*user_defined_p=*/true);
10455 return type;
10457 default:
10458 break;
10461 /* If the type-specifier was for a built-in type, we're done. */
10462 if (type)
10464 tree id;
10466 /* Record the type. */
10467 if (decl_specs
10468 && (token->keyword != RID_SIGNED
10469 && token->keyword != RID_UNSIGNED
10470 && token->keyword != RID_SHORT
10471 && token->keyword != RID_LONG))
10472 cp_parser_set_decl_spec_type (decl_specs,
10473 type,
10474 /*user_defined=*/false);
10475 if (decl_specs)
10476 decl_specs->any_specifiers_p = true;
10478 /* Consume the token. */
10479 id = cp_lexer_consume_token (parser->lexer)->u.value;
10481 /* There is no valid C++ program where a non-template type is
10482 followed by a "<". That usually indicates that the user thought
10483 that the type was a template. */
10484 cp_parser_check_for_invalid_template_id (parser, type);
10486 return TYPE_NAME (type);
10489 /* The type-specifier must be a user-defined type. */
10490 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10492 bool qualified_p;
10493 bool global_p;
10495 /* Don't gobble tokens or issue error messages if this is an
10496 optional type-specifier. */
10497 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10498 cp_parser_parse_tentatively (parser);
10500 /* Look for the optional `::' operator. */
10501 global_p
10502 = (cp_parser_global_scope_opt (parser,
10503 /*current_scope_valid_p=*/false)
10504 != NULL_TREE);
10505 /* Look for the nested-name specifier. */
10506 qualified_p
10507 = (cp_parser_nested_name_specifier_opt (parser,
10508 /*typename_keyword_p=*/false,
10509 /*check_dependency_p=*/true,
10510 /*type_p=*/false,
10511 /*is_declaration=*/false)
10512 != NULL_TREE);
10513 /* If we have seen a nested-name-specifier, and the next token
10514 is `template', then we are using the template-id production. */
10515 if (parser->scope
10516 && cp_parser_optional_template_keyword (parser))
10518 /* Look for the template-id. */
10519 type = cp_parser_template_id (parser,
10520 /*template_keyword_p=*/true,
10521 /*check_dependency_p=*/true,
10522 /*is_declaration=*/false);
10523 /* If the template-id did not name a type, we are out of
10524 luck. */
10525 if (TREE_CODE (type) != TYPE_DECL)
10527 cp_parser_error (parser, "expected template-id for type");
10528 type = NULL_TREE;
10531 /* Otherwise, look for a type-name. */
10532 else
10533 type = cp_parser_type_name (parser);
10534 /* Keep track of all name-lookups performed in class scopes. */
10535 if (type
10536 && !global_p
10537 && !qualified_p
10538 && TREE_CODE (type) == TYPE_DECL
10539 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10540 maybe_note_name_used_in_class (DECL_NAME (type), type);
10541 /* If it didn't work out, we don't have a TYPE. */
10542 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10543 && !cp_parser_parse_definitely (parser))
10544 type = NULL_TREE;
10545 if (type && decl_specs)
10546 cp_parser_set_decl_spec_type (decl_specs, type,
10547 /*user_defined=*/true);
10550 /* If we didn't get a type-name, issue an error message. */
10551 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10553 cp_parser_error (parser, "expected type-name");
10554 return error_mark_node;
10557 /* There is no valid C++ program where a non-template type is
10558 followed by a "<". That usually indicates that the user thought
10559 that the type was a template. */
10560 if (type && type != error_mark_node)
10562 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10563 If it is, then the '<'...'>' enclose protocol names rather than
10564 template arguments, and so everything is fine. */
10565 if (c_dialect_objc ()
10566 && (objc_is_id (type) || objc_is_class_name (type)))
10568 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10569 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10571 /* Clobber the "unqualified" type previously entered into
10572 DECL_SPECS with the new, improved protocol-qualified version. */
10573 if (decl_specs)
10574 decl_specs->type = qual_type;
10576 return qual_type;
10579 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10582 return type;
10585 /* Parse a type-name.
10587 type-name:
10588 class-name
10589 enum-name
10590 typedef-name
10592 enum-name:
10593 identifier
10595 typedef-name:
10596 identifier
10598 Returns a TYPE_DECL for the type. */
10600 static tree
10601 cp_parser_type_name (cp_parser* parser)
10603 tree type_decl;
10604 tree identifier;
10606 /* We can't know yet whether it is a class-name or not. */
10607 cp_parser_parse_tentatively (parser);
10608 /* Try a class-name. */
10609 type_decl = cp_parser_class_name (parser,
10610 /*typename_keyword_p=*/false,
10611 /*template_keyword_p=*/false,
10612 none_type,
10613 /*check_dependency_p=*/true,
10614 /*class_head_p=*/false,
10615 /*is_declaration=*/false);
10616 /* If it's not a class-name, keep looking. */
10617 if (!cp_parser_parse_definitely (parser))
10619 /* It must be a typedef-name or an enum-name. */
10620 identifier = cp_parser_identifier (parser);
10621 if (identifier == error_mark_node)
10622 return error_mark_node;
10624 /* Look up the type-name. */
10625 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10627 if (TREE_CODE (type_decl) != TYPE_DECL
10628 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10630 /* See if this is an Objective-C type. */
10631 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10632 tree type = objc_get_protocol_qualified_type (identifier, protos);
10633 if (type)
10634 type_decl = TYPE_NAME (type);
10637 /* Issue an error if we did not find a type-name. */
10638 if (TREE_CODE (type_decl) != TYPE_DECL)
10640 if (!cp_parser_simulate_error (parser))
10641 cp_parser_name_lookup_error (parser, identifier, type_decl,
10642 "is not a type");
10643 type_decl = error_mark_node;
10645 /* Remember that the name was used in the definition of the
10646 current class so that we can check later to see if the
10647 meaning would have been different after the class was
10648 entirely defined. */
10649 else if (type_decl != error_mark_node
10650 && !parser->scope)
10651 maybe_note_name_used_in_class (identifier, type_decl);
10654 return type_decl;
10658 /* Parse an elaborated-type-specifier. Note that the grammar given
10659 here incorporates the resolution to DR68.
10661 elaborated-type-specifier:
10662 class-key :: [opt] nested-name-specifier [opt] identifier
10663 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10664 enum :: [opt] nested-name-specifier [opt] identifier
10665 typename :: [opt] nested-name-specifier identifier
10666 typename :: [opt] nested-name-specifier template [opt]
10667 template-id
10669 GNU extension:
10671 elaborated-type-specifier:
10672 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10673 class-key attributes :: [opt] nested-name-specifier [opt]
10674 template [opt] template-id
10675 enum attributes :: [opt] nested-name-specifier [opt] identifier
10677 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10678 declared `friend'. If IS_DECLARATION is TRUE, then this
10679 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10680 something is being declared.
10682 Returns the TYPE specified. */
10684 static tree
10685 cp_parser_elaborated_type_specifier (cp_parser* parser,
10686 bool is_friend,
10687 bool is_declaration)
10689 enum tag_types tag_type;
10690 tree identifier;
10691 tree type = NULL_TREE;
10692 tree attributes = NULL_TREE;
10694 /* See if we're looking at the `enum' keyword. */
10695 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10697 /* Consume the `enum' token. */
10698 cp_lexer_consume_token (parser->lexer);
10699 /* Remember that it's an enumeration type. */
10700 tag_type = enum_type;
10701 /* Parse the attributes. */
10702 attributes = cp_parser_attributes_opt (parser);
10704 /* Or, it might be `typename'. */
10705 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10706 RID_TYPENAME))
10708 /* Consume the `typename' token. */
10709 cp_lexer_consume_token (parser->lexer);
10710 /* Remember that it's a `typename' type. */
10711 tag_type = typename_type;
10712 /* The `typename' keyword is only allowed in templates. */
10713 if (!processing_template_decl)
10714 pedwarn ("using %<typename%> outside of template");
10716 /* Otherwise it must be a class-key. */
10717 else
10719 tag_type = cp_parser_class_key (parser);
10720 if (tag_type == none_type)
10721 return error_mark_node;
10722 /* Parse the attributes. */
10723 attributes = cp_parser_attributes_opt (parser);
10726 /* Look for the `::' operator. */
10727 cp_parser_global_scope_opt (parser,
10728 /*current_scope_valid_p=*/false);
10729 /* Look for the nested-name-specifier. */
10730 if (tag_type == typename_type)
10732 if (!cp_parser_nested_name_specifier (parser,
10733 /*typename_keyword_p=*/true,
10734 /*check_dependency_p=*/true,
10735 /*type_p=*/true,
10736 is_declaration))
10737 return error_mark_node;
10739 else
10740 /* Even though `typename' is not present, the proposed resolution
10741 to Core Issue 180 says that in `class A<T>::B', `B' should be
10742 considered a type-name, even if `A<T>' is dependent. */
10743 cp_parser_nested_name_specifier_opt (parser,
10744 /*typename_keyword_p=*/true,
10745 /*check_dependency_p=*/true,
10746 /*type_p=*/true,
10747 is_declaration);
10748 /* For everything but enumeration types, consider a template-id.
10749 For an enumeration type, consider only a plain identifier. */
10750 if (tag_type != enum_type)
10752 bool template_p = false;
10753 tree decl;
10755 /* Allow the `template' keyword. */
10756 template_p = cp_parser_optional_template_keyword (parser);
10757 /* If we didn't see `template', we don't know if there's a
10758 template-id or not. */
10759 if (!template_p)
10760 cp_parser_parse_tentatively (parser);
10761 /* Parse the template-id. */
10762 decl = cp_parser_template_id (parser, template_p,
10763 /*check_dependency_p=*/true,
10764 is_declaration);
10765 /* If we didn't find a template-id, look for an ordinary
10766 identifier. */
10767 if (!template_p && !cp_parser_parse_definitely (parser))
10769 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10770 in effect, then we must assume that, upon instantiation, the
10771 template will correspond to a class. */
10772 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10773 && tag_type == typename_type)
10774 type = make_typename_type (parser->scope, decl,
10775 typename_type,
10776 /*complain=*/tf_error);
10777 else
10778 type = TREE_TYPE (decl);
10781 if (!type)
10783 identifier = cp_parser_identifier (parser);
10785 if (identifier == error_mark_node)
10787 parser->scope = NULL_TREE;
10788 return error_mark_node;
10791 /* For a `typename', we needn't call xref_tag. */
10792 if (tag_type == typename_type
10793 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10794 return cp_parser_make_typename_type (parser, parser->scope,
10795 identifier);
10796 /* Look up a qualified name in the usual way. */
10797 if (parser->scope)
10799 tree decl;
10801 decl = cp_parser_lookup_name (parser, identifier,
10802 tag_type,
10803 /*is_template=*/false,
10804 /*is_namespace=*/false,
10805 /*check_dependency=*/true,
10806 /*ambiguous_decls=*/NULL);
10808 /* If we are parsing friend declaration, DECL may be a
10809 TEMPLATE_DECL tree node here. However, we need to check
10810 whether this TEMPLATE_DECL results in valid code. Consider
10811 the following example:
10813 namespace N {
10814 template <class T> class C {};
10816 class X {
10817 template <class T> friend class N::C; // #1, valid code
10819 template <class T> class Y {
10820 friend class N::C; // #2, invalid code
10823 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10824 name lookup of `N::C'. We see that friend declaration must
10825 be template for the code to be valid. Note that
10826 processing_template_decl does not work here since it is
10827 always 1 for the above two cases. */
10829 decl = (cp_parser_maybe_treat_template_as_class
10830 (decl, /*tag_name_p=*/is_friend
10831 && parser->num_template_parameter_lists));
10833 if (TREE_CODE (decl) != TYPE_DECL)
10835 cp_parser_diagnose_invalid_type_name (parser,
10836 parser->scope,
10837 identifier);
10838 return error_mark_node;
10841 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10843 bool allow_template = (parser->num_template_parameter_lists
10844 || DECL_SELF_REFERENCE_P (decl));
10845 type = check_elaborated_type_specifier (tag_type, decl,
10846 allow_template);
10848 if (type == error_mark_node)
10849 return error_mark_node;
10852 type = TREE_TYPE (decl);
10854 else
10856 /* An elaborated-type-specifier sometimes introduces a new type and
10857 sometimes names an existing type. Normally, the rule is that it
10858 introduces a new type only if there is not an existing type of
10859 the same name already in scope. For example, given:
10861 struct S {};
10862 void f() { struct S s; }
10864 the `struct S' in the body of `f' is the same `struct S' as in
10865 the global scope; the existing definition is used. However, if
10866 there were no global declaration, this would introduce a new
10867 local class named `S'.
10869 An exception to this rule applies to the following code:
10871 namespace N { struct S; }
10873 Here, the elaborated-type-specifier names a new type
10874 unconditionally; even if there is already an `S' in the
10875 containing scope this declaration names a new type.
10876 This exception only applies if the elaborated-type-specifier
10877 forms the complete declaration:
10879 [class.name]
10881 A declaration consisting solely of `class-key identifier ;' is
10882 either a redeclaration of the name in the current scope or a
10883 forward declaration of the identifier as a class name. It
10884 introduces the name into the current scope.
10886 We are in this situation precisely when the next token is a `;'.
10888 An exception to the exception is that a `friend' declaration does
10889 *not* name a new type; i.e., given:
10891 struct S { friend struct T; };
10893 `T' is not a new type in the scope of `S'.
10895 Also, `new struct S' or `sizeof (struct S)' never results in the
10896 definition of a new type; a new type can only be declared in a
10897 declaration context. */
10899 tag_scope ts;
10900 bool template_p;
10902 if (is_friend)
10903 /* Friends have special name lookup rules. */
10904 ts = ts_within_enclosing_non_class;
10905 else if (is_declaration
10906 && cp_lexer_next_token_is (parser->lexer,
10907 CPP_SEMICOLON))
10908 /* This is a `class-key identifier ;' */
10909 ts = ts_current;
10910 else
10911 ts = ts_global;
10913 template_p =
10914 (parser->num_template_parameter_lists
10915 && (cp_parser_next_token_starts_class_definition_p (parser)
10916 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10917 /* An unqualified name was used to reference this type, so
10918 there were no qualifying templates. */
10919 if (!cp_parser_check_template_parameters (parser,
10920 /*num_templates=*/0))
10921 return error_mark_node;
10922 type = xref_tag (tag_type, identifier, ts, template_p);
10926 if (type == error_mark_node)
10927 return error_mark_node;
10929 /* Allow attributes on forward declarations of classes. */
10930 if (attributes)
10932 if (TREE_CODE (type) == TYPENAME_TYPE)
10933 warning (OPT_Wattributes,
10934 "attributes ignored on uninstantiated type");
10935 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10936 && ! processing_explicit_instantiation)
10937 warning (OPT_Wattributes,
10938 "attributes ignored on template instantiation");
10939 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10940 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10941 else
10942 warning (OPT_Wattributes,
10943 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10946 if (tag_type != enum_type)
10947 cp_parser_check_class_key (tag_type, type);
10949 /* A "<" cannot follow an elaborated type specifier. If that
10950 happens, the user was probably trying to form a template-id. */
10951 cp_parser_check_for_invalid_template_id (parser, type);
10953 return type;
10956 /* Parse an enum-specifier.
10958 enum-specifier:
10959 enum identifier [opt] { enumerator-list [opt] }
10961 GNU Extensions:
10962 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10963 attributes[opt]
10965 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10966 if the token stream isn't an enum-specifier after all. */
10968 static tree
10969 cp_parser_enum_specifier (cp_parser* parser)
10971 tree identifier;
10972 tree type;
10973 tree attributes;
10975 /* Parse tentatively so that we can back up if we don't find a
10976 enum-specifier. */
10977 cp_parser_parse_tentatively (parser);
10979 /* Caller guarantees that the current token is 'enum', an identifier
10980 possibly follows, and the token after that is an opening brace.
10981 If we don't have an identifier, fabricate an anonymous name for
10982 the enumeration being defined. */
10983 cp_lexer_consume_token (parser->lexer);
10985 attributes = cp_parser_attributes_opt (parser);
10987 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10988 identifier = cp_parser_identifier (parser);
10989 else
10990 identifier = make_anon_name ();
10992 /* Look for the `{' but don't consume it yet. */
10993 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10994 cp_parser_simulate_error (parser);
10996 if (!cp_parser_parse_definitely (parser))
10997 return NULL_TREE;
10999 /* Issue an error message if type-definitions are forbidden here. */
11000 if (!cp_parser_check_type_definition (parser))
11001 type = error_mark_node;
11002 else
11003 /* Create the new type. We do this before consuming the opening
11004 brace so the enum will be recorded as being on the line of its
11005 tag (or the 'enum' keyword, if there is no tag). */
11006 type = start_enum (identifier);
11008 /* Consume the opening brace. */
11009 cp_lexer_consume_token (parser->lexer);
11011 if (type == error_mark_node)
11013 cp_parser_skip_to_end_of_block_or_statement (parser);
11014 return error_mark_node;
11017 /* If the next token is not '}', then there are some enumerators. */
11018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11019 cp_parser_enumerator_list (parser, type);
11021 /* Consume the final '}'. */
11022 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11024 /* Look for trailing attributes to apply to this enumeration, and
11025 apply them if appropriate. */
11026 if (cp_parser_allow_gnu_extensions_p (parser))
11028 tree trailing_attr = cp_parser_attributes_opt (parser);
11029 cplus_decl_attributes (&type,
11030 trailing_attr,
11031 (int) ATTR_FLAG_TYPE_IN_PLACE);
11034 /* Finish up the enumeration. */
11035 finish_enum (type);
11037 return type;
11040 /* Parse an enumerator-list. The enumerators all have the indicated
11041 TYPE.
11043 enumerator-list:
11044 enumerator-definition
11045 enumerator-list , enumerator-definition */
11047 static void
11048 cp_parser_enumerator_list (cp_parser* parser, tree type)
11050 while (true)
11052 /* Parse an enumerator-definition. */
11053 cp_parser_enumerator_definition (parser, type);
11055 /* If the next token is not a ',', we've reached the end of
11056 the list. */
11057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11058 break;
11059 /* Otherwise, consume the `,' and keep going. */
11060 cp_lexer_consume_token (parser->lexer);
11061 /* If the next token is a `}', there is a trailing comma. */
11062 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11064 if (pedantic && !in_system_header)
11065 pedwarn ("comma at end of enumerator list");
11066 break;
11071 /* Parse an enumerator-definition. The enumerator has the indicated
11072 TYPE.
11074 enumerator-definition:
11075 enumerator
11076 enumerator = constant-expression
11078 enumerator:
11079 identifier */
11081 static void
11082 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11084 tree identifier;
11085 tree value;
11087 /* Look for the identifier. */
11088 identifier = cp_parser_identifier (parser);
11089 if (identifier == error_mark_node)
11090 return;
11092 /* If the next token is an '=', then there is an explicit value. */
11093 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11095 /* Consume the `=' token. */
11096 cp_lexer_consume_token (parser->lexer);
11097 /* Parse the value. */
11098 value = cp_parser_constant_expression (parser,
11099 /*allow_non_constant_p=*/false,
11100 NULL);
11102 else
11103 value = NULL_TREE;
11105 /* Create the enumerator. */
11106 build_enumerator (identifier, value, type);
11109 /* Parse a namespace-name.
11111 namespace-name:
11112 original-namespace-name
11113 namespace-alias
11115 Returns the NAMESPACE_DECL for the namespace. */
11117 static tree
11118 cp_parser_namespace_name (cp_parser* parser)
11120 tree identifier;
11121 tree namespace_decl;
11123 /* Get the name of the namespace. */
11124 identifier = cp_parser_identifier (parser);
11125 if (identifier == error_mark_node)
11126 return error_mark_node;
11128 /* Look up the identifier in the currently active scope. Look only
11129 for namespaces, due to:
11131 [basic.lookup.udir]
11133 When looking up a namespace-name in a using-directive or alias
11134 definition, only namespace names are considered.
11136 And:
11138 [basic.lookup.qual]
11140 During the lookup of a name preceding the :: scope resolution
11141 operator, object, function, and enumerator names are ignored.
11143 (Note that cp_parser_class_or_namespace_name only calls this
11144 function if the token after the name is the scope resolution
11145 operator.) */
11146 namespace_decl = cp_parser_lookup_name (parser, identifier,
11147 none_type,
11148 /*is_template=*/false,
11149 /*is_namespace=*/true,
11150 /*check_dependency=*/true,
11151 /*ambiguous_decls=*/NULL);
11152 /* If it's not a namespace, issue an error. */
11153 if (namespace_decl == error_mark_node
11154 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11156 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11157 error ("%qD is not a namespace-name", identifier);
11158 cp_parser_error (parser, "expected namespace-name");
11159 namespace_decl = error_mark_node;
11162 return namespace_decl;
11165 /* Parse a namespace-definition.
11167 namespace-definition:
11168 named-namespace-definition
11169 unnamed-namespace-definition
11171 named-namespace-definition:
11172 original-namespace-definition
11173 extension-namespace-definition
11175 original-namespace-definition:
11176 namespace identifier { namespace-body }
11178 extension-namespace-definition:
11179 namespace original-namespace-name { namespace-body }
11181 unnamed-namespace-definition:
11182 namespace { namespace-body } */
11184 static void
11185 cp_parser_namespace_definition (cp_parser* parser)
11187 tree identifier, attribs;
11189 /* Look for the `namespace' keyword. */
11190 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11192 /* Get the name of the namespace. We do not attempt to distinguish
11193 between an original-namespace-definition and an
11194 extension-namespace-definition at this point. The semantic
11195 analysis routines are responsible for that. */
11196 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11197 identifier = cp_parser_identifier (parser);
11198 else
11199 identifier = NULL_TREE;
11201 /* Parse any specified attributes. */
11202 attribs = cp_parser_attributes_opt (parser);
11204 /* Look for the `{' to start the namespace. */
11205 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11206 /* Start the namespace. */
11207 push_namespace_with_attribs (identifier, attribs);
11208 /* Parse the body of the namespace. */
11209 cp_parser_namespace_body (parser);
11210 /* Finish the namespace. */
11211 pop_namespace ();
11212 /* Look for the final `}'. */
11213 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11216 /* Parse a namespace-body.
11218 namespace-body:
11219 declaration-seq [opt] */
11221 static void
11222 cp_parser_namespace_body (cp_parser* parser)
11224 cp_parser_declaration_seq_opt (parser);
11227 /* Parse a namespace-alias-definition.
11229 namespace-alias-definition:
11230 namespace identifier = qualified-namespace-specifier ; */
11232 static void
11233 cp_parser_namespace_alias_definition (cp_parser* parser)
11235 tree identifier;
11236 tree namespace_specifier;
11238 /* Look for the `namespace' keyword. */
11239 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11240 /* Look for the identifier. */
11241 identifier = cp_parser_identifier (parser);
11242 if (identifier == error_mark_node)
11243 return;
11244 /* Look for the `=' token. */
11245 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11246 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11248 error ("%<namespace%> definition is not allowed here");
11249 /* Skip the definition. */
11250 cp_lexer_consume_token (parser->lexer);
11251 cp_parser_skip_to_closing_brace (parser);
11252 cp_lexer_consume_token (parser->lexer);
11253 return;
11255 cp_parser_require (parser, CPP_EQ, "`='");
11256 /* Look for the qualified-namespace-specifier. */
11257 namespace_specifier
11258 = cp_parser_qualified_namespace_specifier (parser);
11259 /* Look for the `;' token. */
11260 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11262 /* Register the alias in the symbol table. */
11263 do_namespace_alias (identifier, namespace_specifier);
11266 /* Parse a qualified-namespace-specifier.
11268 qualified-namespace-specifier:
11269 :: [opt] nested-name-specifier [opt] namespace-name
11271 Returns a NAMESPACE_DECL corresponding to the specified
11272 namespace. */
11274 static tree
11275 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11277 /* Look for the optional `::'. */
11278 cp_parser_global_scope_opt (parser,
11279 /*current_scope_valid_p=*/false);
11281 /* Look for the optional nested-name-specifier. */
11282 cp_parser_nested_name_specifier_opt (parser,
11283 /*typename_keyword_p=*/false,
11284 /*check_dependency_p=*/true,
11285 /*type_p=*/false,
11286 /*is_declaration=*/true);
11288 return cp_parser_namespace_name (parser);
11291 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11292 access declaration.
11294 using-declaration:
11295 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11296 using :: unqualified-id ;
11298 access-declaration:
11299 qualified-id ;
11303 static bool
11304 cp_parser_using_declaration (cp_parser* parser,
11305 bool access_declaration_p)
11307 cp_token *token;
11308 bool typename_p = false;
11309 bool global_scope_p;
11310 tree decl;
11311 tree identifier;
11312 tree qscope;
11314 if (access_declaration_p)
11315 cp_parser_parse_tentatively (parser);
11316 else
11318 /* Look for the `using' keyword. */
11319 cp_parser_require_keyword (parser, RID_USING, "`using'");
11321 /* Peek at the next token. */
11322 token = cp_lexer_peek_token (parser->lexer);
11323 /* See if it's `typename'. */
11324 if (token->keyword == RID_TYPENAME)
11326 /* Remember that we've seen it. */
11327 typename_p = true;
11328 /* Consume the `typename' token. */
11329 cp_lexer_consume_token (parser->lexer);
11333 /* Look for the optional global scope qualification. */
11334 global_scope_p
11335 = (cp_parser_global_scope_opt (parser,
11336 /*current_scope_valid_p=*/false)
11337 != NULL_TREE);
11339 /* If we saw `typename', or didn't see `::', then there must be a
11340 nested-name-specifier present. */
11341 if (typename_p || !global_scope_p)
11342 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11343 /*check_dependency_p=*/true,
11344 /*type_p=*/false,
11345 /*is_declaration=*/true);
11346 /* Otherwise, we could be in either of the two productions. In that
11347 case, treat the nested-name-specifier as optional. */
11348 else
11349 qscope = cp_parser_nested_name_specifier_opt (parser,
11350 /*typename_keyword_p=*/false,
11351 /*check_dependency_p=*/true,
11352 /*type_p=*/false,
11353 /*is_declaration=*/true);
11354 if (!qscope)
11355 qscope = global_namespace;
11357 if (access_declaration_p && cp_parser_error_occurred (parser))
11358 /* Something has already gone wrong; there's no need to parse
11359 further. Since an error has occurred, the return value of
11360 cp_parser_parse_definitely will be false, as required. */
11361 return cp_parser_parse_definitely (parser);
11363 /* Parse the unqualified-id. */
11364 identifier = cp_parser_unqualified_id (parser,
11365 /*template_keyword_p=*/false,
11366 /*check_dependency_p=*/true,
11367 /*declarator_p=*/true,
11368 /*optional_p=*/false);
11370 if (access_declaration_p)
11372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11373 cp_parser_simulate_error (parser);
11374 if (!cp_parser_parse_definitely (parser))
11375 return false;
11378 /* The function we call to handle a using-declaration is different
11379 depending on what scope we are in. */
11380 if (qscope == error_mark_node || identifier == error_mark_node)
11382 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11383 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11384 /* [namespace.udecl]
11386 A using declaration shall not name a template-id. */
11387 error ("a template-id may not appear in a using-declaration");
11388 else
11390 if (at_class_scope_p ())
11392 /* Create the USING_DECL. */
11393 decl = do_class_using_decl (parser->scope, identifier);
11394 /* Add it to the list of members in this class. */
11395 finish_member_declaration (decl);
11397 else
11399 decl = cp_parser_lookup_name_simple (parser, identifier);
11400 if (decl == error_mark_node)
11401 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11402 else if (!at_namespace_scope_p ())
11403 do_local_using_decl (decl, qscope, identifier);
11404 else
11405 do_toplevel_using_decl (decl, qscope, identifier);
11409 /* Look for the final `;'. */
11410 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11412 return true;
11415 /* Parse a using-directive.
11417 using-directive:
11418 using namespace :: [opt] nested-name-specifier [opt]
11419 namespace-name ; */
11421 static void
11422 cp_parser_using_directive (cp_parser* parser)
11424 tree namespace_decl;
11425 tree attribs;
11427 /* Look for the `using' keyword. */
11428 cp_parser_require_keyword (parser, RID_USING, "`using'");
11429 /* And the `namespace' keyword. */
11430 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11431 /* Look for the optional `::' operator. */
11432 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11433 /* And the optional nested-name-specifier. */
11434 cp_parser_nested_name_specifier_opt (parser,
11435 /*typename_keyword_p=*/false,
11436 /*check_dependency_p=*/true,
11437 /*type_p=*/false,
11438 /*is_declaration=*/true);
11439 /* Get the namespace being used. */
11440 namespace_decl = cp_parser_namespace_name (parser);
11441 /* And any specified attributes. */
11442 attribs = cp_parser_attributes_opt (parser);
11443 /* Update the symbol table. */
11444 parse_using_directive (namespace_decl, attribs);
11445 /* Look for the final `;'. */
11446 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11449 /* Parse an asm-definition.
11451 asm-definition:
11452 asm ( string-literal ) ;
11454 GNU Extension:
11456 asm-definition:
11457 asm volatile [opt] ( string-literal ) ;
11458 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11459 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11460 : asm-operand-list [opt] ) ;
11461 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11462 : asm-operand-list [opt]
11463 : asm-operand-list [opt] ) ; */
11465 static void
11466 cp_parser_asm_definition (cp_parser* parser)
11468 tree string;
11469 tree outputs = NULL_TREE;
11470 tree inputs = NULL_TREE;
11471 tree clobbers = NULL_TREE;
11472 tree asm_stmt;
11473 bool volatile_p = false;
11474 bool extended_p = false;
11476 /* Look for the `asm' keyword. */
11477 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11478 /* See if the next token is `volatile'. */
11479 if (cp_parser_allow_gnu_extensions_p (parser)
11480 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11482 /* Remember that we saw the `volatile' keyword. */
11483 volatile_p = true;
11484 /* Consume the token. */
11485 cp_lexer_consume_token (parser->lexer);
11487 /* Look for the opening `('. */
11488 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11489 return;
11490 /* Look for the string. */
11491 string = cp_parser_string_literal (parser, false, false);
11492 if (string == error_mark_node)
11494 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11495 /*consume_paren=*/true);
11496 return;
11499 /* If we're allowing GNU extensions, check for the extended assembly
11500 syntax. Unfortunately, the `:' tokens need not be separated by
11501 a space in C, and so, for compatibility, we tolerate that here
11502 too. Doing that means that we have to treat the `::' operator as
11503 two `:' tokens. */
11504 if (cp_parser_allow_gnu_extensions_p (parser)
11505 && parser->in_function_body
11506 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11507 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11509 bool inputs_p = false;
11510 bool clobbers_p = false;
11512 /* The extended syntax was used. */
11513 extended_p = true;
11515 /* Look for outputs. */
11516 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11518 /* Consume the `:'. */
11519 cp_lexer_consume_token (parser->lexer);
11520 /* Parse the output-operands. */
11521 if (cp_lexer_next_token_is_not (parser->lexer,
11522 CPP_COLON)
11523 && cp_lexer_next_token_is_not (parser->lexer,
11524 CPP_SCOPE)
11525 && cp_lexer_next_token_is_not (parser->lexer,
11526 CPP_CLOSE_PAREN))
11527 outputs = cp_parser_asm_operand_list (parser);
11529 /* If the next token is `::', there are no outputs, and the
11530 next token is the beginning of the inputs. */
11531 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11532 /* The inputs are coming next. */
11533 inputs_p = true;
11535 /* Look for inputs. */
11536 if (inputs_p
11537 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11539 /* Consume the `:' or `::'. */
11540 cp_lexer_consume_token (parser->lexer);
11541 /* Parse the output-operands. */
11542 if (cp_lexer_next_token_is_not (parser->lexer,
11543 CPP_COLON)
11544 && cp_lexer_next_token_is_not (parser->lexer,
11545 CPP_CLOSE_PAREN))
11546 inputs = cp_parser_asm_operand_list (parser);
11548 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11549 /* The clobbers are coming next. */
11550 clobbers_p = true;
11552 /* Look for clobbers. */
11553 if (clobbers_p
11554 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11556 /* Consume the `:' or `::'. */
11557 cp_lexer_consume_token (parser->lexer);
11558 /* Parse the clobbers. */
11559 if (cp_lexer_next_token_is_not (parser->lexer,
11560 CPP_CLOSE_PAREN))
11561 clobbers = cp_parser_asm_clobber_list (parser);
11564 /* Look for the closing `)'. */
11565 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11566 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11567 /*consume_paren=*/true);
11568 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11570 /* Create the ASM_EXPR. */
11571 if (parser->in_function_body)
11573 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11574 inputs, clobbers);
11575 /* If the extended syntax was not used, mark the ASM_EXPR. */
11576 if (!extended_p)
11578 tree temp = asm_stmt;
11579 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11580 temp = TREE_OPERAND (temp, 0);
11582 ASM_INPUT_P (temp) = 1;
11585 else
11586 cgraph_add_asm_node (string);
11589 /* Declarators [gram.dcl.decl] */
11591 /* Parse an init-declarator.
11593 init-declarator:
11594 declarator initializer [opt]
11596 GNU Extension:
11598 init-declarator:
11599 declarator asm-specification [opt] attributes [opt] initializer [opt]
11601 function-definition:
11602 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11603 function-body
11604 decl-specifier-seq [opt] declarator function-try-block
11606 GNU Extension:
11608 function-definition:
11609 __extension__ function-definition
11611 The DECL_SPECIFIERS apply to this declarator. Returns a
11612 representation of the entity declared. If MEMBER_P is TRUE, then
11613 this declarator appears in a class scope. The new DECL created by
11614 this declarator is returned.
11616 The CHECKS are access checks that should be performed once we know
11617 what entity is being declared (and, therefore, what classes have
11618 befriended it).
11620 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11621 for a function-definition here as well. If the declarator is a
11622 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11623 be TRUE upon return. By that point, the function-definition will
11624 have been completely parsed.
11626 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11627 is FALSE. */
11629 static tree
11630 cp_parser_init_declarator (cp_parser* parser,
11631 cp_decl_specifier_seq *decl_specifiers,
11632 VEC (deferred_access_check,gc)* checks,
11633 bool function_definition_allowed_p,
11634 bool member_p,
11635 int declares_class_or_enum,
11636 bool* function_definition_p)
11638 cp_token *token;
11639 cp_declarator *declarator;
11640 tree prefix_attributes;
11641 tree attributes;
11642 tree asm_specification;
11643 tree initializer;
11644 tree decl = NULL_TREE;
11645 tree scope;
11646 bool is_initialized;
11647 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11648 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11649 "(...)". */
11650 enum cpp_ttype initialization_kind;
11651 bool is_parenthesized_init = false;
11652 bool is_non_constant_init;
11653 int ctor_dtor_or_conv_p;
11654 bool friend_p;
11655 tree pushed_scope = NULL;
11657 /* Gather the attributes that were provided with the
11658 decl-specifiers. */
11659 prefix_attributes = decl_specifiers->attributes;
11661 /* Assume that this is not the declarator for a function
11662 definition. */
11663 if (function_definition_p)
11664 *function_definition_p = false;
11666 /* Defer access checks while parsing the declarator; we cannot know
11667 what names are accessible until we know what is being
11668 declared. */
11669 resume_deferring_access_checks ();
11671 /* Parse the declarator. */
11672 declarator
11673 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11674 &ctor_dtor_or_conv_p,
11675 /*parenthesized_p=*/NULL,
11676 /*member_p=*/false);
11677 /* Gather up the deferred checks. */
11678 stop_deferring_access_checks ();
11680 /* If the DECLARATOR was erroneous, there's no need to go
11681 further. */
11682 if (declarator == cp_error_declarator)
11683 return error_mark_node;
11685 /* Check that the number of template-parameter-lists is OK. */
11686 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11687 return error_mark_node;
11689 if (declares_class_or_enum & 2)
11690 cp_parser_check_for_definition_in_return_type (declarator,
11691 decl_specifiers->type);
11693 /* Figure out what scope the entity declared by the DECLARATOR is
11694 located in. `grokdeclarator' sometimes changes the scope, so
11695 we compute it now. */
11696 scope = get_scope_of_declarator (declarator);
11698 /* If we're allowing GNU extensions, look for an asm-specification
11699 and attributes. */
11700 if (cp_parser_allow_gnu_extensions_p (parser))
11702 /* Look for an asm-specification. */
11703 asm_specification = cp_parser_asm_specification_opt (parser);
11704 /* And attributes. */
11705 attributes = cp_parser_attributes_opt (parser);
11707 else
11709 asm_specification = NULL_TREE;
11710 attributes = NULL_TREE;
11713 /* Peek at the next token. */
11714 token = cp_lexer_peek_token (parser->lexer);
11715 /* Check to see if the token indicates the start of a
11716 function-definition. */
11717 if (cp_parser_token_starts_function_definition_p (token))
11719 if (!function_definition_allowed_p)
11721 /* If a function-definition should not appear here, issue an
11722 error message. */
11723 cp_parser_error (parser,
11724 "a function-definition is not allowed here");
11725 return error_mark_node;
11727 else
11729 /* Neither attributes nor an asm-specification are allowed
11730 on a function-definition. */
11731 if (asm_specification)
11732 error ("an asm-specification is not allowed on a function-definition");
11733 if (attributes)
11734 error ("attributes are not allowed on a function-definition");
11735 /* This is a function-definition. */
11736 *function_definition_p = true;
11738 /* Parse the function definition. */
11739 if (member_p)
11740 decl = cp_parser_save_member_function_body (parser,
11741 decl_specifiers,
11742 declarator,
11743 prefix_attributes);
11744 else
11745 decl
11746 = (cp_parser_function_definition_from_specifiers_and_declarator
11747 (parser, decl_specifiers, prefix_attributes, declarator));
11749 return decl;
11753 /* [dcl.dcl]
11755 Only in function declarations for constructors, destructors, and
11756 type conversions can the decl-specifier-seq be omitted.
11758 We explicitly postpone this check past the point where we handle
11759 function-definitions because we tolerate function-definitions
11760 that are missing their return types in some modes. */
11761 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11763 cp_parser_error (parser,
11764 "expected constructor, destructor, or type conversion");
11765 return error_mark_node;
11768 /* An `=' or an `(' indicates an initializer. */
11769 if (token->type == CPP_EQ
11770 || token->type == CPP_OPEN_PAREN)
11772 is_initialized = true;
11773 initialization_kind = token->type;
11775 else
11777 /* If the init-declarator isn't initialized and isn't followed by a
11778 `,' or `;', it's not a valid init-declarator. */
11779 if (token->type != CPP_COMMA
11780 && token->type != CPP_SEMICOLON)
11782 cp_parser_error (parser, "expected initializer");
11783 return error_mark_node;
11785 is_initialized = false;
11786 initialization_kind = CPP_EOF;
11789 /* Because start_decl has side-effects, we should only call it if we
11790 know we're going ahead. By this point, we know that we cannot
11791 possibly be looking at any other construct. */
11792 cp_parser_commit_to_tentative_parse (parser);
11794 /* If the decl specifiers were bad, issue an error now that we're
11795 sure this was intended to be a declarator. Then continue
11796 declaring the variable(s), as int, to try to cut down on further
11797 errors. */
11798 if (decl_specifiers->any_specifiers_p
11799 && decl_specifiers->type == error_mark_node)
11801 cp_parser_error (parser, "invalid type in declaration");
11802 decl_specifiers->type = integer_type_node;
11805 /* Check to see whether or not this declaration is a friend. */
11806 friend_p = cp_parser_friend_p (decl_specifiers);
11808 /* Enter the newly declared entry in the symbol table. If we're
11809 processing a declaration in a class-specifier, we wait until
11810 after processing the initializer. */
11811 if (!member_p)
11813 if (parser->in_unbraced_linkage_specification_p)
11814 decl_specifiers->storage_class = sc_extern;
11815 decl = start_decl (declarator, decl_specifiers,
11816 is_initialized, attributes, prefix_attributes,
11817 &pushed_scope);
11819 else if (scope)
11820 /* Enter the SCOPE. That way unqualified names appearing in the
11821 initializer will be looked up in SCOPE. */
11822 pushed_scope = push_scope (scope);
11824 /* Perform deferred access control checks, now that we know in which
11825 SCOPE the declared entity resides. */
11826 if (!member_p && decl)
11828 tree saved_current_function_decl = NULL_TREE;
11830 /* If the entity being declared is a function, pretend that we
11831 are in its scope. If it is a `friend', it may have access to
11832 things that would not otherwise be accessible. */
11833 if (TREE_CODE (decl) == FUNCTION_DECL)
11835 saved_current_function_decl = current_function_decl;
11836 current_function_decl = decl;
11839 /* Perform access checks for template parameters. */
11840 cp_parser_perform_template_parameter_access_checks (checks);
11842 /* Perform the access control checks for the declarator and the
11843 the decl-specifiers. */
11844 perform_deferred_access_checks ();
11846 /* Restore the saved value. */
11847 if (TREE_CODE (decl) == FUNCTION_DECL)
11848 current_function_decl = saved_current_function_decl;
11851 /* Parse the initializer. */
11852 initializer = NULL_TREE;
11853 is_parenthesized_init = false;
11854 is_non_constant_init = true;
11855 if (is_initialized)
11857 if (function_declarator_p (declarator))
11859 if (initialization_kind == CPP_EQ)
11860 initializer = cp_parser_pure_specifier (parser);
11861 else
11863 /* If the declaration was erroneous, we don't really
11864 know what the user intended, so just silently
11865 consume the initializer. */
11866 if (decl != error_mark_node)
11867 error ("initializer provided for function");
11868 cp_parser_skip_to_closing_parenthesis (parser,
11869 /*recovering=*/true,
11870 /*or_comma=*/false,
11871 /*consume_paren=*/true);
11874 else
11875 initializer = cp_parser_initializer (parser,
11876 &is_parenthesized_init,
11877 &is_non_constant_init);
11880 /* The old parser allows attributes to appear after a parenthesized
11881 initializer. Mark Mitchell proposed removing this functionality
11882 on the GCC mailing lists on 2002-08-13. This parser accepts the
11883 attributes -- but ignores them. */
11884 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11885 if (cp_parser_attributes_opt (parser))
11886 warning (OPT_Wattributes,
11887 "attributes after parenthesized initializer ignored");
11889 /* For an in-class declaration, use `grokfield' to create the
11890 declaration. */
11891 if (member_p)
11893 if (pushed_scope)
11895 pop_scope (pushed_scope);
11896 pushed_scope = false;
11898 decl = grokfield (declarator, decl_specifiers,
11899 initializer, !is_non_constant_init,
11900 /*asmspec=*/NULL_TREE,
11901 prefix_attributes);
11902 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11903 cp_parser_save_default_args (parser, decl);
11906 /* Finish processing the declaration. But, skip friend
11907 declarations. */
11908 if (!friend_p && decl && decl != error_mark_node)
11910 cp_finish_decl (decl,
11911 initializer, !is_non_constant_init,
11912 asm_specification,
11913 /* If the initializer is in parentheses, then this is
11914 a direct-initialization, which means that an
11915 `explicit' constructor is OK. Otherwise, an
11916 `explicit' constructor cannot be used. */
11917 ((is_parenthesized_init || !is_initialized)
11918 ? 0 : LOOKUP_ONLYCONVERTING));
11920 else if (flag_cpp0x && friend_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
11921 /* Core issue #226 (C++0x only): A default template-argument
11922 shall not be specified in a friend class template
11923 declaration. */
11924 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
11925 /*is_partial=*/0, /*is_friend_decl=*/1);
11927 if (!friend_p && pushed_scope)
11928 pop_scope (pushed_scope);
11930 return decl;
11933 /* Parse a declarator.
11935 declarator:
11936 direct-declarator
11937 ptr-operator declarator
11939 abstract-declarator:
11940 ptr-operator abstract-declarator [opt]
11941 direct-abstract-declarator
11943 GNU Extensions:
11945 declarator:
11946 attributes [opt] direct-declarator
11947 attributes [opt] ptr-operator declarator
11949 abstract-declarator:
11950 attributes [opt] ptr-operator abstract-declarator [opt]
11951 attributes [opt] direct-abstract-declarator
11953 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11954 detect constructor, destructor or conversion operators. It is set
11955 to -1 if the declarator is a name, and +1 if it is a
11956 function. Otherwise it is set to zero. Usually you just want to
11957 test for >0, but internally the negative value is used.
11959 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11960 a decl-specifier-seq unless it declares a constructor, destructor,
11961 or conversion. It might seem that we could check this condition in
11962 semantic analysis, rather than parsing, but that makes it difficult
11963 to handle something like `f()'. We want to notice that there are
11964 no decl-specifiers, and therefore realize that this is an
11965 expression, not a declaration.)
11967 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11968 the declarator is a direct-declarator of the form "(...)".
11970 MEMBER_P is true iff this declarator is a member-declarator. */
11972 static cp_declarator *
11973 cp_parser_declarator (cp_parser* parser,
11974 cp_parser_declarator_kind dcl_kind,
11975 int* ctor_dtor_or_conv_p,
11976 bool* parenthesized_p,
11977 bool member_p)
11979 cp_token *token;
11980 cp_declarator *declarator;
11981 enum tree_code code;
11982 cp_cv_quals cv_quals;
11983 tree class_type;
11984 tree attributes = NULL_TREE;
11986 /* Assume this is not a constructor, destructor, or type-conversion
11987 operator. */
11988 if (ctor_dtor_or_conv_p)
11989 *ctor_dtor_or_conv_p = 0;
11991 if (cp_parser_allow_gnu_extensions_p (parser))
11992 attributes = cp_parser_attributes_opt (parser);
11994 /* Peek at the next token. */
11995 token = cp_lexer_peek_token (parser->lexer);
11997 /* Check for the ptr-operator production. */
11998 cp_parser_parse_tentatively (parser);
11999 /* Parse the ptr-operator. */
12000 code = cp_parser_ptr_operator (parser,
12001 &class_type,
12002 &cv_quals);
12003 /* If that worked, then we have a ptr-operator. */
12004 if (cp_parser_parse_definitely (parser))
12006 /* If a ptr-operator was found, then this declarator was not
12007 parenthesized. */
12008 if (parenthesized_p)
12009 *parenthesized_p = true;
12010 /* The dependent declarator is optional if we are parsing an
12011 abstract-declarator. */
12012 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12013 cp_parser_parse_tentatively (parser);
12015 /* Parse the dependent declarator. */
12016 declarator = cp_parser_declarator (parser, dcl_kind,
12017 /*ctor_dtor_or_conv_p=*/NULL,
12018 /*parenthesized_p=*/NULL,
12019 /*member_p=*/false);
12021 /* If we are parsing an abstract-declarator, we must handle the
12022 case where the dependent declarator is absent. */
12023 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12024 && !cp_parser_parse_definitely (parser))
12025 declarator = NULL;
12027 /* Build the representation of the ptr-operator. */
12028 if (class_type)
12029 declarator = make_ptrmem_declarator (cv_quals,
12030 class_type,
12031 declarator);
12032 else if (code == INDIRECT_REF)
12033 declarator = make_pointer_declarator (cv_quals, declarator);
12034 else
12035 declarator = make_reference_declarator (cv_quals, declarator);
12037 /* Everything else is a direct-declarator. */
12038 else
12040 if (parenthesized_p)
12041 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12042 CPP_OPEN_PAREN);
12043 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12044 ctor_dtor_or_conv_p,
12045 member_p);
12048 if (attributes && declarator && declarator != cp_error_declarator)
12049 declarator->attributes = attributes;
12051 return declarator;
12054 /* Parse a direct-declarator or direct-abstract-declarator.
12056 direct-declarator:
12057 declarator-id
12058 direct-declarator ( parameter-declaration-clause )
12059 cv-qualifier-seq [opt]
12060 exception-specification [opt]
12061 direct-declarator [ constant-expression [opt] ]
12062 ( declarator )
12064 direct-abstract-declarator:
12065 direct-abstract-declarator [opt]
12066 ( parameter-declaration-clause )
12067 cv-qualifier-seq [opt]
12068 exception-specification [opt]
12069 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12070 ( abstract-declarator )
12072 Returns a representation of the declarator. DCL_KIND is
12073 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12074 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12075 we are parsing a direct-declarator. It is
12076 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12077 of ambiguity we prefer an abstract declarator, as per
12078 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12079 cp_parser_declarator. */
12081 static cp_declarator *
12082 cp_parser_direct_declarator (cp_parser* parser,
12083 cp_parser_declarator_kind dcl_kind,
12084 int* ctor_dtor_or_conv_p,
12085 bool member_p)
12087 cp_token *token;
12088 cp_declarator *declarator = NULL;
12089 tree scope = NULL_TREE;
12090 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12091 bool saved_in_declarator_p = parser->in_declarator_p;
12092 bool first = true;
12093 tree pushed_scope = NULL_TREE;
12095 while (true)
12097 /* Peek at the next token. */
12098 token = cp_lexer_peek_token (parser->lexer);
12099 if (token->type == CPP_OPEN_PAREN)
12101 /* This is either a parameter-declaration-clause, or a
12102 parenthesized declarator. When we know we are parsing a
12103 named declarator, it must be a parenthesized declarator
12104 if FIRST is true. For instance, `(int)' is a
12105 parameter-declaration-clause, with an omitted
12106 direct-abstract-declarator. But `((*))', is a
12107 parenthesized abstract declarator. Finally, when T is a
12108 template parameter `(T)' is a
12109 parameter-declaration-clause, and not a parenthesized
12110 named declarator.
12112 We first try and parse a parameter-declaration-clause,
12113 and then try a nested declarator (if FIRST is true).
12115 It is not an error for it not to be a
12116 parameter-declaration-clause, even when FIRST is
12117 false. Consider,
12119 int i (int);
12120 int i (3);
12122 The first is the declaration of a function while the
12123 second is a the definition of a variable, including its
12124 initializer.
12126 Having seen only the parenthesis, we cannot know which of
12127 these two alternatives should be selected. Even more
12128 complex are examples like:
12130 int i (int (a));
12131 int i (int (3));
12133 The former is a function-declaration; the latter is a
12134 variable initialization.
12136 Thus again, we try a parameter-declaration-clause, and if
12137 that fails, we back out and return. */
12139 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12141 cp_parameter_declarator *params;
12142 unsigned saved_num_template_parameter_lists;
12144 /* In a member-declarator, the only valid interpretation
12145 of a parenthesis is the start of a
12146 parameter-declaration-clause. (It is invalid to
12147 initialize a static data member with a parenthesized
12148 initializer; only the "=" form of initialization is
12149 permitted.) */
12150 if (!member_p)
12151 cp_parser_parse_tentatively (parser);
12153 /* Consume the `('. */
12154 cp_lexer_consume_token (parser->lexer);
12155 if (first)
12157 /* If this is going to be an abstract declarator, we're
12158 in a declarator and we can't have default args. */
12159 parser->default_arg_ok_p = false;
12160 parser->in_declarator_p = true;
12163 /* Inside the function parameter list, surrounding
12164 template-parameter-lists do not apply. */
12165 saved_num_template_parameter_lists
12166 = parser->num_template_parameter_lists;
12167 parser->num_template_parameter_lists = 0;
12169 /* Parse the parameter-declaration-clause. */
12170 params = cp_parser_parameter_declaration_clause (parser);
12172 parser->num_template_parameter_lists
12173 = saved_num_template_parameter_lists;
12175 /* If all went well, parse the cv-qualifier-seq and the
12176 exception-specification. */
12177 if (member_p || cp_parser_parse_definitely (parser))
12179 cp_cv_quals cv_quals;
12180 tree exception_specification;
12182 if (ctor_dtor_or_conv_p)
12183 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12184 first = false;
12185 /* Consume the `)'. */
12186 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12188 /* Parse the cv-qualifier-seq. */
12189 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12190 /* And the exception-specification. */
12191 exception_specification
12192 = cp_parser_exception_specification_opt (parser);
12194 /* Create the function-declarator. */
12195 declarator = make_call_declarator (declarator,
12196 params,
12197 cv_quals,
12198 exception_specification);
12199 /* Any subsequent parameter lists are to do with
12200 return type, so are not those of the declared
12201 function. */
12202 parser->default_arg_ok_p = false;
12204 /* Repeat the main loop. */
12205 continue;
12209 /* If this is the first, we can try a parenthesized
12210 declarator. */
12211 if (first)
12213 bool saved_in_type_id_in_expr_p;
12215 parser->default_arg_ok_p = saved_default_arg_ok_p;
12216 parser->in_declarator_p = saved_in_declarator_p;
12218 /* Consume the `('. */
12219 cp_lexer_consume_token (parser->lexer);
12220 /* Parse the nested declarator. */
12221 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12222 parser->in_type_id_in_expr_p = true;
12223 declarator
12224 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12225 /*parenthesized_p=*/NULL,
12226 member_p);
12227 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12228 first = false;
12229 /* Expect a `)'. */
12230 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12231 declarator = cp_error_declarator;
12232 if (declarator == cp_error_declarator)
12233 break;
12235 goto handle_declarator;
12237 /* Otherwise, we must be done. */
12238 else
12239 break;
12241 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12242 && token->type == CPP_OPEN_SQUARE)
12244 /* Parse an array-declarator. */
12245 tree bounds;
12247 if (ctor_dtor_or_conv_p)
12248 *ctor_dtor_or_conv_p = 0;
12250 first = false;
12251 parser->default_arg_ok_p = false;
12252 parser->in_declarator_p = true;
12253 /* Consume the `['. */
12254 cp_lexer_consume_token (parser->lexer);
12255 /* Peek at the next token. */
12256 token = cp_lexer_peek_token (parser->lexer);
12257 /* If the next token is `]', then there is no
12258 constant-expression. */
12259 if (token->type != CPP_CLOSE_SQUARE)
12261 bool non_constant_p;
12263 bounds
12264 = cp_parser_constant_expression (parser,
12265 /*allow_non_constant=*/true,
12266 &non_constant_p);
12267 if (!non_constant_p)
12268 bounds = fold_non_dependent_expr (bounds);
12269 /* Normally, the array bound must be an integral constant
12270 expression. However, as an extension, we allow VLAs
12271 in function scopes. */
12272 else if (!parser->in_function_body)
12274 error ("array bound is not an integer constant");
12275 bounds = error_mark_node;
12278 else
12279 bounds = NULL_TREE;
12280 /* Look for the closing `]'. */
12281 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12283 declarator = cp_error_declarator;
12284 break;
12287 declarator = make_array_declarator (declarator, bounds);
12289 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12291 tree qualifying_scope;
12292 tree unqualified_name;
12293 special_function_kind sfk;
12294 bool abstract_ok;
12295 bool pack_expansion_p = false;
12297 /* Parse a declarator-id */
12298 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12299 if (abstract_ok)
12301 cp_parser_parse_tentatively (parser);
12303 /* If we see an ellipsis, we should be looking at a
12304 parameter pack. */
12305 if (token->type == CPP_ELLIPSIS)
12307 /* Consume the `...' */
12308 cp_lexer_consume_token (parser->lexer);
12310 pack_expansion_p = true;
12314 unqualified_name
12315 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12316 qualifying_scope = parser->scope;
12317 if (abstract_ok)
12319 bool okay = false;
12321 if (!unqualified_name && pack_expansion_p)
12323 /* Check whether an error occurred. */
12324 okay = !cp_parser_error_occurred (parser);
12326 /* We already consumed the ellipsis to mark a
12327 parameter pack, but we have no way to report it,
12328 so abort the tentative parse. We will be exiting
12329 immediately anyway. */
12330 cp_parser_abort_tentative_parse (parser);
12332 else
12333 okay = cp_parser_parse_definitely (parser);
12335 if (!okay)
12336 unqualified_name = error_mark_node;
12337 else if (unqualified_name
12338 && (qualifying_scope
12339 || (TREE_CODE (unqualified_name)
12340 != IDENTIFIER_NODE)))
12342 cp_parser_error (parser, "expected unqualified-id");
12343 unqualified_name = error_mark_node;
12347 if (!unqualified_name)
12348 return NULL;
12349 if (unqualified_name == error_mark_node)
12351 declarator = cp_error_declarator;
12352 pack_expansion_p = false;
12353 declarator->parameter_pack_p = false;
12354 break;
12357 if (qualifying_scope && at_namespace_scope_p ()
12358 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12360 /* In the declaration of a member of a template class
12361 outside of the class itself, the SCOPE will sometimes
12362 be a TYPENAME_TYPE. For example, given:
12364 template <typename T>
12365 int S<T>::R::i = 3;
12367 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12368 this context, we must resolve S<T>::R to an ordinary
12369 type, rather than a typename type.
12371 The reason we normally avoid resolving TYPENAME_TYPEs
12372 is that a specialization of `S' might render
12373 `S<T>::R' not a type. However, if `S' is
12374 specialized, then this `i' will not be used, so there
12375 is no harm in resolving the types here. */
12376 tree type;
12378 /* Resolve the TYPENAME_TYPE. */
12379 type = resolve_typename_type (qualifying_scope,
12380 /*only_current_p=*/false);
12381 /* If that failed, the declarator is invalid. */
12382 if (type == error_mark_node)
12383 error ("%<%T::%E%> is not a type",
12384 TYPE_CONTEXT (qualifying_scope),
12385 TYPE_IDENTIFIER (qualifying_scope));
12386 qualifying_scope = type;
12389 sfk = sfk_none;
12391 if (unqualified_name)
12393 tree class_type;
12395 if (qualifying_scope
12396 && CLASS_TYPE_P (qualifying_scope))
12397 class_type = qualifying_scope;
12398 else
12399 class_type = current_class_type;
12401 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12403 tree name_type = TREE_TYPE (unqualified_name);
12404 if (class_type && same_type_p (name_type, class_type))
12406 if (qualifying_scope
12407 && CLASSTYPE_USE_TEMPLATE (name_type))
12409 error ("invalid use of constructor as a template");
12410 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12411 "name the constructor in a qualified name",
12412 class_type,
12413 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12414 class_type, name_type);
12415 declarator = cp_error_declarator;
12416 break;
12418 else
12419 unqualified_name = constructor_name (class_type);
12421 else
12423 /* We do not attempt to print the declarator
12424 here because we do not have enough
12425 information about its original syntactic
12426 form. */
12427 cp_parser_error (parser, "invalid declarator");
12428 declarator = cp_error_declarator;
12429 break;
12433 if (class_type)
12435 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12436 sfk = sfk_destructor;
12437 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12438 sfk = sfk_conversion;
12439 else if (/* There's no way to declare a constructor
12440 for an anonymous type, even if the type
12441 got a name for linkage purposes. */
12442 !TYPE_WAS_ANONYMOUS (class_type)
12443 && constructor_name_p (unqualified_name,
12444 class_type))
12446 unqualified_name = constructor_name (class_type);
12447 sfk = sfk_constructor;
12450 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12451 *ctor_dtor_or_conv_p = -1;
12454 declarator = make_id_declarator (qualifying_scope,
12455 unqualified_name,
12456 sfk);
12457 declarator->id_loc = token->location;
12458 declarator->parameter_pack_p = pack_expansion_p;
12460 if (pack_expansion_p)
12461 maybe_warn_variadic_templates ();
12463 handle_declarator:;
12464 scope = get_scope_of_declarator (declarator);
12465 if (scope)
12466 /* Any names that appear after the declarator-id for a
12467 member are looked up in the containing scope. */
12468 pushed_scope = push_scope (scope);
12469 parser->in_declarator_p = true;
12470 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12471 || (declarator && declarator->kind == cdk_id))
12472 /* Default args are only allowed on function
12473 declarations. */
12474 parser->default_arg_ok_p = saved_default_arg_ok_p;
12475 else
12476 parser->default_arg_ok_p = false;
12478 first = false;
12480 /* We're done. */
12481 else
12482 break;
12485 /* For an abstract declarator, we might wind up with nothing at this
12486 point. That's an error; the declarator is not optional. */
12487 if (!declarator)
12488 cp_parser_error (parser, "expected declarator");
12490 /* If we entered a scope, we must exit it now. */
12491 if (pushed_scope)
12492 pop_scope (pushed_scope);
12494 parser->default_arg_ok_p = saved_default_arg_ok_p;
12495 parser->in_declarator_p = saved_in_declarator_p;
12497 return declarator;
12500 /* Parse a ptr-operator.
12502 ptr-operator:
12503 * cv-qualifier-seq [opt]
12505 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12507 GNU Extension:
12509 ptr-operator:
12510 & cv-qualifier-seq [opt]
12512 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12513 Returns ADDR_EXPR if a reference was used. In the case of a
12514 pointer-to-member, *TYPE is filled in with the TYPE containing the
12515 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
12516 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
12517 ERROR_MARK if an error occurred. */
12519 static enum tree_code
12520 cp_parser_ptr_operator (cp_parser* parser,
12521 tree* type,
12522 cp_cv_quals *cv_quals)
12524 enum tree_code code = ERROR_MARK;
12525 cp_token *token;
12527 /* Assume that it's not a pointer-to-member. */
12528 *type = NULL_TREE;
12529 /* And that there are no cv-qualifiers. */
12530 *cv_quals = TYPE_UNQUALIFIED;
12532 /* Peek at the next token. */
12533 token = cp_lexer_peek_token (parser->lexer);
12534 /* If it's a `*' or `&' we have a pointer or reference. */
12535 if (token->type == CPP_MULT || token->type == CPP_AND)
12537 /* Remember which ptr-operator we were processing. */
12538 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12540 /* Consume the `*' or `&'. */
12541 cp_lexer_consume_token (parser->lexer);
12543 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12544 `&', if we are allowing GNU extensions. (The only qualifier
12545 that can legally appear after `&' is `restrict', but that is
12546 enforced during semantic analysis. */
12547 if (code == INDIRECT_REF
12548 || cp_parser_allow_gnu_extensions_p (parser))
12549 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12551 else
12553 /* Try the pointer-to-member case. */
12554 cp_parser_parse_tentatively (parser);
12555 /* Look for the optional `::' operator. */
12556 cp_parser_global_scope_opt (parser,
12557 /*current_scope_valid_p=*/false);
12558 /* Look for the nested-name specifier. */
12559 cp_parser_nested_name_specifier (parser,
12560 /*typename_keyword_p=*/false,
12561 /*check_dependency_p=*/true,
12562 /*type_p=*/false,
12563 /*is_declaration=*/false);
12564 /* If we found it, and the next token is a `*', then we are
12565 indeed looking at a pointer-to-member operator. */
12566 if (!cp_parser_error_occurred (parser)
12567 && cp_parser_require (parser, CPP_MULT, "`*'"))
12569 /* Indicate that the `*' operator was used. */
12570 code = INDIRECT_REF;
12572 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12573 error ("%qD is a namespace", parser->scope);
12574 else
12576 /* The type of which the member is a member is given by the
12577 current SCOPE. */
12578 *type = parser->scope;
12579 /* The next name will not be qualified. */
12580 parser->scope = NULL_TREE;
12581 parser->qualifying_scope = NULL_TREE;
12582 parser->object_scope = NULL_TREE;
12583 /* Look for the optional cv-qualifier-seq. */
12584 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12587 /* If that didn't work we don't have a ptr-operator. */
12588 if (!cp_parser_parse_definitely (parser))
12589 cp_parser_error (parser, "expected ptr-operator");
12592 return code;
12595 /* Parse an (optional) cv-qualifier-seq.
12597 cv-qualifier-seq:
12598 cv-qualifier cv-qualifier-seq [opt]
12600 cv-qualifier:
12601 const
12602 volatile
12604 GNU Extension:
12606 cv-qualifier:
12607 __restrict__
12609 Returns a bitmask representing the cv-qualifiers. */
12611 static cp_cv_quals
12612 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12614 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12616 while (true)
12618 cp_token *token;
12619 cp_cv_quals cv_qualifier;
12621 /* Peek at the next token. */
12622 token = cp_lexer_peek_token (parser->lexer);
12623 /* See if it's a cv-qualifier. */
12624 switch (token->keyword)
12626 case RID_CONST:
12627 cv_qualifier = TYPE_QUAL_CONST;
12628 break;
12630 case RID_VOLATILE:
12631 cv_qualifier = TYPE_QUAL_VOLATILE;
12632 break;
12634 case RID_RESTRICT:
12635 cv_qualifier = TYPE_QUAL_RESTRICT;
12636 break;
12638 default:
12639 cv_qualifier = TYPE_UNQUALIFIED;
12640 break;
12643 if (!cv_qualifier)
12644 break;
12646 if (cv_quals & cv_qualifier)
12648 error ("duplicate cv-qualifier");
12649 cp_lexer_purge_token (parser->lexer);
12651 else
12653 cp_lexer_consume_token (parser->lexer);
12654 cv_quals |= cv_qualifier;
12658 return cv_quals;
12661 /* Parse a declarator-id.
12663 declarator-id:
12664 id-expression
12665 :: [opt] nested-name-specifier [opt] type-name
12667 In the `id-expression' case, the value returned is as for
12668 cp_parser_id_expression if the id-expression was an unqualified-id.
12669 If the id-expression was a qualified-id, then a SCOPE_REF is
12670 returned. The first operand is the scope (either a NAMESPACE_DECL
12671 or TREE_TYPE), but the second is still just a representation of an
12672 unqualified-id. */
12674 static tree
12675 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12677 tree id;
12678 /* The expression must be an id-expression. Assume that qualified
12679 names are the names of types so that:
12681 template <class T>
12682 int S<T>::R::i = 3;
12684 will work; we must treat `S<T>::R' as the name of a type.
12685 Similarly, assume that qualified names are templates, where
12686 required, so that:
12688 template <class T>
12689 int S<T>::R<T>::i = 3;
12691 will work, too. */
12692 id = cp_parser_id_expression (parser,
12693 /*template_keyword_p=*/false,
12694 /*check_dependency_p=*/false,
12695 /*template_p=*/NULL,
12696 /*declarator_p=*/true,
12697 optional_p);
12698 if (id && BASELINK_P (id))
12699 id = BASELINK_FUNCTIONS (id);
12700 return id;
12703 /* Parse a type-id.
12705 type-id:
12706 type-specifier-seq abstract-declarator [opt]
12708 Returns the TYPE specified. */
12710 static tree
12711 cp_parser_type_id (cp_parser* parser)
12713 cp_decl_specifier_seq type_specifier_seq;
12714 cp_declarator *abstract_declarator;
12716 /* Parse the type-specifier-seq. */
12717 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12718 &type_specifier_seq);
12719 if (type_specifier_seq.type == error_mark_node)
12720 return error_mark_node;
12722 /* There might or might not be an abstract declarator. */
12723 cp_parser_parse_tentatively (parser);
12724 /* Look for the declarator. */
12725 abstract_declarator
12726 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12727 /*parenthesized_p=*/NULL,
12728 /*member_p=*/false);
12729 /* Check to see if there really was a declarator. */
12730 if (!cp_parser_parse_definitely (parser))
12731 abstract_declarator = NULL;
12733 return groktypename (&type_specifier_seq, abstract_declarator);
12736 /* Parse a type-specifier-seq.
12738 type-specifier-seq:
12739 type-specifier type-specifier-seq [opt]
12741 GNU extension:
12743 type-specifier-seq:
12744 attributes type-specifier-seq [opt]
12746 If IS_CONDITION is true, we are at the start of a "condition",
12747 e.g., we've just seen "if (".
12749 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12751 static void
12752 cp_parser_type_specifier_seq (cp_parser* parser,
12753 bool is_condition,
12754 cp_decl_specifier_seq *type_specifier_seq)
12756 bool seen_type_specifier = false;
12757 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12759 /* Clear the TYPE_SPECIFIER_SEQ. */
12760 clear_decl_specs (type_specifier_seq);
12762 /* Parse the type-specifiers and attributes. */
12763 while (true)
12765 tree type_specifier;
12766 bool is_cv_qualifier;
12768 /* Check for attributes first. */
12769 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12771 type_specifier_seq->attributes =
12772 chainon (type_specifier_seq->attributes,
12773 cp_parser_attributes_opt (parser));
12774 continue;
12777 /* Look for the type-specifier. */
12778 type_specifier = cp_parser_type_specifier (parser,
12779 flags,
12780 type_specifier_seq,
12781 /*is_declaration=*/false,
12782 NULL,
12783 &is_cv_qualifier);
12784 if (!type_specifier)
12786 /* If the first type-specifier could not be found, this is not a
12787 type-specifier-seq at all. */
12788 if (!seen_type_specifier)
12790 cp_parser_error (parser, "expected type-specifier");
12791 type_specifier_seq->type = error_mark_node;
12792 return;
12794 /* If subsequent type-specifiers could not be found, the
12795 type-specifier-seq is complete. */
12796 break;
12799 seen_type_specifier = true;
12800 /* The standard says that a condition can be:
12802 type-specifier-seq declarator = assignment-expression
12804 However, given:
12806 struct S {};
12807 if (int S = ...)
12809 we should treat the "S" as a declarator, not as a
12810 type-specifier. The standard doesn't say that explicitly for
12811 type-specifier-seq, but it does say that for
12812 decl-specifier-seq in an ordinary declaration. Perhaps it
12813 would be clearer just to allow a decl-specifier-seq here, and
12814 then add a semantic restriction that if any decl-specifiers
12815 that are not type-specifiers appear, the program is invalid. */
12816 if (is_condition && !is_cv_qualifier)
12817 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12820 cp_parser_check_decl_spec (type_specifier_seq);
12823 /* Parse a parameter-declaration-clause.
12825 parameter-declaration-clause:
12826 parameter-declaration-list [opt] ... [opt]
12827 parameter-declaration-list , ...
12829 Returns a representation for the parameter declarations. A return
12830 value of NULL indicates a parameter-declaration-clause consisting
12831 only of an ellipsis. */
12833 static cp_parameter_declarator *
12834 cp_parser_parameter_declaration_clause (cp_parser* parser)
12836 cp_parameter_declarator *parameters;
12837 cp_token *token;
12838 bool ellipsis_p;
12839 bool is_error;
12841 /* Peek at the next token. */
12842 token = cp_lexer_peek_token (parser->lexer);
12843 /* Check for trivial parameter-declaration-clauses. */
12844 if (token->type == CPP_ELLIPSIS)
12846 /* Consume the `...' token. */
12847 cp_lexer_consume_token (parser->lexer);
12848 return NULL;
12850 else if (token->type == CPP_CLOSE_PAREN)
12851 /* There are no parameters. */
12853 #ifndef NO_IMPLICIT_EXTERN_C
12854 if (in_system_header && current_class_type == NULL
12855 && current_lang_name == lang_name_c)
12856 return NULL;
12857 else
12858 #endif
12859 return no_parameters;
12861 /* Check for `(void)', too, which is a special case. */
12862 else if (token->keyword == RID_VOID
12863 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12864 == CPP_CLOSE_PAREN))
12866 /* Consume the `void' token. */
12867 cp_lexer_consume_token (parser->lexer);
12868 /* There are no parameters. */
12869 return no_parameters;
12872 /* Parse the parameter-declaration-list. */
12873 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12874 /* If a parse error occurred while parsing the
12875 parameter-declaration-list, then the entire
12876 parameter-declaration-clause is erroneous. */
12877 if (is_error)
12878 return NULL;
12880 /* Peek at the next token. */
12881 token = cp_lexer_peek_token (parser->lexer);
12882 /* If it's a `,', the clause should terminate with an ellipsis. */
12883 if (token->type == CPP_COMMA)
12885 /* Consume the `,'. */
12886 cp_lexer_consume_token (parser->lexer);
12887 /* Expect an ellipsis. */
12888 ellipsis_p
12889 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12891 /* It might also be `...' if the optional trailing `,' was
12892 omitted. */
12893 else if (token->type == CPP_ELLIPSIS)
12895 /* Consume the `...' token. */
12896 cp_lexer_consume_token (parser->lexer);
12897 /* And remember that we saw it. */
12898 ellipsis_p = true;
12900 else
12901 ellipsis_p = false;
12903 /* Finish the parameter list. */
12904 if (parameters && ellipsis_p)
12905 parameters->ellipsis_p = true;
12907 return parameters;
12910 /* Parse a parameter-declaration-list.
12912 parameter-declaration-list:
12913 parameter-declaration
12914 parameter-declaration-list , parameter-declaration
12916 Returns a representation of the parameter-declaration-list, as for
12917 cp_parser_parameter_declaration_clause. However, the
12918 `void_list_node' is never appended to the list. Upon return,
12919 *IS_ERROR will be true iff an error occurred. */
12921 static cp_parameter_declarator *
12922 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12924 cp_parameter_declarator *parameters = NULL;
12925 cp_parameter_declarator **tail = &parameters;
12926 bool saved_in_unbraced_linkage_specification_p;
12928 /* Assume all will go well. */
12929 *is_error = false;
12930 /* The special considerations that apply to a function within an
12931 unbraced linkage specifications do not apply to the parameters
12932 to the function. */
12933 saved_in_unbraced_linkage_specification_p
12934 = parser->in_unbraced_linkage_specification_p;
12935 parser->in_unbraced_linkage_specification_p = false;
12937 /* Look for more parameters. */
12938 while (true)
12940 cp_parameter_declarator *parameter;
12941 bool parenthesized_p;
12942 /* Parse the parameter. */
12943 parameter
12944 = cp_parser_parameter_declaration (parser,
12945 /*template_parm_p=*/false,
12946 &parenthesized_p);
12948 /* If a parse error occurred parsing the parameter declaration,
12949 then the entire parameter-declaration-list is erroneous. */
12950 if (!parameter)
12952 *is_error = true;
12953 parameters = NULL;
12954 break;
12956 /* Add the new parameter to the list. */
12957 *tail = parameter;
12958 tail = &parameter->next;
12960 /* Peek at the next token. */
12961 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12962 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12963 /* These are for Objective-C++ */
12964 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12965 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12966 /* The parameter-declaration-list is complete. */
12967 break;
12968 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12970 cp_token *token;
12972 /* Peek at the next token. */
12973 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12974 /* If it's an ellipsis, then the list is complete. */
12975 if (token->type == CPP_ELLIPSIS)
12976 break;
12977 /* Otherwise, there must be more parameters. Consume the
12978 `,'. */
12979 cp_lexer_consume_token (parser->lexer);
12980 /* When parsing something like:
12982 int i(float f, double d)
12984 we can tell after seeing the declaration for "f" that we
12985 are not looking at an initialization of a variable "i",
12986 but rather at the declaration of a function "i".
12988 Due to the fact that the parsing of template arguments
12989 (as specified to a template-id) requires backtracking we
12990 cannot use this technique when inside a template argument
12991 list. */
12992 if (!parser->in_template_argument_list_p
12993 && !parser->in_type_id_in_expr_p
12994 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12995 /* However, a parameter-declaration of the form
12996 "foat(f)" (which is a valid declaration of a
12997 parameter "f") can also be interpreted as an
12998 expression (the conversion of "f" to "float"). */
12999 && !parenthesized_p)
13000 cp_parser_commit_to_tentative_parse (parser);
13002 else
13004 cp_parser_error (parser, "expected %<,%> or %<...%>");
13005 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13006 cp_parser_skip_to_closing_parenthesis (parser,
13007 /*recovering=*/true,
13008 /*or_comma=*/false,
13009 /*consume_paren=*/false);
13010 break;
13014 parser->in_unbraced_linkage_specification_p
13015 = saved_in_unbraced_linkage_specification_p;
13017 return parameters;
13020 /* Parse a parameter declaration.
13022 parameter-declaration:
13023 decl-specifier-seq ... [opt] declarator
13024 decl-specifier-seq declarator = assignment-expression
13025 decl-specifier-seq ... [opt] abstract-declarator [opt]
13026 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13028 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13029 declares a template parameter. (In that case, a non-nested `>'
13030 token encountered during the parsing of the assignment-expression
13031 is not interpreted as a greater-than operator.)
13033 Returns a representation of the parameter, or NULL if an error
13034 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13035 true iff the declarator is of the form "(p)". */
13037 static cp_parameter_declarator *
13038 cp_parser_parameter_declaration (cp_parser *parser,
13039 bool template_parm_p,
13040 bool *parenthesized_p)
13042 int declares_class_or_enum;
13043 bool greater_than_is_operator_p;
13044 cp_decl_specifier_seq decl_specifiers;
13045 cp_declarator *declarator;
13046 tree default_argument;
13047 cp_token *token;
13048 const char *saved_message;
13050 /* In a template parameter, `>' is not an operator.
13052 [temp.param]
13054 When parsing a default template-argument for a non-type
13055 template-parameter, the first non-nested `>' is taken as the end
13056 of the template parameter-list rather than a greater-than
13057 operator. */
13058 greater_than_is_operator_p = !template_parm_p;
13060 /* Type definitions may not appear in parameter types. */
13061 saved_message = parser->type_definition_forbidden_message;
13062 parser->type_definition_forbidden_message
13063 = "types may not be defined in parameter types";
13065 /* Parse the declaration-specifiers. */
13066 cp_parser_decl_specifier_seq (parser,
13067 CP_PARSER_FLAGS_NONE,
13068 &decl_specifiers,
13069 &declares_class_or_enum);
13070 /* If an error occurred, there's no reason to attempt to parse the
13071 rest of the declaration. */
13072 if (cp_parser_error_occurred (parser))
13074 parser->type_definition_forbidden_message = saved_message;
13075 return NULL;
13078 /* Peek at the next token. */
13079 token = cp_lexer_peek_token (parser->lexer);
13081 /* If the next token is a `)', `,', `=', `>', or `...', then there
13082 is no declarator. However, when variadic templates are enabled,
13083 there may be a declarator following `...'. */
13084 if (token->type == CPP_CLOSE_PAREN
13085 || token->type == CPP_COMMA
13086 || token->type == CPP_EQ
13087 || token->type == CPP_GREATER)
13089 declarator = NULL;
13090 if (parenthesized_p)
13091 *parenthesized_p = false;
13093 /* Otherwise, there should be a declarator. */
13094 else
13096 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13097 parser->default_arg_ok_p = false;
13099 /* After seeing a decl-specifier-seq, if the next token is not a
13100 "(", there is no possibility that the code is a valid
13101 expression. Therefore, if parsing tentatively, we commit at
13102 this point. */
13103 if (!parser->in_template_argument_list_p
13104 /* In an expression context, having seen:
13106 (int((char ...
13108 we cannot be sure whether we are looking at a
13109 function-type (taking a "char" as a parameter) or a cast
13110 of some object of type "char" to "int". */
13111 && !parser->in_type_id_in_expr_p
13112 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13113 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13114 cp_parser_commit_to_tentative_parse (parser);
13115 /* Parse the declarator. */
13116 declarator = cp_parser_declarator (parser,
13117 CP_PARSER_DECLARATOR_EITHER,
13118 /*ctor_dtor_or_conv_p=*/NULL,
13119 parenthesized_p,
13120 /*member_p=*/false);
13121 parser->default_arg_ok_p = saved_default_arg_ok_p;
13122 /* After the declarator, allow more attributes. */
13123 decl_specifiers.attributes
13124 = chainon (decl_specifiers.attributes,
13125 cp_parser_attributes_opt (parser));
13128 /* If the next token is an ellipsis, and we have not seen a
13129 declarator name, and the type of the declarator contains parameter
13130 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13131 a parameter pack expansion expression. Otherwise, leave the
13132 ellipsis for a C-style variadic function. */
13133 token = cp_lexer_peek_token (parser->lexer);
13134 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13136 tree type = decl_specifiers.type;
13138 if (type && DECL_P (type))
13139 type = TREE_TYPE (type);
13141 if (type
13142 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13143 && declarator_can_be_parameter_pack (declarator)
13144 && (!declarator || !declarator->parameter_pack_p)
13145 && uses_parameter_packs (type))
13147 /* Consume the `...'. */
13148 cp_lexer_consume_token (parser->lexer);
13149 maybe_warn_variadic_templates ();
13151 /* Build a pack expansion type */
13152 if (declarator)
13153 declarator->parameter_pack_p = true;
13154 else
13155 decl_specifiers.type = make_pack_expansion (type);
13159 /* The restriction on defining new types applies only to the type
13160 of the parameter, not to the default argument. */
13161 parser->type_definition_forbidden_message = saved_message;
13163 /* If the next token is `=', then process a default argument. */
13164 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13166 bool saved_greater_than_is_operator_p;
13167 /* Consume the `='. */
13168 cp_lexer_consume_token (parser->lexer);
13170 /* If we are defining a class, then the tokens that make up the
13171 default argument must be saved and processed later. */
13172 if (!template_parm_p && at_class_scope_p ()
13173 && TYPE_BEING_DEFINED (current_class_type))
13175 unsigned depth = 0;
13176 cp_token *first_token;
13177 cp_token *token;
13179 /* Add tokens until we have processed the entire default
13180 argument. We add the range [first_token, token). */
13181 first_token = cp_lexer_peek_token (parser->lexer);
13182 while (true)
13184 bool done = false;
13186 /* Peek at the next token. */
13187 token = cp_lexer_peek_token (parser->lexer);
13188 /* What we do depends on what token we have. */
13189 switch (token->type)
13191 /* In valid code, a default argument must be
13192 immediately followed by a `,' `)', or `...'. */
13193 case CPP_COMMA:
13194 case CPP_CLOSE_PAREN:
13195 case CPP_ELLIPSIS:
13196 /* If we run into a non-nested `;', `}', or `]',
13197 then the code is invalid -- but the default
13198 argument is certainly over. */
13199 case CPP_SEMICOLON:
13200 case CPP_CLOSE_BRACE:
13201 case CPP_CLOSE_SQUARE:
13202 if (depth == 0)
13203 done = true;
13204 /* Update DEPTH, if necessary. */
13205 else if (token->type == CPP_CLOSE_PAREN
13206 || token->type == CPP_CLOSE_BRACE
13207 || token->type == CPP_CLOSE_SQUARE)
13208 --depth;
13209 break;
13211 case CPP_OPEN_PAREN:
13212 case CPP_OPEN_SQUARE:
13213 case CPP_OPEN_BRACE:
13214 ++depth;
13215 break;
13217 case CPP_RSHIFT:
13218 if (!flag_cpp0x)
13219 break;
13220 /* Fall through for C++0x, which treats the `>>'
13221 operator like two `>' tokens in certain
13222 cases. */
13224 case CPP_GREATER:
13225 /* If we see a non-nested `>', and `>' is not an
13226 operator, then it marks the end of the default
13227 argument. */
13228 if (!depth && !greater_than_is_operator_p)
13229 done = true;
13230 break;
13232 /* If we run out of tokens, issue an error message. */
13233 case CPP_EOF:
13234 case CPP_PRAGMA_EOL:
13235 error ("file ends in default argument");
13236 done = true;
13237 break;
13239 case CPP_NAME:
13240 case CPP_SCOPE:
13241 /* In these cases, we should look for template-ids.
13242 For example, if the default argument is
13243 `X<int, double>()', we need to do name lookup to
13244 figure out whether or not `X' is a template; if
13245 so, the `,' does not end the default argument.
13247 That is not yet done. */
13248 break;
13250 default:
13251 break;
13254 /* If we've reached the end, stop. */
13255 if (done)
13256 break;
13258 /* Add the token to the token block. */
13259 token = cp_lexer_consume_token (parser->lexer);
13262 /* Create a DEFAULT_ARG to represented the unparsed default
13263 argument. */
13264 default_argument = make_node (DEFAULT_ARG);
13265 DEFARG_TOKENS (default_argument)
13266 = cp_token_cache_new (first_token, token);
13267 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13269 /* Outside of a class definition, we can just parse the
13270 assignment-expression. */
13271 else
13273 bool saved_local_variables_forbidden_p;
13275 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13276 set correctly. */
13277 saved_greater_than_is_operator_p
13278 = parser->greater_than_is_operator_p;
13279 parser->greater_than_is_operator_p = greater_than_is_operator_p;
13280 /* Local variable names (and the `this' keyword) may not
13281 appear in a default argument. */
13282 saved_local_variables_forbidden_p
13283 = parser->local_variables_forbidden_p;
13284 parser->local_variables_forbidden_p = true;
13285 /* The default argument expression may cause implicitly
13286 defined member functions to be synthesized, which will
13287 result in garbage collection. We must treat this
13288 situation as if we were within the body of function so as
13289 to avoid collecting live data on the stack. */
13290 ++function_depth;
13291 /* Parse the assignment-expression. */
13292 if (template_parm_p)
13293 push_deferring_access_checks (dk_no_deferred);
13294 default_argument
13295 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13296 if (template_parm_p)
13297 pop_deferring_access_checks ();
13298 /* Restore saved state. */
13299 --function_depth;
13300 parser->greater_than_is_operator_p
13301 = saved_greater_than_is_operator_p;
13302 parser->local_variables_forbidden_p
13303 = saved_local_variables_forbidden_p;
13305 if (!parser->default_arg_ok_p)
13307 if (!flag_pedantic_errors)
13308 warning (0, "deprecated use of default argument for parameter of non-function");
13309 else
13311 error ("default arguments are only permitted for function parameters");
13312 default_argument = NULL_TREE;
13316 else
13317 default_argument = NULL_TREE;
13319 return make_parameter_declarator (&decl_specifiers,
13320 declarator,
13321 default_argument);
13324 /* Parse a function-body.
13326 function-body:
13327 compound_statement */
13329 static void
13330 cp_parser_function_body (cp_parser *parser)
13332 cp_parser_compound_statement (parser, NULL, false);
13335 /* Parse a ctor-initializer-opt followed by a function-body. Return
13336 true if a ctor-initializer was present. */
13338 static bool
13339 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13341 tree body;
13342 bool ctor_initializer_p;
13344 /* Begin the function body. */
13345 body = begin_function_body ();
13346 /* Parse the optional ctor-initializer. */
13347 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13348 /* Parse the function-body. */
13349 cp_parser_function_body (parser);
13350 /* Finish the function body. */
13351 finish_function_body (body);
13353 return ctor_initializer_p;
13356 /* Parse an initializer.
13358 initializer:
13359 = initializer-clause
13360 ( expression-list )
13362 Returns an expression representing the initializer. If no
13363 initializer is present, NULL_TREE is returned.
13365 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13366 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13367 set to FALSE if there is no initializer present. If there is an
13368 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13369 is set to true; otherwise it is set to false. */
13371 static tree
13372 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13373 bool* non_constant_p)
13375 cp_token *token;
13376 tree init;
13378 /* Peek at the next token. */
13379 token = cp_lexer_peek_token (parser->lexer);
13381 /* Let our caller know whether or not this initializer was
13382 parenthesized. */
13383 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13384 /* Assume that the initializer is constant. */
13385 *non_constant_p = false;
13387 if (token->type == CPP_EQ)
13389 /* Consume the `='. */
13390 cp_lexer_consume_token (parser->lexer);
13391 /* Parse the initializer-clause. */
13392 init = cp_parser_initializer_clause (parser, non_constant_p);
13394 else if (token->type == CPP_OPEN_PAREN)
13395 init = cp_parser_parenthesized_expression_list (parser, false,
13396 /*cast_p=*/false,
13397 /*allow_expansion_p=*/true,
13398 non_constant_p);
13399 else
13401 /* Anything else is an error. */
13402 cp_parser_error (parser, "expected initializer");
13403 init = error_mark_node;
13406 return init;
13409 /* Parse an initializer-clause.
13411 initializer-clause:
13412 assignment-expression
13413 { initializer-list , [opt] }
13416 Returns an expression representing the initializer.
13418 If the `assignment-expression' production is used the value
13419 returned is simply a representation for the expression.
13421 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13422 the elements of the initializer-list (or NULL, if the last
13423 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13424 NULL_TREE. There is no way to detect whether or not the optional
13425 trailing `,' was provided. NON_CONSTANT_P is as for
13426 cp_parser_initializer. */
13428 static tree
13429 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13431 tree initializer;
13433 /* Assume the expression is constant. */
13434 *non_constant_p = false;
13436 /* If it is not a `{', then we are looking at an
13437 assignment-expression. */
13438 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13440 initializer
13441 = cp_parser_constant_expression (parser,
13442 /*allow_non_constant_p=*/true,
13443 non_constant_p);
13444 if (!*non_constant_p)
13445 initializer = fold_non_dependent_expr (initializer);
13447 else
13449 /* Consume the `{' token. */
13450 cp_lexer_consume_token (parser->lexer);
13451 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13452 initializer = make_node (CONSTRUCTOR);
13453 /* If it's not a `}', then there is a non-trivial initializer. */
13454 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13456 /* Parse the initializer list. */
13457 CONSTRUCTOR_ELTS (initializer)
13458 = cp_parser_initializer_list (parser, non_constant_p);
13459 /* A trailing `,' token is allowed. */
13460 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13461 cp_lexer_consume_token (parser->lexer);
13463 /* Now, there should be a trailing `}'. */
13464 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13467 return initializer;
13470 /* Parse an initializer-list.
13472 initializer-list:
13473 initializer-clause ... [opt]
13474 initializer-list , initializer-clause ... [opt]
13476 GNU Extension:
13478 initializer-list:
13479 identifier : initializer-clause
13480 initializer-list, identifier : initializer-clause
13482 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13483 for the initializer. If the INDEX of the elt is non-NULL, it is the
13484 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13485 as for cp_parser_initializer. */
13487 static VEC(constructor_elt,gc) *
13488 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13490 VEC(constructor_elt,gc) *v = NULL;
13492 /* Assume all of the expressions are constant. */
13493 *non_constant_p = false;
13495 /* Parse the rest of the list. */
13496 while (true)
13498 cp_token *token;
13499 tree identifier;
13500 tree initializer;
13501 bool clause_non_constant_p;
13503 /* If the next token is an identifier and the following one is a
13504 colon, we are looking at the GNU designated-initializer
13505 syntax. */
13506 if (cp_parser_allow_gnu_extensions_p (parser)
13507 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13508 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13510 /* Warn the user that they are using an extension. */
13511 if (pedantic)
13512 pedwarn ("ISO C++ does not allow designated initializers");
13513 /* Consume the identifier. */
13514 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13515 /* Consume the `:'. */
13516 cp_lexer_consume_token (parser->lexer);
13518 else
13519 identifier = NULL_TREE;
13521 /* Parse the initializer. */
13522 initializer = cp_parser_initializer_clause (parser,
13523 &clause_non_constant_p);
13524 /* If any clause is non-constant, so is the entire initializer. */
13525 if (clause_non_constant_p)
13526 *non_constant_p = true;
13528 /* If we have an ellipsis, this is an initializer pack
13529 expansion. */
13530 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13532 /* Consume the `...'. */
13533 cp_lexer_consume_token (parser->lexer);
13535 /* Turn the initializer into an initializer expansion. */
13536 initializer = make_pack_expansion (initializer);
13539 /* Add it to the vector. */
13540 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13542 /* If the next token is not a comma, we have reached the end of
13543 the list. */
13544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13545 break;
13547 /* Peek at the next token. */
13548 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13549 /* If the next token is a `}', then we're still done. An
13550 initializer-clause can have a trailing `,' after the
13551 initializer-list and before the closing `}'. */
13552 if (token->type == CPP_CLOSE_BRACE)
13553 break;
13555 /* Consume the `,' token. */
13556 cp_lexer_consume_token (parser->lexer);
13559 return v;
13562 /* Classes [gram.class] */
13564 /* Parse a class-name.
13566 class-name:
13567 identifier
13568 template-id
13570 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13571 to indicate that names looked up in dependent types should be
13572 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13573 keyword has been used to indicate that the name that appears next
13574 is a template. TAG_TYPE indicates the explicit tag given before
13575 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13576 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13577 is the class being defined in a class-head.
13579 Returns the TYPE_DECL representing the class. */
13581 static tree
13582 cp_parser_class_name (cp_parser *parser,
13583 bool typename_keyword_p,
13584 bool template_keyword_p,
13585 enum tag_types tag_type,
13586 bool check_dependency_p,
13587 bool class_head_p,
13588 bool is_declaration)
13590 tree decl;
13591 tree scope;
13592 bool typename_p;
13593 cp_token *token;
13595 /* All class-names start with an identifier. */
13596 token = cp_lexer_peek_token (parser->lexer);
13597 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13599 cp_parser_error (parser, "expected class-name");
13600 return error_mark_node;
13603 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13604 to a template-id, so we save it here. */
13605 scope = parser->scope;
13606 if (scope == error_mark_node)
13607 return error_mark_node;
13609 /* Any name names a type if we're following the `typename' keyword
13610 in a qualified name where the enclosing scope is type-dependent. */
13611 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13612 && dependent_type_p (scope));
13613 /* Handle the common case (an identifier, but not a template-id)
13614 efficiently. */
13615 if (token->type == CPP_NAME
13616 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13618 cp_token *identifier_token;
13619 tree identifier;
13620 bool ambiguous_p;
13622 /* Look for the identifier. */
13623 identifier_token = cp_lexer_peek_token (parser->lexer);
13624 ambiguous_p = identifier_token->ambiguous_p;
13625 identifier = cp_parser_identifier (parser);
13626 /* If the next token isn't an identifier, we are certainly not
13627 looking at a class-name. */
13628 if (identifier == error_mark_node)
13629 decl = error_mark_node;
13630 /* If we know this is a type-name, there's no need to look it
13631 up. */
13632 else if (typename_p)
13633 decl = identifier;
13634 else
13636 tree ambiguous_decls;
13637 /* If we already know that this lookup is ambiguous, then
13638 we've already issued an error message; there's no reason
13639 to check again. */
13640 if (ambiguous_p)
13642 cp_parser_simulate_error (parser);
13643 return error_mark_node;
13645 /* If the next token is a `::', then the name must be a type
13646 name.
13648 [basic.lookup.qual]
13650 During the lookup for a name preceding the :: scope
13651 resolution operator, object, function, and enumerator
13652 names are ignored. */
13653 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13654 tag_type = typename_type;
13655 /* Look up the name. */
13656 decl = cp_parser_lookup_name (parser, identifier,
13657 tag_type,
13658 /*is_template=*/false,
13659 /*is_namespace=*/false,
13660 check_dependency_p,
13661 &ambiguous_decls);
13662 if (ambiguous_decls)
13664 error ("reference to %qD is ambiguous", identifier);
13665 print_candidates (ambiguous_decls);
13666 if (cp_parser_parsing_tentatively (parser))
13668 identifier_token->ambiguous_p = true;
13669 cp_parser_simulate_error (parser);
13671 return error_mark_node;
13675 else
13677 /* Try a template-id. */
13678 decl = cp_parser_template_id (parser, template_keyword_p,
13679 check_dependency_p,
13680 is_declaration);
13681 if (decl == error_mark_node)
13682 return error_mark_node;
13685 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13687 /* If this is a typename, create a TYPENAME_TYPE. */
13688 if (typename_p && decl != error_mark_node)
13690 decl = make_typename_type (scope, decl, typename_type,
13691 /*complain=*/tf_error);
13692 if (decl != error_mark_node)
13693 decl = TYPE_NAME (decl);
13696 /* Check to see that it is really the name of a class. */
13697 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13698 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13699 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13700 /* Situations like this:
13702 template <typename T> struct A {
13703 typename T::template X<int>::I i;
13706 are problematic. Is `T::template X<int>' a class-name? The
13707 standard does not seem to be definitive, but there is no other
13708 valid interpretation of the following `::'. Therefore, those
13709 names are considered class-names. */
13711 decl = make_typename_type (scope, decl, tag_type, tf_error);
13712 if (decl != error_mark_node)
13713 decl = TYPE_NAME (decl);
13715 else if (TREE_CODE (decl) != TYPE_DECL
13716 || TREE_TYPE (decl) == error_mark_node
13717 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13718 decl = error_mark_node;
13720 if (decl == error_mark_node)
13721 cp_parser_error (parser, "expected class-name");
13723 return decl;
13726 /* Parse a class-specifier.
13728 class-specifier:
13729 class-head { member-specification [opt] }
13731 Returns the TREE_TYPE representing the class. */
13733 static tree
13734 cp_parser_class_specifier (cp_parser* parser)
13736 cp_token *token;
13737 tree type;
13738 tree attributes = NULL_TREE;
13739 int has_trailing_semicolon;
13740 bool nested_name_specifier_p;
13741 unsigned saved_num_template_parameter_lists;
13742 bool saved_in_function_body;
13743 tree old_scope = NULL_TREE;
13744 tree scope = NULL_TREE;
13745 tree bases;
13747 push_deferring_access_checks (dk_no_deferred);
13749 /* Parse the class-head. */
13750 type = cp_parser_class_head (parser,
13751 &nested_name_specifier_p,
13752 &attributes,
13753 &bases);
13754 /* If the class-head was a semantic disaster, skip the entire body
13755 of the class. */
13756 if (!type)
13758 cp_parser_skip_to_end_of_block_or_statement (parser);
13759 pop_deferring_access_checks ();
13760 return error_mark_node;
13763 /* Look for the `{'. */
13764 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13766 pop_deferring_access_checks ();
13767 return error_mark_node;
13770 /* Process the base classes. If they're invalid, skip the
13771 entire class body. */
13772 if (!xref_basetypes (type, bases))
13774 cp_parser_skip_to_closing_brace (parser);
13776 /* Consuming the closing brace yields better error messages
13777 later on. */
13778 cp_lexer_consume_token (parser->lexer);
13779 pop_deferring_access_checks ();
13780 return error_mark_node;
13783 /* Issue an error message if type-definitions are forbidden here. */
13784 cp_parser_check_type_definition (parser);
13785 /* Remember that we are defining one more class. */
13786 ++parser->num_classes_being_defined;
13787 /* Inside the class, surrounding template-parameter-lists do not
13788 apply. */
13789 saved_num_template_parameter_lists
13790 = parser->num_template_parameter_lists;
13791 parser->num_template_parameter_lists = 0;
13792 /* We are not in a function body. */
13793 saved_in_function_body = parser->in_function_body;
13794 parser->in_function_body = false;
13796 /* Start the class. */
13797 if (nested_name_specifier_p)
13799 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13800 old_scope = push_inner_scope (scope);
13802 type = begin_class_definition (type, attributes);
13804 if (type == error_mark_node)
13805 /* If the type is erroneous, skip the entire body of the class. */
13806 cp_parser_skip_to_closing_brace (parser);
13807 else
13808 /* Parse the member-specification. */
13809 cp_parser_member_specification_opt (parser);
13811 /* Look for the trailing `}'. */
13812 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13813 /* We get better error messages by noticing a common problem: a
13814 missing trailing `;'. */
13815 token = cp_lexer_peek_token (parser->lexer);
13816 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13817 /* Look for trailing attributes to apply to this class. */
13818 if (cp_parser_allow_gnu_extensions_p (parser))
13819 attributes = cp_parser_attributes_opt (parser);
13820 if (type != error_mark_node)
13821 type = finish_struct (type, attributes);
13822 if (nested_name_specifier_p)
13823 pop_inner_scope (old_scope, scope);
13824 /* If this class is not itself within the scope of another class,
13825 then we need to parse the bodies of all of the queued function
13826 definitions. Note that the queued functions defined in a class
13827 are not always processed immediately following the
13828 class-specifier for that class. Consider:
13830 struct A {
13831 struct B { void f() { sizeof (A); } };
13834 If `f' were processed before the processing of `A' were
13835 completed, there would be no way to compute the size of `A'.
13836 Note that the nesting we are interested in here is lexical --
13837 not the semantic nesting given by TYPE_CONTEXT. In particular,
13838 for:
13840 struct A { struct B; };
13841 struct A::B { void f() { } };
13843 there is no need to delay the parsing of `A::B::f'. */
13844 if (--parser->num_classes_being_defined == 0)
13846 tree queue_entry;
13847 tree fn;
13848 tree class_type = NULL_TREE;
13849 tree pushed_scope = NULL_TREE;
13851 /* In a first pass, parse default arguments to the functions.
13852 Then, in a second pass, parse the bodies of the functions.
13853 This two-phased approach handles cases like:
13855 struct S {
13856 void f() { g(); }
13857 void g(int i = 3);
13861 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13862 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13863 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13864 TREE_PURPOSE (parser->unparsed_functions_queues)
13865 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13867 fn = TREE_VALUE (queue_entry);
13868 /* If there are default arguments that have not yet been processed,
13869 take care of them now. */
13870 if (class_type != TREE_PURPOSE (queue_entry))
13872 if (pushed_scope)
13873 pop_scope (pushed_scope);
13874 class_type = TREE_PURPOSE (queue_entry);
13875 pushed_scope = push_scope (class_type);
13877 /* Make sure that any template parameters are in scope. */
13878 maybe_begin_member_template_processing (fn);
13879 /* Parse the default argument expressions. */
13880 cp_parser_late_parsing_default_args (parser, fn);
13881 /* Remove any template parameters from the symbol table. */
13882 maybe_end_member_template_processing ();
13884 if (pushed_scope)
13885 pop_scope (pushed_scope);
13886 /* Now parse the body of the functions. */
13887 for (TREE_VALUE (parser->unparsed_functions_queues)
13888 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13889 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13890 TREE_VALUE (parser->unparsed_functions_queues)
13891 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13893 /* Figure out which function we need to process. */
13894 fn = TREE_VALUE (queue_entry);
13895 /* Parse the function. */
13896 cp_parser_late_parsing_for_member (parser, fn);
13900 /* Put back any saved access checks. */
13901 pop_deferring_access_checks ();
13903 /* Restore saved state. */
13904 parser->in_function_body = saved_in_function_body;
13905 parser->num_template_parameter_lists
13906 = saved_num_template_parameter_lists;
13908 return type;
13911 /* Parse a class-head.
13913 class-head:
13914 class-key identifier [opt] base-clause [opt]
13915 class-key nested-name-specifier identifier base-clause [opt]
13916 class-key nested-name-specifier [opt] template-id
13917 base-clause [opt]
13919 GNU Extensions:
13920 class-key attributes identifier [opt] base-clause [opt]
13921 class-key attributes nested-name-specifier identifier base-clause [opt]
13922 class-key attributes nested-name-specifier [opt] template-id
13923 base-clause [opt]
13925 Upon return BASES is initialized to the list of base classes (or
13926 NULL, if there are none) in the same form returned by
13927 cp_parser_base_clause.
13929 Returns the TYPE of the indicated class. Sets
13930 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13931 involving a nested-name-specifier was used, and FALSE otherwise.
13933 Returns error_mark_node if this is not a class-head.
13935 Returns NULL_TREE if the class-head is syntactically valid, but
13936 semantically invalid in a way that means we should skip the entire
13937 body of the class. */
13939 static tree
13940 cp_parser_class_head (cp_parser* parser,
13941 bool* nested_name_specifier_p,
13942 tree *attributes_p,
13943 tree *bases)
13945 tree nested_name_specifier;
13946 enum tag_types class_key;
13947 tree id = NULL_TREE;
13948 tree type = NULL_TREE;
13949 tree attributes;
13950 bool template_id_p = false;
13951 bool qualified_p = false;
13952 bool invalid_nested_name_p = false;
13953 bool invalid_explicit_specialization_p = false;
13954 tree pushed_scope = NULL_TREE;
13955 unsigned num_templates;
13957 /* Assume no nested-name-specifier will be present. */
13958 *nested_name_specifier_p = false;
13959 /* Assume no template parameter lists will be used in defining the
13960 type. */
13961 num_templates = 0;
13963 *bases = NULL_TREE;
13965 /* Look for the class-key. */
13966 class_key = cp_parser_class_key (parser);
13967 if (class_key == none_type)
13968 return error_mark_node;
13970 /* Parse the attributes. */
13971 attributes = cp_parser_attributes_opt (parser);
13973 /* If the next token is `::', that is invalid -- but sometimes
13974 people do try to write:
13976 struct ::S {};
13978 Handle this gracefully by accepting the extra qualifier, and then
13979 issuing an error about it later if this really is a
13980 class-head. If it turns out just to be an elaborated type
13981 specifier, remain silent. */
13982 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13983 qualified_p = true;
13985 push_deferring_access_checks (dk_no_check);
13987 /* Determine the name of the class. Begin by looking for an
13988 optional nested-name-specifier. */
13989 nested_name_specifier
13990 = cp_parser_nested_name_specifier_opt (parser,
13991 /*typename_keyword_p=*/false,
13992 /*check_dependency_p=*/false,
13993 /*type_p=*/false,
13994 /*is_declaration=*/false);
13995 /* If there was a nested-name-specifier, then there *must* be an
13996 identifier. */
13997 if (nested_name_specifier)
13999 /* Although the grammar says `identifier', it really means
14000 `class-name' or `template-name'. You are only allowed to
14001 define a class that has already been declared with this
14002 syntax.
14004 The proposed resolution for Core Issue 180 says that wherever
14005 you see `class T::X' you should treat `X' as a type-name.
14007 It is OK to define an inaccessible class; for example:
14009 class A { class B; };
14010 class A::B {};
14012 We do not know if we will see a class-name, or a
14013 template-name. We look for a class-name first, in case the
14014 class-name is a template-id; if we looked for the
14015 template-name first we would stop after the template-name. */
14016 cp_parser_parse_tentatively (parser);
14017 type = cp_parser_class_name (parser,
14018 /*typename_keyword_p=*/false,
14019 /*template_keyword_p=*/false,
14020 class_type,
14021 /*check_dependency_p=*/false,
14022 /*class_head_p=*/true,
14023 /*is_declaration=*/false);
14024 /* If that didn't work, ignore the nested-name-specifier. */
14025 if (!cp_parser_parse_definitely (parser))
14027 invalid_nested_name_p = true;
14028 id = cp_parser_identifier (parser);
14029 if (id == error_mark_node)
14030 id = NULL_TREE;
14032 /* If we could not find a corresponding TYPE, treat this
14033 declaration like an unqualified declaration. */
14034 if (type == error_mark_node)
14035 nested_name_specifier = NULL_TREE;
14036 /* Otherwise, count the number of templates used in TYPE and its
14037 containing scopes. */
14038 else
14040 tree scope;
14042 for (scope = TREE_TYPE (type);
14043 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14044 scope = (TYPE_P (scope)
14045 ? TYPE_CONTEXT (scope)
14046 : DECL_CONTEXT (scope)))
14047 if (TYPE_P (scope)
14048 && CLASS_TYPE_P (scope)
14049 && CLASSTYPE_TEMPLATE_INFO (scope)
14050 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14051 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14052 ++num_templates;
14055 /* Otherwise, the identifier is optional. */
14056 else
14058 /* We don't know whether what comes next is a template-id,
14059 an identifier, or nothing at all. */
14060 cp_parser_parse_tentatively (parser);
14061 /* Check for a template-id. */
14062 id = cp_parser_template_id (parser,
14063 /*template_keyword_p=*/false,
14064 /*check_dependency_p=*/true,
14065 /*is_declaration=*/true);
14066 /* If that didn't work, it could still be an identifier. */
14067 if (!cp_parser_parse_definitely (parser))
14069 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14070 id = cp_parser_identifier (parser);
14071 else
14072 id = NULL_TREE;
14074 else
14076 template_id_p = true;
14077 ++num_templates;
14081 pop_deferring_access_checks ();
14083 if (id)
14084 cp_parser_check_for_invalid_template_id (parser, id);
14086 /* If it's not a `:' or a `{' then we can't really be looking at a
14087 class-head, since a class-head only appears as part of a
14088 class-specifier. We have to detect this situation before calling
14089 xref_tag, since that has irreversible side-effects. */
14090 if (!cp_parser_next_token_starts_class_definition_p (parser))
14092 cp_parser_error (parser, "expected %<{%> or %<:%>");
14093 return error_mark_node;
14096 /* At this point, we're going ahead with the class-specifier, even
14097 if some other problem occurs. */
14098 cp_parser_commit_to_tentative_parse (parser);
14099 /* Issue the error about the overly-qualified name now. */
14100 if (qualified_p)
14101 cp_parser_error (parser,
14102 "global qualification of class name is invalid");
14103 else if (invalid_nested_name_p)
14104 cp_parser_error (parser,
14105 "qualified name does not name a class");
14106 else if (nested_name_specifier)
14108 tree scope;
14110 /* Reject typedef-names in class heads. */
14111 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14113 error ("invalid class name in declaration of %qD", type);
14114 type = NULL_TREE;
14115 goto done;
14118 /* Figure out in what scope the declaration is being placed. */
14119 scope = current_scope ();
14120 /* If that scope does not contain the scope in which the
14121 class was originally declared, the program is invalid. */
14122 if (scope && !is_ancestor (scope, nested_name_specifier))
14124 error ("declaration of %qD in %qD which does not enclose %qD",
14125 type, scope, nested_name_specifier);
14126 type = NULL_TREE;
14127 goto done;
14129 /* [dcl.meaning]
14131 A declarator-id shall not be qualified exception of the
14132 definition of a ... nested class outside of its class
14133 ... [or] a the definition or explicit instantiation of a
14134 class member of a namespace outside of its namespace. */
14135 if (scope == nested_name_specifier)
14137 pedwarn ("extra qualification ignored");
14138 nested_name_specifier = NULL_TREE;
14139 num_templates = 0;
14142 /* An explicit-specialization must be preceded by "template <>". If
14143 it is not, try to recover gracefully. */
14144 if (at_namespace_scope_p ()
14145 && parser->num_template_parameter_lists == 0
14146 && template_id_p)
14148 error ("an explicit specialization must be preceded by %<template <>%>");
14149 invalid_explicit_specialization_p = true;
14150 /* Take the same action that would have been taken by
14151 cp_parser_explicit_specialization. */
14152 ++parser->num_template_parameter_lists;
14153 begin_specialization ();
14155 /* There must be no "return" statements between this point and the
14156 end of this function; set "type "to the correct return value and
14157 use "goto done;" to return. */
14158 /* Make sure that the right number of template parameters were
14159 present. */
14160 if (!cp_parser_check_template_parameters (parser, num_templates))
14162 /* If something went wrong, there is no point in even trying to
14163 process the class-definition. */
14164 type = NULL_TREE;
14165 goto done;
14168 /* Look up the type. */
14169 if (template_id_p)
14171 type = TREE_TYPE (id);
14172 type = maybe_process_partial_specialization (type);
14173 if (nested_name_specifier)
14174 pushed_scope = push_scope (nested_name_specifier);
14176 else if (nested_name_specifier)
14178 tree class_type;
14180 /* Given:
14182 template <typename T> struct S { struct T };
14183 template <typename T> struct S<T>::T { };
14185 we will get a TYPENAME_TYPE when processing the definition of
14186 `S::T'. We need to resolve it to the actual type before we
14187 try to define it. */
14188 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14190 class_type = resolve_typename_type (TREE_TYPE (type),
14191 /*only_current_p=*/false);
14192 if (class_type != error_mark_node)
14193 type = TYPE_NAME (class_type);
14194 else
14196 cp_parser_error (parser, "could not resolve typename type");
14197 type = error_mark_node;
14201 maybe_process_partial_specialization (TREE_TYPE (type));
14202 class_type = current_class_type;
14203 /* Enter the scope indicated by the nested-name-specifier. */
14204 pushed_scope = push_scope (nested_name_specifier);
14205 /* Get the canonical version of this type. */
14206 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14207 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14208 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14210 type = push_template_decl (type);
14211 if (type == error_mark_node)
14213 type = NULL_TREE;
14214 goto done;
14218 type = TREE_TYPE (type);
14219 *nested_name_specifier_p = true;
14221 else /* The name is not a nested name. */
14223 /* If the class was unnamed, create a dummy name. */
14224 if (!id)
14225 id = make_anon_name ();
14226 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14227 parser->num_template_parameter_lists);
14230 /* Indicate whether this class was declared as a `class' or as a
14231 `struct'. */
14232 if (TREE_CODE (type) == RECORD_TYPE)
14233 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14234 cp_parser_check_class_key (class_key, type);
14236 /* If this type was already complete, and we see another definition,
14237 that's an error. */
14238 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14240 error ("redefinition of %q#T", type);
14241 error ("previous definition of %q+#T", type);
14242 type = NULL_TREE;
14243 goto done;
14245 else if (type == error_mark_node)
14246 type = NULL_TREE;
14248 /* We will have entered the scope containing the class; the names of
14249 base classes should be looked up in that context. For example:
14251 struct A { struct B {}; struct C; };
14252 struct A::C : B {};
14254 is valid. */
14256 /* Get the list of base-classes, if there is one. */
14257 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14258 *bases = cp_parser_base_clause (parser);
14260 done:
14261 /* Leave the scope given by the nested-name-specifier. We will
14262 enter the class scope itself while processing the members. */
14263 if (pushed_scope)
14264 pop_scope (pushed_scope);
14266 if (invalid_explicit_specialization_p)
14268 end_specialization ();
14269 --parser->num_template_parameter_lists;
14271 *attributes_p = attributes;
14272 return type;
14275 /* Parse a class-key.
14277 class-key:
14278 class
14279 struct
14280 union
14282 Returns the kind of class-key specified, or none_type to indicate
14283 error. */
14285 static enum tag_types
14286 cp_parser_class_key (cp_parser* parser)
14288 cp_token *token;
14289 enum tag_types tag_type;
14291 /* Look for the class-key. */
14292 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14293 if (!token)
14294 return none_type;
14296 /* Check to see if the TOKEN is a class-key. */
14297 tag_type = cp_parser_token_is_class_key (token);
14298 if (!tag_type)
14299 cp_parser_error (parser, "expected class-key");
14300 return tag_type;
14303 /* Parse an (optional) member-specification.
14305 member-specification:
14306 member-declaration member-specification [opt]
14307 access-specifier : member-specification [opt] */
14309 static void
14310 cp_parser_member_specification_opt (cp_parser* parser)
14312 while (true)
14314 cp_token *token;
14315 enum rid keyword;
14317 /* Peek at the next token. */
14318 token = cp_lexer_peek_token (parser->lexer);
14319 /* If it's a `}', or EOF then we've seen all the members. */
14320 if (token->type == CPP_CLOSE_BRACE
14321 || token->type == CPP_EOF
14322 || token->type == CPP_PRAGMA_EOL)
14323 break;
14325 /* See if this token is a keyword. */
14326 keyword = token->keyword;
14327 switch (keyword)
14329 case RID_PUBLIC:
14330 case RID_PROTECTED:
14331 case RID_PRIVATE:
14332 /* Consume the access-specifier. */
14333 cp_lexer_consume_token (parser->lexer);
14334 /* Remember which access-specifier is active. */
14335 current_access_specifier = token->u.value;
14336 /* Look for the `:'. */
14337 cp_parser_require (parser, CPP_COLON, "`:'");
14338 break;
14340 default:
14341 /* Accept #pragmas at class scope. */
14342 if (token->type == CPP_PRAGMA)
14344 cp_parser_pragma (parser, pragma_external);
14345 break;
14348 /* Otherwise, the next construction must be a
14349 member-declaration. */
14350 cp_parser_member_declaration (parser);
14355 /* Parse a member-declaration.
14357 member-declaration:
14358 decl-specifier-seq [opt] member-declarator-list [opt] ;
14359 function-definition ; [opt]
14360 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14361 using-declaration
14362 template-declaration
14364 member-declarator-list:
14365 member-declarator
14366 member-declarator-list , member-declarator
14368 member-declarator:
14369 declarator pure-specifier [opt]
14370 declarator constant-initializer [opt]
14371 identifier [opt] : constant-expression
14373 GNU Extensions:
14375 member-declaration:
14376 __extension__ member-declaration
14378 member-declarator:
14379 declarator attributes [opt] pure-specifier [opt]
14380 declarator attributes [opt] constant-initializer [opt]
14381 identifier [opt] attributes [opt] : constant-expression
14383 C++0x Extensions:
14385 member-declaration:
14386 static_assert-declaration */
14388 static void
14389 cp_parser_member_declaration (cp_parser* parser)
14391 cp_decl_specifier_seq decl_specifiers;
14392 tree prefix_attributes;
14393 tree decl;
14394 int declares_class_or_enum;
14395 bool friend_p;
14396 cp_token *token;
14397 int saved_pedantic;
14399 /* Check for the `__extension__' keyword. */
14400 if (cp_parser_extension_opt (parser, &saved_pedantic))
14402 /* Recurse. */
14403 cp_parser_member_declaration (parser);
14404 /* Restore the old value of the PEDANTIC flag. */
14405 pedantic = saved_pedantic;
14407 return;
14410 /* Check for a template-declaration. */
14411 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14413 /* An explicit specialization here is an error condition, and we
14414 expect the specialization handler to detect and report this. */
14415 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14416 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14417 cp_parser_explicit_specialization (parser);
14418 else
14419 cp_parser_template_declaration (parser, /*member_p=*/true);
14421 return;
14424 /* Check for a using-declaration. */
14425 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14427 /* Parse the using-declaration. */
14428 cp_parser_using_declaration (parser,
14429 /*access_declaration_p=*/false);
14430 return;
14433 /* Check for @defs. */
14434 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14436 tree ivar, member;
14437 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14438 ivar = ivar_chains;
14439 while (ivar)
14441 member = ivar;
14442 ivar = TREE_CHAIN (member);
14443 TREE_CHAIN (member) = NULL_TREE;
14444 finish_member_declaration (member);
14446 return;
14449 /* If the next token is `static_assert' we have a static assertion. */
14450 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14452 cp_parser_static_assert (parser, /*member_p=*/true);
14453 return;
14456 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14457 return;
14459 /* Parse the decl-specifier-seq. */
14460 cp_parser_decl_specifier_seq (parser,
14461 CP_PARSER_FLAGS_OPTIONAL,
14462 &decl_specifiers,
14463 &declares_class_or_enum);
14464 prefix_attributes = decl_specifiers.attributes;
14465 decl_specifiers.attributes = NULL_TREE;
14466 /* Check for an invalid type-name. */
14467 if (!decl_specifiers.type
14468 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14469 return;
14470 /* If there is no declarator, then the decl-specifier-seq should
14471 specify a type. */
14472 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14474 /* If there was no decl-specifier-seq, and the next token is a
14475 `;', then we have something like:
14477 struct S { ; };
14479 [class.mem]
14481 Each member-declaration shall declare at least one member
14482 name of the class. */
14483 if (!decl_specifiers.any_specifiers_p)
14485 cp_token *token = cp_lexer_peek_token (parser->lexer);
14486 if (pedantic && !token->in_system_header)
14487 pedwarn ("%Hextra %<;%>", &token->location);
14489 else
14491 tree type;
14493 /* See if this declaration is a friend. */
14494 friend_p = cp_parser_friend_p (&decl_specifiers);
14495 /* If there were decl-specifiers, check to see if there was
14496 a class-declaration. */
14497 type = check_tag_decl (&decl_specifiers);
14498 /* Nested classes have already been added to the class, but
14499 a `friend' needs to be explicitly registered. */
14500 if (friend_p)
14502 /* If the `friend' keyword was present, the friend must
14503 be introduced with a class-key. */
14504 if (!declares_class_or_enum)
14505 error ("a class-key must be used when declaring a friend");
14506 /* In this case:
14508 template <typename T> struct A {
14509 friend struct A<T>::B;
14512 A<T>::B will be represented by a TYPENAME_TYPE, and
14513 therefore not recognized by check_tag_decl. */
14514 if (!type
14515 && decl_specifiers.type
14516 && TYPE_P (decl_specifiers.type))
14517 type = decl_specifiers.type;
14518 if (!type || !TYPE_P (type))
14519 error ("friend declaration does not name a class or "
14520 "function");
14521 else
14522 make_friend_class (current_class_type, type,
14523 /*complain=*/true);
14525 /* If there is no TYPE, an error message will already have
14526 been issued. */
14527 else if (!type || type == error_mark_node)
14529 /* An anonymous aggregate has to be handled specially; such
14530 a declaration really declares a data member (with a
14531 particular type), as opposed to a nested class. */
14532 else if (ANON_AGGR_TYPE_P (type))
14534 /* Remove constructors and such from TYPE, now that we
14535 know it is an anonymous aggregate. */
14536 fixup_anonymous_aggr (type);
14537 /* And make the corresponding data member. */
14538 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14539 /* Add it to the class. */
14540 finish_member_declaration (decl);
14542 else
14543 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14546 else
14548 /* See if these declarations will be friends. */
14549 friend_p = cp_parser_friend_p (&decl_specifiers);
14551 /* Keep going until we hit the `;' at the end of the
14552 declaration. */
14553 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14555 tree attributes = NULL_TREE;
14556 tree first_attribute;
14558 /* Peek at the next token. */
14559 token = cp_lexer_peek_token (parser->lexer);
14561 /* Check for a bitfield declaration. */
14562 if (token->type == CPP_COLON
14563 || (token->type == CPP_NAME
14564 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14565 == CPP_COLON))
14567 tree identifier;
14568 tree width;
14570 /* Get the name of the bitfield. Note that we cannot just
14571 check TOKEN here because it may have been invalidated by
14572 the call to cp_lexer_peek_nth_token above. */
14573 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14574 identifier = cp_parser_identifier (parser);
14575 else
14576 identifier = NULL_TREE;
14578 /* Consume the `:' token. */
14579 cp_lexer_consume_token (parser->lexer);
14580 /* Get the width of the bitfield. */
14581 width
14582 = cp_parser_constant_expression (parser,
14583 /*allow_non_constant=*/false,
14584 NULL);
14586 /* Look for attributes that apply to the bitfield. */
14587 attributes = cp_parser_attributes_opt (parser);
14588 /* Remember which attributes are prefix attributes and
14589 which are not. */
14590 first_attribute = attributes;
14591 /* Combine the attributes. */
14592 attributes = chainon (prefix_attributes, attributes);
14594 /* Create the bitfield declaration. */
14595 decl = grokbitfield (identifier
14596 ? make_id_declarator (NULL_TREE,
14597 identifier,
14598 sfk_none)
14599 : NULL,
14600 &decl_specifiers,
14601 width);
14602 /* Apply the attributes. */
14603 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14605 else
14607 cp_declarator *declarator;
14608 tree initializer;
14609 tree asm_specification;
14610 int ctor_dtor_or_conv_p;
14612 /* Parse the declarator. */
14613 declarator
14614 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14615 &ctor_dtor_or_conv_p,
14616 /*parenthesized_p=*/NULL,
14617 /*member_p=*/true);
14619 /* If something went wrong parsing the declarator, make sure
14620 that we at least consume some tokens. */
14621 if (declarator == cp_error_declarator)
14623 /* Skip to the end of the statement. */
14624 cp_parser_skip_to_end_of_statement (parser);
14625 /* If the next token is not a semicolon, that is
14626 probably because we just skipped over the body of
14627 a function. So, we consume a semicolon if
14628 present, but do not issue an error message if it
14629 is not present. */
14630 if (cp_lexer_next_token_is (parser->lexer,
14631 CPP_SEMICOLON))
14632 cp_lexer_consume_token (parser->lexer);
14633 return;
14636 if (declares_class_or_enum & 2)
14637 cp_parser_check_for_definition_in_return_type
14638 (declarator, decl_specifiers.type);
14640 /* Look for an asm-specification. */
14641 asm_specification = cp_parser_asm_specification_opt (parser);
14642 /* Look for attributes that apply to the declaration. */
14643 attributes = cp_parser_attributes_opt (parser);
14644 /* Remember which attributes are prefix attributes and
14645 which are not. */
14646 first_attribute = attributes;
14647 /* Combine the attributes. */
14648 attributes = chainon (prefix_attributes, attributes);
14650 /* If it's an `=', then we have a constant-initializer or a
14651 pure-specifier. It is not correct to parse the
14652 initializer before registering the member declaration
14653 since the member declaration should be in scope while
14654 its initializer is processed. However, the rest of the
14655 front end does not yet provide an interface that allows
14656 us to handle this correctly. */
14657 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14659 /* In [class.mem]:
14661 A pure-specifier shall be used only in the declaration of
14662 a virtual function.
14664 A member-declarator can contain a constant-initializer
14665 only if it declares a static member of integral or
14666 enumeration type.
14668 Therefore, if the DECLARATOR is for a function, we look
14669 for a pure-specifier; otherwise, we look for a
14670 constant-initializer. When we call `grokfield', it will
14671 perform more stringent semantics checks. */
14672 if (function_declarator_p (declarator))
14673 initializer = cp_parser_pure_specifier (parser);
14674 else
14675 /* Parse the initializer. */
14676 initializer = cp_parser_constant_initializer (parser);
14678 /* Otherwise, there is no initializer. */
14679 else
14680 initializer = NULL_TREE;
14682 /* See if we are probably looking at a function
14683 definition. We are certainly not looking at a
14684 member-declarator. Calling `grokfield' has
14685 side-effects, so we must not do it unless we are sure
14686 that we are looking at a member-declarator. */
14687 if (cp_parser_token_starts_function_definition_p
14688 (cp_lexer_peek_token (parser->lexer)))
14690 /* The grammar does not allow a pure-specifier to be
14691 used when a member function is defined. (It is
14692 possible that this fact is an oversight in the
14693 standard, since a pure function may be defined
14694 outside of the class-specifier. */
14695 if (initializer)
14696 error ("pure-specifier on function-definition");
14697 decl = cp_parser_save_member_function_body (parser,
14698 &decl_specifiers,
14699 declarator,
14700 attributes);
14701 /* If the member was not a friend, declare it here. */
14702 if (!friend_p)
14703 finish_member_declaration (decl);
14704 /* Peek at the next token. */
14705 token = cp_lexer_peek_token (parser->lexer);
14706 /* If the next token is a semicolon, consume it. */
14707 if (token->type == CPP_SEMICOLON)
14709 if (pedantic && !in_system_header)
14710 pedwarn ("extra %<;%>");
14711 cp_lexer_consume_token (parser->lexer);
14713 return;
14715 else
14716 /* Create the declaration. */
14717 decl = grokfield (declarator, &decl_specifiers,
14718 initializer, /*init_const_expr_p=*/true,
14719 asm_specification,
14720 attributes);
14723 /* Reset PREFIX_ATTRIBUTES. */
14724 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14725 attributes = TREE_CHAIN (attributes);
14726 if (attributes)
14727 TREE_CHAIN (attributes) = NULL_TREE;
14729 /* If there is any qualification still in effect, clear it
14730 now; we will be starting fresh with the next declarator. */
14731 parser->scope = NULL_TREE;
14732 parser->qualifying_scope = NULL_TREE;
14733 parser->object_scope = NULL_TREE;
14734 /* If it's a `,', then there are more declarators. */
14735 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14736 cp_lexer_consume_token (parser->lexer);
14737 /* If the next token isn't a `;', then we have a parse error. */
14738 else if (cp_lexer_next_token_is_not (parser->lexer,
14739 CPP_SEMICOLON))
14741 cp_parser_error (parser, "expected %<;%>");
14742 /* Skip tokens until we find a `;'. */
14743 cp_parser_skip_to_end_of_statement (parser);
14745 break;
14748 if (decl)
14750 /* Add DECL to the list of members. */
14751 if (!friend_p)
14752 finish_member_declaration (decl);
14754 if (TREE_CODE (decl) == FUNCTION_DECL)
14755 cp_parser_save_default_args (parser, decl);
14760 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14763 /* Parse a pure-specifier.
14765 pure-specifier:
14768 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14769 Otherwise, ERROR_MARK_NODE is returned. */
14771 static tree
14772 cp_parser_pure_specifier (cp_parser* parser)
14774 cp_token *token;
14776 /* Look for the `=' token. */
14777 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14778 return error_mark_node;
14779 /* Look for the `0' token. */
14780 token = cp_lexer_consume_token (parser->lexer);
14781 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14782 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14784 cp_parser_error (parser,
14785 "invalid pure specifier (only `= 0' is allowed)");
14786 cp_parser_skip_to_end_of_statement (parser);
14787 return error_mark_node;
14789 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14791 error ("templates may not be %<virtual%>");
14792 return error_mark_node;
14795 return integer_zero_node;
14798 /* Parse a constant-initializer.
14800 constant-initializer:
14801 = constant-expression
14803 Returns a representation of the constant-expression. */
14805 static tree
14806 cp_parser_constant_initializer (cp_parser* parser)
14808 /* Look for the `=' token. */
14809 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14810 return error_mark_node;
14812 /* It is invalid to write:
14814 struct S { static const int i = { 7 }; };
14817 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14819 cp_parser_error (parser,
14820 "a brace-enclosed initializer is not allowed here");
14821 /* Consume the opening brace. */
14822 cp_lexer_consume_token (parser->lexer);
14823 /* Skip the initializer. */
14824 cp_parser_skip_to_closing_brace (parser);
14825 /* Look for the trailing `}'. */
14826 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14828 return error_mark_node;
14831 return cp_parser_constant_expression (parser,
14832 /*allow_non_constant=*/false,
14833 NULL);
14836 /* Derived classes [gram.class.derived] */
14838 /* Parse a base-clause.
14840 base-clause:
14841 : base-specifier-list
14843 base-specifier-list:
14844 base-specifier ... [opt]
14845 base-specifier-list , base-specifier ... [opt]
14847 Returns a TREE_LIST representing the base-classes, in the order in
14848 which they were declared. The representation of each node is as
14849 described by cp_parser_base_specifier.
14851 In the case that no bases are specified, this function will return
14852 NULL_TREE, not ERROR_MARK_NODE. */
14854 static tree
14855 cp_parser_base_clause (cp_parser* parser)
14857 tree bases = NULL_TREE;
14859 /* Look for the `:' that begins the list. */
14860 cp_parser_require (parser, CPP_COLON, "`:'");
14862 /* Scan the base-specifier-list. */
14863 while (true)
14865 cp_token *token;
14866 tree base;
14867 bool pack_expansion_p = false;
14869 /* Look for the base-specifier. */
14870 base = cp_parser_base_specifier (parser);
14871 /* Look for the (optional) ellipsis. */
14872 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14874 /* Consume the `...'. */
14875 cp_lexer_consume_token (parser->lexer);
14877 pack_expansion_p = true;
14880 /* Add BASE to the front of the list. */
14881 if (base != error_mark_node)
14883 if (pack_expansion_p)
14884 /* Make this a pack expansion type. */
14885 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
14886 else
14887 check_for_bare_parameter_packs (TREE_VALUE (base));
14889 TREE_CHAIN (base) = bases;
14890 bases = base;
14892 /* Peek at the next token. */
14893 token = cp_lexer_peek_token (parser->lexer);
14894 /* If it's not a comma, then the list is complete. */
14895 if (token->type != CPP_COMMA)
14896 break;
14897 /* Consume the `,'. */
14898 cp_lexer_consume_token (parser->lexer);
14901 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14902 base class had a qualified name. However, the next name that
14903 appears is certainly not qualified. */
14904 parser->scope = NULL_TREE;
14905 parser->qualifying_scope = NULL_TREE;
14906 parser->object_scope = NULL_TREE;
14908 return nreverse (bases);
14911 /* Parse a base-specifier.
14913 base-specifier:
14914 :: [opt] nested-name-specifier [opt] class-name
14915 virtual access-specifier [opt] :: [opt] nested-name-specifier
14916 [opt] class-name
14917 access-specifier virtual [opt] :: [opt] nested-name-specifier
14918 [opt] class-name
14920 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14921 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14922 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14923 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14925 static tree
14926 cp_parser_base_specifier (cp_parser* parser)
14928 cp_token *token;
14929 bool done = false;
14930 bool virtual_p = false;
14931 bool duplicate_virtual_error_issued_p = false;
14932 bool duplicate_access_error_issued_p = false;
14933 bool class_scope_p, template_p;
14934 tree access = access_default_node;
14935 tree type;
14937 /* Process the optional `virtual' and `access-specifier'. */
14938 while (!done)
14940 /* Peek at the next token. */
14941 token = cp_lexer_peek_token (parser->lexer);
14942 /* Process `virtual'. */
14943 switch (token->keyword)
14945 case RID_VIRTUAL:
14946 /* If `virtual' appears more than once, issue an error. */
14947 if (virtual_p && !duplicate_virtual_error_issued_p)
14949 cp_parser_error (parser,
14950 "%<virtual%> specified more than once in base-specified");
14951 duplicate_virtual_error_issued_p = true;
14954 virtual_p = true;
14956 /* Consume the `virtual' token. */
14957 cp_lexer_consume_token (parser->lexer);
14959 break;
14961 case RID_PUBLIC:
14962 case RID_PROTECTED:
14963 case RID_PRIVATE:
14964 /* If more than one access specifier appears, issue an
14965 error. */
14966 if (access != access_default_node
14967 && !duplicate_access_error_issued_p)
14969 cp_parser_error (parser,
14970 "more than one access specifier in base-specified");
14971 duplicate_access_error_issued_p = true;
14974 access = ridpointers[(int) token->keyword];
14976 /* Consume the access-specifier. */
14977 cp_lexer_consume_token (parser->lexer);
14979 break;
14981 default:
14982 done = true;
14983 break;
14986 /* It is not uncommon to see programs mechanically, erroneously, use
14987 the 'typename' keyword to denote (dependent) qualified types
14988 as base classes. */
14989 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14991 if (!processing_template_decl)
14992 error ("keyword %<typename%> not allowed outside of templates");
14993 else
14994 error ("keyword %<typename%> not allowed in this context "
14995 "(the base class is implicitly a type)");
14996 cp_lexer_consume_token (parser->lexer);
14999 /* Look for the optional `::' operator. */
15000 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15001 /* Look for the nested-name-specifier. The simplest way to
15002 implement:
15004 [temp.res]
15006 The keyword `typename' is not permitted in a base-specifier or
15007 mem-initializer; in these contexts a qualified name that
15008 depends on a template-parameter is implicitly assumed to be a
15009 type name.
15011 is to pretend that we have seen the `typename' keyword at this
15012 point. */
15013 cp_parser_nested_name_specifier_opt (parser,
15014 /*typename_keyword_p=*/true,
15015 /*check_dependency_p=*/true,
15016 typename_type,
15017 /*is_declaration=*/true);
15018 /* If the base class is given by a qualified name, assume that names
15019 we see are type names or templates, as appropriate. */
15020 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15021 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15023 /* Finally, look for the class-name. */
15024 type = cp_parser_class_name (parser,
15025 class_scope_p,
15026 template_p,
15027 typename_type,
15028 /*check_dependency_p=*/true,
15029 /*class_head_p=*/false,
15030 /*is_declaration=*/true);
15032 if (type == error_mark_node)
15033 return error_mark_node;
15035 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15038 /* Exception handling [gram.exception] */
15040 /* Parse an (optional) exception-specification.
15042 exception-specification:
15043 throw ( type-id-list [opt] )
15045 Returns a TREE_LIST representing the exception-specification. The
15046 TREE_VALUE of each node is a type. */
15048 static tree
15049 cp_parser_exception_specification_opt (cp_parser* parser)
15051 cp_token *token;
15052 tree type_id_list;
15054 /* Peek at the next token. */
15055 token = cp_lexer_peek_token (parser->lexer);
15056 /* If it's not `throw', then there's no exception-specification. */
15057 if (!cp_parser_is_keyword (token, RID_THROW))
15058 return NULL_TREE;
15060 /* Consume the `throw'. */
15061 cp_lexer_consume_token (parser->lexer);
15063 /* Look for the `('. */
15064 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15066 /* Peek at the next token. */
15067 token = cp_lexer_peek_token (parser->lexer);
15068 /* If it's not a `)', then there is a type-id-list. */
15069 if (token->type != CPP_CLOSE_PAREN)
15071 const char *saved_message;
15073 /* Types may not be defined in an exception-specification. */
15074 saved_message = parser->type_definition_forbidden_message;
15075 parser->type_definition_forbidden_message
15076 = "types may not be defined in an exception-specification";
15077 /* Parse the type-id-list. */
15078 type_id_list = cp_parser_type_id_list (parser);
15079 /* Restore the saved message. */
15080 parser->type_definition_forbidden_message = saved_message;
15082 else
15083 type_id_list = empty_except_spec;
15085 /* Look for the `)'. */
15086 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15088 return type_id_list;
15091 /* Parse an (optional) type-id-list.
15093 type-id-list:
15094 type-id ... [opt]
15095 type-id-list , type-id ... [opt]
15097 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15098 in the order that the types were presented. */
15100 static tree
15101 cp_parser_type_id_list (cp_parser* parser)
15103 tree types = NULL_TREE;
15105 while (true)
15107 cp_token *token;
15108 tree type;
15110 /* Get the next type-id. */
15111 type = cp_parser_type_id (parser);
15112 /* Parse the optional ellipsis. */
15113 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15115 /* Consume the `...'. */
15116 cp_lexer_consume_token (parser->lexer);
15118 /* Turn the type into a pack expansion expression. */
15119 type = make_pack_expansion (type);
15121 /* Add it to the list. */
15122 types = add_exception_specifier (types, type, /*complain=*/1);
15123 /* Peek at the next token. */
15124 token = cp_lexer_peek_token (parser->lexer);
15125 /* If it is not a `,', we are done. */
15126 if (token->type != CPP_COMMA)
15127 break;
15128 /* Consume the `,'. */
15129 cp_lexer_consume_token (parser->lexer);
15132 return nreverse (types);
15135 /* Parse a try-block.
15137 try-block:
15138 try compound-statement handler-seq */
15140 static tree
15141 cp_parser_try_block (cp_parser* parser)
15143 tree try_block;
15145 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15146 try_block = begin_try_block ();
15147 cp_parser_compound_statement (parser, NULL, true);
15148 finish_try_block (try_block);
15149 cp_parser_handler_seq (parser);
15150 finish_handler_sequence (try_block);
15152 return try_block;
15155 /* Parse a function-try-block.
15157 function-try-block:
15158 try ctor-initializer [opt] function-body handler-seq */
15160 static bool
15161 cp_parser_function_try_block (cp_parser* parser)
15163 tree compound_stmt;
15164 tree try_block;
15165 bool ctor_initializer_p;
15167 /* Look for the `try' keyword. */
15168 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15169 return false;
15170 /* Let the rest of the front end know where we are. */
15171 try_block = begin_function_try_block (&compound_stmt);
15172 /* Parse the function-body. */
15173 ctor_initializer_p
15174 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15175 /* We're done with the `try' part. */
15176 finish_function_try_block (try_block);
15177 /* Parse the handlers. */
15178 cp_parser_handler_seq (parser);
15179 /* We're done with the handlers. */
15180 finish_function_handler_sequence (try_block, compound_stmt);
15182 return ctor_initializer_p;
15185 /* Parse a handler-seq.
15187 handler-seq:
15188 handler handler-seq [opt] */
15190 static void
15191 cp_parser_handler_seq (cp_parser* parser)
15193 while (true)
15195 cp_token *token;
15197 /* Parse the handler. */
15198 cp_parser_handler (parser);
15199 /* Peek at the next token. */
15200 token = cp_lexer_peek_token (parser->lexer);
15201 /* If it's not `catch' then there are no more handlers. */
15202 if (!cp_parser_is_keyword (token, RID_CATCH))
15203 break;
15207 /* Parse a handler.
15209 handler:
15210 catch ( exception-declaration ) compound-statement */
15212 static void
15213 cp_parser_handler (cp_parser* parser)
15215 tree handler;
15216 tree declaration;
15218 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15219 handler = begin_handler ();
15220 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15221 declaration = cp_parser_exception_declaration (parser);
15222 finish_handler_parms (declaration, handler);
15223 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15224 cp_parser_compound_statement (parser, NULL, false);
15225 finish_handler (handler);
15228 /* Parse an exception-declaration.
15230 exception-declaration:
15231 type-specifier-seq declarator
15232 type-specifier-seq abstract-declarator
15233 type-specifier-seq
15236 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15237 ellipsis variant is used. */
15239 static tree
15240 cp_parser_exception_declaration (cp_parser* parser)
15242 cp_decl_specifier_seq type_specifiers;
15243 cp_declarator *declarator;
15244 const char *saved_message;
15246 /* If it's an ellipsis, it's easy to handle. */
15247 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15249 /* Consume the `...' token. */
15250 cp_lexer_consume_token (parser->lexer);
15251 return NULL_TREE;
15254 /* Types may not be defined in exception-declarations. */
15255 saved_message = parser->type_definition_forbidden_message;
15256 parser->type_definition_forbidden_message
15257 = "types may not be defined in exception-declarations";
15259 /* Parse the type-specifier-seq. */
15260 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15261 &type_specifiers);
15262 /* If it's a `)', then there is no declarator. */
15263 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15264 declarator = NULL;
15265 else
15266 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15267 /*ctor_dtor_or_conv_p=*/NULL,
15268 /*parenthesized_p=*/NULL,
15269 /*member_p=*/false);
15271 /* Restore the saved message. */
15272 parser->type_definition_forbidden_message = saved_message;
15274 if (!type_specifiers.any_specifiers_p)
15275 return error_mark_node;
15277 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15280 /* Parse a throw-expression.
15282 throw-expression:
15283 throw assignment-expression [opt]
15285 Returns a THROW_EXPR representing the throw-expression. */
15287 static tree
15288 cp_parser_throw_expression (cp_parser* parser)
15290 tree expression;
15291 cp_token* token;
15293 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15294 token = cp_lexer_peek_token (parser->lexer);
15295 /* Figure out whether or not there is an assignment-expression
15296 following the "throw" keyword. */
15297 if (token->type == CPP_COMMA
15298 || token->type == CPP_SEMICOLON
15299 || token->type == CPP_CLOSE_PAREN
15300 || token->type == CPP_CLOSE_SQUARE
15301 || token->type == CPP_CLOSE_BRACE
15302 || token->type == CPP_COLON)
15303 expression = NULL_TREE;
15304 else
15305 expression = cp_parser_assignment_expression (parser,
15306 /*cast_p=*/false);
15308 return build_throw (expression);
15311 /* GNU Extensions */
15313 /* Parse an (optional) asm-specification.
15315 asm-specification:
15316 asm ( string-literal )
15318 If the asm-specification is present, returns a STRING_CST
15319 corresponding to the string-literal. Otherwise, returns
15320 NULL_TREE. */
15322 static tree
15323 cp_parser_asm_specification_opt (cp_parser* parser)
15325 cp_token *token;
15326 tree asm_specification;
15328 /* Peek at the next token. */
15329 token = cp_lexer_peek_token (parser->lexer);
15330 /* If the next token isn't the `asm' keyword, then there's no
15331 asm-specification. */
15332 if (!cp_parser_is_keyword (token, RID_ASM))
15333 return NULL_TREE;
15335 /* Consume the `asm' token. */
15336 cp_lexer_consume_token (parser->lexer);
15337 /* Look for the `('. */
15338 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15340 /* Look for the string-literal. */
15341 asm_specification = cp_parser_string_literal (parser, false, false);
15343 /* Look for the `)'. */
15344 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15346 return asm_specification;
15349 /* Parse an asm-operand-list.
15351 asm-operand-list:
15352 asm-operand
15353 asm-operand-list , asm-operand
15355 asm-operand:
15356 string-literal ( expression )
15357 [ string-literal ] string-literal ( expression )
15359 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15360 each node is the expression. The TREE_PURPOSE is itself a
15361 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15362 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15363 is a STRING_CST for the string literal before the parenthesis. */
15365 static tree
15366 cp_parser_asm_operand_list (cp_parser* parser)
15368 tree asm_operands = NULL_TREE;
15370 while (true)
15372 tree string_literal;
15373 tree expression;
15374 tree name;
15376 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15378 /* Consume the `[' token. */
15379 cp_lexer_consume_token (parser->lexer);
15380 /* Read the operand name. */
15381 name = cp_parser_identifier (parser);
15382 if (name != error_mark_node)
15383 name = build_string (IDENTIFIER_LENGTH (name),
15384 IDENTIFIER_POINTER (name));
15385 /* Look for the closing `]'. */
15386 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15388 else
15389 name = NULL_TREE;
15390 /* Look for the string-literal. */
15391 string_literal = cp_parser_string_literal (parser, false, false);
15393 /* Look for the `('. */
15394 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15395 /* Parse the expression. */
15396 expression = cp_parser_expression (parser, /*cast_p=*/false);
15397 /* Look for the `)'. */
15398 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15400 /* Add this operand to the list. */
15401 asm_operands = tree_cons (build_tree_list (name, string_literal),
15402 expression,
15403 asm_operands);
15404 /* If the next token is not a `,', there are no more
15405 operands. */
15406 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15407 break;
15408 /* Consume the `,'. */
15409 cp_lexer_consume_token (parser->lexer);
15412 return nreverse (asm_operands);
15415 /* Parse an asm-clobber-list.
15417 asm-clobber-list:
15418 string-literal
15419 asm-clobber-list , string-literal
15421 Returns a TREE_LIST, indicating the clobbers in the order that they
15422 appeared. The TREE_VALUE of each node is a STRING_CST. */
15424 static tree
15425 cp_parser_asm_clobber_list (cp_parser* parser)
15427 tree clobbers = NULL_TREE;
15429 while (true)
15431 tree string_literal;
15433 /* Look for the string literal. */
15434 string_literal = cp_parser_string_literal (parser, false, false);
15435 /* Add it to the list. */
15436 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15437 /* If the next token is not a `,', then the list is
15438 complete. */
15439 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15440 break;
15441 /* Consume the `,' token. */
15442 cp_lexer_consume_token (parser->lexer);
15445 return clobbers;
15448 /* Parse an (optional) series of attributes.
15450 attributes:
15451 attributes attribute
15453 attribute:
15454 __attribute__ (( attribute-list [opt] ))
15456 The return value is as for cp_parser_attribute_list. */
15458 static tree
15459 cp_parser_attributes_opt (cp_parser* parser)
15461 tree attributes = NULL_TREE;
15463 while (true)
15465 cp_token *token;
15466 tree attribute_list;
15468 /* Peek at the next token. */
15469 token = cp_lexer_peek_token (parser->lexer);
15470 /* If it's not `__attribute__', then we're done. */
15471 if (token->keyword != RID_ATTRIBUTE)
15472 break;
15474 /* Consume the `__attribute__' keyword. */
15475 cp_lexer_consume_token (parser->lexer);
15476 /* Look for the two `(' tokens. */
15477 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15478 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15480 /* Peek at the next token. */
15481 token = cp_lexer_peek_token (parser->lexer);
15482 if (token->type != CPP_CLOSE_PAREN)
15483 /* Parse the attribute-list. */
15484 attribute_list = cp_parser_attribute_list (parser);
15485 else
15486 /* If the next token is a `)', then there is no attribute
15487 list. */
15488 attribute_list = NULL;
15490 /* Look for the two `)' tokens. */
15491 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15492 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15494 /* Add these new attributes to the list. */
15495 attributes = chainon (attributes, attribute_list);
15498 return attributes;
15501 /* Parse an attribute-list.
15503 attribute-list:
15504 attribute
15505 attribute-list , attribute
15507 attribute:
15508 identifier
15509 identifier ( identifier )
15510 identifier ( identifier , expression-list )
15511 identifier ( expression-list )
15513 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15514 to an attribute. The TREE_PURPOSE of each node is the identifier
15515 indicating which attribute is in use. The TREE_VALUE represents
15516 the arguments, if any. */
15518 static tree
15519 cp_parser_attribute_list (cp_parser* parser)
15521 tree attribute_list = NULL_TREE;
15522 bool save_translate_strings_p = parser->translate_strings_p;
15524 parser->translate_strings_p = false;
15525 while (true)
15527 cp_token *token;
15528 tree identifier;
15529 tree attribute;
15531 /* Look for the identifier. We also allow keywords here; for
15532 example `__attribute__ ((const))' is legal. */
15533 token = cp_lexer_peek_token (parser->lexer);
15534 if (token->type == CPP_NAME
15535 || token->type == CPP_KEYWORD)
15537 tree arguments = NULL_TREE;
15539 /* Consume the token. */
15540 token = cp_lexer_consume_token (parser->lexer);
15542 /* Save away the identifier that indicates which attribute
15543 this is. */
15544 identifier = token->u.value;
15545 attribute = build_tree_list (identifier, NULL_TREE);
15547 /* Peek at the next token. */
15548 token = cp_lexer_peek_token (parser->lexer);
15549 /* If it's an `(', then parse the attribute arguments. */
15550 if (token->type == CPP_OPEN_PAREN)
15552 arguments = cp_parser_parenthesized_expression_list
15553 (parser, true, /*cast_p=*/false,
15554 /*allow_expansion_p=*/false,
15555 /*non_constant_p=*/NULL);
15556 /* Save the arguments away. */
15557 TREE_VALUE (attribute) = arguments;
15560 if (arguments != error_mark_node)
15562 /* Add this attribute to the list. */
15563 TREE_CHAIN (attribute) = attribute_list;
15564 attribute_list = attribute;
15567 token = cp_lexer_peek_token (parser->lexer);
15569 /* Now, look for more attributes. If the next token isn't a
15570 `,', we're done. */
15571 if (token->type != CPP_COMMA)
15572 break;
15574 /* Consume the comma and keep going. */
15575 cp_lexer_consume_token (parser->lexer);
15577 parser->translate_strings_p = save_translate_strings_p;
15579 /* We built up the list in reverse order. */
15580 return nreverse (attribute_list);
15583 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15584 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15585 current value of the PEDANTIC flag, regardless of whether or not
15586 the `__extension__' keyword is present. The caller is responsible
15587 for restoring the value of the PEDANTIC flag. */
15589 static bool
15590 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15592 /* Save the old value of the PEDANTIC flag. */
15593 *saved_pedantic = pedantic;
15595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15597 /* Consume the `__extension__' token. */
15598 cp_lexer_consume_token (parser->lexer);
15599 /* We're not being pedantic while the `__extension__' keyword is
15600 in effect. */
15601 pedantic = 0;
15603 return true;
15606 return false;
15609 /* Parse a label declaration.
15611 label-declaration:
15612 __label__ label-declarator-seq ;
15614 label-declarator-seq:
15615 identifier , label-declarator-seq
15616 identifier */
15618 static void
15619 cp_parser_label_declaration (cp_parser* parser)
15621 /* Look for the `__label__' keyword. */
15622 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15624 while (true)
15626 tree identifier;
15628 /* Look for an identifier. */
15629 identifier = cp_parser_identifier (parser);
15630 /* If we failed, stop. */
15631 if (identifier == error_mark_node)
15632 break;
15633 /* Declare it as a label. */
15634 finish_label_decl (identifier);
15635 /* If the next token is a `;', stop. */
15636 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15637 break;
15638 /* Look for the `,' separating the label declarations. */
15639 cp_parser_require (parser, CPP_COMMA, "`,'");
15642 /* Look for the final `;'. */
15643 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15646 /* Support Functions */
15648 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15649 NAME should have one of the representations used for an
15650 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15651 is returned. If PARSER->SCOPE is a dependent type, then a
15652 SCOPE_REF is returned.
15654 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15655 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15656 was formed. Abstractly, such entities should not be passed to this
15657 function, because they do not need to be looked up, but it is
15658 simpler to check for this special case here, rather than at the
15659 call-sites.
15661 In cases not explicitly covered above, this function returns a
15662 DECL, OVERLOAD, or baselink representing the result of the lookup.
15663 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15664 is returned.
15666 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15667 (e.g., "struct") that was used. In that case bindings that do not
15668 refer to types are ignored.
15670 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15671 ignored.
15673 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15674 are ignored.
15676 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15677 types.
15679 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15680 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15681 NULL_TREE otherwise. */
15683 static tree
15684 cp_parser_lookup_name (cp_parser *parser, tree name,
15685 enum tag_types tag_type,
15686 bool is_template,
15687 bool is_namespace,
15688 bool check_dependency,
15689 tree *ambiguous_decls)
15691 int flags = 0;
15692 tree decl;
15693 tree object_type = parser->context->object_type;
15695 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15696 flags |= LOOKUP_COMPLAIN;
15698 /* Assume that the lookup will be unambiguous. */
15699 if (ambiguous_decls)
15700 *ambiguous_decls = NULL_TREE;
15702 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15703 no longer valid. Note that if we are parsing tentatively, and
15704 the parse fails, OBJECT_TYPE will be automatically restored. */
15705 parser->context->object_type = NULL_TREE;
15707 if (name == error_mark_node)
15708 return error_mark_node;
15710 /* A template-id has already been resolved; there is no lookup to
15711 do. */
15712 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15713 return name;
15714 if (BASELINK_P (name))
15716 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15717 == TEMPLATE_ID_EXPR);
15718 return name;
15721 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15722 it should already have been checked to make sure that the name
15723 used matches the type being destroyed. */
15724 if (TREE_CODE (name) == BIT_NOT_EXPR)
15726 tree type;
15728 /* Figure out to which type this destructor applies. */
15729 if (parser->scope)
15730 type = parser->scope;
15731 else if (object_type)
15732 type = object_type;
15733 else
15734 type = current_class_type;
15735 /* If that's not a class type, there is no destructor. */
15736 if (!type || !CLASS_TYPE_P (type))
15737 return error_mark_node;
15738 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15739 lazily_declare_fn (sfk_destructor, type);
15740 if (!CLASSTYPE_DESTRUCTORS (type))
15741 return error_mark_node;
15742 /* If it was a class type, return the destructor. */
15743 return CLASSTYPE_DESTRUCTORS (type);
15746 /* By this point, the NAME should be an ordinary identifier. If
15747 the id-expression was a qualified name, the qualifying scope is
15748 stored in PARSER->SCOPE at this point. */
15749 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15751 /* Perform the lookup. */
15752 if (parser->scope)
15754 bool dependent_p;
15756 if (parser->scope == error_mark_node)
15757 return error_mark_node;
15759 /* If the SCOPE is dependent, the lookup must be deferred until
15760 the template is instantiated -- unless we are explicitly
15761 looking up names in uninstantiated templates. Even then, we
15762 cannot look up the name if the scope is not a class type; it
15763 might, for example, be a template type parameter. */
15764 dependent_p = (TYPE_P (parser->scope)
15765 && !(parser->in_declarator_p
15766 && currently_open_class (parser->scope))
15767 && dependent_type_p (parser->scope));
15768 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15769 && dependent_p)
15771 if (tag_type)
15773 tree type;
15775 /* The resolution to Core Issue 180 says that `struct
15776 A::B' should be considered a type-name, even if `A'
15777 is dependent. */
15778 type = make_typename_type (parser->scope, name, tag_type,
15779 /*complain=*/tf_error);
15780 decl = TYPE_NAME (type);
15782 else if (is_template
15783 && (cp_parser_next_token_ends_template_argument_p (parser)
15784 || cp_lexer_next_token_is (parser->lexer,
15785 CPP_CLOSE_PAREN)))
15786 decl = make_unbound_class_template (parser->scope,
15787 name, NULL_TREE,
15788 /*complain=*/tf_error);
15789 else
15790 decl = build_qualified_name (/*type=*/NULL_TREE,
15791 parser->scope, name,
15792 is_template);
15794 else
15796 tree pushed_scope = NULL_TREE;
15798 /* If PARSER->SCOPE is a dependent type, then it must be a
15799 class type, and we must not be checking dependencies;
15800 otherwise, we would have processed this lookup above. So
15801 that PARSER->SCOPE is not considered a dependent base by
15802 lookup_member, we must enter the scope here. */
15803 if (dependent_p)
15804 pushed_scope = push_scope (parser->scope);
15805 /* If the PARSER->SCOPE is a template specialization, it
15806 may be instantiated during name lookup. In that case,
15807 errors may be issued. Even if we rollback the current
15808 tentative parse, those errors are valid. */
15809 decl = lookup_qualified_name (parser->scope, name,
15810 tag_type != none_type,
15811 /*complain=*/true);
15812 if (pushed_scope)
15813 pop_scope (pushed_scope);
15815 parser->qualifying_scope = parser->scope;
15816 parser->object_scope = NULL_TREE;
15818 else if (object_type)
15820 tree object_decl = NULL_TREE;
15821 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15822 OBJECT_TYPE is not a class. */
15823 if (CLASS_TYPE_P (object_type))
15824 /* If the OBJECT_TYPE is a template specialization, it may
15825 be instantiated during name lookup. In that case, errors
15826 may be issued. Even if we rollback the current tentative
15827 parse, those errors are valid. */
15828 object_decl = lookup_member (object_type,
15829 name,
15830 /*protect=*/0,
15831 tag_type != none_type);
15832 /* Look it up in the enclosing context, too. */
15833 decl = lookup_name_real (name, tag_type != none_type,
15834 /*nonclass=*/0,
15835 /*block_p=*/true, is_namespace, flags);
15836 parser->object_scope = object_type;
15837 parser->qualifying_scope = NULL_TREE;
15838 if (object_decl)
15839 decl = object_decl;
15841 else
15843 decl = lookup_name_real (name, tag_type != none_type,
15844 /*nonclass=*/0,
15845 /*block_p=*/true, is_namespace, flags);
15846 parser->qualifying_scope = NULL_TREE;
15847 parser->object_scope = NULL_TREE;
15850 /* If the lookup failed, let our caller know. */
15851 if (!decl || decl == error_mark_node)
15852 return error_mark_node;
15854 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15855 if (TREE_CODE (decl) == TREE_LIST)
15857 if (ambiguous_decls)
15858 *ambiguous_decls = decl;
15859 /* The error message we have to print is too complicated for
15860 cp_parser_error, so we incorporate its actions directly. */
15861 if (!cp_parser_simulate_error (parser))
15863 error ("reference to %qD is ambiguous", name);
15864 print_candidates (decl);
15866 return error_mark_node;
15869 gcc_assert (DECL_P (decl)
15870 || TREE_CODE (decl) == OVERLOAD
15871 || TREE_CODE (decl) == SCOPE_REF
15872 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15873 || BASELINK_P (decl));
15875 /* If we have resolved the name of a member declaration, check to
15876 see if the declaration is accessible. When the name resolves to
15877 set of overloaded functions, accessibility is checked when
15878 overload resolution is done.
15880 During an explicit instantiation, access is not checked at all,
15881 as per [temp.explicit]. */
15882 if (DECL_P (decl))
15883 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15885 return decl;
15888 /* Like cp_parser_lookup_name, but for use in the typical case where
15889 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15890 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15892 static tree
15893 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15895 return cp_parser_lookup_name (parser, name,
15896 none_type,
15897 /*is_template=*/false,
15898 /*is_namespace=*/false,
15899 /*check_dependency=*/true,
15900 /*ambiguous_decls=*/NULL);
15903 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15904 the current context, return the TYPE_DECL. If TAG_NAME_P is
15905 true, the DECL indicates the class being defined in a class-head,
15906 or declared in an elaborated-type-specifier.
15908 Otherwise, return DECL. */
15910 static tree
15911 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15913 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15914 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15916 struct A {
15917 template <typename T> struct B;
15920 template <typename T> struct A::B {};
15922 Similarly, in an elaborated-type-specifier:
15924 namespace N { struct X{}; }
15926 struct A {
15927 template <typename T> friend struct N::X;
15930 However, if the DECL refers to a class type, and we are in
15931 the scope of the class, then the name lookup automatically
15932 finds the TYPE_DECL created by build_self_reference rather
15933 than a TEMPLATE_DECL. For example, in:
15935 template <class T> struct S {
15936 S s;
15939 there is no need to handle such case. */
15941 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15942 return DECL_TEMPLATE_RESULT (decl);
15944 return decl;
15947 /* If too many, or too few, template-parameter lists apply to the
15948 declarator, issue an error message. Returns TRUE if all went well,
15949 and FALSE otherwise. */
15951 static bool
15952 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15953 cp_declarator *declarator)
15955 unsigned num_templates;
15957 /* We haven't seen any classes that involve template parameters yet. */
15958 num_templates = 0;
15960 switch (declarator->kind)
15962 case cdk_id:
15963 if (declarator->u.id.qualifying_scope)
15965 tree scope;
15966 tree member;
15968 scope = declarator->u.id.qualifying_scope;
15969 member = declarator->u.id.unqualified_name;
15971 while (scope && CLASS_TYPE_P (scope))
15973 /* You're supposed to have one `template <...>'
15974 for every template class, but you don't need one
15975 for a full specialization. For example:
15977 template <class T> struct S{};
15978 template <> struct S<int> { void f(); };
15979 void S<int>::f () {}
15981 is correct; there shouldn't be a `template <>' for
15982 the definition of `S<int>::f'. */
15983 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15984 /* If SCOPE does not have template information of any
15985 kind, then it is not a template, nor is it nested
15986 within a template. */
15987 break;
15988 if (explicit_class_specialization_p (scope))
15989 break;
15990 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15991 ++num_templates;
15993 scope = TYPE_CONTEXT (scope);
15996 else if (TREE_CODE (declarator->u.id.unqualified_name)
15997 == TEMPLATE_ID_EXPR)
15998 /* If the DECLARATOR has the form `X<y>' then it uses one
15999 additional level of template parameters. */
16000 ++num_templates;
16002 return cp_parser_check_template_parameters (parser,
16003 num_templates);
16005 case cdk_function:
16006 case cdk_array:
16007 case cdk_pointer:
16008 case cdk_reference:
16009 case cdk_ptrmem:
16010 return (cp_parser_check_declarator_template_parameters
16011 (parser, declarator->declarator));
16013 case cdk_error:
16014 return true;
16016 default:
16017 gcc_unreachable ();
16019 return false;
16022 /* NUM_TEMPLATES were used in the current declaration. If that is
16023 invalid, return FALSE and issue an error messages. Otherwise,
16024 return TRUE. */
16026 static bool
16027 cp_parser_check_template_parameters (cp_parser* parser,
16028 unsigned num_templates)
16030 /* If there are more template classes than parameter lists, we have
16031 something like:
16033 template <class T> void S<T>::R<T>::f (); */
16034 if (parser->num_template_parameter_lists < num_templates)
16036 error ("too few template-parameter-lists");
16037 return false;
16039 /* If there are the same number of template classes and parameter
16040 lists, that's OK. */
16041 if (parser->num_template_parameter_lists == num_templates)
16042 return true;
16043 /* If there are more, but only one more, then we are referring to a
16044 member template. That's OK too. */
16045 if (parser->num_template_parameter_lists == num_templates + 1)
16046 return true;
16047 /* Otherwise, there are too many template parameter lists. We have
16048 something like:
16050 template <class T> template <class U> void S::f(); */
16051 error ("too many template-parameter-lists");
16052 return false;
16055 /* Parse an optional `::' token indicating that the following name is
16056 from the global namespace. If so, PARSER->SCOPE is set to the
16057 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16058 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16059 Returns the new value of PARSER->SCOPE, if the `::' token is
16060 present, and NULL_TREE otherwise. */
16062 static tree
16063 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16065 cp_token *token;
16067 /* Peek at the next token. */
16068 token = cp_lexer_peek_token (parser->lexer);
16069 /* If we're looking at a `::' token then we're starting from the
16070 global namespace, not our current location. */
16071 if (token->type == CPP_SCOPE)
16073 /* Consume the `::' token. */
16074 cp_lexer_consume_token (parser->lexer);
16075 /* Set the SCOPE so that we know where to start the lookup. */
16076 parser->scope = global_namespace;
16077 parser->qualifying_scope = global_namespace;
16078 parser->object_scope = NULL_TREE;
16080 return parser->scope;
16082 else if (!current_scope_valid_p)
16084 parser->scope = NULL_TREE;
16085 parser->qualifying_scope = NULL_TREE;
16086 parser->object_scope = NULL_TREE;
16089 return NULL_TREE;
16092 /* Returns TRUE if the upcoming token sequence is the start of a
16093 constructor declarator. If FRIEND_P is true, the declarator is
16094 preceded by the `friend' specifier. */
16096 static bool
16097 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16099 bool constructor_p;
16100 tree type_decl = NULL_TREE;
16101 bool nested_name_p;
16102 cp_token *next_token;
16104 /* The common case is that this is not a constructor declarator, so
16105 try to avoid doing lots of work if at all possible. It's not
16106 valid declare a constructor at function scope. */
16107 if (parser->in_function_body)
16108 return false;
16109 /* And only certain tokens can begin a constructor declarator. */
16110 next_token = cp_lexer_peek_token (parser->lexer);
16111 if (next_token->type != CPP_NAME
16112 && next_token->type != CPP_SCOPE
16113 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16114 && next_token->type != CPP_TEMPLATE_ID)
16115 return false;
16117 /* Parse tentatively; we are going to roll back all of the tokens
16118 consumed here. */
16119 cp_parser_parse_tentatively (parser);
16120 /* Assume that we are looking at a constructor declarator. */
16121 constructor_p = true;
16123 /* Look for the optional `::' operator. */
16124 cp_parser_global_scope_opt (parser,
16125 /*current_scope_valid_p=*/false);
16126 /* Look for the nested-name-specifier. */
16127 nested_name_p
16128 = (cp_parser_nested_name_specifier_opt (parser,
16129 /*typename_keyword_p=*/false,
16130 /*check_dependency_p=*/false,
16131 /*type_p=*/false,
16132 /*is_declaration=*/false)
16133 != NULL_TREE);
16134 /* Outside of a class-specifier, there must be a
16135 nested-name-specifier. */
16136 if (!nested_name_p &&
16137 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16138 || friend_p))
16139 constructor_p = false;
16140 /* If we still think that this might be a constructor-declarator,
16141 look for a class-name. */
16142 if (constructor_p)
16144 /* If we have:
16146 template <typename T> struct S { S(); };
16147 template <typename T> S<T>::S ();
16149 we must recognize that the nested `S' names a class.
16150 Similarly, for:
16152 template <typename T> S<T>::S<T> ();
16154 we must recognize that the nested `S' names a template. */
16155 type_decl = cp_parser_class_name (parser,
16156 /*typename_keyword_p=*/false,
16157 /*template_keyword_p=*/false,
16158 none_type,
16159 /*check_dependency_p=*/false,
16160 /*class_head_p=*/false,
16161 /*is_declaration=*/false);
16162 /* If there was no class-name, then this is not a constructor. */
16163 constructor_p = !cp_parser_error_occurred (parser);
16166 /* If we're still considering a constructor, we have to see a `(',
16167 to begin the parameter-declaration-clause, followed by either a
16168 `)', an `...', or a decl-specifier. We need to check for a
16169 type-specifier to avoid being fooled into thinking that:
16171 S::S (f) (int);
16173 is a constructor. (It is actually a function named `f' that
16174 takes one parameter (of type `int') and returns a value of type
16175 `S::S'. */
16176 if (constructor_p
16177 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16179 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16180 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16181 /* A parameter declaration begins with a decl-specifier,
16182 which is either the "attribute" keyword, a storage class
16183 specifier, or (usually) a type-specifier. */
16184 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16186 tree type;
16187 tree pushed_scope = NULL_TREE;
16188 unsigned saved_num_template_parameter_lists;
16190 /* Names appearing in the type-specifier should be looked up
16191 in the scope of the class. */
16192 if (current_class_type)
16193 type = NULL_TREE;
16194 else
16196 type = TREE_TYPE (type_decl);
16197 if (TREE_CODE (type) == TYPENAME_TYPE)
16199 type = resolve_typename_type (type,
16200 /*only_current_p=*/false);
16201 if (type == error_mark_node)
16203 cp_parser_abort_tentative_parse (parser);
16204 return false;
16207 pushed_scope = push_scope (type);
16210 /* Inside the constructor parameter list, surrounding
16211 template-parameter-lists do not apply. */
16212 saved_num_template_parameter_lists
16213 = parser->num_template_parameter_lists;
16214 parser->num_template_parameter_lists = 0;
16216 /* Look for the type-specifier. */
16217 cp_parser_type_specifier (parser,
16218 CP_PARSER_FLAGS_NONE,
16219 /*decl_specs=*/NULL,
16220 /*is_declarator=*/true,
16221 /*declares_class_or_enum=*/NULL,
16222 /*is_cv_qualifier=*/NULL);
16224 parser->num_template_parameter_lists
16225 = saved_num_template_parameter_lists;
16227 /* Leave the scope of the class. */
16228 if (pushed_scope)
16229 pop_scope (pushed_scope);
16231 constructor_p = !cp_parser_error_occurred (parser);
16234 else
16235 constructor_p = false;
16236 /* We did not really want to consume any tokens. */
16237 cp_parser_abort_tentative_parse (parser);
16239 return constructor_p;
16242 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16243 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16244 they must be performed once we are in the scope of the function.
16246 Returns the function defined. */
16248 static tree
16249 cp_parser_function_definition_from_specifiers_and_declarator
16250 (cp_parser* parser,
16251 cp_decl_specifier_seq *decl_specifiers,
16252 tree attributes,
16253 const cp_declarator *declarator)
16255 tree fn;
16256 bool success_p;
16258 /* Begin the function-definition. */
16259 success_p = start_function (decl_specifiers, declarator, attributes);
16261 /* The things we're about to see are not directly qualified by any
16262 template headers we've seen thus far. */
16263 reset_specialization ();
16265 /* If there were names looked up in the decl-specifier-seq that we
16266 did not check, check them now. We must wait until we are in the
16267 scope of the function to perform the checks, since the function
16268 might be a friend. */
16269 perform_deferred_access_checks ();
16271 if (!success_p)
16273 /* Skip the entire function. */
16274 cp_parser_skip_to_end_of_block_or_statement (parser);
16275 fn = error_mark_node;
16277 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16279 /* Seen already, skip it. An error message has already been output. */
16280 cp_parser_skip_to_end_of_block_or_statement (parser);
16281 fn = current_function_decl;
16282 current_function_decl = NULL_TREE;
16283 /* If this is a function from a class, pop the nested class. */
16284 if (current_class_name)
16285 pop_nested_class ();
16287 else
16288 fn = cp_parser_function_definition_after_declarator (parser,
16289 /*inline_p=*/false);
16291 return fn;
16294 /* Parse the part of a function-definition that follows the
16295 declarator. INLINE_P is TRUE iff this function is an inline
16296 function defined with a class-specifier.
16298 Returns the function defined. */
16300 static tree
16301 cp_parser_function_definition_after_declarator (cp_parser* parser,
16302 bool inline_p)
16304 tree fn;
16305 bool ctor_initializer_p = false;
16306 bool saved_in_unbraced_linkage_specification_p;
16307 bool saved_in_function_body;
16308 unsigned saved_num_template_parameter_lists;
16310 saved_in_function_body = parser->in_function_body;
16311 parser->in_function_body = true;
16312 /* If the next token is `return', then the code may be trying to
16313 make use of the "named return value" extension that G++ used to
16314 support. */
16315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16317 /* Consume the `return' keyword. */
16318 cp_lexer_consume_token (parser->lexer);
16319 /* Look for the identifier that indicates what value is to be
16320 returned. */
16321 cp_parser_identifier (parser);
16322 /* Issue an error message. */
16323 error ("named return values are no longer supported");
16324 /* Skip tokens until we reach the start of the function body. */
16325 while (true)
16327 cp_token *token = cp_lexer_peek_token (parser->lexer);
16328 if (token->type == CPP_OPEN_BRACE
16329 || token->type == CPP_EOF
16330 || token->type == CPP_PRAGMA_EOL)
16331 break;
16332 cp_lexer_consume_token (parser->lexer);
16335 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16336 anything declared inside `f'. */
16337 saved_in_unbraced_linkage_specification_p
16338 = parser->in_unbraced_linkage_specification_p;
16339 parser->in_unbraced_linkage_specification_p = false;
16340 /* Inside the function, surrounding template-parameter-lists do not
16341 apply. */
16342 saved_num_template_parameter_lists
16343 = parser->num_template_parameter_lists;
16344 parser->num_template_parameter_lists = 0;
16345 /* If the next token is `try', then we are looking at a
16346 function-try-block. */
16347 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16348 ctor_initializer_p = cp_parser_function_try_block (parser);
16349 /* A function-try-block includes the function-body, so we only do
16350 this next part if we're not processing a function-try-block. */
16351 else
16352 ctor_initializer_p
16353 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16355 /* Finish the function. */
16356 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16357 (inline_p ? 2 : 0));
16358 /* Generate code for it, if necessary. */
16359 expand_or_defer_fn (fn);
16360 /* Restore the saved values. */
16361 parser->in_unbraced_linkage_specification_p
16362 = saved_in_unbraced_linkage_specification_p;
16363 parser->num_template_parameter_lists
16364 = saved_num_template_parameter_lists;
16365 parser->in_function_body = saved_in_function_body;
16367 return fn;
16370 /* Parse a template-declaration, assuming that the `export' (and
16371 `extern') keywords, if present, has already been scanned. MEMBER_P
16372 is as for cp_parser_template_declaration. */
16374 static void
16375 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16377 tree decl = NULL_TREE;
16378 VEC (deferred_access_check,gc) *checks;
16379 tree parameter_list;
16380 bool friend_p = false;
16381 bool need_lang_pop;
16383 /* Look for the `template' keyword. */
16384 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16385 return;
16387 /* And the `<'. */
16388 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16389 return;
16390 if (at_class_scope_p () && current_function_decl)
16392 /* 14.5.2.2 [temp.mem]
16394 A local class shall not have member templates. */
16395 error ("invalid declaration of member template in local class");
16396 cp_parser_skip_to_end_of_block_or_statement (parser);
16397 return;
16399 /* [temp]
16401 A template ... shall not have C linkage. */
16402 if (current_lang_name == lang_name_c)
16404 error ("template with C linkage");
16405 /* Give it C++ linkage to avoid confusing other parts of the
16406 front end. */
16407 push_lang_context (lang_name_cplusplus);
16408 need_lang_pop = true;
16410 else
16411 need_lang_pop = false;
16413 /* We cannot perform access checks on the template parameter
16414 declarations until we know what is being declared, just as we
16415 cannot check the decl-specifier list. */
16416 push_deferring_access_checks (dk_deferred);
16418 /* If the next token is `>', then we have an invalid
16419 specialization. Rather than complain about an invalid template
16420 parameter, issue an error message here. */
16421 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16423 cp_parser_error (parser, "invalid explicit specialization");
16424 begin_specialization ();
16425 parameter_list = NULL_TREE;
16427 else
16428 /* Parse the template parameters. */
16429 parameter_list = cp_parser_template_parameter_list (parser);
16431 /* Get the deferred access checks from the parameter list. These
16432 will be checked once we know what is being declared, as for a
16433 member template the checks must be performed in the scope of the
16434 class containing the member. */
16435 checks = get_deferred_access_checks ();
16437 /* Look for the `>'. */
16438 cp_parser_skip_to_end_of_template_parameter_list (parser);
16439 /* We just processed one more parameter list. */
16440 ++parser->num_template_parameter_lists;
16441 /* If the next token is `template', there are more template
16442 parameters. */
16443 if (cp_lexer_next_token_is_keyword (parser->lexer,
16444 RID_TEMPLATE))
16445 cp_parser_template_declaration_after_export (parser, member_p);
16446 else
16448 /* There are no access checks when parsing a template, as we do not
16449 know if a specialization will be a friend. */
16450 push_deferring_access_checks (dk_no_check);
16451 decl = cp_parser_single_declaration (parser,
16452 checks,
16453 member_p,
16454 &friend_p);
16455 pop_deferring_access_checks ();
16457 /* If this is a member template declaration, let the front
16458 end know. */
16459 if (member_p && !friend_p && decl)
16461 if (TREE_CODE (decl) == TYPE_DECL)
16462 cp_parser_check_access_in_redeclaration (decl);
16464 decl = finish_member_template_decl (decl);
16466 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16467 make_friend_class (current_class_type, TREE_TYPE (decl),
16468 /*complain=*/true);
16470 /* We are done with the current parameter list. */
16471 --parser->num_template_parameter_lists;
16473 pop_deferring_access_checks ();
16475 /* Finish up. */
16476 finish_template_decl (parameter_list);
16478 /* Register member declarations. */
16479 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16480 finish_member_declaration (decl);
16481 /* For the erroneous case of a template with C linkage, we pushed an
16482 implicit C++ linkage scope; exit that scope now. */
16483 if (need_lang_pop)
16484 pop_lang_context ();
16485 /* If DECL is a function template, we must return to parse it later.
16486 (Even though there is no definition, there might be default
16487 arguments that need handling.) */
16488 if (member_p && decl
16489 && (TREE_CODE (decl) == FUNCTION_DECL
16490 || DECL_FUNCTION_TEMPLATE_P (decl)))
16491 TREE_VALUE (parser->unparsed_functions_queues)
16492 = tree_cons (NULL_TREE, decl,
16493 TREE_VALUE (parser->unparsed_functions_queues));
16496 /* Perform the deferred access checks from a template-parameter-list.
16497 CHECKS is a TREE_LIST of access checks, as returned by
16498 get_deferred_access_checks. */
16500 static void
16501 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16503 ++processing_template_parmlist;
16504 perform_access_checks (checks);
16505 --processing_template_parmlist;
16508 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16509 `function-definition' sequence. MEMBER_P is true, this declaration
16510 appears in a class scope.
16512 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16513 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16515 static tree
16516 cp_parser_single_declaration (cp_parser* parser,
16517 VEC (deferred_access_check,gc)* checks,
16518 bool member_p,
16519 bool* friend_p)
16521 int declares_class_or_enum;
16522 tree decl = NULL_TREE;
16523 cp_decl_specifier_seq decl_specifiers;
16524 bool function_definition_p = false;
16526 /* This function is only used when processing a template
16527 declaration. */
16528 gcc_assert (innermost_scope_kind () == sk_template_parms
16529 || innermost_scope_kind () == sk_template_spec);
16531 /* Defer access checks until we know what is being declared. */
16532 push_deferring_access_checks (dk_deferred);
16534 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16535 alternative. */
16536 cp_parser_decl_specifier_seq (parser,
16537 CP_PARSER_FLAGS_OPTIONAL,
16538 &decl_specifiers,
16539 &declares_class_or_enum);
16540 if (friend_p)
16541 *friend_p = cp_parser_friend_p (&decl_specifiers);
16543 /* There are no template typedefs. */
16544 if (decl_specifiers.specs[(int) ds_typedef])
16546 error ("template declaration of %qs", "typedef");
16547 decl = error_mark_node;
16550 /* Gather up the access checks that occurred the
16551 decl-specifier-seq. */
16552 stop_deferring_access_checks ();
16554 /* Check for the declaration of a template class. */
16555 if (declares_class_or_enum)
16557 if (cp_parser_declares_only_class_p (parser))
16559 decl = shadow_tag (&decl_specifiers);
16561 /* In this case:
16563 struct C {
16564 friend template <typename T> struct A<T>::B;
16567 A<T>::B will be represented by a TYPENAME_TYPE, and
16568 therefore not recognized by shadow_tag. */
16569 if (friend_p && *friend_p
16570 && !decl
16571 && decl_specifiers.type
16572 && TYPE_P (decl_specifiers.type))
16573 decl = decl_specifiers.type;
16575 if (decl && decl != error_mark_node)
16576 decl = TYPE_NAME (decl);
16577 else
16578 decl = error_mark_node;
16580 /* Perform access checks for template parameters. */
16581 cp_parser_perform_template_parameter_access_checks (checks);
16584 /* If it's not a template class, try for a template function. If
16585 the next token is a `;', then this declaration does not declare
16586 anything. But, if there were errors in the decl-specifiers, then
16587 the error might well have come from an attempted class-specifier.
16588 In that case, there's no need to warn about a missing declarator. */
16589 if (!decl
16590 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16591 || decl_specifiers.type != error_mark_node))
16592 decl = cp_parser_init_declarator (parser,
16593 &decl_specifiers,
16594 checks,
16595 /*function_definition_allowed_p=*/true,
16596 member_p,
16597 declares_class_or_enum,
16598 &function_definition_p);
16600 pop_deferring_access_checks ();
16602 /* Clear any current qualification; whatever comes next is the start
16603 of something new. */
16604 parser->scope = NULL_TREE;
16605 parser->qualifying_scope = NULL_TREE;
16606 parser->object_scope = NULL_TREE;
16607 /* Look for a trailing `;' after the declaration. */
16608 if (!function_definition_p
16609 && (decl == error_mark_node
16610 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16611 cp_parser_skip_to_end_of_block_or_statement (parser);
16613 return decl;
16616 /* Parse a cast-expression that is not the operand of a unary "&". */
16618 static tree
16619 cp_parser_simple_cast_expression (cp_parser *parser)
16621 return cp_parser_cast_expression (parser, /*address_p=*/false,
16622 /*cast_p=*/false);
16625 /* Parse a functional cast to TYPE. Returns an expression
16626 representing the cast. */
16628 static tree
16629 cp_parser_functional_cast (cp_parser* parser, tree type)
16631 tree expression_list;
16632 tree cast;
16634 expression_list
16635 = cp_parser_parenthesized_expression_list (parser, false,
16636 /*cast_p=*/true,
16637 /*allow_expansion_p=*/true,
16638 /*non_constant_p=*/NULL);
16640 cast = build_functional_cast (type, expression_list);
16641 /* [expr.const]/1: In an integral constant expression "only type
16642 conversions to integral or enumeration type can be used". */
16643 if (TREE_CODE (type) == TYPE_DECL)
16644 type = TREE_TYPE (type);
16645 if (cast != error_mark_node
16646 && !cast_valid_in_integral_constant_expression_p (type)
16647 && (cp_parser_non_integral_constant_expression
16648 (parser, "a call to a constructor")))
16649 return error_mark_node;
16650 return cast;
16653 /* Save the tokens that make up the body of a member function defined
16654 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16655 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16656 specifiers applied to the declaration. Returns the FUNCTION_DECL
16657 for the member function. */
16659 static tree
16660 cp_parser_save_member_function_body (cp_parser* parser,
16661 cp_decl_specifier_seq *decl_specifiers,
16662 cp_declarator *declarator,
16663 tree attributes)
16665 cp_token *first;
16666 cp_token *last;
16667 tree fn;
16669 /* Create the function-declaration. */
16670 fn = start_method (decl_specifiers, declarator, attributes);
16671 /* If something went badly wrong, bail out now. */
16672 if (fn == error_mark_node)
16674 /* If there's a function-body, skip it. */
16675 if (cp_parser_token_starts_function_definition_p
16676 (cp_lexer_peek_token (parser->lexer)))
16677 cp_parser_skip_to_end_of_block_or_statement (parser);
16678 return error_mark_node;
16681 /* Remember it, if there default args to post process. */
16682 cp_parser_save_default_args (parser, fn);
16684 /* Save away the tokens that make up the body of the
16685 function. */
16686 first = parser->lexer->next_token;
16687 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16688 /* Handle function try blocks. */
16689 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16690 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16691 last = parser->lexer->next_token;
16693 /* Save away the inline definition; we will process it when the
16694 class is complete. */
16695 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16696 DECL_PENDING_INLINE_P (fn) = 1;
16698 /* We need to know that this was defined in the class, so that
16699 friend templates are handled correctly. */
16700 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16702 /* We're done with the inline definition. */
16703 finish_method (fn);
16705 /* Add FN to the queue of functions to be parsed later. */
16706 TREE_VALUE (parser->unparsed_functions_queues)
16707 = tree_cons (NULL_TREE, fn,
16708 TREE_VALUE (parser->unparsed_functions_queues));
16710 return fn;
16713 /* Parse a template-argument-list, as well as the trailing ">" (but
16714 not the opening ">"). See cp_parser_template_argument_list for the
16715 return value. */
16717 static tree
16718 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16720 tree arguments;
16721 tree saved_scope;
16722 tree saved_qualifying_scope;
16723 tree saved_object_scope;
16724 bool saved_greater_than_is_operator_p;
16725 bool saved_skip_evaluation;
16727 /* [temp.names]
16729 When parsing a template-id, the first non-nested `>' is taken as
16730 the end of the template-argument-list rather than a greater-than
16731 operator. */
16732 saved_greater_than_is_operator_p
16733 = parser->greater_than_is_operator_p;
16734 parser->greater_than_is_operator_p = false;
16735 /* Parsing the argument list may modify SCOPE, so we save it
16736 here. */
16737 saved_scope = parser->scope;
16738 saved_qualifying_scope = parser->qualifying_scope;
16739 saved_object_scope = parser->object_scope;
16740 /* We need to evaluate the template arguments, even though this
16741 template-id may be nested within a "sizeof". */
16742 saved_skip_evaluation = skip_evaluation;
16743 skip_evaluation = false;
16744 /* Parse the template-argument-list itself. */
16745 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
16746 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16747 arguments = NULL_TREE;
16748 else
16749 arguments = cp_parser_template_argument_list (parser);
16750 /* Look for the `>' that ends the template-argument-list. If we find
16751 a '>>' instead, it's probably just a typo. */
16752 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16754 if (flag_cpp0x)
16756 /* In C++0x, a `>>' in a template argument list or cast
16757 expression is considered to be two separate `>'
16758 tokens. So, change the current token to a `>', but don't
16759 consume it: it will be consumed later when the outer
16760 template argument list (or cast expression) is parsed.
16761 Note that this replacement of `>' for `>>' is necessary
16762 even if we are parsing tentatively: in the tentative
16763 case, after calling
16764 cp_parser_enclosed_template_argument_list we will always
16765 throw away all of the template arguments and the first
16766 closing `>', either because the template argument list
16767 was erroneous or because we are replacing those tokens
16768 with a CPP_TEMPLATE_ID token. The second `>' (which will
16769 not have been thrown away) is needed either to close an
16770 outer template argument list or to complete a new-style
16771 cast. */
16772 cp_token *token = cp_lexer_peek_token (parser->lexer);
16773 token->type = CPP_GREATER;
16775 else if (!saved_greater_than_is_operator_p)
16777 /* If we're in a nested template argument list, the '>>' has
16778 to be a typo for '> >'. We emit the error message, but we
16779 continue parsing and we push a '>' as next token, so that
16780 the argument list will be parsed correctly. Note that the
16781 global source location is still on the token before the
16782 '>>', so we need to say explicitly where we want it. */
16783 cp_token *token = cp_lexer_peek_token (parser->lexer);
16784 error ("%H%<>>%> should be %<> >%> "
16785 "within a nested template argument list",
16786 &token->location);
16788 token->type = CPP_GREATER;
16790 else
16792 /* If this is not a nested template argument list, the '>>'
16793 is a typo for '>'. Emit an error message and continue.
16794 Same deal about the token location, but here we can get it
16795 right by consuming the '>>' before issuing the diagnostic. */
16796 cp_lexer_consume_token (parser->lexer);
16797 error ("spurious %<>>%>, use %<>%> to terminate "
16798 "a template argument list");
16801 else
16802 cp_parser_skip_to_end_of_template_parameter_list (parser);
16803 /* The `>' token might be a greater-than operator again now. */
16804 parser->greater_than_is_operator_p
16805 = saved_greater_than_is_operator_p;
16806 /* Restore the SAVED_SCOPE. */
16807 parser->scope = saved_scope;
16808 parser->qualifying_scope = saved_qualifying_scope;
16809 parser->object_scope = saved_object_scope;
16810 skip_evaluation = saved_skip_evaluation;
16812 return arguments;
16815 /* MEMBER_FUNCTION is a member function, or a friend. If default
16816 arguments, or the body of the function have not yet been parsed,
16817 parse them now. */
16819 static void
16820 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16822 /* If this member is a template, get the underlying
16823 FUNCTION_DECL. */
16824 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16825 member_function = DECL_TEMPLATE_RESULT (member_function);
16827 /* There should not be any class definitions in progress at this
16828 point; the bodies of members are only parsed outside of all class
16829 definitions. */
16830 gcc_assert (parser->num_classes_being_defined == 0);
16831 /* While we're parsing the member functions we might encounter more
16832 classes. We want to handle them right away, but we don't want
16833 them getting mixed up with functions that are currently in the
16834 queue. */
16835 parser->unparsed_functions_queues
16836 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16838 /* Make sure that any template parameters are in scope. */
16839 maybe_begin_member_template_processing (member_function);
16841 /* If the body of the function has not yet been parsed, parse it
16842 now. */
16843 if (DECL_PENDING_INLINE_P (member_function))
16845 tree function_scope;
16846 cp_token_cache *tokens;
16848 /* The function is no longer pending; we are processing it. */
16849 tokens = DECL_PENDING_INLINE_INFO (member_function);
16850 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16851 DECL_PENDING_INLINE_P (member_function) = 0;
16853 /* If this is a local class, enter the scope of the containing
16854 function. */
16855 function_scope = current_function_decl;
16856 if (function_scope)
16857 push_function_context_to (function_scope);
16860 /* Push the body of the function onto the lexer stack. */
16861 cp_parser_push_lexer_for_tokens (parser, tokens);
16863 /* Let the front end know that we going to be defining this
16864 function. */
16865 start_preparsed_function (member_function, NULL_TREE,
16866 SF_PRE_PARSED | SF_INCLASS_INLINE);
16868 /* Don't do access checking if it is a templated function. */
16869 if (processing_template_decl)
16870 push_deferring_access_checks (dk_no_check);
16872 /* Now, parse the body of the function. */
16873 cp_parser_function_definition_after_declarator (parser,
16874 /*inline_p=*/true);
16876 if (processing_template_decl)
16877 pop_deferring_access_checks ();
16879 /* Leave the scope of the containing function. */
16880 if (function_scope)
16881 pop_function_context_from (function_scope);
16882 cp_parser_pop_lexer (parser);
16885 /* Remove any template parameters from the symbol table. */
16886 maybe_end_member_template_processing ();
16888 /* Restore the queue. */
16889 parser->unparsed_functions_queues
16890 = TREE_CHAIN (parser->unparsed_functions_queues);
16893 /* If DECL contains any default args, remember it on the unparsed
16894 functions queue. */
16896 static void
16897 cp_parser_save_default_args (cp_parser* parser, tree decl)
16899 tree probe;
16901 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16902 probe;
16903 probe = TREE_CHAIN (probe))
16904 if (TREE_PURPOSE (probe))
16906 TREE_PURPOSE (parser->unparsed_functions_queues)
16907 = tree_cons (current_class_type, decl,
16908 TREE_PURPOSE (parser->unparsed_functions_queues));
16909 break;
16913 /* FN is a FUNCTION_DECL which may contains a parameter with an
16914 unparsed DEFAULT_ARG. Parse the default args now. This function
16915 assumes that the current scope is the scope in which the default
16916 argument should be processed. */
16918 static void
16919 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16921 bool saved_local_variables_forbidden_p;
16922 tree parm;
16924 /* While we're parsing the default args, we might (due to the
16925 statement expression extension) encounter more classes. We want
16926 to handle them right away, but we don't want them getting mixed
16927 up with default args that are currently in the queue. */
16928 parser->unparsed_functions_queues
16929 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16931 /* Local variable names (and the `this' keyword) may not appear
16932 in a default argument. */
16933 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16934 parser->local_variables_forbidden_p = true;
16936 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16937 parm;
16938 parm = TREE_CHAIN (parm))
16940 cp_token_cache *tokens;
16941 tree default_arg = TREE_PURPOSE (parm);
16942 tree parsed_arg;
16943 VEC(tree,gc) *insts;
16944 tree copy;
16945 unsigned ix;
16947 if (!default_arg)
16948 continue;
16950 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16951 /* This can happen for a friend declaration for a function
16952 already declared with default arguments. */
16953 continue;
16955 /* Push the saved tokens for the default argument onto the parser's
16956 lexer stack. */
16957 tokens = DEFARG_TOKENS (default_arg);
16958 cp_parser_push_lexer_for_tokens (parser, tokens);
16960 /* Parse the assignment-expression. */
16961 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16963 if (!processing_template_decl)
16964 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16966 TREE_PURPOSE (parm) = parsed_arg;
16968 /* Update any instantiations we've already created. */
16969 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16970 VEC_iterate (tree, insts, ix, copy); ix++)
16971 TREE_PURPOSE (copy) = parsed_arg;
16973 /* If the token stream has not been completely used up, then
16974 there was extra junk after the end of the default
16975 argument. */
16976 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16977 cp_parser_error (parser, "expected %<,%>");
16979 /* Revert to the main lexer. */
16980 cp_parser_pop_lexer (parser);
16983 /* Make sure no default arg is missing. */
16984 check_default_args (fn);
16986 /* Restore the state of local_variables_forbidden_p. */
16987 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16989 /* Restore the queue. */
16990 parser->unparsed_functions_queues
16991 = TREE_CHAIN (parser->unparsed_functions_queues);
16994 /* Parse the operand of `sizeof' (or a similar operator). Returns
16995 either a TYPE or an expression, depending on the form of the
16996 input. The KEYWORD indicates which kind of expression we have
16997 encountered. */
16999 static tree
17000 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17002 static const char *format;
17003 tree expr = NULL_TREE;
17004 const char *saved_message;
17005 bool saved_integral_constant_expression_p;
17006 bool saved_non_integral_constant_expression_p;
17007 bool pack_expansion_p = false;
17009 /* Initialize FORMAT the first time we get here. */
17010 if (!format)
17011 format = "types may not be defined in '%s' expressions";
17013 /* Types cannot be defined in a `sizeof' expression. Save away the
17014 old message. */
17015 saved_message = parser->type_definition_forbidden_message;
17016 /* And create the new one. */
17017 parser->type_definition_forbidden_message
17018 = XNEWVEC (const char, strlen (format)
17019 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17020 + 1 /* `\0' */);
17021 sprintf ((char *) parser->type_definition_forbidden_message,
17022 format, IDENTIFIER_POINTER (ridpointers[keyword]));
17024 /* The restrictions on constant-expressions do not apply inside
17025 sizeof expressions. */
17026 saved_integral_constant_expression_p
17027 = parser->integral_constant_expression_p;
17028 saved_non_integral_constant_expression_p
17029 = parser->non_integral_constant_expression_p;
17030 parser->integral_constant_expression_p = false;
17032 /* If it's a `...', then we are computing the length of a parameter
17033 pack. */
17034 if (keyword == RID_SIZEOF
17035 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17037 /* Consume the `...'. */
17038 cp_lexer_consume_token (parser->lexer);
17039 maybe_warn_variadic_templates ();
17041 /* Note that this is an expansion. */
17042 pack_expansion_p = true;
17045 /* Do not actually evaluate the expression. */
17046 ++skip_evaluation;
17047 /* If it's a `(', then we might be looking at the type-id
17048 construction. */
17049 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17051 tree type;
17052 bool saved_in_type_id_in_expr_p;
17054 /* We can't be sure yet whether we're looking at a type-id or an
17055 expression. */
17056 cp_parser_parse_tentatively (parser);
17057 /* Consume the `('. */
17058 cp_lexer_consume_token (parser->lexer);
17059 /* Parse the type-id. */
17060 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17061 parser->in_type_id_in_expr_p = true;
17062 type = cp_parser_type_id (parser);
17063 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17064 /* Now, look for the trailing `)'. */
17065 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17066 /* If all went well, then we're done. */
17067 if (cp_parser_parse_definitely (parser))
17069 cp_decl_specifier_seq decl_specs;
17071 /* Build a trivial decl-specifier-seq. */
17072 clear_decl_specs (&decl_specs);
17073 decl_specs.type = type;
17075 /* Call grokdeclarator to figure out what type this is. */
17076 expr = grokdeclarator (NULL,
17077 &decl_specs,
17078 TYPENAME,
17079 /*initialized=*/0,
17080 /*attrlist=*/NULL);
17084 /* If the type-id production did not work out, then we must be
17085 looking at the unary-expression production. */
17086 if (!expr)
17087 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17088 /*cast_p=*/false);
17090 if (pack_expansion_p)
17091 /* Build a pack expansion. */
17092 expr = make_pack_expansion (expr);
17094 /* Go back to evaluating expressions. */
17095 --skip_evaluation;
17097 /* Free the message we created. */
17098 free ((char *) parser->type_definition_forbidden_message);
17099 /* And restore the old one. */
17100 parser->type_definition_forbidden_message = saved_message;
17101 parser->integral_constant_expression_p
17102 = saved_integral_constant_expression_p;
17103 parser->non_integral_constant_expression_p
17104 = saved_non_integral_constant_expression_p;
17106 return expr;
17109 /* If the current declaration has no declarator, return true. */
17111 static bool
17112 cp_parser_declares_only_class_p (cp_parser *parser)
17114 /* If the next token is a `;' or a `,' then there is no
17115 declarator. */
17116 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17117 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17120 /* Update the DECL_SPECS to reflect the storage class indicated by
17121 KEYWORD. */
17123 static void
17124 cp_parser_set_storage_class (cp_parser *parser,
17125 cp_decl_specifier_seq *decl_specs,
17126 enum rid keyword)
17128 cp_storage_class storage_class;
17130 if (parser->in_unbraced_linkage_specification_p)
17132 error ("invalid use of %qD in linkage specification",
17133 ridpointers[keyword]);
17134 return;
17136 else if (decl_specs->storage_class != sc_none)
17138 decl_specs->conflicting_specifiers_p = true;
17139 return;
17142 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17143 && decl_specs->specs[(int) ds_thread])
17145 error ("%<__thread%> before %qD", ridpointers[keyword]);
17146 decl_specs->specs[(int) ds_thread] = 0;
17149 switch (keyword)
17151 case RID_AUTO:
17152 storage_class = sc_auto;
17153 break;
17154 case RID_REGISTER:
17155 storage_class = sc_register;
17156 break;
17157 case RID_STATIC:
17158 storage_class = sc_static;
17159 break;
17160 case RID_EXTERN:
17161 storage_class = sc_extern;
17162 break;
17163 case RID_MUTABLE:
17164 storage_class = sc_mutable;
17165 break;
17166 default:
17167 gcc_unreachable ();
17169 decl_specs->storage_class = storage_class;
17171 /* A storage class specifier cannot be applied alongside a typedef
17172 specifier. If there is a typedef specifier present then set
17173 conflicting_specifiers_p which will trigger an error later
17174 on in grokdeclarator. */
17175 if (decl_specs->specs[(int)ds_typedef])
17176 decl_specs->conflicting_specifiers_p = true;
17179 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17180 is true, the type is a user-defined type; otherwise it is a
17181 built-in type specified by a keyword. */
17183 static void
17184 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17185 tree type_spec,
17186 bool user_defined_p)
17188 decl_specs->any_specifiers_p = true;
17190 /* If the user tries to redeclare bool or wchar_t (with, for
17191 example, in "typedef int wchar_t;") we remember that this is what
17192 happened. In system headers, we ignore these declarations so
17193 that G++ can work with system headers that are not C++-safe. */
17194 if (decl_specs->specs[(int) ds_typedef]
17195 && !user_defined_p
17196 && (type_spec == boolean_type_node
17197 || type_spec == wchar_type_node)
17198 && (decl_specs->type
17199 || decl_specs->specs[(int) ds_long]
17200 || decl_specs->specs[(int) ds_short]
17201 || decl_specs->specs[(int) ds_unsigned]
17202 || decl_specs->specs[(int) ds_signed]))
17204 decl_specs->redefined_builtin_type = type_spec;
17205 if (!decl_specs->type)
17207 decl_specs->type = type_spec;
17208 decl_specs->user_defined_type_p = false;
17211 else if (decl_specs->type)
17212 decl_specs->multiple_types_p = true;
17213 else
17215 decl_specs->type = type_spec;
17216 decl_specs->user_defined_type_p = user_defined_p;
17217 decl_specs->redefined_builtin_type = NULL_TREE;
17221 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17222 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17224 static bool
17225 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17227 return decl_specifiers->specs[(int) ds_friend] != 0;
17230 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17231 issue an error message indicating that TOKEN_DESC was expected.
17233 Returns the token consumed, if the token had the appropriate type.
17234 Otherwise, returns NULL. */
17236 static cp_token *
17237 cp_parser_require (cp_parser* parser,
17238 enum cpp_ttype type,
17239 const char* token_desc)
17241 if (cp_lexer_next_token_is (parser->lexer, type))
17242 return cp_lexer_consume_token (parser->lexer);
17243 else
17245 /* Output the MESSAGE -- unless we're parsing tentatively. */
17246 if (!cp_parser_simulate_error (parser))
17248 char *message = concat ("expected ", token_desc, NULL);
17249 cp_parser_error (parser, message);
17250 free (message);
17252 return NULL;
17256 /* An error message is produced if the next token is not '>'.
17257 All further tokens are skipped until the desired token is
17258 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17260 static void
17261 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17263 /* Current level of '< ... >'. */
17264 unsigned level = 0;
17265 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17266 unsigned nesting_depth = 0;
17268 /* Are we ready, yet? If not, issue error message. */
17269 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17270 return;
17272 /* Skip tokens until the desired token is found. */
17273 while (true)
17275 /* Peek at the next token. */
17276 switch (cp_lexer_peek_token (parser->lexer)->type)
17278 case CPP_LESS:
17279 if (!nesting_depth)
17280 ++level;
17281 break;
17283 case CPP_RSHIFT:
17284 if (!flag_cpp0x)
17285 /* C++0x views the `>>' operator as two `>' tokens, but
17286 C++98 does not. */
17287 break;
17288 else if (!nesting_depth && level-- == 0)
17290 /* We've hit a `>>' where the first `>' closes the
17291 template argument list, and the second `>' is
17292 spurious. Just consume the `>>' and stop; we've
17293 already produced at least one error. */
17294 cp_lexer_consume_token (parser->lexer);
17295 return;
17297 /* Fall through for C++0x, so we handle the second `>' in
17298 the `>>'. */
17300 case CPP_GREATER:
17301 if (!nesting_depth && level-- == 0)
17303 /* We've reached the token we want, consume it and stop. */
17304 cp_lexer_consume_token (parser->lexer);
17305 return;
17307 break;
17309 case CPP_OPEN_PAREN:
17310 case CPP_OPEN_SQUARE:
17311 ++nesting_depth;
17312 break;
17314 case CPP_CLOSE_PAREN:
17315 case CPP_CLOSE_SQUARE:
17316 if (nesting_depth-- == 0)
17317 return;
17318 break;
17320 case CPP_EOF:
17321 case CPP_PRAGMA_EOL:
17322 case CPP_SEMICOLON:
17323 case CPP_OPEN_BRACE:
17324 case CPP_CLOSE_BRACE:
17325 /* The '>' was probably forgotten, don't look further. */
17326 return;
17328 default:
17329 break;
17332 /* Consume this token. */
17333 cp_lexer_consume_token (parser->lexer);
17337 /* If the next token is the indicated keyword, consume it. Otherwise,
17338 issue an error message indicating that TOKEN_DESC was expected.
17340 Returns the token consumed, if the token had the appropriate type.
17341 Otherwise, returns NULL. */
17343 static cp_token *
17344 cp_parser_require_keyword (cp_parser* parser,
17345 enum rid keyword,
17346 const char* token_desc)
17348 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17350 if (token && token->keyword != keyword)
17352 dyn_string_t error_msg;
17354 /* Format the error message. */
17355 error_msg = dyn_string_new (0);
17356 dyn_string_append_cstr (error_msg, "expected ");
17357 dyn_string_append_cstr (error_msg, token_desc);
17358 cp_parser_error (parser, error_msg->s);
17359 dyn_string_delete (error_msg);
17360 return NULL;
17363 return token;
17366 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17367 function-definition. */
17369 static bool
17370 cp_parser_token_starts_function_definition_p (cp_token* token)
17372 return (/* An ordinary function-body begins with an `{'. */
17373 token->type == CPP_OPEN_BRACE
17374 /* A ctor-initializer begins with a `:'. */
17375 || token->type == CPP_COLON
17376 /* A function-try-block begins with `try'. */
17377 || token->keyword == RID_TRY
17378 /* The named return value extension begins with `return'. */
17379 || token->keyword == RID_RETURN);
17382 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17383 definition. */
17385 static bool
17386 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17388 cp_token *token;
17390 token = cp_lexer_peek_token (parser->lexer);
17391 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17394 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17395 C++0x) ending a template-argument. */
17397 static bool
17398 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17400 cp_token *token;
17402 token = cp_lexer_peek_token (parser->lexer);
17403 return (token->type == CPP_COMMA
17404 || token->type == CPP_GREATER
17405 || token->type == CPP_ELLIPSIS
17406 || (flag_cpp0x && token->type == CPP_RSHIFT));
17409 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17410 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17412 static bool
17413 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17414 size_t n)
17416 cp_token *token;
17418 token = cp_lexer_peek_nth_token (parser->lexer, n);
17419 if (token->type == CPP_LESS)
17420 return true;
17421 /* Check for the sequence `<::' in the original code. It would be lexed as
17422 `[:', where `[' is a digraph, and there is no whitespace before
17423 `:'. */
17424 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17426 cp_token *token2;
17427 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17428 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17429 return true;
17431 return false;
17434 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17435 or none_type otherwise. */
17437 static enum tag_types
17438 cp_parser_token_is_class_key (cp_token* token)
17440 switch (token->keyword)
17442 case RID_CLASS:
17443 return class_type;
17444 case RID_STRUCT:
17445 return record_type;
17446 case RID_UNION:
17447 return union_type;
17449 default:
17450 return none_type;
17454 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17456 static void
17457 cp_parser_check_class_key (enum tag_types class_key, tree type)
17459 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17460 pedwarn ("%qs tag used in naming %q#T",
17461 class_key == union_type ? "union"
17462 : class_key == record_type ? "struct" : "class",
17463 type);
17466 /* Issue an error message if DECL is redeclared with different
17467 access than its original declaration [class.access.spec/3].
17468 This applies to nested classes and nested class templates.
17469 [class.mem/1]. */
17471 static void
17472 cp_parser_check_access_in_redeclaration (tree decl)
17474 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17475 return;
17477 if ((TREE_PRIVATE (decl)
17478 != (current_access_specifier == access_private_node))
17479 || (TREE_PROTECTED (decl)
17480 != (current_access_specifier == access_protected_node)))
17481 error ("%qD redeclared with different access", decl);
17484 /* Look for the `template' keyword, as a syntactic disambiguator.
17485 Return TRUE iff it is present, in which case it will be
17486 consumed. */
17488 static bool
17489 cp_parser_optional_template_keyword (cp_parser *parser)
17491 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17493 /* The `template' keyword can only be used within templates;
17494 outside templates the parser can always figure out what is a
17495 template and what is not. */
17496 if (!processing_template_decl)
17498 error ("%<template%> (as a disambiguator) is only allowed "
17499 "within templates");
17500 /* If this part of the token stream is rescanned, the same
17501 error message would be generated. So, we purge the token
17502 from the stream. */
17503 cp_lexer_purge_token (parser->lexer);
17504 return false;
17506 else
17508 /* Consume the `template' keyword. */
17509 cp_lexer_consume_token (parser->lexer);
17510 return true;
17514 return false;
17517 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17518 set PARSER->SCOPE, and perform other related actions. */
17520 static void
17521 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17523 int i;
17524 struct tree_check *check_value;
17525 deferred_access_check *chk;
17526 VEC (deferred_access_check,gc) *checks;
17528 /* Get the stored value. */
17529 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17530 /* Perform any access checks that were deferred. */
17531 checks = check_value->checks;
17532 if (checks)
17534 for (i = 0 ;
17535 VEC_iterate (deferred_access_check, checks, i, chk) ;
17536 ++i)
17538 perform_or_defer_access_check (chk->binfo,
17539 chk->decl,
17540 chk->diag_decl);
17543 /* Set the scope from the stored value. */
17544 parser->scope = check_value->value;
17545 parser->qualifying_scope = check_value->qualifying_scope;
17546 parser->object_scope = NULL_TREE;
17549 /* Consume tokens up through a non-nested END token. */
17551 static void
17552 cp_parser_cache_group (cp_parser *parser,
17553 enum cpp_ttype end,
17554 unsigned depth)
17556 while (true)
17558 cp_token *token;
17560 /* Abort a parenthesized expression if we encounter a brace. */
17561 if ((end == CPP_CLOSE_PAREN || depth == 0)
17562 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17563 return;
17564 /* If we've reached the end of the file, stop. */
17565 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17566 || (end != CPP_PRAGMA_EOL
17567 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17568 return;
17569 /* Consume the next token. */
17570 token = cp_lexer_consume_token (parser->lexer);
17571 /* See if it starts a new group. */
17572 if (token->type == CPP_OPEN_BRACE)
17574 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17575 if (depth == 0)
17576 return;
17578 else if (token->type == CPP_OPEN_PAREN)
17579 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17580 else if (token->type == CPP_PRAGMA)
17581 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17582 else if (token->type == end)
17583 return;
17587 /* Begin parsing tentatively. We always save tokens while parsing
17588 tentatively so that if the tentative parsing fails we can restore the
17589 tokens. */
17591 static void
17592 cp_parser_parse_tentatively (cp_parser* parser)
17594 /* Enter a new parsing context. */
17595 parser->context = cp_parser_context_new (parser->context);
17596 /* Begin saving tokens. */
17597 cp_lexer_save_tokens (parser->lexer);
17598 /* In order to avoid repetitive access control error messages,
17599 access checks are queued up until we are no longer parsing
17600 tentatively. */
17601 push_deferring_access_checks (dk_deferred);
17604 /* Commit to the currently active tentative parse. */
17606 static void
17607 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17609 cp_parser_context *context;
17610 cp_lexer *lexer;
17612 /* Mark all of the levels as committed. */
17613 lexer = parser->lexer;
17614 for (context = parser->context; context->next; context = context->next)
17616 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17617 break;
17618 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17619 while (!cp_lexer_saving_tokens (lexer))
17620 lexer = lexer->next;
17621 cp_lexer_commit_tokens (lexer);
17625 /* Abort the currently active tentative parse. All consumed tokens
17626 will be rolled back, and no diagnostics will be issued. */
17628 static void
17629 cp_parser_abort_tentative_parse (cp_parser* parser)
17631 cp_parser_simulate_error (parser);
17632 /* Now, pretend that we want to see if the construct was
17633 successfully parsed. */
17634 cp_parser_parse_definitely (parser);
17637 /* Stop parsing tentatively. If a parse error has occurred, restore the
17638 token stream. Otherwise, commit to the tokens we have consumed.
17639 Returns true if no error occurred; false otherwise. */
17641 static bool
17642 cp_parser_parse_definitely (cp_parser* parser)
17644 bool error_occurred;
17645 cp_parser_context *context;
17647 /* Remember whether or not an error occurred, since we are about to
17648 destroy that information. */
17649 error_occurred = cp_parser_error_occurred (parser);
17650 /* Remove the topmost context from the stack. */
17651 context = parser->context;
17652 parser->context = context->next;
17653 /* If no parse errors occurred, commit to the tentative parse. */
17654 if (!error_occurred)
17656 /* Commit to the tokens read tentatively, unless that was
17657 already done. */
17658 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17659 cp_lexer_commit_tokens (parser->lexer);
17661 pop_to_parent_deferring_access_checks ();
17663 /* Otherwise, if errors occurred, roll back our state so that things
17664 are just as they were before we began the tentative parse. */
17665 else
17667 cp_lexer_rollback_tokens (parser->lexer);
17668 pop_deferring_access_checks ();
17670 /* Add the context to the front of the free list. */
17671 context->next = cp_parser_context_free_list;
17672 cp_parser_context_free_list = context;
17674 return !error_occurred;
17677 /* Returns true if we are parsing tentatively and are not committed to
17678 this tentative parse. */
17680 static bool
17681 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17683 return (cp_parser_parsing_tentatively (parser)
17684 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17687 /* Returns nonzero iff an error has occurred during the most recent
17688 tentative parse. */
17690 static bool
17691 cp_parser_error_occurred (cp_parser* parser)
17693 return (cp_parser_parsing_tentatively (parser)
17694 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17697 /* Returns nonzero if GNU extensions are allowed. */
17699 static bool
17700 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17702 return parser->allow_gnu_extensions_p;
17705 /* Objective-C++ Productions */
17708 /* Parse an Objective-C expression, which feeds into a primary-expression
17709 above.
17711 objc-expression:
17712 objc-message-expression
17713 objc-string-literal
17714 objc-encode-expression
17715 objc-protocol-expression
17716 objc-selector-expression
17718 Returns a tree representation of the expression. */
17720 static tree
17721 cp_parser_objc_expression (cp_parser* parser)
17723 /* Try to figure out what kind of declaration is present. */
17724 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17726 switch (kwd->type)
17728 case CPP_OPEN_SQUARE:
17729 return cp_parser_objc_message_expression (parser);
17731 case CPP_OBJC_STRING:
17732 kwd = cp_lexer_consume_token (parser->lexer);
17733 return objc_build_string_object (kwd->u.value);
17735 case CPP_KEYWORD:
17736 switch (kwd->keyword)
17738 case RID_AT_ENCODE:
17739 return cp_parser_objc_encode_expression (parser);
17741 case RID_AT_PROTOCOL:
17742 return cp_parser_objc_protocol_expression (parser);
17744 case RID_AT_SELECTOR:
17745 return cp_parser_objc_selector_expression (parser);
17747 default:
17748 break;
17750 default:
17751 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17752 cp_parser_skip_to_end_of_block_or_statement (parser);
17755 return error_mark_node;
17758 /* Parse an Objective-C message expression.
17760 objc-message-expression:
17761 [ objc-message-receiver objc-message-args ]
17763 Returns a representation of an Objective-C message. */
17765 static tree
17766 cp_parser_objc_message_expression (cp_parser* parser)
17768 tree receiver, messageargs;
17770 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17771 receiver = cp_parser_objc_message_receiver (parser);
17772 messageargs = cp_parser_objc_message_args (parser);
17773 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17775 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17778 /* Parse an objc-message-receiver.
17780 objc-message-receiver:
17781 expression
17782 simple-type-specifier
17784 Returns a representation of the type or expression. */
17786 static tree
17787 cp_parser_objc_message_receiver (cp_parser* parser)
17789 tree rcv;
17791 /* An Objective-C message receiver may be either (1) a type
17792 or (2) an expression. */
17793 cp_parser_parse_tentatively (parser);
17794 rcv = cp_parser_expression (parser, false);
17796 if (cp_parser_parse_definitely (parser))
17797 return rcv;
17799 rcv = cp_parser_simple_type_specifier (parser,
17800 /*decl_specs=*/NULL,
17801 CP_PARSER_FLAGS_NONE);
17803 return objc_get_class_reference (rcv);
17806 /* Parse the arguments and selectors comprising an Objective-C message.
17808 objc-message-args:
17809 objc-selector
17810 objc-selector-args
17811 objc-selector-args , objc-comma-args
17813 objc-selector-args:
17814 objc-selector [opt] : assignment-expression
17815 objc-selector-args objc-selector [opt] : assignment-expression
17817 objc-comma-args:
17818 assignment-expression
17819 objc-comma-args , assignment-expression
17821 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17822 selector arguments and TREE_VALUE containing a list of comma
17823 arguments. */
17825 static tree
17826 cp_parser_objc_message_args (cp_parser* parser)
17828 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17829 bool maybe_unary_selector_p = true;
17830 cp_token *token = cp_lexer_peek_token (parser->lexer);
17832 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17834 tree selector = NULL_TREE, arg;
17836 if (token->type != CPP_COLON)
17837 selector = cp_parser_objc_selector (parser);
17839 /* Detect if we have a unary selector. */
17840 if (maybe_unary_selector_p
17841 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17842 return build_tree_list (selector, NULL_TREE);
17844 maybe_unary_selector_p = false;
17845 cp_parser_require (parser, CPP_COLON, "`:'");
17846 arg = cp_parser_assignment_expression (parser, false);
17848 sel_args
17849 = chainon (sel_args,
17850 build_tree_list (selector, arg));
17852 token = cp_lexer_peek_token (parser->lexer);
17855 /* Handle non-selector arguments, if any. */
17856 while (token->type == CPP_COMMA)
17858 tree arg;
17860 cp_lexer_consume_token (parser->lexer);
17861 arg = cp_parser_assignment_expression (parser, false);
17863 addl_args
17864 = chainon (addl_args,
17865 build_tree_list (NULL_TREE, arg));
17867 token = cp_lexer_peek_token (parser->lexer);
17870 return build_tree_list (sel_args, addl_args);
17873 /* Parse an Objective-C encode expression.
17875 objc-encode-expression:
17876 @encode objc-typename
17878 Returns an encoded representation of the type argument. */
17880 static tree
17881 cp_parser_objc_encode_expression (cp_parser* parser)
17883 tree type;
17885 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17886 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17887 type = complete_type (cp_parser_type_id (parser));
17888 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17890 if (!type)
17892 error ("%<@encode%> must specify a type as an argument");
17893 return error_mark_node;
17896 return objc_build_encode_expr (type);
17899 /* Parse an Objective-C @defs expression. */
17901 static tree
17902 cp_parser_objc_defs_expression (cp_parser *parser)
17904 tree name;
17906 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17907 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17908 name = cp_parser_identifier (parser);
17909 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17911 return objc_get_class_ivars (name);
17914 /* Parse an Objective-C protocol expression.
17916 objc-protocol-expression:
17917 @protocol ( identifier )
17919 Returns a representation of the protocol expression. */
17921 static tree
17922 cp_parser_objc_protocol_expression (cp_parser* parser)
17924 tree proto;
17926 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17927 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17928 proto = cp_parser_identifier (parser);
17929 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17931 return objc_build_protocol_expr (proto);
17934 /* Parse an Objective-C selector expression.
17936 objc-selector-expression:
17937 @selector ( objc-method-signature )
17939 objc-method-signature:
17940 objc-selector
17941 objc-selector-seq
17943 objc-selector-seq:
17944 objc-selector :
17945 objc-selector-seq objc-selector :
17947 Returns a representation of the method selector. */
17949 static tree
17950 cp_parser_objc_selector_expression (cp_parser* parser)
17952 tree sel_seq = NULL_TREE;
17953 bool maybe_unary_selector_p = true;
17954 cp_token *token;
17956 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17957 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17958 token = cp_lexer_peek_token (parser->lexer);
17960 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17961 || token->type == CPP_SCOPE)
17963 tree selector = NULL_TREE;
17965 if (token->type != CPP_COLON
17966 || token->type == CPP_SCOPE)
17967 selector = cp_parser_objc_selector (parser);
17969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17970 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17972 /* Detect if we have a unary selector. */
17973 if (maybe_unary_selector_p)
17975 sel_seq = selector;
17976 goto finish_selector;
17978 else
17980 cp_parser_error (parser, "expected %<:%>");
17983 maybe_unary_selector_p = false;
17984 token = cp_lexer_consume_token (parser->lexer);
17986 if (token->type == CPP_SCOPE)
17988 sel_seq
17989 = chainon (sel_seq,
17990 build_tree_list (selector, NULL_TREE));
17991 sel_seq
17992 = chainon (sel_seq,
17993 build_tree_list (NULL_TREE, NULL_TREE));
17995 else
17996 sel_seq
17997 = chainon (sel_seq,
17998 build_tree_list (selector, NULL_TREE));
18000 token = cp_lexer_peek_token (parser->lexer);
18003 finish_selector:
18004 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18006 return objc_build_selector_expr (sel_seq);
18009 /* Parse a list of identifiers.
18011 objc-identifier-list:
18012 identifier
18013 objc-identifier-list , identifier
18015 Returns a TREE_LIST of identifier nodes. */
18017 static tree
18018 cp_parser_objc_identifier_list (cp_parser* parser)
18020 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18021 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18023 while (sep->type == CPP_COMMA)
18025 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18026 list = chainon (list,
18027 build_tree_list (NULL_TREE,
18028 cp_parser_identifier (parser)));
18029 sep = cp_lexer_peek_token (parser->lexer);
18032 return list;
18035 /* Parse an Objective-C alias declaration.
18037 objc-alias-declaration:
18038 @compatibility_alias identifier identifier ;
18040 This function registers the alias mapping with the Objective-C front end.
18041 It returns nothing. */
18043 static void
18044 cp_parser_objc_alias_declaration (cp_parser* parser)
18046 tree alias, orig;
18048 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18049 alias = cp_parser_identifier (parser);
18050 orig = cp_parser_identifier (parser);
18051 objc_declare_alias (alias, orig);
18052 cp_parser_consume_semicolon_at_end_of_statement (parser);
18055 /* Parse an Objective-C class forward-declaration.
18057 objc-class-declaration:
18058 @class objc-identifier-list ;
18060 The function registers the forward declarations with the Objective-C
18061 front end. It returns nothing. */
18063 static void
18064 cp_parser_objc_class_declaration (cp_parser* parser)
18066 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18067 objc_declare_class (cp_parser_objc_identifier_list (parser));
18068 cp_parser_consume_semicolon_at_end_of_statement (parser);
18071 /* Parse a list of Objective-C protocol references.
18073 objc-protocol-refs-opt:
18074 objc-protocol-refs [opt]
18076 objc-protocol-refs:
18077 < objc-identifier-list >
18079 Returns a TREE_LIST of identifiers, if any. */
18081 static tree
18082 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18084 tree protorefs = NULL_TREE;
18086 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18088 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18089 protorefs = cp_parser_objc_identifier_list (parser);
18090 cp_parser_require (parser, CPP_GREATER, "`>'");
18093 return protorefs;
18096 /* Parse a Objective-C visibility specification. */
18098 static void
18099 cp_parser_objc_visibility_spec (cp_parser* parser)
18101 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18103 switch (vis->keyword)
18105 case RID_AT_PRIVATE:
18106 objc_set_visibility (2);
18107 break;
18108 case RID_AT_PROTECTED:
18109 objc_set_visibility (0);
18110 break;
18111 case RID_AT_PUBLIC:
18112 objc_set_visibility (1);
18113 break;
18114 default:
18115 return;
18118 /* Eat '@private'/'@protected'/'@public'. */
18119 cp_lexer_consume_token (parser->lexer);
18122 /* Parse an Objective-C method type. */
18124 static void
18125 cp_parser_objc_method_type (cp_parser* parser)
18127 objc_set_method_type
18128 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18129 ? PLUS_EXPR
18130 : MINUS_EXPR);
18133 /* Parse an Objective-C protocol qualifier. */
18135 static tree
18136 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18138 tree quals = NULL_TREE, node;
18139 cp_token *token = cp_lexer_peek_token (parser->lexer);
18141 node = token->u.value;
18143 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18144 && (node == ridpointers [(int) RID_IN]
18145 || node == ridpointers [(int) RID_OUT]
18146 || node == ridpointers [(int) RID_INOUT]
18147 || node == ridpointers [(int) RID_BYCOPY]
18148 || node == ridpointers [(int) RID_BYREF]
18149 || node == ridpointers [(int) RID_ONEWAY]))
18151 quals = tree_cons (NULL_TREE, node, quals);
18152 cp_lexer_consume_token (parser->lexer);
18153 token = cp_lexer_peek_token (parser->lexer);
18154 node = token->u.value;
18157 return quals;
18160 /* Parse an Objective-C typename. */
18162 static tree
18163 cp_parser_objc_typename (cp_parser* parser)
18165 tree typename = NULL_TREE;
18167 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18169 tree proto_quals, cp_type = NULL_TREE;
18171 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18172 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18174 /* An ObjC type name may consist of just protocol qualifiers, in which
18175 case the type shall default to 'id'. */
18176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18177 cp_type = cp_parser_type_id (parser);
18179 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18180 typename = build_tree_list (proto_quals, cp_type);
18183 return typename;
18186 /* Check to see if TYPE refers to an Objective-C selector name. */
18188 static bool
18189 cp_parser_objc_selector_p (enum cpp_ttype type)
18191 return (type == CPP_NAME || type == CPP_KEYWORD
18192 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18193 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18194 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18195 || type == CPP_XOR || type == CPP_XOR_EQ);
18198 /* Parse an Objective-C selector. */
18200 static tree
18201 cp_parser_objc_selector (cp_parser* parser)
18203 cp_token *token = cp_lexer_consume_token (parser->lexer);
18205 if (!cp_parser_objc_selector_p (token->type))
18207 error ("invalid Objective-C++ selector name");
18208 return error_mark_node;
18211 /* C++ operator names are allowed to appear in ObjC selectors. */
18212 switch (token->type)
18214 case CPP_AND_AND: return get_identifier ("and");
18215 case CPP_AND_EQ: return get_identifier ("and_eq");
18216 case CPP_AND: return get_identifier ("bitand");
18217 case CPP_OR: return get_identifier ("bitor");
18218 case CPP_COMPL: return get_identifier ("compl");
18219 case CPP_NOT: return get_identifier ("not");
18220 case CPP_NOT_EQ: return get_identifier ("not_eq");
18221 case CPP_OR_OR: return get_identifier ("or");
18222 case CPP_OR_EQ: return get_identifier ("or_eq");
18223 case CPP_XOR: return get_identifier ("xor");
18224 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18225 default: return token->u.value;
18229 /* Parse an Objective-C params list. */
18231 static tree
18232 cp_parser_objc_method_keyword_params (cp_parser* parser)
18234 tree params = NULL_TREE;
18235 bool maybe_unary_selector_p = true;
18236 cp_token *token = cp_lexer_peek_token (parser->lexer);
18238 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18240 tree selector = NULL_TREE, typename, identifier;
18242 if (token->type != CPP_COLON)
18243 selector = cp_parser_objc_selector (parser);
18245 /* Detect if we have a unary selector. */
18246 if (maybe_unary_selector_p
18247 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18248 return selector;
18250 maybe_unary_selector_p = false;
18251 cp_parser_require (parser, CPP_COLON, "`:'");
18252 typename = cp_parser_objc_typename (parser);
18253 identifier = cp_parser_identifier (parser);
18255 params
18256 = chainon (params,
18257 objc_build_keyword_decl (selector,
18258 typename,
18259 identifier));
18261 token = cp_lexer_peek_token (parser->lexer);
18264 return params;
18267 /* Parse the non-keyword Objective-C params. */
18269 static tree
18270 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18272 tree params = make_node (TREE_LIST);
18273 cp_token *token = cp_lexer_peek_token (parser->lexer);
18274 *ellipsisp = false; /* Initially, assume no ellipsis. */
18276 while (token->type == CPP_COMMA)
18278 cp_parameter_declarator *parmdecl;
18279 tree parm;
18281 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18282 token = cp_lexer_peek_token (parser->lexer);
18284 if (token->type == CPP_ELLIPSIS)
18286 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18287 *ellipsisp = true;
18288 break;
18291 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18292 parm = grokdeclarator (parmdecl->declarator,
18293 &parmdecl->decl_specifiers,
18294 PARM, /*initialized=*/0,
18295 /*attrlist=*/NULL);
18297 chainon (params, build_tree_list (NULL_TREE, parm));
18298 token = cp_lexer_peek_token (parser->lexer);
18301 return params;
18304 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18306 static void
18307 cp_parser_objc_interstitial_code (cp_parser* parser)
18309 cp_token *token = cp_lexer_peek_token (parser->lexer);
18311 /* If the next token is `extern' and the following token is a string
18312 literal, then we have a linkage specification. */
18313 if (token->keyword == RID_EXTERN
18314 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18315 cp_parser_linkage_specification (parser);
18316 /* Handle #pragma, if any. */
18317 else if (token->type == CPP_PRAGMA)
18318 cp_parser_pragma (parser, pragma_external);
18319 /* Allow stray semicolons. */
18320 else if (token->type == CPP_SEMICOLON)
18321 cp_lexer_consume_token (parser->lexer);
18322 /* Finally, try to parse a block-declaration, or a function-definition. */
18323 else
18324 cp_parser_block_declaration (parser, /*statement_p=*/false);
18327 /* Parse a method signature. */
18329 static tree
18330 cp_parser_objc_method_signature (cp_parser* parser)
18332 tree rettype, kwdparms, optparms;
18333 bool ellipsis = false;
18335 cp_parser_objc_method_type (parser);
18336 rettype = cp_parser_objc_typename (parser);
18337 kwdparms = cp_parser_objc_method_keyword_params (parser);
18338 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18340 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18343 /* Pars an Objective-C method prototype list. */
18345 static void
18346 cp_parser_objc_method_prototype_list (cp_parser* parser)
18348 cp_token *token = cp_lexer_peek_token (parser->lexer);
18350 while (token->keyword != RID_AT_END)
18352 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18354 objc_add_method_declaration
18355 (cp_parser_objc_method_signature (parser));
18356 cp_parser_consume_semicolon_at_end_of_statement (parser);
18358 else
18359 /* Allow for interspersed non-ObjC++ code. */
18360 cp_parser_objc_interstitial_code (parser);
18362 token = cp_lexer_peek_token (parser->lexer);
18365 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18366 objc_finish_interface ();
18369 /* Parse an Objective-C method definition list. */
18371 static void
18372 cp_parser_objc_method_definition_list (cp_parser* parser)
18374 cp_token *token = cp_lexer_peek_token (parser->lexer);
18376 while (token->keyword != RID_AT_END)
18378 tree meth;
18380 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18382 push_deferring_access_checks (dk_deferred);
18383 objc_start_method_definition
18384 (cp_parser_objc_method_signature (parser));
18386 /* For historical reasons, we accept an optional semicolon. */
18387 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18388 cp_lexer_consume_token (parser->lexer);
18390 perform_deferred_access_checks ();
18391 stop_deferring_access_checks ();
18392 meth = cp_parser_function_definition_after_declarator (parser,
18393 false);
18394 pop_deferring_access_checks ();
18395 objc_finish_method_definition (meth);
18397 else
18398 /* Allow for interspersed non-ObjC++ code. */
18399 cp_parser_objc_interstitial_code (parser);
18401 token = cp_lexer_peek_token (parser->lexer);
18404 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18405 objc_finish_implementation ();
18408 /* Parse Objective-C ivars. */
18410 static void
18411 cp_parser_objc_class_ivars (cp_parser* parser)
18413 cp_token *token = cp_lexer_peek_token (parser->lexer);
18415 if (token->type != CPP_OPEN_BRACE)
18416 return; /* No ivars specified. */
18418 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18419 token = cp_lexer_peek_token (parser->lexer);
18421 while (token->type != CPP_CLOSE_BRACE)
18423 cp_decl_specifier_seq declspecs;
18424 int decl_class_or_enum_p;
18425 tree prefix_attributes;
18427 cp_parser_objc_visibility_spec (parser);
18429 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18430 break;
18432 cp_parser_decl_specifier_seq (parser,
18433 CP_PARSER_FLAGS_OPTIONAL,
18434 &declspecs,
18435 &decl_class_or_enum_p);
18436 prefix_attributes = declspecs.attributes;
18437 declspecs.attributes = NULL_TREE;
18439 /* Keep going until we hit the `;' at the end of the
18440 declaration. */
18441 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18443 tree width = NULL_TREE, attributes, first_attribute, decl;
18444 cp_declarator *declarator = NULL;
18445 int ctor_dtor_or_conv_p;
18447 /* Check for a (possibly unnamed) bitfield declaration. */
18448 token = cp_lexer_peek_token (parser->lexer);
18449 if (token->type == CPP_COLON)
18450 goto eat_colon;
18452 if (token->type == CPP_NAME
18453 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18454 == CPP_COLON))
18456 /* Get the name of the bitfield. */
18457 declarator = make_id_declarator (NULL_TREE,
18458 cp_parser_identifier (parser),
18459 sfk_none);
18461 eat_colon:
18462 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18463 /* Get the width of the bitfield. */
18464 width
18465 = cp_parser_constant_expression (parser,
18466 /*allow_non_constant=*/false,
18467 NULL);
18469 else
18471 /* Parse the declarator. */
18472 declarator
18473 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18474 &ctor_dtor_or_conv_p,
18475 /*parenthesized_p=*/NULL,
18476 /*member_p=*/false);
18479 /* Look for attributes that apply to the ivar. */
18480 attributes = cp_parser_attributes_opt (parser);
18481 /* Remember which attributes are prefix attributes and
18482 which are not. */
18483 first_attribute = attributes;
18484 /* Combine the attributes. */
18485 attributes = chainon (prefix_attributes, attributes);
18487 if (width)
18489 /* Create the bitfield declaration. */
18490 decl = grokbitfield (declarator, &declspecs, width);
18491 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18493 else
18494 decl = grokfield (declarator, &declspecs,
18495 NULL_TREE, /*init_const_expr_p=*/false,
18496 NULL_TREE, attributes);
18498 /* Add the instance variable. */
18499 objc_add_instance_variable (decl);
18501 /* Reset PREFIX_ATTRIBUTES. */
18502 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18503 attributes = TREE_CHAIN (attributes);
18504 if (attributes)
18505 TREE_CHAIN (attributes) = NULL_TREE;
18507 token = cp_lexer_peek_token (parser->lexer);
18509 if (token->type == CPP_COMMA)
18511 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18512 continue;
18514 break;
18517 cp_parser_consume_semicolon_at_end_of_statement (parser);
18518 token = cp_lexer_peek_token (parser->lexer);
18521 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18522 /* For historical reasons, we accept an optional semicolon. */
18523 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18524 cp_lexer_consume_token (parser->lexer);
18527 /* Parse an Objective-C protocol declaration. */
18529 static void
18530 cp_parser_objc_protocol_declaration (cp_parser* parser)
18532 tree proto, protorefs;
18533 cp_token *tok;
18535 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18536 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18538 error ("identifier expected after %<@protocol%>");
18539 goto finish;
18542 /* See if we have a forward declaration or a definition. */
18543 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18545 /* Try a forward declaration first. */
18546 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18548 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18549 finish:
18550 cp_parser_consume_semicolon_at_end_of_statement (parser);
18553 /* Ok, we got a full-fledged definition (or at least should). */
18554 else
18556 proto = cp_parser_identifier (parser);
18557 protorefs = cp_parser_objc_protocol_refs_opt (parser);
18558 objc_start_protocol (proto, protorefs);
18559 cp_parser_objc_method_prototype_list (parser);
18563 /* Parse an Objective-C superclass or category. */
18565 static void
18566 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18567 tree *categ)
18569 cp_token *next = cp_lexer_peek_token (parser->lexer);
18571 *super = *categ = NULL_TREE;
18572 if (next->type == CPP_COLON)
18574 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18575 *super = cp_parser_identifier (parser);
18577 else if (next->type == CPP_OPEN_PAREN)
18579 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18580 *categ = cp_parser_identifier (parser);
18581 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18585 /* Parse an Objective-C class interface. */
18587 static void
18588 cp_parser_objc_class_interface (cp_parser* parser)
18590 tree name, super, categ, protos;
18592 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
18593 name = cp_parser_identifier (parser);
18594 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18595 protos = cp_parser_objc_protocol_refs_opt (parser);
18597 /* We have either a class or a category on our hands. */
18598 if (categ)
18599 objc_start_category_interface (name, categ, protos);
18600 else
18602 objc_start_class_interface (name, super, protos);
18603 /* Handle instance variable declarations, if any. */
18604 cp_parser_objc_class_ivars (parser);
18605 objc_continue_interface ();
18608 cp_parser_objc_method_prototype_list (parser);
18611 /* Parse an Objective-C class implementation. */
18613 static void
18614 cp_parser_objc_class_implementation (cp_parser* parser)
18616 tree name, super, categ;
18618 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
18619 name = cp_parser_identifier (parser);
18620 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18622 /* We have either a class or a category on our hands. */
18623 if (categ)
18624 objc_start_category_implementation (name, categ);
18625 else
18627 objc_start_class_implementation (name, super);
18628 /* Handle instance variable declarations, if any. */
18629 cp_parser_objc_class_ivars (parser);
18630 objc_continue_implementation ();
18633 cp_parser_objc_method_definition_list (parser);
18636 /* Consume the @end token and finish off the implementation. */
18638 static void
18639 cp_parser_objc_end_implementation (cp_parser* parser)
18641 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18642 objc_finish_implementation ();
18645 /* Parse an Objective-C declaration. */
18647 static void
18648 cp_parser_objc_declaration (cp_parser* parser)
18650 /* Try to figure out what kind of declaration is present. */
18651 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18653 switch (kwd->keyword)
18655 case RID_AT_ALIAS:
18656 cp_parser_objc_alias_declaration (parser);
18657 break;
18658 case RID_AT_CLASS:
18659 cp_parser_objc_class_declaration (parser);
18660 break;
18661 case RID_AT_PROTOCOL:
18662 cp_parser_objc_protocol_declaration (parser);
18663 break;
18664 case RID_AT_INTERFACE:
18665 cp_parser_objc_class_interface (parser);
18666 break;
18667 case RID_AT_IMPLEMENTATION:
18668 cp_parser_objc_class_implementation (parser);
18669 break;
18670 case RID_AT_END:
18671 cp_parser_objc_end_implementation (parser);
18672 break;
18673 default:
18674 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18675 cp_parser_skip_to_end_of_block_or_statement (parser);
18679 /* Parse an Objective-C try-catch-finally statement.
18681 objc-try-catch-finally-stmt:
18682 @try compound-statement objc-catch-clause-seq [opt]
18683 objc-finally-clause [opt]
18685 objc-catch-clause-seq:
18686 objc-catch-clause objc-catch-clause-seq [opt]
18688 objc-catch-clause:
18689 @catch ( exception-declaration ) compound-statement
18691 objc-finally-clause
18692 @finally compound-statement
18694 Returns NULL_TREE. */
18696 static tree
18697 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18698 location_t location;
18699 tree stmt;
18701 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18702 location = cp_lexer_peek_token (parser->lexer)->location;
18703 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18704 node, lest it get absorbed into the surrounding block. */
18705 stmt = push_stmt_list ();
18706 cp_parser_compound_statement (parser, NULL, false);
18707 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18709 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18711 cp_parameter_declarator *parmdecl;
18712 tree parm;
18714 cp_lexer_consume_token (parser->lexer);
18715 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18716 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18717 parm = grokdeclarator (parmdecl->declarator,
18718 &parmdecl->decl_specifiers,
18719 PARM, /*initialized=*/0,
18720 /*attrlist=*/NULL);
18721 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18722 objc_begin_catch_clause (parm);
18723 cp_parser_compound_statement (parser, NULL, false);
18724 objc_finish_catch_clause ();
18727 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18729 cp_lexer_consume_token (parser->lexer);
18730 location = cp_lexer_peek_token (parser->lexer)->location;
18731 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18732 node, lest it get absorbed into the surrounding block. */
18733 stmt = push_stmt_list ();
18734 cp_parser_compound_statement (parser, NULL, false);
18735 objc_build_finally_clause (location, pop_stmt_list (stmt));
18738 return objc_finish_try_stmt ();
18741 /* Parse an Objective-C synchronized statement.
18743 objc-synchronized-stmt:
18744 @synchronized ( expression ) compound-statement
18746 Returns NULL_TREE. */
18748 static tree
18749 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18750 location_t location;
18751 tree lock, stmt;
18753 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18755 location = cp_lexer_peek_token (parser->lexer)->location;
18756 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18757 lock = cp_parser_expression (parser, false);
18758 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18760 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18761 node, lest it get absorbed into the surrounding block. */
18762 stmt = push_stmt_list ();
18763 cp_parser_compound_statement (parser, NULL, false);
18765 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18768 /* Parse an Objective-C throw statement.
18770 objc-throw-stmt:
18771 @throw assignment-expression [opt] ;
18773 Returns a constructed '@throw' statement. */
18775 static tree
18776 cp_parser_objc_throw_statement (cp_parser *parser) {
18777 tree expr = NULL_TREE;
18779 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18781 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18782 expr = cp_parser_assignment_expression (parser, false);
18784 cp_parser_consume_semicolon_at_end_of_statement (parser);
18786 return objc_build_throw_stmt (expr);
18789 /* Parse an Objective-C statement. */
18791 static tree
18792 cp_parser_objc_statement (cp_parser * parser) {
18793 /* Try to figure out what kind of declaration is present. */
18794 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18796 switch (kwd->keyword)
18798 case RID_AT_TRY:
18799 return cp_parser_objc_try_catch_finally_statement (parser);
18800 case RID_AT_SYNCHRONIZED:
18801 return cp_parser_objc_synchronized_statement (parser);
18802 case RID_AT_THROW:
18803 return cp_parser_objc_throw_statement (parser);
18804 default:
18805 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18806 cp_parser_skip_to_end_of_block_or_statement (parser);
18809 return error_mark_node;
18812 /* OpenMP 2.5 parsing routines. */
18814 /* Returns name of the next clause.
18815 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18816 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18817 returned and the token is consumed. */
18819 static pragma_omp_clause
18820 cp_parser_omp_clause_name (cp_parser *parser)
18822 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18824 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18825 result = PRAGMA_OMP_CLAUSE_IF;
18826 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18827 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18828 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18829 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18830 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18832 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18833 const char *p = IDENTIFIER_POINTER (id);
18835 switch (p[0])
18837 case 'c':
18838 if (!strcmp ("copyin", p))
18839 result = PRAGMA_OMP_CLAUSE_COPYIN;
18840 else if (!strcmp ("copyprivate", p))
18841 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18842 break;
18843 case 'f':
18844 if (!strcmp ("firstprivate", p))
18845 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18846 break;
18847 case 'l':
18848 if (!strcmp ("lastprivate", p))
18849 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18850 break;
18851 case 'n':
18852 if (!strcmp ("nowait", p))
18853 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18854 else if (!strcmp ("num_threads", p))
18855 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18856 break;
18857 case 'o':
18858 if (!strcmp ("ordered", p))
18859 result = PRAGMA_OMP_CLAUSE_ORDERED;
18860 break;
18861 case 'r':
18862 if (!strcmp ("reduction", p))
18863 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18864 break;
18865 case 's':
18866 if (!strcmp ("schedule", p))
18867 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18868 else if (!strcmp ("shared", p))
18869 result = PRAGMA_OMP_CLAUSE_SHARED;
18870 break;
18874 if (result != PRAGMA_OMP_CLAUSE_NONE)
18875 cp_lexer_consume_token (parser->lexer);
18877 return result;
18880 /* Validate that a clause of the given type does not already exist. */
18882 static void
18883 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18885 tree c;
18887 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18888 if (OMP_CLAUSE_CODE (c) == code)
18890 error ("too many %qs clauses", name);
18891 break;
18895 /* OpenMP 2.5:
18896 variable-list:
18897 identifier
18898 variable-list , identifier
18900 In addition, we match a closing parenthesis. An opening parenthesis
18901 will have been consumed by the caller.
18903 If KIND is nonzero, create the appropriate node and install the decl
18904 in OMP_CLAUSE_DECL and add the node to the head of the list.
18906 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18907 return the list created. */
18909 static tree
18910 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18911 tree list)
18913 while (1)
18915 tree name, decl;
18917 name = cp_parser_id_expression (parser, /*template_p=*/false,
18918 /*check_dependency_p=*/true,
18919 /*template_p=*/NULL,
18920 /*declarator_p=*/false,
18921 /*optional_p=*/false);
18922 if (name == error_mark_node)
18923 goto skip_comma;
18925 decl = cp_parser_lookup_name_simple (parser, name);
18926 if (decl == error_mark_node)
18927 cp_parser_name_lookup_error (parser, name, decl, NULL);
18928 else if (kind != 0)
18930 tree u = build_omp_clause (kind);
18931 OMP_CLAUSE_DECL (u) = decl;
18932 OMP_CLAUSE_CHAIN (u) = list;
18933 list = u;
18935 else
18936 list = tree_cons (decl, NULL_TREE, list);
18938 get_comma:
18939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18940 break;
18941 cp_lexer_consume_token (parser->lexer);
18944 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18946 int ending;
18948 /* Try to resync to an unnested comma. Copied from
18949 cp_parser_parenthesized_expression_list. */
18950 skip_comma:
18951 ending = cp_parser_skip_to_closing_parenthesis (parser,
18952 /*recovering=*/true,
18953 /*or_comma=*/true,
18954 /*consume_paren=*/true);
18955 if (ending < 0)
18956 goto get_comma;
18959 return list;
18962 /* Similarly, but expect leading and trailing parenthesis. This is a very
18963 common case for omp clauses. */
18965 static tree
18966 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18968 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18969 return cp_parser_omp_var_list_no_open (parser, kind, list);
18970 return list;
18973 /* OpenMP 2.5:
18974 default ( shared | none ) */
18976 static tree
18977 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18979 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18980 tree c;
18982 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18983 return list;
18984 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18986 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18987 const char *p = IDENTIFIER_POINTER (id);
18989 switch (p[0])
18991 case 'n':
18992 if (strcmp ("none", p) != 0)
18993 goto invalid_kind;
18994 kind = OMP_CLAUSE_DEFAULT_NONE;
18995 break;
18997 case 's':
18998 if (strcmp ("shared", p) != 0)
18999 goto invalid_kind;
19000 kind = OMP_CLAUSE_DEFAULT_SHARED;
19001 break;
19003 default:
19004 goto invalid_kind;
19007 cp_lexer_consume_token (parser->lexer);
19009 else
19011 invalid_kind:
19012 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19015 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19016 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19017 /*or_comma=*/false,
19018 /*consume_paren=*/true);
19020 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19021 return list;
19023 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19024 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19025 OMP_CLAUSE_CHAIN (c) = list;
19026 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19028 return c;
19031 /* OpenMP 2.5:
19032 if ( expression ) */
19034 static tree
19035 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19037 tree t, c;
19039 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19040 return list;
19042 t = cp_parser_condition (parser);
19044 if (t == error_mark_node
19045 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19046 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19047 /*or_comma=*/false,
19048 /*consume_paren=*/true);
19050 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19052 c = build_omp_clause (OMP_CLAUSE_IF);
19053 OMP_CLAUSE_IF_EXPR (c) = t;
19054 OMP_CLAUSE_CHAIN (c) = list;
19056 return c;
19059 /* OpenMP 2.5:
19060 nowait */
19062 static tree
19063 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19065 tree c;
19067 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19069 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19070 OMP_CLAUSE_CHAIN (c) = list;
19071 return c;
19074 /* OpenMP 2.5:
19075 num_threads ( expression ) */
19077 static tree
19078 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19080 tree t, c;
19082 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19083 return list;
19085 t = cp_parser_expression (parser, false);
19087 if (t == error_mark_node
19088 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19089 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19090 /*or_comma=*/false,
19091 /*consume_paren=*/true);
19093 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19095 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19096 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19097 OMP_CLAUSE_CHAIN (c) = list;
19099 return c;
19102 /* OpenMP 2.5:
19103 ordered */
19105 static tree
19106 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19108 tree c;
19110 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19112 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19113 OMP_CLAUSE_CHAIN (c) = list;
19114 return c;
19117 /* OpenMP 2.5:
19118 reduction ( reduction-operator : variable-list )
19120 reduction-operator:
19121 One of: + * - & ^ | && || */
19123 static tree
19124 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19126 enum tree_code code;
19127 tree nlist, c;
19129 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19130 return list;
19132 switch (cp_lexer_peek_token (parser->lexer)->type)
19134 case CPP_PLUS:
19135 code = PLUS_EXPR;
19136 break;
19137 case CPP_MULT:
19138 code = MULT_EXPR;
19139 break;
19140 case CPP_MINUS:
19141 code = MINUS_EXPR;
19142 break;
19143 case CPP_AND:
19144 code = BIT_AND_EXPR;
19145 break;
19146 case CPP_XOR:
19147 code = BIT_XOR_EXPR;
19148 break;
19149 case CPP_OR:
19150 code = BIT_IOR_EXPR;
19151 break;
19152 case CPP_AND_AND:
19153 code = TRUTH_ANDIF_EXPR;
19154 break;
19155 case CPP_OR_OR:
19156 code = TRUTH_ORIF_EXPR;
19157 break;
19158 default:
19159 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19160 resync_fail:
19161 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19162 /*or_comma=*/false,
19163 /*consume_paren=*/true);
19164 return list;
19166 cp_lexer_consume_token (parser->lexer);
19168 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19169 goto resync_fail;
19171 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19172 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19173 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19175 return nlist;
19178 /* OpenMP 2.5:
19179 schedule ( schedule-kind )
19180 schedule ( schedule-kind , expression )
19182 schedule-kind:
19183 static | dynamic | guided | runtime */
19185 static tree
19186 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19188 tree c, t;
19190 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19191 return list;
19193 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19195 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19197 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19198 const char *p = IDENTIFIER_POINTER (id);
19200 switch (p[0])
19202 case 'd':
19203 if (strcmp ("dynamic", p) != 0)
19204 goto invalid_kind;
19205 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19206 break;
19208 case 'g':
19209 if (strcmp ("guided", p) != 0)
19210 goto invalid_kind;
19211 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19212 break;
19214 case 'r':
19215 if (strcmp ("runtime", p) != 0)
19216 goto invalid_kind;
19217 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19218 break;
19220 default:
19221 goto invalid_kind;
19224 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19225 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19226 else
19227 goto invalid_kind;
19228 cp_lexer_consume_token (parser->lexer);
19230 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19232 cp_lexer_consume_token (parser->lexer);
19234 t = cp_parser_assignment_expression (parser, false);
19236 if (t == error_mark_node)
19237 goto resync_fail;
19238 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19239 error ("schedule %<runtime%> does not take "
19240 "a %<chunk_size%> parameter");
19241 else
19242 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19244 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19245 goto resync_fail;
19247 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19248 goto resync_fail;
19250 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19251 OMP_CLAUSE_CHAIN (c) = list;
19252 return c;
19254 invalid_kind:
19255 cp_parser_error (parser, "invalid schedule kind");
19256 resync_fail:
19257 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19258 /*or_comma=*/false,
19259 /*consume_paren=*/true);
19260 return list;
19263 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19264 is a bitmask in MASK. Return the list of clauses found; the result
19265 of clause default goes in *pdefault. */
19267 static tree
19268 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19269 const char *where, cp_token *pragma_tok)
19271 tree clauses = NULL;
19273 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19275 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19276 const char *c_name;
19277 tree prev = clauses;
19279 switch (c_kind)
19281 case PRAGMA_OMP_CLAUSE_COPYIN:
19282 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19283 c_name = "copyin";
19284 break;
19285 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19286 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19287 clauses);
19288 c_name = "copyprivate";
19289 break;
19290 case PRAGMA_OMP_CLAUSE_DEFAULT:
19291 clauses = cp_parser_omp_clause_default (parser, clauses);
19292 c_name = "default";
19293 break;
19294 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19295 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19296 clauses);
19297 c_name = "firstprivate";
19298 break;
19299 case PRAGMA_OMP_CLAUSE_IF:
19300 clauses = cp_parser_omp_clause_if (parser, clauses);
19301 c_name = "if";
19302 break;
19303 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19304 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19305 clauses);
19306 c_name = "lastprivate";
19307 break;
19308 case PRAGMA_OMP_CLAUSE_NOWAIT:
19309 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19310 c_name = "nowait";
19311 break;
19312 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19313 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19314 c_name = "num_threads";
19315 break;
19316 case PRAGMA_OMP_CLAUSE_ORDERED:
19317 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19318 c_name = "ordered";
19319 break;
19320 case PRAGMA_OMP_CLAUSE_PRIVATE:
19321 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19322 clauses);
19323 c_name = "private";
19324 break;
19325 case PRAGMA_OMP_CLAUSE_REDUCTION:
19326 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19327 c_name = "reduction";
19328 break;
19329 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19330 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19331 c_name = "schedule";
19332 break;
19333 case PRAGMA_OMP_CLAUSE_SHARED:
19334 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19335 clauses);
19336 c_name = "shared";
19337 break;
19338 default:
19339 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19340 goto saw_error;
19343 if (((mask >> c_kind) & 1) == 0)
19345 /* Remove the invalid clause(s) from the list to avoid
19346 confusing the rest of the compiler. */
19347 clauses = prev;
19348 error ("%qs is not valid for %qs", c_name, where);
19351 saw_error:
19352 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19353 return finish_omp_clauses (clauses);
19356 /* OpenMP 2.5:
19357 structured-block:
19358 statement
19360 In practice, we're also interested in adding the statement to an
19361 outer node. So it is convenient if we work around the fact that
19362 cp_parser_statement calls add_stmt. */
19364 static unsigned
19365 cp_parser_begin_omp_structured_block (cp_parser *parser)
19367 unsigned save = parser->in_statement;
19369 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19370 This preserves the "not within loop or switch" style error messages
19371 for nonsense cases like
19372 void foo() {
19373 #pragma omp single
19374 break;
19377 if (parser->in_statement)
19378 parser->in_statement = IN_OMP_BLOCK;
19380 return save;
19383 static void
19384 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19386 parser->in_statement = save;
19389 static tree
19390 cp_parser_omp_structured_block (cp_parser *parser)
19392 tree stmt = begin_omp_structured_block ();
19393 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19395 cp_parser_statement (parser, NULL_TREE, false, NULL);
19397 cp_parser_end_omp_structured_block (parser, save);
19398 return finish_omp_structured_block (stmt);
19401 /* OpenMP 2.5:
19402 # pragma omp atomic new-line
19403 expression-stmt
19405 expression-stmt:
19406 x binop= expr | x++ | ++x | x-- | --x
19407 binop:
19408 +, *, -, /, &, ^, |, <<, >>
19410 where x is an lvalue expression with scalar type. */
19412 static void
19413 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19415 tree lhs, rhs;
19416 enum tree_code code;
19418 cp_parser_require_pragma_eol (parser, pragma_tok);
19420 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19421 /*cast_p=*/false);
19422 switch (TREE_CODE (lhs))
19424 case ERROR_MARK:
19425 goto saw_error;
19427 case PREINCREMENT_EXPR:
19428 case POSTINCREMENT_EXPR:
19429 lhs = TREE_OPERAND (lhs, 0);
19430 code = PLUS_EXPR;
19431 rhs = integer_one_node;
19432 break;
19434 case PREDECREMENT_EXPR:
19435 case POSTDECREMENT_EXPR:
19436 lhs = TREE_OPERAND (lhs, 0);
19437 code = MINUS_EXPR;
19438 rhs = integer_one_node;
19439 break;
19441 default:
19442 switch (cp_lexer_peek_token (parser->lexer)->type)
19444 case CPP_MULT_EQ:
19445 code = MULT_EXPR;
19446 break;
19447 case CPP_DIV_EQ:
19448 code = TRUNC_DIV_EXPR;
19449 break;
19450 case CPP_PLUS_EQ:
19451 code = PLUS_EXPR;
19452 break;
19453 case CPP_MINUS_EQ:
19454 code = MINUS_EXPR;
19455 break;
19456 case CPP_LSHIFT_EQ:
19457 code = LSHIFT_EXPR;
19458 break;
19459 case CPP_RSHIFT_EQ:
19460 code = RSHIFT_EXPR;
19461 break;
19462 case CPP_AND_EQ:
19463 code = BIT_AND_EXPR;
19464 break;
19465 case CPP_OR_EQ:
19466 code = BIT_IOR_EXPR;
19467 break;
19468 case CPP_XOR_EQ:
19469 code = BIT_XOR_EXPR;
19470 break;
19471 default:
19472 cp_parser_error (parser,
19473 "invalid operator for %<#pragma omp atomic%>");
19474 goto saw_error;
19476 cp_lexer_consume_token (parser->lexer);
19478 rhs = cp_parser_expression (parser, false);
19479 if (rhs == error_mark_node)
19480 goto saw_error;
19481 break;
19483 finish_omp_atomic (code, lhs, rhs);
19484 cp_parser_consume_semicolon_at_end_of_statement (parser);
19485 return;
19487 saw_error:
19488 cp_parser_skip_to_end_of_block_or_statement (parser);
19492 /* OpenMP 2.5:
19493 # pragma omp barrier new-line */
19495 static void
19496 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19498 cp_parser_require_pragma_eol (parser, pragma_tok);
19499 finish_omp_barrier ();
19502 /* OpenMP 2.5:
19503 # pragma omp critical [(name)] new-line
19504 structured-block */
19506 static tree
19507 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19509 tree stmt, name = NULL;
19511 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19513 cp_lexer_consume_token (parser->lexer);
19515 name = cp_parser_identifier (parser);
19517 if (name == error_mark_node
19518 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19519 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19520 /*or_comma=*/false,
19521 /*consume_paren=*/true);
19522 if (name == error_mark_node)
19523 name = NULL;
19525 cp_parser_require_pragma_eol (parser, pragma_tok);
19527 stmt = cp_parser_omp_structured_block (parser);
19528 return c_finish_omp_critical (stmt, name);
19531 /* OpenMP 2.5:
19532 # pragma omp flush flush-vars[opt] new-line
19534 flush-vars:
19535 ( variable-list ) */
19537 static void
19538 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19541 (void) cp_parser_omp_var_list (parser, 0, NULL);
19542 cp_parser_require_pragma_eol (parser, pragma_tok);
19544 finish_omp_flush ();
19547 /* Parse the restricted form of the for statment allowed by OpenMP. */
19549 static tree
19550 cp_parser_omp_for_loop (cp_parser *parser)
19552 tree init, cond, incr, body, decl, pre_body;
19553 location_t loc;
19555 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19557 cp_parser_error (parser, "for statement expected");
19558 return NULL;
19560 loc = cp_lexer_consume_token (parser->lexer)->location;
19561 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19562 return NULL;
19564 init = decl = NULL;
19565 pre_body = push_stmt_list ();
19566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19568 cp_decl_specifier_seq type_specifiers;
19570 /* First, try to parse as an initialized declaration. See
19571 cp_parser_condition, from whence the bulk of this is copied. */
19573 cp_parser_parse_tentatively (parser);
19574 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19575 &type_specifiers);
19576 if (!cp_parser_error_occurred (parser))
19578 tree asm_specification, attributes;
19579 cp_declarator *declarator;
19581 declarator = cp_parser_declarator (parser,
19582 CP_PARSER_DECLARATOR_NAMED,
19583 /*ctor_dtor_or_conv_p=*/NULL,
19584 /*parenthesized_p=*/NULL,
19585 /*member_p=*/false);
19586 attributes = cp_parser_attributes_opt (parser);
19587 asm_specification = cp_parser_asm_specification_opt (parser);
19589 cp_parser_require (parser, CPP_EQ, "`='");
19590 if (cp_parser_parse_definitely (parser))
19592 tree pushed_scope;
19594 decl = start_decl (declarator, &type_specifiers,
19595 /*initialized_p=*/false, attributes,
19596 /*prefix_attributes=*/NULL_TREE,
19597 &pushed_scope);
19599 init = cp_parser_assignment_expression (parser, false);
19601 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19602 asm_specification, LOOKUP_ONLYCONVERTING);
19604 if (pushed_scope)
19605 pop_scope (pushed_scope);
19608 else
19609 cp_parser_abort_tentative_parse (parser);
19611 /* If parsing as an initialized declaration failed, try again as
19612 a simple expression. */
19613 if (decl == NULL)
19614 init = cp_parser_expression (parser, false);
19616 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19617 pre_body = pop_stmt_list (pre_body);
19619 cond = NULL;
19620 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19621 cond = cp_parser_condition (parser);
19622 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19624 incr = NULL;
19625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19626 incr = cp_parser_expression (parser, false);
19628 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19629 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19630 /*or_comma=*/false,
19631 /*consume_paren=*/true);
19633 /* Note that we saved the original contents of this flag when we entered
19634 the structured block, and so we don't need to re-save it here. */
19635 parser->in_statement = IN_OMP_FOR;
19637 /* Note that the grammar doesn't call for a structured block here,
19638 though the loop as a whole is a structured block. */
19639 body = push_stmt_list ();
19640 cp_parser_statement (parser, NULL_TREE, false, NULL);
19641 body = pop_stmt_list (body);
19643 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19646 /* OpenMP 2.5:
19647 #pragma omp for for-clause[optseq] new-line
19648 for-loop */
19650 #define OMP_FOR_CLAUSE_MASK \
19651 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19652 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19653 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19654 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19655 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19656 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19657 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19659 static tree
19660 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19662 tree clauses, sb, ret;
19663 unsigned int save;
19665 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19666 "#pragma omp for", pragma_tok);
19668 sb = begin_omp_structured_block ();
19669 save = cp_parser_begin_omp_structured_block (parser);
19671 ret = cp_parser_omp_for_loop (parser);
19672 if (ret)
19673 OMP_FOR_CLAUSES (ret) = clauses;
19675 cp_parser_end_omp_structured_block (parser, save);
19676 add_stmt (finish_omp_structured_block (sb));
19678 return ret;
19681 /* OpenMP 2.5:
19682 # pragma omp master new-line
19683 structured-block */
19685 static tree
19686 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19688 cp_parser_require_pragma_eol (parser, pragma_tok);
19689 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19692 /* OpenMP 2.5:
19693 # pragma omp ordered new-line
19694 structured-block */
19696 static tree
19697 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19699 cp_parser_require_pragma_eol (parser, pragma_tok);
19700 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19703 /* OpenMP 2.5:
19705 section-scope:
19706 { section-sequence }
19708 section-sequence:
19709 section-directive[opt] structured-block
19710 section-sequence section-directive structured-block */
19712 static tree
19713 cp_parser_omp_sections_scope (cp_parser *parser)
19715 tree stmt, substmt;
19716 bool error_suppress = false;
19717 cp_token *tok;
19719 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19720 return NULL_TREE;
19722 stmt = push_stmt_list ();
19724 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19726 unsigned save;
19728 substmt = begin_omp_structured_block ();
19729 save = cp_parser_begin_omp_structured_block (parser);
19731 while (1)
19733 cp_parser_statement (parser, NULL_TREE, false, NULL);
19735 tok = cp_lexer_peek_token (parser->lexer);
19736 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19737 break;
19738 if (tok->type == CPP_CLOSE_BRACE)
19739 break;
19740 if (tok->type == CPP_EOF)
19741 break;
19744 cp_parser_end_omp_structured_block (parser, save);
19745 substmt = finish_omp_structured_block (substmt);
19746 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19747 add_stmt (substmt);
19750 while (1)
19752 tok = cp_lexer_peek_token (parser->lexer);
19753 if (tok->type == CPP_CLOSE_BRACE)
19754 break;
19755 if (tok->type == CPP_EOF)
19756 break;
19758 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19760 cp_lexer_consume_token (parser->lexer);
19761 cp_parser_require_pragma_eol (parser, tok);
19762 error_suppress = false;
19764 else if (!error_suppress)
19766 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19767 error_suppress = true;
19770 substmt = cp_parser_omp_structured_block (parser);
19771 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19772 add_stmt (substmt);
19774 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19776 substmt = pop_stmt_list (stmt);
19778 stmt = make_node (OMP_SECTIONS);
19779 TREE_TYPE (stmt) = void_type_node;
19780 OMP_SECTIONS_BODY (stmt) = substmt;
19782 add_stmt (stmt);
19783 return stmt;
19786 /* OpenMP 2.5:
19787 # pragma omp sections sections-clause[optseq] newline
19788 sections-scope */
19790 #define OMP_SECTIONS_CLAUSE_MASK \
19791 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19792 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19793 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19794 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19795 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19797 static tree
19798 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19800 tree clauses, ret;
19802 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19803 "#pragma omp sections", pragma_tok);
19805 ret = cp_parser_omp_sections_scope (parser);
19806 if (ret)
19807 OMP_SECTIONS_CLAUSES (ret) = clauses;
19809 return ret;
19812 /* OpenMP 2.5:
19813 # pragma parallel parallel-clause new-line
19814 # pragma parallel for parallel-for-clause new-line
19815 # pragma parallel sections parallel-sections-clause new-line */
19817 #define OMP_PARALLEL_CLAUSE_MASK \
19818 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19819 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19820 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19821 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19822 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19823 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19824 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19825 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19827 static tree
19828 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19830 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19831 const char *p_name = "#pragma omp parallel";
19832 tree stmt, clauses, par_clause, ws_clause, block;
19833 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19834 unsigned int save;
19836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19838 cp_lexer_consume_token (parser->lexer);
19839 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19840 p_name = "#pragma omp parallel for";
19841 mask |= OMP_FOR_CLAUSE_MASK;
19842 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19844 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19846 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19847 const char *p = IDENTIFIER_POINTER (id);
19848 if (strcmp (p, "sections") == 0)
19850 cp_lexer_consume_token (parser->lexer);
19851 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19852 p_name = "#pragma omp parallel sections";
19853 mask |= OMP_SECTIONS_CLAUSE_MASK;
19854 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19858 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19859 block = begin_omp_parallel ();
19860 save = cp_parser_begin_omp_structured_block (parser);
19862 switch (p_kind)
19864 case PRAGMA_OMP_PARALLEL:
19865 cp_parser_already_scoped_statement (parser);
19866 par_clause = clauses;
19867 break;
19869 case PRAGMA_OMP_PARALLEL_FOR:
19870 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19871 stmt = cp_parser_omp_for_loop (parser);
19872 if (stmt)
19873 OMP_FOR_CLAUSES (stmt) = ws_clause;
19874 break;
19876 case PRAGMA_OMP_PARALLEL_SECTIONS:
19877 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19878 stmt = cp_parser_omp_sections_scope (parser);
19879 if (stmt)
19880 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19881 break;
19883 default:
19884 gcc_unreachable ();
19887 cp_parser_end_omp_structured_block (parser, save);
19888 stmt = finish_omp_parallel (par_clause, block);
19889 if (p_kind != PRAGMA_OMP_PARALLEL)
19890 OMP_PARALLEL_COMBINED (stmt) = 1;
19891 return stmt;
19894 /* OpenMP 2.5:
19895 # pragma omp single single-clause[optseq] new-line
19896 structured-block */
19898 #define OMP_SINGLE_CLAUSE_MASK \
19899 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19900 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19901 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19902 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19904 static tree
19905 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19907 tree stmt = make_node (OMP_SINGLE);
19908 TREE_TYPE (stmt) = void_type_node;
19910 OMP_SINGLE_CLAUSES (stmt)
19911 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19912 "#pragma omp single", pragma_tok);
19913 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19915 return add_stmt (stmt);
19918 /* OpenMP 2.5:
19919 # pragma omp threadprivate (variable-list) */
19921 static void
19922 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19924 tree vars;
19926 vars = cp_parser_omp_var_list (parser, 0, NULL);
19927 cp_parser_require_pragma_eol (parser, pragma_tok);
19929 finish_omp_threadprivate (vars);
19932 /* Main entry point to OpenMP statement pragmas. */
19934 static void
19935 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19937 tree stmt;
19939 switch (pragma_tok->pragma_kind)
19941 case PRAGMA_OMP_ATOMIC:
19942 cp_parser_omp_atomic (parser, pragma_tok);
19943 return;
19944 case PRAGMA_OMP_CRITICAL:
19945 stmt = cp_parser_omp_critical (parser, pragma_tok);
19946 break;
19947 case PRAGMA_OMP_FOR:
19948 stmt = cp_parser_omp_for (parser, pragma_tok);
19949 break;
19950 case PRAGMA_OMP_MASTER:
19951 stmt = cp_parser_omp_master (parser, pragma_tok);
19952 break;
19953 case PRAGMA_OMP_ORDERED:
19954 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19955 break;
19956 case PRAGMA_OMP_PARALLEL:
19957 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19958 break;
19959 case PRAGMA_OMP_SECTIONS:
19960 stmt = cp_parser_omp_sections (parser, pragma_tok);
19961 break;
19962 case PRAGMA_OMP_SINGLE:
19963 stmt = cp_parser_omp_single (parser, pragma_tok);
19964 break;
19965 default:
19966 gcc_unreachable ();
19969 if (stmt)
19970 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19973 /* The parser. */
19975 static GTY (()) cp_parser *the_parser;
19978 /* Special handling for the first token or line in the file. The first
19979 thing in the file might be #pragma GCC pch_preprocess, which loads a
19980 PCH file, which is a GC collection point. So we need to handle this
19981 first pragma without benefit of an existing lexer structure.
19983 Always returns one token to the caller in *FIRST_TOKEN. This is
19984 either the true first token of the file, or the first token after
19985 the initial pragma. */
19987 static void
19988 cp_parser_initial_pragma (cp_token *first_token)
19990 tree name = NULL;
19992 cp_lexer_get_preprocessor_token (NULL, first_token);
19993 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19994 return;
19996 cp_lexer_get_preprocessor_token (NULL, first_token);
19997 if (first_token->type == CPP_STRING)
19999 name = first_token->u.value;
20001 cp_lexer_get_preprocessor_token (NULL, first_token);
20002 if (first_token->type != CPP_PRAGMA_EOL)
20003 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20005 else
20006 error ("expected string literal");
20008 /* Skip to the end of the pragma. */
20009 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20010 cp_lexer_get_preprocessor_token (NULL, first_token);
20012 /* Now actually load the PCH file. */
20013 if (name)
20014 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20016 /* Read one more token to return to our caller. We have to do this
20017 after reading the PCH file in, since its pointers have to be
20018 live. */
20019 cp_lexer_get_preprocessor_token (NULL, first_token);
20022 /* Normal parsing of a pragma token. Here we can (and must) use the
20023 regular lexer. */
20025 static bool
20026 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20028 cp_token *pragma_tok;
20029 unsigned int id;
20031 pragma_tok = cp_lexer_consume_token (parser->lexer);
20032 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20033 parser->lexer->in_pragma = true;
20035 id = pragma_tok->pragma_kind;
20036 switch (id)
20038 case PRAGMA_GCC_PCH_PREPROCESS:
20039 error ("%<#pragma GCC pch_preprocess%> must be first");
20040 break;
20042 case PRAGMA_OMP_BARRIER:
20043 switch (context)
20045 case pragma_compound:
20046 cp_parser_omp_barrier (parser, pragma_tok);
20047 return false;
20048 case pragma_stmt:
20049 error ("%<#pragma omp barrier%> may only be "
20050 "used in compound statements");
20051 break;
20052 default:
20053 goto bad_stmt;
20055 break;
20057 case PRAGMA_OMP_FLUSH:
20058 switch (context)
20060 case pragma_compound:
20061 cp_parser_omp_flush (parser, pragma_tok);
20062 return false;
20063 case pragma_stmt:
20064 error ("%<#pragma omp flush%> may only be "
20065 "used in compound statements");
20066 break;
20067 default:
20068 goto bad_stmt;
20070 break;
20072 case PRAGMA_OMP_THREADPRIVATE:
20073 cp_parser_omp_threadprivate (parser, pragma_tok);
20074 return false;
20076 case PRAGMA_OMP_ATOMIC:
20077 case PRAGMA_OMP_CRITICAL:
20078 case PRAGMA_OMP_FOR:
20079 case PRAGMA_OMP_MASTER:
20080 case PRAGMA_OMP_ORDERED:
20081 case PRAGMA_OMP_PARALLEL:
20082 case PRAGMA_OMP_SECTIONS:
20083 case PRAGMA_OMP_SINGLE:
20084 if (context == pragma_external)
20085 goto bad_stmt;
20086 cp_parser_omp_construct (parser, pragma_tok);
20087 return true;
20089 case PRAGMA_OMP_SECTION:
20090 error ("%<#pragma omp section%> may only be used in "
20091 "%<#pragma omp sections%> construct");
20092 break;
20094 default:
20095 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20096 c_invoke_pragma_handler (id);
20097 break;
20099 bad_stmt:
20100 cp_parser_error (parser, "expected declaration specifiers");
20101 break;
20104 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20105 return false;
20108 /* The interface the pragma parsers have to the lexer. */
20110 enum cpp_ttype
20111 pragma_lex (tree *value)
20113 cp_token *tok;
20114 enum cpp_ttype ret;
20116 tok = cp_lexer_peek_token (the_parser->lexer);
20118 ret = tok->type;
20119 *value = tok->u.value;
20121 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20122 ret = CPP_EOF;
20123 else if (ret == CPP_STRING)
20124 *value = cp_parser_string_literal (the_parser, false, false);
20125 else
20127 cp_lexer_consume_token (the_parser->lexer);
20128 if (ret == CPP_KEYWORD)
20129 ret = CPP_NAME;
20132 return ret;
20136 /* External interface. */
20138 /* Parse one entire translation unit. */
20140 void
20141 c_parse_file (void)
20143 bool error_occurred;
20144 static bool already_called = false;
20146 if (already_called)
20148 sorry ("inter-module optimizations not implemented for C++");
20149 return;
20151 already_called = true;
20153 the_parser = cp_parser_new ();
20154 push_deferring_access_checks (flag_access_control
20155 ? dk_no_deferred : dk_no_check);
20156 error_occurred = cp_parser_translation_unit (the_parser);
20157 the_parser = NULL;
20160 #include "gt-cp-parser.h"