Daily bump.
[official-gcc.git] / gcc / cp / parser.c
bloba5bd05519b4e8991964b341b143775d11bf2b6aa
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 *);
1714 /* Declarators [gram.dcl.decl] */
1716 static tree cp_parser_init_declarator
1717 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1718 static cp_declarator *cp_parser_declarator
1719 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1720 static cp_declarator *cp_parser_direct_declarator
1721 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1722 static enum tree_code cp_parser_ptr_operator
1723 (cp_parser *, tree *, cp_cv_quals *);
1724 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1725 (cp_parser *);
1726 static tree cp_parser_declarator_id
1727 (cp_parser *, bool);
1728 static tree cp_parser_type_id
1729 (cp_parser *);
1730 static void cp_parser_type_specifier_seq
1731 (cp_parser *, bool, cp_decl_specifier_seq *);
1732 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1733 (cp_parser *);
1734 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1735 (cp_parser *, bool *);
1736 static cp_parameter_declarator *cp_parser_parameter_declaration
1737 (cp_parser *, bool, bool *);
1738 static tree cp_parser_default_argument
1739 (cp_parser *, bool);
1740 static void cp_parser_function_body
1741 (cp_parser *);
1742 static tree cp_parser_initializer
1743 (cp_parser *, bool *, bool *);
1744 static tree cp_parser_initializer_clause
1745 (cp_parser *, bool *);
1746 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1747 (cp_parser *, bool *);
1749 static bool cp_parser_ctor_initializer_opt_and_function_body
1750 (cp_parser *);
1752 /* Classes [gram.class] */
1754 static tree cp_parser_class_name
1755 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1756 static tree cp_parser_class_specifier
1757 (cp_parser *);
1758 static tree cp_parser_class_head
1759 (cp_parser *, bool *, tree *, tree *);
1760 static enum tag_types cp_parser_class_key
1761 (cp_parser *);
1762 static void cp_parser_member_specification_opt
1763 (cp_parser *);
1764 static void cp_parser_member_declaration
1765 (cp_parser *);
1766 static tree cp_parser_pure_specifier
1767 (cp_parser *);
1768 static tree cp_parser_constant_initializer
1769 (cp_parser *);
1771 /* Derived classes [gram.class.derived] */
1773 static tree cp_parser_base_clause
1774 (cp_parser *);
1775 static tree cp_parser_base_specifier
1776 (cp_parser *);
1778 /* Special member functions [gram.special] */
1780 static tree cp_parser_conversion_function_id
1781 (cp_parser *);
1782 static tree cp_parser_conversion_type_id
1783 (cp_parser *);
1784 static cp_declarator *cp_parser_conversion_declarator_opt
1785 (cp_parser *);
1786 static bool cp_parser_ctor_initializer_opt
1787 (cp_parser *);
1788 static void cp_parser_mem_initializer_list
1789 (cp_parser *);
1790 static tree cp_parser_mem_initializer
1791 (cp_parser *);
1792 static tree cp_parser_mem_initializer_id
1793 (cp_parser *);
1795 /* Overloading [gram.over] */
1797 static tree cp_parser_operator_function_id
1798 (cp_parser *);
1799 static tree cp_parser_operator
1800 (cp_parser *);
1802 /* Templates [gram.temp] */
1804 static void cp_parser_template_declaration
1805 (cp_parser *, bool);
1806 static tree cp_parser_template_parameter_list
1807 (cp_parser *);
1808 static tree cp_parser_template_parameter
1809 (cp_parser *, bool *, bool *);
1810 static tree cp_parser_type_parameter
1811 (cp_parser *, bool *);
1812 static tree cp_parser_template_id
1813 (cp_parser *, bool, bool, bool);
1814 static tree cp_parser_template_name
1815 (cp_parser *, bool, bool, bool, bool *);
1816 static tree cp_parser_template_argument_list
1817 (cp_parser *);
1818 static tree cp_parser_template_argument
1819 (cp_parser *);
1820 static void cp_parser_explicit_instantiation
1821 (cp_parser *);
1822 static void cp_parser_explicit_specialization
1823 (cp_parser *);
1825 /* Exception handling [gram.exception] */
1827 static tree cp_parser_try_block
1828 (cp_parser *);
1829 static bool cp_parser_function_try_block
1830 (cp_parser *);
1831 static void cp_parser_handler_seq
1832 (cp_parser *);
1833 static void cp_parser_handler
1834 (cp_parser *);
1835 static tree cp_parser_exception_declaration
1836 (cp_parser *);
1837 static tree cp_parser_throw_expression
1838 (cp_parser *);
1839 static tree cp_parser_exception_specification_opt
1840 (cp_parser *);
1841 static tree cp_parser_type_id_list
1842 (cp_parser *);
1844 /* GNU Extensions */
1846 static tree cp_parser_asm_specification_opt
1847 (cp_parser *);
1848 static tree cp_parser_asm_operand_list
1849 (cp_parser *);
1850 static tree cp_parser_asm_clobber_list
1851 (cp_parser *);
1852 static tree cp_parser_attributes_opt
1853 (cp_parser *);
1854 static tree cp_parser_attribute_list
1855 (cp_parser *);
1856 static bool cp_parser_extension_opt
1857 (cp_parser *, int *);
1858 static void cp_parser_label_declaration
1859 (cp_parser *);
1861 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1862 static bool cp_parser_pragma
1863 (cp_parser *, enum pragma_context);
1865 /* Objective-C++ Productions */
1867 static tree cp_parser_objc_message_receiver
1868 (cp_parser *);
1869 static tree cp_parser_objc_message_args
1870 (cp_parser *);
1871 static tree cp_parser_objc_message_expression
1872 (cp_parser *);
1873 static tree cp_parser_objc_encode_expression
1874 (cp_parser *);
1875 static tree cp_parser_objc_defs_expression
1876 (cp_parser *);
1877 static tree cp_parser_objc_protocol_expression
1878 (cp_parser *);
1879 static tree cp_parser_objc_selector_expression
1880 (cp_parser *);
1881 static tree cp_parser_objc_expression
1882 (cp_parser *);
1883 static bool cp_parser_objc_selector_p
1884 (enum cpp_ttype);
1885 static tree cp_parser_objc_selector
1886 (cp_parser *);
1887 static tree cp_parser_objc_protocol_refs_opt
1888 (cp_parser *);
1889 static void cp_parser_objc_declaration
1890 (cp_parser *);
1891 static tree cp_parser_objc_statement
1892 (cp_parser *);
1894 /* Utility Routines */
1896 static tree cp_parser_lookup_name
1897 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1898 static tree cp_parser_lookup_name_simple
1899 (cp_parser *, tree);
1900 static tree cp_parser_maybe_treat_template_as_class
1901 (tree, bool);
1902 static bool cp_parser_check_declarator_template_parameters
1903 (cp_parser *, cp_declarator *);
1904 static bool cp_parser_check_template_parameters
1905 (cp_parser *, unsigned);
1906 static tree cp_parser_simple_cast_expression
1907 (cp_parser *);
1908 static tree cp_parser_global_scope_opt
1909 (cp_parser *, bool);
1910 static bool cp_parser_constructor_declarator_p
1911 (cp_parser *, bool);
1912 static tree cp_parser_function_definition_from_specifiers_and_declarator
1913 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1914 static tree cp_parser_function_definition_after_declarator
1915 (cp_parser *, bool);
1916 static void cp_parser_template_declaration_after_export
1917 (cp_parser *, bool);
1918 static void cp_parser_perform_template_parameter_access_checks
1919 (VEC (deferred_access_check,gc)*);
1920 static tree cp_parser_single_declaration
1921 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1922 static tree cp_parser_functional_cast
1923 (cp_parser *, tree);
1924 static tree cp_parser_save_member_function_body
1925 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1926 static tree cp_parser_enclosed_template_argument_list
1927 (cp_parser *);
1928 static void cp_parser_save_default_args
1929 (cp_parser *, tree);
1930 static void cp_parser_late_parsing_for_member
1931 (cp_parser *, tree);
1932 static void cp_parser_late_parsing_default_args
1933 (cp_parser *, tree);
1934 static tree cp_parser_sizeof_operand
1935 (cp_parser *, enum rid);
1936 static tree cp_parser_trait_expr
1937 (cp_parser *, enum rid);
1938 static bool cp_parser_declares_only_class_p
1939 (cp_parser *);
1940 static void cp_parser_set_storage_class
1941 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1942 static void cp_parser_set_decl_spec_type
1943 (cp_decl_specifier_seq *, tree, bool);
1944 static bool cp_parser_friend_p
1945 (const cp_decl_specifier_seq *);
1946 static cp_token *cp_parser_require
1947 (cp_parser *, enum cpp_ttype, const char *);
1948 static cp_token *cp_parser_require_keyword
1949 (cp_parser *, enum rid, const char *);
1950 static bool cp_parser_token_starts_function_definition_p
1951 (cp_token *);
1952 static bool cp_parser_next_token_starts_class_definition_p
1953 (cp_parser *);
1954 static bool cp_parser_next_token_ends_template_argument_p
1955 (cp_parser *);
1956 static bool cp_parser_nth_token_starts_template_argument_list_p
1957 (cp_parser *, size_t);
1958 static enum tag_types cp_parser_token_is_class_key
1959 (cp_token *);
1960 static void cp_parser_check_class_key
1961 (enum tag_types, tree type);
1962 static void cp_parser_check_access_in_redeclaration
1963 (tree type);
1964 static bool cp_parser_optional_template_keyword
1965 (cp_parser *);
1966 static void cp_parser_pre_parsed_nested_name_specifier
1967 (cp_parser *);
1968 static void cp_parser_cache_group
1969 (cp_parser *, enum cpp_ttype, unsigned);
1970 static void cp_parser_parse_tentatively
1971 (cp_parser *);
1972 static void cp_parser_commit_to_tentative_parse
1973 (cp_parser *);
1974 static void cp_parser_abort_tentative_parse
1975 (cp_parser *);
1976 static bool cp_parser_parse_definitely
1977 (cp_parser *);
1978 static inline bool cp_parser_parsing_tentatively
1979 (cp_parser *);
1980 static bool cp_parser_uncommitted_to_tentative_parse_p
1981 (cp_parser *);
1982 static void cp_parser_error
1983 (cp_parser *, const char *);
1984 static void cp_parser_name_lookup_error
1985 (cp_parser *, tree, tree, const char *);
1986 static bool cp_parser_simulate_error
1987 (cp_parser *);
1988 static bool cp_parser_check_type_definition
1989 (cp_parser *);
1990 static void cp_parser_check_for_definition_in_return_type
1991 (cp_declarator *, tree);
1992 static void cp_parser_check_for_invalid_template_id
1993 (cp_parser *, tree);
1994 static bool cp_parser_non_integral_constant_expression
1995 (cp_parser *, const char *);
1996 static void cp_parser_diagnose_invalid_type_name
1997 (cp_parser *, tree, tree);
1998 static bool cp_parser_parse_and_diagnose_invalid_type_name
1999 (cp_parser *);
2000 static int cp_parser_skip_to_closing_parenthesis
2001 (cp_parser *, bool, bool, bool);
2002 static void cp_parser_skip_to_end_of_statement
2003 (cp_parser *);
2004 static void cp_parser_consume_semicolon_at_end_of_statement
2005 (cp_parser *);
2006 static void cp_parser_skip_to_end_of_block_or_statement
2007 (cp_parser *);
2008 static bool cp_parser_skip_to_closing_brace
2009 (cp_parser *);
2010 static void cp_parser_skip_to_end_of_template_parameter_list
2011 (cp_parser *);
2012 static void cp_parser_skip_to_pragma_eol
2013 (cp_parser*, cp_token *);
2014 static bool cp_parser_error_occurred
2015 (cp_parser *);
2016 static bool cp_parser_allow_gnu_extensions_p
2017 (cp_parser *);
2018 static bool cp_parser_is_string_literal
2019 (cp_token *);
2020 static bool cp_parser_is_keyword
2021 (cp_token *, enum rid);
2022 static tree cp_parser_make_typename_type
2023 (cp_parser *, tree, tree);
2024 static cp_declarator * cp_parser_make_indirect_declarator
2025 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2027 /* Returns nonzero if we are parsing tentatively. */
2029 static inline bool
2030 cp_parser_parsing_tentatively (cp_parser* parser)
2032 return parser->context->next != NULL;
2035 /* Returns nonzero if TOKEN is a string literal. */
2037 static bool
2038 cp_parser_is_string_literal (cp_token* token)
2040 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2043 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2045 static bool
2046 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2048 return token->keyword == keyword;
2051 /* If not parsing tentatively, issue a diagnostic of the form
2052 FILE:LINE: MESSAGE before TOKEN
2053 where TOKEN is the next token in the input stream. MESSAGE
2054 (specified by the caller) is usually of the form "expected
2055 OTHER-TOKEN". */
2057 static void
2058 cp_parser_error (cp_parser* parser, const char* message)
2060 if (!cp_parser_simulate_error (parser))
2062 cp_token *token = cp_lexer_peek_token (parser->lexer);
2063 /* This diagnostic makes more sense if it is tagged to the line
2064 of the token we just peeked at. */
2065 cp_lexer_set_source_position_from_token (token);
2067 if (token->type == CPP_PRAGMA)
2069 error ("%<#pragma%> is not allowed here");
2070 cp_parser_skip_to_pragma_eol (parser, token);
2071 return;
2074 c_parse_error (message,
2075 /* Because c_parser_error does not understand
2076 CPP_KEYWORD, keywords are treated like
2077 identifiers. */
2078 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2079 token->u.value);
2083 /* Issue an error about name-lookup failing. NAME is the
2084 IDENTIFIER_NODE DECL is the result of
2085 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2086 the thing that we hoped to find. */
2088 static void
2089 cp_parser_name_lookup_error (cp_parser* parser,
2090 tree name,
2091 tree decl,
2092 const char* desired)
2094 /* If name lookup completely failed, tell the user that NAME was not
2095 declared. */
2096 if (decl == error_mark_node)
2098 if (parser->scope && parser->scope != global_namespace)
2099 error ("%<%E::%E%> has not been declared",
2100 parser->scope, name);
2101 else if (parser->scope == global_namespace)
2102 error ("%<::%E%> has not been declared", name);
2103 else if (parser->object_scope
2104 && !CLASS_TYPE_P (parser->object_scope))
2105 error ("request for member %qE in non-class type %qT",
2106 name, parser->object_scope);
2107 else if (parser->object_scope)
2108 error ("%<%T::%E%> has not been declared",
2109 parser->object_scope, name);
2110 else
2111 error ("%qE has not been declared", name);
2113 else if (parser->scope && parser->scope != global_namespace)
2114 error ("%<%E::%E%> %s", parser->scope, name, desired);
2115 else if (parser->scope == global_namespace)
2116 error ("%<::%E%> %s", name, desired);
2117 else
2118 error ("%qE %s", name, desired);
2121 /* If we are parsing tentatively, remember that an error has occurred
2122 during this tentative parse. Returns true if the error was
2123 simulated; false if a message should be issued by the caller. */
2125 static bool
2126 cp_parser_simulate_error (cp_parser* parser)
2128 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2130 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2131 return true;
2133 return false;
2136 /* Check for repeated decl-specifiers. */
2138 static void
2139 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2141 cp_decl_spec ds;
2143 for (ds = ds_first; ds != ds_last; ++ds)
2145 unsigned count = decl_specs->specs[(int)ds];
2146 if (count < 2)
2147 continue;
2148 /* The "long" specifier is a special case because of "long long". */
2149 if (ds == ds_long)
2151 if (count > 2)
2152 error ("%<long long long%> is too long for GCC");
2153 else if (pedantic && !in_system_header && warn_long_long
2154 && cxx_dialect == cxx98)
2155 pedwarn ("ISO C++ 1998 does not support %<long long%>");
2157 else if (count > 1)
2159 static const char *const decl_spec_names[] = {
2160 "signed",
2161 "unsigned",
2162 "short",
2163 "long",
2164 "const",
2165 "volatile",
2166 "restrict",
2167 "inline",
2168 "virtual",
2169 "explicit",
2170 "friend",
2171 "typedef",
2172 "__complex",
2173 "__thread"
2175 error ("duplicate %qs", decl_spec_names[(int)ds]);
2180 /* This function is called when a type is defined. If type
2181 definitions are forbidden at this point, an error message is
2182 issued. */
2184 static bool
2185 cp_parser_check_type_definition (cp_parser* parser)
2187 /* If types are forbidden here, issue a message. */
2188 if (parser->type_definition_forbidden_message)
2190 /* Use `%s' to print the string in case there are any escape
2191 characters in the message. */
2192 error ("%s", parser->type_definition_forbidden_message);
2193 return false;
2195 return true;
2198 /* This function is called when the DECLARATOR is processed. The TYPE
2199 was a type defined in the decl-specifiers. If it is invalid to
2200 define a type in the decl-specifiers for DECLARATOR, an error is
2201 issued. */
2203 static void
2204 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2205 tree type)
2207 /* [dcl.fct] forbids type definitions in return types.
2208 Unfortunately, it's not easy to know whether or not we are
2209 processing a return type until after the fact. */
2210 while (declarator
2211 && (declarator->kind == cdk_pointer
2212 || declarator->kind == cdk_reference
2213 || declarator->kind == cdk_ptrmem))
2214 declarator = declarator->declarator;
2215 if (declarator
2216 && declarator->kind == cdk_function)
2218 error ("new types may not be defined in a return type");
2219 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2220 type);
2224 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2225 "<" in any valid C++ program. If the next token is indeed "<",
2226 issue a message warning the user about what appears to be an
2227 invalid attempt to form a template-id. */
2229 static void
2230 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2231 tree type)
2233 cp_token_position start = 0;
2235 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2237 if (TYPE_P (type))
2238 error ("%qT is not a template", type);
2239 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2240 error ("%qE is not a template", type);
2241 else
2242 error ("invalid template-id");
2243 /* Remember the location of the invalid "<". */
2244 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2245 start = cp_lexer_token_position (parser->lexer, true);
2246 /* Consume the "<". */
2247 cp_lexer_consume_token (parser->lexer);
2248 /* Parse the template arguments. */
2249 cp_parser_enclosed_template_argument_list (parser);
2250 /* Permanently remove the invalid template arguments so that
2251 this error message is not issued again. */
2252 if (start)
2253 cp_lexer_purge_tokens_after (parser->lexer, start);
2257 /* If parsing an integral constant-expression, issue an error message
2258 about the fact that THING appeared and return true. Otherwise,
2259 return false. In either case, set
2260 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2262 static bool
2263 cp_parser_non_integral_constant_expression (cp_parser *parser,
2264 const char *thing)
2266 parser->non_integral_constant_expression_p = true;
2267 if (parser->integral_constant_expression_p)
2269 if (!parser->allow_non_integral_constant_expression_p)
2271 error ("%s cannot appear in a constant-expression", thing);
2272 return true;
2275 return false;
2278 /* Emit a diagnostic for an invalid type name. SCOPE is the
2279 qualifying scope (or NULL, if none) for ID. This function commits
2280 to the current active tentative parse, if any. (Otherwise, the
2281 problematic construct might be encountered again later, resulting
2282 in duplicate error messages.) */
2284 static void
2285 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2287 tree decl, old_scope;
2288 /* Try to lookup the identifier. */
2289 old_scope = parser->scope;
2290 parser->scope = scope;
2291 decl = cp_parser_lookup_name_simple (parser, id);
2292 parser->scope = old_scope;
2293 /* If the lookup found a template-name, it means that the user forgot
2294 to specify an argument list. Emit a useful error message. */
2295 if (TREE_CODE (decl) == TEMPLATE_DECL)
2296 error ("invalid use of template-name %qE without an argument list", decl);
2297 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2298 error ("invalid use of destructor %qD as a type", id);
2299 else if (TREE_CODE (decl) == TYPE_DECL)
2300 /* Something like 'unsigned A a;' */
2301 error ("invalid combination of multiple type-specifiers");
2302 else if (!parser->scope)
2304 /* Issue an error message. */
2305 error ("%qE does not name a type", id);
2306 /* If we're in a template class, it's possible that the user was
2307 referring to a type from a base class. For example:
2309 template <typename T> struct A { typedef T X; };
2310 template <typename T> struct B : public A<T> { X x; };
2312 The user should have said "typename A<T>::X". */
2313 if (processing_template_decl && current_class_type
2314 && TYPE_BINFO (current_class_type))
2316 tree b;
2318 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2320 b = TREE_CHAIN (b))
2322 tree base_type = BINFO_TYPE (b);
2323 if (CLASS_TYPE_P (base_type)
2324 && dependent_type_p (base_type))
2326 tree field;
2327 /* Go from a particular instantiation of the
2328 template (which will have an empty TYPE_FIELDs),
2329 to the main version. */
2330 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2331 for (field = TYPE_FIELDS (base_type);
2332 field;
2333 field = TREE_CHAIN (field))
2334 if (TREE_CODE (field) == TYPE_DECL
2335 && DECL_NAME (field) == id)
2337 inform ("(perhaps %<typename %T::%E%> was intended)",
2338 BINFO_TYPE (b), id);
2339 break;
2341 if (field)
2342 break;
2347 /* Here we diagnose qualified-ids where the scope is actually correct,
2348 but the identifier does not resolve to a valid type name. */
2349 else if (parser->scope != error_mark_node)
2351 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2352 error ("%qE in namespace %qE does not name a type",
2353 id, parser->scope);
2354 else if (TYPE_P (parser->scope))
2355 error ("%qE in class %qT does not name a type", id, parser->scope);
2356 else
2357 gcc_unreachable ();
2359 cp_parser_commit_to_tentative_parse (parser);
2362 /* Check for a common situation where a type-name should be present,
2363 but is not, and issue a sensible error message. Returns true if an
2364 invalid type-name was detected.
2366 The situation handled by this function are variable declarations of the
2367 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2368 Usually, `ID' should name a type, but if we got here it means that it
2369 does not. We try to emit the best possible error message depending on
2370 how exactly the id-expression looks like. */
2372 static bool
2373 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2375 tree id;
2377 cp_parser_parse_tentatively (parser);
2378 id = cp_parser_id_expression (parser,
2379 /*template_keyword_p=*/false,
2380 /*check_dependency_p=*/true,
2381 /*template_p=*/NULL,
2382 /*declarator_p=*/true,
2383 /*optional_p=*/false);
2384 /* After the id-expression, there should be a plain identifier,
2385 otherwise this is not a simple variable declaration. Also, if
2386 the scope is dependent, we cannot do much. */
2387 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2388 || (parser->scope && TYPE_P (parser->scope)
2389 && dependent_type_p (parser->scope))
2390 || TREE_CODE (id) == TYPE_DECL)
2392 cp_parser_abort_tentative_parse (parser);
2393 return false;
2395 if (!cp_parser_parse_definitely (parser))
2396 return false;
2398 /* Emit a diagnostic for the invalid type. */
2399 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2400 /* Skip to the end of the declaration; there's no point in
2401 trying to process it. */
2402 cp_parser_skip_to_end_of_block_or_statement (parser);
2403 return true;
2406 /* Consume tokens up to, and including, the next non-nested closing `)'.
2407 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2408 are doing error recovery. Returns -1 if OR_COMMA is true and we
2409 found an unnested comma. */
2411 static int
2412 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2413 bool recovering,
2414 bool or_comma,
2415 bool consume_paren)
2417 unsigned paren_depth = 0;
2418 unsigned brace_depth = 0;
2420 if (recovering && !or_comma
2421 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2422 return 0;
2424 while (true)
2426 cp_token * token = cp_lexer_peek_token (parser->lexer);
2428 switch (token->type)
2430 case CPP_EOF:
2431 case CPP_PRAGMA_EOL:
2432 /* If we've run out of tokens, then there is no closing `)'. */
2433 return 0;
2435 case CPP_SEMICOLON:
2436 /* This matches the processing in skip_to_end_of_statement. */
2437 if (!brace_depth)
2438 return 0;
2439 break;
2441 case CPP_OPEN_BRACE:
2442 ++brace_depth;
2443 break;
2444 case CPP_CLOSE_BRACE:
2445 if (!brace_depth--)
2446 return 0;
2447 break;
2449 case CPP_COMMA:
2450 if (recovering && or_comma && !brace_depth && !paren_depth)
2451 return -1;
2452 break;
2454 case CPP_OPEN_PAREN:
2455 if (!brace_depth)
2456 ++paren_depth;
2457 break;
2459 case CPP_CLOSE_PAREN:
2460 if (!brace_depth && !paren_depth--)
2462 if (consume_paren)
2463 cp_lexer_consume_token (parser->lexer);
2464 return 1;
2466 break;
2468 default:
2469 break;
2472 /* Consume the token. */
2473 cp_lexer_consume_token (parser->lexer);
2477 /* Consume tokens until we reach the end of the current statement.
2478 Normally, that will be just before consuming a `;'. However, if a
2479 non-nested `}' comes first, then we stop before consuming that. */
2481 static void
2482 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2484 unsigned nesting_depth = 0;
2486 while (true)
2488 cp_token *token = cp_lexer_peek_token (parser->lexer);
2490 switch (token->type)
2492 case CPP_EOF:
2493 case CPP_PRAGMA_EOL:
2494 /* If we've run out of tokens, stop. */
2495 return;
2497 case CPP_SEMICOLON:
2498 /* If the next token is a `;', we have reached the end of the
2499 statement. */
2500 if (!nesting_depth)
2501 return;
2502 break;
2504 case CPP_CLOSE_BRACE:
2505 /* If this is a non-nested '}', stop before consuming it.
2506 That way, when confronted with something like:
2508 { 3 + }
2510 we stop before consuming the closing '}', even though we
2511 have not yet reached a `;'. */
2512 if (nesting_depth == 0)
2513 return;
2515 /* If it is the closing '}' for a block that we have
2516 scanned, stop -- but only after consuming the token.
2517 That way given:
2519 void f g () { ... }
2520 typedef int I;
2522 we will stop after the body of the erroneously declared
2523 function, but before consuming the following `typedef'
2524 declaration. */
2525 if (--nesting_depth == 0)
2527 cp_lexer_consume_token (parser->lexer);
2528 return;
2531 case CPP_OPEN_BRACE:
2532 ++nesting_depth;
2533 break;
2535 default:
2536 break;
2539 /* Consume the token. */
2540 cp_lexer_consume_token (parser->lexer);
2544 /* This function is called at the end of a statement or declaration.
2545 If the next token is a semicolon, it is consumed; otherwise, error
2546 recovery is attempted. */
2548 static void
2549 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2551 /* Look for the trailing `;'. */
2552 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2554 /* If there is additional (erroneous) input, skip to the end of
2555 the statement. */
2556 cp_parser_skip_to_end_of_statement (parser);
2557 /* If the next token is now a `;', consume it. */
2558 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2559 cp_lexer_consume_token (parser->lexer);
2563 /* Skip tokens until we have consumed an entire block, or until we
2564 have consumed a non-nested `;'. */
2566 static void
2567 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2569 int nesting_depth = 0;
2571 while (nesting_depth >= 0)
2573 cp_token *token = cp_lexer_peek_token (parser->lexer);
2575 switch (token->type)
2577 case CPP_EOF:
2578 case CPP_PRAGMA_EOL:
2579 /* If we've run out of tokens, stop. */
2580 return;
2582 case CPP_SEMICOLON:
2583 /* Stop if this is an unnested ';'. */
2584 if (!nesting_depth)
2585 nesting_depth = -1;
2586 break;
2588 case CPP_CLOSE_BRACE:
2589 /* Stop if this is an unnested '}', or closes the outermost
2590 nesting level. */
2591 nesting_depth--;
2592 if (!nesting_depth)
2593 nesting_depth = -1;
2594 break;
2596 case CPP_OPEN_BRACE:
2597 /* Nest. */
2598 nesting_depth++;
2599 break;
2601 default:
2602 break;
2605 /* Consume the token. */
2606 cp_lexer_consume_token (parser->lexer);
2610 /* Skip tokens until a non-nested closing curly brace is the next
2611 token, or there are no more tokens. Return true in the first case,
2612 false otherwise. */
2614 static bool
2615 cp_parser_skip_to_closing_brace (cp_parser *parser)
2617 unsigned nesting_depth = 0;
2619 while (true)
2621 cp_token *token = cp_lexer_peek_token (parser->lexer);
2623 switch (token->type)
2625 case CPP_EOF:
2626 case CPP_PRAGMA_EOL:
2627 /* If we've run out of tokens, stop. */
2628 return false;
2630 case CPP_CLOSE_BRACE:
2631 /* If the next token is a non-nested `}', then we have reached
2632 the end of the current block. */
2633 if (nesting_depth-- == 0)
2634 return true;
2635 break;
2637 case CPP_OPEN_BRACE:
2638 /* If it the next token is a `{', then we are entering a new
2639 block. Consume the entire block. */
2640 ++nesting_depth;
2641 break;
2643 default:
2644 break;
2647 /* Consume the token. */
2648 cp_lexer_consume_token (parser->lexer);
2652 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2653 parameter is the PRAGMA token, allowing us to purge the entire pragma
2654 sequence. */
2656 static void
2657 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2659 cp_token *token;
2661 parser->lexer->in_pragma = false;
2664 token = cp_lexer_consume_token (parser->lexer);
2665 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2667 /* Ensure that the pragma is not parsed again. */
2668 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2671 /* Require pragma end of line, resyncing with it as necessary. The
2672 arguments are as for cp_parser_skip_to_pragma_eol. */
2674 static void
2675 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2677 parser->lexer->in_pragma = false;
2678 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2679 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2682 /* This is a simple wrapper around make_typename_type. When the id is
2683 an unresolved identifier node, we can provide a superior diagnostic
2684 using cp_parser_diagnose_invalid_type_name. */
2686 static tree
2687 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2689 tree result;
2690 if (TREE_CODE (id) == IDENTIFIER_NODE)
2692 result = make_typename_type (scope, id, typename_type,
2693 /*complain=*/tf_none);
2694 if (result == error_mark_node)
2695 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2696 return result;
2698 return make_typename_type (scope, id, typename_type, tf_error);
2701 /* This is a wrapper around the
2702 make_{pointer,ptrmem,reference}_declarator functions that decides
2703 which one to call based on the CODE and CLASS_TYPE arguments. The
2704 CODE argument should be one of the values returned by
2705 cp_parser_ptr_operator. */
2706 static cp_declarator *
2707 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2708 cp_cv_quals cv_qualifiers,
2709 cp_declarator *target)
2711 if (code == ERROR_MARK)
2712 return cp_error_declarator;
2714 if (code == INDIRECT_REF)
2715 if (class_type == NULL_TREE)
2716 return make_pointer_declarator (cv_qualifiers, target);
2717 else
2718 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2719 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2720 return make_reference_declarator (cv_qualifiers, target, false);
2721 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2722 return make_reference_declarator (cv_qualifiers, target, true);
2723 gcc_unreachable ();
2726 /* Create a new C++ parser. */
2728 static cp_parser *
2729 cp_parser_new (void)
2731 cp_parser *parser;
2732 cp_lexer *lexer;
2733 unsigned i;
2735 /* cp_lexer_new_main is called before calling ggc_alloc because
2736 cp_lexer_new_main might load a PCH file. */
2737 lexer = cp_lexer_new_main ();
2739 /* Initialize the binops_by_token so that we can get the tree
2740 directly from the token. */
2741 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2742 binops_by_token[binops[i].token_type] = binops[i];
2744 parser = GGC_CNEW (cp_parser);
2745 parser->lexer = lexer;
2746 parser->context = cp_parser_context_new (NULL);
2748 /* For now, we always accept GNU extensions. */
2749 parser->allow_gnu_extensions_p = 1;
2751 /* The `>' token is a greater-than operator, not the end of a
2752 template-id. */
2753 parser->greater_than_is_operator_p = true;
2755 parser->default_arg_ok_p = true;
2757 /* We are not parsing a constant-expression. */
2758 parser->integral_constant_expression_p = false;
2759 parser->allow_non_integral_constant_expression_p = false;
2760 parser->non_integral_constant_expression_p = false;
2762 /* Local variable names are not forbidden. */
2763 parser->local_variables_forbidden_p = false;
2765 /* We are not processing an `extern "C"' declaration. */
2766 parser->in_unbraced_linkage_specification_p = false;
2768 /* We are not processing a declarator. */
2769 parser->in_declarator_p = false;
2771 /* We are not processing a template-argument-list. */
2772 parser->in_template_argument_list_p = false;
2774 /* We are not in an iteration statement. */
2775 parser->in_statement = 0;
2777 /* We are not in a switch statement. */
2778 parser->in_switch_statement_p = false;
2780 /* We are not parsing a type-id inside an expression. */
2781 parser->in_type_id_in_expr_p = false;
2783 /* Declarations aren't implicitly extern "C". */
2784 parser->implicit_extern_c = false;
2786 /* String literals should be translated to the execution character set. */
2787 parser->translate_strings_p = true;
2789 /* We are not parsing a function body. */
2790 parser->in_function_body = false;
2792 /* The unparsed function queue is empty. */
2793 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2795 /* There are no classes being defined. */
2796 parser->num_classes_being_defined = 0;
2798 /* No template parameters apply. */
2799 parser->num_template_parameter_lists = 0;
2801 return parser;
2804 /* Create a cp_lexer structure which will emit the tokens in CACHE
2805 and push it onto the parser's lexer stack. This is used for delayed
2806 parsing of in-class method bodies and default arguments, and should
2807 not be confused with tentative parsing. */
2808 static void
2809 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2811 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2812 lexer->next = parser->lexer;
2813 parser->lexer = lexer;
2815 /* Move the current source position to that of the first token in the
2816 new lexer. */
2817 cp_lexer_set_source_position_from_token (lexer->next_token);
2820 /* Pop the top lexer off the parser stack. This is never used for the
2821 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2822 static void
2823 cp_parser_pop_lexer (cp_parser *parser)
2825 cp_lexer *lexer = parser->lexer;
2826 parser->lexer = lexer->next;
2827 cp_lexer_destroy (lexer);
2829 /* Put the current source position back where it was before this
2830 lexer was pushed. */
2831 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2834 /* Lexical conventions [gram.lex] */
2836 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2837 identifier. */
2839 static tree
2840 cp_parser_identifier (cp_parser* parser)
2842 cp_token *token;
2844 /* Look for the identifier. */
2845 token = cp_parser_require (parser, CPP_NAME, "identifier");
2846 /* Return the value. */
2847 return token ? token->u.value : error_mark_node;
2850 /* Parse a sequence of adjacent string constants. Returns a
2851 TREE_STRING representing the combined, nul-terminated string
2852 constant. If TRANSLATE is true, translate the string to the
2853 execution character set. If WIDE_OK is true, a wide string is
2854 invalid here.
2856 C++98 [lex.string] says that if a narrow string literal token is
2857 adjacent to a wide string literal token, the behavior is undefined.
2858 However, C99 6.4.5p4 says that this results in a wide string literal.
2859 We follow C99 here, for consistency with the C front end.
2861 This code is largely lifted from lex_string() in c-lex.c.
2863 FUTURE: ObjC++ will need to handle @-strings here. */
2864 static tree
2865 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2867 tree value;
2868 bool wide = false;
2869 size_t count;
2870 struct obstack str_ob;
2871 cpp_string str, istr, *strs;
2872 cp_token *tok;
2874 tok = cp_lexer_peek_token (parser->lexer);
2875 if (!cp_parser_is_string_literal (tok))
2877 cp_parser_error (parser, "expected string-literal");
2878 return error_mark_node;
2881 /* Try to avoid the overhead of creating and destroying an obstack
2882 for the common case of just one string. */
2883 if (!cp_parser_is_string_literal
2884 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2886 cp_lexer_consume_token (parser->lexer);
2888 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2889 str.len = TREE_STRING_LENGTH (tok->u.value);
2890 count = 1;
2891 if (tok->type == CPP_WSTRING)
2892 wide = true;
2894 strs = &str;
2896 else
2898 gcc_obstack_init (&str_ob);
2899 count = 0;
2903 cp_lexer_consume_token (parser->lexer);
2904 count++;
2905 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2906 str.len = TREE_STRING_LENGTH (tok->u.value);
2907 if (tok->type == CPP_WSTRING)
2908 wide = true;
2910 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2912 tok = cp_lexer_peek_token (parser->lexer);
2914 while (cp_parser_is_string_literal (tok));
2916 strs = (cpp_string *) obstack_finish (&str_ob);
2919 if (wide && !wide_ok)
2921 cp_parser_error (parser, "a wide string is invalid in this context");
2922 wide = false;
2925 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2926 (parse_in, strs, count, &istr, wide))
2928 value = build_string (istr.len, (const char *)istr.text);
2929 free (CONST_CAST (unsigned char *, istr.text));
2931 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2932 value = fix_string_type (value);
2934 else
2935 /* cpp_interpret_string has issued an error. */
2936 value = error_mark_node;
2938 if (count > 1)
2939 obstack_free (&str_ob, 0);
2941 return value;
2945 /* Basic concepts [gram.basic] */
2947 /* Parse a translation-unit.
2949 translation-unit:
2950 declaration-seq [opt]
2952 Returns TRUE if all went well. */
2954 static bool
2955 cp_parser_translation_unit (cp_parser* parser)
2957 /* The address of the first non-permanent object on the declarator
2958 obstack. */
2959 static void *declarator_obstack_base;
2961 bool success;
2963 /* Create the declarator obstack, if necessary. */
2964 if (!cp_error_declarator)
2966 gcc_obstack_init (&declarator_obstack);
2967 /* Create the error declarator. */
2968 cp_error_declarator = make_declarator (cdk_error);
2969 /* Create the empty parameter list. */
2970 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2971 /* Remember where the base of the declarator obstack lies. */
2972 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2975 cp_parser_declaration_seq_opt (parser);
2977 /* If there are no tokens left then all went well. */
2978 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2980 /* Get rid of the token array; we don't need it any more. */
2981 cp_lexer_destroy (parser->lexer);
2982 parser->lexer = NULL;
2984 /* This file might have been a context that's implicitly extern
2985 "C". If so, pop the lang context. (Only relevant for PCH.) */
2986 if (parser->implicit_extern_c)
2988 pop_lang_context ();
2989 parser->implicit_extern_c = false;
2992 /* Finish up. */
2993 finish_translation_unit ();
2995 success = true;
2997 else
2999 cp_parser_error (parser, "expected declaration");
3000 success = false;
3003 /* Make sure the declarator obstack was fully cleaned up. */
3004 gcc_assert (obstack_next_free (&declarator_obstack)
3005 == declarator_obstack_base);
3007 /* All went well. */
3008 return success;
3011 /* Expressions [gram.expr] */
3013 /* Parse a primary-expression.
3015 primary-expression:
3016 literal
3017 this
3018 ( expression )
3019 id-expression
3021 GNU Extensions:
3023 primary-expression:
3024 ( compound-statement )
3025 __builtin_va_arg ( assignment-expression , type-id )
3026 __builtin_offsetof ( type-id , offsetof-expression )
3028 C++ Extensions:
3029 __has_nothrow_assign ( type-id )
3030 __has_nothrow_constructor ( type-id )
3031 __has_nothrow_copy ( type-id )
3032 __has_trivial_assign ( type-id )
3033 __has_trivial_constructor ( type-id )
3034 __has_trivial_copy ( type-id )
3035 __has_trivial_destructor ( type-id )
3036 __has_virtual_destructor ( type-id )
3037 __is_abstract ( type-id )
3038 __is_base_of ( type-id , type-id )
3039 __is_class ( type-id )
3040 __is_convertible_to ( type-id , type-id )
3041 __is_empty ( type-id )
3042 __is_enum ( type-id )
3043 __is_pod ( type-id )
3044 __is_polymorphic ( type-id )
3045 __is_union ( type-id )
3047 Objective-C++ Extension:
3049 primary-expression:
3050 objc-expression
3052 literal:
3053 __null
3055 ADDRESS_P is true iff this expression was immediately preceded by
3056 "&" and therefore might denote a pointer-to-member. CAST_P is true
3057 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3058 true iff this expression is a template argument.
3060 Returns a representation of the expression. Upon return, *IDK
3061 indicates what kind of id-expression (if any) was present. */
3063 static tree
3064 cp_parser_primary_expression (cp_parser *parser,
3065 bool address_p,
3066 bool cast_p,
3067 bool template_arg_p,
3068 cp_id_kind *idk)
3070 cp_token *token;
3072 /* Assume the primary expression is not an id-expression. */
3073 *idk = CP_ID_KIND_NONE;
3075 /* Peek at the next token. */
3076 token = cp_lexer_peek_token (parser->lexer);
3077 switch (token->type)
3079 /* literal:
3080 integer-literal
3081 character-literal
3082 floating-literal
3083 string-literal
3084 boolean-literal */
3085 case CPP_CHAR:
3086 case CPP_WCHAR:
3087 case CPP_NUMBER:
3088 token = cp_lexer_consume_token (parser->lexer);
3089 /* Floating-point literals are only allowed in an integral
3090 constant expression if they are cast to an integral or
3091 enumeration type. */
3092 if (TREE_CODE (token->u.value) == REAL_CST
3093 && parser->integral_constant_expression_p
3094 && pedantic)
3096 /* CAST_P will be set even in invalid code like "int(2.7 +
3097 ...)". Therefore, we have to check that the next token
3098 is sure to end the cast. */
3099 if (cast_p)
3101 cp_token *next_token;
3103 next_token = cp_lexer_peek_token (parser->lexer);
3104 if (/* The comma at the end of an
3105 enumerator-definition. */
3106 next_token->type != CPP_COMMA
3107 /* The curly brace at the end of an enum-specifier. */
3108 && next_token->type != CPP_CLOSE_BRACE
3109 /* The end of a statement. */
3110 && next_token->type != CPP_SEMICOLON
3111 /* The end of the cast-expression. */
3112 && next_token->type != CPP_CLOSE_PAREN
3113 /* The end of an array bound. */
3114 && next_token->type != CPP_CLOSE_SQUARE
3115 /* The closing ">" in a template-argument-list. */
3116 && (next_token->type != CPP_GREATER
3117 || parser->greater_than_is_operator_p)
3118 /* C++0x only: A ">>" treated like two ">" tokens,
3119 in a template-argument-list. */
3120 && (next_token->type != CPP_RSHIFT
3121 || (cxx_dialect == cxx98)
3122 || parser->greater_than_is_operator_p))
3123 cast_p = false;
3126 /* If we are within a cast, then the constraint that the
3127 cast is to an integral or enumeration type will be
3128 checked at that point. If we are not within a cast, then
3129 this code is invalid. */
3130 if (!cast_p)
3131 cp_parser_non_integral_constant_expression
3132 (parser, "floating-point literal");
3134 return token->u.value;
3136 case CPP_STRING:
3137 case CPP_WSTRING:
3138 /* ??? Should wide strings be allowed when parser->translate_strings_p
3139 is false (i.e. in attributes)? If not, we can kill the third
3140 argument to cp_parser_string_literal. */
3141 return cp_parser_string_literal (parser,
3142 parser->translate_strings_p,
3143 true);
3145 case CPP_OPEN_PAREN:
3147 tree expr;
3148 bool saved_greater_than_is_operator_p;
3150 /* Consume the `('. */
3151 cp_lexer_consume_token (parser->lexer);
3152 /* Within a parenthesized expression, a `>' token is always
3153 the greater-than operator. */
3154 saved_greater_than_is_operator_p
3155 = parser->greater_than_is_operator_p;
3156 parser->greater_than_is_operator_p = true;
3157 /* If we see `( { ' then we are looking at the beginning of
3158 a GNU statement-expression. */
3159 if (cp_parser_allow_gnu_extensions_p (parser)
3160 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3162 /* Statement-expressions are not allowed by the standard. */
3163 if (pedantic)
3164 pedwarn ("ISO C++ forbids braced-groups within expressions");
3166 /* And they're not allowed outside of a function-body; you
3167 cannot, for example, write:
3169 int i = ({ int j = 3; j + 1; });
3171 at class or namespace scope. */
3172 if (!parser->in_function_body
3173 || parser->in_template_argument_list_p)
3175 error ("statement-expressions are not allowed outside "
3176 "functions nor in template-argument lists");
3177 cp_parser_skip_to_end_of_block_or_statement (parser);
3178 expr = error_mark_node;
3180 else
3182 /* Start the statement-expression. */
3183 expr = begin_stmt_expr ();
3184 /* Parse the compound-statement. */
3185 cp_parser_compound_statement (parser, expr, false);
3186 /* Finish up. */
3187 expr = finish_stmt_expr (expr, false);
3190 else
3192 /* Parse the parenthesized expression. */
3193 expr = cp_parser_expression (parser, cast_p);
3194 /* Let the front end know that this expression was
3195 enclosed in parentheses. This matters in case, for
3196 example, the expression is of the form `A::B', since
3197 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3198 not. */
3199 finish_parenthesized_expr (expr);
3201 /* The `>' token might be the end of a template-id or
3202 template-parameter-list now. */
3203 parser->greater_than_is_operator_p
3204 = saved_greater_than_is_operator_p;
3205 /* Consume the `)'. */
3206 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3207 cp_parser_skip_to_end_of_statement (parser);
3209 return expr;
3212 case CPP_KEYWORD:
3213 switch (token->keyword)
3215 /* These two are the boolean literals. */
3216 case RID_TRUE:
3217 cp_lexer_consume_token (parser->lexer);
3218 return boolean_true_node;
3219 case RID_FALSE:
3220 cp_lexer_consume_token (parser->lexer);
3221 return boolean_false_node;
3223 /* The `__null' literal. */
3224 case RID_NULL:
3225 cp_lexer_consume_token (parser->lexer);
3226 return null_node;
3228 /* Recognize the `this' keyword. */
3229 case RID_THIS:
3230 cp_lexer_consume_token (parser->lexer);
3231 if (parser->local_variables_forbidden_p)
3233 error ("%<this%> may not be used in this context");
3234 return error_mark_node;
3236 /* Pointers cannot appear in constant-expressions. */
3237 if (cp_parser_non_integral_constant_expression (parser,
3238 "`this'"))
3239 return error_mark_node;
3240 return finish_this_expr ();
3242 /* The `operator' keyword can be the beginning of an
3243 id-expression. */
3244 case RID_OPERATOR:
3245 goto id_expression;
3247 case RID_FUNCTION_NAME:
3248 case RID_PRETTY_FUNCTION_NAME:
3249 case RID_C99_FUNCTION_NAME:
3250 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3251 __func__ are the names of variables -- but they are
3252 treated specially. Therefore, they are handled here,
3253 rather than relying on the generic id-expression logic
3254 below. Grammatically, these names are id-expressions.
3256 Consume the token. */
3257 token = cp_lexer_consume_token (parser->lexer);
3258 /* Look up the name. */
3259 return finish_fname (token->u.value);
3261 case RID_VA_ARG:
3263 tree expression;
3264 tree type;
3266 /* The `__builtin_va_arg' construct is used to handle
3267 `va_arg'. Consume the `__builtin_va_arg' token. */
3268 cp_lexer_consume_token (parser->lexer);
3269 /* Look for the opening `('. */
3270 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3271 /* Now, parse the assignment-expression. */
3272 expression = cp_parser_assignment_expression (parser,
3273 /*cast_p=*/false);
3274 /* Look for the `,'. */
3275 cp_parser_require (parser, CPP_COMMA, "`,'");
3276 /* Parse the type-id. */
3277 type = cp_parser_type_id (parser);
3278 /* Look for the closing `)'. */
3279 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3280 /* Using `va_arg' in a constant-expression is not
3281 allowed. */
3282 if (cp_parser_non_integral_constant_expression (parser,
3283 "`va_arg'"))
3284 return error_mark_node;
3285 return build_x_va_arg (expression, type);
3288 case RID_OFFSETOF:
3289 return cp_parser_builtin_offsetof (parser);
3291 case RID_HAS_NOTHROW_ASSIGN:
3292 case RID_HAS_NOTHROW_CONSTRUCTOR:
3293 case RID_HAS_NOTHROW_COPY:
3294 case RID_HAS_TRIVIAL_ASSIGN:
3295 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3296 case RID_HAS_TRIVIAL_COPY:
3297 case RID_HAS_TRIVIAL_DESTRUCTOR:
3298 case RID_HAS_VIRTUAL_DESTRUCTOR:
3299 case RID_IS_ABSTRACT:
3300 case RID_IS_BASE_OF:
3301 case RID_IS_CLASS:
3302 case RID_IS_CONVERTIBLE_TO:
3303 case RID_IS_EMPTY:
3304 case RID_IS_ENUM:
3305 case RID_IS_POD:
3306 case RID_IS_POLYMORPHIC:
3307 case RID_IS_UNION:
3308 return cp_parser_trait_expr (parser, token->keyword);
3310 /* Objective-C++ expressions. */
3311 case RID_AT_ENCODE:
3312 case RID_AT_PROTOCOL:
3313 case RID_AT_SELECTOR:
3314 return cp_parser_objc_expression (parser);
3316 default:
3317 cp_parser_error (parser, "expected primary-expression");
3318 return error_mark_node;
3321 /* An id-expression can start with either an identifier, a
3322 `::' as the beginning of a qualified-id, or the "operator"
3323 keyword. */
3324 case CPP_NAME:
3325 case CPP_SCOPE:
3326 case CPP_TEMPLATE_ID:
3327 case CPP_NESTED_NAME_SPECIFIER:
3329 tree id_expression;
3330 tree decl;
3331 const char *error_msg;
3332 bool template_p;
3333 bool done;
3335 id_expression:
3336 /* Parse the id-expression. */
3337 id_expression
3338 = cp_parser_id_expression (parser,
3339 /*template_keyword_p=*/false,
3340 /*check_dependency_p=*/true,
3341 &template_p,
3342 /*declarator_p=*/false,
3343 /*optional_p=*/false);
3344 if (id_expression == error_mark_node)
3345 return error_mark_node;
3346 token = cp_lexer_peek_token (parser->lexer);
3347 done = (token->type != CPP_OPEN_SQUARE
3348 && token->type != CPP_OPEN_PAREN
3349 && token->type != CPP_DOT
3350 && token->type != CPP_DEREF
3351 && token->type != CPP_PLUS_PLUS
3352 && token->type != CPP_MINUS_MINUS);
3353 /* If we have a template-id, then no further lookup is
3354 required. If the template-id was for a template-class, we
3355 will sometimes have a TYPE_DECL at this point. */
3356 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3357 || TREE_CODE (id_expression) == TYPE_DECL)
3358 decl = id_expression;
3359 /* Look up the name. */
3360 else
3362 tree ambiguous_decls;
3364 decl = cp_parser_lookup_name (parser, id_expression,
3365 none_type,
3366 template_p,
3367 /*is_namespace=*/false,
3368 /*check_dependency=*/true,
3369 &ambiguous_decls);
3370 /* If the lookup was ambiguous, an error will already have
3371 been issued. */
3372 if (ambiguous_decls)
3373 return error_mark_node;
3375 /* In Objective-C++, an instance variable (ivar) may be preferred
3376 to whatever cp_parser_lookup_name() found. */
3377 decl = objc_lookup_ivar (decl, id_expression);
3379 /* If name lookup gives us a SCOPE_REF, then the
3380 qualifying scope was dependent. */
3381 if (TREE_CODE (decl) == SCOPE_REF)
3383 /* At this point, we do not know if DECL is a valid
3384 integral constant expression. We assume that it is
3385 in fact such an expression, so that code like:
3387 template <int N> struct A {
3388 int a[B<N>::i];
3391 is accepted. At template-instantiation time, we
3392 will check that B<N>::i is actually a constant. */
3393 return decl;
3395 /* Check to see if DECL is a local variable in a context
3396 where that is forbidden. */
3397 if (parser->local_variables_forbidden_p
3398 && local_variable_p (decl))
3400 /* It might be that we only found DECL because we are
3401 trying to be generous with pre-ISO scoping rules.
3402 For example, consider:
3404 int i;
3405 void g() {
3406 for (int i = 0; i < 10; ++i) {}
3407 extern void f(int j = i);
3410 Here, name look up will originally find the out
3411 of scope `i'. We need to issue a warning message,
3412 but then use the global `i'. */
3413 decl = check_for_out_of_scope_variable (decl);
3414 if (local_variable_p (decl))
3416 error ("local variable %qD may not appear in this context",
3417 decl);
3418 return error_mark_node;
3423 decl = (finish_id_expression
3424 (id_expression, decl, parser->scope,
3425 idk,
3426 parser->integral_constant_expression_p,
3427 parser->allow_non_integral_constant_expression_p,
3428 &parser->non_integral_constant_expression_p,
3429 template_p, done, address_p,
3430 template_arg_p,
3431 &error_msg));
3432 if (error_msg)
3433 cp_parser_error (parser, error_msg);
3434 return decl;
3437 /* Anything else is an error. */
3438 default:
3439 /* ...unless we have an Objective-C++ message or string literal,
3440 that is. */
3441 if (c_dialect_objc ()
3442 && (token->type == CPP_OPEN_SQUARE
3443 || token->type == CPP_OBJC_STRING))
3444 return cp_parser_objc_expression (parser);
3446 cp_parser_error (parser, "expected primary-expression");
3447 return error_mark_node;
3451 /* Parse an id-expression.
3453 id-expression:
3454 unqualified-id
3455 qualified-id
3457 qualified-id:
3458 :: [opt] nested-name-specifier template [opt] unqualified-id
3459 :: identifier
3460 :: operator-function-id
3461 :: template-id
3463 Return a representation of the unqualified portion of the
3464 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3465 a `::' or nested-name-specifier.
3467 Often, if the id-expression was a qualified-id, the caller will
3468 want to make a SCOPE_REF to represent the qualified-id. This
3469 function does not do this in order to avoid wastefully creating
3470 SCOPE_REFs when they are not required.
3472 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3473 `template' keyword.
3475 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3476 uninstantiated templates.
3478 If *TEMPLATE_P is non-NULL, it is set to true iff the
3479 `template' keyword is used to explicitly indicate that the entity
3480 named is a template.
3482 If DECLARATOR_P is true, the id-expression is appearing as part of
3483 a declarator, rather than as part of an expression. */
3485 static tree
3486 cp_parser_id_expression (cp_parser *parser,
3487 bool template_keyword_p,
3488 bool check_dependency_p,
3489 bool *template_p,
3490 bool declarator_p,
3491 bool optional_p)
3493 bool global_scope_p;
3494 bool nested_name_specifier_p;
3496 /* Assume the `template' keyword was not used. */
3497 if (template_p)
3498 *template_p = template_keyword_p;
3500 /* Look for the optional `::' operator. */
3501 global_scope_p
3502 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3503 != NULL_TREE);
3504 /* Look for the optional nested-name-specifier. */
3505 nested_name_specifier_p
3506 = (cp_parser_nested_name_specifier_opt (parser,
3507 /*typename_keyword_p=*/false,
3508 check_dependency_p,
3509 /*type_p=*/false,
3510 declarator_p)
3511 != NULL_TREE);
3512 /* If there is a nested-name-specifier, then we are looking at
3513 the first qualified-id production. */
3514 if (nested_name_specifier_p)
3516 tree saved_scope;
3517 tree saved_object_scope;
3518 tree saved_qualifying_scope;
3519 tree unqualified_id;
3520 bool is_template;
3522 /* See if the next token is the `template' keyword. */
3523 if (!template_p)
3524 template_p = &is_template;
3525 *template_p = cp_parser_optional_template_keyword (parser);
3526 /* Name lookup we do during the processing of the
3527 unqualified-id might obliterate SCOPE. */
3528 saved_scope = parser->scope;
3529 saved_object_scope = parser->object_scope;
3530 saved_qualifying_scope = parser->qualifying_scope;
3531 /* Process the final unqualified-id. */
3532 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3533 check_dependency_p,
3534 declarator_p,
3535 /*optional_p=*/false);
3536 /* Restore the SAVED_SCOPE for our caller. */
3537 parser->scope = saved_scope;
3538 parser->object_scope = saved_object_scope;
3539 parser->qualifying_scope = saved_qualifying_scope;
3541 return unqualified_id;
3543 /* Otherwise, if we are in global scope, then we are looking at one
3544 of the other qualified-id productions. */
3545 else if (global_scope_p)
3547 cp_token *token;
3548 tree id;
3550 /* Peek at the next token. */
3551 token = cp_lexer_peek_token (parser->lexer);
3553 /* If it's an identifier, and the next token is not a "<", then
3554 we can avoid the template-id case. This is an optimization
3555 for this common case. */
3556 if (token->type == CPP_NAME
3557 && !cp_parser_nth_token_starts_template_argument_list_p
3558 (parser, 2))
3559 return cp_parser_identifier (parser);
3561 cp_parser_parse_tentatively (parser);
3562 /* Try a template-id. */
3563 id = cp_parser_template_id (parser,
3564 /*template_keyword_p=*/false,
3565 /*check_dependency_p=*/true,
3566 declarator_p);
3567 /* If that worked, we're done. */
3568 if (cp_parser_parse_definitely (parser))
3569 return id;
3571 /* Peek at the next token. (Changes in the token buffer may
3572 have invalidated the pointer obtained above.) */
3573 token = cp_lexer_peek_token (parser->lexer);
3575 switch (token->type)
3577 case CPP_NAME:
3578 return cp_parser_identifier (parser);
3580 case CPP_KEYWORD:
3581 if (token->keyword == RID_OPERATOR)
3582 return cp_parser_operator_function_id (parser);
3583 /* Fall through. */
3585 default:
3586 cp_parser_error (parser, "expected id-expression");
3587 return error_mark_node;
3590 else
3591 return cp_parser_unqualified_id (parser, template_keyword_p,
3592 /*check_dependency_p=*/true,
3593 declarator_p,
3594 optional_p);
3597 /* Parse an unqualified-id.
3599 unqualified-id:
3600 identifier
3601 operator-function-id
3602 conversion-function-id
3603 ~ class-name
3604 template-id
3606 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3607 keyword, in a construct like `A::template ...'.
3609 Returns a representation of unqualified-id. For the `identifier'
3610 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3611 production a BIT_NOT_EXPR is returned; the operand of the
3612 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3613 other productions, see the documentation accompanying the
3614 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3615 names are looked up in uninstantiated templates. If DECLARATOR_P
3616 is true, the unqualified-id is appearing as part of a declarator,
3617 rather than as part of an expression. */
3619 static tree
3620 cp_parser_unqualified_id (cp_parser* parser,
3621 bool template_keyword_p,
3622 bool check_dependency_p,
3623 bool declarator_p,
3624 bool optional_p)
3626 cp_token *token;
3628 /* Peek at the next token. */
3629 token = cp_lexer_peek_token (parser->lexer);
3631 switch (token->type)
3633 case CPP_NAME:
3635 tree id;
3637 /* We don't know yet whether or not this will be a
3638 template-id. */
3639 cp_parser_parse_tentatively (parser);
3640 /* Try a template-id. */
3641 id = cp_parser_template_id (parser, template_keyword_p,
3642 check_dependency_p,
3643 declarator_p);
3644 /* If it worked, we're done. */
3645 if (cp_parser_parse_definitely (parser))
3646 return id;
3647 /* Otherwise, it's an ordinary identifier. */
3648 return cp_parser_identifier (parser);
3651 case CPP_TEMPLATE_ID:
3652 return cp_parser_template_id (parser, template_keyword_p,
3653 check_dependency_p,
3654 declarator_p);
3656 case CPP_COMPL:
3658 tree type_decl;
3659 tree qualifying_scope;
3660 tree object_scope;
3661 tree scope;
3662 bool done;
3664 /* Consume the `~' token. */
3665 cp_lexer_consume_token (parser->lexer);
3666 /* Parse the class-name. The standard, as written, seems to
3667 say that:
3669 template <typename T> struct S { ~S (); };
3670 template <typename T> S<T>::~S() {}
3672 is invalid, since `~' must be followed by a class-name, but
3673 `S<T>' is dependent, and so not known to be a class.
3674 That's not right; we need to look in uninstantiated
3675 templates. A further complication arises from:
3677 template <typename T> void f(T t) {
3678 t.T::~T();
3681 Here, it is not possible to look up `T' in the scope of `T'
3682 itself. We must look in both the current scope, and the
3683 scope of the containing complete expression.
3685 Yet another issue is:
3687 struct S {
3688 int S;
3689 ~S();
3692 S::~S() {}
3694 The standard does not seem to say that the `S' in `~S'
3695 should refer to the type `S' and not the data member
3696 `S::S'. */
3698 /* DR 244 says that we look up the name after the "~" in the
3699 same scope as we looked up the qualifying name. That idea
3700 isn't fully worked out; it's more complicated than that. */
3701 scope = parser->scope;
3702 object_scope = parser->object_scope;
3703 qualifying_scope = parser->qualifying_scope;
3705 /* Check for invalid scopes. */
3706 if (scope == error_mark_node)
3708 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3709 cp_lexer_consume_token (parser->lexer);
3710 return error_mark_node;
3712 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3714 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3715 error ("scope %qT before %<~%> is not a class-name", scope);
3716 cp_parser_simulate_error (parser);
3717 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3718 cp_lexer_consume_token (parser->lexer);
3719 return error_mark_node;
3721 gcc_assert (!scope || TYPE_P (scope));
3723 /* If the name is of the form "X::~X" it's OK. */
3724 token = cp_lexer_peek_token (parser->lexer);
3725 if (scope
3726 && token->type == CPP_NAME
3727 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3728 == CPP_OPEN_PAREN)
3729 && constructor_name_p (token->u.value, scope))
3731 cp_lexer_consume_token (parser->lexer);
3732 return build_nt (BIT_NOT_EXPR, scope);
3735 /* If there was an explicit qualification (S::~T), first look
3736 in the scope given by the qualification (i.e., S). */
3737 done = false;
3738 type_decl = NULL_TREE;
3739 if (scope)
3741 cp_parser_parse_tentatively (parser);
3742 type_decl = cp_parser_class_name (parser,
3743 /*typename_keyword_p=*/false,
3744 /*template_keyword_p=*/false,
3745 none_type,
3746 /*check_dependency=*/false,
3747 /*class_head_p=*/false,
3748 declarator_p);
3749 if (cp_parser_parse_definitely (parser))
3750 done = true;
3752 /* In "N::S::~S", look in "N" as well. */
3753 if (!done && scope && qualifying_scope)
3755 cp_parser_parse_tentatively (parser);
3756 parser->scope = qualifying_scope;
3757 parser->object_scope = NULL_TREE;
3758 parser->qualifying_scope = NULL_TREE;
3759 type_decl
3760 = cp_parser_class_name (parser,
3761 /*typename_keyword_p=*/false,
3762 /*template_keyword_p=*/false,
3763 none_type,
3764 /*check_dependency=*/false,
3765 /*class_head_p=*/false,
3766 declarator_p);
3767 if (cp_parser_parse_definitely (parser))
3768 done = true;
3770 /* In "p->S::~T", look in the scope given by "*p" as well. */
3771 else if (!done && object_scope)
3773 cp_parser_parse_tentatively (parser);
3774 parser->scope = object_scope;
3775 parser->object_scope = NULL_TREE;
3776 parser->qualifying_scope = NULL_TREE;
3777 type_decl
3778 = cp_parser_class_name (parser,
3779 /*typename_keyword_p=*/false,
3780 /*template_keyword_p=*/false,
3781 none_type,
3782 /*check_dependency=*/false,
3783 /*class_head_p=*/false,
3784 declarator_p);
3785 if (cp_parser_parse_definitely (parser))
3786 done = true;
3788 /* Look in the surrounding context. */
3789 if (!done)
3791 parser->scope = NULL_TREE;
3792 parser->object_scope = NULL_TREE;
3793 parser->qualifying_scope = NULL_TREE;
3794 type_decl
3795 = cp_parser_class_name (parser,
3796 /*typename_keyword_p=*/false,
3797 /*template_keyword_p=*/false,
3798 none_type,
3799 /*check_dependency=*/false,
3800 /*class_head_p=*/false,
3801 declarator_p);
3803 /* If an error occurred, assume that the name of the
3804 destructor is the same as the name of the qualifying
3805 class. That allows us to keep parsing after running
3806 into ill-formed destructor names. */
3807 if (type_decl == error_mark_node && scope)
3808 return build_nt (BIT_NOT_EXPR, scope);
3809 else if (type_decl == error_mark_node)
3810 return error_mark_node;
3812 /* Check that destructor name and scope match. */
3813 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3815 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3816 error ("declaration of %<~%T%> as member of %qT",
3817 type_decl, scope);
3818 cp_parser_simulate_error (parser);
3819 return error_mark_node;
3822 /* [class.dtor]
3824 A typedef-name that names a class shall not be used as the
3825 identifier in the declarator for a destructor declaration. */
3826 if (declarator_p
3827 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3828 && !DECL_SELF_REFERENCE_P (type_decl)
3829 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3830 error ("typedef-name %qD used as destructor declarator",
3831 type_decl);
3833 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3836 case CPP_KEYWORD:
3837 if (token->keyword == RID_OPERATOR)
3839 tree id;
3841 /* This could be a template-id, so we try that first. */
3842 cp_parser_parse_tentatively (parser);
3843 /* Try a template-id. */
3844 id = cp_parser_template_id (parser, template_keyword_p,
3845 /*check_dependency_p=*/true,
3846 declarator_p);
3847 /* If that worked, we're done. */
3848 if (cp_parser_parse_definitely (parser))
3849 return id;
3850 /* We still don't know whether we're looking at an
3851 operator-function-id or a conversion-function-id. */
3852 cp_parser_parse_tentatively (parser);
3853 /* Try an operator-function-id. */
3854 id = cp_parser_operator_function_id (parser);
3855 /* If that didn't work, try a conversion-function-id. */
3856 if (!cp_parser_parse_definitely (parser))
3857 id = cp_parser_conversion_function_id (parser);
3859 return id;
3861 /* Fall through. */
3863 default:
3864 if (optional_p)
3865 return NULL_TREE;
3866 cp_parser_error (parser, "expected unqualified-id");
3867 return error_mark_node;
3871 /* Parse an (optional) nested-name-specifier.
3873 nested-name-specifier:
3874 class-or-namespace-name :: nested-name-specifier [opt]
3875 class-or-namespace-name :: template nested-name-specifier [opt]
3877 PARSER->SCOPE should be set appropriately before this function is
3878 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3879 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3880 in name lookups.
3882 Sets PARSER->SCOPE to the class (TYPE) or namespace
3883 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3884 it unchanged if there is no nested-name-specifier. Returns the new
3885 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3887 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3888 part of a declaration and/or decl-specifier. */
3890 static tree
3891 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3892 bool typename_keyword_p,
3893 bool check_dependency_p,
3894 bool type_p,
3895 bool is_declaration)
3897 bool success = false;
3898 cp_token_position start = 0;
3899 cp_token *token;
3901 /* Remember where the nested-name-specifier starts. */
3902 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3904 start = cp_lexer_token_position (parser->lexer, false);
3905 push_deferring_access_checks (dk_deferred);
3908 while (true)
3910 tree new_scope;
3911 tree old_scope;
3912 tree saved_qualifying_scope;
3913 bool template_keyword_p;
3915 /* Spot cases that cannot be the beginning of a
3916 nested-name-specifier. */
3917 token = cp_lexer_peek_token (parser->lexer);
3919 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3920 the already parsed nested-name-specifier. */
3921 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3923 /* Grab the nested-name-specifier and continue the loop. */
3924 cp_parser_pre_parsed_nested_name_specifier (parser);
3925 /* If we originally encountered this nested-name-specifier
3926 with IS_DECLARATION set to false, we will not have
3927 resolved TYPENAME_TYPEs, so we must do so here. */
3928 if (is_declaration
3929 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3931 new_scope = resolve_typename_type (parser->scope,
3932 /*only_current_p=*/false);
3933 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3934 parser->scope = new_scope;
3936 success = true;
3937 continue;
3940 /* Spot cases that cannot be the beginning of a
3941 nested-name-specifier. On the second and subsequent times
3942 through the loop, we look for the `template' keyword. */
3943 if (success && token->keyword == RID_TEMPLATE)
3945 /* A template-id can start a nested-name-specifier. */
3946 else if (token->type == CPP_TEMPLATE_ID)
3948 else
3950 /* If the next token is not an identifier, then it is
3951 definitely not a class-or-namespace-name. */
3952 if (token->type != CPP_NAME)
3953 break;
3954 /* If the following token is neither a `<' (to begin a
3955 template-id), nor a `::', then we are not looking at a
3956 nested-name-specifier. */
3957 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3958 if (token->type != CPP_SCOPE
3959 && !cp_parser_nth_token_starts_template_argument_list_p
3960 (parser, 2))
3961 break;
3964 /* The nested-name-specifier is optional, so we parse
3965 tentatively. */
3966 cp_parser_parse_tentatively (parser);
3968 /* Look for the optional `template' keyword, if this isn't the
3969 first time through the loop. */
3970 if (success)
3971 template_keyword_p = cp_parser_optional_template_keyword (parser);
3972 else
3973 template_keyword_p = false;
3975 /* Save the old scope since the name lookup we are about to do
3976 might destroy it. */
3977 old_scope = parser->scope;
3978 saved_qualifying_scope = parser->qualifying_scope;
3979 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3980 look up names in "X<T>::I" in order to determine that "Y" is
3981 a template. So, if we have a typename at this point, we make
3982 an effort to look through it. */
3983 if (is_declaration
3984 && !typename_keyword_p
3985 && parser->scope
3986 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3987 parser->scope = resolve_typename_type (parser->scope,
3988 /*only_current_p=*/false);
3989 /* Parse the qualifying entity. */
3990 new_scope
3991 = cp_parser_class_or_namespace_name (parser,
3992 typename_keyword_p,
3993 template_keyword_p,
3994 check_dependency_p,
3995 type_p,
3996 is_declaration);
3997 /* Look for the `::' token. */
3998 cp_parser_require (parser, CPP_SCOPE, "`::'");
4000 /* If we found what we wanted, we keep going; otherwise, we're
4001 done. */
4002 if (!cp_parser_parse_definitely (parser))
4004 bool error_p = false;
4006 /* Restore the OLD_SCOPE since it was valid before the
4007 failed attempt at finding the last
4008 class-or-namespace-name. */
4009 parser->scope = old_scope;
4010 parser->qualifying_scope = saved_qualifying_scope;
4011 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4012 break;
4013 /* If the next token is an identifier, and the one after
4014 that is a `::', then any valid interpretation would have
4015 found a class-or-namespace-name. */
4016 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4017 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4018 == CPP_SCOPE)
4019 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4020 != CPP_COMPL))
4022 token = cp_lexer_consume_token (parser->lexer);
4023 if (!error_p)
4025 if (!token->ambiguous_p)
4027 tree decl;
4028 tree ambiguous_decls;
4030 decl = cp_parser_lookup_name (parser, token->u.value,
4031 none_type,
4032 /*is_template=*/false,
4033 /*is_namespace=*/false,
4034 /*check_dependency=*/true,
4035 &ambiguous_decls);
4036 if (TREE_CODE (decl) == TEMPLATE_DECL)
4037 error ("%qD used without template parameters", decl);
4038 else if (ambiguous_decls)
4040 error ("reference to %qD is ambiguous",
4041 token->u.value);
4042 print_candidates (ambiguous_decls);
4043 decl = error_mark_node;
4045 else
4046 cp_parser_name_lookup_error
4047 (parser, token->u.value, decl,
4048 "is not a class or namespace");
4050 parser->scope = error_mark_node;
4051 error_p = true;
4052 /* Treat this as a successful nested-name-specifier
4053 due to:
4055 [basic.lookup.qual]
4057 If the name found is not a class-name (clause
4058 _class_) or namespace-name (_namespace.def_), the
4059 program is ill-formed. */
4060 success = true;
4062 cp_lexer_consume_token (parser->lexer);
4064 break;
4066 /* We've found one valid nested-name-specifier. */
4067 success = true;
4068 /* Name lookup always gives us a DECL. */
4069 if (TREE_CODE (new_scope) == TYPE_DECL)
4070 new_scope = TREE_TYPE (new_scope);
4071 /* Uses of "template" must be followed by actual templates. */
4072 if (template_keyword_p
4073 && !(CLASS_TYPE_P (new_scope)
4074 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4075 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4076 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4077 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4078 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4079 == TEMPLATE_ID_EXPR)))
4080 pedwarn (TYPE_P (new_scope)
4081 ? "%qT is not a template"
4082 : "%qD is not a template",
4083 new_scope);
4084 /* If it is a class scope, try to complete it; we are about to
4085 be looking up names inside the class. */
4086 if (TYPE_P (new_scope)
4087 /* Since checking types for dependency can be expensive,
4088 avoid doing it if the type is already complete. */
4089 && !COMPLETE_TYPE_P (new_scope)
4090 /* Do not try to complete dependent types. */
4091 && !dependent_type_p (new_scope))
4093 new_scope = complete_type (new_scope);
4094 /* If it is a typedef to current class, use the current
4095 class instead, as the typedef won't have any names inside
4096 it yet. */
4097 if (!COMPLETE_TYPE_P (new_scope)
4098 && currently_open_class (new_scope))
4099 new_scope = TYPE_MAIN_VARIANT (new_scope);
4101 /* Make sure we look in the right scope the next time through
4102 the loop. */
4103 parser->scope = new_scope;
4106 /* If parsing tentatively, replace the sequence of tokens that makes
4107 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4108 token. That way, should we re-parse the token stream, we will
4109 not have to repeat the effort required to do the parse, nor will
4110 we issue duplicate error messages. */
4111 if (success && start)
4113 cp_token *token;
4115 token = cp_lexer_token_at (parser->lexer, start);
4116 /* Reset the contents of the START token. */
4117 token->type = CPP_NESTED_NAME_SPECIFIER;
4118 /* Retrieve any deferred checks. Do not pop this access checks yet
4119 so the memory will not be reclaimed during token replacing below. */
4120 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4121 token->u.tree_check_value->value = parser->scope;
4122 token->u.tree_check_value->checks = get_deferred_access_checks ();
4123 token->u.tree_check_value->qualifying_scope =
4124 parser->qualifying_scope;
4125 token->keyword = RID_MAX;
4127 /* Purge all subsequent tokens. */
4128 cp_lexer_purge_tokens_after (parser->lexer, start);
4131 if (start)
4132 pop_to_parent_deferring_access_checks ();
4134 return success ? parser->scope : NULL_TREE;
4137 /* Parse a nested-name-specifier. See
4138 cp_parser_nested_name_specifier_opt for details. This function
4139 behaves identically, except that it will an issue an error if no
4140 nested-name-specifier is present. */
4142 static tree
4143 cp_parser_nested_name_specifier (cp_parser *parser,
4144 bool typename_keyword_p,
4145 bool check_dependency_p,
4146 bool type_p,
4147 bool is_declaration)
4149 tree scope;
4151 /* Look for the nested-name-specifier. */
4152 scope = cp_parser_nested_name_specifier_opt (parser,
4153 typename_keyword_p,
4154 check_dependency_p,
4155 type_p,
4156 is_declaration);
4157 /* If it was not present, issue an error message. */
4158 if (!scope)
4160 cp_parser_error (parser, "expected nested-name-specifier");
4161 parser->scope = NULL_TREE;
4164 return scope;
4167 /* Parse a class-or-namespace-name.
4169 class-or-namespace-name:
4170 class-name
4171 namespace-name
4173 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4174 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4175 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4176 TYPE_P is TRUE iff the next name should be taken as a class-name,
4177 even the same name is declared to be another entity in the same
4178 scope.
4180 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4181 specified by the class-or-namespace-name. If neither is found the
4182 ERROR_MARK_NODE is returned. */
4184 static tree
4185 cp_parser_class_or_namespace_name (cp_parser *parser,
4186 bool typename_keyword_p,
4187 bool template_keyword_p,
4188 bool check_dependency_p,
4189 bool type_p,
4190 bool is_declaration)
4192 tree saved_scope;
4193 tree saved_qualifying_scope;
4194 tree saved_object_scope;
4195 tree scope;
4196 bool only_class_p;
4198 /* Before we try to parse the class-name, we must save away the
4199 current PARSER->SCOPE since cp_parser_class_name will destroy
4200 it. */
4201 saved_scope = parser->scope;
4202 saved_qualifying_scope = parser->qualifying_scope;
4203 saved_object_scope = parser->object_scope;
4204 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4205 there is no need to look for a namespace-name. */
4206 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4207 if (!only_class_p)
4208 cp_parser_parse_tentatively (parser);
4209 scope = cp_parser_class_name (parser,
4210 typename_keyword_p,
4211 template_keyword_p,
4212 type_p ? class_type : none_type,
4213 check_dependency_p,
4214 /*class_head_p=*/false,
4215 is_declaration);
4216 /* If that didn't work, try for a namespace-name. */
4217 if (!only_class_p && !cp_parser_parse_definitely (parser))
4219 /* Restore the saved scope. */
4220 parser->scope = saved_scope;
4221 parser->qualifying_scope = saved_qualifying_scope;
4222 parser->object_scope = saved_object_scope;
4223 /* If we are not looking at an identifier followed by the scope
4224 resolution operator, then this is not part of a
4225 nested-name-specifier. (Note that this function is only used
4226 to parse the components of a nested-name-specifier.) */
4227 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4228 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4229 return error_mark_node;
4230 scope = cp_parser_namespace_name (parser);
4233 return scope;
4236 /* Parse a postfix-expression.
4238 postfix-expression:
4239 primary-expression
4240 postfix-expression [ expression ]
4241 postfix-expression ( expression-list [opt] )
4242 simple-type-specifier ( expression-list [opt] )
4243 typename :: [opt] nested-name-specifier identifier
4244 ( expression-list [opt] )
4245 typename :: [opt] nested-name-specifier template [opt] template-id
4246 ( expression-list [opt] )
4247 postfix-expression . template [opt] id-expression
4248 postfix-expression -> template [opt] id-expression
4249 postfix-expression . pseudo-destructor-name
4250 postfix-expression -> pseudo-destructor-name
4251 postfix-expression ++
4252 postfix-expression --
4253 dynamic_cast < type-id > ( expression )
4254 static_cast < type-id > ( expression )
4255 reinterpret_cast < type-id > ( expression )
4256 const_cast < type-id > ( expression )
4257 typeid ( expression )
4258 typeid ( type-id )
4260 GNU Extension:
4262 postfix-expression:
4263 ( type-id ) { initializer-list , [opt] }
4265 This extension is a GNU version of the C99 compound-literal
4266 construct. (The C99 grammar uses `type-name' instead of `type-id',
4267 but they are essentially the same concept.)
4269 If ADDRESS_P is true, the postfix expression is the operand of the
4270 `&' operator. CAST_P is true if this expression is the target of a
4271 cast.
4273 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4274 class member access expressions [expr.ref].
4276 Returns a representation of the expression. */
4278 static tree
4279 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4280 bool member_access_only_p)
4282 cp_token *token;
4283 enum rid keyword;
4284 cp_id_kind idk = CP_ID_KIND_NONE;
4285 tree postfix_expression = NULL_TREE;
4286 bool is_member_access = false;
4288 /* Peek at the next token. */
4289 token = cp_lexer_peek_token (parser->lexer);
4290 /* Some of the productions are determined by keywords. */
4291 keyword = token->keyword;
4292 switch (keyword)
4294 case RID_DYNCAST:
4295 case RID_STATCAST:
4296 case RID_REINTCAST:
4297 case RID_CONSTCAST:
4299 tree type;
4300 tree expression;
4301 const char *saved_message;
4303 /* All of these can be handled in the same way from the point
4304 of view of parsing. Begin by consuming the token
4305 identifying the cast. */
4306 cp_lexer_consume_token (parser->lexer);
4308 /* New types cannot be defined in the cast. */
4309 saved_message = parser->type_definition_forbidden_message;
4310 parser->type_definition_forbidden_message
4311 = "types may not be defined in casts";
4313 /* Look for the opening `<'. */
4314 cp_parser_require (parser, CPP_LESS, "`<'");
4315 /* Parse the type to which we are casting. */
4316 type = cp_parser_type_id (parser);
4317 /* Look for the closing `>'. */
4318 cp_parser_require (parser, CPP_GREATER, "`>'");
4319 /* Restore the old message. */
4320 parser->type_definition_forbidden_message = saved_message;
4322 /* And the expression which is being cast. */
4323 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4324 expression = cp_parser_expression (parser, /*cast_p=*/true);
4325 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4327 /* Only type conversions to integral or enumeration types
4328 can be used in constant-expressions. */
4329 if (!cast_valid_in_integral_constant_expression_p (type)
4330 && (cp_parser_non_integral_constant_expression
4331 (parser,
4332 "a cast to a type other than an integral or "
4333 "enumeration type")))
4334 return error_mark_node;
4336 switch (keyword)
4338 case RID_DYNCAST:
4339 postfix_expression
4340 = build_dynamic_cast (type, expression);
4341 break;
4342 case RID_STATCAST:
4343 postfix_expression
4344 = build_static_cast (type, expression);
4345 break;
4346 case RID_REINTCAST:
4347 postfix_expression
4348 = build_reinterpret_cast (type, expression);
4349 break;
4350 case RID_CONSTCAST:
4351 postfix_expression
4352 = build_const_cast (type, expression);
4353 break;
4354 default:
4355 gcc_unreachable ();
4358 break;
4360 case RID_TYPEID:
4362 tree type;
4363 const char *saved_message;
4364 bool saved_in_type_id_in_expr_p;
4366 /* Consume the `typeid' token. */
4367 cp_lexer_consume_token (parser->lexer);
4368 /* Look for the `(' token. */
4369 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4370 /* Types cannot be defined in a `typeid' expression. */
4371 saved_message = parser->type_definition_forbidden_message;
4372 parser->type_definition_forbidden_message
4373 = "types may not be defined in a `typeid\' expression";
4374 /* We can't be sure yet whether we're looking at a type-id or an
4375 expression. */
4376 cp_parser_parse_tentatively (parser);
4377 /* Try a type-id first. */
4378 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4379 parser->in_type_id_in_expr_p = true;
4380 type = cp_parser_type_id (parser);
4381 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4382 /* Look for the `)' token. Otherwise, we can't be sure that
4383 we're not looking at an expression: consider `typeid (int
4384 (3))', for example. */
4385 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4386 /* If all went well, simply lookup the type-id. */
4387 if (cp_parser_parse_definitely (parser))
4388 postfix_expression = get_typeid (type);
4389 /* Otherwise, fall back to the expression variant. */
4390 else
4392 tree expression;
4394 /* Look for an expression. */
4395 expression = cp_parser_expression (parser, /*cast_p=*/false);
4396 /* Compute its typeid. */
4397 postfix_expression = build_typeid (expression);
4398 /* Look for the `)' token. */
4399 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4401 /* Restore the saved message. */
4402 parser->type_definition_forbidden_message = saved_message;
4403 /* `typeid' may not appear in an integral constant expression. */
4404 if (cp_parser_non_integral_constant_expression(parser,
4405 "`typeid' operator"))
4406 return error_mark_node;
4408 break;
4410 case RID_TYPENAME:
4412 tree type;
4413 /* The syntax permitted here is the same permitted for an
4414 elaborated-type-specifier. */
4415 type = cp_parser_elaborated_type_specifier (parser,
4416 /*is_friend=*/false,
4417 /*is_declaration=*/false);
4418 postfix_expression = cp_parser_functional_cast (parser, type);
4420 break;
4422 default:
4424 tree type;
4426 /* If the next thing is a simple-type-specifier, we may be
4427 looking at a functional cast. We could also be looking at
4428 an id-expression. So, we try the functional cast, and if
4429 that doesn't work we fall back to the primary-expression. */
4430 cp_parser_parse_tentatively (parser);
4431 /* Look for the simple-type-specifier. */
4432 type = cp_parser_simple_type_specifier (parser,
4433 /*decl_specs=*/NULL,
4434 CP_PARSER_FLAGS_NONE);
4435 /* Parse the cast itself. */
4436 if (!cp_parser_error_occurred (parser))
4437 postfix_expression
4438 = cp_parser_functional_cast (parser, type);
4439 /* If that worked, we're done. */
4440 if (cp_parser_parse_definitely (parser))
4441 break;
4443 /* If the functional-cast didn't work out, try a
4444 compound-literal. */
4445 if (cp_parser_allow_gnu_extensions_p (parser)
4446 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4448 VEC(constructor_elt,gc) *initializer_list = NULL;
4449 bool saved_in_type_id_in_expr_p;
4451 cp_parser_parse_tentatively (parser);
4452 /* Consume the `('. */
4453 cp_lexer_consume_token (parser->lexer);
4454 /* Parse the type. */
4455 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4456 parser->in_type_id_in_expr_p = true;
4457 type = cp_parser_type_id (parser);
4458 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4459 /* Look for the `)'. */
4460 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4461 /* Look for the `{'. */
4462 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4463 /* If things aren't going well, there's no need to
4464 keep going. */
4465 if (!cp_parser_error_occurred (parser))
4467 bool non_constant_p;
4468 /* Parse the initializer-list. */
4469 initializer_list
4470 = cp_parser_initializer_list (parser, &non_constant_p);
4471 /* Allow a trailing `,'. */
4472 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4473 cp_lexer_consume_token (parser->lexer);
4474 /* Look for the final `}'. */
4475 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4477 /* If that worked, we're definitely looking at a
4478 compound-literal expression. */
4479 if (cp_parser_parse_definitely (parser))
4481 /* Warn the user that a compound literal is not
4482 allowed in standard C++. */
4483 if (pedantic)
4484 pedwarn ("ISO C++ forbids compound-literals");
4485 /* For simplicity, we disallow compound literals in
4486 constant-expressions. We could
4487 allow compound literals of integer type, whose
4488 initializer was a constant, in constant
4489 expressions. Permitting that usage, as a further
4490 extension, would not change the meaning of any
4491 currently accepted programs. (Of course, as
4492 compound literals are not part of ISO C++, the
4493 standard has nothing to say.) */
4494 if (cp_parser_non_integral_constant_expression
4495 (parser, "non-constant compound literals"))
4497 postfix_expression = error_mark_node;
4498 break;
4500 /* Form the representation of the compound-literal. */
4501 postfix_expression
4502 = finish_compound_literal (type, initializer_list);
4503 break;
4507 /* It must be a primary-expression. */
4508 postfix_expression
4509 = cp_parser_primary_expression (parser, address_p, cast_p,
4510 /*template_arg_p=*/false,
4511 &idk);
4513 break;
4516 /* Keep looping until the postfix-expression is complete. */
4517 while (true)
4519 if (idk == CP_ID_KIND_UNQUALIFIED
4520 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4521 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4522 /* It is not a Koenig lookup function call. */
4523 postfix_expression
4524 = unqualified_name_lookup_error (postfix_expression);
4526 /* Peek at the next token. */
4527 token = cp_lexer_peek_token (parser->lexer);
4529 switch (token->type)
4531 case CPP_OPEN_SQUARE:
4532 postfix_expression
4533 = cp_parser_postfix_open_square_expression (parser,
4534 postfix_expression,
4535 false);
4536 idk = CP_ID_KIND_NONE;
4537 is_member_access = false;
4538 break;
4540 case CPP_OPEN_PAREN:
4541 /* postfix-expression ( expression-list [opt] ) */
4543 bool koenig_p;
4544 bool is_builtin_constant_p;
4545 bool saved_integral_constant_expression_p = false;
4546 bool saved_non_integral_constant_expression_p = false;
4547 tree args;
4549 is_member_access = false;
4551 is_builtin_constant_p
4552 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4553 if (is_builtin_constant_p)
4555 /* The whole point of __builtin_constant_p is to allow
4556 non-constant expressions to appear as arguments. */
4557 saved_integral_constant_expression_p
4558 = parser->integral_constant_expression_p;
4559 saved_non_integral_constant_expression_p
4560 = parser->non_integral_constant_expression_p;
4561 parser->integral_constant_expression_p = false;
4563 args = (cp_parser_parenthesized_expression_list
4564 (parser, /*is_attribute_list=*/false,
4565 /*cast_p=*/false, /*allow_expansion_p=*/true,
4566 /*non_constant_p=*/NULL));
4567 if (is_builtin_constant_p)
4569 parser->integral_constant_expression_p
4570 = saved_integral_constant_expression_p;
4571 parser->non_integral_constant_expression_p
4572 = saved_non_integral_constant_expression_p;
4575 if (args == error_mark_node)
4577 postfix_expression = error_mark_node;
4578 break;
4581 /* Function calls are not permitted in
4582 constant-expressions. */
4583 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4584 && cp_parser_non_integral_constant_expression (parser,
4585 "a function call"))
4587 postfix_expression = error_mark_node;
4588 break;
4591 koenig_p = false;
4592 if (idk == CP_ID_KIND_UNQUALIFIED)
4594 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4596 if (args)
4598 koenig_p = true;
4599 postfix_expression
4600 = perform_koenig_lookup (postfix_expression, args);
4602 else
4603 postfix_expression
4604 = unqualified_fn_lookup_error (postfix_expression);
4606 /* We do not perform argument-dependent lookup if
4607 normal lookup finds a non-function, in accordance
4608 with the expected resolution of DR 218. */
4609 else if (args && is_overloaded_fn (postfix_expression))
4611 tree fn = get_first_fn (postfix_expression);
4613 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4614 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4616 /* Only do argument dependent lookup if regular
4617 lookup does not find a set of member functions.
4618 [basic.lookup.koenig]/2a */
4619 if (!DECL_FUNCTION_MEMBER_P (fn))
4621 koenig_p = true;
4622 postfix_expression
4623 = perform_koenig_lookup (postfix_expression, args);
4628 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4630 tree instance = TREE_OPERAND (postfix_expression, 0);
4631 tree fn = TREE_OPERAND (postfix_expression, 1);
4633 if (processing_template_decl
4634 && (type_dependent_expression_p (instance)
4635 || (!BASELINK_P (fn)
4636 && TREE_CODE (fn) != FIELD_DECL)
4637 || type_dependent_expression_p (fn)
4638 || any_type_dependent_arguments_p (args)))
4640 postfix_expression
4641 = build_nt_call_list (postfix_expression, args);
4642 break;
4645 if (BASELINK_P (fn))
4646 postfix_expression
4647 = (build_new_method_call
4648 (instance, fn, args, NULL_TREE,
4649 (idk == CP_ID_KIND_QUALIFIED
4650 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4651 /*fn_p=*/NULL));
4652 else
4653 postfix_expression
4654 = finish_call_expr (postfix_expression, args,
4655 /*disallow_virtual=*/false,
4656 /*koenig_p=*/false);
4658 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4659 || TREE_CODE (postfix_expression) == MEMBER_REF
4660 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4661 postfix_expression = (build_offset_ref_call_from_tree
4662 (postfix_expression, args));
4663 else if (idk == CP_ID_KIND_QUALIFIED)
4664 /* A call to a static class member, or a namespace-scope
4665 function. */
4666 postfix_expression
4667 = finish_call_expr (postfix_expression, args,
4668 /*disallow_virtual=*/true,
4669 koenig_p);
4670 else
4671 /* All other function calls. */
4672 postfix_expression
4673 = finish_call_expr (postfix_expression, args,
4674 /*disallow_virtual=*/false,
4675 koenig_p);
4677 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4678 idk = CP_ID_KIND_NONE;
4680 break;
4682 case CPP_DOT:
4683 case CPP_DEREF:
4684 /* postfix-expression . template [opt] id-expression
4685 postfix-expression . pseudo-destructor-name
4686 postfix-expression -> template [opt] id-expression
4687 postfix-expression -> pseudo-destructor-name */
4689 /* Consume the `.' or `->' operator. */
4690 cp_lexer_consume_token (parser->lexer);
4692 postfix_expression
4693 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4694 postfix_expression,
4695 false, &idk);
4697 is_member_access = true;
4698 break;
4700 case CPP_PLUS_PLUS:
4701 /* postfix-expression ++ */
4702 /* Consume the `++' token. */
4703 cp_lexer_consume_token (parser->lexer);
4704 /* Generate a representation for the complete expression. */
4705 postfix_expression
4706 = finish_increment_expr (postfix_expression,
4707 POSTINCREMENT_EXPR);
4708 /* Increments may not appear in constant-expressions. */
4709 if (cp_parser_non_integral_constant_expression (parser,
4710 "an increment"))
4711 postfix_expression = error_mark_node;
4712 idk = CP_ID_KIND_NONE;
4713 is_member_access = false;
4714 break;
4716 case CPP_MINUS_MINUS:
4717 /* postfix-expression -- */
4718 /* Consume the `--' token. */
4719 cp_lexer_consume_token (parser->lexer);
4720 /* Generate a representation for the complete expression. */
4721 postfix_expression
4722 = finish_increment_expr (postfix_expression,
4723 POSTDECREMENT_EXPR);
4724 /* Decrements may not appear in constant-expressions. */
4725 if (cp_parser_non_integral_constant_expression (parser,
4726 "a decrement"))
4727 postfix_expression = error_mark_node;
4728 idk = CP_ID_KIND_NONE;
4729 is_member_access = false;
4730 break;
4732 default:
4733 if (member_access_only_p)
4734 return is_member_access? postfix_expression : error_mark_node;
4735 else
4736 return postfix_expression;
4740 /* We should never get here. */
4741 gcc_unreachable ();
4742 return error_mark_node;
4745 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4746 by cp_parser_builtin_offsetof. We're looking for
4748 postfix-expression [ expression ]
4750 FOR_OFFSETOF is set if we're being called in that context, which
4751 changes how we deal with integer constant expressions. */
4753 static tree
4754 cp_parser_postfix_open_square_expression (cp_parser *parser,
4755 tree postfix_expression,
4756 bool for_offsetof)
4758 tree index;
4760 /* Consume the `[' token. */
4761 cp_lexer_consume_token (parser->lexer);
4763 /* Parse the index expression. */
4764 /* ??? For offsetof, there is a question of what to allow here. If
4765 offsetof is not being used in an integral constant expression context,
4766 then we *could* get the right answer by computing the value at runtime.
4767 If we are in an integral constant expression context, then we might
4768 could accept any constant expression; hard to say without analysis.
4769 Rather than open the barn door too wide right away, allow only integer
4770 constant expressions here. */
4771 if (for_offsetof)
4772 index = cp_parser_constant_expression (parser, false, NULL);
4773 else
4774 index = cp_parser_expression (parser, /*cast_p=*/false);
4776 /* Look for the closing `]'. */
4777 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4779 /* Build the ARRAY_REF. */
4780 postfix_expression = grok_array_decl (postfix_expression, index);
4782 /* When not doing offsetof, array references are not permitted in
4783 constant-expressions. */
4784 if (!for_offsetof
4785 && (cp_parser_non_integral_constant_expression
4786 (parser, "an array reference")))
4787 postfix_expression = error_mark_node;
4789 return postfix_expression;
4792 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4793 by cp_parser_builtin_offsetof. We're looking for
4795 postfix-expression . template [opt] id-expression
4796 postfix-expression . pseudo-destructor-name
4797 postfix-expression -> template [opt] id-expression
4798 postfix-expression -> pseudo-destructor-name
4800 FOR_OFFSETOF is set if we're being called in that context. That sorta
4801 limits what of the above we'll actually accept, but nevermind.
4802 TOKEN_TYPE is the "." or "->" token, which will already have been
4803 removed from the stream. */
4805 static tree
4806 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4807 enum cpp_ttype token_type,
4808 tree postfix_expression,
4809 bool for_offsetof, cp_id_kind *idk)
4811 tree name;
4812 bool dependent_p;
4813 bool pseudo_destructor_p;
4814 tree scope = NULL_TREE;
4816 /* If this is a `->' operator, dereference the pointer. */
4817 if (token_type == CPP_DEREF)
4818 postfix_expression = build_x_arrow (postfix_expression);
4819 /* Check to see whether or not the expression is type-dependent. */
4820 dependent_p = type_dependent_expression_p (postfix_expression);
4821 /* The identifier following the `->' or `.' is not qualified. */
4822 parser->scope = NULL_TREE;
4823 parser->qualifying_scope = NULL_TREE;
4824 parser->object_scope = NULL_TREE;
4825 *idk = CP_ID_KIND_NONE;
4826 /* Enter the scope corresponding to the type of the object
4827 given by the POSTFIX_EXPRESSION. */
4828 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4830 scope = TREE_TYPE (postfix_expression);
4831 /* According to the standard, no expression should ever have
4832 reference type. Unfortunately, we do not currently match
4833 the standard in this respect in that our internal representation
4834 of an expression may have reference type even when the standard
4835 says it does not. Therefore, we have to manually obtain the
4836 underlying type here. */
4837 scope = non_reference (scope);
4838 /* The type of the POSTFIX_EXPRESSION must be complete. */
4839 if (scope == unknown_type_node)
4841 error ("%qE does not have class type", postfix_expression);
4842 scope = NULL_TREE;
4844 else
4845 scope = complete_type_or_else (scope, NULL_TREE);
4846 /* Let the name lookup machinery know that we are processing a
4847 class member access expression. */
4848 parser->context->object_type = scope;
4849 /* If something went wrong, we want to be able to discern that case,
4850 as opposed to the case where there was no SCOPE due to the type
4851 of expression being dependent. */
4852 if (!scope)
4853 scope = error_mark_node;
4854 /* If the SCOPE was erroneous, make the various semantic analysis
4855 functions exit quickly -- and without issuing additional error
4856 messages. */
4857 if (scope == error_mark_node)
4858 postfix_expression = error_mark_node;
4861 /* Assume this expression is not a pseudo-destructor access. */
4862 pseudo_destructor_p = false;
4864 /* If the SCOPE is a scalar type, then, if this is a valid program,
4865 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4866 is type dependent, it can be pseudo-destructor-name or something else.
4867 Try to parse it as pseudo-destructor-name first. */
4868 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4870 tree s;
4871 tree type;
4873 cp_parser_parse_tentatively (parser);
4874 /* Parse the pseudo-destructor-name. */
4875 s = NULL_TREE;
4876 cp_parser_pseudo_destructor_name (parser, &s, &type);
4877 if (dependent_p
4878 && (cp_parser_error_occurred (parser)
4879 || TREE_CODE (type) != TYPE_DECL
4880 || !SCALAR_TYPE_P (TREE_TYPE (type))))
4881 cp_parser_abort_tentative_parse (parser);
4882 else if (cp_parser_parse_definitely (parser))
4884 pseudo_destructor_p = true;
4885 postfix_expression
4886 = finish_pseudo_destructor_expr (postfix_expression,
4887 s, TREE_TYPE (type));
4891 if (!pseudo_destructor_p)
4893 /* If the SCOPE is not a scalar type, we are looking at an
4894 ordinary class member access expression, rather than a
4895 pseudo-destructor-name. */
4896 bool template_p;
4897 /* Parse the id-expression. */
4898 name = (cp_parser_id_expression
4899 (parser,
4900 cp_parser_optional_template_keyword (parser),
4901 /*check_dependency_p=*/true,
4902 &template_p,
4903 /*declarator_p=*/false,
4904 /*optional_p=*/false));
4905 /* In general, build a SCOPE_REF if the member name is qualified.
4906 However, if the name was not dependent and has already been
4907 resolved; there is no need to build the SCOPE_REF. For example;
4909 struct X { void f(); };
4910 template <typename T> void f(T* t) { t->X::f(); }
4912 Even though "t" is dependent, "X::f" is not and has been resolved
4913 to a BASELINK; there is no need to include scope information. */
4915 /* But we do need to remember that there was an explicit scope for
4916 virtual function calls. */
4917 if (parser->scope)
4918 *idk = CP_ID_KIND_QUALIFIED;
4920 /* If the name is a template-id that names a type, we will get a
4921 TYPE_DECL here. That is invalid code. */
4922 if (TREE_CODE (name) == TYPE_DECL)
4924 error ("invalid use of %qD", name);
4925 postfix_expression = error_mark_node;
4927 else
4929 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4931 name = build_qualified_name (/*type=*/NULL_TREE,
4932 parser->scope,
4933 name,
4934 template_p);
4935 parser->scope = NULL_TREE;
4936 parser->qualifying_scope = NULL_TREE;
4937 parser->object_scope = NULL_TREE;
4939 if (scope && name && BASELINK_P (name))
4940 adjust_result_of_qualified_name_lookup
4941 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4942 postfix_expression
4943 = finish_class_member_access_expr (postfix_expression, name,
4944 template_p);
4948 /* We no longer need to look up names in the scope of the object on
4949 the left-hand side of the `.' or `->' operator. */
4950 parser->context->object_type = NULL_TREE;
4952 /* Outside of offsetof, these operators may not appear in
4953 constant-expressions. */
4954 if (!for_offsetof
4955 && (cp_parser_non_integral_constant_expression
4956 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4957 postfix_expression = error_mark_node;
4959 return postfix_expression;
4962 /* Parse a parenthesized expression-list.
4964 expression-list:
4965 assignment-expression
4966 expression-list, assignment-expression
4968 attribute-list:
4969 expression-list
4970 identifier
4971 identifier, expression-list
4973 CAST_P is true if this expression is the target of a cast.
4975 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4976 argument pack.
4978 Returns a TREE_LIST. The TREE_VALUE of each node is a
4979 representation of an assignment-expression. Note that a TREE_LIST
4980 is returned even if there is only a single expression in the list.
4981 error_mark_node is returned if the ( and or ) are
4982 missing. NULL_TREE is returned on no expressions. The parentheses
4983 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4984 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4985 indicates whether or not all of the expressions in the list were
4986 constant. */
4988 static tree
4989 cp_parser_parenthesized_expression_list (cp_parser* parser,
4990 bool is_attribute_list,
4991 bool cast_p,
4992 bool allow_expansion_p,
4993 bool *non_constant_p)
4995 tree expression_list = NULL_TREE;
4996 bool fold_expr_p = is_attribute_list;
4997 tree identifier = NULL_TREE;
4998 bool saved_greater_than_is_operator_p;
5000 /* Assume all the expressions will be constant. */
5001 if (non_constant_p)
5002 *non_constant_p = false;
5004 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5005 return error_mark_node;
5007 /* Within a parenthesized expression, a `>' token is always
5008 the greater-than operator. */
5009 saved_greater_than_is_operator_p
5010 = parser->greater_than_is_operator_p;
5011 parser->greater_than_is_operator_p = true;
5013 /* Consume expressions until there are no more. */
5014 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5015 while (true)
5017 tree expr;
5019 /* At the beginning of attribute lists, check to see if the
5020 next token is an identifier. */
5021 if (is_attribute_list
5022 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5024 cp_token *token;
5026 /* Consume the identifier. */
5027 token = cp_lexer_consume_token (parser->lexer);
5028 /* Save the identifier. */
5029 identifier = token->u.value;
5031 else
5033 /* Parse the next assignment-expression. */
5034 if (non_constant_p)
5036 bool expr_non_constant_p;
5037 expr = (cp_parser_constant_expression
5038 (parser, /*allow_non_constant_p=*/true,
5039 &expr_non_constant_p));
5040 if (expr_non_constant_p)
5041 *non_constant_p = true;
5043 else
5044 expr = cp_parser_assignment_expression (parser, cast_p);
5046 if (fold_expr_p)
5047 expr = fold_non_dependent_expr (expr);
5049 /* If we have an ellipsis, then this is an expression
5050 expansion. */
5051 if (allow_expansion_p
5052 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5054 /* Consume the `...'. */
5055 cp_lexer_consume_token (parser->lexer);
5057 /* Build the argument pack. */
5058 expr = make_pack_expansion (expr);
5061 /* Add it to the list. We add error_mark_node
5062 expressions to the list, so that we can still tell if
5063 the correct form for a parenthesized expression-list
5064 is found. That gives better errors. */
5065 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5067 if (expr == error_mark_node)
5068 goto skip_comma;
5071 /* After the first item, attribute lists look the same as
5072 expression lists. */
5073 is_attribute_list = false;
5075 get_comma:;
5076 /* If the next token isn't a `,', then we are done. */
5077 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5078 break;
5080 /* Otherwise, consume the `,' and keep going. */
5081 cp_lexer_consume_token (parser->lexer);
5084 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5086 int ending;
5088 skip_comma:;
5089 /* We try and resync to an unnested comma, as that will give the
5090 user better diagnostics. */
5091 ending = cp_parser_skip_to_closing_parenthesis (parser,
5092 /*recovering=*/true,
5093 /*or_comma=*/true,
5094 /*consume_paren=*/true);
5095 if (ending < 0)
5096 goto get_comma;
5097 if (!ending)
5099 parser->greater_than_is_operator_p
5100 = saved_greater_than_is_operator_p;
5101 return error_mark_node;
5105 parser->greater_than_is_operator_p
5106 = saved_greater_than_is_operator_p;
5108 /* We built up the list in reverse order so we must reverse it now. */
5109 expression_list = nreverse (expression_list);
5110 if (identifier)
5111 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5113 return expression_list;
5116 /* Parse a pseudo-destructor-name.
5118 pseudo-destructor-name:
5119 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5120 :: [opt] nested-name-specifier template template-id :: ~ type-name
5121 :: [opt] nested-name-specifier [opt] ~ type-name
5123 If either of the first two productions is used, sets *SCOPE to the
5124 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5125 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5126 or ERROR_MARK_NODE if the parse fails. */
5128 static void
5129 cp_parser_pseudo_destructor_name (cp_parser* parser,
5130 tree* scope,
5131 tree* type)
5133 bool nested_name_specifier_p;
5135 /* Assume that things will not work out. */
5136 *type = error_mark_node;
5138 /* Look for the optional `::' operator. */
5139 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5140 /* Look for the optional nested-name-specifier. */
5141 nested_name_specifier_p
5142 = (cp_parser_nested_name_specifier_opt (parser,
5143 /*typename_keyword_p=*/false,
5144 /*check_dependency_p=*/true,
5145 /*type_p=*/false,
5146 /*is_declaration=*/true)
5147 != NULL_TREE);
5148 /* Now, if we saw a nested-name-specifier, we might be doing the
5149 second production. */
5150 if (nested_name_specifier_p
5151 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5153 /* Consume the `template' keyword. */
5154 cp_lexer_consume_token (parser->lexer);
5155 /* Parse the template-id. */
5156 cp_parser_template_id (parser,
5157 /*template_keyword_p=*/true,
5158 /*check_dependency_p=*/false,
5159 /*is_declaration=*/true);
5160 /* Look for the `::' token. */
5161 cp_parser_require (parser, CPP_SCOPE, "`::'");
5163 /* If the next token is not a `~', then there might be some
5164 additional qualification. */
5165 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5167 /* At this point, we're looking for "type-name :: ~". The type-name
5168 must not be a class-name, since this is a pseudo-destructor. So,
5169 it must be either an enum-name, or a typedef-name -- both of which
5170 are just identifiers. So, we peek ahead to check that the "::"
5171 and "~" tokens are present; if they are not, then we can avoid
5172 calling type_name. */
5173 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5174 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5175 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5177 cp_parser_error (parser, "non-scalar type");
5178 return;
5181 /* Look for the type-name. */
5182 *scope = TREE_TYPE (cp_parser_type_name (parser));
5184 if (*scope == error_mark_node)
5185 return;
5187 /* Look for the `::' token. */
5188 cp_parser_require (parser, CPP_SCOPE, "`::'");
5190 else
5191 *scope = NULL_TREE;
5193 /* Look for the `~'. */
5194 cp_parser_require (parser, CPP_COMPL, "`~'");
5195 /* Look for the type-name again. We are not responsible for
5196 checking that it matches the first type-name. */
5197 *type = cp_parser_type_name (parser);
5200 /* Parse a unary-expression.
5202 unary-expression:
5203 postfix-expression
5204 ++ cast-expression
5205 -- cast-expression
5206 unary-operator cast-expression
5207 sizeof unary-expression
5208 sizeof ( type-id )
5209 new-expression
5210 delete-expression
5212 GNU Extensions:
5214 unary-expression:
5215 __extension__ cast-expression
5216 __alignof__ unary-expression
5217 __alignof__ ( type-id )
5218 __real__ cast-expression
5219 __imag__ cast-expression
5220 && identifier
5222 ADDRESS_P is true iff the unary-expression is appearing as the
5223 operand of the `&' operator. CAST_P is true if this expression is
5224 the target of a cast.
5226 Returns a representation of the expression. */
5228 static tree
5229 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5231 cp_token *token;
5232 enum tree_code unary_operator;
5234 /* Peek at the next token. */
5235 token = cp_lexer_peek_token (parser->lexer);
5236 /* Some keywords give away the kind of expression. */
5237 if (token->type == CPP_KEYWORD)
5239 enum rid keyword = token->keyword;
5241 switch (keyword)
5243 case RID_ALIGNOF:
5244 case RID_SIZEOF:
5246 tree operand;
5247 enum tree_code op;
5249 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5250 /* Consume the token. */
5251 cp_lexer_consume_token (parser->lexer);
5252 /* Parse the operand. */
5253 operand = cp_parser_sizeof_operand (parser, keyword);
5255 if (TYPE_P (operand))
5256 return cxx_sizeof_or_alignof_type (operand, op, true);
5257 else
5258 return cxx_sizeof_or_alignof_expr (operand, op);
5261 case RID_NEW:
5262 return cp_parser_new_expression (parser);
5264 case RID_DELETE:
5265 return cp_parser_delete_expression (parser);
5267 case RID_EXTENSION:
5269 /* The saved value of the PEDANTIC flag. */
5270 int saved_pedantic;
5271 tree expr;
5273 /* Save away the PEDANTIC flag. */
5274 cp_parser_extension_opt (parser, &saved_pedantic);
5275 /* Parse the cast-expression. */
5276 expr = cp_parser_simple_cast_expression (parser);
5277 /* Restore the PEDANTIC flag. */
5278 pedantic = saved_pedantic;
5280 return expr;
5283 case RID_REALPART:
5284 case RID_IMAGPART:
5286 tree expression;
5288 /* Consume the `__real__' or `__imag__' token. */
5289 cp_lexer_consume_token (parser->lexer);
5290 /* Parse the cast-expression. */
5291 expression = cp_parser_simple_cast_expression (parser);
5292 /* Create the complete representation. */
5293 return build_x_unary_op ((keyword == RID_REALPART
5294 ? REALPART_EXPR : IMAGPART_EXPR),
5295 expression);
5297 break;
5299 default:
5300 break;
5304 /* Look for the `:: new' and `:: delete', which also signal the
5305 beginning of a new-expression, or delete-expression,
5306 respectively. If the next token is `::', then it might be one of
5307 these. */
5308 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5310 enum rid keyword;
5312 /* See if the token after the `::' is one of the keywords in
5313 which we're interested. */
5314 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5315 /* If it's `new', we have a new-expression. */
5316 if (keyword == RID_NEW)
5317 return cp_parser_new_expression (parser);
5318 /* Similarly, for `delete'. */
5319 else if (keyword == RID_DELETE)
5320 return cp_parser_delete_expression (parser);
5323 /* Look for a unary operator. */
5324 unary_operator = cp_parser_unary_operator (token);
5325 /* The `++' and `--' operators can be handled similarly, even though
5326 they are not technically unary-operators in the grammar. */
5327 if (unary_operator == ERROR_MARK)
5329 if (token->type == CPP_PLUS_PLUS)
5330 unary_operator = PREINCREMENT_EXPR;
5331 else if (token->type == CPP_MINUS_MINUS)
5332 unary_operator = PREDECREMENT_EXPR;
5333 /* Handle the GNU address-of-label extension. */
5334 else if (cp_parser_allow_gnu_extensions_p (parser)
5335 && token->type == CPP_AND_AND)
5337 tree identifier;
5338 tree expression;
5340 /* Consume the '&&' token. */
5341 cp_lexer_consume_token (parser->lexer);
5342 /* Look for the identifier. */
5343 identifier = cp_parser_identifier (parser);
5344 /* Create an expression representing the address. */
5345 expression = finish_label_address_expr (identifier);
5346 if (cp_parser_non_integral_constant_expression (parser,
5347 "the address of a label"))
5348 expression = error_mark_node;
5349 return expression;
5352 if (unary_operator != ERROR_MARK)
5354 tree cast_expression;
5355 tree expression = error_mark_node;
5356 const char *non_constant_p = NULL;
5358 /* Consume the operator token. */
5359 token = cp_lexer_consume_token (parser->lexer);
5360 /* Parse the cast-expression. */
5361 cast_expression
5362 = cp_parser_cast_expression (parser,
5363 unary_operator == ADDR_EXPR,
5364 /*cast_p=*/false);
5365 /* Now, build an appropriate representation. */
5366 switch (unary_operator)
5368 case INDIRECT_REF:
5369 non_constant_p = "`*'";
5370 expression = build_x_indirect_ref (cast_expression, "unary *");
5371 break;
5373 case ADDR_EXPR:
5374 non_constant_p = "`&'";
5375 /* Fall through. */
5376 case BIT_NOT_EXPR:
5377 expression = build_x_unary_op (unary_operator, cast_expression);
5378 break;
5380 case PREINCREMENT_EXPR:
5381 case PREDECREMENT_EXPR:
5382 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5383 ? "`++'" : "`--'");
5384 /* Fall through. */
5385 case UNARY_PLUS_EXPR:
5386 case NEGATE_EXPR:
5387 case TRUTH_NOT_EXPR:
5388 expression = finish_unary_op_expr (unary_operator, cast_expression);
5389 break;
5391 default:
5392 gcc_unreachable ();
5395 if (non_constant_p
5396 && cp_parser_non_integral_constant_expression (parser,
5397 non_constant_p))
5398 expression = error_mark_node;
5400 return expression;
5403 return cp_parser_postfix_expression (parser, address_p, cast_p,
5404 /*member_access_only_p=*/false);
5407 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5408 unary-operator, the corresponding tree code is returned. */
5410 static enum tree_code
5411 cp_parser_unary_operator (cp_token* token)
5413 switch (token->type)
5415 case CPP_MULT:
5416 return INDIRECT_REF;
5418 case CPP_AND:
5419 return ADDR_EXPR;
5421 case CPP_PLUS:
5422 return UNARY_PLUS_EXPR;
5424 case CPP_MINUS:
5425 return NEGATE_EXPR;
5427 case CPP_NOT:
5428 return TRUTH_NOT_EXPR;
5430 case CPP_COMPL:
5431 return BIT_NOT_EXPR;
5433 default:
5434 return ERROR_MARK;
5438 /* Parse a new-expression.
5440 new-expression:
5441 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5442 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5444 Returns a representation of the expression. */
5446 static tree
5447 cp_parser_new_expression (cp_parser* parser)
5449 bool global_scope_p;
5450 tree placement;
5451 tree type;
5452 tree initializer;
5453 tree nelts;
5455 /* Look for the optional `::' operator. */
5456 global_scope_p
5457 = (cp_parser_global_scope_opt (parser,
5458 /*current_scope_valid_p=*/false)
5459 != NULL_TREE);
5460 /* Look for the `new' operator. */
5461 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5462 /* There's no easy way to tell a new-placement from the
5463 `( type-id )' construct. */
5464 cp_parser_parse_tentatively (parser);
5465 /* Look for a new-placement. */
5466 placement = cp_parser_new_placement (parser);
5467 /* If that didn't work out, there's no new-placement. */
5468 if (!cp_parser_parse_definitely (parser))
5469 placement = NULL_TREE;
5471 /* If the next token is a `(', then we have a parenthesized
5472 type-id. */
5473 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5475 /* Consume the `('. */
5476 cp_lexer_consume_token (parser->lexer);
5477 /* Parse the type-id. */
5478 type = cp_parser_type_id (parser);
5479 /* Look for the closing `)'. */
5480 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5481 /* There should not be a direct-new-declarator in this production,
5482 but GCC used to allowed this, so we check and emit a sensible error
5483 message for this case. */
5484 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5486 error ("array bound forbidden after parenthesized type-id");
5487 inform ("try removing the parentheses around the type-id");
5488 cp_parser_direct_new_declarator (parser);
5490 nelts = NULL_TREE;
5492 /* Otherwise, there must be a new-type-id. */
5493 else
5494 type = cp_parser_new_type_id (parser, &nelts);
5496 /* If the next token is a `(', then we have a new-initializer. */
5497 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5498 initializer = cp_parser_new_initializer (parser);
5499 else
5500 initializer = NULL_TREE;
5502 /* A new-expression may not appear in an integral constant
5503 expression. */
5504 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5505 return error_mark_node;
5507 /* Create a representation of the new-expression. */
5508 return build_new (placement, type, nelts, initializer, global_scope_p);
5511 /* Parse a new-placement.
5513 new-placement:
5514 ( expression-list )
5516 Returns the same representation as for an expression-list. */
5518 static tree
5519 cp_parser_new_placement (cp_parser* parser)
5521 tree expression_list;
5523 /* Parse the expression-list. */
5524 expression_list = (cp_parser_parenthesized_expression_list
5525 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5526 /*non_constant_p=*/NULL));
5528 return expression_list;
5531 /* Parse a new-type-id.
5533 new-type-id:
5534 type-specifier-seq new-declarator [opt]
5536 Returns the TYPE allocated. If the new-type-id indicates an array
5537 type, *NELTS is set to the number of elements in the last array
5538 bound; the TYPE will not include the last array bound. */
5540 static tree
5541 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5543 cp_decl_specifier_seq type_specifier_seq;
5544 cp_declarator *new_declarator;
5545 cp_declarator *declarator;
5546 cp_declarator *outer_declarator;
5547 const char *saved_message;
5548 tree type;
5550 /* The type-specifier sequence must not contain type definitions.
5551 (It cannot contain declarations of new types either, but if they
5552 are not definitions we will catch that because they are not
5553 complete.) */
5554 saved_message = parser->type_definition_forbidden_message;
5555 parser->type_definition_forbidden_message
5556 = "types may not be defined in a new-type-id";
5557 /* Parse the type-specifier-seq. */
5558 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5559 &type_specifier_seq);
5560 /* Restore the old message. */
5561 parser->type_definition_forbidden_message = saved_message;
5562 /* Parse the new-declarator. */
5563 new_declarator = cp_parser_new_declarator_opt (parser);
5565 /* Determine the number of elements in the last array dimension, if
5566 any. */
5567 *nelts = NULL_TREE;
5568 /* Skip down to the last array dimension. */
5569 declarator = new_declarator;
5570 outer_declarator = NULL;
5571 while (declarator && (declarator->kind == cdk_pointer
5572 || declarator->kind == cdk_ptrmem))
5574 outer_declarator = declarator;
5575 declarator = declarator->declarator;
5577 while (declarator
5578 && declarator->kind == cdk_array
5579 && declarator->declarator
5580 && declarator->declarator->kind == cdk_array)
5582 outer_declarator = declarator;
5583 declarator = declarator->declarator;
5586 if (declarator && declarator->kind == cdk_array)
5588 *nelts = declarator->u.array.bounds;
5589 if (*nelts == error_mark_node)
5590 *nelts = integer_one_node;
5592 if (outer_declarator)
5593 outer_declarator->declarator = declarator->declarator;
5594 else
5595 new_declarator = NULL;
5598 type = groktypename (&type_specifier_seq, new_declarator);
5599 return type;
5602 /* Parse an (optional) new-declarator.
5604 new-declarator:
5605 ptr-operator new-declarator [opt]
5606 direct-new-declarator
5608 Returns the declarator. */
5610 static cp_declarator *
5611 cp_parser_new_declarator_opt (cp_parser* parser)
5613 enum tree_code code;
5614 tree type;
5615 cp_cv_quals cv_quals;
5617 /* We don't know if there's a ptr-operator next, or not. */
5618 cp_parser_parse_tentatively (parser);
5619 /* Look for a ptr-operator. */
5620 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5621 /* If that worked, look for more new-declarators. */
5622 if (cp_parser_parse_definitely (parser))
5624 cp_declarator *declarator;
5626 /* Parse another optional declarator. */
5627 declarator = cp_parser_new_declarator_opt (parser);
5629 return cp_parser_make_indirect_declarator
5630 (code, type, cv_quals, declarator);
5633 /* If the next token is a `[', there is a direct-new-declarator. */
5634 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5635 return cp_parser_direct_new_declarator (parser);
5637 return NULL;
5640 /* Parse a direct-new-declarator.
5642 direct-new-declarator:
5643 [ expression ]
5644 direct-new-declarator [constant-expression]
5648 static cp_declarator *
5649 cp_parser_direct_new_declarator (cp_parser* parser)
5651 cp_declarator *declarator = NULL;
5653 while (true)
5655 tree expression;
5657 /* Look for the opening `['. */
5658 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5659 /* The first expression is not required to be constant. */
5660 if (!declarator)
5662 expression = cp_parser_expression (parser, /*cast_p=*/false);
5663 /* The standard requires that the expression have integral
5664 type. DR 74 adds enumeration types. We believe that the
5665 real intent is that these expressions be handled like the
5666 expression in a `switch' condition, which also allows
5667 classes with a single conversion to integral or
5668 enumeration type. */
5669 if (!processing_template_decl)
5671 expression
5672 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5673 expression,
5674 /*complain=*/true);
5675 if (!expression)
5677 error ("expression in new-declarator must have integral "
5678 "or enumeration type");
5679 expression = error_mark_node;
5683 /* But all the other expressions must be. */
5684 else
5685 expression
5686 = cp_parser_constant_expression (parser,
5687 /*allow_non_constant=*/false,
5688 NULL);
5689 /* Look for the closing `]'. */
5690 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5692 /* Add this bound to the declarator. */
5693 declarator = make_array_declarator (declarator, expression);
5695 /* If the next token is not a `[', then there are no more
5696 bounds. */
5697 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5698 break;
5701 return declarator;
5704 /* Parse a new-initializer.
5706 new-initializer:
5707 ( expression-list [opt] )
5709 Returns a representation of the expression-list. If there is no
5710 expression-list, VOID_ZERO_NODE is returned. */
5712 static tree
5713 cp_parser_new_initializer (cp_parser* parser)
5715 tree expression_list;
5717 expression_list = (cp_parser_parenthesized_expression_list
5718 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5719 /*non_constant_p=*/NULL));
5720 if (!expression_list)
5721 expression_list = void_zero_node;
5723 return expression_list;
5726 /* Parse a delete-expression.
5728 delete-expression:
5729 :: [opt] delete cast-expression
5730 :: [opt] delete [ ] cast-expression
5732 Returns a representation of the expression. */
5734 static tree
5735 cp_parser_delete_expression (cp_parser* parser)
5737 bool global_scope_p;
5738 bool array_p;
5739 tree expression;
5741 /* Look for the optional `::' operator. */
5742 global_scope_p
5743 = (cp_parser_global_scope_opt (parser,
5744 /*current_scope_valid_p=*/false)
5745 != NULL_TREE);
5746 /* Look for the `delete' keyword. */
5747 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5748 /* See if the array syntax is in use. */
5749 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5751 /* Consume the `[' token. */
5752 cp_lexer_consume_token (parser->lexer);
5753 /* Look for the `]' token. */
5754 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5755 /* Remember that this is the `[]' construct. */
5756 array_p = true;
5758 else
5759 array_p = false;
5761 /* Parse the cast-expression. */
5762 expression = cp_parser_simple_cast_expression (parser);
5764 /* A delete-expression may not appear in an integral constant
5765 expression. */
5766 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5767 return error_mark_node;
5769 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5772 /* Parse a cast-expression.
5774 cast-expression:
5775 unary-expression
5776 ( type-id ) cast-expression
5778 ADDRESS_P is true iff the unary-expression is appearing as the
5779 operand of the `&' operator. CAST_P is true if this expression is
5780 the target of a cast.
5782 Returns a representation of the expression. */
5784 static tree
5785 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5787 /* If it's a `(', then we might be looking at a cast. */
5788 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5790 tree type = NULL_TREE;
5791 tree expr = NULL_TREE;
5792 bool compound_literal_p;
5793 const char *saved_message;
5795 /* There's no way to know yet whether or not this is a cast.
5796 For example, `(int (3))' is a unary-expression, while `(int)
5797 3' is a cast. So, we resort to parsing tentatively. */
5798 cp_parser_parse_tentatively (parser);
5799 /* Types may not be defined in a cast. */
5800 saved_message = parser->type_definition_forbidden_message;
5801 parser->type_definition_forbidden_message
5802 = "types may not be defined in casts";
5803 /* Consume the `('. */
5804 cp_lexer_consume_token (parser->lexer);
5805 /* A very tricky bit is that `(struct S) { 3 }' is a
5806 compound-literal (which we permit in C++ as an extension).
5807 But, that construct is not a cast-expression -- it is a
5808 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5809 is legal; if the compound-literal were a cast-expression,
5810 you'd need an extra set of parentheses.) But, if we parse
5811 the type-id, and it happens to be a class-specifier, then we
5812 will commit to the parse at that point, because we cannot
5813 undo the action that is done when creating a new class. So,
5814 then we cannot back up and do a postfix-expression.
5816 Therefore, we scan ahead to the closing `)', and check to see
5817 if the token after the `)' is a `{'. If so, we are not
5818 looking at a cast-expression.
5820 Save tokens so that we can put them back. */
5821 cp_lexer_save_tokens (parser->lexer);
5822 /* Skip tokens until the next token is a closing parenthesis.
5823 If we find the closing `)', and the next token is a `{', then
5824 we are looking at a compound-literal. */
5825 compound_literal_p
5826 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5827 /*consume_paren=*/true)
5828 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5829 /* Roll back the tokens we skipped. */
5830 cp_lexer_rollback_tokens (parser->lexer);
5831 /* If we were looking at a compound-literal, simulate an error
5832 so that the call to cp_parser_parse_definitely below will
5833 fail. */
5834 if (compound_literal_p)
5835 cp_parser_simulate_error (parser);
5836 else
5838 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5839 parser->in_type_id_in_expr_p = true;
5840 /* Look for the type-id. */
5841 type = cp_parser_type_id (parser);
5842 /* Look for the closing `)'. */
5843 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5844 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5847 /* Restore the saved message. */
5848 parser->type_definition_forbidden_message = saved_message;
5850 /* If ok so far, parse the dependent expression. We cannot be
5851 sure it is a cast. Consider `(T ())'. It is a parenthesized
5852 ctor of T, but looks like a cast to function returning T
5853 without a dependent expression. */
5854 if (!cp_parser_error_occurred (parser))
5855 expr = cp_parser_cast_expression (parser,
5856 /*address_p=*/false,
5857 /*cast_p=*/true);
5859 if (cp_parser_parse_definitely (parser))
5861 /* Warn about old-style casts, if so requested. */
5862 if (warn_old_style_cast
5863 && !in_system_header
5864 && !VOID_TYPE_P (type)
5865 && current_lang_name != lang_name_c)
5866 warning (OPT_Wold_style_cast, "use of old-style cast");
5868 /* Only type conversions to integral or enumeration types
5869 can be used in constant-expressions. */
5870 if (!cast_valid_in_integral_constant_expression_p (type)
5871 && (cp_parser_non_integral_constant_expression
5872 (parser,
5873 "a cast to a type other than an integral or "
5874 "enumeration type")))
5875 return error_mark_node;
5877 /* Perform the cast. */
5878 expr = build_c_cast (type, expr);
5879 return expr;
5883 /* If we get here, then it's not a cast, so it must be a
5884 unary-expression. */
5885 return cp_parser_unary_expression (parser, address_p, cast_p);
5888 /* Parse a binary expression of the general form:
5890 pm-expression:
5891 cast-expression
5892 pm-expression .* cast-expression
5893 pm-expression ->* cast-expression
5895 multiplicative-expression:
5896 pm-expression
5897 multiplicative-expression * pm-expression
5898 multiplicative-expression / pm-expression
5899 multiplicative-expression % pm-expression
5901 additive-expression:
5902 multiplicative-expression
5903 additive-expression + multiplicative-expression
5904 additive-expression - multiplicative-expression
5906 shift-expression:
5907 additive-expression
5908 shift-expression << additive-expression
5909 shift-expression >> additive-expression
5911 relational-expression:
5912 shift-expression
5913 relational-expression < shift-expression
5914 relational-expression > shift-expression
5915 relational-expression <= shift-expression
5916 relational-expression >= shift-expression
5918 GNU Extension:
5920 relational-expression:
5921 relational-expression <? shift-expression
5922 relational-expression >? shift-expression
5924 equality-expression:
5925 relational-expression
5926 equality-expression == relational-expression
5927 equality-expression != relational-expression
5929 and-expression:
5930 equality-expression
5931 and-expression & equality-expression
5933 exclusive-or-expression:
5934 and-expression
5935 exclusive-or-expression ^ and-expression
5937 inclusive-or-expression:
5938 exclusive-or-expression
5939 inclusive-or-expression | exclusive-or-expression
5941 logical-and-expression:
5942 inclusive-or-expression
5943 logical-and-expression && inclusive-or-expression
5945 logical-or-expression:
5946 logical-and-expression
5947 logical-or-expression || logical-and-expression
5949 All these are implemented with a single function like:
5951 binary-expression:
5952 simple-cast-expression
5953 binary-expression <token> binary-expression
5955 CAST_P is true if this expression is the target of a cast.
5957 The binops_by_token map is used to get the tree codes for each <token> type.
5958 binary-expressions are associated according to a precedence table. */
5960 #define TOKEN_PRECEDENCE(token) \
5961 (((token->type == CPP_GREATER \
5962 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5963 && !parser->greater_than_is_operator_p) \
5964 ? PREC_NOT_OPERATOR \
5965 : binops_by_token[token->type].prec)
5967 static tree
5968 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5970 cp_parser_expression_stack stack;
5971 cp_parser_expression_stack_entry *sp = &stack[0];
5972 tree lhs, rhs;
5973 cp_token *token;
5974 enum tree_code tree_type, lhs_type, rhs_type;
5975 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5976 bool overloaded_p;
5978 /* Parse the first expression. */
5979 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5980 lhs_type = ERROR_MARK;
5982 for (;;)
5984 /* Get an operator token. */
5985 token = cp_lexer_peek_token (parser->lexer);
5987 if (warn_cxx0x_compat
5988 && token->type == CPP_RSHIFT
5989 && !parser->greater_than_is_operator_p)
5991 warning (OPT_Wc__0x_compat,
5992 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5993 &token->location);
5994 warning (OPT_Wc__0x_compat,
5995 "suggest parentheses around %<>>%> expression");
5998 new_prec = TOKEN_PRECEDENCE (token);
6000 /* Popping an entry off the stack means we completed a subexpression:
6001 - either we found a token which is not an operator (`>' where it is not
6002 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6003 will happen repeatedly;
6004 - or, we found an operator which has lower priority. This is the case
6005 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6006 parsing `3 * 4'. */
6007 if (new_prec <= prec)
6009 if (sp == stack)
6010 break;
6011 else
6012 goto pop;
6015 get_rhs:
6016 tree_type = binops_by_token[token->type].tree_type;
6018 /* We used the operator token. */
6019 cp_lexer_consume_token (parser->lexer);
6021 /* Extract another operand. It may be the RHS of this expression
6022 or the LHS of a new, higher priority expression. */
6023 rhs = cp_parser_simple_cast_expression (parser);
6024 rhs_type = ERROR_MARK;
6026 /* Get another operator token. Look up its precedence to avoid
6027 building a useless (immediately popped) stack entry for common
6028 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6029 token = cp_lexer_peek_token (parser->lexer);
6030 lookahead_prec = TOKEN_PRECEDENCE (token);
6031 if (lookahead_prec > new_prec)
6033 /* ... and prepare to parse the RHS of the new, higher priority
6034 expression. Since precedence levels on the stack are
6035 monotonically increasing, we do not have to care about
6036 stack overflows. */
6037 sp->prec = prec;
6038 sp->tree_type = tree_type;
6039 sp->lhs = lhs;
6040 sp->lhs_type = lhs_type;
6041 sp++;
6042 lhs = rhs;
6043 lhs_type = rhs_type;
6044 prec = new_prec;
6045 new_prec = lookahead_prec;
6046 goto get_rhs;
6048 pop:
6049 /* If the stack is not empty, we have parsed into LHS the right side
6050 (`4' in the example above) of an expression we had suspended.
6051 We can use the information on the stack to recover the LHS (`3')
6052 from the stack together with the tree code (`MULT_EXPR'), and
6053 the precedence of the higher level subexpression
6054 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6055 which will be used to actually build the additive expression. */
6056 --sp;
6057 prec = sp->prec;
6058 tree_type = sp->tree_type;
6059 rhs = lhs;
6060 rhs_type = lhs_type;
6061 lhs = sp->lhs;
6062 lhs_type = sp->lhs_type;
6065 overloaded_p = false;
6066 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6067 &overloaded_p);
6068 lhs_type = tree_type;
6070 /* If the binary operator required the use of an overloaded operator,
6071 then this expression cannot be an integral constant-expression.
6072 An overloaded operator can be used even if both operands are
6073 otherwise permissible in an integral constant-expression if at
6074 least one of the operands is of enumeration type. */
6076 if (overloaded_p
6077 && (cp_parser_non_integral_constant_expression
6078 (parser, "calls to overloaded operators")))
6079 return error_mark_node;
6082 return lhs;
6086 /* Parse the `? expression : assignment-expression' part of a
6087 conditional-expression. The LOGICAL_OR_EXPR is the
6088 logical-or-expression that started the conditional-expression.
6089 Returns a representation of the entire conditional-expression.
6091 This routine is used by cp_parser_assignment_expression.
6093 ? expression : assignment-expression
6095 GNU Extensions:
6097 ? : assignment-expression */
6099 static tree
6100 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6102 tree expr;
6103 tree assignment_expr;
6105 /* Consume the `?' token. */
6106 cp_lexer_consume_token (parser->lexer);
6107 if (cp_parser_allow_gnu_extensions_p (parser)
6108 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6109 /* Implicit true clause. */
6110 expr = NULL_TREE;
6111 else
6112 /* Parse the expression. */
6113 expr = cp_parser_expression (parser, /*cast_p=*/false);
6115 /* The next token should be a `:'. */
6116 cp_parser_require (parser, CPP_COLON, "`:'");
6117 /* Parse the assignment-expression. */
6118 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6120 /* Build the conditional-expression. */
6121 return build_x_conditional_expr (logical_or_expr,
6122 expr,
6123 assignment_expr);
6126 /* Parse an assignment-expression.
6128 assignment-expression:
6129 conditional-expression
6130 logical-or-expression assignment-operator assignment_expression
6131 throw-expression
6133 CAST_P is true if this expression is the target of a cast.
6135 Returns a representation for the expression. */
6137 static tree
6138 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6140 tree expr;
6142 /* If the next token is the `throw' keyword, then we're looking at
6143 a throw-expression. */
6144 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6145 expr = cp_parser_throw_expression (parser);
6146 /* Otherwise, it must be that we are looking at a
6147 logical-or-expression. */
6148 else
6150 /* Parse the binary expressions (logical-or-expression). */
6151 expr = cp_parser_binary_expression (parser, cast_p);
6152 /* If the next token is a `?' then we're actually looking at a
6153 conditional-expression. */
6154 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6155 return cp_parser_question_colon_clause (parser, expr);
6156 else
6158 enum tree_code assignment_operator;
6160 /* If it's an assignment-operator, we're using the second
6161 production. */
6162 assignment_operator
6163 = cp_parser_assignment_operator_opt (parser);
6164 if (assignment_operator != ERROR_MARK)
6166 tree rhs;
6168 /* Parse the right-hand side of the assignment. */
6169 rhs = cp_parser_assignment_expression (parser, cast_p);
6170 /* An assignment may not appear in a
6171 constant-expression. */
6172 if (cp_parser_non_integral_constant_expression (parser,
6173 "an assignment"))
6174 return error_mark_node;
6175 /* Build the assignment expression. */
6176 expr = build_x_modify_expr (expr,
6177 assignment_operator,
6178 rhs);
6183 return expr;
6186 /* Parse an (optional) assignment-operator.
6188 assignment-operator: one of
6189 = *= /= %= += -= >>= <<= &= ^= |=
6191 GNU Extension:
6193 assignment-operator: one of
6194 <?= >?=
6196 If the next token is an assignment operator, the corresponding tree
6197 code is returned, and the token is consumed. For example, for
6198 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6199 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6200 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6201 operator, ERROR_MARK is returned. */
6203 static enum tree_code
6204 cp_parser_assignment_operator_opt (cp_parser* parser)
6206 enum tree_code op;
6207 cp_token *token;
6209 /* Peek at the next toen. */
6210 token = cp_lexer_peek_token (parser->lexer);
6212 switch (token->type)
6214 case CPP_EQ:
6215 op = NOP_EXPR;
6216 break;
6218 case CPP_MULT_EQ:
6219 op = MULT_EXPR;
6220 break;
6222 case CPP_DIV_EQ:
6223 op = TRUNC_DIV_EXPR;
6224 break;
6226 case CPP_MOD_EQ:
6227 op = TRUNC_MOD_EXPR;
6228 break;
6230 case CPP_PLUS_EQ:
6231 op = PLUS_EXPR;
6232 break;
6234 case CPP_MINUS_EQ:
6235 op = MINUS_EXPR;
6236 break;
6238 case CPP_RSHIFT_EQ:
6239 op = RSHIFT_EXPR;
6240 break;
6242 case CPP_LSHIFT_EQ:
6243 op = LSHIFT_EXPR;
6244 break;
6246 case CPP_AND_EQ:
6247 op = BIT_AND_EXPR;
6248 break;
6250 case CPP_XOR_EQ:
6251 op = BIT_XOR_EXPR;
6252 break;
6254 case CPP_OR_EQ:
6255 op = BIT_IOR_EXPR;
6256 break;
6258 default:
6259 /* Nothing else is an assignment operator. */
6260 op = ERROR_MARK;
6263 /* If it was an assignment operator, consume it. */
6264 if (op != ERROR_MARK)
6265 cp_lexer_consume_token (parser->lexer);
6267 return op;
6270 /* Parse an expression.
6272 expression:
6273 assignment-expression
6274 expression , assignment-expression
6276 CAST_P is true if this expression is the target of a cast.
6278 Returns a representation of the expression. */
6280 static tree
6281 cp_parser_expression (cp_parser* parser, bool cast_p)
6283 tree expression = NULL_TREE;
6285 while (true)
6287 tree assignment_expression;
6289 /* Parse the next assignment-expression. */
6290 assignment_expression
6291 = cp_parser_assignment_expression (parser, cast_p);
6292 /* If this is the first assignment-expression, we can just
6293 save it away. */
6294 if (!expression)
6295 expression = assignment_expression;
6296 else
6297 expression = build_x_compound_expr (expression,
6298 assignment_expression);
6299 /* If the next token is not a comma, then we are done with the
6300 expression. */
6301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6302 break;
6303 /* Consume the `,'. */
6304 cp_lexer_consume_token (parser->lexer);
6305 /* A comma operator cannot appear in a constant-expression. */
6306 if (cp_parser_non_integral_constant_expression (parser,
6307 "a comma operator"))
6308 expression = error_mark_node;
6311 return expression;
6314 /* Parse a constant-expression.
6316 constant-expression:
6317 conditional-expression
6319 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6320 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6321 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6322 is false, NON_CONSTANT_P should be NULL. */
6324 static tree
6325 cp_parser_constant_expression (cp_parser* parser,
6326 bool allow_non_constant_p,
6327 bool *non_constant_p)
6329 bool saved_integral_constant_expression_p;
6330 bool saved_allow_non_integral_constant_expression_p;
6331 bool saved_non_integral_constant_expression_p;
6332 tree expression;
6334 /* It might seem that we could simply parse the
6335 conditional-expression, and then check to see if it were
6336 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6337 one that the compiler can figure out is constant, possibly after
6338 doing some simplifications or optimizations. The standard has a
6339 precise definition of constant-expression, and we must honor
6340 that, even though it is somewhat more restrictive.
6342 For example:
6344 int i[(2, 3)];
6346 is not a legal declaration, because `(2, 3)' is not a
6347 constant-expression. The `,' operator is forbidden in a
6348 constant-expression. However, GCC's constant-folding machinery
6349 will fold this operation to an INTEGER_CST for `3'. */
6351 /* Save the old settings. */
6352 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6353 saved_allow_non_integral_constant_expression_p
6354 = parser->allow_non_integral_constant_expression_p;
6355 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6356 /* We are now parsing a constant-expression. */
6357 parser->integral_constant_expression_p = true;
6358 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6359 parser->non_integral_constant_expression_p = false;
6360 /* Although the grammar says "conditional-expression", we parse an
6361 "assignment-expression", which also permits "throw-expression"
6362 and the use of assignment operators. In the case that
6363 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6364 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6365 actually essential that we look for an assignment-expression.
6366 For example, cp_parser_initializer_clauses uses this function to
6367 determine whether a particular assignment-expression is in fact
6368 constant. */
6369 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6370 /* Restore the old settings. */
6371 parser->integral_constant_expression_p
6372 = saved_integral_constant_expression_p;
6373 parser->allow_non_integral_constant_expression_p
6374 = saved_allow_non_integral_constant_expression_p;
6375 if (allow_non_constant_p)
6376 *non_constant_p = parser->non_integral_constant_expression_p;
6377 else if (parser->non_integral_constant_expression_p)
6378 expression = error_mark_node;
6379 parser->non_integral_constant_expression_p
6380 = saved_non_integral_constant_expression_p;
6382 return expression;
6385 /* Parse __builtin_offsetof.
6387 offsetof-expression:
6388 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6390 offsetof-member-designator:
6391 id-expression
6392 | offsetof-member-designator "." id-expression
6393 | offsetof-member-designator "[" expression "]" */
6395 static tree
6396 cp_parser_builtin_offsetof (cp_parser *parser)
6398 int save_ice_p, save_non_ice_p;
6399 tree type, expr;
6400 cp_id_kind dummy;
6402 /* We're about to accept non-integral-constant things, but will
6403 definitely yield an integral constant expression. Save and
6404 restore these values around our local parsing. */
6405 save_ice_p = parser->integral_constant_expression_p;
6406 save_non_ice_p = parser->non_integral_constant_expression_p;
6408 /* Consume the "__builtin_offsetof" token. */
6409 cp_lexer_consume_token (parser->lexer);
6410 /* Consume the opening `('. */
6411 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6412 /* Parse the type-id. */
6413 type = cp_parser_type_id (parser);
6414 /* Look for the `,'. */
6415 cp_parser_require (parser, CPP_COMMA, "`,'");
6417 /* Build the (type *)null that begins the traditional offsetof macro. */
6418 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6420 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6421 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6422 true, &dummy);
6423 while (true)
6425 cp_token *token = cp_lexer_peek_token (parser->lexer);
6426 switch (token->type)
6428 case CPP_OPEN_SQUARE:
6429 /* offsetof-member-designator "[" expression "]" */
6430 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6431 break;
6433 case CPP_DOT:
6434 /* offsetof-member-designator "." identifier */
6435 cp_lexer_consume_token (parser->lexer);
6436 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6437 true, &dummy);
6438 break;
6440 case CPP_CLOSE_PAREN:
6441 /* Consume the ")" token. */
6442 cp_lexer_consume_token (parser->lexer);
6443 goto success;
6445 default:
6446 /* Error. We know the following require will fail, but
6447 that gives the proper error message. */
6448 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6449 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6450 expr = error_mark_node;
6451 goto failure;
6455 success:
6456 /* If we're processing a template, we can't finish the semantics yet.
6457 Otherwise we can fold the entire expression now. */
6458 if (processing_template_decl)
6459 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6460 else
6461 expr = finish_offsetof (expr);
6463 failure:
6464 parser->integral_constant_expression_p = save_ice_p;
6465 parser->non_integral_constant_expression_p = save_non_ice_p;
6467 return expr;
6470 /* Parse a trait expression. */
6472 static tree
6473 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6475 cp_trait_kind kind;
6476 tree type1, type2 = NULL_TREE;
6477 bool binary = false;
6478 cp_decl_specifier_seq decl_specs;
6480 switch (keyword)
6482 case RID_HAS_NOTHROW_ASSIGN:
6483 kind = CPTK_HAS_NOTHROW_ASSIGN;
6484 break;
6485 case RID_HAS_NOTHROW_CONSTRUCTOR:
6486 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6487 break;
6488 case RID_HAS_NOTHROW_COPY:
6489 kind = CPTK_HAS_NOTHROW_COPY;
6490 break;
6491 case RID_HAS_TRIVIAL_ASSIGN:
6492 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6493 break;
6494 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6495 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6496 break;
6497 case RID_HAS_TRIVIAL_COPY:
6498 kind = CPTK_HAS_TRIVIAL_COPY;
6499 break;
6500 case RID_HAS_TRIVIAL_DESTRUCTOR:
6501 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6502 break;
6503 case RID_HAS_VIRTUAL_DESTRUCTOR:
6504 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6505 break;
6506 case RID_IS_ABSTRACT:
6507 kind = CPTK_IS_ABSTRACT;
6508 break;
6509 case RID_IS_BASE_OF:
6510 kind = CPTK_IS_BASE_OF;
6511 binary = true;
6512 break;
6513 case RID_IS_CLASS:
6514 kind = CPTK_IS_CLASS;
6515 break;
6516 case RID_IS_CONVERTIBLE_TO:
6517 kind = CPTK_IS_CONVERTIBLE_TO;
6518 binary = true;
6519 break;
6520 case RID_IS_EMPTY:
6521 kind = CPTK_IS_EMPTY;
6522 break;
6523 case RID_IS_ENUM:
6524 kind = CPTK_IS_ENUM;
6525 break;
6526 case RID_IS_POD:
6527 kind = CPTK_IS_POD;
6528 break;
6529 case RID_IS_POLYMORPHIC:
6530 kind = CPTK_IS_POLYMORPHIC;
6531 break;
6532 case RID_IS_UNION:
6533 kind = CPTK_IS_UNION;
6534 break;
6535 default:
6536 gcc_unreachable ();
6539 /* Consume the token. */
6540 cp_lexer_consume_token (parser->lexer);
6542 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6544 type1 = cp_parser_type_id (parser);
6546 if (type1 == error_mark_node)
6547 return error_mark_node;
6549 /* Build a trivial decl-specifier-seq. */
6550 clear_decl_specs (&decl_specs);
6551 decl_specs.type = type1;
6553 /* Call grokdeclarator to figure out what type this is. */
6554 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6555 /*initialized=*/0, /*attrlist=*/NULL);
6557 if (binary)
6559 cp_parser_require (parser, CPP_COMMA, "`,'");
6561 type2 = cp_parser_type_id (parser);
6563 if (type2 == error_mark_node)
6564 return error_mark_node;
6566 /* Build a trivial decl-specifier-seq. */
6567 clear_decl_specs (&decl_specs);
6568 decl_specs.type = type2;
6570 /* Call grokdeclarator to figure out what type this is. */
6571 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6572 /*initialized=*/0, /*attrlist=*/NULL);
6575 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6577 /* Complete the trait expression, which may mean either processing
6578 the trait expr now or saving it for template instantiation. */
6579 return finish_trait_expr (kind, type1, type2);
6582 /* Statements [gram.stmt.stmt] */
6584 /* Parse a statement.
6586 statement:
6587 labeled-statement
6588 expression-statement
6589 compound-statement
6590 selection-statement
6591 iteration-statement
6592 jump-statement
6593 declaration-statement
6594 try-block
6596 IN_COMPOUND is true when the statement is nested inside a
6597 cp_parser_compound_statement; this matters for certain pragmas.
6599 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6600 is a (possibly labeled) if statement which is not enclosed in braces
6601 and has an else clause. This is used to implement -Wparentheses. */
6603 static void
6604 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6605 bool in_compound, bool *if_p)
6607 tree statement;
6608 cp_token *token;
6609 location_t statement_location;
6611 restart:
6612 if (if_p != NULL)
6613 *if_p = false;
6614 /* There is no statement yet. */
6615 statement = NULL_TREE;
6616 /* Peek at the next token. */
6617 token = cp_lexer_peek_token (parser->lexer);
6618 /* Remember the location of the first token in the statement. */
6619 statement_location = token->location;
6620 /* If this is a keyword, then that will often determine what kind of
6621 statement we have. */
6622 if (token->type == CPP_KEYWORD)
6624 enum rid keyword = token->keyword;
6626 switch (keyword)
6628 case RID_CASE:
6629 case RID_DEFAULT:
6630 /* Looks like a labeled-statement with a case label.
6631 Parse the label, and then use tail recursion to parse
6632 the statement. */
6633 cp_parser_label_for_labeled_statement (parser);
6634 goto restart;
6636 case RID_IF:
6637 case RID_SWITCH:
6638 statement = cp_parser_selection_statement (parser, if_p);
6639 break;
6641 case RID_WHILE:
6642 case RID_DO:
6643 case RID_FOR:
6644 statement = cp_parser_iteration_statement (parser);
6645 break;
6647 case RID_BREAK:
6648 case RID_CONTINUE:
6649 case RID_RETURN:
6650 case RID_GOTO:
6651 statement = cp_parser_jump_statement (parser);
6652 break;
6654 /* Objective-C++ exception-handling constructs. */
6655 case RID_AT_TRY:
6656 case RID_AT_CATCH:
6657 case RID_AT_FINALLY:
6658 case RID_AT_SYNCHRONIZED:
6659 case RID_AT_THROW:
6660 statement = cp_parser_objc_statement (parser);
6661 break;
6663 case RID_TRY:
6664 statement = cp_parser_try_block (parser);
6665 break;
6667 case RID_NAMESPACE:
6668 /* This must be a namespace alias definition. */
6669 cp_parser_declaration_statement (parser);
6670 return;
6672 default:
6673 /* It might be a keyword like `int' that can start a
6674 declaration-statement. */
6675 break;
6678 else if (token->type == CPP_NAME)
6680 /* If the next token is a `:', then we are looking at a
6681 labeled-statement. */
6682 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6683 if (token->type == CPP_COLON)
6685 /* Looks like a labeled-statement with an ordinary label.
6686 Parse the label, and then use tail recursion to parse
6687 the statement. */
6688 cp_parser_label_for_labeled_statement (parser);
6689 goto restart;
6692 /* Anything that starts with a `{' must be a compound-statement. */
6693 else if (token->type == CPP_OPEN_BRACE)
6694 statement = cp_parser_compound_statement (parser, NULL, false);
6695 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6696 a statement all its own. */
6697 else if (token->type == CPP_PRAGMA)
6699 /* Only certain OpenMP pragmas are attached to statements, and thus
6700 are considered statements themselves. All others are not. In
6701 the context of a compound, accept the pragma as a "statement" and
6702 return so that we can check for a close brace. Otherwise we
6703 require a real statement and must go back and read one. */
6704 if (in_compound)
6705 cp_parser_pragma (parser, pragma_compound);
6706 else if (!cp_parser_pragma (parser, pragma_stmt))
6707 goto restart;
6708 return;
6710 else if (token->type == CPP_EOF)
6712 cp_parser_error (parser, "expected statement");
6713 return;
6716 /* Everything else must be a declaration-statement or an
6717 expression-statement. Try for the declaration-statement
6718 first, unless we are looking at a `;', in which case we know that
6719 we have an expression-statement. */
6720 if (!statement)
6722 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6724 cp_parser_parse_tentatively (parser);
6725 /* Try to parse the declaration-statement. */
6726 cp_parser_declaration_statement (parser);
6727 /* If that worked, we're done. */
6728 if (cp_parser_parse_definitely (parser))
6729 return;
6731 /* Look for an expression-statement instead. */
6732 statement = cp_parser_expression_statement (parser, in_statement_expr);
6735 /* Set the line number for the statement. */
6736 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6737 SET_EXPR_LOCATION (statement, statement_location);
6740 /* Parse the label for a labeled-statement, i.e.
6742 identifier :
6743 case constant-expression :
6744 default :
6746 GNU Extension:
6747 case constant-expression ... constant-expression : statement
6749 When a label is parsed without errors, the label is added to the
6750 parse tree by the finish_* functions, so this function doesn't
6751 have to return the label. */
6753 static void
6754 cp_parser_label_for_labeled_statement (cp_parser* parser)
6756 cp_token *token;
6758 /* The next token should be an identifier. */
6759 token = cp_lexer_peek_token (parser->lexer);
6760 if (token->type != CPP_NAME
6761 && token->type != CPP_KEYWORD)
6763 cp_parser_error (parser, "expected labeled-statement");
6764 return;
6767 switch (token->keyword)
6769 case RID_CASE:
6771 tree expr, expr_hi;
6772 cp_token *ellipsis;
6774 /* Consume the `case' token. */
6775 cp_lexer_consume_token (parser->lexer);
6776 /* Parse the constant-expression. */
6777 expr = cp_parser_constant_expression (parser,
6778 /*allow_non_constant_p=*/false,
6779 NULL);
6781 ellipsis = cp_lexer_peek_token (parser->lexer);
6782 if (ellipsis->type == CPP_ELLIPSIS)
6784 /* Consume the `...' token. */
6785 cp_lexer_consume_token (parser->lexer);
6786 expr_hi =
6787 cp_parser_constant_expression (parser,
6788 /*allow_non_constant_p=*/false,
6789 NULL);
6790 /* We don't need to emit warnings here, as the common code
6791 will do this for us. */
6793 else
6794 expr_hi = NULL_TREE;
6796 if (parser->in_switch_statement_p)
6797 finish_case_label (expr, expr_hi);
6798 else
6799 error ("case label %qE not within a switch statement", expr);
6801 break;
6803 case RID_DEFAULT:
6804 /* Consume the `default' token. */
6805 cp_lexer_consume_token (parser->lexer);
6807 if (parser->in_switch_statement_p)
6808 finish_case_label (NULL_TREE, NULL_TREE);
6809 else
6810 error ("case label not within a switch statement");
6811 break;
6813 default:
6814 /* Anything else must be an ordinary label. */
6815 finish_label_stmt (cp_parser_identifier (parser));
6816 break;
6819 /* Require the `:' token. */
6820 cp_parser_require (parser, CPP_COLON, "`:'");
6823 /* Parse an expression-statement.
6825 expression-statement:
6826 expression [opt] ;
6828 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6829 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6830 indicates whether this expression-statement is part of an
6831 expression statement. */
6833 static tree
6834 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6836 tree statement = NULL_TREE;
6838 /* If the next token is a ';', then there is no expression
6839 statement. */
6840 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6841 statement = cp_parser_expression (parser, /*cast_p=*/false);
6843 /* Consume the final `;'. */
6844 cp_parser_consume_semicolon_at_end_of_statement (parser);
6846 if (in_statement_expr
6847 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6848 /* This is the final expression statement of a statement
6849 expression. */
6850 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6851 else if (statement)
6852 statement = finish_expr_stmt (statement);
6853 else
6854 finish_stmt ();
6856 return statement;
6859 /* Parse a compound-statement.
6861 compound-statement:
6862 { statement-seq [opt] }
6864 GNU extension:
6866 compound-statement:
6867 { label-declaration-seq [opt] statement-seq [opt] }
6869 label-declaration-seq:
6870 label-declaration
6871 label-declaration-seq label-declaration
6873 Returns a tree representing the statement. */
6875 static tree
6876 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6877 bool in_try)
6879 tree compound_stmt;
6881 /* Consume the `{'. */
6882 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6883 return error_mark_node;
6884 /* Begin the compound-statement. */
6885 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6886 /* If the next keyword is `__label__' we have a label declaration. */
6887 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6888 cp_parser_label_declaration (parser);
6889 /* Parse an (optional) statement-seq. */
6890 cp_parser_statement_seq_opt (parser, in_statement_expr);
6891 /* Finish the compound-statement. */
6892 finish_compound_stmt (compound_stmt);
6893 /* Consume the `}'. */
6894 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6896 return compound_stmt;
6899 /* Parse an (optional) statement-seq.
6901 statement-seq:
6902 statement
6903 statement-seq [opt] statement */
6905 static void
6906 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6908 /* Scan statements until there aren't any more. */
6909 while (true)
6911 cp_token *token = cp_lexer_peek_token (parser->lexer);
6913 /* If we're looking at a `}', then we've run out of statements. */
6914 if (token->type == CPP_CLOSE_BRACE
6915 || token->type == CPP_EOF
6916 || token->type == CPP_PRAGMA_EOL)
6917 break;
6919 /* If we are in a compound statement and find 'else' then
6920 something went wrong. */
6921 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6923 if (parser->in_statement & IN_IF_STMT)
6924 break;
6925 else
6927 token = cp_lexer_consume_token (parser->lexer);
6928 error ("%<else%> without a previous %<if%>");
6932 /* Parse the statement. */
6933 cp_parser_statement (parser, in_statement_expr, true, NULL);
6937 /* Parse a selection-statement.
6939 selection-statement:
6940 if ( condition ) statement
6941 if ( condition ) statement else statement
6942 switch ( condition ) statement
6944 Returns the new IF_STMT or SWITCH_STMT.
6946 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6947 is a (possibly labeled) if statement which is not enclosed in
6948 braces and has an else clause. This is used to implement
6949 -Wparentheses. */
6951 static tree
6952 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6954 cp_token *token;
6955 enum rid keyword;
6957 if (if_p != NULL)
6958 *if_p = false;
6960 /* Peek at the next token. */
6961 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6963 /* See what kind of keyword it is. */
6964 keyword = token->keyword;
6965 switch (keyword)
6967 case RID_IF:
6968 case RID_SWITCH:
6970 tree statement;
6971 tree condition;
6973 /* Look for the `('. */
6974 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6976 cp_parser_skip_to_end_of_statement (parser);
6977 return error_mark_node;
6980 /* Begin the selection-statement. */
6981 if (keyword == RID_IF)
6982 statement = begin_if_stmt ();
6983 else
6984 statement = begin_switch_stmt ();
6986 /* Parse the condition. */
6987 condition = cp_parser_condition (parser);
6988 /* Look for the `)'. */
6989 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6990 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6991 /*consume_paren=*/true);
6993 if (keyword == RID_IF)
6995 bool nested_if;
6996 unsigned char in_statement;
6998 /* Add the condition. */
6999 finish_if_stmt_cond (condition, statement);
7001 /* Parse the then-clause. */
7002 in_statement = parser->in_statement;
7003 parser->in_statement |= IN_IF_STMT;
7004 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7005 parser->in_statement = in_statement;
7007 finish_then_clause (statement);
7009 /* If the next token is `else', parse the else-clause. */
7010 if (cp_lexer_next_token_is_keyword (parser->lexer,
7011 RID_ELSE))
7013 /* Consume the `else' keyword. */
7014 cp_lexer_consume_token (parser->lexer);
7015 begin_else_clause (statement);
7016 /* Parse the else-clause. */
7017 cp_parser_implicitly_scoped_statement (parser, NULL);
7018 finish_else_clause (statement);
7020 /* If we are currently parsing a then-clause, then
7021 IF_P will not be NULL. We set it to true to
7022 indicate that this if statement has an else clause.
7023 This may trigger the Wparentheses warning below
7024 when we get back up to the parent if statement. */
7025 if (if_p != NULL)
7026 *if_p = true;
7028 else
7030 /* This if statement does not have an else clause. If
7031 NESTED_IF is true, then the then-clause is an if
7032 statement which does have an else clause. We warn
7033 about the potential ambiguity. */
7034 if (nested_if)
7035 warning (OPT_Wparentheses,
7036 ("%Hsuggest explicit braces "
7037 "to avoid ambiguous %<else%>"),
7038 EXPR_LOCUS (statement));
7041 /* Now we're all done with the if-statement. */
7042 finish_if_stmt (statement);
7044 else
7046 bool in_switch_statement_p;
7047 unsigned char in_statement;
7049 /* Add the condition. */
7050 finish_switch_cond (condition, statement);
7052 /* Parse the body of the switch-statement. */
7053 in_switch_statement_p = parser->in_switch_statement_p;
7054 in_statement = parser->in_statement;
7055 parser->in_switch_statement_p = true;
7056 parser->in_statement |= IN_SWITCH_STMT;
7057 cp_parser_implicitly_scoped_statement (parser, NULL);
7058 parser->in_switch_statement_p = in_switch_statement_p;
7059 parser->in_statement = in_statement;
7061 /* Now we're all done with the switch-statement. */
7062 finish_switch_stmt (statement);
7065 return statement;
7067 break;
7069 default:
7070 cp_parser_error (parser, "expected selection-statement");
7071 return error_mark_node;
7075 /* Parse a condition.
7077 condition:
7078 expression
7079 type-specifier-seq declarator = assignment-expression
7081 GNU Extension:
7083 condition:
7084 type-specifier-seq declarator asm-specification [opt]
7085 attributes [opt] = assignment-expression
7087 Returns the expression that should be tested. */
7089 static tree
7090 cp_parser_condition (cp_parser* parser)
7092 cp_decl_specifier_seq type_specifiers;
7093 const char *saved_message;
7095 /* Try the declaration first. */
7096 cp_parser_parse_tentatively (parser);
7097 /* New types are not allowed in the type-specifier-seq for a
7098 condition. */
7099 saved_message = parser->type_definition_forbidden_message;
7100 parser->type_definition_forbidden_message
7101 = "types may not be defined in conditions";
7102 /* Parse the type-specifier-seq. */
7103 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7104 &type_specifiers);
7105 /* Restore the saved message. */
7106 parser->type_definition_forbidden_message = saved_message;
7107 /* If all is well, we might be looking at a declaration. */
7108 if (!cp_parser_error_occurred (parser))
7110 tree decl;
7111 tree asm_specification;
7112 tree attributes;
7113 cp_declarator *declarator;
7114 tree initializer = NULL_TREE;
7116 /* Parse the declarator. */
7117 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7118 /*ctor_dtor_or_conv_p=*/NULL,
7119 /*parenthesized_p=*/NULL,
7120 /*member_p=*/false);
7121 /* Parse the attributes. */
7122 attributes = cp_parser_attributes_opt (parser);
7123 /* Parse the asm-specification. */
7124 asm_specification = cp_parser_asm_specification_opt (parser);
7125 /* If the next token is not an `=', then we might still be
7126 looking at an expression. For example:
7128 if (A(a).x)
7130 looks like a decl-specifier-seq and a declarator -- but then
7131 there is no `=', so this is an expression. */
7132 cp_parser_require (parser, CPP_EQ, "`='");
7133 /* If we did see an `=', then we are looking at a declaration
7134 for sure. */
7135 if (cp_parser_parse_definitely (parser))
7137 tree pushed_scope;
7138 bool non_constant_p;
7140 /* Create the declaration. */
7141 decl = start_decl (declarator, &type_specifiers,
7142 /*initialized_p=*/true,
7143 attributes, /*prefix_attributes=*/NULL_TREE,
7144 &pushed_scope);
7145 /* Parse the assignment-expression. */
7146 initializer
7147 = cp_parser_constant_expression (parser,
7148 /*allow_non_constant_p=*/true,
7149 &non_constant_p);
7150 if (!non_constant_p)
7151 initializer = fold_non_dependent_expr (initializer);
7153 /* Process the initializer. */
7154 cp_finish_decl (decl,
7155 initializer, !non_constant_p,
7156 asm_specification,
7157 LOOKUP_ONLYCONVERTING);
7159 if (pushed_scope)
7160 pop_scope (pushed_scope);
7162 return convert_from_reference (decl);
7165 /* If we didn't even get past the declarator successfully, we are
7166 definitely not looking at a declaration. */
7167 else
7168 cp_parser_abort_tentative_parse (parser);
7170 /* Otherwise, we are looking at an expression. */
7171 return cp_parser_expression (parser, /*cast_p=*/false);
7174 /* We check for a ) immediately followed by ; with no whitespacing
7175 between. This is used to issue a warning for:
7177 while (...);
7179 and:
7181 for (...);
7183 as the semicolon is probably extraneous.
7185 On parse errors, the next token might not be a ), so do nothing in
7186 that case. */
7188 static void
7189 check_empty_body (cp_parser* parser, const char* type)
7191 cp_token *token;
7192 cp_token *close_paren;
7193 expanded_location close_loc;
7194 expanded_location semi_loc;
7196 close_paren = cp_lexer_peek_token (parser->lexer);
7197 if (close_paren->type != CPP_CLOSE_PAREN)
7198 return;
7200 close_loc = expand_location (close_paren->location);
7201 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7203 if (token->type != CPP_SEMICOLON
7204 || (token->flags & PREV_WHITE))
7205 return;
7207 semi_loc = expand_location (token->location);
7208 if (close_loc.line == semi_loc.line
7209 #ifdef USE_MAPPED_LOCATION
7210 && close_loc.column+1 == semi_loc.column
7211 #endif
7213 warning (OPT_Wempty_body,
7214 "suggest a space before %<;%> or explicit braces around empty "
7215 "body in %<%s%> statement",
7216 type);
7219 /* Parse an iteration-statement.
7221 iteration-statement:
7222 while ( condition ) statement
7223 do statement while ( expression ) ;
7224 for ( for-init-statement condition [opt] ; expression [opt] )
7225 statement
7227 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7229 static tree
7230 cp_parser_iteration_statement (cp_parser* parser)
7232 cp_token *token;
7233 enum rid keyword;
7234 tree statement;
7235 unsigned char in_statement;
7237 /* Peek at the next token. */
7238 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7239 if (!token)
7240 return error_mark_node;
7242 /* Remember whether or not we are already within an iteration
7243 statement. */
7244 in_statement = parser->in_statement;
7246 /* See what kind of keyword it is. */
7247 keyword = token->keyword;
7248 switch (keyword)
7250 case RID_WHILE:
7252 tree condition;
7254 /* Begin the while-statement. */
7255 statement = begin_while_stmt ();
7256 /* Look for the `('. */
7257 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7258 /* Parse the condition. */
7259 condition = cp_parser_condition (parser);
7260 finish_while_stmt_cond (condition, statement);
7261 check_empty_body (parser, "while");
7262 /* Look for the `)'. */
7263 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7264 /* Parse the dependent statement. */
7265 parser->in_statement = IN_ITERATION_STMT;
7266 cp_parser_already_scoped_statement (parser);
7267 parser->in_statement = in_statement;
7268 /* We're done with the while-statement. */
7269 finish_while_stmt (statement);
7271 break;
7273 case RID_DO:
7275 tree expression;
7277 /* Begin the do-statement. */
7278 statement = begin_do_stmt ();
7279 /* Parse the body of the do-statement. */
7280 parser->in_statement = IN_ITERATION_STMT;
7281 cp_parser_implicitly_scoped_statement (parser, NULL);
7282 parser->in_statement = in_statement;
7283 finish_do_body (statement);
7284 /* Look for the `while' keyword. */
7285 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7286 /* Look for the `('. */
7287 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7288 /* Parse the expression. */
7289 expression = cp_parser_expression (parser, /*cast_p=*/false);
7290 /* We're done with the do-statement. */
7291 finish_do_stmt (expression, statement);
7292 /* Look for the `)'. */
7293 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7294 /* Look for the `;'. */
7295 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7297 break;
7299 case RID_FOR:
7301 tree condition = NULL_TREE;
7302 tree expression = NULL_TREE;
7304 /* Begin the for-statement. */
7305 statement = begin_for_stmt ();
7306 /* Look for the `('. */
7307 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7308 /* Parse the initialization. */
7309 cp_parser_for_init_statement (parser);
7310 finish_for_init_stmt (statement);
7312 /* If there's a condition, process it. */
7313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7314 condition = cp_parser_condition (parser);
7315 finish_for_cond (condition, statement);
7316 /* Look for the `;'. */
7317 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7319 /* If there's an expression, process it. */
7320 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7321 expression = cp_parser_expression (parser, /*cast_p=*/false);
7322 finish_for_expr (expression, statement);
7323 check_empty_body (parser, "for");
7324 /* Look for the `)'. */
7325 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7327 /* Parse the body of the for-statement. */
7328 parser->in_statement = IN_ITERATION_STMT;
7329 cp_parser_already_scoped_statement (parser);
7330 parser->in_statement = in_statement;
7332 /* We're done with the for-statement. */
7333 finish_for_stmt (statement);
7335 break;
7337 default:
7338 cp_parser_error (parser, "expected iteration-statement");
7339 statement = error_mark_node;
7340 break;
7343 return statement;
7346 /* Parse a for-init-statement.
7348 for-init-statement:
7349 expression-statement
7350 simple-declaration */
7352 static void
7353 cp_parser_for_init_statement (cp_parser* parser)
7355 /* If the next token is a `;', then we have an empty
7356 expression-statement. Grammatically, this is also a
7357 simple-declaration, but an invalid one, because it does not
7358 declare anything. Therefore, if we did not handle this case
7359 specially, we would issue an error message about an invalid
7360 declaration. */
7361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7363 /* We're going to speculatively look for a declaration, falling back
7364 to an expression, if necessary. */
7365 cp_parser_parse_tentatively (parser);
7366 /* Parse the declaration. */
7367 cp_parser_simple_declaration (parser,
7368 /*function_definition_allowed_p=*/false);
7369 /* If the tentative parse failed, then we shall need to look for an
7370 expression-statement. */
7371 if (cp_parser_parse_definitely (parser))
7372 return;
7375 cp_parser_expression_statement (parser, false);
7378 /* Parse a jump-statement.
7380 jump-statement:
7381 break ;
7382 continue ;
7383 return expression [opt] ;
7384 goto identifier ;
7386 GNU extension:
7388 jump-statement:
7389 goto * expression ;
7391 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7393 static tree
7394 cp_parser_jump_statement (cp_parser* parser)
7396 tree statement = error_mark_node;
7397 cp_token *token;
7398 enum rid keyword;
7399 unsigned char in_statement;
7401 /* Peek at the next token. */
7402 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7403 if (!token)
7404 return error_mark_node;
7406 /* See what kind of keyword it is. */
7407 keyword = token->keyword;
7408 switch (keyword)
7410 case RID_BREAK:
7411 in_statement = parser->in_statement & ~IN_IF_STMT;
7412 switch (in_statement)
7414 case 0:
7415 error ("break statement not within loop or switch");
7416 break;
7417 default:
7418 gcc_assert ((in_statement & IN_SWITCH_STMT)
7419 || in_statement == IN_ITERATION_STMT);
7420 statement = finish_break_stmt ();
7421 break;
7422 case IN_OMP_BLOCK:
7423 error ("invalid exit from OpenMP structured block");
7424 break;
7425 case IN_OMP_FOR:
7426 error ("break statement used with OpenMP for loop");
7427 break;
7429 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7430 break;
7432 case RID_CONTINUE:
7433 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7435 case 0:
7436 error ("continue statement not within a loop");
7437 break;
7438 case IN_ITERATION_STMT:
7439 case IN_OMP_FOR:
7440 statement = finish_continue_stmt ();
7441 break;
7442 case IN_OMP_BLOCK:
7443 error ("invalid exit from OpenMP structured block");
7444 break;
7445 default:
7446 gcc_unreachable ();
7448 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7449 break;
7451 case RID_RETURN:
7453 tree expr;
7455 /* If the next token is a `;', then there is no
7456 expression. */
7457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7458 expr = cp_parser_expression (parser, /*cast_p=*/false);
7459 else
7460 expr = NULL_TREE;
7461 /* Build the return-statement. */
7462 statement = finish_return_stmt (expr);
7463 /* Look for the final `;'. */
7464 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7466 break;
7468 case RID_GOTO:
7469 /* Create the goto-statement. */
7470 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7472 /* Issue a warning about this use of a GNU extension. */
7473 if (pedantic)
7474 pedwarn ("ISO C++ forbids computed gotos");
7475 /* Consume the '*' token. */
7476 cp_lexer_consume_token (parser->lexer);
7477 /* Parse the dependent expression. */
7478 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7480 else
7481 finish_goto_stmt (cp_parser_identifier (parser));
7482 /* Look for the final `;'. */
7483 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7484 break;
7486 default:
7487 cp_parser_error (parser, "expected jump-statement");
7488 break;
7491 return statement;
7494 /* Parse a declaration-statement.
7496 declaration-statement:
7497 block-declaration */
7499 static void
7500 cp_parser_declaration_statement (cp_parser* parser)
7502 void *p;
7504 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7505 p = obstack_alloc (&declarator_obstack, 0);
7507 /* Parse the block-declaration. */
7508 cp_parser_block_declaration (parser, /*statement_p=*/true);
7510 /* Free any declarators allocated. */
7511 obstack_free (&declarator_obstack, p);
7513 /* Finish off the statement. */
7514 finish_stmt ();
7517 /* Some dependent statements (like `if (cond) statement'), are
7518 implicitly in their own scope. In other words, if the statement is
7519 a single statement (as opposed to a compound-statement), it is
7520 none-the-less treated as if it were enclosed in braces. Any
7521 declarations appearing in the dependent statement are out of scope
7522 after control passes that point. This function parses a statement,
7523 but ensures that is in its own scope, even if it is not a
7524 compound-statement.
7526 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7527 is a (possibly labeled) if statement which is not enclosed in
7528 braces and has an else clause. This is used to implement
7529 -Wparentheses.
7531 Returns the new statement. */
7533 static tree
7534 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7536 tree statement;
7538 if (if_p != NULL)
7539 *if_p = false;
7541 /* Mark if () ; with a special NOP_EXPR. */
7542 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7544 cp_lexer_consume_token (parser->lexer);
7545 statement = add_stmt (build_empty_stmt ());
7547 /* if a compound is opened, we simply parse the statement directly. */
7548 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7549 statement = cp_parser_compound_statement (parser, NULL, false);
7550 /* If the token is not a `{', then we must take special action. */
7551 else
7553 /* Create a compound-statement. */
7554 statement = begin_compound_stmt (0);
7555 /* Parse the dependent-statement. */
7556 cp_parser_statement (parser, NULL_TREE, false, if_p);
7557 /* Finish the dummy compound-statement. */
7558 finish_compound_stmt (statement);
7561 /* Return the statement. */
7562 return statement;
7565 /* For some dependent statements (like `while (cond) statement'), we
7566 have already created a scope. Therefore, even if the dependent
7567 statement is a compound-statement, we do not want to create another
7568 scope. */
7570 static void
7571 cp_parser_already_scoped_statement (cp_parser* parser)
7573 /* If the token is a `{', then we must take special action. */
7574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7575 cp_parser_statement (parser, NULL_TREE, false, NULL);
7576 else
7578 /* Avoid calling cp_parser_compound_statement, so that we
7579 don't create a new scope. Do everything else by hand. */
7580 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7581 cp_parser_statement_seq_opt (parser, NULL_TREE);
7582 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7586 /* Declarations [gram.dcl.dcl] */
7588 /* Parse an optional declaration-sequence.
7590 declaration-seq:
7591 declaration
7592 declaration-seq declaration */
7594 static void
7595 cp_parser_declaration_seq_opt (cp_parser* parser)
7597 while (true)
7599 cp_token *token;
7601 token = cp_lexer_peek_token (parser->lexer);
7603 if (token->type == CPP_CLOSE_BRACE
7604 || token->type == CPP_EOF
7605 || token->type == CPP_PRAGMA_EOL)
7606 break;
7608 if (token->type == CPP_SEMICOLON)
7610 /* A declaration consisting of a single semicolon is
7611 invalid. Allow it unless we're being pedantic. */
7612 cp_lexer_consume_token (parser->lexer);
7613 if (pedantic && !in_system_header)
7614 pedwarn ("extra %<;%>");
7615 continue;
7618 /* If we're entering or exiting a region that's implicitly
7619 extern "C", modify the lang context appropriately. */
7620 if (!parser->implicit_extern_c && token->implicit_extern_c)
7622 push_lang_context (lang_name_c);
7623 parser->implicit_extern_c = true;
7625 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7627 pop_lang_context ();
7628 parser->implicit_extern_c = false;
7631 if (token->type == CPP_PRAGMA)
7633 /* A top-level declaration can consist solely of a #pragma.
7634 A nested declaration cannot, so this is done here and not
7635 in cp_parser_declaration. (A #pragma at block scope is
7636 handled in cp_parser_statement.) */
7637 cp_parser_pragma (parser, pragma_external);
7638 continue;
7641 /* Parse the declaration itself. */
7642 cp_parser_declaration (parser);
7646 /* Parse a declaration.
7648 declaration:
7649 block-declaration
7650 function-definition
7651 template-declaration
7652 explicit-instantiation
7653 explicit-specialization
7654 linkage-specification
7655 namespace-definition
7657 GNU extension:
7659 declaration:
7660 __extension__ declaration */
7662 static void
7663 cp_parser_declaration (cp_parser* parser)
7665 cp_token token1;
7666 cp_token token2;
7667 int saved_pedantic;
7668 void *p;
7670 /* Check for the `__extension__' keyword. */
7671 if (cp_parser_extension_opt (parser, &saved_pedantic))
7673 /* Parse the qualified declaration. */
7674 cp_parser_declaration (parser);
7675 /* Restore the PEDANTIC flag. */
7676 pedantic = saved_pedantic;
7678 return;
7681 /* Try to figure out what kind of declaration is present. */
7682 token1 = *cp_lexer_peek_token (parser->lexer);
7684 if (token1.type != CPP_EOF)
7685 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7686 else
7688 token2.type = CPP_EOF;
7689 token2.keyword = RID_MAX;
7692 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7693 p = obstack_alloc (&declarator_obstack, 0);
7695 /* If the next token is `extern' and the following token is a string
7696 literal, then we have a linkage specification. */
7697 if (token1.keyword == RID_EXTERN
7698 && cp_parser_is_string_literal (&token2))
7699 cp_parser_linkage_specification (parser);
7700 /* If the next token is `template', then we have either a template
7701 declaration, an explicit instantiation, or an explicit
7702 specialization. */
7703 else if (token1.keyword == RID_TEMPLATE)
7705 /* `template <>' indicates a template specialization. */
7706 if (token2.type == CPP_LESS
7707 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7708 cp_parser_explicit_specialization (parser);
7709 /* `template <' indicates a template declaration. */
7710 else if (token2.type == CPP_LESS)
7711 cp_parser_template_declaration (parser, /*member_p=*/false);
7712 /* Anything else must be an explicit instantiation. */
7713 else
7714 cp_parser_explicit_instantiation (parser);
7716 /* If the next token is `export', then we have a template
7717 declaration. */
7718 else if (token1.keyword == RID_EXPORT)
7719 cp_parser_template_declaration (parser, /*member_p=*/false);
7720 /* If the next token is `extern', 'static' or 'inline' and the one
7721 after that is `template', we have a GNU extended explicit
7722 instantiation directive. */
7723 else if (cp_parser_allow_gnu_extensions_p (parser)
7724 && (token1.keyword == RID_EXTERN
7725 || token1.keyword == RID_STATIC
7726 || token1.keyword == RID_INLINE)
7727 && token2.keyword == RID_TEMPLATE)
7728 cp_parser_explicit_instantiation (parser);
7729 /* If the next token is `namespace', check for a named or unnamed
7730 namespace definition. */
7731 else if (token1.keyword == RID_NAMESPACE
7732 && (/* A named namespace definition. */
7733 (token2.type == CPP_NAME
7734 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7735 != CPP_EQ))
7736 /* An unnamed namespace definition. */
7737 || token2.type == CPP_OPEN_BRACE
7738 || token2.keyword == RID_ATTRIBUTE))
7739 cp_parser_namespace_definition (parser);
7740 /* Objective-C++ declaration/definition. */
7741 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7742 cp_parser_objc_declaration (parser);
7743 /* We must have either a block declaration or a function
7744 definition. */
7745 else
7746 /* Try to parse a block-declaration, or a function-definition. */
7747 cp_parser_block_declaration (parser, /*statement_p=*/false);
7749 /* Free any declarators allocated. */
7750 obstack_free (&declarator_obstack, p);
7753 /* Parse a block-declaration.
7755 block-declaration:
7756 simple-declaration
7757 asm-definition
7758 namespace-alias-definition
7759 using-declaration
7760 using-directive
7762 GNU Extension:
7764 block-declaration:
7765 __extension__ block-declaration
7767 C++0x Extension:
7769 block-declaration:
7770 static_assert-declaration
7772 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7773 part of a declaration-statement. */
7775 static void
7776 cp_parser_block_declaration (cp_parser *parser,
7777 bool statement_p)
7779 cp_token *token1;
7780 int saved_pedantic;
7782 /* Check for the `__extension__' keyword. */
7783 if (cp_parser_extension_opt (parser, &saved_pedantic))
7785 /* Parse the qualified declaration. */
7786 cp_parser_block_declaration (parser, statement_p);
7787 /* Restore the PEDANTIC flag. */
7788 pedantic = saved_pedantic;
7790 return;
7793 /* Peek at the next token to figure out which kind of declaration is
7794 present. */
7795 token1 = cp_lexer_peek_token (parser->lexer);
7797 /* If the next keyword is `asm', we have an asm-definition. */
7798 if (token1->keyword == RID_ASM)
7800 if (statement_p)
7801 cp_parser_commit_to_tentative_parse (parser);
7802 cp_parser_asm_definition (parser);
7804 /* If the next keyword is `namespace', we have a
7805 namespace-alias-definition. */
7806 else if (token1->keyword == RID_NAMESPACE)
7807 cp_parser_namespace_alias_definition (parser);
7808 /* If the next keyword is `using', we have either a
7809 using-declaration or a using-directive. */
7810 else if (token1->keyword == RID_USING)
7812 cp_token *token2;
7814 if (statement_p)
7815 cp_parser_commit_to_tentative_parse (parser);
7816 /* If the token after `using' is `namespace', then we have a
7817 using-directive. */
7818 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7819 if (token2->keyword == RID_NAMESPACE)
7820 cp_parser_using_directive (parser);
7821 /* Otherwise, it's a using-declaration. */
7822 else
7823 cp_parser_using_declaration (parser,
7824 /*access_declaration_p=*/false);
7826 /* If the next keyword is `__label__' we have a misplaced label
7827 declaration. */
7828 else if (token1->keyword == RID_LABEL)
7830 cp_lexer_consume_token (parser->lexer);
7831 error ("%<__label__%> not at the beginning of a block");
7832 cp_parser_skip_to_end_of_statement (parser);
7833 /* If the next token is now a `;', consume it. */
7834 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7835 cp_lexer_consume_token (parser->lexer);
7837 /* If the next token is `static_assert' we have a static assertion. */
7838 else if (token1->keyword == RID_STATIC_ASSERT)
7839 cp_parser_static_assert (parser, /*member_p=*/false);
7840 /* Anything else must be a simple-declaration. */
7841 else
7842 cp_parser_simple_declaration (parser, !statement_p);
7845 /* Parse a simple-declaration.
7847 simple-declaration:
7848 decl-specifier-seq [opt] init-declarator-list [opt] ;
7850 init-declarator-list:
7851 init-declarator
7852 init-declarator-list , init-declarator
7854 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7855 function-definition as a simple-declaration. */
7857 static void
7858 cp_parser_simple_declaration (cp_parser* parser,
7859 bool function_definition_allowed_p)
7861 cp_decl_specifier_seq decl_specifiers;
7862 int declares_class_or_enum;
7863 bool saw_declarator;
7865 /* Defer access checks until we know what is being declared; the
7866 checks for names appearing in the decl-specifier-seq should be
7867 done as if we were in the scope of the thing being declared. */
7868 push_deferring_access_checks (dk_deferred);
7870 /* Parse the decl-specifier-seq. We have to keep track of whether
7871 or not the decl-specifier-seq declares a named class or
7872 enumeration type, since that is the only case in which the
7873 init-declarator-list is allowed to be empty.
7875 [dcl.dcl]
7877 In a simple-declaration, the optional init-declarator-list can be
7878 omitted only when declaring a class or enumeration, that is when
7879 the decl-specifier-seq contains either a class-specifier, an
7880 elaborated-type-specifier, or an enum-specifier. */
7881 cp_parser_decl_specifier_seq (parser,
7882 CP_PARSER_FLAGS_OPTIONAL,
7883 &decl_specifiers,
7884 &declares_class_or_enum);
7885 /* We no longer need to defer access checks. */
7886 stop_deferring_access_checks ();
7888 /* In a block scope, a valid declaration must always have a
7889 decl-specifier-seq. By not trying to parse declarators, we can
7890 resolve the declaration/expression ambiguity more quickly. */
7891 if (!function_definition_allowed_p
7892 && !decl_specifiers.any_specifiers_p)
7894 cp_parser_error (parser, "expected declaration");
7895 goto done;
7898 /* If the next two tokens are both identifiers, the code is
7899 erroneous. The usual cause of this situation is code like:
7901 T t;
7903 where "T" should name a type -- but does not. */
7904 if (!decl_specifiers.type
7905 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7907 /* If parsing tentatively, we should commit; we really are
7908 looking at a declaration. */
7909 cp_parser_commit_to_tentative_parse (parser);
7910 /* Give up. */
7911 goto done;
7914 /* If we have seen at least one decl-specifier, and the next token
7915 is not a parenthesis, then we must be looking at a declaration.
7916 (After "int (" we might be looking at a functional cast.) */
7917 if (decl_specifiers.any_specifiers_p
7918 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7919 cp_parser_commit_to_tentative_parse (parser);
7921 /* Keep going until we hit the `;' at the end of the simple
7922 declaration. */
7923 saw_declarator = false;
7924 while (cp_lexer_next_token_is_not (parser->lexer,
7925 CPP_SEMICOLON))
7927 cp_token *token;
7928 bool function_definition_p;
7929 tree decl;
7931 if (saw_declarator)
7933 /* If we are processing next declarator, coma is expected */
7934 token = cp_lexer_peek_token (parser->lexer);
7935 gcc_assert (token->type == CPP_COMMA);
7936 cp_lexer_consume_token (parser->lexer);
7938 else
7939 saw_declarator = true;
7941 /* Parse the init-declarator. */
7942 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7943 /*checks=*/NULL,
7944 function_definition_allowed_p,
7945 /*member_p=*/false,
7946 declares_class_or_enum,
7947 &function_definition_p);
7948 /* If an error occurred while parsing tentatively, exit quickly.
7949 (That usually happens when in the body of a function; each
7950 statement is treated as a declaration-statement until proven
7951 otherwise.) */
7952 if (cp_parser_error_occurred (parser))
7953 goto done;
7954 /* Handle function definitions specially. */
7955 if (function_definition_p)
7957 /* If the next token is a `,', then we are probably
7958 processing something like:
7960 void f() {}, *p;
7962 which is erroneous. */
7963 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7964 error ("mixing declarations and function-definitions is forbidden");
7965 /* Otherwise, we're done with the list of declarators. */
7966 else
7968 pop_deferring_access_checks ();
7969 return;
7972 /* The next token should be either a `,' or a `;'. */
7973 token = cp_lexer_peek_token (parser->lexer);
7974 /* If it's a `,', there are more declarators to come. */
7975 if (token->type == CPP_COMMA)
7976 /* will be consumed next time around */;
7977 /* If it's a `;', we are done. */
7978 else if (token->type == CPP_SEMICOLON)
7979 break;
7980 /* Anything else is an error. */
7981 else
7983 /* If we have already issued an error message we don't need
7984 to issue another one. */
7985 if (decl != error_mark_node
7986 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7987 cp_parser_error (parser, "expected %<,%> or %<;%>");
7988 /* Skip tokens until we reach the end of the statement. */
7989 cp_parser_skip_to_end_of_statement (parser);
7990 /* If the next token is now a `;', consume it. */
7991 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7992 cp_lexer_consume_token (parser->lexer);
7993 goto done;
7995 /* After the first time around, a function-definition is not
7996 allowed -- even if it was OK at first. For example:
7998 int i, f() {}
8000 is not valid. */
8001 function_definition_allowed_p = false;
8004 /* Issue an error message if no declarators are present, and the
8005 decl-specifier-seq does not itself declare a class or
8006 enumeration. */
8007 if (!saw_declarator)
8009 if (cp_parser_declares_only_class_p (parser))
8010 shadow_tag (&decl_specifiers);
8011 /* Perform any deferred access checks. */
8012 perform_deferred_access_checks ();
8015 /* Consume the `;'. */
8016 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8018 done:
8019 pop_deferring_access_checks ();
8022 /* Parse a decl-specifier-seq.
8024 decl-specifier-seq:
8025 decl-specifier-seq [opt] decl-specifier
8027 decl-specifier:
8028 storage-class-specifier
8029 type-specifier
8030 function-specifier
8031 friend
8032 typedef
8034 GNU Extension:
8036 decl-specifier:
8037 attributes
8039 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8041 The parser flags FLAGS is used to control type-specifier parsing.
8043 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8044 flags:
8046 1: one of the decl-specifiers is an elaborated-type-specifier
8047 (i.e., a type declaration)
8048 2: one of the decl-specifiers is an enum-specifier or a
8049 class-specifier (i.e., a type definition)
8053 static void
8054 cp_parser_decl_specifier_seq (cp_parser* parser,
8055 cp_parser_flags flags,
8056 cp_decl_specifier_seq *decl_specs,
8057 int* declares_class_or_enum)
8059 bool constructor_possible_p = !parser->in_declarator_p;
8061 /* Clear DECL_SPECS. */
8062 clear_decl_specs (decl_specs);
8064 /* Assume no class or enumeration type is declared. */
8065 *declares_class_or_enum = 0;
8067 /* Keep reading specifiers until there are no more to read. */
8068 while (true)
8070 bool constructor_p;
8071 bool found_decl_spec;
8072 cp_token *token;
8074 /* Peek at the next token. */
8075 token = cp_lexer_peek_token (parser->lexer);
8076 /* Handle attributes. */
8077 if (token->keyword == RID_ATTRIBUTE)
8079 /* Parse the attributes. */
8080 decl_specs->attributes
8081 = chainon (decl_specs->attributes,
8082 cp_parser_attributes_opt (parser));
8083 continue;
8085 /* Assume we will find a decl-specifier keyword. */
8086 found_decl_spec = true;
8087 /* If the next token is an appropriate keyword, we can simply
8088 add it to the list. */
8089 switch (token->keyword)
8091 /* decl-specifier:
8092 friend */
8093 case RID_FRIEND:
8094 if (!at_class_scope_p ())
8096 error ("%<friend%> used outside of class");
8097 cp_lexer_purge_token (parser->lexer);
8099 else
8101 ++decl_specs->specs[(int) ds_friend];
8102 /* Consume the token. */
8103 cp_lexer_consume_token (parser->lexer);
8105 break;
8107 /* function-specifier:
8108 inline
8109 virtual
8110 explicit */
8111 case RID_INLINE:
8112 case RID_VIRTUAL:
8113 case RID_EXPLICIT:
8114 cp_parser_function_specifier_opt (parser, decl_specs);
8115 break;
8117 /* decl-specifier:
8118 typedef */
8119 case RID_TYPEDEF:
8120 ++decl_specs->specs[(int) ds_typedef];
8121 /* Consume the token. */
8122 cp_lexer_consume_token (parser->lexer);
8123 /* A constructor declarator cannot appear in a typedef. */
8124 constructor_possible_p = false;
8125 /* The "typedef" keyword can only occur in a declaration; we
8126 may as well commit at this point. */
8127 cp_parser_commit_to_tentative_parse (parser);
8129 if (decl_specs->storage_class != sc_none)
8130 decl_specs->conflicting_specifiers_p = true;
8131 break;
8133 /* storage-class-specifier:
8134 auto
8135 register
8136 static
8137 extern
8138 mutable
8140 GNU Extension:
8141 thread */
8142 case RID_AUTO:
8143 case RID_REGISTER:
8144 case RID_STATIC:
8145 case RID_EXTERN:
8146 case RID_MUTABLE:
8147 /* Consume the token. */
8148 cp_lexer_consume_token (parser->lexer);
8149 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8150 break;
8151 case RID_THREAD:
8152 /* Consume the token. */
8153 cp_lexer_consume_token (parser->lexer);
8154 ++decl_specs->specs[(int) ds_thread];
8155 break;
8157 default:
8158 /* We did not yet find a decl-specifier yet. */
8159 found_decl_spec = false;
8160 break;
8163 /* Constructors are a special case. The `S' in `S()' is not a
8164 decl-specifier; it is the beginning of the declarator. */
8165 constructor_p
8166 = (!found_decl_spec
8167 && constructor_possible_p
8168 && (cp_parser_constructor_declarator_p
8169 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8171 /* If we don't have a DECL_SPEC yet, then we must be looking at
8172 a type-specifier. */
8173 if (!found_decl_spec && !constructor_p)
8175 int decl_spec_declares_class_or_enum;
8176 bool is_cv_qualifier;
8177 tree type_spec;
8179 type_spec
8180 = cp_parser_type_specifier (parser, flags,
8181 decl_specs,
8182 /*is_declaration=*/true,
8183 &decl_spec_declares_class_or_enum,
8184 &is_cv_qualifier);
8186 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8188 /* If this type-specifier referenced a user-defined type
8189 (a typedef, class-name, etc.), then we can't allow any
8190 more such type-specifiers henceforth.
8192 [dcl.spec]
8194 The longest sequence of decl-specifiers that could
8195 possibly be a type name is taken as the
8196 decl-specifier-seq of a declaration. The sequence shall
8197 be self-consistent as described below.
8199 [dcl.type]
8201 As a general rule, at most one type-specifier is allowed
8202 in the complete decl-specifier-seq of a declaration. The
8203 only exceptions are the following:
8205 -- const or volatile can be combined with any other
8206 type-specifier.
8208 -- signed or unsigned can be combined with char, long,
8209 short, or int.
8211 -- ..
8213 Example:
8215 typedef char* Pc;
8216 void g (const int Pc);
8218 Here, Pc is *not* part of the decl-specifier seq; it's
8219 the declarator. Therefore, once we see a type-specifier
8220 (other than a cv-qualifier), we forbid any additional
8221 user-defined types. We *do* still allow things like `int
8222 int' to be considered a decl-specifier-seq, and issue the
8223 error message later. */
8224 if (type_spec && !is_cv_qualifier)
8225 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8226 /* A constructor declarator cannot follow a type-specifier. */
8227 if (type_spec)
8229 constructor_possible_p = false;
8230 found_decl_spec = true;
8234 /* If we still do not have a DECL_SPEC, then there are no more
8235 decl-specifiers. */
8236 if (!found_decl_spec)
8237 break;
8239 decl_specs->any_specifiers_p = true;
8240 /* After we see one decl-specifier, further decl-specifiers are
8241 always optional. */
8242 flags |= CP_PARSER_FLAGS_OPTIONAL;
8245 cp_parser_check_decl_spec (decl_specs);
8247 /* Don't allow a friend specifier with a class definition. */
8248 if (decl_specs->specs[(int) ds_friend] != 0
8249 && (*declares_class_or_enum & 2))
8250 error ("class definition may not be declared a friend");
8253 /* Parse an (optional) storage-class-specifier.
8255 storage-class-specifier:
8256 auto
8257 register
8258 static
8259 extern
8260 mutable
8262 GNU Extension:
8264 storage-class-specifier:
8265 thread
8267 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8269 static tree
8270 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8272 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8274 case RID_AUTO:
8275 case RID_REGISTER:
8276 case RID_STATIC:
8277 case RID_EXTERN:
8278 case RID_MUTABLE:
8279 case RID_THREAD:
8280 /* Consume the token. */
8281 return cp_lexer_consume_token (parser->lexer)->u.value;
8283 default:
8284 return NULL_TREE;
8288 /* Parse an (optional) function-specifier.
8290 function-specifier:
8291 inline
8292 virtual
8293 explicit
8295 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8296 Updates DECL_SPECS, if it is non-NULL. */
8298 static tree
8299 cp_parser_function_specifier_opt (cp_parser* parser,
8300 cp_decl_specifier_seq *decl_specs)
8302 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8304 case RID_INLINE:
8305 if (decl_specs)
8306 ++decl_specs->specs[(int) ds_inline];
8307 break;
8309 case RID_VIRTUAL:
8310 /* 14.5.2.3 [temp.mem]
8312 A member function template shall not be virtual. */
8313 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8314 error ("templates may not be %<virtual%>");
8315 else if (decl_specs)
8316 ++decl_specs->specs[(int) ds_virtual];
8317 break;
8319 case RID_EXPLICIT:
8320 if (decl_specs)
8321 ++decl_specs->specs[(int) ds_explicit];
8322 break;
8324 default:
8325 return NULL_TREE;
8328 /* Consume the token. */
8329 return cp_lexer_consume_token (parser->lexer)->u.value;
8332 /* Parse a linkage-specification.
8334 linkage-specification:
8335 extern string-literal { declaration-seq [opt] }
8336 extern string-literal declaration */
8338 static void
8339 cp_parser_linkage_specification (cp_parser* parser)
8341 tree linkage;
8343 /* Look for the `extern' keyword. */
8344 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8346 /* Look for the string-literal. */
8347 linkage = cp_parser_string_literal (parser, false, false);
8349 /* Transform the literal into an identifier. If the literal is a
8350 wide-character string, or contains embedded NULs, then we can't
8351 handle it as the user wants. */
8352 if (strlen (TREE_STRING_POINTER (linkage))
8353 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8355 cp_parser_error (parser, "invalid linkage-specification");
8356 /* Assume C++ linkage. */
8357 linkage = lang_name_cplusplus;
8359 else
8360 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8362 /* We're now using the new linkage. */
8363 push_lang_context (linkage);
8365 /* If the next token is a `{', then we're using the first
8366 production. */
8367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8369 /* Consume the `{' token. */
8370 cp_lexer_consume_token (parser->lexer);
8371 /* Parse the declarations. */
8372 cp_parser_declaration_seq_opt (parser);
8373 /* Look for the closing `}'. */
8374 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8376 /* Otherwise, there's just one declaration. */
8377 else
8379 bool saved_in_unbraced_linkage_specification_p;
8381 saved_in_unbraced_linkage_specification_p
8382 = parser->in_unbraced_linkage_specification_p;
8383 parser->in_unbraced_linkage_specification_p = true;
8384 cp_parser_declaration (parser);
8385 parser->in_unbraced_linkage_specification_p
8386 = saved_in_unbraced_linkage_specification_p;
8389 /* We're done with the linkage-specification. */
8390 pop_lang_context ();
8393 /* Parse a static_assert-declaration.
8395 static_assert-declaration:
8396 static_assert ( constant-expression , string-literal ) ;
8398 If MEMBER_P, this static_assert is a class member. */
8400 static void
8401 cp_parser_static_assert(cp_parser *parser, bool member_p)
8403 tree condition;
8404 tree message;
8405 cp_token *token;
8406 location_t saved_loc;
8408 /* Peek at the `static_assert' token so we can keep track of exactly
8409 where the static assertion started. */
8410 token = cp_lexer_peek_token (parser->lexer);
8411 saved_loc = token->location;
8413 /* Look for the `static_assert' keyword. */
8414 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8415 "`static_assert'"))
8416 return;
8418 /* We know we are in a static assertion; commit to any tentative
8419 parse. */
8420 if (cp_parser_parsing_tentatively (parser))
8421 cp_parser_commit_to_tentative_parse (parser);
8423 /* Parse the `(' starting the static assertion condition. */
8424 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8426 /* Parse the constant-expression. */
8427 condition =
8428 cp_parser_constant_expression (parser,
8429 /*allow_non_constant_p=*/false,
8430 /*non_constant_p=*/NULL);
8432 /* Parse the separating `,'. */
8433 cp_parser_require (parser, CPP_COMMA, "`,'");
8435 /* Parse the string-literal message. */
8436 message = cp_parser_string_literal (parser,
8437 /*translate=*/false,
8438 /*wide_ok=*/true);
8440 /* A `)' completes the static assertion. */
8441 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8442 cp_parser_skip_to_closing_parenthesis (parser,
8443 /*recovering=*/true,
8444 /*or_comma=*/false,
8445 /*consume_paren=*/true);
8447 /* A semicolon terminates the declaration. */
8448 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8450 /* Complete the static assertion, which may mean either processing
8451 the static assert now or saving it for template instantiation. */
8452 finish_static_assert (condition, message, saved_loc, member_p);
8455 /* Parse a `decltype' type. Returns the type.
8457 simple-type-specifier:
8458 decltype ( expression ) */
8460 static tree
8461 cp_parser_decltype (cp_parser *parser)
8463 tree expr;
8464 bool id_expression_or_member_access_p = false;
8465 const char *saved_message;
8466 bool saved_integral_constant_expression_p;
8467 bool saved_non_integral_constant_expression_p;
8469 /* Look for the `decltype' token. */
8470 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "`decltype'"))
8471 return error_mark_node;
8473 /* Types cannot be defined in a `decltype' expression. Save away the
8474 old message. */
8475 saved_message = parser->type_definition_forbidden_message;
8477 /* And create the new one. */
8478 parser->type_definition_forbidden_message
8479 = "types may not be defined in `decltype' expressions";
8481 /* The restrictions on constant-expressions do not apply inside
8482 decltype expressions. */
8483 saved_integral_constant_expression_p
8484 = parser->integral_constant_expression_p;
8485 saved_non_integral_constant_expression_p
8486 = parser->non_integral_constant_expression_p;
8487 parser->integral_constant_expression_p = false;
8489 /* Do not actually evaluate the expression. */
8490 ++skip_evaluation;
8492 /* Parse the opening `('. */
8493 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
8494 return error_mark_node;
8496 /* First, try parsing an id-expression. */
8497 cp_parser_parse_tentatively (parser);
8498 expr = cp_parser_id_expression (parser,
8499 /*template_keyword_p=*/false,
8500 /*check_dependency_p=*/true,
8501 /*template_p=*/NULL,
8502 /*declarator_p=*/false,
8503 /*optional_p=*/false);
8505 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8507 bool non_integral_constant_expression_p = false;
8508 tree id_expression = expr;
8509 cp_id_kind idk;
8510 const char *error_msg;
8512 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8513 /* Lookup the name we got back from the id-expression. */
8514 expr = cp_parser_lookup_name (parser, expr,
8515 none_type,
8516 /*is_template=*/false,
8517 /*is_namespace=*/false,
8518 /*check_dependency=*/true,
8519 /*ambiguous_decls=*/NULL);
8521 if (expr
8522 && expr != error_mark_node
8523 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8524 && TREE_CODE (expr) != TYPE_DECL
8525 && (TREE_CODE (expr) != BIT_NOT_EXPR
8526 || !TYPE_P (TREE_OPERAND (expr, 0)))
8527 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8529 /* Complete lookup of the id-expression. */
8530 expr = (finish_id_expression
8531 (id_expression, expr, parser->scope, &idk,
8532 /*integral_constant_expression_p=*/false,
8533 /*allow_non_integral_constant_expression_p=*/true,
8534 &non_integral_constant_expression_p,
8535 /*template_p=*/false,
8536 /*done=*/true,
8537 /*address_p=*/false,
8538 /*template_arg_p=*/false,
8539 &error_msg));
8541 if (expr == error_mark_node)
8542 /* We found an id-expression, but it was something that we
8543 should not have found. This is an error, not something
8544 we can recover from, so note that we found an
8545 id-expression and we'll recover as gracefully as
8546 possible. */
8547 id_expression_or_member_access_p = true;
8550 if (expr
8551 && expr != error_mark_node
8552 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8553 /* We have an id-expression. */
8554 id_expression_or_member_access_p = true;
8557 if (!id_expression_or_member_access_p)
8559 /* Abort the id-expression parse. */
8560 cp_parser_abort_tentative_parse (parser);
8562 /* Parsing tentatively, again. */
8563 cp_parser_parse_tentatively (parser);
8565 /* Parse a class member access. */
8566 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8567 /*cast_p=*/false,
8568 /*member_access_only_p=*/true);
8570 if (expr
8571 && expr != error_mark_node
8572 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8573 /* We have an id-expression. */
8574 id_expression_or_member_access_p = true;
8577 if (id_expression_or_member_access_p)
8578 /* We have parsed the complete id-expression or member access. */
8579 cp_parser_parse_definitely (parser);
8580 else
8582 /* Abort our attempt to parse an id-expression or member access
8583 expression. */
8584 cp_parser_abort_tentative_parse (parser);
8586 /* Parse a full expression. */
8587 expr = cp_parser_expression (parser, /*cast_p=*/false);
8590 /* Go back to evaluating expressions. */
8591 --skip_evaluation;
8593 /* Restore the old message and the integral constant expression
8594 flags. */
8595 parser->type_definition_forbidden_message = saved_message;
8596 parser->integral_constant_expression_p
8597 = saved_integral_constant_expression_p;
8598 parser->non_integral_constant_expression_p
8599 = saved_non_integral_constant_expression_p;
8601 if (expr == error_mark_node)
8603 /* Skip everything up to the closing `)'. */
8604 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8605 /*consume_paren=*/true);
8606 return error_mark_node;
8609 /* Parse to the closing `)'. */
8610 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8612 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8613 /*consume_paren=*/true);
8614 return error_mark_node;
8617 return finish_decltype_type (expr, id_expression_or_member_access_p);
8620 /* Special member functions [gram.special] */
8622 /* Parse a conversion-function-id.
8624 conversion-function-id:
8625 operator conversion-type-id
8627 Returns an IDENTIFIER_NODE representing the operator. */
8629 static tree
8630 cp_parser_conversion_function_id (cp_parser* parser)
8632 tree type;
8633 tree saved_scope;
8634 tree saved_qualifying_scope;
8635 tree saved_object_scope;
8636 tree pushed_scope = NULL_TREE;
8638 /* Look for the `operator' token. */
8639 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8640 return error_mark_node;
8641 /* When we parse the conversion-type-id, the current scope will be
8642 reset. However, we need that information in able to look up the
8643 conversion function later, so we save it here. */
8644 saved_scope = parser->scope;
8645 saved_qualifying_scope = parser->qualifying_scope;
8646 saved_object_scope = parser->object_scope;
8647 /* We must enter the scope of the class so that the names of
8648 entities declared within the class are available in the
8649 conversion-type-id. For example, consider:
8651 struct S {
8652 typedef int I;
8653 operator I();
8656 S::operator I() { ... }
8658 In order to see that `I' is a type-name in the definition, we
8659 must be in the scope of `S'. */
8660 if (saved_scope)
8661 pushed_scope = push_scope (saved_scope);
8662 /* Parse the conversion-type-id. */
8663 type = cp_parser_conversion_type_id (parser);
8664 /* Leave the scope of the class, if any. */
8665 if (pushed_scope)
8666 pop_scope (pushed_scope);
8667 /* Restore the saved scope. */
8668 parser->scope = saved_scope;
8669 parser->qualifying_scope = saved_qualifying_scope;
8670 parser->object_scope = saved_object_scope;
8671 /* If the TYPE is invalid, indicate failure. */
8672 if (type == error_mark_node)
8673 return error_mark_node;
8674 return mangle_conv_op_name_for_type (type);
8677 /* Parse a conversion-type-id:
8679 conversion-type-id:
8680 type-specifier-seq conversion-declarator [opt]
8682 Returns the TYPE specified. */
8684 static tree
8685 cp_parser_conversion_type_id (cp_parser* parser)
8687 tree attributes;
8688 cp_decl_specifier_seq type_specifiers;
8689 cp_declarator *declarator;
8690 tree type_specified;
8692 /* Parse the attributes. */
8693 attributes = cp_parser_attributes_opt (parser);
8694 /* Parse the type-specifiers. */
8695 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8696 &type_specifiers);
8697 /* If that didn't work, stop. */
8698 if (type_specifiers.type == error_mark_node)
8699 return error_mark_node;
8700 /* Parse the conversion-declarator. */
8701 declarator = cp_parser_conversion_declarator_opt (parser);
8703 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8704 /*initialized=*/0, &attributes);
8705 if (attributes)
8706 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8707 return type_specified;
8710 /* Parse an (optional) conversion-declarator.
8712 conversion-declarator:
8713 ptr-operator conversion-declarator [opt]
8717 static cp_declarator *
8718 cp_parser_conversion_declarator_opt (cp_parser* parser)
8720 enum tree_code code;
8721 tree class_type;
8722 cp_cv_quals cv_quals;
8724 /* We don't know if there's a ptr-operator next, or not. */
8725 cp_parser_parse_tentatively (parser);
8726 /* Try the ptr-operator. */
8727 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8728 /* If it worked, look for more conversion-declarators. */
8729 if (cp_parser_parse_definitely (parser))
8731 cp_declarator *declarator;
8733 /* Parse another optional declarator. */
8734 declarator = cp_parser_conversion_declarator_opt (parser);
8736 return cp_parser_make_indirect_declarator
8737 (code, class_type, cv_quals, declarator);
8740 return NULL;
8743 /* Parse an (optional) ctor-initializer.
8745 ctor-initializer:
8746 : mem-initializer-list
8748 Returns TRUE iff the ctor-initializer was actually present. */
8750 static bool
8751 cp_parser_ctor_initializer_opt (cp_parser* parser)
8753 /* If the next token is not a `:', then there is no
8754 ctor-initializer. */
8755 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8757 /* Do default initialization of any bases and members. */
8758 if (DECL_CONSTRUCTOR_P (current_function_decl))
8759 finish_mem_initializers (NULL_TREE);
8761 return false;
8764 /* Consume the `:' token. */
8765 cp_lexer_consume_token (parser->lexer);
8766 /* And the mem-initializer-list. */
8767 cp_parser_mem_initializer_list (parser);
8769 return true;
8772 /* Parse a mem-initializer-list.
8774 mem-initializer-list:
8775 mem-initializer ... [opt]
8776 mem-initializer ... [opt] , mem-initializer-list */
8778 static void
8779 cp_parser_mem_initializer_list (cp_parser* parser)
8781 tree mem_initializer_list = NULL_TREE;
8783 /* Let the semantic analysis code know that we are starting the
8784 mem-initializer-list. */
8785 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8786 error ("only constructors take base initializers");
8788 /* Loop through the list. */
8789 while (true)
8791 tree mem_initializer;
8793 /* Parse the mem-initializer. */
8794 mem_initializer = cp_parser_mem_initializer (parser);
8795 /* If the next token is a `...', we're expanding member initializers. */
8796 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8798 /* Consume the `...'. */
8799 cp_lexer_consume_token (parser->lexer);
8801 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8802 can be expanded but members cannot. */
8803 if (mem_initializer != error_mark_node
8804 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8806 error ("cannot expand initializer for member %<%D%>",
8807 TREE_PURPOSE (mem_initializer));
8808 mem_initializer = error_mark_node;
8811 /* Construct the pack expansion type. */
8812 if (mem_initializer != error_mark_node)
8813 mem_initializer = make_pack_expansion (mem_initializer);
8815 /* Add it to the list, unless it was erroneous. */
8816 if (mem_initializer != error_mark_node)
8818 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8819 mem_initializer_list = mem_initializer;
8821 /* If the next token is not a `,', we're done. */
8822 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8823 break;
8824 /* Consume the `,' token. */
8825 cp_lexer_consume_token (parser->lexer);
8828 /* Perform semantic analysis. */
8829 if (DECL_CONSTRUCTOR_P (current_function_decl))
8830 finish_mem_initializers (mem_initializer_list);
8833 /* Parse a mem-initializer.
8835 mem-initializer:
8836 mem-initializer-id ( expression-list [opt] )
8838 GNU extension:
8840 mem-initializer:
8841 ( expression-list [opt] )
8843 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8844 class) or FIELD_DECL (for a non-static data member) to initialize;
8845 the TREE_VALUE is the expression-list. An empty initialization
8846 list is represented by void_list_node. */
8848 static tree
8849 cp_parser_mem_initializer (cp_parser* parser)
8851 tree mem_initializer_id;
8852 tree expression_list;
8853 tree member;
8855 /* Find out what is being initialized. */
8856 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8858 pedwarn ("anachronistic old-style base class initializer");
8859 mem_initializer_id = NULL_TREE;
8861 else
8862 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8863 member = expand_member_init (mem_initializer_id);
8864 if (member && !DECL_P (member))
8865 in_base_initializer = 1;
8867 expression_list
8868 = cp_parser_parenthesized_expression_list (parser, false,
8869 /*cast_p=*/false,
8870 /*allow_expansion_p=*/true,
8871 /*non_constant_p=*/NULL);
8872 if (expression_list == error_mark_node)
8873 return error_mark_node;
8874 if (!expression_list)
8875 expression_list = void_type_node;
8877 in_base_initializer = 0;
8879 return member ? build_tree_list (member, expression_list) : error_mark_node;
8882 /* Parse a mem-initializer-id.
8884 mem-initializer-id:
8885 :: [opt] nested-name-specifier [opt] class-name
8886 identifier
8888 Returns a TYPE indicating the class to be initializer for the first
8889 production. Returns an IDENTIFIER_NODE indicating the data member
8890 to be initialized for the second production. */
8892 static tree
8893 cp_parser_mem_initializer_id (cp_parser* parser)
8895 bool global_scope_p;
8896 bool nested_name_specifier_p;
8897 bool template_p = false;
8898 tree id;
8900 /* `typename' is not allowed in this context ([temp.res]). */
8901 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8903 error ("keyword %<typename%> not allowed in this context (a qualified "
8904 "member initializer is implicitly a type)");
8905 cp_lexer_consume_token (parser->lexer);
8907 /* Look for the optional `::' operator. */
8908 global_scope_p
8909 = (cp_parser_global_scope_opt (parser,
8910 /*current_scope_valid_p=*/false)
8911 != NULL_TREE);
8912 /* Look for the optional nested-name-specifier. The simplest way to
8913 implement:
8915 [temp.res]
8917 The keyword `typename' is not permitted in a base-specifier or
8918 mem-initializer; in these contexts a qualified name that
8919 depends on a template-parameter is implicitly assumed to be a
8920 type name.
8922 is to assume that we have seen the `typename' keyword at this
8923 point. */
8924 nested_name_specifier_p
8925 = (cp_parser_nested_name_specifier_opt (parser,
8926 /*typename_keyword_p=*/true,
8927 /*check_dependency_p=*/true,
8928 /*type_p=*/true,
8929 /*is_declaration=*/true)
8930 != NULL_TREE);
8931 if (nested_name_specifier_p)
8932 template_p = cp_parser_optional_template_keyword (parser);
8933 /* If there is a `::' operator or a nested-name-specifier, then we
8934 are definitely looking for a class-name. */
8935 if (global_scope_p || nested_name_specifier_p)
8936 return cp_parser_class_name (parser,
8937 /*typename_keyword_p=*/true,
8938 /*template_keyword_p=*/template_p,
8939 none_type,
8940 /*check_dependency_p=*/true,
8941 /*class_head_p=*/false,
8942 /*is_declaration=*/true);
8943 /* Otherwise, we could also be looking for an ordinary identifier. */
8944 cp_parser_parse_tentatively (parser);
8945 /* Try a class-name. */
8946 id = cp_parser_class_name (parser,
8947 /*typename_keyword_p=*/true,
8948 /*template_keyword_p=*/false,
8949 none_type,
8950 /*check_dependency_p=*/true,
8951 /*class_head_p=*/false,
8952 /*is_declaration=*/true);
8953 /* If we found one, we're done. */
8954 if (cp_parser_parse_definitely (parser))
8955 return id;
8956 /* Otherwise, look for an ordinary identifier. */
8957 return cp_parser_identifier (parser);
8960 /* Overloading [gram.over] */
8962 /* Parse an operator-function-id.
8964 operator-function-id:
8965 operator operator
8967 Returns an IDENTIFIER_NODE for the operator which is a
8968 human-readable spelling of the identifier, e.g., `operator +'. */
8970 static tree
8971 cp_parser_operator_function_id (cp_parser* parser)
8973 /* Look for the `operator' keyword. */
8974 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8975 return error_mark_node;
8976 /* And then the name of the operator itself. */
8977 return cp_parser_operator (parser);
8980 /* Parse an operator.
8982 operator:
8983 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8984 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8985 || ++ -- , ->* -> () []
8987 GNU Extensions:
8989 operator:
8990 <? >? <?= >?=
8992 Returns an IDENTIFIER_NODE for the operator which is a
8993 human-readable spelling of the identifier, e.g., `operator +'. */
8995 static tree
8996 cp_parser_operator (cp_parser* parser)
8998 tree id = NULL_TREE;
8999 cp_token *token;
9001 /* Peek at the next token. */
9002 token = cp_lexer_peek_token (parser->lexer);
9003 /* Figure out which operator we have. */
9004 switch (token->type)
9006 case CPP_KEYWORD:
9008 enum tree_code op;
9010 /* The keyword should be either `new' or `delete'. */
9011 if (token->keyword == RID_NEW)
9012 op = NEW_EXPR;
9013 else if (token->keyword == RID_DELETE)
9014 op = DELETE_EXPR;
9015 else
9016 break;
9018 /* Consume the `new' or `delete' token. */
9019 cp_lexer_consume_token (parser->lexer);
9021 /* Peek at the next token. */
9022 token = cp_lexer_peek_token (parser->lexer);
9023 /* If it's a `[' token then this is the array variant of the
9024 operator. */
9025 if (token->type == CPP_OPEN_SQUARE)
9027 /* Consume the `[' token. */
9028 cp_lexer_consume_token (parser->lexer);
9029 /* Look for the `]' token. */
9030 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9031 id = ansi_opname (op == NEW_EXPR
9032 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9034 /* Otherwise, we have the non-array variant. */
9035 else
9036 id = ansi_opname (op);
9038 return id;
9041 case CPP_PLUS:
9042 id = ansi_opname (PLUS_EXPR);
9043 break;
9045 case CPP_MINUS:
9046 id = ansi_opname (MINUS_EXPR);
9047 break;
9049 case CPP_MULT:
9050 id = ansi_opname (MULT_EXPR);
9051 break;
9053 case CPP_DIV:
9054 id = ansi_opname (TRUNC_DIV_EXPR);
9055 break;
9057 case CPP_MOD:
9058 id = ansi_opname (TRUNC_MOD_EXPR);
9059 break;
9061 case CPP_XOR:
9062 id = ansi_opname (BIT_XOR_EXPR);
9063 break;
9065 case CPP_AND:
9066 id = ansi_opname (BIT_AND_EXPR);
9067 break;
9069 case CPP_OR:
9070 id = ansi_opname (BIT_IOR_EXPR);
9071 break;
9073 case CPP_COMPL:
9074 id = ansi_opname (BIT_NOT_EXPR);
9075 break;
9077 case CPP_NOT:
9078 id = ansi_opname (TRUTH_NOT_EXPR);
9079 break;
9081 case CPP_EQ:
9082 id = ansi_assopname (NOP_EXPR);
9083 break;
9085 case CPP_LESS:
9086 id = ansi_opname (LT_EXPR);
9087 break;
9089 case CPP_GREATER:
9090 id = ansi_opname (GT_EXPR);
9091 break;
9093 case CPP_PLUS_EQ:
9094 id = ansi_assopname (PLUS_EXPR);
9095 break;
9097 case CPP_MINUS_EQ:
9098 id = ansi_assopname (MINUS_EXPR);
9099 break;
9101 case CPP_MULT_EQ:
9102 id = ansi_assopname (MULT_EXPR);
9103 break;
9105 case CPP_DIV_EQ:
9106 id = ansi_assopname (TRUNC_DIV_EXPR);
9107 break;
9109 case CPP_MOD_EQ:
9110 id = ansi_assopname (TRUNC_MOD_EXPR);
9111 break;
9113 case CPP_XOR_EQ:
9114 id = ansi_assopname (BIT_XOR_EXPR);
9115 break;
9117 case CPP_AND_EQ:
9118 id = ansi_assopname (BIT_AND_EXPR);
9119 break;
9121 case CPP_OR_EQ:
9122 id = ansi_assopname (BIT_IOR_EXPR);
9123 break;
9125 case CPP_LSHIFT:
9126 id = ansi_opname (LSHIFT_EXPR);
9127 break;
9129 case CPP_RSHIFT:
9130 id = ansi_opname (RSHIFT_EXPR);
9131 break;
9133 case CPP_LSHIFT_EQ:
9134 id = ansi_assopname (LSHIFT_EXPR);
9135 break;
9137 case CPP_RSHIFT_EQ:
9138 id = ansi_assopname (RSHIFT_EXPR);
9139 break;
9141 case CPP_EQ_EQ:
9142 id = ansi_opname (EQ_EXPR);
9143 break;
9145 case CPP_NOT_EQ:
9146 id = ansi_opname (NE_EXPR);
9147 break;
9149 case CPP_LESS_EQ:
9150 id = ansi_opname (LE_EXPR);
9151 break;
9153 case CPP_GREATER_EQ:
9154 id = ansi_opname (GE_EXPR);
9155 break;
9157 case CPP_AND_AND:
9158 id = ansi_opname (TRUTH_ANDIF_EXPR);
9159 break;
9161 case CPP_OR_OR:
9162 id = ansi_opname (TRUTH_ORIF_EXPR);
9163 break;
9165 case CPP_PLUS_PLUS:
9166 id = ansi_opname (POSTINCREMENT_EXPR);
9167 break;
9169 case CPP_MINUS_MINUS:
9170 id = ansi_opname (PREDECREMENT_EXPR);
9171 break;
9173 case CPP_COMMA:
9174 id = ansi_opname (COMPOUND_EXPR);
9175 break;
9177 case CPP_DEREF_STAR:
9178 id = ansi_opname (MEMBER_REF);
9179 break;
9181 case CPP_DEREF:
9182 id = ansi_opname (COMPONENT_REF);
9183 break;
9185 case CPP_OPEN_PAREN:
9186 /* Consume the `('. */
9187 cp_lexer_consume_token (parser->lexer);
9188 /* Look for the matching `)'. */
9189 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9190 return ansi_opname (CALL_EXPR);
9192 case CPP_OPEN_SQUARE:
9193 /* Consume the `['. */
9194 cp_lexer_consume_token (parser->lexer);
9195 /* Look for the matching `]'. */
9196 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9197 return ansi_opname (ARRAY_REF);
9199 default:
9200 /* Anything else is an error. */
9201 break;
9204 /* If we have selected an identifier, we need to consume the
9205 operator token. */
9206 if (id)
9207 cp_lexer_consume_token (parser->lexer);
9208 /* Otherwise, no valid operator name was present. */
9209 else
9211 cp_parser_error (parser, "expected operator");
9212 id = error_mark_node;
9215 return id;
9218 /* Parse a template-declaration.
9220 template-declaration:
9221 export [opt] template < template-parameter-list > declaration
9223 If MEMBER_P is TRUE, this template-declaration occurs within a
9224 class-specifier.
9226 The grammar rule given by the standard isn't correct. What
9227 is really meant is:
9229 template-declaration:
9230 export [opt] template-parameter-list-seq
9231 decl-specifier-seq [opt] init-declarator [opt] ;
9232 export [opt] template-parameter-list-seq
9233 function-definition
9235 template-parameter-list-seq:
9236 template-parameter-list-seq [opt]
9237 template < template-parameter-list > */
9239 static void
9240 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9242 /* Check for `export'. */
9243 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9245 /* Consume the `export' token. */
9246 cp_lexer_consume_token (parser->lexer);
9247 /* Warn that we do not support `export'. */
9248 warning (0, "keyword %<export%> not implemented, and will be ignored");
9251 cp_parser_template_declaration_after_export (parser, member_p);
9254 /* Parse a template-parameter-list.
9256 template-parameter-list:
9257 template-parameter
9258 template-parameter-list , template-parameter
9260 Returns a TREE_LIST. Each node represents a template parameter.
9261 The nodes are connected via their TREE_CHAINs. */
9263 static tree
9264 cp_parser_template_parameter_list (cp_parser* parser)
9266 tree parameter_list = NULL_TREE;
9268 begin_template_parm_list ();
9269 while (true)
9271 tree parameter;
9272 cp_token *token;
9273 bool is_non_type;
9274 bool is_parameter_pack;
9276 /* Parse the template-parameter. */
9277 parameter = cp_parser_template_parameter (parser,
9278 &is_non_type,
9279 &is_parameter_pack);
9280 /* Add it to the list. */
9281 if (parameter != error_mark_node)
9282 parameter_list = process_template_parm (parameter_list,
9283 parameter,
9284 is_non_type,
9285 is_parameter_pack);
9286 else
9288 tree err_parm = build_tree_list (parameter, parameter);
9289 TREE_VALUE (err_parm) = error_mark_node;
9290 parameter_list = chainon (parameter_list, err_parm);
9293 /* Peek at the next token. */
9294 token = cp_lexer_peek_token (parser->lexer);
9295 /* If it's not a `,', we're done. */
9296 if (token->type != CPP_COMMA)
9297 break;
9298 /* Otherwise, consume the `,' token. */
9299 cp_lexer_consume_token (parser->lexer);
9302 return end_template_parm_list (parameter_list);
9305 /* Parse a template-parameter.
9307 template-parameter:
9308 type-parameter
9309 parameter-declaration
9311 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9312 the parameter. The TREE_PURPOSE is the default value, if any.
9313 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9314 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9315 set to true iff this parameter is a parameter pack. */
9317 static tree
9318 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9319 bool *is_parameter_pack)
9321 cp_token *token;
9322 cp_parameter_declarator *parameter_declarator;
9323 cp_declarator *id_declarator;
9324 tree parm;
9326 /* Assume it is a type parameter or a template parameter. */
9327 *is_non_type = false;
9328 /* Assume it not a parameter pack. */
9329 *is_parameter_pack = false;
9330 /* Peek at the next token. */
9331 token = cp_lexer_peek_token (parser->lexer);
9332 /* If it is `class' or `template', we have a type-parameter. */
9333 if (token->keyword == RID_TEMPLATE)
9334 return cp_parser_type_parameter (parser, is_parameter_pack);
9335 /* If it is `class' or `typename' we do not know yet whether it is a
9336 type parameter or a non-type parameter. Consider:
9338 template <typename T, typename T::X X> ...
9342 template <class C, class D*> ...
9344 Here, the first parameter is a type parameter, and the second is
9345 a non-type parameter. We can tell by looking at the token after
9346 the identifier -- if it is a `,', `=', or `>' then we have a type
9347 parameter. */
9348 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9350 /* Peek at the token after `class' or `typename'. */
9351 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9352 /* If it's an ellipsis, we have a template type parameter
9353 pack. */
9354 if (token->type == CPP_ELLIPSIS)
9355 return cp_parser_type_parameter (parser, is_parameter_pack);
9356 /* If it's an identifier, skip it. */
9357 if (token->type == CPP_NAME)
9358 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9359 /* Now, see if the token looks like the end of a template
9360 parameter. */
9361 if (token->type == CPP_COMMA
9362 || token->type == CPP_EQ
9363 || token->type == CPP_GREATER)
9364 return cp_parser_type_parameter (parser, is_parameter_pack);
9367 /* Otherwise, it is a non-type parameter.
9369 [temp.param]
9371 When parsing a default template-argument for a non-type
9372 template-parameter, the first non-nested `>' is taken as the end
9373 of the template parameter-list rather than a greater-than
9374 operator. */
9375 *is_non_type = true;
9376 parameter_declarator
9377 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9378 /*parenthesized_p=*/NULL);
9380 /* If the parameter declaration is marked as a parameter pack, set
9381 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9382 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9383 grokdeclarator. */
9384 if (parameter_declarator
9385 && parameter_declarator->declarator
9386 && parameter_declarator->declarator->parameter_pack_p)
9388 *is_parameter_pack = true;
9389 parameter_declarator->declarator->parameter_pack_p = false;
9392 /* If the next token is an ellipsis, and we don't already have it
9393 marked as a parameter pack, then we have a parameter pack (that
9394 has no declarator). */
9395 if (!*is_parameter_pack
9396 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9397 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9399 /* Consume the `...'. */
9400 cp_lexer_consume_token (parser->lexer);
9401 maybe_warn_variadic_templates ();
9403 *is_parameter_pack = true;
9405 /* Parameter packs cannot have default arguments. However, a
9406 user may try to do so, so we'll parse them and give an
9407 appropriate diagnostic here. */
9408 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9410 /* Consume the `='. */
9411 cp_lexer_consume_token (parser->lexer);
9413 /* Find the name of the parameter pack. */
9414 id_declarator = parameter_declarator->declarator;
9415 while (id_declarator && id_declarator->kind != cdk_id)
9416 id_declarator = id_declarator->declarator;
9418 if (id_declarator && id_declarator->kind == cdk_id)
9419 error ("template parameter pack %qD cannot have a default argument",
9420 id_declarator->u.id.unqualified_name);
9421 else
9422 error ("template parameter pack cannot have a default argument");
9424 /* Parse the default argument, but throw away the result. */
9425 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9429 parm = grokdeclarator (parameter_declarator->declarator,
9430 &parameter_declarator->decl_specifiers,
9431 PARM, /*initialized=*/0,
9432 /*attrlist=*/NULL);
9433 if (parm == error_mark_node)
9434 return error_mark_node;
9436 return build_tree_list (parameter_declarator->default_argument, parm);
9439 /* Parse a type-parameter.
9441 type-parameter:
9442 class identifier [opt]
9443 class identifier [opt] = type-id
9444 typename identifier [opt]
9445 typename identifier [opt] = type-id
9446 template < template-parameter-list > class identifier [opt]
9447 template < template-parameter-list > class identifier [opt]
9448 = id-expression
9450 GNU Extension (variadic templates):
9452 type-parameter:
9453 class ... identifier [opt]
9454 typename ... identifier [opt]
9456 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9457 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9458 the declaration of the parameter.
9460 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9462 static tree
9463 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9465 cp_token *token;
9466 tree parameter;
9468 /* Look for a keyword to tell us what kind of parameter this is. */
9469 token = cp_parser_require (parser, CPP_KEYWORD,
9470 "`class', `typename', or `template'");
9471 if (!token)
9472 return error_mark_node;
9474 switch (token->keyword)
9476 case RID_CLASS:
9477 case RID_TYPENAME:
9479 tree identifier;
9480 tree default_argument;
9482 /* If the next token is an ellipsis, we have a template
9483 argument pack. */
9484 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9486 /* Consume the `...' token. */
9487 cp_lexer_consume_token (parser->lexer);
9488 maybe_warn_variadic_templates ();
9490 *is_parameter_pack = true;
9493 /* If the next token is an identifier, then it names the
9494 parameter. */
9495 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9496 identifier = cp_parser_identifier (parser);
9497 else
9498 identifier = NULL_TREE;
9500 /* Create the parameter. */
9501 parameter = finish_template_type_parm (class_type_node, identifier);
9503 /* If the next token is an `=', we have a default argument. */
9504 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9506 /* Consume the `=' token. */
9507 cp_lexer_consume_token (parser->lexer);
9508 /* Parse the default-argument. */
9509 push_deferring_access_checks (dk_no_deferred);
9510 default_argument = cp_parser_type_id (parser);
9512 /* Template parameter packs cannot have default
9513 arguments. */
9514 if (*is_parameter_pack)
9516 if (identifier)
9517 error ("template parameter pack %qD cannot have a default argument",
9518 identifier);
9519 else
9520 error ("template parameter packs cannot have default arguments");
9521 default_argument = NULL_TREE;
9523 pop_deferring_access_checks ();
9525 else
9526 default_argument = NULL_TREE;
9528 /* Create the combined representation of the parameter and the
9529 default argument. */
9530 parameter = build_tree_list (default_argument, parameter);
9532 break;
9534 case RID_TEMPLATE:
9536 tree parameter_list;
9537 tree identifier;
9538 tree default_argument;
9540 /* Look for the `<'. */
9541 cp_parser_require (parser, CPP_LESS, "`<'");
9542 /* Parse the template-parameter-list. */
9543 parameter_list = cp_parser_template_parameter_list (parser);
9544 /* Look for the `>'. */
9545 cp_parser_require (parser, CPP_GREATER, "`>'");
9546 /* Look for the `class' keyword. */
9547 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9548 /* If the next token is an ellipsis, we have a template
9549 argument pack. */
9550 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9552 /* Consume the `...' token. */
9553 cp_lexer_consume_token (parser->lexer);
9554 maybe_warn_variadic_templates ();
9556 *is_parameter_pack = true;
9558 /* If the next token is an `=', then there is a
9559 default-argument. If the next token is a `>', we are at
9560 the end of the parameter-list. If the next token is a `,',
9561 then we are at the end of this parameter. */
9562 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9563 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9564 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9566 identifier = cp_parser_identifier (parser);
9567 /* Treat invalid names as if the parameter were nameless. */
9568 if (identifier == error_mark_node)
9569 identifier = NULL_TREE;
9571 else
9572 identifier = NULL_TREE;
9574 /* Create the template parameter. */
9575 parameter = finish_template_template_parm (class_type_node,
9576 identifier);
9578 /* If the next token is an `=', then there is a
9579 default-argument. */
9580 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9582 bool is_template;
9584 /* Consume the `='. */
9585 cp_lexer_consume_token (parser->lexer);
9586 /* Parse the id-expression. */
9587 push_deferring_access_checks (dk_no_deferred);
9588 default_argument
9589 = cp_parser_id_expression (parser,
9590 /*template_keyword_p=*/false,
9591 /*check_dependency_p=*/true,
9592 /*template_p=*/&is_template,
9593 /*declarator_p=*/false,
9594 /*optional_p=*/false);
9595 if (TREE_CODE (default_argument) == TYPE_DECL)
9596 /* If the id-expression was a template-id that refers to
9597 a template-class, we already have the declaration here,
9598 so no further lookup is needed. */
9600 else
9601 /* Look up the name. */
9602 default_argument
9603 = cp_parser_lookup_name (parser, default_argument,
9604 none_type,
9605 /*is_template=*/is_template,
9606 /*is_namespace=*/false,
9607 /*check_dependency=*/true,
9608 /*ambiguous_decls=*/NULL);
9609 /* See if the default argument is valid. */
9610 default_argument
9611 = check_template_template_default_arg (default_argument);
9613 /* Template parameter packs cannot have default
9614 arguments. */
9615 if (*is_parameter_pack)
9617 if (identifier)
9618 error ("template parameter pack %qD cannot have a default argument",
9619 identifier);
9620 else
9621 error ("template parameter packs cannot have default arguments");
9622 default_argument = NULL_TREE;
9624 pop_deferring_access_checks ();
9626 else
9627 default_argument = NULL_TREE;
9629 /* Create the combined representation of the parameter and the
9630 default argument. */
9631 parameter = build_tree_list (default_argument, parameter);
9633 break;
9635 default:
9636 gcc_unreachable ();
9637 break;
9640 return parameter;
9643 /* Parse a template-id.
9645 template-id:
9646 template-name < template-argument-list [opt] >
9648 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9649 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9650 returned. Otherwise, if the template-name names a function, or set
9651 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9652 names a class, returns a TYPE_DECL for the specialization.
9654 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9655 uninstantiated templates. */
9657 static tree
9658 cp_parser_template_id (cp_parser *parser,
9659 bool template_keyword_p,
9660 bool check_dependency_p,
9661 bool is_declaration)
9663 int i;
9664 tree template;
9665 tree arguments;
9666 tree template_id;
9667 cp_token_position start_of_id = 0;
9668 deferred_access_check *chk;
9669 VEC (deferred_access_check,gc) *access_check;
9670 cp_token *next_token, *next_token_2;
9671 bool is_identifier;
9673 /* If the next token corresponds to a template-id, there is no need
9674 to reparse it. */
9675 next_token = cp_lexer_peek_token (parser->lexer);
9676 if (next_token->type == CPP_TEMPLATE_ID)
9678 struct tree_check *check_value;
9680 /* Get the stored value. */
9681 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9682 /* Perform any access checks that were deferred. */
9683 access_check = check_value->checks;
9684 if (access_check)
9686 for (i = 0 ;
9687 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9688 ++i)
9690 perform_or_defer_access_check (chk->binfo,
9691 chk->decl,
9692 chk->diag_decl);
9695 /* Return the stored value. */
9696 return check_value->value;
9699 /* Avoid performing name lookup if there is no possibility of
9700 finding a template-id. */
9701 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9702 || (next_token->type == CPP_NAME
9703 && !cp_parser_nth_token_starts_template_argument_list_p
9704 (parser, 2)))
9706 cp_parser_error (parser, "expected template-id");
9707 return error_mark_node;
9710 /* Remember where the template-id starts. */
9711 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9712 start_of_id = cp_lexer_token_position (parser->lexer, false);
9714 push_deferring_access_checks (dk_deferred);
9716 /* Parse the template-name. */
9717 is_identifier = false;
9718 template = cp_parser_template_name (parser, template_keyword_p,
9719 check_dependency_p,
9720 is_declaration,
9721 &is_identifier);
9722 if (template == error_mark_node || is_identifier)
9724 pop_deferring_access_checks ();
9725 return template;
9728 /* If we find the sequence `[:' after a template-name, it's probably
9729 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9730 parse correctly the argument list. */
9731 next_token = cp_lexer_peek_token (parser->lexer);
9732 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9733 if (next_token->type == CPP_OPEN_SQUARE
9734 && next_token->flags & DIGRAPH
9735 && next_token_2->type == CPP_COLON
9736 && !(next_token_2->flags & PREV_WHITE))
9738 cp_parser_parse_tentatively (parser);
9739 /* Change `:' into `::'. */
9740 next_token_2->type = CPP_SCOPE;
9741 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9742 CPP_LESS. */
9743 cp_lexer_consume_token (parser->lexer);
9744 /* Parse the arguments. */
9745 arguments = cp_parser_enclosed_template_argument_list (parser);
9746 if (!cp_parser_parse_definitely (parser))
9748 /* If we couldn't parse an argument list, then we revert our changes
9749 and return simply an error. Maybe this is not a template-id
9750 after all. */
9751 next_token_2->type = CPP_COLON;
9752 cp_parser_error (parser, "expected %<<%>");
9753 pop_deferring_access_checks ();
9754 return error_mark_node;
9756 /* Otherwise, emit an error about the invalid digraph, but continue
9757 parsing because we got our argument list. */
9758 pedwarn ("%<<::%> cannot begin a template-argument list");
9759 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9760 "between %<<%> and %<::%>");
9761 if (!flag_permissive)
9763 static bool hint;
9764 if (!hint)
9766 inform ("(if you use -fpermissive G++ will accept your code)");
9767 hint = true;
9771 else
9773 /* Look for the `<' that starts the template-argument-list. */
9774 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9776 pop_deferring_access_checks ();
9777 return error_mark_node;
9779 /* Parse the arguments. */
9780 arguments = cp_parser_enclosed_template_argument_list (parser);
9783 /* Build a representation of the specialization. */
9784 if (TREE_CODE (template) == IDENTIFIER_NODE)
9785 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9786 else if (DECL_CLASS_TEMPLATE_P (template)
9787 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9789 bool entering_scope;
9790 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9791 template (rather than some instantiation thereof) only if
9792 is not nested within some other construct. For example, in
9793 "template <typename T> void f(T) { A<T>::", A<T> is just an
9794 instantiation of A. */
9795 entering_scope = (template_parm_scope_p ()
9796 && cp_lexer_next_token_is (parser->lexer,
9797 CPP_SCOPE));
9798 template_id
9799 = finish_template_type (template, arguments, entering_scope);
9801 else
9803 /* If it's not a class-template or a template-template, it should be
9804 a function-template. */
9805 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9806 || TREE_CODE (template) == OVERLOAD
9807 || BASELINK_P (template)));
9809 template_id = lookup_template_function (template, arguments);
9812 /* If parsing tentatively, replace the sequence of tokens that makes
9813 up the template-id with a CPP_TEMPLATE_ID token. That way,
9814 should we re-parse the token stream, we will not have to repeat
9815 the effort required to do the parse, nor will we issue duplicate
9816 error messages about problems during instantiation of the
9817 template. */
9818 if (start_of_id)
9820 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9822 /* Reset the contents of the START_OF_ID token. */
9823 token->type = CPP_TEMPLATE_ID;
9824 /* Retrieve any deferred checks. Do not pop this access checks yet
9825 so the memory will not be reclaimed during token replacing below. */
9826 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9827 token->u.tree_check_value->value = template_id;
9828 token->u.tree_check_value->checks = get_deferred_access_checks ();
9829 token->keyword = RID_MAX;
9831 /* Purge all subsequent tokens. */
9832 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9834 /* ??? Can we actually assume that, if template_id ==
9835 error_mark_node, we will have issued a diagnostic to the
9836 user, as opposed to simply marking the tentative parse as
9837 failed? */
9838 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9839 error ("parse error in template argument list");
9842 pop_deferring_access_checks ();
9843 return template_id;
9846 /* Parse a template-name.
9848 template-name:
9849 identifier
9851 The standard should actually say:
9853 template-name:
9854 identifier
9855 operator-function-id
9857 A defect report has been filed about this issue.
9859 A conversion-function-id cannot be a template name because they cannot
9860 be part of a template-id. In fact, looking at this code:
9862 a.operator K<int>()
9864 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9865 It is impossible to call a templated conversion-function-id with an
9866 explicit argument list, since the only allowed template parameter is
9867 the type to which it is converting.
9869 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9870 `template' keyword, in a construction like:
9872 T::template f<3>()
9874 In that case `f' is taken to be a template-name, even though there
9875 is no way of knowing for sure.
9877 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9878 name refers to a set of overloaded functions, at least one of which
9879 is a template, or an IDENTIFIER_NODE with the name of the template,
9880 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9881 names are looked up inside uninstantiated templates. */
9883 static tree
9884 cp_parser_template_name (cp_parser* parser,
9885 bool template_keyword_p,
9886 bool check_dependency_p,
9887 bool is_declaration,
9888 bool *is_identifier)
9890 tree identifier;
9891 tree decl;
9892 tree fns;
9894 /* If the next token is `operator', then we have either an
9895 operator-function-id or a conversion-function-id. */
9896 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9898 /* We don't know whether we're looking at an
9899 operator-function-id or a conversion-function-id. */
9900 cp_parser_parse_tentatively (parser);
9901 /* Try an operator-function-id. */
9902 identifier = cp_parser_operator_function_id (parser);
9903 /* If that didn't work, try a conversion-function-id. */
9904 if (!cp_parser_parse_definitely (parser))
9906 cp_parser_error (parser, "expected template-name");
9907 return error_mark_node;
9910 /* Look for the identifier. */
9911 else
9912 identifier = cp_parser_identifier (parser);
9914 /* If we didn't find an identifier, we don't have a template-id. */
9915 if (identifier == error_mark_node)
9916 return error_mark_node;
9918 /* If the name immediately followed the `template' keyword, then it
9919 is a template-name. However, if the next token is not `<', then
9920 we do not treat it as a template-name, since it is not being used
9921 as part of a template-id. This enables us to handle constructs
9922 like:
9924 template <typename T> struct S { S(); };
9925 template <typename T> S<T>::S();
9927 correctly. We would treat `S' as a template -- if it were `S<T>'
9928 -- but we do not if there is no `<'. */
9930 if (processing_template_decl
9931 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9933 /* In a declaration, in a dependent context, we pretend that the
9934 "template" keyword was present in order to improve error
9935 recovery. For example, given:
9937 template <typename T> void f(T::X<int>);
9939 we want to treat "X<int>" as a template-id. */
9940 if (is_declaration
9941 && !template_keyword_p
9942 && parser->scope && TYPE_P (parser->scope)
9943 && check_dependency_p
9944 && dependent_type_p (parser->scope)
9945 /* Do not do this for dtors (or ctors), since they never
9946 need the template keyword before their name. */
9947 && !constructor_name_p (identifier, parser->scope))
9949 cp_token_position start = 0;
9951 /* Explain what went wrong. */
9952 error ("non-template %qD used as template", identifier);
9953 inform ("use %<%T::template %D%> to indicate that it is a template",
9954 parser->scope, identifier);
9955 /* If parsing tentatively, find the location of the "<" token. */
9956 if (cp_parser_simulate_error (parser))
9957 start = cp_lexer_token_position (parser->lexer, true);
9958 /* Parse the template arguments so that we can issue error
9959 messages about them. */
9960 cp_lexer_consume_token (parser->lexer);
9961 cp_parser_enclosed_template_argument_list (parser);
9962 /* Skip tokens until we find a good place from which to
9963 continue parsing. */
9964 cp_parser_skip_to_closing_parenthesis (parser,
9965 /*recovering=*/true,
9966 /*or_comma=*/true,
9967 /*consume_paren=*/false);
9968 /* If parsing tentatively, permanently remove the
9969 template argument list. That will prevent duplicate
9970 error messages from being issued about the missing
9971 "template" keyword. */
9972 if (start)
9973 cp_lexer_purge_tokens_after (parser->lexer, start);
9974 if (is_identifier)
9975 *is_identifier = true;
9976 return identifier;
9979 /* If the "template" keyword is present, then there is generally
9980 no point in doing name-lookup, so we just return IDENTIFIER.
9981 But, if the qualifying scope is non-dependent then we can
9982 (and must) do name-lookup normally. */
9983 if (template_keyword_p
9984 && (!parser->scope
9985 || (TYPE_P (parser->scope)
9986 && dependent_type_p (parser->scope))))
9987 return identifier;
9990 /* Look up the name. */
9991 decl = cp_parser_lookup_name (parser, identifier,
9992 none_type,
9993 /*is_template=*/false,
9994 /*is_namespace=*/false,
9995 check_dependency_p,
9996 /*ambiguous_decls=*/NULL);
9997 decl = maybe_get_template_decl_from_type_decl (decl);
9999 /* If DECL is a template, then the name was a template-name. */
10000 if (TREE_CODE (decl) == TEMPLATE_DECL)
10002 else
10004 tree fn = NULL_TREE;
10006 /* The standard does not explicitly indicate whether a name that
10007 names a set of overloaded declarations, some of which are
10008 templates, is a template-name. However, such a name should
10009 be a template-name; otherwise, there is no way to form a
10010 template-id for the overloaded templates. */
10011 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10012 if (TREE_CODE (fns) == OVERLOAD)
10013 for (fn = fns; fn; fn = OVL_NEXT (fn))
10014 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10015 break;
10017 if (!fn)
10019 /* The name does not name a template. */
10020 cp_parser_error (parser, "expected template-name");
10021 return error_mark_node;
10025 /* If DECL is dependent, and refers to a function, then just return
10026 its name; we will look it up again during template instantiation. */
10027 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10029 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10030 if (TYPE_P (scope) && dependent_type_p (scope))
10031 return identifier;
10034 return decl;
10037 /* Parse a template-argument-list.
10039 template-argument-list:
10040 template-argument ... [opt]
10041 template-argument-list , template-argument ... [opt]
10043 Returns a TREE_VEC containing the arguments. */
10045 static tree
10046 cp_parser_template_argument_list (cp_parser* parser)
10048 tree fixed_args[10];
10049 unsigned n_args = 0;
10050 unsigned alloced = 10;
10051 tree *arg_ary = fixed_args;
10052 tree vec;
10053 bool saved_in_template_argument_list_p;
10054 bool saved_ice_p;
10055 bool saved_non_ice_p;
10057 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10058 parser->in_template_argument_list_p = true;
10059 /* Even if the template-id appears in an integral
10060 constant-expression, the contents of the argument list do
10061 not. */
10062 saved_ice_p = parser->integral_constant_expression_p;
10063 parser->integral_constant_expression_p = false;
10064 saved_non_ice_p = parser->non_integral_constant_expression_p;
10065 parser->non_integral_constant_expression_p = false;
10066 /* Parse the arguments. */
10069 tree argument;
10071 if (n_args)
10072 /* Consume the comma. */
10073 cp_lexer_consume_token (parser->lexer);
10075 /* Parse the template-argument. */
10076 argument = cp_parser_template_argument (parser);
10078 /* If the next token is an ellipsis, we're expanding a template
10079 argument pack. */
10080 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10082 /* Consume the `...' token. */
10083 cp_lexer_consume_token (parser->lexer);
10085 /* Make the argument into a TYPE_PACK_EXPANSION or
10086 EXPR_PACK_EXPANSION. */
10087 argument = make_pack_expansion (argument);
10090 if (n_args == alloced)
10092 alloced *= 2;
10094 if (arg_ary == fixed_args)
10096 arg_ary = XNEWVEC (tree, alloced);
10097 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10099 else
10100 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10102 arg_ary[n_args++] = argument;
10104 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10106 vec = make_tree_vec (n_args);
10108 while (n_args--)
10109 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10111 if (arg_ary != fixed_args)
10112 free (arg_ary);
10113 parser->non_integral_constant_expression_p = saved_non_ice_p;
10114 parser->integral_constant_expression_p = saved_ice_p;
10115 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10116 return vec;
10119 /* Parse a template-argument.
10121 template-argument:
10122 assignment-expression
10123 type-id
10124 id-expression
10126 The representation is that of an assignment-expression, type-id, or
10127 id-expression -- except that the qualified id-expression is
10128 evaluated, so that the value returned is either a DECL or an
10129 OVERLOAD.
10131 Although the standard says "assignment-expression", it forbids
10132 throw-expressions or assignments in the template argument.
10133 Therefore, we use "conditional-expression" instead. */
10135 static tree
10136 cp_parser_template_argument (cp_parser* parser)
10138 tree argument;
10139 bool template_p;
10140 bool address_p;
10141 bool maybe_type_id = false;
10142 cp_token *token;
10143 cp_id_kind idk;
10145 /* There's really no way to know what we're looking at, so we just
10146 try each alternative in order.
10148 [temp.arg]
10150 In a template-argument, an ambiguity between a type-id and an
10151 expression is resolved to a type-id, regardless of the form of
10152 the corresponding template-parameter.
10154 Therefore, we try a type-id first. */
10155 cp_parser_parse_tentatively (parser);
10156 argument = cp_parser_type_id (parser);
10157 /* If there was no error parsing the type-id but the next token is a '>>',
10158 we probably found a typo for '> >'. But there are type-id which are
10159 also valid expressions. For instance:
10161 struct X { int operator >> (int); };
10162 template <int V> struct Foo {};
10163 Foo<X () >> 5> r;
10165 Here 'X()' is a valid type-id of a function type, but the user just
10166 wanted to write the expression "X() >> 5". Thus, we remember that we
10167 found a valid type-id, but we still try to parse the argument as an
10168 expression to see what happens. */
10169 if (!cp_parser_error_occurred (parser)
10170 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10172 maybe_type_id = true;
10173 cp_parser_abort_tentative_parse (parser);
10175 else
10177 /* If the next token isn't a `,' or a `>', then this argument wasn't
10178 really finished. This means that the argument is not a valid
10179 type-id. */
10180 if (!cp_parser_next_token_ends_template_argument_p (parser))
10181 cp_parser_error (parser, "expected template-argument");
10182 /* If that worked, we're done. */
10183 if (cp_parser_parse_definitely (parser))
10184 return argument;
10186 /* We're still not sure what the argument will be. */
10187 cp_parser_parse_tentatively (parser);
10188 /* Try a template. */
10189 argument = cp_parser_id_expression (parser,
10190 /*template_keyword_p=*/false,
10191 /*check_dependency_p=*/true,
10192 &template_p,
10193 /*declarator_p=*/false,
10194 /*optional_p=*/false);
10195 /* If the next token isn't a `,' or a `>', then this argument wasn't
10196 really finished. */
10197 if (!cp_parser_next_token_ends_template_argument_p (parser))
10198 cp_parser_error (parser, "expected template-argument");
10199 if (!cp_parser_error_occurred (parser))
10201 /* Figure out what is being referred to. If the id-expression
10202 was for a class template specialization, then we will have a
10203 TYPE_DECL at this point. There is no need to do name lookup
10204 at this point in that case. */
10205 if (TREE_CODE (argument) != TYPE_DECL)
10206 argument = cp_parser_lookup_name (parser, argument,
10207 none_type,
10208 /*is_template=*/template_p,
10209 /*is_namespace=*/false,
10210 /*check_dependency=*/true,
10211 /*ambiguous_decls=*/NULL);
10212 if (TREE_CODE (argument) != TEMPLATE_DECL
10213 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10214 cp_parser_error (parser, "expected template-name");
10216 if (cp_parser_parse_definitely (parser))
10217 return argument;
10218 /* It must be a non-type argument. There permitted cases are given
10219 in [temp.arg.nontype]:
10221 -- an integral constant-expression of integral or enumeration
10222 type; or
10224 -- the name of a non-type template-parameter; or
10226 -- the name of an object or function with external linkage...
10228 -- the address of an object or function with external linkage...
10230 -- a pointer to member... */
10231 /* Look for a non-type template parameter. */
10232 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10234 cp_parser_parse_tentatively (parser);
10235 argument = cp_parser_primary_expression (parser,
10236 /*adress_p=*/false,
10237 /*cast_p=*/false,
10238 /*template_arg_p=*/true,
10239 &idk);
10240 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10241 || !cp_parser_next_token_ends_template_argument_p (parser))
10242 cp_parser_simulate_error (parser);
10243 if (cp_parser_parse_definitely (parser))
10244 return argument;
10247 /* If the next token is "&", the argument must be the address of an
10248 object or function with external linkage. */
10249 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10250 if (address_p)
10251 cp_lexer_consume_token (parser->lexer);
10252 /* See if we might have an id-expression. */
10253 token = cp_lexer_peek_token (parser->lexer);
10254 if (token->type == CPP_NAME
10255 || token->keyword == RID_OPERATOR
10256 || token->type == CPP_SCOPE
10257 || token->type == CPP_TEMPLATE_ID
10258 || token->type == CPP_NESTED_NAME_SPECIFIER)
10260 cp_parser_parse_tentatively (parser);
10261 argument = cp_parser_primary_expression (parser,
10262 address_p,
10263 /*cast_p=*/false,
10264 /*template_arg_p=*/true,
10265 &idk);
10266 if (cp_parser_error_occurred (parser)
10267 || !cp_parser_next_token_ends_template_argument_p (parser))
10268 cp_parser_abort_tentative_parse (parser);
10269 else
10271 if (TREE_CODE (argument) == INDIRECT_REF)
10273 gcc_assert (REFERENCE_REF_P (argument));
10274 argument = TREE_OPERAND (argument, 0);
10277 if (TREE_CODE (argument) == VAR_DECL)
10279 /* A variable without external linkage might still be a
10280 valid constant-expression, so no error is issued here
10281 if the external-linkage check fails. */
10282 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10283 cp_parser_simulate_error (parser);
10285 else if (is_overloaded_fn (argument))
10286 /* All overloaded functions are allowed; if the external
10287 linkage test does not pass, an error will be issued
10288 later. */
10290 else if (address_p
10291 && (TREE_CODE (argument) == OFFSET_REF
10292 || TREE_CODE (argument) == SCOPE_REF))
10293 /* A pointer-to-member. */
10295 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10297 else
10298 cp_parser_simulate_error (parser);
10300 if (cp_parser_parse_definitely (parser))
10302 if (address_p)
10303 argument = build_x_unary_op (ADDR_EXPR, argument);
10304 return argument;
10308 /* If the argument started with "&", there are no other valid
10309 alternatives at this point. */
10310 if (address_p)
10312 cp_parser_error (parser, "invalid non-type template argument");
10313 return error_mark_node;
10316 /* If the argument wasn't successfully parsed as a type-id followed
10317 by '>>', the argument can only be a constant expression now.
10318 Otherwise, we try parsing the constant-expression tentatively,
10319 because the argument could really be a type-id. */
10320 if (maybe_type_id)
10321 cp_parser_parse_tentatively (parser);
10322 argument = cp_parser_constant_expression (parser,
10323 /*allow_non_constant_p=*/false,
10324 /*non_constant_p=*/NULL);
10325 argument = fold_non_dependent_expr (argument);
10326 if (!maybe_type_id)
10327 return argument;
10328 if (!cp_parser_next_token_ends_template_argument_p (parser))
10329 cp_parser_error (parser, "expected template-argument");
10330 if (cp_parser_parse_definitely (parser))
10331 return argument;
10332 /* We did our best to parse the argument as a non type-id, but that
10333 was the only alternative that matched (albeit with a '>' after
10334 it). We can assume it's just a typo from the user, and a
10335 diagnostic will then be issued. */
10336 return cp_parser_type_id (parser);
10339 /* Parse an explicit-instantiation.
10341 explicit-instantiation:
10342 template declaration
10344 Although the standard says `declaration', what it really means is:
10346 explicit-instantiation:
10347 template decl-specifier-seq [opt] declarator [opt] ;
10349 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10350 supposed to be allowed. A defect report has been filed about this
10351 issue.
10353 GNU Extension:
10355 explicit-instantiation:
10356 storage-class-specifier template
10357 decl-specifier-seq [opt] declarator [opt] ;
10358 function-specifier template
10359 decl-specifier-seq [opt] declarator [opt] ; */
10361 static void
10362 cp_parser_explicit_instantiation (cp_parser* parser)
10364 int declares_class_or_enum;
10365 cp_decl_specifier_seq decl_specifiers;
10366 tree extension_specifier = NULL_TREE;
10368 /* Look for an (optional) storage-class-specifier or
10369 function-specifier. */
10370 if (cp_parser_allow_gnu_extensions_p (parser))
10372 extension_specifier
10373 = cp_parser_storage_class_specifier_opt (parser);
10374 if (!extension_specifier)
10375 extension_specifier
10376 = cp_parser_function_specifier_opt (parser,
10377 /*decl_specs=*/NULL);
10380 /* Look for the `template' keyword. */
10381 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10382 /* Let the front end know that we are processing an explicit
10383 instantiation. */
10384 begin_explicit_instantiation ();
10385 /* [temp.explicit] says that we are supposed to ignore access
10386 control while processing explicit instantiation directives. */
10387 push_deferring_access_checks (dk_no_check);
10388 /* Parse a decl-specifier-seq. */
10389 cp_parser_decl_specifier_seq (parser,
10390 CP_PARSER_FLAGS_OPTIONAL,
10391 &decl_specifiers,
10392 &declares_class_or_enum);
10393 /* If there was exactly one decl-specifier, and it declared a class,
10394 and there's no declarator, then we have an explicit type
10395 instantiation. */
10396 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10398 tree type;
10400 type = check_tag_decl (&decl_specifiers);
10401 /* Turn access control back on for names used during
10402 template instantiation. */
10403 pop_deferring_access_checks ();
10404 if (type)
10405 do_type_instantiation (type, extension_specifier,
10406 /*complain=*/tf_error);
10408 else
10410 cp_declarator *declarator;
10411 tree decl;
10413 /* Parse the declarator. */
10414 declarator
10415 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10416 /*ctor_dtor_or_conv_p=*/NULL,
10417 /*parenthesized_p=*/NULL,
10418 /*member_p=*/false);
10419 if (declares_class_or_enum & 2)
10420 cp_parser_check_for_definition_in_return_type (declarator,
10421 decl_specifiers.type);
10422 if (declarator != cp_error_declarator)
10424 decl = grokdeclarator (declarator, &decl_specifiers,
10425 NORMAL, 0, &decl_specifiers.attributes);
10426 /* Turn access control back on for names used during
10427 template instantiation. */
10428 pop_deferring_access_checks ();
10429 /* Do the explicit instantiation. */
10430 do_decl_instantiation (decl, extension_specifier);
10432 else
10434 pop_deferring_access_checks ();
10435 /* Skip the body of the explicit instantiation. */
10436 cp_parser_skip_to_end_of_statement (parser);
10439 /* We're done with the instantiation. */
10440 end_explicit_instantiation ();
10442 cp_parser_consume_semicolon_at_end_of_statement (parser);
10445 /* Parse an explicit-specialization.
10447 explicit-specialization:
10448 template < > declaration
10450 Although the standard says `declaration', what it really means is:
10452 explicit-specialization:
10453 template <> decl-specifier [opt] init-declarator [opt] ;
10454 template <> function-definition
10455 template <> explicit-specialization
10456 template <> template-declaration */
10458 static void
10459 cp_parser_explicit_specialization (cp_parser* parser)
10461 bool need_lang_pop;
10462 /* Look for the `template' keyword. */
10463 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10464 /* Look for the `<'. */
10465 cp_parser_require (parser, CPP_LESS, "`<'");
10466 /* Look for the `>'. */
10467 cp_parser_require (parser, CPP_GREATER, "`>'");
10468 /* We have processed another parameter list. */
10469 ++parser->num_template_parameter_lists;
10470 /* [temp]
10472 A template ... explicit specialization ... shall not have C
10473 linkage. */
10474 if (current_lang_name == lang_name_c)
10476 error ("template specialization with C linkage");
10477 /* Give it C++ linkage to avoid confusing other parts of the
10478 front end. */
10479 push_lang_context (lang_name_cplusplus);
10480 need_lang_pop = true;
10482 else
10483 need_lang_pop = false;
10484 /* Let the front end know that we are beginning a specialization. */
10485 if (!begin_specialization ())
10487 end_specialization ();
10488 cp_parser_skip_to_end_of_block_or_statement (parser);
10489 return;
10492 /* If the next keyword is `template', we need to figure out whether
10493 or not we're looking a template-declaration. */
10494 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10496 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10497 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10498 cp_parser_template_declaration_after_export (parser,
10499 /*member_p=*/false);
10500 else
10501 cp_parser_explicit_specialization (parser);
10503 else
10504 /* Parse the dependent declaration. */
10505 cp_parser_single_declaration (parser,
10506 /*checks=*/NULL,
10507 /*member_p=*/false,
10508 /*explicit_specialization_p=*/true,
10509 /*friend_p=*/NULL);
10510 /* We're done with the specialization. */
10511 end_specialization ();
10512 /* For the erroneous case of a template with C linkage, we pushed an
10513 implicit C++ linkage scope; exit that scope now. */
10514 if (need_lang_pop)
10515 pop_lang_context ();
10516 /* We're done with this parameter list. */
10517 --parser->num_template_parameter_lists;
10520 /* Parse a type-specifier.
10522 type-specifier:
10523 simple-type-specifier
10524 class-specifier
10525 enum-specifier
10526 elaborated-type-specifier
10527 cv-qualifier
10529 GNU Extension:
10531 type-specifier:
10532 __complex__
10534 Returns a representation of the type-specifier. For a
10535 class-specifier, enum-specifier, or elaborated-type-specifier, a
10536 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10538 The parser flags FLAGS is used to control type-specifier parsing.
10540 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10541 in a decl-specifier-seq.
10543 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10544 class-specifier, enum-specifier, or elaborated-type-specifier, then
10545 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10546 if a type is declared; 2 if it is defined. Otherwise, it is set to
10547 zero.
10549 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10550 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10551 is set to FALSE. */
10553 static tree
10554 cp_parser_type_specifier (cp_parser* parser,
10555 cp_parser_flags flags,
10556 cp_decl_specifier_seq *decl_specs,
10557 bool is_declaration,
10558 int* declares_class_or_enum,
10559 bool* is_cv_qualifier)
10561 tree type_spec = NULL_TREE;
10562 cp_token *token;
10563 enum rid keyword;
10564 cp_decl_spec ds = ds_last;
10566 /* Assume this type-specifier does not declare a new type. */
10567 if (declares_class_or_enum)
10568 *declares_class_or_enum = 0;
10569 /* And that it does not specify a cv-qualifier. */
10570 if (is_cv_qualifier)
10571 *is_cv_qualifier = false;
10572 /* Peek at the next token. */
10573 token = cp_lexer_peek_token (parser->lexer);
10575 /* If we're looking at a keyword, we can use that to guide the
10576 production we choose. */
10577 keyword = token->keyword;
10578 switch (keyword)
10580 case RID_ENUM:
10581 /* Look for the enum-specifier. */
10582 type_spec = cp_parser_enum_specifier (parser);
10583 /* If that worked, we're done. */
10584 if (type_spec)
10586 if (declares_class_or_enum)
10587 *declares_class_or_enum = 2;
10588 if (decl_specs)
10589 cp_parser_set_decl_spec_type (decl_specs,
10590 type_spec,
10591 /*user_defined_p=*/true);
10592 return type_spec;
10594 else
10595 goto elaborated_type_specifier;
10597 /* Any of these indicate either a class-specifier, or an
10598 elaborated-type-specifier. */
10599 case RID_CLASS:
10600 case RID_STRUCT:
10601 case RID_UNION:
10602 /* Parse tentatively so that we can back up if we don't find a
10603 class-specifier. */
10604 cp_parser_parse_tentatively (parser);
10605 /* Look for the class-specifier. */
10606 type_spec = cp_parser_class_specifier (parser);
10607 /* If that worked, we're done. */
10608 if (cp_parser_parse_definitely (parser))
10610 if (declares_class_or_enum)
10611 *declares_class_or_enum = 2;
10612 if (decl_specs)
10613 cp_parser_set_decl_spec_type (decl_specs,
10614 type_spec,
10615 /*user_defined_p=*/true);
10616 return type_spec;
10619 /* Fall through. */
10620 elaborated_type_specifier:
10621 /* We're declaring (not defining) a class or enum. */
10622 if (declares_class_or_enum)
10623 *declares_class_or_enum = 1;
10625 /* Fall through. */
10626 case RID_TYPENAME:
10627 /* Look for an elaborated-type-specifier. */
10628 type_spec
10629 = (cp_parser_elaborated_type_specifier
10630 (parser,
10631 decl_specs && decl_specs->specs[(int) ds_friend],
10632 is_declaration));
10633 if (decl_specs)
10634 cp_parser_set_decl_spec_type (decl_specs,
10635 type_spec,
10636 /*user_defined_p=*/true);
10637 return type_spec;
10639 case RID_CONST:
10640 ds = ds_const;
10641 if (is_cv_qualifier)
10642 *is_cv_qualifier = true;
10643 break;
10645 case RID_VOLATILE:
10646 ds = ds_volatile;
10647 if (is_cv_qualifier)
10648 *is_cv_qualifier = true;
10649 break;
10651 case RID_RESTRICT:
10652 ds = ds_restrict;
10653 if (is_cv_qualifier)
10654 *is_cv_qualifier = true;
10655 break;
10657 case RID_COMPLEX:
10658 /* The `__complex__' keyword is a GNU extension. */
10659 ds = ds_complex;
10660 break;
10662 default:
10663 break;
10666 /* Handle simple keywords. */
10667 if (ds != ds_last)
10669 if (decl_specs)
10671 ++decl_specs->specs[(int)ds];
10672 decl_specs->any_specifiers_p = true;
10674 return cp_lexer_consume_token (parser->lexer)->u.value;
10677 /* If we do not already have a type-specifier, assume we are looking
10678 at a simple-type-specifier. */
10679 type_spec = cp_parser_simple_type_specifier (parser,
10680 decl_specs,
10681 flags);
10683 /* If we didn't find a type-specifier, and a type-specifier was not
10684 optional in this context, issue an error message. */
10685 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10687 cp_parser_error (parser, "expected type specifier");
10688 return error_mark_node;
10691 return type_spec;
10694 /* Parse a simple-type-specifier.
10696 simple-type-specifier:
10697 :: [opt] nested-name-specifier [opt] type-name
10698 :: [opt] nested-name-specifier template template-id
10699 char
10700 wchar_t
10701 bool
10702 short
10704 long
10705 signed
10706 unsigned
10707 float
10708 double
10709 void
10711 C++0x Extension:
10713 simple-type-specifier:
10714 decltype ( expression )
10716 GNU Extension:
10718 simple-type-specifier:
10719 __typeof__ unary-expression
10720 __typeof__ ( type-id )
10722 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10723 appropriately updated. */
10725 static tree
10726 cp_parser_simple_type_specifier (cp_parser* parser,
10727 cp_decl_specifier_seq *decl_specs,
10728 cp_parser_flags flags)
10730 tree type = NULL_TREE;
10731 cp_token *token;
10733 /* Peek at the next token. */
10734 token = cp_lexer_peek_token (parser->lexer);
10736 /* If we're looking at a keyword, things are easy. */
10737 switch (token->keyword)
10739 case RID_CHAR:
10740 if (decl_specs)
10741 decl_specs->explicit_char_p = true;
10742 type = char_type_node;
10743 break;
10744 case RID_WCHAR:
10745 type = wchar_type_node;
10746 break;
10747 case RID_BOOL:
10748 type = boolean_type_node;
10749 break;
10750 case RID_SHORT:
10751 if (decl_specs)
10752 ++decl_specs->specs[(int) ds_short];
10753 type = short_integer_type_node;
10754 break;
10755 case RID_INT:
10756 if (decl_specs)
10757 decl_specs->explicit_int_p = true;
10758 type = integer_type_node;
10759 break;
10760 case RID_LONG:
10761 if (decl_specs)
10762 ++decl_specs->specs[(int) ds_long];
10763 type = long_integer_type_node;
10764 break;
10765 case RID_SIGNED:
10766 if (decl_specs)
10767 ++decl_specs->specs[(int) ds_signed];
10768 type = integer_type_node;
10769 break;
10770 case RID_UNSIGNED:
10771 if (decl_specs)
10772 ++decl_specs->specs[(int) ds_unsigned];
10773 type = unsigned_type_node;
10774 break;
10775 case RID_FLOAT:
10776 type = float_type_node;
10777 break;
10778 case RID_DOUBLE:
10779 type = double_type_node;
10780 break;
10781 case RID_VOID:
10782 type = void_type_node;
10783 break;
10785 case RID_DECLTYPE:
10786 /* Parse the `decltype' type. */
10787 type = cp_parser_decltype (parser);
10789 if (decl_specs)
10790 cp_parser_set_decl_spec_type (decl_specs, type,
10791 /*user_defined_p=*/true);
10793 return type;
10795 case RID_TYPEOF:
10796 /* Consume the `typeof' token. */
10797 cp_lexer_consume_token (parser->lexer);
10798 /* Parse the operand to `typeof'. */
10799 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10800 /* If it is not already a TYPE, take its type. */
10801 if (!TYPE_P (type))
10802 type = finish_typeof (type);
10804 if (decl_specs)
10805 cp_parser_set_decl_spec_type (decl_specs, type,
10806 /*user_defined_p=*/true);
10808 return type;
10810 default:
10811 break;
10814 /* If the type-specifier was for a built-in type, we're done. */
10815 if (type)
10817 tree id;
10819 /* Record the type. */
10820 if (decl_specs
10821 && (token->keyword != RID_SIGNED
10822 && token->keyword != RID_UNSIGNED
10823 && token->keyword != RID_SHORT
10824 && token->keyword != RID_LONG))
10825 cp_parser_set_decl_spec_type (decl_specs,
10826 type,
10827 /*user_defined=*/false);
10828 if (decl_specs)
10829 decl_specs->any_specifiers_p = true;
10831 /* Consume the token. */
10832 id = cp_lexer_consume_token (parser->lexer)->u.value;
10834 /* There is no valid C++ program where a non-template type is
10835 followed by a "<". That usually indicates that the user thought
10836 that the type was a template. */
10837 cp_parser_check_for_invalid_template_id (parser, type);
10839 return TYPE_NAME (type);
10842 /* The type-specifier must be a user-defined type. */
10843 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10845 bool qualified_p;
10846 bool global_p;
10848 /* Don't gobble tokens or issue error messages if this is an
10849 optional type-specifier. */
10850 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10851 cp_parser_parse_tentatively (parser);
10853 /* Look for the optional `::' operator. */
10854 global_p
10855 = (cp_parser_global_scope_opt (parser,
10856 /*current_scope_valid_p=*/false)
10857 != NULL_TREE);
10858 /* Look for the nested-name specifier. */
10859 qualified_p
10860 = (cp_parser_nested_name_specifier_opt (parser,
10861 /*typename_keyword_p=*/false,
10862 /*check_dependency_p=*/true,
10863 /*type_p=*/false,
10864 /*is_declaration=*/false)
10865 != NULL_TREE);
10866 /* If we have seen a nested-name-specifier, and the next token
10867 is `template', then we are using the template-id production. */
10868 if (parser->scope
10869 && cp_parser_optional_template_keyword (parser))
10871 /* Look for the template-id. */
10872 type = cp_parser_template_id (parser,
10873 /*template_keyword_p=*/true,
10874 /*check_dependency_p=*/true,
10875 /*is_declaration=*/false);
10876 /* If the template-id did not name a type, we are out of
10877 luck. */
10878 if (TREE_CODE (type) != TYPE_DECL)
10880 cp_parser_error (parser, "expected template-id for type");
10881 type = NULL_TREE;
10884 /* Otherwise, look for a type-name. */
10885 else
10886 type = cp_parser_type_name (parser);
10887 /* Keep track of all name-lookups performed in class scopes. */
10888 if (type
10889 && !global_p
10890 && !qualified_p
10891 && TREE_CODE (type) == TYPE_DECL
10892 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10893 maybe_note_name_used_in_class (DECL_NAME (type), type);
10894 /* If it didn't work out, we don't have a TYPE. */
10895 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10896 && !cp_parser_parse_definitely (parser))
10897 type = NULL_TREE;
10898 if (type && decl_specs)
10899 cp_parser_set_decl_spec_type (decl_specs, type,
10900 /*user_defined=*/true);
10903 /* If we didn't get a type-name, issue an error message. */
10904 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10906 cp_parser_error (parser, "expected type-name");
10907 return error_mark_node;
10910 /* There is no valid C++ program where a non-template type is
10911 followed by a "<". That usually indicates that the user thought
10912 that the type was a template. */
10913 if (type && type != error_mark_node)
10915 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10916 If it is, then the '<'...'>' enclose protocol names rather than
10917 template arguments, and so everything is fine. */
10918 if (c_dialect_objc ()
10919 && (objc_is_id (type) || objc_is_class_name (type)))
10921 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10922 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10924 /* Clobber the "unqualified" type previously entered into
10925 DECL_SPECS with the new, improved protocol-qualified version. */
10926 if (decl_specs)
10927 decl_specs->type = qual_type;
10929 return qual_type;
10932 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10935 return type;
10938 /* Parse a type-name.
10940 type-name:
10941 class-name
10942 enum-name
10943 typedef-name
10945 enum-name:
10946 identifier
10948 typedef-name:
10949 identifier
10951 Returns a TYPE_DECL for the type. */
10953 static tree
10954 cp_parser_type_name (cp_parser* parser)
10956 tree type_decl;
10957 tree identifier;
10959 /* We can't know yet whether it is a class-name or not. */
10960 cp_parser_parse_tentatively (parser);
10961 /* Try a class-name. */
10962 type_decl = cp_parser_class_name (parser,
10963 /*typename_keyword_p=*/false,
10964 /*template_keyword_p=*/false,
10965 none_type,
10966 /*check_dependency_p=*/true,
10967 /*class_head_p=*/false,
10968 /*is_declaration=*/false);
10969 /* If it's not a class-name, keep looking. */
10970 if (!cp_parser_parse_definitely (parser))
10972 /* It must be a typedef-name or an enum-name. */
10973 identifier = cp_parser_identifier (parser);
10974 if (identifier == error_mark_node)
10975 return error_mark_node;
10977 /* Look up the type-name. */
10978 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10980 if (TREE_CODE (type_decl) != TYPE_DECL
10981 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10983 /* See if this is an Objective-C type. */
10984 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10985 tree type = objc_get_protocol_qualified_type (identifier, protos);
10986 if (type)
10987 type_decl = TYPE_NAME (type);
10990 /* Issue an error if we did not find a type-name. */
10991 if (TREE_CODE (type_decl) != TYPE_DECL)
10993 if (!cp_parser_simulate_error (parser))
10994 cp_parser_name_lookup_error (parser, identifier, type_decl,
10995 "is not a type");
10996 type_decl = error_mark_node;
10998 /* Remember that the name was used in the definition of the
10999 current class so that we can check later to see if the
11000 meaning would have been different after the class was
11001 entirely defined. */
11002 else if (type_decl != error_mark_node
11003 && !parser->scope)
11004 maybe_note_name_used_in_class (identifier, type_decl);
11007 return type_decl;
11011 /* Parse an elaborated-type-specifier. Note that the grammar given
11012 here incorporates the resolution to DR68.
11014 elaborated-type-specifier:
11015 class-key :: [opt] nested-name-specifier [opt] identifier
11016 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11017 enum :: [opt] nested-name-specifier [opt] identifier
11018 typename :: [opt] nested-name-specifier identifier
11019 typename :: [opt] nested-name-specifier template [opt]
11020 template-id
11022 GNU extension:
11024 elaborated-type-specifier:
11025 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11026 class-key attributes :: [opt] nested-name-specifier [opt]
11027 template [opt] template-id
11028 enum attributes :: [opt] nested-name-specifier [opt] identifier
11030 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11031 declared `friend'. If IS_DECLARATION is TRUE, then this
11032 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11033 something is being declared.
11035 Returns the TYPE specified. */
11037 static tree
11038 cp_parser_elaborated_type_specifier (cp_parser* parser,
11039 bool is_friend,
11040 bool is_declaration)
11042 enum tag_types tag_type;
11043 tree identifier;
11044 tree type = NULL_TREE;
11045 tree attributes = NULL_TREE;
11047 /* See if we're looking at the `enum' keyword. */
11048 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11050 /* Consume the `enum' token. */
11051 cp_lexer_consume_token (parser->lexer);
11052 /* Remember that it's an enumeration type. */
11053 tag_type = enum_type;
11054 /* Parse the attributes. */
11055 attributes = cp_parser_attributes_opt (parser);
11057 /* Or, it might be `typename'. */
11058 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11059 RID_TYPENAME))
11061 /* Consume the `typename' token. */
11062 cp_lexer_consume_token (parser->lexer);
11063 /* Remember that it's a `typename' type. */
11064 tag_type = typename_type;
11065 /* The `typename' keyword is only allowed in templates. */
11066 if (!processing_template_decl)
11067 pedwarn ("using %<typename%> outside of template");
11069 /* Otherwise it must be a class-key. */
11070 else
11072 tag_type = cp_parser_class_key (parser);
11073 if (tag_type == none_type)
11074 return error_mark_node;
11075 /* Parse the attributes. */
11076 attributes = cp_parser_attributes_opt (parser);
11079 /* Look for the `::' operator. */
11080 cp_parser_global_scope_opt (parser,
11081 /*current_scope_valid_p=*/false);
11082 /* Look for the nested-name-specifier. */
11083 if (tag_type == typename_type)
11085 if (!cp_parser_nested_name_specifier (parser,
11086 /*typename_keyword_p=*/true,
11087 /*check_dependency_p=*/true,
11088 /*type_p=*/true,
11089 is_declaration))
11090 return error_mark_node;
11092 else
11093 /* Even though `typename' is not present, the proposed resolution
11094 to Core Issue 180 says that in `class A<T>::B', `B' should be
11095 considered a type-name, even if `A<T>' is dependent. */
11096 cp_parser_nested_name_specifier_opt (parser,
11097 /*typename_keyword_p=*/true,
11098 /*check_dependency_p=*/true,
11099 /*type_p=*/true,
11100 is_declaration);
11101 /* For everything but enumeration types, consider a template-id.
11102 For an enumeration type, consider only a plain identifier. */
11103 if (tag_type != enum_type)
11105 bool template_p = false;
11106 tree decl;
11108 /* Allow the `template' keyword. */
11109 template_p = cp_parser_optional_template_keyword (parser);
11110 /* If we didn't see `template', we don't know if there's a
11111 template-id or not. */
11112 if (!template_p)
11113 cp_parser_parse_tentatively (parser);
11114 /* Parse the template-id. */
11115 decl = cp_parser_template_id (parser, template_p,
11116 /*check_dependency_p=*/true,
11117 is_declaration);
11118 /* If we didn't find a template-id, look for an ordinary
11119 identifier. */
11120 if (!template_p && !cp_parser_parse_definitely (parser))
11122 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11123 in effect, then we must assume that, upon instantiation, the
11124 template will correspond to a class. */
11125 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11126 && tag_type == typename_type)
11127 type = make_typename_type (parser->scope, decl,
11128 typename_type,
11129 /*complain=*/tf_error);
11130 else
11131 type = TREE_TYPE (decl);
11134 if (!type)
11136 identifier = cp_parser_identifier (parser);
11138 if (identifier == error_mark_node)
11140 parser->scope = NULL_TREE;
11141 return error_mark_node;
11144 /* For a `typename', we needn't call xref_tag. */
11145 if (tag_type == typename_type
11146 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11147 return cp_parser_make_typename_type (parser, parser->scope,
11148 identifier);
11149 /* Look up a qualified name in the usual way. */
11150 if (parser->scope)
11152 tree decl;
11153 tree ambiguous_decls;
11155 decl = cp_parser_lookup_name (parser, identifier,
11156 tag_type,
11157 /*is_template=*/false,
11158 /*is_namespace=*/false,
11159 /*check_dependency=*/true,
11160 &ambiguous_decls);
11162 /* If the lookup was ambiguous, an error will already have been
11163 issued. */
11164 if (ambiguous_decls)
11165 return error_mark_node;
11167 /* If we are parsing friend declaration, DECL may be a
11168 TEMPLATE_DECL tree node here. However, we need to check
11169 whether this TEMPLATE_DECL results in valid code. Consider
11170 the following example:
11172 namespace N {
11173 template <class T> class C {};
11175 class X {
11176 template <class T> friend class N::C; // #1, valid code
11178 template <class T> class Y {
11179 friend class N::C; // #2, invalid code
11182 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11183 name lookup of `N::C'. We see that friend declaration must
11184 be template for the code to be valid. Note that
11185 processing_template_decl does not work here since it is
11186 always 1 for the above two cases. */
11188 decl = (cp_parser_maybe_treat_template_as_class
11189 (decl, /*tag_name_p=*/is_friend
11190 && parser->num_template_parameter_lists));
11192 if (TREE_CODE (decl) != TYPE_DECL)
11194 cp_parser_diagnose_invalid_type_name (parser,
11195 parser->scope,
11196 identifier);
11197 return error_mark_node;
11200 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11202 bool allow_template = (parser->num_template_parameter_lists
11203 || DECL_SELF_REFERENCE_P (decl));
11204 type = check_elaborated_type_specifier (tag_type, decl,
11205 allow_template);
11207 if (type == error_mark_node)
11208 return error_mark_node;
11211 /* Forward declarations of nested types, such as
11213 class C1::C2;
11214 class C1::C2::C3;
11216 are invalid unless all components preceding the final '::'
11217 are complete. If all enclosing types are complete, these
11218 declarations become merely pointless.
11220 Invalid forward declarations of nested types are errors
11221 caught elsewhere in parsing. Those that are pointless arrive
11222 here. */
11224 if (cp_parser_declares_only_class_p (parser)
11225 && !is_friend && !processing_explicit_instantiation)
11226 warning (0, "declaration %qD does not declare anything", decl);
11228 type = TREE_TYPE (decl);
11230 else
11232 /* An elaborated-type-specifier sometimes introduces a new type and
11233 sometimes names an existing type. Normally, the rule is that it
11234 introduces a new type only if there is not an existing type of
11235 the same name already in scope. For example, given:
11237 struct S {};
11238 void f() { struct S s; }
11240 the `struct S' in the body of `f' is the same `struct S' as in
11241 the global scope; the existing definition is used. However, if
11242 there were no global declaration, this would introduce a new
11243 local class named `S'.
11245 An exception to this rule applies to the following code:
11247 namespace N { struct S; }
11249 Here, the elaborated-type-specifier names a new type
11250 unconditionally; even if there is already an `S' in the
11251 containing scope this declaration names a new type.
11252 This exception only applies if the elaborated-type-specifier
11253 forms the complete declaration:
11255 [class.name]
11257 A declaration consisting solely of `class-key identifier ;' is
11258 either a redeclaration of the name in the current scope or a
11259 forward declaration of the identifier as a class name. It
11260 introduces the name into the current scope.
11262 We are in this situation precisely when the next token is a `;'.
11264 An exception to the exception is that a `friend' declaration does
11265 *not* name a new type; i.e., given:
11267 struct S { friend struct T; };
11269 `T' is not a new type in the scope of `S'.
11271 Also, `new struct S' or `sizeof (struct S)' never results in the
11272 definition of a new type; a new type can only be declared in a
11273 declaration context. */
11275 tag_scope ts;
11276 bool template_p;
11278 if (is_friend)
11279 /* Friends have special name lookup rules. */
11280 ts = ts_within_enclosing_non_class;
11281 else if (is_declaration
11282 && cp_lexer_next_token_is (parser->lexer,
11283 CPP_SEMICOLON))
11284 /* This is a `class-key identifier ;' */
11285 ts = ts_current;
11286 else
11287 ts = ts_global;
11289 template_p =
11290 (parser->num_template_parameter_lists
11291 && (cp_parser_next_token_starts_class_definition_p (parser)
11292 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11293 /* An unqualified name was used to reference this type, so
11294 there were no qualifying templates. */
11295 if (!cp_parser_check_template_parameters (parser,
11296 /*num_templates=*/0))
11297 return error_mark_node;
11298 type = xref_tag (tag_type, identifier, ts, template_p);
11302 if (type == error_mark_node)
11303 return error_mark_node;
11305 /* Allow attributes on forward declarations of classes. */
11306 if (attributes)
11308 if (TREE_CODE (type) == TYPENAME_TYPE)
11309 warning (OPT_Wattributes,
11310 "attributes ignored on uninstantiated type");
11311 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11312 && ! processing_explicit_instantiation)
11313 warning (OPT_Wattributes,
11314 "attributes ignored on template instantiation");
11315 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11316 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11317 else
11318 warning (OPT_Wattributes,
11319 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11322 if (tag_type != enum_type)
11323 cp_parser_check_class_key (tag_type, type);
11325 /* A "<" cannot follow an elaborated type specifier. If that
11326 happens, the user was probably trying to form a template-id. */
11327 cp_parser_check_for_invalid_template_id (parser, type);
11329 return type;
11332 /* Parse an enum-specifier.
11334 enum-specifier:
11335 enum identifier [opt] { enumerator-list [opt] }
11337 GNU Extensions:
11338 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11339 attributes[opt]
11341 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11342 if the token stream isn't an enum-specifier after all. */
11344 static tree
11345 cp_parser_enum_specifier (cp_parser* parser)
11347 tree identifier;
11348 tree type;
11349 tree attributes;
11351 /* Parse tentatively so that we can back up if we don't find a
11352 enum-specifier. */
11353 cp_parser_parse_tentatively (parser);
11355 /* Caller guarantees that the current token is 'enum', an identifier
11356 possibly follows, and the token after that is an opening brace.
11357 If we don't have an identifier, fabricate an anonymous name for
11358 the enumeration being defined. */
11359 cp_lexer_consume_token (parser->lexer);
11361 attributes = cp_parser_attributes_opt (parser);
11363 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11364 identifier = cp_parser_identifier (parser);
11365 else
11366 identifier = make_anon_name ();
11368 /* Look for the `{' but don't consume it yet. */
11369 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11370 cp_parser_simulate_error (parser);
11372 if (!cp_parser_parse_definitely (parser))
11373 return NULL_TREE;
11375 /* Issue an error message if type-definitions are forbidden here. */
11376 if (!cp_parser_check_type_definition (parser))
11377 type = error_mark_node;
11378 else
11379 /* Create the new type. We do this before consuming the opening
11380 brace so the enum will be recorded as being on the line of its
11381 tag (or the 'enum' keyword, if there is no tag). */
11382 type = start_enum (identifier);
11384 /* Consume the opening brace. */
11385 cp_lexer_consume_token (parser->lexer);
11387 if (type == error_mark_node)
11389 cp_parser_skip_to_end_of_block_or_statement (parser);
11390 return error_mark_node;
11393 /* If the next token is not '}', then there are some enumerators. */
11394 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11395 cp_parser_enumerator_list (parser, type);
11397 /* Consume the final '}'. */
11398 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11400 /* Look for trailing attributes to apply to this enumeration, and
11401 apply them if appropriate. */
11402 if (cp_parser_allow_gnu_extensions_p (parser))
11404 tree trailing_attr = cp_parser_attributes_opt (parser);
11405 cplus_decl_attributes (&type,
11406 trailing_attr,
11407 (int) ATTR_FLAG_TYPE_IN_PLACE);
11410 /* Finish up the enumeration. */
11411 finish_enum (type);
11413 return type;
11416 /* Parse an enumerator-list. The enumerators all have the indicated
11417 TYPE.
11419 enumerator-list:
11420 enumerator-definition
11421 enumerator-list , enumerator-definition */
11423 static void
11424 cp_parser_enumerator_list (cp_parser* parser, tree type)
11426 while (true)
11428 /* Parse an enumerator-definition. */
11429 cp_parser_enumerator_definition (parser, type);
11431 /* If the next token is not a ',', we've reached the end of
11432 the list. */
11433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11434 break;
11435 /* Otherwise, consume the `,' and keep going. */
11436 cp_lexer_consume_token (parser->lexer);
11437 /* If the next token is a `}', there is a trailing comma. */
11438 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11440 if (pedantic && !in_system_header)
11441 pedwarn ("comma at end of enumerator list");
11442 break;
11447 /* Parse an enumerator-definition. The enumerator has the indicated
11448 TYPE.
11450 enumerator-definition:
11451 enumerator
11452 enumerator = constant-expression
11454 enumerator:
11455 identifier */
11457 static void
11458 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11460 tree identifier;
11461 tree value;
11463 /* Look for the identifier. */
11464 identifier = cp_parser_identifier (parser);
11465 if (identifier == error_mark_node)
11466 return;
11468 /* If the next token is an '=', then there is an explicit value. */
11469 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11471 /* Consume the `=' token. */
11472 cp_lexer_consume_token (parser->lexer);
11473 /* Parse the value. */
11474 value = cp_parser_constant_expression (parser,
11475 /*allow_non_constant_p=*/false,
11476 NULL);
11478 else
11479 value = NULL_TREE;
11481 /* Create the enumerator. */
11482 build_enumerator (identifier, value, type);
11485 /* Parse a namespace-name.
11487 namespace-name:
11488 original-namespace-name
11489 namespace-alias
11491 Returns the NAMESPACE_DECL for the namespace. */
11493 static tree
11494 cp_parser_namespace_name (cp_parser* parser)
11496 tree identifier;
11497 tree namespace_decl;
11499 /* Get the name of the namespace. */
11500 identifier = cp_parser_identifier (parser);
11501 if (identifier == error_mark_node)
11502 return error_mark_node;
11504 /* Look up the identifier in the currently active scope. Look only
11505 for namespaces, due to:
11507 [basic.lookup.udir]
11509 When looking up a namespace-name in a using-directive or alias
11510 definition, only namespace names are considered.
11512 And:
11514 [basic.lookup.qual]
11516 During the lookup of a name preceding the :: scope resolution
11517 operator, object, function, and enumerator names are ignored.
11519 (Note that cp_parser_class_or_namespace_name only calls this
11520 function if the token after the name is the scope resolution
11521 operator.) */
11522 namespace_decl = cp_parser_lookup_name (parser, identifier,
11523 none_type,
11524 /*is_template=*/false,
11525 /*is_namespace=*/true,
11526 /*check_dependency=*/true,
11527 /*ambiguous_decls=*/NULL);
11528 /* If it's not a namespace, issue an error. */
11529 if (namespace_decl == error_mark_node
11530 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11532 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11533 error ("%qD is not a namespace-name", identifier);
11534 cp_parser_error (parser, "expected namespace-name");
11535 namespace_decl = error_mark_node;
11538 return namespace_decl;
11541 /* Parse a namespace-definition.
11543 namespace-definition:
11544 named-namespace-definition
11545 unnamed-namespace-definition
11547 named-namespace-definition:
11548 original-namespace-definition
11549 extension-namespace-definition
11551 original-namespace-definition:
11552 namespace identifier { namespace-body }
11554 extension-namespace-definition:
11555 namespace original-namespace-name { namespace-body }
11557 unnamed-namespace-definition:
11558 namespace { namespace-body } */
11560 static void
11561 cp_parser_namespace_definition (cp_parser* parser)
11563 tree identifier, attribs;
11564 bool has_visibility;
11566 /* Look for the `namespace' keyword. */
11567 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11569 /* Get the name of the namespace. We do not attempt to distinguish
11570 between an original-namespace-definition and an
11571 extension-namespace-definition at this point. The semantic
11572 analysis routines are responsible for that. */
11573 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11574 identifier = cp_parser_identifier (parser);
11575 else
11576 identifier = NULL_TREE;
11578 /* Parse any specified attributes. */
11579 attribs = cp_parser_attributes_opt (parser);
11581 /* Look for the `{' to start the namespace. */
11582 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11583 /* Start the namespace. */
11584 push_namespace (identifier);
11586 has_visibility = handle_namespace_attrs (current_namespace, attribs);
11588 /* Parse the body of the namespace. */
11589 cp_parser_namespace_body (parser);
11591 #ifdef HANDLE_PRAGMA_VISIBILITY
11592 if (has_visibility)
11593 pop_visibility ();
11594 #endif
11596 /* Finish the namespace. */
11597 pop_namespace ();
11598 /* Look for the final `}'. */
11599 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11602 /* Parse a namespace-body.
11604 namespace-body:
11605 declaration-seq [opt] */
11607 static void
11608 cp_parser_namespace_body (cp_parser* parser)
11610 cp_parser_declaration_seq_opt (parser);
11613 /* Parse a namespace-alias-definition.
11615 namespace-alias-definition:
11616 namespace identifier = qualified-namespace-specifier ; */
11618 static void
11619 cp_parser_namespace_alias_definition (cp_parser* parser)
11621 tree identifier;
11622 tree namespace_specifier;
11624 /* Look for the `namespace' keyword. */
11625 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11626 /* Look for the identifier. */
11627 identifier = cp_parser_identifier (parser);
11628 if (identifier == error_mark_node)
11629 return;
11630 /* Look for the `=' token. */
11631 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11632 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11634 error ("%<namespace%> definition is not allowed here");
11635 /* Skip the definition. */
11636 cp_lexer_consume_token (parser->lexer);
11637 if (cp_parser_skip_to_closing_brace (parser))
11638 cp_lexer_consume_token (parser->lexer);
11639 return;
11641 cp_parser_require (parser, CPP_EQ, "`='");
11642 /* Look for the qualified-namespace-specifier. */
11643 namespace_specifier
11644 = cp_parser_qualified_namespace_specifier (parser);
11645 /* Look for the `;' token. */
11646 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11648 /* Register the alias in the symbol table. */
11649 do_namespace_alias (identifier, namespace_specifier);
11652 /* Parse a qualified-namespace-specifier.
11654 qualified-namespace-specifier:
11655 :: [opt] nested-name-specifier [opt] namespace-name
11657 Returns a NAMESPACE_DECL corresponding to the specified
11658 namespace. */
11660 static tree
11661 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11663 /* Look for the optional `::'. */
11664 cp_parser_global_scope_opt (parser,
11665 /*current_scope_valid_p=*/false);
11667 /* Look for the optional nested-name-specifier. */
11668 cp_parser_nested_name_specifier_opt (parser,
11669 /*typename_keyword_p=*/false,
11670 /*check_dependency_p=*/true,
11671 /*type_p=*/false,
11672 /*is_declaration=*/true);
11674 return cp_parser_namespace_name (parser);
11677 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11678 access declaration.
11680 using-declaration:
11681 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11682 using :: unqualified-id ;
11684 access-declaration:
11685 qualified-id ;
11689 static bool
11690 cp_parser_using_declaration (cp_parser* parser,
11691 bool access_declaration_p)
11693 cp_token *token;
11694 bool typename_p = false;
11695 bool global_scope_p;
11696 tree decl;
11697 tree identifier;
11698 tree qscope;
11700 if (access_declaration_p)
11701 cp_parser_parse_tentatively (parser);
11702 else
11704 /* Look for the `using' keyword. */
11705 cp_parser_require_keyword (parser, RID_USING, "`using'");
11707 /* Peek at the next token. */
11708 token = cp_lexer_peek_token (parser->lexer);
11709 /* See if it's `typename'. */
11710 if (token->keyword == RID_TYPENAME)
11712 /* Remember that we've seen it. */
11713 typename_p = true;
11714 /* Consume the `typename' token. */
11715 cp_lexer_consume_token (parser->lexer);
11719 /* Look for the optional global scope qualification. */
11720 global_scope_p
11721 = (cp_parser_global_scope_opt (parser,
11722 /*current_scope_valid_p=*/false)
11723 != NULL_TREE);
11725 /* If we saw `typename', or didn't see `::', then there must be a
11726 nested-name-specifier present. */
11727 if (typename_p || !global_scope_p)
11728 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11729 /*check_dependency_p=*/true,
11730 /*type_p=*/false,
11731 /*is_declaration=*/true);
11732 /* Otherwise, we could be in either of the two productions. In that
11733 case, treat the nested-name-specifier as optional. */
11734 else
11735 qscope = cp_parser_nested_name_specifier_opt (parser,
11736 /*typename_keyword_p=*/false,
11737 /*check_dependency_p=*/true,
11738 /*type_p=*/false,
11739 /*is_declaration=*/true);
11740 if (!qscope)
11741 qscope = global_namespace;
11743 if (access_declaration_p && cp_parser_error_occurred (parser))
11744 /* Something has already gone wrong; there's no need to parse
11745 further. Since an error has occurred, the return value of
11746 cp_parser_parse_definitely will be false, as required. */
11747 return cp_parser_parse_definitely (parser);
11749 /* Parse the unqualified-id. */
11750 identifier = cp_parser_unqualified_id (parser,
11751 /*template_keyword_p=*/false,
11752 /*check_dependency_p=*/true,
11753 /*declarator_p=*/true,
11754 /*optional_p=*/false);
11756 if (access_declaration_p)
11758 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11759 cp_parser_simulate_error (parser);
11760 if (!cp_parser_parse_definitely (parser))
11761 return false;
11764 /* The function we call to handle a using-declaration is different
11765 depending on what scope we are in. */
11766 if (qscope == error_mark_node || identifier == error_mark_node)
11768 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11769 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11770 /* [namespace.udecl]
11772 A using declaration shall not name a template-id. */
11773 error ("a template-id may not appear in a using-declaration");
11774 else
11776 if (at_class_scope_p ())
11778 /* Create the USING_DECL. */
11779 decl = do_class_using_decl (parser->scope, identifier);
11781 if (check_for_bare_parameter_packs (decl))
11782 return false;
11783 else
11784 /* Add it to the list of members in this class. */
11785 finish_member_declaration (decl);
11787 else
11789 decl = cp_parser_lookup_name_simple (parser, identifier);
11790 if (decl == error_mark_node)
11791 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11792 else if (check_for_bare_parameter_packs (decl))
11793 return false;
11794 else if (!at_namespace_scope_p ())
11795 do_local_using_decl (decl, qscope, identifier);
11796 else
11797 do_toplevel_using_decl (decl, qscope, identifier);
11801 /* Look for the final `;'. */
11802 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11804 return true;
11807 /* Parse a using-directive.
11809 using-directive:
11810 using namespace :: [opt] nested-name-specifier [opt]
11811 namespace-name ; */
11813 static void
11814 cp_parser_using_directive (cp_parser* parser)
11816 tree namespace_decl;
11817 tree attribs;
11819 /* Look for the `using' keyword. */
11820 cp_parser_require_keyword (parser, RID_USING, "`using'");
11821 /* And the `namespace' keyword. */
11822 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11823 /* Look for the optional `::' operator. */
11824 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11825 /* And the optional nested-name-specifier. */
11826 cp_parser_nested_name_specifier_opt (parser,
11827 /*typename_keyword_p=*/false,
11828 /*check_dependency_p=*/true,
11829 /*type_p=*/false,
11830 /*is_declaration=*/true);
11831 /* Get the namespace being used. */
11832 namespace_decl = cp_parser_namespace_name (parser);
11833 /* And any specified attributes. */
11834 attribs = cp_parser_attributes_opt (parser);
11835 /* Update the symbol table. */
11836 parse_using_directive (namespace_decl, attribs);
11837 /* Look for the final `;'. */
11838 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11841 /* Parse an asm-definition.
11843 asm-definition:
11844 asm ( string-literal ) ;
11846 GNU Extension:
11848 asm-definition:
11849 asm volatile [opt] ( string-literal ) ;
11850 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11851 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11852 : asm-operand-list [opt] ) ;
11853 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11854 : asm-operand-list [opt]
11855 : asm-operand-list [opt] ) ; */
11857 static void
11858 cp_parser_asm_definition (cp_parser* parser)
11860 tree string;
11861 tree outputs = NULL_TREE;
11862 tree inputs = NULL_TREE;
11863 tree clobbers = NULL_TREE;
11864 tree asm_stmt;
11865 bool volatile_p = false;
11866 bool extended_p = false;
11867 bool invalid_inputs_p = false;
11868 bool invalid_outputs_p = false;
11870 /* Look for the `asm' keyword. */
11871 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11872 /* See if the next token is `volatile'. */
11873 if (cp_parser_allow_gnu_extensions_p (parser)
11874 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11876 /* Remember that we saw the `volatile' keyword. */
11877 volatile_p = true;
11878 /* Consume the token. */
11879 cp_lexer_consume_token (parser->lexer);
11881 /* Look for the opening `('. */
11882 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11883 return;
11884 /* Look for the string. */
11885 string = cp_parser_string_literal (parser, false, false);
11886 if (string == error_mark_node)
11888 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11889 /*consume_paren=*/true);
11890 return;
11893 /* If we're allowing GNU extensions, check for the extended assembly
11894 syntax. Unfortunately, the `:' tokens need not be separated by
11895 a space in C, and so, for compatibility, we tolerate that here
11896 too. Doing that means that we have to treat the `::' operator as
11897 two `:' tokens. */
11898 if (cp_parser_allow_gnu_extensions_p (parser)
11899 && parser->in_function_body
11900 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11901 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11903 bool inputs_p = false;
11904 bool clobbers_p = false;
11906 /* The extended syntax was used. */
11907 extended_p = true;
11909 /* Look for outputs. */
11910 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11912 /* Consume the `:'. */
11913 cp_lexer_consume_token (parser->lexer);
11914 /* Parse the output-operands. */
11915 if (cp_lexer_next_token_is_not (parser->lexer,
11916 CPP_COLON)
11917 && cp_lexer_next_token_is_not (parser->lexer,
11918 CPP_SCOPE)
11919 && cp_lexer_next_token_is_not (parser->lexer,
11920 CPP_CLOSE_PAREN))
11921 outputs = cp_parser_asm_operand_list (parser);
11923 if (outputs == error_mark_node)
11924 invalid_outputs_p = true;
11926 /* If the next token is `::', there are no outputs, and the
11927 next token is the beginning of the inputs. */
11928 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11929 /* The inputs are coming next. */
11930 inputs_p = true;
11932 /* Look for inputs. */
11933 if (inputs_p
11934 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11936 /* Consume the `:' or `::'. */
11937 cp_lexer_consume_token (parser->lexer);
11938 /* Parse the output-operands. */
11939 if (cp_lexer_next_token_is_not (parser->lexer,
11940 CPP_COLON)
11941 && cp_lexer_next_token_is_not (parser->lexer,
11942 CPP_CLOSE_PAREN))
11943 inputs = cp_parser_asm_operand_list (parser);
11945 if (inputs == error_mark_node)
11946 invalid_inputs_p = true;
11948 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11949 /* The clobbers are coming next. */
11950 clobbers_p = true;
11952 /* Look for clobbers. */
11953 if (clobbers_p
11954 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11956 /* Consume the `:' or `::'. */
11957 cp_lexer_consume_token (parser->lexer);
11958 /* Parse the clobbers. */
11959 if (cp_lexer_next_token_is_not (parser->lexer,
11960 CPP_CLOSE_PAREN))
11961 clobbers = cp_parser_asm_clobber_list (parser);
11964 /* Look for the closing `)'. */
11965 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11966 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11967 /*consume_paren=*/true);
11968 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11970 if (!invalid_inputs_p && !invalid_outputs_p)
11972 /* Create the ASM_EXPR. */
11973 if (parser->in_function_body)
11975 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11976 inputs, clobbers);
11977 /* If the extended syntax was not used, mark the ASM_EXPR. */
11978 if (!extended_p)
11980 tree temp = asm_stmt;
11981 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11982 temp = TREE_OPERAND (temp, 0);
11984 ASM_INPUT_P (temp) = 1;
11987 else
11988 cgraph_add_asm_node (string);
11992 /* Declarators [gram.dcl.decl] */
11994 /* Parse an init-declarator.
11996 init-declarator:
11997 declarator initializer [opt]
11999 GNU Extension:
12001 init-declarator:
12002 declarator asm-specification [opt] attributes [opt] initializer [opt]
12004 function-definition:
12005 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12006 function-body
12007 decl-specifier-seq [opt] declarator function-try-block
12009 GNU Extension:
12011 function-definition:
12012 __extension__ function-definition
12014 The DECL_SPECIFIERS apply to this declarator. Returns a
12015 representation of the entity declared. If MEMBER_P is TRUE, then
12016 this declarator appears in a class scope. The new DECL created by
12017 this declarator is returned.
12019 The CHECKS are access checks that should be performed once we know
12020 what entity is being declared (and, therefore, what classes have
12021 befriended it).
12023 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12024 for a function-definition here as well. If the declarator is a
12025 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12026 be TRUE upon return. By that point, the function-definition will
12027 have been completely parsed.
12029 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12030 is FALSE. */
12032 static tree
12033 cp_parser_init_declarator (cp_parser* parser,
12034 cp_decl_specifier_seq *decl_specifiers,
12035 VEC (deferred_access_check,gc)* checks,
12036 bool function_definition_allowed_p,
12037 bool member_p,
12038 int declares_class_or_enum,
12039 bool* function_definition_p)
12041 cp_token *token;
12042 cp_declarator *declarator;
12043 tree prefix_attributes;
12044 tree attributes;
12045 tree asm_specification;
12046 tree initializer;
12047 tree decl = NULL_TREE;
12048 tree scope;
12049 bool is_initialized;
12050 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12051 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12052 "(...)". */
12053 enum cpp_ttype initialization_kind;
12054 bool is_parenthesized_init = false;
12055 bool is_non_constant_init;
12056 int ctor_dtor_or_conv_p;
12057 bool friend_p;
12058 tree pushed_scope = NULL;
12060 /* Gather the attributes that were provided with the
12061 decl-specifiers. */
12062 prefix_attributes = decl_specifiers->attributes;
12064 /* Assume that this is not the declarator for a function
12065 definition. */
12066 if (function_definition_p)
12067 *function_definition_p = false;
12069 /* Defer access checks while parsing the declarator; we cannot know
12070 what names are accessible until we know what is being
12071 declared. */
12072 resume_deferring_access_checks ();
12074 /* Parse the declarator. */
12075 declarator
12076 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12077 &ctor_dtor_or_conv_p,
12078 /*parenthesized_p=*/NULL,
12079 /*member_p=*/false);
12080 /* Gather up the deferred checks. */
12081 stop_deferring_access_checks ();
12083 /* If the DECLARATOR was erroneous, there's no need to go
12084 further. */
12085 if (declarator == cp_error_declarator)
12086 return error_mark_node;
12088 /* Check that the number of template-parameter-lists is OK. */
12089 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12090 return error_mark_node;
12092 if (declares_class_or_enum & 2)
12093 cp_parser_check_for_definition_in_return_type (declarator,
12094 decl_specifiers->type);
12096 /* Figure out what scope the entity declared by the DECLARATOR is
12097 located in. `grokdeclarator' sometimes changes the scope, so
12098 we compute it now. */
12099 scope = get_scope_of_declarator (declarator);
12101 /* If we're allowing GNU extensions, look for an asm-specification
12102 and attributes. */
12103 if (cp_parser_allow_gnu_extensions_p (parser))
12105 /* Look for an asm-specification. */
12106 asm_specification = cp_parser_asm_specification_opt (parser);
12107 /* And attributes. */
12108 attributes = cp_parser_attributes_opt (parser);
12110 else
12112 asm_specification = NULL_TREE;
12113 attributes = NULL_TREE;
12116 /* Peek at the next token. */
12117 token = cp_lexer_peek_token (parser->lexer);
12118 /* Check to see if the token indicates the start of a
12119 function-definition. */
12120 if (cp_parser_token_starts_function_definition_p (token))
12122 if (!function_definition_allowed_p)
12124 /* If a function-definition should not appear here, issue an
12125 error message. */
12126 cp_parser_error (parser,
12127 "a function-definition is not allowed here");
12128 return error_mark_node;
12130 else
12132 /* Neither attributes nor an asm-specification are allowed
12133 on a function-definition. */
12134 if (asm_specification)
12135 error ("an asm-specification is not allowed on a function-definition");
12136 if (attributes)
12137 error ("attributes are not allowed on a function-definition");
12138 /* This is a function-definition. */
12139 *function_definition_p = true;
12141 /* Parse the function definition. */
12142 if (member_p)
12143 decl = cp_parser_save_member_function_body (parser,
12144 decl_specifiers,
12145 declarator,
12146 prefix_attributes);
12147 else
12148 decl
12149 = (cp_parser_function_definition_from_specifiers_and_declarator
12150 (parser, decl_specifiers, prefix_attributes, declarator));
12152 return decl;
12156 /* [dcl.dcl]
12158 Only in function declarations for constructors, destructors, and
12159 type conversions can the decl-specifier-seq be omitted.
12161 We explicitly postpone this check past the point where we handle
12162 function-definitions because we tolerate function-definitions
12163 that are missing their return types in some modes. */
12164 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12166 cp_parser_error (parser,
12167 "expected constructor, destructor, or type conversion");
12168 return error_mark_node;
12171 /* An `=' or an `(' indicates an initializer. */
12172 if (token->type == CPP_EQ
12173 || token->type == CPP_OPEN_PAREN)
12175 is_initialized = true;
12176 initialization_kind = token->type;
12178 else
12180 /* If the init-declarator isn't initialized and isn't followed by a
12181 `,' or `;', it's not a valid init-declarator. */
12182 if (token->type != CPP_COMMA
12183 && token->type != CPP_SEMICOLON)
12185 cp_parser_error (parser, "expected initializer");
12186 return error_mark_node;
12188 is_initialized = false;
12189 initialization_kind = CPP_EOF;
12192 /* Because start_decl has side-effects, we should only call it if we
12193 know we're going ahead. By this point, we know that we cannot
12194 possibly be looking at any other construct. */
12195 cp_parser_commit_to_tentative_parse (parser);
12197 /* If the decl specifiers were bad, issue an error now that we're
12198 sure this was intended to be a declarator. Then continue
12199 declaring the variable(s), as int, to try to cut down on further
12200 errors. */
12201 if (decl_specifiers->any_specifiers_p
12202 && decl_specifiers->type == error_mark_node)
12204 cp_parser_error (parser, "invalid type in declaration");
12205 decl_specifiers->type = integer_type_node;
12208 /* Check to see whether or not this declaration is a friend. */
12209 friend_p = cp_parser_friend_p (decl_specifiers);
12211 /* Enter the newly declared entry in the symbol table. If we're
12212 processing a declaration in a class-specifier, we wait until
12213 after processing the initializer. */
12214 if (!member_p)
12216 if (parser->in_unbraced_linkage_specification_p)
12217 decl_specifiers->storage_class = sc_extern;
12218 decl = start_decl (declarator, decl_specifiers,
12219 is_initialized, attributes, prefix_attributes,
12220 &pushed_scope);
12222 else if (scope)
12223 /* Enter the SCOPE. That way unqualified names appearing in the
12224 initializer will be looked up in SCOPE. */
12225 pushed_scope = push_scope (scope);
12227 /* Perform deferred access control checks, now that we know in which
12228 SCOPE the declared entity resides. */
12229 if (!member_p && decl)
12231 tree saved_current_function_decl = NULL_TREE;
12233 /* If the entity being declared is a function, pretend that we
12234 are in its scope. If it is a `friend', it may have access to
12235 things that would not otherwise be accessible. */
12236 if (TREE_CODE (decl) == FUNCTION_DECL)
12238 saved_current_function_decl = current_function_decl;
12239 current_function_decl = decl;
12242 /* Perform access checks for template parameters. */
12243 cp_parser_perform_template_parameter_access_checks (checks);
12245 /* Perform the access control checks for the declarator and the
12246 the decl-specifiers. */
12247 perform_deferred_access_checks ();
12249 /* Restore the saved value. */
12250 if (TREE_CODE (decl) == FUNCTION_DECL)
12251 current_function_decl = saved_current_function_decl;
12254 /* Parse the initializer. */
12255 initializer = NULL_TREE;
12256 is_parenthesized_init = false;
12257 is_non_constant_init = true;
12258 if (is_initialized)
12260 if (function_declarator_p (declarator))
12262 if (initialization_kind == CPP_EQ)
12263 initializer = cp_parser_pure_specifier (parser);
12264 else
12266 /* If the declaration was erroneous, we don't really
12267 know what the user intended, so just silently
12268 consume the initializer. */
12269 if (decl != error_mark_node)
12270 error ("initializer provided for function");
12271 cp_parser_skip_to_closing_parenthesis (parser,
12272 /*recovering=*/true,
12273 /*or_comma=*/false,
12274 /*consume_paren=*/true);
12277 else
12278 initializer = cp_parser_initializer (parser,
12279 &is_parenthesized_init,
12280 &is_non_constant_init);
12283 /* The old parser allows attributes to appear after a parenthesized
12284 initializer. Mark Mitchell proposed removing this functionality
12285 on the GCC mailing lists on 2002-08-13. This parser accepts the
12286 attributes -- but ignores them. */
12287 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12288 if (cp_parser_attributes_opt (parser))
12289 warning (OPT_Wattributes,
12290 "attributes after parenthesized initializer ignored");
12292 /* For an in-class declaration, use `grokfield' to create the
12293 declaration. */
12294 if (member_p)
12296 if (pushed_scope)
12298 pop_scope (pushed_scope);
12299 pushed_scope = false;
12301 decl = grokfield (declarator, decl_specifiers,
12302 initializer, !is_non_constant_init,
12303 /*asmspec=*/NULL_TREE,
12304 prefix_attributes);
12305 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12306 cp_parser_save_default_args (parser, decl);
12309 /* Finish processing the declaration. But, skip friend
12310 declarations. */
12311 if (!friend_p && decl && decl != error_mark_node)
12313 cp_finish_decl (decl,
12314 initializer, !is_non_constant_init,
12315 asm_specification,
12316 /* If the initializer is in parentheses, then this is
12317 a direct-initialization, which means that an
12318 `explicit' constructor is OK. Otherwise, an
12319 `explicit' constructor cannot be used. */
12320 ((is_parenthesized_init || !is_initialized)
12321 ? 0 : LOOKUP_ONLYCONVERTING));
12323 else if ((cxx_dialect != cxx98) && friend_p
12324 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12325 /* Core issue #226 (C++0x only): A default template-argument
12326 shall not be specified in a friend class template
12327 declaration. */
12328 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12329 /*is_partial=*/0, /*is_friend_decl=*/1);
12331 if (!friend_p && pushed_scope)
12332 pop_scope (pushed_scope);
12334 return decl;
12337 /* Parse a declarator.
12339 declarator:
12340 direct-declarator
12341 ptr-operator declarator
12343 abstract-declarator:
12344 ptr-operator abstract-declarator [opt]
12345 direct-abstract-declarator
12347 GNU Extensions:
12349 declarator:
12350 attributes [opt] direct-declarator
12351 attributes [opt] ptr-operator declarator
12353 abstract-declarator:
12354 attributes [opt] ptr-operator abstract-declarator [opt]
12355 attributes [opt] direct-abstract-declarator
12357 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12358 detect constructor, destructor or conversion operators. It is set
12359 to -1 if the declarator is a name, and +1 if it is a
12360 function. Otherwise it is set to zero. Usually you just want to
12361 test for >0, but internally the negative value is used.
12363 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12364 a decl-specifier-seq unless it declares a constructor, destructor,
12365 or conversion. It might seem that we could check this condition in
12366 semantic analysis, rather than parsing, but that makes it difficult
12367 to handle something like `f()'. We want to notice that there are
12368 no decl-specifiers, and therefore realize that this is an
12369 expression, not a declaration.)
12371 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12372 the declarator is a direct-declarator of the form "(...)".
12374 MEMBER_P is true iff this declarator is a member-declarator. */
12376 static cp_declarator *
12377 cp_parser_declarator (cp_parser* parser,
12378 cp_parser_declarator_kind dcl_kind,
12379 int* ctor_dtor_or_conv_p,
12380 bool* parenthesized_p,
12381 bool member_p)
12383 cp_token *token;
12384 cp_declarator *declarator;
12385 enum tree_code code;
12386 cp_cv_quals cv_quals;
12387 tree class_type;
12388 tree attributes = NULL_TREE;
12390 /* Assume this is not a constructor, destructor, or type-conversion
12391 operator. */
12392 if (ctor_dtor_or_conv_p)
12393 *ctor_dtor_or_conv_p = 0;
12395 if (cp_parser_allow_gnu_extensions_p (parser))
12396 attributes = cp_parser_attributes_opt (parser);
12398 /* Peek at the next token. */
12399 token = cp_lexer_peek_token (parser->lexer);
12401 /* Check for the ptr-operator production. */
12402 cp_parser_parse_tentatively (parser);
12403 /* Parse the ptr-operator. */
12404 code = cp_parser_ptr_operator (parser,
12405 &class_type,
12406 &cv_quals);
12407 /* If that worked, then we have a ptr-operator. */
12408 if (cp_parser_parse_definitely (parser))
12410 /* If a ptr-operator was found, then this declarator was not
12411 parenthesized. */
12412 if (parenthesized_p)
12413 *parenthesized_p = true;
12414 /* The dependent declarator is optional if we are parsing an
12415 abstract-declarator. */
12416 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12417 cp_parser_parse_tentatively (parser);
12419 /* Parse the dependent declarator. */
12420 declarator = cp_parser_declarator (parser, dcl_kind,
12421 /*ctor_dtor_or_conv_p=*/NULL,
12422 /*parenthesized_p=*/NULL,
12423 /*member_p=*/false);
12425 /* If we are parsing an abstract-declarator, we must handle the
12426 case where the dependent declarator is absent. */
12427 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12428 && !cp_parser_parse_definitely (parser))
12429 declarator = NULL;
12431 declarator = cp_parser_make_indirect_declarator
12432 (code, class_type, cv_quals, declarator);
12434 /* Everything else is a direct-declarator. */
12435 else
12437 if (parenthesized_p)
12438 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12439 CPP_OPEN_PAREN);
12440 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12441 ctor_dtor_or_conv_p,
12442 member_p);
12445 if (attributes && declarator && declarator != cp_error_declarator)
12446 declarator->attributes = attributes;
12448 return declarator;
12451 /* Parse a direct-declarator or direct-abstract-declarator.
12453 direct-declarator:
12454 declarator-id
12455 direct-declarator ( parameter-declaration-clause )
12456 cv-qualifier-seq [opt]
12457 exception-specification [opt]
12458 direct-declarator [ constant-expression [opt] ]
12459 ( declarator )
12461 direct-abstract-declarator:
12462 direct-abstract-declarator [opt]
12463 ( parameter-declaration-clause )
12464 cv-qualifier-seq [opt]
12465 exception-specification [opt]
12466 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12467 ( abstract-declarator )
12469 Returns a representation of the declarator. DCL_KIND is
12470 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12471 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12472 we are parsing a direct-declarator. It is
12473 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12474 of ambiguity we prefer an abstract declarator, as per
12475 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12476 cp_parser_declarator. */
12478 static cp_declarator *
12479 cp_parser_direct_declarator (cp_parser* parser,
12480 cp_parser_declarator_kind dcl_kind,
12481 int* ctor_dtor_or_conv_p,
12482 bool member_p)
12484 cp_token *token;
12485 cp_declarator *declarator = NULL;
12486 tree scope = NULL_TREE;
12487 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12488 bool saved_in_declarator_p = parser->in_declarator_p;
12489 bool first = true;
12490 tree pushed_scope = NULL_TREE;
12492 while (true)
12494 /* Peek at the next token. */
12495 token = cp_lexer_peek_token (parser->lexer);
12496 if (token->type == CPP_OPEN_PAREN)
12498 /* This is either a parameter-declaration-clause, or a
12499 parenthesized declarator. When we know we are parsing a
12500 named declarator, it must be a parenthesized declarator
12501 if FIRST is true. For instance, `(int)' is a
12502 parameter-declaration-clause, with an omitted
12503 direct-abstract-declarator. But `((*))', is a
12504 parenthesized abstract declarator. Finally, when T is a
12505 template parameter `(T)' is a
12506 parameter-declaration-clause, and not a parenthesized
12507 named declarator.
12509 We first try and parse a parameter-declaration-clause,
12510 and then try a nested declarator (if FIRST is true).
12512 It is not an error for it not to be a
12513 parameter-declaration-clause, even when FIRST is
12514 false. Consider,
12516 int i (int);
12517 int i (3);
12519 The first is the declaration of a function while the
12520 second is a the definition of a variable, including its
12521 initializer.
12523 Having seen only the parenthesis, we cannot know which of
12524 these two alternatives should be selected. Even more
12525 complex are examples like:
12527 int i (int (a));
12528 int i (int (3));
12530 The former is a function-declaration; the latter is a
12531 variable initialization.
12533 Thus again, we try a parameter-declaration-clause, and if
12534 that fails, we back out and return. */
12536 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12538 cp_parameter_declarator *params;
12539 unsigned saved_num_template_parameter_lists;
12541 /* In a member-declarator, the only valid interpretation
12542 of a parenthesis is the start of a
12543 parameter-declaration-clause. (It is invalid to
12544 initialize a static data member with a parenthesized
12545 initializer; only the "=" form of initialization is
12546 permitted.) */
12547 if (!member_p)
12548 cp_parser_parse_tentatively (parser);
12550 /* Consume the `('. */
12551 cp_lexer_consume_token (parser->lexer);
12552 if (first)
12554 /* If this is going to be an abstract declarator, we're
12555 in a declarator and we can't have default args. */
12556 parser->default_arg_ok_p = false;
12557 parser->in_declarator_p = true;
12560 /* Inside the function parameter list, surrounding
12561 template-parameter-lists do not apply. */
12562 saved_num_template_parameter_lists
12563 = parser->num_template_parameter_lists;
12564 parser->num_template_parameter_lists = 0;
12566 /* Parse the parameter-declaration-clause. */
12567 params = cp_parser_parameter_declaration_clause (parser);
12569 parser->num_template_parameter_lists
12570 = saved_num_template_parameter_lists;
12572 /* If all went well, parse the cv-qualifier-seq and the
12573 exception-specification. */
12574 if (member_p || cp_parser_parse_definitely (parser))
12576 cp_cv_quals cv_quals;
12577 tree exception_specification;
12579 if (ctor_dtor_or_conv_p)
12580 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12581 first = false;
12582 /* Consume the `)'. */
12583 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12585 /* Parse the cv-qualifier-seq. */
12586 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12587 /* And the exception-specification. */
12588 exception_specification
12589 = cp_parser_exception_specification_opt (parser);
12591 /* Create the function-declarator. */
12592 declarator = make_call_declarator (declarator,
12593 params,
12594 cv_quals,
12595 exception_specification);
12596 /* Any subsequent parameter lists are to do with
12597 return type, so are not those of the declared
12598 function. */
12599 parser->default_arg_ok_p = false;
12601 /* Repeat the main loop. */
12602 continue;
12606 /* If this is the first, we can try a parenthesized
12607 declarator. */
12608 if (first)
12610 bool saved_in_type_id_in_expr_p;
12612 parser->default_arg_ok_p = saved_default_arg_ok_p;
12613 parser->in_declarator_p = saved_in_declarator_p;
12615 /* Consume the `('. */
12616 cp_lexer_consume_token (parser->lexer);
12617 /* Parse the nested declarator. */
12618 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12619 parser->in_type_id_in_expr_p = true;
12620 declarator
12621 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12622 /*parenthesized_p=*/NULL,
12623 member_p);
12624 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12625 first = false;
12626 /* Expect a `)'. */
12627 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12628 declarator = cp_error_declarator;
12629 if (declarator == cp_error_declarator)
12630 break;
12632 goto handle_declarator;
12634 /* Otherwise, we must be done. */
12635 else
12636 break;
12638 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12639 && token->type == CPP_OPEN_SQUARE)
12641 /* Parse an array-declarator. */
12642 tree bounds;
12644 if (ctor_dtor_or_conv_p)
12645 *ctor_dtor_or_conv_p = 0;
12647 first = false;
12648 parser->default_arg_ok_p = false;
12649 parser->in_declarator_p = true;
12650 /* Consume the `['. */
12651 cp_lexer_consume_token (parser->lexer);
12652 /* Peek at the next token. */
12653 token = cp_lexer_peek_token (parser->lexer);
12654 /* If the next token is `]', then there is no
12655 constant-expression. */
12656 if (token->type != CPP_CLOSE_SQUARE)
12658 bool non_constant_p;
12660 bounds
12661 = cp_parser_constant_expression (parser,
12662 /*allow_non_constant=*/true,
12663 &non_constant_p);
12664 if (!non_constant_p)
12665 bounds = fold_non_dependent_expr (bounds);
12666 /* Normally, the array bound must be an integral constant
12667 expression. However, as an extension, we allow VLAs
12668 in function scopes. */
12669 else if (!parser->in_function_body)
12671 error ("array bound is not an integer constant");
12672 bounds = error_mark_node;
12675 else
12676 bounds = NULL_TREE;
12677 /* Look for the closing `]'. */
12678 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12680 declarator = cp_error_declarator;
12681 break;
12684 declarator = make_array_declarator (declarator, bounds);
12686 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12688 tree qualifying_scope;
12689 tree unqualified_name;
12690 special_function_kind sfk;
12691 bool abstract_ok;
12692 bool pack_expansion_p = false;
12694 /* Parse a declarator-id */
12695 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12696 if (abstract_ok)
12698 cp_parser_parse_tentatively (parser);
12700 /* If we see an ellipsis, we should be looking at a
12701 parameter pack. */
12702 if (token->type == CPP_ELLIPSIS)
12704 /* Consume the `...' */
12705 cp_lexer_consume_token (parser->lexer);
12707 pack_expansion_p = true;
12711 unqualified_name
12712 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12713 qualifying_scope = parser->scope;
12714 if (abstract_ok)
12716 bool okay = false;
12718 if (!unqualified_name && pack_expansion_p)
12720 /* Check whether an error occurred. */
12721 okay = !cp_parser_error_occurred (parser);
12723 /* We already consumed the ellipsis to mark a
12724 parameter pack, but we have no way to report it,
12725 so abort the tentative parse. We will be exiting
12726 immediately anyway. */
12727 cp_parser_abort_tentative_parse (parser);
12729 else
12730 okay = cp_parser_parse_definitely (parser);
12732 if (!okay)
12733 unqualified_name = error_mark_node;
12734 else if (unqualified_name
12735 && (qualifying_scope
12736 || (TREE_CODE (unqualified_name)
12737 != IDENTIFIER_NODE)))
12739 cp_parser_error (parser, "expected unqualified-id");
12740 unqualified_name = error_mark_node;
12744 if (!unqualified_name)
12745 return NULL;
12746 if (unqualified_name == error_mark_node)
12748 declarator = cp_error_declarator;
12749 pack_expansion_p = false;
12750 declarator->parameter_pack_p = false;
12751 break;
12754 if (qualifying_scope && at_namespace_scope_p ()
12755 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12757 /* In the declaration of a member of a template class
12758 outside of the class itself, the SCOPE will sometimes
12759 be a TYPENAME_TYPE. For example, given:
12761 template <typename T>
12762 int S<T>::R::i = 3;
12764 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12765 this context, we must resolve S<T>::R to an ordinary
12766 type, rather than a typename type.
12768 The reason we normally avoid resolving TYPENAME_TYPEs
12769 is that a specialization of `S' might render
12770 `S<T>::R' not a type. However, if `S' is
12771 specialized, then this `i' will not be used, so there
12772 is no harm in resolving the types here. */
12773 tree type;
12775 /* Resolve the TYPENAME_TYPE. */
12776 type = resolve_typename_type (qualifying_scope,
12777 /*only_current_p=*/false);
12778 /* If that failed, the declarator is invalid. */
12779 if (TREE_CODE (type) == TYPENAME_TYPE)
12780 error ("%<%T::%E%> is not a type",
12781 TYPE_CONTEXT (qualifying_scope),
12782 TYPE_IDENTIFIER (qualifying_scope));
12783 qualifying_scope = type;
12786 sfk = sfk_none;
12788 if (unqualified_name)
12790 tree class_type;
12792 if (qualifying_scope
12793 && CLASS_TYPE_P (qualifying_scope))
12794 class_type = qualifying_scope;
12795 else
12796 class_type = current_class_type;
12798 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12800 tree name_type = TREE_TYPE (unqualified_name);
12801 if (class_type && same_type_p (name_type, class_type))
12803 if (qualifying_scope
12804 && CLASSTYPE_USE_TEMPLATE (name_type))
12806 error ("invalid use of constructor as a template");
12807 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12808 "name the constructor in a qualified name",
12809 class_type,
12810 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12811 class_type, name_type);
12812 declarator = cp_error_declarator;
12813 break;
12815 else
12816 unqualified_name = constructor_name (class_type);
12818 else
12820 /* We do not attempt to print the declarator
12821 here because we do not have enough
12822 information about its original syntactic
12823 form. */
12824 cp_parser_error (parser, "invalid declarator");
12825 declarator = cp_error_declarator;
12826 break;
12830 if (class_type)
12832 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12833 sfk = sfk_destructor;
12834 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12835 sfk = sfk_conversion;
12836 else if (/* There's no way to declare a constructor
12837 for an anonymous type, even if the type
12838 got a name for linkage purposes. */
12839 !TYPE_WAS_ANONYMOUS (class_type)
12840 && constructor_name_p (unqualified_name,
12841 class_type))
12843 unqualified_name = constructor_name (class_type);
12844 sfk = sfk_constructor;
12847 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12848 *ctor_dtor_or_conv_p = -1;
12851 declarator = make_id_declarator (qualifying_scope,
12852 unqualified_name,
12853 sfk);
12854 declarator->id_loc = token->location;
12855 declarator->parameter_pack_p = pack_expansion_p;
12857 if (pack_expansion_p)
12858 maybe_warn_variadic_templates ();
12860 handle_declarator:;
12861 scope = get_scope_of_declarator (declarator);
12862 if (scope)
12863 /* Any names that appear after the declarator-id for a
12864 member are looked up in the containing scope. */
12865 pushed_scope = push_scope (scope);
12866 parser->in_declarator_p = true;
12867 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12868 || (declarator && declarator->kind == cdk_id))
12869 /* Default args are only allowed on function
12870 declarations. */
12871 parser->default_arg_ok_p = saved_default_arg_ok_p;
12872 else
12873 parser->default_arg_ok_p = false;
12875 first = false;
12877 /* We're done. */
12878 else
12879 break;
12882 /* For an abstract declarator, we might wind up with nothing at this
12883 point. That's an error; the declarator is not optional. */
12884 if (!declarator)
12885 cp_parser_error (parser, "expected declarator");
12887 /* If we entered a scope, we must exit it now. */
12888 if (pushed_scope)
12889 pop_scope (pushed_scope);
12891 parser->default_arg_ok_p = saved_default_arg_ok_p;
12892 parser->in_declarator_p = saved_in_declarator_p;
12894 return declarator;
12897 /* Parse a ptr-operator.
12899 ptr-operator:
12900 * cv-qualifier-seq [opt]
12902 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12904 GNU Extension:
12906 ptr-operator:
12907 & cv-qualifier-seq [opt]
12909 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12910 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12911 an rvalue reference. In the case of a pointer-to-member, *TYPE is
12912 filled in with the TYPE containing the member. *CV_QUALS is
12913 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12914 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
12915 Note that the tree codes returned by this function have nothing
12916 to do with the types of trees that will be eventually be created
12917 to represent the pointer or reference type being parsed. They are
12918 just constants with suggestive names. */
12919 static enum tree_code
12920 cp_parser_ptr_operator (cp_parser* parser,
12921 tree* type,
12922 cp_cv_quals *cv_quals)
12924 enum tree_code code = ERROR_MARK;
12925 cp_token *token;
12927 /* Assume that it's not a pointer-to-member. */
12928 *type = NULL_TREE;
12929 /* And that there are no cv-qualifiers. */
12930 *cv_quals = TYPE_UNQUALIFIED;
12932 /* Peek at the next token. */
12933 token = cp_lexer_peek_token (parser->lexer);
12935 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
12936 if (token->type == CPP_MULT)
12937 code = INDIRECT_REF;
12938 else if (token->type == CPP_AND)
12939 code = ADDR_EXPR;
12940 else if ((cxx_dialect != cxx98) &&
12941 token->type == CPP_AND_AND) /* C++0x only */
12942 code = NON_LVALUE_EXPR;
12944 if (code != ERROR_MARK)
12946 /* Consume the `*', `&' or `&&'. */
12947 cp_lexer_consume_token (parser->lexer);
12949 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12950 `&', if we are allowing GNU extensions. (The only qualifier
12951 that can legally appear after `&' is `restrict', but that is
12952 enforced during semantic analysis. */
12953 if (code == INDIRECT_REF
12954 || cp_parser_allow_gnu_extensions_p (parser))
12955 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12957 else
12959 /* Try the pointer-to-member case. */
12960 cp_parser_parse_tentatively (parser);
12961 /* Look for the optional `::' operator. */
12962 cp_parser_global_scope_opt (parser,
12963 /*current_scope_valid_p=*/false);
12964 /* Look for the nested-name specifier. */
12965 cp_parser_nested_name_specifier (parser,
12966 /*typename_keyword_p=*/false,
12967 /*check_dependency_p=*/true,
12968 /*type_p=*/false,
12969 /*is_declaration=*/false);
12970 /* If we found it, and the next token is a `*', then we are
12971 indeed looking at a pointer-to-member operator. */
12972 if (!cp_parser_error_occurred (parser)
12973 && cp_parser_require (parser, CPP_MULT, "`*'"))
12975 /* Indicate that the `*' operator was used. */
12976 code = INDIRECT_REF;
12978 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12979 error ("%qD is a namespace", parser->scope);
12980 else
12982 /* The type of which the member is a member is given by the
12983 current SCOPE. */
12984 *type = parser->scope;
12985 /* The next name will not be qualified. */
12986 parser->scope = NULL_TREE;
12987 parser->qualifying_scope = NULL_TREE;
12988 parser->object_scope = NULL_TREE;
12989 /* Look for the optional cv-qualifier-seq. */
12990 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12993 /* If that didn't work we don't have a ptr-operator. */
12994 if (!cp_parser_parse_definitely (parser))
12995 cp_parser_error (parser, "expected ptr-operator");
12998 return code;
13001 /* Parse an (optional) cv-qualifier-seq.
13003 cv-qualifier-seq:
13004 cv-qualifier cv-qualifier-seq [opt]
13006 cv-qualifier:
13007 const
13008 volatile
13010 GNU Extension:
13012 cv-qualifier:
13013 __restrict__
13015 Returns a bitmask representing the cv-qualifiers. */
13017 static cp_cv_quals
13018 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13020 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13022 while (true)
13024 cp_token *token;
13025 cp_cv_quals cv_qualifier;
13027 /* Peek at the next token. */
13028 token = cp_lexer_peek_token (parser->lexer);
13029 /* See if it's a cv-qualifier. */
13030 switch (token->keyword)
13032 case RID_CONST:
13033 cv_qualifier = TYPE_QUAL_CONST;
13034 break;
13036 case RID_VOLATILE:
13037 cv_qualifier = TYPE_QUAL_VOLATILE;
13038 break;
13040 case RID_RESTRICT:
13041 cv_qualifier = TYPE_QUAL_RESTRICT;
13042 break;
13044 default:
13045 cv_qualifier = TYPE_UNQUALIFIED;
13046 break;
13049 if (!cv_qualifier)
13050 break;
13052 if (cv_quals & cv_qualifier)
13054 error ("duplicate cv-qualifier");
13055 cp_lexer_purge_token (parser->lexer);
13057 else
13059 cp_lexer_consume_token (parser->lexer);
13060 cv_quals |= cv_qualifier;
13064 return cv_quals;
13067 /* Parse a declarator-id.
13069 declarator-id:
13070 id-expression
13071 :: [opt] nested-name-specifier [opt] type-name
13073 In the `id-expression' case, the value returned is as for
13074 cp_parser_id_expression if the id-expression was an unqualified-id.
13075 If the id-expression was a qualified-id, then a SCOPE_REF is
13076 returned. The first operand is the scope (either a NAMESPACE_DECL
13077 or TREE_TYPE), but the second is still just a representation of an
13078 unqualified-id. */
13080 static tree
13081 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13083 tree id;
13084 /* The expression must be an id-expression. Assume that qualified
13085 names are the names of types so that:
13087 template <class T>
13088 int S<T>::R::i = 3;
13090 will work; we must treat `S<T>::R' as the name of a type.
13091 Similarly, assume that qualified names are templates, where
13092 required, so that:
13094 template <class T>
13095 int S<T>::R<T>::i = 3;
13097 will work, too. */
13098 id = cp_parser_id_expression (parser,
13099 /*template_keyword_p=*/false,
13100 /*check_dependency_p=*/false,
13101 /*template_p=*/NULL,
13102 /*declarator_p=*/true,
13103 optional_p);
13104 if (id && BASELINK_P (id))
13105 id = BASELINK_FUNCTIONS (id);
13106 return id;
13109 /* Parse a type-id.
13111 type-id:
13112 type-specifier-seq abstract-declarator [opt]
13114 Returns the TYPE specified. */
13116 static tree
13117 cp_parser_type_id (cp_parser* parser)
13119 cp_decl_specifier_seq type_specifier_seq;
13120 cp_declarator *abstract_declarator;
13122 /* Parse the type-specifier-seq. */
13123 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13124 &type_specifier_seq);
13125 if (type_specifier_seq.type == error_mark_node)
13126 return error_mark_node;
13128 /* There might or might not be an abstract declarator. */
13129 cp_parser_parse_tentatively (parser);
13130 /* Look for the declarator. */
13131 abstract_declarator
13132 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13133 /*parenthesized_p=*/NULL,
13134 /*member_p=*/false);
13135 /* Check to see if there really was a declarator. */
13136 if (!cp_parser_parse_definitely (parser))
13137 abstract_declarator = NULL;
13139 return groktypename (&type_specifier_seq, abstract_declarator);
13142 /* Parse a type-specifier-seq.
13144 type-specifier-seq:
13145 type-specifier type-specifier-seq [opt]
13147 GNU extension:
13149 type-specifier-seq:
13150 attributes type-specifier-seq [opt]
13152 If IS_CONDITION is true, we are at the start of a "condition",
13153 e.g., we've just seen "if (".
13155 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13157 static void
13158 cp_parser_type_specifier_seq (cp_parser* parser,
13159 bool is_condition,
13160 cp_decl_specifier_seq *type_specifier_seq)
13162 bool seen_type_specifier = false;
13163 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13165 /* Clear the TYPE_SPECIFIER_SEQ. */
13166 clear_decl_specs (type_specifier_seq);
13168 /* Parse the type-specifiers and attributes. */
13169 while (true)
13171 tree type_specifier;
13172 bool is_cv_qualifier;
13174 /* Check for attributes first. */
13175 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13177 type_specifier_seq->attributes =
13178 chainon (type_specifier_seq->attributes,
13179 cp_parser_attributes_opt (parser));
13180 continue;
13183 /* Look for the type-specifier. */
13184 type_specifier = cp_parser_type_specifier (parser,
13185 flags,
13186 type_specifier_seq,
13187 /*is_declaration=*/false,
13188 NULL,
13189 &is_cv_qualifier);
13190 if (!type_specifier)
13192 /* If the first type-specifier could not be found, this is not a
13193 type-specifier-seq at all. */
13194 if (!seen_type_specifier)
13196 cp_parser_error (parser, "expected type-specifier");
13197 type_specifier_seq->type = error_mark_node;
13198 return;
13200 /* If subsequent type-specifiers could not be found, the
13201 type-specifier-seq is complete. */
13202 break;
13205 seen_type_specifier = true;
13206 /* The standard says that a condition can be:
13208 type-specifier-seq declarator = assignment-expression
13210 However, given:
13212 struct S {};
13213 if (int S = ...)
13215 we should treat the "S" as a declarator, not as a
13216 type-specifier. The standard doesn't say that explicitly for
13217 type-specifier-seq, but it does say that for
13218 decl-specifier-seq in an ordinary declaration. Perhaps it
13219 would be clearer just to allow a decl-specifier-seq here, and
13220 then add a semantic restriction that if any decl-specifiers
13221 that are not type-specifiers appear, the program is invalid. */
13222 if (is_condition && !is_cv_qualifier)
13223 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13226 cp_parser_check_decl_spec (type_specifier_seq);
13229 /* Parse a parameter-declaration-clause.
13231 parameter-declaration-clause:
13232 parameter-declaration-list [opt] ... [opt]
13233 parameter-declaration-list , ...
13235 Returns a representation for the parameter declarations. A return
13236 value of NULL indicates a parameter-declaration-clause consisting
13237 only of an ellipsis. */
13239 static cp_parameter_declarator *
13240 cp_parser_parameter_declaration_clause (cp_parser* parser)
13242 cp_parameter_declarator *parameters;
13243 cp_token *token;
13244 bool ellipsis_p;
13245 bool is_error;
13247 /* Peek at the next token. */
13248 token = cp_lexer_peek_token (parser->lexer);
13249 /* Check for trivial parameter-declaration-clauses. */
13250 if (token->type == CPP_ELLIPSIS)
13252 /* Consume the `...' token. */
13253 cp_lexer_consume_token (parser->lexer);
13254 return NULL;
13256 else if (token->type == CPP_CLOSE_PAREN)
13257 /* There are no parameters. */
13259 #ifndef NO_IMPLICIT_EXTERN_C
13260 if (in_system_header && current_class_type == NULL
13261 && current_lang_name == lang_name_c)
13262 return NULL;
13263 else
13264 #endif
13265 return no_parameters;
13267 /* Check for `(void)', too, which is a special case. */
13268 else if (token->keyword == RID_VOID
13269 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13270 == CPP_CLOSE_PAREN))
13272 /* Consume the `void' token. */
13273 cp_lexer_consume_token (parser->lexer);
13274 /* There are no parameters. */
13275 return no_parameters;
13278 /* Parse the parameter-declaration-list. */
13279 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13280 /* If a parse error occurred while parsing the
13281 parameter-declaration-list, then the entire
13282 parameter-declaration-clause is erroneous. */
13283 if (is_error)
13284 return NULL;
13286 /* Peek at the next token. */
13287 token = cp_lexer_peek_token (parser->lexer);
13288 /* If it's a `,', the clause should terminate with an ellipsis. */
13289 if (token->type == CPP_COMMA)
13291 /* Consume the `,'. */
13292 cp_lexer_consume_token (parser->lexer);
13293 /* Expect an ellipsis. */
13294 ellipsis_p
13295 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13297 /* It might also be `...' if the optional trailing `,' was
13298 omitted. */
13299 else if (token->type == CPP_ELLIPSIS)
13301 /* Consume the `...' token. */
13302 cp_lexer_consume_token (parser->lexer);
13303 /* And remember that we saw it. */
13304 ellipsis_p = true;
13306 else
13307 ellipsis_p = false;
13309 /* Finish the parameter list. */
13310 if (parameters && ellipsis_p)
13311 parameters->ellipsis_p = true;
13313 return parameters;
13316 /* Parse a parameter-declaration-list.
13318 parameter-declaration-list:
13319 parameter-declaration
13320 parameter-declaration-list , parameter-declaration
13322 Returns a representation of the parameter-declaration-list, as for
13323 cp_parser_parameter_declaration_clause. However, the
13324 `void_list_node' is never appended to the list. Upon return,
13325 *IS_ERROR will be true iff an error occurred. */
13327 static cp_parameter_declarator *
13328 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13330 cp_parameter_declarator *parameters = NULL;
13331 cp_parameter_declarator **tail = &parameters;
13332 bool saved_in_unbraced_linkage_specification_p;
13334 /* Assume all will go well. */
13335 *is_error = false;
13336 /* The special considerations that apply to a function within an
13337 unbraced linkage specifications do not apply to the parameters
13338 to the function. */
13339 saved_in_unbraced_linkage_specification_p
13340 = parser->in_unbraced_linkage_specification_p;
13341 parser->in_unbraced_linkage_specification_p = false;
13343 /* Look for more parameters. */
13344 while (true)
13346 cp_parameter_declarator *parameter;
13347 bool parenthesized_p;
13348 /* Parse the parameter. */
13349 parameter
13350 = cp_parser_parameter_declaration (parser,
13351 /*template_parm_p=*/false,
13352 &parenthesized_p);
13354 /* If a parse error occurred parsing the parameter declaration,
13355 then the entire parameter-declaration-list is erroneous. */
13356 if (!parameter)
13358 *is_error = true;
13359 parameters = NULL;
13360 break;
13362 /* Add the new parameter to the list. */
13363 *tail = parameter;
13364 tail = &parameter->next;
13366 /* Peek at the next token. */
13367 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13368 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13369 /* These are for Objective-C++ */
13370 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13371 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13372 /* The parameter-declaration-list is complete. */
13373 break;
13374 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13376 cp_token *token;
13378 /* Peek at the next token. */
13379 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13380 /* If it's an ellipsis, then the list is complete. */
13381 if (token->type == CPP_ELLIPSIS)
13382 break;
13383 /* Otherwise, there must be more parameters. Consume the
13384 `,'. */
13385 cp_lexer_consume_token (parser->lexer);
13386 /* When parsing something like:
13388 int i(float f, double d)
13390 we can tell after seeing the declaration for "f" that we
13391 are not looking at an initialization of a variable "i",
13392 but rather at the declaration of a function "i".
13394 Due to the fact that the parsing of template arguments
13395 (as specified to a template-id) requires backtracking we
13396 cannot use this technique when inside a template argument
13397 list. */
13398 if (!parser->in_template_argument_list_p
13399 && !parser->in_type_id_in_expr_p
13400 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13401 /* However, a parameter-declaration of the form
13402 "foat(f)" (which is a valid declaration of a
13403 parameter "f") can also be interpreted as an
13404 expression (the conversion of "f" to "float"). */
13405 && !parenthesized_p)
13406 cp_parser_commit_to_tentative_parse (parser);
13408 else
13410 cp_parser_error (parser, "expected %<,%> or %<...%>");
13411 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13412 cp_parser_skip_to_closing_parenthesis (parser,
13413 /*recovering=*/true,
13414 /*or_comma=*/false,
13415 /*consume_paren=*/false);
13416 break;
13420 parser->in_unbraced_linkage_specification_p
13421 = saved_in_unbraced_linkage_specification_p;
13423 return parameters;
13426 /* Parse a parameter declaration.
13428 parameter-declaration:
13429 decl-specifier-seq ... [opt] declarator
13430 decl-specifier-seq declarator = assignment-expression
13431 decl-specifier-seq ... [opt] abstract-declarator [opt]
13432 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13434 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13435 declares a template parameter. (In that case, a non-nested `>'
13436 token encountered during the parsing of the assignment-expression
13437 is not interpreted as a greater-than operator.)
13439 Returns a representation of the parameter, or NULL if an error
13440 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13441 true iff the declarator is of the form "(p)". */
13443 static cp_parameter_declarator *
13444 cp_parser_parameter_declaration (cp_parser *parser,
13445 bool template_parm_p,
13446 bool *parenthesized_p)
13448 int declares_class_or_enum;
13449 bool greater_than_is_operator_p;
13450 cp_decl_specifier_seq decl_specifiers;
13451 cp_declarator *declarator;
13452 tree default_argument;
13453 cp_token *token;
13454 const char *saved_message;
13456 /* In a template parameter, `>' is not an operator.
13458 [temp.param]
13460 When parsing a default template-argument for a non-type
13461 template-parameter, the first non-nested `>' is taken as the end
13462 of the template parameter-list rather than a greater-than
13463 operator. */
13464 greater_than_is_operator_p = !template_parm_p;
13466 /* Type definitions may not appear in parameter types. */
13467 saved_message = parser->type_definition_forbidden_message;
13468 parser->type_definition_forbidden_message
13469 = "types may not be defined in parameter types";
13471 /* Parse the declaration-specifiers. */
13472 cp_parser_decl_specifier_seq (parser,
13473 CP_PARSER_FLAGS_NONE,
13474 &decl_specifiers,
13475 &declares_class_or_enum);
13476 /* If an error occurred, there's no reason to attempt to parse the
13477 rest of the declaration. */
13478 if (cp_parser_error_occurred (parser))
13480 parser->type_definition_forbidden_message = saved_message;
13481 return NULL;
13484 /* Peek at the next token. */
13485 token = cp_lexer_peek_token (parser->lexer);
13487 /* If the next token is a `)', `,', `=', `>', or `...', then there
13488 is no declarator. However, when variadic templates are enabled,
13489 there may be a declarator following `...'. */
13490 if (token->type == CPP_CLOSE_PAREN
13491 || token->type == CPP_COMMA
13492 || token->type == CPP_EQ
13493 || token->type == CPP_GREATER)
13495 declarator = NULL;
13496 if (parenthesized_p)
13497 *parenthesized_p = false;
13499 /* Otherwise, there should be a declarator. */
13500 else
13502 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13503 parser->default_arg_ok_p = false;
13505 /* After seeing a decl-specifier-seq, if the next token is not a
13506 "(", there is no possibility that the code is a valid
13507 expression. Therefore, if parsing tentatively, we commit at
13508 this point. */
13509 if (!parser->in_template_argument_list_p
13510 /* In an expression context, having seen:
13512 (int((char ...
13514 we cannot be sure whether we are looking at a
13515 function-type (taking a "char" as a parameter) or a cast
13516 of some object of type "char" to "int". */
13517 && !parser->in_type_id_in_expr_p
13518 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13519 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13520 cp_parser_commit_to_tentative_parse (parser);
13521 /* Parse the declarator. */
13522 declarator = cp_parser_declarator (parser,
13523 CP_PARSER_DECLARATOR_EITHER,
13524 /*ctor_dtor_or_conv_p=*/NULL,
13525 parenthesized_p,
13526 /*member_p=*/false);
13527 parser->default_arg_ok_p = saved_default_arg_ok_p;
13528 /* After the declarator, allow more attributes. */
13529 decl_specifiers.attributes
13530 = chainon (decl_specifiers.attributes,
13531 cp_parser_attributes_opt (parser));
13534 /* If the next token is an ellipsis, and we have not seen a
13535 declarator name, and the type of the declarator contains parameter
13536 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13537 a parameter pack expansion expression. Otherwise, leave the
13538 ellipsis for a C-style variadic function. */
13539 token = cp_lexer_peek_token (parser->lexer);
13540 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13542 tree type = decl_specifiers.type;
13544 if (type && DECL_P (type))
13545 type = TREE_TYPE (type);
13547 if (type
13548 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13549 && declarator_can_be_parameter_pack (declarator)
13550 && (!declarator || !declarator->parameter_pack_p)
13551 && uses_parameter_packs (type))
13553 /* Consume the `...'. */
13554 cp_lexer_consume_token (parser->lexer);
13555 maybe_warn_variadic_templates ();
13557 /* Build a pack expansion type */
13558 if (declarator)
13559 declarator->parameter_pack_p = true;
13560 else
13561 decl_specifiers.type = make_pack_expansion (type);
13565 /* The restriction on defining new types applies only to the type
13566 of the parameter, not to the default argument. */
13567 parser->type_definition_forbidden_message = saved_message;
13569 /* If the next token is `=', then process a default argument. */
13570 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13572 /* Consume the `='. */
13573 cp_lexer_consume_token (parser->lexer);
13575 /* If we are defining a class, then the tokens that make up the
13576 default argument must be saved and processed later. */
13577 if (!template_parm_p && at_class_scope_p ()
13578 && TYPE_BEING_DEFINED (current_class_type))
13580 unsigned depth = 0;
13581 cp_token *first_token;
13582 cp_token *token;
13584 /* Add tokens until we have processed the entire default
13585 argument. We add the range [first_token, token). */
13586 first_token = cp_lexer_peek_token (parser->lexer);
13587 while (true)
13589 bool done = false;
13591 /* Peek at the next token. */
13592 token = cp_lexer_peek_token (parser->lexer);
13593 /* What we do depends on what token we have. */
13594 switch (token->type)
13596 /* In valid code, a default argument must be
13597 immediately followed by a `,' `)', or `...'. */
13598 case CPP_COMMA:
13599 case CPP_CLOSE_PAREN:
13600 case CPP_ELLIPSIS:
13601 /* If we run into a non-nested `;', `}', or `]',
13602 then the code is invalid -- but the default
13603 argument is certainly over. */
13604 case CPP_SEMICOLON:
13605 case CPP_CLOSE_BRACE:
13606 case CPP_CLOSE_SQUARE:
13607 if (depth == 0)
13608 done = true;
13609 /* Update DEPTH, if necessary. */
13610 else if (token->type == CPP_CLOSE_PAREN
13611 || token->type == CPP_CLOSE_BRACE
13612 || token->type == CPP_CLOSE_SQUARE)
13613 --depth;
13614 break;
13616 case CPP_OPEN_PAREN:
13617 case CPP_OPEN_SQUARE:
13618 case CPP_OPEN_BRACE:
13619 ++depth;
13620 break;
13622 case CPP_RSHIFT:
13623 if (cxx_dialect == cxx98)
13624 break;
13625 /* Fall through for C++0x, which treats the `>>'
13626 operator like two `>' tokens in certain
13627 cases. */
13629 case CPP_GREATER:
13630 /* If we see a non-nested `>', and `>' is not an
13631 operator, then it marks the end of the default
13632 argument. */
13633 if (!depth && !greater_than_is_operator_p)
13634 done = true;
13635 break;
13637 /* If we run out of tokens, issue an error message. */
13638 case CPP_EOF:
13639 case CPP_PRAGMA_EOL:
13640 error ("file ends in default argument");
13641 done = true;
13642 break;
13644 case CPP_NAME:
13645 case CPP_SCOPE:
13646 /* In these cases, we should look for template-ids.
13647 For example, if the default argument is
13648 `X<int, double>()', we need to do name lookup to
13649 figure out whether or not `X' is a template; if
13650 so, the `,' does not end the default argument.
13652 That is not yet done. */
13653 break;
13655 default:
13656 break;
13659 /* If we've reached the end, stop. */
13660 if (done)
13661 break;
13663 /* Add the token to the token block. */
13664 token = cp_lexer_consume_token (parser->lexer);
13667 /* Create a DEFAULT_ARG to represent the unparsed default
13668 argument. */
13669 default_argument = make_node (DEFAULT_ARG);
13670 DEFARG_TOKENS (default_argument)
13671 = cp_token_cache_new (first_token, token);
13672 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13674 /* Outside of a class definition, we can just parse the
13675 assignment-expression. */
13676 else
13677 default_argument
13678 = cp_parser_default_argument (parser, template_parm_p);
13680 if (!parser->default_arg_ok_p)
13682 if (!flag_pedantic_errors)
13683 warning (0, "deprecated use of default argument for parameter of non-function");
13684 else
13686 error ("default arguments are only permitted for function parameters");
13687 default_argument = NULL_TREE;
13690 else if ((declarator && declarator->parameter_pack_p)
13691 || (decl_specifiers.type
13692 && PACK_EXPANSION_P (decl_specifiers.type)))
13694 const char* kind = template_parm_p? "template " : "";
13696 /* Find the name of the parameter pack. */
13697 cp_declarator *id_declarator = declarator;
13698 while (id_declarator && id_declarator->kind != cdk_id)
13699 id_declarator = id_declarator->declarator;
13701 if (id_declarator && id_declarator->kind == cdk_id)
13702 error ("%sparameter pack %qD cannot have a default argument",
13703 kind, id_declarator->u.id.unqualified_name);
13704 else
13705 error ("%sparameter pack cannot have a default argument",
13706 kind);
13708 default_argument = NULL_TREE;
13711 else
13712 default_argument = NULL_TREE;
13714 return make_parameter_declarator (&decl_specifiers,
13715 declarator,
13716 default_argument);
13719 /* Parse a default argument and return it.
13721 TEMPLATE_PARM_P is true if this is a default argument for a
13722 non-type template parameter. */
13723 static tree
13724 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
13726 tree default_argument = NULL_TREE;
13727 bool saved_greater_than_is_operator_p;
13728 bool saved_local_variables_forbidden_p;
13730 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13731 set correctly. */
13732 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
13733 parser->greater_than_is_operator_p = !template_parm_p;
13734 /* Local variable names (and the `this' keyword) may not
13735 appear in a default argument. */
13736 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13737 parser->local_variables_forbidden_p = true;
13738 /* The default argument expression may cause implicitly
13739 defined member functions to be synthesized, which will
13740 result in garbage collection. We must treat this
13741 situation as if we were within the body of function so as
13742 to avoid collecting live data on the stack. */
13743 ++function_depth;
13744 /* Parse the assignment-expression. */
13745 if (template_parm_p)
13746 push_deferring_access_checks (dk_no_deferred);
13747 default_argument
13748 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13749 if (template_parm_p)
13750 pop_deferring_access_checks ();
13751 /* Restore saved state. */
13752 --function_depth;
13753 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
13754 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13756 return default_argument;
13759 /* Parse a function-body.
13761 function-body:
13762 compound_statement */
13764 static void
13765 cp_parser_function_body (cp_parser *parser)
13767 cp_parser_compound_statement (parser, NULL, false);
13770 /* Parse a ctor-initializer-opt followed by a function-body. Return
13771 true if a ctor-initializer was present. */
13773 static bool
13774 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13776 tree body;
13777 bool ctor_initializer_p;
13779 /* Begin the function body. */
13780 body = begin_function_body ();
13781 /* Parse the optional ctor-initializer. */
13782 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13783 /* Parse the function-body. */
13784 cp_parser_function_body (parser);
13785 /* Finish the function body. */
13786 finish_function_body (body);
13788 return ctor_initializer_p;
13791 /* Parse an initializer.
13793 initializer:
13794 = initializer-clause
13795 ( expression-list )
13797 Returns an expression representing the initializer. If no
13798 initializer is present, NULL_TREE is returned.
13800 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13801 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13802 set to FALSE if there is no initializer present. If there is an
13803 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13804 is set to true; otherwise it is set to false. */
13806 static tree
13807 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13808 bool* non_constant_p)
13810 cp_token *token;
13811 tree init;
13813 /* Peek at the next token. */
13814 token = cp_lexer_peek_token (parser->lexer);
13816 /* Let our caller know whether or not this initializer was
13817 parenthesized. */
13818 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13819 /* Assume that the initializer is constant. */
13820 *non_constant_p = false;
13822 if (token->type == CPP_EQ)
13824 /* Consume the `='. */
13825 cp_lexer_consume_token (parser->lexer);
13826 /* Parse the initializer-clause. */
13827 init = cp_parser_initializer_clause (parser, non_constant_p);
13829 else if (token->type == CPP_OPEN_PAREN)
13830 init = cp_parser_parenthesized_expression_list (parser, false,
13831 /*cast_p=*/false,
13832 /*allow_expansion_p=*/true,
13833 non_constant_p);
13834 else
13836 /* Anything else is an error. */
13837 cp_parser_error (parser, "expected initializer");
13838 init = error_mark_node;
13841 return init;
13844 /* Parse an initializer-clause.
13846 initializer-clause:
13847 assignment-expression
13848 { initializer-list , [opt] }
13851 Returns an expression representing the initializer.
13853 If the `assignment-expression' production is used the value
13854 returned is simply a representation for the expression.
13856 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13857 the elements of the initializer-list (or NULL, if the last
13858 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13859 NULL_TREE. There is no way to detect whether or not the optional
13860 trailing `,' was provided. NON_CONSTANT_P is as for
13861 cp_parser_initializer. */
13863 static tree
13864 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13866 tree initializer;
13868 /* Assume the expression is constant. */
13869 *non_constant_p = false;
13871 /* If it is not a `{', then we are looking at an
13872 assignment-expression. */
13873 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13875 initializer
13876 = cp_parser_constant_expression (parser,
13877 /*allow_non_constant_p=*/true,
13878 non_constant_p);
13879 if (!*non_constant_p)
13880 initializer = fold_non_dependent_expr (initializer);
13882 else
13884 /* Consume the `{' token. */
13885 cp_lexer_consume_token (parser->lexer);
13886 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13887 initializer = make_node (CONSTRUCTOR);
13888 /* If it's not a `}', then there is a non-trivial initializer. */
13889 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13891 /* Parse the initializer list. */
13892 CONSTRUCTOR_ELTS (initializer)
13893 = cp_parser_initializer_list (parser, non_constant_p);
13894 /* A trailing `,' token is allowed. */
13895 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13896 cp_lexer_consume_token (parser->lexer);
13898 /* Now, there should be a trailing `}'. */
13899 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13902 return initializer;
13905 /* Parse an initializer-list.
13907 initializer-list:
13908 initializer-clause ... [opt]
13909 initializer-list , initializer-clause ... [opt]
13911 GNU Extension:
13913 initializer-list:
13914 identifier : initializer-clause
13915 initializer-list, identifier : initializer-clause
13917 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13918 for the initializer. If the INDEX of the elt is non-NULL, it is the
13919 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13920 as for cp_parser_initializer. */
13922 static VEC(constructor_elt,gc) *
13923 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13925 VEC(constructor_elt,gc) *v = NULL;
13927 /* Assume all of the expressions are constant. */
13928 *non_constant_p = false;
13930 /* Parse the rest of the list. */
13931 while (true)
13933 cp_token *token;
13934 tree identifier;
13935 tree initializer;
13936 bool clause_non_constant_p;
13938 /* If the next token is an identifier and the following one is a
13939 colon, we are looking at the GNU designated-initializer
13940 syntax. */
13941 if (cp_parser_allow_gnu_extensions_p (parser)
13942 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13943 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13945 /* Warn the user that they are using an extension. */
13946 if (pedantic)
13947 pedwarn ("ISO C++ does not allow designated initializers");
13948 /* Consume the identifier. */
13949 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13950 /* Consume the `:'. */
13951 cp_lexer_consume_token (parser->lexer);
13953 else
13954 identifier = NULL_TREE;
13956 /* Parse the initializer. */
13957 initializer = cp_parser_initializer_clause (parser,
13958 &clause_non_constant_p);
13959 /* If any clause is non-constant, so is the entire initializer. */
13960 if (clause_non_constant_p)
13961 *non_constant_p = true;
13963 /* If we have an ellipsis, this is an initializer pack
13964 expansion. */
13965 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13967 /* Consume the `...'. */
13968 cp_lexer_consume_token (parser->lexer);
13970 /* Turn the initializer into an initializer expansion. */
13971 initializer = make_pack_expansion (initializer);
13974 /* Add it to the vector. */
13975 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13977 /* If the next token is not a comma, we have reached the end of
13978 the list. */
13979 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13980 break;
13982 /* Peek at the next token. */
13983 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13984 /* If the next token is a `}', then we're still done. An
13985 initializer-clause can have a trailing `,' after the
13986 initializer-list and before the closing `}'. */
13987 if (token->type == CPP_CLOSE_BRACE)
13988 break;
13990 /* Consume the `,' token. */
13991 cp_lexer_consume_token (parser->lexer);
13994 return v;
13997 /* Classes [gram.class] */
13999 /* Parse a class-name.
14001 class-name:
14002 identifier
14003 template-id
14005 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14006 to indicate that names looked up in dependent types should be
14007 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14008 keyword has been used to indicate that the name that appears next
14009 is a template. TAG_TYPE indicates the explicit tag given before
14010 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14011 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14012 is the class being defined in a class-head.
14014 Returns the TYPE_DECL representing the class. */
14016 static tree
14017 cp_parser_class_name (cp_parser *parser,
14018 bool typename_keyword_p,
14019 bool template_keyword_p,
14020 enum tag_types tag_type,
14021 bool check_dependency_p,
14022 bool class_head_p,
14023 bool is_declaration)
14025 tree decl;
14026 tree scope;
14027 bool typename_p;
14028 cp_token *token;
14030 /* All class-names start with an identifier. */
14031 token = cp_lexer_peek_token (parser->lexer);
14032 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14034 cp_parser_error (parser, "expected class-name");
14035 return error_mark_node;
14038 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14039 to a template-id, so we save it here. */
14040 scope = parser->scope;
14041 if (scope == error_mark_node)
14042 return error_mark_node;
14044 /* Any name names a type if we're following the `typename' keyword
14045 in a qualified name where the enclosing scope is type-dependent. */
14046 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14047 && dependent_type_p (scope));
14048 /* Handle the common case (an identifier, but not a template-id)
14049 efficiently. */
14050 if (token->type == CPP_NAME
14051 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14053 cp_token *identifier_token;
14054 tree identifier;
14055 bool ambiguous_p;
14057 /* Look for the identifier. */
14058 identifier_token = cp_lexer_peek_token (parser->lexer);
14059 ambiguous_p = identifier_token->ambiguous_p;
14060 identifier = cp_parser_identifier (parser);
14061 /* If the next token isn't an identifier, we are certainly not
14062 looking at a class-name. */
14063 if (identifier == error_mark_node)
14064 decl = error_mark_node;
14065 /* If we know this is a type-name, there's no need to look it
14066 up. */
14067 else if (typename_p)
14068 decl = identifier;
14069 else
14071 tree ambiguous_decls;
14072 /* If we already know that this lookup is ambiguous, then
14073 we've already issued an error message; there's no reason
14074 to check again. */
14075 if (ambiguous_p)
14077 cp_parser_simulate_error (parser);
14078 return error_mark_node;
14080 /* If the next token is a `::', then the name must be a type
14081 name.
14083 [basic.lookup.qual]
14085 During the lookup for a name preceding the :: scope
14086 resolution operator, object, function, and enumerator
14087 names are ignored. */
14088 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14089 tag_type = typename_type;
14090 /* Look up the name. */
14091 decl = cp_parser_lookup_name (parser, identifier,
14092 tag_type,
14093 /*is_template=*/false,
14094 /*is_namespace=*/false,
14095 check_dependency_p,
14096 &ambiguous_decls);
14097 if (ambiguous_decls)
14099 error ("reference to %qD is ambiguous", identifier);
14100 print_candidates (ambiguous_decls);
14101 if (cp_parser_parsing_tentatively (parser))
14103 identifier_token->ambiguous_p = true;
14104 cp_parser_simulate_error (parser);
14106 return error_mark_node;
14110 else
14112 /* Try a template-id. */
14113 decl = cp_parser_template_id (parser, template_keyword_p,
14114 check_dependency_p,
14115 is_declaration);
14116 if (decl == error_mark_node)
14117 return error_mark_node;
14120 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14122 /* If this is a typename, create a TYPENAME_TYPE. */
14123 if (typename_p && decl != error_mark_node)
14125 decl = make_typename_type (scope, decl, typename_type,
14126 /*complain=*/tf_error);
14127 if (decl != error_mark_node)
14128 decl = TYPE_NAME (decl);
14131 /* Check to see that it is really the name of a class. */
14132 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14133 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14134 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14135 /* Situations like this:
14137 template <typename T> struct A {
14138 typename T::template X<int>::I i;
14141 are problematic. Is `T::template X<int>' a class-name? The
14142 standard does not seem to be definitive, but there is no other
14143 valid interpretation of the following `::'. Therefore, those
14144 names are considered class-names. */
14146 decl = make_typename_type (scope, decl, tag_type, tf_error);
14147 if (decl != error_mark_node)
14148 decl = TYPE_NAME (decl);
14150 else if (TREE_CODE (decl) != TYPE_DECL
14151 || TREE_TYPE (decl) == error_mark_node
14152 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
14153 decl = error_mark_node;
14155 if (decl == error_mark_node)
14156 cp_parser_error (parser, "expected class-name");
14158 return decl;
14161 /* Parse a class-specifier.
14163 class-specifier:
14164 class-head { member-specification [opt] }
14166 Returns the TREE_TYPE representing the class. */
14168 static tree
14169 cp_parser_class_specifier (cp_parser* parser)
14171 cp_token *token;
14172 tree type;
14173 tree attributes = NULL_TREE;
14174 int has_trailing_semicolon;
14175 bool nested_name_specifier_p;
14176 unsigned saved_num_template_parameter_lists;
14177 bool saved_in_function_body;
14178 tree old_scope = NULL_TREE;
14179 tree scope = NULL_TREE;
14180 tree bases;
14182 push_deferring_access_checks (dk_no_deferred);
14184 /* Parse the class-head. */
14185 type = cp_parser_class_head (parser,
14186 &nested_name_specifier_p,
14187 &attributes,
14188 &bases);
14189 /* If the class-head was a semantic disaster, skip the entire body
14190 of the class. */
14191 if (!type)
14193 cp_parser_skip_to_end_of_block_or_statement (parser);
14194 pop_deferring_access_checks ();
14195 return error_mark_node;
14198 /* Look for the `{'. */
14199 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14201 pop_deferring_access_checks ();
14202 return error_mark_node;
14205 /* Process the base classes. If they're invalid, skip the
14206 entire class body. */
14207 if (!xref_basetypes (type, bases))
14209 /* Consuming the closing brace yields better error messages
14210 later on. */
14211 if (cp_parser_skip_to_closing_brace (parser))
14212 cp_lexer_consume_token (parser->lexer);
14213 pop_deferring_access_checks ();
14214 return error_mark_node;
14217 /* Issue an error message if type-definitions are forbidden here. */
14218 cp_parser_check_type_definition (parser);
14219 /* Remember that we are defining one more class. */
14220 ++parser->num_classes_being_defined;
14221 /* Inside the class, surrounding template-parameter-lists do not
14222 apply. */
14223 saved_num_template_parameter_lists
14224 = parser->num_template_parameter_lists;
14225 parser->num_template_parameter_lists = 0;
14226 /* We are not in a function body. */
14227 saved_in_function_body = parser->in_function_body;
14228 parser->in_function_body = false;
14230 /* Start the class. */
14231 if (nested_name_specifier_p)
14233 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14234 old_scope = push_inner_scope (scope);
14236 type = begin_class_definition (type, attributes);
14238 if (type == error_mark_node)
14239 /* If the type is erroneous, skip the entire body of the class. */
14240 cp_parser_skip_to_closing_brace (parser);
14241 else
14242 /* Parse the member-specification. */
14243 cp_parser_member_specification_opt (parser);
14245 /* Look for the trailing `}'. */
14246 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14247 /* We get better error messages by noticing a common problem: a
14248 missing trailing `;'. */
14249 token = cp_lexer_peek_token (parser->lexer);
14250 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14251 /* Look for trailing attributes to apply to this class. */
14252 if (cp_parser_allow_gnu_extensions_p (parser))
14253 attributes = cp_parser_attributes_opt (parser);
14254 if (type != error_mark_node)
14255 type = finish_struct (type, attributes);
14256 if (nested_name_specifier_p)
14257 pop_inner_scope (old_scope, scope);
14258 /* If this class is not itself within the scope of another class,
14259 then we need to parse the bodies of all of the queued function
14260 definitions. Note that the queued functions defined in a class
14261 are not always processed immediately following the
14262 class-specifier for that class. Consider:
14264 struct A {
14265 struct B { void f() { sizeof (A); } };
14268 If `f' were processed before the processing of `A' were
14269 completed, there would be no way to compute the size of `A'.
14270 Note that the nesting we are interested in here is lexical --
14271 not the semantic nesting given by TYPE_CONTEXT. In particular,
14272 for:
14274 struct A { struct B; };
14275 struct A::B { void f() { } };
14277 there is no need to delay the parsing of `A::B::f'. */
14278 if (--parser->num_classes_being_defined == 0)
14280 tree queue_entry;
14281 tree fn;
14282 tree class_type = NULL_TREE;
14283 tree pushed_scope = NULL_TREE;
14285 /* In a first pass, parse default arguments to the functions.
14286 Then, in a second pass, parse the bodies of the functions.
14287 This two-phased approach handles cases like:
14289 struct S {
14290 void f() { g(); }
14291 void g(int i = 3);
14295 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14296 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14297 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14298 TREE_PURPOSE (parser->unparsed_functions_queues)
14299 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14301 fn = TREE_VALUE (queue_entry);
14302 /* If there are default arguments that have not yet been processed,
14303 take care of them now. */
14304 if (class_type != TREE_PURPOSE (queue_entry))
14306 if (pushed_scope)
14307 pop_scope (pushed_scope);
14308 class_type = TREE_PURPOSE (queue_entry);
14309 pushed_scope = push_scope (class_type);
14311 /* Make sure that any template parameters are in scope. */
14312 maybe_begin_member_template_processing (fn);
14313 /* Parse the default argument expressions. */
14314 cp_parser_late_parsing_default_args (parser, fn);
14315 /* Remove any template parameters from the symbol table. */
14316 maybe_end_member_template_processing ();
14318 if (pushed_scope)
14319 pop_scope (pushed_scope);
14320 /* Now parse the body of the functions. */
14321 for (TREE_VALUE (parser->unparsed_functions_queues)
14322 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14323 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14324 TREE_VALUE (parser->unparsed_functions_queues)
14325 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14327 /* Figure out which function we need to process. */
14328 fn = TREE_VALUE (queue_entry);
14329 /* Parse the function. */
14330 cp_parser_late_parsing_for_member (parser, fn);
14334 /* Put back any saved access checks. */
14335 pop_deferring_access_checks ();
14337 /* Restore saved state. */
14338 parser->in_function_body = saved_in_function_body;
14339 parser->num_template_parameter_lists
14340 = saved_num_template_parameter_lists;
14342 return type;
14345 /* Parse a class-head.
14347 class-head:
14348 class-key identifier [opt] base-clause [opt]
14349 class-key nested-name-specifier identifier base-clause [opt]
14350 class-key nested-name-specifier [opt] template-id
14351 base-clause [opt]
14353 GNU Extensions:
14354 class-key attributes identifier [opt] base-clause [opt]
14355 class-key attributes nested-name-specifier identifier base-clause [opt]
14356 class-key attributes nested-name-specifier [opt] template-id
14357 base-clause [opt]
14359 Upon return BASES is initialized to the list of base classes (or
14360 NULL, if there are none) in the same form returned by
14361 cp_parser_base_clause.
14363 Returns the TYPE of the indicated class. Sets
14364 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14365 involving a nested-name-specifier was used, and FALSE otherwise.
14367 Returns error_mark_node if this is not a class-head.
14369 Returns NULL_TREE if the class-head is syntactically valid, but
14370 semantically invalid in a way that means we should skip the entire
14371 body of the class. */
14373 static tree
14374 cp_parser_class_head (cp_parser* parser,
14375 bool* nested_name_specifier_p,
14376 tree *attributes_p,
14377 tree *bases)
14379 tree nested_name_specifier;
14380 enum tag_types class_key;
14381 tree id = NULL_TREE;
14382 tree type = NULL_TREE;
14383 tree attributes;
14384 bool template_id_p = false;
14385 bool qualified_p = false;
14386 bool invalid_nested_name_p = false;
14387 bool invalid_explicit_specialization_p = false;
14388 tree pushed_scope = NULL_TREE;
14389 unsigned num_templates;
14391 /* Assume no nested-name-specifier will be present. */
14392 *nested_name_specifier_p = false;
14393 /* Assume no template parameter lists will be used in defining the
14394 type. */
14395 num_templates = 0;
14397 *bases = NULL_TREE;
14399 /* Look for the class-key. */
14400 class_key = cp_parser_class_key (parser);
14401 if (class_key == none_type)
14402 return error_mark_node;
14404 /* Parse the attributes. */
14405 attributes = cp_parser_attributes_opt (parser);
14407 /* If the next token is `::', that is invalid -- but sometimes
14408 people do try to write:
14410 struct ::S {};
14412 Handle this gracefully by accepting the extra qualifier, and then
14413 issuing an error about it later if this really is a
14414 class-head. If it turns out just to be an elaborated type
14415 specifier, remain silent. */
14416 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14417 qualified_p = true;
14419 push_deferring_access_checks (dk_no_check);
14421 /* Determine the name of the class. Begin by looking for an
14422 optional nested-name-specifier. */
14423 nested_name_specifier
14424 = cp_parser_nested_name_specifier_opt (parser,
14425 /*typename_keyword_p=*/false,
14426 /*check_dependency_p=*/false,
14427 /*type_p=*/false,
14428 /*is_declaration=*/false);
14429 /* If there was a nested-name-specifier, then there *must* be an
14430 identifier. */
14431 if (nested_name_specifier)
14433 /* Although the grammar says `identifier', it really means
14434 `class-name' or `template-name'. You are only allowed to
14435 define a class that has already been declared with this
14436 syntax.
14438 The proposed resolution for Core Issue 180 says that wherever
14439 you see `class T::X' you should treat `X' as a type-name.
14441 It is OK to define an inaccessible class; for example:
14443 class A { class B; };
14444 class A::B {};
14446 We do not know if we will see a class-name, or a
14447 template-name. We look for a class-name first, in case the
14448 class-name is a template-id; if we looked for the
14449 template-name first we would stop after the template-name. */
14450 cp_parser_parse_tentatively (parser);
14451 type = cp_parser_class_name (parser,
14452 /*typename_keyword_p=*/false,
14453 /*template_keyword_p=*/false,
14454 class_type,
14455 /*check_dependency_p=*/false,
14456 /*class_head_p=*/true,
14457 /*is_declaration=*/false);
14458 /* If that didn't work, ignore the nested-name-specifier. */
14459 if (!cp_parser_parse_definitely (parser))
14461 invalid_nested_name_p = true;
14462 id = cp_parser_identifier (parser);
14463 if (id == error_mark_node)
14464 id = NULL_TREE;
14466 /* If we could not find a corresponding TYPE, treat this
14467 declaration like an unqualified declaration. */
14468 if (type == error_mark_node)
14469 nested_name_specifier = NULL_TREE;
14470 /* Otherwise, count the number of templates used in TYPE and its
14471 containing scopes. */
14472 else
14474 tree scope;
14476 for (scope = TREE_TYPE (type);
14477 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14478 scope = (TYPE_P (scope)
14479 ? TYPE_CONTEXT (scope)
14480 : DECL_CONTEXT (scope)))
14481 if (TYPE_P (scope)
14482 && CLASS_TYPE_P (scope)
14483 && CLASSTYPE_TEMPLATE_INFO (scope)
14484 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14485 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14486 ++num_templates;
14489 /* Otherwise, the identifier is optional. */
14490 else
14492 /* We don't know whether what comes next is a template-id,
14493 an identifier, or nothing at all. */
14494 cp_parser_parse_tentatively (parser);
14495 /* Check for a template-id. */
14496 id = cp_parser_template_id (parser,
14497 /*template_keyword_p=*/false,
14498 /*check_dependency_p=*/true,
14499 /*is_declaration=*/true);
14500 /* If that didn't work, it could still be an identifier. */
14501 if (!cp_parser_parse_definitely (parser))
14503 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14504 id = cp_parser_identifier (parser);
14505 else
14506 id = NULL_TREE;
14508 else
14510 template_id_p = true;
14511 ++num_templates;
14515 pop_deferring_access_checks ();
14517 if (id)
14518 cp_parser_check_for_invalid_template_id (parser, id);
14520 /* If it's not a `:' or a `{' then we can't really be looking at a
14521 class-head, since a class-head only appears as part of a
14522 class-specifier. We have to detect this situation before calling
14523 xref_tag, since that has irreversible side-effects. */
14524 if (!cp_parser_next_token_starts_class_definition_p (parser))
14526 cp_parser_error (parser, "expected %<{%> or %<:%>");
14527 return error_mark_node;
14530 /* At this point, we're going ahead with the class-specifier, even
14531 if some other problem occurs. */
14532 cp_parser_commit_to_tentative_parse (parser);
14533 /* Issue the error about the overly-qualified name now. */
14534 if (qualified_p)
14535 cp_parser_error (parser,
14536 "global qualification of class name is invalid");
14537 else if (invalid_nested_name_p)
14538 cp_parser_error (parser,
14539 "qualified name does not name a class");
14540 else if (nested_name_specifier)
14542 tree scope;
14544 /* Reject typedef-names in class heads. */
14545 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14547 error ("invalid class name in declaration of %qD", type);
14548 type = NULL_TREE;
14549 goto done;
14552 /* Figure out in what scope the declaration is being placed. */
14553 scope = current_scope ();
14554 /* If that scope does not contain the scope in which the
14555 class was originally declared, the program is invalid. */
14556 if (scope && !is_ancestor (scope, nested_name_specifier))
14558 if (at_namespace_scope_p ())
14559 error ("declaration of %qD in namespace %qD which does not "
14560 "enclose %qD", type, scope, nested_name_specifier);
14561 else
14562 error ("declaration of %qD in %qD which does not enclose %qD",
14563 type, scope, nested_name_specifier);
14564 type = NULL_TREE;
14565 goto done;
14567 /* [dcl.meaning]
14569 A declarator-id shall not be qualified exception of the
14570 definition of a ... nested class outside of its class
14571 ... [or] a the definition or explicit instantiation of a
14572 class member of a namespace outside of its namespace. */
14573 if (scope == nested_name_specifier)
14575 pedwarn ("extra qualification ignored");
14576 nested_name_specifier = NULL_TREE;
14577 num_templates = 0;
14580 /* An explicit-specialization must be preceded by "template <>". If
14581 it is not, try to recover gracefully. */
14582 if (at_namespace_scope_p ()
14583 && parser->num_template_parameter_lists == 0
14584 && template_id_p)
14586 error ("an explicit specialization must be preceded by %<template <>%>");
14587 invalid_explicit_specialization_p = true;
14588 /* Take the same action that would have been taken by
14589 cp_parser_explicit_specialization. */
14590 ++parser->num_template_parameter_lists;
14591 begin_specialization ();
14593 /* There must be no "return" statements between this point and the
14594 end of this function; set "type "to the correct return value and
14595 use "goto done;" to return. */
14596 /* Make sure that the right number of template parameters were
14597 present. */
14598 if (!cp_parser_check_template_parameters (parser, num_templates))
14600 /* If something went wrong, there is no point in even trying to
14601 process the class-definition. */
14602 type = NULL_TREE;
14603 goto done;
14606 /* Look up the type. */
14607 if (template_id_p)
14609 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
14610 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
14611 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
14613 error ("function template %qD redeclared as a class template", id);
14614 type = error_mark_node;
14616 else
14618 type = TREE_TYPE (id);
14619 type = maybe_process_partial_specialization (type);
14621 if (nested_name_specifier)
14622 pushed_scope = push_scope (nested_name_specifier);
14624 else if (nested_name_specifier)
14626 tree class_type;
14628 /* Given:
14630 template <typename T> struct S { struct T };
14631 template <typename T> struct S<T>::T { };
14633 we will get a TYPENAME_TYPE when processing the definition of
14634 `S::T'. We need to resolve it to the actual type before we
14635 try to define it. */
14636 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14638 class_type = resolve_typename_type (TREE_TYPE (type),
14639 /*only_current_p=*/false);
14640 if (TREE_CODE (class_type) != TYPENAME_TYPE)
14641 type = TYPE_NAME (class_type);
14642 else
14644 cp_parser_error (parser, "could not resolve typename type");
14645 type = error_mark_node;
14649 maybe_process_partial_specialization (TREE_TYPE (type));
14650 class_type = current_class_type;
14651 /* Enter the scope indicated by the nested-name-specifier. */
14652 pushed_scope = push_scope (nested_name_specifier);
14653 /* Get the canonical version of this type. */
14654 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14655 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14656 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14658 type = push_template_decl (type);
14659 if (type == error_mark_node)
14661 type = NULL_TREE;
14662 goto done;
14666 type = TREE_TYPE (type);
14667 *nested_name_specifier_p = true;
14669 else /* The name is not a nested name. */
14671 /* If the class was unnamed, create a dummy name. */
14672 if (!id)
14673 id = make_anon_name ();
14674 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14675 parser->num_template_parameter_lists);
14678 /* Indicate whether this class was declared as a `class' or as a
14679 `struct'. */
14680 if (TREE_CODE (type) == RECORD_TYPE)
14681 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14682 cp_parser_check_class_key (class_key, type);
14684 /* If this type was already complete, and we see another definition,
14685 that's an error. */
14686 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14688 error ("redefinition of %q#T", type);
14689 error ("previous definition of %q+#T", type);
14690 type = NULL_TREE;
14691 goto done;
14693 else if (type == error_mark_node)
14694 type = NULL_TREE;
14696 /* We will have entered the scope containing the class; the names of
14697 base classes should be looked up in that context. For example:
14699 struct A { struct B {}; struct C; };
14700 struct A::C : B {};
14702 is valid. */
14704 /* Get the list of base-classes, if there is one. */
14705 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14706 *bases = cp_parser_base_clause (parser);
14708 done:
14709 /* Leave the scope given by the nested-name-specifier. We will
14710 enter the class scope itself while processing the members. */
14711 if (pushed_scope)
14712 pop_scope (pushed_scope);
14714 if (invalid_explicit_specialization_p)
14716 end_specialization ();
14717 --parser->num_template_parameter_lists;
14719 *attributes_p = attributes;
14720 return type;
14723 /* Parse a class-key.
14725 class-key:
14726 class
14727 struct
14728 union
14730 Returns the kind of class-key specified, or none_type to indicate
14731 error. */
14733 static enum tag_types
14734 cp_parser_class_key (cp_parser* parser)
14736 cp_token *token;
14737 enum tag_types tag_type;
14739 /* Look for the class-key. */
14740 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14741 if (!token)
14742 return none_type;
14744 /* Check to see if the TOKEN is a class-key. */
14745 tag_type = cp_parser_token_is_class_key (token);
14746 if (!tag_type)
14747 cp_parser_error (parser, "expected class-key");
14748 return tag_type;
14751 /* Parse an (optional) member-specification.
14753 member-specification:
14754 member-declaration member-specification [opt]
14755 access-specifier : member-specification [opt] */
14757 static void
14758 cp_parser_member_specification_opt (cp_parser* parser)
14760 while (true)
14762 cp_token *token;
14763 enum rid keyword;
14765 /* Peek at the next token. */
14766 token = cp_lexer_peek_token (parser->lexer);
14767 /* If it's a `}', or EOF then we've seen all the members. */
14768 if (token->type == CPP_CLOSE_BRACE
14769 || token->type == CPP_EOF
14770 || token->type == CPP_PRAGMA_EOL)
14771 break;
14773 /* See if this token is a keyword. */
14774 keyword = token->keyword;
14775 switch (keyword)
14777 case RID_PUBLIC:
14778 case RID_PROTECTED:
14779 case RID_PRIVATE:
14780 /* Consume the access-specifier. */
14781 cp_lexer_consume_token (parser->lexer);
14782 /* Remember which access-specifier is active. */
14783 current_access_specifier = token->u.value;
14784 /* Look for the `:'. */
14785 cp_parser_require (parser, CPP_COLON, "`:'");
14786 break;
14788 default:
14789 /* Accept #pragmas at class scope. */
14790 if (token->type == CPP_PRAGMA)
14792 cp_parser_pragma (parser, pragma_external);
14793 break;
14796 /* Otherwise, the next construction must be a
14797 member-declaration. */
14798 cp_parser_member_declaration (parser);
14803 /* Parse a member-declaration.
14805 member-declaration:
14806 decl-specifier-seq [opt] member-declarator-list [opt] ;
14807 function-definition ; [opt]
14808 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14809 using-declaration
14810 template-declaration
14812 member-declarator-list:
14813 member-declarator
14814 member-declarator-list , member-declarator
14816 member-declarator:
14817 declarator pure-specifier [opt]
14818 declarator constant-initializer [opt]
14819 identifier [opt] : constant-expression
14821 GNU Extensions:
14823 member-declaration:
14824 __extension__ member-declaration
14826 member-declarator:
14827 declarator attributes [opt] pure-specifier [opt]
14828 declarator attributes [opt] constant-initializer [opt]
14829 identifier [opt] attributes [opt] : constant-expression
14831 C++0x Extensions:
14833 member-declaration:
14834 static_assert-declaration */
14836 static void
14837 cp_parser_member_declaration (cp_parser* parser)
14839 cp_decl_specifier_seq decl_specifiers;
14840 tree prefix_attributes;
14841 tree decl;
14842 int declares_class_or_enum;
14843 bool friend_p;
14844 cp_token *token;
14845 int saved_pedantic;
14847 /* Check for the `__extension__' keyword. */
14848 if (cp_parser_extension_opt (parser, &saved_pedantic))
14850 /* Recurse. */
14851 cp_parser_member_declaration (parser);
14852 /* Restore the old value of the PEDANTIC flag. */
14853 pedantic = saved_pedantic;
14855 return;
14858 /* Check for a template-declaration. */
14859 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14861 /* An explicit specialization here is an error condition, and we
14862 expect the specialization handler to detect and report this. */
14863 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14864 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14865 cp_parser_explicit_specialization (parser);
14866 else
14867 cp_parser_template_declaration (parser, /*member_p=*/true);
14869 return;
14872 /* Check for a using-declaration. */
14873 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14875 /* Parse the using-declaration. */
14876 cp_parser_using_declaration (parser,
14877 /*access_declaration_p=*/false);
14878 return;
14881 /* Check for @defs. */
14882 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14884 tree ivar, member;
14885 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14886 ivar = ivar_chains;
14887 while (ivar)
14889 member = ivar;
14890 ivar = TREE_CHAIN (member);
14891 TREE_CHAIN (member) = NULL_TREE;
14892 finish_member_declaration (member);
14894 return;
14897 /* If the next token is `static_assert' we have a static assertion. */
14898 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14900 cp_parser_static_assert (parser, /*member_p=*/true);
14901 return;
14904 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14905 return;
14907 /* Parse the decl-specifier-seq. */
14908 cp_parser_decl_specifier_seq (parser,
14909 CP_PARSER_FLAGS_OPTIONAL,
14910 &decl_specifiers,
14911 &declares_class_or_enum);
14912 prefix_attributes = decl_specifiers.attributes;
14913 decl_specifiers.attributes = NULL_TREE;
14914 /* Check for an invalid type-name. */
14915 if (!decl_specifiers.type
14916 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14917 return;
14918 /* If there is no declarator, then the decl-specifier-seq should
14919 specify a type. */
14920 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14922 /* If there was no decl-specifier-seq, and the next token is a
14923 `;', then we have something like:
14925 struct S { ; };
14927 [class.mem]
14929 Each member-declaration shall declare at least one member
14930 name of the class. */
14931 if (!decl_specifiers.any_specifiers_p)
14933 cp_token *token = cp_lexer_peek_token (parser->lexer);
14934 if (pedantic && !token->in_system_header)
14935 pedwarn ("%Hextra %<;%>", &token->location);
14937 else
14939 tree type;
14941 /* See if this declaration is a friend. */
14942 friend_p = cp_parser_friend_p (&decl_specifiers);
14943 /* If there were decl-specifiers, check to see if there was
14944 a class-declaration. */
14945 type = check_tag_decl (&decl_specifiers);
14946 /* Nested classes have already been added to the class, but
14947 a `friend' needs to be explicitly registered. */
14948 if (friend_p)
14950 /* If the `friend' keyword was present, the friend must
14951 be introduced with a class-key. */
14952 if (!declares_class_or_enum)
14953 error ("a class-key must be used when declaring a friend");
14954 /* In this case:
14956 template <typename T> struct A {
14957 friend struct A<T>::B;
14960 A<T>::B will be represented by a TYPENAME_TYPE, and
14961 therefore not recognized by check_tag_decl. */
14962 if (!type
14963 && decl_specifiers.type
14964 && TYPE_P (decl_specifiers.type))
14965 type = decl_specifiers.type;
14966 if (!type || !TYPE_P (type))
14967 error ("friend declaration does not name a class or "
14968 "function");
14969 else
14970 make_friend_class (current_class_type, type,
14971 /*complain=*/true);
14973 /* If there is no TYPE, an error message will already have
14974 been issued. */
14975 else if (!type || type == error_mark_node)
14977 /* An anonymous aggregate has to be handled specially; such
14978 a declaration really declares a data member (with a
14979 particular type), as opposed to a nested class. */
14980 else if (ANON_AGGR_TYPE_P (type))
14982 /* Remove constructors and such from TYPE, now that we
14983 know it is an anonymous aggregate. */
14984 fixup_anonymous_aggr (type);
14985 /* And make the corresponding data member. */
14986 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14987 /* Add it to the class. */
14988 finish_member_declaration (decl);
14990 else
14991 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14994 else
14996 /* See if these declarations will be friends. */
14997 friend_p = cp_parser_friend_p (&decl_specifiers);
14999 /* Keep going until we hit the `;' at the end of the
15000 declaration. */
15001 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15003 tree attributes = NULL_TREE;
15004 tree first_attribute;
15006 /* Peek at the next token. */
15007 token = cp_lexer_peek_token (parser->lexer);
15009 /* Check for a bitfield declaration. */
15010 if (token->type == CPP_COLON
15011 || (token->type == CPP_NAME
15012 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15013 == CPP_COLON))
15015 tree identifier;
15016 tree width;
15018 /* Get the name of the bitfield. Note that we cannot just
15019 check TOKEN here because it may have been invalidated by
15020 the call to cp_lexer_peek_nth_token above. */
15021 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15022 identifier = cp_parser_identifier (parser);
15023 else
15024 identifier = NULL_TREE;
15026 /* Consume the `:' token. */
15027 cp_lexer_consume_token (parser->lexer);
15028 /* Get the width of the bitfield. */
15029 width
15030 = cp_parser_constant_expression (parser,
15031 /*allow_non_constant=*/false,
15032 NULL);
15034 /* Look for attributes that apply to the bitfield. */
15035 attributes = cp_parser_attributes_opt (parser);
15036 /* Remember which attributes are prefix attributes and
15037 which are not. */
15038 first_attribute = attributes;
15039 /* Combine the attributes. */
15040 attributes = chainon (prefix_attributes, attributes);
15042 /* Create the bitfield declaration. */
15043 decl = grokbitfield (identifier
15044 ? make_id_declarator (NULL_TREE,
15045 identifier,
15046 sfk_none)
15047 : NULL,
15048 &decl_specifiers,
15049 width);
15050 /* Apply the attributes. */
15051 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
15053 else
15055 cp_declarator *declarator;
15056 tree initializer;
15057 tree asm_specification;
15058 int ctor_dtor_or_conv_p;
15060 /* Parse the declarator. */
15061 declarator
15062 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15063 &ctor_dtor_or_conv_p,
15064 /*parenthesized_p=*/NULL,
15065 /*member_p=*/true);
15067 /* If something went wrong parsing the declarator, make sure
15068 that we at least consume some tokens. */
15069 if (declarator == cp_error_declarator)
15071 /* Skip to the end of the statement. */
15072 cp_parser_skip_to_end_of_statement (parser);
15073 /* If the next token is not a semicolon, that is
15074 probably because we just skipped over the body of
15075 a function. So, we consume a semicolon if
15076 present, but do not issue an error message if it
15077 is not present. */
15078 if (cp_lexer_next_token_is (parser->lexer,
15079 CPP_SEMICOLON))
15080 cp_lexer_consume_token (parser->lexer);
15081 return;
15084 if (declares_class_or_enum & 2)
15085 cp_parser_check_for_definition_in_return_type
15086 (declarator, decl_specifiers.type);
15088 /* Look for an asm-specification. */
15089 asm_specification = cp_parser_asm_specification_opt (parser);
15090 /* Look for attributes that apply to the declaration. */
15091 attributes = cp_parser_attributes_opt (parser);
15092 /* Remember which attributes are prefix attributes and
15093 which are not. */
15094 first_attribute = attributes;
15095 /* Combine the attributes. */
15096 attributes = chainon (prefix_attributes, attributes);
15098 /* If it's an `=', then we have a constant-initializer or a
15099 pure-specifier. It is not correct to parse the
15100 initializer before registering the member declaration
15101 since the member declaration should be in scope while
15102 its initializer is processed. However, the rest of the
15103 front end does not yet provide an interface that allows
15104 us to handle this correctly. */
15105 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15107 /* In [class.mem]:
15109 A pure-specifier shall be used only in the declaration of
15110 a virtual function.
15112 A member-declarator can contain a constant-initializer
15113 only if it declares a static member of integral or
15114 enumeration type.
15116 Therefore, if the DECLARATOR is for a function, we look
15117 for a pure-specifier; otherwise, we look for a
15118 constant-initializer. When we call `grokfield', it will
15119 perform more stringent semantics checks. */
15120 if (function_declarator_p (declarator))
15121 initializer = cp_parser_pure_specifier (parser);
15122 else
15123 /* Parse the initializer. */
15124 initializer = cp_parser_constant_initializer (parser);
15126 /* Otherwise, there is no initializer. */
15127 else
15128 initializer = NULL_TREE;
15130 /* See if we are probably looking at a function
15131 definition. We are certainly not looking at a
15132 member-declarator. Calling `grokfield' has
15133 side-effects, so we must not do it unless we are sure
15134 that we are looking at a member-declarator. */
15135 if (cp_parser_token_starts_function_definition_p
15136 (cp_lexer_peek_token (parser->lexer)))
15138 /* The grammar does not allow a pure-specifier to be
15139 used when a member function is defined. (It is
15140 possible that this fact is an oversight in the
15141 standard, since a pure function may be defined
15142 outside of the class-specifier. */
15143 if (initializer)
15144 error ("pure-specifier on function-definition");
15145 decl = cp_parser_save_member_function_body (parser,
15146 &decl_specifiers,
15147 declarator,
15148 attributes);
15149 /* If the member was not a friend, declare it here. */
15150 if (!friend_p)
15151 finish_member_declaration (decl);
15152 /* Peek at the next token. */
15153 token = cp_lexer_peek_token (parser->lexer);
15154 /* If the next token is a semicolon, consume it. */
15155 if (token->type == CPP_SEMICOLON)
15156 cp_lexer_consume_token (parser->lexer);
15157 return;
15159 else
15160 /* Create the declaration. */
15161 decl = grokfield (declarator, &decl_specifiers,
15162 initializer, /*init_const_expr_p=*/true,
15163 asm_specification,
15164 attributes);
15167 /* Reset PREFIX_ATTRIBUTES. */
15168 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15169 attributes = TREE_CHAIN (attributes);
15170 if (attributes)
15171 TREE_CHAIN (attributes) = NULL_TREE;
15173 /* If there is any qualification still in effect, clear it
15174 now; we will be starting fresh with the next declarator. */
15175 parser->scope = NULL_TREE;
15176 parser->qualifying_scope = NULL_TREE;
15177 parser->object_scope = NULL_TREE;
15178 /* If it's a `,', then there are more declarators. */
15179 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15180 cp_lexer_consume_token (parser->lexer);
15181 /* If the next token isn't a `;', then we have a parse error. */
15182 else if (cp_lexer_next_token_is_not (parser->lexer,
15183 CPP_SEMICOLON))
15185 cp_parser_error (parser, "expected %<;%>");
15186 /* Skip tokens until we find a `;'. */
15187 cp_parser_skip_to_end_of_statement (parser);
15189 break;
15192 if (decl)
15194 /* Add DECL to the list of members. */
15195 if (!friend_p)
15196 finish_member_declaration (decl);
15198 if (TREE_CODE (decl) == FUNCTION_DECL)
15199 cp_parser_save_default_args (parser, decl);
15204 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15207 /* Parse a pure-specifier.
15209 pure-specifier:
15212 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15213 Otherwise, ERROR_MARK_NODE is returned. */
15215 static tree
15216 cp_parser_pure_specifier (cp_parser* parser)
15218 cp_token *token;
15220 /* Look for the `=' token. */
15221 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15222 return error_mark_node;
15223 /* Look for the `0' token. */
15224 token = cp_lexer_consume_token (parser->lexer);
15225 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15226 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15228 cp_parser_error (parser,
15229 "invalid pure specifier (only `= 0' is allowed)");
15230 cp_parser_skip_to_end_of_statement (parser);
15231 return error_mark_node;
15233 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15235 error ("templates may not be %<virtual%>");
15236 return error_mark_node;
15239 return integer_zero_node;
15242 /* Parse a constant-initializer.
15244 constant-initializer:
15245 = constant-expression
15247 Returns a representation of the constant-expression. */
15249 static tree
15250 cp_parser_constant_initializer (cp_parser* parser)
15252 /* Look for the `=' token. */
15253 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15254 return error_mark_node;
15256 /* It is invalid to write:
15258 struct S { static const int i = { 7 }; };
15261 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15263 cp_parser_error (parser,
15264 "a brace-enclosed initializer is not allowed here");
15265 /* Consume the opening brace. */
15266 cp_lexer_consume_token (parser->lexer);
15267 /* Skip the initializer. */
15268 cp_parser_skip_to_closing_brace (parser);
15269 /* Look for the trailing `}'. */
15270 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15272 return error_mark_node;
15275 return cp_parser_constant_expression (parser,
15276 /*allow_non_constant=*/false,
15277 NULL);
15280 /* Derived classes [gram.class.derived] */
15282 /* Parse a base-clause.
15284 base-clause:
15285 : base-specifier-list
15287 base-specifier-list:
15288 base-specifier ... [opt]
15289 base-specifier-list , base-specifier ... [opt]
15291 Returns a TREE_LIST representing the base-classes, in the order in
15292 which they were declared. The representation of each node is as
15293 described by cp_parser_base_specifier.
15295 In the case that no bases are specified, this function will return
15296 NULL_TREE, not ERROR_MARK_NODE. */
15298 static tree
15299 cp_parser_base_clause (cp_parser* parser)
15301 tree bases = NULL_TREE;
15303 /* Look for the `:' that begins the list. */
15304 cp_parser_require (parser, CPP_COLON, "`:'");
15306 /* Scan the base-specifier-list. */
15307 while (true)
15309 cp_token *token;
15310 tree base;
15311 bool pack_expansion_p = false;
15313 /* Look for the base-specifier. */
15314 base = cp_parser_base_specifier (parser);
15315 /* Look for the (optional) ellipsis. */
15316 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15318 /* Consume the `...'. */
15319 cp_lexer_consume_token (parser->lexer);
15321 pack_expansion_p = true;
15324 /* Add BASE to the front of the list. */
15325 if (base != error_mark_node)
15327 if (pack_expansion_p)
15328 /* Make this a pack expansion type. */
15329 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15332 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15334 TREE_CHAIN (base) = bases;
15335 bases = base;
15338 /* Peek at the next token. */
15339 token = cp_lexer_peek_token (parser->lexer);
15340 /* If it's not a comma, then the list is complete. */
15341 if (token->type != CPP_COMMA)
15342 break;
15343 /* Consume the `,'. */
15344 cp_lexer_consume_token (parser->lexer);
15347 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15348 base class had a qualified name. However, the next name that
15349 appears is certainly not qualified. */
15350 parser->scope = NULL_TREE;
15351 parser->qualifying_scope = NULL_TREE;
15352 parser->object_scope = NULL_TREE;
15354 return nreverse (bases);
15357 /* Parse a base-specifier.
15359 base-specifier:
15360 :: [opt] nested-name-specifier [opt] class-name
15361 virtual access-specifier [opt] :: [opt] nested-name-specifier
15362 [opt] class-name
15363 access-specifier virtual [opt] :: [opt] nested-name-specifier
15364 [opt] class-name
15366 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15367 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15368 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15369 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15371 static tree
15372 cp_parser_base_specifier (cp_parser* parser)
15374 cp_token *token;
15375 bool done = false;
15376 bool virtual_p = false;
15377 bool duplicate_virtual_error_issued_p = false;
15378 bool duplicate_access_error_issued_p = false;
15379 bool class_scope_p, template_p;
15380 tree access = access_default_node;
15381 tree type;
15383 /* Process the optional `virtual' and `access-specifier'. */
15384 while (!done)
15386 /* Peek at the next token. */
15387 token = cp_lexer_peek_token (parser->lexer);
15388 /* Process `virtual'. */
15389 switch (token->keyword)
15391 case RID_VIRTUAL:
15392 /* If `virtual' appears more than once, issue an error. */
15393 if (virtual_p && !duplicate_virtual_error_issued_p)
15395 cp_parser_error (parser,
15396 "%<virtual%> specified more than once in base-specified");
15397 duplicate_virtual_error_issued_p = true;
15400 virtual_p = true;
15402 /* Consume the `virtual' token. */
15403 cp_lexer_consume_token (parser->lexer);
15405 break;
15407 case RID_PUBLIC:
15408 case RID_PROTECTED:
15409 case RID_PRIVATE:
15410 /* If more than one access specifier appears, issue an
15411 error. */
15412 if (access != access_default_node
15413 && !duplicate_access_error_issued_p)
15415 cp_parser_error (parser,
15416 "more than one access specifier in base-specified");
15417 duplicate_access_error_issued_p = true;
15420 access = ridpointers[(int) token->keyword];
15422 /* Consume the access-specifier. */
15423 cp_lexer_consume_token (parser->lexer);
15425 break;
15427 default:
15428 done = true;
15429 break;
15432 /* It is not uncommon to see programs mechanically, erroneously, use
15433 the 'typename' keyword to denote (dependent) qualified types
15434 as base classes. */
15435 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15437 if (!processing_template_decl)
15438 error ("keyword %<typename%> not allowed outside of templates");
15439 else
15440 error ("keyword %<typename%> not allowed in this context "
15441 "(the base class is implicitly a type)");
15442 cp_lexer_consume_token (parser->lexer);
15445 /* Look for the optional `::' operator. */
15446 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15447 /* Look for the nested-name-specifier. The simplest way to
15448 implement:
15450 [temp.res]
15452 The keyword `typename' is not permitted in a base-specifier or
15453 mem-initializer; in these contexts a qualified name that
15454 depends on a template-parameter is implicitly assumed to be a
15455 type name.
15457 is to pretend that we have seen the `typename' keyword at this
15458 point. */
15459 cp_parser_nested_name_specifier_opt (parser,
15460 /*typename_keyword_p=*/true,
15461 /*check_dependency_p=*/true,
15462 typename_type,
15463 /*is_declaration=*/true);
15464 /* If the base class is given by a qualified name, assume that names
15465 we see are type names or templates, as appropriate. */
15466 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15467 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15469 /* Finally, look for the class-name. */
15470 type = cp_parser_class_name (parser,
15471 class_scope_p,
15472 template_p,
15473 typename_type,
15474 /*check_dependency_p=*/true,
15475 /*class_head_p=*/false,
15476 /*is_declaration=*/true);
15478 if (type == error_mark_node)
15479 return error_mark_node;
15481 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15484 /* Exception handling [gram.exception] */
15486 /* Parse an (optional) exception-specification.
15488 exception-specification:
15489 throw ( type-id-list [opt] )
15491 Returns a TREE_LIST representing the exception-specification. The
15492 TREE_VALUE of each node is a type. */
15494 static tree
15495 cp_parser_exception_specification_opt (cp_parser* parser)
15497 cp_token *token;
15498 tree type_id_list;
15500 /* Peek at the next token. */
15501 token = cp_lexer_peek_token (parser->lexer);
15502 /* If it's not `throw', then there's no exception-specification. */
15503 if (!cp_parser_is_keyword (token, RID_THROW))
15504 return NULL_TREE;
15506 /* Consume the `throw'. */
15507 cp_lexer_consume_token (parser->lexer);
15509 /* Look for the `('. */
15510 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15512 /* Peek at the next token. */
15513 token = cp_lexer_peek_token (parser->lexer);
15514 /* If it's not a `)', then there is a type-id-list. */
15515 if (token->type != CPP_CLOSE_PAREN)
15517 const char *saved_message;
15519 /* Types may not be defined in an exception-specification. */
15520 saved_message = parser->type_definition_forbidden_message;
15521 parser->type_definition_forbidden_message
15522 = "types may not be defined in an exception-specification";
15523 /* Parse the type-id-list. */
15524 type_id_list = cp_parser_type_id_list (parser);
15525 /* Restore the saved message. */
15526 parser->type_definition_forbidden_message = saved_message;
15528 else
15529 type_id_list = empty_except_spec;
15531 /* Look for the `)'. */
15532 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15534 return type_id_list;
15537 /* Parse an (optional) type-id-list.
15539 type-id-list:
15540 type-id ... [opt]
15541 type-id-list , type-id ... [opt]
15543 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15544 in the order that the types were presented. */
15546 static tree
15547 cp_parser_type_id_list (cp_parser* parser)
15549 tree types = NULL_TREE;
15551 while (true)
15553 cp_token *token;
15554 tree type;
15556 /* Get the next type-id. */
15557 type = cp_parser_type_id (parser);
15558 /* Parse the optional ellipsis. */
15559 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15561 /* Consume the `...'. */
15562 cp_lexer_consume_token (parser->lexer);
15564 /* Turn the type into a pack expansion expression. */
15565 type = make_pack_expansion (type);
15567 /* Add it to the list. */
15568 types = add_exception_specifier (types, type, /*complain=*/1);
15569 /* Peek at the next token. */
15570 token = cp_lexer_peek_token (parser->lexer);
15571 /* If it is not a `,', we are done. */
15572 if (token->type != CPP_COMMA)
15573 break;
15574 /* Consume the `,'. */
15575 cp_lexer_consume_token (parser->lexer);
15578 return nreverse (types);
15581 /* Parse a try-block.
15583 try-block:
15584 try compound-statement handler-seq */
15586 static tree
15587 cp_parser_try_block (cp_parser* parser)
15589 tree try_block;
15591 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15592 try_block = begin_try_block ();
15593 cp_parser_compound_statement (parser, NULL, true);
15594 finish_try_block (try_block);
15595 cp_parser_handler_seq (parser);
15596 finish_handler_sequence (try_block);
15598 return try_block;
15601 /* Parse a function-try-block.
15603 function-try-block:
15604 try ctor-initializer [opt] function-body handler-seq */
15606 static bool
15607 cp_parser_function_try_block (cp_parser* parser)
15609 tree compound_stmt;
15610 tree try_block;
15611 bool ctor_initializer_p;
15613 /* Look for the `try' keyword. */
15614 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15615 return false;
15616 /* Let the rest of the front end know where we are. */
15617 try_block = begin_function_try_block (&compound_stmt);
15618 /* Parse the function-body. */
15619 ctor_initializer_p
15620 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15621 /* We're done with the `try' part. */
15622 finish_function_try_block (try_block);
15623 /* Parse the handlers. */
15624 cp_parser_handler_seq (parser);
15625 /* We're done with the handlers. */
15626 finish_function_handler_sequence (try_block, compound_stmt);
15628 return ctor_initializer_p;
15631 /* Parse a handler-seq.
15633 handler-seq:
15634 handler handler-seq [opt] */
15636 static void
15637 cp_parser_handler_seq (cp_parser* parser)
15639 while (true)
15641 cp_token *token;
15643 /* Parse the handler. */
15644 cp_parser_handler (parser);
15645 /* Peek at the next token. */
15646 token = cp_lexer_peek_token (parser->lexer);
15647 /* If it's not `catch' then there are no more handlers. */
15648 if (!cp_parser_is_keyword (token, RID_CATCH))
15649 break;
15653 /* Parse a handler.
15655 handler:
15656 catch ( exception-declaration ) compound-statement */
15658 static void
15659 cp_parser_handler (cp_parser* parser)
15661 tree handler;
15662 tree declaration;
15664 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15665 handler = begin_handler ();
15666 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15667 declaration = cp_parser_exception_declaration (parser);
15668 finish_handler_parms (declaration, handler);
15669 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15670 cp_parser_compound_statement (parser, NULL, false);
15671 finish_handler (handler);
15674 /* Parse an exception-declaration.
15676 exception-declaration:
15677 type-specifier-seq declarator
15678 type-specifier-seq abstract-declarator
15679 type-specifier-seq
15682 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15683 ellipsis variant is used. */
15685 static tree
15686 cp_parser_exception_declaration (cp_parser* parser)
15688 cp_decl_specifier_seq type_specifiers;
15689 cp_declarator *declarator;
15690 const char *saved_message;
15692 /* If it's an ellipsis, it's easy to handle. */
15693 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15695 /* Consume the `...' token. */
15696 cp_lexer_consume_token (parser->lexer);
15697 return NULL_TREE;
15700 /* Types may not be defined in exception-declarations. */
15701 saved_message = parser->type_definition_forbidden_message;
15702 parser->type_definition_forbidden_message
15703 = "types may not be defined in exception-declarations";
15705 /* Parse the type-specifier-seq. */
15706 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15707 &type_specifiers);
15708 /* If it's a `)', then there is no declarator. */
15709 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15710 declarator = NULL;
15711 else
15712 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15713 /*ctor_dtor_or_conv_p=*/NULL,
15714 /*parenthesized_p=*/NULL,
15715 /*member_p=*/false);
15717 /* Restore the saved message. */
15718 parser->type_definition_forbidden_message = saved_message;
15720 if (!type_specifiers.any_specifiers_p)
15721 return error_mark_node;
15723 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15726 /* Parse a throw-expression.
15728 throw-expression:
15729 throw assignment-expression [opt]
15731 Returns a THROW_EXPR representing the throw-expression. */
15733 static tree
15734 cp_parser_throw_expression (cp_parser* parser)
15736 tree expression;
15737 cp_token* token;
15739 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15740 token = cp_lexer_peek_token (parser->lexer);
15741 /* Figure out whether or not there is an assignment-expression
15742 following the "throw" keyword. */
15743 if (token->type == CPP_COMMA
15744 || token->type == CPP_SEMICOLON
15745 || token->type == CPP_CLOSE_PAREN
15746 || token->type == CPP_CLOSE_SQUARE
15747 || token->type == CPP_CLOSE_BRACE
15748 || token->type == CPP_COLON)
15749 expression = NULL_TREE;
15750 else
15751 expression = cp_parser_assignment_expression (parser,
15752 /*cast_p=*/false);
15754 return build_throw (expression);
15757 /* GNU Extensions */
15759 /* Parse an (optional) asm-specification.
15761 asm-specification:
15762 asm ( string-literal )
15764 If the asm-specification is present, returns a STRING_CST
15765 corresponding to the string-literal. Otherwise, returns
15766 NULL_TREE. */
15768 static tree
15769 cp_parser_asm_specification_opt (cp_parser* parser)
15771 cp_token *token;
15772 tree asm_specification;
15774 /* Peek at the next token. */
15775 token = cp_lexer_peek_token (parser->lexer);
15776 /* If the next token isn't the `asm' keyword, then there's no
15777 asm-specification. */
15778 if (!cp_parser_is_keyword (token, RID_ASM))
15779 return NULL_TREE;
15781 /* Consume the `asm' token. */
15782 cp_lexer_consume_token (parser->lexer);
15783 /* Look for the `('. */
15784 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15786 /* Look for the string-literal. */
15787 asm_specification = cp_parser_string_literal (parser, false, false);
15789 /* Look for the `)'. */
15790 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15792 return asm_specification;
15795 /* Parse an asm-operand-list.
15797 asm-operand-list:
15798 asm-operand
15799 asm-operand-list , asm-operand
15801 asm-operand:
15802 string-literal ( expression )
15803 [ string-literal ] string-literal ( expression )
15805 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15806 each node is the expression. The TREE_PURPOSE is itself a
15807 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15808 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15809 is a STRING_CST for the string literal before the parenthesis. Returns
15810 ERROR_MARK_NODE if any of the operands are invalid. */
15812 static tree
15813 cp_parser_asm_operand_list (cp_parser* parser)
15815 tree asm_operands = NULL_TREE;
15816 bool invalid_operands = false;
15818 while (true)
15820 tree string_literal;
15821 tree expression;
15822 tree name;
15824 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15826 /* Consume the `[' token. */
15827 cp_lexer_consume_token (parser->lexer);
15828 /* Read the operand name. */
15829 name = cp_parser_identifier (parser);
15830 if (name != error_mark_node)
15831 name = build_string (IDENTIFIER_LENGTH (name),
15832 IDENTIFIER_POINTER (name));
15833 /* Look for the closing `]'. */
15834 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15836 else
15837 name = NULL_TREE;
15838 /* Look for the string-literal. */
15839 string_literal = cp_parser_string_literal (parser, false, false);
15841 /* Look for the `('. */
15842 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15843 /* Parse the expression. */
15844 expression = cp_parser_expression (parser, /*cast_p=*/false);
15845 /* Look for the `)'. */
15846 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15848 if (name == error_mark_node
15849 || string_literal == error_mark_node
15850 || expression == error_mark_node)
15851 invalid_operands = true;
15853 /* Add this operand to the list. */
15854 asm_operands = tree_cons (build_tree_list (name, string_literal),
15855 expression,
15856 asm_operands);
15857 /* If the next token is not a `,', there are no more
15858 operands. */
15859 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15860 break;
15861 /* Consume the `,'. */
15862 cp_lexer_consume_token (parser->lexer);
15865 return invalid_operands ? error_mark_node : nreverse (asm_operands);
15868 /* Parse an asm-clobber-list.
15870 asm-clobber-list:
15871 string-literal
15872 asm-clobber-list , string-literal
15874 Returns a TREE_LIST, indicating the clobbers in the order that they
15875 appeared. The TREE_VALUE of each node is a STRING_CST. */
15877 static tree
15878 cp_parser_asm_clobber_list (cp_parser* parser)
15880 tree clobbers = NULL_TREE;
15882 while (true)
15884 tree string_literal;
15886 /* Look for the string literal. */
15887 string_literal = cp_parser_string_literal (parser, false, false);
15888 /* Add it to the list. */
15889 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15890 /* If the next token is not a `,', then the list is
15891 complete. */
15892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15893 break;
15894 /* Consume the `,' token. */
15895 cp_lexer_consume_token (parser->lexer);
15898 return clobbers;
15901 /* Parse an (optional) series of attributes.
15903 attributes:
15904 attributes attribute
15906 attribute:
15907 __attribute__ (( attribute-list [opt] ))
15909 The return value is as for cp_parser_attribute_list. */
15911 static tree
15912 cp_parser_attributes_opt (cp_parser* parser)
15914 tree attributes = NULL_TREE;
15916 while (true)
15918 cp_token *token;
15919 tree attribute_list;
15921 /* Peek at the next token. */
15922 token = cp_lexer_peek_token (parser->lexer);
15923 /* If it's not `__attribute__', then we're done. */
15924 if (token->keyword != RID_ATTRIBUTE)
15925 break;
15927 /* Consume the `__attribute__' keyword. */
15928 cp_lexer_consume_token (parser->lexer);
15929 /* Look for the two `(' tokens. */
15930 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15931 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15933 /* Peek at the next token. */
15934 token = cp_lexer_peek_token (parser->lexer);
15935 if (token->type != CPP_CLOSE_PAREN)
15936 /* Parse the attribute-list. */
15937 attribute_list = cp_parser_attribute_list (parser);
15938 else
15939 /* If the next token is a `)', then there is no attribute
15940 list. */
15941 attribute_list = NULL;
15943 /* Look for the two `)' tokens. */
15944 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15945 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15947 /* Add these new attributes to the list. */
15948 attributes = chainon (attributes, attribute_list);
15951 return attributes;
15954 /* Parse an attribute-list.
15956 attribute-list:
15957 attribute
15958 attribute-list , attribute
15960 attribute:
15961 identifier
15962 identifier ( identifier )
15963 identifier ( identifier , expression-list )
15964 identifier ( expression-list )
15966 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15967 to an attribute. The TREE_PURPOSE of each node is the identifier
15968 indicating which attribute is in use. The TREE_VALUE represents
15969 the arguments, if any. */
15971 static tree
15972 cp_parser_attribute_list (cp_parser* parser)
15974 tree attribute_list = NULL_TREE;
15975 bool save_translate_strings_p = parser->translate_strings_p;
15977 parser->translate_strings_p = false;
15978 while (true)
15980 cp_token *token;
15981 tree identifier;
15982 tree attribute;
15984 /* Look for the identifier. We also allow keywords here; for
15985 example `__attribute__ ((const))' is legal. */
15986 token = cp_lexer_peek_token (parser->lexer);
15987 if (token->type == CPP_NAME
15988 || token->type == CPP_KEYWORD)
15990 tree arguments = NULL_TREE;
15992 /* Consume the token. */
15993 token = cp_lexer_consume_token (parser->lexer);
15995 /* Save away the identifier that indicates which attribute
15996 this is. */
15997 identifier = token->u.value;
15998 attribute = build_tree_list (identifier, NULL_TREE);
16000 /* Peek at the next token. */
16001 token = cp_lexer_peek_token (parser->lexer);
16002 /* If it's an `(', then parse the attribute arguments. */
16003 if (token->type == CPP_OPEN_PAREN)
16005 arguments = cp_parser_parenthesized_expression_list
16006 (parser, true, /*cast_p=*/false,
16007 /*allow_expansion_p=*/false,
16008 /*non_constant_p=*/NULL);
16009 /* Save the arguments away. */
16010 TREE_VALUE (attribute) = arguments;
16013 if (arguments != error_mark_node)
16015 /* Add this attribute to the list. */
16016 TREE_CHAIN (attribute) = attribute_list;
16017 attribute_list = attribute;
16020 token = cp_lexer_peek_token (parser->lexer);
16022 /* Now, look for more attributes. If the next token isn't a
16023 `,', we're done. */
16024 if (token->type != CPP_COMMA)
16025 break;
16027 /* Consume the comma and keep going. */
16028 cp_lexer_consume_token (parser->lexer);
16030 parser->translate_strings_p = save_translate_strings_p;
16032 /* We built up the list in reverse order. */
16033 return nreverse (attribute_list);
16036 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16037 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16038 current value of the PEDANTIC flag, regardless of whether or not
16039 the `__extension__' keyword is present. The caller is responsible
16040 for restoring the value of the PEDANTIC flag. */
16042 static bool
16043 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16045 /* Save the old value of the PEDANTIC flag. */
16046 *saved_pedantic = pedantic;
16048 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16050 /* Consume the `__extension__' token. */
16051 cp_lexer_consume_token (parser->lexer);
16052 /* We're not being pedantic while the `__extension__' keyword is
16053 in effect. */
16054 pedantic = 0;
16056 return true;
16059 return false;
16062 /* Parse a label declaration.
16064 label-declaration:
16065 __label__ label-declarator-seq ;
16067 label-declarator-seq:
16068 identifier , label-declarator-seq
16069 identifier */
16071 static void
16072 cp_parser_label_declaration (cp_parser* parser)
16074 /* Look for the `__label__' keyword. */
16075 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
16077 while (true)
16079 tree identifier;
16081 /* Look for an identifier. */
16082 identifier = cp_parser_identifier (parser);
16083 /* If we failed, stop. */
16084 if (identifier == error_mark_node)
16085 break;
16086 /* Declare it as a label. */
16087 finish_label_decl (identifier);
16088 /* If the next token is a `;', stop. */
16089 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16090 break;
16091 /* Look for the `,' separating the label declarations. */
16092 cp_parser_require (parser, CPP_COMMA, "`,'");
16095 /* Look for the final `;'. */
16096 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
16099 /* Support Functions */
16101 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16102 NAME should have one of the representations used for an
16103 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16104 is returned. If PARSER->SCOPE is a dependent type, then a
16105 SCOPE_REF is returned.
16107 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16108 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16109 was formed. Abstractly, such entities should not be passed to this
16110 function, because they do not need to be looked up, but it is
16111 simpler to check for this special case here, rather than at the
16112 call-sites.
16114 In cases not explicitly covered above, this function returns a
16115 DECL, OVERLOAD, or baselink representing the result of the lookup.
16116 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16117 is returned.
16119 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16120 (e.g., "struct") that was used. In that case bindings that do not
16121 refer to types are ignored.
16123 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16124 ignored.
16126 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16127 are ignored.
16129 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16130 types.
16132 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16133 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16134 NULL_TREE otherwise. */
16136 static tree
16137 cp_parser_lookup_name (cp_parser *parser, tree name,
16138 enum tag_types tag_type,
16139 bool is_template,
16140 bool is_namespace,
16141 bool check_dependency,
16142 tree *ambiguous_decls)
16144 int flags = 0;
16145 tree decl;
16146 tree object_type = parser->context->object_type;
16148 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16149 flags |= LOOKUP_COMPLAIN;
16151 /* Assume that the lookup will be unambiguous. */
16152 if (ambiguous_decls)
16153 *ambiguous_decls = NULL_TREE;
16155 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16156 no longer valid. Note that if we are parsing tentatively, and
16157 the parse fails, OBJECT_TYPE will be automatically restored. */
16158 parser->context->object_type = NULL_TREE;
16160 if (name == error_mark_node)
16161 return error_mark_node;
16163 /* A template-id has already been resolved; there is no lookup to
16164 do. */
16165 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16166 return name;
16167 if (BASELINK_P (name))
16169 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16170 == TEMPLATE_ID_EXPR);
16171 return name;
16174 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16175 it should already have been checked to make sure that the name
16176 used matches the type being destroyed. */
16177 if (TREE_CODE (name) == BIT_NOT_EXPR)
16179 tree type;
16181 /* Figure out to which type this destructor applies. */
16182 if (parser->scope)
16183 type = parser->scope;
16184 else if (object_type)
16185 type = object_type;
16186 else
16187 type = current_class_type;
16188 /* If that's not a class type, there is no destructor. */
16189 if (!type || !CLASS_TYPE_P (type))
16190 return error_mark_node;
16191 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16192 lazily_declare_fn (sfk_destructor, type);
16193 if (!CLASSTYPE_DESTRUCTORS (type))
16194 return error_mark_node;
16195 /* If it was a class type, return the destructor. */
16196 return CLASSTYPE_DESTRUCTORS (type);
16199 /* By this point, the NAME should be an ordinary identifier. If
16200 the id-expression was a qualified name, the qualifying scope is
16201 stored in PARSER->SCOPE at this point. */
16202 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16204 /* Perform the lookup. */
16205 if (parser->scope)
16207 bool dependent_p;
16209 if (parser->scope == error_mark_node)
16210 return error_mark_node;
16212 /* If the SCOPE is dependent, the lookup must be deferred until
16213 the template is instantiated -- unless we are explicitly
16214 looking up names in uninstantiated templates. Even then, we
16215 cannot look up the name if the scope is not a class type; it
16216 might, for example, be a template type parameter. */
16217 dependent_p = (TYPE_P (parser->scope)
16218 && !(parser->in_declarator_p
16219 && currently_open_class (parser->scope))
16220 && dependent_type_p (parser->scope));
16221 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16222 && dependent_p)
16224 if (tag_type)
16226 tree type;
16228 /* The resolution to Core Issue 180 says that `struct
16229 A::B' should be considered a type-name, even if `A'
16230 is dependent. */
16231 type = make_typename_type (parser->scope, name, tag_type,
16232 /*complain=*/tf_error);
16233 decl = TYPE_NAME (type);
16235 else if (is_template
16236 && (cp_parser_next_token_ends_template_argument_p (parser)
16237 || cp_lexer_next_token_is (parser->lexer,
16238 CPP_CLOSE_PAREN)))
16239 decl = make_unbound_class_template (parser->scope,
16240 name, NULL_TREE,
16241 /*complain=*/tf_error);
16242 else
16243 decl = build_qualified_name (/*type=*/NULL_TREE,
16244 parser->scope, name,
16245 is_template);
16247 else
16249 tree pushed_scope = NULL_TREE;
16251 /* If PARSER->SCOPE is a dependent type, then it must be a
16252 class type, and we must not be checking dependencies;
16253 otherwise, we would have processed this lookup above. So
16254 that PARSER->SCOPE is not considered a dependent base by
16255 lookup_member, we must enter the scope here. */
16256 if (dependent_p)
16257 pushed_scope = push_scope (parser->scope);
16258 /* If the PARSER->SCOPE is a template specialization, it
16259 may be instantiated during name lookup. In that case,
16260 errors may be issued. Even if we rollback the current
16261 tentative parse, those errors are valid. */
16262 decl = lookup_qualified_name (parser->scope, name,
16263 tag_type != none_type,
16264 /*complain=*/true);
16265 if (pushed_scope)
16266 pop_scope (pushed_scope);
16268 parser->qualifying_scope = parser->scope;
16269 parser->object_scope = NULL_TREE;
16271 else if (object_type)
16273 tree object_decl = NULL_TREE;
16274 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16275 OBJECT_TYPE is not a class. */
16276 if (CLASS_TYPE_P (object_type))
16277 /* If the OBJECT_TYPE is a template specialization, it may
16278 be instantiated during name lookup. In that case, errors
16279 may be issued. Even if we rollback the current tentative
16280 parse, those errors are valid. */
16281 object_decl = lookup_member (object_type,
16282 name,
16283 /*protect=*/0,
16284 tag_type != none_type);
16285 /* Look it up in the enclosing context, too. */
16286 decl = lookup_name_real (name, tag_type != none_type,
16287 /*nonclass=*/0,
16288 /*block_p=*/true, is_namespace, flags);
16289 parser->object_scope = object_type;
16290 parser->qualifying_scope = NULL_TREE;
16291 if (object_decl)
16292 decl = object_decl;
16294 else
16296 decl = lookup_name_real (name, tag_type != none_type,
16297 /*nonclass=*/0,
16298 /*block_p=*/true, is_namespace, flags);
16299 parser->qualifying_scope = NULL_TREE;
16300 parser->object_scope = NULL_TREE;
16303 /* If the lookup failed, let our caller know. */
16304 if (!decl || decl == error_mark_node)
16305 return error_mark_node;
16307 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16308 if (TREE_CODE (decl) == TREE_LIST)
16310 if (ambiguous_decls)
16311 *ambiguous_decls = decl;
16312 /* The error message we have to print is too complicated for
16313 cp_parser_error, so we incorporate its actions directly. */
16314 if (!cp_parser_simulate_error (parser))
16316 error ("reference to %qD is ambiguous", name);
16317 print_candidates (decl);
16319 return error_mark_node;
16322 gcc_assert (DECL_P (decl)
16323 || TREE_CODE (decl) == OVERLOAD
16324 || TREE_CODE (decl) == SCOPE_REF
16325 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16326 || BASELINK_P (decl));
16328 /* If we have resolved the name of a member declaration, check to
16329 see if the declaration is accessible. When the name resolves to
16330 set of overloaded functions, accessibility is checked when
16331 overload resolution is done.
16333 During an explicit instantiation, access is not checked at all,
16334 as per [temp.explicit]. */
16335 if (DECL_P (decl))
16336 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16338 return decl;
16341 /* Like cp_parser_lookup_name, but for use in the typical case where
16342 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16343 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16345 static tree
16346 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16348 return cp_parser_lookup_name (parser, name,
16349 none_type,
16350 /*is_template=*/false,
16351 /*is_namespace=*/false,
16352 /*check_dependency=*/true,
16353 /*ambiguous_decls=*/NULL);
16356 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16357 the current context, return the TYPE_DECL. If TAG_NAME_P is
16358 true, the DECL indicates the class being defined in a class-head,
16359 or declared in an elaborated-type-specifier.
16361 Otherwise, return DECL. */
16363 static tree
16364 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16366 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16367 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16369 struct A {
16370 template <typename T> struct B;
16373 template <typename T> struct A::B {};
16375 Similarly, in an elaborated-type-specifier:
16377 namespace N { struct X{}; }
16379 struct A {
16380 template <typename T> friend struct N::X;
16383 However, if the DECL refers to a class type, and we are in
16384 the scope of the class, then the name lookup automatically
16385 finds the TYPE_DECL created by build_self_reference rather
16386 than a TEMPLATE_DECL. For example, in:
16388 template <class T> struct S {
16389 S s;
16392 there is no need to handle such case. */
16394 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16395 return DECL_TEMPLATE_RESULT (decl);
16397 return decl;
16400 /* If too many, or too few, template-parameter lists apply to the
16401 declarator, issue an error message. Returns TRUE if all went well,
16402 and FALSE otherwise. */
16404 static bool
16405 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16406 cp_declarator *declarator)
16408 unsigned num_templates;
16410 /* We haven't seen any classes that involve template parameters yet. */
16411 num_templates = 0;
16413 switch (declarator->kind)
16415 case cdk_id:
16416 if (declarator->u.id.qualifying_scope)
16418 tree scope;
16419 tree member;
16421 scope = declarator->u.id.qualifying_scope;
16422 member = declarator->u.id.unqualified_name;
16424 while (scope && CLASS_TYPE_P (scope))
16426 /* You're supposed to have one `template <...>'
16427 for every template class, but you don't need one
16428 for a full specialization. For example:
16430 template <class T> struct S{};
16431 template <> struct S<int> { void f(); };
16432 void S<int>::f () {}
16434 is correct; there shouldn't be a `template <>' for
16435 the definition of `S<int>::f'. */
16436 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16437 /* If SCOPE does not have template information of any
16438 kind, then it is not a template, nor is it nested
16439 within a template. */
16440 break;
16441 if (explicit_class_specialization_p (scope))
16442 break;
16443 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16444 ++num_templates;
16446 scope = TYPE_CONTEXT (scope);
16449 else if (TREE_CODE (declarator->u.id.unqualified_name)
16450 == TEMPLATE_ID_EXPR)
16451 /* If the DECLARATOR has the form `X<y>' then it uses one
16452 additional level of template parameters. */
16453 ++num_templates;
16455 return cp_parser_check_template_parameters (parser,
16456 num_templates);
16458 case cdk_function:
16459 case cdk_array:
16460 case cdk_pointer:
16461 case cdk_reference:
16462 case cdk_ptrmem:
16463 return (cp_parser_check_declarator_template_parameters
16464 (parser, declarator->declarator));
16466 case cdk_error:
16467 return true;
16469 default:
16470 gcc_unreachable ();
16472 return false;
16475 /* NUM_TEMPLATES were used in the current declaration. If that is
16476 invalid, return FALSE and issue an error messages. Otherwise,
16477 return TRUE. */
16479 static bool
16480 cp_parser_check_template_parameters (cp_parser* parser,
16481 unsigned num_templates)
16483 /* If there are more template classes than parameter lists, we have
16484 something like:
16486 template <class T> void S<T>::R<T>::f (); */
16487 if (parser->num_template_parameter_lists < num_templates)
16489 error ("too few template-parameter-lists");
16490 return false;
16492 /* If there are the same number of template classes and parameter
16493 lists, that's OK. */
16494 if (parser->num_template_parameter_lists == num_templates)
16495 return true;
16496 /* If there are more, but only one more, then we are referring to a
16497 member template. That's OK too. */
16498 if (parser->num_template_parameter_lists == num_templates + 1)
16499 return true;
16500 /* Otherwise, there are too many template parameter lists. We have
16501 something like:
16503 template <class T> template <class U> void S::f(); */
16504 error ("too many template-parameter-lists");
16505 return false;
16508 /* Parse an optional `::' token indicating that the following name is
16509 from the global namespace. If so, PARSER->SCOPE is set to the
16510 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16511 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16512 Returns the new value of PARSER->SCOPE, if the `::' token is
16513 present, and NULL_TREE otherwise. */
16515 static tree
16516 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16518 cp_token *token;
16520 /* Peek at the next token. */
16521 token = cp_lexer_peek_token (parser->lexer);
16522 /* If we're looking at a `::' token then we're starting from the
16523 global namespace, not our current location. */
16524 if (token->type == CPP_SCOPE)
16526 /* Consume the `::' token. */
16527 cp_lexer_consume_token (parser->lexer);
16528 /* Set the SCOPE so that we know where to start the lookup. */
16529 parser->scope = global_namespace;
16530 parser->qualifying_scope = global_namespace;
16531 parser->object_scope = NULL_TREE;
16533 return parser->scope;
16535 else if (!current_scope_valid_p)
16537 parser->scope = NULL_TREE;
16538 parser->qualifying_scope = NULL_TREE;
16539 parser->object_scope = NULL_TREE;
16542 return NULL_TREE;
16545 /* Returns TRUE if the upcoming token sequence is the start of a
16546 constructor declarator. If FRIEND_P is true, the declarator is
16547 preceded by the `friend' specifier. */
16549 static bool
16550 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16552 bool constructor_p;
16553 tree type_decl = NULL_TREE;
16554 bool nested_name_p;
16555 cp_token *next_token;
16557 /* The common case is that this is not a constructor declarator, so
16558 try to avoid doing lots of work if at all possible. It's not
16559 valid declare a constructor at function scope. */
16560 if (parser->in_function_body)
16561 return false;
16562 /* And only certain tokens can begin a constructor declarator. */
16563 next_token = cp_lexer_peek_token (parser->lexer);
16564 if (next_token->type != CPP_NAME
16565 && next_token->type != CPP_SCOPE
16566 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16567 && next_token->type != CPP_TEMPLATE_ID)
16568 return false;
16570 /* Parse tentatively; we are going to roll back all of the tokens
16571 consumed here. */
16572 cp_parser_parse_tentatively (parser);
16573 /* Assume that we are looking at a constructor declarator. */
16574 constructor_p = true;
16576 /* Look for the optional `::' operator. */
16577 cp_parser_global_scope_opt (parser,
16578 /*current_scope_valid_p=*/false);
16579 /* Look for the nested-name-specifier. */
16580 nested_name_p
16581 = (cp_parser_nested_name_specifier_opt (parser,
16582 /*typename_keyword_p=*/false,
16583 /*check_dependency_p=*/false,
16584 /*type_p=*/false,
16585 /*is_declaration=*/false)
16586 != NULL_TREE);
16587 /* Outside of a class-specifier, there must be a
16588 nested-name-specifier. */
16589 if (!nested_name_p &&
16590 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16591 || friend_p))
16592 constructor_p = false;
16593 /* If we still think that this might be a constructor-declarator,
16594 look for a class-name. */
16595 if (constructor_p)
16597 /* If we have:
16599 template <typename T> struct S { S(); };
16600 template <typename T> S<T>::S ();
16602 we must recognize that the nested `S' names a class.
16603 Similarly, for:
16605 template <typename T> S<T>::S<T> ();
16607 we must recognize that the nested `S' names a template. */
16608 type_decl = cp_parser_class_name (parser,
16609 /*typename_keyword_p=*/false,
16610 /*template_keyword_p=*/false,
16611 none_type,
16612 /*check_dependency_p=*/false,
16613 /*class_head_p=*/false,
16614 /*is_declaration=*/false);
16615 /* If there was no class-name, then this is not a constructor. */
16616 constructor_p = !cp_parser_error_occurred (parser);
16619 /* If we're still considering a constructor, we have to see a `(',
16620 to begin the parameter-declaration-clause, followed by either a
16621 `)', an `...', or a decl-specifier. We need to check for a
16622 type-specifier to avoid being fooled into thinking that:
16624 S::S (f) (int);
16626 is a constructor. (It is actually a function named `f' that
16627 takes one parameter (of type `int') and returns a value of type
16628 `S::S'. */
16629 if (constructor_p
16630 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16632 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16633 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16634 /* A parameter declaration begins with a decl-specifier,
16635 which is either the "attribute" keyword, a storage class
16636 specifier, or (usually) a type-specifier. */
16637 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16639 tree type;
16640 tree pushed_scope = NULL_TREE;
16641 unsigned saved_num_template_parameter_lists;
16643 /* Names appearing in the type-specifier should be looked up
16644 in the scope of the class. */
16645 if (current_class_type)
16646 type = NULL_TREE;
16647 else
16649 type = TREE_TYPE (type_decl);
16650 if (TREE_CODE (type) == TYPENAME_TYPE)
16652 type = resolve_typename_type (type,
16653 /*only_current_p=*/false);
16654 if (TREE_CODE (type) == TYPENAME_TYPE)
16656 cp_parser_abort_tentative_parse (parser);
16657 return false;
16660 pushed_scope = push_scope (type);
16663 /* Inside the constructor parameter list, surrounding
16664 template-parameter-lists do not apply. */
16665 saved_num_template_parameter_lists
16666 = parser->num_template_parameter_lists;
16667 parser->num_template_parameter_lists = 0;
16669 /* Look for the type-specifier. */
16670 cp_parser_type_specifier (parser,
16671 CP_PARSER_FLAGS_NONE,
16672 /*decl_specs=*/NULL,
16673 /*is_declarator=*/true,
16674 /*declares_class_or_enum=*/NULL,
16675 /*is_cv_qualifier=*/NULL);
16677 parser->num_template_parameter_lists
16678 = saved_num_template_parameter_lists;
16680 /* Leave the scope of the class. */
16681 if (pushed_scope)
16682 pop_scope (pushed_scope);
16684 constructor_p = !cp_parser_error_occurred (parser);
16687 else
16688 constructor_p = false;
16689 /* We did not really want to consume any tokens. */
16690 cp_parser_abort_tentative_parse (parser);
16692 return constructor_p;
16695 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16696 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16697 they must be performed once we are in the scope of the function.
16699 Returns the function defined. */
16701 static tree
16702 cp_parser_function_definition_from_specifiers_and_declarator
16703 (cp_parser* parser,
16704 cp_decl_specifier_seq *decl_specifiers,
16705 tree attributes,
16706 const cp_declarator *declarator)
16708 tree fn;
16709 bool success_p;
16711 /* Begin the function-definition. */
16712 success_p = start_function (decl_specifiers, declarator, attributes);
16714 /* The things we're about to see are not directly qualified by any
16715 template headers we've seen thus far. */
16716 reset_specialization ();
16718 /* If there were names looked up in the decl-specifier-seq that we
16719 did not check, check them now. We must wait until we are in the
16720 scope of the function to perform the checks, since the function
16721 might be a friend. */
16722 perform_deferred_access_checks ();
16724 if (!success_p)
16726 /* Skip the entire function. */
16727 cp_parser_skip_to_end_of_block_or_statement (parser);
16728 fn = error_mark_node;
16730 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16732 /* Seen already, skip it. An error message has already been output. */
16733 cp_parser_skip_to_end_of_block_or_statement (parser);
16734 fn = current_function_decl;
16735 current_function_decl = NULL_TREE;
16736 /* If this is a function from a class, pop the nested class. */
16737 if (current_class_name)
16738 pop_nested_class ();
16740 else
16741 fn = cp_parser_function_definition_after_declarator (parser,
16742 /*inline_p=*/false);
16744 return fn;
16747 /* Parse the part of a function-definition that follows the
16748 declarator. INLINE_P is TRUE iff this function is an inline
16749 function defined with a class-specifier.
16751 Returns the function defined. */
16753 static tree
16754 cp_parser_function_definition_after_declarator (cp_parser* parser,
16755 bool inline_p)
16757 tree fn;
16758 bool ctor_initializer_p = false;
16759 bool saved_in_unbraced_linkage_specification_p;
16760 bool saved_in_function_body;
16761 unsigned saved_num_template_parameter_lists;
16763 saved_in_function_body = parser->in_function_body;
16764 parser->in_function_body = true;
16765 /* If the next token is `return', then the code may be trying to
16766 make use of the "named return value" extension that G++ used to
16767 support. */
16768 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16770 /* Consume the `return' keyword. */
16771 cp_lexer_consume_token (parser->lexer);
16772 /* Look for the identifier that indicates what value is to be
16773 returned. */
16774 cp_parser_identifier (parser);
16775 /* Issue an error message. */
16776 error ("named return values are no longer supported");
16777 /* Skip tokens until we reach the start of the function body. */
16778 while (true)
16780 cp_token *token = cp_lexer_peek_token (parser->lexer);
16781 if (token->type == CPP_OPEN_BRACE
16782 || token->type == CPP_EOF
16783 || token->type == CPP_PRAGMA_EOL)
16784 break;
16785 cp_lexer_consume_token (parser->lexer);
16788 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16789 anything declared inside `f'. */
16790 saved_in_unbraced_linkage_specification_p
16791 = parser->in_unbraced_linkage_specification_p;
16792 parser->in_unbraced_linkage_specification_p = false;
16793 /* Inside the function, surrounding template-parameter-lists do not
16794 apply. */
16795 saved_num_template_parameter_lists
16796 = parser->num_template_parameter_lists;
16797 parser->num_template_parameter_lists = 0;
16798 /* If the next token is `try', then we are looking at a
16799 function-try-block. */
16800 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16801 ctor_initializer_p = cp_parser_function_try_block (parser);
16802 /* A function-try-block includes the function-body, so we only do
16803 this next part if we're not processing a function-try-block. */
16804 else
16805 ctor_initializer_p
16806 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16808 /* Finish the function. */
16809 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16810 (inline_p ? 2 : 0));
16811 /* Generate code for it, if necessary. */
16812 expand_or_defer_fn (fn);
16813 /* Restore the saved values. */
16814 parser->in_unbraced_linkage_specification_p
16815 = saved_in_unbraced_linkage_specification_p;
16816 parser->num_template_parameter_lists
16817 = saved_num_template_parameter_lists;
16818 parser->in_function_body = saved_in_function_body;
16820 return fn;
16823 /* Parse a template-declaration, assuming that the `export' (and
16824 `extern') keywords, if present, has already been scanned. MEMBER_P
16825 is as for cp_parser_template_declaration. */
16827 static void
16828 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16830 tree decl = NULL_TREE;
16831 VEC (deferred_access_check,gc) *checks;
16832 tree parameter_list;
16833 bool friend_p = false;
16834 bool need_lang_pop;
16836 /* Look for the `template' keyword. */
16837 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16838 return;
16840 /* And the `<'. */
16841 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16842 return;
16843 if (at_class_scope_p () && current_function_decl)
16845 /* 14.5.2.2 [temp.mem]
16847 A local class shall not have member templates. */
16848 error ("invalid declaration of member template in local class");
16849 cp_parser_skip_to_end_of_block_or_statement (parser);
16850 return;
16852 /* [temp]
16854 A template ... shall not have C linkage. */
16855 if (current_lang_name == lang_name_c)
16857 error ("template with C linkage");
16858 /* Give it C++ linkage to avoid confusing other parts of the
16859 front end. */
16860 push_lang_context (lang_name_cplusplus);
16861 need_lang_pop = true;
16863 else
16864 need_lang_pop = false;
16866 /* We cannot perform access checks on the template parameter
16867 declarations until we know what is being declared, just as we
16868 cannot check the decl-specifier list. */
16869 push_deferring_access_checks (dk_deferred);
16871 /* If the next token is `>', then we have an invalid
16872 specialization. Rather than complain about an invalid template
16873 parameter, issue an error message here. */
16874 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16876 cp_parser_error (parser, "invalid explicit specialization");
16877 begin_specialization ();
16878 parameter_list = NULL_TREE;
16880 else
16881 /* Parse the template parameters. */
16882 parameter_list = cp_parser_template_parameter_list (parser);
16884 /* Get the deferred access checks from the parameter list. These
16885 will be checked once we know what is being declared, as for a
16886 member template the checks must be performed in the scope of the
16887 class containing the member. */
16888 checks = get_deferred_access_checks ();
16890 /* Look for the `>'. */
16891 cp_parser_skip_to_end_of_template_parameter_list (parser);
16892 /* We just processed one more parameter list. */
16893 ++parser->num_template_parameter_lists;
16894 /* If the next token is `template', there are more template
16895 parameters. */
16896 if (cp_lexer_next_token_is_keyword (parser->lexer,
16897 RID_TEMPLATE))
16898 cp_parser_template_declaration_after_export (parser, member_p);
16899 else
16901 /* There are no access checks when parsing a template, as we do not
16902 know if a specialization will be a friend. */
16903 push_deferring_access_checks (dk_no_check);
16904 decl = cp_parser_single_declaration (parser,
16905 checks,
16906 member_p,
16907 /*explicit_specialization_p=*/false,
16908 &friend_p);
16909 pop_deferring_access_checks ();
16911 /* If this is a member template declaration, let the front
16912 end know. */
16913 if (member_p && !friend_p && decl)
16915 if (TREE_CODE (decl) == TYPE_DECL)
16916 cp_parser_check_access_in_redeclaration (decl);
16918 decl = finish_member_template_decl (decl);
16920 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16921 make_friend_class (current_class_type, TREE_TYPE (decl),
16922 /*complain=*/true);
16924 /* We are done with the current parameter list. */
16925 --parser->num_template_parameter_lists;
16927 pop_deferring_access_checks ();
16929 /* Finish up. */
16930 finish_template_decl (parameter_list);
16932 /* Register member declarations. */
16933 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16934 finish_member_declaration (decl);
16935 /* For the erroneous case of a template with C linkage, we pushed an
16936 implicit C++ linkage scope; exit that scope now. */
16937 if (need_lang_pop)
16938 pop_lang_context ();
16939 /* If DECL is a function template, we must return to parse it later.
16940 (Even though there is no definition, there might be default
16941 arguments that need handling.) */
16942 if (member_p && decl
16943 && (TREE_CODE (decl) == FUNCTION_DECL
16944 || DECL_FUNCTION_TEMPLATE_P (decl)))
16945 TREE_VALUE (parser->unparsed_functions_queues)
16946 = tree_cons (NULL_TREE, decl,
16947 TREE_VALUE (parser->unparsed_functions_queues));
16950 /* Perform the deferred access checks from a template-parameter-list.
16951 CHECKS is a TREE_LIST of access checks, as returned by
16952 get_deferred_access_checks. */
16954 static void
16955 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16957 ++processing_template_parmlist;
16958 perform_access_checks (checks);
16959 --processing_template_parmlist;
16962 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16963 `function-definition' sequence. MEMBER_P is true, this declaration
16964 appears in a class scope.
16966 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16967 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16969 static tree
16970 cp_parser_single_declaration (cp_parser* parser,
16971 VEC (deferred_access_check,gc)* checks,
16972 bool member_p,
16973 bool explicit_specialization_p,
16974 bool* friend_p)
16976 int declares_class_or_enum;
16977 tree decl = NULL_TREE;
16978 cp_decl_specifier_seq decl_specifiers;
16979 bool function_definition_p = false;
16981 /* This function is only used when processing a template
16982 declaration. */
16983 gcc_assert (innermost_scope_kind () == sk_template_parms
16984 || innermost_scope_kind () == sk_template_spec);
16986 /* Defer access checks until we know what is being declared. */
16987 push_deferring_access_checks (dk_deferred);
16989 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16990 alternative. */
16991 cp_parser_decl_specifier_seq (parser,
16992 CP_PARSER_FLAGS_OPTIONAL,
16993 &decl_specifiers,
16994 &declares_class_or_enum);
16995 if (friend_p)
16996 *friend_p = cp_parser_friend_p (&decl_specifiers);
16998 /* There are no template typedefs. */
16999 if (decl_specifiers.specs[(int) ds_typedef])
17001 error ("template declaration of %qs", "typedef");
17002 decl = error_mark_node;
17005 /* Gather up the access checks that occurred the
17006 decl-specifier-seq. */
17007 stop_deferring_access_checks ();
17009 /* Check for the declaration of a template class. */
17010 if (declares_class_or_enum)
17012 if (cp_parser_declares_only_class_p (parser))
17014 decl = shadow_tag (&decl_specifiers);
17016 /* In this case:
17018 struct C {
17019 friend template <typename T> struct A<T>::B;
17022 A<T>::B will be represented by a TYPENAME_TYPE, and
17023 therefore not recognized by shadow_tag. */
17024 if (friend_p && *friend_p
17025 && !decl
17026 && decl_specifiers.type
17027 && TYPE_P (decl_specifiers.type))
17028 decl = decl_specifiers.type;
17030 if (decl && decl != error_mark_node)
17031 decl = TYPE_NAME (decl);
17032 else
17033 decl = error_mark_node;
17035 /* Perform access checks for template parameters. */
17036 cp_parser_perform_template_parameter_access_checks (checks);
17039 /* If it's not a template class, try for a template function. If
17040 the next token is a `;', then this declaration does not declare
17041 anything. But, if there were errors in the decl-specifiers, then
17042 the error might well have come from an attempted class-specifier.
17043 In that case, there's no need to warn about a missing declarator. */
17044 if (!decl
17045 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17046 || decl_specifiers.type != error_mark_node))
17048 decl = cp_parser_init_declarator (parser,
17049 &decl_specifiers,
17050 checks,
17051 /*function_definition_allowed_p=*/true,
17052 member_p,
17053 declares_class_or_enum,
17054 &function_definition_p);
17056 /* 7.1.1-1 [dcl.stc]
17058 A storage-class-specifier shall not be specified in an explicit
17059 specialization... */
17060 if (decl
17061 && explicit_specialization_p
17062 && decl_specifiers.storage_class != sc_none)
17064 error ("explicit template specialization cannot have a storage class");
17065 decl = error_mark_node;
17069 pop_deferring_access_checks ();
17071 /* Clear any current qualification; whatever comes next is the start
17072 of something new. */
17073 parser->scope = NULL_TREE;
17074 parser->qualifying_scope = NULL_TREE;
17075 parser->object_scope = NULL_TREE;
17076 /* Look for a trailing `;' after the declaration. */
17077 if (!function_definition_p
17078 && (decl == error_mark_node
17079 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
17080 cp_parser_skip_to_end_of_block_or_statement (parser);
17082 return decl;
17085 /* Parse a cast-expression that is not the operand of a unary "&". */
17087 static tree
17088 cp_parser_simple_cast_expression (cp_parser *parser)
17090 return cp_parser_cast_expression (parser, /*address_p=*/false,
17091 /*cast_p=*/false);
17094 /* Parse a functional cast to TYPE. Returns an expression
17095 representing the cast. */
17097 static tree
17098 cp_parser_functional_cast (cp_parser* parser, tree type)
17100 tree expression_list;
17101 tree cast;
17103 expression_list
17104 = cp_parser_parenthesized_expression_list (parser, false,
17105 /*cast_p=*/true,
17106 /*allow_expansion_p=*/true,
17107 /*non_constant_p=*/NULL);
17109 cast = build_functional_cast (type, expression_list);
17110 /* [expr.const]/1: In an integral constant expression "only type
17111 conversions to integral or enumeration type can be used". */
17112 if (TREE_CODE (type) == TYPE_DECL)
17113 type = TREE_TYPE (type);
17114 if (cast != error_mark_node
17115 && !cast_valid_in_integral_constant_expression_p (type)
17116 && (cp_parser_non_integral_constant_expression
17117 (parser, "a call to a constructor")))
17118 return error_mark_node;
17119 return cast;
17122 /* Save the tokens that make up the body of a member function defined
17123 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17124 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17125 specifiers applied to the declaration. Returns the FUNCTION_DECL
17126 for the member function. */
17128 static tree
17129 cp_parser_save_member_function_body (cp_parser* parser,
17130 cp_decl_specifier_seq *decl_specifiers,
17131 cp_declarator *declarator,
17132 tree attributes)
17134 cp_token *first;
17135 cp_token *last;
17136 tree fn;
17138 /* Create the function-declaration. */
17139 fn = start_method (decl_specifiers, declarator, attributes);
17140 /* If something went badly wrong, bail out now. */
17141 if (fn == error_mark_node)
17143 /* If there's a function-body, skip it. */
17144 if (cp_parser_token_starts_function_definition_p
17145 (cp_lexer_peek_token (parser->lexer)))
17146 cp_parser_skip_to_end_of_block_or_statement (parser);
17147 return error_mark_node;
17150 /* Remember it, if there default args to post process. */
17151 cp_parser_save_default_args (parser, fn);
17153 /* Save away the tokens that make up the body of the
17154 function. */
17155 first = parser->lexer->next_token;
17156 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17157 /* Handle function try blocks. */
17158 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17159 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17160 last = parser->lexer->next_token;
17162 /* Save away the inline definition; we will process it when the
17163 class is complete. */
17164 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17165 DECL_PENDING_INLINE_P (fn) = 1;
17167 /* We need to know that this was defined in the class, so that
17168 friend templates are handled correctly. */
17169 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17171 /* We're done with the inline definition. */
17172 finish_method (fn);
17174 /* Add FN to the queue of functions to be parsed later. */
17175 TREE_VALUE (parser->unparsed_functions_queues)
17176 = tree_cons (NULL_TREE, fn,
17177 TREE_VALUE (parser->unparsed_functions_queues));
17179 return fn;
17182 /* Parse a template-argument-list, as well as the trailing ">" (but
17183 not the opening ">"). See cp_parser_template_argument_list for the
17184 return value. */
17186 static tree
17187 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17189 tree arguments;
17190 tree saved_scope;
17191 tree saved_qualifying_scope;
17192 tree saved_object_scope;
17193 bool saved_greater_than_is_operator_p;
17194 bool saved_skip_evaluation;
17196 /* [temp.names]
17198 When parsing a template-id, the first non-nested `>' is taken as
17199 the end of the template-argument-list rather than a greater-than
17200 operator. */
17201 saved_greater_than_is_operator_p
17202 = parser->greater_than_is_operator_p;
17203 parser->greater_than_is_operator_p = false;
17204 /* Parsing the argument list may modify SCOPE, so we save it
17205 here. */
17206 saved_scope = parser->scope;
17207 saved_qualifying_scope = parser->qualifying_scope;
17208 saved_object_scope = parser->object_scope;
17209 /* We need to evaluate the template arguments, even though this
17210 template-id may be nested within a "sizeof". */
17211 saved_skip_evaluation = skip_evaluation;
17212 skip_evaluation = false;
17213 /* Parse the template-argument-list itself. */
17214 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17215 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17216 arguments = NULL_TREE;
17217 else
17218 arguments = cp_parser_template_argument_list (parser);
17219 /* Look for the `>' that ends the template-argument-list. If we find
17220 a '>>' instead, it's probably just a typo. */
17221 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17223 if (cxx_dialect != cxx98)
17225 /* In C++0x, a `>>' in a template argument list or cast
17226 expression is considered to be two separate `>'
17227 tokens. So, change the current token to a `>', but don't
17228 consume it: it will be consumed later when the outer
17229 template argument list (or cast expression) is parsed.
17230 Note that this replacement of `>' for `>>' is necessary
17231 even if we are parsing tentatively: in the tentative
17232 case, after calling
17233 cp_parser_enclosed_template_argument_list we will always
17234 throw away all of the template arguments and the first
17235 closing `>', either because the template argument list
17236 was erroneous or because we are replacing those tokens
17237 with a CPP_TEMPLATE_ID token. The second `>' (which will
17238 not have been thrown away) is needed either to close an
17239 outer template argument list or to complete a new-style
17240 cast. */
17241 cp_token *token = cp_lexer_peek_token (parser->lexer);
17242 token->type = CPP_GREATER;
17244 else if (!saved_greater_than_is_operator_p)
17246 /* If we're in a nested template argument list, the '>>' has
17247 to be a typo for '> >'. We emit the error message, but we
17248 continue parsing and we push a '>' as next token, so that
17249 the argument list will be parsed correctly. Note that the
17250 global source location is still on the token before the
17251 '>>', so we need to say explicitly where we want it. */
17252 cp_token *token = cp_lexer_peek_token (parser->lexer);
17253 error ("%H%<>>%> should be %<> >%> "
17254 "within a nested template argument list",
17255 &token->location);
17257 token->type = CPP_GREATER;
17259 else
17261 /* If this is not a nested template argument list, the '>>'
17262 is a typo for '>'. Emit an error message and continue.
17263 Same deal about the token location, but here we can get it
17264 right by consuming the '>>' before issuing the diagnostic. */
17265 cp_lexer_consume_token (parser->lexer);
17266 error ("spurious %<>>%>, use %<>%> to terminate "
17267 "a template argument list");
17270 else
17271 cp_parser_skip_to_end_of_template_parameter_list (parser);
17272 /* The `>' token might be a greater-than operator again now. */
17273 parser->greater_than_is_operator_p
17274 = saved_greater_than_is_operator_p;
17275 /* Restore the SAVED_SCOPE. */
17276 parser->scope = saved_scope;
17277 parser->qualifying_scope = saved_qualifying_scope;
17278 parser->object_scope = saved_object_scope;
17279 skip_evaluation = saved_skip_evaluation;
17281 return arguments;
17284 /* MEMBER_FUNCTION is a member function, or a friend. If default
17285 arguments, or the body of the function have not yet been parsed,
17286 parse them now. */
17288 static void
17289 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17291 /* If this member is a template, get the underlying
17292 FUNCTION_DECL. */
17293 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17294 member_function = DECL_TEMPLATE_RESULT (member_function);
17296 /* There should not be any class definitions in progress at this
17297 point; the bodies of members are only parsed outside of all class
17298 definitions. */
17299 gcc_assert (parser->num_classes_being_defined == 0);
17300 /* While we're parsing the member functions we might encounter more
17301 classes. We want to handle them right away, but we don't want
17302 them getting mixed up with functions that are currently in the
17303 queue. */
17304 parser->unparsed_functions_queues
17305 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17307 /* Make sure that any template parameters are in scope. */
17308 maybe_begin_member_template_processing (member_function);
17310 /* If the body of the function has not yet been parsed, parse it
17311 now. */
17312 if (DECL_PENDING_INLINE_P (member_function))
17314 tree function_scope;
17315 cp_token_cache *tokens;
17317 /* The function is no longer pending; we are processing it. */
17318 tokens = DECL_PENDING_INLINE_INFO (member_function);
17319 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17320 DECL_PENDING_INLINE_P (member_function) = 0;
17322 /* If this is a local class, enter the scope of the containing
17323 function. */
17324 function_scope = current_function_decl;
17325 if (function_scope)
17326 push_function_context_to (function_scope);
17329 /* Push the body of the function onto the lexer stack. */
17330 cp_parser_push_lexer_for_tokens (parser, tokens);
17332 /* Let the front end know that we going to be defining this
17333 function. */
17334 start_preparsed_function (member_function, NULL_TREE,
17335 SF_PRE_PARSED | SF_INCLASS_INLINE);
17337 /* Don't do access checking if it is a templated function. */
17338 if (processing_template_decl)
17339 push_deferring_access_checks (dk_no_check);
17341 /* Now, parse the body of the function. */
17342 cp_parser_function_definition_after_declarator (parser,
17343 /*inline_p=*/true);
17345 if (processing_template_decl)
17346 pop_deferring_access_checks ();
17348 /* Leave the scope of the containing function. */
17349 if (function_scope)
17350 pop_function_context_from (function_scope);
17351 cp_parser_pop_lexer (parser);
17354 /* Remove any template parameters from the symbol table. */
17355 maybe_end_member_template_processing ();
17357 /* Restore the queue. */
17358 parser->unparsed_functions_queues
17359 = TREE_CHAIN (parser->unparsed_functions_queues);
17362 /* If DECL contains any default args, remember it on the unparsed
17363 functions queue. */
17365 static void
17366 cp_parser_save_default_args (cp_parser* parser, tree decl)
17368 tree probe;
17370 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17371 probe;
17372 probe = TREE_CHAIN (probe))
17373 if (TREE_PURPOSE (probe))
17375 TREE_PURPOSE (parser->unparsed_functions_queues)
17376 = tree_cons (current_class_type, decl,
17377 TREE_PURPOSE (parser->unparsed_functions_queues));
17378 break;
17382 /* FN is a FUNCTION_DECL which may contains a parameter with an
17383 unparsed DEFAULT_ARG. Parse the default args now. This function
17384 assumes that the current scope is the scope in which the default
17385 argument should be processed. */
17387 static void
17388 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17390 bool saved_local_variables_forbidden_p;
17391 tree parm;
17393 /* While we're parsing the default args, we might (due to the
17394 statement expression extension) encounter more classes. We want
17395 to handle them right away, but we don't want them getting mixed
17396 up with default args that are currently in the queue. */
17397 parser->unparsed_functions_queues
17398 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17400 /* Local variable names (and the `this' keyword) may not appear
17401 in a default argument. */
17402 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17403 parser->local_variables_forbidden_p = true;
17405 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17406 parm;
17407 parm = TREE_CHAIN (parm))
17409 cp_token_cache *tokens;
17410 tree default_arg = TREE_PURPOSE (parm);
17411 tree parsed_arg;
17412 VEC(tree,gc) *insts;
17413 tree copy;
17414 unsigned ix;
17416 if (!default_arg)
17417 continue;
17419 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17420 /* This can happen for a friend declaration for a function
17421 already declared with default arguments. */
17422 continue;
17424 /* Push the saved tokens for the default argument onto the parser's
17425 lexer stack. */
17426 tokens = DEFARG_TOKENS (default_arg);
17427 cp_parser_push_lexer_for_tokens (parser, tokens);
17429 /* Parse the assignment-expression. */
17430 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17432 if (!processing_template_decl)
17433 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17435 TREE_PURPOSE (parm) = parsed_arg;
17437 /* Update any instantiations we've already created. */
17438 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17439 VEC_iterate (tree, insts, ix, copy); ix++)
17440 TREE_PURPOSE (copy) = parsed_arg;
17442 /* If the token stream has not been completely used up, then
17443 there was extra junk after the end of the default
17444 argument. */
17445 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17446 cp_parser_error (parser, "expected %<,%>");
17448 /* Revert to the main lexer. */
17449 cp_parser_pop_lexer (parser);
17452 /* Make sure no default arg is missing. */
17453 check_default_args (fn);
17455 /* Restore the state of local_variables_forbidden_p. */
17456 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17458 /* Restore the queue. */
17459 parser->unparsed_functions_queues
17460 = TREE_CHAIN (parser->unparsed_functions_queues);
17463 /* Parse the operand of `sizeof' (or a similar operator). Returns
17464 either a TYPE or an expression, depending on the form of the
17465 input. The KEYWORD indicates which kind of expression we have
17466 encountered. */
17468 static tree
17469 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17471 static const char *format;
17472 tree expr = NULL_TREE;
17473 const char *saved_message;
17474 char *tmp;
17475 bool saved_integral_constant_expression_p;
17476 bool saved_non_integral_constant_expression_p;
17477 bool pack_expansion_p = false;
17479 /* Initialize FORMAT the first time we get here. */
17480 if (!format)
17481 format = "types may not be defined in '%s' expressions";
17483 /* Types cannot be defined in a `sizeof' expression. Save away the
17484 old message. */
17485 saved_message = parser->type_definition_forbidden_message;
17486 /* And create the new one. */
17487 parser->type_definition_forbidden_message = tmp
17488 = XNEWVEC (char, strlen (format)
17489 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17490 + 1 /* `\0' */);
17491 sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17493 /* The restrictions on constant-expressions do not apply inside
17494 sizeof expressions. */
17495 saved_integral_constant_expression_p
17496 = parser->integral_constant_expression_p;
17497 saved_non_integral_constant_expression_p
17498 = parser->non_integral_constant_expression_p;
17499 parser->integral_constant_expression_p = false;
17501 /* If it's a `...', then we are computing the length of a parameter
17502 pack. */
17503 if (keyword == RID_SIZEOF
17504 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17506 /* Consume the `...'. */
17507 cp_lexer_consume_token (parser->lexer);
17508 maybe_warn_variadic_templates ();
17510 /* Note that this is an expansion. */
17511 pack_expansion_p = true;
17514 /* Do not actually evaluate the expression. */
17515 ++skip_evaluation;
17516 /* If it's a `(', then we might be looking at the type-id
17517 construction. */
17518 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17520 tree type;
17521 bool saved_in_type_id_in_expr_p;
17523 /* We can't be sure yet whether we're looking at a type-id or an
17524 expression. */
17525 cp_parser_parse_tentatively (parser);
17526 /* Consume the `('. */
17527 cp_lexer_consume_token (parser->lexer);
17528 /* Parse the type-id. */
17529 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17530 parser->in_type_id_in_expr_p = true;
17531 type = cp_parser_type_id (parser);
17532 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17533 /* Now, look for the trailing `)'. */
17534 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17535 /* If all went well, then we're done. */
17536 if (cp_parser_parse_definitely (parser))
17538 cp_decl_specifier_seq decl_specs;
17540 /* Build a trivial decl-specifier-seq. */
17541 clear_decl_specs (&decl_specs);
17542 decl_specs.type = type;
17544 /* Call grokdeclarator to figure out what type this is. */
17545 expr = grokdeclarator (NULL,
17546 &decl_specs,
17547 TYPENAME,
17548 /*initialized=*/0,
17549 /*attrlist=*/NULL);
17553 /* If the type-id production did not work out, then we must be
17554 looking at the unary-expression production. */
17555 if (!expr)
17556 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17557 /*cast_p=*/false);
17559 if (pack_expansion_p)
17560 /* Build a pack expansion. */
17561 expr = make_pack_expansion (expr);
17563 /* Go back to evaluating expressions. */
17564 --skip_evaluation;
17566 /* Free the message we created. */
17567 free (tmp);
17568 /* And restore the old one. */
17569 parser->type_definition_forbidden_message = saved_message;
17570 parser->integral_constant_expression_p
17571 = saved_integral_constant_expression_p;
17572 parser->non_integral_constant_expression_p
17573 = saved_non_integral_constant_expression_p;
17575 return expr;
17578 /* If the current declaration has no declarator, return true. */
17580 static bool
17581 cp_parser_declares_only_class_p (cp_parser *parser)
17583 /* If the next token is a `;' or a `,' then there is no
17584 declarator. */
17585 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17586 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17589 /* Update the DECL_SPECS to reflect the storage class indicated by
17590 KEYWORD. */
17592 static void
17593 cp_parser_set_storage_class (cp_parser *parser,
17594 cp_decl_specifier_seq *decl_specs,
17595 enum rid keyword)
17597 cp_storage_class storage_class;
17599 if (parser->in_unbraced_linkage_specification_p)
17601 error ("invalid use of %qD in linkage specification",
17602 ridpointers[keyword]);
17603 return;
17605 else if (decl_specs->storage_class != sc_none)
17607 decl_specs->conflicting_specifiers_p = true;
17608 return;
17611 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17612 && decl_specs->specs[(int) ds_thread])
17614 error ("%<__thread%> before %qD", ridpointers[keyword]);
17615 decl_specs->specs[(int) ds_thread] = 0;
17618 switch (keyword)
17620 case RID_AUTO:
17621 storage_class = sc_auto;
17622 break;
17623 case RID_REGISTER:
17624 storage_class = sc_register;
17625 break;
17626 case RID_STATIC:
17627 storage_class = sc_static;
17628 break;
17629 case RID_EXTERN:
17630 storage_class = sc_extern;
17631 break;
17632 case RID_MUTABLE:
17633 storage_class = sc_mutable;
17634 break;
17635 default:
17636 gcc_unreachable ();
17638 decl_specs->storage_class = storage_class;
17640 /* A storage class specifier cannot be applied alongside a typedef
17641 specifier. If there is a typedef specifier present then set
17642 conflicting_specifiers_p which will trigger an error later
17643 on in grokdeclarator. */
17644 if (decl_specs->specs[(int)ds_typedef])
17645 decl_specs->conflicting_specifiers_p = true;
17648 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17649 is true, the type is a user-defined type; otherwise it is a
17650 built-in type specified by a keyword. */
17652 static void
17653 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17654 tree type_spec,
17655 bool user_defined_p)
17657 decl_specs->any_specifiers_p = true;
17659 /* If the user tries to redeclare bool or wchar_t (with, for
17660 example, in "typedef int wchar_t;") we remember that this is what
17661 happened. In system headers, we ignore these declarations so
17662 that G++ can work with system headers that are not C++-safe. */
17663 if (decl_specs->specs[(int) ds_typedef]
17664 && !user_defined_p
17665 && (type_spec == boolean_type_node
17666 || type_spec == wchar_type_node)
17667 && (decl_specs->type
17668 || decl_specs->specs[(int) ds_long]
17669 || decl_specs->specs[(int) ds_short]
17670 || decl_specs->specs[(int) ds_unsigned]
17671 || decl_specs->specs[(int) ds_signed]))
17673 decl_specs->redefined_builtin_type = type_spec;
17674 if (!decl_specs->type)
17676 decl_specs->type = type_spec;
17677 decl_specs->user_defined_type_p = false;
17680 else if (decl_specs->type)
17681 decl_specs->multiple_types_p = true;
17682 else
17684 decl_specs->type = type_spec;
17685 decl_specs->user_defined_type_p = user_defined_p;
17686 decl_specs->redefined_builtin_type = NULL_TREE;
17690 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17691 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17693 static bool
17694 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17696 return decl_specifiers->specs[(int) ds_friend] != 0;
17699 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17700 issue an error message indicating that TOKEN_DESC was expected.
17702 Returns the token consumed, if the token had the appropriate type.
17703 Otherwise, returns NULL. */
17705 static cp_token *
17706 cp_parser_require (cp_parser* parser,
17707 enum cpp_ttype type,
17708 const char* token_desc)
17710 if (cp_lexer_next_token_is (parser->lexer, type))
17711 return cp_lexer_consume_token (parser->lexer);
17712 else
17714 /* Output the MESSAGE -- unless we're parsing tentatively. */
17715 if (!cp_parser_simulate_error (parser))
17717 char *message = concat ("expected ", token_desc, NULL);
17718 cp_parser_error (parser, message);
17719 free (message);
17721 return NULL;
17725 /* An error message is produced if the next token is not '>'.
17726 All further tokens are skipped until the desired token is
17727 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17729 static void
17730 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17732 /* Current level of '< ... >'. */
17733 unsigned level = 0;
17734 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17735 unsigned nesting_depth = 0;
17737 /* Are we ready, yet? If not, issue error message. */
17738 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17739 return;
17741 /* Skip tokens until the desired token is found. */
17742 while (true)
17744 /* Peek at the next token. */
17745 switch (cp_lexer_peek_token (parser->lexer)->type)
17747 case CPP_LESS:
17748 if (!nesting_depth)
17749 ++level;
17750 break;
17752 case CPP_RSHIFT:
17753 if (cxx_dialect == cxx98)
17754 /* C++0x views the `>>' operator as two `>' tokens, but
17755 C++98 does not. */
17756 break;
17757 else if (!nesting_depth && level-- == 0)
17759 /* We've hit a `>>' where the first `>' closes the
17760 template argument list, and the second `>' is
17761 spurious. Just consume the `>>' and stop; we've
17762 already produced at least one error. */
17763 cp_lexer_consume_token (parser->lexer);
17764 return;
17766 /* Fall through for C++0x, so we handle the second `>' in
17767 the `>>'. */
17769 case CPP_GREATER:
17770 if (!nesting_depth && level-- == 0)
17772 /* We've reached the token we want, consume it and stop. */
17773 cp_lexer_consume_token (parser->lexer);
17774 return;
17776 break;
17778 case CPP_OPEN_PAREN:
17779 case CPP_OPEN_SQUARE:
17780 ++nesting_depth;
17781 break;
17783 case CPP_CLOSE_PAREN:
17784 case CPP_CLOSE_SQUARE:
17785 if (nesting_depth-- == 0)
17786 return;
17787 break;
17789 case CPP_EOF:
17790 case CPP_PRAGMA_EOL:
17791 case CPP_SEMICOLON:
17792 case CPP_OPEN_BRACE:
17793 case CPP_CLOSE_BRACE:
17794 /* The '>' was probably forgotten, don't look further. */
17795 return;
17797 default:
17798 break;
17801 /* Consume this token. */
17802 cp_lexer_consume_token (parser->lexer);
17806 /* If the next token is the indicated keyword, consume it. Otherwise,
17807 issue an error message indicating that TOKEN_DESC was expected.
17809 Returns the token consumed, if the token had the appropriate type.
17810 Otherwise, returns NULL. */
17812 static cp_token *
17813 cp_parser_require_keyword (cp_parser* parser,
17814 enum rid keyword,
17815 const char* token_desc)
17817 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17819 if (token && token->keyword != keyword)
17821 dyn_string_t error_msg;
17823 /* Format the error message. */
17824 error_msg = dyn_string_new (0);
17825 dyn_string_append_cstr (error_msg, "expected ");
17826 dyn_string_append_cstr (error_msg, token_desc);
17827 cp_parser_error (parser, error_msg->s);
17828 dyn_string_delete (error_msg);
17829 return NULL;
17832 return token;
17835 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17836 function-definition. */
17838 static bool
17839 cp_parser_token_starts_function_definition_p (cp_token* token)
17841 return (/* An ordinary function-body begins with an `{'. */
17842 token->type == CPP_OPEN_BRACE
17843 /* A ctor-initializer begins with a `:'. */
17844 || token->type == CPP_COLON
17845 /* A function-try-block begins with `try'. */
17846 || token->keyword == RID_TRY
17847 /* The named return value extension begins with `return'. */
17848 || token->keyword == RID_RETURN);
17851 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17852 definition. */
17854 static bool
17855 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17857 cp_token *token;
17859 token = cp_lexer_peek_token (parser->lexer);
17860 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17863 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17864 C++0x) ending a template-argument. */
17866 static bool
17867 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17869 cp_token *token;
17871 token = cp_lexer_peek_token (parser->lexer);
17872 return (token->type == CPP_COMMA
17873 || token->type == CPP_GREATER
17874 || token->type == CPP_ELLIPSIS
17875 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17878 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17879 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17881 static bool
17882 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17883 size_t n)
17885 cp_token *token;
17887 token = cp_lexer_peek_nth_token (parser->lexer, n);
17888 if (token->type == CPP_LESS)
17889 return true;
17890 /* Check for the sequence `<::' in the original code. It would be lexed as
17891 `[:', where `[' is a digraph, and there is no whitespace before
17892 `:'. */
17893 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17895 cp_token *token2;
17896 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17897 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17898 return true;
17900 return false;
17903 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17904 or none_type otherwise. */
17906 static enum tag_types
17907 cp_parser_token_is_class_key (cp_token* token)
17909 switch (token->keyword)
17911 case RID_CLASS:
17912 return class_type;
17913 case RID_STRUCT:
17914 return record_type;
17915 case RID_UNION:
17916 return union_type;
17918 default:
17919 return none_type;
17923 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17925 static void
17926 cp_parser_check_class_key (enum tag_types class_key, tree type)
17928 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17929 pedwarn ("%qs tag used in naming %q#T",
17930 class_key == union_type ? "union"
17931 : class_key == record_type ? "struct" : "class",
17932 type);
17935 /* Issue an error message if DECL is redeclared with different
17936 access than its original declaration [class.access.spec/3].
17937 This applies to nested classes and nested class templates.
17938 [class.mem/1]. */
17940 static void
17941 cp_parser_check_access_in_redeclaration (tree decl)
17943 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
17944 return;
17946 if ((TREE_PRIVATE (decl)
17947 != (current_access_specifier == access_private_node))
17948 || (TREE_PROTECTED (decl)
17949 != (current_access_specifier == access_protected_node)))
17950 error ("%qD redeclared with different access", decl);
17953 /* Look for the `template' keyword, as a syntactic disambiguator.
17954 Return TRUE iff it is present, in which case it will be
17955 consumed. */
17957 static bool
17958 cp_parser_optional_template_keyword (cp_parser *parser)
17960 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17962 /* The `template' keyword can only be used within templates;
17963 outside templates the parser can always figure out what is a
17964 template and what is not. */
17965 if (!processing_template_decl)
17967 error ("%<template%> (as a disambiguator) is only allowed "
17968 "within templates");
17969 /* If this part of the token stream is rescanned, the same
17970 error message would be generated. So, we purge the token
17971 from the stream. */
17972 cp_lexer_purge_token (parser->lexer);
17973 return false;
17975 else
17977 /* Consume the `template' keyword. */
17978 cp_lexer_consume_token (parser->lexer);
17979 return true;
17983 return false;
17986 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17987 set PARSER->SCOPE, and perform other related actions. */
17989 static void
17990 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17992 int i;
17993 struct tree_check *check_value;
17994 deferred_access_check *chk;
17995 VEC (deferred_access_check,gc) *checks;
17997 /* Get the stored value. */
17998 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17999 /* Perform any access checks that were deferred. */
18000 checks = check_value->checks;
18001 if (checks)
18003 for (i = 0 ;
18004 VEC_iterate (deferred_access_check, checks, i, chk) ;
18005 ++i)
18007 perform_or_defer_access_check (chk->binfo,
18008 chk->decl,
18009 chk->diag_decl);
18012 /* Set the scope from the stored value. */
18013 parser->scope = check_value->value;
18014 parser->qualifying_scope = check_value->qualifying_scope;
18015 parser->object_scope = NULL_TREE;
18018 /* Consume tokens up through a non-nested END token. */
18020 static void
18021 cp_parser_cache_group (cp_parser *parser,
18022 enum cpp_ttype end,
18023 unsigned depth)
18025 while (true)
18027 cp_token *token;
18029 /* Abort a parenthesized expression if we encounter a brace. */
18030 if ((end == CPP_CLOSE_PAREN || depth == 0)
18031 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18032 return;
18033 /* If we've reached the end of the file, stop. */
18034 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
18035 || (end != CPP_PRAGMA_EOL
18036 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
18037 return;
18038 /* Consume the next token. */
18039 token = cp_lexer_consume_token (parser->lexer);
18040 /* See if it starts a new group. */
18041 if (token->type == CPP_OPEN_BRACE)
18043 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18044 if (depth == 0)
18045 return;
18047 else if (token->type == CPP_OPEN_PAREN)
18048 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18049 else if (token->type == CPP_PRAGMA)
18050 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18051 else if (token->type == end)
18052 return;
18056 /* Begin parsing tentatively. We always save tokens while parsing
18057 tentatively so that if the tentative parsing fails we can restore the
18058 tokens. */
18060 static void
18061 cp_parser_parse_tentatively (cp_parser* parser)
18063 /* Enter a new parsing context. */
18064 parser->context = cp_parser_context_new (parser->context);
18065 /* Begin saving tokens. */
18066 cp_lexer_save_tokens (parser->lexer);
18067 /* In order to avoid repetitive access control error messages,
18068 access checks are queued up until we are no longer parsing
18069 tentatively. */
18070 push_deferring_access_checks (dk_deferred);
18073 /* Commit to the currently active tentative parse. */
18075 static void
18076 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18078 cp_parser_context *context;
18079 cp_lexer *lexer;
18081 /* Mark all of the levels as committed. */
18082 lexer = parser->lexer;
18083 for (context = parser->context; context->next; context = context->next)
18085 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18086 break;
18087 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18088 while (!cp_lexer_saving_tokens (lexer))
18089 lexer = lexer->next;
18090 cp_lexer_commit_tokens (lexer);
18094 /* Abort the currently active tentative parse. All consumed tokens
18095 will be rolled back, and no diagnostics will be issued. */
18097 static void
18098 cp_parser_abort_tentative_parse (cp_parser* parser)
18100 cp_parser_simulate_error (parser);
18101 /* Now, pretend that we want to see if the construct was
18102 successfully parsed. */
18103 cp_parser_parse_definitely (parser);
18106 /* Stop parsing tentatively. If a parse error has occurred, restore the
18107 token stream. Otherwise, commit to the tokens we have consumed.
18108 Returns true if no error occurred; false otherwise. */
18110 static bool
18111 cp_parser_parse_definitely (cp_parser* parser)
18113 bool error_occurred;
18114 cp_parser_context *context;
18116 /* Remember whether or not an error occurred, since we are about to
18117 destroy that information. */
18118 error_occurred = cp_parser_error_occurred (parser);
18119 /* Remove the topmost context from the stack. */
18120 context = parser->context;
18121 parser->context = context->next;
18122 /* If no parse errors occurred, commit to the tentative parse. */
18123 if (!error_occurred)
18125 /* Commit to the tokens read tentatively, unless that was
18126 already done. */
18127 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18128 cp_lexer_commit_tokens (parser->lexer);
18130 pop_to_parent_deferring_access_checks ();
18132 /* Otherwise, if errors occurred, roll back our state so that things
18133 are just as they were before we began the tentative parse. */
18134 else
18136 cp_lexer_rollback_tokens (parser->lexer);
18137 pop_deferring_access_checks ();
18139 /* Add the context to the front of the free list. */
18140 context->next = cp_parser_context_free_list;
18141 cp_parser_context_free_list = context;
18143 return !error_occurred;
18146 /* Returns true if we are parsing tentatively and are not committed to
18147 this tentative parse. */
18149 static bool
18150 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18152 return (cp_parser_parsing_tentatively (parser)
18153 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18156 /* Returns nonzero iff an error has occurred during the most recent
18157 tentative parse. */
18159 static bool
18160 cp_parser_error_occurred (cp_parser* parser)
18162 return (cp_parser_parsing_tentatively (parser)
18163 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18166 /* Returns nonzero if GNU extensions are allowed. */
18168 static bool
18169 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18171 return parser->allow_gnu_extensions_p;
18174 /* Objective-C++ Productions */
18177 /* Parse an Objective-C expression, which feeds into a primary-expression
18178 above.
18180 objc-expression:
18181 objc-message-expression
18182 objc-string-literal
18183 objc-encode-expression
18184 objc-protocol-expression
18185 objc-selector-expression
18187 Returns a tree representation of the expression. */
18189 static tree
18190 cp_parser_objc_expression (cp_parser* parser)
18192 /* Try to figure out what kind of declaration is present. */
18193 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18195 switch (kwd->type)
18197 case CPP_OPEN_SQUARE:
18198 return cp_parser_objc_message_expression (parser);
18200 case CPP_OBJC_STRING:
18201 kwd = cp_lexer_consume_token (parser->lexer);
18202 return objc_build_string_object (kwd->u.value);
18204 case CPP_KEYWORD:
18205 switch (kwd->keyword)
18207 case RID_AT_ENCODE:
18208 return cp_parser_objc_encode_expression (parser);
18210 case RID_AT_PROTOCOL:
18211 return cp_parser_objc_protocol_expression (parser);
18213 case RID_AT_SELECTOR:
18214 return cp_parser_objc_selector_expression (parser);
18216 default:
18217 break;
18219 default:
18220 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18221 cp_parser_skip_to_end_of_block_or_statement (parser);
18224 return error_mark_node;
18227 /* Parse an Objective-C message expression.
18229 objc-message-expression:
18230 [ objc-message-receiver objc-message-args ]
18232 Returns a representation of an Objective-C message. */
18234 static tree
18235 cp_parser_objc_message_expression (cp_parser* parser)
18237 tree receiver, messageargs;
18239 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18240 receiver = cp_parser_objc_message_receiver (parser);
18241 messageargs = cp_parser_objc_message_args (parser);
18242 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18244 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18247 /* Parse an objc-message-receiver.
18249 objc-message-receiver:
18250 expression
18251 simple-type-specifier
18253 Returns a representation of the type or expression. */
18255 static tree
18256 cp_parser_objc_message_receiver (cp_parser* parser)
18258 tree rcv;
18260 /* An Objective-C message receiver may be either (1) a type
18261 or (2) an expression. */
18262 cp_parser_parse_tentatively (parser);
18263 rcv = cp_parser_expression (parser, false);
18265 if (cp_parser_parse_definitely (parser))
18266 return rcv;
18268 rcv = cp_parser_simple_type_specifier (parser,
18269 /*decl_specs=*/NULL,
18270 CP_PARSER_FLAGS_NONE);
18272 return objc_get_class_reference (rcv);
18275 /* Parse the arguments and selectors comprising an Objective-C message.
18277 objc-message-args:
18278 objc-selector
18279 objc-selector-args
18280 objc-selector-args , objc-comma-args
18282 objc-selector-args:
18283 objc-selector [opt] : assignment-expression
18284 objc-selector-args objc-selector [opt] : assignment-expression
18286 objc-comma-args:
18287 assignment-expression
18288 objc-comma-args , assignment-expression
18290 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18291 selector arguments and TREE_VALUE containing a list of comma
18292 arguments. */
18294 static tree
18295 cp_parser_objc_message_args (cp_parser* parser)
18297 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18298 bool maybe_unary_selector_p = true;
18299 cp_token *token = cp_lexer_peek_token (parser->lexer);
18301 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18303 tree selector = NULL_TREE, arg;
18305 if (token->type != CPP_COLON)
18306 selector = cp_parser_objc_selector (parser);
18308 /* Detect if we have a unary selector. */
18309 if (maybe_unary_selector_p
18310 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18311 return build_tree_list (selector, NULL_TREE);
18313 maybe_unary_selector_p = false;
18314 cp_parser_require (parser, CPP_COLON, "`:'");
18315 arg = cp_parser_assignment_expression (parser, false);
18317 sel_args
18318 = chainon (sel_args,
18319 build_tree_list (selector, arg));
18321 token = cp_lexer_peek_token (parser->lexer);
18324 /* Handle non-selector arguments, if any. */
18325 while (token->type == CPP_COMMA)
18327 tree arg;
18329 cp_lexer_consume_token (parser->lexer);
18330 arg = cp_parser_assignment_expression (parser, false);
18332 addl_args
18333 = chainon (addl_args,
18334 build_tree_list (NULL_TREE, arg));
18336 token = cp_lexer_peek_token (parser->lexer);
18339 return build_tree_list (sel_args, addl_args);
18342 /* Parse an Objective-C encode expression.
18344 objc-encode-expression:
18345 @encode objc-typename
18347 Returns an encoded representation of the type argument. */
18349 static tree
18350 cp_parser_objc_encode_expression (cp_parser* parser)
18352 tree type;
18354 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
18355 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18356 type = complete_type (cp_parser_type_id (parser));
18357 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18359 if (!type)
18361 error ("%<@encode%> must specify a type as an argument");
18362 return error_mark_node;
18365 return objc_build_encode_expr (type);
18368 /* Parse an Objective-C @defs expression. */
18370 static tree
18371 cp_parser_objc_defs_expression (cp_parser *parser)
18373 tree name;
18375 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18376 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18377 name = cp_parser_identifier (parser);
18378 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18380 return objc_get_class_ivars (name);
18383 /* Parse an Objective-C protocol expression.
18385 objc-protocol-expression:
18386 @protocol ( identifier )
18388 Returns a representation of the protocol expression. */
18390 static tree
18391 cp_parser_objc_protocol_expression (cp_parser* parser)
18393 tree proto;
18395 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18396 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18397 proto = cp_parser_identifier (parser);
18398 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18400 return objc_build_protocol_expr (proto);
18403 /* Parse an Objective-C selector expression.
18405 objc-selector-expression:
18406 @selector ( objc-method-signature )
18408 objc-method-signature:
18409 objc-selector
18410 objc-selector-seq
18412 objc-selector-seq:
18413 objc-selector :
18414 objc-selector-seq objc-selector :
18416 Returns a representation of the method selector. */
18418 static tree
18419 cp_parser_objc_selector_expression (cp_parser* parser)
18421 tree sel_seq = NULL_TREE;
18422 bool maybe_unary_selector_p = true;
18423 cp_token *token;
18425 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18426 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18427 token = cp_lexer_peek_token (parser->lexer);
18429 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18430 || token->type == CPP_SCOPE)
18432 tree selector = NULL_TREE;
18434 if (token->type != CPP_COLON
18435 || token->type == CPP_SCOPE)
18436 selector = cp_parser_objc_selector (parser);
18438 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18439 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18441 /* Detect if we have a unary selector. */
18442 if (maybe_unary_selector_p)
18444 sel_seq = selector;
18445 goto finish_selector;
18447 else
18449 cp_parser_error (parser, "expected %<:%>");
18452 maybe_unary_selector_p = false;
18453 token = cp_lexer_consume_token (parser->lexer);
18455 if (token->type == CPP_SCOPE)
18457 sel_seq
18458 = chainon (sel_seq,
18459 build_tree_list (selector, NULL_TREE));
18460 sel_seq
18461 = chainon (sel_seq,
18462 build_tree_list (NULL_TREE, NULL_TREE));
18464 else
18465 sel_seq
18466 = chainon (sel_seq,
18467 build_tree_list (selector, NULL_TREE));
18469 token = cp_lexer_peek_token (parser->lexer);
18472 finish_selector:
18473 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18475 return objc_build_selector_expr (sel_seq);
18478 /* Parse a list of identifiers.
18480 objc-identifier-list:
18481 identifier
18482 objc-identifier-list , identifier
18484 Returns a TREE_LIST of identifier nodes. */
18486 static tree
18487 cp_parser_objc_identifier_list (cp_parser* parser)
18489 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18490 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18492 while (sep->type == CPP_COMMA)
18494 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18495 list = chainon (list,
18496 build_tree_list (NULL_TREE,
18497 cp_parser_identifier (parser)));
18498 sep = cp_lexer_peek_token (parser->lexer);
18501 return list;
18504 /* Parse an Objective-C alias declaration.
18506 objc-alias-declaration:
18507 @compatibility_alias identifier identifier ;
18509 This function registers the alias mapping with the Objective-C front end.
18510 It returns nothing. */
18512 static void
18513 cp_parser_objc_alias_declaration (cp_parser* parser)
18515 tree alias, orig;
18517 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18518 alias = cp_parser_identifier (parser);
18519 orig = cp_parser_identifier (parser);
18520 objc_declare_alias (alias, orig);
18521 cp_parser_consume_semicolon_at_end_of_statement (parser);
18524 /* Parse an Objective-C class forward-declaration.
18526 objc-class-declaration:
18527 @class objc-identifier-list ;
18529 The function registers the forward declarations with the Objective-C
18530 front end. It returns nothing. */
18532 static void
18533 cp_parser_objc_class_declaration (cp_parser* parser)
18535 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18536 objc_declare_class (cp_parser_objc_identifier_list (parser));
18537 cp_parser_consume_semicolon_at_end_of_statement (parser);
18540 /* Parse a list of Objective-C protocol references.
18542 objc-protocol-refs-opt:
18543 objc-protocol-refs [opt]
18545 objc-protocol-refs:
18546 < objc-identifier-list >
18548 Returns a TREE_LIST of identifiers, if any. */
18550 static tree
18551 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18553 tree protorefs = NULL_TREE;
18555 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18557 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18558 protorefs = cp_parser_objc_identifier_list (parser);
18559 cp_parser_require (parser, CPP_GREATER, "`>'");
18562 return protorefs;
18565 /* Parse a Objective-C visibility specification. */
18567 static void
18568 cp_parser_objc_visibility_spec (cp_parser* parser)
18570 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18572 switch (vis->keyword)
18574 case RID_AT_PRIVATE:
18575 objc_set_visibility (2);
18576 break;
18577 case RID_AT_PROTECTED:
18578 objc_set_visibility (0);
18579 break;
18580 case RID_AT_PUBLIC:
18581 objc_set_visibility (1);
18582 break;
18583 default:
18584 return;
18587 /* Eat '@private'/'@protected'/'@public'. */
18588 cp_lexer_consume_token (parser->lexer);
18591 /* Parse an Objective-C method type. */
18593 static void
18594 cp_parser_objc_method_type (cp_parser* parser)
18596 objc_set_method_type
18597 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18598 ? PLUS_EXPR
18599 : MINUS_EXPR);
18602 /* Parse an Objective-C protocol qualifier. */
18604 static tree
18605 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18607 tree quals = NULL_TREE, node;
18608 cp_token *token = cp_lexer_peek_token (parser->lexer);
18610 node = token->u.value;
18612 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18613 && (node == ridpointers [(int) RID_IN]
18614 || node == ridpointers [(int) RID_OUT]
18615 || node == ridpointers [(int) RID_INOUT]
18616 || node == ridpointers [(int) RID_BYCOPY]
18617 || node == ridpointers [(int) RID_BYREF]
18618 || node == ridpointers [(int) RID_ONEWAY]))
18620 quals = tree_cons (NULL_TREE, node, quals);
18621 cp_lexer_consume_token (parser->lexer);
18622 token = cp_lexer_peek_token (parser->lexer);
18623 node = token->u.value;
18626 return quals;
18629 /* Parse an Objective-C typename. */
18631 static tree
18632 cp_parser_objc_typename (cp_parser* parser)
18634 tree typename = NULL_TREE;
18636 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18638 tree proto_quals, cp_type = NULL_TREE;
18640 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18641 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18643 /* An ObjC type name may consist of just protocol qualifiers, in which
18644 case the type shall default to 'id'. */
18645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18646 cp_type = cp_parser_type_id (parser);
18648 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18649 typename = build_tree_list (proto_quals, cp_type);
18652 return typename;
18655 /* Check to see if TYPE refers to an Objective-C selector name. */
18657 static bool
18658 cp_parser_objc_selector_p (enum cpp_ttype type)
18660 return (type == CPP_NAME || type == CPP_KEYWORD
18661 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18662 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18663 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18664 || type == CPP_XOR || type == CPP_XOR_EQ);
18667 /* Parse an Objective-C selector. */
18669 static tree
18670 cp_parser_objc_selector (cp_parser* parser)
18672 cp_token *token = cp_lexer_consume_token (parser->lexer);
18674 if (!cp_parser_objc_selector_p (token->type))
18676 error ("invalid Objective-C++ selector name");
18677 return error_mark_node;
18680 /* C++ operator names are allowed to appear in ObjC selectors. */
18681 switch (token->type)
18683 case CPP_AND_AND: return get_identifier ("and");
18684 case CPP_AND_EQ: return get_identifier ("and_eq");
18685 case CPP_AND: return get_identifier ("bitand");
18686 case CPP_OR: return get_identifier ("bitor");
18687 case CPP_COMPL: return get_identifier ("compl");
18688 case CPP_NOT: return get_identifier ("not");
18689 case CPP_NOT_EQ: return get_identifier ("not_eq");
18690 case CPP_OR_OR: return get_identifier ("or");
18691 case CPP_OR_EQ: return get_identifier ("or_eq");
18692 case CPP_XOR: return get_identifier ("xor");
18693 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18694 default: return token->u.value;
18698 /* Parse an Objective-C params list. */
18700 static tree
18701 cp_parser_objc_method_keyword_params (cp_parser* parser)
18703 tree params = NULL_TREE;
18704 bool maybe_unary_selector_p = true;
18705 cp_token *token = cp_lexer_peek_token (parser->lexer);
18707 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18709 tree selector = NULL_TREE, typename, identifier;
18711 if (token->type != CPP_COLON)
18712 selector = cp_parser_objc_selector (parser);
18714 /* Detect if we have a unary selector. */
18715 if (maybe_unary_selector_p
18716 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18717 return selector;
18719 maybe_unary_selector_p = false;
18720 cp_parser_require (parser, CPP_COLON, "`:'");
18721 typename = cp_parser_objc_typename (parser);
18722 identifier = cp_parser_identifier (parser);
18724 params
18725 = chainon (params,
18726 objc_build_keyword_decl (selector,
18727 typename,
18728 identifier));
18730 token = cp_lexer_peek_token (parser->lexer);
18733 return params;
18736 /* Parse the non-keyword Objective-C params. */
18738 static tree
18739 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18741 tree params = make_node (TREE_LIST);
18742 cp_token *token = cp_lexer_peek_token (parser->lexer);
18743 *ellipsisp = false; /* Initially, assume no ellipsis. */
18745 while (token->type == CPP_COMMA)
18747 cp_parameter_declarator *parmdecl;
18748 tree parm;
18750 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18751 token = cp_lexer_peek_token (parser->lexer);
18753 if (token->type == CPP_ELLIPSIS)
18755 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18756 *ellipsisp = true;
18757 break;
18760 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18761 parm = grokdeclarator (parmdecl->declarator,
18762 &parmdecl->decl_specifiers,
18763 PARM, /*initialized=*/0,
18764 /*attrlist=*/NULL);
18766 chainon (params, build_tree_list (NULL_TREE, parm));
18767 token = cp_lexer_peek_token (parser->lexer);
18770 return params;
18773 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18775 static void
18776 cp_parser_objc_interstitial_code (cp_parser* parser)
18778 cp_token *token = cp_lexer_peek_token (parser->lexer);
18780 /* If the next token is `extern' and the following token is a string
18781 literal, then we have a linkage specification. */
18782 if (token->keyword == RID_EXTERN
18783 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18784 cp_parser_linkage_specification (parser);
18785 /* Handle #pragma, if any. */
18786 else if (token->type == CPP_PRAGMA)
18787 cp_parser_pragma (parser, pragma_external);
18788 /* Allow stray semicolons. */
18789 else if (token->type == CPP_SEMICOLON)
18790 cp_lexer_consume_token (parser->lexer);
18791 /* Finally, try to parse a block-declaration, or a function-definition. */
18792 else
18793 cp_parser_block_declaration (parser, /*statement_p=*/false);
18796 /* Parse a method signature. */
18798 static tree
18799 cp_parser_objc_method_signature (cp_parser* parser)
18801 tree rettype, kwdparms, optparms;
18802 bool ellipsis = false;
18804 cp_parser_objc_method_type (parser);
18805 rettype = cp_parser_objc_typename (parser);
18806 kwdparms = cp_parser_objc_method_keyword_params (parser);
18807 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18809 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18812 /* Pars an Objective-C method prototype list. */
18814 static void
18815 cp_parser_objc_method_prototype_list (cp_parser* parser)
18817 cp_token *token = cp_lexer_peek_token (parser->lexer);
18819 while (token->keyword != RID_AT_END)
18821 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18823 objc_add_method_declaration
18824 (cp_parser_objc_method_signature (parser));
18825 cp_parser_consume_semicolon_at_end_of_statement (parser);
18827 else
18828 /* Allow for interspersed non-ObjC++ code. */
18829 cp_parser_objc_interstitial_code (parser);
18831 token = cp_lexer_peek_token (parser->lexer);
18834 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18835 objc_finish_interface ();
18838 /* Parse an Objective-C method definition list. */
18840 static void
18841 cp_parser_objc_method_definition_list (cp_parser* parser)
18843 cp_token *token = cp_lexer_peek_token (parser->lexer);
18845 while (token->keyword != RID_AT_END)
18847 tree meth;
18849 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18851 push_deferring_access_checks (dk_deferred);
18852 objc_start_method_definition
18853 (cp_parser_objc_method_signature (parser));
18855 /* For historical reasons, we accept an optional semicolon. */
18856 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18857 cp_lexer_consume_token (parser->lexer);
18859 perform_deferred_access_checks ();
18860 stop_deferring_access_checks ();
18861 meth = cp_parser_function_definition_after_declarator (parser,
18862 false);
18863 pop_deferring_access_checks ();
18864 objc_finish_method_definition (meth);
18866 else
18867 /* Allow for interspersed non-ObjC++ code. */
18868 cp_parser_objc_interstitial_code (parser);
18870 token = cp_lexer_peek_token (parser->lexer);
18873 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18874 objc_finish_implementation ();
18877 /* Parse Objective-C ivars. */
18879 static void
18880 cp_parser_objc_class_ivars (cp_parser* parser)
18882 cp_token *token = cp_lexer_peek_token (parser->lexer);
18884 if (token->type != CPP_OPEN_BRACE)
18885 return; /* No ivars specified. */
18887 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18888 token = cp_lexer_peek_token (parser->lexer);
18890 while (token->type != CPP_CLOSE_BRACE)
18892 cp_decl_specifier_seq declspecs;
18893 int decl_class_or_enum_p;
18894 tree prefix_attributes;
18896 cp_parser_objc_visibility_spec (parser);
18898 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18899 break;
18901 cp_parser_decl_specifier_seq (parser,
18902 CP_PARSER_FLAGS_OPTIONAL,
18903 &declspecs,
18904 &decl_class_or_enum_p);
18905 prefix_attributes = declspecs.attributes;
18906 declspecs.attributes = NULL_TREE;
18908 /* Keep going until we hit the `;' at the end of the
18909 declaration. */
18910 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18912 tree width = NULL_TREE, attributes, first_attribute, decl;
18913 cp_declarator *declarator = NULL;
18914 int ctor_dtor_or_conv_p;
18916 /* Check for a (possibly unnamed) bitfield declaration. */
18917 token = cp_lexer_peek_token (parser->lexer);
18918 if (token->type == CPP_COLON)
18919 goto eat_colon;
18921 if (token->type == CPP_NAME
18922 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18923 == CPP_COLON))
18925 /* Get the name of the bitfield. */
18926 declarator = make_id_declarator (NULL_TREE,
18927 cp_parser_identifier (parser),
18928 sfk_none);
18930 eat_colon:
18931 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18932 /* Get the width of the bitfield. */
18933 width
18934 = cp_parser_constant_expression (parser,
18935 /*allow_non_constant=*/false,
18936 NULL);
18938 else
18940 /* Parse the declarator. */
18941 declarator
18942 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18943 &ctor_dtor_or_conv_p,
18944 /*parenthesized_p=*/NULL,
18945 /*member_p=*/false);
18948 /* Look for attributes that apply to the ivar. */
18949 attributes = cp_parser_attributes_opt (parser);
18950 /* Remember which attributes are prefix attributes and
18951 which are not. */
18952 first_attribute = attributes;
18953 /* Combine the attributes. */
18954 attributes = chainon (prefix_attributes, attributes);
18956 if (width)
18958 /* Create the bitfield declaration. */
18959 decl = grokbitfield (declarator, &declspecs, width);
18960 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18962 else
18963 decl = grokfield (declarator, &declspecs,
18964 NULL_TREE, /*init_const_expr_p=*/false,
18965 NULL_TREE, attributes);
18967 /* Add the instance variable. */
18968 objc_add_instance_variable (decl);
18970 /* Reset PREFIX_ATTRIBUTES. */
18971 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18972 attributes = TREE_CHAIN (attributes);
18973 if (attributes)
18974 TREE_CHAIN (attributes) = NULL_TREE;
18976 token = cp_lexer_peek_token (parser->lexer);
18978 if (token->type == CPP_COMMA)
18980 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18981 continue;
18983 break;
18986 cp_parser_consume_semicolon_at_end_of_statement (parser);
18987 token = cp_lexer_peek_token (parser->lexer);
18990 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18991 /* For historical reasons, we accept an optional semicolon. */
18992 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18993 cp_lexer_consume_token (parser->lexer);
18996 /* Parse an Objective-C protocol declaration. */
18998 static void
18999 cp_parser_objc_protocol_declaration (cp_parser* parser)
19001 tree proto, protorefs;
19002 cp_token *tok;
19004 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19007 error ("identifier expected after %<@protocol%>");
19008 goto finish;
19011 /* See if we have a forward declaration or a definition. */
19012 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19014 /* Try a forward declaration first. */
19015 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19017 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19018 finish:
19019 cp_parser_consume_semicolon_at_end_of_statement (parser);
19022 /* Ok, we got a full-fledged definition (or at least should). */
19023 else
19025 proto = cp_parser_identifier (parser);
19026 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19027 objc_start_protocol (proto, protorefs);
19028 cp_parser_objc_method_prototype_list (parser);
19032 /* Parse an Objective-C superclass or category. */
19034 static void
19035 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19036 tree *categ)
19038 cp_token *next = cp_lexer_peek_token (parser->lexer);
19040 *super = *categ = NULL_TREE;
19041 if (next->type == CPP_COLON)
19043 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19044 *super = cp_parser_identifier (parser);
19046 else if (next->type == CPP_OPEN_PAREN)
19048 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19049 *categ = cp_parser_identifier (parser);
19050 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19054 /* Parse an Objective-C class interface. */
19056 static void
19057 cp_parser_objc_class_interface (cp_parser* parser)
19059 tree name, super, categ, protos;
19061 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19062 name = cp_parser_identifier (parser);
19063 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19064 protos = cp_parser_objc_protocol_refs_opt (parser);
19066 /* We have either a class or a category on our hands. */
19067 if (categ)
19068 objc_start_category_interface (name, categ, protos);
19069 else
19071 objc_start_class_interface (name, super, protos);
19072 /* Handle instance variable declarations, if any. */
19073 cp_parser_objc_class_ivars (parser);
19074 objc_continue_interface ();
19077 cp_parser_objc_method_prototype_list (parser);
19080 /* Parse an Objective-C class implementation. */
19082 static void
19083 cp_parser_objc_class_implementation (cp_parser* parser)
19085 tree name, super, categ;
19087 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19088 name = cp_parser_identifier (parser);
19089 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19091 /* We have either a class or a category on our hands. */
19092 if (categ)
19093 objc_start_category_implementation (name, categ);
19094 else
19096 objc_start_class_implementation (name, super);
19097 /* Handle instance variable declarations, if any. */
19098 cp_parser_objc_class_ivars (parser);
19099 objc_continue_implementation ();
19102 cp_parser_objc_method_definition_list (parser);
19105 /* Consume the @end token and finish off the implementation. */
19107 static void
19108 cp_parser_objc_end_implementation (cp_parser* parser)
19110 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19111 objc_finish_implementation ();
19114 /* Parse an Objective-C declaration. */
19116 static void
19117 cp_parser_objc_declaration (cp_parser* parser)
19119 /* Try to figure out what kind of declaration is present. */
19120 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19122 switch (kwd->keyword)
19124 case RID_AT_ALIAS:
19125 cp_parser_objc_alias_declaration (parser);
19126 break;
19127 case RID_AT_CLASS:
19128 cp_parser_objc_class_declaration (parser);
19129 break;
19130 case RID_AT_PROTOCOL:
19131 cp_parser_objc_protocol_declaration (parser);
19132 break;
19133 case RID_AT_INTERFACE:
19134 cp_parser_objc_class_interface (parser);
19135 break;
19136 case RID_AT_IMPLEMENTATION:
19137 cp_parser_objc_class_implementation (parser);
19138 break;
19139 case RID_AT_END:
19140 cp_parser_objc_end_implementation (parser);
19141 break;
19142 default:
19143 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19144 cp_parser_skip_to_end_of_block_or_statement (parser);
19148 /* Parse an Objective-C try-catch-finally statement.
19150 objc-try-catch-finally-stmt:
19151 @try compound-statement objc-catch-clause-seq [opt]
19152 objc-finally-clause [opt]
19154 objc-catch-clause-seq:
19155 objc-catch-clause objc-catch-clause-seq [opt]
19157 objc-catch-clause:
19158 @catch ( exception-declaration ) compound-statement
19160 objc-finally-clause
19161 @finally compound-statement
19163 Returns NULL_TREE. */
19165 static tree
19166 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19167 location_t location;
19168 tree stmt;
19170 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19171 location = cp_lexer_peek_token (parser->lexer)->location;
19172 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19173 node, lest it get absorbed into the surrounding block. */
19174 stmt = push_stmt_list ();
19175 cp_parser_compound_statement (parser, NULL, false);
19176 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19178 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19180 cp_parameter_declarator *parmdecl;
19181 tree parm;
19183 cp_lexer_consume_token (parser->lexer);
19184 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19185 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19186 parm = grokdeclarator (parmdecl->declarator,
19187 &parmdecl->decl_specifiers,
19188 PARM, /*initialized=*/0,
19189 /*attrlist=*/NULL);
19190 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19191 objc_begin_catch_clause (parm);
19192 cp_parser_compound_statement (parser, NULL, false);
19193 objc_finish_catch_clause ();
19196 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19198 cp_lexer_consume_token (parser->lexer);
19199 location = cp_lexer_peek_token (parser->lexer)->location;
19200 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19201 node, lest it get absorbed into the surrounding block. */
19202 stmt = push_stmt_list ();
19203 cp_parser_compound_statement (parser, NULL, false);
19204 objc_build_finally_clause (location, pop_stmt_list (stmt));
19207 return objc_finish_try_stmt ();
19210 /* Parse an Objective-C synchronized statement.
19212 objc-synchronized-stmt:
19213 @synchronized ( expression ) compound-statement
19215 Returns NULL_TREE. */
19217 static tree
19218 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19219 location_t location;
19220 tree lock, stmt;
19222 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19224 location = cp_lexer_peek_token (parser->lexer)->location;
19225 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19226 lock = cp_parser_expression (parser, false);
19227 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19229 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19230 node, lest it get absorbed into the surrounding block. */
19231 stmt = push_stmt_list ();
19232 cp_parser_compound_statement (parser, NULL, false);
19234 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19237 /* Parse an Objective-C throw statement.
19239 objc-throw-stmt:
19240 @throw assignment-expression [opt] ;
19242 Returns a constructed '@throw' statement. */
19244 static tree
19245 cp_parser_objc_throw_statement (cp_parser *parser) {
19246 tree expr = NULL_TREE;
19248 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19251 expr = cp_parser_assignment_expression (parser, false);
19253 cp_parser_consume_semicolon_at_end_of_statement (parser);
19255 return objc_build_throw_stmt (expr);
19258 /* Parse an Objective-C statement. */
19260 static tree
19261 cp_parser_objc_statement (cp_parser * parser) {
19262 /* Try to figure out what kind of declaration is present. */
19263 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19265 switch (kwd->keyword)
19267 case RID_AT_TRY:
19268 return cp_parser_objc_try_catch_finally_statement (parser);
19269 case RID_AT_SYNCHRONIZED:
19270 return cp_parser_objc_synchronized_statement (parser);
19271 case RID_AT_THROW:
19272 return cp_parser_objc_throw_statement (parser);
19273 default:
19274 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19275 cp_parser_skip_to_end_of_block_or_statement (parser);
19278 return error_mark_node;
19281 /* OpenMP 2.5 parsing routines. */
19283 /* Returns name of the next clause.
19284 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19285 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19286 returned and the token is consumed. */
19288 static pragma_omp_clause
19289 cp_parser_omp_clause_name (cp_parser *parser)
19291 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19293 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19294 result = PRAGMA_OMP_CLAUSE_IF;
19295 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19296 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19297 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19298 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19299 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19301 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19302 const char *p = IDENTIFIER_POINTER (id);
19304 switch (p[0])
19306 case 'c':
19307 if (!strcmp ("copyin", p))
19308 result = PRAGMA_OMP_CLAUSE_COPYIN;
19309 else if (!strcmp ("copyprivate", p))
19310 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19311 break;
19312 case 'f':
19313 if (!strcmp ("firstprivate", p))
19314 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19315 break;
19316 case 'l':
19317 if (!strcmp ("lastprivate", p))
19318 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19319 break;
19320 case 'n':
19321 if (!strcmp ("nowait", p))
19322 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19323 else if (!strcmp ("num_threads", p))
19324 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19325 break;
19326 case 'o':
19327 if (!strcmp ("ordered", p))
19328 result = PRAGMA_OMP_CLAUSE_ORDERED;
19329 break;
19330 case 'r':
19331 if (!strcmp ("reduction", p))
19332 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19333 break;
19334 case 's':
19335 if (!strcmp ("schedule", p))
19336 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19337 else if (!strcmp ("shared", p))
19338 result = PRAGMA_OMP_CLAUSE_SHARED;
19339 break;
19343 if (result != PRAGMA_OMP_CLAUSE_NONE)
19344 cp_lexer_consume_token (parser->lexer);
19346 return result;
19349 /* Validate that a clause of the given type does not already exist. */
19351 static void
19352 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19354 tree c;
19356 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19357 if (OMP_CLAUSE_CODE (c) == code)
19359 error ("too many %qs clauses", name);
19360 break;
19364 /* OpenMP 2.5:
19365 variable-list:
19366 identifier
19367 variable-list , identifier
19369 In addition, we match a closing parenthesis. An opening parenthesis
19370 will have been consumed by the caller.
19372 If KIND is nonzero, create the appropriate node and install the decl
19373 in OMP_CLAUSE_DECL and add the node to the head of the list.
19375 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19376 return the list created. */
19378 static tree
19379 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19380 tree list)
19382 while (1)
19384 tree name, decl;
19386 name = cp_parser_id_expression (parser, /*template_p=*/false,
19387 /*check_dependency_p=*/true,
19388 /*template_p=*/NULL,
19389 /*declarator_p=*/false,
19390 /*optional_p=*/false);
19391 if (name == error_mark_node)
19392 goto skip_comma;
19394 decl = cp_parser_lookup_name_simple (parser, name);
19395 if (decl == error_mark_node)
19396 cp_parser_name_lookup_error (parser, name, decl, NULL);
19397 else if (kind != 0)
19399 tree u = build_omp_clause (kind);
19400 OMP_CLAUSE_DECL (u) = decl;
19401 OMP_CLAUSE_CHAIN (u) = list;
19402 list = u;
19404 else
19405 list = tree_cons (decl, NULL_TREE, list);
19407 get_comma:
19408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19409 break;
19410 cp_lexer_consume_token (parser->lexer);
19413 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19415 int ending;
19417 /* Try to resync to an unnested comma. Copied from
19418 cp_parser_parenthesized_expression_list. */
19419 skip_comma:
19420 ending = cp_parser_skip_to_closing_parenthesis (parser,
19421 /*recovering=*/true,
19422 /*or_comma=*/true,
19423 /*consume_paren=*/true);
19424 if (ending < 0)
19425 goto get_comma;
19428 return list;
19431 /* Similarly, but expect leading and trailing parenthesis. This is a very
19432 common case for omp clauses. */
19434 static tree
19435 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19437 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19438 return cp_parser_omp_var_list_no_open (parser, kind, list);
19439 return list;
19442 /* OpenMP 2.5:
19443 default ( shared | none ) */
19445 static tree
19446 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19448 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19449 tree c;
19451 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19452 return list;
19453 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19455 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19456 const char *p = IDENTIFIER_POINTER (id);
19458 switch (p[0])
19460 case 'n':
19461 if (strcmp ("none", p) != 0)
19462 goto invalid_kind;
19463 kind = OMP_CLAUSE_DEFAULT_NONE;
19464 break;
19466 case 's':
19467 if (strcmp ("shared", p) != 0)
19468 goto invalid_kind;
19469 kind = OMP_CLAUSE_DEFAULT_SHARED;
19470 break;
19472 default:
19473 goto invalid_kind;
19476 cp_lexer_consume_token (parser->lexer);
19478 else
19480 invalid_kind:
19481 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19484 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19485 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19486 /*or_comma=*/false,
19487 /*consume_paren=*/true);
19489 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19490 return list;
19492 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19493 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19494 OMP_CLAUSE_CHAIN (c) = list;
19495 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19497 return c;
19500 /* OpenMP 2.5:
19501 if ( expression ) */
19503 static tree
19504 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19506 tree t, c;
19508 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19509 return list;
19511 t = cp_parser_condition (parser);
19513 if (t == error_mark_node
19514 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19515 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19516 /*or_comma=*/false,
19517 /*consume_paren=*/true);
19519 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19521 c = build_omp_clause (OMP_CLAUSE_IF);
19522 OMP_CLAUSE_IF_EXPR (c) = t;
19523 OMP_CLAUSE_CHAIN (c) = list;
19525 return c;
19528 /* OpenMP 2.5:
19529 nowait */
19531 static tree
19532 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19534 tree c;
19536 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19538 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19539 OMP_CLAUSE_CHAIN (c) = list;
19540 return c;
19543 /* OpenMP 2.5:
19544 num_threads ( expression ) */
19546 static tree
19547 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19549 tree t, c;
19551 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19552 return list;
19554 t = cp_parser_expression (parser, false);
19556 if (t == error_mark_node
19557 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19558 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19559 /*or_comma=*/false,
19560 /*consume_paren=*/true);
19562 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19564 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19565 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19566 OMP_CLAUSE_CHAIN (c) = list;
19568 return c;
19571 /* OpenMP 2.5:
19572 ordered */
19574 static tree
19575 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19577 tree c;
19579 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19581 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19582 OMP_CLAUSE_CHAIN (c) = list;
19583 return c;
19586 /* OpenMP 2.5:
19587 reduction ( reduction-operator : variable-list )
19589 reduction-operator:
19590 One of: + * - & ^ | && || */
19592 static tree
19593 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19595 enum tree_code code;
19596 tree nlist, c;
19598 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19599 return list;
19601 switch (cp_lexer_peek_token (parser->lexer)->type)
19603 case CPP_PLUS:
19604 code = PLUS_EXPR;
19605 break;
19606 case CPP_MULT:
19607 code = MULT_EXPR;
19608 break;
19609 case CPP_MINUS:
19610 code = MINUS_EXPR;
19611 break;
19612 case CPP_AND:
19613 code = BIT_AND_EXPR;
19614 break;
19615 case CPP_XOR:
19616 code = BIT_XOR_EXPR;
19617 break;
19618 case CPP_OR:
19619 code = BIT_IOR_EXPR;
19620 break;
19621 case CPP_AND_AND:
19622 code = TRUTH_ANDIF_EXPR;
19623 break;
19624 case CPP_OR_OR:
19625 code = TRUTH_ORIF_EXPR;
19626 break;
19627 default:
19628 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19629 resync_fail:
19630 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19631 /*or_comma=*/false,
19632 /*consume_paren=*/true);
19633 return list;
19635 cp_lexer_consume_token (parser->lexer);
19637 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19638 goto resync_fail;
19640 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19641 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19642 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19644 return nlist;
19647 /* OpenMP 2.5:
19648 schedule ( schedule-kind )
19649 schedule ( schedule-kind , expression )
19651 schedule-kind:
19652 static | dynamic | guided | runtime */
19654 static tree
19655 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19657 tree c, t;
19659 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19660 return list;
19662 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19664 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19666 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19667 const char *p = IDENTIFIER_POINTER (id);
19669 switch (p[0])
19671 case 'd':
19672 if (strcmp ("dynamic", p) != 0)
19673 goto invalid_kind;
19674 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19675 break;
19677 case 'g':
19678 if (strcmp ("guided", p) != 0)
19679 goto invalid_kind;
19680 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19681 break;
19683 case 'r':
19684 if (strcmp ("runtime", p) != 0)
19685 goto invalid_kind;
19686 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19687 break;
19689 default:
19690 goto invalid_kind;
19693 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19694 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19695 else
19696 goto invalid_kind;
19697 cp_lexer_consume_token (parser->lexer);
19699 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19701 cp_lexer_consume_token (parser->lexer);
19703 t = cp_parser_assignment_expression (parser, false);
19705 if (t == error_mark_node)
19706 goto resync_fail;
19707 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19708 error ("schedule %<runtime%> does not take "
19709 "a %<chunk_size%> parameter");
19710 else
19711 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19713 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19714 goto resync_fail;
19716 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19717 goto resync_fail;
19719 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19720 OMP_CLAUSE_CHAIN (c) = list;
19721 return c;
19723 invalid_kind:
19724 cp_parser_error (parser, "invalid schedule kind");
19725 resync_fail:
19726 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19727 /*or_comma=*/false,
19728 /*consume_paren=*/true);
19729 return list;
19732 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19733 is a bitmask in MASK. Return the list of clauses found; the result
19734 of clause default goes in *pdefault. */
19736 static tree
19737 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19738 const char *where, cp_token *pragma_tok)
19740 tree clauses = NULL;
19741 bool first = true;
19743 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19745 pragma_omp_clause c_kind;
19746 const char *c_name;
19747 tree prev = clauses;
19749 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19750 cp_lexer_consume_token (parser->lexer);
19752 c_kind = cp_parser_omp_clause_name (parser);
19753 first = false;
19755 switch (c_kind)
19757 case PRAGMA_OMP_CLAUSE_COPYIN:
19758 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19759 c_name = "copyin";
19760 break;
19761 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19762 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19763 clauses);
19764 c_name = "copyprivate";
19765 break;
19766 case PRAGMA_OMP_CLAUSE_DEFAULT:
19767 clauses = cp_parser_omp_clause_default (parser, clauses);
19768 c_name = "default";
19769 break;
19770 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19771 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19772 clauses);
19773 c_name = "firstprivate";
19774 break;
19775 case PRAGMA_OMP_CLAUSE_IF:
19776 clauses = cp_parser_omp_clause_if (parser, clauses);
19777 c_name = "if";
19778 break;
19779 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19780 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19781 clauses);
19782 c_name = "lastprivate";
19783 break;
19784 case PRAGMA_OMP_CLAUSE_NOWAIT:
19785 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19786 c_name = "nowait";
19787 break;
19788 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19789 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19790 c_name = "num_threads";
19791 break;
19792 case PRAGMA_OMP_CLAUSE_ORDERED:
19793 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19794 c_name = "ordered";
19795 break;
19796 case PRAGMA_OMP_CLAUSE_PRIVATE:
19797 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19798 clauses);
19799 c_name = "private";
19800 break;
19801 case PRAGMA_OMP_CLAUSE_REDUCTION:
19802 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19803 c_name = "reduction";
19804 break;
19805 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19806 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19807 c_name = "schedule";
19808 break;
19809 case PRAGMA_OMP_CLAUSE_SHARED:
19810 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19811 clauses);
19812 c_name = "shared";
19813 break;
19814 default:
19815 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19816 goto saw_error;
19819 if (((mask >> c_kind) & 1) == 0)
19821 /* Remove the invalid clause(s) from the list to avoid
19822 confusing the rest of the compiler. */
19823 clauses = prev;
19824 error ("%qs is not valid for %qs", c_name, where);
19827 saw_error:
19828 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19829 return finish_omp_clauses (clauses);
19832 /* OpenMP 2.5:
19833 structured-block:
19834 statement
19836 In practice, we're also interested in adding the statement to an
19837 outer node. So it is convenient if we work around the fact that
19838 cp_parser_statement calls add_stmt. */
19840 static unsigned
19841 cp_parser_begin_omp_structured_block (cp_parser *parser)
19843 unsigned save = parser->in_statement;
19845 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19846 This preserves the "not within loop or switch" style error messages
19847 for nonsense cases like
19848 void foo() {
19849 #pragma omp single
19850 break;
19853 if (parser->in_statement)
19854 parser->in_statement = IN_OMP_BLOCK;
19856 return save;
19859 static void
19860 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19862 parser->in_statement = save;
19865 static tree
19866 cp_parser_omp_structured_block (cp_parser *parser)
19868 tree stmt = begin_omp_structured_block ();
19869 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19871 cp_parser_statement (parser, NULL_TREE, false, NULL);
19873 cp_parser_end_omp_structured_block (parser, save);
19874 return finish_omp_structured_block (stmt);
19877 /* OpenMP 2.5:
19878 # pragma omp atomic new-line
19879 expression-stmt
19881 expression-stmt:
19882 x binop= expr | x++ | ++x | x-- | --x
19883 binop:
19884 +, *, -, /, &, ^, |, <<, >>
19886 where x is an lvalue expression with scalar type. */
19888 static void
19889 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19891 tree lhs, rhs;
19892 enum tree_code code;
19894 cp_parser_require_pragma_eol (parser, pragma_tok);
19896 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19897 /*cast_p=*/false);
19898 switch (TREE_CODE (lhs))
19900 case ERROR_MARK:
19901 goto saw_error;
19903 case PREINCREMENT_EXPR:
19904 case POSTINCREMENT_EXPR:
19905 lhs = TREE_OPERAND (lhs, 0);
19906 code = PLUS_EXPR;
19907 rhs = integer_one_node;
19908 break;
19910 case PREDECREMENT_EXPR:
19911 case POSTDECREMENT_EXPR:
19912 lhs = TREE_OPERAND (lhs, 0);
19913 code = MINUS_EXPR;
19914 rhs = integer_one_node;
19915 break;
19917 default:
19918 switch (cp_lexer_peek_token (parser->lexer)->type)
19920 case CPP_MULT_EQ:
19921 code = MULT_EXPR;
19922 break;
19923 case CPP_DIV_EQ:
19924 code = TRUNC_DIV_EXPR;
19925 break;
19926 case CPP_PLUS_EQ:
19927 code = PLUS_EXPR;
19928 break;
19929 case CPP_MINUS_EQ:
19930 code = MINUS_EXPR;
19931 break;
19932 case CPP_LSHIFT_EQ:
19933 code = LSHIFT_EXPR;
19934 break;
19935 case CPP_RSHIFT_EQ:
19936 code = RSHIFT_EXPR;
19937 break;
19938 case CPP_AND_EQ:
19939 code = BIT_AND_EXPR;
19940 break;
19941 case CPP_OR_EQ:
19942 code = BIT_IOR_EXPR;
19943 break;
19944 case CPP_XOR_EQ:
19945 code = BIT_XOR_EXPR;
19946 break;
19947 default:
19948 cp_parser_error (parser,
19949 "invalid operator for %<#pragma omp atomic%>");
19950 goto saw_error;
19952 cp_lexer_consume_token (parser->lexer);
19954 rhs = cp_parser_expression (parser, false);
19955 if (rhs == error_mark_node)
19956 goto saw_error;
19957 break;
19959 finish_omp_atomic (code, lhs, rhs);
19960 cp_parser_consume_semicolon_at_end_of_statement (parser);
19961 return;
19963 saw_error:
19964 cp_parser_skip_to_end_of_block_or_statement (parser);
19968 /* OpenMP 2.5:
19969 # pragma omp barrier new-line */
19971 static void
19972 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19974 cp_parser_require_pragma_eol (parser, pragma_tok);
19975 finish_omp_barrier ();
19978 /* OpenMP 2.5:
19979 # pragma omp critical [(name)] new-line
19980 structured-block */
19982 static tree
19983 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19985 tree stmt, name = NULL;
19987 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19989 cp_lexer_consume_token (parser->lexer);
19991 name = cp_parser_identifier (parser);
19993 if (name == error_mark_node
19994 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19995 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19996 /*or_comma=*/false,
19997 /*consume_paren=*/true);
19998 if (name == error_mark_node)
19999 name = NULL;
20001 cp_parser_require_pragma_eol (parser, pragma_tok);
20003 stmt = cp_parser_omp_structured_block (parser);
20004 return c_finish_omp_critical (stmt, name);
20007 /* OpenMP 2.5:
20008 # pragma omp flush flush-vars[opt] new-line
20010 flush-vars:
20011 ( variable-list ) */
20013 static void
20014 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20016 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20017 (void) cp_parser_omp_var_list (parser, 0, NULL);
20018 cp_parser_require_pragma_eol (parser, pragma_tok);
20020 finish_omp_flush ();
20023 /* Parse the restricted form of the for statment allowed by OpenMP. */
20025 static tree
20026 cp_parser_omp_for_loop (cp_parser *parser)
20028 tree init, cond, incr, body, decl, pre_body;
20029 location_t loc;
20031 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20033 cp_parser_error (parser, "for statement expected");
20034 return NULL;
20036 loc = cp_lexer_consume_token (parser->lexer)->location;
20037 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20038 return NULL;
20040 init = decl = NULL;
20041 pre_body = push_stmt_list ();
20042 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20044 cp_decl_specifier_seq type_specifiers;
20046 /* First, try to parse as an initialized declaration. See
20047 cp_parser_condition, from whence the bulk of this is copied. */
20049 cp_parser_parse_tentatively (parser);
20050 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20051 &type_specifiers);
20052 if (!cp_parser_error_occurred (parser))
20054 tree asm_specification, attributes;
20055 cp_declarator *declarator;
20057 declarator = cp_parser_declarator (parser,
20058 CP_PARSER_DECLARATOR_NAMED,
20059 /*ctor_dtor_or_conv_p=*/NULL,
20060 /*parenthesized_p=*/NULL,
20061 /*member_p=*/false);
20062 attributes = cp_parser_attributes_opt (parser);
20063 asm_specification = cp_parser_asm_specification_opt (parser);
20065 cp_parser_require (parser, CPP_EQ, "`='");
20066 if (cp_parser_parse_definitely (parser))
20068 tree pushed_scope;
20070 decl = start_decl (declarator, &type_specifiers,
20071 /*initialized_p=*/false, attributes,
20072 /*prefix_attributes=*/NULL_TREE,
20073 &pushed_scope);
20075 init = cp_parser_assignment_expression (parser, false);
20077 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20078 init = error_mark_node;
20079 else
20080 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
20081 asm_specification, LOOKUP_ONLYCONVERTING);
20083 if (pushed_scope)
20084 pop_scope (pushed_scope);
20087 else
20088 cp_parser_abort_tentative_parse (parser);
20090 /* If parsing as an initialized declaration failed, try again as
20091 a simple expression. */
20092 if (decl == NULL)
20093 init = cp_parser_expression (parser, false);
20095 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20096 pre_body = pop_stmt_list (pre_body);
20098 cond = NULL;
20099 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20100 cond = cp_parser_condition (parser);
20101 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20103 incr = NULL;
20104 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20105 incr = cp_parser_expression (parser, false);
20107 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20108 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20109 /*or_comma=*/false,
20110 /*consume_paren=*/true);
20112 /* Note that we saved the original contents of this flag when we entered
20113 the structured block, and so we don't need to re-save it here. */
20114 parser->in_statement = IN_OMP_FOR;
20116 /* Note that the grammar doesn't call for a structured block here,
20117 though the loop as a whole is a structured block. */
20118 body = push_stmt_list ();
20119 cp_parser_statement (parser, NULL_TREE, false, NULL);
20120 body = pop_stmt_list (body);
20122 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
20125 /* OpenMP 2.5:
20126 #pragma omp for for-clause[optseq] new-line
20127 for-loop */
20129 #define OMP_FOR_CLAUSE_MASK \
20130 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20131 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20132 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20133 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20134 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
20135 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
20136 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20138 static tree
20139 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20141 tree clauses, sb, ret;
20142 unsigned int save;
20144 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20145 "#pragma omp for", pragma_tok);
20147 sb = begin_omp_structured_block ();
20148 save = cp_parser_begin_omp_structured_block (parser);
20150 ret = cp_parser_omp_for_loop (parser);
20151 if (ret)
20152 OMP_FOR_CLAUSES (ret) = clauses;
20154 cp_parser_end_omp_structured_block (parser, save);
20155 add_stmt (finish_omp_structured_block (sb));
20157 return ret;
20160 /* OpenMP 2.5:
20161 # pragma omp master new-line
20162 structured-block */
20164 static tree
20165 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20167 cp_parser_require_pragma_eol (parser, pragma_tok);
20168 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20171 /* OpenMP 2.5:
20172 # pragma omp ordered new-line
20173 structured-block */
20175 static tree
20176 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20178 cp_parser_require_pragma_eol (parser, pragma_tok);
20179 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20182 /* OpenMP 2.5:
20184 section-scope:
20185 { section-sequence }
20187 section-sequence:
20188 section-directive[opt] structured-block
20189 section-sequence section-directive structured-block */
20191 static tree
20192 cp_parser_omp_sections_scope (cp_parser *parser)
20194 tree stmt, substmt;
20195 bool error_suppress = false;
20196 cp_token *tok;
20198 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20199 return NULL_TREE;
20201 stmt = push_stmt_list ();
20203 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20205 unsigned save;
20207 substmt = begin_omp_structured_block ();
20208 save = cp_parser_begin_omp_structured_block (parser);
20210 while (1)
20212 cp_parser_statement (parser, NULL_TREE, false, NULL);
20214 tok = cp_lexer_peek_token (parser->lexer);
20215 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20216 break;
20217 if (tok->type == CPP_CLOSE_BRACE)
20218 break;
20219 if (tok->type == CPP_EOF)
20220 break;
20223 cp_parser_end_omp_structured_block (parser, save);
20224 substmt = finish_omp_structured_block (substmt);
20225 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20226 add_stmt (substmt);
20229 while (1)
20231 tok = cp_lexer_peek_token (parser->lexer);
20232 if (tok->type == CPP_CLOSE_BRACE)
20233 break;
20234 if (tok->type == CPP_EOF)
20235 break;
20237 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20239 cp_lexer_consume_token (parser->lexer);
20240 cp_parser_require_pragma_eol (parser, tok);
20241 error_suppress = false;
20243 else if (!error_suppress)
20245 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20246 error_suppress = true;
20249 substmt = cp_parser_omp_structured_block (parser);
20250 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20251 add_stmt (substmt);
20253 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20255 substmt = pop_stmt_list (stmt);
20257 stmt = make_node (OMP_SECTIONS);
20258 TREE_TYPE (stmt) = void_type_node;
20259 OMP_SECTIONS_BODY (stmt) = substmt;
20261 add_stmt (stmt);
20262 return stmt;
20265 /* OpenMP 2.5:
20266 # pragma omp sections sections-clause[optseq] newline
20267 sections-scope */
20269 #define OMP_SECTIONS_CLAUSE_MASK \
20270 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20271 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20272 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20273 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20274 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20276 static tree
20277 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20279 tree clauses, ret;
20281 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20282 "#pragma omp sections", pragma_tok);
20284 ret = cp_parser_omp_sections_scope (parser);
20285 if (ret)
20286 OMP_SECTIONS_CLAUSES (ret) = clauses;
20288 return ret;
20291 /* OpenMP 2.5:
20292 # pragma parallel parallel-clause new-line
20293 # pragma parallel for parallel-for-clause new-line
20294 # pragma parallel sections parallel-sections-clause new-line */
20296 #define OMP_PARALLEL_CLAUSE_MASK \
20297 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
20298 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20299 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20300 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
20301 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
20302 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
20303 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20304 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20306 static tree
20307 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20309 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20310 const char *p_name = "#pragma omp parallel";
20311 tree stmt, clauses, par_clause, ws_clause, block;
20312 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20313 unsigned int save;
20315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20317 cp_lexer_consume_token (parser->lexer);
20318 p_kind = PRAGMA_OMP_PARALLEL_FOR;
20319 p_name = "#pragma omp parallel for";
20320 mask |= OMP_FOR_CLAUSE_MASK;
20321 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20323 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20325 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20326 const char *p = IDENTIFIER_POINTER (id);
20327 if (strcmp (p, "sections") == 0)
20329 cp_lexer_consume_token (parser->lexer);
20330 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20331 p_name = "#pragma omp parallel sections";
20332 mask |= OMP_SECTIONS_CLAUSE_MASK;
20333 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20337 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20338 block = begin_omp_parallel ();
20339 save = cp_parser_begin_omp_structured_block (parser);
20341 switch (p_kind)
20343 case PRAGMA_OMP_PARALLEL:
20344 cp_parser_statement (parser, NULL_TREE, false, NULL);
20345 par_clause = clauses;
20346 break;
20348 case PRAGMA_OMP_PARALLEL_FOR:
20349 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20350 stmt = cp_parser_omp_for_loop (parser);
20351 if (stmt)
20352 OMP_FOR_CLAUSES (stmt) = ws_clause;
20353 break;
20355 case PRAGMA_OMP_PARALLEL_SECTIONS:
20356 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20357 stmt = cp_parser_omp_sections_scope (parser);
20358 if (stmt)
20359 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20360 break;
20362 default:
20363 gcc_unreachable ();
20366 cp_parser_end_omp_structured_block (parser, save);
20367 stmt = finish_omp_parallel (par_clause, block);
20368 if (p_kind != PRAGMA_OMP_PARALLEL)
20369 OMP_PARALLEL_COMBINED (stmt) = 1;
20370 return stmt;
20373 /* OpenMP 2.5:
20374 # pragma omp single single-clause[optseq] new-line
20375 structured-block */
20377 #define OMP_SINGLE_CLAUSE_MASK \
20378 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20379 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20380 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20381 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20383 static tree
20384 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20386 tree stmt = make_node (OMP_SINGLE);
20387 TREE_TYPE (stmt) = void_type_node;
20389 OMP_SINGLE_CLAUSES (stmt)
20390 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20391 "#pragma omp single", pragma_tok);
20392 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20394 return add_stmt (stmt);
20397 /* OpenMP 2.5:
20398 # pragma omp threadprivate (variable-list) */
20400 static void
20401 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20403 tree vars;
20405 vars = cp_parser_omp_var_list (parser, 0, NULL);
20406 cp_parser_require_pragma_eol (parser, pragma_tok);
20408 finish_omp_threadprivate (vars);
20411 /* Main entry point to OpenMP statement pragmas. */
20413 static void
20414 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20416 tree stmt;
20418 switch (pragma_tok->pragma_kind)
20420 case PRAGMA_OMP_ATOMIC:
20421 cp_parser_omp_atomic (parser, pragma_tok);
20422 return;
20423 case PRAGMA_OMP_CRITICAL:
20424 stmt = cp_parser_omp_critical (parser, pragma_tok);
20425 break;
20426 case PRAGMA_OMP_FOR:
20427 stmt = cp_parser_omp_for (parser, pragma_tok);
20428 break;
20429 case PRAGMA_OMP_MASTER:
20430 stmt = cp_parser_omp_master (parser, pragma_tok);
20431 break;
20432 case PRAGMA_OMP_ORDERED:
20433 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20434 break;
20435 case PRAGMA_OMP_PARALLEL:
20436 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20437 break;
20438 case PRAGMA_OMP_SECTIONS:
20439 stmt = cp_parser_omp_sections (parser, pragma_tok);
20440 break;
20441 case PRAGMA_OMP_SINGLE:
20442 stmt = cp_parser_omp_single (parser, pragma_tok);
20443 break;
20444 default:
20445 gcc_unreachable ();
20448 if (stmt)
20449 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20452 /* The parser. */
20454 static GTY (()) cp_parser *the_parser;
20457 /* Special handling for the first token or line in the file. The first
20458 thing in the file might be #pragma GCC pch_preprocess, which loads a
20459 PCH file, which is a GC collection point. So we need to handle this
20460 first pragma without benefit of an existing lexer structure.
20462 Always returns one token to the caller in *FIRST_TOKEN. This is
20463 either the true first token of the file, or the first token after
20464 the initial pragma. */
20466 static void
20467 cp_parser_initial_pragma (cp_token *first_token)
20469 tree name = NULL;
20471 cp_lexer_get_preprocessor_token (NULL, first_token);
20472 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20473 return;
20475 cp_lexer_get_preprocessor_token (NULL, first_token);
20476 if (first_token->type == CPP_STRING)
20478 name = first_token->u.value;
20480 cp_lexer_get_preprocessor_token (NULL, first_token);
20481 if (first_token->type != CPP_PRAGMA_EOL)
20482 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20484 else
20485 error ("expected string literal");
20487 /* Skip to the end of the pragma. */
20488 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20489 cp_lexer_get_preprocessor_token (NULL, first_token);
20491 /* Now actually load the PCH file. */
20492 if (name)
20493 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20495 /* Read one more token to return to our caller. We have to do this
20496 after reading the PCH file in, since its pointers have to be
20497 live. */
20498 cp_lexer_get_preprocessor_token (NULL, first_token);
20501 /* Normal parsing of a pragma token. Here we can (and must) use the
20502 regular lexer. */
20504 static bool
20505 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20507 cp_token *pragma_tok;
20508 unsigned int id;
20510 pragma_tok = cp_lexer_consume_token (parser->lexer);
20511 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20512 parser->lexer->in_pragma = true;
20514 id = pragma_tok->pragma_kind;
20515 switch (id)
20517 case PRAGMA_GCC_PCH_PREPROCESS:
20518 error ("%<#pragma GCC pch_preprocess%> must be first");
20519 break;
20521 case PRAGMA_OMP_BARRIER:
20522 switch (context)
20524 case pragma_compound:
20525 cp_parser_omp_barrier (parser, pragma_tok);
20526 return false;
20527 case pragma_stmt:
20528 error ("%<#pragma omp barrier%> may only be "
20529 "used in compound statements");
20530 break;
20531 default:
20532 goto bad_stmt;
20534 break;
20536 case PRAGMA_OMP_FLUSH:
20537 switch (context)
20539 case pragma_compound:
20540 cp_parser_omp_flush (parser, pragma_tok);
20541 return false;
20542 case pragma_stmt:
20543 error ("%<#pragma omp flush%> may only be "
20544 "used in compound statements");
20545 break;
20546 default:
20547 goto bad_stmt;
20549 break;
20551 case PRAGMA_OMP_THREADPRIVATE:
20552 cp_parser_omp_threadprivate (parser, pragma_tok);
20553 return false;
20555 case PRAGMA_OMP_ATOMIC:
20556 case PRAGMA_OMP_CRITICAL:
20557 case PRAGMA_OMP_FOR:
20558 case PRAGMA_OMP_MASTER:
20559 case PRAGMA_OMP_ORDERED:
20560 case PRAGMA_OMP_PARALLEL:
20561 case PRAGMA_OMP_SECTIONS:
20562 case PRAGMA_OMP_SINGLE:
20563 if (context == pragma_external)
20564 goto bad_stmt;
20565 cp_parser_omp_construct (parser, pragma_tok);
20566 return true;
20568 case PRAGMA_OMP_SECTION:
20569 error ("%<#pragma omp section%> may only be used in "
20570 "%<#pragma omp sections%> construct");
20571 break;
20573 default:
20574 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20575 c_invoke_pragma_handler (id);
20576 break;
20578 bad_stmt:
20579 cp_parser_error (parser, "expected declaration specifiers");
20580 break;
20583 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20584 return false;
20587 /* The interface the pragma parsers have to the lexer. */
20589 enum cpp_ttype
20590 pragma_lex (tree *value)
20592 cp_token *tok;
20593 enum cpp_ttype ret;
20595 tok = cp_lexer_peek_token (the_parser->lexer);
20597 ret = tok->type;
20598 *value = tok->u.value;
20600 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20601 ret = CPP_EOF;
20602 else if (ret == CPP_STRING)
20603 *value = cp_parser_string_literal (the_parser, false, false);
20604 else
20606 cp_lexer_consume_token (the_parser->lexer);
20607 if (ret == CPP_KEYWORD)
20608 ret = CPP_NAME;
20611 return ret;
20615 /* External interface. */
20617 /* Parse one entire translation unit. */
20619 void
20620 c_parse_file (void)
20622 bool error_occurred;
20623 static bool already_called = false;
20625 if (already_called)
20627 sorry ("inter-module optimizations not implemented for C++");
20628 return;
20630 already_called = true;
20632 the_parser = cp_parser_new ();
20633 push_deferring_access_checks (flag_access_control
20634 ? dk_no_deferred : dk_no_check);
20635 error_occurred = cp_parser_translation_unit (the_parser);
20636 the_parser = NULL;
20639 #include "gt-cp-parser.h"