2008-01-25 Douglas Gregor <doug.gregor@gmail.com>
[official-gcc.git] / gcc / cp / parser.c
blobfdd1ceda7904c34c3e0481cb2697d22fb5fae4b4
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
42 /* The lexer. */
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
48 qualifying scope. */
50 struct tree_check GTY(())
52 /* The value associated with the token. */
53 tree value;
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
61 /* A C++ token. */
63 typedef struct cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
70 /* Token flags. */
71 unsigned char flags;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a system header. */
75 BOOL_BITFIELD in_system_header : 1;
76 /* True if this token is from a context where it is implicitly extern "C" */
77 BOOL_BITFIELD implicit_extern_c : 1;
78 /* True for a CPP_NAME token that is not a keyword (i.e., for which
79 KEYWORD is RID_MAX) iff this name was looked up and found to be
80 ambiguous. An error has already been reported. */
81 BOOL_BITFIELD ambiguous_p : 1;
82 /* The input file stack index at which this token was found. */
83 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
84 /* The value associated with this token, if any. */
85 union cp_token_value {
86 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
87 struct tree_check* GTY((tag ("1"))) tree_check_value;
88 /* Use for all other tokens. */
89 tree GTY((tag ("0"))) value;
90 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
91 /* The location at which this token was found. */
92 location_t location;
93 } cp_token;
95 /* We use a stack of token pointer for saving token sets. */
96 typedef struct cp_token *cp_token_position;
97 DEF_VEC_P (cp_token_position);
98 DEF_VEC_ALLOC_P (cp_token_position,heap);
100 static cp_token eof_token =
102 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
103 #if USE_MAPPED_LOCATION
105 #else
106 {0, 0}
107 #endif
110 /* The cp_lexer structure represents the C++ lexer. It is responsible
111 for managing the token stream from the preprocessor and supplying
112 it to the parser. Tokens are never added to the cp_lexer after
113 it is created. */
115 typedef struct cp_lexer GTY (())
117 /* The memory allocated for the buffer. NULL if this lexer does not
118 own the token buffer. */
119 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
120 /* If the lexer owns the buffer, this is the number of tokens in the
121 buffer. */
122 size_t buffer_length;
124 /* A pointer just past the last available token. The tokens
125 in this lexer are [buffer, last_token). */
126 cp_token_position GTY ((skip)) last_token;
128 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
129 no more available tokens. */
130 cp_token_position GTY ((skip)) next_token;
132 /* A stack indicating positions at which cp_lexer_save_tokens was
133 called. The top entry is the most recent position at which we
134 began saving tokens. If the stack is non-empty, we are saving
135 tokens. */
136 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
138 /* The next lexer in a linked list of lexers. */
139 struct cp_lexer *next;
141 /* True if we should output debugging information. */
142 bool debugging_p;
144 /* True if we're in the context of parsing a pragma, and should not
145 increment past the end-of-line marker. */
146 bool in_pragma;
147 } cp_lexer;
149 /* cp_token_cache is a range of tokens. There is no need to represent
150 allocate heap memory for it, since tokens are never removed from the
151 lexer's array. There is also no need for the GC to walk through
152 a cp_token_cache, since everything in here is referenced through
153 a lexer. */
155 typedef struct cp_token_cache GTY(())
157 /* The beginning of the token range. */
158 cp_token * GTY((skip)) first;
160 /* Points immediately after the last token in the range. */
161 cp_token * GTY ((skip)) last;
162 } cp_token_cache;
164 /* Prototypes. */
166 static cp_lexer *cp_lexer_new_main
167 (void);
168 static cp_lexer *cp_lexer_new_from_tokens
169 (cp_token_cache *tokens);
170 static void cp_lexer_destroy
171 (cp_lexer *);
172 static int cp_lexer_saving_tokens
173 (const cp_lexer *);
174 static cp_token_position cp_lexer_token_position
175 (cp_lexer *, bool);
176 static cp_token *cp_lexer_token_at
177 (cp_lexer *, cp_token_position);
178 static void cp_lexer_get_preprocessor_token
179 (cp_lexer *, cp_token *);
180 static inline cp_token *cp_lexer_peek_token
181 (cp_lexer *);
182 static cp_token *cp_lexer_peek_nth_token
183 (cp_lexer *, size_t);
184 static inline bool cp_lexer_next_token_is
185 (cp_lexer *, enum cpp_ttype);
186 static bool cp_lexer_next_token_is_not
187 (cp_lexer *, enum cpp_ttype);
188 static bool cp_lexer_next_token_is_keyword
189 (cp_lexer *, enum rid);
190 static cp_token *cp_lexer_consume_token
191 (cp_lexer *);
192 static void cp_lexer_purge_token
193 (cp_lexer *);
194 static void cp_lexer_purge_tokens_after
195 (cp_lexer *, cp_token_position);
196 static void cp_lexer_save_tokens
197 (cp_lexer *);
198 static void cp_lexer_commit_tokens
199 (cp_lexer *);
200 static void cp_lexer_rollback_tokens
201 (cp_lexer *);
202 #ifdef ENABLE_CHECKING
203 static void cp_lexer_print_token
204 (FILE *, cp_token *);
205 static inline bool cp_lexer_debugging_p
206 (cp_lexer *);
207 static void cp_lexer_start_debugging
208 (cp_lexer *) ATTRIBUTE_UNUSED;
209 static void cp_lexer_stop_debugging
210 (cp_lexer *) ATTRIBUTE_UNUSED;
211 #else
212 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
213 about passing NULL to functions that require non-NULL arguments
214 (fputs, fprintf). It will never be used, so all we need is a value
215 of the right type that's guaranteed not to be NULL. */
216 #define cp_lexer_debug_stream stdout
217 #define cp_lexer_print_token(str, tok) (void) 0
218 #define cp_lexer_debugging_p(lexer) 0
219 #endif /* ENABLE_CHECKING */
221 static cp_token_cache *cp_token_cache_new
222 (cp_token *, cp_token *);
224 static void cp_parser_initial_pragma
225 (cp_token *);
227 /* Manifest constants. */
228 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
229 #define CP_SAVED_TOKEN_STACK 5
231 /* A token type for keywords, as opposed to ordinary identifiers. */
232 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
234 /* A token type for template-ids. If a template-id is processed while
235 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
236 the value of the CPP_TEMPLATE_ID is whatever was returned by
237 cp_parser_template_id. */
238 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
240 /* A token type for nested-name-specifiers. If a
241 nested-name-specifier is processed while parsing tentatively, it is
242 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
243 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
244 cp_parser_nested_name_specifier_opt. */
245 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
247 /* A token type for tokens that are not tokens at all; these are used
248 to represent slots in the array where there used to be a token
249 that has now been deleted. */
250 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
252 /* The number of token types, including C++-specific ones. */
253 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
255 /* Variables. */
257 #ifdef ENABLE_CHECKING
258 /* The stream to which debugging output should be written. */
259 static FILE *cp_lexer_debug_stream;
260 #endif /* ENABLE_CHECKING */
262 /* Create a new main C++ lexer, the lexer that gets tokens from the
263 preprocessor. */
265 static cp_lexer *
266 cp_lexer_new_main (void)
268 cp_token first_token;
269 cp_lexer *lexer;
270 cp_token *pos;
271 size_t alloc;
272 size_t space;
273 cp_token *buffer;
275 /* It's possible that parsing the first pragma will load a PCH file,
276 which is a GC collection point. So we have to do that before
277 allocating any memory. */
278 cp_parser_initial_pragma (&first_token);
280 c_common_no_more_pch ();
282 /* Allocate the memory. */
283 lexer = GGC_CNEW (cp_lexer);
285 #ifdef ENABLE_CHECKING
286 /* Initially we are not debugging. */
287 lexer->debugging_p = false;
288 #endif /* ENABLE_CHECKING */
289 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
290 CP_SAVED_TOKEN_STACK);
292 /* Create the buffer. */
293 alloc = CP_LEXER_BUFFER_SIZE;
294 buffer = GGC_NEWVEC (cp_token, alloc);
296 /* Put the first token in the buffer. */
297 space = alloc;
298 pos = buffer;
299 *pos = first_token;
301 /* Get the remaining tokens from the preprocessor. */
302 while (pos->type != CPP_EOF)
304 pos++;
305 if (!--space)
307 space = alloc;
308 alloc *= 2;
309 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
310 pos = buffer + space;
312 cp_lexer_get_preprocessor_token (lexer, pos);
314 lexer->buffer = buffer;
315 lexer->buffer_length = alloc - space;
316 lexer->last_token = pos;
317 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
319 /* Subsequent preprocessor diagnostics should use compiler
320 diagnostic functions to get the compiler source location. */
321 cpp_get_options (parse_in)->client_diagnostic = true;
322 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
324 gcc_assert (lexer->next_token->type != CPP_PURGED);
325 return lexer;
328 /* Create a new lexer whose token stream is primed with the tokens in
329 CACHE. When these tokens are exhausted, no new tokens will be read. */
331 static cp_lexer *
332 cp_lexer_new_from_tokens (cp_token_cache *cache)
334 cp_token *first = cache->first;
335 cp_token *last = cache->last;
336 cp_lexer *lexer = GGC_CNEW (cp_lexer);
338 /* We do not own the buffer. */
339 lexer->buffer = NULL;
340 lexer->buffer_length = 0;
341 lexer->next_token = first == last ? &eof_token : first;
342 lexer->last_token = last;
344 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
345 CP_SAVED_TOKEN_STACK);
347 #ifdef ENABLE_CHECKING
348 /* Initially we are not debugging. */
349 lexer->debugging_p = false;
350 #endif
352 gcc_assert (lexer->next_token->type != CPP_PURGED);
353 return lexer;
356 /* Frees all resources associated with LEXER. */
358 static void
359 cp_lexer_destroy (cp_lexer *lexer)
361 if (lexer->buffer)
362 ggc_free (lexer->buffer);
363 VEC_free (cp_token_position, heap, lexer->saved_tokens);
364 ggc_free (lexer);
367 /* Returns nonzero if debugging information should be output. */
369 #ifdef ENABLE_CHECKING
371 static inline bool
372 cp_lexer_debugging_p (cp_lexer *lexer)
374 return lexer->debugging_p;
377 #endif /* ENABLE_CHECKING */
379 static inline cp_token_position
380 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
382 gcc_assert (!previous_p || lexer->next_token != &eof_token);
384 return lexer->next_token - previous_p;
387 static inline cp_token *
388 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
390 return pos;
393 /* nonzero if we are presently saving tokens. */
395 static inline int
396 cp_lexer_saving_tokens (const cp_lexer* lexer)
398 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
401 /* Store the next token from the preprocessor in *TOKEN. Return true
402 if we reach EOF. If LEXER is NULL, assume we are handling an
403 initial #pragma pch_preprocess, and thus want the lexer to return
404 processed strings. */
406 static void
407 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
409 static int is_extern_c = 0;
411 /* Get a new token from the preprocessor. */
412 token->type
413 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
414 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
415 token->input_file_stack_index = input_file_stack_tick;
416 token->keyword = RID_MAX;
417 token->pragma_kind = PRAGMA_NONE;
418 token->in_system_header = in_system_header;
420 /* On some systems, some header files are surrounded by an
421 implicit extern "C" block. Set a flag in the token if it
422 comes from such a header. */
423 is_extern_c += pending_lang_change;
424 pending_lang_change = 0;
425 token->implicit_extern_c = is_extern_c > 0;
427 /* Check to see if this token is a keyword. */
428 if (token->type == CPP_NAME)
430 if (C_IS_RESERVED_WORD (token->u.value))
432 /* Mark this token as a keyword. */
433 token->type = CPP_KEYWORD;
434 /* Record which keyword. */
435 token->keyword = C_RID_CODE (token->u.value);
436 /* Update the value. Some keywords are mapped to particular
437 entities, rather than simply having the value of the
438 corresponding IDENTIFIER_NODE. For example, `__const' is
439 mapped to `const'. */
440 token->u.value = ridpointers[token->keyword];
442 else
444 if (warn_cxx0x_compat
445 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
446 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
448 /* Warn about the C++0x keyword (but still treat it as
449 an identifier). */
450 warning (OPT_Wc__0x_compat,
451 "identifier %<%s%> will become a keyword in C++0x",
452 IDENTIFIER_POINTER (token->u.value));
454 /* Clear out the C_RID_CODE so we don't warn about this
455 particular identifier-turned-keyword again. */
456 C_RID_CODE (token->u.value) = RID_MAX;
459 token->ambiguous_p = false;
460 token->keyword = RID_MAX;
463 /* Handle Objective-C++ keywords. */
464 else if (token->type == CPP_AT_NAME)
466 token->type = CPP_KEYWORD;
467 switch (C_RID_CODE (token->u.value))
469 /* Map 'class' to '@class', 'private' to '@private', etc. */
470 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
471 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
472 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
473 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
474 case RID_THROW: token->keyword = RID_AT_THROW; break;
475 case RID_TRY: token->keyword = RID_AT_TRY; break;
476 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
477 default: token->keyword = C_RID_CODE (token->u.value);
480 else if (token->type == CPP_PRAGMA)
482 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
483 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
484 token->u.value = NULL_TREE;
488 /* Update the globals input_location and in_system_header and the
489 input file stack from TOKEN. */
490 static inline void
491 cp_lexer_set_source_position_from_token (cp_token *token)
493 if (token->type != CPP_EOF)
495 input_location = token->location;
496 in_system_header = token->in_system_header;
497 restore_input_file_stack (token->input_file_stack_index);
501 /* Return a pointer to the next token in the token stream, but do not
502 consume it. */
504 static inline cp_token *
505 cp_lexer_peek_token (cp_lexer *lexer)
507 if (cp_lexer_debugging_p (lexer))
509 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
510 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
511 putc ('\n', cp_lexer_debug_stream);
513 return lexer->next_token;
516 /* Return true if the next token has the indicated TYPE. */
518 static inline bool
519 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
521 return cp_lexer_peek_token (lexer)->type == type;
524 /* Return true if the next token does not have the indicated TYPE. */
526 static inline bool
527 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
529 return !cp_lexer_next_token_is (lexer, type);
532 /* Return true if the next token is the indicated KEYWORD. */
534 static inline bool
535 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
537 return cp_lexer_peek_token (lexer)->keyword == keyword;
540 /* Return true if the next token is a keyword for a decl-specifier. */
542 static bool
543 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
545 cp_token *token;
547 token = cp_lexer_peek_token (lexer);
548 switch (token->keyword)
550 /* Storage classes. */
551 case RID_AUTO:
552 case RID_REGISTER:
553 case RID_STATIC:
554 case RID_EXTERN:
555 case RID_MUTABLE:
556 case RID_THREAD:
557 /* Elaborated type specifiers. */
558 case RID_ENUM:
559 case RID_CLASS:
560 case RID_STRUCT:
561 case RID_UNION:
562 case RID_TYPENAME:
563 /* Simple type specifiers. */
564 case RID_CHAR:
565 case RID_WCHAR:
566 case RID_BOOL:
567 case RID_SHORT:
568 case RID_INT:
569 case RID_LONG:
570 case RID_SIGNED:
571 case RID_UNSIGNED:
572 case RID_FLOAT:
573 case RID_DOUBLE:
574 case RID_VOID:
575 /* GNU extensions. */
576 case RID_ATTRIBUTE:
577 case RID_TYPEOF:
578 /* C++0x extensions. */
579 case RID_DECLTYPE:
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 = &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 = &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 = &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 *, bool);
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,
964 bool rvalue_ref)
966 cp_declarator *declarator;
968 declarator = make_declarator (cdk_reference);
969 declarator->declarator = target;
970 declarator->u.reference.qualifiers = cv_qualifiers;
971 declarator->u.reference.rvalue_ref = rvalue_ref;
972 if (target)
974 declarator->parameter_pack_p = target->parameter_pack_p;
975 target->parameter_pack_p = false;
977 else
978 declarator->parameter_pack_p = false;
980 return declarator;
983 /* Like make_pointer_declarator -- but for a pointer to a non-static
984 member of CLASS_TYPE. */
986 cp_declarator *
987 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
988 cp_declarator *pointee)
990 cp_declarator *declarator;
992 declarator = make_declarator (cdk_ptrmem);
993 declarator->declarator = pointee;
994 declarator->u.pointer.qualifiers = cv_qualifiers;
995 declarator->u.pointer.class_type = class_type;
997 if (pointee)
999 declarator->parameter_pack_p = pointee->parameter_pack_p;
1000 pointee->parameter_pack_p = false;
1002 else
1003 declarator->parameter_pack_p = false;
1005 return declarator;
1008 /* Make a declarator for the function given by TARGET, with the
1009 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1010 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1011 indicates what exceptions can be thrown. */
1013 cp_declarator *
1014 make_call_declarator (cp_declarator *target,
1015 cp_parameter_declarator *parms,
1016 cp_cv_quals cv_qualifiers,
1017 tree exception_specification)
1019 cp_declarator *declarator;
1021 declarator = make_declarator (cdk_function);
1022 declarator->declarator = target;
1023 declarator->u.function.parameters = parms;
1024 declarator->u.function.qualifiers = cv_qualifiers;
1025 declarator->u.function.exception_specification = exception_specification;
1026 if (target)
1028 declarator->parameter_pack_p = target->parameter_pack_p;
1029 target->parameter_pack_p = false;
1031 else
1032 declarator->parameter_pack_p = false;
1034 return declarator;
1037 /* Make a declarator for an array of BOUNDS elements, each of which is
1038 defined by ELEMENT. */
1040 cp_declarator *
1041 make_array_declarator (cp_declarator *element, tree bounds)
1043 cp_declarator *declarator;
1045 declarator = make_declarator (cdk_array);
1046 declarator->declarator = element;
1047 declarator->u.array.bounds = bounds;
1048 if (element)
1050 declarator->parameter_pack_p = element->parameter_pack_p;
1051 element->parameter_pack_p = false;
1053 else
1054 declarator->parameter_pack_p = false;
1056 return declarator;
1059 /* Determine whether the declarator we've seen so far can be a
1060 parameter pack, when followed by an ellipsis. */
1061 static bool
1062 declarator_can_be_parameter_pack (cp_declarator *declarator)
1064 /* Search for a declarator name, or any other declarator that goes
1065 after the point where the ellipsis could appear in a parameter
1066 pack. If we find any of these, then this declarator can not be
1067 made into a parameter pack. */
1068 bool found = false;
1069 while (declarator && !found)
1071 switch ((int)declarator->kind)
1073 case cdk_id:
1074 case cdk_array:
1075 found = true;
1076 break;
1078 case cdk_error:
1079 return true;
1081 default:
1082 declarator = declarator->declarator;
1083 break;
1087 return !found;
1090 cp_parameter_declarator *no_parameters;
1092 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1093 DECLARATOR and DEFAULT_ARGUMENT. */
1095 cp_parameter_declarator *
1096 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1097 cp_declarator *declarator,
1098 tree default_argument)
1100 cp_parameter_declarator *parameter;
1102 parameter = ((cp_parameter_declarator *)
1103 alloc_declarator (sizeof (cp_parameter_declarator)));
1104 parameter->next = NULL;
1105 if (decl_specifiers)
1106 parameter->decl_specifiers = *decl_specifiers;
1107 else
1108 clear_decl_specs (&parameter->decl_specifiers);
1109 parameter->declarator = declarator;
1110 parameter->default_argument = default_argument;
1111 parameter->ellipsis_p = false;
1113 return parameter;
1116 /* Returns true iff DECLARATOR is a declaration for a function. */
1118 static bool
1119 function_declarator_p (const cp_declarator *declarator)
1121 while (declarator)
1123 if (declarator->kind == cdk_function
1124 && declarator->declarator->kind == cdk_id)
1125 return true;
1126 if (declarator->kind == cdk_id
1127 || declarator->kind == cdk_error)
1128 return false;
1129 declarator = declarator->declarator;
1131 return false;
1134 /* The parser. */
1136 /* Overview
1137 --------
1139 A cp_parser parses the token stream as specified by the C++
1140 grammar. Its job is purely parsing, not semantic analysis. For
1141 example, the parser breaks the token stream into declarators,
1142 expressions, statements, and other similar syntactic constructs.
1143 It does not check that the types of the expressions on either side
1144 of an assignment-statement are compatible, or that a function is
1145 not declared with a parameter of type `void'.
1147 The parser invokes routines elsewhere in the compiler to perform
1148 semantic analysis and to build up the abstract syntax tree for the
1149 code processed.
1151 The parser (and the template instantiation code, which is, in a
1152 way, a close relative of parsing) are the only parts of the
1153 compiler that should be calling push_scope and pop_scope, or
1154 related functions. The parser (and template instantiation code)
1155 keeps track of what scope is presently active; everything else
1156 should simply honor that. (The code that generates static
1157 initializers may also need to set the scope, in order to check
1158 access control correctly when emitting the initializers.)
1160 Methodology
1161 -----------
1163 The parser is of the standard recursive-descent variety. Upcoming
1164 tokens in the token stream are examined in order to determine which
1165 production to use when parsing a non-terminal. Some C++ constructs
1166 require arbitrary look ahead to disambiguate. For example, it is
1167 impossible, in the general case, to tell whether a statement is an
1168 expression or declaration without scanning the entire statement.
1169 Therefore, the parser is capable of "parsing tentatively." When the
1170 parser is not sure what construct comes next, it enters this mode.
1171 Then, while we attempt to parse the construct, the parser queues up
1172 error messages, rather than issuing them immediately, and saves the
1173 tokens it consumes. If the construct is parsed successfully, the
1174 parser "commits", i.e., it issues any queued error messages and
1175 the tokens that were being preserved are permanently discarded.
1176 If, however, the construct is not parsed successfully, the parser
1177 rolls back its state completely so that it can resume parsing using
1178 a different alternative.
1180 Future Improvements
1181 -------------------
1183 The performance of the parser could probably be improved substantially.
1184 We could often eliminate the need to parse tentatively by looking ahead
1185 a little bit. In some places, this approach might not entirely eliminate
1186 the need to parse tentatively, but it might still speed up the average
1187 case. */
1189 /* Flags that are passed to some parsing functions. These values can
1190 be bitwise-ored together. */
1192 typedef enum cp_parser_flags
1194 /* No flags. */
1195 CP_PARSER_FLAGS_NONE = 0x0,
1196 /* The construct is optional. If it is not present, then no error
1197 should be issued. */
1198 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1199 /* When parsing a type-specifier, do not allow user-defined types. */
1200 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1201 } cp_parser_flags;
1203 /* The different kinds of declarators we want to parse. */
1205 typedef enum cp_parser_declarator_kind
1207 /* We want an abstract declarator. */
1208 CP_PARSER_DECLARATOR_ABSTRACT,
1209 /* We want a named declarator. */
1210 CP_PARSER_DECLARATOR_NAMED,
1211 /* We don't mind, but the name must be an unqualified-id. */
1212 CP_PARSER_DECLARATOR_EITHER
1213 } cp_parser_declarator_kind;
1215 /* The precedence values used to parse binary expressions. The minimum value
1216 of PREC must be 1, because zero is reserved to quickly discriminate
1217 binary operators from other tokens. */
1219 enum cp_parser_prec
1221 PREC_NOT_OPERATOR,
1222 PREC_LOGICAL_OR_EXPRESSION,
1223 PREC_LOGICAL_AND_EXPRESSION,
1224 PREC_INCLUSIVE_OR_EXPRESSION,
1225 PREC_EXCLUSIVE_OR_EXPRESSION,
1226 PREC_AND_EXPRESSION,
1227 PREC_EQUALITY_EXPRESSION,
1228 PREC_RELATIONAL_EXPRESSION,
1229 PREC_SHIFT_EXPRESSION,
1230 PREC_ADDITIVE_EXPRESSION,
1231 PREC_MULTIPLICATIVE_EXPRESSION,
1232 PREC_PM_EXPRESSION,
1233 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1236 /* A mapping from a token type to a corresponding tree node type, with a
1237 precedence value. */
1239 typedef struct cp_parser_binary_operations_map_node
1241 /* The token type. */
1242 enum cpp_ttype token_type;
1243 /* The corresponding tree code. */
1244 enum tree_code tree_type;
1245 /* The precedence of this operator. */
1246 enum cp_parser_prec prec;
1247 } cp_parser_binary_operations_map_node;
1249 /* The status of a tentative parse. */
1251 typedef enum cp_parser_status_kind
1253 /* No errors have occurred. */
1254 CP_PARSER_STATUS_KIND_NO_ERROR,
1255 /* An error has occurred. */
1256 CP_PARSER_STATUS_KIND_ERROR,
1257 /* We are committed to this tentative parse, whether or not an error
1258 has occurred. */
1259 CP_PARSER_STATUS_KIND_COMMITTED
1260 } cp_parser_status_kind;
1262 typedef struct cp_parser_expression_stack_entry
1264 /* Left hand side of the binary operation we are currently
1265 parsing. */
1266 tree lhs;
1267 /* Original tree code for left hand side, if it was a binary
1268 expression itself (used for -Wparentheses). */
1269 enum tree_code lhs_type;
1270 /* Tree code for the binary operation we are parsing. */
1271 enum tree_code tree_type;
1272 /* Precedence of the binary operation we are parsing. */
1273 int prec;
1274 } cp_parser_expression_stack_entry;
1276 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1277 entries because precedence levels on the stack are monotonically
1278 increasing. */
1279 typedef struct cp_parser_expression_stack_entry
1280 cp_parser_expression_stack[NUM_PREC_VALUES];
1282 /* Context that is saved and restored when parsing tentatively. */
1283 typedef struct cp_parser_context GTY (())
1285 /* If this is a tentative parsing context, the status of the
1286 tentative parse. */
1287 enum cp_parser_status_kind status;
1288 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1289 that are looked up in this context must be looked up both in the
1290 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1291 the context of the containing expression. */
1292 tree object_type;
1294 /* The next parsing context in the stack. */
1295 struct cp_parser_context *next;
1296 } cp_parser_context;
1298 /* Prototypes. */
1300 /* Constructors and destructors. */
1302 static cp_parser_context *cp_parser_context_new
1303 (cp_parser_context *);
1305 /* Class variables. */
1307 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1309 /* The operator-precedence table used by cp_parser_binary_expression.
1310 Transformed into an associative array (binops_by_token) by
1311 cp_parser_new. */
1313 static const cp_parser_binary_operations_map_node binops[] = {
1314 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1315 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1317 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1318 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1322 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1324 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1325 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1327 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1328 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1330 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1332 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1333 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1335 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1337 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1339 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1341 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1343 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1346 /* The same as binops, but initialized by cp_parser_new so that
1347 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1348 for speed. */
1349 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1351 /* Constructors and destructors. */
1353 /* Construct a new context. The context below this one on the stack
1354 is given by NEXT. */
1356 static cp_parser_context *
1357 cp_parser_context_new (cp_parser_context* next)
1359 cp_parser_context *context;
1361 /* Allocate the storage. */
1362 if (cp_parser_context_free_list != NULL)
1364 /* Pull the first entry from the free list. */
1365 context = cp_parser_context_free_list;
1366 cp_parser_context_free_list = context->next;
1367 memset (context, 0, sizeof (*context));
1369 else
1370 context = GGC_CNEW (cp_parser_context);
1372 /* No errors have occurred yet in this context. */
1373 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1374 /* If this is not the bottomost context, copy information that we
1375 need from the previous context. */
1376 if (next)
1378 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1379 expression, then we are parsing one in this context, too. */
1380 context->object_type = next->object_type;
1381 /* Thread the stack. */
1382 context->next = next;
1385 return context;
1388 /* The cp_parser structure represents the C++ parser. */
1390 typedef struct cp_parser GTY(())
1392 /* The lexer from which we are obtaining tokens. */
1393 cp_lexer *lexer;
1395 /* The scope in which names should be looked up. If NULL_TREE, then
1396 we look up names in the scope that is currently open in the
1397 source program. If non-NULL, this is either a TYPE or
1398 NAMESPACE_DECL for the scope in which we should look. It can
1399 also be ERROR_MARK, when we've parsed a bogus scope.
1401 This value is not cleared automatically after a name is looked
1402 up, so we must be careful to clear it before starting a new look
1403 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1404 will look up `Z' in the scope of `X', rather than the current
1405 scope.) Unfortunately, it is difficult to tell when name lookup
1406 is complete, because we sometimes peek at a token, look it up,
1407 and then decide not to consume it. */
1408 tree scope;
1410 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1411 last lookup took place. OBJECT_SCOPE is used if an expression
1412 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1413 respectively. QUALIFYING_SCOPE is used for an expression of the
1414 form "X::Y"; it refers to X. */
1415 tree object_scope;
1416 tree qualifying_scope;
1418 /* A stack of parsing contexts. All but the bottom entry on the
1419 stack will be tentative contexts.
1421 We parse tentatively in order to determine which construct is in
1422 use in some situations. For example, in order to determine
1423 whether a statement is an expression-statement or a
1424 declaration-statement we parse it tentatively as a
1425 declaration-statement. If that fails, we then reparse the same
1426 token stream as an expression-statement. */
1427 cp_parser_context *context;
1429 /* True if we are parsing GNU C++. If this flag is not set, then
1430 GNU extensions are not recognized. */
1431 bool allow_gnu_extensions_p;
1433 /* TRUE if the `>' token should be interpreted as the greater-than
1434 operator. FALSE if it is the end of a template-id or
1435 template-parameter-list. In C++0x mode, this flag also applies to
1436 `>>' tokens, which are viewed as two consecutive `>' tokens when
1437 this flag is FALSE. */
1438 bool greater_than_is_operator_p;
1440 /* TRUE if default arguments are allowed within a parameter list
1441 that starts at this point. FALSE if only a gnu extension makes
1442 them permissible. */
1443 bool default_arg_ok_p;
1445 /* TRUE if we are parsing an integral constant-expression. See
1446 [expr.const] for a precise definition. */
1447 bool integral_constant_expression_p;
1449 /* TRUE if we are parsing an integral constant-expression -- but a
1450 non-constant expression should be permitted as well. This flag
1451 is used when parsing an array bound so that GNU variable-length
1452 arrays are tolerated. */
1453 bool allow_non_integral_constant_expression_p;
1455 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1456 been seen that makes the expression non-constant. */
1457 bool non_integral_constant_expression_p;
1459 /* TRUE if local variable names and `this' are forbidden in the
1460 current context. */
1461 bool local_variables_forbidden_p;
1463 /* TRUE if the declaration we are parsing is part of a
1464 linkage-specification of the form `extern string-literal
1465 declaration'. */
1466 bool in_unbraced_linkage_specification_p;
1468 /* TRUE if we are presently parsing a declarator, after the
1469 direct-declarator. */
1470 bool in_declarator_p;
1472 /* TRUE if we are presently parsing a template-argument-list. */
1473 bool in_template_argument_list_p;
1475 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1476 to IN_OMP_BLOCK if parsing OpenMP structured block and
1477 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1478 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1479 iteration-statement, OpenMP block or loop within that switch. */
1480 #define IN_SWITCH_STMT 1
1481 #define IN_ITERATION_STMT 2
1482 #define IN_OMP_BLOCK 4
1483 #define IN_OMP_FOR 8
1484 #define IN_IF_STMT 16
1485 unsigned char in_statement;
1487 /* TRUE if we are presently parsing the body of a switch statement.
1488 Note that this doesn't quite overlap with in_statement above.
1489 The difference relates to giving the right sets of error messages:
1490 "case not in switch" vs "break statement used with OpenMP...". */
1491 bool in_switch_statement_p;
1493 /* TRUE if we are parsing a type-id in an expression context. In
1494 such a situation, both "type (expr)" and "type (type)" are valid
1495 alternatives. */
1496 bool in_type_id_in_expr_p;
1498 /* TRUE if we are currently in a header file where declarations are
1499 implicitly extern "C". */
1500 bool implicit_extern_c;
1502 /* TRUE if strings in expressions should be translated to the execution
1503 character set. */
1504 bool translate_strings_p;
1506 /* TRUE if we are presently parsing the body of a function, but not
1507 a local class. */
1508 bool in_function_body;
1510 /* If non-NULL, then we are parsing a construct where new type
1511 definitions are not permitted. The string stored here will be
1512 issued as an error message if a type is defined. */
1513 const char *type_definition_forbidden_message;
1515 /* A list of lists. The outer list is a stack, used for member
1516 functions of local classes. At each level there are two sub-list,
1517 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1518 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1519 TREE_VALUE's. The functions are chained in reverse declaration
1520 order.
1522 The TREE_PURPOSE sublist contains those functions with default
1523 arguments that need post processing, and the TREE_VALUE sublist
1524 contains those functions with definitions that need post
1525 processing.
1527 These lists can only be processed once the outermost class being
1528 defined is complete. */
1529 tree unparsed_functions_queues;
1531 /* The number of classes whose definitions are currently in
1532 progress. */
1533 unsigned num_classes_being_defined;
1535 /* The number of template parameter lists that apply directly to the
1536 current declaration. */
1537 unsigned num_template_parameter_lists;
1538 } cp_parser;
1540 /* Prototypes. */
1542 /* Constructors and destructors. */
1544 static cp_parser *cp_parser_new
1545 (void);
1547 /* Routines to parse various constructs.
1549 Those that return `tree' will return the error_mark_node (rather
1550 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1551 Sometimes, they will return an ordinary node if error-recovery was
1552 attempted, even though a parse error occurred. So, to check
1553 whether or not a parse error occurred, you should always use
1554 cp_parser_error_occurred. If the construct is optional (indicated
1555 either by an `_opt' in the name of the function that does the
1556 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1557 the construct is not present. */
1559 /* Lexical conventions [gram.lex] */
1561 static tree cp_parser_identifier
1562 (cp_parser *);
1563 static tree cp_parser_string_literal
1564 (cp_parser *, bool, bool);
1566 /* Basic concepts [gram.basic] */
1568 static bool cp_parser_translation_unit
1569 (cp_parser *);
1571 /* Expressions [gram.expr] */
1573 static tree cp_parser_primary_expression
1574 (cp_parser *, bool, bool, bool, cp_id_kind *);
1575 static tree cp_parser_id_expression
1576 (cp_parser *, bool, bool, bool *, bool, bool);
1577 static tree cp_parser_unqualified_id
1578 (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier_opt
1580 (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_nested_name_specifier
1582 (cp_parser *, bool, bool, bool, bool);
1583 static tree cp_parser_class_or_namespace_name
1584 (cp_parser *, bool, bool, bool, bool, bool);
1585 static tree cp_parser_postfix_expression
1586 (cp_parser *, bool, bool, bool);
1587 static tree cp_parser_postfix_open_square_expression
1588 (cp_parser *, tree, bool);
1589 static tree cp_parser_postfix_dot_deref_expression
1590 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1591 static tree cp_parser_parenthesized_expression_list
1592 (cp_parser *, bool, bool, bool, bool *);
1593 static void cp_parser_pseudo_destructor_name
1594 (cp_parser *, tree *, tree *);
1595 static tree cp_parser_unary_expression
1596 (cp_parser *, bool, bool);
1597 static enum tree_code cp_parser_unary_operator
1598 (cp_token *);
1599 static tree cp_parser_new_expression
1600 (cp_parser *);
1601 static tree cp_parser_new_placement
1602 (cp_parser *);
1603 static tree cp_parser_new_type_id
1604 (cp_parser *, tree *);
1605 static cp_declarator *cp_parser_new_declarator_opt
1606 (cp_parser *);
1607 static cp_declarator *cp_parser_direct_new_declarator
1608 (cp_parser *);
1609 static tree cp_parser_new_initializer
1610 (cp_parser *);
1611 static tree cp_parser_delete_expression
1612 (cp_parser *);
1613 static tree cp_parser_cast_expression
1614 (cp_parser *, bool, bool);
1615 static tree cp_parser_binary_expression
1616 (cp_parser *, bool);
1617 static tree cp_parser_question_colon_clause
1618 (cp_parser *, tree);
1619 static tree cp_parser_assignment_expression
1620 (cp_parser *, bool);
1621 static enum tree_code cp_parser_assignment_operator_opt
1622 (cp_parser *);
1623 static tree cp_parser_expression
1624 (cp_parser *, bool);
1625 static tree cp_parser_constant_expression
1626 (cp_parser *, bool, bool *);
1627 static tree cp_parser_builtin_offsetof
1628 (cp_parser *);
1630 /* Statements [gram.stmt.stmt] */
1632 static void cp_parser_statement
1633 (cp_parser *, tree, bool, bool *);
1634 static void cp_parser_label_for_labeled_statement
1635 (cp_parser *);
1636 static tree cp_parser_expression_statement
1637 (cp_parser *, tree);
1638 static tree cp_parser_compound_statement
1639 (cp_parser *, tree, bool);
1640 static void cp_parser_statement_seq_opt
1641 (cp_parser *, tree);
1642 static tree cp_parser_selection_statement
1643 (cp_parser *, bool *);
1644 static tree cp_parser_condition
1645 (cp_parser *);
1646 static tree cp_parser_iteration_statement
1647 (cp_parser *);
1648 static void cp_parser_for_init_statement
1649 (cp_parser *);
1650 static tree cp_parser_jump_statement
1651 (cp_parser *);
1652 static void cp_parser_declaration_statement
1653 (cp_parser *);
1655 static tree cp_parser_implicitly_scoped_statement
1656 (cp_parser *, bool *);
1657 static void cp_parser_already_scoped_statement
1658 (cp_parser *);
1660 /* Declarations [gram.dcl.dcl] */
1662 static void cp_parser_declaration_seq_opt
1663 (cp_parser *);
1664 static void cp_parser_declaration
1665 (cp_parser *);
1666 static void cp_parser_block_declaration
1667 (cp_parser *, bool);
1668 static void cp_parser_simple_declaration
1669 (cp_parser *, bool);
1670 static void cp_parser_decl_specifier_seq
1671 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1672 static tree cp_parser_storage_class_specifier_opt
1673 (cp_parser *);
1674 static tree cp_parser_function_specifier_opt
1675 (cp_parser *, cp_decl_specifier_seq *);
1676 static tree cp_parser_type_specifier
1677 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1678 int *, bool *);
1679 static tree cp_parser_simple_type_specifier
1680 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1681 static tree cp_parser_type_name
1682 (cp_parser *);
1683 static tree cp_parser_elaborated_type_specifier
1684 (cp_parser *, bool, bool);
1685 static tree cp_parser_enum_specifier
1686 (cp_parser *);
1687 static void cp_parser_enumerator_list
1688 (cp_parser *, tree);
1689 static void cp_parser_enumerator_definition
1690 (cp_parser *, tree);
1691 static tree cp_parser_namespace_name
1692 (cp_parser *);
1693 static void cp_parser_namespace_definition
1694 (cp_parser *);
1695 static void cp_parser_namespace_body
1696 (cp_parser *);
1697 static tree cp_parser_qualified_namespace_specifier
1698 (cp_parser *);
1699 static void cp_parser_namespace_alias_definition
1700 (cp_parser *);
1701 static bool cp_parser_using_declaration
1702 (cp_parser *, bool);
1703 static void cp_parser_using_directive
1704 (cp_parser *);
1705 static void cp_parser_asm_definition
1706 (cp_parser *);
1707 static void cp_parser_linkage_specification
1708 (cp_parser *);
1709 static void cp_parser_static_assert
1710 (cp_parser *, bool);
1711 static tree cp_parser_decltype
1712 (cp_parser *);
1713 static void cp_parser_concept_definition
1714 (cp_parser *);
1716 /* Declarators [gram.dcl.decl] */
1718 static tree cp_parser_init_declarator
1719 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1720 static cp_declarator *cp_parser_declarator
1721 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1722 static cp_declarator *cp_parser_direct_declarator
1723 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1724 static enum tree_code cp_parser_ptr_operator
1725 (cp_parser *, tree *, cp_cv_quals *);
1726 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1727 (cp_parser *);
1728 static tree cp_parser_declarator_id
1729 (cp_parser *, bool);
1730 static tree cp_parser_type_id
1731 (cp_parser *);
1732 static void cp_parser_type_specifier_seq
1733 (cp_parser *, bool, cp_decl_specifier_seq *);
1734 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1735 (cp_parser *);
1736 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1737 (cp_parser *, bool *);
1738 static cp_parameter_declarator *cp_parser_parameter_declaration
1739 (cp_parser *, bool, bool *);
1740 static tree cp_parser_default_argument
1741 (cp_parser *, bool);
1742 static void cp_parser_function_body
1743 (cp_parser *);
1744 static tree cp_parser_initializer
1745 (cp_parser *, bool *, bool *);
1746 static tree cp_parser_initializer_clause
1747 (cp_parser *, bool *);
1748 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1749 (cp_parser *, bool *);
1751 static bool cp_parser_ctor_initializer_opt_and_function_body
1752 (cp_parser *);
1754 /* Classes [gram.class] */
1756 static tree cp_parser_class_name
1757 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1758 static tree cp_parser_class_specifier
1759 (cp_parser *);
1760 static tree cp_parser_class_head
1761 (cp_parser *, bool *, tree *, tree *);
1762 static enum tag_types cp_parser_class_key
1763 (cp_parser *);
1764 static void cp_parser_member_specification_opt
1765 (cp_parser *);
1766 static void cp_parser_member_declaration
1767 (cp_parser *);
1768 static tree cp_parser_pure_specifier
1769 (cp_parser *);
1770 static tree cp_parser_constant_initializer
1771 (cp_parser *);
1773 /* Derived classes [gram.class.derived] */
1775 static tree cp_parser_base_clause
1776 (cp_parser *);
1777 static tree cp_parser_base_specifier
1778 (cp_parser *);
1780 /* Special member functions [gram.special] */
1782 static tree cp_parser_conversion_function_id
1783 (cp_parser *);
1784 static tree cp_parser_conversion_type_id
1785 (cp_parser *);
1786 static cp_declarator *cp_parser_conversion_declarator_opt
1787 (cp_parser *);
1788 static bool cp_parser_ctor_initializer_opt
1789 (cp_parser *);
1790 static void cp_parser_mem_initializer_list
1791 (cp_parser *);
1792 static tree cp_parser_mem_initializer
1793 (cp_parser *);
1794 static tree cp_parser_mem_initializer_id
1795 (cp_parser *);
1797 /* Overloading [gram.over] */
1799 static tree cp_parser_operator_function_id
1800 (cp_parser *);
1801 static tree cp_parser_operator
1802 (cp_parser *);
1804 /* Templates [gram.temp] */
1806 static void cp_parser_template_declaration
1807 (cp_parser *, bool);
1808 static tree cp_parser_template_parameter_list
1809 (cp_parser *);
1810 static tree cp_parser_template_parameter
1811 (cp_parser *, bool *, bool *);
1812 static tree cp_parser_type_parameter
1813 (cp_parser *, bool *);
1814 static tree cp_parser_template_id
1815 (cp_parser *, bool, bool, bool);
1816 static tree cp_parser_template_name
1817 (cp_parser *, bool, bool, bool, bool *);
1818 static tree cp_parser_template_argument_list
1819 (cp_parser *);
1820 static tree cp_parser_template_argument
1821 (cp_parser *);
1822 static void cp_parser_explicit_instantiation
1823 (cp_parser *);
1824 static void cp_parser_explicit_specialization
1825 (cp_parser *);
1826 static tree cp_parser_refinement_clause
1827 (cp_parser *);
1828 static tree cp_parser_refinement_specifier
1829 (cp_parser *);
1830 static void cp_parser_concept_body
1831 (cp_parser *);
1833 /* Exception handling [gram.exception] */
1835 static tree cp_parser_try_block
1836 (cp_parser *);
1837 static bool cp_parser_function_try_block
1838 (cp_parser *);
1839 static void cp_parser_handler_seq
1840 (cp_parser *);
1841 static void cp_parser_handler
1842 (cp_parser *);
1843 static tree cp_parser_exception_declaration
1844 (cp_parser *);
1845 static tree cp_parser_throw_expression
1846 (cp_parser *);
1847 static tree cp_parser_exception_specification_opt
1848 (cp_parser *);
1849 static tree cp_parser_type_id_list
1850 (cp_parser *);
1852 /* GNU Extensions */
1854 static tree cp_parser_asm_specification_opt
1855 (cp_parser *);
1856 static tree cp_parser_asm_operand_list
1857 (cp_parser *);
1858 static tree cp_parser_asm_clobber_list
1859 (cp_parser *);
1860 static tree cp_parser_attributes_opt
1861 (cp_parser *);
1862 static tree cp_parser_attribute_list
1863 (cp_parser *);
1864 static bool cp_parser_extension_opt
1865 (cp_parser *, int *);
1866 static void cp_parser_label_declaration
1867 (cp_parser *);
1869 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1870 static bool cp_parser_pragma
1871 (cp_parser *, enum pragma_context);
1873 /* Objective-C++ Productions */
1875 static tree cp_parser_objc_message_receiver
1876 (cp_parser *);
1877 static tree cp_parser_objc_message_args
1878 (cp_parser *);
1879 static tree cp_parser_objc_message_expression
1880 (cp_parser *);
1881 static tree cp_parser_objc_encode_expression
1882 (cp_parser *);
1883 static tree cp_parser_objc_defs_expression
1884 (cp_parser *);
1885 static tree cp_parser_objc_protocol_expression
1886 (cp_parser *);
1887 static tree cp_parser_objc_selector_expression
1888 (cp_parser *);
1889 static tree cp_parser_objc_expression
1890 (cp_parser *);
1891 static bool cp_parser_objc_selector_p
1892 (enum cpp_ttype);
1893 static tree cp_parser_objc_selector
1894 (cp_parser *);
1895 static tree cp_parser_objc_protocol_refs_opt
1896 (cp_parser *);
1897 static void cp_parser_objc_declaration
1898 (cp_parser *);
1899 static tree cp_parser_objc_statement
1900 (cp_parser *);
1902 /* Utility Routines */
1904 static tree cp_parser_lookup_name
1905 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1906 static tree cp_parser_lookup_name_simple
1907 (cp_parser *, tree);
1908 static tree cp_parser_maybe_treat_template_as_class
1909 (tree, bool);
1910 static bool cp_parser_check_declarator_template_parameters
1911 (cp_parser *, cp_declarator *);
1912 static bool cp_parser_check_template_parameters
1913 (cp_parser *, unsigned);
1914 static tree cp_parser_simple_cast_expression
1915 (cp_parser *);
1916 static tree cp_parser_global_scope_opt
1917 (cp_parser *, bool);
1918 static bool cp_parser_constructor_declarator_p
1919 (cp_parser *, bool);
1920 static tree cp_parser_function_definition_from_specifiers_and_declarator
1921 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1922 static tree cp_parser_function_definition_after_declarator
1923 (cp_parser *, bool);
1924 static void cp_parser_template_declaration_after_export
1925 (cp_parser *, bool);
1926 static void cp_parser_perform_template_parameter_access_checks
1927 (VEC (deferred_access_check,gc)*);
1928 static tree cp_parser_single_declaration
1929 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1930 static tree cp_parser_functional_cast
1931 (cp_parser *, tree);
1932 static tree cp_parser_save_member_function_body
1933 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1934 static tree cp_parser_enclosed_template_argument_list
1935 (cp_parser *);
1936 static void cp_parser_save_default_args
1937 (cp_parser *, tree);
1938 static void cp_parser_late_parsing_for_member
1939 (cp_parser *, tree);
1940 static void cp_parser_late_parsing_default_args
1941 (cp_parser *, tree);
1942 static tree cp_parser_sizeof_operand
1943 (cp_parser *, enum rid);
1944 static tree cp_parser_trait_expr
1945 (cp_parser *, enum rid);
1946 static bool cp_parser_declares_only_class_p
1947 (cp_parser *);
1948 static void cp_parser_set_storage_class
1949 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1950 static void cp_parser_set_decl_spec_type
1951 (cp_decl_specifier_seq *, tree, bool);
1952 static bool cp_parser_friend_p
1953 (const cp_decl_specifier_seq *);
1954 static cp_token *cp_parser_require
1955 (cp_parser *, enum cpp_ttype, const char *);
1956 static cp_token *cp_parser_require_keyword
1957 (cp_parser *, enum rid, const char *);
1958 static bool cp_parser_token_starts_function_definition_p
1959 (cp_token *);
1960 static bool cp_parser_next_token_starts_class_definition_p
1961 (cp_parser *);
1962 static bool cp_parser_next_token_ends_template_argument_p
1963 (cp_parser *);
1964 static bool cp_parser_nth_token_starts_template_argument_list_p
1965 (cp_parser *, size_t);
1966 static enum tag_types cp_parser_token_is_class_key
1967 (cp_token *);
1968 static void cp_parser_check_class_key
1969 (enum tag_types, tree type);
1970 static void cp_parser_check_access_in_redeclaration
1971 (tree type);
1972 static bool cp_parser_optional_template_keyword
1973 (cp_parser *);
1974 static void cp_parser_pre_parsed_nested_name_specifier
1975 (cp_parser *);
1976 static void cp_parser_cache_group
1977 (cp_parser *, enum cpp_ttype, unsigned);
1978 static void cp_parser_parse_tentatively
1979 (cp_parser *);
1980 static void cp_parser_commit_to_tentative_parse
1981 (cp_parser *);
1982 static void cp_parser_abort_tentative_parse
1983 (cp_parser *);
1984 static bool cp_parser_parse_definitely
1985 (cp_parser *);
1986 static inline bool cp_parser_parsing_tentatively
1987 (cp_parser *);
1988 static bool cp_parser_uncommitted_to_tentative_parse_p
1989 (cp_parser *);
1990 static void cp_parser_error
1991 (cp_parser *, const char *);
1992 static void cp_parser_name_lookup_error
1993 (cp_parser *, tree, tree, const char *);
1994 static bool cp_parser_simulate_error
1995 (cp_parser *);
1996 static bool cp_parser_check_type_definition
1997 (cp_parser *);
1998 static void cp_parser_check_for_definition_in_return_type
1999 (cp_declarator *, tree);
2000 static void cp_parser_check_for_invalid_template_id
2001 (cp_parser *, tree);
2002 static bool cp_parser_non_integral_constant_expression
2003 (cp_parser *, const char *);
2004 static void cp_parser_diagnose_invalid_type_name
2005 (cp_parser *, tree, tree);
2006 static bool cp_parser_parse_and_diagnose_invalid_type_name
2007 (cp_parser *);
2008 static int cp_parser_skip_to_closing_parenthesis
2009 (cp_parser *, bool, bool, bool);
2010 static void cp_parser_skip_to_end_of_statement
2011 (cp_parser *);
2012 static void cp_parser_consume_semicolon_at_end_of_statement
2013 (cp_parser *);
2014 static void cp_parser_skip_to_end_of_block_or_statement
2015 (cp_parser *);
2016 static bool cp_parser_skip_to_closing_brace
2017 (cp_parser *);
2018 static void cp_parser_skip_to_end_of_template_parameter_list
2019 (cp_parser *);
2020 static void cp_parser_skip_to_pragma_eol
2021 (cp_parser*, cp_token *);
2022 static bool cp_parser_error_occurred
2023 (cp_parser *);
2024 static bool cp_parser_allow_gnu_extensions_p
2025 (cp_parser *);
2026 static bool cp_parser_is_string_literal
2027 (cp_token *);
2028 static bool cp_parser_is_keyword
2029 (cp_token *, enum rid);
2030 static tree cp_parser_make_typename_type
2031 (cp_parser *, tree, tree);
2032 static cp_declarator * cp_parser_make_indirect_declarator
2033 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2035 /* Returns nonzero if we are parsing tentatively. */
2037 static inline bool
2038 cp_parser_parsing_tentatively (cp_parser* parser)
2040 return parser->context->next != NULL;
2043 /* Returns nonzero if TOKEN is a string literal. */
2045 static bool
2046 cp_parser_is_string_literal (cp_token* token)
2048 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2051 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2053 static bool
2054 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2056 return token->keyword == keyword;
2059 /* If not parsing tentatively, issue a diagnostic of the form
2060 FILE:LINE: MESSAGE before TOKEN
2061 where TOKEN is the next token in the input stream. MESSAGE
2062 (specified by the caller) is usually of the form "expected
2063 OTHER-TOKEN". */
2065 static void
2066 cp_parser_error (cp_parser* parser, const char* message)
2068 if (!cp_parser_simulate_error (parser))
2070 cp_token *token = cp_lexer_peek_token (parser->lexer);
2071 /* This diagnostic makes more sense if it is tagged to the line
2072 of the token we just peeked at. */
2073 cp_lexer_set_source_position_from_token (token);
2075 if (token->type == CPP_PRAGMA)
2077 error ("%<#pragma%> is not allowed here");
2078 cp_parser_skip_to_pragma_eol (parser, token);
2079 return;
2082 c_parse_error (message,
2083 /* Because c_parser_error does not understand
2084 CPP_KEYWORD, keywords are treated like
2085 identifiers. */
2086 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2087 token->u.value);
2091 /* Issue an error about name-lookup failing. NAME is the
2092 IDENTIFIER_NODE DECL is the result of
2093 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2094 the thing that we hoped to find. */
2096 static void
2097 cp_parser_name_lookup_error (cp_parser* parser,
2098 tree name,
2099 tree decl,
2100 const char* desired)
2102 /* If name lookup completely failed, tell the user that NAME was not
2103 declared. */
2104 if (decl == error_mark_node)
2106 if (parser->scope && parser->scope != global_namespace)
2107 error ("%<%E::%E%> has not been declared",
2108 parser->scope, name);
2109 else if (parser->scope == global_namespace)
2110 error ("%<::%E%> has not been declared", name);
2111 else if (parser->object_scope
2112 && !CLASS_TYPE_P (parser->object_scope))
2113 error ("request for member %qE in non-class type %qT",
2114 name, parser->object_scope);
2115 else if (parser->object_scope)
2116 error ("%<%T::%E%> has not been declared",
2117 parser->object_scope, name);
2118 else
2119 error ("%qE has not been declared", name);
2121 else if (parser->scope && parser->scope != global_namespace)
2122 error ("%<%E::%E%> %s", parser->scope, name, desired);
2123 else if (parser->scope == global_namespace)
2124 error ("%<::%E%> %s", name, desired);
2125 else
2126 error ("%qE %s", name, desired);
2129 /* If we are parsing tentatively, remember that an error has occurred
2130 during this tentative parse. Returns true if the error was
2131 simulated; false if a message should be issued by the caller. */
2133 static bool
2134 cp_parser_simulate_error (cp_parser* parser)
2136 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2138 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2139 return true;
2141 return false;
2144 /* Check for repeated decl-specifiers. */
2146 static void
2147 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2149 cp_decl_spec ds;
2151 for (ds = ds_first; ds != ds_last; ++ds)
2153 unsigned count = decl_specs->specs[(int)ds];
2154 if (count < 2)
2155 continue;
2156 /* The "long" specifier is a special case because of "long long". */
2157 if (ds == ds_long)
2159 if (count > 2)
2160 error ("%<long long long%> is too long for GCC");
2161 else if (pedantic && !in_system_header && warn_long_long
2162 && cxx_dialect == cxx98)
2163 pedwarn ("ISO C++ 1998 does not support %<long long%>");
2165 else if (count > 1)
2167 static const char *const decl_spec_names[] = {
2168 "signed",
2169 "unsigned",
2170 "short",
2171 "long",
2172 "const",
2173 "volatile",
2174 "restrict",
2175 "inline",
2176 "virtual",
2177 "explicit",
2178 "friend",
2179 "typedef",
2180 "__complex",
2181 "__thread"
2183 error ("duplicate %qs", decl_spec_names[(int)ds]);
2188 /* This function is called when a type is defined. If type
2189 definitions are forbidden at this point, an error message is
2190 issued. */
2192 static bool
2193 cp_parser_check_type_definition (cp_parser* parser)
2195 /* If types are forbidden here, issue a message. */
2196 if (parser->type_definition_forbidden_message)
2198 /* Use `%s' to print the string in case there are any escape
2199 characters in the message. */
2200 error ("%s", parser->type_definition_forbidden_message);
2201 return false;
2203 return true;
2206 /* This function is called when the DECLARATOR is processed. The TYPE
2207 was a type defined in the decl-specifiers. If it is invalid to
2208 define a type in the decl-specifiers for DECLARATOR, an error is
2209 issued. */
2211 static void
2212 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2213 tree type)
2215 /* [dcl.fct] forbids type definitions in return types.
2216 Unfortunately, it's not easy to know whether or not we are
2217 processing a return type until after the fact. */
2218 while (declarator
2219 && (declarator->kind == cdk_pointer
2220 || declarator->kind == cdk_reference
2221 || declarator->kind == cdk_ptrmem))
2222 declarator = declarator->declarator;
2223 if (declarator
2224 && declarator->kind == cdk_function)
2226 error ("new types may not be defined in a return type");
2227 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2228 type);
2232 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2233 "<" in any valid C++ program. If the next token is indeed "<",
2234 issue a message warning the user about what appears to be an
2235 invalid attempt to form a template-id. */
2237 static void
2238 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2239 tree type)
2241 cp_token_position start = 0;
2243 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2245 if (TYPE_P (type))
2246 error ("%qT is not a template", type);
2247 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2248 error ("%qE is not a template", type);
2249 else
2250 error ("invalid template-id");
2251 /* Remember the location of the invalid "<". */
2252 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2253 start = cp_lexer_token_position (parser->lexer, true);
2254 /* Consume the "<". */
2255 cp_lexer_consume_token (parser->lexer);
2256 /* Parse the template arguments. */
2257 cp_parser_enclosed_template_argument_list (parser);
2258 /* Permanently remove the invalid template arguments so that
2259 this error message is not issued again. */
2260 if (start)
2261 cp_lexer_purge_tokens_after (parser->lexer, start);
2265 /* If parsing an integral constant-expression, issue an error message
2266 about the fact that THING appeared and return true. Otherwise,
2267 return false. In either case, set
2268 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2270 static bool
2271 cp_parser_non_integral_constant_expression (cp_parser *parser,
2272 const char *thing)
2274 parser->non_integral_constant_expression_p = true;
2275 if (parser->integral_constant_expression_p)
2277 if (!parser->allow_non_integral_constant_expression_p)
2279 error ("%s cannot appear in a constant-expression", thing);
2280 return true;
2283 return false;
2286 /* Emit a diagnostic for an invalid type name. SCOPE is the
2287 qualifying scope (or NULL, if none) for ID. This function commits
2288 to the current active tentative parse, if any. (Otherwise, the
2289 problematic construct might be encountered again later, resulting
2290 in duplicate error messages.) */
2292 static void
2293 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2295 tree decl, old_scope;
2296 /* Try to lookup the identifier. */
2297 old_scope = parser->scope;
2298 parser->scope = scope;
2299 decl = cp_parser_lookup_name_simple (parser, id);
2300 parser->scope = old_scope;
2301 /* If the lookup found a template-name, it means that the user forgot
2302 to specify an argument list. Emit a useful error message. */
2303 if (TREE_CODE (decl) == TEMPLATE_DECL)
2304 error ("invalid use of template-name %qE without an argument list", decl);
2305 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2306 error ("invalid use of destructor %qD as a type", id);
2307 else if (TREE_CODE (decl) == TYPE_DECL)
2308 /* Something like 'unsigned A a;' */
2309 error ("invalid combination of multiple type-specifiers");
2310 else if (!parser->scope)
2312 /* Issue an error message. */
2313 error ("%qE does not name a type", id);
2314 /* If we're in a template class, it's possible that the user was
2315 referring to a type from a base class. For example:
2317 template <typename T> struct A { typedef T X; };
2318 template <typename T> struct B : public A<T> { X x; };
2320 The user should have said "typename A<T>::X". */
2321 if (processing_template_decl && current_class_type
2322 && TYPE_BINFO (current_class_type))
2324 tree b;
2326 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2328 b = TREE_CHAIN (b))
2330 tree base_type = BINFO_TYPE (b);
2331 if (CLASS_TYPE_P (base_type)
2332 && dependent_type_p (base_type))
2334 tree field;
2335 /* Go from a particular instantiation of the
2336 template (which will have an empty TYPE_FIELDs),
2337 to the main version. */
2338 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2339 for (field = TYPE_FIELDS (base_type);
2340 field;
2341 field = TREE_CHAIN (field))
2342 if (TREE_CODE (field) == TYPE_DECL
2343 && DECL_NAME (field) == id)
2345 inform ("(perhaps %<typename %T::%E%> was intended)",
2346 BINFO_TYPE (b), id);
2347 break;
2349 if (field)
2350 break;
2355 /* Here we diagnose qualified-ids where the scope is actually correct,
2356 but the identifier does not resolve to a valid type name. */
2357 else if (parser->scope != error_mark_node)
2359 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2360 error ("%qE in namespace %qE does not name a type",
2361 id, parser->scope);
2362 else if (TYPE_P (parser->scope))
2363 error ("%qE in class %qT does not name a type", id, parser->scope);
2364 else
2365 gcc_unreachable ();
2367 cp_parser_commit_to_tentative_parse (parser);
2370 /* Check for a common situation where a type-name should be present,
2371 but is not, and issue a sensible error message. Returns true if an
2372 invalid type-name was detected.
2374 The situation handled by this function are variable declarations of the
2375 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2376 Usually, `ID' should name a type, but if we got here it means that it
2377 does not. We try to emit the best possible error message depending on
2378 how exactly the id-expression looks like. */
2380 static bool
2381 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2383 tree id;
2385 cp_parser_parse_tentatively (parser);
2386 id = cp_parser_id_expression (parser,
2387 /*template_keyword_p=*/false,
2388 /*check_dependency_p=*/true,
2389 /*template_p=*/NULL,
2390 /*declarator_p=*/true,
2391 /*optional_p=*/false);
2392 /* After the id-expression, there should be a plain identifier,
2393 otherwise this is not a simple variable declaration. Also, if
2394 the scope is dependent, we cannot do much. */
2395 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2396 || (parser->scope && TYPE_P (parser->scope)
2397 && dependent_type_p (parser->scope))
2398 || TREE_CODE (id) == TYPE_DECL)
2400 cp_parser_abort_tentative_parse (parser);
2401 return false;
2403 if (!cp_parser_parse_definitely (parser))
2404 return false;
2406 /* Emit a diagnostic for the invalid type. */
2407 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2408 /* Skip to the end of the declaration; there's no point in
2409 trying to process it. */
2410 cp_parser_skip_to_end_of_block_or_statement (parser);
2411 return true;
2414 /* Consume tokens up to, and including, the next non-nested closing `)'.
2415 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2416 are doing error recovery. Returns -1 if OR_COMMA is true and we
2417 found an unnested comma. */
2419 static int
2420 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2421 bool recovering,
2422 bool or_comma,
2423 bool consume_paren)
2425 unsigned paren_depth = 0;
2426 unsigned brace_depth = 0;
2428 if (recovering && !or_comma
2429 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2430 return 0;
2432 while (true)
2434 cp_token * token = cp_lexer_peek_token (parser->lexer);
2436 switch (token->type)
2438 case CPP_EOF:
2439 case CPP_PRAGMA_EOL:
2440 /* If we've run out of tokens, then there is no closing `)'. */
2441 return 0;
2443 case CPP_SEMICOLON:
2444 /* This matches the processing in skip_to_end_of_statement. */
2445 if (!brace_depth)
2446 return 0;
2447 break;
2449 case CPP_OPEN_BRACE:
2450 ++brace_depth;
2451 break;
2452 case CPP_CLOSE_BRACE:
2453 if (!brace_depth--)
2454 return 0;
2455 break;
2457 case CPP_COMMA:
2458 if (recovering && or_comma && !brace_depth && !paren_depth)
2459 return -1;
2460 break;
2462 case CPP_OPEN_PAREN:
2463 if (!brace_depth)
2464 ++paren_depth;
2465 break;
2467 case CPP_CLOSE_PAREN:
2468 if (!brace_depth && !paren_depth--)
2470 if (consume_paren)
2471 cp_lexer_consume_token (parser->lexer);
2472 return 1;
2474 break;
2476 default:
2477 break;
2480 /* Consume the token. */
2481 cp_lexer_consume_token (parser->lexer);
2485 /* Consume tokens until we reach the end of the current statement.
2486 Normally, that will be just before consuming a `;'. However, if a
2487 non-nested `}' comes first, then we stop before consuming that. */
2489 static void
2490 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2492 unsigned nesting_depth = 0;
2494 while (true)
2496 cp_token *token = cp_lexer_peek_token (parser->lexer);
2498 switch (token->type)
2500 case CPP_EOF:
2501 case CPP_PRAGMA_EOL:
2502 /* If we've run out of tokens, stop. */
2503 return;
2505 case CPP_SEMICOLON:
2506 /* If the next token is a `;', we have reached the end of the
2507 statement. */
2508 if (!nesting_depth)
2509 return;
2510 break;
2512 case CPP_CLOSE_BRACE:
2513 /* If this is a non-nested '}', stop before consuming it.
2514 That way, when confronted with something like:
2516 { 3 + }
2518 we stop before consuming the closing '}', even though we
2519 have not yet reached a `;'. */
2520 if (nesting_depth == 0)
2521 return;
2523 /* If it is the closing '}' for a block that we have
2524 scanned, stop -- but only after consuming the token.
2525 That way given:
2527 void f g () { ... }
2528 typedef int I;
2530 we will stop after the body of the erroneously declared
2531 function, but before consuming the following `typedef'
2532 declaration. */
2533 if (--nesting_depth == 0)
2535 cp_lexer_consume_token (parser->lexer);
2536 return;
2539 case CPP_OPEN_BRACE:
2540 ++nesting_depth;
2541 break;
2543 default:
2544 break;
2547 /* Consume the token. */
2548 cp_lexer_consume_token (parser->lexer);
2552 /* This function is called at the end of a statement or declaration.
2553 If the next token is a semicolon, it is consumed; otherwise, error
2554 recovery is attempted. */
2556 static void
2557 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2559 /* Look for the trailing `;'. */
2560 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2562 /* If there is additional (erroneous) input, skip to the end of
2563 the statement. */
2564 cp_parser_skip_to_end_of_statement (parser);
2565 /* If the next token is now a `;', consume it. */
2566 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2567 cp_lexer_consume_token (parser->lexer);
2571 /* Skip tokens until we have consumed an entire block, or until we
2572 have consumed a non-nested `;'. */
2574 static void
2575 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2577 int nesting_depth = 0;
2579 while (nesting_depth >= 0)
2581 cp_token *token = cp_lexer_peek_token (parser->lexer);
2583 switch (token->type)
2585 case CPP_EOF:
2586 case CPP_PRAGMA_EOL:
2587 /* If we've run out of tokens, stop. */
2588 return;
2590 case CPP_SEMICOLON:
2591 /* Stop if this is an unnested ';'. */
2592 if (!nesting_depth)
2593 nesting_depth = -1;
2594 break;
2596 case CPP_CLOSE_BRACE:
2597 /* Stop if this is an unnested '}', or closes the outermost
2598 nesting level. */
2599 nesting_depth--;
2600 if (!nesting_depth)
2601 nesting_depth = -1;
2602 break;
2604 case CPP_OPEN_BRACE:
2605 /* Nest. */
2606 nesting_depth++;
2607 break;
2609 default:
2610 break;
2613 /* Consume the token. */
2614 cp_lexer_consume_token (parser->lexer);
2618 /* Skip tokens until a non-nested closing curly brace is the next
2619 token, or there are no more tokens. Return true in the first case,
2620 false otherwise. */
2622 static bool
2623 cp_parser_skip_to_closing_brace (cp_parser *parser)
2625 unsigned nesting_depth = 0;
2627 while (true)
2629 cp_token *token = cp_lexer_peek_token (parser->lexer);
2631 switch (token->type)
2633 case CPP_EOF:
2634 case CPP_PRAGMA_EOL:
2635 /* If we've run out of tokens, stop. */
2636 return false;
2638 case CPP_CLOSE_BRACE:
2639 /* If the next token is a non-nested `}', then we have reached
2640 the end of the current block. */
2641 if (nesting_depth-- == 0)
2642 return true;
2643 break;
2645 case CPP_OPEN_BRACE:
2646 /* If it the next token is a `{', then we are entering a new
2647 block. Consume the entire block. */
2648 ++nesting_depth;
2649 break;
2651 default:
2652 break;
2655 /* Consume the token. */
2656 cp_lexer_consume_token (parser->lexer);
2660 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2661 parameter is the PRAGMA token, allowing us to purge the entire pragma
2662 sequence. */
2664 static void
2665 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2667 cp_token *token;
2669 parser->lexer->in_pragma = false;
2672 token = cp_lexer_consume_token (parser->lexer);
2673 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2675 /* Ensure that the pragma is not parsed again. */
2676 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2679 /* Require pragma end of line, resyncing with it as necessary. The
2680 arguments are as for cp_parser_skip_to_pragma_eol. */
2682 static void
2683 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2685 parser->lexer->in_pragma = false;
2686 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2687 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2690 /* This is a simple wrapper around make_typename_type. When the id is
2691 an unresolved identifier node, we can provide a superior diagnostic
2692 using cp_parser_diagnose_invalid_type_name. */
2694 static tree
2695 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2697 tree result;
2698 if (TREE_CODE (id) == IDENTIFIER_NODE)
2700 result = make_typename_type (scope, id, typename_type,
2701 /*complain=*/tf_none);
2702 if (result == error_mark_node)
2703 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2704 return result;
2706 return make_typename_type (scope, id, typename_type, tf_error);
2709 /* This is a wrapper around the
2710 make_{pointer,ptrmem,reference}_declarator functions that decides
2711 which one to call based on the CODE and CLASS_TYPE arguments. The
2712 CODE argument should be one of the values returned by
2713 cp_parser_ptr_operator. */
2714 static cp_declarator *
2715 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2716 cp_cv_quals cv_qualifiers,
2717 cp_declarator *target)
2719 if (code == ERROR_MARK)
2720 return cp_error_declarator;
2722 if (code == INDIRECT_REF)
2723 if (class_type == NULL_TREE)
2724 return make_pointer_declarator (cv_qualifiers, target);
2725 else
2726 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2727 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2728 return make_reference_declarator (cv_qualifiers, target, false);
2729 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2730 return make_reference_declarator (cv_qualifiers, target, true);
2731 gcc_unreachable ();
2734 /* Create a new C++ parser. */
2736 static cp_parser *
2737 cp_parser_new (void)
2739 cp_parser *parser;
2740 cp_lexer *lexer;
2741 unsigned i;
2743 /* cp_lexer_new_main is called before calling ggc_alloc because
2744 cp_lexer_new_main might load a PCH file. */
2745 lexer = cp_lexer_new_main ();
2747 /* Initialize the binops_by_token so that we can get the tree
2748 directly from the token. */
2749 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2750 binops_by_token[binops[i].token_type] = binops[i];
2752 parser = GGC_CNEW (cp_parser);
2753 parser->lexer = lexer;
2754 parser->context = cp_parser_context_new (NULL);
2756 /* For now, we always accept GNU extensions. */
2757 parser->allow_gnu_extensions_p = 1;
2759 /* The `>' token is a greater-than operator, not the end of a
2760 template-id. */
2761 parser->greater_than_is_operator_p = true;
2763 parser->default_arg_ok_p = true;
2765 /* We are not parsing a constant-expression. */
2766 parser->integral_constant_expression_p = false;
2767 parser->allow_non_integral_constant_expression_p = false;
2768 parser->non_integral_constant_expression_p = false;
2770 /* Local variable names are not forbidden. */
2771 parser->local_variables_forbidden_p = false;
2773 /* We are not processing an `extern "C"' declaration. */
2774 parser->in_unbraced_linkage_specification_p = false;
2776 /* We are not processing a declarator. */
2777 parser->in_declarator_p = false;
2779 /* We are not processing a template-argument-list. */
2780 parser->in_template_argument_list_p = false;
2782 /* We are not in an iteration statement. */
2783 parser->in_statement = 0;
2785 /* We are not in a switch statement. */
2786 parser->in_switch_statement_p = false;
2788 /* We are not parsing a type-id inside an expression. */
2789 parser->in_type_id_in_expr_p = false;
2791 /* Declarations aren't implicitly extern "C". */
2792 parser->implicit_extern_c = false;
2794 /* String literals should be translated to the execution character set. */
2795 parser->translate_strings_p = true;
2797 /* We are not parsing a function body. */
2798 parser->in_function_body = false;
2800 /* The unparsed function queue is empty. */
2801 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2803 /* There are no classes being defined. */
2804 parser->num_classes_being_defined = 0;
2806 /* No template parameters apply. */
2807 parser->num_template_parameter_lists = 0;
2809 return parser;
2812 /* Create a cp_lexer structure which will emit the tokens in CACHE
2813 and push it onto the parser's lexer stack. This is used for delayed
2814 parsing of in-class method bodies and default arguments, and should
2815 not be confused with tentative parsing. */
2816 static void
2817 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2819 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2820 lexer->next = parser->lexer;
2821 parser->lexer = lexer;
2823 /* Move the current source position to that of the first token in the
2824 new lexer. */
2825 cp_lexer_set_source_position_from_token (lexer->next_token);
2828 /* Pop the top lexer off the parser stack. This is never used for the
2829 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2830 static void
2831 cp_parser_pop_lexer (cp_parser *parser)
2833 cp_lexer *lexer = parser->lexer;
2834 parser->lexer = lexer->next;
2835 cp_lexer_destroy (lexer);
2837 /* Put the current source position back where it was before this
2838 lexer was pushed. */
2839 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2842 /* Lexical conventions [gram.lex] */
2844 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2845 identifier. */
2847 static tree
2848 cp_parser_identifier (cp_parser* parser)
2850 cp_token *token;
2852 /* Look for the identifier. */
2853 token = cp_parser_require (parser, CPP_NAME, "identifier");
2854 /* Return the value. */
2855 return token ? token->u.value : error_mark_node;
2858 /* Parse a sequence of adjacent string constants. Returns a
2859 TREE_STRING representing the combined, nul-terminated string
2860 constant. If TRANSLATE is true, translate the string to the
2861 execution character set. If WIDE_OK is true, a wide string is
2862 invalid here.
2864 C++98 [lex.string] says that if a narrow string literal token is
2865 adjacent to a wide string literal token, the behavior is undefined.
2866 However, C99 6.4.5p4 says that this results in a wide string literal.
2867 We follow C99 here, for consistency with the C front end.
2869 This code is largely lifted from lex_string() in c-lex.c.
2871 FUTURE: ObjC++ will need to handle @-strings here. */
2872 static tree
2873 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2875 tree value;
2876 bool wide = false;
2877 size_t count;
2878 struct obstack str_ob;
2879 cpp_string str, istr, *strs;
2880 cp_token *tok;
2882 tok = cp_lexer_peek_token (parser->lexer);
2883 if (!cp_parser_is_string_literal (tok))
2885 cp_parser_error (parser, "expected string-literal");
2886 return error_mark_node;
2889 /* Try to avoid the overhead of creating and destroying an obstack
2890 for the common case of just one string. */
2891 if (!cp_parser_is_string_literal
2892 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2894 cp_lexer_consume_token (parser->lexer);
2896 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2897 str.len = TREE_STRING_LENGTH (tok->u.value);
2898 count = 1;
2899 if (tok->type == CPP_WSTRING)
2900 wide = true;
2902 strs = &str;
2904 else
2906 gcc_obstack_init (&str_ob);
2907 count = 0;
2911 cp_lexer_consume_token (parser->lexer);
2912 count++;
2913 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2914 str.len = TREE_STRING_LENGTH (tok->u.value);
2915 if (tok->type == CPP_WSTRING)
2916 wide = true;
2918 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2920 tok = cp_lexer_peek_token (parser->lexer);
2922 while (cp_parser_is_string_literal (tok));
2924 strs = (cpp_string *) obstack_finish (&str_ob);
2927 if (wide && !wide_ok)
2929 cp_parser_error (parser, "a wide string is invalid in this context");
2930 wide = false;
2933 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2934 (parse_in, strs, count, &istr, wide))
2936 value = build_string (istr.len, (const char *)istr.text);
2937 free (CONST_CAST (unsigned char *, istr.text));
2939 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2940 value = fix_string_type (value);
2942 else
2943 /* cpp_interpret_string has issued an error. */
2944 value = error_mark_node;
2946 if (count > 1)
2947 obstack_free (&str_ob, 0);
2949 return value;
2953 /* Basic concepts [gram.basic] */
2955 /* Parse a translation-unit.
2957 translation-unit:
2958 declaration-seq [opt]
2960 Returns TRUE if all went well. */
2962 static bool
2963 cp_parser_translation_unit (cp_parser* parser)
2965 /* The address of the first non-permanent object on the declarator
2966 obstack. */
2967 static void *declarator_obstack_base;
2969 bool success;
2971 /* Create the declarator obstack, if necessary. */
2972 if (!cp_error_declarator)
2974 gcc_obstack_init (&declarator_obstack);
2975 /* Create the error declarator. */
2976 cp_error_declarator = make_declarator (cdk_error);
2977 /* Create the empty parameter list. */
2978 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2979 /* Remember where the base of the declarator obstack lies. */
2980 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2983 cp_parser_declaration_seq_opt (parser);
2985 /* If there are no tokens left then all went well. */
2986 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2988 /* Get rid of the token array; we don't need it any more. */
2989 cp_lexer_destroy (parser->lexer);
2990 parser->lexer = NULL;
2992 /* This file might have been a context that's implicitly extern
2993 "C". If so, pop the lang context. (Only relevant for PCH.) */
2994 if (parser->implicit_extern_c)
2996 pop_lang_context ();
2997 parser->implicit_extern_c = false;
3000 /* Finish up. */
3001 finish_translation_unit ();
3003 success = true;
3005 else
3007 cp_parser_error (parser, "expected declaration");
3008 success = false;
3011 /* Make sure the declarator obstack was fully cleaned up. */
3012 gcc_assert (obstack_next_free (&declarator_obstack)
3013 == declarator_obstack_base);
3015 /* All went well. */
3016 return success;
3019 /* Expressions [gram.expr] */
3021 /* Parse a primary-expression.
3023 primary-expression:
3024 literal
3025 this
3026 ( expression )
3027 id-expression
3029 GNU Extensions:
3031 primary-expression:
3032 ( compound-statement )
3033 __builtin_va_arg ( assignment-expression , type-id )
3034 __builtin_offsetof ( type-id , offsetof-expression )
3036 C++ Extensions:
3037 __has_nothrow_assign ( type-id )
3038 __has_nothrow_constructor ( type-id )
3039 __has_nothrow_copy ( type-id )
3040 __has_trivial_assign ( type-id )
3041 __has_trivial_constructor ( type-id )
3042 __has_trivial_copy ( type-id )
3043 __has_trivial_destructor ( type-id )
3044 __has_virtual_destructor ( type-id )
3045 __is_abstract ( type-id )
3046 __is_base_of ( type-id , type-id )
3047 __is_class ( type-id )
3048 __is_convertible_to ( type-id , type-id )
3049 __is_empty ( type-id )
3050 __is_enum ( type-id )
3051 __is_pod ( type-id )
3052 __is_polymorphic ( type-id )
3053 __is_union ( type-id )
3055 Objective-C++ Extension:
3057 primary-expression:
3058 objc-expression
3060 literal:
3061 __null
3063 ADDRESS_P is true iff this expression was immediately preceded by
3064 "&" and therefore might denote a pointer-to-member. CAST_P is true
3065 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3066 true iff this expression is a template argument.
3068 Returns a representation of the expression. Upon return, *IDK
3069 indicates what kind of id-expression (if any) was present. */
3071 static tree
3072 cp_parser_primary_expression (cp_parser *parser,
3073 bool address_p,
3074 bool cast_p,
3075 bool template_arg_p,
3076 cp_id_kind *idk)
3078 cp_token *token;
3080 /* Assume the primary expression is not an id-expression. */
3081 *idk = CP_ID_KIND_NONE;
3083 /* Peek at the next token. */
3084 token = cp_lexer_peek_token (parser->lexer);
3085 switch (token->type)
3087 /* literal:
3088 integer-literal
3089 character-literal
3090 floating-literal
3091 string-literal
3092 boolean-literal */
3093 case CPP_CHAR:
3094 case CPP_WCHAR:
3095 case CPP_NUMBER:
3096 token = cp_lexer_consume_token (parser->lexer);
3097 /* Floating-point literals are only allowed in an integral
3098 constant expression if they are cast to an integral or
3099 enumeration type. */
3100 if (TREE_CODE (token->u.value) == REAL_CST
3101 && parser->integral_constant_expression_p
3102 && pedantic)
3104 /* CAST_P will be set even in invalid code like "int(2.7 +
3105 ...)". Therefore, we have to check that the next token
3106 is sure to end the cast. */
3107 if (cast_p)
3109 cp_token *next_token;
3111 next_token = cp_lexer_peek_token (parser->lexer);
3112 if (/* The comma at the end of an
3113 enumerator-definition. */
3114 next_token->type != CPP_COMMA
3115 /* The curly brace at the end of an enum-specifier. */
3116 && next_token->type != CPP_CLOSE_BRACE
3117 /* The end of a statement. */
3118 && next_token->type != CPP_SEMICOLON
3119 /* The end of the cast-expression. */
3120 && next_token->type != CPP_CLOSE_PAREN
3121 /* The end of an array bound. */
3122 && next_token->type != CPP_CLOSE_SQUARE
3123 /* The closing ">" in a template-argument-list. */
3124 && (next_token->type != CPP_GREATER
3125 || parser->greater_than_is_operator_p)
3126 /* C++0x only: A ">>" treated like two ">" tokens,
3127 in a template-argument-list. */
3128 && (next_token->type != CPP_RSHIFT
3129 || (cxx_dialect == cxx98)
3130 || parser->greater_than_is_operator_p))
3131 cast_p = false;
3134 /* If we are within a cast, then the constraint that the
3135 cast is to an integral or enumeration type will be
3136 checked at that point. If we are not within a cast, then
3137 this code is invalid. */
3138 if (!cast_p)
3139 cp_parser_non_integral_constant_expression
3140 (parser, "floating-point literal");
3142 return token->u.value;
3144 case CPP_STRING:
3145 case CPP_WSTRING:
3146 /* ??? Should wide strings be allowed when parser->translate_strings_p
3147 is false (i.e. in attributes)? If not, we can kill the third
3148 argument to cp_parser_string_literal. */
3149 return cp_parser_string_literal (parser,
3150 parser->translate_strings_p,
3151 true);
3153 case CPP_OPEN_PAREN:
3155 tree expr;
3156 bool saved_greater_than_is_operator_p;
3158 /* Consume the `('. */
3159 cp_lexer_consume_token (parser->lexer);
3160 /* Within a parenthesized expression, a `>' token is always
3161 the greater-than operator. */
3162 saved_greater_than_is_operator_p
3163 = parser->greater_than_is_operator_p;
3164 parser->greater_than_is_operator_p = true;
3165 /* If we see `( { ' then we are looking at the beginning of
3166 a GNU statement-expression. */
3167 if (cp_parser_allow_gnu_extensions_p (parser)
3168 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3170 /* Statement-expressions are not allowed by the standard. */
3171 if (pedantic)
3172 pedwarn ("ISO C++ forbids braced-groups within expressions");
3174 /* And they're not allowed outside of a function-body; you
3175 cannot, for example, write:
3177 int i = ({ int j = 3; j + 1; });
3179 at class or namespace scope. */
3180 if (!parser->in_function_body
3181 || parser->in_template_argument_list_p)
3183 error ("statement-expressions are not allowed outside "
3184 "functions nor in template-argument lists");
3185 cp_parser_skip_to_end_of_block_or_statement (parser);
3186 expr = error_mark_node;
3188 else
3190 /* Start the statement-expression. */
3191 expr = begin_stmt_expr ();
3192 /* Parse the compound-statement. */
3193 cp_parser_compound_statement (parser, expr, false);
3194 /* Finish up. */
3195 expr = finish_stmt_expr (expr, false);
3198 else
3200 /* Parse the parenthesized expression. */
3201 expr = cp_parser_expression (parser, cast_p);
3202 /* Let the front end know that this expression was
3203 enclosed in parentheses. This matters in case, for
3204 example, the expression is of the form `A::B', since
3205 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3206 not. */
3207 finish_parenthesized_expr (expr);
3209 /* The `>' token might be the end of a template-id or
3210 template-parameter-list now. */
3211 parser->greater_than_is_operator_p
3212 = saved_greater_than_is_operator_p;
3213 /* Consume the `)'. */
3214 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3215 cp_parser_skip_to_end_of_statement (parser);
3217 return expr;
3220 case CPP_KEYWORD:
3221 switch (token->keyword)
3223 /* These two are the boolean literals. */
3224 case RID_TRUE:
3225 cp_lexer_consume_token (parser->lexer);
3226 return boolean_true_node;
3227 case RID_FALSE:
3228 cp_lexer_consume_token (parser->lexer);
3229 return boolean_false_node;
3231 /* The `__null' literal. */
3232 case RID_NULL:
3233 cp_lexer_consume_token (parser->lexer);
3234 return null_node;
3236 /* Recognize the `this' keyword. */
3237 case RID_THIS:
3238 cp_lexer_consume_token (parser->lexer);
3239 if (parser->local_variables_forbidden_p)
3241 error ("%<this%> may not be used in this context");
3242 return error_mark_node;
3244 /* Pointers cannot appear in constant-expressions. */
3245 if (cp_parser_non_integral_constant_expression (parser,
3246 "`this'"))
3247 return error_mark_node;
3248 return finish_this_expr ();
3250 /* The `operator' keyword can be the beginning of an
3251 id-expression. */
3252 case RID_OPERATOR:
3253 goto id_expression;
3255 case RID_FUNCTION_NAME:
3256 case RID_PRETTY_FUNCTION_NAME:
3257 case RID_C99_FUNCTION_NAME:
3258 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3259 __func__ are the names of variables -- but they are
3260 treated specially. Therefore, they are handled here,
3261 rather than relying on the generic id-expression logic
3262 below. Grammatically, these names are id-expressions.
3264 Consume the token. */
3265 token = cp_lexer_consume_token (parser->lexer);
3266 /* Look up the name. */
3267 return finish_fname (token->u.value);
3269 case RID_VA_ARG:
3271 tree expression;
3272 tree type;
3274 /* The `__builtin_va_arg' construct is used to handle
3275 `va_arg'. Consume the `__builtin_va_arg' token. */
3276 cp_lexer_consume_token (parser->lexer);
3277 /* Look for the opening `('. */
3278 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3279 /* Now, parse the assignment-expression. */
3280 expression = cp_parser_assignment_expression (parser,
3281 /*cast_p=*/false);
3282 /* Look for the `,'. */
3283 cp_parser_require (parser, CPP_COMMA, "`,'");
3284 /* Parse the type-id. */
3285 type = cp_parser_type_id (parser);
3286 /* Look for the closing `)'. */
3287 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3288 /* Using `va_arg' in a constant-expression is not
3289 allowed. */
3290 if (cp_parser_non_integral_constant_expression (parser,
3291 "`va_arg'"))
3292 return error_mark_node;
3293 return build_x_va_arg (expression, type);
3296 case RID_OFFSETOF:
3297 return cp_parser_builtin_offsetof (parser);
3299 case RID_HAS_NOTHROW_ASSIGN:
3300 case RID_HAS_NOTHROW_CONSTRUCTOR:
3301 case RID_HAS_NOTHROW_COPY:
3302 case RID_HAS_TRIVIAL_ASSIGN:
3303 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3304 case RID_HAS_TRIVIAL_COPY:
3305 case RID_HAS_TRIVIAL_DESTRUCTOR:
3306 case RID_HAS_VIRTUAL_DESTRUCTOR:
3307 case RID_IS_ABSTRACT:
3308 case RID_IS_BASE_OF:
3309 case RID_IS_CLASS:
3310 case RID_IS_CONVERTIBLE_TO:
3311 case RID_IS_EMPTY:
3312 case RID_IS_ENUM:
3313 case RID_IS_POD:
3314 case RID_IS_POLYMORPHIC:
3315 case RID_IS_UNION:
3316 return cp_parser_trait_expr (parser, token->keyword);
3318 /* Objective-C++ expressions. */
3319 case RID_AT_ENCODE:
3320 case RID_AT_PROTOCOL:
3321 case RID_AT_SELECTOR:
3322 return cp_parser_objc_expression (parser);
3324 default:
3325 cp_parser_error (parser, "expected primary-expression");
3326 return error_mark_node;
3329 /* An id-expression can start with either an identifier, a
3330 `::' as the beginning of a qualified-id, or the "operator"
3331 keyword. */
3332 case CPP_NAME:
3333 case CPP_SCOPE:
3334 case CPP_TEMPLATE_ID:
3335 case CPP_NESTED_NAME_SPECIFIER:
3337 tree id_expression;
3338 tree decl;
3339 const char *error_msg;
3340 bool template_p;
3341 bool done;
3343 id_expression:
3344 /* Parse the id-expression. */
3345 id_expression
3346 = cp_parser_id_expression (parser,
3347 /*template_keyword_p=*/false,
3348 /*check_dependency_p=*/true,
3349 &template_p,
3350 /*declarator_p=*/false,
3351 /*optional_p=*/false);
3352 if (id_expression == error_mark_node)
3353 return error_mark_node;
3354 token = cp_lexer_peek_token (parser->lexer);
3355 done = (token->type != CPP_OPEN_SQUARE
3356 && token->type != CPP_OPEN_PAREN
3357 && token->type != CPP_DOT
3358 && token->type != CPP_DEREF
3359 && token->type != CPP_PLUS_PLUS
3360 && token->type != CPP_MINUS_MINUS);
3361 /* If we have a template-id, then no further lookup is
3362 required. If the template-id was for a template-class, we
3363 will sometimes have a TYPE_DECL at this point. */
3364 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3365 || TREE_CODE (id_expression) == TYPE_DECL)
3366 decl = id_expression;
3367 /* Look up the name. */
3368 else
3370 tree ambiguous_decls;
3372 decl = cp_parser_lookup_name (parser, id_expression,
3373 none_type,
3374 template_p,
3375 /*is_namespace=*/false,
3376 /*check_dependency=*/true,
3377 &ambiguous_decls);
3378 /* If the lookup was ambiguous, an error will already have
3379 been issued. */
3380 if (ambiguous_decls)
3381 return error_mark_node;
3383 /* In Objective-C++, an instance variable (ivar) may be preferred
3384 to whatever cp_parser_lookup_name() found. */
3385 decl = objc_lookup_ivar (decl, id_expression);
3387 /* If name lookup gives us a SCOPE_REF, then the
3388 qualifying scope was dependent. */
3389 if (TREE_CODE (decl) == SCOPE_REF)
3391 /* At this point, we do not know if DECL is a valid
3392 integral constant expression. We assume that it is
3393 in fact such an expression, so that code like:
3395 template <int N> struct A {
3396 int a[B<N>::i];
3399 is accepted. At template-instantiation time, we
3400 will check that B<N>::i is actually a constant. */
3401 return decl;
3403 /* Check to see if DECL is a local variable in a context
3404 where that is forbidden. */
3405 if (parser->local_variables_forbidden_p
3406 && local_variable_p (decl))
3408 /* It might be that we only found DECL because we are
3409 trying to be generous with pre-ISO scoping rules.
3410 For example, consider:
3412 int i;
3413 void g() {
3414 for (int i = 0; i < 10; ++i) {}
3415 extern void f(int j = i);
3418 Here, name look up will originally find the out
3419 of scope `i'. We need to issue a warning message,
3420 but then use the global `i'. */
3421 decl = check_for_out_of_scope_variable (decl);
3422 if (local_variable_p (decl))
3424 error ("local variable %qD may not appear in this context",
3425 decl);
3426 return error_mark_node;
3431 decl = (finish_id_expression
3432 (id_expression, decl, parser->scope,
3433 idk,
3434 parser->integral_constant_expression_p,
3435 parser->allow_non_integral_constant_expression_p,
3436 &parser->non_integral_constant_expression_p,
3437 template_p, done, address_p,
3438 template_arg_p,
3439 &error_msg));
3440 if (error_msg)
3441 cp_parser_error (parser, error_msg);
3442 return decl;
3445 /* Anything else is an error. */
3446 default:
3447 /* ...unless we have an Objective-C++ message or string literal,
3448 that is. */
3449 if (c_dialect_objc ()
3450 && (token->type == CPP_OPEN_SQUARE
3451 || token->type == CPP_OBJC_STRING))
3452 return cp_parser_objc_expression (parser);
3454 cp_parser_error (parser, "expected primary-expression");
3455 return error_mark_node;
3459 /* Parse an id-expression.
3461 id-expression:
3462 unqualified-id
3463 qualified-id
3465 qualified-id:
3466 :: [opt] nested-name-specifier template [opt] unqualified-id
3467 :: identifier
3468 :: operator-function-id
3469 :: template-id
3471 Return a representation of the unqualified portion of the
3472 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3473 a `::' or nested-name-specifier.
3475 Often, if the id-expression was a qualified-id, the caller will
3476 want to make a SCOPE_REF to represent the qualified-id. This
3477 function does not do this in order to avoid wastefully creating
3478 SCOPE_REFs when they are not required.
3480 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3481 `template' keyword.
3483 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3484 uninstantiated templates.
3486 If *TEMPLATE_P is non-NULL, it is set to true iff the
3487 `template' keyword is used to explicitly indicate that the entity
3488 named is a template.
3490 If DECLARATOR_P is true, the id-expression is appearing as part of
3491 a declarator, rather than as part of an expression. */
3493 static tree
3494 cp_parser_id_expression (cp_parser *parser,
3495 bool template_keyword_p,
3496 bool check_dependency_p,
3497 bool *template_p,
3498 bool declarator_p,
3499 bool optional_p)
3501 bool global_scope_p;
3502 bool nested_name_specifier_p;
3504 /* Assume the `template' keyword was not used. */
3505 if (template_p)
3506 *template_p = template_keyword_p;
3508 /* Look for the optional `::' operator. */
3509 global_scope_p
3510 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3511 != NULL_TREE);
3512 /* Look for the optional nested-name-specifier. */
3513 nested_name_specifier_p
3514 = (cp_parser_nested_name_specifier_opt (parser,
3515 /*typename_keyword_p=*/false,
3516 check_dependency_p,
3517 /*type_p=*/false,
3518 declarator_p)
3519 != NULL_TREE);
3520 /* If there is a nested-name-specifier, then we are looking at
3521 the first qualified-id production. */
3522 if (nested_name_specifier_p)
3524 tree saved_scope;
3525 tree saved_object_scope;
3526 tree saved_qualifying_scope;
3527 tree unqualified_id;
3528 bool is_template;
3530 /* See if the next token is the `template' keyword. */
3531 if (!template_p)
3532 template_p = &is_template;
3533 *template_p = cp_parser_optional_template_keyword (parser);
3534 /* Name lookup we do during the processing of the
3535 unqualified-id might obliterate SCOPE. */
3536 saved_scope = parser->scope;
3537 saved_object_scope = parser->object_scope;
3538 saved_qualifying_scope = parser->qualifying_scope;
3539 /* Process the final unqualified-id. */
3540 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3541 check_dependency_p,
3542 declarator_p,
3543 /*optional_p=*/false);
3544 /* Restore the SAVED_SCOPE for our caller. */
3545 parser->scope = saved_scope;
3546 parser->object_scope = saved_object_scope;
3547 parser->qualifying_scope = saved_qualifying_scope;
3549 return unqualified_id;
3551 /* Otherwise, if we are in global scope, then we are looking at one
3552 of the other qualified-id productions. */
3553 else if (global_scope_p)
3555 cp_token *token;
3556 tree id;
3558 /* Peek at the next token. */
3559 token = cp_lexer_peek_token (parser->lexer);
3561 /* If it's an identifier, and the next token is not a "<", then
3562 we can avoid the template-id case. This is an optimization
3563 for this common case. */
3564 if (token->type == CPP_NAME
3565 && !cp_parser_nth_token_starts_template_argument_list_p
3566 (parser, 2))
3567 return cp_parser_identifier (parser);
3569 cp_parser_parse_tentatively (parser);
3570 /* Try a template-id. */
3571 id = cp_parser_template_id (parser,
3572 /*template_keyword_p=*/false,
3573 /*check_dependency_p=*/true,
3574 declarator_p);
3575 /* If that worked, we're done. */
3576 if (cp_parser_parse_definitely (parser))
3577 return id;
3579 /* Peek at the next token. (Changes in the token buffer may
3580 have invalidated the pointer obtained above.) */
3581 token = cp_lexer_peek_token (parser->lexer);
3583 switch (token->type)
3585 case CPP_NAME:
3586 return cp_parser_identifier (parser);
3588 case CPP_KEYWORD:
3589 if (token->keyword == RID_OPERATOR)
3590 return cp_parser_operator_function_id (parser);
3591 /* Fall through. */
3593 default:
3594 cp_parser_error (parser, "expected id-expression");
3595 return error_mark_node;
3598 else
3599 return cp_parser_unqualified_id (parser, template_keyword_p,
3600 /*check_dependency_p=*/true,
3601 declarator_p,
3602 optional_p);
3605 /* Parse an unqualified-id.
3607 unqualified-id:
3608 identifier
3609 operator-function-id
3610 conversion-function-id
3611 ~ class-name
3612 template-id
3614 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3615 keyword, in a construct like `A::template ...'.
3617 Returns a representation of unqualified-id. For the `identifier'
3618 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3619 production a BIT_NOT_EXPR is returned; the operand of the
3620 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3621 other productions, see the documentation accompanying the
3622 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3623 names are looked up in uninstantiated templates. If DECLARATOR_P
3624 is true, the unqualified-id is appearing as part of a declarator,
3625 rather than as part of an expression. */
3627 static tree
3628 cp_parser_unqualified_id (cp_parser* parser,
3629 bool template_keyword_p,
3630 bool check_dependency_p,
3631 bool declarator_p,
3632 bool optional_p)
3634 cp_token *token;
3636 /* Peek at the next token. */
3637 token = cp_lexer_peek_token (parser->lexer);
3639 switch (token->type)
3641 case CPP_NAME:
3643 tree id;
3645 /* We don't know yet whether or not this will be a
3646 template-id. */
3647 cp_parser_parse_tentatively (parser);
3648 /* Try a template-id. */
3649 id = cp_parser_template_id (parser, template_keyword_p,
3650 check_dependency_p,
3651 declarator_p);
3652 /* If it worked, we're done. */
3653 if (cp_parser_parse_definitely (parser))
3654 return id;
3655 /* Otherwise, it's an ordinary identifier. */
3656 return cp_parser_identifier (parser);
3659 case CPP_TEMPLATE_ID:
3660 return cp_parser_template_id (parser, template_keyword_p,
3661 check_dependency_p,
3662 declarator_p);
3664 case CPP_COMPL:
3666 tree type_decl;
3667 tree qualifying_scope;
3668 tree object_scope;
3669 tree scope;
3670 bool done;
3672 /* Consume the `~' token. */
3673 cp_lexer_consume_token (parser->lexer);
3674 /* Parse the class-name. The standard, as written, seems to
3675 say that:
3677 template <typename T> struct S { ~S (); };
3678 template <typename T> S<T>::~S() {}
3680 is invalid, since `~' must be followed by a class-name, but
3681 `S<T>' is dependent, and so not known to be a class.
3682 That's not right; we need to look in uninstantiated
3683 templates. A further complication arises from:
3685 template <typename T> void f(T t) {
3686 t.T::~T();
3689 Here, it is not possible to look up `T' in the scope of `T'
3690 itself. We must look in both the current scope, and the
3691 scope of the containing complete expression.
3693 Yet another issue is:
3695 struct S {
3696 int S;
3697 ~S();
3700 S::~S() {}
3702 The standard does not seem to say that the `S' in `~S'
3703 should refer to the type `S' and not the data member
3704 `S::S'. */
3706 /* DR 244 says that we look up the name after the "~" in the
3707 same scope as we looked up the qualifying name. That idea
3708 isn't fully worked out; it's more complicated than that. */
3709 scope = parser->scope;
3710 object_scope = parser->object_scope;
3711 qualifying_scope = parser->qualifying_scope;
3713 /* Check for invalid scopes. */
3714 if (scope == error_mark_node)
3716 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3717 cp_lexer_consume_token (parser->lexer);
3718 return error_mark_node;
3720 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3722 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3723 error ("scope %qT before %<~%> is not a class-name", scope);
3724 cp_parser_simulate_error (parser);
3725 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3726 cp_lexer_consume_token (parser->lexer);
3727 return error_mark_node;
3729 gcc_assert (!scope || TYPE_P (scope));
3731 /* If the name is of the form "X::~X" it's OK. */
3732 token = cp_lexer_peek_token (parser->lexer);
3733 if (scope
3734 && token->type == CPP_NAME
3735 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3736 == CPP_OPEN_PAREN)
3737 && constructor_name_p (token->u.value, scope))
3739 cp_lexer_consume_token (parser->lexer);
3740 return build_nt (BIT_NOT_EXPR, scope);
3743 /* If there was an explicit qualification (S::~T), first look
3744 in the scope given by the qualification (i.e., S). */
3745 done = false;
3746 type_decl = NULL_TREE;
3747 if (scope)
3749 cp_parser_parse_tentatively (parser);
3750 type_decl = cp_parser_class_name (parser,
3751 /*typename_keyword_p=*/false,
3752 /*template_keyword_p=*/false,
3753 none_type,
3754 /*check_dependency=*/false,
3755 /*class_head_p=*/false,
3756 declarator_p);
3757 if (cp_parser_parse_definitely (parser))
3758 done = true;
3760 /* In "N::S::~S", look in "N" as well. */
3761 if (!done && scope && qualifying_scope)
3763 cp_parser_parse_tentatively (parser);
3764 parser->scope = qualifying_scope;
3765 parser->object_scope = NULL_TREE;
3766 parser->qualifying_scope = NULL_TREE;
3767 type_decl
3768 = cp_parser_class_name (parser,
3769 /*typename_keyword_p=*/false,
3770 /*template_keyword_p=*/false,
3771 none_type,
3772 /*check_dependency=*/false,
3773 /*class_head_p=*/false,
3774 declarator_p);
3775 if (cp_parser_parse_definitely (parser))
3776 done = true;
3778 /* In "p->S::~T", look in the scope given by "*p" as well. */
3779 else if (!done && object_scope)
3781 cp_parser_parse_tentatively (parser);
3782 parser->scope = object_scope;
3783 parser->object_scope = NULL_TREE;
3784 parser->qualifying_scope = NULL_TREE;
3785 type_decl
3786 = cp_parser_class_name (parser,
3787 /*typename_keyword_p=*/false,
3788 /*template_keyword_p=*/false,
3789 none_type,
3790 /*check_dependency=*/false,
3791 /*class_head_p=*/false,
3792 declarator_p);
3793 if (cp_parser_parse_definitely (parser))
3794 done = true;
3796 /* Look in the surrounding context. */
3797 if (!done)
3799 parser->scope = NULL_TREE;
3800 parser->object_scope = NULL_TREE;
3801 parser->qualifying_scope = NULL_TREE;
3802 type_decl
3803 = cp_parser_class_name (parser,
3804 /*typename_keyword_p=*/false,
3805 /*template_keyword_p=*/false,
3806 none_type,
3807 /*check_dependency=*/false,
3808 /*class_head_p=*/false,
3809 declarator_p);
3811 /* If an error occurred, assume that the name of the
3812 destructor is the same as the name of the qualifying
3813 class. That allows us to keep parsing after running
3814 into ill-formed destructor names. */
3815 if (type_decl == error_mark_node && scope)
3816 return build_nt (BIT_NOT_EXPR, scope);
3817 else if (type_decl == error_mark_node)
3818 return error_mark_node;
3820 /* Check that destructor name and scope match. */
3821 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3823 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3824 error ("declaration of %<~%T%> as member of %qT",
3825 type_decl, scope);
3826 cp_parser_simulate_error (parser);
3827 return error_mark_node;
3830 /* [class.dtor]
3832 A typedef-name that names a class shall not be used as the
3833 identifier in the declarator for a destructor declaration. */
3834 if (declarator_p
3835 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3836 && !DECL_SELF_REFERENCE_P (type_decl)
3837 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3838 error ("typedef-name %qD used as destructor declarator",
3839 type_decl);
3841 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3844 case CPP_KEYWORD:
3845 if (token->keyword == RID_OPERATOR)
3847 tree id;
3849 /* This could be a template-id, so we try that first. */
3850 cp_parser_parse_tentatively (parser);
3851 /* Try a template-id. */
3852 id = cp_parser_template_id (parser, template_keyword_p,
3853 /*check_dependency_p=*/true,
3854 declarator_p);
3855 /* If that worked, we're done. */
3856 if (cp_parser_parse_definitely (parser))
3857 return id;
3858 /* We still don't know whether we're looking at an
3859 operator-function-id or a conversion-function-id. */
3860 cp_parser_parse_tentatively (parser);
3861 /* Try an operator-function-id. */
3862 id = cp_parser_operator_function_id (parser);
3863 /* If that didn't work, try a conversion-function-id. */
3864 if (!cp_parser_parse_definitely (parser))
3865 id = cp_parser_conversion_function_id (parser);
3867 return id;
3869 /* Fall through. */
3871 default:
3872 if (optional_p)
3873 return NULL_TREE;
3874 cp_parser_error (parser, "expected unqualified-id");
3875 return error_mark_node;
3879 /* Parse an (optional) nested-name-specifier.
3881 nested-name-specifier:
3882 class-or-namespace-name :: nested-name-specifier [opt]
3883 class-or-namespace-name :: template nested-name-specifier [opt]
3885 PARSER->SCOPE should be set appropriately before this function is
3886 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3887 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3888 in name lookups.
3890 Sets PARSER->SCOPE to the class (TYPE) or namespace
3891 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3892 it unchanged if there is no nested-name-specifier. Returns the new
3893 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3895 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3896 part of a declaration and/or decl-specifier. */
3898 static tree
3899 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3900 bool typename_keyword_p,
3901 bool check_dependency_p,
3902 bool type_p,
3903 bool is_declaration)
3905 bool success = false;
3906 cp_token_position start = 0;
3907 cp_token *token;
3909 /* Remember where the nested-name-specifier starts. */
3910 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3912 start = cp_lexer_token_position (parser->lexer, false);
3913 push_deferring_access_checks (dk_deferred);
3916 while (true)
3918 tree new_scope;
3919 tree old_scope;
3920 tree saved_qualifying_scope;
3921 bool template_keyword_p;
3923 /* Spot cases that cannot be the beginning of a
3924 nested-name-specifier. */
3925 token = cp_lexer_peek_token (parser->lexer);
3927 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3928 the already parsed nested-name-specifier. */
3929 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3931 /* Grab the nested-name-specifier and continue the loop. */
3932 cp_parser_pre_parsed_nested_name_specifier (parser);
3933 /* If we originally encountered this nested-name-specifier
3934 with IS_DECLARATION set to false, we will not have
3935 resolved TYPENAME_TYPEs, so we must do so here. */
3936 if (is_declaration
3937 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3939 new_scope = resolve_typename_type (parser->scope,
3940 /*only_current_p=*/false);
3941 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3942 parser->scope = new_scope;
3944 success = true;
3945 continue;
3948 /* Spot cases that cannot be the beginning of a
3949 nested-name-specifier. On the second and subsequent times
3950 through the loop, we look for the `template' keyword. */
3951 if (success && token->keyword == RID_TEMPLATE)
3953 /* A template-id can start a nested-name-specifier. */
3954 else if (token->type == CPP_TEMPLATE_ID)
3956 else
3958 /* If the next token is not an identifier, then it is
3959 definitely not a class-or-namespace-name. */
3960 if (token->type != CPP_NAME)
3961 break;
3962 /* If the following token is neither a `<' (to begin a
3963 template-id), nor a `::', then we are not looking at a
3964 nested-name-specifier. */
3965 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3966 if (token->type != CPP_SCOPE
3967 && !cp_parser_nth_token_starts_template_argument_list_p
3968 (parser, 2))
3969 break;
3972 /* The nested-name-specifier is optional, so we parse
3973 tentatively. */
3974 cp_parser_parse_tentatively (parser);
3976 /* Look for the optional `template' keyword, if this isn't the
3977 first time through the loop. */
3978 if (success)
3979 template_keyword_p = cp_parser_optional_template_keyword (parser);
3980 else
3981 template_keyword_p = false;
3983 /* Save the old scope since the name lookup we are about to do
3984 might destroy it. */
3985 old_scope = parser->scope;
3986 saved_qualifying_scope = parser->qualifying_scope;
3987 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3988 look up names in "X<T>::I" in order to determine that "Y" is
3989 a template. So, if we have a typename at this point, we make
3990 an effort to look through it. */
3991 if (is_declaration
3992 && !typename_keyword_p
3993 && parser->scope
3994 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3995 parser->scope = resolve_typename_type (parser->scope,
3996 /*only_current_p=*/false);
3997 /* Parse the qualifying entity. */
3998 new_scope
3999 = cp_parser_class_or_namespace_name (parser,
4000 typename_keyword_p,
4001 template_keyword_p,
4002 check_dependency_p,
4003 type_p,
4004 is_declaration);
4005 /* Look for the `::' token. */
4006 cp_parser_require (parser, CPP_SCOPE, "`::'");
4008 /* If we found what we wanted, we keep going; otherwise, we're
4009 done. */
4010 if (!cp_parser_parse_definitely (parser))
4012 bool error_p = false;
4014 /* Restore the OLD_SCOPE since it was valid before the
4015 failed attempt at finding the last
4016 class-or-namespace-name. */
4017 parser->scope = old_scope;
4018 parser->qualifying_scope = saved_qualifying_scope;
4019 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4020 break;
4021 /* If the next token is an identifier, and the one after
4022 that is a `::', then any valid interpretation would have
4023 found a class-or-namespace-name. */
4024 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4025 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4026 == CPP_SCOPE)
4027 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4028 != CPP_COMPL))
4030 token = cp_lexer_consume_token (parser->lexer);
4031 if (!error_p)
4033 if (!token->ambiguous_p)
4035 tree decl;
4036 tree ambiguous_decls;
4038 decl = cp_parser_lookup_name (parser, token->u.value,
4039 none_type,
4040 /*is_template=*/false,
4041 /*is_namespace=*/false,
4042 /*check_dependency=*/true,
4043 &ambiguous_decls);
4044 if (TREE_CODE (decl) == TEMPLATE_DECL)
4045 error ("%qD used without template parameters", decl);
4046 else if (ambiguous_decls)
4048 error ("reference to %qD is ambiguous",
4049 token->u.value);
4050 print_candidates (ambiguous_decls);
4051 decl = error_mark_node;
4053 else
4054 cp_parser_name_lookup_error
4055 (parser, token->u.value, decl,
4056 "is not a class or namespace");
4058 parser->scope = error_mark_node;
4059 error_p = true;
4060 /* Treat this as a successful nested-name-specifier
4061 due to:
4063 [basic.lookup.qual]
4065 If the name found is not a class-name (clause
4066 _class_) or namespace-name (_namespace.def_), the
4067 program is ill-formed. */
4068 success = true;
4070 cp_lexer_consume_token (parser->lexer);
4072 break;
4074 /* We've found one valid nested-name-specifier. */
4075 success = true;
4076 /* Name lookup always gives us a DECL. */
4077 if (TREE_CODE (new_scope) == TYPE_DECL)
4078 new_scope = TREE_TYPE (new_scope);
4079 /* Uses of "template" must be followed by actual templates. */
4080 if (template_keyword_p
4081 && !(CLASS_TYPE_P (new_scope)
4082 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4083 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4084 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4085 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4086 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4087 == TEMPLATE_ID_EXPR)))
4088 pedwarn (TYPE_P (new_scope)
4089 ? "%qT is not a template"
4090 : "%qD is not a template",
4091 new_scope);
4092 /* If it is a class scope, try to complete it; we are about to
4093 be looking up names inside the class. */
4094 if (TYPE_P (new_scope)
4095 /* Since checking types for dependency can be expensive,
4096 avoid doing it if the type is already complete. */
4097 && !COMPLETE_TYPE_P (new_scope)
4098 /* Do not try to complete dependent types. */
4099 && !dependent_type_p (new_scope))
4101 new_scope = complete_type (new_scope);
4102 /* If it is a typedef to current class, use the current
4103 class instead, as the typedef won't have any names inside
4104 it yet. */
4105 if (!COMPLETE_TYPE_P (new_scope)
4106 && currently_open_class (new_scope))
4107 new_scope = TYPE_MAIN_VARIANT (new_scope);
4109 /* Make sure we look in the right scope the next time through
4110 the loop. */
4111 parser->scope = new_scope;
4114 /* If parsing tentatively, replace the sequence of tokens that makes
4115 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4116 token. That way, should we re-parse the token stream, we will
4117 not have to repeat the effort required to do the parse, nor will
4118 we issue duplicate error messages. */
4119 if (success && start)
4121 cp_token *token;
4123 token = cp_lexer_token_at (parser->lexer, start);
4124 /* Reset the contents of the START token. */
4125 token->type = CPP_NESTED_NAME_SPECIFIER;
4126 /* Retrieve any deferred checks. Do not pop this access checks yet
4127 so the memory will not be reclaimed during token replacing below. */
4128 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4129 token->u.tree_check_value->value = parser->scope;
4130 token->u.tree_check_value->checks = get_deferred_access_checks ();
4131 token->u.tree_check_value->qualifying_scope =
4132 parser->qualifying_scope;
4133 token->keyword = RID_MAX;
4135 /* Purge all subsequent tokens. */
4136 cp_lexer_purge_tokens_after (parser->lexer, start);
4139 if (start)
4140 pop_to_parent_deferring_access_checks ();
4142 return success ? parser->scope : NULL_TREE;
4145 /* Parse a nested-name-specifier. See
4146 cp_parser_nested_name_specifier_opt for details. This function
4147 behaves identically, except that it will an issue an error if no
4148 nested-name-specifier is present. */
4150 static tree
4151 cp_parser_nested_name_specifier (cp_parser *parser,
4152 bool typename_keyword_p,
4153 bool check_dependency_p,
4154 bool type_p,
4155 bool is_declaration)
4157 tree scope;
4159 /* Look for the nested-name-specifier. */
4160 scope = cp_parser_nested_name_specifier_opt (parser,
4161 typename_keyword_p,
4162 check_dependency_p,
4163 type_p,
4164 is_declaration);
4165 /* If it was not present, issue an error message. */
4166 if (!scope)
4168 cp_parser_error (parser, "expected nested-name-specifier");
4169 parser->scope = NULL_TREE;
4172 return scope;
4175 /* Parse a class-or-namespace-name.
4177 class-or-namespace-name:
4178 class-name
4179 namespace-name
4181 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4182 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4183 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4184 TYPE_P is TRUE iff the next name should be taken as a class-name,
4185 even the same name is declared to be another entity in the same
4186 scope.
4188 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4189 specified by the class-or-namespace-name. If neither is found the
4190 ERROR_MARK_NODE is returned. */
4192 static tree
4193 cp_parser_class_or_namespace_name (cp_parser *parser,
4194 bool typename_keyword_p,
4195 bool template_keyword_p,
4196 bool check_dependency_p,
4197 bool type_p,
4198 bool is_declaration)
4200 tree saved_scope;
4201 tree saved_qualifying_scope;
4202 tree saved_object_scope;
4203 tree scope;
4204 bool only_class_p;
4206 /* Before we try to parse the class-name, we must save away the
4207 current PARSER->SCOPE since cp_parser_class_name will destroy
4208 it. */
4209 saved_scope = parser->scope;
4210 saved_qualifying_scope = parser->qualifying_scope;
4211 saved_object_scope = parser->object_scope;
4212 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4213 there is no need to look for a namespace-name. */
4214 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4215 if (!only_class_p)
4216 cp_parser_parse_tentatively (parser);
4217 scope = cp_parser_class_name (parser,
4218 typename_keyword_p,
4219 template_keyword_p,
4220 type_p ? class_type : none_type,
4221 check_dependency_p,
4222 /*class_head_p=*/false,
4223 is_declaration);
4224 /* If that didn't work, try for a namespace-name. */
4225 if (!only_class_p && !cp_parser_parse_definitely (parser))
4227 /* Restore the saved scope. */
4228 parser->scope = saved_scope;
4229 parser->qualifying_scope = saved_qualifying_scope;
4230 parser->object_scope = saved_object_scope;
4231 /* If we are not looking at an identifier followed by the scope
4232 resolution operator, then this is not part of a
4233 nested-name-specifier. (Note that this function is only used
4234 to parse the components of a nested-name-specifier.) */
4235 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4236 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4237 return error_mark_node;
4238 scope = cp_parser_namespace_name (parser);
4241 return scope;
4244 /* Parse a postfix-expression.
4246 postfix-expression:
4247 primary-expression
4248 postfix-expression [ expression ]
4249 postfix-expression ( expression-list [opt] )
4250 simple-type-specifier ( expression-list [opt] )
4251 typename :: [opt] nested-name-specifier identifier
4252 ( expression-list [opt] )
4253 typename :: [opt] nested-name-specifier template [opt] template-id
4254 ( expression-list [opt] )
4255 postfix-expression . template [opt] id-expression
4256 postfix-expression -> template [opt] id-expression
4257 postfix-expression . pseudo-destructor-name
4258 postfix-expression -> pseudo-destructor-name
4259 postfix-expression ++
4260 postfix-expression --
4261 dynamic_cast < type-id > ( expression )
4262 static_cast < type-id > ( expression )
4263 reinterpret_cast < type-id > ( expression )
4264 const_cast < type-id > ( expression )
4265 typeid ( expression )
4266 typeid ( type-id )
4268 GNU Extension:
4270 postfix-expression:
4271 ( type-id ) { initializer-list , [opt] }
4273 This extension is a GNU version of the C99 compound-literal
4274 construct. (The C99 grammar uses `type-name' instead of `type-id',
4275 but they are essentially the same concept.)
4277 If ADDRESS_P is true, the postfix expression is the operand of the
4278 `&' operator. CAST_P is true if this expression is the target of a
4279 cast.
4281 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4282 class member access expressions [expr.ref].
4284 Returns a representation of the expression. */
4286 static tree
4287 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4288 bool member_access_only_p)
4290 cp_token *token;
4291 enum rid keyword;
4292 cp_id_kind idk = CP_ID_KIND_NONE;
4293 tree postfix_expression = NULL_TREE;
4294 bool is_member_access = false;
4296 /* Peek at the next token. */
4297 token = cp_lexer_peek_token (parser->lexer);
4298 /* Some of the productions are determined by keywords. */
4299 keyword = token->keyword;
4300 switch (keyword)
4302 case RID_DYNCAST:
4303 case RID_STATCAST:
4304 case RID_REINTCAST:
4305 case RID_CONSTCAST:
4307 tree type;
4308 tree expression;
4309 const char *saved_message;
4311 /* All of these can be handled in the same way from the point
4312 of view of parsing. Begin by consuming the token
4313 identifying the cast. */
4314 cp_lexer_consume_token (parser->lexer);
4316 /* New types cannot be defined in the cast. */
4317 saved_message = parser->type_definition_forbidden_message;
4318 parser->type_definition_forbidden_message
4319 = "types may not be defined in casts";
4321 /* Look for the opening `<'. */
4322 cp_parser_require (parser, CPP_LESS, "`<'");
4323 /* Parse the type to which we are casting. */
4324 type = cp_parser_type_id (parser);
4325 /* Look for the closing `>'. */
4326 cp_parser_require (parser, CPP_GREATER, "`>'");
4327 /* Restore the old message. */
4328 parser->type_definition_forbidden_message = saved_message;
4330 /* And the expression which is being cast. */
4331 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4332 expression = cp_parser_expression (parser, /*cast_p=*/true);
4333 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4335 /* Only type conversions to integral or enumeration types
4336 can be used in constant-expressions. */
4337 if (!cast_valid_in_integral_constant_expression_p (type)
4338 && (cp_parser_non_integral_constant_expression
4339 (parser,
4340 "a cast to a type other than an integral or "
4341 "enumeration type")))
4342 return error_mark_node;
4344 switch (keyword)
4346 case RID_DYNCAST:
4347 postfix_expression
4348 = build_dynamic_cast (type, expression);
4349 break;
4350 case RID_STATCAST:
4351 postfix_expression
4352 = build_static_cast (type, expression);
4353 break;
4354 case RID_REINTCAST:
4355 postfix_expression
4356 = build_reinterpret_cast (type, expression);
4357 break;
4358 case RID_CONSTCAST:
4359 postfix_expression
4360 = build_const_cast (type, expression);
4361 break;
4362 default:
4363 gcc_unreachable ();
4366 break;
4368 case RID_TYPEID:
4370 tree type;
4371 const char *saved_message;
4372 bool saved_in_type_id_in_expr_p;
4374 /* Consume the `typeid' token. */
4375 cp_lexer_consume_token (parser->lexer);
4376 /* Look for the `(' token. */
4377 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4378 /* Types cannot be defined in a `typeid' expression. */
4379 saved_message = parser->type_definition_forbidden_message;
4380 parser->type_definition_forbidden_message
4381 = "types may not be defined in a `typeid\' expression";
4382 /* We can't be sure yet whether we're looking at a type-id or an
4383 expression. */
4384 cp_parser_parse_tentatively (parser);
4385 /* Try a type-id first. */
4386 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4387 parser->in_type_id_in_expr_p = true;
4388 type = cp_parser_type_id (parser);
4389 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4390 /* Look for the `)' token. Otherwise, we can't be sure that
4391 we're not looking at an expression: consider `typeid (int
4392 (3))', for example. */
4393 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4394 /* If all went well, simply lookup the type-id. */
4395 if (cp_parser_parse_definitely (parser))
4396 postfix_expression = get_typeid (type);
4397 /* Otherwise, fall back to the expression variant. */
4398 else
4400 tree expression;
4402 /* Look for an expression. */
4403 expression = cp_parser_expression (parser, /*cast_p=*/false);
4404 /* Compute its typeid. */
4405 postfix_expression = build_typeid (expression);
4406 /* Look for the `)' token. */
4407 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4409 /* Restore the saved message. */
4410 parser->type_definition_forbidden_message = saved_message;
4411 /* `typeid' may not appear in an integral constant expression. */
4412 if (cp_parser_non_integral_constant_expression(parser,
4413 "`typeid' operator"))
4414 return error_mark_node;
4416 break;
4418 case RID_TYPENAME:
4420 tree type;
4421 /* The syntax permitted here is the same permitted for an
4422 elaborated-type-specifier. */
4423 type = cp_parser_elaborated_type_specifier (parser,
4424 /*is_friend=*/false,
4425 /*is_declaration=*/false);
4426 postfix_expression = cp_parser_functional_cast (parser, type);
4428 break;
4430 default:
4432 tree type;
4434 /* If the next thing is a simple-type-specifier, we may be
4435 looking at a functional cast. We could also be looking at
4436 an id-expression. So, we try the functional cast, and if
4437 that doesn't work we fall back to the primary-expression. */
4438 cp_parser_parse_tentatively (parser);
4439 /* Look for the simple-type-specifier. */
4440 type = cp_parser_simple_type_specifier (parser,
4441 /*decl_specs=*/NULL,
4442 CP_PARSER_FLAGS_NONE);
4443 /* Parse the cast itself. */
4444 if (!cp_parser_error_occurred (parser))
4445 postfix_expression
4446 = cp_parser_functional_cast (parser, type);
4447 /* If that worked, we're done. */
4448 if (cp_parser_parse_definitely (parser))
4449 break;
4451 /* If the functional-cast didn't work out, try a
4452 compound-literal. */
4453 if (cp_parser_allow_gnu_extensions_p (parser)
4454 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4456 VEC(constructor_elt,gc) *initializer_list = NULL;
4457 bool saved_in_type_id_in_expr_p;
4459 cp_parser_parse_tentatively (parser);
4460 /* Consume the `('. */
4461 cp_lexer_consume_token (parser->lexer);
4462 /* Parse the type. */
4463 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4464 parser->in_type_id_in_expr_p = true;
4465 type = cp_parser_type_id (parser);
4466 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4467 /* Look for the `)'. */
4468 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4469 /* Look for the `{'. */
4470 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4471 /* If things aren't going well, there's no need to
4472 keep going. */
4473 if (!cp_parser_error_occurred (parser))
4475 bool non_constant_p;
4476 /* Parse the initializer-list. */
4477 initializer_list
4478 = cp_parser_initializer_list (parser, &non_constant_p);
4479 /* Allow a trailing `,'. */
4480 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4481 cp_lexer_consume_token (parser->lexer);
4482 /* Look for the final `}'. */
4483 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4485 /* If that worked, we're definitely looking at a
4486 compound-literal expression. */
4487 if (cp_parser_parse_definitely (parser))
4489 /* Warn the user that a compound literal is not
4490 allowed in standard C++. */
4491 if (pedantic)
4492 pedwarn ("ISO C++ forbids compound-literals");
4493 /* For simplicity, we disallow compound literals in
4494 constant-expressions. We could
4495 allow compound literals of integer type, whose
4496 initializer was a constant, in constant
4497 expressions. Permitting that usage, as a further
4498 extension, would not change the meaning of any
4499 currently accepted programs. (Of course, as
4500 compound literals are not part of ISO C++, the
4501 standard has nothing to say.) */
4502 if (cp_parser_non_integral_constant_expression
4503 (parser, "non-constant compound literals"))
4505 postfix_expression = error_mark_node;
4506 break;
4508 /* Form the representation of the compound-literal. */
4509 postfix_expression
4510 = finish_compound_literal (type, initializer_list);
4511 break;
4515 /* It must be a primary-expression. */
4516 postfix_expression
4517 = cp_parser_primary_expression (parser, address_p, cast_p,
4518 /*template_arg_p=*/false,
4519 &idk);
4521 break;
4524 /* Keep looping until the postfix-expression is complete. */
4525 while (true)
4527 if (idk == CP_ID_KIND_UNQUALIFIED
4528 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4529 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4530 /* It is not a Koenig lookup function call. */
4531 postfix_expression
4532 = unqualified_name_lookup_error (postfix_expression);
4534 /* Peek at the next token. */
4535 token = cp_lexer_peek_token (parser->lexer);
4537 switch (token->type)
4539 case CPP_OPEN_SQUARE:
4540 postfix_expression
4541 = cp_parser_postfix_open_square_expression (parser,
4542 postfix_expression,
4543 false);
4544 idk = CP_ID_KIND_NONE;
4545 is_member_access = false;
4546 break;
4548 case CPP_OPEN_PAREN:
4549 /* postfix-expression ( expression-list [opt] ) */
4551 bool koenig_p;
4552 bool is_builtin_constant_p;
4553 bool saved_integral_constant_expression_p = false;
4554 bool saved_non_integral_constant_expression_p = false;
4555 tree args;
4557 is_member_access = false;
4559 is_builtin_constant_p
4560 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4561 if (is_builtin_constant_p)
4563 /* The whole point of __builtin_constant_p is to allow
4564 non-constant expressions to appear as arguments. */
4565 saved_integral_constant_expression_p
4566 = parser->integral_constant_expression_p;
4567 saved_non_integral_constant_expression_p
4568 = parser->non_integral_constant_expression_p;
4569 parser->integral_constant_expression_p = false;
4571 args = (cp_parser_parenthesized_expression_list
4572 (parser, /*is_attribute_list=*/false,
4573 /*cast_p=*/false, /*allow_expansion_p=*/true,
4574 /*non_constant_p=*/NULL));
4575 if (is_builtin_constant_p)
4577 parser->integral_constant_expression_p
4578 = saved_integral_constant_expression_p;
4579 parser->non_integral_constant_expression_p
4580 = saved_non_integral_constant_expression_p;
4583 if (args == error_mark_node)
4585 postfix_expression = error_mark_node;
4586 break;
4589 /* Function calls are not permitted in
4590 constant-expressions. */
4591 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4592 && cp_parser_non_integral_constant_expression (parser,
4593 "a function call"))
4595 postfix_expression = error_mark_node;
4596 break;
4599 koenig_p = false;
4600 if (idk == CP_ID_KIND_UNQUALIFIED)
4602 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4604 if (args)
4606 koenig_p = true;
4607 postfix_expression
4608 = perform_koenig_lookup (postfix_expression, args);
4610 else
4611 postfix_expression
4612 = unqualified_fn_lookup_error (postfix_expression);
4614 /* We do not perform argument-dependent lookup if
4615 normal lookup finds a non-function, in accordance
4616 with the expected resolution of DR 218. */
4617 else if (args && is_overloaded_fn (postfix_expression))
4619 tree fn = get_first_fn (postfix_expression);
4621 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4622 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4624 /* Only do argument dependent lookup if regular
4625 lookup does not find a set of member functions.
4626 [basic.lookup.koenig]/2a */
4627 if (!DECL_FUNCTION_MEMBER_P (fn))
4629 koenig_p = true;
4630 postfix_expression
4631 = perform_koenig_lookup (postfix_expression, args);
4636 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4638 tree instance = TREE_OPERAND (postfix_expression, 0);
4639 tree fn = TREE_OPERAND (postfix_expression, 1);
4641 if (processing_template_decl
4642 && (type_dependent_expression_p (instance)
4643 || (!BASELINK_P (fn)
4644 && TREE_CODE (fn) != FIELD_DECL)
4645 || type_dependent_expression_p (fn)
4646 || any_type_dependent_arguments_p (args)))
4648 postfix_expression
4649 = build_nt_call_list (postfix_expression, args);
4650 break;
4653 if (BASELINK_P (fn))
4654 postfix_expression
4655 = (build_new_method_call
4656 (instance, fn, args, NULL_TREE,
4657 (idk == CP_ID_KIND_QUALIFIED
4658 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4659 /*fn_p=*/NULL));
4660 else
4661 postfix_expression
4662 = finish_call_expr (postfix_expression, args,
4663 /*disallow_virtual=*/false,
4664 /*koenig_p=*/false);
4666 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4667 || TREE_CODE (postfix_expression) == MEMBER_REF
4668 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4669 postfix_expression = (build_offset_ref_call_from_tree
4670 (postfix_expression, args));
4671 else if (idk == CP_ID_KIND_QUALIFIED)
4672 /* A call to a static class member, or a namespace-scope
4673 function. */
4674 postfix_expression
4675 = finish_call_expr (postfix_expression, args,
4676 /*disallow_virtual=*/true,
4677 koenig_p);
4678 else
4679 /* All other function calls. */
4680 postfix_expression
4681 = finish_call_expr (postfix_expression, args,
4682 /*disallow_virtual=*/false,
4683 koenig_p);
4685 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4686 idk = CP_ID_KIND_NONE;
4688 break;
4690 case CPP_DOT:
4691 case CPP_DEREF:
4692 /* postfix-expression . template [opt] id-expression
4693 postfix-expression . pseudo-destructor-name
4694 postfix-expression -> template [opt] id-expression
4695 postfix-expression -> pseudo-destructor-name */
4697 /* Consume the `.' or `->' operator. */
4698 cp_lexer_consume_token (parser->lexer);
4700 postfix_expression
4701 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4702 postfix_expression,
4703 false, &idk);
4705 is_member_access = true;
4706 break;
4708 case CPP_PLUS_PLUS:
4709 /* postfix-expression ++ */
4710 /* Consume the `++' token. */
4711 cp_lexer_consume_token (parser->lexer);
4712 /* Generate a representation for the complete expression. */
4713 postfix_expression
4714 = finish_increment_expr (postfix_expression,
4715 POSTINCREMENT_EXPR);
4716 /* Increments may not appear in constant-expressions. */
4717 if (cp_parser_non_integral_constant_expression (parser,
4718 "an increment"))
4719 postfix_expression = error_mark_node;
4720 idk = CP_ID_KIND_NONE;
4721 is_member_access = false;
4722 break;
4724 case CPP_MINUS_MINUS:
4725 /* postfix-expression -- */
4726 /* Consume the `--' token. */
4727 cp_lexer_consume_token (parser->lexer);
4728 /* Generate a representation for the complete expression. */
4729 postfix_expression
4730 = finish_increment_expr (postfix_expression,
4731 POSTDECREMENT_EXPR);
4732 /* Decrements may not appear in constant-expressions. */
4733 if (cp_parser_non_integral_constant_expression (parser,
4734 "a decrement"))
4735 postfix_expression = error_mark_node;
4736 idk = CP_ID_KIND_NONE;
4737 is_member_access = false;
4738 break;
4740 default:
4741 if (member_access_only_p)
4742 return is_member_access? postfix_expression : error_mark_node;
4743 else
4744 return postfix_expression;
4748 /* We should never get here. */
4749 gcc_unreachable ();
4750 return error_mark_node;
4753 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4754 by cp_parser_builtin_offsetof. We're looking for
4756 postfix-expression [ expression ]
4758 FOR_OFFSETOF is set if we're being called in that context, which
4759 changes how we deal with integer constant expressions. */
4761 static tree
4762 cp_parser_postfix_open_square_expression (cp_parser *parser,
4763 tree postfix_expression,
4764 bool for_offsetof)
4766 tree index;
4768 /* Consume the `[' token. */
4769 cp_lexer_consume_token (parser->lexer);
4771 /* Parse the index expression. */
4772 /* ??? For offsetof, there is a question of what to allow here. If
4773 offsetof is not being used in an integral constant expression context,
4774 then we *could* get the right answer by computing the value at runtime.
4775 If we are in an integral constant expression context, then we might
4776 could accept any constant expression; hard to say without analysis.
4777 Rather than open the barn door too wide right away, allow only integer
4778 constant expressions here. */
4779 if (for_offsetof)
4780 index = cp_parser_constant_expression (parser, false, NULL);
4781 else
4782 index = cp_parser_expression (parser, /*cast_p=*/false);
4784 /* Look for the closing `]'. */
4785 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4787 /* Build the ARRAY_REF. */
4788 postfix_expression = grok_array_decl (postfix_expression, index);
4790 /* When not doing offsetof, array references are not permitted in
4791 constant-expressions. */
4792 if (!for_offsetof
4793 && (cp_parser_non_integral_constant_expression
4794 (parser, "an array reference")))
4795 postfix_expression = error_mark_node;
4797 return postfix_expression;
4800 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4801 by cp_parser_builtin_offsetof. We're looking for
4803 postfix-expression . template [opt] id-expression
4804 postfix-expression . pseudo-destructor-name
4805 postfix-expression -> template [opt] id-expression
4806 postfix-expression -> pseudo-destructor-name
4808 FOR_OFFSETOF is set if we're being called in that context. That sorta
4809 limits what of the above we'll actually accept, but nevermind.
4810 TOKEN_TYPE is the "." or "->" token, which will already have been
4811 removed from the stream. */
4813 static tree
4814 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4815 enum cpp_ttype token_type,
4816 tree postfix_expression,
4817 bool for_offsetof, cp_id_kind *idk)
4819 tree name;
4820 bool dependent_p;
4821 bool pseudo_destructor_p;
4822 tree scope = NULL_TREE;
4824 /* If this is a `->' operator, dereference the pointer. */
4825 if (token_type == CPP_DEREF)
4826 postfix_expression = build_x_arrow (postfix_expression);
4827 /* Check to see whether or not the expression is type-dependent. */
4828 dependent_p = type_dependent_expression_p (postfix_expression);
4829 /* The identifier following the `->' or `.' is not qualified. */
4830 parser->scope = NULL_TREE;
4831 parser->qualifying_scope = NULL_TREE;
4832 parser->object_scope = NULL_TREE;
4833 *idk = CP_ID_KIND_NONE;
4834 /* Enter the scope corresponding to the type of the object
4835 given by the POSTFIX_EXPRESSION. */
4836 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4838 scope = TREE_TYPE (postfix_expression);
4839 /* According to the standard, no expression should ever have
4840 reference type. Unfortunately, we do not currently match
4841 the standard in this respect in that our internal representation
4842 of an expression may have reference type even when the standard
4843 says it does not. Therefore, we have to manually obtain the
4844 underlying type here. */
4845 scope = non_reference (scope);
4846 /* The type of the POSTFIX_EXPRESSION must be complete. */
4847 if (scope == unknown_type_node)
4849 error ("%qE does not have class type", postfix_expression);
4850 scope = NULL_TREE;
4852 else
4853 scope = complete_type_or_else (scope, NULL_TREE);
4854 /* Let the name lookup machinery know that we are processing a
4855 class member access expression. */
4856 parser->context->object_type = scope;
4857 /* If something went wrong, we want to be able to discern that case,
4858 as opposed to the case where there was no SCOPE due to the type
4859 of expression being dependent. */
4860 if (!scope)
4861 scope = error_mark_node;
4862 /* If the SCOPE was erroneous, make the various semantic analysis
4863 functions exit quickly -- and without issuing additional error
4864 messages. */
4865 if (scope == error_mark_node)
4866 postfix_expression = error_mark_node;
4869 /* Assume this expression is not a pseudo-destructor access. */
4870 pseudo_destructor_p = false;
4872 /* If the SCOPE is a scalar type, then, if this is a valid program,
4873 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4874 is type dependent, it can be pseudo-destructor-name or something else.
4875 Try to parse it as pseudo-destructor-name first. */
4876 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4878 tree s;
4879 tree type;
4881 cp_parser_parse_tentatively (parser);
4882 /* Parse the pseudo-destructor-name. */
4883 s = NULL_TREE;
4884 cp_parser_pseudo_destructor_name (parser, &s, &type);
4885 if (dependent_p
4886 && (cp_parser_error_occurred (parser)
4887 || TREE_CODE (type) != TYPE_DECL
4888 || !SCALAR_TYPE_P (TREE_TYPE (type))))
4889 cp_parser_abort_tentative_parse (parser);
4890 else if (cp_parser_parse_definitely (parser))
4892 pseudo_destructor_p = true;
4893 postfix_expression
4894 = finish_pseudo_destructor_expr (postfix_expression,
4895 s, TREE_TYPE (type));
4899 if (!pseudo_destructor_p)
4901 /* If the SCOPE is not a scalar type, we are looking at an
4902 ordinary class member access expression, rather than a
4903 pseudo-destructor-name. */
4904 bool template_p;
4905 /* Parse the id-expression. */
4906 name = (cp_parser_id_expression
4907 (parser,
4908 cp_parser_optional_template_keyword (parser),
4909 /*check_dependency_p=*/true,
4910 &template_p,
4911 /*declarator_p=*/false,
4912 /*optional_p=*/false));
4913 /* In general, build a SCOPE_REF if the member name is qualified.
4914 However, if the name was not dependent and has already been
4915 resolved; there is no need to build the SCOPE_REF. For example;
4917 struct X { void f(); };
4918 template <typename T> void f(T* t) { t->X::f(); }
4920 Even though "t" is dependent, "X::f" is not and has been resolved
4921 to a BASELINK; there is no need to include scope information. */
4923 /* But we do need to remember that there was an explicit scope for
4924 virtual function calls. */
4925 if (parser->scope)
4926 *idk = CP_ID_KIND_QUALIFIED;
4928 /* If the name is a template-id that names a type, we will get a
4929 TYPE_DECL here. That is invalid code. */
4930 if (TREE_CODE (name) == TYPE_DECL)
4932 error ("invalid use of %qD", name);
4933 postfix_expression = error_mark_node;
4935 else
4937 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4939 name = build_qualified_name (/*type=*/NULL_TREE,
4940 parser->scope,
4941 name,
4942 template_p);
4943 parser->scope = NULL_TREE;
4944 parser->qualifying_scope = NULL_TREE;
4945 parser->object_scope = NULL_TREE;
4947 if (scope && name && BASELINK_P (name))
4948 adjust_result_of_qualified_name_lookup
4949 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4950 postfix_expression
4951 = finish_class_member_access_expr (postfix_expression, name,
4952 template_p);
4956 /* We no longer need to look up names in the scope of the object on
4957 the left-hand side of the `.' or `->' operator. */
4958 parser->context->object_type = NULL_TREE;
4960 /* Outside of offsetof, these operators may not appear in
4961 constant-expressions. */
4962 if (!for_offsetof
4963 && (cp_parser_non_integral_constant_expression
4964 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4965 postfix_expression = error_mark_node;
4967 return postfix_expression;
4970 /* Parse a parenthesized expression-list.
4972 expression-list:
4973 assignment-expression
4974 expression-list, assignment-expression
4976 attribute-list:
4977 expression-list
4978 identifier
4979 identifier, expression-list
4981 CAST_P is true if this expression is the target of a cast.
4983 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4984 argument pack.
4986 Returns a TREE_LIST. The TREE_VALUE of each node is a
4987 representation of an assignment-expression. Note that a TREE_LIST
4988 is returned even if there is only a single expression in the list.
4989 error_mark_node is returned if the ( and or ) are
4990 missing. NULL_TREE is returned on no expressions. The parentheses
4991 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4992 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4993 indicates whether or not all of the expressions in the list were
4994 constant. */
4996 static tree
4997 cp_parser_parenthesized_expression_list (cp_parser* parser,
4998 bool is_attribute_list,
4999 bool cast_p,
5000 bool allow_expansion_p,
5001 bool *non_constant_p)
5003 tree expression_list = NULL_TREE;
5004 bool fold_expr_p = is_attribute_list;
5005 tree identifier = NULL_TREE;
5006 bool saved_greater_than_is_operator_p;
5008 /* Assume all the expressions will be constant. */
5009 if (non_constant_p)
5010 *non_constant_p = false;
5012 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5013 return error_mark_node;
5015 /* Within a parenthesized expression, a `>' token is always
5016 the greater-than operator. */
5017 saved_greater_than_is_operator_p
5018 = parser->greater_than_is_operator_p;
5019 parser->greater_than_is_operator_p = true;
5021 /* Consume expressions until there are no more. */
5022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5023 while (true)
5025 tree expr;
5027 /* At the beginning of attribute lists, check to see if the
5028 next token is an identifier. */
5029 if (is_attribute_list
5030 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5032 cp_token *token;
5034 /* Consume the identifier. */
5035 token = cp_lexer_consume_token (parser->lexer);
5036 /* Save the identifier. */
5037 identifier = token->u.value;
5039 else
5041 /* Parse the next assignment-expression. */
5042 if (non_constant_p)
5044 bool expr_non_constant_p;
5045 expr = (cp_parser_constant_expression
5046 (parser, /*allow_non_constant_p=*/true,
5047 &expr_non_constant_p));
5048 if (expr_non_constant_p)
5049 *non_constant_p = true;
5051 else
5052 expr = cp_parser_assignment_expression (parser, cast_p);
5054 if (fold_expr_p)
5055 expr = fold_non_dependent_expr (expr);
5057 /* If we have an ellipsis, then this is an expression
5058 expansion. */
5059 if (allow_expansion_p
5060 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5062 /* Consume the `...'. */
5063 cp_lexer_consume_token (parser->lexer);
5065 /* Build the argument pack. */
5066 expr = make_pack_expansion (expr);
5069 /* Add it to the list. We add error_mark_node
5070 expressions to the list, so that we can still tell if
5071 the correct form for a parenthesized expression-list
5072 is found. That gives better errors. */
5073 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5075 if (expr == error_mark_node)
5076 goto skip_comma;
5079 /* After the first item, attribute lists look the same as
5080 expression lists. */
5081 is_attribute_list = false;
5083 get_comma:;
5084 /* If the next token isn't a `,', then we are done. */
5085 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5086 break;
5088 /* Otherwise, consume the `,' and keep going. */
5089 cp_lexer_consume_token (parser->lexer);
5092 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5094 int ending;
5096 skip_comma:;
5097 /* We try and resync to an unnested comma, as that will give the
5098 user better diagnostics. */
5099 ending = cp_parser_skip_to_closing_parenthesis (parser,
5100 /*recovering=*/true,
5101 /*or_comma=*/true,
5102 /*consume_paren=*/true);
5103 if (ending < 0)
5104 goto get_comma;
5105 if (!ending)
5107 parser->greater_than_is_operator_p
5108 = saved_greater_than_is_operator_p;
5109 return error_mark_node;
5113 parser->greater_than_is_operator_p
5114 = saved_greater_than_is_operator_p;
5116 /* We built up the list in reverse order so we must reverse it now. */
5117 expression_list = nreverse (expression_list);
5118 if (identifier)
5119 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5121 return expression_list;
5124 /* Parse a pseudo-destructor-name.
5126 pseudo-destructor-name:
5127 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5128 :: [opt] nested-name-specifier template template-id :: ~ type-name
5129 :: [opt] nested-name-specifier [opt] ~ type-name
5131 If either of the first two productions is used, sets *SCOPE to the
5132 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5133 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5134 or ERROR_MARK_NODE if the parse fails. */
5136 static void
5137 cp_parser_pseudo_destructor_name (cp_parser* parser,
5138 tree* scope,
5139 tree* type)
5141 bool nested_name_specifier_p;
5143 /* Assume that things will not work out. */
5144 *type = error_mark_node;
5146 /* Look for the optional `::' operator. */
5147 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5148 /* Look for the optional nested-name-specifier. */
5149 nested_name_specifier_p
5150 = (cp_parser_nested_name_specifier_opt (parser,
5151 /*typename_keyword_p=*/false,
5152 /*check_dependency_p=*/true,
5153 /*type_p=*/false,
5154 /*is_declaration=*/true)
5155 != NULL_TREE);
5156 /* Now, if we saw a nested-name-specifier, we might be doing the
5157 second production. */
5158 if (nested_name_specifier_p
5159 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5161 /* Consume the `template' keyword. */
5162 cp_lexer_consume_token (parser->lexer);
5163 /* Parse the template-id. */
5164 cp_parser_template_id (parser,
5165 /*template_keyword_p=*/true,
5166 /*check_dependency_p=*/false,
5167 /*is_declaration=*/true);
5168 /* Look for the `::' token. */
5169 cp_parser_require (parser, CPP_SCOPE, "`::'");
5171 /* If the next token is not a `~', then there might be some
5172 additional qualification. */
5173 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5175 /* Look for the type-name. */
5176 *scope = TREE_TYPE (cp_parser_type_name (parser));
5178 if (*scope == error_mark_node)
5179 return;
5181 /* If we don't have ::~, then something has gone wrong. Since
5182 the only caller of this function is looking for something
5183 after `.' or `->' after a scalar type, most likely the
5184 program is trying to get a member of a non-aggregate
5185 type. */
5186 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5187 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5189 cp_parser_error (parser, "request for member of non-aggregate type");
5190 return;
5193 /* Look for the `::' token. */
5194 cp_parser_require (parser, CPP_SCOPE, "`::'");
5196 else
5197 *scope = NULL_TREE;
5199 /* Look for the `~'. */
5200 cp_parser_require (parser, CPP_COMPL, "`~'");
5201 /* Look for the type-name again. We are not responsible for
5202 checking that it matches the first type-name. */
5203 *type = cp_parser_type_name (parser);
5206 /* Parse a unary-expression.
5208 unary-expression:
5209 postfix-expression
5210 ++ cast-expression
5211 -- cast-expression
5212 unary-operator cast-expression
5213 sizeof unary-expression
5214 sizeof ( type-id )
5215 new-expression
5216 delete-expression
5218 GNU Extensions:
5220 unary-expression:
5221 __extension__ cast-expression
5222 __alignof__ unary-expression
5223 __alignof__ ( type-id )
5224 __real__ cast-expression
5225 __imag__ cast-expression
5226 && identifier
5228 ADDRESS_P is true iff the unary-expression is appearing as the
5229 operand of the `&' operator. CAST_P is true if this expression is
5230 the target of a cast.
5232 Returns a representation of the expression. */
5234 static tree
5235 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5237 cp_token *token;
5238 enum tree_code unary_operator;
5240 /* Peek at the next token. */
5241 token = cp_lexer_peek_token (parser->lexer);
5242 /* Some keywords give away the kind of expression. */
5243 if (token->type == CPP_KEYWORD)
5245 enum rid keyword = token->keyword;
5247 switch (keyword)
5249 case RID_ALIGNOF:
5250 case RID_SIZEOF:
5252 tree operand;
5253 enum tree_code op;
5255 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5256 /* Consume the token. */
5257 cp_lexer_consume_token (parser->lexer);
5258 /* Parse the operand. */
5259 operand = cp_parser_sizeof_operand (parser, keyword);
5261 if (TYPE_P (operand))
5262 return cxx_sizeof_or_alignof_type (operand, op, true);
5263 else
5264 return cxx_sizeof_or_alignof_expr (operand, op);
5267 case RID_NEW:
5268 return cp_parser_new_expression (parser);
5270 case RID_DELETE:
5271 return cp_parser_delete_expression (parser);
5273 case RID_EXTENSION:
5275 /* The saved value of the PEDANTIC flag. */
5276 int saved_pedantic;
5277 tree expr;
5279 /* Save away the PEDANTIC flag. */
5280 cp_parser_extension_opt (parser, &saved_pedantic);
5281 /* Parse the cast-expression. */
5282 expr = cp_parser_simple_cast_expression (parser);
5283 /* Restore the PEDANTIC flag. */
5284 pedantic = saved_pedantic;
5286 return expr;
5289 case RID_REALPART:
5290 case RID_IMAGPART:
5292 tree expression;
5294 /* Consume the `__real__' or `__imag__' token. */
5295 cp_lexer_consume_token (parser->lexer);
5296 /* Parse the cast-expression. */
5297 expression = cp_parser_simple_cast_expression (parser);
5298 /* Create the complete representation. */
5299 return build_x_unary_op ((keyword == RID_REALPART
5300 ? REALPART_EXPR : IMAGPART_EXPR),
5301 expression);
5303 break;
5305 default:
5306 break;
5310 /* Look for the `:: new' and `:: delete', which also signal the
5311 beginning of a new-expression, or delete-expression,
5312 respectively. If the next token is `::', then it might be one of
5313 these. */
5314 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5316 enum rid keyword;
5318 /* See if the token after the `::' is one of the keywords in
5319 which we're interested. */
5320 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5321 /* If it's `new', we have a new-expression. */
5322 if (keyword == RID_NEW)
5323 return cp_parser_new_expression (parser);
5324 /* Similarly, for `delete'. */
5325 else if (keyword == RID_DELETE)
5326 return cp_parser_delete_expression (parser);
5329 /* Look for a unary operator. */
5330 unary_operator = cp_parser_unary_operator (token);
5331 /* The `++' and `--' operators can be handled similarly, even though
5332 they are not technically unary-operators in the grammar. */
5333 if (unary_operator == ERROR_MARK)
5335 if (token->type == CPP_PLUS_PLUS)
5336 unary_operator = PREINCREMENT_EXPR;
5337 else if (token->type == CPP_MINUS_MINUS)
5338 unary_operator = PREDECREMENT_EXPR;
5339 /* Handle the GNU address-of-label extension. */
5340 else if (cp_parser_allow_gnu_extensions_p (parser)
5341 && token->type == CPP_AND_AND)
5343 tree identifier;
5344 tree expression;
5346 /* Consume the '&&' token. */
5347 cp_lexer_consume_token (parser->lexer);
5348 /* Look for the identifier. */
5349 identifier = cp_parser_identifier (parser);
5350 /* Create an expression representing the address. */
5351 expression = finish_label_address_expr (identifier);
5352 if (cp_parser_non_integral_constant_expression (parser,
5353 "the address of a label"))
5354 expression = error_mark_node;
5355 return expression;
5358 if (unary_operator != ERROR_MARK)
5360 tree cast_expression;
5361 tree expression = error_mark_node;
5362 const char *non_constant_p = NULL;
5364 /* Consume the operator token. */
5365 token = cp_lexer_consume_token (parser->lexer);
5366 /* Parse the cast-expression. */
5367 cast_expression
5368 = cp_parser_cast_expression (parser,
5369 unary_operator == ADDR_EXPR,
5370 /*cast_p=*/false);
5371 /* Now, build an appropriate representation. */
5372 switch (unary_operator)
5374 case INDIRECT_REF:
5375 non_constant_p = "`*'";
5376 expression = build_x_indirect_ref (cast_expression, "unary *");
5377 break;
5379 case ADDR_EXPR:
5380 non_constant_p = "`&'";
5381 /* Fall through. */
5382 case BIT_NOT_EXPR:
5383 expression = build_x_unary_op (unary_operator, cast_expression);
5384 break;
5386 case PREINCREMENT_EXPR:
5387 case PREDECREMENT_EXPR:
5388 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5389 ? "`++'" : "`--'");
5390 /* Fall through. */
5391 case UNARY_PLUS_EXPR:
5392 case NEGATE_EXPR:
5393 case TRUTH_NOT_EXPR:
5394 expression = finish_unary_op_expr (unary_operator, cast_expression);
5395 break;
5397 default:
5398 gcc_unreachable ();
5401 if (non_constant_p
5402 && cp_parser_non_integral_constant_expression (parser,
5403 non_constant_p))
5404 expression = error_mark_node;
5406 return expression;
5409 return cp_parser_postfix_expression (parser, address_p, cast_p,
5410 /*member_access_only_p=*/false);
5413 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5414 unary-operator, the corresponding tree code is returned. */
5416 static enum tree_code
5417 cp_parser_unary_operator (cp_token* token)
5419 switch (token->type)
5421 case CPP_MULT:
5422 return INDIRECT_REF;
5424 case CPP_AND:
5425 return ADDR_EXPR;
5427 case CPP_PLUS:
5428 return UNARY_PLUS_EXPR;
5430 case CPP_MINUS:
5431 return NEGATE_EXPR;
5433 case CPP_NOT:
5434 return TRUTH_NOT_EXPR;
5436 case CPP_COMPL:
5437 return BIT_NOT_EXPR;
5439 default:
5440 return ERROR_MARK;
5444 /* Parse a new-expression.
5446 new-expression:
5447 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5448 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5450 Returns a representation of the expression. */
5452 static tree
5453 cp_parser_new_expression (cp_parser* parser)
5455 bool global_scope_p;
5456 tree placement;
5457 tree type;
5458 tree initializer;
5459 tree nelts;
5461 /* Look for the optional `::' operator. */
5462 global_scope_p
5463 = (cp_parser_global_scope_opt (parser,
5464 /*current_scope_valid_p=*/false)
5465 != NULL_TREE);
5466 /* Look for the `new' operator. */
5467 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5468 /* There's no easy way to tell a new-placement from the
5469 `( type-id )' construct. */
5470 cp_parser_parse_tentatively (parser);
5471 /* Look for a new-placement. */
5472 placement = cp_parser_new_placement (parser);
5473 /* If that didn't work out, there's no new-placement. */
5474 if (!cp_parser_parse_definitely (parser))
5475 placement = NULL_TREE;
5477 /* If the next token is a `(', then we have a parenthesized
5478 type-id. */
5479 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5481 /* Consume the `('. */
5482 cp_lexer_consume_token (parser->lexer);
5483 /* Parse the type-id. */
5484 type = cp_parser_type_id (parser);
5485 /* Look for the closing `)'. */
5486 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5487 /* There should not be a direct-new-declarator in this production,
5488 but GCC used to allowed this, so we check and emit a sensible error
5489 message for this case. */
5490 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5492 error ("array bound forbidden after parenthesized type-id");
5493 inform ("try removing the parentheses around the type-id");
5494 cp_parser_direct_new_declarator (parser);
5496 nelts = NULL_TREE;
5498 /* Otherwise, there must be a new-type-id. */
5499 else
5500 type = cp_parser_new_type_id (parser, &nelts);
5502 /* If the next token is a `(', then we have a new-initializer. */
5503 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5504 initializer = cp_parser_new_initializer (parser);
5505 else
5506 initializer = NULL_TREE;
5508 /* A new-expression may not appear in an integral constant
5509 expression. */
5510 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5511 return error_mark_node;
5513 /* Create a representation of the new-expression. */
5514 return build_new (placement, type, nelts, initializer, global_scope_p);
5517 /* Parse a new-placement.
5519 new-placement:
5520 ( expression-list )
5522 Returns the same representation as for an expression-list. */
5524 static tree
5525 cp_parser_new_placement (cp_parser* parser)
5527 tree expression_list;
5529 /* Parse the expression-list. */
5530 expression_list = (cp_parser_parenthesized_expression_list
5531 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5532 /*non_constant_p=*/NULL));
5534 return expression_list;
5537 /* Parse a new-type-id.
5539 new-type-id:
5540 type-specifier-seq new-declarator [opt]
5542 Returns the TYPE allocated. If the new-type-id indicates an array
5543 type, *NELTS is set to the number of elements in the last array
5544 bound; the TYPE will not include the last array bound. */
5546 static tree
5547 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5549 cp_decl_specifier_seq type_specifier_seq;
5550 cp_declarator *new_declarator;
5551 cp_declarator *declarator;
5552 cp_declarator *outer_declarator;
5553 const char *saved_message;
5554 tree type;
5556 /* The type-specifier sequence must not contain type definitions.
5557 (It cannot contain declarations of new types either, but if they
5558 are not definitions we will catch that because they are not
5559 complete.) */
5560 saved_message = parser->type_definition_forbidden_message;
5561 parser->type_definition_forbidden_message
5562 = "types may not be defined in a new-type-id";
5563 /* Parse the type-specifier-seq. */
5564 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5565 &type_specifier_seq);
5566 /* Restore the old message. */
5567 parser->type_definition_forbidden_message = saved_message;
5568 /* Parse the new-declarator. */
5569 new_declarator = cp_parser_new_declarator_opt (parser);
5571 /* Determine the number of elements in the last array dimension, if
5572 any. */
5573 *nelts = NULL_TREE;
5574 /* Skip down to the last array dimension. */
5575 declarator = new_declarator;
5576 outer_declarator = NULL;
5577 while (declarator && (declarator->kind == cdk_pointer
5578 || declarator->kind == cdk_ptrmem))
5580 outer_declarator = declarator;
5581 declarator = declarator->declarator;
5583 while (declarator
5584 && declarator->kind == cdk_array
5585 && declarator->declarator
5586 && declarator->declarator->kind == cdk_array)
5588 outer_declarator = declarator;
5589 declarator = declarator->declarator;
5592 if (declarator && declarator->kind == cdk_array)
5594 *nelts = declarator->u.array.bounds;
5595 if (*nelts == error_mark_node)
5596 *nelts = integer_one_node;
5598 if (outer_declarator)
5599 outer_declarator->declarator = declarator->declarator;
5600 else
5601 new_declarator = NULL;
5604 type = groktypename (&type_specifier_seq, new_declarator);
5605 return type;
5608 /* Parse an (optional) new-declarator.
5610 new-declarator:
5611 ptr-operator new-declarator [opt]
5612 direct-new-declarator
5614 Returns the declarator. */
5616 static cp_declarator *
5617 cp_parser_new_declarator_opt (cp_parser* parser)
5619 enum tree_code code;
5620 tree type;
5621 cp_cv_quals cv_quals;
5623 /* We don't know if there's a ptr-operator next, or not. */
5624 cp_parser_parse_tentatively (parser);
5625 /* Look for a ptr-operator. */
5626 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5627 /* If that worked, look for more new-declarators. */
5628 if (cp_parser_parse_definitely (parser))
5630 cp_declarator *declarator;
5632 /* Parse another optional declarator. */
5633 declarator = cp_parser_new_declarator_opt (parser);
5635 return cp_parser_make_indirect_declarator
5636 (code, type, cv_quals, declarator);
5639 /* If the next token is a `[', there is a direct-new-declarator. */
5640 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5641 return cp_parser_direct_new_declarator (parser);
5643 return NULL;
5646 /* Parse a direct-new-declarator.
5648 direct-new-declarator:
5649 [ expression ]
5650 direct-new-declarator [constant-expression]
5654 static cp_declarator *
5655 cp_parser_direct_new_declarator (cp_parser* parser)
5657 cp_declarator *declarator = NULL;
5659 while (true)
5661 tree expression;
5663 /* Look for the opening `['. */
5664 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5665 /* The first expression is not required to be constant. */
5666 if (!declarator)
5668 expression = cp_parser_expression (parser, /*cast_p=*/false);
5669 /* The standard requires that the expression have integral
5670 type. DR 74 adds enumeration types. We believe that the
5671 real intent is that these expressions be handled like the
5672 expression in a `switch' condition, which also allows
5673 classes with a single conversion to integral or
5674 enumeration type. */
5675 if (!processing_template_decl)
5677 expression
5678 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5679 expression,
5680 /*complain=*/true);
5681 if (!expression)
5683 error ("expression in new-declarator must have integral "
5684 "or enumeration type");
5685 expression = error_mark_node;
5689 /* But all the other expressions must be. */
5690 else
5691 expression
5692 = cp_parser_constant_expression (parser,
5693 /*allow_non_constant=*/false,
5694 NULL);
5695 /* Look for the closing `]'. */
5696 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5698 /* Add this bound to the declarator. */
5699 declarator = make_array_declarator (declarator, expression);
5701 /* If the next token is not a `[', then there are no more
5702 bounds. */
5703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5704 break;
5707 return declarator;
5710 /* Parse a new-initializer.
5712 new-initializer:
5713 ( expression-list [opt] )
5715 Returns a representation of the expression-list. If there is no
5716 expression-list, VOID_ZERO_NODE is returned. */
5718 static tree
5719 cp_parser_new_initializer (cp_parser* parser)
5721 tree expression_list;
5723 expression_list = (cp_parser_parenthesized_expression_list
5724 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5725 /*non_constant_p=*/NULL));
5726 if (!expression_list)
5727 expression_list = void_zero_node;
5729 return expression_list;
5732 /* Parse a delete-expression.
5734 delete-expression:
5735 :: [opt] delete cast-expression
5736 :: [opt] delete [ ] cast-expression
5738 Returns a representation of the expression. */
5740 static tree
5741 cp_parser_delete_expression (cp_parser* parser)
5743 bool global_scope_p;
5744 bool array_p;
5745 tree expression;
5747 /* Look for the optional `::' operator. */
5748 global_scope_p
5749 = (cp_parser_global_scope_opt (parser,
5750 /*current_scope_valid_p=*/false)
5751 != NULL_TREE);
5752 /* Look for the `delete' keyword. */
5753 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5754 /* See if the array syntax is in use. */
5755 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5757 /* Consume the `[' token. */
5758 cp_lexer_consume_token (parser->lexer);
5759 /* Look for the `]' token. */
5760 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5761 /* Remember that this is the `[]' construct. */
5762 array_p = true;
5764 else
5765 array_p = false;
5767 /* Parse the cast-expression. */
5768 expression = cp_parser_simple_cast_expression (parser);
5770 /* A delete-expression may not appear in an integral constant
5771 expression. */
5772 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5773 return error_mark_node;
5775 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5778 /* Parse a cast-expression.
5780 cast-expression:
5781 unary-expression
5782 ( type-id ) cast-expression
5784 ADDRESS_P is true iff the unary-expression is appearing as the
5785 operand of the `&' operator. CAST_P is true if this expression is
5786 the target of a cast.
5788 Returns a representation of the expression. */
5790 static tree
5791 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5793 /* If it's a `(', then we might be looking at a cast. */
5794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5796 tree type = NULL_TREE;
5797 tree expr = NULL_TREE;
5798 bool compound_literal_p;
5799 const char *saved_message;
5801 /* There's no way to know yet whether or not this is a cast.
5802 For example, `(int (3))' is a unary-expression, while `(int)
5803 3' is a cast. So, we resort to parsing tentatively. */
5804 cp_parser_parse_tentatively (parser);
5805 /* Types may not be defined in a cast. */
5806 saved_message = parser->type_definition_forbidden_message;
5807 parser->type_definition_forbidden_message
5808 = "types may not be defined in casts";
5809 /* Consume the `('. */
5810 cp_lexer_consume_token (parser->lexer);
5811 /* A very tricky bit is that `(struct S) { 3 }' is a
5812 compound-literal (which we permit in C++ as an extension).
5813 But, that construct is not a cast-expression -- it is a
5814 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5815 is legal; if the compound-literal were a cast-expression,
5816 you'd need an extra set of parentheses.) But, if we parse
5817 the type-id, and it happens to be a class-specifier, then we
5818 will commit to the parse at that point, because we cannot
5819 undo the action that is done when creating a new class. So,
5820 then we cannot back up and do a postfix-expression.
5822 Therefore, we scan ahead to the closing `)', and check to see
5823 if the token after the `)' is a `{'. If so, we are not
5824 looking at a cast-expression.
5826 Save tokens so that we can put them back. */
5827 cp_lexer_save_tokens (parser->lexer);
5828 /* Skip tokens until the next token is a closing parenthesis.
5829 If we find the closing `)', and the next token is a `{', then
5830 we are looking at a compound-literal. */
5831 compound_literal_p
5832 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5833 /*consume_paren=*/true)
5834 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5835 /* Roll back the tokens we skipped. */
5836 cp_lexer_rollback_tokens (parser->lexer);
5837 /* If we were looking at a compound-literal, simulate an error
5838 so that the call to cp_parser_parse_definitely below will
5839 fail. */
5840 if (compound_literal_p)
5841 cp_parser_simulate_error (parser);
5842 else
5844 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5845 parser->in_type_id_in_expr_p = true;
5846 /* Look for the type-id. */
5847 type = cp_parser_type_id (parser);
5848 /* Look for the closing `)'. */
5849 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5850 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5853 /* Restore the saved message. */
5854 parser->type_definition_forbidden_message = saved_message;
5856 /* If ok so far, parse the dependent expression. We cannot be
5857 sure it is a cast. Consider `(T ())'. It is a parenthesized
5858 ctor of T, but looks like a cast to function returning T
5859 without a dependent expression. */
5860 if (!cp_parser_error_occurred (parser))
5861 expr = cp_parser_cast_expression (parser,
5862 /*address_p=*/false,
5863 /*cast_p=*/true);
5865 if (cp_parser_parse_definitely (parser))
5867 /* Warn about old-style casts, if so requested. */
5868 if (warn_old_style_cast
5869 && !in_system_header
5870 && !VOID_TYPE_P (type)
5871 && current_lang_name != lang_name_c)
5872 warning (OPT_Wold_style_cast, "use of old-style cast");
5874 /* Only type conversions to integral or enumeration types
5875 can be used in constant-expressions. */
5876 if (!cast_valid_in_integral_constant_expression_p (type)
5877 && (cp_parser_non_integral_constant_expression
5878 (parser,
5879 "a cast to a type other than an integral or "
5880 "enumeration type")))
5881 return error_mark_node;
5883 /* Perform the cast. */
5884 expr = build_c_cast (type, expr);
5885 return expr;
5889 /* If we get here, then it's not a cast, so it must be a
5890 unary-expression. */
5891 return cp_parser_unary_expression (parser, address_p, cast_p);
5894 /* Parse a binary expression of the general form:
5896 pm-expression:
5897 cast-expression
5898 pm-expression .* cast-expression
5899 pm-expression ->* cast-expression
5901 multiplicative-expression:
5902 pm-expression
5903 multiplicative-expression * pm-expression
5904 multiplicative-expression / pm-expression
5905 multiplicative-expression % pm-expression
5907 additive-expression:
5908 multiplicative-expression
5909 additive-expression + multiplicative-expression
5910 additive-expression - multiplicative-expression
5912 shift-expression:
5913 additive-expression
5914 shift-expression << additive-expression
5915 shift-expression >> additive-expression
5917 relational-expression:
5918 shift-expression
5919 relational-expression < shift-expression
5920 relational-expression > shift-expression
5921 relational-expression <= shift-expression
5922 relational-expression >= shift-expression
5924 GNU Extension:
5926 relational-expression:
5927 relational-expression <? shift-expression
5928 relational-expression >? shift-expression
5930 equality-expression:
5931 relational-expression
5932 equality-expression == relational-expression
5933 equality-expression != relational-expression
5935 and-expression:
5936 equality-expression
5937 and-expression & equality-expression
5939 exclusive-or-expression:
5940 and-expression
5941 exclusive-or-expression ^ and-expression
5943 inclusive-or-expression:
5944 exclusive-or-expression
5945 inclusive-or-expression | exclusive-or-expression
5947 logical-and-expression:
5948 inclusive-or-expression
5949 logical-and-expression && inclusive-or-expression
5951 logical-or-expression:
5952 logical-and-expression
5953 logical-or-expression || logical-and-expression
5955 All these are implemented with a single function like:
5957 binary-expression:
5958 simple-cast-expression
5959 binary-expression <token> binary-expression
5961 CAST_P is true if this expression is the target of a cast.
5963 The binops_by_token map is used to get the tree codes for each <token> type.
5964 binary-expressions are associated according to a precedence table. */
5966 #define TOKEN_PRECEDENCE(token) \
5967 (((token->type == CPP_GREATER \
5968 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5969 && !parser->greater_than_is_operator_p) \
5970 ? PREC_NOT_OPERATOR \
5971 : binops_by_token[token->type].prec)
5973 static tree
5974 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5976 cp_parser_expression_stack stack;
5977 cp_parser_expression_stack_entry *sp = &stack[0];
5978 tree lhs, rhs;
5979 cp_token *token;
5980 enum tree_code tree_type, lhs_type, rhs_type;
5981 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5982 bool overloaded_p;
5984 /* Parse the first expression. */
5985 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5986 lhs_type = ERROR_MARK;
5988 for (;;)
5990 /* Get an operator token. */
5991 token = cp_lexer_peek_token (parser->lexer);
5993 if (warn_cxx0x_compat
5994 && token->type == CPP_RSHIFT
5995 && !parser->greater_than_is_operator_p)
5997 warning (OPT_Wc__0x_compat,
5998 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5999 &token->location);
6000 warning (OPT_Wc__0x_compat,
6001 "suggest parentheses around %<>>%> expression");
6004 new_prec = TOKEN_PRECEDENCE (token);
6006 /* Popping an entry off the stack means we completed a subexpression:
6007 - either we found a token which is not an operator (`>' where it is not
6008 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6009 will happen repeatedly;
6010 - or, we found an operator which has lower priority. This is the case
6011 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6012 parsing `3 * 4'. */
6013 if (new_prec <= prec)
6015 if (sp == stack)
6016 break;
6017 else
6018 goto pop;
6021 get_rhs:
6022 tree_type = binops_by_token[token->type].tree_type;
6024 /* We used the operator token. */
6025 cp_lexer_consume_token (parser->lexer);
6027 /* Extract another operand. It may be the RHS of this expression
6028 or the LHS of a new, higher priority expression. */
6029 rhs = cp_parser_simple_cast_expression (parser);
6030 rhs_type = ERROR_MARK;
6032 /* Get another operator token. Look up its precedence to avoid
6033 building a useless (immediately popped) stack entry for common
6034 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6035 token = cp_lexer_peek_token (parser->lexer);
6036 lookahead_prec = TOKEN_PRECEDENCE (token);
6037 if (lookahead_prec > new_prec)
6039 /* ... and prepare to parse the RHS of the new, higher priority
6040 expression. Since precedence levels on the stack are
6041 monotonically increasing, we do not have to care about
6042 stack overflows. */
6043 sp->prec = prec;
6044 sp->tree_type = tree_type;
6045 sp->lhs = lhs;
6046 sp->lhs_type = lhs_type;
6047 sp++;
6048 lhs = rhs;
6049 lhs_type = rhs_type;
6050 prec = new_prec;
6051 new_prec = lookahead_prec;
6052 goto get_rhs;
6054 pop:
6055 /* If the stack is not empty, we have parsed into LHS the right side
6056 (`4' in the example above) of an expression we had suspended.
6057 We can use the information on the stack to recover the LHS (`3')
6058 from the stack together with the tree code (`MULT_EXPR'), and
6059 the precedence of the higher level subexpression
6060 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6061 which will be used to actually build the additive expression. */
6062 --sp;
6063 prec = sp->prec;
6064 tree_type = sp->tree_type;
6065 rhs = lhs;
6066 rhs_type = lhs_type;
6067 lhs = sp->lhs;
6068 lhs_type = sp->lhs_type;
6071 overloaded_p = false;
6072 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6073 &overloaded_p);
6074 lhs_type = tree_type;
6076 /* If the binary operator required the use of an overloaded operator,
6077 then this expression cannot be an integral constant-expression.
6078 An overloaded operator can be used even if both operands are
6079 otherwise permissible in an integral constant-expression if at
6080 least one of the operands is of enumeration type. */
6082 if (overloaded_p
6083 && (cp_parser_non_integral_constant_expression
6084 (parser, "calls to overloaded operators")))
6085 return error_mark_node;
6088 return lhs;
6092 /* Parse the `? expression : assignment-expression' part of a
6093 conditional-expression. The LOGICAL_OR_EXPR is the
6094 logical-or-expression that started the conditional-expression.
6095 Returns a representation of the entire conditional-expression.
6097 This routine is used by cp_parser_assignment_expression.
6099 ? expression : assignment-expression
6101 GNU Extensions:
6103 ? : assignment-expression */
6105 static tree
6106 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6108 tree expr;
6109 tree assignment_expr;
6111 /* Consume the `?' token. */
6112 cp_lexer_consume_token (parser->lexer);
6113 if (cp_parser_allow_gnu_extensions_p (parser)
6114 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6115 /* Implicit true clause. */
6116 expr = NULL_TREE;
6117 else
6118 /* Parse the expression. */
6119 expr = cp_parser_expression (parser, /*cast_p=*/false);
6121 /* The next token should be a `:'. */
6122 cp_parser_require (parser, CPP_COLON, "`:'");
6123 /* Parse the assignment-expression. */
6124 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6126 /* Build the conditional-expression. */
6127 return build_x_conditional_expr (logical_or_expr,
6128 expr,
6129 assignment_expr);
6132 /* Parse an assignment-expression.
6134 assignment-expression:
6135 conditional-expression
6136 logical-or-expression assignment-operator assignment_expression
6137 throw-expression
6139 CAST_P is true if this expression is the target of a cast.
6141 Returns a representation for the expression. */
6143 static tree
6144 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6146 tree expr;
6148 /* If the next token is the `throw' keyword, then we're looking at
6149 a throw-expression. */
6150 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6151 expr = cp_parser_throw_expression (parser);
6152 /* Otherwise, it must be that we are looking at a
6153 logical-or-expression. */
6154 else
6156 /* Parse the binary expressions (logical-or-expression). */
6157 expr = cp_parser_binary_expression (parser, cast_p);
6158 /* If the next token is a `?' then we're actually looking at a
6159 conditional-expression. */
6160 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6161 return cp_parser_question_colon_clause (parser, expr);
6162 else
6164 enum tree_code assignment_operator;
6166 /* If it's an assignment-operator, we're using the second
6167 production. */
6168 assignment_operator
6169 = cp_parser_assignment_operator_opt (parser);
6170 if (assignment_operator != ERROR_MARK)
6172 tree rhs;
6174 /* Parse the right-hand side of the assignment. */
6175 rhs = cp_parser_assignment_expression (parser, cast_p);
6176 /* An assignment may not appear in a
6177 constant-expression. */
6178 if (cp_parser_non_integral_constant_expression (parser,
6179 "an assignment"))
6180 return error_mark_node;
6181 /* Build the assignment expression. */
6182 expr = build_x_modify_expr (expr,
6183 assignment_operator,
6184 rhs);
6189 return expr;
6192 /* Parse an (optional) assignment-operator.
6194 assignment-operator: one of
6195 = *= /= %= += -= >>= <<= &= ^= |=
6197 GNU Extension:
6199 assignment-operator: one of
6200 <?= >?=
6202 If the next token is an assignment operator, the corresponding tree
6203 code is returned, and the token is consumed. For example, for
6204 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6205 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6206 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6207 operator, ERROR_MARK is returned. */
6209 static enum tree_code
6210 cp_parser_assignment_operator_opt (cp_parser* parser)
6212 enum tree_code op;
6213 cp_token *token;
6215 /* Peek at the next toen. */
6216 token = cp_lexer_peek_token (parser->lexer);
6218 switch (token->type)
6220 case CPP_EQ:
6221 op = NOP_EXPR;
6222 break;
6224 case CPP_MULT_EQ:
6225 op = MULT_EXPR;
6226 break;
6228 case CPP_DIV_EQ:
6229 op = TRUNC_DIV_EXPR;
6230 break;
6232 case CPP_MOD_EQ:
6233 op = TRUNC_MOD_EXPR;
6234 break;
6236 case CPP_PLUS_EQ:
6237 op = PLUS_EXPR;
6238 break;
6240 case CPP_MINUS_EQ:
6241 op = MINUS_EXPR;
6242 break;
6244 case CPP_RSHIFT_EQ:
6245 op = RSHIFT_EXPR;
6246 break;
6248 case CPP_LSHIFT_EQ:
6249 op = LSHIFT_EXPR;
6250 break;
6252 case CPP_AND_EQ:
6253 op = BIT_AND_EXPR;
6254 break;
6256 case CPP_XOR_EQ:
6257 op = BIT_XOR_EXPR;
6258 break;
6260 case CPP_OR_EQ:
6261 op = BIT_IOR_EXPR;
6262 break;
6264 default:
6265 /* Nothing else is an assignment operator. */
6266 op = ERROR_MARK;
6269 /* If it was an assignment operator, consume it. */
6270 if (op != ERROR_MARK)
6271 cp_lexer_consume_token (parser->lexer);
6273 return op;
6276 /* Parse an expression.
6278 expression:
6279 assignment-expression
6280 expression , assignment-expression
6282 CAST_P is true if this expression is the target of a cast.
6284 Returns a representation of the expression. */
6286 static tree
6287 cp_parser_expression (cp_parser* parser, bool cast_p)
6289 tree expression = NULL_TREE;
6291 while (true)
6293 tree assignment_expression;
6295 /* Parse the next assignment-expression. */
6296 assignment_expression
6297 = cp_parser_assignment_expression (parser, cast_p);
6298 /* If this is the first assignment-expression, we can just
6299 save it away. */
6300 if (!expression)
6301 expression = assignment_expression;
6302 else
6303 expression = build_x_compound_expr (expression,
6304 assignment_expression);
6305 /* If the next token is not a comma, then we are done with the
6306 expression. */
6307 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6308 break;
6309 /* Consume the `,'. */
6310 cp_lexer_consume_token (parser->lexer);
6311 /* A comma operator cannot appear in a constant-expression. */
6312 if (cp_parser_non_integral_constant_expression (parser,
6313 "a comma operator"))
6314 expression = error_mark_node;
6317 return expression;
6320 /* Parse a constant-expression.
6322 constant-expression:
6323 conditional-expression
6325 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6326 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6327 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6328 is false, NON_CONSTANT_P should be NULL. */
6330 static tree
6331 cp_parser_constant_expression (cp_parser* parser,
6332 bool allow_non_constant_p,
6333 bool *non_constant_p)
6335 bool saved_integral_constant_expression_p;
6336 bool saved_allow_non_integral_constant_expression_p;
6337 bool saved_non_integral_constant_expression_p;
6338 tree expression;
6340 /* It might seem that we could simply parse the
6341 conditional-expression, and then check to see if it were
6342 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6343 one that the compiler can figure out is constant, possibly after
6344 doing some simplifications or optimizations. The standard has a
6345 precise definition of constant-expression, and we must honor
6346 that, even though it is somewhat more restrictive.
6348 For example:
6350 int i[(2, 3)];
6352 is not a legal declaration, because `(2, 3)' is not a
6353 constant-expression. The `,' operator is forbidden in a
6354 constant-expression. However, GCC's constant-folding machinery
6355 will fold this operation to an INTEGER_CST for `3'. */
6357 /* Save the old settings. */
6358 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6359 saved_allow_non_integral_constant_expression_p
6360 = parser->allow_non_integral_constant_expression_p;
6361 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6362 /* We are now parsing a constant-expression. */
6363 parser->integral_constant_expression_p = true;
6364 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6365 parser->non_integral_constant_expression_p = false;
6366 /* Although the grammar says "conditional-expression", we parse an
6367 "assignment-expression", which also permits "throw-expression"
6368 and the use of assignment operators. In the case that
6369 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6370 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6371 actually essential that we look for an assignment-expression.
6372 For example, cp_parser_initializer_clauses uses this function to
6373 determine whether a particular assignment-expression is in fact
6374 constant. */
6375 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6376 /* Restore the old settings. */
6377 parser->integral_constant_expression_p
6378 = saved_integral_constant_expression_p;
6379 parser->allow_non_integral_constant_expression_p
6380 = saved_allow_non_integral_constant_expression_p;
6381 if (allow_non_constant_p)
6382 *non_constant_p = parser->non_integral_constant_expression_p;
6383 else if (parser->non_integral_constant_expression_p)
6384 expression = error_mark_node;
6385 parser->non_integral_constant_expression_p
6386 = saved_non_integral_constant_expression_p;
6388 return expression;
6391 /* Parse __builtin_offsetof.
6393 offsetof-expression:
6394 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6396 offsetof-member-designator:
6397 id-expression
6398 | offsetof-member-designator "." id-expression
6399 | offsetof-member-designator "[" expression "]" */
6401 static tree
6402 cp_parser_builtin_offsetof (cp_parser *parser)
6404 int save_ice_p, save_non_ice_p;
6405 tree type, expr;
6406 cp_id_kind dummy;
6408 /* We're about to accept non-integral-constant things, but will
6409 definitely yield an integral constant expression. Save and
6410 restore these values around our local parsing. */
6411 save_ice_p = parser->integral_constant_expression_p;
6412 save_non_ice_p = parser->non_integral_constant_expression_p;
6414 /* Consume the "__builtin_offsetof" token. */
6415 cp_lexer_consume_token (parser->lexer);
6416 /* Consume the opening `('. */
6417 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6418 /* Parse the type-id. */
6419 type = cp_parser_type_id (parser);
6420 /* Look for the `,'. */
6421 cp_parser_require (parser, CPP_COMMA, "`,'");
6423 /* Build the (type *)null that begins the traditional offsetof macro. */
6424 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6426 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6427 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6428 true, &dummy);
6429 while (true)
6431 cp_token *token = cp_lexer_peek_token (parser->lexer);
6432 switch (token->type)
6434 case CPP_OPEN_SQUARE:
6435 /* offsetof-member-designator "[" expression "]" */
6436 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6437 break;
6439 case CPP_DOT:
6440 /* offsetof-member-designator "." identifier */
6441 cp_lexer_consume_token (parser->lexer);
6442 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6443 true, &dummy);
6444 break;
6446 case CPP_CLOSE_PAREN:
6447 /* Consume the ")" token. */
6448 cp_lexer_consume_token (parser->lexer);
6449 goto success;
6451 default:
6452 /* Error. We know the following require will fail, but
6453 that gives the proper error message. */
6454 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6455 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6456 expr = error_mark_node;
6457 goto failure;
6461 success:
6462 /* If we're processing a template, we can't finish the semantics yet.
6463 Otherwise we can fold the entire expression now. */
6464 if (processing_template_decl)
6465 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6466 else
6467 expr = finish_offsetof (expr);
6469 failure:
6470 parser->integral_constant_expression_p = save_ice_p;
6471 parser->non_integral_constant_expression_p = save_non_ice_p;
6473 return expr;
6476 /* Parse a trait expression. */
6478 static tree
6479 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6481 cp_trait_kind kind;
6482 tree type1, type2 = NULL_TREE;
6483 bool binary = false;
6484 cp_decl_specifier_seq decl_specs;
6486 switch (keyword)
6488 case RID_HAS_NOTHROW_ASSIGN:
6489 kind = CPTK_HAS_NOTHROW_ASSIGN;
6490 break;
6491 case RID_HAS_NOTHROW_CONSTRUCTOR:
6492 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6493 break;
6494 case RID_HAS_NOTHROW_COPY:
6495 kind = CPTK_HAS_NOTHROW_COPY;
6496 break;
6497 case RID_HAS_TRIVIAL_ASSIGN:
6498 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6499 break;
6500 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6501 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6502 break;
6503 case RID_HAS_TRIVIAL_COPY:
6504 kind = CPTK_HAS_TRIVIAL_COPY;
6505 break;
6506 case RID_HAS_TRIVIAL_DESTRUCTOR:
6507 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6508 break;
6509 case RID_HAS_VIRTUAL_DESTRUCTOR:
6510 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6511 break;
6512 case RID_IS_ABSTRACT:
6513 kind = CPTK_IS_ABSTRACT;
6514 break;
6515 case RID_IS_BASE_OF:
6516 kind = CPTK_IS_BASE_OF;
6517 binary = true;
6518 break;
6519 case RID_IS_CLASS:
6520 kind = CPTK_IS_CLASS;
6521 break;
6522 case RID_IS_CONVERTIBLE_TO:
6523 kind = CPTK_IS_CONVERTIBLE_TO;
6524 binary = true;
6525 break;
6526 case RID_IS_EMPTY:
6527 kind = CPTK_IS_EMPTY;
6528 break;
6529 case RID_IS_ENUM:
6530 kind = CPTK_IS_ENUM;
6531 break;
6532 case RID_IS_POD:
6533 kind = CPTK_IS_POD;
6534 break;
6535 case RID_IS_POLYMORPHIC:
6536 kind = CPTK_IS_POLYMORPHIC;
6537 break;
6538 case RID_IS_UNION:
6539 kind = CPTK_IS_UNION;
6540 break;
6541 default:
6542 gcc_unreachable ();
6545 /* Consume the token. */
6546 cp_lexer_consume_token (parser->lexer);
6548 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6550 type1 = cp_parser_type_id (parser);
6552 if (type1 == error_mark_node)
6553 return error_mark_node;
6555 /* Build a trivial decl-specifier-seq. */
6556 clear_decl_specs (&decl_specs);
6557 decl_specs.type = type1;
6559 /* Call grokdeclarator to figure out what type this is. */
6560 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6561 /*initialized=*/0, /*attrlist=*/NULL);
6563 if (binary)
6565 cp_parser_require (parser, CPP_COMMA, "`,'");
6567 type2 = cp_parser_type_id (parser);
6569 if (type2 == error_mark_node)
6570 return error_mark_node;
6572 /* Build a trivial decl-specifier-seq. */
6573 clear_decl_specs (&decl_specs);
6574 decl_specs.type = type2;
6576 /* Call grokdeclarator to figure out what type this is. */
6577 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6578 /*initialized=*/0, /*attrlist=*/NULL);
6581 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6583 /* Complete the trait expression, which may mean either processing
6584 the trait expr now or saving it for template instantiation. */
6585 return finish_trait_expr (kind, type1, type2);
6588 /* Statements [gram.stmt.stmt] */
6590 /* Parse a statement.
6592 statement:
6593 labeled-statement
6594 expression-statement
6595 compound-statement
6596 selection-statement
6597 iteration-statement
6598 jump-statement
6599 declaration-statement
6600 try-block
6602 IN_COMPOUND is true when the statement is nested inside a
6603 cp_parser_compound_statement; this matters for certain pragmas.
6605 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6606 is a (possibly labeled) if statement which is not enclosed in braces
6607 and has an else clause. This is used to implement -Wparentheses. */
6609 static void
6610 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6611 bool in_compound, bool *if_p)
6613 tree statement;
6614 cp_token *token;
6615 location_t statement_location;
6617 restart:
6618 if (if_p != NULL)
6619 *if_p = false;
6620 /* There is no statement yet. */
6621 statement = NULL_TREE;
6622 /* Peek at the next token. */
6623 token = cp_lexer_peek_token (parser->lexer);
6624 /* Remember the location of the first token in the statement. */
6625 statement_location = token->location;
6626 /* If this is a keyword, then that will often determine what kind of
6627 statement we have. */
6628 if (token->type == CPP_KEYWORD)
6630 enum rid keyword = token->keyword;
6632 switch (keyword)
6634 case RID_CASE:
6635 case RID_DEFAULT:
6636 /* Looks like a labeled-statement with a case label.
6637 Parse the label, and then use tail recursion to parse
6638 the statement. */
6639 cp_parser_label_for_labeled_statement (parser);
6640 goto restart;
6642 case RID_IF:
6643 case RID_SWITCH:
6644 statement = cp_parser_selection_statement (parser, if_p);
6645 break;
6647 case RID_WHILE:
6648 case RID_DO:
6649 case RID_FOR:
6650 statement = cp_parser_iteration_statement (parser);
6651 break;
6653 case RID_BREAK:
6654 case RID_CONTINUE:
6655 case RID_RETURN:
6656 case RID_GOTO:
6657 statement = cp_parser_jump_statement (parser);
6658 break;
6660 /* Objective-C++ exception-handling constructs. */
6661 case RID_AT_TRY:
6662 case RID_AT_CATCH:
6663 case RID_AT_FINALLY:
6664 case RID_AT_SYNCHRONIZED:
6665 case RID_AT_THROW:
6666 statement = cp_parser_objc_statement (parser);
6667 break;
6669 case RID_TRY:
6670 statement = cp_parser_try_block (parser);
6671 break;
6673 case RID_NAMESPACE:
6674 /* This must be a namespace alias definition. */
6675 cp_parser_declaration_statement (parser);
6676 return;
6678 default:
6679 /* It might be a keyword like `int' that can start a
6680 declaration-statement. */
6681 break;
6684 else if (token->type == CPP_NAME)
6686 /* If the next token is a `:', then we are looking at a
6687 labeled-statement. */
6688 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6689 if (token->type == CPP_COLON)
6691 /* Looks like a labeled-statement with an ordinary label.
6692 Parse the label, and then use tail recursion to parse
6693 the statement. */
6694 cp_parser_label_for_labeled_statement (parser);
6695 goto restart;
6698 /* Anything that starts with a `{' must be a compound-statement. */
6699 else if (token->type == CPP_OPEN_BRACE)
6700 statement = cp_parser_compound_statement (parser, NULL, false);
6701 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6702 a statement all its own. */
6703 else if (token->type == CPP_PRAGMA)
6705 /* Only certain OpenMP pragmas are attached to statements, and thus
6706 are considered statements themselves. All others are not. In
6707 the context of a compound, accept the pragma as a "statement" and
6708 return so that we can check for a close brace. Otherwise we
6709 require a real statement and must go back and read one. */
6710 if (in_compound)
6711 cp_parser_pragma (parser, pragma_compound);
6712 else if (!cp_parser_pragma (parser, pragma_stmt))
6713 goto restart;
6714 return;
6716 else if (token->type == CPP_EOF)
6718 cp_parser_error (parser, "expected statement");
6719 return;
6722 /* Everything else must be a declaration-statement or an
6723 expression-statement. Try for the declaration-statement
6724 first, unless we are looking at a `;', in which case we know that
6725 we have an expression-statement. */
6726 if (!statement)
6728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6730 cp_parser_parse_tentatively (parser);
6731 /* Try to parse the declaration-statement. */
6732 cp_parser_declaration_statement (parser);
6733 /* If that worked, we're done. */
6734 if (cp_parser_parse_definitely (parser))
6735 return;
6737 /* Look for an expression-statement instead. */
6738 statement = cp_parser_expression_statement (parser, in_statement_expr);
6741 /* Set the line number for the statement. */
6742 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6743 SET_EXPR_LOCATION (statement, statement_location);
6746 /* Parse the label for a labeled-statement, i.e.
6748 identifier :
6749 case constant-expression :
6750 default :
6752 GNU Extension:
6753 case constant-expression ... constant-expression : statement
6755 When a label is parsed without errors, the label is added to the
6756 parse tree by the finish_* functions, so this function doesn't
6757 have to return the label. */
6759 static void
6760 cp_parser_label_for_labeled_statement (cp_parser* parser)
6762 cp_token *token;
6764 /* The next token should be an identifier. */
6765 token = cp_lexer_peek_token (parser->lexer);
6766 if (token->type != CPP_NAME
6767 && token->type != CPP_KEYWORD)
6769 cp_parser_error (parser, "expected labeled-statement");
6770 return;
6773 switch (token->keyword)
6775 case RID_CASE:
6777 tree expr, expr_hi;
6778 cp_token *ellipsis;
6780 /* Consume the `case' token. */
6781 cp_lexer_consume_token (parser->lexer);
6782 /* Parse the constant-expression. */
6783 expr = cp_parser_constant_expression (parser,
6784 /*allow_non_constant_p=*/false,
6785 NULL);
6787 ellipsis = cp_lexer_peek_token (parser->lexer);
6788 if (ellipsis->type == CPP_ELLIPSIS)
6790 /* Consume the `...' token. */
6791 cp_lexer_consume_token (parser->lexer);
6792 expr_hi =
6793 cp_parser_constant_expression (parser,
6794 /*allow_non_constant_p=*/false,
6795 NULL);
6796 /* We don't need to emit warnings here, as the common code
6797 will do this for us. */
6799 else
6800 expr_hi = NULL_TREE;
6802 if (parser->in_switch_statement_p)
6803 finish_case_label (expr, expr_hi);
6804 else
6805 error ("case label %qE not within a switch statement", expr);
6807 break;
6809 case RID_DEFAULT:
6810 /* Consume the `default' token. */
6811 cp_lexer_consume_token (parser->lexer);
6813 if (parser->in_switch_statement_p)
6814 finish_case_label (NULL_TREE, NULL_TREE);
6815 else
6816 error ("case label not within a switch statement");
6817 break;
6819 default:
6820 /* Anything else must be an ordinary label. */
6821 finish_label_stmt (cp_parser_identifier (parser));
6822 break;
6825 /* Require the `:' token. */
6826 cp_parser_require (parser, CPP_COLON, "`:'");
6829 /* Parse an expression-statement.
6831 expression-statement:
6832 expression [opt] ;
6834 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6835 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6836 indicates whether this expression-statement is part of an
6837 expression statement. */
6839 static tree
6840 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6842 tree statement = NULL_TREE;
6844 /* If the next token is a ';', then there is no expression
6845 statement. */
6846 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6847 statement = cp_parser_expression (parser, /*cast_p=*/false);
6849 /* Consume the final `;'. */
6850 cp_parser_consume_semicolon_at_end_of_statement (parser);
6852 if (in_statement_expr
6853 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6854 /* This is the final expression statement of a statement
6855 expression. */
6856 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6857 else if (statement)
6858 statement = finish_expr_stmt (statement);
6859 else
6860 finish_stmt ();
6862 return statement;
6865 /* Parse a compound-statement.
6867 compound-statement:
6868 { statement-seq [opt] }
6870 GNU extension:
6872 compound-statement:
6873 { label-declaration-seq [opt] statement-seq [opt] }
6875 label-declaration-seq:
6876 label-declaration
6877 label-declaration-seq label-declaration
6879 Returns a tree representing the statement. */
6881 static tree
6882 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6883 bool in_try)
6885 tree compound_stmt;
6887 /* Consume the `{'. */
6888 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6889 return error_mark_node;
6890 /* Begin the compound-statement. */
6891 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6892 /* If the next keyword is `__label__' we have a label declaration. */
6893 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6894 cp_parser_label_declaration (parser);
6895 /* Parse an (optional) statement-seq. */
6896 cp_parser_statement_seq_opt (parser, in_statement_expr);
6897 /* Finish the compound-statement. */
6898 finish_compound_stmt (compound_stmt);
6899 /* Consume the `}'. */
6900 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6902 return compound_stmt;
6905 /* Parse an (optional) statement-seq.
6907 statement-seq:
6908 statement
6909 statement-seq [opt] statement */
6911 static void
6912 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6914 /* Scan statements until there aren't any more. */
6915 while (true)
6917 cp_token *token = cp_lexer_peek_token (parser->lexer);
6919 /* If we're looking at a `}', then we've run out of statements. */
6920 if (token->type == CPP_CLOSE_BRACE
6921 || token->type == CPP_EOF
6922 || token->type == CPP_PRAGMA_EOL)
6923 break;
6925 /* If we are in a compound statement and find 'else' then
6926 something went wrong. */
6927 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6929 if (parser->in_statement & IN_IF_STMT)
6930 break;
6931 else
6933 token = cp_lexer_consume_token (parser->lexer);
6934 error ("%<else%> without a previous %<if%>");
6938 /* Parse the statement. */
6939 cp_parser_statement (parser, in_statement_expr, true, NULL);
6943 /* Parse a selection-statement.
6945 selection-statement:
6946 if ( condition ) statement
6947 if ( condition ) statement else statement
6948 switch ( condition ) statement
6950 Returns the new IF_STMT or SWITCH_STMT.
6952 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6953 is a (possibly labeled) if statement which is not enclosed in
6954 braces and has an else clause. This is used to implement
6955 -Wparentheses. */
6957 static tree
6958 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6960 cp_token *token;
6961 enum rid keyword;
6963 if (if_p != NULL)
6964 *if_p = false;
6966 /* Peek at the next token. */
6967 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6969 /* See what kind of keyword it is. */
6970 keyword = token->keyword;
6971 switch (keyword)
6973 case RID_IF:
6974 case RID_SWITCH:
6976 tree statement;
6977 tree condition;
6979 /* Look for the `('. */
6980 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6982 cp_parser_skip_to_end_of_statement (parser);
6983 return error_mark_node;
6986 /* Begin the selection-statement. */
6987 if (keyword == RID_IF)
6988 statement = begin_if_stmt ();
6989 else
6990 statement = begin_switch_stmt ();
6992 /* Parse the condition. */
6993 condition = cp_parser_condition (parser);
6994 /* Look for the `)'. */
6995 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6996 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6997 /*consume_paren=*/true);
6999 if (keyword == RID_IF)
7001 bool nested_if;
7002 unsigned char in_statement;
7004 /* Add the condition. */
7005 finish_if_stmt_cond (condition, statement);
7007 /* Parse the then-clause. */
7008 in_statement = parser->in_statement;
7009 parser->in_statement |= IN_IF_STMT;
7010 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7011 parser->in_statement = in_statement;
7013 finish_then_clause (statement);
7015 /* If the next token is `else', parse the else-clause. */
7016 if (cp_lexer_next_token_is_keyword (parser->lexer,
7017 RID_ELSE))
7019 /* Consume the `else' keyword. */
7020 cp_lexer_consume_token (parser->lexer);
7021 begin_else_clause (statement);
7022 /* Parse the else-clause. */
7023 cp_parser_implicitly_scoped_statement (parser, NULL);
7024 finish_else_clause (statement);
7026 /* If we are currently parsing a then-clause, then
7027 IF_P will not be NULL. We set it to true to
7028 indicate that this if statement has an else clause.
7029 This may trigger the Wparentheses warning below
7030 when we get back up to the parent if statement. */
7031 if (if_p != NULL)
7032 *if_p = true;
7034 else
7036 /* This if statement does not have an else clause. If
7037 NESTED_IF is true, then the then-clause is an if
7038 statement which does have an else clause. We warn
7039 about the potential ambiguity. */
7040 if (nested_if)
7041 warning (OPT_Wparentheses,
7042 ("%Hsuggest explicit braces "
7043 "to avoid ambiguous %<else%>"),
7044 EXPR_LOCUS (statement));
7047 /* Now we're all done with the if-statement. */
7048 finish_if_stmt (statement);
7050 else
7052 bool in_switch_statement_p;
7053 unsigned char in_statement;
7055 /* Add the condition. */
7056 finish_switch_cond (condition, statement);
7058 /* Parse the body of the switch-statement. */
7059 in_switch_statement_p = parser->in_switch_statement_p;
7060 in_statement = parser->in_statement;
7061 parser->in_switch_statement_p = true;
7062 parser->in_statement |= IN_SWITCH_STMT;
7063 cp_parser_implicitly_scoped_statement (parser, NULL);
7064 parser->in_switch_statement_p = in_switch_statement_p;
7065 parser->in_statement = in_statement;
7067 /* Now we're all done with the switch-statement. */
7068 finish_switch_stmt (statement);
7071 return statement;
7073 break;
7075 default:
7076 cp_parser_error (parser, "expected selection-statement");
7077 return error_mark_node;
7081 /* Parse a condition.
7083 condition:
7084 expression
7085 type-specifier-seq declarator = assignment-expression
7087 GNU Extension:
7089 condition:
7090 type-specifier-seq declarator asm-specification [opt]
7091 attributes [opt] = assignment-expression
7093 Returns the expression that should be tested. */
7095 static tree
7096 cp_parser_condition (cp_parser* parser)
7098 cp_decl_specifier_seq type_specifiers;
7099 const char *saved_message;
7101 /* Try the declaration first. */
7102 cp_parser_parse_tentatively (parser);
7103 /* New types are not allowed in the type-specifier-seq for a
7104 condition. */
7105 saved_message = parser->type_definition_forbidden_message;
7106 parser->type_definition_forbidden_message
7107 = "types may not be defined in conditions";
7108 /* Parse the type-specifier-seq. */
7109 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7110 &type_specifiers);
7111 /* Restore the saved message. */
7112 parser->type_definition_forbidden_message = saved_message;
7113 /* If all is well, we might be looking at a declaration. */
7114 if (!cp_parser_error_occurred (parser))
7116 tree decl;
7117 tree asm_specification;
7118 tree attributes;
7119 cp_declarator *declarator;
7120 tree initializer = NULL_TREE;
7122 /* Parse the declarator. */
7123 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7124 /*ctor_dtor_or_conv_p=*/NULL,
7125 /*parenthesized_p=*/NULL,
7126 /*member_p=*/false);
7127 /* Parse the attributes. */
7128 attributes = cp_parser_attributes_opt (parser);
7129 /* Parse the asm-specification. */
7130 asm_specification = cp_parser_asm_specification_opt (parser);
7131 /* If the next token is not an `=', then we might still be
7132 looking at an expression. For example:
7134 if (A(a).x)
7136 looks like a decl-specifier-seq and a declarator -- but then
7137 there is no `=', so this is an expression. */
7138 cp_parser_require (parser, CPP_EQ, "`='");
7139 /* If we did see an `=', then we are looking at a declaration
7140 for sure. */
7141 if (cp_parser_parse_definitely (parser))
7143 tree pushed_scope;
7144 bool non_constant_p;
7146 /* Create the declaration. */
7147 decl = start_decl (declarator, &type_specifiers,
7148 /*initialized_p=*/true,
7149 attributes, /*prefix_attributes=*/NULL_TREE,
7150 &pushed_scope);
7151 /* Parse the assignment-expression. */
7152 initializer
7153 = cp_parser_constant_expression (parser,
7154 /*allow_non_constant_p=*/true,
7155 &non_constant_p);
7156 if (!non_constant_p)
7157 initializer = fold_non_dependent_expr (initializer);
7159 /* Process the initializer. */
7160 cp_finish_decl (decl,
7161 initializer, !non_constant_p,
7162 asm_specification,
7163 LOOKUP_ONLYCONVERTING);
7165 if (pushed_scope)
7166 pop_scope (pushed_scope);
7168 return convert_from_reference (decl);
7171 /* If we didn't even get past the declarator successfully, we are
7172 definitely not looking at a declaration. */
7173 else
7174 cp_parser_abort_tentative_parse (parser);
7176 /* Otherwise, we are looking at an expression. */
7177 return cp_parser_expression (parser, /*cast_p=*/false);
7180 /* We check for a ) immediately followed by ; with no whitespacing
7181 between. This is used to issue a warning for:
7183 while (...);
7185 and:
7187 for (...);
7189 as the semicolon is probably extraneous.
7191 On parse errors, the next token might not be a ), so do nothing in
7192 that case. */
7194 static void
7195 check_empty_body (cp_parser* parser, const char* type)
7197 cp_token *token;
7198 cp_token *close_paren;
7199 expanded_location close_loc;
7200 expanded_location semi_loc;
7202 close_paren = cp_lexer_peek_token (parser->lexer);
7203 if (close_paren->type != CPP_CLOSE_PAREN)
7204 return;
7206 close_loc = expand_location (close_paren->location);
7207 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7209 if (token->type != CPP_SEMICOLON
7210 || (token->flags & PREV_WHITE))
7211 return;
7213 semi_loc = expand_location (token->location);
7214 if (close_loc.line == semi_loc.line
7215 #ifdef USE_MAPPED_LOCATION
7216 && close_loc.column+1 == semi_loc.column
7217 #endif
7219 warning (OPT_Wempty_body,
7220 "suggest a space before %<;%> or explicit braces around empty "
7221 "body in %<%s%> statement",
7222 type);
7225 /* Parse an iteration-statement.
7227 iteration-statement:
7228 while ( condition ) statement
7229 do statement while ( expression ) ;
7230 for ( for-init-statement condition [opt] ; expression [opt] )
7231 statement
7233 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7235 static tree
7236 cp_parser_iteration_statement (cp_parser* parser)
7238 cp_token *token;
7239 enum rid keyword;
7240 tree statement;
7241 unsigned char in_statement;
7243 /* Peek at the next token. */
7244 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7245 if (!token)
7246 return error_mark_node;
7248 /* Remember whether or not we are already within an iteration
7249 statement. */
7250 in_statement = parser->in_statement;
7252 /* See what kind of keyword it is. */
7253 keyword = token->keyword;
7254 switch (keyword)
7256 case RID_WHILE:
7258 tree condition;
7260 /* Begin the while-statement. */
7261 statement = begin_while_stmt ();
7262 /* Look for the `('. */
7263 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7264 /* Parse the condition. */
7265 condition = cp_parser_condition (parser);
7266 finish_while_stmt_cond (condition, statement);
7267 check_empty_body (parser, "while");
7268 /* Look for the `)'. */
7269 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7270 /* Parse the dependent statement. */
7271 parser->in_statement = IN_ITERATION_STMT;
7272 cp_parser_already_scoped_statement (parser);
7273 parser->in_statement = in_statement;
7274 /* We're done with the while-statement. */
7275 finish_while_stmt (statement);
7277 break;
7279 case RID_DO:
7281 tree expression;
7283 /* Begin the do-statement. */
7284 statement = begin_do_stmt ();
7285 /* Parse the body of the do-statement. */
7286 parser->in_statement = IN_ITERATION_STMT;
7287 cp_parser_implicitly_scoped_statement (parser, NULL);
7288 parser->in_statement = in_statement;
7289 finish_do_body (statement);
7290 /* Look for the `while' keyword. */
7291 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7292 /* Look for the `('. */
7293 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7294 /* Parse the expression. */
7295 expression = cp_parser_expression (parser, /*cast_p=*/false);
7296 /* We're done with the do-statement. */
7297 finish_do_stmt (expression, statement);
7298 /* Look for the `)'. */
7299 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7300 /* Look for the `;'. */
7301 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7303 break;
7305 case RID_FOR:
7307 tree condition = NULL_TREE;
7308 tree expression = NULL_TREE;
7310 /* Begin the for-statement. */
7311 statement = begin_for_stmt ();
7312 /* Look for the `('. */
7313 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7314 /* Parse the initialization. */
7315 cp_parser_for_init_statement (parser);
7316 finish_for_init_stmt (statement);
7318 /* If there's a condition, process it. */
7319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7320 condition = cp_parser_condition (parser);
7321 finish_for_cond (condition, statement);
7322 /* Look for the `;'. */
7323 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7325 /* If there's an expression, process it. */
7326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7327 expression = cp_parser_expression (parser, /*cast_p=*/false);
7328 finish_for_expr (expression, statement);
7329 check_empty_body (parser, "for");
7330 /* Look for the `)'. */
7331 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7333 /* Parse the body of the for-statement. */
7334 parser->in_statement = IN_ITERATION_STMT;
7335 cp_parser_already_scoped_statement (parser);
7336 parser->in_statement = in_statement;
7338 /* We're done with the for-statement. */
7339 finish_for_stmt (statement);
7341 break;
7343 default:
7344 cp_parser_error (parser, "expected iteration-statement");
7345 statement = error_mark_node;
7346 break;
7349 return statement;
7352 /* Parse a for-init-statement.
7354 for-init-statement:
7355 expression-statement
7356 simple-declaration */
7358 static void
7359 cp_parser_for_init_statement (cp_parser* parser)
7361 /* If the next token is a `;', then we have an empty
7362 expression-statement. Grammatically, this is also a
7363 simple-declaration, but an invalid one, because it does not
7364 declare anything. Therefore, if we did not handle this case
7365 specially, we would issue an error message about an invalid
7366 declaration. */
7367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7369 /* We're going to speculatively look for a declaration, falling back
7370 to an expression, if necessary. */
7371 cp_parser_parse_tentatively (parser);
7372 /* Parse the declaration. */
7373 cp_parser_simple_declaration (parser,
7374 /*function_definition_allowed_p=*/false);
7375 /* If the tentative parse failed, then we shall need to look for an
7376 expression-statement. */
7377 if (cp_parser_parse_definitely (parser))
7378 return;
7381 cp_parser_expression_statement (parser, false);
7384 /* Parse a jump-statement.
7386 jump-statement:
7387 break ;
7388 continue ;
7389 return expression [opt] ;
7390 goto identifier ;
7392 GNU extension:
7394 jump-statement:
7395 goto * expression ;
7397 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7399 static tree
7400 cp_parser_jump_statement (cp_parser* parser)
7402 tree statement = error_mark_node;
7403 cp_token *token;
7404 enum rid keyword;
7405 unsigned char in_statement;
7407 /* Peek at the next token. */
7408 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7409 if (!token)
7410 return error_mark_node;
7412 /* See what kind of keyword it is. */
7413 keyword = token->keyword;
7414 switch (keyword)
7416 case RID_BREAK:
7417 in_statement = parser->in_statement & ~IN_IF_STMT;
7418 switch (in_statement)
7420 case 0:
7421 error ("break statement not within loop or switch");
7422 break;
7423 default:
7424 gcc_assert ((in_statement & IN_SWITCH_STMT)
7425 || in_statement == IN_ITERATION_STMT);
7426 statement = finish_break_stmt ();
7427 break;
7428 case IN_OMP_BLOCK:
7429 error ("invalid exit from OpenMP structured block");
7430 break;
7431 case IN_OMP_FOR:
7432 error ("break statement used with OpenMP for loop");
7433 break;
7435 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7436 break;
7438 case RID_CONTINUE:
7439 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7441 case 0:
7442 error ("continue statement not within a loop");
7443 break;
7444 case IN_ITERATION_STMT:
7445 case IN_OMP_FOR:
7446 statement = finish_continue_stmt ();
7447 break;
7448 case IN_OMP_BLOCK:
7449 error ("invalid exit from OpenMP structured block");
7450 break;
7451 default:
7452 gcc_unreachable ();
7454 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7455 break;
7457 case RID_RETURN:
7459 tree expr;
7461 /* If the next token is a `;', then there is no
7462 expression. */
7463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7464 expr = cp_parser_expression (parser, /*cast_p=*/false);
7465 else
7466 expr = NULL_TREE;
7467 /* Build the return-statement. */
7468 statement = finish_return_stmt (expr);
7469 /* Look for the final `;'. */
7470 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7472 break;
7474 case RID_GOTO:
7475 /* Create the goto-statement. */
7476 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7478 /* Issue a warning about this use of a GNU extension. */
7479 if (pedantic)
7480 pedwarn ("ISO C++ forbids computed gotos");
7481 /* Consume the '*' token. */
7482 cp_lexer_consume_token (parser->lexer);
7483 /* Parse the dependent expression. */
7484 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7486 else
7487 finish_goto_stmt (cp_parser_identifier (parser));
7488 /* Look for the final `;'. */
7489 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7490 break;
7492 default:
7493 cp_parser_error (parser, "expected jump-statement");
7494 break;
7497 return statement;
7500 /* Parse a declaration-statement.
7502 declaration-statement:
7503 block-declaration */
7505 static void
7506 cp_parser_declaration_statement (cp_parser* parser)
7508 void *p;
7510 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7511 p = obstack_alloc (&declarator_obstack, 0);
7513 /* Parse the block-declaration. */
7514 cp_parser_block_declaration (parser, /*statement_p=*/true);
7516 /* Free any declarators allocated. */
7517 obstack_free (&declarator_obstack, p);
7519 /* Finish off the statement. */
7520 finish_stmt ();
7523 /* Some dependent statements (like `if (cond) statement'), are
7524 implicitly in their own scope. In other words, if the statement is
7525 a single statement (as opposed to a compound-statement), it is
7526 none-the-less treated as if it were enclosed in braces. Any
7527 declarations appearing in the dependent statement are out of scope
7528 after control passes that point. This function parses a statement,
7529 but ensures that is in its own scope, even if it is not a
7530 compound-statement.
7532 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7533 is a (possibly labeled) if statement which is not enclosed in
7534 braces and has an else clause. This is used to implement
7535 -Wparentheses.
7537 Returns the new statement. */
7539 static tree
7540 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7542 tree statement;
7544 if (if_p != NULL)
7545 *if_p = false;
7547 /* Mark if () ; with a special NOP_EXPR. */
7548 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7550 cp_lexer_consume_token (parser->lexer);
7551 statement = add_stmt (build_empty_stmt ());
7553 /* if a compound is opened, we simply parse the statement directly. */
7554 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7555 statement = cp_parser_compound_statement (parser, NULL, false);
7556 /* If the token is not a `{', then we must take special action. */
7557 else
7559 /* Create a compound-statement. */
7560 statement = begin_compound_stmt (0);
7561 /* Parse the dependent-statement. */
7562 cp_parser_statement (parser, NULL_TREE, false, if_p);
7563 /* Finish the dummy compound-statement. */
7564 finish_compound_stmt (statement);
7567 /* Return the statement. */
7568 return statement;
7571 /* For some dependent statements (like `while (cond) statement'), we
7572 have already created a scope. Therefore, even if the dependent
7573 statement is a compound-statement, we do not want to create another
7574 scope. */
7576 static void
7577 cp_parser_already_scoped_statement (cp_parser* parser)
7579 /* If the token is a `{', then we must take special action. */
7580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7581 cp_parser_statement (parser, NULL_TREE, false, NULL);
7582 else
7584 /* Avoid calling cp_parser_compound_statement, so that we
7585 don't create a new scope. Do everything else by hand. */
7586 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7587 cp_parser_statement_seq_opt (parser, NULL_TREE);
7588 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7592 /* Declarations [gram.dcl.dcl] */
7594 /* Parse an optional declaration-sequence.
7596 declaration-seq:
7597 declaration
7598 declaration-seq declaration */
7600 static void
7601 cp_parser_declaration_seq_opt (cp_parser* parser)
7603 while (true)
7605 cp_token *token;
7607 token = cp_lexer_peek_token (parser->lexer);
7609 if (token->type == CPP_CLOSE_BRACE
7610 || token->type == CPP_EOF
7611 || token->type == CPP_PRAGMA_EOL)
7612 break;
7614 if (token->type == CPP_SEMICOLON)
7616 /* A declaration consisting of a single semicolon is
7617 invalid. Allow it unless we're being pedantic. */
7618 cp_lexer_consume_token (parser->lexer);
7619 if (pedantic && !in_system_header)
7620 pedwarn ("extra %<;%>");
7621 continue;
7624 /* If we're entering or exiting a region that's implicitly
7625 extern "C", modify the lang context appropriately. */
7626 if (!parser->implicit_extern_c && token->implicit_extern_c)
7628 push_lang_context (lang_name_c);
7629 parser->implicit_extern_c = true;
7631 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7633 pop_lang_context ();
7634 parser->implicit_extern_c = false;
7637 if (token->type == CPP_PRAGMA)
7639 /* A top-level declaration can consist solely of a #pragma.
7640 A nested declaration cannot, so this is done here and not
7641 in cp_parser_declaration. (A #pragma at block scope is
7642 handled in cp_parser_statement.) */
7643 cp_parser_pragma (parser, pragma_external);
7644 continue;
7647 /* Parse the declaration itself. */
7648 cp_parser_declaration (parser);
7652 /* Parse a declaration.
7654 declaration:
7655 block-declaration
7656 function-definition
7657 template-declaration
7658 explicit-instantiation
7659 explicit-specialization
7660 linkage-specification
7661 namespace-definition
7663 C++0x:
7665 declaration:
7666 concept-definition
7668 GNU extension:
7670 declaration:
7671 __extension__ declaration */
7673 static void
7674 cp_parser_declaration (cp_parser* parser)
7676 cp_token token1;
7677 cp_token token2;
7678 int saved_pedantic;
7679 void *p;
7681 /* Check for the `__extension__' keyword. */
7682 if (cp_parser_extension_opt (parser, &saved_pedantic))
7684 /* Parse the qualified declaration. */
7685 cp_parser_declaration (parser);
7686 /* Restore the PEDANTIC flag. */
7687 pedantic = saved_pedantic;
7689 return;
7692 /* Try to figure out what kind of declaration is present. */
7693 token1 = *cp_lexer_peek_token (parser->lexer);
7695 if (token1.type != CPP_EOF)
7696 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7697 else
7699 token2.type = CPP_EOF;
7700 token2.keyword = RID_MAX;
7703 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7704 p = obstack_alloc (&declarator_obstack, 0);
7706 /* If the next token is `extern' and the following token is a string
7707 literal, then we have a linkage specification. */
7708 if (token1.keyword == RID_EXTERN
7709 && cp_parser_is_string_literal (&token2))
7710 cp_parser_linkage_specification (parser);
7711 /* If the next token is `template', then we have either a template
7712 declaration, an explicit instantiation, or an explicit
7713 specialization. */
7714 else if (token1.keyword == RID_TEMPLATE)
7716 /* `template <>' indicates a template specialization. */
7717 if (token2.type == CPP_LESS
7718 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7719 cp_parser_explicit_specialization (parser);
7720 /* `template <' indicates a template declaration. */
7721 else if (token2.type == CPP_LESS)
7722 cp_parser_template_declaration (parser, /*member_p=*/false);
7723 /* Anything else must be an explicit instantiation. */
7724 else
7725 cp_parser_explicit_instantiation (parser);
7727 /* If the next token is `export', then we have a template
7728 declaration. */
7729 else if (token1.keyword == RID_EXPORT)
7730 cp_parser_template_declaration (parser, /*member_p=*/false);
7731 /* If the next token is `extern', 'static' or 'inline' and the one
7732 after that is `template', we have a GNU extended explicit
7733 instantiation directive. */
7734 else if (cp_parser_allow_gnu_extensions_p (parser)
7735 && (token1.keyword == RID_EXTERN
7736 || token1.keyword == RID_STATIC
7737 || token1.keyword == RID_INLINE)
7738 && token2.keyword == RID_TEMPLATE)
7739 cp_parser_explicit_instantiation (parser);
7740 /* If the next token is `namespace', check for a named or unnamed
7741 namespace definition. */
7742 else if (token1.keyword == RID_NAMESPACE
7743 && (/* A named namespace definition. */
7744 (token2.type == CPP_NAME
7745 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7746 != CPP_EQ))
7747 /* An unnamed namespace definition. */
7748 || token2.type == CPP_OPEN_BRACE
7749 || token2.keyword == RID_ATTRIBUTE))
7750 cp_parser_namespace_definition (parser);
7751 /* If the next token is `concept', or we have the sequence `auto
7752 concept', then we are defining a concept. */
7753 else if (token1.keyword == RID_CONCEPT
7754 || (token1.keyword == RID_AUTO && token2.keyword == RID_CONCEPT))
7755 cp_parser_concept_definition (parser);
7756 /* Objective-C++ declaration/definition. */
7757 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7758 cp_parser_objc_declaration (parser);
7759 /* We must have either a block declaration or a function
7760 definition. */
7761 else
7762 /* Try to parse a block-declaration, or a function-definition. */
7763 cp_parser_block_declaration (parser, /*statement_p=*/false);
7765 /* Free any declarators allocated. */
7766 obstack_free (&declarator_obstack, p);
7769 /* Parse a block-declaration.
7771 block-declaration:
7772 simple-declaration
7773 asm-definition
7774 namespace-alias-definition
7775 using-declaration
7776 using-directive
7778 GNU Extension:
7780 block-declaration:
7781 __extension__ block-declaration
7783 C++0x Extension:
7785 block-declaration:
7786 static_assert-declaration
7788 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7789 part of a declaration-statement. */
7791 static void
7792 cp_parser_block_declaration (cp_parser *parser,
7793 bool statement_p)
7795 cp_token *token1;
7796 int saved_pedantic;
7798 /* Check for the `__extension__' keyword. */
7799 if (cp_parser_extension_opt (parser, &saved_pedantic))
7801 /* Parse the qualified declaration. */
7802 cp_parser_block_declaration (parser, statement_p);
7803 /* Restore the PEDANTIC flag. */
7804 pedantic = saved_pedantic;
7806 return;
7809 /* Peek at the next token to figure out which kind of declaration is
7810 present. */
7811 token1 = cp_lexer_peek_token (parser->lexer);
7813 /* If the next keyword is `asm', we have an asm-definition. */
7814 if (token1->keyword == RID_ASM)
7816 if (statement_p)
7817 cp_parser_commit_to_tentative_parse (parser);
7818 cp_parser_asm_definition (parser);
7820 /* If the next keyword is `namespace', we have a
7821 namespace-alias-definition. */
7822 else if (token1->keyword == RID_NAMESPACE)
7823 cp_parser_namespace_alias_definition (parser);
7824 /* If the next keyword is `using', we have either a
7825 using-declaration or a using-directive. */
7826 else if (token1->keyword == RID_USING)
7828 cp_token *token2;
7830 if (statement_p)
7831 cp_parser_commit_to_tentative_parse (parser);
7832 /* If the token after `using' is `namespace', then we have a
7833 using-directive. */
7834 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7835 if (token2->keyword == RID_NAMESPACE)
7836 cp_parser_using_directive (parser);
7837 /* Otherwise, it's a using-declaration. */
7838 else
7839 cp_parser_using_declaration (parser,
7840 /*access_declaration_p=*/false);
7842 /* If the next keyword is `__label__' we have a misplaced label
7843 declaration. */
7844 else if (token1->keyword == RID_LABEL)
7846 cp_lexer_consume_token (parser->lexer);
7847 error ("%<__label__%> not at the beginning of a block");
7848 cp_parser_skip_to_end_of_statement (parser);
7849 /* If the next token is now a `;', consume it. */
7850 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7851 cp_lexer_consume_token (parser->lexer);
7853 /* If the next token is `static_assert' we have a static assertion. */
7854 else if (token1->keyword == RID_STATIC_ASSERT)
7855 cp_parser_static_assert (parser, /*member_p=*/false);
7856 /* Anything else must be a simple-declaration. */
7857 else
7858 cp_parser_simple_declaration (parser, !statement_p);
7861 /* Parse a simple-declaration.
7863 simple-declaration:
7864 decl-specifier-seq [opt] init-declarator-list [opt] ;
7866 init-declarator-list:
7867 init-declarator
7868 init-declarator-list , init-declarator
7870 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7871 function-definition as a simple-declaration. */
7873 static void
7874 cp_parser_simple_declaration (cp_parser* parser,
7875 bool function_definition_allowed_p)
7877 cp_decl_specifier_seq decl_specifiers;
7878 int declares_class_or_enum;
7879 bool saw_declarator;
7881 /* Defer access checks until we know what is being declared; the
7882 checks for names appearing in the decl-specifier-seq should be
7883 done as if we were in the scope of the thing being declared. */
7884 push_deferring_access_checks (dk_deferred);
7886 /* Parse the decl-specifier-seq. We have to keep track of whether
7887 or not the decl-specifier-seq declares a named class or
7888 enumeration type, since that is the only case in which the
7889 init-declarator-list is allowed to be empty.
7891 [dcl.dcl]
7893 In a simple-declaration, the optional init-declarator-list can be
7894 omitted only when declaring a class or enumeration, that is when
7895 the decl-specifier-seq contains either a class-specifier, an
7896 elaborated-type-specifier, or an enum-specifier. */
7897 cp_parser_decl_specifier_seq (parser,
7898 CP_PARSER_FLAGS_OPTIONAL,
7899 &decl_specifiers,
7900 &declares_class_or_enum);
7901 /* We no longer need to defer access checks. */
7902 stop_deferring_access_checks ();
7904 /* In a block scope, a valid declaration must always have a
7905 decl-specifier-seq. By not trying to parse declarators, we can
7906 resolve the declaration/expression ambiguity more quickly. */
7907 if (!function_definition_allowed_p
7908 && !decl_specifiers.any_specifiers_p)
7910 cp_parser_error (parser, "expected declaration");
7911 goto done;
7914 /* If the next two tokens are both identifiers, the code is
7915 erroneous. The usual cause of this situation is code like:
7917 T t;
7919 where "T" should name a type -- but does not. */
7920 if (!decl_specifiers.type
7921 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7923 /* If parsing tentatively, we should commit; we really are
7924 looking at a declaration. */
7925 cp_parser_commit_to_tentative_parse (parser);
7926 /* Give up. */
7927 goto done;
7930 /* If we have seen at least one decl-specifier, and the next token
7931 is not a parenthesis, then we must be looking at a declaration.
7932 (After "int (" we might be looking at a functional cast.) */
7933 if (decl_specifiers.any_specifiers_p
7934 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7935 cp_parser_commit_to_tentative_parse (parser);
7937 /* Keep going until we hit the `;' at the end of the simple
7938 declaration. */
7939 saw_declarator = false;
7940 while (cp_lexer_next_token_is_not (parser->lexer,
7941 CPP_SEMICOLON))
7943 cp_token *token;
7944 bool function_definition_p;
7945 tree decl;
7947 if (saw_declarator)
7949 /* If we are processing next declarator, coma is expected */
7950 token = cp_lexer_peek_token (parser->lexer);
7951 gcc_assert (token->type == CPP_COMMA);
7952 cp_lexer_consume_token (parser->lexer);
7954 else
7955 saw_declarator = true;
7957 /* Parse the init-declarator. */
7958 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7959 /*checks=*/NULL,
7960 function_definition_allowed_p,
7961 /*member_p=*/false,
7962 declares_class_or_enum,
7963 &function_definition_p);
7964 /* If an error occurred while parsing tentatively, exit quickly.
7965 (That usually happens when in the body of a function; each
7966 statement is treated as a declaration-statement until proven
7967 otherwise.) */
7968 if (cp_parser_error_occurred (parser))
7969 goto done;
7970 /* Handle function definitions specially. */
7971 if (function_definition_p)
7973 /* If the next token is a `,', then we are probably
7974 processing something like:
7976 void f() {}, *p;
7978 which is erroneous. */
7979 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7980 error ("mixing declarations and function-definitions is forbidden");
7981 /* Otherwise, we're done with the list of declarators. */
7982 else
7984 pop_deferring_access_checks ();
7985 return;
7988 /* The next token should be either a `,' or a `;'. */
7989 token = cp_lexer_peek_token (parser->lexer);
7990 /* If it's a `,', there are more declarators to come. */
7991 if (token->type == CPP_COMMA)
7992 /* will be consumed next time around */;
7993 /* If it's a `;', we are done. */
7994 else if (token->type == CPP_SEMICOLON)
7995 break;
7996 /* Anything else is an error. */
7997 else
7999 /* If we have already issued an error message we don't need
8000 to issue another one. */
8001 if (decl != error_mark_node
8002 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8003 cp_parser_error (parser, "expected %<,%> or %<;%>");
8004 /* Skip tokens until we reach the end of the statement. */
8005 cp_parser_skip_to_end_of_statement (parser);
8006 /* If the next token is now a `;', consume it. */
8007 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8008 cp_lexer_consume_token (parser->lexer);
8009 goto done;
8011 /* After the first time around, a function-definition is not
8012 allowed -- even if it was OK at first. For example:
8014 int i, f() {}
8016 is not valid. */
8017 function_definition_allowed_p = false;
8020 /* Issue an error message if no declarators are present, and the
8021 decl-specifier-seq does not itself declare a class or
8022 enumeration. */
8023 if (!saw_declarator)
8025 if (cp_parser_declares_only_class_p (parser))
8026 shadow_tag (&decl_specifiers);
8027 /* Perform any deferred access checks. */
8028 perform_deferred_access_checks ();
8031 /* Consume the `;'. */
8032 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8034 done:
8035 pop_deferring_access_checks ();
8038 /* Parse a decl-specifier-seq.
8040 decl-specifier-seq:
8041 decl-specifier-seq [opt] decl-specifier
8043 decl-specifier:
8044 storage-class-specifier
8045 type-specifier
8046 function-specifier
8047 friend
8048 typedef
8050 GNU Extension:
8052 decl-specifier:
8053 attributes
8055 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8057 The parser flags FLAGS is used to control type-specifier parsing.
8059 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8060 flags:
8062 1: one of the decl-specifiers is an elaborated-type-specifier
8063 (i.e., a type declaration)
8064 2: one of the decl-specifiers is an enum-specifier or a
8065 class-specifier (i.e., a type definition)
8069 static void
8070 cp_parser_decl_specifier_seq (cp_parser* parser,
8071 cp_parser_flags flags,
8072 cp_decl_specifier_seq *decl_specs,
8073 int* declares_class_or_enum)
8075 bool constructor_possible_p = !parser->in_declarator_p;
8077 /* Clear DECL_SPECS. */
8078 clear_decl_specs (decl_specs);
8080 /* Assume no class or enumeration type is declared. */
8081 *declares_class_or_enum = 0;
8083 /* Keep reading specifiers until there are no more to read. */
8084 while (true)
8086 bool constructor_p;
8087 bool found_decl_spec;
8088 cp_token *token;
8090 /* Peek at the next token. */
8091 token = cp_lexer_peek_token (parser->lexer);
8092 /* Handle attributes. */
8093 if (token->keyword == RID_ATTRIBUTE)
8095 /* Parse the attributes. */
8096 decl_specs->attributes
8097 = chainon (decl_specs->attributes,
8098 cp_parser_attributes_opt (parser));
8099 continue;
8101 /* Assume we will find a decl-specifier keyword. */
8102 found_decl_spec = true;
8103 /* If the next token is an appropriate keyword, we can simply
8104 add it to the list. */
8105 switch (token->keyword)
8107 /* decl-specifier:
8108 friend */
8109 case RID_FRIEND:
8110 if (!at_class_scope_p ())
8112 error ("%<friend%> used outside of class");
8113 cp_lexer_purge_token (parser->lexer);
8115 else
8117 ++decl_specs->specs[(int) ds_friend];
8118 /* Consume the token. */
8119 cp_lexer_consume_token (parser->lexer);
8121 break;
8123 /* function-specifier:
8124 inline
8125 virtual
8126 explicit */
8127 case RID_INLINE:
8128 case RID_VIRTUAL:
8129 case RID_EXPLICIT:
8130 cp_parser_function_specifier_opt (parser, decl_specs);
8131 break;
8133 /* decl-specifier:
8134 typedef */
8135 case RID_TYPEDEF:
8136 ++decl_specs->specs[(int) ds_typedef];
8137 /* Consume the token. */
8138 cp_lexer_consume_token (parser->lexer);
8139 /* A constructor declarator cannot appear in a typedef. */
8140 constructor_possible_p = false;
8141 /* The "typedef" keyword can only occur in a declaration; we
8142 may as well commit at this point. */
8143 cp_parser_commit_to_tentative_parse (parser);
8145 if (decl_specs->storage_class != sc_none)
8146 decl_specs->conflicting_specifiers_p = true;
8147 break;
8149 /* storage-class-specifier:
8150 auto
8151 register
8152 static
8153 extern
8154 mutable
8156 GNU Extension:
8157 thread */
8158 case RID_AUTO:
8159 case RID_REGISTER:
8160 case RID_STATIC:
8161 case RID_EXTERN:
8162 case RID_MUTABLE:
8163 /* Consume the token. */
8164 cp_lexer_consume_token (parser->lexer);
8165 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8166 break;
8167 case RID_THREAD:
8168 /* Consume the token. */
8169 cp_lexer_consume_token (parser->lexer);
8170 ++decl_specs->specs[(int) ds_thread];
8171 break;
8173 default:
8174 /* We did not yet find a decl-specifier yet. */
8175 found_decl_spec = false;
8176 break;
8179 /* Constructors are a special case. The `S' in `S()' is not a
8180 decl-specifier; it is the beginning of the declarator. */
8181 constructor_p
8182 = (!found_decl_spec
8183 && constructor_possible_p
8184 && (cp_parser_constructor_declarator_p
8185 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8187 /* If we don't have a DECL_SPEC yet, then we must be looking at
8188 a type-specifier. */
8189 if (!found_decl_spec && !constructor_p)
8191 int decl_spec_declares_class_or_enum;
8192 bool is_cv_qualifier;
8193 tree type_spec;
8195 type_spec
8196 = cp_parser_type_specifier (parser, flags,
8197 decl_specs,
8198 /*is_declaration=*/true,
8199 &decl_spec_declares_class_or_enum,
8200 &is_cv_qualifier);
8202 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8204 /* If this type-specifier referenced a user-defined type
8205 (a typedef, class-name, etc.), then we can't allow any
8206 more such type-specifiers henceforth.
8208 [dcl.spec]
8210 The longest sequence of decl-specifiers that could
8211 possibly be a type name is taken as the
8212 decl-specifier-seq of a declaration. The sequence shall
8213 be self-consistent as described below.
8215 [dcl.type]
8217 As a general rule, at most one type-specifier is allowed
8218 in the complete decl-specifier-seq of a declaration. The
8219 only exceptions are the following:
8221 -- const or volatile can be combined with any other
8222 type-specifier.
8224 -- signed or unsigned can be combined with char, long,
8225 short, or int.
8227 -- ..
8229 Example:
8231 typedef char* Pc;
8232 void g (const int Pc);
8234 Here, Pc is *not* part of the decl-specifier seq; it's
8235 the declarator. Therefore, once we see a type-specifier
8236 (other than a cv-qualifier), we forbid any additional
8237 user-defined types. We *do* still allow things like `int
8238 int' to be considered a decl-specifier-seq, and issue the
8239 error message later. */
8240 if (type_spec && !is_cv_qualifier)
8241 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8242 /* A constructor declarator cannot follow a type-specifier. */
8243 if (type_spec)
8245 constructor_possible_p = false;
8246 found_decl_spec = true;
8250 /* If we still do not have a DECL_SPEC, then there are no more
8251 decl-specifiers. */
8252 if (!found_decl_spec)
8253 break;
8255 decl_specs->any_specifiers_p = true;
8256 /* After we see one decl-specifier, further decl-specifiers are
8257 always optional. */
8258 flags |= CP_PARSER_FLAGS_OPTIONAL;
8261 cp_parser_check_decl_spec (decl_specs);
8263 /* Don't allow a friend specifier with a class definition. */
8264 if (decl_specs->specs[(int) ds_friend] != 0
8265 && (*declares_class_or_enum & 2))
8266 error ("class definition may not be declared a friend");
8269 /* Parse an (optional) storage-class-specifier.
8271 storage-class-specifier:
8272 auto
8273 register
8274 static
8275 extern
8276 mutable
8278 GNU Extension:
8280 storage-class-specifier:
8281 thread
8283 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8285 static tree
8286 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8288 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8290 case RID_AUTO:
8291 case RID_REGISTER:
8292 case RID_STATIC:
8293 case RID_EXTERN:
8294 case RID_MUTABLE:
8295 case RID_THREAD:
8296 /* Consume the token. */
8297 return cp_lexer_consume_token (parser->lexer)->u.value;
8299 default:
8300 return NULL_TREE;
8304 /* Parse an (optional) function-specifier.
8306 function-specifier:
8307 inline
8308 virtual
8309 explicit
8311 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8312 Updates DECL_SPECS, if it is non-NULL. */
8314 static tree
8315 cp_parser_function_specifier_opt (cp_parser* parser,
8316 cp_decl_specifier_seq *decl_specs)
8318 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8320 case RID_INLINE:
8321 if (decl_specs)
8322 ++decl_specs->specs[(int) ds_inline];
8323 break;
8325 case RID_VIRTUAL:
8326 /* 14.5.2.3 [temp.mem]
8328 A member function template shall not be virtual. */
8329 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8330 error ("templates may not be %<virtual%>");
8331 else if (decl_specs)
8332 ++decl_specs->specs[(int) ds_virtual];
8333 break;
8335 case RID_EXPLICIT:
8336 if (decl_specs)
8337 ++decl_specs->specs[(int) ds_explicit];
8338 break;
8340 default:
8341 return NULL_TREE;
8344 /* Consume the token. */
8345 return cp_lexer_consume_token (parser->lexer)->u.value;
8348 /* Parse a linkage-specification.
8350 linkage-specification:
8351 extern string-literal { declaration-seq [opt] }
8352 extern string-literal declaration */
8354 static void
8355 cp_parser_linkage_specification (cp_parser* parser)
8357 tree linkage;
8359 /* Look for the `extern' keyword. */
8360 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8362 /* Look for the string-literal. */
8363 linkage = cp_parser_string_literal (parser, false, false);
8365 /* Transform the literal into an identifier. If the literal is a
8366 wide-character string, or contains embedded NULs, then we can't
8367 handle it as the user wants. */
8368 if (strlen (TREE_STRING_POINTER (linkage))
8369 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8371 cp_parser_error (parser, "invalid linkage-specification");
8372 /* Assume C++ linkage. */
8373 linkage = lang_name_cplusplus;
8375 else
8376 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8378 /* We're now using the new linkage. */
8379 push_lang_context (linkage);
8381 /* If the next token is a `{', then we're using the first
8382 production. */
8383 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8385 /* Consume the `{' token. */
8386 cp_lexer_consume_token (parser->lexer);
8387 /* Parse the declarations. */
8388 cp_parser_declaration_seq_opt (parser);
8389 /* Look for the closing `}'. */
8390 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8392 /* Otherwise, there's just one declaration. */
8393 else
8395 bool saved_in_unbraced_linkage_specification_p;
8397 saved_in_unbraced_linkage_specification_p
8398 = parser->in_unbraced_linkage_specification_p;
8399 parser->in_unbraced_linkage_specification_p = true;
8400 cp_parser_declaration (parser);
8401 parser->in_unbraced_linkage_specification_p
8402 = saved_in_unbraced_linkage_specification_p;
8405 /* We're done with the linkage-specification. */
8406 pop_lang_context ();
8409 /* Parse a static_assert-declaration.
8411 static_assert-declaration:
8412 static_assert ( constant-expression , string-literal ) ;
8414 If MEMBER_P, this static_assert is a class member. */
8416 static void
8417 cp_parser_static_assert(cp_parser *parser, bool member_p)
8419 tree condition;
8420 tree message;
8421 cp_token *token;
8422 location_t saved_loc;
8424 /* Peek at the `static_assert' token so we can keep track of exactly
8425 where the static assertion started. */
8426 token = cp_lexer_peek_token (parser->lexer);
8427 saved_loc = token->location;
8429 /* Look for the `static_assert' keyword. */
8430 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8431 "`static_assert'"))
8432 return;
8434 /* We know we are in a static assertion; commit to any tentative
8435 parse. */
8436 if (cp_parser_parsing_tentatively (parser))
8437 cp_parser_commit_to_tentative_parse (parser);
8439 /* Parse the `(' starting the static assertion condition. */
8440 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8442 /* Parse the constant-expression. */
8443 condition =
8444 cp_parser_constant_expression (parser,
8445 /*allow_non_constant_p=*/false,
8446 /*non_constant_p=*/NULL);
8448 /* Parse the separating `,'. */
8449 cp_parser_require (parser, CPP_COMMA, "`,'");
8451 /* Parse the string-literal message. */
8452 message = cp_parser_string_literal (parser,
8453 /*translate=*/false,
8454 /*wide_ok=*/true);
8456 /* A `)' completes the static assertion. */
8457 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8458 cp_parser_skip_to_closing_parenthesis (parser,
8459 /*recovering=*/true,
8460 /*or_comma=*/false,
8461 /*consume_paren=*/true);
8463 /* A semicolon terminates the declaration. */
8464 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8466 /* Complete the static assertion, which may mean either processing
8467 the static assert now or saving it for template instantiation. */
8468 finish_static_assert (condition, message, saved_loc, member_p);
8471 /* Parse a `decltype' type. Returns the type.
8473 simple-type-specifier:
8474 decltype ( expression ) */
8476 static tree
8477 cp_parser_decltype (cp_parser *parser)
8479 tree expr;
8480 bool id_expression_or_member_access_p = false;
8481 const char *saved_message;
8482 bool saved_integral_constant_expression_p;
8483 bool saved_non_integral_constant_expression_p;
8485 /* Look for the `decltype' token. */
8486 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "`decltype'"))
8487 return error_mark_node;
8489 /* Types cannot be defined in a `decltype' expression. Save away the
8490 old message. */
8491 saved_message = parser->type_definition_forbidden_message;
8493 /* And create the new one. */
8494 parser->type_definition_forbidden_message
8495 = "types may not be defined in `decltype' expressions";
8497 /* The restrictions on constant-expressions do not apply inside
8498 decltype expressions. */
8499 saved_integral_constant_expression_p
8500 = parser->integral_constant_expression_p;
8501 saved_non_integral_constant_expression_p
8502 = parser->non_integral_constant_expression_p;
8503 parser->integral_constant_expression_p = false;
8505 /* Do not actually evaluate the expression. */
8506 ++skip_evaluation;
8508 /* Parse the opening `('. */
8509 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
8510 return error_mark_node;
8512 /* First, try parsing an id-expression. */
8513 cp_parser_parse_tentatively (parser);
8514 expr = cp_parser_id_expression (parser,
8515 /*template_keyword_p=*/false,
8516 /*check_dependency_p=*/true,
8517 /*template_p=*/NULL,
8518 /*declarator_p=*/false,
8519 /*optional_p=*/false);
8521 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8523 bool non_integral_constant_expression_p = false;
8524 tree id_expression = expr;
8525 cp_id_kind idk;
8526 const char *error_msg;
8528 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8529 /* Lookup the name we got back from the id-expression. */
8530 expr = cp_parser_lookup_name (parser, expr,
8531 none_type,
8532 /*is_template=*/false,
8533 /*is_namespace=*/false,
8534 /*check_dependency=*/true,
8535 /*ambiguous_decls=*/NULL);
8537 if (expr
8538 && expr != error_mark_node
8539 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8540 && TREE_CODE (expr) != TYPE_DECL
8541 && (TREE_CODE (expr) != BIT_NOT_EXPR
8542 || !TYPE_P (TREE_OPERAND (expr, 0)))
8543 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8545 /* Complete lookup of the id-expression. */
8546 expr = (finish_id_expression
8547 (id_expression, expr, parser->scope, &idk,
8548 /*integral_constant_expression_p=*/false,
8549 /*allow_non_integral_constant_expression_p=*/true,
8550 &non_integral_constant_expression_p,
8551 /*template_p=*/false,
8552 /*done=*/true,
8553 /*address_p=*/false,
8554 /*template_arg_p=*/false,
8555 &error_msg));
8557 if (expr == error_mark_node)
8558 /* We found an id-expression, but it was something that we
8559 should not have found. This is an error, not something
8560 we can recover from, so note that we found an
8561 id-expression and we'll recover as gracefully as
8562 possible. */
8563 id_expression_or_member_access_p = true;
8566 if (expr
8567 && expr != error_mark_node
8568 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8569 /* We have an id-expression. */
8570 id_expression_or_member_access_p = true;
8573 if (!id_expression_or_member_access_p)
8575 /* Abort the id-expression parse. */
8576 cp_parser_abort_tentative_parse (parser);
8578 /* Parsing tentatively, again. */
8579 cp_parser_parse_tentatively (parser);
8581 /* Parse a class member access. */
8582 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8583 /*cast_p=*/false,
8584 /*member_access_only_p=*/true);
8586 if (expr
8587 && expr != error_mark_node
8588 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8589 /* We have an id-expression. */
8590 id_expression_or_member_access_p = true;
8593 if (id_expression_or_member_access_p)
8594 /* We have parsed the complete id-expression or member access. */
8595 cp_parser_parse_definitely (parser);
8596 else
8598 /* Abort our attempt to parse an id-expression or member access
8599 expression. */
8600 cp_parser_abort_tentative_parse (parser);
8602 /* Parse a full expression. */
8603 expr = cp_parser_expression (parser, /*cast_p=*/false);
8606 /* Go back to evaluating expressions. */
8607 --skip_evaluation;
8609 /* Restore the old message and the integral constant expression
8610 flags. */
8611 parser->type_definition_forbidden_message = saved_message;
8612 parser->integral_constant_expression_p
8613 = saved_integral_constant_expression_p;
8614 parser->non_integral_constant_expression_p
8615 = saved_non_integral_constant_expression_p;
8617 if (expr == error_mark_node)
8619 /* Skip everything up to the closing `)'. */
8620 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8621 /*consume_paren=*/true);
8622 return error_mark_node;
8625 /* Parse to the closing `)'. */
8626 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8628 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8629 /*consume_paren=*/true);
8630 return error_mark_node;
8633 return finish_decltype_type (expr, id_expression_or_member_access_p);
8636 /* Parse a concept definition.
8638 concept-definition:
8639 auto [opt] concept identifier < template-parameter-list >
8640 refinement-clause [opt] concept-body ; [opt] */
8642 static void
8643 cp_parser_concept_definition (cp_parser *parser)
8645 bool implicit_p = false;
8646 tree concept_name = NULL_TREE;
8647 tree concept_parms = NULL_TREE;
8648 cp_token *token;
8649 tree the_concept = NULL_TREE;
8650 unsigned saved_num_template_parameter_lists;
8652 /* Look for the (optional) `auto' keyword. */
8653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
8655 cp_lexer_consume_token (parser->lexer);
8657 /* This is an implicit concept. */
8658 implicit_p = true;
8661 /* Look for the `concept' keyword. */
8662 if (!cp_parser_require_keyword (parser, RID_CONCEPT, "`concept'"))
8663 return;
8665 /* Concepts shall only be defined at namespace scope. */
8666 if (!at_namespace_scope_p ())
8667 error ("concepts can only be defined in a namespace or global scope");
8669 /* Get the name of the concept. */
8670 concept_name = cp_parser_identifier (parser);
8672 /* Get the `<' that starts the template parameter list. */
8673 cp_parser_require (parser, CPP_LESS, "`<'");
8675 /* Get the template parameter list. */
8676 concept_parms = cp_parser_template_parameter_list (parser);
8678 /* Look for the `>'. */
8679 cp_parser_skip_to_end_of_template_parameter_list (parser);
8681 /* We just parsed a template parameter list. */
8682 ++parser->num_template_parameter_lists;
8684 /* Declare and start defining this concept. */
8685 the_concept = begin_concept_definition (concept_name, implicit_p);
8687 /* Peek at the next token. */
8688 token = cp_lexer_peek_token (parser->lexer);
8690 /* If it's a `:', parse refinements. */
8691 if (token->type == CPP_COLON)
8693 /* Parse the refinement clause. */
8694 tree refinements = cp_parser_refinement_clause (parser);
8696 /* Attach the refinements to the concept. */
8697 if (the_concept != error_mark_node && refinements != error_mark_node)
8698 xref_refinements (the_concept, refinements);
8700 /* Peek at the next token. */
8701 token = cp_lexer_peek_token (parser->lexer);
8704 /* Remember that we are defining one more class (actually a
8705 concept). */
8706 ++parser->num_classes_being_defined;
8708 /* Inside the concept, surrounding template-parameter-lists do not
8709 apply. */
8710 saved_num_template_parameter_lists
8711 = parser->num_template_parameter_lists;
8712 parser->num_template_parameter_lists = 0;
8714 /* Find the '{' that starts the concept definition. */
8715 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8717 /* Skip over any junk before the `{'. */
8718 while (token->type != CPP_OPEN_BRACE
8719 && token->type != CPP_CLOSE_BRACE
8720 && token->type != CPP_EOF)
8722 cp_lexer_consume_token (parser->lexer);
8723 token = cp_lexer_peek_token (parser->lexer);
8726 if (token->type == CPP_OPEN_BRACE)
8727 /* Consume the starting `{'. */
8728 cp_lexer_consume_token (parser->lexer);
8729 else
8730 the_concept = error_mark_node;
8733 if (the_concept == error_mark_node)
8734 /* If the concept is already broken, skip the entire body. */
8735 cp_parser_skip_to_closing_brace (parser);
8736 else
8737 /* Parse the concept body. */
8738 cp_parser_concept_body (parser);
8740 /* Get the '}' that finishes the concept body. */
8741 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8743 /* If the next token is an `;', consume it. */
8744 token = cp_lexer_peek_token (parser->lexer);
8745 if (token->type == CPP_SEMICOLON)
8746 cp_lexer_consume_token (parser->lexer);
8748 /* Finish the concept definition. */
8749 if (the_concept != error_mark_node)
8750 the_concept = finish_concept_definition (the_concept);
8752 /* Restore number of template parameter lists. */
8753 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8755 /* We are no longer inside a class or concept. */
8756 --parser->num_classes_being_defined;
8758 /* We are done with this template parameter list. */
8759 --parser->num_template_parameter_lists;
8761 /* Finish up the template declaration. */
8762 finish_template_decl (concept_parms);
8765 /* Special member functions [gram.special] */
8767 /* Parse a conversion-function-id.
8769 conversion-function-id:
8770 operator conversion-type-id
8772 Returns an IDENTIFIER_NODE representing the operator. */
8774 static tree
8775 cp_parser_conversion_function_id (cp_parser* parser)
8777 tree type;
8778 tree saved_scope;
8779 tree saved_qualifying_scope;
8780 tree saved_object_scope;
8781 tree pushed_scope = NULL_TREE;
8783 /* Look for the `operator' token. */
8784 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8785 return error_mark_node;
8786 /* When we parse the conversion-type-id, the current scope will be
8787 reset. However, we need that information in able to look up the
8788 conversion function later, so we save it here. */
8789 saved_scope = parser->scope;
8790 saved_qualifying_scope = parser->qualifying_scope;
8791 saved_object_scope = parser->object_scope;
8792 /* We must enter the scope of the class so that the names of
8793 entities declared within the class are available in the
8794 conversion-type-id. For example, consider:
8796 struct S {
8797 typedef int I;
8798 operator I();
8801 S::operator I() { ... }
8803 In order to see that `I' is a type-name in the definition, we
8804 must be in the scope of `S'. */
8805 if (saved_scope)
8806 pushed_scope = push_scope (saved_scope);
8807 /* Parse the conversion-type-id. */
8808 type = cp_parser_conversion_type_id (parser);
8809 /* Leave the scope of the class, if any. */
8810 if (pushed_scope)
8811 pop_scope (pushed_scope);
8812 /* Restore the saved scope. */
8813 parser->scope = saved_scope;
8814 parser->qualifying_scope = saved_qualifying_scope;
8815 parser->object_scope = saved_object_scope;
8816 /* If the TYPE is invalid, indicate failure. */
8817 if (type == error_mark_node)
8818 return error_mark_node;
8819 return mangle_conv_op_name_for_type (type);
8822 /* Parse a conversion-type-id:
8824 conversion-type-id:
8825 type-specifier-seq conversion-declarator [opt]
8827 Returns the TYPE specified. */
8829 static tree
8830 cp_parser_conversion_type_id (cp_parser* parser)
8832 tree attributes;
8833 cp_decl_specifier_seq type_specifiers;
8834 cp_declarator *declarator;
8835 tree type_specified;
8837 /* Parse the attributes. */
8838 attributes = cp_parser_attributes_opt (parser);
8839 /* Parse the type-specifiers. */
8840 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8841 &type_specifiers);
8842 /* If that didn't work, stop. */
8843 if (type_specifiers.type == error_mark_node)
8844 return error_mark_node;
8845 /* Parse the conversion-declarator. */
8846 declarator = cp_parser_conversion_declarator_opt (parser);
8848 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8849 /*initialized=*/0, &attributes);
8850 if (attributes)
8851 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8852 return type_specified;
8855 /* Parse an (optional) conversion-declarator.
8857 conversion-declarator:
8858 ptr-operator conversion-declarator [opt]
8862 static cp_declarator *
8863 cp_parser_conversion_declarator_opt (cp_parser* parser)
8865 enum tree_code code;
8866 tree class_type;
8867 cp_cv_quals cv_quals;
8869 /* We don't know if there's a ptr-operator next, or not. */
8870 cp_parser_parse_tentatively (parser);
8871 /* Try the ptr-operator. */
8872 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8873 /* If it worked, look for more conversion-declarators. */
8874 if (cp_parser_parse_definitely (parser))
8876 cp_declarator *declarator;
8878 /* Parse another optional declarator. */
8879 declarator = cp_parser_conversion_declarator_opt (parser);
8881 return cp_parser_make_indirect_declarator
8882 (code, class_type, cv_quals, declarator);
8885 return NULL;
8888 /* Parse an (optional) ctor-initializer.
8890 ctor-initializer:
8891 : mem-initializer-list
8893 Returns TRUE iff the ctor-initializer was actually present. */
8895 static bool
8896 cp_parser_ctor_initializer_opt (cp_parser* parser)
8898 /* If the next token is not a `:', then there is no
8899 ctor-initializer. */
8900 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8902 /* Do default initialization of any bases and members. */
8903 if (DECL_CONSTRUCTOR_P (current_function_decl))
8904 finish_mem_initializers (NULL_TREE);
8906 return false;
8909 /* Consume the `:' token. */
8910 cp_lexer_consume_token (parser->lexer);
8911 /* And the mem-initializer-list. */
8912 cp_parser_mem_initializer_list (parser);
8914 return true;
8917 /* Parse a mem-initializer-list.
8919 mem-initializer-list:
8920 mem-initializer ... [opt]
8921 mem-initializer ... [opt] , mem-initializer-list */
8923 static void
8924 cp_parser_mem_initializer_list (cp_parser* parser)
8926 tree mem_initializer_list = NULL_TREE;
8928 /* Let the semantic analysis code know that we are starting the
8929 mem-initializer-list. */
8930 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8931 error ("only constructors take base initializers");
8933 /* Loop through the list. */
8934 while (true)
8936 tree mem_initializer;
8938 /* Parse the mem-initializer. */
8939 mem_initializer = cp_parser_mem_initializer (parser);
8940 /* If the next token is a `...', we're expanding member initializers. */
8941 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8943 /* Consume the `...'. */
8944 cp_lexer_consume_token (parser->lexer);
8946 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8947 can be expanded but members cannot. */
8948 if (mem_initializer != error_mark_node
8949 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8951 error ("cannot expand initializer for member %<%D%>",
8952 TREE_PURPOSE (mem_initializer));
8953 mem_initializer = error_mark_node;
8956 /* Construct the pack expansion type. */
8957 if (mem_initializer != error_mark_node)
8958 mem_initializer = make_pack_expansion (mem_initializer);
8960 /* Add it to the list, unless it was erroneous. */
8961 if (mem_initializer != error_mark_node)
8963 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8964 mem_initializer_list = mem_initializer;
8966 /* If the next token is not a `,', we're done. */
8967 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8968 break;
8969 /* Consume the `,' token. */
8970 cp_lexer_consume_token (parser->lexer);
8973 /* Perform semantic analysis. */
8974 if (DECL_CONSTRUCTOR_P (current_function_decl))
8975 finish_mem_initializers (mem_initializer_list);
8978 /* Parse a mem-initializer.
8980 mem-initializer:
8981 mem-initializer-id ( expression-list [opt] )
8983 GNU extension:
8985 mem-initializer:
8986 ( expression-list [opt] )
8988 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8989 class) or FIELD_DECL (for a non-static data member) to initialize;
8990 the TREE_VALUE is the expression-list. An empty initialization
8991 list is represented by void_list_node. */
8993 static tree
8994 cp_parser_mem_initializer (cp_parser* parser)
8996 tree mem_initializer_id;
8997 tree expression_list;
8998 tree member;
9000 /* Find out what is being initialized. */
9001 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9003 pedwarn ("anachronistic old-style base class initializer");
9004 mem_initializer_id = NULL_TREE;
9006 else
9007 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9008 member = expand_member_init (mem_initializer_id);
9009 if (member && !DECL_P (member))
9010 in_base_initializer = 1;
9012 expression_list
9013 = cp_parser_parenthesized_expression_list (parser, false,
9014 /*cast_p=*/false,
9015 /*allow_expansion_p=*/true,
9016 /*non_constant_p=*/NULL);
9017 if (expression_list == error_mark_node)
9018 return error_mark_node;
9019 if (!expression_list)
9020 expression_list = void_type_node;
9022 in_base_initializer = 0;
9024 return member ? build_tree_list (member, expression_list) : error_mark_node;
9027 /* Parse a mem-initializer-id.
9029 mem-initializer-id:
9030 :: [opt] nested-name-specifier [opt] class-name
9031 identifier
9033 Returns a TYPE indicating the class to be initializer for the first
9034 production. Returns an IDENTIFIER_NODE indicating the data member
9035 to be initialized for the second production. */
9037 static tree
9038 cp_parser_mem_initializer_id (cp_parser* parser)
9040 bool global_scope_p;
9041 bool nested_name_specifier_p;
9042 bool template_p = false;
9043 tree id;
9045 /* `typename' is not allowed in this context ([temp.res]). */
9046 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9048 error ("keyword %<typename%> not allowed in this context (a qualified "
9049 "member initializer is implicitly a type)");
9050 cp_lexer_consume_token (parser->lexer);
9052 /* Look for the optional `::' operator. */
9053 global_scope_p
9054 = (cp_parser_global_scope_opt (parser,
9055 /*current_scope_valid_p=*/false)
9056 != NULL_TREE);
9057 /* Look for the optional nested-name-specifier. The simplest way to
9058 implement:
9060 [temp.res]
9062 The keyword `typename' is not permitted in a base-specifier or
9063 mem-initializer; in these contexts a qualified name that
9064 depends on a template-parameter is implicitly assumed to be a
9065 type name.
9067 is to assume that we have seen the `typename' keyword at this
9068 point. */
9069 nested_name_specifier_p
9070 = (cp_parser_nested_name_specifier_opt (parser,
9071 /*typename_keyword_p=*/true,
9072 /*check_dependency_p=*/true,
9073 /*type_p=*/true,
9074 /*is_declaration=*/true)
9075 != NULL_TREE);
9076 if (nested_name_specifier_p)
9077 template_p = cp_parser_optional_template_keyword (parser);
9078 /* If there is a `::' operator or a nested-name-specifier, then we
9079 are definitely looking for a class-name. */
9080 if (global_scope_p || nested_name_specifier_p)
9081 return cp_parser_class_name (parser,
9082 /*typename_keyword_p=*/true,
9083 /*template_keyword_p=*/template_p,
9084 none_type,
9085 /*check_dependency_p=*/true,
9086 /*class_head_p=*/false,
9087 /*is_declaration=*/true);
9088 /* Otherwise, we could also be looking for an ordinary identifier. */
9089 cp_parser_parse_tentatively (parser);
9090 /* Try a class-name. */
9091 id = cp_parser_class_name (parser,
9092 /*typename_keyword_p=*/true,
9093 /*template_keyword_p=*/false,
9094 none_type,
9095 /*check_dependency_p=*/true,
9096 /*class_head_p=*/false,
9097 /*is_declaration=*/true);
9098 /* If we found one, we're done. */
9099 if (cp_parser_parse_definitely (parser))
9100 return id;
9101 /* Otherwise, look for an ordinary identifier. */
9102 return cp_parser_identifier (parser);
9105 /* Overloading [gram.over] */
9107 /* Parse an operator-function-id.
9109 operator-function-id:
9110 operator operator
9112 Returns an IDENTIFIER_NODE for the operator which is a
9113 human-readable spelling of the identifier, e.g., `operator +'. */
9115 static tree
9116 cp_parser_operator_function_id (cp_parser* parser)
9118 /* Look for the `operator' keyword. */
9119 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
9120 return error_mark_node;
9121 /* And then the name of the operator itself. */
9122 return cp_parser_operator (parser);
9125 /* Parse an operator.
9127 operator:
9128 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9129 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9130 || ++ -- , ->* -> () []
9132 GNU Extensions:
9134 operator:
9135 <? >? <?= >?=
9137 Returns an IDENTIFIER_NODE for the operator which is a
9138 human-readable spelling of the identifier, e.g., `operator +'. */
9140 static tree
9141 cp_parser_operator (cp_parser* parser)
9143 tree id = NULL_TREE;
9144 cp_token *token;
9146 /* Peek at the next token. */
9147 token = cp_lexer_peek_token (parser->lexer);
9148 /* Figure out which operator we have. */
9149 switch (token->type)
9151 case CPP_KEYWORD:
9153 enum tree_code op;
9155 /* The keyword should be either `new' or `delete'. */
9156 if (token->keyword == RID_NEW)
9157 op = NEW_EXPR;
9158 else if (token->keyword == RID_DELETE)
9159 op = DELETE_EXPR;
9160 else
9161 break;
9163 /* Consume the `new' or `delete' token. */
9164 cp_lexer_consume_token (parser->lexer);
9166 /* Peek at the next token. */
9167 token = cp_lexer_peek_token (parser->lexer);
9168 /* If it's a `[' token then this is the array variant of the
9169 operator. */
9170 if (token->type == CPP_OPEN_SQUARE)
9172 /* Consume the `[' token. */
9173 cp_lexer_consume_token (parser->lexer);
9174 /* Look for the `]' token. */
9175 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9176 id = ansi_opname (op == NEW_EXPR
9177 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9179 /* Otherwise, we have the non-array variant. */
9180 else
9181 id = ansi_opname (op);
9183 return id;
9186 case CPP_PLUS:
9187 id = ansi_opname (PLUS_EXPR);
9188 break;
9190 case CPP_MINUS:
9191 id = ansi_opname (MINUS_EXPR);
9192 break;
9194 case CPP_MULT:
9195 id = ansi_opname (MULT_EXPR);
9196 break;
9198 case CPP_DIV:
9199 id = ansi_opname (TRUNC_DIV_EXPR);
9200 break;
9202 case CPP_MOD:
9203 id = ansi_opname (TRUNC_MOD_EXPR);
9204 break;
9206 case CPP_XOR:
9207 id = ansi_opname (BIT_XOR_EXPR);
9208 break;
9210 case CPP_AND:
9211 id = ansi_opname (BIT_AND_EXPR);
9212 break;
9214 case CPP_OR:
9215 id = ansi_opname (BIT_IOR_EXPR);
9216 break;
9218 case CPP_COMPL:
9219 id = ansi_opname (BIT_NOT_EXPR);
9220 break;
9222 case CPP_NOT:
9223 id = ansi_opname (TRUTH_NOT_EXPR);
9224 break;
9226 case CPP_EQ:
9227 id = ansi_assopname (NOP_EXPR);
9228 break;
9230 case CPP_LESS:
9231 id = ansi_opname (LT_EXPR);
9232 break;
9234 case CPP_GREATER:
9235 id = ansi_opname (GT_EXPR);
9236 break;
9238 case CPP_PLUS_EQ:
9239 id = ansi_assopname (PLUS_EXPR);
9240 break;
9242 case CPP_MINUS_EQ:
9243 id = ansi_assopname (MINUS_EXPR);
9244 break;
9246 case CPP_MULT_EQ:
9247 id = ansi_assopname (MULT_EXPR);
9248 break;
9250 case CPP_DIV_EQ:
9251 id = ansi_assopname (TRUNC_DIV_EXPR);
9252 break;
9254 case CPP_MOD_EQ:
9255 id = ansi_assopname (TRUNC_MOD_EXPR);
9256 break;
9258 case CPP_XOR_EQ:
9259 id = ansi_assopname (BIT_XOR_EXPR);
9260 break;
9262 case CPP_AND_EQ:
9263 id = ansi_assopname (BIT_AND_EXPR);
9264 break;
9266 case CPP_OR_EQ:
9267 id = ansi_assopname (BIT_IOR_EXPR);
9268 break;
9270 case CPP_LSHIFT:
9271 id = ansi_opname (LSHIFT_EXPR);
9272 break;
9274 case CPP_RSHIFT:
9275 id = ansi_opname (RSHIFT_EXPR);
9276 break;
9278 case CPP_LSHIFT_EQ:
9279 id = ansi_assopname (LSHIFT_EXPR);
9280 break;
9282 case CPP_RSHIFT_EQ:
9283 id = ansi_assopname (RSHIFT_EXPR);
9284 break;
9286 case CPP_EQ_EQ:
9287 id = ansi_opname (EQ_EXPR);
9288 break;
9290 case CPP_NOT_EQ:
9291 id = ansi_opname (NE_EXPR);
9292 break;
9294 case CPP_LESS_EQ:
9295 id = ansi_opname (LE_EXPR);
9296 break;
9298 case CPP_GREATER_EQ:
9299 id = ansi_opname (GE_EXPR);
9300 break;
9302 case CPP_AND_AND:
9303 id = ansi_opname (TRUTH_ANDIF_EXPR);
9304 break;
9306 case CPP_OR_OR:
9307 id = ansi_opname (TRUTH_ORIF_EXPR);
9308 break;
9310 case CPP_PLUS_PLUS:
9311 id = ansi_opname (POSTINCREMENT_EXPR);
9312 break;
9314 case CPP_MINUS_MINUS:
9315 id = ansi_opname (PREDECREMENT_EXPR);
9316 break;
9318 case CPP_COMMA:
9319 id = ansi_opname (COMPOUND_EXPR);
9320 break;
9322 case CPP_DEREF_STAR:
9323 id = ansi_opname (MEMBER_REF);
9324 break;
9326 case CPP_DEREF:
9327 id = ansi_opname (COMPONENT_REF);
9328 break;
9330 case CPP_OPEN_PAREN:
9331 /* Consume the `('. */
9332 cp_lexer_consume_token (parser->lexer);
9333 /* Look for the matching `)'. */
9334 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9335 return ansi_opname (CALL_EXPR);
9337 case CPP_OPEN_SQUARE:
9338 /* Consume the `['. */
9339 cp_lexer_consume_token (parser->lexer);
9340 /* Look for the matching `]'. */
9341 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9342 return ansi_opname (ARRAY_REF);
9344 default:
9345 /* Anything else is an error. */
9346 break;
9349 /* If we have selected an identifier, we need to consume the
9350 operator token. */
9351 if (id)
9352 cp_lexer_consume_token (parser->lexer);
9353 /* Otherwise, no valid operator name was present. */
9354 else
9356 cp_parser_error (parser, "expected operator");
9357 id = error_mark_node;
9360 return id;
9363 /* Parse a template-declaration.
9365 template-declaration:
9366 export [opt] template < template-parameter-list > declaration
9368 If MEMBER_P is TRUE, this template-declaration occurs within a
9369 class-specifier.
9371 The grammar rule given by the standard isn't correct. What
9372 is really meant is:
9374 template-declaration:
9375 export [opt] template-parameter-list-seq
9376 decl-specifier-seq [opt] init-declarator [opt] ;
9377 export [opt] template-parameter-list-seq
9378 function-definition
9380 template-parameter-list-seq:
9381 template-parameter-list-seq [opt]
9382 template < template-parameter-list > */
9384 static void
9385 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9387 /* Check for `export'. */
9388 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9390 /* Consume the `export' token. */
9391 cp_lexer_consume_token (parser->lexer);
9392 /* Warn that we do not support `export'. */
9393 warning (0, "keyword %<export%> not implemented, and will be ignored");
9396 cp_parser_template_declaration_after_export (parser, member_p);
9399 /* Parse a template-parameter-list.
9401 template-parameter-list:
9402 template-parameter
9403 template-parameter-list , template-parameter
9405 Returns a TREE_LIST. Each node represents a template parameter.
9406 The nodes are connected via their TREE_CHAINs. */
9408 static tree
9409 cp_parser_template_parameter_list (cp_parser* parser)
9411 tree parameter_list = NULL_TREE;
9413 begin_template_parm_list ();
9414 while (true)
9416 tree parameter;
9417 cp_token *token;
9418 bool is_non_type;
9419 bool is_parameter_pack;
9421 /* Parse the template-parameter. */
9422 parameter = cp_parser_template_parameter (parser,
9423 &is_non_type,
9424 &is_parameter_pack);
9425 /* Add it to the list. */
9426 if (parameter != error_mark_node)
9427 parameter_list = process_template_parm (parameter_list,
9428 parameter,
9429 is_non_type,
9430 is_parameter_pack);
9431 else
9433 tree err_parm = build_tree_list (parameter, parameter);
9434 TREE_VALUE (err_parm) = error_mark_node;
9435 parameter_list = chainon (parameter_list, err_parm);
9438 /* Peek at the next token. */
9439 token = cp_lexer_peek_token (parser->lexer);
9440 /* If it's not a `,', we're done. */
9441 if (token->type != CPP_COMMA)
9442 break;
9443 /* Otherwise, consume the `,' token. */
9444 cp_lexer_consume_token (parser->lexer);
9447 return end_template_parm_list (parameter_list);
9450 /* Parse a template-parameter.
9452 template-parameter:
9453 type-parameter
9454 parameter-declaration
9456 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9457 the parameter. The TREE_PURPOSE is the default value, if any.
9458 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9459 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9460 set to true iff this parameter is a parameter pack. */
9462 static tree
9463 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9464 bool *is_parameter_pack)
9466 cp_token *token;
9467 cp_parameter_declarator *parameter_declarator;
9468 cp_declarator *id_declarator;
9469 tree parm;
9471 /* Assume it is a type parameter or a template parameter. */
9472 *is_non_type = false;
9473 /* Assume it not a parameter pack. */
9474 *is_parameter_pack = false;
9475 /* Peek at the next token. */
9476 token = cp_lexer_peek_token (parser->lexer);
9477 /* If it is `class' or `template', we have a type-parameter. */
9478 if (token->keyword == RID_TEMPLATE)
9479 return cp_parser_type_parameter (parser, is_parameter_pack);
9480 /* If it is `class' or `typename' we do not know yet whether it is a
9481 type parameter or a non-type parameter. Consider:
9483 template <typename T, typename T::X X> ...
9487 template <class C, class D*> ...
9489 Here, the first parameter is a type parameter, and the second is
9490 a non-type parameter. We can tell by looking at the token after
9491 the identifier -- if it is a `,', `=', or `>' then we have a type
9492 parameter. */
9493 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9495 /* Peek at the token after `class' or `typename'. */
9496 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9497 /* If it's an ellipsis, we have a template type parameter
9498 pack. */
9499 if (token->type == CPP_ELLIPSIS)
9500 return cp_parser_type_parameter (parser, is_parameter_pack);
9501 /* If it's an identifier, skip it. */
9502 if (token->type == CPP_NAME)
9503 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9504 /* Now, see if the token looks like the end of a template
9505 parameter. */
9506 if (token->type == CPP_COMMA
9507 || token->type == CPP_EQ
9508 || token->type == CPP_GREATER)
9509 return cp_parser_type_parameter (parser, is_parameter_pack);
9512 /* Otherwise, it is a non-type parameter.
9514 [temp.param]
9516 When parsing a default template-argument for a non-type
9517 template-parameter, the first non-nested `>' is taken as the end
9518 of the template parameter-list rather than a greater-than
9519 operator. */
9520 *is_non_type = true;
9521 parameter_declarator
9522 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9523 /*parenthesized_p=*/NULL);
9525 /* If the parameter declaration is marked as a parameter pack, set
9526 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9527 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9528 grokdeclarator. */
9529 if (parameter_declarator
9530 && parameter_declarator->declarator
9531 && parameter_declarator->declarator->parameter_pack_p)
9533 *is_parameter_pack = true;
9534 parameter_declarator->declarator->parameter_pack_p = false;
9537 /* If the next token is an ellipsis, and we don't already have it
9538 marked as a parameter pack, then we have a parameter pack (that
9539 has no declarator). */
9540 if (!*is_parameter_pack
9541 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9542 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9544 /* Consume the `...'. */
9545 cp_lexer_consume_token (parser->lexer);
9546 maybe_warn_variadic_templates ();
9548 *is_parameter_pack = true;
9550 /* Parameter packs cannot have default arguments. However, a
9551 user may try to do so, so we'll parse them and give an
9552 appropriate diagnostic here. */
9553 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9555 /* Consume the `='. */
9556 cp_lexer_consume_token (parser->lexer);
9558 /* Find the name of the parameter pack. */
9559 id_declarator = parameter_declarator->declarator;
9560 while (id_declarator && id_declarator->kind != cdk_id)
9561 id_declarator = id_declarator->declarator;
9563 if (id_declarator && id_declarator->kind == cdk_id)
9564 error ("template parameter pack %qD cannot have a default argument",
9565 id_declarator->u.id.unqualified_name);
9566 else
9567 error ("template parameter pack cannot have a default argument");
9569 /* Parse the default argument, but throw away the result. */
9570 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9574 parm = grokdeclarator (parameter_declarator->declarator,
9575 &parameter_declarator->decl_specifiers,
9576 PARM, /*initialized=*/0,
9577 /*attrlist=*/NULL);
9578 if (parm == error_mark_node)
9579 return error_mark_node;
9581 return build_tree_list (parameter_declarator->default_argument, parm);
9584 /* Parse a type-parameter.
9586 type-parameter:
9587 class identifier [opt]
9588 class identifier [opt] = type-id
9589 typename identifier [opt]
9590 typename identifier [opt] = type-id
9591 template < template-parameter-list > class identifier [opt]
9592 template < template-parameter-list > class identifier [opt]
9593 = id-expression
9595 GNU Extension (variadic templates):
9597 type-parameter:
9598 class ... identifier [opt]
9599 typename ... identifier [opt]
9601 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9602 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9603 the declaration of the parameter.
9605 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9607 static tree
9608 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9610 cp_token *token;
9611 tree parameter;
9613 /* Look for a keyword to tell us what kind of parameter this is. */
9614 token = cp_parser_require (parser, CPP_KEYWORD,
9615 "`class', `typename', or `template'");
9616 if (!token)
9617 return error_mark_node;
9619 switch (token->keyword)
9621 case RID_CLASS:
9622 case RID_TYPENAME:
9624 tree identifier;
9625 tree default_argument;
9627 /* If the next token is an ellipsis, we have a template
9628 argument pack. */
9629 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9631 /* Consume the `...' token. */
9632 cp_lexer_consume_token (parser->lexer);
9633 maybe_warn_variadic_templates ();
9635 *is_parameter_pack = true;
9638 /* If the next token is an identifier, then it names the
9639 parameter. */
9640 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9641 identifier = cp_parser_identifier (parser);
9642 else
9643 identifier = NULL_TREE;
9645 /* Create the parameter. */
9646 parameter = finish_template_type_parm (class_type_node, identifier);
9648 /* If the next token is an `=', we have a default argument. */
9649 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9651 /* Consume the `=' token. */
9652 cp_lexer_consume_token (parser->lexer);
9653 /* Parse the default-argument. */
9654 push_deferring_access_checks (dk_no_deferred);
9655 default_argument = cp_parser_type_id (parser);
9657 /* Template parameter packs cannot have default
9658 arguments. */
9659 if (*is_parameter_pack)
9661 if (identifier)
9662 error ("template parameter pack %qD cannot have a default argument",
9663 identifier);
9664 else
9665 error ("template parameter packs cannot have default arguments");
9666 default_argument = NULL_TREE;
9668 pop_deferring_access_checks ();
9670 else
9671 default_argument = NULL_TREE;
9673 /* Create the combined representation of the parameter and the
9674 default argument. */
9675 parameter = build_tree_list (default_argument, parameter);
9677 break;
9679 case RID_TEMPLATE:
9681 tree parameter_list;
9682 tree identifier;
9683 tree default_argument;
9685 /* Look for the `<'. */
9686 cp_parser_require (parser, CPP_LESS, "`<'");
9687 /* Parse the template-parameter-list. */
9688 parameter_list = cp_parser_template_parameter_list (parser);
9689 /* Look for the `>'. */
9690 cp_parser_require (parser, CPP_GREATER, "`>'");
9691 /* Look for the `class' keyword. */
9692 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9693 /* If the next token is an ellipsis, we have a template
9694 argument pack. */
9695 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9697 /* Consume the `...' token. */
9698 cp_lexer_consume_token (parser->lexer);
9699 maybe_warn_variadic_templates ();
9701 *is_parameter_pack = true;
9703 /* If the next token is an `=', then there is a
9704 default-argument. If the next token is a `>', we are at
9705 the end of the parameter-list. If the next token is a `,',
9706 then we are at the end of this parameter. */
9707 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9708 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9709 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9711 identifier = cp_parser_identifier (parser);
9712 /* Treat invalid names as if the parameter were nameless. */
9713 if (identifier == error_mark_node)
9714 identifier = NULL_TREE;
9716 else
9717 identifier = NULL_TREE;
9719 /* Create the template parameter. */
9720 parameter = finish_template_template_parm (class_type_node,
9721 identifier);
9723 /* If the next token is an `=', then there is a
9724 default-argument. */
9725 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9727 bool is_template;
9729 /* Consume the `='. */
9730 cp_lexer_consume_token (parser->lexer);
9731 /* Parse the id-expression. */
9732 push_deferring_access_checks (dk_no_deferred);
9733 default_argument
9734 = cp_parser_id_expression (parser,
9735 /*template_keyword_p=*/false,
9736 /*check_dependency_p=*/true,
9737 /*template_p=*/&is_template,
9738 /*declarator_p=*/false,
9739 /*optional_p=*/false);
9740 if (TREE_CODE (default_argument) == TYPE_DECL)
9741 /* If the id-expression was a template-id that refers to
9742 a template-class, we already have the declaration here,
9743 so no further lookup is needed. */
9745 else
9746 /* Look up the name. */
9747 default_argument
9748 = cp_parser_lookup_name (parser, default_argument,
9749 none_type,
9750 /*is_template=*/is_template,
9751 /*is_namespace=*/false,
9752 /*check_dependency=*/true,
9753 /*ambiguous_decls=*/NULL);
9754 /* See if the default argument is valid. */
9755 default_argument
9756 = check_template_template_default_arg (default_argument);
9758 /* Template parameter packs cannot have default
9759 arguments. */
9760 if (*is_parameter_pack)
9762 if (identifier)
9763 error ("template parameter pack %qD cannot have a default argument",
9764 identifier);
9765 else
9766 error ("template parameter packs cannot have default arguments");
9767 default_argument = NULL_TREE;
9769 pop_deferring_access_checks ();
9771 else
9772 default_argument = NULL_TREE;
9774 /* Create the combined representation of the parameter and the
9775 default argument. */
9776 parameter = build_tree_list (default_argument, parameter);
9778 break;
9780 default:
9781 gcc_unreachable ();
9782 break;
9785 return parameter;
9788 /* Parse a template-id.
9790 template-id:
9791 template-name < template-argument-list [opt] >
9793 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9794 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9795 returned. Otherwise, if the template-name names a function, or set
9796 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9797 names a class, returns a TYPE_DECL for the specialization.
9799 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9800 uninstantiated templates. */
9802 static tree
9803 cp_parser_template_id (cp_parser *parser,
9804 bool template_keyword_p,
9805 bool check_dependency_p,
9806 bool is_declaration)
9808 int i;
9809 tree template;
9810 tree arguments;
9811 tree template_id;
9812 cp_token_position start_of_id = 0;
9813 deferred_access_check *chk;
9814 VEC (deferred_access_check,gc) *access_check;
9815 cp_token *next_token, *next_token_2;
9816 bool is_identifier;
9818 /* If the next token corresponds to a template-id, there is no need
9819 to reparse it. */
9820 next_token = cp_lexer_peek_token (parser->lexer);
9821 if (next_token->type == CPP_TEMPLATE_ID)
9823 struct tree_check *check_value;
9825 /* Get the stored value. */
9826 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9827 /* Perform any access checks that were deferred. */
9828 access_check = check_value->checks;
9829 if (access_check)
9831 for (i = 0 ;
9832 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9833 ++i)
9835 perform_or_defer_access_check (chk->binfo,
9836 chk->decl,
9837 chk->diag_decl);
9840 /* Return the stored value. */
9841 return check_value->value;
9844 /* Avoid performing name lookup if there is no possibility of
9845 finding a template-id. */
9846 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9847 || (next_token->type == CPP_NAME
9848 && !cp_parser_nth_token_starts_template_argument_list_p
9849 (parser, 2)))
9851 cp_parser_error (parser, "expected template-id");
9852 return error_mark_node;
9855 /* Remember where the template-id starts. */
9856 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9857 start_of_id = cp_lexer_token_position (parser->lexer, false);
9859 push_deferring_access_checks (dk_deferred);
9861 /* Parse the template-name. */
9862 is_identifier = false;
9863 template = cp_parser_template_name (parser, template_keyword_p,
9864 check_dependency_p,
9865 is_declaration,
9866 &is_identifier);
9867 if (template == error_mark_node || is_identifier)
9869 pop_deferring_access_checks ();
9870 return template;
9873 /* If we find the sequence `[:' after a template-name, it's probably
9874 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9875 parse correctly the argument list. */
9876 next_token = cp_lexer_peek_token (parser->lexer);
9877 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9878 if (next_token->type == CPP_OPEN_SQUARE
9879 && next_token->flags & DIGRAPH
9880 && next_token_2->type == CPP_COLON
9881 && !(next_token_2->flags & PREV_WHITE))
9883 cp_parser_parse_tentatively (parser);
9884 /* Change `:' into `::'. */
9885 next_token_2->type = CPP_SCOPE;
9886 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9887 CPP_LESS. */
9888 cp_lexer_consume_token (parser->lexer);
9889 /* Parse the arguments. */
9890 arguments = cp_parser_enclosed_template_argument_list (parser);
9891 if (!cp_parser_parse_definitely (parser))
9893 /* If we couldn't parse an argument list, then we revert our changes
9894 and return simply an error. Maybe this is not a template-id
9895 after all. */
9896 next_token_2->type = CPP_COLON;
9897 cp_parser_error (parser, "expected %<<%>");
9898 pop_deferring_access_checks ();
9899 return error_mark_node;
9901 /* Otherwise, emit an error about the invalid digraph, but continue
9902 parsing because we got our argument list. */
9903 pedwarn ("%<<::%> cannot begin a template-argument list");
9904 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9905 "between %<<%> and %<::%>");
9906 if (!flag_permissive)
9908 static bool hint;
9909 if (!hint)
9911 inform ("(if you use -fpermissive G++ will accept your code)");
9912 hint = true;
9916 else
9918 /* Look for the `<' that starts the template-argument-list. */
9919 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9921 pop_deferring_access_checks ();
9922 return error_mark_node;
9924 /* Parse the arguments. */
9925 arguments = cp_parser_enclosed_template_argument_list (parser);
9928 /* Build a representation of the specialization. */
9929 if (TREE_CODE (template) == IDENTIFIER_NODE)
9930 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9931 else if (DECL_CLASS_TEMPLATE_P (template)
9932 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9934 bool entering_scope;
9935 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9936 template (rather than some instantiation thereof) only if
9937 is not nested within some other construct. For example, in
9938 "template <typename T> void f(T) { A<T>::", A<T> is just an
9939 instantiation of A. */
9940 entering_scope = (template_parm_scope_p ()
9941 && cp_lexer_next_token_is (parser->lexer,
9942 CPP_SCOPE));
9943 template_id
9944 = finish_template_type (template, arguments, entering_scope);
9946 else
9948 /* If it's not a class-template or a template-template, it should be
9949 a function-template. */
9950 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9951 || TREE_CODE (template) == OVERLOAD
9952 || BASELINK_P (template)));
9954 template_id = lookup_template_function (template, arguments);
9957 /* If parsing tentatively, replace the sequence of tokens that makes
9958 up the template-id with a CPP_TEMPLATE_ID token. That way,
9959 should we re-parse the token stream, we will not have to repeat
9960 the effort required to do the parse, nor will we issue duplicate
9961 error messages about problems during instantiation of the
9962 template. */
9963 if (start_of_id)
9965 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9967 /* Reset the contents of the START_OF_ID token. */
9968 token->type = CPP_TEMPLATE_ID;
9969 /* Retrieve any deferred checks. Do not pop this access checks yet
9970 so the memory will not be reclaimed during token replacing below. */
9971 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9972 token->u.tree_check_value->value = template_id;
9973 token->u.tree_check_value->checks = get_deferred_access_checks ();
9974 token->keyword = RID_MAX;
9976 /* Purge all subsequent tokens. */
9977 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9979 /* ??? Can we actually assume that, if template_id ==
9980 error_mark_node, we will have issued a diagnostic to the
9981 user, as opposed to simply marking the tentative parse as
9982 failed? */
9983 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9984 error ("parse error in template argument list");
9987 pop_deferring_access_checks ();
9988 return template_id;
9991 /* Parse a template-name.
9993 template-name:
9994 identifier
9996 The standard should actually say:
9998 template-name:
9999 identifier
10000 operator-function-id
10002 A defect report has been filed about this issue.
10004 A conversion-function-id cannot be a template name because they cannot
10005 be part of a template-id. In fact, looking at this code:
10007 a.operator K<int>()
10009 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10010 It is impossible to call a templated conversion-function-id with an
10011 explicit argument list, since the only allowed template parameter is
10012 the type to which it is converting.
10014 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10015 `template' keyword, in a construction like:
10017 T::template f<3>()
10019 In that case `f' is taken to be a template-name, even though there
10020 is no way of knowing for sure.
10022 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10023 name refers to a set of overloaded functions, at least one of which
10024 is a template, or an IDENTIFIER_NODE with the name of the template,
10025 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10026 names are looked up inside uninstantiated templates. */
10028 static tree
10029 cp_parser_template_name (cp_parser* parser,
10030 bool template_keyword_p,
10031 bool check_dependency_p,
10032 bool is_declaration,
10033 bool *is_identifier)
10035 tree identifier;
10036 tree decl;
10037 tree fns;
10039 /* If the next token is `operator', then we have either an
10040 operator-function-id or a conversion-function-id. */
10041 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10043 /* We don't know whether we're looking at an
10044 operator-function-id or a conversion-function-id. */
10045 cp_parser_parse_tentatively (parser);
10046 /* Try an operator-function-id. */
10047 identifier = cp_parser_operator_function_id (parser);
10048 /* If that didn't work, try a conversion-function-id. */
10049 if (!cp_parser_parse_definitely (parser))
10051 cp_parser_error (parser, "expected template-name");
10052 return error_mark_node;
10055 /* Look for the identifier. */
10056 else
10057 identifier = cp_parser_identifier (parser);
10059 /* If we didn't find an identifier, we don't have a template-id. */
10060 if (identifier == error_mark_node)
10061 return error_mark_node;
10063 /* If the name immediately followed the `template' keyword, then it
10064 is a template-name. However, if the next token is not `<', then
10065 we do not treat it as a template-name, since it is not being used
10066 as part of a template-id. This enables us to handle constructs
10067 like:
10069 template <typename T> struct S { S(); };
10070 template <typename T> S<T>::S();
10072 correctly. We would treat `S' as a template -- if it were `S<T>'
10073 -- but we do not if there is no `<'. */
10075 if (processing_template_decl
10076 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10078 /* In a declaration, in a dependent context, we pretend that the
10079 "template" keyword was present in order to improve error
10080 recovery. For example, given:
10082 template <typename T> void f(T::X<int>);
10084 we want to treat "X<int>" as a template-id. */
10085 if (is_declaration
10086 && !template_keyword_p
10087 && parser->scope && TYPE_P (parser->scope)
10088 && check_dependency_p
10089 && dependent_type_p (parser->scope)
10090 /* Do not do this for dtors (or ctors), since they never
10091 need the template keyword before their name. */
10092 && !constructor_name_p (identifier, parser->scope))
10094 cp_token_position start = 0;
10096 /* Explain what went wrong. */
10097 error ("non-template %qD used as template", identifier);
10098 inform ("use %<%T::template %D%> to indicate that it is a template",
10099 parser->scope, identifier);
10100 /* If parsing tentatively, find the location of the "<" token. */
10101 if (cp_parser_simulate_error (parser))
10102 start = cp_lexer_token_position (parser->lexer, true);
10103 /* Parse the template arguments so that we can issue error
10104 messages about them. */
10105 cp_lexer_consume_token (parser->lexer);
10106 cp_parser_enclosed_template_argument_list (parser);
10107 /* Skip tokens until we find a good place from which to
10108 continue parsing. */
10109 cp_parser_skip_to_closing_parenthesis (parser,
10110 /*recovering=*/true,
10111 /*or_comma=*/true,
10112 /*consume_paren=*/false);
10113 /* If parsing tentatively, permanently remove the
10114 template argument list. That will prevent duplicate
10115 error messages from being issued about the missing
10116 "template" keyword. */
10117 if (start)
10118 cp_lexer_purge_tokens_after (parser->lexer, start);
10119 if (is_identifier)
10120 *is_identifier = true;
10121 return identifier;
10124 /* If the "template" keyword is present, then there is generally
10125 no point in doing name-lookup, so we just return IDENTIFIER.
10126 But, if the qualifying scope is non-dependent then we can
10127 (and must) do name-lookup normally. */
10128 if (template_keyword_p
10129 && (!parser->scope
10130 || (TYPE_P (parser->scope)
10131 && dependent_type_p (parser->scope))))
10132 return identifier;
10135 /* Look up the name. */
10136 decl = cp_parser_lookup_name (parser, identifier,
10137 none_type,
10138 /*is_template=*/false,
10139 /*is_namespace=*/false,
10140 check_dependency_p,
10141 /*ambiguous_decls=*/NULL);
10142 decl = maybe_get_template_decl_from_type_decl (decl);
10144 /* If DECL is a template, then the name was a template-name. */
10145 if (TREE_CODE (decl) == TEMPLATE_DECL)
10147 else
10149 tree fn = NULL_TREE;
10151 /* The standard does not explicitly indicate whether a name that
10152 names a set of overloaded declarations, some of which are
10153 templates, is a template-name. However, such a name should
10154 be a template-name; otherwise, there is no way to form a
10155 template-id for the overloaded templates. */
10156 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10157 if (TREE_CODE (fns) == OVERLOAD)
10158 for (fn = fns; fn; fn = OVL_NEXT (fn))
10159 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10160 break;
10162 if (!fn)
10164 /* The name does not name a template. */
10165 cp_parser_error (parser, "expected template-name");
10166 return error_mark_node;
10170 /* If DECL is dependent, and refers to a function, then just return
10171 its name; we will look it up again during template instantiation. */
10172 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10174 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10175 if (TYPE_P (scope) && dependent_type_p (scope))
10176 return identifier;
10179 return decl;
10182 /* Parse a template-argument-list.
10184 template-argument-list:
10185 template-argument ... [opt]
10186 template-argument-list , template-argument ... [opt]
10188 Returns a TREE_VEC containing the arguments. */
10190 static tree
10191 cp_parser_template_argument_list (cp_parser* parser)
10193 tree fixed_args[10];
10194 unsigned n_args = 0;
10195 unsigned alloced = 10;
10196 tree *arg_ary = fixed_args;
10197 tree vec;
10198 bool saved_in_template_argument_list_p;
10199 bool saved_ice_p;
10200 bool saved_non_ice_p;
10202 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10203 parser->in_template_argument_list_p = true;
10204 /* Even if the template-id appears in an integral
10205 constant-expression, the contents of the argument list do
10206 not. */
10207 saved_ice_p = parser->integral_constant_expression_p;
10208 parser->integral_constant_expression_p = false;
10209 saved_non_ice_p = parser->non_integral_constant_expression_p;
10210 parser->non_integral_constant_expression_p = false;
10211 /* Parse the arguments. */
10214 tree argument;
10216 if (n_args)
10217 /* Consume the comma. */
10218 cp_lexer_consume_token (parser->lexer);
10220 /* Parse the template-argument. */
10221 argument = cp_parser_template_argument (parser);
10223 /* If the next token is an ellipsis, we're expanding a template
10224 argument pack. */
10225 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10227 /* Consume the `...' token. */
10228 cp_lexer_consume_token (parser->lexer);
10230 /* Make the argument into a TYPE_PACK_EXPANSION or
10231 EXPR_PACK_EXPANSION. */
10232 argument = make_pack_expansion (argument);
10235 if (n_args == alloced)
10237 alloced *= 2;
10239 if (arg_ary == fixed_args)
10241 arg_ary = XNEWVEC (tree, alloced);
10242 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10244 else
10245 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10247 arg_ary[n_args++] = argument;
10249 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10251 vec = make_tree_vec (n_args);
10253 while (n_args--)
10254 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10256 if (arg_ary != fixed_args)
10257 free (arg_ary);
10258 parser->non_integral_constant_expression_p = saved_non_ice_p;
10259 parser->integral_constant_expression_p = saved_ice_p;
10260 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10261 return vec;
10264 /* Parse a template-argument.
10266 template-argument:
10267 assignment-expression
10268 type-id
10269 id-expression
10271 The representation is that of an assignment-expression, type-id, or
10272 id-expression -- except that the qualified id-expression is
10273 evaluated, so that the value returned is either a DECL or an
10274 OVERLOAD.
10276 Although the standard says "assignment-expression", it forbids
10277 throw-expressions or assignments in the template argument.
10278 Therefore, we use "conditional-expression" instead. */
10280 static tree
10281 cp_parser_template_argument (cp_parser* parser)
10283 tree argument;
10284 bool template_p;
10285 bool address_p;
10286 bool maybe_type_id = false;
10287 cp_token *token;
10288 cp_id_kind idk;
10290 /* There's really no way to know what we're looking at, so we just
10291 try each alternative in order.
10293 [temp.arg]
10295 In a template-argument, an ambiguity between a type-id and an
10296 expression is resolved to a type-id, regardless of the form of
10297 the corresponding template-parameter.
10299 Therefore, we try a type-id first. */
10300 cp_parser_parse_tentatively (parser);
10301 argument = cp_parser_type_id (parser);
10302 /* If there was no error parsing the type-id but the next token is a '>>',
10303 we probably found a typo for '> >'. But there are type-id which are
10304 also valid expressions. For instance:
10306 struct X { int operator >> (int); };
10307 template <int V> struct Foo {};
10308 Foo<X () >> 5> r;
10310 Here 'X()' is a valid type-id of a function type, but the user just
10311 wanted to write the expression "X() >> 5". Thus, we remember that we
10312 found a valid type-id, but we still try to parse the argument as an
10313 expression to see what happens. */
10314 if (!cp_parser_error_occurred (parser)
10315 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10317 maybe_type_id = true;
10318 cp_parser_abort_tentative_parse (parser);
10320 else
10322 /* If the next token isn't a `,' or a `>', then this argument wasn't
10323 really finished. This means that the argument is not a valid
10324 type-id. */
10325 if (!cp_parser_next_token_ends_template_argument_p (parser))
10326 cp_parser_error (parser, "expected template-argument");
10327 /* If that worked, we're done. */
10328 if (cp_parser_parse_definitely (parser))
10329 return argument;
10331 /* We're still not sure what the argument will be. */
10332 cp_parser_parse_tentatively (parser);
10333 /* Try a template. */
10334 argument = cp_parser_id_expression (parser,
10335 /*template_keyword_p=*/false,
10336 /*check_dependency_p=*/true,
10337 &template_p,
10338 /*declarator_p=*/false,
10339 /*optional_p=*/false);
10340 /* If the next token isn't a `,' or a `>', then this argument wasn't
10341 really finished. */
10342 if (!cp_parser_next_token_ends_template_argument_p (parser))
10343 cp_parser_error (parser, "expected template-argument");
10344 if (!cp_parser_error_occurred (parser))
10346 /* Figure out what is being referred to. If the id-expression
10347 was for a class template specialization, then we will have a
10348 TYPE_DECL at this point. There is no need to do name lookup
10349 at this point in that case. */
10350 if (TREE_CODE (argument) != TYPE_DECL)
10351 argument = cp_parser_lookup_name (parser, argument,
10352 none_type,
10353 /*is_template=*/template_p,
10354 /*is_namespace=*/false,
10355 /*check_dependency=*/true,
10356 /*ambiguous_decls=*/NULL);
10357 if (TREE_CODE (argument) != TEMPLATE_DECL
10358 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10359 cp_parser_error (parser, "expected template-name");
10361 if (cp_parser_parse_definitely (parser))
10362 return argument;
10363 /* It must be a non-type argument. There permitted cases are given
10364 in [temp.arg.nontype]:
10366 -- an integral constant-expression of integral or enumeration
10367 type; or
10369 -- the name of a non-type template-parameter; or
10371 -- the name of an object or function with external linkage...
10373 -- the address of an object or function with external linkage...
10375 -- a pointer to member... */
10376 /* Look for a non-type template parameter. */
10377 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10379 cp_parser_parse_tentatively (parser);
10380 argument = cp_parser_primary_expression (parser,
10381 /*adress_p=*/false,
10382 /*cast_p=*/false,
10383 /*template_arg_p=*/true,
10384 &idk);
10385 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10386 || !cp_parser_next_token_ends_template_argument_p (parser))
10387 cp_parser_simulate_error (parser);
10388 if (cp_parser_parse_definitely (parser))
10389 return argument;
10392 /* If the next token is "&", the argument must be the address of an
10393 object or function with external linkage. */
10394 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10395 if (address_p)
10396 cp_lexer_consume_token (parser->lexer);
10397 /* See if we might have an id-expression. */
10398 token = cp_lexer_peek_token (parser->lexer);
10399 if (token->type == CPP_NAME
10400 || token->keyword == RID_OPERATOR
10401 || token->type == CPP_SCOPE
10402 || token->type == CPP_TEMPLATE_ID
10403 || token->type == CPP_NESTED_NAME_SPECIFIER)
10405 cp_parser_parse_tentatively (parser);
10406 argument = cp_parser_primary_expression (parser,
10407 address_p,
10408 /*cast_p=*/false,
10409 /*template_arg_p=*/true,
10410 &idk);
10411 if (cp_parser_error_occurred (parser)
10412 || !cp_parser_next_token_ends_template_argument_p (parser))
10413 cp_parser_abort_tentative_parse (parser);
10414 else
10416 if (TREE_CODE (argument) == INDIRECT_REF)
10418 gcc_assert (REFERENCE_REF_P (argument));
10419 argument = TREE_OPERAND (argument, 0);
10422 if (TREE_CODE (argument) == VAR_DECL)
10424 /* A variable without external linkage might still be a
10425 valid constant-expression, so no error is issued here
10426 if the external-linkage check fails. */
10427 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10428 cp_parser_simulate_error (parser);
10430 else if (is_overloaded_fn (argument))
10431 /* All overloaded functions are allowed; if the external
10432 linkage test does not pass, an error will be issued
10433 later. */
10435 else if (address_p
10436 && (TREE_CODE (argument) == OFFSET_REF
10437 || TREE_CODE (argument) == SCOPE_REF))
10438 /* A pointer-to-member. */
10440 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10442 else
10443 cp_parser_simulate_error (parser);
10445 if (cp_parser_parse_definitely (parser))
10447 if (address_p)
10448 argument = build_x_unary_op (ADDR_EXPR, argument);
10449 return argument;
10453 /* If the argument started with "&", there are no other valid
10454 alternatives at this point. */
10455 if (address_p)
10457 cp_parser_error (parser, "invalid non-type template argument");
10458 return error_mark_node;
10461 /* If the argument wasn't successfully parsed as a type-id followed
10462 by '>>', the argument can only be a constant expression now.
10463 Otherwise, we try parsing the constant-expression tentatively,
10464 because the argument could really be a type-id. */
10465 if (maybe_type_id)
10466 cp_parser_parse_tentatively (parser);
10467 argument = cp_parser_constant_expression (parser,
10468 /*allow_non_constant_p=*/false,
10469 /*non_constant_p=*/NULL);
10470 argument = fold_non_dependent_expr (argument);
10471 if (!maybe_type_id)
10472 return argument;
10473 if (!cp_parser_next_token_ends_template_argument_p (parser))
10474 cp_parser_error (parser, "expected template-argument");
10475 if (cp_parser_parse_definitely (parser))
10476 return argument;
10477 /* We did our best to parse the argument as a non type-id, but that
10478 was the only alternative that matched (albeit with a '>' after
10479 it). We can assume it's just a typo from the user, and a
10480 diagnostic will then be issued. */
10481 return cp_parser_type_id (parser);
10484 /* Parse an explicit-instantiation.
10486 explicit-instantiation:
10487 template declaration
10489 Although the standard says `declaration', what it really means is:
10491 explicit-instantiation:
10492 template decl-specifier-seq [opt] declarator [opt] ;
10494 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10495 supposed to be allowed. A defect report has been filed about this
10496 issue.
10498 GNU Extension:
10500 explicit-instantiation:
10501 storage-class-specifier template
10502 decl-specifier-seq [opt] declarator [opt] ;
10503 function-specifier template
10504 decl-specifier-seq [opt] declarator [opt] ; */
10506 static void
10507 cp_parser_explicit_instantiation (cp_parser* parser)
10509 int declares_class_or_enum;
10510 cp_decl_specifier_seq decl_specifiers;
10511 tree extension_specifier = NULL_TREE;
10513 /* Look for an (optional) storage-class-specifier or
10514 function-specifier. */
10515 if (cp_parser_allow_gnu_extensions_p (parser))
10517 extension_specifier
10518 = cp_parser_storage_class_specifier_opt (parser);
10519 if (!extension_specifier)
10520 extension_specifier
10521 = cp_parser_function_specifier_opt (parser,
10522 /*decl_specs=*/NULL);
10525 /* Look for the `template' keyword. */
10526 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10527 /* Let the front end know that we are processing an explicit
10528 instantiation. */
10529 begin_explicit_instantiation ();
10530 /* [temp.explicit] says that we are supposed to ignore access
10531 control while processing explicit instantiation directives. */
10532 push_deferring_access_checks (dk_no_check);
10533 /* Parse a decl-specifier-seq. */
10534 cp_parser_decl_specifier_seq (parser,
10535 CP_PARSER_FLAGS_OPTIONAL,
10536 &decl_specifiers,
10537 &declares_class_or_enum);
10538 /* If there was exactly one decl-specifier, and it declared a class,
10539 and there's no declarator, then we have an explicit type
10540 instantiation. */
10541 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10543 tree type;
10545 type = check_tag_decl (&decl_specifiers);
10546 /* Turn access control back on for names used during
10547 template instantiation. */
10548 pop_deferring_access_checks ();
10549 if (type)
10550 do_type_instantiation (type, extension_specifier,
10551 /*complain=*/tf_error);
10553 else
10555 cp_declarator *declarator;
10556 tree decl;
10558 /* Parse the declarator. */
10559 declarator
10560 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10561 /*ctor_dtor_or_conv_p=*/NULL,
10562 /*parenthesized_p=*/NULL,
10563 /*member_p=*/false);
10564 if (declares_class_or_enum & 2)
10565 cp_parser_check_for_definition_in_return_type (declarator,
10566 decl_specifiers.type);
10567 if (declarator != cp_error_declarator)
10569 decl = grokdeclarator (declarator, &decl_specifiers,
10570 NORMAL, 0, &decl_specifiers.attributes);
10571 /* Turn access control back on for names used during
10572 template instantiation. */
10573 pop_deferring_access_checks ();
10574 /* Do the explicit instantiation. */
10575 do_decl_instantiation (decl, extension_specifier);
10577 else
10579 pop_deferring_access_checks ();
10580 /* Skip the body of the explicit instantiation. */
10581 cp_parser_skip_to_end_of_statement (parser);
10584 /* We're done with the instantiation. */
10585 end_explicit_instantiation ();
10587 cp_parser_consume_semicolon_at_end_of_statement (parser);
10590 /* Parse an explicit-specialization.
10592 explicit-specialization:
10593 template < > declaration
10595 Although the standard says `declaration', what it really means is:
10597 explicit-specialization:
10598 template <> decl-specifier [opt] init-declarator [opt] ;
10599 template <> function-definition
10600 template <> explicit-specialization
10601 template <> template-declaration */
10603 static void
10604 cp_parser_explicit_specialization (cp_parser* parser)
10606 bool need_lang_pop;
10607 /* Look for the `template' keyword. */
10608 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10609 /* Look for the `<'. */
10610 cp_parser_require (parser, CPP_LESS, "`<'");
10611 /* Look for the `>'. */
10612 cp_parser_require (parser, CPP_GREATER, "`>'");
10613 /* We have processed another parameter list. */
10614 ++parser->num_template_parameter_lists;
10615 /* [temp]
10617 A template ... explicit specialization ... shall not have C
10618 linkage. */
10619 if (current_lang_name == lang_name_c)
10621 error ("template specialization with C linkage");
10622 /* Give it C++ linkage to avoid confusing other parts of the
10623 front end. */
10624 push_lang_context (lang_name_cplusplus);
10625 need_lang_pop = true;
10627 else
10628 need_lang_pop = false;
10629 /* Let the front end know that we are beginning a specialization. */
10630 if (!begin_specialization ())
10632 end_specialization ();
10633 cp_parser_skip_to_end_of_block_or_statement (parser);
10634 return;
10637 /* If the next keyword is `template', we need to figure out whether
10638 or not we're looking a template-declaration. */
10639 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10641 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10642 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10643 cp_parser_template_declaration_after_export (parser,
10644 /*member_p=*/false);
10645 else
10646 cp_parser_explicit_specialization (parser);
10648 else
10649 /* Parse the dependent declaration. */
10650 cp_parser_single_declaration (parser,
10651 /*checks=*/NULL,
10652 /*member_p=*/false,
10653 /*explicit_specialization_p=*/true,
10654 /*friend_p=*/NULL);
10655 /* We're done with the specialization. */
10656 end_specialization ();
10657 /* For the erroneous case of a template with C linkage, we pushed an
10658 implicit C++ linkage scope; exit that scope now. */
10659 if (need_lang_pop)
10660 pop_lang_context ();
10661 /* We're done with this parameter list. */
10662 --parser->num_template_parameter_lists;
10665 /* Parses a refinement clause. Returns a TREE_LIST of refined
10666 concepts, in which each TREE_VALUE contains the name of a concept
10667 instance.
10669 refinement-clause:
10670 : refinement-specifier-list
10672 refinement-specifier-list:
10673 refinement-specifier , refinement-specifier-list
10674 refinement-specifier */
10676 static tree
10677 cp_parser_refinement_clause (cp_parser *parser)
10679 tree refinements = NULL_TREE;
10681 /* Parse the `:' that starts the refinement clause. */
10682 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
10683 return NULL_TREE;
10685 while (true)
10687 tree concept_id;
10688 cp_token *token;
10690 /* Get the refinement-specifier. */
10691 concept_id = cp_parser_refinement_specifier (parser);
10693 if (concept_id != error_mark_node)
10694 /* Add the refinement-specifier to the list of
10695 refinements. */
10696 refinements = tree_cons (NULL_TREE, concept_id, refinements);
10698 /* Peek at the next token. */
10699 token = cp_lexer_peek_token (parser->lexer);
10701 /* If it's not a `,', we're done. */
10702 if (token->type != CPP_COMMA)
10703 break;
10705 /* Consume the `,'. */
10706 cp_lexer_consume_token (parser->lexer);
10709 return nreverse (refinements);
10712 /* Parse a refinement-specifier.
10714 refinement-specifier:
10715 :: [opt] nested-name-specifier [opt] concept-id */
10716 static tree
10717 cp_parser_refinement_specifier (cp_parser *parser)
10719 tree concept_id;
10721 /* Look for the optional `::' operator. */
10722 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10724 /* Look for the optional nested-name-specifier. */
10725 cp_parser_nested_name_specifier_opt (parser,
10726 /*typename_keyword_p=*/false,
10727 /*check_dependency_p=*/true,
10728 /*type_p=*/true,
10729 /*is_declaration=*/false);
10731 /* A concept-id is just a template-id that may only refer to a
10732 concept. */
10733 concept_id = cp_parser_template_id (parser,
10734 /*template_keyword_p=*/false,
10735 /*check_dependency_p=*/true,
10736 /*is_declaration=*/false);
10738 if (TREE_CODE (concept_id) == TYPE_DECL)
10740 /* Retrieve the RECORD_TYPE itself. */
10741 concept_id = TREE_TYPE (concept_id);
10743 if (TREE_CODE (concept_id) != RECORD_TYPE
10744 || !CLASSTYPE_USE_CONCEPT (concept_id))
10746 /* Refinements can only refer to concepts. */
10747 error ("refinement specifier %<%T%> does not refer to a concept",
10748 concept_id);
10749 return error_mark_node;
10752 /* Ensure that there are no "bare" parameter packs within this
10753 refinement specifier. */
10754 if (check_for_bare_parameter_packs (&concept_id))
10755 return error_mark_node;
10757 return concept_id;
10760 return error_mark_node;
10763 static void
10764 cp_parser_concept_body (cp_parser *parser)
10766 /* TODO */(void)parser;
10769 /* Parse a type-specifier.
10771 type-specifier:
10772 simple-type-specifier
10773 class-specifier
10774 enum-specifier
10775 elaborated-type-specifier
10776 cv-qualifier
10778 GNU Extension:
10780 type-specifier:
10781 __complex__
10783 Returns a representation of the type-specifier. For a
10784 class-specifier, enum-specifier, or elaborated-type-specifier, a
10785 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10787 The parser flags FLAGS is used to control type-specifier parsing.
10789 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10790 in a decl-specifier-seq.
10792 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10793 class-specifier, enum-specifier, or elaborated-type-specifier, then
10794 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10795 if a type is declared; 2 if it is defined. Otherwise, it is set to
10796 zero.
10798 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10799 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10800 is set to FALSE. */
10802 static tree
10803 cp_parser_type_specifier (cp_parser* parser,
10804 cp_parser_flags flags,
10805 cp_decl_specifier_seq *decl_specs,
10806 bool is_declaration,
10807 int* declares_class_or_enum,
10808 bool* is_cv_qualifier)
10810 tree type_spec = NULL_TREE;
10811 cp_token *token;
10812 enum rid keyword;
10813 cp_decl_spec ds = ds_last;
10815 /* Assume this type-specifier does not declare a new type. */
10816 if (declares_class_or_enum)
10817 *declares_class_or_enum = 0;
10818 /* And that it does not specify a cv-qualifier. */
10819 if (is_cv_qualifier)
10820 *is_cv_qualifier = false;
10821 /* Peek at the next token. */
10822 token = cp_lexer_peek_token (parser->lexer);
10824 /* If we're looking at a keyword, we can use that to guide the
10825 production we choose. */
10826 keyword = token->keyword;
10827 switch (keyword)
10829 case RID_ENUM:
10830 /* Look for the enum-specifier. */
10831 type_spec = cp_parser_enum_specifier (parser);
10832 /* If that worked, we're done. */
10833 if (type_spec)
10835 if (declares_class_or_enum)
10836 *declares_class_or_enum = 2;
10837 if (decl_specs)
10838 cp_parser_set_decl_spec_type (decl_specs,
10839 type_spec,
10840 /*user_defined_p=*/true);
10841 return type_spec;
10843 else
10844 goto elaborated_type_specifier;
10846 /* Any of these indicate either a class-specifier, or an
10847 elaborated-type-specifier. */
10848 case RID_CLASS:
10849 case RID_STRUCT:
10850 case RID_UNION:
10851 /* Parse tentatively so that we can back up if we don't find a
10852 class-specifier. */
10853 cp_parser_parse_tentatively (parser);
10854 /* Look for the class-specifier. */
10855 type_spec = cp_parser_class_specifier (parser);
10856 /* If that worked, we're done. */
10857 if (cp_parser_parse_definitely (parser))
10859 if (declares_class_or_enum)
10860 *declares_class_or_enum = 2;
10861 if (decl_specs)
10862 cp_parser_set_decl_spec_type (decl_specs,
10863 type_spec,
10864 /*user_defined_p=*/true);
10865 return type_spec;
10868 /* Fall through. */
10869 elaborated_type_specifier:
10870 /* We're declaring (not defining) a class or enum. */
10871 if (declares_class_or_enum)
10872 *declares_class_or_enum = 1;
10874 /* Fall through. */
10875 case RID_TYPENAME:
10876 /* Look for an elaborated-type-specifier. */
10877 type_spec
10878 = (cp_parser_elaborated_type_specifier
10879 (parser,
10880 decl_specs && decl_specs->specs[(int) ds_friend],
10881 is_declaration));
10882 if (decl_specs)
10883 cp_parser_set_decl_spec_type (decl_specs,
10884 type_spec,
10885 /*user_defined_p=*/true);
10886 return type_spec;
10888 case RID_CONST:
10889 ds = ds_const;
10890 if (is_cv_qualifier)
10891 *is_cv_qualifier = true;
10892 break;
10894 case RID_VOLATILE:
10895 ds = ds_volatile;
10896 if (is_cv_qualifier)
10897 *is_cv_qualifier = true;
10898 break;
10900 case RID_RESTRICT:
10901 ds = ds_restrict;
10902 if (is_cv_qualifier)
10903 *is_cv_qualifier = true;
10904 break;
10906 case RID_COMPLEX:
10907 /* The `__complex__' keyword is a GNU extension. */
10908 ds = ds_complex;
10909 break;
10911 default:
10912 break;
10915 /* Handle simple keywords. */
10916 if (ds != ds_last)
10918 if (decl_specs)
10920 ++decl_specs->specs[(int)ds];
10921 decl_specs->any_specifiers_p = true;
10923 return cp_lexer_consume_token (parser->lexer)->u.value;
10926 /* If we do not already have a type-specifier, assume we are looking
10927 at a simple-type-specifier. */
10928 type_spec = cp_parser_simple_type_specifier (parser,
10929 decl_specs,
10930 flags);
10932 /* If we didn't find a type-specifier, and a type-specifier was not
10933 optional in this context, issue an error message. */
10934 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10936 cp_parser_error (parser, "expected type specifier");
10937 return error_mark_node;
10940 return type_spec;
10943 /* Parse a simple-type-specifier.
10945 simple-type-specifier:
10946 :: [opt] nested-name-specifier [opt] type-name
10947 :: [opt] nested-name-specifier template template-id
10948 char
10949 wchar_t
10950 bool
10951 short
10953 long
10954 signed
10955 unsigned
10956 float
10957 double
10958 void
10960 C++0x Extension:
10962 simple-type-specifier:
10963 decltype ( expression )
10965 GNU Extension:
10967 simple-type-specifier:
10968 __typeof__ unary-expression
10969 __typeof__ ( type-id )
10971 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10972 appropriately updated. */
10974 static tree
10975 cp_parser_simple_type_specifier (cp_parser* parser,
10976 cp_decl_specifier_seq *decl_specs,
10977 cp_parser_flags flags)
10979 tree type = NULL_TREE;
10980 cp_token *token;
10982 /* Peek at the next token. */
10983 token = cp_lexer_peek_token (parser->lexer);
10985 /* If we're looking at a keyword, things are easy. */
10986 switch (token->keyword)
10988 case RID_CHAR:
10989 if (decl_specs)
10990 decl_specs->explicit_char_p = true;
10991 type = char_type_node;
10992 break;
10993 case RID_WCHAR:
10994 type = wchar_type_node;
10995 break;
10996 case RID_BOOL:
10997 type = boolean_type_node;
10998 break;
10999 case RID_SHORT:
11000 if (decl_specs)
11001 ++decl_specs->specs[(int) ds_short];
11002 type = short_integer_type_node;
11003 break;
11004 case RID_INT:
11005 if (decl_specs)
11006 decl_specs->explicit_int_p = true;
11007 type = integer_type_node;
11008 break;
11009 case RID_LONG:
11010 if (decl_specs)
11011 ++decl_specs->specs[(int) ds_long];
11012 type = long_integer_type_node;
11013 break;
11014 case RID_SIGNED:
11015 if (decl_specs)
11016 ++decl_specs->specs[(int) ds_signed];
11017 type = integer_type_node;
11018 break;
11019 case RID_UNSIGNED:
11020 if (decl_specs)
11021 ++decl_specs->specs[(int) ds_unsigned];
11022 type = unsigned_type_node;
11023 break;
11024 case RID_FLOAT:
11025 type = float_type_node;
11026 break;
11027 case RID_DOUBLE:
11028 type = double_type_node;
11029 break;
11030 case RID_VOID:
11031 type = void_type_node;
11032 break;
11034 case RID_DECLTYPE:
11035 /* Parse the `decltype' type. */
11036 type = cp_parser_decltype (parser);
11038 if (decl_specs)
11039 cp_parser_set_decl_spec_type (decl_specs, type,
11040 /*user_defined_p=*/true);
11042 return type;
11044 case RID_TYPEOF:
11045 /* Consume the `typeof' token. */
11046 cp_lexer_consume_token (parser->lexer);
11047 /* Parse the operand to `typeof'. */
11048 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11049 /* If it is not already a TYPE, take its type. */
11050 if (!TYPE_P (type))
11051 type = finish_typeof (type);
11053 if (decl_specs)
11054 cp_parser_set_decl_spec_type (decl_specs, type,
11055 /*user_defined_p=*/true);
11057 return type;
11059 default:
11060 break;
11063 /* If the type-specifier was for a built-in type, we're done. */
11064 if (type)
11066 tree id;
11068 /* Record the type. */
11069 if (decl_specs
11070 && (token->keyword != RID_SIGNED
11071 && token->keyword != RID_UNSIGNED
11072 && token->keyword != RID_SHORT
11073 && token->keyword != RID_LONG))
11074 cp_parser_set_decl_spec_type (decl_specs,
11075 type,
11076 /*user_defined=*/false);
11077 if (decl_specs)
11078 decl_specs->any_specifiers_p = true;
11080 /* Consume the token. */
11081 id = cp_lexer_consume_token (parser->lexer)->u.value;
11083 /* There is no valid C++ program where a non-template type is
11084 followed by a "<". That usually indicates that the user thought
11085 that the type was a template. */
11086 cp_parser_check_for_invalid_template_id (parser, type);
11088 return TYPE_NAME (type);
11091 /* The type-specifier must be a user-defined type. */
11092 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11094 bool qualified_p;
11095 bool global_p;
11097 /* Don't gobble tokens or issue error messages if this is an
11098 optional type-specifier. */
11099 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11100 cp_parser_parse_tentatively (parser);
11102 /* Look for the optional `::' operator. */
11103 global_p
11104 = (cp_parser_global_scope_opt (parser,
11105 /*current_scope_valid_p=*/false)
11106 != NULL_TREE);
11107 /* Look for the nested-name specifier. */
11108 qualified_p
11109 = (cp_parser_nested_name_specifier_opt (parser,
11110 /*typename_keyword_p=*/false,
11111 /*check_dependency_p=*/true,
11112 /*type_p=*/false,
11113 /*is_declaration=*/false)
11114 != NULL_TREE);
11115 /* If we have seen a nested-name-specifier, and the next token
11116 is `template', then we are using the template-id production. */
11117 if (parser->scope
11118 && cp_parser_optional_template_keyword (parser))
11120 /* Look for the template-id. */
11121 type = cp_parser_template_id (parser,
11122 /*template_keyword_p=*/true,
11123 /*check_dependency_p=*/true,
11124 /*is_declaration=*/false);
11125 /* If the template-id did not name a type, we are out of
11126 luck. */
11127 if (TREE_CODE (type) != TYPE_DECL)
11129 cp_parser_error (parser, "expected template-id for type");
11130 type = NULL_TREE;
11133 /* Otherwise, look for a type-name. */
11134 else
11135 type = cp_parser_type_name (parser);
11136 /* Keep track of all name-lookups performed in class scopes. */
11137 if (type
11138 && !global_p
11139 && !qualified_p
11140 && TREE_CODE (type) == TYPE_DECL
11141 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11142 maybe_note_name_used_in_class (DECL_NAME (type), type);
11143 /* If it didn't work out, we don't have a TYPE. */
11144 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11145 && !cp_parser_parse_definitely (parser))
11146 type = NULL_TREE;
11147 if (type && decl_specs)
11148 cp_parser_set_decl_spec_type (decl_specs, type,
11149 /*user_defined=*/true);
11152 /* If we didn't get a type-name, issue an error message. */
11153 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11155 cp_parser_error (parser, "expected type-name");
11156 return error_mark_node;
11159 /* There is no valid C++ program where a non-template type is
11160 followed by a "<". That usually indicates that the user thought
11161 that the type was a template. */
11162 if (type && type != error_mark_node)
11164 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11165 If it is, then the '<'...'>' enclose protocol names rather than
11166 template arguments, and so everything is fine. */
11167 if (c_dialect_objc ()
11168 && (objc_is_id (type) || objc_is_class_name (type)))
11170 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11171 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11173 /* Clobber the "unqualified" type previously entered into
11174 DECL_SPECS with the new, improved protocol-qualified version. */
11175 if (decl_specs)
11176 decl_specs->type = qual_type;
11178 return qual_type;
11181 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
11184 return type;
11187 /* Parse a type-name.
11189 type-name:
11190 class-name
11191 enum-name
11192 typedef-name
11194 enum-name:
11195 identifier
11197 typedef-name:
11198 identifier
11200 Returns a TYPE_DECL for the type. */
11202 static tree
11203 cp_parser_type_name (cp_parser* parser)
11205 tree type_decl;
11206 tree identifier;
11208 /* We can't know yet whether it is a class-name or not. */
11209 cp_parser_parse_tentatively (parser);
11210 /* Try a class-name. */
11211 type_decl = cp_parser_class_name (parser,
11212 /*typename_keyword_p=*/false,
11213 /*template_keyword_p=*/false,
11214 none_type,
11215 /*check_dependency_p=*/true,
11216 /*class_head_p=*/false,
11217 /*is_declaration=*/false);
11218 /* If it's not a class-name, keep looking. */
11219 if (!cp_parser_parse_definitely (parser))
11221 /* It must be a typedef-name or an enum-name. */
11222 identifier = cp_parser_identifier (parser);
11223 if (identifier == error_mark_node)
11224 return error_mark_node;
11226 /* Look up the type-name. */
11227 type_decl = cp_parser_lookup_name_simple (parser, identifier);
11229 if (TREE_CODE (type_decl) != TYPE_DECL
11230 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11232 /* See if this is an Objective-C type. */
11233 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11234 tree type = objc_get_protocol_qualified_type (identifier, protos);
11235 if (type)
11236 type_decl = TYPE_NAME (type);
11239 /* Issue an error if we did not find a type-name. */
11240 if (TREE_CODE (type_decl) != TYPE_DECL)
11242 if (!cp_parser_simulate_error (parser))
11243 cp_parser_name_lookup_error (parser, identifier, type_decl,
11244 "is not a type");
11245 type_decl = error_mark_node;
11247 /* Remember that the name was used in the definition of the
11248 current class so that we can check later to see if the
11249 meaning would have been different after the class was
11250 entirely defined. */
11251 else if (type_decl != error_mark_node
11252 && !parser->scope)
11253 maybe_note_name_used_in_class (identifier, type_decl);
11256 return type_decl;
11260 /* Parse an elaborated-type-specifier. Note that the grammar given
11261 here incorporates the resolution to DR68.
11263 elaborated-type-specifier:
11264 class-key :: [opt] nested-name-specifier [opt] identifier
11265 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11266 enum :: [opt] nested-name-specifier [opt] identifier
11267 typename :: [opt] nested-name-specifier identifier
11268 typename :: [opt] nested-name-specifier template [opt]
11269 template-id
11271 GNU extension:
11273 elaborated-type-specifier:
11274 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11275 class-key attributes :: [opt] nested-name-specifier [opt]
11276 template [opt] template-id
11277 enum attributes :: [opt] nested-name-specifier [opt] identifier
11279 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11280 declared `friend'. If IS_DECLARATION is TRUE, then this
11281 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11282 something is being declared.
11284 Returns the TYPE specified. */
11286 static tree
11287 cp_parser_elaborated_type_specifier (cp_parser* parser,
11288 bool is_friend,
11289 bool is_declaration)
11291 enum tag_types tag_type;
11292 tree identifier;
11293 tree type = NULL_TREE;
11294 tree attributes = NULL_TREE;
11296 /* See if we're looking at the `enum' keyword. */
11297 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11299 /* Consume the `enum' token. */
11300 cp_lexer_consume_token (parser->lexer);
11301 /* Remember that it's an enumeration type. */
11302 tag_type = enum_type;
11303 /* Parse the attributes. */
11304 attributes = cp_parser_attributes_opt (parser);
11306 /* Or, it might be `typename'. */
11307 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11308 RID_TYPENAME))
11310 /* Consume the `typename' token. */
11311 cp_lexer_consume_token (parser->lexer);
11312 /* Remember that it's a `typename' type. */
11313 tag_type = typename_type;
11314 /* The `typename' keyword is only allowed in templates. */
11315 if (!processing_template_decl)
11316 pedwarn ("using %<typename%> outside of template");
11318 /* Otherwise it must be a class-key. */
11319 else
11321 tag_type = cp_parser_class_key (parser);
11322 if (tag_type == none_type)
11323 return error_mark_node;
11324 /* Parse the attributes. */
11325 attributes = cp_parser_attributes_opt (parser);
11328 /* Look for the `::' operator. */
11329 cp_parser_global_scope_opt (parser,
11330 /*current_scope_valid_p=*/false);
11331 /* Look for the nested-name-specifier. */
11332 if (tag_type == typename_type)
11334 if (!cp_parser_nested_name_specifier (parser,
11335 /*typename_keyword_p=*/true,
11336 /*check_dependency_p=*/true,
11337 /*type_p=*/true,
11338 is_declaration))
11339 return error_mark_node;
11341 else
11342 /* Even though `typename' is not present, the proposed resolution
11343 to Core Issue 180 says that in `class A<T>::B', `B' should be
11344 considered a type-name, even if `A<T>' is dependent. */
11345 cp_parser_nested_name_specifier_opt (parser,
11346 /*typename_keyword_p=*/true,
11347 /*check_dependency_p=*/true,
11348 /*type_p=*/true,
11349 is_declaration);
11350 /* For everything but enumeration types, consider a template-id.
11351 For an enumeration type, consider only a plain identifier. */
11352 if (tag_type != enum_type)
11354 bool template_p = false;
11355 tree decl;
11357 /* Allow the `template' keyword. */
11358 template_p = cp_parser_optional_template_keyword (parser);
11359 /* If we didn't see `template', we don't know if there's a
11360 template-id or not. */
11361 if (!template_p)
11362 cp_parser_parse_tentatively (parser);
11363 /* Parse the template-id. */
11364 decl = cp_parser_template_id (parser, template_p,
11365 /*check_dependency_p=*/true,
11366 is_declaration);
11367 /* If we didn't find a template-id, look for an ordinary
11368 identifier. */
11369 if (!template_p && !cp_parser_parse_definitely (parser))
11371 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11372 in effect, then we must assume that, upon instantiation, the
11373 template will correspond to a class. */
11374 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11375 && tag_type == typename_type)
11376 type = make_typename_type (parser->scope, decl,
11377 typename_type,
11378 /*complain=*/tf_error);
11379 else
11380 type = TREE_TYPE (decl);
11383 if (!type)
11385 identifier = cp_parser_identifier (parser);
11387 if (identifier == error_mark_node)
11389 parser->scope = NULL_TREE;
11390 return error_mark_node;
11393 /* For a `typename', we needn't call xref_tag. */
11394 if (tag_type == typename_type
11395 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11396 return cp_parser_make_typename_type (parser, parser->scope,
11397 identifier);
11398 /* Look up a qualified name in the usual way. */
11399 if (parser->scope)
11401 tree decl;
11402 tree ambiguous_decls;
11404 decl = cp_parser_lookup_name (parser, identifier,
11405 tag_type,
11406 /*is_template=*/false,
11407 /*is_namespace=*/false,
11408 /*check_dependency=*/true,
11409 &ambiguous_decls);
11411 /* If the lookup was ambiguous, an error will already have been
11412 issued. */
11413 if (ambiguous_decls)
11414 return error_mark_node;
11416 /* If we are parsing friend declaration, DECL may be a
11417 TEMPLATE_DECL tree node here. However, we need to check
11418 whether this TEMPLATE_DECL results in valid code. Consider
11419 the following example:
11421 namespace N {
11422 template <class T> class C {};
11424 class X {
11425 template <class T> friend class N::C; // #1, valid code
11427 template <class T> class Y {
11428 friend class N::C; // #2, invalid code
11431 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11432 name lookup of `N::C'. We see that friend declaration must
11433 be template for the code to be valid. Note that
11434 processing_template_decl does not work here since it is
11435 always 1 for the above two cases. */
11437 decl = (cp_parser_maybe_treat_template_as_class
11438 (decl, /*tag_name_p=*/is_friend
11439 && parser->num_template_parameter_lists));
11441 if (TREE_CODE (decl) != TYPE_DECL)
11443 cp_parser_diagnose_invalid_type_name (parser,
11444 parser->scope,
11445 identifier);
11446 return error_mark_node;
11449 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11451 bool allow_template = (parser->num_template_parameter_lists
11452 || DECL_SELF_REFERENCE_P (decl));
11453 type = check_elaborated_type_specifier (tag_type, decl,
11454 allow_template);
11456 if (type == error_mark_node)
11457 return error_mark_node;
11460 /* Forward declarations of nested types, such as
11462 class C1::C2;
11463 class C1::C2::C3;
11465 are invalid unless all components preceding the final '::'
11466 are complete. If all enclosing types are complete, these
11467 declarations become merely pointless.
11469 Invalid forward declarations of nested types are errors
11470 caught elsewhere in parsing. Those that are pointless arrive
11471 here. */
11473 if (cp_parser_declares_only_class_p (parser)
11474 && !is_friend && !processing_explicit_instantiation)
11475 warning (0, "declaration %qD does not declare anything", decl);
11477 type = TREE_TYPE (decl);
11479 else
11481 /* An elaborated-type-specifier sometimes introduces a new type and
11482 sometimes names an existing type. Normally, the rule is that it
11483 introduces a new type only if there is not an existing type of
11484 the same name already in scope. For example, given:
11486 struct S {};
11487 void f() { struct S s; }
11489 the `struct S' in the body of `f' is the same `struct S' as in
11490 the global scope; the existing definition is used. However, if
11491 there were no global declaration, this would introduce a new
11492 local class named `S'.
11494 An exception to this rule applies to the following code:
11496 namespace N { struct S; }
11498 Here, the elaborated-type-specifier names a new type
11499 unconditionally; even if there is already an `S' in the
11500 containing scope this declaration names a new type.
11501 This exception only applies if the elaborated-type-specifier
11502 forms the complete declaration:
11504 [class.name]
11506 A declaration consisting solely of `class-key identifier ;' is
11507 either a redeclaration of the name in the current scope or a
11508 forward declaration of the identifier as a class name. It
11509 introduces the name into the current scope.
11511 We are in this situation precisely when the next token is a `;'.
11513 An exception to the exception is that a `friend' declaration does
11514 *not* name a new type; i.e., given:
11516 struct S { friend struct T; };
11518 `T' is not a new type in the scope of `S'.
11520 Also, `new struct S' or `sizeof (struct S)' never results in the
11521 definition of a new type; a new type can only be declared in a
11522 declaration context. */
11524 tag_scope ts;
11525 bool template_p;
11527 if (is_friend)
11528 /* Friends have special name lookup rules. */
11529 ts = ts_within_enclosing_non_class;
11530 else if (is_declaration
11531 && cp_lexer_next_token_is (parser->lexer,
11532 CPP_SEMICOLON))
11533 /* This is a `class-key identifier ;' */
11534 ts = ts_current;
11535 else
11536 ts = ts_global;
11538 template_p =
11539 (parser->num_template_parameter_lists
11540 && (cp_parser_next_token_starts_class_definition_p (parser)
11541 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11542 /* An unqualified name was used to reference this type, so
11543 there were no qualifying templates. */
11544 if (!cp_parser_check_template_parameters (parser,
11545 /*num_templates=*/0))
11546 return error_mark_node;
11547 type = xref_tag (tag_type, identifier, ts, template_p);
11551 if (type == error_mark_node)
11552 return error_mark_node;
11554 /* Allow attributes on forward declarations of classes. */
11555 if (attributes)
11557 if (TREE_CODE (type) == TYPENAME_TYPE)
11558 warning (OPT_Wattributes,
11559 "attributes ignored on uninstantiated type");
11560 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11561 && ! processing_explicit_instantiation)
11562 warning (OPT_Wattributes,
11563 "attributes ignored on template instantiation");
11564 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11565 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11566 else
11567 warning (OPT_Wattributes,
11568 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11571 if (tag_type != enum_type)
11572 cp_parser_check_class_key (tag_type, type);
11574 /* A "<" cannot follow an elaborated type specifier. If that
11575 happens, the user was probably trying to form a template-id. */
11576 cp_parser_check_for_invalid_template_id (parser, type);
11578 return type;
11581 /* Parse an enum-specifier.
11583 enum-specifier:
11584 enum identifier [opt] { enumerator-list [opt] }
11586 GNU Extensions:
11587 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11588 attributes[opt]
11590 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11591 if the token stream isn't an enum-specifier after all. */
11593 static tree
11594 cp_parser_enum_specifier (cp_parser* parser)
11596 tree identifier;
11597 tree type;
11598 tree attributes;
11600 /* Parse tentatively so that we can back up if we don't find a
11601 enum-specifier. */
11602 cp_parser_parse_tentatively (parser);
11604 /* Caller guarantees that the current token is 'enum', an identifier
11605 possibly follows, and the token after that is an opening brace.
11606 If we don't have an identifier, fabricate an anonymous name for
11607 the enumeration being defined. */
11608 cp_lexer_consume_token (parser->lexer);
11610 attributes = cp_parser_attributes_opt (parser);
11612 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11613 identifier = cp_parser_identifier (parser);
11614 else
11615 identifier = make_anon_name ();
11617 /* Look for the `{' but don't consume it yet. */
11618 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11619 cp_parser_simulate_error (parser);
11621 if (!cp_parser_parse_definitely (parser))
11622 return NULL_TREE;
11624 /* Issue an error message if type-definitions are forbidden here. */
11625 if (!cp_parser_check_type_definition (parser))
11626 type = error_mark_node;
11627 else
11628 /* Create the new type. We do this before consuming the opening
11629 brace so the enum will be recorded as being on the line of its
11630 tag (or the 'enum' keyword, if there is no tag). */
11631 type = start_enum (identifier);
11633 /* Consume the opening brace. */
11634 cp_lexer_consume_token (parser->lexer);
11636 if (type == error_mark_node)
11638 cp_parser_skip_to_end_of_block_or_statement (parser);
11639 return error_mark_node;
11642 /* If the next token is not '}', then there are some enumerators. */
11643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11644 cp_parser_enumerator_list (parser, type);
11646 /* Consume the final '}'. */
11647 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11649 /* Look for trailing attributes to apply to this enumeration, and
11650 apply them if appropriate. */
11651 if (cp_parser_allow_gnu_extensions_p (parser))
11653 tree trailing_attr = cp_parser_attributes_opt (parser);
11654 cplus_decl_attributes (&type,
11655 trailing_attr,
11656 (int) ATTR_FLAG_TYPE_IN_PLACE);
11659 /* Finish up the enumeration. */
11660 finish_enum (type);
11662 return type;
11665 /* Parse an enumerator-list. The enumerators all have the indicated
11666 TYPE.
11668 enumerator-list:
11669 enumerator-definition
11670 enumerator-list , enumerator-definition */
11672 static void
11673 cp_parser_enumerator_list (cp_parser* parser, tree type)
11675 while (true)
11677 /* Parse an enumerator-definition. */
11678 cp_parser_enumerator_definition (parser, type);
11680 /* If the next token is not a ',', we've reached the end of
11681 the list. */
11682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11683 break;
11684 /* Otherwise, consume the `,' and keep going. */
11685 cp_lexer_consume_token (parser->lexer);
11686 /* If the next token is a `}', there is a trailing comma. */
11687 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11689 if (pedantic && !in_system_header)
11690 pedwarn ("comma at end of enumerator list");
11691 break;
11696 /* Parse an enumerator-definition. The enumerator has the indicated
11697 TYPE.
11699 enumerator-definition:
11700 enumerator
11701 enumerator = constant-expression
11703 enumerator:
11704 identifier */
11706 static void
11707 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11709 tree identifier;
11710 tree value;
11712 /* Look for the identifier. */
11713 identifier = cp_parser_identifier (parser);
11714 if (identifier == error_mark_node)
11715 return;
11717 /* If the next token is an '=', then there is an explicit value. */
11718 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11720 /* Consume the `=' token. */
11721 cp_lexer_consume_token (parser->lexer);
11722 /* Parse the value. */
11723 value = cp_parser_constant_expression (parser,
11724 /*allow_non_constant_p=*/false,
11725 NULL);
11727 else
11728 value = NULL_TREE;
11730 /* Create the enumerator. */
11731 build_enumerator (identifier, value, type);
11734 /* Parse a namespace-name.
11736 namespace-name:
11737 original-namespace-name
11738 namespace-alias
11740 Returns the NAMESPACE_DECL for the namespace. */
11742 static tree
11743 cp_parser_namespace_name (cp_parser* parser)
11745 tree identifier;
11746 tree namespace_decl;
11748 /* Get the name of the namespace. */
11749 identifier = cp_parser_identifier (parser);
11750 if (identifier == error_mark_node)
11751 return error_mark_node;
11753 /* Look up the identifier in the currently active scope. Look only
11754 for namespaces, due to:
11756 [basic.lookup.udir]
11758 When looking up a namespace-name in a using-directive or alias
11759 definition, only namespace names are considered.
11761 And:
11763 [basic.lookup.qual]
11765 During the lookup of a name preceding the :: scope resolution
11766 operator, object, function, and enumerator names are ignored.
11768 (Note that cp_parser_class_or_namespace_name only calls this
11769 function if the token after the name is the scope resolution
11770 operator.) */
11771 namespace_decl = cp_parser_lookup_name (parser, identifier,
11772 none_type,
11773 /*is_template=*/false,
11774 /*is_namespace=*/true,
11775 /*check_dependency=*/true,
11776 /*ambiguous_decls=*/NULL);
11777 /* If it's not a namespace, issue an error. */
11778 if (namespace_decl == error_mark_node
11779 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11781 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11782 error ("%qD is not a namespace-name", identifier);
11783 cp_parser_error (parser, "expected namespace-name");
11784 namespace_decl = error_mark_node;
11787 return namespace_decl;
11790 /* Parse a namespace-definition.
11792 namespace-definition:
11793 named-namespace-definition
11794 unnamed-namespace-definition
11796 named-namespace-definition:
11797 original-namespace-definition
11798 extension-namespace-definition
11800 original-namespace-definition:
11801 namespace identifier { namespace-body }
11803 extension-namespace-definition:
11804 namespace original-namespace-name { namespace-body }
11806 unnamed-namespace-definition:
11807 namespace { namespace-body } */
11809 static void
11810 cp_parser_namespace_definition (cp_parser* parser)
11812 tree identifier, attribs;
11813 bool has_visibility;
11815 /* Look for the `namespace' keyword. */
11816 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11818 /* Get the name of the namespace. We do not attempt to distinguish
11819 between an original-namespace-definition and an
11820 extension-namespace-definition at this point. The semantic
11821 analysis routines are responsible for that. */
11822 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11823 identifier = cp_parser_identifier (parser);
11824 else
11825 identifier = NULL_TREE;
11827 /* Parse any specified attributes. */
11828 attribs = cp_parser_attributes_opt (parser);
11830 /* Look for the `{' to start the namespace. */
11831 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11832 /* Start the namespace. */
11833 push_namespace (identifier);
11835 has_visibility = handle_namespace_attrs (current_namespace, attribs);
11837 /* Parse the body of the namespace. */
11838 cp_parser_namespace_body (parser);
11840 #ifdef HANDLE_PRAGMA_VISIBILITY
11841 if (has_visibility)
11842 pop_visibility ();
11843 #endif
11845 /* Finish the namespace. */
11846 pop_namespace ();
11847 /* Look for the final `}'. */
11848 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11851 /* Parse a namespace-body.
11853 namespace-body:
11854 declaration-seq [opt] */
11856 static void
11857 cp_parser_namespace_body (cp_parser* parser)
11859 cp_parser_declaration_seq_opt (parser);
11862 /* Parse a namespace-alias-definition.
11864 namespace-alias-definition:
11865 namespace identifier = qualified-namespace-specifier ; */
11867 static void
11868 cp_parser_namespace_alias_definition (cp_parser* parser)
11870 tree identifier;
11871 tree namespace_specifier;
11873 /* Look for the `namespace' keyword. */
11874 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11875 /* Look for the identifier. */
11876 identifier = cp_parser_identifier (parser);
11877 if (identifier == error_mark_node)
11878 return;
11879 /* Look for the `=' token. */
11880 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11881 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11883 error ("%<namespace%> definition is not allowed here");
11884 /* Skip the definition. */
11885 cp_lexer_consume_token (parser->lexer);
11886 if (cp_parser_skip_to_closing_brace (parser))
11887 cp_lexer_consume_token (parser->lexer);
11888 return;
11890 cp_parser_require (parser, CPP_EQ, "`='");
11891 /* Look for the qualified-namespace-specifier. */
11892 namespace_specifier
11893 = cp_parser_qualified_namespace_specifier (parser);
11894 /* Look for the `;' token. */
11895 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11897 /* Register the alias in the symbol table. */
11898 do_namespace_alias (identifier, namespace_specifier);
11901 /* Parse a qualified-namespace-specifier.
11903 qualified-namespace-specifier:
11904 :: [opt] nested-name-specifier [opt] namespace-name
11906 Returns a NAMESPACE_DECL corresponding to the specified
11907 namespace. */
11909 static tree
11910 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11912 /* Look for the optional `::'. */
11913 cp_parser_global_scope_opt (parser,
11914 /*current_scope_valid_p=*/false);
11916 /* Look for the optional nested-name-specifier. */
11917 cp_parser_nested_name_specifier_opt (parser,
11918 /*typename_keyword_p=*/false,
11919 /*check_dependency_p=*/true,
11920 /*type_p=*/false,
11921 /*is_declaration=*/true);
11923 return cp_parser_namespace_name (parser);
11926 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11927 access declaration.
11929 using-declaration:
11930 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11931 using :: unqualified-id ;
11933 access-declaration:
11934 qualified-id ;
11938 static bool
11939 cp_parser_using_declaration (cp_parser* parser,
11940 bool access_declaration_p)
11942 cp_token *token;
11943 bool typename_p = false;
11944 bool global_scope_p;
11945 tree decl;
11946 tree identifier;
11947 tree qscope;
11949 if (access_declaration_p)
11950 cp_parser_parse_tentatively (parser);
11951 else
11953 /* Look for the `using' keyword. */
11954 cp_parser_require_keyword (parser, RID_USING, "`using'");
11956 /* Peek at the next token. */
11957 token = cp_lexer_peek_token (parser->lexer);
11958 /* See if it's `typename'. */
11959 if (token->keyword == RID_TYPENAME)
11961 /* Remember that we've seen it. */
11962 typename_p = true;
11963 /* Consume the `typename' token. */
11964 cp_lexer_consume_token (parser->lexer);
11968 /* Look for the optional global scope qualification. */
11969 global_scope_p
11970 = (cp_parser_global_scope_opt (parser,
11971 /*current_scope_valid_p=*/false)
11972 != NULL_TREE);
11974 /* If we saw `typename', or didn't see `::', then there must be a
11975 nested-name-specifier present. */
11976 if (typename_p || !global_scope_p)
11977 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11978 /*check_dependency_p=*/true,
11979 /*type_p=*/false,
11980 /*is_declaration=*/true);
11981 /* Otherwise, we could be in either of the two productions. In that
11982 case, treat the nested-name-specifier as optional. */
11983 else
11984 qscope = cp_parser_nested_name_specifier_opt (parser,
11985 /*typename_keyword_p=*/false,
11986 /*check_dependency_p=*/true,
11987 /*type_p=*/false,
11988 /*is_declaration=*/true);
11989 if (!qscope)
11990 qscope = global_namespace;
11992 if (access_declaration_p && cp_parser_error_occurred (parser))
11993 /* Something has already gone wrong; there's no need to parse
11994 further. Since an error has occurred, the return value of
11995 cp_parser_parse_definitely will be false, as required. */
11996 return cp_parser_parse_definitely (parser);
11998 /* Parse the unqualified-id. */
11999 identifier = cp_parser_unqualified_id (parser,
12000 /*template_keyword_p=*/false,
12001 /*check_dependency_p=*/true,
12002 /*declarator_p=*/true,
12003 /*optional_p=*/false);
12005 if (access_declaration_p)
12007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12008 cp_parser_simulate_error (parser);
12009 if (!cp_parser_parse_definitely (parser))
12010 return false;
12013 /* The function we call to handle a using-declaration is different
12014 depending on what scope we are in. */
12015 if (qscope == error_mark_node || identifier == error_mark_node)
12017 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12018 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12019 /* [namespace.udecl]
12021 A using declaration shall not name a template-id. */
12022 error ("a template-id may not appear in a using-declaration");
12023 else
12025 if (at_class_scope_p ())
12027 /* Create the USING_DECL. */
12028 decl = do_class_using_decl (parser->scope, identifier);
12030 if (check_for_bare_parameter_packs (&decl))
12031 return false;
12032 else
12033 /* Add it to the list of members in this class. */
12034 finish_member_declaration (decl);
12036 else
12038 decl = cp_parser_lookup_name_simple (parser, identifier);
12039 if (decl == error_mark_node)
12040 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
12041 else if (check_for_bare_parameter_packs (&decl))
12042 return false;
12043 else if (!at_namespace_scope_p ())
12044 do_local_using_decl (decl, qscope, identifier);
12045 else
12046 do_toplevel_using_decl (decl, qscope, identifier);
12050 /* Look for the final `;'. */
12051 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12053 return true;
12056 /* Parse a using-directive.
12058 using-directive:
12059 using namespace :: [opt] nested-name-specifier [opt]
12060 namespace-name ; */
12062 static void
12063 cp_parser_using_directive (cp_parser* parser)
12065 tree namespace_decl;
12066 tree attribs;
12068 /* Look for the `using' keyword. */
12069 cp_parser_require_keyword (parser, RID_USING, "`using'");
12070 /* And the `namespace' keyword. */
12071 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
12072 /* Look for the optional `::' operator. */
12073 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12074 /* And the optional nested-name-specifier. */
12075 cp_parser_nested_name_specifier_opt (parser,
12076 /*typename_keyword_p=*/false,
12077 /*check_dependency_p=*/true,
12078 /*type_p=*/false,
12079 /*is_declaration=*/true);
12080 /* Get the namespace being used. */
12081 namespace_decl = cp_parser_namespace_name (parser);
12082 /* And any specified attributes. */
12083 attribs = cp_parser_attributes_opt (parser);
12084 /* Update the symbol table. */
12085 parse_using_directive (namespace_decl, attribs);
12086 /* Look for the final `;'. */
12087 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12090 /* Parse an asm-definition.
12092 asm-definition:
12093 asm ( string-literal ) ;
12095 GNU Extension:
12097 asm-definition:
12098 asm volatile [opt] ( string-literal ) ;
12099 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12100 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12101 : asm-operand-list [opt] ) ;
12102 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12103 : asm-operand-list [opt]
12104 : asm-operand-list [opt] ) ; */
12106 static void
12107 cp_parser_asm_definition (cp_parser* parser)
12109 tree string;
12110 tree outputs = NULL_TREE;
12111 tree inputs = NULL_TREE;
12112 tree clobbers = NULL_TREE;
12113 tree asm_stmt;
12114 bool volatile_p = false;
12115 bool extended_p = false;
12116 bool invalid_inputs_p = false;
12117 bool invalid_outputs_p = false;
12119 /* Look for the `asm' keyword. */
12120 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
12121 /* See if the next token is `volatile'. */
12122 if (cp_parser_allow_gnu_extensions_p (parser)
12123 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12125 /* Remember that we saw the `volatile' keyword. */
12126 volatile_p = true;
12127 /* Consume the token. */
12128 cp_lexer_consume_token (parser->lexer);
12130 /* Look for the opening `('. */
12131 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
12132 return;
12133 /* Look for the string. */
12134 string = cp_parser_string_literal (parser, false, false);
12135 if (string == error_mark_node)
12137 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12138 /*consume_paren=*/true);
12139 return;
12142 /* If we're allowing GNU extensions, check for the extended assembly
12143 syntax. Unfortunately, the `:' tokens need not be separated by
12144 a space in C, and so, for compatibility, we tolerate that here
12145 too. Doing that means that we have to treat the `::' operator as
12146 two `:' tokens. */
12147 if (cp_parser_allow_gnu_extensions_p (parser)
12148 && parser->in_function_body
12149 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12150 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12152 bool inputs_p = false;
12153 bool clobbers_p = false;
12155 /* The extended syntax was used. */
12156 extended_p = true;
12158 /* Look for outputs. */
12159 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12161 /* Consume the `:'. */
12162 cp_lexer_consume_token (parser->lexer);
12163 /* Parse the output-operands. */
12164 if (cp_lexer_next_token_is_not (parser->lexer,
12165 CPP_COLON)
12166 && cp_lexer_next_token_is_not (parser->lexer,
12167 CPP_SCOPE)
12168 && cp_lexer_next_token_is_not (parser->lexer,
12169 CPP_CLOSE_PAREN))
12170 outputs = cp_parser_asm_operand_list (parser);
12172 if (outputs == error_mark_node)
12173 invalid_outputs_p = true;
12175 /* If the next token is `::', there are no outputs, and the
12176 next token is the beginning of the inputs. */
12177 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12178 /* The inputs are coming next. */
12179 inputs_p = true;
12181 /* Look for inputs. */
12182 if (inputs_p
12183 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12185 /* Consume the `:' or `::'. */
12186 cp_lexer_consume_token (parser->lexer);
12187 /* Parse the output-operands. */
12188 if (cp_lexer_next_token_is_not (parser->lexer,
12189 CPP_COLON)
12190 && cp_lexer_next_token_is_not (parser->lexer,
12191 CPP_CLOSE_PAREN))
12192 inputs = cp_parser_asm_operand_list (parser);
12194 if (inputs == error_mark_node)
12195 invalid_inputs_p = true;
12197 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12198 /* The clobbers are coming next. */
12199 clobbers_p = true;
12201 /* Look for clobbers. */
12202 if (clobbers_p
12203 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12205 /* Consume the `:' or `::'. */
12206 cp_lexer_consume_token (parser->lexer);
12207 /* Parse the clobbers. */
12208 if (cp_lexer_next_token_is_not (parser->lexer,
12209 CPP_CLOSE_PAREN))
12210 clobbers = cp_parser_asm_clobber_list (parser);
12213 /* Look for the closing `)'. */
12214 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12215 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12216 /*consume_paren=*/true);
12217 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12219 if (!invalid_inputs_p && !invalid_outputs_p)
12221 /* Create the ASM_EXPR. */
12222 if (parser->in_function_body)
12224 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12225 inputs, clobbers);
12226 /* If the extended syntax was not used, mark the ASM_EXPR. */
12227 if (!extended_p)
12229 tree temp = asm_stmt;
12230 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12231 temp = TREE_OPERAND (temp, 0);
12233 ASM_INPUT_P (temp) = 1;
12236 else
12237 cgraph_add_asm_node (string);
12241 /* Declarators [gram.dcl.decl] */
12243 /* Parse an init-declarator.
12245 init-declarator:
12246 declarator initializer [opt]
12248 GNU Extension:
12250 init-declarator:
12251 declarator asm-specification [opt] attributes [opt] initializer [opt]
12253 function-definition:
12254 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12255 function-body
12256 decl-specifier-seq [opt] declarator function-try-block
12258 GNU Extension:
12260 function-definition:
12261 __extension__ function-definition
12263 The DECL_SPECIFIERS apply to this declarator. Returns a
12264 representation of the entity declared. If MEMBER_P is TRUE, then
12265 this declarator appears in a class scope. The new DECL created by
12266 this declarator is returned.
12268 The CHECKS are access checks that should be performed once we know
12269 what entity is being declared (and, therefore, what classes have
12270 befriended it).
12272 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12273 for a function-definition here as well. If the declarator is a
12274 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12275 be TRUE upon return. By that point, the function-definition will
12276 have been completely parsed.
12278 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12279 is FALSE. */
12281 static tree
12282 cp_parser_init_declarator (cp_parser* parser,
12283 cp_decl_specifier_seq *decl_specifiers,
12284 VEC (deferred_access_check,gc)* checks,
12285 bool function_definition_allowed_p,
12286 bool member_p,
12287 int declares_class_or_enum,
12288 bool* function_definition_p)
12290 cp_token *token;
12291 cp_declarator *declarator;
12292 tree prefix_attributes;
12293 tree attributes;
12294 tree asm_specification;
12295 tree initializer;
12296 tree decl = NULL_TREE;
12297 tree scope;
12298 bool is_initialized;
12299 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12300 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12301 "(...)". */
12302 enum cpp_ttype initialization_kind;
12303 bool is_parenthesized_init = false;
12304 bool is_non_constant_init;
12305 int ctor_dtor_or_conv_p;
12306 bool friend_p;
12307 tree pushed_scope = NULL;
12309 /* Gather the attributes that were provided with the
12310 decl-specifiers. */
12311 prefix_attributes = decl_specifiers->attributes;
12313 /* Assume that this is not the declarator for a function
12314 definition. */
12315 if (function_definition_p)
12316 *function_definition_p = false;
12318 /* Defer access checks while parsing the declarator; we cannot know
12319 what names are accessible until we know what is being
12320 declared. */
12321 resume_deferring_access_checks ();
12323 /* Parse the declarator. */
12324 declarator
12325 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12326 &ctor_dtor_or_conv_p,
12327 /*parenthesized_p=*/NULL,
12328 /*member_p=*/false);
12329 /* Gather up the deferred checks. */
12330 stop_deferring_access_checks ();
12332 /* If the DECLARATOR was erroneous, there's no need to go
12333 further. */
12334 if (declarator == cp_error_declarator)
12335 return error_mark_node;
12337 /* Check that the number of template-parameter-lists is OK. */
12338 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12339 return error_mark_node;
12341 if (declares_class_or_enum & 2)
12342 cp_parser_check_for_definition_in_return_type (declarator,
12343 decl_specifiers->type);
12345 /* Figure out what scope the entity declared by the DECLARATOR is
12346 located in. `grokdeclarator' sometimes changes the scope, so
12347 we compute it now. */
12348 scope = get_scope_of_declarator (declarator);
12350 /* If we're allowing GNU extensions, look for an asm-specification
12351 and attributes. */
12352 if (cp_parser_allow_gnu_extensions_p (parser))
12354 /* Look for an asm-specification. */
12355 asm_specification = cp_parser_asm_specification_opt (parser);
12356 /* And attributes. */
12357 attributes = cp_parser_attributes_opt (parser);
12359 else
12361 asm_specification = NULL_TREE;
12362 attributes = NULL_TREE;
12365 /* Peek at the next token. */
12366 token = cp_lexer_peek_token (parser->lexer);
12367 /* Check to see if the token indicates the start of a
12368 function-definition. */
12369 if (cp_parser_token_starts_function_definition_p (token))
12371 if (!function_definition_allowed_p)
12373 /* If a function-definition should not appear here, issue an
12374 error message. */
12375 cp_parser_error (parser,
12376 "a function-definition is not allowed here");
12377 return error_mark_node;
12379 else
12381 /* Neither attributes nor an asm-specification are allowed
12382 on a function-definition. */
12383 if (asm_specification)
12384 error ("an asm-specification is not allowed on a function-definition");
12385 if (attributes)
12386 error ("attributes are not allowed on a function-definition");
12387 /* This is a function-definition. */
12388 *function_definition_p = true;
12390 /* Parse the function definition. */
12391 if (member_p)
12392 decl = cp_parser_save_member_function_body (parser,
12393 decl_specifiers,
12394 declarator,
12395 prefix_attributes);
12396 else
12397 decl
12398 = (cp_parser_function_definition_from_specifiers_and_declarator
12399 (parser, decl_specifiers, prefix_attributes, declarator));
12401 return decl;
12405 /* [dcl.dcl]
12407 Only in function declarations for constructors, destructors, and
12408 type conversions can the decl-specifier-seq be omitted.
12410 We explicitly postpone this check past the point where we handle
12411 function-definitions because we tolerate function-definitions
12412 that are missing their return types in some modes. */
12413 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12415 cp_parser_error (parser,
12416 "expected constructor, destructor, or type conversion");
12417 return error_mark_node;
12420 /* An `=' or an `(' indicates an initializer. */
12421 if (token->type == CPP_EQ
12422 || token->type == CPP_OPEN_PAREN)
12424 is_initialized = true;
12425 initialization_kind = token->type;
12427 else
12429 /* If the init-declarator isn't initialized and isn't followed by a
12430 `,' or `;', it's not a valid init-declarator. */
12431 if (token->type != CPP_COMMA
12432 && token->type != CPP_SEMICOLON)
12434 cp_parser_error (parser, "expected initializer");
12435 return error_mark_node;
12437 is_initialized = false;
12438 initialization_kind = CPP_EOF;
12441 /* Because start_decl has side-effects, we should only call it if we
12442 know we're going ahead. By this point, we know that we cannot
12443 possibly be looking at any other construct. */
12444 cp_parser_commit_to_tentative_parse (parser);
12446 /* If the decl specifiers were bad, issue an error now that we're
12447 sure this was intended to be a declarator. Then continue
12448 declaring the variable(s), as int, to try to cut down on further
12449 errors. */
12450 if (decl_specifiers->any_specifiers_p
12451 && decl_specifiers->type == error_mark_node)
12453 cp_parser_error (parser, "invalid type in declaration");
12454 decl_specifiers->type = integer_type_node;
12457 /* Check to see whether or not this declaration is a friend. */
12458 friend_p = cp_parser_friend_p (decl_specifiers);
12460 /* Enter the newly declared entry in the symbol table. If we're
12461 processing a declaration in a class-specifier, we wait until
12462 after processing the initializer. */
12463 if (!member_p)
12465 if (parser->in_unbraced_linkage_specification_p)
12466 decl_specifiers->storage_class = sc_extern;
12467 decl = start_decl (declarator, decl_specifiers,
12468 is_initialized, attributes, prefix_attributes,
12469 &pushed_scope);
12471 else if (scope)
12472 /* Enter the SCOPE. That way unqualified names appearing in the
12473 initializer will be looked up in SCOPE. */
12474 pushed_scope = push_scope (scope);
12476 /* Perform deferred access control checks, now that we know in which
12477 SCOPE the declared entity resides. */
12478 if (!member_p && decl)
12480 tree saved_current_function_decl = NULL_TREE;
12482 /* If the entity being declared is a function, pretend that we
12483 are in its scope. If it is a `friend', it may have access to
12484 things that would not otherwise be accessible. */
12485 if (TREE_CODE (decl) == FUNCTION_DECL)
12487 saved_current_function_decl = current_function_decl;
12488 current_function_decl = decl;
12491 /* Perform access checks for template parameters. */
12492 cp_parser_perform_template_parameter_access_checks (checks);
12494 /* Perform the access control checks for the declarator and the
12495 the decl-specifiers. */
12496 perform_deferred_access_checks ();
12498 /* Restore the saved value. */
12499 if (TREE_CODE (decl) == FUNCTION_DECL)
12500 current_function_decl = saved_current_function_decl;
12503 /* Parse the initializer. */
12504 initializer = NULL_TREE;
12505 is_parenthesized_init = false;
12506 is_non_constant_init = true;
12507 if (is_initialized)
12509 if (function_declarator_p (declarator))
12511 if (initialization_kind == CPP_EQ)
12512 initializer = cp_parser_pure_specifier (parser);
12513 else
12515 /* If the declaration was erroneous, we don't really
12516 know what the user intended, so just silently
12517 consume the initializer. */
12518 if (decl != error_mark_node)
12519 error ("initializer provided for function");
12520 cp_parser_skip_to_closing_parenthesis (parser,
12521 /*recovering=*/true,
12522 /*or_comma=*/false,
12523 /*consume_paren=*/true);
12526 else
12527 initializer = cp_parser_initializer (parser,
12528 &is_parenthesized_init,
12529 &is_non_constant_init);
12532 /* The old parser allows attributes to appear after a parenthesized
12533 initializer. Mark Mitchell proposed removing this functionality
12534 on the GCC mailing lists on 2002-08-13. This parser accepts the
12535 attributes -- but ignores them. */
12536 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12537 if (cp_parser_attributes_opt (parser))
12538 warning (OPT_Wattributes,
12539 "attributes after parenthesized initializer ignored");
12541 /* For an in-class declaration, use `grokfield' to create the
12542 declaration. */
12543 if (member_p)
12545 if (pushed_scope)
12547 pop_scope (pushed_scope);
12548 pushed_scope = false;
12550 decl = grokfield (declarator, decl_specifiers,
12551 initializer, !is_non_constant_init,
12552 /*asmspec=*/NULL_TREE,
12553 prefix_attributes);
12554 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12555 cp_parser_save_default_args (parser, decl);
12558 /* Finish processing the declaration. But, skip friend
12559 declarations. */
12560 if (!friend_p && decl && decl != error_mark_node)
12562 cp_finish_decl (decl,
12563 initializer, !is_non_constant_init,
12564 asm_specification,
12565 /* If the initializer is in parentheses, then this is
12566 a direct-initialization, which means that an
12567 `explicit' constructor is OK. Otherwise, an
12568 `explicit' constructor cannot be used. */
12569 ((is_parenthesized_init || !is_initialized)
12570 ? 0 : LOOKUP_ONLYCONVERTING));
12572 else if ((cxx_dialect != cxx98) && friend_p
12573 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12574 /* Core issue #226 (C++0x only): A default template-argument
12575 shall not be specified in a friend class template
12576 declaration. */
12577 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12578 /*is_partial=*/0, /*is_friend_decl=*/1);
12580 if (!friend_p && pushed_scope)
12581 pop_scope (pushed_scope);
12583 return decl;
12586 /* Parse a declarator.
12588 declarator:
12589 direct-declarator
12590 ptr-operator declarator
12592 abstract-declarator:
12593 ptr-operator abstract-declarator [opt]
12594 direct-abstract-declarator
12596 GNU Extensions:
12598 declarator:
12599 attributes [opt] direct-declarator
12600 attributes [opt] ptr-operator declarator
12602 abstract-declarator:
12603 attributes [opt] ptr-operator abstract-declarator [opt]
12604 attributes [opt] direct-abstract-declarator
12606 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12607 detect constructor, destructor or conversion operators. It is set
12608 to -1 if the declarator is a name, and +1 if it is a
12609 function. Otherwise it is set to zero. Usually you just want to
12610 test for >0, but internally the negative value is used.
12612 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12613 a decl-specifier-seq unless it declares a constructor, destructor,
12614 or conversion. It might seem that we could check this condition in
12615 semantic analysis, rather than parsing, but that makes it difficult
12616 to handle something like `f()'. We want to notice that there are
12617 no decl-specifiers, and therefore realize that this is an
12618 expression, not a declaration.)
12620 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12621 the declarator is a direct-declarator of the form "(...)".
12623 MEMBER_P is true iff this declarator is a member-declarator. */
12625 static cp_declarator *
12626 cp_parser_declarator (cp_parser* parser,
12627 cp_parser_declarator_kind dcl_kind,
12628 int* ctor_dtor_or_conv_p,
12629 bool* parenthesized_p,
12630 bool member_p)
12632 cp_token *token;
12633 cp_declarator *declarator;
12634 enum tree_code code;
12635 cp_cv_quals cv_quals;
12636 tree class_type;
12637 tree attributes = NULL_TREE;
12639 /* Assume this is not a constructor, destructor, or type-conversion
12640 operator. */
12641 if (ctor_dtor_or_conv_p)
12642 *ctor_dtor_or_conv_p = 0;
12644 if (cp_parser_allow_gnu_extensions_p (parser))
12645 attributes = cp_parser_attributes_opt (parser);
12647 /* Peek at the next token. */
12648 token = cp_lexer_peek_token (parser->lexer);
12650 /* Check for the ptr-operator production. */
12651 cp_parser_parse_tentatively (parser);
12652 /* Parse the ptr-operator. */
12653 code = cp_parser_ptr_operator (parser,
12654 &class_type,
12655 &cv_quals);
12656 /* If that worked, then we have a ptr-operator. */
12657 if (cp_parser_parse_definitely (parser))
12659 /* If a ptr-operator was found, then this declarator was not
12660 parenthesized. */
12661 if (parenthesized_p)
12662 *parenthesized_p = true;
12663 /* The dependent declarator is optional if we are parsing an
12664 abstract-declarator. */
12665 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12666 cp_parser_parse_tentatively (parser);
12668 /* Parse the dependent declarator. */
12669 declarator = cp_parser_declarator (parser, dcl_kind,
12670 /*ctor_dtor_or_conv_p=*/NULL,
12671 /*parenthesized_p=*/NULL,
12672 /*member_p=*/false);
12674 /* If we are parsing an abstract-declarator, we must handle the
12675 case where the dependent declarator is absent. */
12676 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12677 && !cp_parser_parse_definitely (parser))
12678 declarator = NULL;
12680 declarator = cp_parser_make_indirect_declarator
12681 (code, class_type, cv_quals, declarator);
12683 /* Everything else is a direct-declarator. */
12684 else
12686 if (parenthesized_p)
12687 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12688 CPP_OPEN_PAREN);
12689 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12690 ctor_dtor_or_conv_p,
12691 member_p);
12694 if (attributes && declarator && declarator != cp_error_declarator)
12695 declarator->attributes = attributes;
12697 return declarator;
12700 /* Parse a direct-declarator or direct-abstract-declarator.
12702 direct-declarator:
12703 declarator-id
12704 direct-declarator ( parameter-declaration-clause )
12705 cv-qualifier-seq [opt]
12706 exception-specification [opt]
12707 direct-declarator [ constant-expression [opt] ]
12708 ( declarator )
12710 direct-abstract-declarator:
12711 direct-abstract-declarator [opt]
12712 ( parameter-declaration-clause )
12713 cv-qualifier-seq [opt]
12714 exception-specification [opt]
12715 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12716 ( abstract-declarator )
12718 Returns a representation of the declarator. DCL_KIND is
12719 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12720 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12721 we are parsing a direct-declarator. It is
12722 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12723 of ambiguity we prefer an abstract declarator, as per
12724 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12725 cp_parser_declarator. */
12727 static cp_declarator *
12728 cp_parser_direct_declarator (cp_parser* parser,
12729 cp_parser_declarator_kind dcl_kind,
12730 int* ctor_dtor_or_conv_p,
12731 bool member_p)
12733 cp_token *token;
12734 cp_declarator *declarator = NULL;
12735 tree scope = NULL_TREE;
12736 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12737 bool saved_in_declarator_p = parser->in_declarator_p;
12738 bool first = true;
12739 tree pushed_scope = NULL_TREE;
12741 while (true)
12743 /* Peek at the next token. */
12744 token = cp_lexer_peek_token (parser->lexer);
12745 if (token->type == CPP_OPEN_PAREN)
12747 /* This is either a parameter-declaration-clause, or a
12748 parenthesized declarator. When we know we are parsing a
12749 named declarator, it must be a parenthesized declarator
12750 if FIRST is true. For instance, `(int)' is a
12751 parameter-declaration-clause, with an omitted
12752 direct-abstract-declarator. But `((*))', is a
12753 parenthesized abstract declarator. Finally, when T is a
12754 template parameter `(T)' is a
12755 parameter-declaration-clause, and not a parenthesized
12756 named declarator.
12758 We first try and parse a parameter-declaration-clause,
12759 and then try a nested declarator (if FIRST is true).
12761 It is not an error for it not to be a
12762 parameter-declaration-clause, even when FIRST is
12763 false. Consider,
12765 int i (int);
12766 int i (3);
12768 The first is the declaration of a function while the
12769 second is a the definition of a variable, including its
12770 initializer.
12772 Having seen only the parenthesis, we cannot know which of
12773 these two alternatives should be selected. Even more
12774 complex are examples like:
12776 int i (int (a));
12777 int i (int (3));
12779 The former is a function-declaration; the latter is a
12780 variable initialization.
12782 Thus again, we try a parameter-declaration-clause, and if
12783 that fails, we back out and return. */
12785 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12787 cp_parameter_declarator *params;
12788 unsigned saved_num_template_parameter_lists;
12790 /* In a member-declarator, the only valid interpretation
12791 of a parenthesis is the start of a
12792 parameter-declaration-clause. (It is invalid to
12793 initialize a static data member with a parenthesized
12794 initializer; only the "=" form of initialization is
12795 permitted.) */
12796 if (!member_p)
12797 cp_parser_parse_tentatively (parser);
12799 /* Consume the `('. */
12800 cp_lexer_consume_token (parser->lexer);
12801 if (first)
12803 /* If this is going to be an abstract declarator, we're
12804 in a declarator and we can't have default args. */
12805 parser->default_arg_ok_p = false;
12806 parser->in_declarator_p = true;
12809 /* Inside the function parameter list, surrounding
12810 template-parameter-lists do not apply. */
12811 saved_num_template_parameter_lists
12812 = parser->num_template_parameter_lists;
12813 parser->num_template_parameter_lists = 0;
12815 /* Parse the parameter-declaration-clause. */
12816 params = cp_parser_parameter_declaration_clause (parser);
12818 parser->num_template_parameter_lists
12819 = saved_num_template_parameter_lists;
12821 /* If all went well, parse the cv-qualifier-seq and the
12822 exception-specification. */
12823 if (member_p || cp_parser_parse_definitely (parser))
12825 cp_cv_quals cv_quals;
12826 tree exception_specification;
12828 if (ctor_dtor_or_conv_p)
12829 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12830 first = false;
12831 /* Consume the `)'. */
12832 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12834 /* Parse the cv-qualifier-seq. */
12835 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12836 /* And the exception-specification. */
12837 exception_specification
12838 = cp_parser_exception_specification_opt (parser);
12840 /* Create the function-declarator. */
12841 declarator = make_call_declarator (declarator,
12842 params,
12843 cv_quals,
12844 exception_specification);
12845 /* Any subsequent parameter lists are to do with
12846 return type, so are not those of the declared
12847 function. */
12848 parser->default_arg_ok_p = false;
12850 /* Repeat the main loop. */
12851 continue;
12855 /* If this is the first, we can try a parenthesized
12856 declarator. */
12857 if (first)
12859 bool saved_in_type_id_in_expr_p;
12861 parser->default_arg_ok_p = saved_default_arg_ok_p;
12862 parser->in_declarator_p = saved_in_declarator_p;
12864 /* Consume the `('. */
12865 cp_lexer_consume_token (parser->lexer);
12866 /* Parse the nested declarator. */
12867 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12868 parser->in_type_id_in_expr_p = true;
12869 declarator
12870 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12871 /*parenthesized_p=*/NULL,
12872 member_p);
12873 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12874 first = false;
12875 /* Expect a `)'. */
12876 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12877 declarator = cp_error_declarator;
12878 if (declarator == cp_error_declarator)
12879 break;
12881 goto handle_declarator;
12883 /* Otherwise, we must be done. */
12884 else
12885 break;
12887 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12888 && token->type == CPP_OPEN_SQUARE)
12890 /* Parse an array-declarator. */
12891 tree bounds;
12893 if (ctor_dtor_or_conv_p)
12894 *ctor_dtor_or_conv_p = 0;
12896 first = false;
12897 parser->default_arg_ok_p = false;
12898 parser->in_declarator_p = true;
12899 /* Consume the `['. */
12900 cp_lexer_consume_token (parser->lexer);
12901 /* Peek at the next token. */
12902 token = cp_lexer_peek_token (parser->lexer);
12903 /* If the next token is `]', then there is no
12904 constant-expression. */
12905 if (token->type != CPP_CLOSE_SQUARE)
12907 bool non_constant_p;
12909 bounds
12910 = cp_parser_constant_expression (parser,
12911 /*allow_non_constant=*/true,
12912 &non_constant_p);
12913 if (!non_constant_p)
12914 bounds = fold_non_dependent_expr (bounds);
12915 /* Normally, the array bound must be an integral constant
12916 expression. However, as an extension, we allow VLAs
12917 in function scopes. */
12918 else if (!parser->in_function_body)
12920 error ("array bound is not an integer constant");
12921 bounds = error_mark_node;
12924 else
12925 bounds = NULL_TREE;
12926 /* Look for the closing `]'. */
12927 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12929 declarator = cp_error_declarator;
12930 break;
12933 declarator = make_array_declarator (declarator, bounds);
12935 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12937 tree qualifying_scope;
12938 tree unqualified_name;
12939 special_function_kind sfk;
12940 bool abstract_ok;
12941 bool pack_expansion_p = false;
12943 /* Parse a declarator-id */
12944 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12945 if (abstract_ok)
12947 cp_parser_parse_tentatively (parser);
12949 /* If we see an ellipsis, we should be looking at a
12950 parameter pack. */
12951 if (token->type == CPP_ELLIPSIS)
12953 /* Consume the `...' */
12954 cp_lexer_consume_token (parser->lexer);
12956 pack_expansion_p = true;
12960 unqualified_name
12961 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12962 qualifying_scope = parser->scope;
12963 if (abstract_ok)
12965 bool okay = false;
12967 if (!unqualified_name && pack_expansion_p)
12969 /* Check whether an error occurred. */
12970 okay = !cp_parser_error_occurred (parser);
12972 /* We already consumed the ellipsis to mark a
12973 parameter pack, but we have no way to report it,
12974 so abort the tentative parse. We will be exiting
12975 immediately anyway. */
12976 cp_parser_abort_tentative_parse (parser);
12978 else
12979 okay = cp_parser_parse_definitely (parser);
12981 if (!okay)
12982 unqualified_name = error_mark_node;
12983 else if (unqualified_name
12984 && (qualifying_scope
12985 || (TREE_CODE (unqualified_name)
12986 != IDENTIFIER_NODE)))
12988 cp_parser_error (parser, "expected unqualified-id");
12989 unqualified_name = error_mark_node;
12993 if (!unqualified_name)
12994 return NULL;
12995 if (unqualified_name == error_mark_node)
12997 declarator = cp_error_declarator;
12998 pack_expansion_p = false;
12999 declarator->parameter_pack_p = false;
13000 break;
13003 if (qualifying_scope && at_namespace_scope_p ()
13004 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13006 /* In the declaration of a member of a template class
13007 outside of the class itself, the SCOPE will sometimes
13008 be a TYPENAME_TYPE. For example, given:
13010 template <typename T>
13011 int S<T>::R::i = 3;
13013 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13014 this context, we must resolve S<T>::R to an ordinary
13015 type, rather than a typename type.
13017 The reason we normally avoid resolving TYPENAME_TYPEs
13018 is that a specialization of `S' might render
13019 `S<T>::R' not a type. However, if `S' is
13020 specialized, then this `i' will not be used, so there
13021 is no harm in resolving the types here. */
13022 tree type;
13024 /* Resolve the TYPENAME_TYPE. */
13025 type = resolve_typename_type (qualifying_scope,
13026 /*only_current_p=*/false);
13027 /* If that failed, the declarator is invalid. */
13028 if (TREE_CODE (type) == TYPENAME_TYPE)
13029 error ("%<%T::%E%> is not a type",
13030 TYPE_CONTEXT (qualifying_scope),
13031 TYPE_IDENTIFIER (qualifying_scope));
13032 qualifying_scope = type;
13035 sfk = sfk_none;
13037 if (unqualified_name)
13039 tree class_type;
13041 if (qualifying_scope
13042 && CLASS_TYPE_P (qualifying_scope))
13043 class_type = qualifying_scope;
13044 else
13045 class_type = current_class_type;
13047 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13049 tree name_type = TREE_TYPE (unqualified_name);
13050 if (class_type && same_type_p (name_type, class_type))
13052 if (qualifying_scope
13053 && CLASSTYPE_USE_TEMPLATE (name_type))
13055 error ("invalid use of constructor as a template");
13056 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
13057 "name the constructor in a qualified name",
13058 class_type,
13059 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13060 class_type, name_type);
13061 declarator = cp_error_declarator;
13062 break;
13064 else
13065 unqualified_name = constructor_name (class_type);
13067 else
13069 /* We do not attempt to print the declarator
13070 here because we do not have enough
13071 information about its original syntactic
13072 form. */
13073 cp_parser_error (parser, "invalid declarator");
13074 declarator = cp_error_declarator;
13075 break;
13079 if (class_type)
13081 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13082 sfk = sfk_destructor;
13083 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13084 sfk = sfk_conversion;
13085 else if (/* There's no way to declare a constructor
13086 for an anonymous type, even if the type
13087 got a name for linkage purposes. */
13088 !TYPE_WAS_ANONYMOUS (class_type)
13089 && constructor_name_p (unqualified_name,
13090 class_type))
13092 unqualified_name = constructor_name (class_type);
13093 sfk = sfk_constructor;
13096 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13097 *ctor_dtor_or_conv_p = -1;
13100 declarator = make_id_declarator (qualifying_scope,
13101 unqualified_name,
13102 sfk);
13103 declarator->id_loc = token->location;
13104 declarator->parameter_pack_p = pack_expansion_p;
13106 if (pack_expansion_p)
13107 maybe_warn_variadic_templates ();
13109 handle_declarator:;
13110 scope = get_scope_of_declarator (declarator);
13111 if (scope)
13112 /* Any names that appear after the declarator-id for a
13113 member are looked up in the containing scope. */
13114 pushed_scope = push_scope (scope);
13115 parser->in_declarator_p = true;
13116 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13117 || (declarator && declarator->kind == cdk_id))
13118 /* Default args are only allowed on function
13119 declarations. */
13120 parser->default_arg_ok_p = saved_default_arg_ok_p;
13121 else
13122 parser->default_arg_ok_p = false;
13124 first = false;
13126 /* We're done. */
13127 else
13128 break;
13131 /* For an abstract declarator, we might wind up with nothing at this
13132 point. That's an error; the declarator is not optional. */
13133 if (!declarator)
13134 cp_parser_error (parser, "expected declarator");
13136 /* If we entered a scope, we must exit it now. */
13137 if (pushed_scope)
13138 pop_scope (pushed_scope);
13140 parser->default_arg_ok_p = saved_default_arg_ok_p;
13141 parser->in_declarator_p = saved_in_declarator_p;
13143 return declarator;
13146 /* Parse a ptr-operator.
13148 ptr-operator:
13149 * cv-qualifier-seq [opt]
13151 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13153 GNU Extension:
13155 ptr-operator:
13156 & cv-qualifier-seq [opt]
13158 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13159 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13160 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13161 filled in with the TYPE containing the member. *CV_QUALS is
13162 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13163 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13164 Note that the tree codes returned by this function have nothing
13165 to do with the types of trees that will be eventually be created
13166 to represent the pointer or reference type being parsed. They are
13167 just constants with suggestive names. */
13168 static enum tree_code
13169 cp_parser_ptr_operator (cp_parser* parser,
13170 tree* type,
13171 cp_cv_quals *cv_quals)
13173 enum tree_code code = ERROR_MARK;
13174 cp_token *token;
13176 /* Assume that it's not a pointer-to-member. */
13177 *type = NULL_TREE;
13178 /* And that there are no cv-qualifiers. */
13179 *cv_quals = TYPE_UNQUALIFIED;
13181 /* Peek at the next token. */
13182 token = cp_lexer_peek_token (parser->lexer);
13184 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13185 if (token->type == CPP_MULT)
13186 code = INDIRECT_REF;
13187 else if (token->type == CPP_AND)
13188 code = ADDR_EXPR;
13189 else if ((cxx_dialect != cxx98) &&
13190 token->type == CPP_AND_AND) /* C++0x only */
13191 code = NON_LVALUE_EXPR;
13193 if (code != ERROR_MARK)
13195 /* Consume the `*', `&' or `&&'. */
13196 cp_lexer_consume_token (parser->lexer);
13198 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13199 `&', if we are allowing GNU extensions. (The only qualifier
13200 that can legally appear after `&' is `restrict', but that is
13201 enforced during semantic analysis. */
13202 if (code == INDIRECT_REF
13203 || cp_parser_allow_gnu_extensions_p (parser))
13204 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13206 else
13208 /* Try the pointer-to-member case. */
13209 cp_parser_parse_tentatively (parser);
13210 /* Look for the optional `::' operator. */
13211 cp_parser_global_scope_opt (parser,
13212 /*current_scope_valid_p=*/false);
13213 /* Look for the nested-name specifier. */
13214 cp_parser_nested_name_specifier (parser,
13215 /*typename_keyword_p=*/false,
13216 /*check_dependency_p=*/true,
13217 /*type_p=*/false,
13218 /*is_declaration=*/false);
13219 /* If we found it, and the next token is a `*', then we are
13220 indeed looking at a pointer-to-member operator. */
13221 if (!cp_parser_error_occurred (parser)
13222 && cp_parser_require (parser, CPP_MULT, "`*'"))
13224 /* Indicate that the `*' operator was used. */
13225 code = INDIRECT_REF;
13227 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13228 error ("%qD is a namespace", parser->scope);
13229 else
13231 /* The type of which the member is a member is given by the
13232 current SCOPE. */
13233 *type = parser->scope;
13234 /* The next name will not be qualified. */
13235 parser->scope = NULL_TREE;
13236 parser->qualifying_scope = NULL_TREE;
13237 parser->object_scope = NULL_TREE;
13238 /* Look for the optional cv-qualifier-seq. */
13239 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13242 /* If that didn't work we don't have a ptr-operator. */
13243 if (!cp_parser_parse_definitely (parser))
13244 cp_parser_error (parser, "expected ptr-operator");
13247 return code;
13250 /* Parse an (optional) cv-qualifier-seq.
13252 cv-qualifier-seq:
13253 cv-qualifier cv-qualifier-seq [opt]
13255 cv-qualifier:
13256 const
13257 volatile
13259 GNU Extension:
13261 cv-qualifier:
13262 __restrict__
13264 Returns a bitmask representing the cv-qualifiers. */
13266 static cp_cv_quals
13267 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13269 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13271 while (true)
13273 cp_token *token;
13274 cp_cv_quals cv_qualifier;
13276 /* Peek at the next token. */
13277 token = cp_lexer_peek_token (parser->lexer);
13278 /* See if it's a cv-qualifier. */
13279 switch (token->keyword)
13281 case RID_CONST:
13282 cv_qualifier = TYPE_QUAL_CONST;
13283 break;
13285 case RID_VOLATILE:
13286 cv_qualifier = TYPE_QUAL_VOLATILE;
13287 break;
13289 case RID_RESTRICT:
13290 cv_qualifier = TYPE_QUAL_RESTRICT;
13291 break;
13293 default:
13294 cv_qualifier = TYPE_UNQUALIFIED;
13295 break;
13298 if (!cv_qualifier)
13299 break;
13301 if (cv_quals & cv_qualifier)
13303 error ("duplicate cv-qualifier");
13304 cp_lexer_purge_token (parser->lexer);
13306 else
13308 cp_lexer_consume_token (parser->lexer);
13309 cv_quals |= cv_qualifier;
13313 return cv_quals;
13316 /* Parse a declarator-id.
13318 declarator-id:
13319 id-expression
13320 :: [opt] nested-name-specifier [opt] type-name
13322 In the `id-expression' case, the value returned is as for
13323 cp_parser_id_expression if the id-expression was an unqualified-id.
13324 If the id-expression was a qualified-id, then a SCOPE_REF is
13325 returned. The first operand is the scope (either a NAMESPACE_DECL
13326 or TREE_TYPE), but the second is still just a representation of an
13327 unqualified-id. */
13329 static tree
13330 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13332 tree id;
13333 /* The expression must be an id-expression. Assume that qualified
13334 names are the names of types so that:
13336 template <class T>
13337 int S<T>::R::i = 3;
13339 will work; we must treat `S<T>::R' as the name of a type.
13340 Similarly, assume that qualified names are templates, where
13341 required, so that:
13343 template <class T>
13344 int S<T>::R<T>::i = 3;
13346 will work, too. */
13347 id = cp_parser_id_expression (parser,
13348 /*template_keyword_p=*/false,
13349 /*check_dependency_p=*/false,
13350 /*template_p=*/NULL,
13351 /*declarator_p=*/true,
13352 optional_p);
13353 if (id && BASELINK_P (id))
13354 id = BASELINK_FUNCTIONS (id);
13355 return id;
13358 /* Parse a type-id.
13360 type-id:
13361 type-specifier-seq abstract-declarator [opt]
13363 Returns the TYPE specified. */
13365 static tree
13366 cp_parser_type_id (cp_parser* parser)
13368 cp_decl_specifier_seq type_specifier_seq;
13369 cp_declarator *abstract_declarator;
13371 /* Parse the type-specifier-seq. */
13372 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13373 &type_specifier_seq);
13374 if (type_specifier_seq.type == error_mark_node)
13375 return error_mark_node;
13377 /* There might or might not be an abstract declarator. */
13378 cp_parser_parse_tentatively (parser);
13379 /* Look for the declarator. */
13380 abstract_declarator
13381 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13382 /*parenthesized_p=*/NULL,
13383 /*member_p=*/false);
13384 /* Check to see if there really was a declarator. */
13385 if (!cp_parser_parse_definitely (parser))
13386 abstract_declarator = NULL;
13388 return groktypename (&type_specifier_seq, abstract_declarator);
13391 /* Parse a type-specifier-seq.
13393 type-specifier-seq:
13394 type-specifier type-specifier-seq [opt]
13396 GNU extension:
13398 type-specifier-seq:
13399 attributes type-specifier-seq [opt]
13401 If IS_CONDITION is true, we are at the start of a "condition",
13402 e.g., we've just seen "if (".
13404 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13406 static void
13407 cp_parser_type_specifier_seq (cp_parser* parser,
13408 bool is_condition,
13409 cp_decl_specifier_seq *type_specifier_seq)
13411 bool seen_type_specifier = false;
13412 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13414 /* Clear the TYPE_SPECIFIER_SEQ. */
13415 clear_decl_specs (type_specifier_seq);
13417 /* Parse the type-specifiers and attributes. */
13418 while (true)
13420 tree type_specifier;
13421 bool is_cv_qualifier;
13423 /* Check for attributes first. */
13424 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13426 type_specifier_seq->attributes =
13427 chainon (type_specifier_seq->attributes,
13428 cp_parser_attributes_opt (parser));
13429 continue;
13432 /* Look for the type-specifier. */
13433 type_specifier = cp_parser_type_specifier (parser,
13434 flags,
13435 type_specifier_seq,
13436 /*is_declaration=*/false,
13437 NULL,
13438 &is_cv_qualifier);
13439 if (!type_specifier)
13441 /* If the first type-specifier could not be found, this is not a
13442 type-specifier-seq at all. */
13443 if (!seen_type_specifier)
13445 cp_parser_error (parser, "expected type-specifier");
13446 type_specifier_seq->type = error_mark_node;
13447 return;
13449 /* If subsequent type-specifiers could not be found, the
13450 type-specifier-seq is complete. */
13451 break;
13454 seen_type_specifier = true;
13455 /* The standard says that a condition can be:
13457 type-specifier-seq declarator = assignment-expression
13459 However, given:
13461 struct S {};
13462 if (int S = ...)
13464 we should treat the "S" as a declarator, not as a
13465 type-specifier. The standard doesn't say that explicitly for
13466 type-specifier-seq, but it does say that for
13467 decl-specifier-seq in an ordinary declaration. Perhaps it
13468 would be clearer just to allow a decl-specifier-seq here, and
13469 then add a semantic restriction that if any decl-specifiers
13470 that are not type-specifiers appear, the program is invalid. */
13471 if (is_condition && !is_cv_qualifier)
13472 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13475 cp_parser_check_decl_spec (type_specifier_seq);
13478 /* Parse a parameter-declaration-clause.
13480 parameter-declaration-clause:
13481 parameter-declaration-list [opt] ... [opt]
13482 parameter-declaration-list , ...
13484 Returns a representation for the parameter declarations. A return
13485 value of NULL indicates a parameter-declaration-clause consisting
13486 only of an ellipsis. */
13488 static cp_parameter_declarator *
13489 cp_parser_parameter_declaration_clause (cp_parser* parser)
13491 cp_parameter_declarator *parameters;
13492 cp_token *token;
13493 bool ellipsis_p;
13494 bool is_error;
13496 /* Peek at the next token. */
13497 token = cp_lexer_peek_token (parser->lexer);
13498 /* Check for trivial parameter-declaration-clauses. */
13499 if (token->type == CPP_ELLIPSIS)
13501 /* Consume the `...' token. */
13502 cp_lexer_consume_token (parser->lexer);
13503 return NULL;
13505 else if (token->type == CPP_CLOSE_PAREN)
13506 /* There are no parameters. */
13508 #ifndef NO_IMPLICIT_EXTERN_C
13509 if (in_system_header && current_class_type == NULL
13510 && current_lang_name == lang_name_c)
13511 return NULL;
13512 else
13513 #endif
13514 return no_parameters;
13516 /* Check for `(void)', too, which is a special case. */
13517 else if (token->keyword == RID_VOID
13518 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13519 == CPP_CLOSE_PAREN))
13521 /* Consume the `void' token. */
13522 cp_lexer_consume_token (parser->lexer);
13523 /* There are no parameters. */
13524 return no_parameters;
13527 /* Parse the parameter-declaration-list. */
13528 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13529 /* If a parse error occurred while parsing the
13530 parameter-declaration-list, then the entire
13531 parameter-declaration-clause is erroneous. */
13532 if (is_error)
13533 return NULL;
13535 /* Peek at the next token. */
13536 token = cp_lexer_peek_token (parser->lexer);
13537 /* If it's a `,', the clause should terminate with an ellipsis. */
13538 if (token->type == CPP_COMMA)
13540 /* Consume the `,'. */
13541 cp_lexer_consume_token (parser->lexer);
13542 /* Expect an ellipsis. */
13543 ellipsis_p
13544 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13546 /* It might also be `...' if the optional trailing `,' was
13547 omitted. */
13548 else if (token->type == CPP_ELLIPSIS)
13550 /* Consume the `...' token. */
13551 cp_lexer_consume_token (parser->lexer);
13552 /* And remember that we saw it. */
13553 ellipsis_p = true;
13555 else
13556 ellipsis_p = false;
13558 /* Finish the parameter list. */
13559 if (parameters && ellipsis_p)
13560 parameters->ellipsis_p = true;
13562 return parameters;
13565 /* Parse a parameter-declaration-list.
13567 parameter-declaration-list:
13568 parameter-declaration
13569 parameter-declaration-list , parameter-declaration
13571 Returns a representation of the parameter-declaration-list, as for
13572 cp_parser_parameter_declaration_clause. However, the
13573 `void_list_node' is never appended to the list. Upon return,
13574 *IS_ERROR will be true iff an error occurred. */
13576 static cp_parameter_declarator *
13577 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13579 cp_parameter_declarator *parameters = NULL;
13580 cp_parameter_declarator **tail = &parameters;
13581 bool saved_in_unbraced_linkage_specification_p;
13583 /* Assume all will go well. */
13584 *is_error = false;
13585 /* The special considerations that apply to a function within an
13586 unbraced linkage specifications do not apply to the parameters
13587 to the function. */
13588 saved_in_unbraced_linkage_specification_p
13589 = parser->in_unbraced_linkage_specification_p;
13590 parser->in_unbraced_linkage_specification_p = false;
13592 /* Look for more parameters. */
13593 while (true)
13595 cp_parameter_declarator *parameter;
13596 bool parenthesized_p;
13597 /* Parse the parameter. */
13598 parameter
13599 = cp_parser_parameter_declaration (parser,
13600 /*template_parm_p=*/false,
13601 &parenthesized_p);
13603 /* If a parse error occurred parsing the parameter declaration,
13604 then the entire parameter-declaration-list is erroneous. */
13605 if (!parameter)
13607 *is_error = true;
13608 parameters = NULL;
13609 break;
13611 /* Add the new parameter to the list. */
13612 *tail = parameter;
13613 tail = &parameter->next;
13615 /* Peek at the next token. */
13616 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13617 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13618 /* These are for Objective-C++ */
13619 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13620 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13621 /* The parameter-declaration-list is complete. */
13622 break;
13623 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13625 cp_token *token;
13627 /* Peek at the next token. */
13628 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13629 /* If it's an ellipsis, then the list is complete. */
13630 if (token->type == CPP_ELLIPSIS)
13631 break;
13632 /* Otherwise, there must be more parameters. Consume the
13633 `,'. */
13634 cp_lexer_consume_token (parser->lexer);
13635 /* When parsing something like:
13637 int i(float f, double d)
13639 we can tell after seeing the declaration for "f" that we
13640 are not looking at an initialization of a variable "i",
13641 but rather at the declaration of a function "i".
13643 Due to the fact that the parsing of template arguments
13644 (as specified to a template-id) requires backtracking we
13645 cannot use this technique when inside a template argument
13646 list. */
13647 if (!parser->in_template_argument_list_p
13648 && !parser->in_type_id_in_expr_p
13649 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13650 /* However, a parameter-declaration of the form
13651 "foat(f)" (which is a valid declaration of a
13652 parameter "f") can also be interpreted as an
13653 expression (the conversion of "f" to "float"). */
13654 && !parenthesized_p)
13655 cp_parser_commit_to_tentative_parse (parser);
13657 else
13659 cp_parser_error (parser, "expected %<,%> or %<...%>");
13660 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13661 cp_parser_skip_to_closing_parenthesis (parser,
13662 /*recovering=*/true,
13663 /*or_comma=*/false,
13664 /*consume_paren=*/false);
13665 break;
13669 parser->in_unbraced_linkage_specification_p
13670 = saved_in_unbraced_linkage_specification_p;
13672 return parameters;
13675 /* Parse a parameter declaration.
13677 parameter-declaration:
13678 decl-specifier-seq ... [opt] declarator
13679 decl-specifier-seq declarator = assignment-expression
13680 decl-specifier-seq ... [opt] abstract-declarator [opt]
13681 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13683 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13684 declares a template parameter. (In that case, a non-nested `>'
13685 token encountered during the parsing of the assignment-expression
13686 is not interpreted as a greater-than operator.)
13688 Returns a representation of the parameter, or NULL if an error
13689 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13690 true iff the declarator is of the form "(p)". */
13692 static cp_parameter_declarator *
13693 cp_parser_parameter_declaration (cp_parser *parser,
13694 bool template_parm_p,
13695 bool *parenthesized_p)
13697 int declares_class_or_enum;
13698 bool greater_than_is_operator_p;
13699 cp_decl_specifier_seq decl_specifiers;
13700 cp_declarator *declarator;
13701 tree default_argument;
13702 cp_token *token;
13703 const char *saved_message;
13705 /* In a template parameter, `>' is not an operator.
13707 [temp.param]
13709 When parsing a default template-argument for a non-type
13710 template-parameter, the first non-nested `>' is taken as the end
13711 of the template parameter-list rather than a greater-than
13712 operator. */
13713 greater_than_is_operator_p = !template_parm_p;
13715 /* Type definitions may not appear in parameter types. */
13716 saved_message = parser->type_definition_forbidden_message;
13717 parser->type_definition_forbidden_message
13718 = "types may not be defined in parameter types";
13720 /* Parse the declaration-specifiers. */
13721 cp_parser_decl_specifier_seq (parser,
13722 CP_PARSER_FLAGS_NONE,
13723 &decl_specifiers,
13724 &declares_class_or_enum);
13725 /* If an error occurred, there's no reason to attempt to parse the
13726 rest of the declaration. */
13727 if (cp_parser_error_occurred (parser))
13729 parser->type_definition_forbidden_message = saved_message;
13730 return NULL;
13733 /* Peek at the next token. */
13734 token = cp_lexer_peek_token (parser->lexer);
13736 /* If the next token is a `)', `,', `=', `>', or `...', then there
13737 is no declarator. However, when variadic templates are enabled,
13738 there may be a declarator following `...'. */
13739 if (token->type == CPP_CLOSE_PAREN
13740 || token->type == CPP_COMMA
13741 || token->type == CPP_EQ
13742 || token->type == CPP_GREATER)
13744 declarator = NULL;
13745 if (parenthesized_p)
13746 *parenthesized_p = false;
13748 /* Otherwise, there should be a declarator. */
13749 else
13751 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13752 parser->default_arg_ok_p = false;
13754 /* After seeing a decl-specifier-seq, if the next token is not a
13755 "(", there is no possibility that the code is a valid
13756 expression. Therefore, if parsing tentatively, we commit at
13757 this point. */
13758 if (!parser->in_template_argument_list_p
13759 /* In an expression context, having seen:
13761 (int((char ...
13763 we cannot be sure whether we are looking at a
13764 function-type (taking a "char" as a parameter) or a cast
13765 of some object of type "char" to "int". */
13766 && !parser->in_type_id_in_expr_p
13767 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13768 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13769 cp_parser_commit_to_tentative_parse (parser);
13770 /* Parse the declarator. */
13771 declarator = cp_parser_declarator (parser,
13772 CP_PARSER_DECLARATOR_EITHER,
13773 /*ctor_dtor_or_conv_p=*/NULL,
13774 parenthesized_p,
13775 /*member_p=*/false);
13776 parser->default_arg_ok_p = saved_default_arg_ok_p;
13777 /* After the declarator, allow more attributes. */
13778 decl_specifiers.attributes
13779 = chainon (decl_specifiers.attributes,
13780 cp_parser_attributes_opt (parser));
13783 /* If the next token is an ellipsis, and we have not seen a
13784 declarator name, and the type of the declarator contains parameter
13785 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13786 a parameter pack expansion expression. Otherwise, leave the
13787 ellipsis for a C-style variadic function. */
13788 token = cp_lexer_peek_token (parser->lexer);
13789 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13791 tree type = decl_specifiers.type;
13793 if (type && DECL_P (type))
13794 type = TREE_TYPE (type);
13796 if (type
13797 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13798 && declarator_can_be_parameter_pack (declarator)
13799 && (!declarator || !declarator->parameter_pack_p)
13800 && uses_parameter_packs (type))
13802 /* Consume the `...'. */
13803 cp_lexer_consume_token (parser->lexer);
13804 maybe_warn_variadic_templates ();
13806 /* Build a pack expansion type */
13807 if (declarator)
13808 declarator->parameter_pack_p = true;
13809 else
13810 decl_specifiers.type = make_pack_expansion (type);
13814 /* The restriction on defining new types applies only to the type
13815 of the parameter, not to the default argument. */
13816 parser->type_definition_forbidden_message = saved_message;
13818 /* If the next token is `=', then process a default argument. */
13819 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13821 /* Consume the `='. */
13822 cp_lexer_consume_token (parser->lexer);
13824 /* If we are defining a class, then the tokens that make up the
13825 default argument must be saved and processed later. */
13826 if (!template_parm_p && at_class_scope_p ()
13827 && TYPE_BEING_DEFINED (current_class_type))
13829 unsigned depth = 0;
13830 cp_token *first_token;
13831 cp_token *token;
13833 /* Add tokens until we have processed the entire default
13834 argument. We add the range [first_token, token). */
13835 first_token = cp_lexer_peek_token (parser->lexer);
13836 while (true)
13838 bool done = false;
13840 /* Peek at the next token. */
13841 token = cp_lexer_peek_token (parser->lexer);
13842 /* What we do depends on what token we have. */
13843 switch (token->type)
13845 /* In valid code, a default argument must be
13846 immediately followed by a `,' `)', or `...'. */
13847 case CPP_COMMA:
13848 case CPP_CLOSE_PAREN:
13849 case CPP_ELLIPSIS:
13850 /* If we run into a non-nested `;', `}', or `]',
13851 then the code is invalid -- but the default
13852 argument is certainly over. */
13853 case CPP_SEMICOLON:
13854 case CPP_CLOSE_BRACE:
13855 case CPP_CLOSE_SQUARE:
13856 if (depth == 0)
13857 done = true;
13858 /* Update DEPTH, if necessary. */
13859 else if (token->type == CPP_CLOSE_PAREN
13860 || token->type == CPP_CLOSE_BRACE
13861 || token->type == CPP_CLOSE_SQUARE)
13862 --depth;
13863 break;
13865 case CPP_OPEN_PAREN:
13866 case CPP_OPEN_SQUARE:
13867 case CPP_OPEN_BRACE:
13868 ++depth;
13869 break;
13871 case CPP_RSHIFT:
13872 if (cxx_dialect == cxx98)
13873 break;
13874 /* Fall through for C++0x, which treats the `>>'
13875 operator like two `>' tokens in certain
13876 cases. */
13878 case CPP_GREATER:
13879 /* If we see a non-nested `>', and `>' is not an
13880 operator, then it marks the end of the default
13881 argument. */
13882 if (!depth && !greater_than_is_operator_p)
13883 done = true;
13884 break;
13886 /* If we run out of tokens, issue an error message. */
13887 case CPP_EOF:
13888 case CPP_PRAGMA_EOL:
13889 error ("file ends in default argument");
13890 done = true;
13891 break;
13893 case CPP_NAME:
13894 case CPP_SCOPE:
13895 /* In these cases, we should look for template-ids.
13896 For example, if the default argument is
13897 `X<int, double>()', we need to do name lookup to
13898 figure out whether or not `X' is a template; if
13899 so, the `,' does not end the default argument.
13901 That is not yet done. */
13902 break;
13904 default:
13905 break;
13908 /* If we've reached the end, stop. */
13909 if (done)
13910 break;
13912 /* Add the token to the token block. */
13913 token = cp_lexer_consume_token (parser->lexer);
13916 /* Create a DEFAULT_ARG to represent the unparsed default
13917 argument. */
13918 default_argument = make_node (DEFAULT_ARG);
13919 DEFARG_TOKENS (default_argument)
13920 = cp_token_cache_new (first_token, token);
13921 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13923 /* Outside of a class definition, we can just parse the
13924 assignment-expression. */
13925 else
13926 default_argument
13927 = cp_parser_default_argument (parser, template_parm_p);
13929 if (!parser->default_arg_ok_p)
13931 if (!flag_pedantic_errors)
13932 warning (0, "deprecated use of default argument for parameter of non-function");
13933 else
13935 error ("default arguments are only permitted for function parameters");
13936 default_argument = NULL_TREE;
13939 else if ((declarator && declarator->parameter_pack_p)
13940 || (decl_specifiers.type
13941 && PACK_EXPANSION_P (decl_specifiers.type)))
13943 const char* kind = template_parm_p? "template " : "";
13945 /* Find the name of the parameter pack. */
13946 cp_declarator *id_declarator = declarator;
13947 while (id_declarator && id_declarator->kind != cdk_id)
13948 id_declarator = id_declarator->declarator;
13950 if (id_declarator && id_declarator->kind == cdk_id)
13951 error ("%sparameter pack %qD cannot have a default argument",
13952 kind, id_declarator->u.id.unqualified_name);
13953 else
13954 error ("%sparameter pack cannot have a default argument",
13955 kind);
13957 default_argument = NULL_TREE;
13960 else
13961 default_argument = NULL_TREE;
13963 return make_parameter_declarator (&decl_specifiers,
13964 declarator,
13965 default_argument);
13968 /* Parse a default argument and return it.
13970 TEMPLATE_PARM_P is true if this is a default argument for a
13971 non-type template parameter. */
13972 static tree
13973 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
13975 tree default_argument = NULL_TREE;
13976 bool saved_greater_than_is_operator_p;
13977 bool saved_local_variables_forbidden_p;
13979 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13980 set correctly. */
13981 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
13982 parser->greater_than_is_operator_p = !template_parm_p;
13983 /* Local variable names (and the `this' keyword) may not
13984 appear in a default argument. */
13985 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13986 parser->local_variables_forbidden_p = true;
13987 /* The default argument expression may cause implicitly
13988 defined member functions to be synthesized, which will
13989 result in garbage collection. We must treat this
13990 situation as if we were within the body of function so as
13991 to avoid collecting live data on the stack. */
13992 ++function_depth;
13993 /* Parse the assignment-expression. */
13994 if (template_parm_p)
13995 push_deferring_access_checks (dk_no_deferred);
13996 default_argument
13997 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13998 if (template_parm_p)
13999 pop_deferring_access_checks ();
14000 /* Restore saved state. */
14001 --function_depth;
14002 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14003 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14005 return default_argument;
14008 /* Parse a function-body.
14010 function-body:
14011 compound_statement */
14013 static void
14014 cp_parser_function_body (cp_parser *parser)
14016 cp_parser_compound_statement (parser, NULL, false);
14019 /* Parse a ctor-initializer-opt followed by a function-body. Return
14020 true if a ctor-initializer was present. */
14022 static bool
14023 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14025 tree body;
14026 bool ctor_initializer_p;
14028 /* Begin the function body. */
14029 body = begin_function_body ();
14030 /* Parse the optional ctor-initializer. */
14031 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14032 /* Parse the function-body. */
14033 cp_parser_function_body (parser);
14034 /* Finish the function body. */
14035 finish_function_body (body);
14037 return ctor_initializer_p;
14040 /* Parse an initializer.
14042 initializer:
14043 = initializer-clause
14044 ( expression-list )
14046 Returns an expression representing the initializer. If no
14047 initializer is present, NULL_TREE is returned.
14049 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
14050 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
14051 set to FALSE if there is no initializer present. If there is an
14052 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14053 is set to true; otherwise it is set to false. */
14055 static tree
14056 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
14057 bool* non_constant_p)
14059 cp_token *token;
14060 tree init;
14062 /* Peek at the next token. */
14063 token = cp_lexer_peek_token (parser->lexer);
14065 /* Let our caller know whether or not this initializer was
14066 parenthesized. */
14067 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
14068 /* Assume that the initializer is constant. */
14069 *non_constant_p = false;
14071 if (token->type == CPP_EQ)
14073 /* Consume the `='. */
14074 cp_lexer_consume_token (parser->lexer);
14075 /* Parse the initializer-clause. */
14076 init = cp_parser_initializer_clause (parser, non_constant_p);
14078 else if (token->type == CPP_OPEN_PAREN)
14079 init = cp_parser_parenthesized_expression_list (parser, false,
14080 /*cast_p=*/false,
14081 /*allow_expansion_p=*/true,
14082 non_constant_p);
14083 else
14085 /* Anything else is an error. */
14086 cp_parser_error (parser, "expected initializer");
14087 init = error_mark_node;
14090 return init;
14093 /* Parse an initializer-clause.
14095 initializer-clause:
14096 assignment-expression
14097 { initializer-list , [opt] }
14100 Returns an expression representing the initializer.
14102 If the `assignment-expression' production is used the value
14103 returned is simply a representation for the expression.
14105 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
14106 the elements of the initializer-list (or NULL, if the last
14107 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14108 NULL_TREE. There is no way to detect whether or not the optional
14109 trailing `,' was provided. NON_CONSTANT_P is as for
14110 cp_parser_initializer. */
14112 static tree
14113 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14115 tree initializer;
14117 /* Assume the expression is constant. */
14118 *non_constant_p = false;
14120 /* If it is not a `{', then we are looking at an
14121 assignment-expression. */
14122 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14124 initializer
14125 = cp_parser_constant_expression (parser,
14126 /*allow_non_constant_p=*/true,
14127 non_constant_p);
14128 if (!*non_constant_p)
14129 initializer = fold_non_dependent_expr (initializer);
14131 else
14133 /* Consume the `{' token. */
14134 cp_lexer_consume_token (parser->lexer);
14135 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14136 initializer = make_node (CONSTRUCTOR);
14137 /* If it's not a `}', then there is a non-trivial initializer. */
14138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14140 /* Parse the initializer list. */
14141 CONSTRUCTOR_ELTS (initializer)
14142 = cp_parser_initializer_list (parser, non_constant_p);
14143 /* A trailing `,' token is allowed. */
14144 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14145 cp_lexer_consume_token (parser->lexer);
14147 /* Now, there should be a trailing `}'. */
14148 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14151 return initializer;
14154 /* Parse an initializer-list.
14156 initializer-list:
14157 initializer-clause ... [opt]
14158 initializer-list , initializer-clause ... [opt]
14160 GNU Extension:
14162 initializer-list:
14163 identifier : initializer-clause
14164 initializer-list, identifier : initializer-clause
14166 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14167 for the initializer. If the INDEX of the elt is non-NULL, it is the
14168 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14169 as for cp_parser_initializer. */
14171 static VEC(constructor_elt,gc) *
14172 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14174 VEC(constructor_elt,gc) *v = NULL;
14176 /* Assume all of the expressions are constant. */
14177 *non_constant_p = false;
14179 /* Parse the rest of the list. */
14180 while (true)
14182 cp_token *token;
14183 tree identifier;
14184 tree initializer;
14185 bool clause_non_constant_p;
14187 /* If the next token is an identifier and the following one is a
14188 colon, we are looking at the GNU designated-initializer
14189 syntax. */
14190 if (cp_parser_allow_gnu_extensions_p (parser)
14191 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14192 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14194 /* Warn the user that they are using an extension. */
14195 if (pedantic)
14196 pedwarn ("ISO C++ does not allow designated initializers");
14197 /* Consume the identifier. */
14198 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14199 /* Consume the `:'. */
14200 cp_lexer_consume_token (parser->lexer);
14202 else
14203 identifier = NULL_TREE;
14205 /* Parse the initializer. */
14206 initializer = cp_parser_initializer_clause (parser,
14207 &clause_non_constant_p);
14208 /* If any clause is non-constant, so is the entire initializer. */
14209 if (clause_non_constant_p)
14210 *non_constant_p = true;
14212 /* If we have an ellipsis, this is an initializer pack
14213 expansion. */
14214 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14216 /* Consume the `...'. */
14217 cp_lexer_consume_token (parser->lexer);
14219 /* Turn the initializer into an initializer expansion. */
14220 initializer = make_pack_expansion (initializer);
14223 /* Add it to the vector. */
14224 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14226 /* If the next token is not a comma, we have reached the end of
14227 the list. */
14228 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14229 break;
14231 /* Peek at the next token. */
14232 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14233 /* If the next token is a `}', then we're still done. An
14234 initializer-clause can have a trailing `,' after the
14235 initializer-list and before the closing `}'. */
14236 if (token->type == CPP_CLOSE_BRACE)
14237 break;
14239 /* Consume the `,' token. */
14240 cp_lexer_consume_token (parser->lexer);
14243 return v;
14246 /* Classes [gram.class] */
14248 /* Parse a class-name.
14250 class-name:
14251 identifier
14252 template-id
14254 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14255 to indicate that names looked up in dependent types should be
14256 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14257 keyword has been used to indicate that the name that appears next
14258 is a template. TAG_TYPE indicates the explicit tag given before
14259 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14260 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14261 is the class being defined in a class-head.
14263 Returns the TYPE_DECL representing the class. */
14265 static tree
14266 cp_parser_class_name (cp_parser *parser,
14267 bool typename_keyword_p,
14268 bool template_keyword_p,
14269 enum tag_types tag_type,
14270 bool check_dependency_p,
14271 bool class_head_p,
14272 bool is_declaration)
14274 tree decl;
14275 tree scope;
14276 bool typename_p;
14277 cp_token *token;
14279 /* All class-names start with an identifier. */
14280 token = cp_lexer_peek_token (parser->lexer);
14281 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14283 cp_parser_error (parser, "expected class-name");
14284 return error_mark_node;
14287 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14288 to a template-id, so we save it here. */
14289 scope = parser->scope;
14290 if (scope == error_mark_node)
14291 return error_mark_node;
14293 /* Any name names a type if we're following the `typename' keyword
14294 in a qualified name where the enclosing scope is type-dependent. */
14295 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14296 && dependent_type_p (scope));
14297 /* Handle the common case (an identifier, but not a template-id)
14298 efficiently. */
14299 if (token->type == CPP_NAME
14300 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14302 cp_token *identifier_token;
14303 tree identifier;
14304 bool ambiguous_p;
14306 /* Look for the identifier. */
14307 identifier_token = cp_lexer_peek_token (parser->lexer);
14308 ambiguous_p = identifier_token->ambiguous_p;
14309 identifier = cp_parser_identifier (parser);
14310 /* If the next token isn't an identifier, we are certainly not
14311 looking at a class-name. */
14312 if (identifier == error_mark_node)
14313 decl = error_mark_node;
14314 /* If we know this is a type-name, there's no need to look it
14315 up. */
14316 else if (typename_p)
14317 decl = identifier;
14318 else
14320 tree ambiguous_decls;
14321 /* If we already know that this lookup is ambiguous, then
14322 we've already issued an error message; there's no reason
14323 to check again. */
14324 if (ambiguous_p)
14326 cp_parser_simulate_error (parser);
14327 return error_mark_node;
14329 /* If the next token is a `::', then the name must be a type
14330 name.
14332 [basic.lookup.qual]
14334 During the lookup for a name preceding the :: scope
14335 resolution operator, object, function, and enumerator
14336 names are ignored. */
14337 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14338 tag_type = typename_type;
14339 /* Look up the name. */
14340 decl = cp_parser_lookup_name (parser, identifier,
14341 tag_type,
14342 /*is_template=*/false,
14343 /*is_namespace=*/false,
14344 check_dependency_p,
14345 &ambiguous_decls);
14346 if (ambiguous_decls)
14348 error ("reference to %qD is ambiguous", identifier);
14349 print_candidates (ambiguous_decls);
14350 if (cp_parser_parsing_tentatively (parser))
14352 identifier_token->ambiguous_p = true;
14353 cp_parser_simulate_error (parser);
14355 return error_mark_node;
14359 else
14361 /* Try a template-id. */
14362 decl = cp_parser_template_id (parser, template_keyword_p,
14363 check_dependency_p,
14364 is_declaration);
14365 if (decl == error_mark_node)
14366 return error_mark_node;
14369 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14371 /* If this is a typename, create a TYPENAME_TYPE. */
14372 if (typename_p && decl != error_mark_node)
14374 decl = make_typename_type (scope, decl, typename_type,
14375 /*complain=*/tf_error);
14376 if (decl != error_mark_node)
14377 decl = TYPE_NAME (decl);
14380 /* Check to see that it is really the name of a class. */
14381 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14382 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14383 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14384 /* Situations like this:
14386 template <typename T> struct A {
14387 typename T::template X<int>::I i;
14390 are problematic. Is `T::template X<int>' a class-name? The
14391 standard does not seem to be definitive, but there is no other
14392 valid interpretation of the following `::'. Therefore, those
14393 names are considered class-names. */
14395 decl = make_typename_type (scope, decl, tag_type, tf_error);
14396 if (decl != error_mark_node)
14397 decl = TYPE_NAME (decl);
14399 else if (TREE_CODE (decl) != TYPE_DECL
14400 || TREE_TYPE (decl) == error_mark_node
14401 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
14402 decl = error_mark_node;
14404 if (decl == error_mark_node)
14405 cp_parser_error (parser, "expected class-name");
14407 return decl;
14410 /* Parse a class-specifier.
14412 class-specifier:
14413 class-head { member-specification [opt] }
14415 Returns the TREE_TYPE representing the class. */
14417 static tree
14418 cp_parser_class_specifier (cp_parser* parser)
14420 cp_token *token;
14421 tree type;
14422 tree attributes = NULL_TREE;
14423 int has_trailing_semicolon;
14424 bool nested_name_specifier_p;
14425 unsigned saved_num_template_parameter_lists;
14426 bool saved_in_function_body;
14427 tree old_scope = NULL_TREE;
14428 tree scope = NULL_TREE;
14429 tree bases;
14431 push_deferring_access_checks (dk_no_deferred);
14433 /* Parse the class-head. */
14434 type = cp_parser_class_head (parser,
14435 &nested_name_specifier_p,
14436 &attributes,
14437 &bases);
14438 /* If the class-head was a semantic disaster, skip the entire body
14439 of the class. */
14440 if (!type)
14442 cp_parser_skip_to_end_of_block_or_statement (parser);
14443 pop_deferring_access_checks ();
14444 return error_mark_node;
14447 /* Look for the `{'. */
14448 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14450 pop_deferring_access_checks ();
14451 return error_mark_node;
14454 /* Process the base classes. If they're invalid, skip the
14455 entire class body. */
14456 if (!xref_basetypes (type, bases))
14458 /* Consuming the closing brace yields better error messages
14459 later on. */
14460 if (cp_parser_skip_to_closing_brace (parser))
14461 cp_lexer_consume_token (parser->lexer);
14462 pop_deferring_access_checks ();
14463 return error_mark_node;
14466 /* Issue an error message if type-definitions are forbidden here. */
14467 cp_parser_check_type_definition (parser);
14468 /* Remember that we are defining one more class. */
14469 ++parser->num_classes_being_defined;
14470 /* Inside the class, surrounding template-parameter-lists do not
14471 apply. */
14472 saved_num_template_parameter_lists
14473 = parser->num_template_parameter_lists;
14474 parser->num_template_parameter_lists = 0;
14475 /* We are not in a function body. */
14476 saved_in_function_body = parser->in_function_body;
14477 parser->in_function_body = false;
14479 /* Start the class. */
14480 if (nested_name_specifier_p)
14482 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14483 old_scope = push_inner_scope (scope);
14485 type = begin_class_definition (type, attributes);
14487 if (type == error_mark_node)
14488 /* If the type is erroneous, skip the entire body of the class. */
14489 cp_parser_skip_to_closing_brace (parser);
14490 else
14491 /* Parse the member-specification. */
14492 cp_parser_member_specification_opt (parser);
14494 /* Look for the trailing `}'. */
14495 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14496 /* We get better error messages by noticing a common problem: a
14497 missing trailing `;'. */
14498 token = cp_lexer_peek_token (parser->lexer);
14499 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14500 /* Look for trailing attributes to apply to this class. */
14501 if (cp_parser_allow_gnu_extensions_p (parser))
14502 attributes = cp_parser_attributes_opt (parser);
14503 if (type != error_mark_node)
14504 type = finish_struct (type, attributes);
14505 if (nested_name_specifier_p)
14506 pop_inner_scope (old_scope, scope);
14507 /* If this class is not itself within the scope of another class,
14508 then we need to parse the bodies of all of the queued function
14509 definitions. Note that the queued functions defined in a class
14510 are not always processed immediately following the
14511 class-specifier for that class. Consider:
14513 struct A {
14514 struct B { void f() { sizeof (A); } };
14517 If `f' were processed before the processing of `A' were
14518 completed, there would be no way to compute the size of `A'.
14519 Note that the nesting we are interested in here is lexical --
14520 not the semantic nesting given by TYPE_CONTEXT. In particular,
14521 for:
14523 struct A { struct B; };
14524 struct A::B { void f() { } };
14526 there is no need to delay the parsing of `A::B::f'. */
14527 if (--parser->num_classes_being_defined == 0)
14529 tree queue_entry;
14530 tree fn;
14531 tree class_type = NULL_TREE;
14532 tree pushed_scope = NULL_TREE;
14534 /* In a first pass, parse default arguments to the functions.
14535 Then, in a second pass, parse the bodies of the functions.
14536 This two-phased approach handles cases like:
14538 struct S {
14539 void f() { g(); }
14540 void g(int i = 3);
14544 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14545 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14546 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14547 TREE_PURPOSE (parser->unparsed_functions_queues)
14548 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14550 fn = TREE_VALUE (queue_entry);
14551 /* If there are default arguments that have not yet been processed,
14552 take care of them now. */
14553 if (class_type != TREE_PURPOSE (queue_entry))
14555 if (pushed_scope)
14556 pop_scope (pushed_scope);
14557 class_type = TREE_PURPOSE (queue_entry);
14558 pushed_scope = push_scope (class_type);
14560 /* Make sure that any template parameters are in scope. */
14561 maybe_begin_member_template_processing (fn);
14562 /* Parse the default argument expressions. */
14563 cp_parser_late_parsing_default_args (parser, fn);
14564 /* Remove any template parameters from the symbol table. */
14565 maybe_end_member_template_processing ();
14567 if (pushed_scope)
14568 pop_scope (pushed_scope);
14569 /* Now parse the body of the functions. */
14570 for (TREE_VALUE (parser->unparsed_functions_queues)
14571 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14572 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14573 TREE_VALUE (parser->unparsed_functions_queues)
14574 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14576 /* Figure out which function we need to process. */
14577 fn = TREE_VALUE (queue_entry);
14578 /* Parse the function. */
14579 cp_parser_late_parsing_for_member (parser, fn);
14583 /* Put back any saved access checks. */
14584 pop_deferring_access_checks ();
14586 /* Restore saved state. */
14587 parser->in_function_body = saved_in_function_body;
14588 parser->num_template_parameter_lists
14589 = saved_num_template_parameter_lists;
14591 return type;
14594 /* Parse a class-head.
14596 class-head:
14597 class-key identifier [opt] base-clause [opt]
14598 class-key nested-name-specifier identifier base-clause [opt]
14599 class-key nested-name-specifier [opt] template-id
14600 base-clause [opt]
14602 GNU Extensions:
14603 class-key attributes identifier [opt] base-clause [opt]
14604 class-key attributes nested-name-specifier identifier base-clause [opt]
14605 class-key attributes nested-name-specifier [opt] template-id
14606 base-clause [opt]
14608 Upon return BASES is initialized to the list of base classes (or
14609 NULL, if there are none) in the same form returned by
14610 cp_parser_base_clause.
14612 Returns the TYPE of the indicated class. Sets
14613 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14614 involving a nested-name-specifier was used, and FALSE otherwise.
14616 Returns error_mark_node if this is not a class-head.
14618 Returns NULL_TREE if the class-head is syntactically valid, but
14619 semantically invalid in a way that means we should skip the entire
14620 body of the class. */
14622 static tree
14623 cp_parser_class_head (cp_parser* parser,
14624 bool* nested_name_specifier_p,
14625 tree *attributes_p,
14626 tree *bases)
14628 tree nested_name_specifier;
14629 enum tag_types class_key;
14630 tree id = NULL_TREE;
14631 tree type = NULL_TREE;
14632 tree attributes;
14633 bool template_id_p = false;
14634 bool qualified_p = false;
14635 bool invalid_nested_name_p = false;
14636 bool invalid_explicit_specialization_p = false;
14637 tree pushed_scope = NULL_TREE;
14638 unsigned num_templates;
14640 /* Assume no nested-name-specifier will be present. */
14641 *nested_name_specifier_p = false;
14642 /* Assume no template parameter lists will be used in defining the
14643 type. */
14644 num_templates = 0;
14646 *bases = NULL_TREE;
14648 /* Look for the class-key. */
14649 class_key = cp_parser_class_key (parser);
14650 if (class_key == none_type)
14651 return error_mark_node;
14653 /* Parse the attributes. */
14654 attributes = cp_parser_attributes_opt (parser);
14656 /* If the next token is `::', that is invalid -- but sometimes
14657 people do try to write:
14659 struct ::S {};
14661 Handle this gracefully by accepting the extra qualifier, and then
14662 issuing an error about it later if this really is a
14663 class-head. If it turns out just to be an elaborated type
14664 specifier, remain silent. */
14665 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14666 qualified_p = true;
14668 push_deferring_access_checks (dk_no_check);
14670 /* Determine the name of the class. Begin by looking for an
14671 optional nested-name-specifier. */
14672 nested_name_specifier
14673 = cp_parser_nested_name_specifier_opt (parser,
14674 /*typename_keyword_p=*/false,
14675 /*check_dependency_p=*/false,
14676 /*type_p=*/false,
14677 /*is_declaration=*/false);
14678 /* If there was a nested-name-specifier, then there *must* be an
14679 identifier. */
14680 if (nested_name_specifier)
14682 /* Although the grammar says `identifier', it really means
14683 `class-name' or `template-name'. You are only allowed to
14684 define a class that has already been declared with this
14685 syntax.
14687 The proposed resolution for Core Issue 180 says that wherever
14688 you see `class T::X' you should treat `X' as a type-name.
14690 It is OK to define an inaccessible class; for example:
14692 class A { class B; };
14693 class A::B {};
14695 We do not know if we will see a class-name, or a
14696 template-name. We look for a class-name first, in case the
14697 class-name is a template-id; if we looked for the
14698 template-name first we would stop after the template-name. */
14699 cp_parser_parse_tentatively (parser);
14700 type = cp_parser_class_name (parser,
14701 /*typename_keyword_p=*/false,
14702 /*template_keyword_p=*/false,
14703 class_type,
14704 /*check_dependency_p=*/false,
14705 /*class_head_p=*/true,
14706 /*is_declaration=*/false);
14707 /* If that didn't work, ignore the nested-name-specifier. */
14708 if (!cp_parser_parse_definitely (parser))
14710 invalid_nested_name_p = true;
14711 id = cp_parser_identifier (parser);
14712 if (id == error_mark_node)
14713 id = NULL_TREE;
14715 /* If we could not find a corresponding TYPE, treat this
14716 declaration like an unqualified declaration. */
14717 if (type == error_mark_node)
14718 nested_name_specifier = NULL_TREE;
14719 /* Otherwise, count the number of templates used in TYPE and its
14720 containing scopes. */
14721 else
14723 tree scope;
14725 for (scope = TREE_TYPE (type);
14726 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14727 scope = (TYPE_P (scope)
14728 ? TYPE_CONTEXT (scope)
14729 : DECL_CONTEXT (scope)))
14730 if (TYPE_P (scope)
14731 && CLASS_TYPE_P (scope)
14732 && CLASSTYPE_TEMPLATE_INFO (scope)
14733 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14734 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14735 ++num_templates;
14738 /* Otherwise, the identifier is optional. */
14739 else
14741 /* We don't know whether what comes next is a template-id,
14742 an identifier, or nothing at all. */
14743 cp_parser_parse_tentatively (parser);
14744 /* Check for a template-id. */
14745 id = cp_parser_template_id (parser,
14746 /*template_keyword_p=*/false,
14747 /*check_dependency_p=*/true,
14748 /*is_declaration=*/true);
14749 /* If that didn't work, it could still be an identifier. */
14750 if (!cp_parser_parse_definitely (parser))
14752 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14753 id = cp_parser_identifier (parser);
14754 else
14755 id = NULL_TREE;
14757 else
14759 template_id_p = true;
14760 ++num_templates;
14764 pop_deferring_access_checks ();
14766 if (id)
14767 cp_parser_check_for_invalid_template_id (parser, id);
14769 /* If it's not a `:' or a `{' then we can't really be looking at a
14770 class-head, since a class-head only appears as part of a
14771 class-specifier. We have to detect this situation before calling
14772 xref_tag, since that has irreversible side-effects. */
14773 if (!cp_parser_next_token_starts_class_definition_p (parser))
14775 cp_parser_error (parser, "expected %<{%> or %<:%>");
14776 return error_mark_node;
14779 /* At this point, we're going ahead with the class-specifier, even
14780 if some other problem occurs. */
14781 cp_parser_commit_to_tentative_parse (parser);
14782 /* Issue the error about the overly-qualified name now. */
14783 if (qualified_p)
14784 cp_parser_error (parser,
14785 "global qualification of class name is invalid");
14786 else if (invalid_nested_name_p)
14787 cp_parser_error (parser,
14788 "qualified name does not name a class");
14789 else if (nested_name_specifier)
14791 tree scope;
14793 /* Reject typedef-names in class heads. */
14794 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14796 error ("invalid class name in declaration of %qD", type);
14797 type = NULL_TREE;
14798 goto done;
14801 /* Figure out in what scope the declaration is being placed. */
14802 scope = current_scope ();
14803 /* If that scope does not contain the scope in which the
14804 class was originally declared, the program is invalid. */
14805 if (scope && !is_ancestor (scope, nested_name_specifier))
14807 if (at_namespace_scope_p ())
14808 error ("declaration of %qD in namespace %qD which does not "
14809 "enclose %qD", type, scope, nested_name_specifier);
14810 else
14811 error ("declaration of %qD in %qD which does not enclose %qD",
14812 type, scope, nested_name_specifier);
14813 type = NULL_TREE;
14814 goto done;
14816 /* [dcl.meaning]
14818 A declarator-id shall not be qualified exception of the
14819 definition of a ... nested class outside of its class
14820 ... [or] a the definition or explicit instantiation of a
14821 class member of a namespace outside of its namespace. */
14822 if (scope == nested_name_specifier)
14824 pedwarn ("extra qualification ignored");
14825 nested_name_specifier = NULL_TREE;
14826 num_templates = 0;
14829 /* An explicit-specialization must be preceded by "template <>". If
14830 it is not, try to recover gracefully. */
14831 if (at_namespace_scope_p ()
14832 && parser->num_template_parameter_lists == 0
14833 && template_id_p)
14835 error ("an explicit specialization must be preceded by %<template <>%>");
14836 invalid_explicit_specialization_p = true;
14837 /* Take the same action that would have been taken by
14838 cp_parser_explicit_specialization. */
14839 ++parser->num_template_parameter_lists;
14840 begin_specialization ();
14842 /* There must be no "return" statements between this point and the
14843 end of this function; set "type "to the correct return value and
14844 use "goto done;" to return. */
14845 /* Make sure that the right number of template parameters were
14846 present. */
14847 if (!cp_parser_check_template_parameters (parser, num_templates))
14849 /* If something went wrong, there is no point in even trying to
14850 process the class-definition. */
14851 type = NULL_TREE;
14852 goto done;
14855 /* Look up the type. */
14856 if (template_id_p)
14858 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
14859 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
14860 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
14862 error ("function template %qD redeclared as a class template", id);
14863 type = error_mark_node;
14865 else
14867 type = TREE_TYPE (id);
14868 type = maybe_process_partial_specialization (type);
14870 if (nested_name_specifier)
14871 pushed_scope = push_scope (nested_name_specifier);
14873 else if (nested_name_specifier)
14875 tree class_type;
14877 /* Given:
14879 template <typename T> struct S { struct T };
14880 template <typename T> struct S<T>::T { };
14882 we will get a TYPENAME_TYPE when processing the definition of
14883 `S::T'. We need to resolve it to the actual type before we
14884 try to define it. */
14885 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14887 class_type = resolve_typename_type (TREE_TYPE (type),
14888 /*only_current_p=*/false);
14889 if (TREE_CODE (class_type) != TYPENAME_TYPE)
14890 type = TYPE_NAME (class_type);
14891 else
14893 cp_parser_error (parser, "could not resolve typename type");
14894 type = error_mark_node;
14898 maybe_process_partial_specialization (TREE_TYPE (type));
14899 class_type = current_class_type;
14900 /* Enter the scope indicated by the nested-name-specifier. */
14901 pushed_scope = push_scope (nested_name_specifier);
14902 /* Get the canonical version of this type. */
14903 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14904 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14905 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14907 type = push_template_decl (type);
14908 if (type == error_mark_node)
14910 type = NULL_TREE;
14911 goto done;
14915 type = TREE_TYPE (type);
14916 *nested_name_specifier_p = true;
14918 else /* The name is not a nested name. */
14920 /* If the class was unnamed, create a dummy name. */
14921 if (!id)
14922 id = make_anon_name ();
14923 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14924 parser->num_template_parameter_lists);
14927 /* Indicate whether this class was declared as a `class' or as a
14928 `struct'. */
14929 if (TREE_CODE (type) == RECORD_TYPE)
14930 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14931 cp_parser_check_class_key (class_key, type);
14933 /* If this type was already complete, and we see another definition,
14934 that's an error. */
14935 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14937 error ("redefinition of %q#T", type);
14938 error ("previous definition of %q+#T", type);
14939 type = NULL_TREE;
14940 goto done;
14942 else if (type == error_mark_node)
14943 type = NULL_TREE;
14945 /* We will have entered the scope containing the class; the names of
14946 base classes should be looked up in that context. For example:
14948 struct A { struct B {}; struct C; };
14949 struct A::C : B {};
14951 is valid. */
14953 /* Get the list of base-classes, if there is one. */
14954 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14955 *bases = cp_parser_base_clause (parser);
14957 done:
14958 /* Leave the scope given by the nested-name-specifier. We will
14959 enter the class scope itself while processing the members. */
14960 if (pushed_scope)
14961 pop_scope (pushed_scope);
14963 if (invalid_explicit_specialization_p)
14965 end_specialization ();
14966 --parser->num_template_parameter_lists;
14968 *attributes_p = attributes;
14969 return type;
14972 /* Parse a class-key.
14974 class-key:
14975 class
14976 struct
14977 union
14979 Returns the kind of class-key specified, or none_type to indicate
14980 error. */
14982 static enum tag_types
14983 cp_parser_class_key (cp_parser* parser)
14985 cp_token *token;
14986 enum tag_types tag_type;
14988 /* Look for the class-key. */
14989 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14990 if (!token)
14991 return none_type;
14993 /* Check to see if the TOKEN is a class-key. */
14994 tag_type = cp_parser_token_is_class_key (token);
14995 if (!tag_type)
14996 cp_parser_error (parser, "expected class-key");
14997 return tag_type;
15000 /* Parse an (optional) member-specification.
15002 member-specification:
15003 member-declaration member-specification [opt]
15004 access-specifier : member-specification [opt] */
15006 static void
15007 cp_parser_member_specification_opt (cp_parser* parser)
15009 while (true)
15011 cp_token *token;
15012 enum rid keyword;
15014 /* Peek at the next token. */
15015 token = cp_lexer_peek_token (parser->lexer);
15016 /* If it's a `}', or EOF then we've seen all the members. */
15017 if (token->type == CPP_CLOSE_BRACE
15018 || token->type == CPP_EOF
15019 || token->type == CPP_PRAGMA_EOL)
15020 break;
15022 /* See if this token is a keyword. */
15023 keyword = token->keyword;
15024 switch (keyword)
15026 case RID_PUBLIC:
15027 case RID_PROTECTED:
15028 case RID_PRIVATE:
15029 /* Consume the access-specifier. */
15030 cp_lexer_consume_token (parser->lexer);
15031 /* Remember which access-specifier is active. */
15032 current_access_specifier = token->u.value;
15033 /* Look for the `:'. */
15034 cp_parser_require (parser, CPP_COLON, "`:'");
15035 break;
15037 default:
15038 /* Accept #pragmas at class scope. */
15039 if (token->type == CPP_PRAGMA)
15041 cp_parser_pragma (parser, pragma_external);
15042 break;
15045 /* Otherwise, the next construction must be a
15046 member-declaration. */
15047 cp_parser_member_declaration (parser);
15052 /* Parse a member-declaration.
15054 member-declaration:
15055 decl-specifier-seq [opt] member-declarator-list [opt] ;
15056 function-definition ; [opt]
15057 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15058 using-declaration
15059 template-declaration
15061 member-declarator-list:
15062 member-declarator
15063 member-declarator-list , member-declarator
15065 member-declarator:
15066 declarator pure-specifier [opt]
15067 declarator constant-initializer [opt]
15068 identifier [opt] : constant-expression
15070 GNU Extensions:
15072 member-declaration:
15073 __extension__ member-declaration
15075 member-declarator:
15076 declarator attributes [opt] pure-specifier [opt]
15077 declarator attributes [opt] constant-initializer [opt]
15078 identifier [opt] attributes [opt] : constant-expression
15080 C++0x Extensions:
15082 member-declaration:
15083 static_assert-declaration */
15085 static void
15086 cp_parser_member_declaration (cp_parser* parser)
15088 cp_decl_specifier_seq decl_specifiers;
15089 tree prefix_attributes;
15090 tree decl;
15091 int declares_class_or_enum;
15092 bool friend_p;
15093 cp_token *token;
15094 int saved_pedantic;
15096 /* Check for the `__extension__' keyword. */
15097 if (cp_parser_extension_opt (parser, &saved_pedantic))
15099 /* Recurse. */
15100 cp_parser_member_declaration (parser);
15101 /* Restore the old value of the PEDANTIC flag. */
15102 pedantic = saved_pedantic;
15104 return;
15107 /* Check for a template-declaration. */
15108 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15110 /* An explicit specialization here is an error condition, and we
15111 expect the specialization handler to detect and report this. */
15112 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15113 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15114 cp_parser_explicit_specialization (parser);
15115 else
15116 cp_parser_template_declaration (parser, /*member_p=*/true);
15118 return;
15121 /* Check for a using-declaration. */
15122 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15124 /* Parse the using-declaration. */
15125 cp_parser_using_declaration (parser,
15126 /*access_declaration_p=*/false);
15127 return;
15130 /* Check for @defs. */
15131 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15133 tree ivar, member;
15134 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15135 ivar = ivar_chains;
15136 while (ivar)
15138 member = ivar;
15139 ivar = TREE_CHAIN (member);
15140 TREE_CHAIN (member) = NULL_TREE;
15141 finish_member_declaration (member);
15143 return;
15146 /* If the next token is `static_assert' we have a static assertion. */
15147 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15149 cp_parser_static_assert (parser, /*member_p=*/true);
15150 return;
15153 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15154 return;
15156 /* Parse the decl-specifier-seq. */
15157 cp_parser_decl_specifier_seq (parser,
15158 CP_PARSER_FLAGS_OPTIONAL,
15159 &decl_specifiers,
15160 &declares_class_or_enum);
15161 prefix_attributes = decl_specifiers.attributes;
15162 decl_specifiers.attributes = NULL_TREE;
15163 /* Check for an invalid type-name. */
15164 if (!decl_specifiers.type
15165 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15166 return;
15167 /* If there is no declarator, then the decl-specifier-seq should
15168 specify a type. */
15169 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15171 /* If there was no decl-specifier-seq, and the next token is a
15172 `;', then we have something like:
15174 struct S { ; };
15176 [class.mem]
15178 Each member-declaration shall declare at least one member
15179 name of the class. */
15180 if (!decl_specifiers.any_specifiers_p)
15182 cp_token *token = cp_lexer_peek_token (parser->lexer);
15183 if (pedantic && !token->in_system_header)
15184 pedwarn ("%Hextra %<;%>", &token->location);
15186 else
15188 tree type;
15190 /* See if this declaration is a friend. */
15191 friend_p = cp_parser_friend_p (&decl_specifiers);
15192 /* If there were decl-specifiers, check to see if there was
15193 a class-declaration. */
15194 type = check_tag_decl (&decl_specifiers);
15195 /* Nested classes have already been added to the class, but
15196 a `friend' needs to be explicitly registered. */
15197 if (friend_p)
15199 /* If the `friend' keyword was present, the friend must
15200 be introduced with a class-key. */
15201 if (!declares_class_or_enum)
15202 error ("a class-key must be used when declaring a friend");
15203 /* In this case:
15205 template <typename T> struct A {
15206 friend struct A<T>::B;
15209 A<T>::B will be represented by a TYPENAME_TYPE, and
15210 therefore not recognized by check_tag_decl. */
15211 if (!type
15212 && decl_specifiers.type
15213 && TYPE_P (decl_specifiers.type))
15214 type = decl_specifiers.type;
15215 if (!type || !TYPE_P (type))
15216 error ("friend declaration does not name a class or "
15217 "function");
15218 else
15219 make_friend_class (current_class_type, type,
15220 /*complain=*/true);
15222 /* If there is no TYPE, an error message will already have
15223 been issued. */
15224 else if (!type || type == error_mark_node)
15226 /* An anonymous aggregate has to be handled specially; such
15227 a declaration really declares a data member (with a
15228 particular type), as opposed to a nested class. */
15229 else if (ANON_AGGR_TYPE_P (type))
15231 /* Remove constructors and such from TYPE, now that we
15232 know it is an anonymous aggregate. */
15233 fixup_anonymous_aggr (type);
15234 /* And make the corresponding data member. */
15235 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15236 /* Add it to the class. */
15237 finish_member_declaration (decl);
15239 else
15240 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
15243 else
15245 /* See if these declarations will be friends. */
15246 friend_p = cp_parser_friend_p (&decl_specifiers);
15248 /* Keep going until we hit the `;' at the end of the
15249 declaration. */
15250 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15252 tree attributes = NULL_TREE;
15253 tree first_attribute;
15255 /* Peek at the next token. */
15256 token = cp_lexer_peek_token (parser->lexer);
15258 /* Check for a bitfield declaration. */
15259 if (token->type == CPP_COLON
15260 || (token->type == CPP_NAME
15261 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15262 == CPP_COLON))
15264 tree identifier;
15265 tree width;
15267 /* Get the name of the bitfield. Note that we cannot just
15268 check TOKEN here because it may have been invalidated by
15269 the call to cp_lexer_peek_nth_token above. */
15270 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15271 identifier = cp_parser_identifier (parser);
15272 else
15273 identifier = NULL_TREE;
15275 /* Consume the `:' token. */
15276 cp_lexer_consume_token (parser->lexer);
15277 /* Get the width of the bitfield. */
15278 width
15279 = cp_parser_constant_expression (parser,
15280 /*allow_non_constant=*/false,
15281 NULL);
15283 /* Look for attributes that apply to the bitfield. */
15284 attributes = cp_parser_attributes_opt (parser);
15285 /* Remember which attributes are prefix attributes and
15286 which are not. */
15287 first_attribute = attributes;
15288 /* Combine the attributes. */
15289 attributes = chainon (prefix_attributes, attributes);
15291 /* Create the bitfield declaration. */
15292 decl = grokbitfield (identifier
15293 ? make_id_declarator (NULL_TREE,
15294 identifier,
15295 sfk_none)
15296 : NULL,
15297 &decl_specifiers,
15298 width);
15299 /* Apply the attributes. */
15300 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
15302 else
15304 cp_declarator *declarator;
15305 tree initializer;
15306 tree asm_specification;
15307 int ctor_dtor_or_conv_p;
15309 /* Parse the declarator. */
15310 declarator
15311 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15312 &ctor_dtor_or_conv_p,
15313 /*parenthesized_p=*/NULL,
15314 /*member_p=*/true);
15316 /* If something went wrong parsing the declarator, make sure
15317 that we at least consume some tokens. */
15318 if (declarator == cp_error_declarator)
15320 /* Skip to the end of the statement. */
15321 cp_parser_skip_to_end_of_statement (parser);
15322 /* If the next token is not a semicolon, that is
15323 probably because we just skipped over the body of
15324 a function. So, we consume a semicolon if
15325 present, but do not issue an error message if it
15326 is not present. */
15327 if (cp_lexer_next_token_is (parser->lexer,
15328 CPP_SEMICOLON))
15329 cp_lexer_consume_token (parser->lexer);
15330 return;
15333 if (declares_class_or_enum & 2)
15334 cp_parser_check_for_definition_in_return_type
15335 (declarator, decl_specifiers.type);
15337 /* Look for an asm-specification. */
15338 asm_specification = cp_parser_asm_specification_opt (parser);
15339 /* Look for attributes that apply to the declaration. */
15340 attributes = cp_parser_attributes_opt (parser);
15341 /* Remember which attributes are prefix attributes and
15342 which are not. */
15343 first_attribute = attributes;
15344 /* Combine the attributes. */
15345 attributes = chainon (prefix_attributes, attributes);
15347 /* If it's an `=', then we have a constant-initializer or a
15348 pure-specifier. It is not correct to parse the
15349 initializer before registering the member declaration
15350 since the member declaration should be in scope while
15351 its initializer is processed. However, the rest of the
15352 front end does not yet provide an interface that allows
15353 us to handle this correctly. */
15354 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15356 /* In [class.mem]:
15358 A pure-specifier shall be used only in the declaration of
15359 a virtual function.
15361 A member-declarator can contain a constant-initializer
15362 only if it declares a static member of integral or
15363 enumeration type.
15365 Therefore, if the DECLARATOR is for a function, we look
15366 for a pure-specifier; otherwise, we look for a
15367 constant-initializer. When we call `grokfield', it will
15368 perform more stringent semantics checks. */
15369 if (function_declarator_p (declarator))
15370 initializer = cp_parser_pure_specifier (parser);
15371 else
15372 /* Parse the initializer. */
15373 initializer = cp_parser_constant_initializer (parser);
15375 /* Otherwise, there is no initializer. */
15376 else
15377 initializer = NULL_TREE;
15379 /* See if we are probably looking at a function
15380 definition. We are certainly not looking at a
15381 member-declarator. Calling `grokfield' has
15382 side-effects, so we must not do it unless we are sure
15383 that we are looking at a member-declarator. */
15384 if (cp_parser_token_starts_function_definition_p
15385 (cp_lexer_peek_token (parser->lexer)))
15387 /* The grammar does not allow a pure-specifier to be
15388 used when a member function is defined. (It is
15389 possible that this fact is an oversight in the
15390 standard, since a pure function may be defined
15391 outside of the class-specifier. */
15392 if (initializer)
15393 error ("pure-specifier on function-definition");
15394 decl = cp_parser_save_member_function_body (parser,
15395 &decl_specifiers,
15396 declarator,
15397 attributes);
15398 /* If the member was not a friend, declare it here. */
15399 if (!friend_p)
15400 finish_member_declaration (decl);
15401 /* Peek at the next token. */
15402 token = cp_lexer_peek_token (parser->lexer);
15403 /* If the next token is a semicolon, consume it. */
15404 if (token->type == CPP_SEMICOLON)
15405 cp_lexer_consume_token (parser->lexer);
15406 return;
15408 else
15409 /* Create the declaration. */
15410 decl = grokfield (declarator, &decl_specifiers,
15411 initializer, /*init_const_expr_p=*/true,
15412 asm_specification,
15413 attributes);
15416 /* Reset PREFIX_ATTRIBUTES. */
15417 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15418 attributes = TREE_CHAIN (attributes);
15419 if (attributes)
15420 TREE_CHAIN (attributes) = NULL_TREE;
15422 /* If there is any qualification still in effect, clear it
15423 now; we will be starting fresh with the next declarator. */
15424 parser->scope = NULL_TREE;
15425 parser->qualifying_scope = NULL_TREE;
15426 parser->object_scope = NULL_TREE;
15427 /* If it's a `,', then there are more declarators. */
15428 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15429 cp_lexer_consume_token (parser->lexer);
15430 /* If the next token isn't a `;', then we have a parse error. */
15431 else if (cp_lexer_next_token_is_not (parser->lexer,
15432 CPP_SEMICOLON))
15434 cp_parser_error (parser, "expected %<;%>");
15435 /* Skip tokens until we find a `;'. */
15436 cp_parser_skip_to_end_of_statement (parser);
15438 break;
15441 if (decl)
15443 /* Add DECL to the list of members. */
15444 if (!friend_p)
15445 finish_member_declaration (decl);
15447 if (TREE_CODE (decl) == FUNCTION_DECL)
15448 cp_parser_save_default_args (parser, decl);
15453 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15456 /* Parse a pure-specifier.
15458 pure-specifier:
15461 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15462 Otherwise, ERROR_MARK_NODE is returned. */
15464 static tree
15465 cp_parser_pure_specifier (cp_parser* parser)
15467 cp_token *token;
15469 /* Look for the `=' token. */
15470 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15471 return error_mark_node;
15472 /* Look for the `0' token. */
15473 token = cp_lexer_consume_token (parser->lexer);
15474 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15475 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15477 cp_parser_error (parser,
15478 "invalid pure specifier (only `= 0' is allowed)");
15479 cp_parser_skip_to_end_of_statement (parser);
15480 return error_mark_node;
15482 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15484 error ("templates may not be %<virtual%>");
15485 return error_mark_node;
15488 return integer_zero_node;
15491 /* Parse a constant-initializer.
15493 constant-initializer:
15494 = constant-expression
15496 Returns a representation of the constant-expression. */
15498 static tree
15499 cp_parser_constant_initializer (cp_parser* parser)
15501 /* Look for the `=' token. */
15502 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15503 return error_mark_node;
15505 /* It is invalid to write:
15507 struct S { static const int i = { 7 }; };
15510 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15512 cp_parser_error (parser,
15513 "a brace-enclosed initializer is not allowed here");
15514 /* Consume the opening brace. */
15515 cp_lexer_consume_token (parser->lexer);
15516 /* Skip the initializer. */
15517 cp_parser_skip_to_closing_brace (parser);
15518 /* Look for the trailing `}'. */
15519 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15521 return error_mark_node;
15524 return cp_parser_constant_expression (parser,
15525 /*allow_non_constant=*/false,
15526 NULL);
15529 /* Derived classes [gram.class.derived] */
15531 /* Parse a base-clause.
15533 base-clause:
15534 : base-specifier-list
15536 base-specifier-list:
15537 base-specifier ... [opt]
15538 base-specifier-list , base-specifier ... [opt]
15540 Returns a TREE_LIST representing the base-classes, in the order in
15541 which they were declared. The representation of each node is as
15542 described by cp_parser_base_specifier.
15544 In the case that no bases are specified, this function will return
15545 NULL_TREE, not ERROR_MARK_NODE. */
15547 static tree
15548 cp_parser_base_clause (cp_parser* parser)
15550 tree bases = NULL_TREE;
15552 /* Look for the `:' that begins the list. */
15553 cp_parser_require (parser, CPP_COLON, "`:'");
15555 /* Scan the base-specifier-list. */
15556 while (true)
15558 cp_token *token;
15559 tree base;
15560 bool pack_expansion_p = false;
15562 /* Look for the base-specifier. */
15563 base = cp_parser_base_specifier (parser);
15564 /* Look for the (optional) ellipsis. */
15565 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15567 /* Consume the `...'. */
15568 cp_lexer_consume_token (parser->lexer);
15570 pack_expansion_p = true;
15573 /* Add BASE to the front of the list. */
15574 if (base != error_mark_node)
15576 if (pack_expansion_p)
15577 /* Make this a pack expansion type. */
15578 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15581 if (!check_for_bare_parameter_packs (&TREE_VALUE (base)))
15583 TREE_CHAIN (base) = bases;
15584 bases = base;
15587 /* Peek at the next token. */
15588 token = cp_lexer_peek_token (parser->lexer);
15589 /* If it's not a comma, then the list is complete. */
15590 if (token->type != CPP_COMMA)
15591 break;
15592 /* Consume the `,'. */
15593 cp_lexer_consume_token (parser->lexer);
15596 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15597 base class had a qualified name. However, the next name that
15598 appears is certainly not qualified. */
15599 parser->scope = NULL_TREE;
15600 parser->qualifying_scope = NULL_TREE;
15601 parser->object_scope = NULL_TREE;
15603 return nreverse (bases);
15606 /* Parse a base-specifier.
15608 base-specifier:
15609 :: [opt] nested-name-specifier [opt] class-name
15610 virtual access-specifier [opt] :: [opt] nested-name-specifier
15611 [opt] class-name
15612 access-specifier virtual [opt] :: [opt] nested-name-specifier
15613 [opt] class-name
15615 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15616 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15617 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15618 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15620 static tree
15621 cp_parser_base_specifier (cp_parser* parser)
15623 cp_token *token;
15624 bool done = false;
15625 bool virtual_p = false;
15626 bool duplicate_virtual_error_issued_p = false;
15627 bool duplicate_access_error_issued_p = false;
15628 bool class_scope_p, template_p;
15629 tree access = access_default_node;
15630 tree type;
15632 /* Process the optional `virtual' and `access-specifier'. */
15633 while (!done)
15635 /* Peek at the next token. */
15636 token = cp_lexer_peek_token (parser->lexer);
15637 /* Process `virtual'. */
15638 switch (token->keyword)
15640 case RID_VIRTUAL:
15641 /* If `virtual' appears more than once, issue an error. */
15642 if (virtual_p && !duplicate_virtual_error_issued_p)
15644 cp_parser_error (parser,
15645 "%<virtual%> specified more than once in base-specified");
15646 duplicate_virtual_error_issued_p = true;
15649 virtual_p = true;
15651 /* Consume the `virtual' token. */
15652 cp_lexer_consume_token (parser->lexer);
15654 break;
15656 case RID_PUBLIC:
15657 case RID_PROTECTED:
15658 case RID_PRIVATE:
15659 /* If more than one access specifier appears, issue an
15660 error. */
15661 if (access != access_default_node
15662 && !duplicate_access_error_issued_p)
15664 cp_parser_error (parser,
15665 "more than one access specifier in base-specified");
15666 duplicate_access_error_issued_p = true;
15669 access = ridpointers[(int) token->keyword];
15671 /* Consume the access-specifier. */
15672 cp_lexer_consume_token (parser->lexer);
15674 break;
15676 default:
15677 done = true;
15678 break;
15681 /* It is not uncommon to see programs mechanically, erroneously, use
15682 the 'typename' keyword to denote (dependent) qualified types
15683 as base classes. */
15684 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15686 if (!processing_template_decl)
15687 error ("keyword %<typename%> not allowed outside of templates");
15688 else
15689 error ("keyword %<typename%> not allowed in this context "
15690 "(the base class is implicitly a type)");
15691 cp_lexer_consume_token (parser->lexer);
15694 /* Look for the optional `::' operator. */
15695 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15696 /* Look for the nested-name-specifier. The simplest way to
15697 implement:
15699 [temp.res]
15701 The keyword `typename' is not permitted in a base-specifier or
15702 mem-initializer; in these contexts a qualified name that
15703 depends on a template-parameter is implicitly assumed to be a
15704 type name.
15706 is to pretend that we have seen the `typename' keyword at this
15707 point. */
15708 cp_parser_nested_name_specifier_opt (parser,
15709 /*typename_keyword_p=*/true,
15710 /*check_dependency_p=*/true,
15711 typename_type,
15712 /*is_declaration=*/true);
15713 /* If the base class is given by a qualified name, assume that names
15714 we see are type names or templates, as appropriate. */
15715 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15716 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15718 /* Finally, look for the class-name. */
15719 type = cp_parser_class_name (parser,
15720 class_scope_p,
15721 template_p,
15722 typename_type,
15723 /*check_dependency_p=*/true,
15724 /*class_head_p=*/false,
15725 /*is_declaration=*/true);
15727 if (type == error_mark_node)
15728 return error_mark_node;
15730 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15733 /* Exception handling [gram.exception] */
15735 /* Parse an (optional) exception-specification.
15737 exception-specification:
15738 throw ( type-id-list [opt] )
15740 Returns a TREE_LIST representing the exception-specification. The
15741 TREE_VALUE of each node is a type. */
15743 static tree
15744 cp_parser_exception_specification_opt (cp_parser* parser)
15746 cp_token *token;
15747 tree type_id_list;
15749 /* Peek at the next token. */
15750 token = cp_lexer_peek_token (parser->lexer);
15751 /* If it's not `throw', then there's no exception-specification. */
15752 if (!cp_parser_is_keyword (token, RID_THROW))
15753 return NULL_TREE;
15755 /* Consume the `throw'. */
15756 cp_lexer_consume_token (parser->lexer);
15758 /* Look for the `('. */
15759 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15761 /* Peek at the next token. */
15762 token = cp_lexer_peek_token (parser->lexer);
15763 /* If it's not a `)', then there is a type-id-list. */
15764 if (token->type != CPP_CLOSE_PAREN)
15766 const char *saved_message;
15768 /* Types may not be defined in an exception-specification. */
15769 saved_message = parser->type_definition_forbidden_message;
15770 parser->type_definition_forbidden_message
15771 = "types may not be defined in an exception-specification";
15772 /* Parse the type-id-list. */
15773 type_id_list = cp_parser_type_id_list (parser);
15774 /* Restore the saved message. */
15775 parser->type_definition_forbidden_message = saved_message;
15777 else
15778 type_id_list = empty_except_spec;
15780 /* Look for the `)'. */
15781 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15783 return type_id_list;
15786 /* Parse an (optional) type-id-list.
15788 type-id-list:
15789 type-id ... [opt]
15790 type-id-list , type-id ... [opt]
15792 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15793 in the order that the types were presented. */
15795 static tree
15796 cp_parser_type_id_list (cp_parser* parser)
15798 tree types = NULL_TREE;
15800 while (true)
15802 cp_token *token;
15803 tree type;
15805 /* Get the next type-id. */
15806 type = cp_parser_type_id (parser);
15807 /* Parse the optional ellipsis. */
15808 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15810 /* Consume the `...'. */
15811 cp_lexer_consume_token (parser->lexer);
15813 /* Turn the type into a pack expansion expression. */
15814 type = make_pack_expansion (type);
15816 /* Add it to the list. */
15817 types = add_exception_specifier (types, type, /*complain=*/1);
15818 /* Peek at the next token. */
15819 token = cp_lexer_peek_token (parser->lexer);
15820 /* If it is not a `,', we are done. */
15821 if (token->type != CPP_COMMA)
15822 break;
15823 /* Consume the `,'. */
15824 cp_lexer_consume_token (parser->lexer);
15827 return nreverse (types);
15830 /* Parse a try-block.
15832 try-block:
15833 try compound-statement handler-seq */
15835 static tree
15836 cp_parser_try_block (cp_parser* parser)
15838 tree try_block;
15840 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15841 try_block = begin_try_block ();
15842 cp_parser_compound_statement (parser, NULL, true);
15843 finish_try_block (try_block);
15844 cp_parser_handler_seq (parser);
15845 finish_handler_sequence (try_block);
15847 return try_block;
15850 /* Parse a function-try-block.
15852 function-try-block:
15853 try ctor-initializer [opt] function-body handler-seq */
15855 static bool
15856 cp_parser_function_try_block (cp_parser* parser)
15858 tree compound_stmt;
15859 tree try_block;
15860 bool ctor_initializer_p;
15862 /* Look for the `try' keyword. */
15863 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15864 return false;
15865 /* Let the rest of the front end know where we are. */
15866 try_block = begin_function_try_block (&compound_stmt);
15867 /* Parse the function-body. */
15868 ctor_initializer_p
15869 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15870 /* We're done with the `try' part. */
15871 finish_function_try_block (try_block);
15872 /* Parse the handlers. */
15873 cp_parser_handler_seq (parser);
15874 /* We're done with the handlers. */
15875 finish_function_handler_sequence (try_block, compound_stmt);
15877 return ctor_initializer_p;
15880 /* Parse a handler-seq.
15882 handler-seq:
15883 handler handler-seq [opt] */
15885 static void
15886 cp_parser_handler_seq (cp_parser* parser)
15888 while (true)
15890 cp_token *token;
15892 /* Parse the handler. */
15893 cp_parser_handler (parser);
15894 /* Peek at the next token. */
15895 token = cp_lexer_peek_token (parser->lexer);
15896 /* If it's not `catch' then there are no more handlers. */
15897 if (!cp_parser_is_keyword (token, RID_CATCH))
15898 break;
15902 /* Parse a handler.
15904 handler:
15905 catch ( exception-declaration ) compound-statement */
15907 static void
15908 cp_parser_handler (cp_parser* parser)
15910 tree handler;
15911 tree declaration;
15913 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15914 handler = begin_handler ();
15915 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15916 declaration = cp_parser_exception_declaration (parser);
15917 finish_handler_parms (declaration, handler);
15918 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15919 cp_parser_compound_statement (parser, NULL, false);
15920 finish_handler (handler);
15923 /* Parse an exception-declaration.
15925 exception-declaration:
15926 type-specifier-seq declarator
15927 type-specifier-seq abstract-declarator
15928 type-specifier-seq
15931 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15932 ellipsis variant is used. */
15934 static tree
15935 cp_parser_exception_declaration (cp_parser* parser)
15937 cp_decl_specifier_seq type_specifiers;
15938 cp_declarator *declarator;
15939 const char *saved_message;
15941 /* If it's an ellipsis, it's easy to handle. */
15942 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15944 /* Consume the `...' token. */
15945 cp_lexer_consume_token (parser->lexer);
15946 return NULL_TREE;
15949 /* Types may not be defined in exception-declarations. */
15950 saved_message = parser->type_definition_forbidden_message;
15951 parser->type_definition_forbidden_message
15952 = "types may not be defined in exception-declarations";
15954 /* Parse the type-specifier-seq. */
15955 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15956 &type_specifiers);
15957 /* If it's a `)', then there is no declarator. */
15958 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15959 declarator = NULL;
15960 else
15961 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15962 /*ctor_dtor_or_conv_p=*/NULL,
15963 /*parenthesized_p=*/NULL,
15964 /*member_p=*/false);
15966 /* Restore the saved message. */
15967 parser->type_definition_forbidden_message = saved_message;
15969 if (!type_specifiers.any_specifiers_p)
15970 return error_mark_node;
15972 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15975 /* Parse a throw-expression.
15977 throw-expression:
15978 throw assignment-expression [opt]
15980 Returns a THROW_EXPR representing the throw-expression. */
15982 static tree
15983 cp_parser_throw_expression (cp_parser* parser)
15985 tree expression;
15986 cp_token* token;
15988 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15989 token = cp_lexer_peek_token (parser->lexer);
15990 /* Figure out whether or not there is an assignment-expression
15991 following the "throw" keyword. */
15992 if (token->type == CPP_COMMA
15993 || token->type == CPP_SEMICOLON
15994 || token->type == CPP_CLOSE_PAREN
15995 || token->type == CPP_CLOSE_SQUARE
15996 || token->type == CPP_CLOSE_BRACE
15997 || token->type == CPP_COLON)
15998 expression = NULL_TREE;
15999 else
16000 expression = cp_parser_assignment_expression (parser,
16001 /*cast_p=*/false);
16003 return build_throw (expression);
16006 /* GNU Extensions */
16008 /* Parse an (optional) asm-specification.
16010 asm-specification:
16011 asm ( string-literal )
16013 If the asm-specification is present, returns a STRING_CST
16014 corresponding to the string-literal. Otherwise, returns
16015 NULL_TREE. */
16017 static tree
16018 cp_parser_asm_specification_opt (cp_parser* parser)
16020 cp_token *token;
16021 tree asm_specification;
16023 /* Peek at the next token. */
16024 token = cp_lexer_peek_token (parser->lexer);
16025 /* If the next token isn't the `asm' keyword, then there's no
16026 asm-specification. */
16027 if (!cp_parser_is_keyword (token, RID_ASM))
16028 return NULL_TREE;
16030 /* Consume the `asm' token. */
16031 cp_lexer_consume_token (parser->lexer);
16032 /* Look for the `('. */
16033 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16035 /* Look for the string-literal. */
16036 asm_specification = cp_parser_string_literal (parser, false, false);
16038 /* Look for the `)'. */
16039 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
16041 return asm_specification;
16044 /* Parse an asm-operand-list.
16046 asm-operand-list:
16047 asm-operand
16048 asm-operand-list , asm-operand
16050 asm-operand:
16051 string-literal ( expression )
16052 [ string-literal ] string-literal ( expression )
16054 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16055 each node is the expression. The TREE_PURPOSE is itself a
16056 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16057 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16058 is a STRING_CST for the string literal before the parenthesis. Returns
16059 ERROR_MARK_NODE if any of the operands are invalid. */
16061 static tree
16062 cp_parser_asm_operand_list (cp_parser* parser)
16064 tree asm_operands = NULL_TREE;
16065 bool invalid_operands = false;
16067 while (true)
16069 tree string_literal;
16070 tree expression;
16071 tree name;
16073 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16075 /* Consume the `[' token. */
16076 cp_lexer_consume_token (parser->lexer);
16077 /* Read the operand name. */
16078 name = cp_parser_identifier (parser);
16079 if (name != error_mark_node)
16080 name = build_string (IDENTIFIER_LENGTH (name),
16081 IDENTIFIER_POINTER (name));
16082 /* Look for the closing `]'. */
16083 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16085 else
16086 name = NULL_TREE;
16087 /* Look for the string-literal. */
16088 string_literal = cp_parser_string_literal (parser, false, false);
16090 /* Look for the `('. */
16091 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16092 /* Parse the expression. */
16093 expression = cp_parser_expression (parser, /*cast_p=*/false);
16094 /* Look for the `)'. */
16095 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16097 if (name == error_mark_node
16098 || string_literal == error_mark_node
16099 || expression == error_mark_node)
16100 invalid_operands = true;
16102 /* Add this operand to the list. */
16103 asm_operands = tree_cons (build_tree_list (name, string_literal),
16104 expression,
16105 asm_operands);
16106 /* If the next token is not a `,', there are no more
16107 operands. */
16108 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16109 break;
16110 /* Consume the `,'. */
16111 cp_lexer_consume_token (parser->lexer);
16114 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16117 /* Parse an asm-clobber-list.
16119 asm-clobber-list:
16120 string-literal
16121 asm-clobber-list , string-literal
16123 Returns a TREE_LIST, indicating the clobbers in the order that they
16124 appeared. The TREE_VALUE of each node is a STRING_CST. */
16126 static tree
16127 cp_parser_asm_clobber_list (cp_parser* parser)
16129 tree clobbers = NULL_TREE;
16131 while (true)
16133 tree string_literal;
16135 /* Look for the string literal. */
16136 string_literal = cp_parser_string_literal (parser, false, false);
16137 /* Add it to the list. */
16138 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16139 /* If the next token is not a `,', then the list is
16140 complete. */
16141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16142 break;
16143 /* Consume the `,' token. */
16144 cp_lexer_consume_token (parser->lexer);
16147 return clobbers;
16150 /* Parse an (optional) series of attributes.
16152 attributes:
16153 attributes attribute
16155 attribute:
16156 __attribute__ (( attribute-list [opt] ))
16158 The return value is as for cp_parser_attribute_list. */
16160 static tree
16161 cp_parser_attributes_opt (cp_parser* parser)
16163 tree attributes = NULL_TREE;
16165 while (true)
16167 cp_token *token;
16168 tree attribute_list;
16170 /* Peek at the next token. */
16171 token = cp_lexer_peek_token (parser->lexer);
16172 /* If it's not `__attribute__', then we're done. */
16173 if (token->keyword != RID_ATTRIBUTE)
16174 break;
16176 /* Consume the `__attribute__' keyword. */
16177 cp_lexer_consume_token (parser->lexer);
16178 /* Look for the two `(' tokens. */
16179 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16180 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16182 /* Peek at the next token. */
16183 token = cp_lexer_peek_token (parser->lexer);
16184 if (token->type != CPP_CLOSE_PAREN)
16185 /* Parse the attribute-list. */
16186 attribute_list = cp_parser_attribute_list (parser);
16187 else
16188 /* If the next token is a `)', then there is no attribute
16189 list. */
16190 attribute_list = NULL;
16192 /* Look for the two `)' tokens. */
16193 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16194 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16196 /* Add these new attributes to the list. */
16197 attributes = chainon (attributes, attribute_list);
16200 return attributes;
16203 /* Parse an attribute-list.
16205 attribute-list:
16206 attribute
16207 attribute-list , attribute
16209 attribute:
16210 identifier
16211 identifier ( identifier )
16212 identifier ( identifier , expression-list )
16213 identifier ( expression-list )
16215 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16216 to an attribute. The TREE_PURPOSE of each node is the identifier
16217 indicating which attribute is in use. The TREE_VALUE represents
16218 the arguments, if any. */
16220 static tree
16221 cp_parser_attribute_list (cp_parser* parser)
16223 tree attribute_list = NULL_TREE;
16224 bool save_translate_strings_p = parser->translate_strings_p;
16226 parser->translate_strings_p = false;
16227 while (true)
16229 cp_token *token;
16230 tree identifier;
16231 tree attribute;
16233 /* Look for the identifier. We also allow keywords here; for
16234 example `__attribute__ ((const))' is legal. */
16235 token = cp_lexer_peek_token (parser->lexer);
16236 if (token->type == CPP_NAME
16237 || token->type == CPP_KEYWORD)
16239 tree arguments = NULL_TREE;
16241 /* Consume the token. */
16242 token = cp_lexer_consume_token (parser->lexer);
16244 /* Save away the identifier that indicates which attribute
16245 this is. */
16246 identifier = token->u.value;
16247 attribute = build_tree_list (identifier, NULL_TREE);
16249 /* Peek at the next token. */
16250 token = cp_lexer_peek_token (parser->lexer);
16251 /* If it's an `(', then parse the attribute arguments. */
16252 if (token->type == CPP_OPEN_PAREN)
16254 arguments = cp_parser_parenthesized_expression_list
16255 (parser, true, /*cast_p=*/false,
16256 /*allow_expansion_p=*/false,
16257 /*non_constant_p=*/NULL);
16258 /* Save the arguments away. */
16259 TREE_VALUE (attribute) = arguments;
16262 if (arguments != error_mark_node)
16264 /* Add this attribute to the list. */
16265 TREE_CHAIN (attribute) = attribute_list;
16266 attribute_list = attribute;
16269 token = cp_lexer_peek_token (parser->lexer);
16271 /* Now, look for more attributes. If the next token isn't a
16272 `,', we're done. */
16273 if (token->type != CPP_COMMA)
16274 break;
16276 /* Consume the comma and keep going. */
16277 cp_lexer_consume_token (parser->lexer);
16279 parser->translate_strings_p = save_translate_strings_p;
16281 /* We built up the list in reverse order. */
16282 return nreverse (attribute_list);
16285 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16286 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16287 current value of the PEDANTIC flag, regardless of whether or not
16288 the `__extension__' keyword is present. The caller is responsible
16289 for restoring the value of the PEDANTIC flag. */
16291 static bool
16292 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16294 /* Save the old value of the PEDANTIC flag. */
16295 *saved_pedantic = pedantic;
16297 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16299 /* Consume the `__extension__' token. */
16300 cp_lexer_consume_token (parser->lexer);
16301 /* We're not being pedantic while the `__extension__' keyword is
16302 in effect. */
16303 pedantic = 0;
16305 return true;
16308 return false;
16311 /* Parse a label declaration.
16313 label-declaration:
16314 __label__ label-declarator-seq ;
16316 label-declarator-seq:
16317 identifier , label-declarator-seq
16318 identifier */
16320 static void
16321 cp_parser_label_declaration (cp_parser* parser)
16323 /* Look for the `__label__' keyword. */
16324 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
16326 while (true)
16328 tree identifier;
16330 /* Look for an identifier. */
16331 identifier = cp_parser_identifier (parser);
16332 /* If we failed, stop. */
16333 if (identifier == error_mark_node)
16334 break;
16335 /* Declare it as a label. */
16336 finish_label_decl (identifier);
16337 /* If the next token is a `;', stop. */
16338 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16339 break;
16340 /* Look for the `,' separating the label declarations. */
16341 cp_parser_require (parser, CPP_COMMA, "`,'");
16344 /* Look for the final `;'. */
16345 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
16348 /* Support Functions */
16350 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16351 NAME should have one of the representations used for an
16352 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16353 is returned. If PARSER->SCOPE is a dependent type, then a
16354 SCOPE_REF is returned.
16356 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16357 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16358 was formed. Abstractly, such entities should not be passed to this
16359 function, because they do not need to be looked up, but it is
16360 simpler to check for this special case here, rather than at the
16361 call-sites.
16363 In cases not explicitly covered above, this function returns a
16364 DECL, OVERLOAD, or baselink representing the result of the lookup.
16365 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16366 is returned.
16368 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16369 (e.g., "struct") that was used. In that case bindings that do not
16370 refer to types are ignored.
16372 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16373 ignored.
16375 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16376 are ignored.
16378 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16379 types.
16381 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16382 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16383 NULL_TREE otherwise. */
16385 static tree
16386 cp_parser_lookup_name (cp_parser *parser, tree name,
16387 enum tag_types tag_type,
16388 bool is_template,
16389 bool is_namespace,
16390 bool check_dependency,
16391 tree *ambiguous_decls)
16393 int flags = 0;
16394 tree decl;
16395 tree object_type = parser->context->object_type;
16397 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16398 flags |= LOOKUP_COMPLAIN;
16400 /* Assume that the lookup will be unambiguous. */
16401 if (ambiguous_decls)
16402 *ambiguous_decls = NULL_TREE;
16404 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16405 no longer valid. Note that if we are parsing tentatively, and
16406 the parse fails, OBJECT_TYPE will be automatically restored. */
16407 parser->context->object_type = NULL_TREE;
16409 if (name == error_mark_node)
16410 return error_mark_node;
16412 /* A template-id has already been resolved; there is no lookup to
16413 do. */
16414 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16415 return name;
16416 if (BASELINK_P (name))
16418 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16419 == TEMPLATE_ID_EXPR);
16420 return name;
16423 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16424 it should already have been checked to make sure that the name
16425 used matches the type being destroyed. */
16426 if (TREE_CODE (name) == BIT_NOT_EXPR)
16428 tree type;
16430 /* Figure out to which type this destructor applies. */
16431 if (parser->scope)
16432 type = parser->scope;
16433 else if (object_type)
16434 type = object_type;
16435 else
16436 type = current_class_type;
16437 /* If that's not a class type, there is no destructor. */
16438 if (!type || !CLASS_TYPE_P (type))
16439 return error_mark_node;
16440 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16441 lazily_declare_fn (sfk_destructor, type);
16442 if (!CLASSTYPE_DESTRUCTORS (type))
16443 return error_mark_node;
16444 /* If it was a class type, return the destructor. */
16445 return CLASSTYPE_DESTRUCTORS (type);
16448 /* By this point, the NAME should be an ordinary identifier. If
16449 the id-expression was a qualified name, the qualifying scope is
16450 stored in PARSER->SCOPE at this point. */
16451 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16453 /* Perform the lookup. */
16454 if (parser->scope)
16456 bool dependent_p;
16458 if (parser->scope == error_mark_node)
16459 return error_mark_node;
16461 /* If the SCOPE is dependent, the lookup must be deferred until
16462 the template is instantiated -- unless we are explicitly
16463 looking up names in uninstantiated templates. Even then, we
16464 cannot look up the name if the scope is not a class type; it
16465 might, for example, be a template type parameter. */
16466 dependent_p = (TYPE_P (parser->scope)
16467 && !(parser->in_declarator_p
16468 && currently_open_class (parser->scope))
16469 && dependent_type_p (parser->scope));
16470 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16471 && dependent_p)
16473 if (tag_type)
16475 tree type;
16477 /* The resolution to Core Issue 180 says that `struct
16478 A::B' should be considered a type-name, even if `A'
16479 is dependent. */
16480 type = make_typename_type (parser->scope, name, tag_type,
16481 /*complain=*/tf_error);
16482 decl = TYPE_NAME (type);
16484 else if (is_template
16485 && (cp_parser_next_token_ends_template_argument_p (parser)
16486 || cp_lexer_next_token_is (parser->lexer,
16487 CPP_CLOSE_PAREN)))
16488 decl = make_unbound_class_template (parser->scope,
16489 name, NULL_TREE,
16490 /*complain=*/tf_error);
16491 else
16492 decl = build_qualified_name (/*type=*/NULL_TREE,
16493 parser->scope, name,
16494 is_template);
16496 else
16498 tree pushed_scope = NULL_TREE;
16500 /* If PARSER->SCOPE is a dependent type, then it must be a
16501 class type, and we must not be checking dependencies;
16502 otherwise, we would have processed this lookup above. So
16503 that PARSER->SCOPE is not considered a dependent base by
16504 lookup_member, we must enter the scope here. */
16505 if (dependent_p)
16506 pushed_scope = push_scope (parser->scope);
16507 /* If the PARSER->SCOPE is a template specialization, it
16508 may be instantiated during name lookup. In that case,
16509 errors may be issued. Even if we rollback the current
16510 tentative parse, those errors are valid. */
16511 decl = lookup_qualified_name (parser->scope, name,
16512 tag_type != none_type,
16513 /*complain=*/true);
16514 if (pushed_scope)
16515 pop_scope (pushed_scope);
16517 parser->qualifying_scope = parser->scope;
16518 parser->object_scope = NULL_TREE;
16520 else if (object_type)
16522 tree object_decl = NULL_TREE;
16523 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16524 OBJECT_TYPE is not a class. */
16525 if (CLASS_TYPE_P (object_type))
16526 /* If the OBJECT_TYPE is a template specialization, it may
16527 be instantiated during name lookup. In that case, errors
16528 may be issued. Even if we rollback the current tentative
16529 parse, those errors are valid. */
16530 object_decl = lookup_member (object_type,
16531 name,
16532 /*protect=*/0,
16533 tag_type != none_type);
16534 /* Look it up in the enclosing context, too. */
16535 decl = lookup_name_real (name, tag_type != none_type,
16536 /*nonclass=*/0,
16537 /*block_p=*/true, is_namespace, flags);
16538 parser->object_scope = object_type;
16539 parser->qualifying_scope = NULL_TREE;
16540 if (object_decl)
16541 decl = object_decl;
16543 else
16545 decl = lookup_name_real (name, tag_type != none_type,
16546 /*nonclass=*/0,
16547 /*block_p=*/true, is_namespace, flags);
16548 parser->qualifying_scope = NULL_TREE;
16549 parser->object_scope = NULL_TREE;
16552 /* If the lookup failed, let our caller know. */
16553 if (!decl || decl == error_mark_node)
16554 return error_mark_node;
16556 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16557 if (TREE_CODE (decl) == TREE_LIST)
16559 if (ambiguous_decls)
16560 *ambiguous_decls = decl;
16561 /* The error message we have to print is too complicated for
16562 cp_parser_error, so we incorporate its actions directly. */
16563 if (!cp_parser_simulate_error (parser))
16565 error ("reference to %qD is ambiguous", name);
16566 print_candidates (decl);
16568 return error_mark_node;
16571 gcc_assert (DECL_P (decl)
16572 || TREE_CODE (decl) == OVERLOAD
16573 || TREE_CODE (decl) == SCOPE_REF
16574 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16575 || BASELINK_P (decl));
16577 /* If we have resolved the name of a member declaration, check to
16578 see if the declaration is accessible. When the name resolves to
16579 set of overloaded functions, accessibility is checked when
16580 overload resolution is done.
16582 During an explicit instantiation, access is not checked at all,
16583 as per [temp.explicit]. */
16584 if (DECL_P (decl))
16585 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16587 return decl;
16590 /* Like cp_parser_lookup_name, but for use in the typical case where
16591 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16592 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16594 static tree
16595 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16597 return cp_parser_lookup_name (parser, name,
16598 none_type,
16599 /*is_template=*/false,
16600 /*is_namespace=*/false,
16601 /*check_dependency=*/true,
16602 /*ambiguous_decls=*/NULL);
16605 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16606 the current context, return the TYPE_DECL. If TAG_NAME_P is
16607 true, the DECL indicates the class being defined in a class-head,
16608 or declared in an elaborated-type-specifier.
16610 Otherwise, return DECL. */
16612 static tree
16613 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16615 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16616 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16618 struct A {
16619 template <typename T> struct B;
16622 template <typename T> struct A::B {};
16624 Similarly, in an elaborated-type-specifier:
16626 namespace N { struct X{}; }
16628 struct A {
16629 template <typename T> friend struct N::X;
16632 However, if the DECL refers to a class type, and we are in
16633 the scope of the class, then the name lookup automatically
16634 finds the TYPE_DECL created by build_self_reference rather
16635 than a TEMPLATE_DECL. For example, in:
16637 template <class T> struct S {
16638 S s;
16641 there is no need to handle such case. */
16643 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16644 return DECL_TEMPLATE_RESULT (decl);
16646 return decl;
16649 /* If too many, or too few, template-parameter lists apply to the
16650 declarator, issue an error message. Returns TRUE if all went well,
16651 and FALSE otherwise. */
16653 static bool
16654 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16655 cp_declarator *declarator)
16657 unsigned num_templates;
16659 /* We haven't seen any classes that involve template parameters yet. */
16660 num_templates = 0;
16662 switch (declarator->kind)
16664 case cdk_id:
16665 if (declarator->u.id.qualifying_scope)
16667 tree scope;
16668 tree member;
16670 scope = declarator->u.id.qualifying_scope;
16671 member = declarator->u.id.unqualified_name;
16673 while (scope && CLASS_TYPE_P (scope))
16675 /* You're supposed to have one `template <...>'
16676 for every template class, but you don't need one
16677 for a full specialization. For example:
16679 template <class T> struct S{};
16680 template <> struct S<int> { void f(); };
16681 void S<int>::f () {}
16683 is correct; there shouldn't be a `template <>' for
16684 the definition of `S<int>::f'. */
16685 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16686 /* If SCOPE does not have template information of any
16687 kind, then it is not a template, nor is it nested
16688 within a template. */
16689 break;
16690 if (explicit_class_specialization_p (scope))
16691 break;
16692 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16693 ++num_templates;
16695 scope = TYPE_CONTEXT (scope);
16698 else if (TREE_CODE (declarator->u.id.unqualified_name)
16699 == TEMPLATE_ID_EXPR)
16700 /* If the DECLARATOR has the form `X<y>' then it uses one
16701 additional level of template parameters. */
16702 ++num_templates;
16704 return cp_parser_check_template_parameters (parser,
16705 num_templates);
16707 case cdk_function:
16708 case cdk_array:
16709 case cdk_pointer:
16710 case cdk_reference:
16711 case cdk_ptrmem:
16712 return (cp_parser_check_declarator_template_parameters
16713 (parser, declarator->declarator));
16715 case cdk_error:
16716 return true;
16718 default:
16719 gcc_unreachable ();
16721 return false;
16724 /* NUM_TEMPLATES were used in the current declaration. If that is
16725 invalid, return FALSE and issue an error messages. Otherwise,
16726 return TRUE. */
16728 static bool
16729 cp_parser_check_template_parameters (cp_parser* parser,
16730 unsigned num_templates)
16732 /* If there are more template classes than parameter lists, we have
16733 something like:
16735 template <class T> void S<T>::R<T>::f (); */
16736 if (parser->num_template_parameter_lists < num_templates)
16738 error ("too few template-parameter-lists");
16739 return false;
16741 /* If there are the same number of template classes and parameter
16742 lists, that's OK. */
16743 if (parser->num_template_parameter_lists == num_templates)
16744 return true;
16745 /* If there are more, but only one more, then we are referring to a
16746 member template. That's OK too. */
16747 if (parser->num_template_parameter_lists == num_templates + 1)
16748 return true;
16749 /* Otherwise, there are too many template parameter lists. We have
16750 something like:
16752 template <class T> template <class U> void S::f(); */
16753 error ("too many template-parameter-lists");
16754 return false;
16757 /* Parse an optional `::' token indicating that the following name is
16758 from the global namespace. If so, PARSER->SCOPE is set to the
16759 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16760 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16761 Returns the new value of PARSER->SCOPE, if the `::' token is
16762 present, and NULL_TREE otherwise. */
16764 static tree
16765 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16767 cp_token *token;
16769 /* Peek at the next token. */
16770 token = cp_lexer_peek_token (parser->lexer);
16771 /* If we're looking at a `::' token then we're starting from the
16772 global namespace, not our current location. */
16773 if (token->type == CPP_SCOPE)
16775 /* Consume the `::' token. */
16776 cp_lexer_consume_token (parser->lexer);
16777 /* Set the SCOPE so that we know where to start the lookup. */
16778 parser->scope = global_namespace;
16779 parser->qualifying_scope = global_namespace;
16780 parser->object_scope = NULL_TREE;
16782 return parser->scope;
16784 else if (!current_scope_valid_p)
16786 parser->scope = NULL_TREE;
16787 parser->qualifying_scope = NULL_TREE;
16788 parser->object_scope = NULL_TREE;
16791 return NULL_TREE;
16794 /* Returns TRUE if the upcoming token sequence is the start of a
16795 constructor declarator. If FRIEND_P is true, the declarator is
16796 preceded by the `friend' specifier. */
16798 static bool
16799 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16801 bool constructor_p;
16802 tree type_decl = NULL_TREE;
16803 bool nested_name_p;
16804 cp_token *next_token;
16806 /* The common case is that this is not a constructor declarator, so
16807 try to avoid doing lots of work if at all possible. It's not
16808 valid declare a constructor at function scope. */
16809 if (parser->in_function_body)
16810 return false;
16811 /* And only certain tokens can begin a constructor declarator. */
16812 next_token = cp_lexer_peek_token (parser->lexer);
16813 if (next_token->type != CPP_NAME
16814 && next_token->type != CPP_SCOPE
16815 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16816 && next_token->type != CPP_TEMPLATE_ID)
16817 return false;
16819 /* Parse tentatively; we are going to roll back all of the tokens
16820 consumed here. */
16821 cp_parser_parse_tentatively (parser);
16822 /* Assume that we are looking at a constructor declarator. */
16823 constructor_p = true;
16825 /* Look for the optional `::' operator. */
16826 cp_parser_global_scope_opt (parser,
16827 /*current_scope_valid_p=*/false);
16828 /* Look for the nested-name-specifier. */
16829 nested_name_p
16830 = (cp_parser_nested_name_specifier_opt (parser,
16831 /*typename_keyword_p=*/false,
16832 /*check_dependency_p=*/false,
16833 /*type_p=*/false,
16834 /*is_declaration=*/false)
16835 != NULL_TREE);
16836 /* Outside of a class-specifier, there must be a
16837 nested-name-specifier. */
16838 if (!nested_name_p &&
16839 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16840 || friend_p))
16841 constructor_p = false;
16842 /* If we still think that this might be a constructor-declarator,
16843 look for a class-name. */
16844 if (constructor_p)
16846 /* If we have:
16848 template <typename T> struct S { S(); };
16849 template <typename T> S<T>::S ();
16851 we must recognize that the nested `S' names a class.
16852 Similarly, for:
16854 template <typename T> S<T>::S<T> ();
16856 we must recognize that the nested `S' names a template. */
16857 type_decl = cp_parser_class_name (parser,
16858 /*typename_keyword_p=*/false,
16859 /*template_keyword_p=*/false,
16860 none_type,
16861 /*check_dependency_p=*/false,
16862 /*class_head_p=*/false,
16863 /*is_declaration=*/false);
16864 /* If there was no class-name, then this is not a constructor. */
16865 constructor_p = !cp_parser_error_occurred (parser);
16868 /* If we're still considering a constructor, we have to see a `(',
16869 to begin the parameter-declaration-clause, followed by either a
16870 `)', an `...', or a decl-specifier. We need to check for a
16871 type-specifier to avoid being fooled into thinking that:
16873 S::S (f) (int);
16875 is a constructor. (It is actually a function named `f' that
16876 takes one parameter (of type `int') and returns a value of type
16877 `S::S'. */
16878 if (constructor_p
16879 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16881 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16882 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16883 /* A parameter declaration begins with a decl-specifier,
16884 which is either the "attribute" keyword, a storage class
16885 specifier, or (usually) a type-specifier. */
16886 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16888 tree type;
16889 tree pushed_scope = NULL_TREE;
16890 unsigned saved_num_template_parameter_lists;
16892 /* Names appearing in the type-specifier should be looked up
16893 in the scope of the class. */
16894 if (current_class_type)
16895 type = NULL_TREE;
16896 else
16898 type = TREE_TYPE (type_decl);
16899 if (TREE_CODE (type) == TYPENAME_TYPE)
16901 type = resolve_typename_type (type,
16902 /*only_current_p=*/false);
16903 if (TREE_CODE (type) == TYPENAME_TYPE)
16905 cp_parser_abort_tentative_parse (parser);
16906 return false;
16909 pushed_scope = push_scope (type);
16912 /* Inside the constructor parameter list, surrounding
16913 template-parameter-lists do not apply. */
16914 saved_num_template_parameter_lists
16915 = parser->num_template_parameter_lists;
16916 parser->num_template_parameter_lists = 0;
16918 /* Look for the type-specifier. */
16919 cp_parser_type_specifier (parser,
16920 CP_PARSER_FLAGS_NONE,
16921 /*decl_specs=*/NULL,
16922 /*is_declarator=*/true,
16923 /*declares_class_or_enum=*/NULL,
16924 /*is_cv_qualifier=*/NULL);
16926 parser->num_template_parameter_lists
16927 = saved_num_template_parameter_lists;
16929 /* Leave the scope of the class. */
16930 if (pushed_scope)
16931 pop_scope (pushed_scope);
16933 constructor_p = !cp_parser_error_occurred (parser);
16936 else
16937 constructor_p = false;
16938 /* We did not really want to consume any tokens. */
16939 cp_parser_abort_tentative_parse (parser);
16941 return constructor_p;
16944 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16945 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16946 they must be performed once we are in the scope of the function.
16948 Returns the function defined. */
16950 static tree
16951 cp_parser_function_definition_from_specifiers_and_declarator
16952 (cp_parser* parser,
16953 cp_decl_specifier_seq *decl_specifiers,
16954 tree attributes,
16955 const cp_declarator *declarator)
16957 tree fn;
16958 bool success_p;
16960 /* Begin the function-definition. */
16961 success_p = start_function (decl_specifiers, declarator, attributes);
16963 /* The things we're about to see are not directly qualified by any
16964 template headers we've seen thus far. */
16965 reset_specialization ();
16967 /* If there were names looked up in the decl-specifier-seq that we
16968 did not check, check them now. We must wait until we are in the
16969 scope of the function to perform the checks, since the function
16970 might be a friend. */
16971 perform_deferred_access_checks ();
16973 if (!success_p)
16975 /* Skip the entire function. */
16976 cp_parser_skip_to_end_of_block_or_statement (parser);
16977 fn = error_mark_node;
16979 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16981 /* Seen already, skip it. An error message has already been output. */
16982 cp_parser_skip_to_end_of_block_or_statement (parser);
16983 fn = current_function_decl;
16984 current_function_decl = NULL_TREE;
16985 /* If this is a function from a class, pop the nested class. */
16986 if (current_class_name)
16987 pop_nested_class ();
16989 else
16990 fn = cp_parser_function_definition_after_declarator (parser,
16991 /*inline_p=*/false);
16993 return fn;
16996 /* Parse the part of a function-definition that follows the
16997 declarator. INLINE_P is TRUE iff this function is an inline
16998 function defined with a class-specifier.
17000 Returns the function defined. */
17002 static tree
17003 cp_parser_function_definition_after_declarator (cp_parser* parser,
17004 bool inline_p)
17006 tree fn;
17007 bool ctor_initializer_p = false;
17008 bool saved_in_unbraced_linkage_specification_p;
17009 bool saved_in_function_body;
17010 unsigned saved_num_template_parameter_lists;
17012 saved_in_function_body = parser->in_function_body;
17013 parser->in_function_body = true;
17014 /* If the next token is `return', then the code may be trying to
17015 make use of the "named return value" extension that G++ used to
17016 support. */
17017 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17019 /* Consume the `return' keyword. */
17020 cp_lexer_consume_token (parser->lexer);
17021 /* Look for the identifier that indicates what value is to be
17022 returned. */
17023 cp_parser_identifier (parser);
17024 /* Issue an error message. */
17025 error ("named return values are no longer supported");
17026 /* Skip tokens until we reach the start of the function body. */
17027 while (true)
17029 cp_token *token = cp_lexer_peek_token (parser->lexer);
17030 if (token->type == CPP_OPEN_BRACE
17031 || token->type == CPP_EOF
17032 || token->type == CPP_PRAGMA_EOL)
17033 break;
17034 cp_lexer_consume_token (parser->lexer);
17037 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17038 anything declared inside `f'. */
17039 saved_in_unbraced_linkage_specification_p
17040 = parser->in_unbraced_linkage_specification_p;
17041 parser->in_unbraced_linkage_specification_p = false;
17042 /* Inside the function, surrounding template-parameter-lists do not
17043 apply. */
17044 saved_num_template_parameter_lists
17045 = parser->num_template_parameter_lists;
17046 parser->num_template_parameter_lists = 0;
17047 /* If the next token is `try', then we are looking at a
17048 function-try-block. */
17049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17050 ctor_initializer_p = cp_parser_function_try_block (parser);
17051 /* A function-try-block includes the function-body, so we only do
17052 this next part if we're not processing a function-try-block. */
17053 else
17054 ctor_initializer_p
17055 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17057 /* Finish the function. */
17058 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17059 (inline_p ? 2 : 0));
17060 /* Generate code for it, if necessary. */
17061 expand_or_defer_fn (fn);
17062 /* Restore the saved values. */
17063 parser->in_unbraced_linkage_specification_p
17064 = saved_in_unbraced_linkage_specification_p;
17065 parser->num_template_parameter_lists
17066 = saved_num_template_parameter_lists;
17067 parser->in_function_body = saved_in_function_body;
17069 return fn;
17072 /* Parse a template-declaration, assuming that the `export' (and
17073 `extern') keywords, if present, has already been scanned. MEMBER_P
17074 is as for cp_parser_template_declaration. */
17076 static void
17077 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17079 tree decl = NULL_TREE;
17080 VEC (deferred_access_check,gc) *checks;
17081 tree parameter_list;
17082 bool friend_p = false;
17083 bool need_lang_pop;
17085 /* Look for the `template' keyword. */
17086 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
17087 return;
17089 /* And the `<'. */
17090 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
17091 return;
17092 if (at_class_scope_p () && current_function_decl)
17094 /* 14.5.2.2 [temp.mem]
17096 A local class shall not have member templates. */
17097 error ("invalid declaration of member template in local class");
17098 cp_parser_skip_to_end_of_block_or_statement (parser);
17099 return;
17101 /* [temp]
17103 A template ... shall not have C linkage. */
17104 if (current_lang_name == lang_name_c)
17106 error ("template with C linkage");
17107 /* Give it C++ linkage to avoid confusing other parts of the
17108 front end. */
17109 push_lang_context (lang_name_cplusplus);
17110 need_lang_pop = true;
17112 else
17113 need_lang_pop = false;
17115 /* We cannot perform access checks on the template parameter
17116 declarations until we know what is being declared, just as we
17117 cannot check the decl-specifier list. */
17118 push_deferring_access_checks (dk_deferred);
17120 /* If the next token is `>', then we have an invalid
17121 specialization. Rather than complain about an invalid template
17122 parameter, issue an error message here. */
17123 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17125 cp_parser_error (parser, "invalid explicit specialization");
17126 begin_specialization ();
17127 parameter_list = NULL_TREE;
17129 else
17130 /* Parse the template parameters. */
17131 parameter_list = cp_parser_template_parameter_list (parser);
17133 /* Get the deferred access checks from the parameter list. These
17134 will be checked once we know what is being declared, as for a
17135 member template the checks must be performed in the scope of the
17136 class containing the member. */
17137 checks = get_deferred_access_checks ();
17139 /* Look for the `>'. */
17140 cp_parser_skip_to_end_of_template_parameter_list (parser);
17141 /* We just processed one more parameter list. */
17142 ++parser->num_template_parameter_lists;
17143 /* If the next token is `template', there are more template
17144 parameters. */
17145 if (cp_lexer_next_token_is_keyword (parser->lexer,
17146 RID_TEMPLATE))
17147 cp_parser_template_declaration_after_export (parser, member_p);
17148 else
17150 /* There are no access checks when parsing a template, as we do not
17151 know if a specialization will be a friend. */
17152 push_deferring_access_checks (dk_no_check);
17153 decl = cp_parser_single_declaration (parser,
17154 checks,
17155 member_p,
17156 /*explicit_specialization_p=*/false,
17157 &friend_p);
17158 pop_deferring_access_checks ();
17160 /* If this is a member template declaration, let the front
17161 end know. */
17162 if (member_p && !friend_p && decl)
17164 if (TREE_CODE (decl) == TYPE_DECL)
17165 cp_parser_check_access_in_redeclaration (decl);
17167 decl = finish_member_template_decl (decl);
17169 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17170 make_friend_class (current_class_type, TREE_TYPE (decl),
17171 /*complain=*/true);
17173 /* We are done with the current parameter list. */
17174 --parser->num_template_parameter_lists;
17176 pop_deferring_access_checks ();
17178 /* Finish up. */
17179 finish_template_decl (parameter_list);
17181 /* Register member declarations. */
17182 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17183 finish_member_declaration (decl);
17184 /* For the erroneous case of a template with C linkage, we pushed an
17185 implicit C++ linkage scope; exit that scope now. */
17186 if (need_lang_pop)
17187 pop_lang_context ();
17188 /* If DECL is a function template, we must return to parse it later.
17189 (Even though there is no definition, there might be default
17190 arguments that need handling.) */
17191 if (member_p && decl
17192 && (TREE_CODE (decl) == FUNCTION_DECL
17193 || DECL_FUNCTION_TEMPLATE_P (decl)))
17194 TREE_VALUE (parser->unparsed_functions_queues)
17195 = tree_cons (NULL_TREE, decl,
17196 TREE_VALUE (parser->unparsed_functions_queues));
17199 /* Perform the deferred access checks from a template-parameter-list.
17200 CHECKS is a TREE_LIST of access checks, as returned by
17201 get_deferred_access_checks. */
17203 static void
17204 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17206 ++processing_template_parmlist;
17207 perform_access_checks (checks);
17208 --processing_template_parmlist;
17211 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17212 `function-definition' sequence. MEMBER_P is true, this declaration
17213 appears in a class scope.
17215 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17216 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17218 static tree
17219 cp_parser_single_declaration (cp_parser* parser,
17220 VEC (deferred_access_check,gc)* checks,
17221 bool member_p,
17222 bool explicit_specialization_p,
17223 bool* friend_p)
17225 int declares_class_or_enum;
17226 tree decl = NULL_TREE;
17227 cp_decl_specifier_seq decl_specifiers;
17228 bool function_definition_p = false;
17230 /* This function is only used when processing a template
17231 declaration. */
17232 gcc_assert (innermost_scope_kind () == sk_template_parms
17233 || innermost_scope_kind () == sk_template_spec);
17235 /* Defer access checks until we know what is being declared. */
17236 push_deferring_access_checks (dk_deferred);
17238 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17239 alternative. */
17240 cp_parser_decl_specifier_seq (parser,
17241 CP_PARSER_FLAGS_OPTIONAL,
17242 &decl_specifiers,
17243 &declares_class_or_enum);
17244 if (friend_p)
17245 *friend_p = cp_parser_friend_p (&decl_specifiers);
17247 /* There are no template typedefs. */
17248 if (decl_specifiers.specs[(int) ds_typedef])
17250 error ("template declaration of %qs", "typedef");
17251 decl = error_mark_node;
17254 /* Gather up the access checks that occurred the
17255 decl-specifier-seq. */
17256 stop_deferring_access_checks ();
17258 /* Check for the declaration of a template class. */
17259 if (declares_class_or_enum)
17261 if (cp_parser_declares_only_class_p (parser))
17263 decl = shadow_tag (&decl_specifiers);
17265 /* In this case:
17267 struct C {
17268 friend template <typename T> struct A<T>::B;
17271 A<T>::B will be represented by a TYPENAME_TYPE, and
17272 therefore not recognized by shadow_tag. */
17273 if (friend_p && *friend_p
17274 && !decl
17275 && decl_specifiers.type
17276 && TYPE_P (decl_specifiers.type))
17277 decl = decl_specifiers.type;
17279 if (decl && decl != error_mark_node)
17280 decl = TYPE_NAME (decl);
17281 else
17282 decl = error_mark_node;
17284 /* Perform access checks for template parameters. */
17285 cp_parser_perform_template_parameter_access_checks (checks);
17288 /* If it's not a template class, try for a template function. If
17289 the next token is a `;', then this declaration does not declare
17290 anything. But, if there were errors in the decl-specifiers, then
17291 the error might well have come from an attempted class-specifier.
17292 In that case, there's no need to warn about a missing declarator. */
17293 if (!decl
17294 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17295 || decl_specifiers.type != error_mark_node))
17297 decl = cp_parser_init_declarator (parser,
17298 &decl_specifiers,
17299 checks,
17300 /*function_definition_allowed_p=*/true,
17301 member_p,
17302 declares_class_or_enum,
17303 &function_definition_p);
17305 /* 7.1.1-1 [dcl.stc]
17307 A storage-class-specifier shall not be specified in an explicit
17308 specialization... */
17309 if (decl
17310 && explicit_specialization_p
17311 && decl_specifiers.storage_class != sc_none)
17313 error ("explicit template specialization cannot have a storage class");
17314 decl = error_mark_node;
17318 pop_deferring_access_checks ();
17320 /* Clear any current qualification; whatever comes next is the start
17321 of something new. */
17322 parser->scope = NULL_TREE;
17323 parser->qualifying_scope = NULL_TREE;
17324 parser->object_scope = NULL_TREE;
17325 /* Look for a trailing `;' after the declaration. */
17326 if (!function_definition_p
17327 && (decl == error_mark_node
17328 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
17329 cp_parser_skip_to_end_of_block_or_statement (parser);
17331 return decl;
17334 /* Parse a cast-expression that is not the operand of a unary "&". */
17336 static tree
17337 cp_parser_simple_cast_expression (cp_parser *parser)
17339 return cp_parser_cast_expression (parser, /*address_p=*/false,
17340 /*cast_p=*/false);
17343 /* Parse a functional cast to TYPE. Returns an expression
17344 representing the cast. */
17346 static tree
17347 cp_parser_functional_cast (cp_parser* parser, tree type)
17349 tree expression_list;
17350 tree cast;
17352 expression_list
17353 = cp_parser_parenthesized_expression_list (parser, false,
17354 /*cast_p=*/true,
17355 /*allow_expansion_p=*/true,
17356 /*non_constant_p=*/NULL);
17358 cast = build_functional_cast (type, expression_list);
17359 /* [expr.const]/1: In an integral constant expression "only type
17360 conversions to integral or enumeration type can be used". */
17361 if (TREE_CODE (type) == TYPE_DECL)
17362 type = TREE_TYPE (type);
17363 if (cast != error_mark_node
17364 && !cast_valid_in_integral_constant_expression_p (type)
17365 && (cp_parser_non_integral_constant_expression
17366 (parser, "a call to a constructor")))
17367 return error_mark_node;
17368 return cast;
17371 /* Save the tokens that make up the body of a member function defined
17372 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17373 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17374 specifiers applied to the declaration. Returns the FUNCTION_DECL
17375 for the member function. */
17377 static tree
17378 cp_parser_save_member_function_body (cp_parser* parser,
17379 cp_decl_specifier_seq *decl_specifiers,
17380 cp_declarator *declarator,
17381 tree attributes)
17383 cp_token *first;
17384 cp_token *last;
17385 tree fn;
17387 /* Create the function-declaration. */
17388 fn = start_method (decl_specifiers, declarator, attributes);
17389 /* If something went badly wrong, bail out now. */
17390 if (fn == error_mark_node)
17392 /* If there's a function-body, skip it. */
17393 if (cp_parser_token_starts_function_definition_p
17394 (cp_lexer_peek_token (parser->lexer)))
17395 cp_parser_skip_to_end_of_block_or_statement (parser);
17396 return error_mark_node;
17399 /* Remember it, if there default args to post process. */
17400 cp_parser_save_default_args (parser, fn);
17402 /* Save away the tokens that make up the body of the
17403 function. */
17404 first = parser->lexer->next_token;
17405 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17406 /* Handle function try blocks. */
17407 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17408 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17409 last = parser->lexer->next_token;
17411 /* Save away the inline definition; we will process it when the
17412 class is complete. */
17413 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17414 DECL_PENDING_INLINE_P (fn) = 1;
17416 /* We need to know that this was defined in the class, so that
17417 friend templates are handled correctly. */
17418 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17420 /* We're done with the inline definition. */
17421 finish_method (fn);
17423 /* Add FN to the queue of functions to be parsed later. */
17424 TREE_VALUE (parser->unparsed_functions_queues)
17425 = tree_cons (NULL_TREE, fn,
17426 TREE_VALUE (parser->unparsed_functions_queues));
17428 return fn;
17431 /* Parse a template-argument-list, as well as the trailing ">" (but
17432 not the opening ">"). See cp_parser_template_argument_list for the
17433 return value. */
17435 static tree
17436 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17438 tree arguments;
17439 tree saved_scope;
17440 tree saved_qualifying_scope;
17441 tree saved_object_scope;
17442 bool saved_greater_than_is_operator_p;
17443 bool saved_skip_evaluation;
17445 /* [temp.names]
17447 When parsing a template-id, the first non-nested `>' is taken as
17448 the end of the template-argument-list rather than a greater-than
17449 operator. */
17450 saved_greater_than_is_operator_p
17451 = parser->greater_than_is_operator_p;
17452 parser->greater_than_is_operator_p = false;
17453 /* Parsing the argument list may modify SCOPE, so we save it
17454 here. */
17455 saved_scope = parser->scope;
17456 saved_qualifying_scope = parser->qualifying_scope;
17457 saved_object_scope = parser->object_scope;
17458 /* We need to evaluate the template arguments, even though this
17459 template-id may be nested within a "sizeof". */
17460 saved_skip_evaluation = skip_evaluation;
17461 skip_evaluation = false;
17462 /* Parse the template-argument-list itself. */
17463 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17464 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17465 arguments = NULL_TREE;
17466 else
17467 arguments = cp_parser_template_argument_list (parser);
17468 /* Look for the `>' that ends the template-argument-list. If we find
17469 a '>>' instead, it's probably just a typo. */
17470 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17472 if (cxx_dialect != cxx98)
17474 /* In C++0x, a `>>' in a template argument list or cast
17475 expression is considered to be two separate `>'
17476 tokens. So, change the current token to a `>', but don't
17477 consume it: it will be consumed later when the outer
17478 template argument list (or cast expression) is parsed.
17479 Note that this replacement of `>' for `>>' is necessary
17480 even if we are parsing tentatively: in the tentative
17481 case, after calling
17482 cp_parser_enclosed_template_argument_list we will always
17483 throw away all of the template arguments and the first
17484 closing `>', either because the template argument list
17485 was erroneous or because we are replacing those tokens
17486 with a CPP_TEMPLATE_ID token. The second `>' (which will
17487 not have been thrown away) is needed either to close an
17488 outer template argument list or to complete a new-style
17489 cast. */
17490 cp_token *token = cp_lexer_peek_token (parser->lexer);
17491 token->type = CPP_GREATER;
17493 else if (!saved_greater_than_is_operator_p)
17495 /* If we're in a nested template argument list, the '>>' has
17496 to be a typo for '> >'. We emit the error message, but we
17497 continue parsing and we push a '>' as next token, so that
17498 the argument list will be parsed correctly. Note that the
17499 global source location is still on the token before the
17500 '>>', so we need to say explicitly where we want it. */
17501 cp_token *token = cp_lexer_peek_token (parser->lexer);
17502 error ("%H%<>>%> should be %<> >%> "
17503 "within a nested template argument list",
17504 &token->location);
17506 token->type = CPP_GREATER;
17508 else
17510 /* If this is not a nested template argument list, the '>>'
17511 is a typo for '>'. Emit an error message and continue.
17512 Same deal about the token location, but here we can get it
17513 right by consuming the '>>' before issuing the diagnostic. */
17514 cp_lexer_consume_token (parser->lexer);
17515 error ("spurious %<>>%>, use %<>%> to terminate "
17516 "a template argument list");
17519 else
17520 cp_parser_skip_to_end_of_template_parameter_list (parser);
17521 /* The `>' token might be a greater-than operator again now. */
17522 parser->greater_than_is_operator_p
17523 = saved_greater_than_is_operator_p;
17524 /* Restore the SAVED_SCOPE. */
17525 parser->scope = saved_scope;
17526 parser->qualifying_scope = saved_qualifying_scope;
17527 parser->object_scope = saved_object_scope;
17528 skip_evaluation = saved_skip_evaluation;
17530 return arguments;
17533 /* MEMBER_FUNCTION is a member function, or a friend. If default
17534 arguments, or the body of the function have not yet been parsed,
17535 parse them now. */
17537 static void
17538 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17540 /* If this member is a template, get the underlying
17541 FUNCTION_DECL. */
17542 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17543 member_function = DECL_TEMPLATE_RESULT (member_function);
17545 /* There should not be any class definitions in progress at this
17546 point; the bodies of members are only parsed outside of all class
17547 definitions. */
17548 gcc_assert (parser->num_classes_being_defined == 0);
17549 /* While we're parsing the member functions we might encounter more
17550 classes. We want to handle them right away, but we don't want
17551 them getting mixed up with functions that are currently in the
17552 queue. */
17553 parser->unparsed_functions_queues
17554 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17556 /* Make sure that any template parameters are in scope. */
17557 maybe_begin_member_template_processing (member_function);
17559 /* If the body of the function has not yet been parsed, parse it
17560 now. */
17561 if (DECL_PENDING_INLINE_P (member_function))
17563 tree function_scope;
17564 cp_token_cache *tokens;
17566 /* The function is no longer pending; we are processing it. */
17567 tokens = DECL_PENDING_INLINE_INFO (member_function);
17568 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17569 DECL_PENDING_INLINE_P (member_function) = 0;
17571 /* If this is a local class, enter the scope of the containing
17572 function. */
17573 function_scope = current_function_decl;
17574 if (function_scope)
17575 push_function_context_to (function_scope);
17578 /* Push the body of the function onto the lexer stack. */
17579 cp_parser_push_lexer_for_tokens (parser, tokens);
17581 /* Let the front end know that we going to be defining this
17582 function. */
17583 start_preparsed_function (member_function, NULL_TREE,
17584 SF_PRE_PARSED | SF_INCLASS_INLINE);
17586 /* Don't do access checking if it is a templated function. */
17587 if (processing_template_decl)
17588 push_deferring_access_checks (dk_no_check);
17590 /* Now, parse the body of the function. */
17591 cp_parser_function_definition_after_declarator (parser,
17592 /*inline_p=*/true);
17594 if (processing_template_decl)
17595 pop_deferring_access_checks ();
17597 /* Leave the scope of the containing function. */
17598 if (function_scope)
17599 pop_function_context_from (function_scope);
17600 cp_parser_pop_lexer (parser);
17603 /* Remove any template parameters from the symbol table. */
17604 maybe_end_member_template_processing ();
17606 /* Restore the queue. */
17607 parser->unparsed_functions_queues
17608 = TREE_CHAIN (parser->unparsed_functions_queues);
17611 /* If DECL contains any default args, remember it on the unparsed
17612 functions queue. */
17614 static void
17615 cp_parser_save_default_args (cp_parser* parser, tree decl)
17617 tree probe;
17619 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17620 probe;
17621 probe = TREE_CHAIN (probe))
17622 if (TREE_PURPOSE (probe))
17624 TREE_PURPOSE (parser->unparsed_functions_queues)
17625 = tree_cons (current_class_type, decl,
17626 TREE_PURPOSE (parser->unparsed_functions_queues));
17627 break;
17631 /* FN is a FUNCTION_DECL which may contains a parameter with an
17632 unparsed DEFAULT_ARG. Parse the default args now. This function
17633 assumes that the current scope is the scope in which the default
17634 argument should be processed. */
17636 static void
17637 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17639 bool saved_local_variables_forbidden_p;
17640 tree parm;
17642 /* While we're parsing the default args, we might (due to the
17643 statement expression extension) encounter more classes. We want
17644 to handle them right away, but we don't want them getting mixed
17645 up with default args that are currently in the queue. */
17646 parser->unparsed_functions_queues
17647 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17649 /* Local variable names (and the `this' keyword) may not appear
17650 in a default argument. */
17651 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17652 parser->local_variables_forbidden_p = true;
17654 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17655 parm;
17656 parm = TREE_CHAIN (parm))
17658 cp_token_cache *tokens;
17659 tree default_arg = TREE_PURPOSE (parm);
17660 tree parsed_arg;
17661 VEC(tree,gc) *insts;
17662 tree copy;
17663 unsigned ix;
17665 if (!default_arg)
17666 continue;
17668 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17669 /* This can happen for a friend declaration for a function
17670 already declared with default arguments. */
17671 continue;
17673 /* Push the saved tokens for the default argument onto the parser's
17674 lexer stack. */
17675 tokens = DEFARG_TOKENS (default_arg);
17676 cp_parser_push_lexer_for_tokens (parser, tokens);
17678 /* Parse the assignment-expression. */
17679 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17681 if (!processing_template_decl)
17682 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17684 TREE_PURPOSE (parm) = parsed_arg;
17686 /* Update any instantiations we've already created. */
17687 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17688 VEC_iterate (tree, insts, ix, copy); ix++)
17689 TREE_PURPOSE (copy) = parsed_arg;
17691 /* If the token stream has not been completely used up, then
17692 there was extra junk after the end of the default
17693 argument. */
17694 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17695 cp_parser_error (parser, "expected %<,%>");
17697 /* Revert to the main lexer. */
17698 cp_parser_pop_lexer (parser);
17701 /* Make sure no default arg is missing. */
17702 check_default_args (fn);
17704 /* Restore the state of local_variables_forbidden_p. */
17705 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17707 /* Restore the queue. */
17708 parser->unparsed_functions_queues
17709 = TREE_CHAIN (parser->unparsed_functions_queues);
17712 /* Parse the operand of `sizeof' (or a similar operator). Returns
17713 either a TYPE or an expression, depending on the form of the
17714 input. The KEYWORD indicates which kind of expression we have
17715 encountered. */
17717 static tree
17718 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17720 static const char *format;
17721 tree expr = NULL_TREE;
17722 const char *saved_message;
17723 char *tmp;
17724 bool saved_integral_constant_expression_p;
17725 bool saved_non_integral_constant_expression_p;
17726 bool pack_expansion_p = false;
17728 /* Initialize FORMAT the first time we get here. */
17729 if (!format)
17730 format = "types may not be defined in '%s' expressions";
17732 /* Types cannot be defined in a `sizeof' expression. Save away the
17733 old message. */
17734 saved_message = parser->type_definition_forbidden_message;
17735 /* And create the new one. */
17736 parser->type_definition_forbidden_message = tmp
17737 = XNEWVEC (char, strlen (format)
17738 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17739 + 1 /* `\0' */);
17740 sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17742 /* The restrictions on constant-expressions do not apply inside
17743 sizeof expressions. */
17744 saved_integral_constant_expression_p
17745 = parser->integral_constant_expression_p;
17746 saved_non_integral_constant_expression_p
17747 = parser->non_integral_constant_expression_p;
17748 parser->integral_constant_expression_p = false;
17750 /* If it's a `...', then we are computing the length of a parameter
17751 pack. */
17752 if (keyword == RID_SIZEOF
17753 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17755 /* Consume the `...'. */
17756 cp_lexer_consume_token (parser->lexer);
17757 maybe_warn_variadic_templates ();
17759 /* Note that this is an expansion. */
17760 pack_expansion_p = true;
17763 /* Do not actually evaluate the expression. */
17764 ++skip_evaluation;
17765 /* If it's a `(', then we might be looking at the type-id
17766 construction. */
17767 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17769 tree type;
17770 bool saved_in_type_id_in_expr_p;
17772 /* We can't be sure yet whether we're looking at a type-id or an
17773 expression. */
17774 cp_parser_parse_tentatively (parser);
17775 /* Consume the `('. */
17776 cp_lexer_consume_token (parser->lexer);
17777 /* Parse the type-id. */
17778 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17779 parser->in_type_id_in_expr_p = true;
17780 type = cp_parser_type_id (parser);
17781 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17782 /* Now, look for the trailing `)'. */
17783 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17784 /* If all went well, then we're done. */
17785 if (cp_parser_parse_definitely (parser))
17787 cp_decl_specifier_seq decl_specs;
17789 /* Build a trivial decl-specifier-seq. */
17790 clear_decl_specs (&decl_specs);
17791 decl_specs.type = type;
17793 /* Call grokdeclarator to figure out what type this is. */
17794 expr = grokdeclarator (NULL,
17795 &decl_specs,
17796 TYPENAME,
17797 /*initialized=*/0,
17798 /*attrlist=*/NULL);
17802 /* If the type-id production did not work out, then we must be
17803 looking at the unary-expression production. */
17804 if (!expr)
17805 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17806 /*cast_p=*/false);
17808 if (pack_expansion_p)
17809 /* Build a pack expansion. */
17810 expr = make_pack_expansion (expr);
17812 /* Go back to evaluating expressions. */
17813 --skip_evaluation;
17815 /* Free the message we created. */
17816 free (tmp);
17817 /* And restore the old one. */
17818 parser->type_definition_forbidden_message = saved_message;
17819 parser->integral_constant_expression_p
17820 = saved_integral_constant_expression_p;
17821 parser->non_integral_constant_expression_p
17822 = saved_non_integral_constant_expression_p;
17824 return expr;
17827 /* If the current declaration has no declarator, return true. */
17829 static bool
17830 cp_parser_declares_only_class_p (cp_parser *parser)
17832 /* If the next token is a `;' or a `,' then there is no
17833 declarator. */
17834 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17835 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17838 /* Update the DECL_SPECS to reflect the storage class indicated by
17839 KEYWORD. */
17841 static void
17842 cp_parser_set_storage_class (cp_parser *parser,
17843 cp_decl_specifier_seq *decl_specs,
17844 enum rid keyword)
17846 cp_storage_class storage_class;
17848 if (parser->in_unbraced_linkage_specification_p)
17850 error ("invalid use of %qD in linkage specification",
17851 ridpointers[keyword]);
17852 return;
17854 else if (decl_specs->storage_class != sc_none)
17856 decl_specs->conflicting_specifiers_p = true;
17857 return;
17860 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17861 && decl_specs->specs[(int) ds_thread])
17863 error ("%<__thread%> before %qD", ridpointers[keyword]);
17864 decl_specs->specs[(int) ds_thread] = 0;
17867 switch (keyword)
17869 case RID_AUTO:
17870 storage_class = sc_auto;
17871 break;
17872 case RID_REGISTER:
17873 storage_class = sc_register;
17874 break;
17875 case RID_STATIC:
17876 storage_class = sc_static;
17877 break;
17878 case RID_EXTERN:
17879 storage_class = sc_extern;
17880 break;
17881 case RID_MUTABLE:
17882 storage_class = sc_mutable;
17883 break;
17884 default:
17885 gcc_unreachable ();
17887 decl_specs->storage_class = storage_class;
17889 /* A storage class specifier cannot be applied alongside a typedef
17890 specifier. If there is a typedef specifier present then set
17891 conflicting_specifiers_p which will trigger an error later
17892 on in grokdeclarator. */
17893 if (decl_specs->specs[(int)ds_typedef])
17894 decl_specs->conflicting_specifiers_p = true;
17897 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17898 is true, the type is a user-defined type; otherwise it is a
17899 built-in type specified by a keyword. */
17901 static void
17902 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17903 tree type_spec,
17904 bool user_defined_p)
17906 decl_specs->any_specifiers_p = true;
17908 /* If the user tries to redeclare bool or wchar_t (with, for
17909 example, in "typedef int wchar_t;") we remember that this is what
17910 happened. In system headers, we ignore these declarations so
17911 that G++ can work with system headers that are not C++-safe. */
17912 if (decl_specs->specs[(int) ds_typedef]
17913 && !user_defined_p
17914 && (type_spec == boolean_type_node
17915 || type_spec == wchar_type_node)
17916 && (decl_specs->type
17917 || decl_specs->specs[(int) ds_long]
17918 || decl_specs->specs[(int) ds_short]
17919 || decl_specs->specs[(int) ds_unsigned]
17920 || decl_specs->specs[(int) ds_signed]))
17922 decl_specs->redefined_builtin_type = type_spec;
17923 if (!decl_specs->type)
17925 decl_specs->type = type_spec;
17926 decl_specs->user_defined_type_p = false;
17929 else if (decl_specs->type)
17930 decl_specs->multiple_types_p = true;
17931 else
17933 decl_specs->type = type_spec;
17934 decl_specs->user_defined_type_p = user_defined_p;
17935 decl_specs->redefined_builtin_type = NULL_TREE;
17939 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17940 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17942 static bool
17943 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17945 return decl_specifiers->specs[(int) ds_friend] != 0;
17948 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17949 issue an error message indicating that TOKEN_DESC was expected.
17951 Returns the token consumed, if the token had the appropriate type.
17952 Otherwise, returns NULL. */
17954 static cp_token *
17955 cp_parser_require (cp_parser* parser,
17956 enum cpp_ttype type,
17957 const char* token_desc)
17959 if (cp_lexer_next_token_is (parser->lexer, type))
17960 return cp_lexer_consume_token (parser->lexer);
17961 else
17963 /* Output the MESSAGE -- unless we're parsing tentatively. */
17964 if (!cp_parser_simulate_error (parser))
17966 char *message = concat ("expected ", token_desc, NULL);
17967 cp_parser_error (parser, message);
17968 free (message);
17970 return NULL;
17974 /* An error message is produced if the next token is not '>'.
17975 All further tokens are skipped until the desired token is
17976 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17978 static void
17979 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17981 /* Current level of '< ... >'. */
17982 unsigned level = 0;
17983 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17984 unsigned nesting_depth = 0;
17986 /* Are we ready, yet? If not, issue error message. */
17987 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17988 return;
17990 /* Skip tokens until the desired token is found. */
17991 while (true)
17993 /* Peek at the next token. */
17994 switch (cp_lexer_peek_token (parser->lexer)->type)
17996 case CPP_LESS:
17997 if (!nesting_depth)
17998 ++level;
17999 break;
18001 case CPP_RSHIFT:
18002 if (cxx_dialect == cxx98)
18003 /* C++0x views the `>>' operator as two `>' tokens, but
18004 C++98 does not. */
18005 break;
18006 else if (!nesting_depth && level-- == 0)
18008 /* We've hit a `>>' where the first `>' closes the
18009 template argument list, and the second `>' is
18010 spurious. Just consume the `>>' and stop; we've
18011 already produced at least one error. */
18012 cp_lexer_consume_token (parser->lexer);
18013 return;
18015 /* Fall through for C++0x, so we handle the second `>' in
18016 the `>>'. */
18018 case CPP_GREATER:
18019 if (!nesting_depth && level-- == 0)
18021 /* We've reached the token we want, consume it and stop. */
18022 cp_lexer_consume_token (parser->lexer);
18023 return;
18025 break;
18027 case CPP_OPEN_PAREN:
18028 case CPP_OPEN_SQUARE:
18029 ++nesting_depth;
18030 break;
18032 case CPP_CLOSE_PAREN:
18033 case CPP_CLOSE_SQUARE:
18034 if (nesting_depth-- == 0)
18035 return;
18036 break;
18038 case CPP_EOF:
18039 case CPP_PRAGMA_EOL:
18040 case CPP_SEMICOLON:
18041 case CPP_OPEN_BRACE:
18042 case CPP_CLOSE_BRACE:
18043 /* The '>' was probably forgotten, don't look further. */
18044 return;
18046 default:
18047 break;
18050 /* Consume this token. */
18051 cp_lexer_consume_token (parser->lexer);
18055 /* If the next token is the indicated keyword, consume it. Otherwise,
18056 issue an error message indicating that TOKEN_DESC was expected.
18058 Returns the token consumed, if the token had the appropriate type.
18059 Otherwise, returns NULL. */
18061 static cp_token *
18062 cp_parser_require_keyword (cp_parser* parser,
18063 enum rid keyword,
18064 const char* token_desc)
18066 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18068 if (token && token->keyword != keyword)
18070 dyn_string_t error_msg;
18072 /* Format the error message. */
18073 error_msg = dyn_string_new (0);
18074 dyn_string_append_cstr (error_msg, "expected ");
18075 dyn_string_append_cstr (error_msg, token_desc);
18076 cp_parser_error (parser, error_msg->s);
18077 dyn_string_delete (error_msg);
18078 return NULL;
18081 return token;
18084 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18085 function-definition. */
18087 static bool
18088 cp_parser_token_starts_function_definition_p (cp_token* token)
18090 return (/* An ordinary function-body begins with an `{'. */
18091 token->type == CPP_OPEN_BRACE
18092 /* A ctor-initializer begins with a `:'. */
18093 || token->type == CPP_COLON
18094 /* A function-try-block begins with `try'. */
18095 || token->keyword == RID_TRY
18096 /* The named return value extension begins with `return'. */
18097 || token->keyword == RID_RETURN);
18100 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18101 definition. */
18103 static bool
18104 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18106 cp_token *token;
18108 token = cp_lexer_peek_token (parser->lexer);
18109 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18112 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18113 C++0x) ending a template-argument. */
18115 static bool
18116 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18118 cp_token *token;
18120 token = cp_lexer_peek_token (parser->lexer);
18121 return (token->type == CPP_COMMA
18122 || token->type == CPP_GREATER
18123 || token->type == CPP_ELLIPSIS
18124 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18127 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18128 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18130 static bool
18131 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18132 size_t n)
18134 cp_token *token;
18136 token = cp_lexer_peek_nth_token (parser->lexer, n);
18137 if (token->type == CPP_LESS)
18138 return true;
18139 /* Check for the sequence `<::' in the original code. It would be lexed as
18140 `[:', where `[' is a digraph, and there is no whitespace before
18141 `:'. */
18142 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18144 cp_token *token2;
18145 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18146 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18147 return true;
18149 return false;
18152 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18153 or none_type otherwise. */
18155 static enum tag_types
18156 cp_parser_token_is_class_key (cp_token* token)
18158 switch (token->keyword)
18160 case RID_CLASS:
18161 return class_type;
18162 case RID_STRUCT:
18163 return record_type;
18164 case RID_UNION:
18165 return union_type;
18167 default:
18168 return none_type;
18172 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18174 static void
18175 cp_parser_check_class_key (enum tag_types class_key, tree type)
18177 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18178 pedwarn ("%qs tag used in naming %q#T",
18179 class_key == union_type ? "union"
18180 : class_key == record_type ? "struct" : "class",
18181 type);
18184 /* Issue an error message if DECL is redeclared with different
18185 access than its original declaration [class.access.spec/3].
18186 This applies to nested classes and nested class templates.
18187 [class.mem/1]. */
18189 static void
18190 cp_parser_check_access_in_redeclaration (tree decl)
18192 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18193 return;
18195 if ((TREE_PRIVATE (decl)
18196 != (current_access_specifier == access_private_node))
18197 || (TREE_PROTECTED (decl)
18198 != (current_access_specifier == access_protected_node)))
18199 error ("%qD redeclared with different access", decl);
18202 /* Look for the `template' keyword, as a syntactic disambiguator.
18203 Return TRUE iff it is present, in which case it will be
18204 consumed. */
18206 static bool
18207 cp_parser_optional_template_keyword (cp_parser *parser)
18209 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18211 /* The `template' keyword can only be used within templates;
18212 outside templates the parser can always figure out what is a
18213 template and what is not. */
18214 if (!processing_template_decl)
18216 error ("%<template%> (as a disambiguator) is only allowed "
18217 "within templates");
18218 /* If this part of the token stream is rescanned, the same
18219 error message would be generated. So, we purge the token
18220 from the stream. */
18221 cp_lexer_purge_token (parser->lexer);
18222 return false;
18224 else
18226 /* Consume the `template' keyword. */
18227 cp_lexer_consume_token (parser->lexer);
18228 return true;
18232 return false;
18235 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18236 set PARSER->SCOPE, and perform other related actions. */
18238 static void
18239 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18241 int i;
18242 struct tree_check *check_value;
18243 deferred_access_check *chk;
18244 VEC (deferred_access_check,gc) *checks;
18246 /* Get the stored value. */
18247 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18248 /* Perform any access checks that were deferred. */
18249 checks = check_value->checks;
18250 if (checks)
18252 for (i = 0 ;
18253 VEC_iterate (deferred_access_check, checks, i, chk) ;
18254 ++i)
18256 perform_or_defer_access_check (chk->binfo,
18257 chk->decl,
18258 chk->diag_decl);
18261 /* Set the scope from the stored value. */
18262 parser->scope = check_value->value;
18263 parser->qualifying_scope = check_value->qualifying_scope;
18264 parser->object_scope = NULL_TREE;
18267 /* Consume tokens up through a non-nested END token. */
18269 static void
18270 cp_parser_cache_group (cp_parser *parser,
18271 enum cpp_ttype end,
18272 unsigned depth)
18274 while (true)
18276 cp_token *token;
18278 /* Abort a parenthesized expression if we encounter a brace. */
18279 if ((end == CPP_CLOSE_PAREN || depth == 0)
18280 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18281 return;
18282 /* If we've reached the end of the file, stop. */
18283 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
18284 || (end != CPP_PRAGMA_EOL
18285 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
18286 return;
18287 /* Consume the next token. */
18288 token = cp_lexer_consume_token (parser->lexer);
18289 /* See if it starts a new group. */
18290 if (token->type == CPP_OPEN_BRACE)
18292 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18293 if (depth == 0)
18294 return;
18296 else if (token->type == CPP_OPEN_PAREN)
18297 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18298 else if (token->type == CPP_PRAGMA)
18299 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18300 else if (token->type == end)
18301 return;
18305 /* Begin parsing tentatively. We always save tokens while parsing
18306 tentatively so that if the tentative parsing fails we can restore the
18307 tokens. */
18309 static void
18310 cp_parser_parse_tentatively (cp_parser* parser)
18312 /* Enter a new parsing context. */
18313 parser->context = cp_parser_context_new (parser->context);
18314 /* Begin saving tokens. */
18315 cp_lexer_save_tokens (parser->lexer);
18316 /* In order to avoid repetitive access control error messages,
18317 access checks are queued up until we are no longer parsing
18318 tentatively. */
18319 push_deferring_access_checks (dk_deferred);
18322 /* Commit to the currently active tentative parse. */
18324 static void
18325 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18327 cp_parser_context *context;
18328 cp_lexer *lexer;
18330 /* Mark all of the levels as committed. */
18331 lexer = parser->lexer;
18332 for (context = parser->context; context->next; context = context->next)
18334 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18335 break;
18336 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18337 while (!cp_lexer_saving_tokens (lexer))
18338 lexer = lexer->next;
18339 cp_lexer_commit_tokens (lexer);
18343 /* Abort the currently active tentative parse. All consumed tokens
18344 will be rolled back, and no diagnostics will be issued. */
18346 static void
18347 cp_parser_abort_tentative_parse (cp_parser* parser)
18349 cp_parser_simulate_error (parser);
18350 /* Now, pretend that we want to see if the construct was
18351 successfully parsed. */
18352 cp_parser_parse_definitely (parser);
18355 /* Stop parsing tentatively. If a parse error has occurred, restore the
18356 token stream. Otherwise, commit to the tokens we have consumed.
18357 Returns true if no error occurred; false otherwise. */
18359 static bool
18360 cp_parser_parse_definitely (cp_parser* parser)
18362 bool error_occurred;
18363 cp_parser_context *context;
18365 /* Remember whether or not an error occurred, since we are about to
18366 destroy that information. */
18367 error_occurred = cp_parser_error_occurred (parser);
18368 /* Remove the topmost context from the stack. */
18369 context = parser->context;
18370 parser->context = context->next;
18371 /* If no parse errors occurred, commit to the tentative parse. */
18372 if (!error_occurred)
18374 /* Commit to the tokens read tentatively, unless that was
18375 already done. */
18376 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18377 cp_lexer_commit_tokens (parser->lexer);
18379 pop_to_parent_deferring_access_checks ();
18381 /* Otherwise, if errors occurred, roll back our state so that things
18382 are just as they were before we began the tentative parse. */
18383 else
18385 cp_lexer_rollback_tokens (parser->lexer);
18386 pop_deferring_access_checks ();
18388 /* Add the context to the front of the free list. */
18389 context->next = cp_parser_context_free_list;
18390 cp_parser_context_free_list = context;
18392 return !error_occurred;
18395 /* Returns true if we are parsing tentatively and are not committed to
18396 this tentative parse. */
18398 static bool
18399 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18401 return (cp_parser_parsing_tentatively (parser)
18402 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18405 /* Returns nonzero iff an error has occurred during the most recent
18406 tentative parse. */
18408 static bool
18409 cp_parser_error_occurred (cp_parser* parser)
18411 return (cp_parser_parsing_tentatively (parser)
18412 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18415 /* Returns nonzero if GNU extensions are allowed. */
18417 static bool
18418 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18420 return parser->allow_gnu_extensions_p;
18423 /* Objective-C++ Productions */
18426 /* Parse an Objective-C expression, which feeds into a primary-expression
18427 above.
18429 objc-expression:
18430 objc-message-expression
18431 objc-string-literal
18432 objc-encode-expression
18433 objc-protocol-expression
18434 objc-selector-expression
18436 Returns a tree representation of the expression. */
18438 static tree
18439 cp_parser_objc_expression (cp_parser* parser)
18441 /* Try to figure out what kind of declaration is present. */
18442 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18444 switch (kwd->type)
18446 case CPP_OPEN_SQUARE:
18447 return cp_parser_objc_message_expression (parser);
18449 case CPP_OBJC_STRING:
18450 kwd = cp_lexer_consume_token (parser->lexer);
18451 return objc_build_string_object (kwd->u.value);
18453 case CPP_KEYWORD:
18454 switch (kwd->keyword)
18456 case RID_AT_ENCODE:
18457 return cp_parser_objc_encode_expression (parser);
18459 case RID_AT_PROTOCOL:
18460 return cp_parser_objc_protocol_expression (parser);
18462 case RID_AT_SELECTOR:
18463 return cp_parser_objc_selector_expression (parser);
18465 default:
18466 break;
18468 default:
18469 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18470 cp_parser_skip_to_end_of_block_or_statement (parser);
18473 return error_mark_node;
18476 /* Parse an Objective-C message expression.
18478 objc-message-expression:
18479 [ objc-message-receiver objc-message-args ]
18481 Returns a representation of an Objective-C message. */
18483 static tree
18484 cp_parser_objc_message_expression (cp_parser* parser)
18486 tree receiver, messageargs;
18488 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18489 receiver = cp_parser_objc_message_receiver (parser);
18490 messageargs = cp_parser_objc_message_args (parser);
18491 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18493 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18496 /* Parse an objc-message-receiver.
18498 objc-message-receiver:
18499 expression
18500 simple-type-specifier
18502 Returns a representation of the type or expression. */
18504 static tree
18505 cp_parser_objc_message_receiver (cp_parser* parser)
18507 tree rcv;
18509 /* An Objective-C message receiver may be either (1) a type
18510 or (2) an expression. */
18511 cp_parser_parse_tentatively (parser);
18512 rcv = cp_parser_expression (parser, false);
18514 if (cp_parser_parse_definitely (parser))
18515 return rcv;
18517 rcv = cp_parser_simple_type_specifier (parser,
18518 /*decl_specs=*/NULL,
18519 CP_PARSER_FLAGS_NONE);
18521 return objc_get_class_reference (rcv);
18524 /* Parse the arguments and selectors comprising an Objective-C message.
18526 objc-message-args:
18527 objc-selector
18528 objc-selector-args
18529 objc-selector-args , objc-comma-args
18531 objc-selector-args:
18532 objc-selector [opt] : assignment-expression
18533 objc-selector-args objc-selector [opt] : assignment-expression
18535 objc-comma-args:
18536 assignment-expression
18537 objc-comma-args , assignment-expression
18539 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18540 selector arguments and TREE_VALUE containing a list of comma
18541 arguments. */
18543 static tree
18544 cp_parser_objc_message_args (cp_parser* parser)
18546 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18547 bool maybe_unary_selector_p = true;
18548 cp_token *token = cp_lexer_peek_token (parser->lexer);
18550 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18552 tree selector = NULL_TREE, arg;
18554 if (token->type != CPP_COLON)
18555 selector = cp_parser_objc_selector (parser);
18557 /* Detect if we have a unary selector. */
18558 if (maybe_unary_selector_p
18559 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18560 return build_tree_list (selector, NULL_TREE);
18562 maybe_unary_selector_p = false;
18563 cp_parser_require (parser, CPP_COLON, "`:'");
18564 arg = cp_parser_assignment_expression (parser, false);
18566 sel_args
18567 = chainon (sel_args,
18568 build_tree_list (selector, arg));
18570 token = cp_lexer_peek_token (parser->lexer);
18573 /* Handle non-selector arguments, if any. */
18574 while (token->type == CPP_COMMA)
18576 tree arg;
18578 cp_lexer_consume_token (parser->lexer);
18579 arg = cp_parser_assignment_expression (parser, false);
18581 addl_args
18582 = chainon (addl_args,
18583 build_tree_list (NULL_TREE, arg));
18585 token = cp_lexer_peek_token (parser->lexer);
18588 return build_tree_list (sel_args, addl_args);
18591 /* Parse an Objective-C encode expression.
18593 objc-encode-expression:
18594 @encode objc-typename
18596 Returns an encoded representation of the type argument. */
18598 static tree
18599 cp_parser_objc_encode_expression (cp_parser* parser)
18601 tree type;
18603 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
18604 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18605 type = complete_type (cp_parser_type_id (parser));
18606 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18608 if (!type)
18610 error ("%<@encode%> must specify a type as an argument");
18611 return error_mark_node;
18614 return objc_build_encode_expr (type);
18617 /* Parse an Objective-C @defs expression. */
18619 static tree
18620 cp_parser_objc_defs_expression (cp_parser *parser)
18622 tree name;
18624 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18625 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18626 name = cp_parser_identifier (parser);
18627 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18629 return objc_get_class_ivars (name);
18632 /* Parse an Objective-C protocol expression.
18634 objc-protocol-expression:
18635 @protocol ( identifier )
18637 Returns a representation of the protocol expression. */
18639 static tree
18640 cp_parser_objc_protocol_expression (cp_parser* parser)
18642 tree proto;
18644 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18645 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18646 proto = cp_parser_identifier (parser);
18647 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18649 return objc_build_protocol_expr (proto);
18652 /* Parse an Objective-C selector expression.
18654 objc-selector-expression:
18655 @selector ( objc-method-signature )
18657 objc-method-signature:
18658 objc-selector
18659 objc-selector-seq
18661 objc-selector-seq:
18662 objc-selector :
18663 objc-selector-seq objc-selector :
18665 Returns a representation of the method selector. */
18667 static tree
18668 cp_parser_objc_selector_expression (cp_parser* parser)
18670 tree sel_seq = NULL_TREE;
18671 bool maybe_unary_selector_p = true;
18672 cp_token *token;
18674 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18675 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18676 token = cp_lexer_peek_token (parser->lexer);
18678 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18679 || token->type == CPP_SCOPE)
18681 tree selector = NULL_TREE;
18683 if (token->type != CPP_COLON
18684 || token->type == CPP_SCOPE)
18685 selector = cp_parser_objc_selector (parser);
18687 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18688 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18690 /* Detect if we have a unary selector. */
18691 if (maybe_unary_selector_p)
18693 sel_seq = selector;
18694 goto finish_selector;
18696 else
18698 cp_parser_error (parser, "expected %<:%>");
18701 maybe_unary_selector_p = false;
18702 token = cp_lexer_consume_token (parser->lexer);
18704 if (token->type == CPP_SCOPE)
18706 sel_seq
18707 = chainon (sel_seq,
18708 build_tree_list (selector, NULL_TREE));
18709 sel_seq
18710 = chainon (sel_seq,
18711 build_tree_list (NULL_TREE, NULL_TREE));
18713 else
18714 sel_seq
18715 = chainon (sel_seq,
18716 build_tree_list (selector, NULL_TREE));
18718 token = cp_lexer_peek_token (parser->lexer);
18721 finish_selector:
18722 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18724 return objc_build_selector_expr (sel_seq);
18727 /* Parse a list of identifiers.
18729 objc-identifier-list:
18730 identifier
18731 objc-identifier-list , identifier
18733 Returns a TREE_LIST of identifier nodes. */
18735 static tree
18736 cp_parser_objc_identifier_list (cp_parser* parser)
18738 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18739 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18741 while (sep->type == CPP_COMMA)
18743 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18744 list = chainon (list,
18745 build_tree_list (NULL_TREE,
18746 cp_parser_identifier (parser)));
18747 sep = cp_lexer_peek_token (parser->lexer);
18750 return list;
18753 /* Parse an Objective-C alias declaration.
18755 objc-alias-declaration:
18756 @compatibility_alias identifier identifier ;
18758 This function registers the alias mapping with the Objective-C front end.
18759 It returns nothing. */
18761 static void
18762 cp_parser_objc_alias_declaration (cp_parser* parser)
18764 tree alias, orig;
18766 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18767 alias = cp_parser_identifier (parser);
18768 orig = cp_parser_identifier (parser);
18769 objc_declare_alias (alias, orig);
18770 cp_parser_consume_semicolon_at_end_of_statement (parser);
18773 /* Parse an Objective-C class forward-declaration.
18775 objc-class-declaration:
18776 @class objc-identifier-list ;
18778 The function registers the forward declarations with the Objective-C
18779 front end. It returns nothing. */
18781 static void
18782 cp_parser_objc_class_declaration (cp_parser* parser)
18784 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18785 objc_declare_class (cp_parser_objc_identifier_list (parser));
18786 cp_parser_consume_semicolon_at_end_of_statement (parser);
18789 /* Parse a list of Objective-C protocol references.
18791 objc-protocol-refs-opt:
18792 objc-protocol-refs [opt]
18794 objc-protocol-refs:
18795 < objc-identifier-list >
18797 Returns a TREE_LIST of identifiers, if any. */
18799 static tree
18800 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18802 tree protorefs = NULL_TREE;
18804 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18806 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18807 protorefs = cp_parser_objc_identifier_list (parser);
18808 cp_parser_require (parser, CPP_GREATER, "`>'");
18811 return protorefs;
18814 /* Parse a Objective-C visibility specification. */
18816 static void
18817 cp_parser_objc_visibility_spec (cp_parser* parser)
18819 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18821 switch (vis->keyword)
18823 case RID_AT_PRIVATE:
18824 objc_set_visibility (2);
18825 break;
18826 case RID_AT_PROTECTED:
18827 objc_set_visibility (0);
18828 break;
18829 case RID_AT_PUBLIC:
18830 objc_set_visibility (1);
18831 break;
18832 default:
18833 return;
18836 /* Eat '@private'/'@protected'/'@public'. */
18837 cp_lexer_consume_token (parser->lexer);
18840 /* Parse an Objective-C method type. */
18842 static void
18843 cp_parser_objc_method_type (cp_parser* parser)
18845 objc_set_method_type
18846 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18847 ? PLUS_EXPR
18848 : MINUS_EXPR);
18851 /* Parse an Objective-C protocol qualifier. */
18853 static tree
18854 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18856 tree quals = NULL_TREE, node;
18857 cp_token *token = cp_lexer_peek_token (parser->lexer);
18859 node = token->u.value;
18861 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18862 && (node == ridpointers [(int) RID_IN]
18863 || node == ridpointers [(int) RID_OUT]
18864 || node == ridpointers [(int) RID_INOUT]
18865 || node == ridpointers [(int) RID_BYCOPY]
18866 || node == ridpointers [(int) RID_BYREF]
18867 || node == ridpointers [(int) RID_ONEWAY]))
18869 quals = tree_cons (NULL_TREE, node, quals);
18870 cp_lexer_consume_token (parser->lexer);
18871 token = cp_lexer_peek_token (parser->lexer);
18872 node = token->u.value;
18875 return quals;
18878 /* Parse an Objective-C typename. */
18880 static tree
18881 cp_parser_objc_typename (cp_parser* parser)
18883 tree typename = NULL_TREE;
18885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18887 tree proto_quals, cp_type = NULL_TREE;
18889 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18890 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18892 /* An ObjC type name may consist of just protocol qualifiers, in which
18893 case the type shall default to 'id'. */
18894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18895 cp_type = cp_parser_type_id (parser);
18897 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18898 typename = build_tree_list (proto_quals, cp_type);
18901 return typename;
18904 /* Check to see if TYPE refers to an Objective-C selector name. */
18906 static bool
18907 cp_parser_objc_selector_p (enum cpp_ttype type)
18909 return (type == CPP_NAME || type == CPP_KEYWORD
18910 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18911 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18912 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18913 || type == CPP_XOR || type == CPP_XOR_EQ);
18916 /* Parse an Objective-C selector. */
18918 static tree
18919 cp_parser_objc_selector (cp_parser* parser)
18921 cp_token *token = cp_lexer_consume_token (parser->lexer);
18923 if (!cp_parser_objc_selector_p (token->type))
18925 error ("invalid Objective-C++ selector name");
18926 return error_mark_node;
18929 /* C++ operator names are allowed to appear in ObjC selectors. */
18930 switch (token->type)
18932 case CPP_AND_AND: return get_identifier ("and");
18933 case CPP_AND_EQ: return get_identifier ("and_eq");
18934 case CPP_AND: return get_identifier ("bitand");
18935 case CPP_OR: return get_identifier ("bitor");
18936 case CPP_COMPL: return get_identifier ("compl");
18937 case CPP_NOT: return get_identifier ("not");
18938 case CPP_NOT_EQ: return get_identifier ("not_eq");
18939 case CPP_OR_OR: return get_identifier ("or");
18940 case CPP_OR_EQ: return get_identifier ("or_eq");
18941 case CPP_XOR: return get_identifier ("xor");
18942 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18943 default: return token->u.value;
18947 /* Parse an Objective-C params list. */
18949 static tree
18950 cp_parser_objc_method_keyword_params (cp_parser* parser)
18952 tree params = NULL_TREE;
18953 bool maybe_unary_selector_p = true;
18954 cp_token *token = cp_lexer_peek_token (parser->lexer);
18956 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18958 tree selector = NULL_TREE, typename, identifier;
18960 if (token->type != CPP_COLON)
18961 selector = cp_parser_objc_selector (parser);
18963 /* Detect if we have a unary selector. */
18964 if (maybe_unary_selector_p
18965 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18966 return selector;
18968 maybe_unary_selector_p = false;
18969 cp_parser_require (parser, CPP_COLON, "`:'");
18970 typename = cp_parser_objc_typename (parser);
18971 identifier = cp_parser_identifier (parser);
18973 params
18974 = chainon (params,
18975 objc_build_keyword_decl (selector,
18976 typename,
18977 identifier));
18979 token = cp_lexer_peek_token (parser->lexer);
18982 return params;
18985 /* Parse the non-keyword Objective-C params. */
18987 static tree
18988 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18990 tree params = make_node (TREE_LIST);
18991 cp_token *token = cp_lexer_peek_token (parser->lexer);
18992 *ellipsisp = false; /* Initially, assume no ellipsis. */
18994 while (token->type == CPP_COMMA)
18996 cp_parameter_declarator *parmdecl;
18997 tree parm;
18999 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19000 token = cp_lexer_peek_token (parser->lexer);
19002 if (token->type == CPP_ELLIPSIS)
19004 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19005 *ellipsisp = true;
19006 break;
19009 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19010 parm = grokdeclarator (parmdecl->declarator,
19011 &parmdecl->decl_specifiers,
19012 PARM, /*initialized=*/0,
19013 /*attrlist=*/NULL);
19015 chainon (params, build_tree_list (NULL_TREE, parm));
19016 token = cp_lexer_peek_token (parser->lexer);
19019 return params;
19022 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19024 static void
19025 cp_parser_objc_interstitial_code (cp_parser* parser)
19027 cp_token *token = cp_lexer_peek_token (parser->lexer);
19029 /* If the next token is `extern' and the following token is a string
19030 literal, then we have a linkage specification. */
19031 if (token->keyword == RID_EXTERN
19032 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19033 cp_parser_linkage_specification (parser);
19034 /* Handle #pragma, if any. */
19035 else if (token->type == CPP_PRAGMA)
19036 cp_parser_pragma (parser, pragma_external);
19037 /* Allow stray semicolons. */
19038 else if (token->type == CPP_SEMICOLON)
19039 cp_lexer_consume_token (parser->lexer);
19040 /* Finally, try to parse a block-declaration, or a function-definition. */
19041 else
19042 cp_parser_block_declaration (parser, /*statement_p=*/false);
19045 /* Parse a method signature. */
19047 static tree
19048 cp_parser_objc_method_signature (cp_parser* parser)
19050 tree rettype, kwdparms, optparms;
19051 bool ellipsis = false;
19053 cp_parser_objc_method_type (parser);
19054 rettype = cp_parser_objc_typename (parser);
19055 kwdparms = cp_parser_objc_method_keyword_params (parser);
19056 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19058 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19061 /* Pars an Objective-C method prototype list. */
19063 static void
19064 cp_parser_objc_method_prototype_list (cp_parser* parser)
19066 cp_token *token = cp_lexer_peek_token (parser->lexer);
19068 while (token->keyword != RID_AT_END)
19070 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19072 objc_add_method_declaration
19073 (cp_parser_objc_method_signature (parser));
19074 cp_parser_consume_semicolon_at_end_of_statement (parser);
19076 else
19077 /* Allow for interspersed non-ObjC++ code. */
19078 cp_parser_objc_interstitial_code (parser);
19080 token = cp_lexer_peek_token (parser->lexer);
19083 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19084 objc_finish_interface ();
19087 /* Parse an Objective-C method definition list. */
19089 static void
19090 cp_parser_objc_method_definition_list (cp_parser* parser)
19092 cp_token *token = cp_lexer_peek_token (parser->lexer);
19094 while (token->keyword != RID_AT_END)
19096 tree meth;
19098 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19100 push_deferring_access_checks (dk_deferred);
19101 objc_start_method_definition
19102 (cp_parser_objc_method_signature (parser));
19104 /* For historical reasons, we accept an optional semicolon. */
19105 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19106 cp_lexer_consume_token (parser->lexer);
19108 perform_deferred_access_checks ();
19109 stop_deferring_access_checks ();
19110 meth = cp_parser_function_definition_after_declarator (parser,
19111 false);
19112 pop_deferring_access_checks ();
19113 objc_finish_method_definition (meth);
19115 else
19116 /* Allow for interspersed non-ObjC++ code. */
19117 cp_parser_objc_interstitial_code (parser);
19119 token = cp_lexer_peek_token (parser->lexer);
19122 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19123 objc_finish_implementation ();
19126 /* Parse Objective-C ivars. */
19128 static void
19129 cp_parser_objc_class_ivars (cp_parser* parser)
19131 cp_token *token = cp_lexer_peek_token (parser->lexer);
19133 if (token->type != CPP_OPEN_BRACE)
19134 return; /* No ivars specified. */
19136 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19137 token = cp_lexer_peek_token (parser->lexer);
19139 while (token->type != CPP_CLOSE_BRACE)
19141 cp_decl_specifier_seq declspecs;
19142 int decl_class_or_enum_p;
19143 tree prefix_attributes;
19145 cp_parser_objc_visibility_spec (parser);
19147 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19148 break;
19150 cp_parser_decl_specifier_seq (parser,
19151 CP_PARSER_FLAGS_OPTIONAL,
19152 &declspecs,
19153 &decl_class_or_enum_p);
19154 prefix_attributes = declspecs.attributes;
19155 declspecs.attributes = NULL_TREE;
19157 /* Keep going until we hit the `;' at the end of the
19158 declaration. */
19159 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19161 tree width = NULL_TREE, attributes, first_attribute, decl;
19162 cp_declarator *declarator = NULL;
19163 int ctor_dtor_or_conv_p;
19165 /* Check for a (possibly unnamed) bitfield declaration. */
19166 token = cp_lexer_peek_token (parser->lexer);
19167 if (token->type == CPP_COLON)
19168 goto eat_colon;
19170 if (token->type == CPP_NAME
19171 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19172 == CPP_COLON))
19174 /* Get the name of the bitfield. */
19175 declarator = make_id_declarator (NULL_TREE,
19176 cp_parser_identifier (parser),
19177 sfk_none);
19179 eat_colon:
19180 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19181 /* Get the width of the bitfield. */
19182 width
19183 = cp_parser_constant_expression (parser,
19184 /*allow_non_constant=*/false,
19185 NULL);
19187 else
19189 /* Parse the declarator. */
19190 declarator
19191 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19192 &ctor_dtor_or_conv_p,
19193 /*parenthesized_p=*/NULL,
19194 /*member_p=*/false);
19197 /* Look for attributes that apply to the ivar. */
19198 attributes = cp_parser_attributes_opt (parser);
19199 /* Remember which attributes are prefix attributes and
19200 which are not. */
19201 first_attribute = attributes;
19202 /* Combine the attributes. */
19203 attributes = chainon (prefix_attributes, attributes);
19205 if (width)
19207 /* Create the bitfield declaration. */
19208 decl = grokbitfield (declarator, &declspecs, width);
19209 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
19211 else
19212 decl = grokfield (declarator, &declspecs,
19213 NULL_TREE, /*init_const_expr_p=*/false,
19214 NULL_TREE, attributes);
19216 /* Add the instance variable. */
19217 objc_add_instance_variable (decl);
19219 /* Reset PREFIX_ATTRIBUTES. */
19220 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19221 attributes = TREE_CHAIN (attributes);
19222 if (attributes)
19223 TREE_CHAIN (attributes) = NULL_TREE;
19225 token = cp_lexer_peek_token (parser->lexer);
19227 if (token->type == CPP_COMMA)
19229 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19230 continue;
19232 break;
19235 cp_parser_consume_semicolon_at_end_of_statement (parser);
19236 token = cp_lexer_peek_token (parser->lexer);
19239 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19240 /* For historical reasons, we accept an optional semicolon. */
19241 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19242 cp_lexer_consume_token (parser->lexer);
19245 /* Parse an Objective-C protocol declaration. */
19247 static void
19248 cp_parser_objc_protocol_declaration (cp_parser* parser)
19250 tree proto, protorefs;
19251 cp_token *tok;
19253 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19256 error ("identifier expected after %<@protocol%>");
19257 goto finish;
19260 /* See if we have a forward declaration or a definition. */
19261 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19263 /* Try a forward declaration first. */
19264 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19266 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19267 finish:
19268 cp_parser_consume_semicolon_at_end_of_statement (parser);
19271 /* Ok, we got a full-fledged definition (or at least should). */
19272 else
19274 proto = cp_parser_identifier (parser);
19275 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19276 objc_start_protocol (proto, protorefs);
19277 cp_parser_objc_method_prototype_list (parser);
19281 /* Parse an Objective-C superclass or category. */
19283 static void
19284 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19285 tree *categ)
19287 cp_token *next = cp_lexer_peek_token (parser->lexer);
19289 *super = *categ = NULL_TREE;
19290 if (next->type == CPP_COLON)
19292 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19293 *super = cp_parser_identifier (parser);
19295 else if (next->type == CPP_OPEN_PAREN)
19297 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19298 *categ = cp_parser_identifier (parser);
19299 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19303 /* Parse an Objective-C class interface. */
19305 static void
19306 cp_parser_objc_class_interface (cp_parser* parser)
19308 tree name, super, categ, protos;
19310 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19311 name = cp_parser_identifier (parser);
19312 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19313 protos = cp_parser_objc_protocol_refs_opt (parser);
19315 /* We have either a class or a category on our hands. */
19316 if (categ)
19317 objc_start_category_interface (name, categ, protos);
19318 else
19320 objc_start_class_interface (name, super, protos);
19321 /* Handle instance variable declarations, if any. */
19322 cp_parser_objc_class_ivars (parser);
19323 objc_continue_interface ();
19326 cp_parser_objc_method_prototype_list (parser);
19329 /* Parse an Objective-C class implementation. */
19331 static void
19332 cp_parser_objc_class_implementation (cp_parser* parser)
19334 tree name, super, categ;
19336 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19337 name = cp_parser_identifier (parser);
19338 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19340 /* We have either a class or a category on our hands. */
19341 if (categ)
19342 objc_start_category_implementation (name, categ);
19343 else
19345 objc_start_class_implementation (name, super);
19346 /* Handle instance variable declarations, if any. */
19347 cp_parser_objc_class_ivars (parser);
19348 objc_continue_implementation ();
19351 cp_parser_objc_method_definition_list (parser);
19354 /* Consume the @end token and finish off the implementation. */
19356 static void
19357 cp_parser_objc_end_implementation (cp_parser* parser)
19359 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19360 objc_finish_implementation ();
19363 /* Parse an Objective-C declaration. */
19365 static void
19366 cp_parser_objc_declaration (cp_parser* parser)
19368 /* Try to figure out what kind of declaration is present. */
19369 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19371 switch (kwd->keyword)
19373 case RID_AT_ALIAS:
19374 cp_parser_objc_alias_declaration (parser);
19375 break;
19376 case RID_AT_CLASS:
19377 cp_parser_objc_class_declaration (parser);
19378 break;
19379 case RID_AT_PROTOCOL:
19380 cp_parser_objc_protocol_declaration (parser);
19381 break;
19382 case RID_AT_INTERFACE:
19383 cp_parser_objc_class_interface (parser);
19384 break;
19385 case RID_AT_IMPLEMENTATION:
19386 cp_parser_objc_class_implementation (parser);
19387 break;
19388 case RID_AT_END:
19389 cp_parser_objc_end_implementation (parser);
19390 break;
19391 default:
19392 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19393 cp_parser_skip_to_end_of_block_or_statement (parser);
19397 /* Parse an Objective-C try-catch-finally statement.
19399 objc-try-catch-finally-stmt:
19400 @try compound-statement objc-catch-clause-seq [opt]
19401 objc-finally-clause [opt]
19403 objc-catch-clause-seq:
19404 objc-catch-clause objc-catch-clause-seq [opt]
19406 objc-catch-clause:
19407 @catch ( exception-declaration ) compound-statement
19409 objc-finally-clause
19410 @finally compound-statement
19412 Returns NULL_TREE. */
19414 static tree
19415 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19416 location_t location;
19417 tree stmt;
19419 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19420 location = cp_lexer_peek_token (parser->lexer)->location;
19421 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19422 node, lest it get absorbed into the surrounding block. */
19423 stmt = push_stmt_list ();
19424 cp_parser_compound_statement (parser, NULL, false);
19425 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19427 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19429 cp_parameter_declarator *parmdecl;
19430 tree parm;
19432 cp_lexer_consume_token (parser->lexer);
19433 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19434 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19435 parm = grokdeclarator (parmdecl->declarator,
19436 &parmdecl->decl_specifiers,
19437 PARM, /*initialized=*/0,
19438 /*attrlist=*/NULL);
19439 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19440 objc_begin_catch_clause (parm);
19441 cp_parser_compound_statement (parser, NULL, false);
19442 objc_finish_catch_clause ();
19445 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19447 cp_lexer_consume_token (parser->lexer);
19448 location = cp_lexer_peek_token (parser->lexer)->location;
19449 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19450 node, lest it get absorbed into the surrounding block. */
19451 stmt = push_stmt_list ();
19452 cp_parser_compound_statement (parser, NULL, false);
19453 objc_build_finally_clause (location, pop_stmt_list (stmt));
19456 return objc_finish_try_stmt ();
19459 /* Parse an Objective-C synchronized statement.
19461 objc-synchronized-stmt:
19462 @synchronized ( expression ) compound-statement
19464 Returns NULL_TREE. */
19466 static tree
19467 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19468 location_t location;
19469 tree lock, stmt;
19471 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19473 location = cp_lexer_peek_token (parser->lexer)->location;
19474 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19475 lock = cp_parser_expression (parser, false);
19476 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19478 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19479 node, lest it get absorbed into the surrounding block. */
19480 stmt = push_stmt_list ();
19481 cp_parser_compound_statement (parser, NULL, false);
19483 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19486 /* Parse an Objective-C throw statement.
19488 objc-throw-stmt:
19489 @throw assignment-expression [opt] ;
19491 Returns a constructed '@throw' statement. */
19493 static tree
19494 cp_parser_objc_throw_statement (cp_parser *parser) {
19495 tree expr = NULL_TREE;
19497 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19500 expr = cp_parser_assignment_expression (parser, false);
19502 cp_parser_consume_semicolon_at_end_of_statement (parser);
19504 return objc_build_throw_stmt (expr);
19507 /* Parse an Objective-C statement. */
19509 static tree
19510 cp_parser_objc_statement (cp_parser * parser) {
19511 /* Try to figure out what kind of declaration is present. */
19512 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19514 switch (kwd->keyword)
19516 case RID_AT_TRY:
19517 return cp_parser_objc_try_catch_finally_statement (parser);
19518 case RID_AT_SYNCHRONIZED:
19519 return cp_parser_objc_synchronized_statement (parser);
19520 case RID_AT_THROW:
19521 return cp_parser_objc_throw_statement (parser);
19522 default:
19523 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19524 cp_parser_skip_to_end_of_block_or_statement (parser);
19527 return error_mark_node;
19530 /* OpenMP 2.5 parsing routines. */
19532 /* Returns name of the next clause.
19533 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19534 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19535 returned and the token is consumed. */
19537 static pragma_omp_clause
19538 cp_parser_omp_clause_name (cp_parser *parser)
19540 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19542 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19543 result = PRAGMA_OMP_CLAUSE_IF;
19544 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19545 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19546 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19547 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19548 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19550 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19551 const char *p = IDENTIFIER_POINTER (id);
19553 switch (p[0])
19555 case 'c':
19556 if (!strcmp ("copyin", p))
19557 result = PRAGMA_OMP_CLAUSE_COPYIN;
19558 else if (!strcmp ("copyprivate", p))
19559 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19560 break;
19561 case 'f':
19562 if (!strcmp ("firstprivate", p))
19563 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19564 break;
19565 case 'l':
19566 if (!strcmp ("lastprivate", p))
19567 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19568 break;
19569 case 'n':
19570 if (!strcmp ("nowait", p))
19571 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19572 else if (!strcmp ("num_threads", p))
19573 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19574 break;
19575 case 'o':
19576 if (!strcmp ("ordered", p))
19577 result = PRAGMA_OMP_CLAUSE_ORDERED;
19578 break;
19579 case 'r':
19580 if (!strcmp ("reduction", p))
19581 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19582 break;
19583 case 's':
19584 if (!strcmp ("schedule", p))
19585 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19586 else if (!strcmp ("shared", p))
19587 result = PRAGMA_OMP_CLAUSE_SHARED;
19588 break;
19592 if (result != PRAGMA_OMP_CLAUSE_NONE)
19593 cp_lexer_consume_token (parser->lexer);
19595 return result;
19598 /* Validate that a clause of the given type does not already exist. */
19600 static void
19601 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19603 tree c;
19605 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19606 if (OMP_CLAUSE_CODE (c) == code)
19608 error ("too many %qs clauses", name);
19609 break;
19613 /* OpenMP 2.5:
19614 variable-list:
19615 identifier
19616 variable-list , identifier
19618 In addition, we match a closing parenthesis. An opening parenthesis
19619 will have been consumed by the caller.
19621 If KIND is nonzero, create the appropriate node and install the decl
19622 in OMP_CLAUSE_DECL and add the node to the head of the list.
19624 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19625 return the list created. */
19627 static tree
19628 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19629 tree list)
19631 while (1)
19633 tree name, decl;
19635 name = cp_parser_id_expression (parser, /*template_p=*/false,
19636 /*check_dependency_p=*/true,
19637 /*template_p=*/NULL,
19638 /*declarator_p=*/false,
19639 /*optional_p=*/false);
19640 if (name == error_mark_node)
19641 goto skip_comma;
19643 decl = cp_parser_lookup_name_simple (parser, name);
19644 if (decl == error_mark_node)
19645 cp_parser_name_lookup_error (parser, name, decl, NULL);
19646 else if (kind != 0)
19648 tree u = build_omp_clause (kind);
19649 OMP_CLAUSE_DECL (u) = decl;
19650 OMP_CLAUSE_CHAIN (u) = list;
19651 list = u;
19653 else
19654 list = tree_cons (decl, NULL_TREE, list);
19656 get_comma:
19657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19658 break;
19659 cp_lexer_consume_token (parser->lexer);
19662 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19664 int ending;
19666 /* Try to resync to an unnested comma. Copied from
19667 cp_parser_parenthesized_expression_list. */
19668 skip_comma:
19669 ending = cp_parser_skip_to_closing_parenthesis (parser,
19670 /*recovering=*/true,
19671 /*or_comma=*/true,
19672 /*consume_paren=*/true);
19673 if (ending < 0)
19674 goto get_comma;
19677 return list;
19680 /* Similarly, but expect leading and trailing parenthesis. This is a very
19681 common case for omp clauses. */
19683 static tree
19684 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19686 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19687 return cp_parser_omp_var_list_no_open (parser, kind, list);
19688 return list;
19691 /* OpenMP 2.5:
19692 default ( shared | none ) */
19694 static tree
19695 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19697 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19698 tree c;
19700 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19701 return list;
19702 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19704 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19705 const char *p = IDENTIFIER_POINTER (id);
19707 switch (p[0])
19709 case 'n':
19710 if (strcmp ("none", p) != 0)
19711 goto invalid_kind;
19712 kind = OMP_CLAUSE_DEFAULT_NONE;
19713 break;
19715 case 's':
19716 if (strcmp ("shared", p) != 0)
19717 goto invalid_kind;
19718 kind = OMP_CLAUSE_DEFAULT_SHARED;
19719 break;
19721 default:
19722 goto invalid_kind;
19725 cp_lexer_consume_token (parser->lexer);
19727 else
19729 invalid_kind:
19730 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19733 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19734 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19735 /*or_comma=*/false,
19736 /*consume_paren=*/true);
19738 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19739 return list;
19741 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19742 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19743 OMP_CLAUSE_CHAIN (c) = list;
19744 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19746 return c;
19749 /* OpenMP 2.5:
19750 if ( expression ) */
19752 static tree
19753 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19755 tree t, c;
19757 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19758 return list;
19760 t = cp_parser_condition (parser);
19762 if (t == error_mark_node
19763 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19764 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19765 /*or_comma=*/false,
19766 /*consume_paren=*/true);
19768 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19770 c = build_omp_clause (OMP_CLAUSE_IF);
19771 OMP_CLAUSE_IF_EXPR (c) = t;
19772 OMP_CLAUSE_CHAIN (c) = list;
19774 return c;
19777 /* OpenMP 2.5:
19778 nowait */
19780 static tree
19781 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19783 tree c;
19785 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19787 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19788 OMP_CLAUSE_CHAIN (c) = list;
19789 return c;
19792 /* OpenMP 2.5:
19793 num_threads ( expression ) */
19795 static tree
19796 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19798 tree t, c;
19800 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19801 return list;
19803 t = cp_parser_expression (parser, false);
19805 if (t == error_mark_node
19806 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19807 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19808 /*or_comma=*/false,
19809 /*consume_paren=*/true);
19811 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19813 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19814 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19815 OMP_CLAUSE_CHAIN (c) = list;
19817 return c;
19820 /* OpenMP 2.5:
19821 ordered */
19823 static tree
19824 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19826 tree c;
19828 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19830 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19831 OMP_CLAUSE_CHAIN (c) = list;
19832 return c;
19835 /* OpenMP 2.5:
19836 reduction ( reduction-operator : variable-list )
19838 reduction-operator:
19839 One of: + * - & ^ | && || */
19841 static tree
19842 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19844 enum tree_code code;
19845 tree nlist, c;
19847 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19848 return list;
19850 switch (cp_lexer_peek_token (parser->lexer)->type)
19852 case CPP_PLUS:
19853 code = PLUS_EXPR;
19854 break;
19855 case CPP_MULT:
19856 code = MULT_EXPR;
19857 break;
19858 case CPP_MINUS:
19859 code = MINUS_EXPR;
19860 break;
19861 case CPP_AND:
19862 code = BIT_AND_EXPR;
19863 break;
19864 case CPP_XOR:
19865 code = BIT_XOR_EXPR;
19866 break;
19867 case CPP_OR:
19868 code = BIT_IOR_EXPR;
19869 break;
19870 case CPP_AND_AND:
19871 code = TRUTH_ANDIF_EXPR;
19872 break;
19873 case CPP_OR_OR:
19874 code = TRUTH_ORIF_EXPR;
19875 break;
19876 default:
19877 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19878 resync_fail:
19879 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19880 /*or_comma=*/false,
19881 /*consume_paren=*/true);
19882 return list;
19884 cp_lexer_consume_token (parser->lexer);
19886 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19887 goto resync_fail;
19889 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19890 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19891 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19893 return nlist;
19896 /* OpenMP 2.5:
19897 schedule ( schedule-kind )
19898 schedule ( schedule-kind , expression )
19900 schedule-kind:
19901 static | dynamic | guided | runtime */
19903 static tree
19904 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19906 tree c, t;
19908 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19909 return list;
19911 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19913 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19915 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19916 const char *p = IDENTIFIER_POINTER (id);
19918 switch (p[0])
19920 case 'd':
19921 if (strcmp ("dynamic", p) != 0)
19922 goto invalid_kind;
19923 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19924 break;
19926 case 'g':
19927 if (strcmp ("guided", p) != 0)
19928 goto invalid_kind;
19929 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19930 break;
19932 case 'r':
19933 if (strcmp ("runtime", p) != 0)
19934 goto invalid_kind;
19935 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19936 break;
19938 default:
19939 goto invalid_kind;
19942 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19943 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19944 else
19945 goto invalid_kind;
19946 cp_lexer_consume_token (parser->lexer);
19948 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19950 cp_lexer_consume_token (parser->lexer);
19952 t = cp_parser_assignment_expression (parser, false);
19954 if (t == error_mark_node)
19955 goto resync_fail;
19956 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19957 error ("schedule %<runtime%> does not take "
19958 "a %<chunk_size%> parameter");
19959 else
19960 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19962 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19963 goto resync_fail;
19965 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19966 goto resync_fail;
19968 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19969 OMP_CLAUSE_CHAIN (c) = list;
19970 return c;
19972 invalid_kind:
19973 cp_parser_error (parser, "invalid schedule kind");
19974 resync_fail:
19975 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19976 /*or_comma=*/false,
19977 /*consume_paren=*/true);
19978 return list;
19981 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19982 is a bitmask in MASK. Return the list of clauses found; the result
19983 of clause default goes in *pdefault. */
19985 static tree
19986 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19987 const char *where, cp_token *pragma_tok)
19989 tree clauses = NULL;
19990 bool first = true;
19992 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19994 pragma_omp_clause c_kind;
19995 const char *c_name;
19996 tree prev = clauses;
19998 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19999 cp_lexer_consume_token (parser->lexer);
20001 c_kind = cp_parser_omp_clause_name (parser);
20002 first = false;
20004 switch (c_kind)
20006 case PRAGMA_OMP_CLAUSE_COPYIN:
20007 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20008 c_name = "copyin";
20009 break;
20010 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20011 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20012 clauses);
20013 c_name = "copyprivate";
20014 break;
20015 case PRAGMA_OMP_CLAUSE_DEFAULT:
20016 clauses = cp_parser_omp_clause_default (parser, clauses);
20017 c_name = "default";
20018 break;
20019 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20020 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20021 clauses);
20022 c_name = "firstprivate";
20023 break;
20024 case PRAGMA_OMP_CLAUSE_IF:
20025 clauses = cp_parser_omp_clause_if (parser, clauses);
20026 c_name = "if";
20027 break;
20028 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20029 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20030 clauses);
20031 c_name = "lastprivate";
20032 break;
20033 case PRAGMA_OMP_CLAUSE_NOWAIT:
20034 clauses = cp_parser_omp_clause_nowait (parser, clauses);
20035 c_name = "nowait";
20036 break;
20037 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20038 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
20039 c_name = "num_threads";
20040 break;
20041 case PRAGMA_OMP_CLAUSE_ORDERED:
20042 clauses = cp_parser_omp_clause_ordered (parser, clauses);
20043 c_name = "ordered";
20044 break;
20045 case PRAGMA_OMP_CLAUSE_PRIVATE:
20046 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20047 clauses);
20048 c_name = "private";
20049 break;
20050 case PRAGMA_OMP_CLAUSE_REDUCTION:
20051 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20052 c_name = "reduction";
20053 break;
20054 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20055 clauses = cp_parser_omp_clause_schedule (parser, clauses);
20056 c_name = "schedule";
20057 break;
20058 case PRAGMA_OMP_CLAUSE_SHARED:
20059 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20060 clauses);
20061 c_name = "shared";
20062 break;
20063 default:
20064 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20065 goto saw_error;
20068 if (((mask >> c_kind) & 1) == 0)
20070 /* Remove the invalid clause(s) from the list to avoid
20071 confusing the rest of the compiler. */
20072 clauses = prev;
20073 error ("%qs is not valid for %qs", c_name, where);
20076 saw_error:
20077 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20078 return finish_omp_clauses (clauses);
20081 /* OpenMP 2.5:
20082 structured-block:
20083 statement
20085 In practice, we're also interested in adding the statement to an
20086 outer node. So it is convenient if we work around the fact that
20087 cp_parser_statement calls add_stmt. */
20089 static unsigned
20090 cp_parser_begin_omp_structured_block (cp_parser *parser)
20092 unsigned save = parser->in_statement;
20094 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20095 This preserves the "not within loop or switch" style error messages
20096 for nonsense cases like
20097 void foo() {
20098 #pragma omp single
20099 break;
20102 if (parser->in_statement)
20103 parser->in_statement = IN_OMP_BLOCK;
20105 return save;
20108 static void
20109 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20111 parser->in_statement = save;
20114 static tree
20115 cp_parser_omp_structured_block (cp_parser *parser)
20117 tree stmt = begin_omp_structured_block ();
20118 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20120 cp_parser_statement (parser, NULL_TREE, false, NULL);
20122 cp_parser_end_omp_structured_block (parser, save);
20123 return finish_omp_structured_block (stmt);
20126 /* OpenMP 2.5:
20127 # pragma omp atomic new-line
20128 expression-stmt
20130 expression-stmt:
20131 x binop= expr | x++ | ++x | x-- | --x
20132 binop:
20133 +, *, -, /, &, ^, |, <<, >>
20135 where x is an lvalue expression with scalar type. */
20137 static void
20138 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20140 tree lhs, rhs;
20141 enum tree_code code;
20143 cp_parser_require_pragma_eol (parser, pragma_tok);
20145 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20146 /*cast_p=*/false);
20147 switch (TREE_CODE (lhs))
20149 case ERROR_MARK:
20150 goto saw_error;
20152 case PREINCREMENT_EXPR:
20153 case POSTINCREMENT_EXPR:
20154 lhs = TREE_OPERAND (lhs, 0);
20155 code = PLUS_EXPR;
20156 rhs = integer_one_node;
20157 break;
20159 case PREDECREMENT_EXPR:
20160 case POSTDECREMENT_EXPR:
20161 lhs = TREE_OPERAND (lhs, 0);
20162 code = MINUS_EXPR;
20163 rhs = integer_one_node;
20164 break;
20166 default:
20167 switch (cp_lexer_peek_token (parser->lexer)->type)
20169 case CPP_MULT_EQ:
20170 code = MULT_EXPR;
20171 break;
20172 case CPP_DIV_EQ:
20173 code = TRUNC_DIV_EXPR;
20174 break;
20175 case CPP_PLUS_EQ:
20176 code = PLUS_EXPR;
20177 break;
20178 case CPP_MINUS_EQ:
20179 code = MINUS_EXPR;
20180 break;
20181 case CPP_LSHIFT_EQ:
20182 code = LSHIFT_EXPR;
20183 break;
20184 case CPP_RSHIFT_EQ:
20185 code = RSHIFT_EXPR;
20186 break;
20187 case CPP_AND_EQ:
20188 code = BIT_AND_EXPR;
20189 break;
20190 case CPP_OR_EQ:
20191 code = BIT_IOR_EXPR;
20192 break;
20193 case CPP_XOR_EQ:
20194 code = BIT_XOR_EXPR;
20195 break;
20196 default:
20197 cp_parser_error (parser,
20198 "invalid operator for %<#pragma omp atomic%>");
20199 goto saw_error;
20201 cp_lexer_consume_token (parser->lexer);
20203 rhs = cp_parser_expression (parser, false);
20204 if (rhs == error_mark_node)
20205 goto saw_error;
20206 break;
20208 finish_omp_atomic (code, lhs, rhs);
20209 cp_parser_consume_semicolon_at_end_of_statement (parser);
20210 return;
20212 saw_error:
20213 cp_parser_skip_to_end_of_block_or_statement (parser);
20217 /* OpenMP 2.5:
20218 # pragma omp barrier new-line */
20220 static void
20221 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20223 cp_parser_require_pragma_eol (parser, pragma_tok);
20224 finish_omp_barrier ();
20227 /* OpenMP 2.5:
20228 # pragma omp critical [(name)] new-line
20229 structured-block */
20231 static tree
20232 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20234 tree stmt, name = NULL;
20236 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20238 cp_lexer_consume_token (parser->lexer);
20240 name = cp_parser_identifier (parser);
20242 if (name == error_mark_node
20243 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20244 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20245 /*or_comma=*/false,
20246 /*consume_paren=*/true);
20247 if (name == error_mark_node)
20248 name = NULL;
20250 cp_parser_require_pragma_eol (parser, pragma_tok);
20252 stmt = cp_parser_omp_structured_block (parser);
20253 return c_finish_omp_critical (stmt, name);
20256 /* OpenMP 2.5:
20257 # pragma omp flush flush-vars[opt] new-line
20259 flush-vars:
20260 ( variable-list ) */
20262 static void
20263 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20265 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20266 (void) cp_parser_omp_var_list (parser, 0, NULL);
20267 cp_parser_require_pragma_eol (parser, pragma_tok);
20269 finish_omp_flush ();
20272 /* Parse the restricted form of the for statment allowed by OpenMP. */
20274 static tree
20275 cp_parser_omp_for_loop (cp_parser *parser)
20277 tree init, cond, incr, body, decl, pre_body;
20278 location_t loc;
20280 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20282 cp_parser_error (parser, "for statement expected");
20283 return NULL;
20285 loc = cp_lexer_consume_token (parser->lexer)->location;
20286 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20287 return NULL;
20289 init = decl = NULL;
20290 pre_body = push_stmt_list ();
20291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20293 cp_decl_specifier_seq type_specifiers;
20295 /* First, try to parse as an initialized declaration. See
20296 cp_parser_condition, from whence the bulk of this is copied. */
20298 cp_parser_parse_tentatively (parser);
20299 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20300 &type_specifiers);
20301 if (!cp_parser_error_occurred (parser))
20303 tree asm_specification, attributes;
20304 cp_declarator *declarator;
20306 declarator = cp_parser_declarator (parser,
20307 CP_PARSER_DECLARATOR_NAMED,
20308 /*ctor_dtor_or_conv_p=*/NULL,
20309 /*parenthesized_p=*/NULL,
20310 /*member_p=*/false);
20311 attributes = cp_parser_attributes_opt (parser);
20312 asm_specification = cp_parser_asm_specification_opt (parser);
20314 cp_parser_require (parser, CPP_EQ, "`='");
20315 if (cp_parser_parse_definitely (parser))
20317 tree pushed_scope;
20319 decl = start_decl (declarator, &type_specifiers,
20320 /*initialized_p=*/false, attributes,
20321 /*prefix_attributes=*/NULL_TREE,
20322 &pushed_scope);
20324 init = cp_parser_assignment_expression (parser, false);
20326 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
20327 asm_specification, LOOKUP_ONLYCONVERTING);
20329 if (pushed_scope)
20330 pop_scope (pushed_scope);
20333 else
20334 cp_parser_abort_tentative_parse (parser);
20336 /* If parsing as an initialized declaration failed, try again as
20337 a simple expression. */
20338 if (decl == NULL)
20339 init = cp_parser_expression (parser, false);
20341 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20342 pre_body = pop_stmt_list (pre_body);
20344 cond = NULL;
20345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20346 cond = cp_parser_condition (parser);
20347 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20349 incr = NULL;
20350 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20351 incr = cp_parser_expression (parser, false);
20353 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20354 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20355 /*or_comma=*/false,
20356 /*consume_paren=*/true);
20358 /* Note that we saved the original contents of this flag when we entered
20359 the structured block, and so we don't need to re-save it here. */
20360 parser->in_statement = IN_OMP_FOR;
20362 /* Note that the grammar doesn't call for a structured block here,
20363 though the loop as a whole is a structured block. */
20364 body = push_stmt_list ();
20365 cp_parser_statement (parser, NULL_TREE, false, NULL);
20366 body = pop_stmt_list (body);
20368 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
20371 /* OpenMP 2.5:
20372 #pragma omp for for-clause[optseq] new-line
20373 for-loop */
20375 #define OMP_FOR_CLAUSE_MASK \
20376 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20377 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20378 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20379 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20380 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
20381 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
20382 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20384 static tree
20385 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20387 tree clauses, sb, ret;
20388 unsigned int save;
20390 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20391 "#pragma omp for", pragma_tok);
20393 sb = begin_omp_structured_block ();
20394 save = cp_parser_begin_omp_structured_block (parser);
20396 ret = cp_parser_omp_for_loop (parser);
20397 if (ret)
20398 OMP_FOR_CLAUSES (ret) = clauses;
20400 cp_parser_end_omp_structured_block (parser, save);
20401 add_stmt (finish_omp_structured_block (sb));
20403 return ret;
20406 /* OpenMP 2.5:
20407 # pragma omp master new-line
20408 structured-block */
20410 static tree
20411 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20413 cp_parser_require_pragma_eol (parser, pragma_tok);
20414 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20417 /* OpenMP 2.5:
20418 # pragma omp ordered new-line
20419 structured-block */
20421 static tree
20422 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20424 cp_parser_require_pragma_eol (parser, pragma_tok);
20425 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20428 /* OpenMP 2.5:
20430 section-scope:
20431 { section-sequence }
20433 section-sequence:
20434 section-directive[opt] structured-block
20435 section-sequence section-directive structured-block */
20437 static tree
20438 cp_parser_omp_sections_scope (cp_parser *parser)
20440 tree stmt, substmt;
20441 bool error_suppress = false;
20442 cp_token *tok;
20444 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20445 return NULL_TREE;
20447 stmt = push_stmt_list ();
20449 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20451 unsigned save;
20453 substmt = begin_omp_structured_block ();
20454 save = cp_parser_begin_omp_structured_block (parser);
20456 while (1)
20458 cp_parser_statement (parser, NULL_TREE, false, NULL);
20460 tok = cp_lexer_peek_token (parser->lexer);
20461 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20462 break;
20463 if (tok->type == CPP_CLOSE_BRACE)
20464 break;
20465 if (tok->type == CPP_EOF)
20466 break;
20469 cp_parser_end_omp_structured_block (parser, save);
20470 substmt = finish_omp_structured_block (substmt);
20471 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20472 add_stmt (substmt);
20475 while (1)
20477 tok = cp_lexer_peek_token (parser->lexer);
20478 if (tok->type == CPP_CLOSE_BRACE)
20479 break;
20480 if (tok->type == CPP_EOF)
20481 break;
20483 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20485 cp_lexer_consume_token (parser->lexer);
20486 cp_parser_require_pragma_eol (parser, tok);
20487 error_suppress = false;
20489 else if (!error_suppress)
20491 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20492 error_suppress = true;
20495 substmt = cp_parser_omp_structured_block (parser);
20496 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20497 add_stmt (substmt);
20499 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20501 substmt = pop_stmt_list (stmt);
20503 stmt = make_node (OMP_SECTIONS);
20504 TREE_TYPE (stmt) = void_type_node;
20505 OMP_SECTIONS_BODY (stmt) = substmt;
20507 add_stmt (stmt);
20508 return stmt;
20511 /* OpenMP 2.5:
20512 # pragma omp sections sections-clause[optseq] newline
20513 sections-scope */
20515 #define OMP_SECTIONS_CLAUSE_MASK \
20516 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20517 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20518 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20519 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20520 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20522 static tree
20523 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20525 tree clauses, ret;
20527 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20528 "#pragma omp sections", pragma_tok);
20530 ret = cp_parser_omp_sections_scope (parser);
20531 if (ret)
20532 OMP_SECTIONS_CLAUSES (ret) = clauses;
20534 return ret;
20537 /* OpenMP 2.5:
20538 # pragma parallel parallel-clause new-line
20539 # pragma parallel for parallel-for-clause new-line
20540 # pragma parallel sections parallel-sections-clause new-line */
20542 #define OMP_PARALLEL_CLAUSE_MASK \
20543 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
20544 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20545 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20546 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
20547 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
20548 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
20549 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20550 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20552 static tree
20553 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20555 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20556 const char *p_name = "#pragma omp parallel";
20557 tree stmt, clauses, par_clause, ws_clause, block;
20558 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20559 unsigned int save;
20561 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20563 cp_lexer_consume_token (parser->lexer);
20564 p_kind = PRAGMA_OMP_PARALLEL_FOR;
20565 p_name = "#pragma omp parallel for";
20566 mask |= OMP_FOR_CLAUSE_MASK;
20567 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20569 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20571 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20572 const char *p = IDENTIFIER_POINTER (id);
20573 if (strcmp (p, "sections") == 0)
20575 cp_lexer_consume_token (parser->lexer);
20576 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20577 p_name = "#pragma omp parallel sections";
20578 mask |= OMP_SECTIONS_CLAUSE_MASK;
20579 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20583 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20584 block = begin_omp_parallel ();
20585 save = cp_parser_begin_omp_structured_block (parser);
20587 switch (p_kind)
20589 case PRAGMA_OMP_PARALLEL:
20590 cp_parser_statement (parser, NULL_TREE, false, NULL);
20591 par_clause = clauses;
20592 break;
20594 case PRAGMA_OMP_PARALLEL_FOR:
20595 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20596 stmt = cp_parser_omp_for_loop (parser);
20597 if (stmt)
20598 OMP_FOR_CLAUSES (stmt) = ws_clause;
20599 break;
20601 case PRAGMA_OMP_PARALLEL_SECTIONS:
20602 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20603 stmt = cp_parser_omp_sections_scope (parser);
20604 if (stmt)
20605 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20606 break;
20608 default:
20609 gcc_unreachable ();
20612 cp_parser_end_omp_structured_block (parser, save);
20613 stmt = finish_omp_parallel (par_clause, block);
20614 if (p_kind != PRAGMA_OMP_PARALLEL)
20615 OMP_PARALLEL_COMBINED (stmt) = 1;
20616 return stmt;
20619 /* OpenMP 2.5:
20620 # pragma omp single single-clause[optseq] new-line
20621 structured-block */
20623 #define OMP_SINGLE_CLAUSE_MASK \
20624 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20625 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20626 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20627 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20629 static tree
20630 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20632 tree stmt = make_node (OMP_SINGLE);
20633 TREE_TYPE (stmt) = void_type_node;
20635 OMP_SINGLE_CLAUSES (stmt)
20636 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20637 "#pragma omp single", pragma_tok);
20638 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20640 return add_stmt (stmt);
20643 /* OpenMP 2.5:
20644 # pragma omp threadprivate (variable-list) */
20646 static void
20647 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20649 tree vars;
20651 vars = cp_parser_omp_var_list (parser, 0, NULL);
20652 cp_parser_require_pragma_eol (parser, pragma_tok);
20654 finish_omp_threadprivate (vars);
20657 /* Main entry point to OpenMP statement pragmas. */
20659 static void
20660 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20662 tree stmt;
20664 switch (pragma_tok->pragma_kind)
20666 case PRAGMA_OMP_ATOMIC:
20667 cp_parser_omp_atomic (parser, pragma_tok);
20668 return;
20669 case PRAGMA_OMP_CRITICAL:
20670 stmt = cp_parser_omp_critical (parser, pragma_tok);
20671 break;
20672 case PRAGMA_OMP_FOR:
20673 stmt = cp_parser_omp_for (parser, pragma_tok);
20674 break;
20675 case PRAGMA_OMP_MASTER:
20676 stmt = cp_parser_omp_master (parser, pragma_tok);
20677 break;
20678 case PRAGMA_OMP_ORDERED:
20679 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20680 break;
20681 case PRAGMA_OMP_PARALLEL:
20682 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20683 break;
20684 case PRAGMA_OMP_SECTIONS:
20685 stmt = cp_parser_omp_sections (parser, pragma_tok);
20686 break;
20687 case PRAGMA_OMP_SINGLE:
20688 stmt = cp_parser_omp_single (parser, pragma_tok);
20689 break;
20690 default:
20691 gcc_unreachable ();
20694 if (stmt)
20695 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20698 /* The parser. */
20700 static GTY (()) cp_parser *the_parser;
20703 /* Special handling for the first token or line in the file. The first
20704 thing in the file might be #pragma GCC pch_preprocess, which loads a
20705 PCH file, which is a GC collection point. So we need to handle this
20706 first pragma without benefit of an existing lexer structure.
20708 Always returns one token to the caller in *FIRST_TOKEN. This is
20709 either the true first token of the file, or the first token after
20710 the initial pragma. */
20712 static void
20713 cp_parser_initial_pragma (cp_token *first_token)
20715 tree name = NULL;
20717 cp_lexer_get_preprocessor_token (NULL, first_token);
20718 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20719 return;
20721 cp_lexer_get_preprocessor_token (NULL, first_token);
20722 if (first_token->type == CPP_STRING)
20724 name = first_token->u.value;
20726 cp_lexer_get_preprocessor_token (NULL, first_token);
20727 if (first_token->type != CPP_PRAGMA_EOL)
20728 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20730 else
20731 error ("expected string literal");
20733 /* Skip to the end of the pragma. */
20734 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20735 cp_lexer_get_preprocessor_token (NULL, first_token);
20737 /* Now actually load the PCH file. */
20738 if (name)
20739 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20741 /* Read one more token to return to our caller. We have to do this
20742 after reading the PCH file in, since its pointers have to be
20743 live. */
20744 cp_lexer_get_preprocessor_token (NULL, first_token);
20747 /* Normal parsing of a pragma token. Here we can (and must) use the
20748 regular lexer. */
20750 static bool
20751 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20753 cp_token *pragma_tok;
20754 unsigned int id;
20756 pragma_tok = cp_lexer_consume_token (parser->lexer);
20757 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20758 parser->lexer->in_pragma = true;
20760 id = pragma_tok->pragma_kind;
20761 switch (id)
20763 case PRAGMA_GCC_PCH_PREPROCESS:
20764 error ("%<#pragma GCC pch_preprocess%> must be first");
20765 break;
20767 case PRAGMA_OMP_BARRIER:
20768 switch (context)
20770 case pragma_compound:
20771 cp_parser_omp_barrier (parser, pragma_tok);
20772 return false;
20773 case pragma_stmt:
20774 error ("%<#pragma omp barrier%> may only be "
20775 "used in compound statements");
20776 break;
20777 default:
20778 goto bad_stmt;
20780 break;
20782 case PRAGMA_OMP_FLUSH:
20783 switch (context)
20785 case pragma_compound:
20786 cp_parser_omp_flush (parser, pragma_tok);
20787 return false;
20788 case pragma_stmt:
20789 error ("%<#pragma omp flush%> may only be "
20790 "used in compound statements");
20791 break;
20792 default:
20793 goto bad_stmt;
20795 break;
20797 case PRAGMA_OMP_THREADPRIVATE:
20798 cp_parser_omp_threadprivate (parser, pragma_tok);
20799 return false;
20801 case PRAGMA_OMP_ATOMIC:
20802 case PRAGMA_OMP_CRITICAL:
20803 case PRAGMA_OMP_FOR:
20804 case PRAGMA_OMP_MASTER:
20805 case PRAGMA_OMP_ORDERED:
20806 case PRAGMA_OMP_PARALLEL:
20807 case PRAGMA_OMP_SECTIONS:
20808 case PRAGMA_OMP_SINGLE:
20809 if (context == pragma_external)
20810 goto bad_stmt;
20811 cp_parser_omp_construct (parser, pragma_tok);
20812 return true;
20814 case PRAGMA_OMP_SECTION:
20815 error ("%<#pragma omp section%> may only be used in "
20816 "%<#pragma omp sections%> construct");
20817 break;
20819 default:
20820 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20821 c_invoke_pragma_handler (id);
20822 break;
20824 bad_stmt:
20825 cp_parser_error (parser, "expected declaration specifiers");
20826 break;
20829 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20830 return false;
20833 /* The interface the pragma parsers have to the lexer. */
20835 enum cpp_ttype
20836 pragma_lex (tree *value)
20838 cp_token *tok;
20839 enum cpp_ttype ret;
20841 tok = cp_lexer_peek_token (the_parser->lexer);
20843 ret = tok->type;
20844 *value = tok->u.value;
20846 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20847 ret = CPP_EOF;
20848 else if (ret == CPP_STRING)
20849 *value = cp_parser_string_literal (the_parser, false, false);
20850 else
20852 cp_lexer_consume_token (the_parser->lexer);
20853 if (ret == CPP_KEYWORD)
20854 ret = CPP_NAME;
20857 return ret;
20861 /* External interface. */
20863 /* Parse one entire translation unit. */
20865 void
20866 c_parse_file (void)
20868 bool error_occurred;
20869 static bool already_called = false;
20871 if (already_called)
20873 sorry ("inter-module optimizations not implemented for C++");
20874 return;
20876 already_called = true;
20878 the_parser = cp_parser_new ();
20879 push_deferring_access_checks (flag_access_control
20880 ? dk_no_deferred : dk_no_check);
20881 error_occurred = cp_parser_translation_unit (the_parser);
20882 the_parser = NULL;
20885 #include "gt-cp-parser.h"