* config/alpha/alpha.md, arm/arm.c, darwin.c, frv/frv.md,
[official-gcc.git] / gcc / cp / parser.c
blob82cb796985890e47f42ee3c6c6e3f23a0ce56148
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
43 /* The lexer. */
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
49 qualifying scope. */
51 struct tree_check GTY(())
53 /* The value associated with the token. */
54 tree value;
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
62 /* A C++ token. */
64 typedef struct cp_token GTY (())
66 /* The kind of token. */
67 ENUM_BITFIELD (cpp_ttype) type : 8;
68 /* If this token is a keyword, this value indicates which keyword.
69 Otherwise, this value is RID_MAX. */
70 ENUM_BITFIELD (rid) keyword : 8;
71 /* Token flags. */
72 unsigned char flags;
73 /* Identifier for the pragma. */
74 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
75 /* True if this token is from a system header. */
76 BOOL_BITFIELD in_system_header : 1;
77 /* True if this token is from a context where it is implicitly extern "C" */
78 BOOL_BITFIELD implicit_extern_c : 1;
79 /* True for a CPP_NAME token that is not a keyword (i.e., for which
80 KEYWORD is RID_MAX) iff this name was looked up and found to be
81 ambiguous. An error has already been reported. */
82 BOOL_BITFIELD ambiguous_p : 1;
83 /* The input file stack index at which this token was found. */
84 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
85 /* The value associated with this token, if any. */
86 union cp_token_value {
87 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
88 struct tree_check* GTY((tag ("1"))) tree_check_value;
89 /* Use for all other tokens. */
90 tree GTY((tag ("0"))) value;
91 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
92 /* The location at which this token was found. */
93 location_t location;
94 } cp_token;
96 /* We use a stack of token pointer for saving token sets. */
97 typedef struct cp_token *cp_token_position;
98 DEF_VEC_P (cp_token_position);
99 DEF_VEC_ALLOC_P (cp_token_position,heap);
101 static const cp_token eof_token =
103 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
104 #if USE_MAPPED_LOCATION
106 #else
107 {0, 0}
108 #endif
111 /* The cp_lexer structure represents the C++ lexer. It is responsible
112 for managing the token stream from the preprocessor and supplying
113 it to the parser. Tokens are never added to the cp_lexer after
114 it is created. */
116 typedef struct cp_lexer GTY (())
118 /* The memory allocated for the buffer. NULL if this lexer does not
119 own the token buffer. */
120 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
121 /* If the lexer owns the buffer, this is the number of tokens in the
122 buffer. */
123 size_t buffer_length;
125 /* A pointer just past the last available token. The tokens
126 in this lexer are [buffer, last_token). */
127 cp_token_position GTY ((skip)) last_token;
129 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
130 no more available tokens. */
131 cp_token_position GTY ((skip)) next_token;
133 /* A stack indicating positions at which cp_lexer_save_tokens was
134 called. The top entry is the most recent position at which we
135 began saving tokens. If the stack is non-empty, we are saving
136 tokens. */
137 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
139 /* The next lexer in a linked list of lexers. */
140 struct cp_lexer *next;
142 /* True if we should output debugging information. */
143 bool debugging_p;
145 /* True if we're in the context of parsing a pragma, and should not
146 increment past the end-of-line marker. */
147 bool in_pragma;
148 } cp_lexer;
150 /* cp_token_cache is a range of tokens. There is no need to represent
151 allocate heap memory for it, since tokens are never removed from the
152 lexer's array. There is also no need for the GC to walk through
153 a cp_token_cache, since everything in here is referenced through
154 a lexer. */
156 typedef struct cp_token_cache GTY(())
158 /* The beginning of the token range. */
159 cp_token * GTY((skip)) first;
161 /* Points immediately after the last token in the range. */
162 cp_token * GTY ((skip)) last;
163 } cp_token_cache;
165 /* Prototypes. */
167 static cp_lexer *cp_lexer_new_main
168 (void);
169 static cp_lexer *cp_lexer_new_from_tokens
170 (cp_token_cache *tokens);
171 static void cp_lexer_destroy
172 (cp_lexer *);
173 static int cp_lexer_saving_tokens
174 (const cp_lexer *);
175 static cp_token_position cp_lexer_token_position
176 (cp_lexer *, bool);
177 static cp_token *cp_lexer_token_at
178 (cp_lexer *, cp_token_position);
179 static void cp_lexer_get_preprocessor_token
180 (cp_lexer *, cp_token *);
181 static inline cp_token *cp_lexer_peek_token
182 (cp_lexer *);
183 static cp_token *cp_lexer_peek_nth_token
184 (cp_lexer *, size_t);
185 static inline bool cp_lexer_next_token_is
186 (cp_lexer *, enum cpp_ttype);
187 static bool cp_lexer_next_token_is_not
188 (cp_lexer *, enum cpp_ttype);
189 static bool cp_lexer_next_token_is_keyword
190 (cp_lexer *, enum rid);
191 static cp_token *cp_lexer_consume_token
192 (cp_lexer *);
193 static void cp_lexer_purge_token
194 (cp_lexer *);
195 static void cp_lexer_purge_tokens_after
196 (cp_lexer *, cp_token_position);
197 static void cp_lexer_save_tokens
198 (cp_lexer *);
199 static void cp_lexer_commit_tokens
200 (cp_lexer *);
201 static void cp_lexer_rollback_tokens
202 (cp_lexer *);
203 #ifdef ENABLE_CHECKING
204 static void cp_lexer_print_token
205 (FILE *, cp_token *);
206 static inline bool cp_lexer_debugging_p
207 (cp_lexer *);
208 static void cp_lexer_start_debugging
209 (cp_lexer *) ATTRIBUTE_UNUSED;
210 static void cp_lexer_stop_debugging
211 (cp_lexer *) ATTRIBUTE_UNUSED;
212 #else
213 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
214 about passing NULL to functions that require non-NULL arguments
215 (fputs, fprintf). It will never be used, so all we need is a value
216 of the right type that's guaranteed not to be NULL. */
217 #define cp_lexer_debug_stream stdout
218 #define cp_lexer_print_token(str, tok) (void) 0
219 #define cp_lexer_debugging_p(lexer) 0
220 #endif /* ENABLE_CHECKING */
222 static cp_token_cache *cp_token_cache_new
223 (cp_token *, cp_token *);
225 static void cp_parser_initial_pragma
226 (cp_token *);
228 /* Manifest constants. */
229 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
230 #define CP_SAVED_TOKEN_STACK 5
232 /* A token type for keywords, as opposed to ordinary identifiers. */
233 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
235 /* A token type for template-ids. If a template-id is processed while
236 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
237 the value of the CPP_TEMPLATE_ID is whatever was returned by
238 cp_parser_template_id. */
239 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
241 /* A token type for nested-name-specifiers. If a
242 nested-name-specifier is processed while parsing tentatively, it is
243 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
244 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
245 cp_parser_nested_name_specifier_opt. */
246 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
248 /* A token type for tokens that are not tokens at all; these are used
249 to represent slots in the array where there used to be a token
250 that has now been deleted. */
251 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
253 /* The number of token types, including C++-specific ones. */
254 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
256 /* Variables. */
258 #ifdef ENABLE_CHECKING
259 /* The stream to which debugging output should be written. */
260 static FILE *cp_lexer_debug_stream;
261 #endif /* ENABLE_CHECKING */
263 /* Create a new main C++ lexer, the lexer that gets tokens from the
264 preprocessor. */
266 static cp_lexer *
267 cp_lexer_new_main (void)
269 cp_token first_token;
270 cp_lexer *lexer;
271 cp_token *pos;
272 size_t alloc;
273 size_t space;
274 cp_token *buffer;
276 /* It's possible that parsing the first pragma will load a PCH file,
277 which is a GC collection point. So we have to do that before
278 allocating any memory. */
279 cp_parser_initial_pragma (&first_token);
281 /* Tell c_lex_with_flags not to merge string constants. */
282 c_lex_return_raw_strings = true;
284 c_common_no_more_pch ();
286 /* Allocate the memory. */
287 lexer = GGC_CNEW (cp_lexer);
289 #ifdef ENABLE_CHECKING
290 /* Initially we are not debugging. */
291 lexer->debugging_p = false;
292 #endif /* ENABLE_CHECKING */
293 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
294 CP_SAVED_TOKEN_STACK);
296 /* Create the buffer. */
297 alloc = CP_LEXER_BUFFER_SIZE;
298 buffer = GGC_NEWVEC (cp_token, alloc);
300 /* Put the first token in the buffer. */
301 space = alloc;
302 pos = buffer;
303 *pos = first_token;
305 /* Get the remaining tokens from the preprocessor. */
306 while (pos->type != CPP_EOF)
308 pos++;
309 if (!--space)
311 space = alloc;
312 alloc *= 2;
313 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
314 pos = buffer + space;
316 cp_lexer_get_preprocessor_token (lexer, pos);
318 lexer->buffer = buffer;
319 lexer->buffer_length = alloc - space;
320 lexer->last_token = pos;
321 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
323 /* Subsequent preprocessor diagnostics should use compiler
324 diagnostic functions to get the compiler source location. */
325 cpp_get_options (parse_in)->client_diagnostic = true;
326 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
328 gcc_assert (lexer->next_token->type != CPP_PURGED);
329 return lexer;
332 /* Create a new lexer whose token stream is primed with the tokens in
333 CACHE. When these tokens are exhausted, no new tokens will be read. */
335 static cp_lexer *
336 cp_lexer_new_from_tokens (cp_token_cache *cache)
338 cp_token *first = cache->first;
339 cp_token *last = cache->last;
340 cp_lexer *lexer = GGC_CNEW (cp_lexer);
342 /* We do not own the buffer. */
343 lexer->buffer = NULL;
344 lexer->buffer_length = 0;
345 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
346 lexer->last_token = last;
348 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
349 CP_SAVED_TOKEN_STACK);
351 #ifdef ENABLE_CHECKING
352 /* Initially we are not debugging. */
353 lexer->debugging_p = false;
354 #endif
356 gcc_assert (lexer->next_token->type != CPP_PURGED);
357 return lexer;
360 /* Frees all resources associated with LEXER. */
362 static void
363 cp_lexer_destroy (cp_lexer *lexer)
365 if (lexer->buffer)
366 ggc_free (lexer->buffer);
367 VEC_free (cp_token_position, heap, lexer->saved_tokens);
368 ggc_free (lexer);
371 /* Returns nonzero if debugging information should be output. */
373 #ifdef ENABLE_CHECKING
375 static inline bool
376 cp_lexer_debugging_p (cp_lexer *lexer)
378 return lexer->debugging_p;
381 #endif /* ENABLE_CHECKING */
383 static inline cp_token_position
384 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
386 gcc_assert (!previous_p || lexer->next_token != &eof_token);
388 return lexer->next_token - previous_p;
391 static inline cp_token *
392 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
394 return pos;
397 /* nonzero if we are presently saving tokens. */
399 static inline int
400 cp_lexer_saving_tokens (const cp_lexer* lexer)
402 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
405 /* Store the next token from the preprocessor in *TOKEN. Return true
406 if we reach EOF. */
408 static void
409 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
410 cp_token *token)
412 static int is_extern_c = 0;
414 /* Get a new token from the preprocessor. */
415 token->type
416 = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
417 token->input_file_stack_index = input_file_stack_tick;
418 token->keyword = RID_MAX;
419 token->pragma_kind = PRAGMA_NONE;
420 token->in_system_header = in_system_header;
422 /* On some systems, some header files are surrounded by an
423 implicit extern "C" block. Set a flag in the token if it
424 comes from such a header. */
425 is_extern_c += pending_lang_change;
426 pending_lang_change = 0;
427 token->implicit_extern_c = is_extern_c > 0;
429 /* Check to see if this token is a keyword. */
430 if (token->type == CPP_NAME)
432 if (C_IS_RESERVED_WORD (token->u.value))
434 /* Mark this token as a keyword. */
435 token->type = CPP_KEYWORD;
436 /* Record which keyword. */
437 token->keyword = C_RID_CODE (token->u.value);
438 /* Update the value. Some keywords are mapped to particular
439 entities, rather than simply having the value of the
440 corresponding IDENTIFIER_NODE. For example, `__const' is
441 mapped to `const'. */
442 token->u.value = ridpointers[token->keyword];
444 else
446 token->ambiguous_p = false;
447 token->keyword = RID_MAX;
450 /* Handle Objective-C++ keywords. */
451 else if (token->type == CPP_AT_NAME)
453 token->type = CPP_KEYWORD;
454 switch (C_RID_CODE (token->u.value))
456 /* Map 'class' to '@class', 'private' to '@private', etc. */
457 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
458 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
459 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
460 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
461 case RID_THROW: token->keyword = RID_AT_THROW; break;
462 case RID_TRY: token->keyword = RID_AT_TRY; break;
463 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
464 default: token->keyword = C_RID_CODE (token->u.value);
467 else if (token->type == CPP_PRAGMA)
469 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
470 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
471 token->u.value = NULL_TREE;
475 /* Update the globals input_location and in_system_header and the
476 input file stack from TOKEN. */
477 static inline void
478 cp_lexer_set_source_position_from_token (cp_token *token)
480 if (token->type != CPP_EOF)
482 input_location = token->location;
483 in_system_header = token->in_system_header;
484 restore_input_file_stack (token->input_file_stack_index);
488 /* Return a pointer to the next token in the token stream, but do not
489 consume it. */
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
494 if (cp_lexer_debugging_p (lexer))
496 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498 putc ('\n', cp_lexer_debug_stream);
500 return lexer->next_token;
503 /* Return true if the next token has the indicated TYPE. */
505 static inline bool
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
508 return cp_lexer_peek_token (lexer)->type == type;
511 /* Return true if the next token does not have the indicated TYPE. */
513 static inline bool
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
516 return !cp_lexer_next_token_is (lexer, type);
519 /* Return true if the next token is the indicated KEYWORD. */
521 static inline bool
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
524 return cp_lexer_peek_token (lexer)->keyword == keyword;
527 /* Return true if the next token is a keyword for a decl-specifier. */
529 static bool
530 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
532 cp_token *token;
534 token = cp_lexer_peek_token (lexer);
535 switch (token->keyword)
537 /* Storage classes. */
538 case RID_AUTO:
539 case RID_REGISTER:
540 case RID_STATIC:
541 case RID_EXTERN:
542 case RID_MUTABLE:
543 case RID_THREAD:
544 /* Elaborated type specifiers. */
545 case RID_ENUM:
546 case RID_CLASS:
547 case RID_STRUCT:
548 case RID_UNION:
549 case RID_TYPENAME:
550 /* Simple type specifiers. */
551 case RID_CHAR:
552 case RID_WCHAR:
553 case RID_BOOL:
554 case RID_SHORT:
555 case RID_INT:
556 case RID_LONG:
557 case RID_SIGNED:
558 case RID_UNSIGNED:
559 case RID_FLOAT:
560 case RID_DOUBLE:
561 case RID_VOID:
562 /* GNU extensions. */
563 case RID_ATTRIBUTE:
564 case RID_TYPEOF:
565 return true;
567 default:
568 return false;
572 /* Return a pointer to the Nth token in the token stream. If N is 1,
573 then this is precisely equivalent to cp_lexer_peek_token (except
574 that it is not inline). One would like to disallow that case, but
575 there is one case (cp_parser_nth_token_starts_template_id) where
576 the caller passes a variable for N and it might be 1. */
578 static cp_token *
579 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
581 cp_token *token;
583 /* N is 1-based, not zero-based. */
584 gcc_assert (n > 0);
586 if (cp_lexer_debugging_p (lexer))
587 fprintf (cp_lexer_debug_stream,
588 "cp_lexer: peeking ahead %ld at token: ", (long)n);
590 --n;
591 token = lexer->next_token;
592 gcc_assert (!n || token != &eof_token);
593 while (n != 0)
595 ++token;
596 if (token == lexer->last_token)
598 token = (cp_token *)&eof_token;
599 break;
602 if (token->type != CPP_PURGED)
603 --n;
606 if (cp_lexer_debugging_p (lexer))
608 cp_lexer_print_token (cp_lexer_debug_stream, token);
609 putc ('\n', cp_lexer_debug_stream);
612 return token;
615 /* Return the next token, and advance the lexer's next_token pointer
616 to point to the next non-purged token. */
618 static cp_token *
619 cp_lexer_consume_token (cp_lexer* lexer)
621 cp_token *token = lexer->next_token;
623 gcc_assert (token != &eof_token);
624 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
628 lexer->next_token++;
629 if (lexer->next_token == lexer->last_token)
631 lexer->next_token = (cp_token *)&eof_token;
632 break;
636 while (lexer->next_token->type == CPP_PURGED);
638 cp_lexer_set_source_position_from_token (token);
640 /* Provide debugging output. */
641 if (cp_lexer_debugging_p (lexer))
643 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
644 cp_lexer_print_token (cp_lexer_debug_stream, token);
645 putc ('\n', cp_lexer_debug_stream);
648 return token;
651 /* Permanently remove the next token from the token stream, and
652 advance the next_token pointer to refer to the next non-purged
653 token. */
655 static void
656 cp_lexer_purge_token (cp_lexer *lexer)
658 cp_token *tok = lexer->next_token;
660 gcc_assert (tok != &eof_token);
661 tok->type = CPP_PURGED;
662 tok->location = UNKNOWN_LOCATION;
663 tok->u.value = NULL_TREE;
664 tok->keyword = RID_MAX;
668 tok++;
669 if (tok == lexer->last_token)
671 tok = (cp_token *)&eof_token;
672 break;
675 while (tok->type == CPP_PURGED);
676 lexer->next_token = tok;
679 /* Permanently remove all tokens after TOK, up to, but not
680 including, the token that will be returned next by
681 cp_lexer_peek_token. */
683 static void
684 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
686 cp_token *peek = lexer->next_token;
688 if (peek == &eof_token)
689 peek = lexer->last_token;
691 gcc_assert (tok < peek);
693 for ( tok += 1; tok != peek; tok += 1)
695 tok->type = CPP_PURGED;
696 tok->location = UNKNOWN_LOCATION;
697 tok->u.value = NULL_TREE;
698 tok->keyword = RID_MAX;
702 /* Begin saving tokens. All tokens consumed after this point will be
703 preserved. */
705 static void
706 cp_lexer_save_tokens (cp_lexer* lexer)
708 /* Provide debugging output. */
709 if (cp_lexer_debugging_p (lexer))
710 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
712 VEC_safe_push (cp_token_position, heap,
713 lexer->saved_tokens, lexer->next_token);
716 /* Commit to the portion of the token stream most recently saved. */
718 static void
719 cp_lexer_commit_tokens (cp_lexer* lexer)
721 /* Provide debugging output. */
722 if (cp_lexer_debugging_p (lexer))
723 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
725 VEC_pop (cp_token_position, lexer->saved_tokens);
728 /* Return all tokens saved since the last call to cp_lexer_save_tokens
729 to the token stream. Stop saving tokens. */
731 static void
732 cp_lexer_rollback_tokens (cp_lexer* lexer)
734 /* Provide debugging output. */
735 if (cp_lexer_debugging_p (lexer))
736 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
738 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
741 /* Print a representation of the TOKEN on the STREAM. */
743 #ifdef ENABLE_CHECKING
745 static void
746 cp_lexer_print_token (FILE * stream, cp_token *token)
748 /* We don't use cpp_type2name here because the parser defines
749 a few tokens of its own. */
750 static const char *const token_names[] = {
751 /* cpplib-defined token types */
752 #define OP(e, s) #e,
753 #define TK(e, s) #e,
754 TTYPE_TABLE
755 #undef OP
756 #undef TK
757 /* C++ parser token types - see "Manifest constants", above. */
758 "KEYWORD",
759 "TEMPLATE_ID",
760 "NESTED_NAME_SPECIFIER",
761 "PURGED"
764 /* If we have a name for the token, print it out. Otherwise, we
765 simply give the numeric code. */
766 gcc_assert (token->type < ARRAY_SIZE(token_names));
767 fputs (token_names[token->type], stream);
769 /* For some tokens, print the associated data. */
770 switch (token->type)
772 case CPP_KEYWORD:
773 /* Some keywords have a value that is not an IDENTIFIER_NODE.
774 For example, `struct' is mapped to an INTEGER_CST. */
775 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
776 break;
777 /* else fall through */
778 case CPP_NAME:
779 fputs (IDENTIFIER_POINTER (token->u.value), stream);
780 break;
782 case CPP_STRING:
783 case CPP_WSTRING:
784 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
785 break;
787 default:
788 break;
792 /* Start emitting debugging information. */
794 static void
795 cp_lexer_start_debugging (cp_lexer* lexer)
797 lexer->debugging_p = true;
800 /* Stop emitting debugging information. */
802 static void
803 cp_lexer_stop_debugging (cp_lexer* lexer)
805 lexer->debugging_p = false;
808 #endif /* ENABLE_CHECKING */
810 /* Create a new cp_token_cache, representing a range of tokens. */
812 static cp_token_cache *
813 cp_token_cache_new (cp_token *first, cp_token *last)
815 cp_token_cache *cache = GGC_NEW (cp_token_cache);
816 cache->first = first;
817 cache->last = last;
818 return cache;
822 /* Decl-specifiers. */
824 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
826 static void
827 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
829 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
832 /* Declarators. */
834 /* Nothing other than the parser should be creating declarators;
835 declarators are a semi-syntactic representation of C++ entities.
836 Other parts of the front end that need to create entities (like
837 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
839 static cp_declarator *make_call_declarator
840 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
841 static cp_declarator *make_array_declarator
842 (cp_declarator *, tree);
843 static cp_declarator *make_pointer_declarator
844 (cp_cv_quals, cp_declarator *);
845 static cp_declarator *make_reference_declarator
846 (cp_cv_quals, cp_declarator *);
847 static cp_parameter_declarator *make_parameter_declarator
848 (cp_decl_specifier_seq *, cp_declarator *, tree);
849 static cp_declarator *make_ptrmem_declarator
850 (cp_cv_quals, tree, cp_declarator *);
852 /* An erroneous declarator. */
853 static cp_declarator *cp_error_declarator;
855 /* The obstack on which declarators and related data structures are
856 allocated. */
857 static struct obstack declarator_obstack;
859 /* Alloc BYTES from the declarator memory pool. */
861 static inline void *
862 alloc_declarator (size_t bytes)
864 return obstack_alloc (&declarator_obstack, bytes);
867 /* Allocate a declarator of the indicated KIND. Clear fields that are
868 common to all declarators. */
870 static cp_declarator *
871 make_declarator (cp_declarator_kind kind)
873 cp_declarator *declarator;
875 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
876 declarator->kind = kind;
877 declarator->attributes = NULL_TREE;
878 declarator->declarator = NULL;
880 return declarator;
883 /* Make a declarator for a generalized identifier. If
884 QUALIFYING_SCOPE is non-NULL, the identifier is
885 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
886 UNQUALIFIED_NAME. SFK indicates the kind of special function this
887 is, if any. */
889 static cp_declarator *
890 make_id_declarator (tree qualifying_scope, tree unqualified_name,
891 special_function_kind sfk)
893 cp_declarator *declarator;
895 /* It is valid to write:
897 class C { void f(); };
898 typedef C D;
899 void D::f();
901 The standard is not clear about whether `typedef const C D' is
902 legal; as of 2002-09-15 the committee is considering that
903 question. EDG 3.0 allows that syntax. Therefore, we do as
904 well. */
905 if (qualifying_scope && TYPE_P (qualifying_scope))
906 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
908 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
909 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
910 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
912 declarator = make_declarator (cdk_id);
913 declarator->u.id.qualifying_scope = qualifying_scope;
914 declarator->u.id.unqualified_name = unqualified_name;
915 declarator->u.id.sfk = sfk;
917 return declarator;
920 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
921 of modifiers such as const or volatile to apply to the pointer
922 type, represented as identifiers. */
924 cp_declarator *
925 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
927 cp_declarator *declarator;
929 declarator = make_declarator (cdk_pointer);
930 declarator->declarator = target;
931 declarator->u.pointer.qualifiers = cv_qualifiers;
932 declarator->u.pointer.class_type = NULL_TREE;
934 return declarator;
937 /* Like make_pointer_declarator -- but for references. */
939 cp_declarator *
940 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
942 cp_declarator *declarator;
944 declarator = make_declarator (cdk_reference);
945 declarator->declarator = target;
946 declarator->u.pointer.qualifiers = cv_qualifiers;
947 declarator->u.pointer.class_type = NULL_TREE;
949 return declarator;
952 /* Like make_pointer_declarator -- but for a pointer to a non-static
953 member of CLASS_TYPE. */
955 cp_declarator *
956 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
957 cp_declarator *pointee)
959 cp_declarator *declarator;
961 declarator = make_declarator (cdk_ptrmem);
962 declarator->declarator = pointee;
963 declarator->u.pointer.qualifiers = cv_qualifiers;
964 declarator->u.pointer.class_type = class_type;
966 return declarator;
969 /* Make a declarator for the function given by TARGET, with the
970 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
971 "const"-qualified member function. The EXCEPTION_SPECIFICATION
972 indicates what exceptions can be thrown. */
974 cp_declarator *
975 make_call_declarator (cp_declarator *target,
976 cp_parameter_declarator *parms,
977 cp_cv_quals cv_qualifiers,
978 tree exception_specification)
980 cp_declarator *declarator;
982 declarator = make_declarator (cdk_function);
983 declarator->declarator = target;
984 declarator->u.function.parameters = parms;
985 declarator->u.function.qualifiers = cv_qualifiers;
986 declarator->u.function.exception_specification = exception_specification;
988 return declarator;
991 /* Make a declarator for an array of BOUNDS elements, each of which is
992 defined by ELEMENT. */
994 cp_declarator *
995 make_array_declarator (cp_declarator *element, tree bounds)
997 cp_declarator *declarator;
999 declarator = make_declarator (cdk_array);
1000 declarator->declarator = element;
1001 declarator->u.array.bounds = bounds;
1003 return declarator;
1006 cp_parameter_declarator *no_parameters;
1008 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1009 DECLARATOR and DEFAULT_ARGUMENT. */
1011 cp_parameter_declarator *
1012 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1013 cp_declarator *declarator,
1014 tree default_argument)
1016 cp_parameter_declarator *parameter;
1018 parameter = ((cp_parameter_declarator *)
1019 alloc_declarator (sizeof (cp_parameter_declarator)));
1020 parameter->next = NULL;
1021 if (decl_specifiers)
1022 parameter->decl_specifiers = *decl_specifiers;
1023 else
1024 clear_decl_specs (&parameter->decl_specifiers);
1025 parameter->declarator = declarator;
1026 parameter->default_argument = default_argument;
1027 parameter->ellipsis_p = false;
1029 return parameter;
1032 /* Returns true iff DECLARATOR is a declaration for a function. */
1034 static bool
1035 function_declarator_p (const cp_declarator *declarator)
1037 while (declarator)
1039 if (declarator->kind == cdk_function
1040 && declarator->declarator->kind == cdk_id)
1041 return true;
1042 if (declarator->kind == cdk_id
1043 || declarator->kind == cdk_error)
1044 return false;
1045 declarator = declarator->declarator;
1047 return false;
1050 /* The parser. */
1052 /* Overview
1053 --------
1055 A cp_parser parses the token stream as specified by the C++
1056 grammar. Its job is purely parsing, not semantic analysis. For
1057 example, the parser breaks the token stream into declarators,
1058 expressions, statements, and other similar syntactic constructs.
1059 It does not check that the types of the expressions on either side
1060 of an assignment-statement are compatible, or that a function is
1061 not declared with a parameter of type `void'.
1063 The parser invokes routines elsewhere in the compiler to perform
1064 semantic analysis and to build up the abstract syntax tree for the
1065 code processed.
1067 The parser (and the template instantiation code, which is, in a
1068 way, a close relative of parsing) are the only parts of the
1069 compiler that should be calling push_scope and pop_scope, or
1070 related functions. The parser (and template instantiation code)
1071 keeps track of what scope is presently active; everything else
1072 should simply honor that. (The code that generates static
1073 initializers may also need to set the scope, in order to check
1074 access control correctly when emitting the initializers.)
1076 Methodology
1077 -----------
1079 The parser is of the standard recursive-descent variety. Upcoming
1080 tokens in the token stream are examined in order to determine which
1081 production to use when parsing a non-terminal. Some C++ constructs
1082 require arbitrary look ahead to disambiguate. For example, it is
1083 impossible, in the general case, to tell whether a statement is an
1084 expression or declaration without scanning the entire statement.
1085 Therefore, the parser is capable of "parsing tentatively." When the
1086 parser is not sure what construct comes next, it enters this mode.
1087 Then, while we attempt to parse the construct, the parser queues up
1088 error messages, rather than issuing them immediately, and saves the
1089 tokens it consumes. If the construct is parsed successfully, the
1090 parser "commits", i.e., it issues any queued error messages and
1091 the tokens that were being preserved are permanently discarded.
1092 If, however, the construct is not parsed successfully, the parser
1093 rolls back its state completely so that it can resume parsing using
1094 a different alternative.
1096 Future Improvements
1097 -------------------
1099 The performance of the parser could probably be improved substantially.
1100 We could often eliminate the need to parse tentatively by looking ahead
1101 a little bit. In some places, this approach might not entirely eliminate
1102 the need to parse tentatively, but it might still speed up the average
1103 case. */
1105 /* Flags that are passed to some parsing functions. These values can
1106 be bitwise-ored together. */
1108 typedef enum cp_parser_flags
1110 /* No flags. */
1111 CP_PARSER_FLAGS_NONE = 0x0,
1112 /* The construct is optional. If it is not present, then no error
1113 should be issued. */
1114 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1115 /* When parsing a type-specifier, do not allow user-defined types. */
1116 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1117 } cp_parser_flags;
1119 /* The different kinds of declarators we want to parse. */
1121 typedef enum cp_parser_declarator_kind
1123 /* We want an abstract declarator. */
1124 CP_PARSER_DECLARATOR_ABSTRACT,
1125 /* We want a named declarator. */
1126 CP_PARSER_DECLARATOR_NAMED,
1127 /* We don't mind, but the name must be an unqualified-id. */
1128 CP_PARSER_DECLARATOR_EITHER
1129 } cp_parser_declarator_kind;
1131 /* The precedence values used to parse binary expressions. The minimum value
1132 of PREC must be 1, because zero is reserved to quickly discriminate
1133 binary operators from other tokens. */
1135 enum cp_parser_prec
1137 PREC_NOT_OPERATOR,
1138 PREC_LOGICAL_OR_EXPRESSION,
1139 PREC_LOGICAL_AND_EXPRESSION,
1140 PREC_INCLUSIVE_OR_EXPRESSION,
1141 PREC_EXCLUSIVE_OR_EXPRESSION,
1142 PREC_AND_EXPRESSION,
1143 PREC_EQUALITY_EXPRESSION,
1144 PREC_RELATIONAL_EXPRESSION,
1145 PREC_SHIFT_EXPRESSION,
1146 PREC_ADDITIVE_EXPRESSION,
1147 PREC_MULTIPLICATIVE_EXPRESSION,
1148 PREC_PM_EXPRESSION,
1149 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1152 /* A mapping from a token type to a corresponding tree node type, with a
1153 precedence value. */
1155 typedef struct cp_parser_binary_operations_map_node
1157 /* The token type. */
1158 enum cpp_ttype token_type;
1159 /* The corresponding tree code. */
1160 enum tree_code tree_type;
1161 /* The precedence of this operator. */
1162 enum cp_parser_prec prec;
1163 } cp_parser_binary_operations_map_node;
1165 /* The status of a tentative parse. */
1167 typedef enum cp_parser_status_kind
1169 /* No errors have occurred. */
1170 CP_PARSER_STATUS_KIND_NO_ERROR,
1171 /* An error has occurred. */
1172 CP_PARSER_STATUS_KIND_ERROR,
1173 /* We are committed to this tentative parse, whether or not an error
1174 has occurred. */
1175 CP_PARSER_STATUS_KIND_COMMITTED
1176 } cp_parser_status_kind;
1178 typedef struct cp_parser_expression_stack_entry
1180 /* Left hand side of the binary operation we are currently
1181 parsing. */
1182 tree lhs;
1183 /* Original tree code for left hand side, if it was a binary
1184 expression itself (used for -Wparentheses). */
1185 enum tree_code lhs_type;
1186 /* Tree code for the binary operation we are parsing. */
1187 enum tree_code tree_type;
1188 /* Precedence of the binary operation we are parsing. */
1189 int prec;
1190 } cp_parser_expression_stack_entry;
1192 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1193 entries because precedence levels on the stack are monotonically
1194 increasing. */
1195 typedef struct cp_parser_expression_stack_entry
1196 cp_parser_expression_stack[NUM_PREC_VALUES];
1198 /* Context that is saved and restored when parsing tentatively. */
1199 typedef struct cp_parser_context GTY (())
1201 /* If this is a tentative parsing context, the status of the
1202 tentative parse. */
1203 enum cp_parser_status_kind status;
1204 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1205 that are looked up in this context must be looked up both in the
1206 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1207 the context of the containing expression. */
1208 tree object_type;
1210 /* The next parsing context in the stack. */
1211 struct cp_parser_context *next;
1212 } cp_parser_context;
1214 /* Prototypes. */
1216 /* Constructors and destructors. */
1218 static cp_parser_context *cp_parser_context_new
1219 (cp_parser_context *);
1221 /* Class variables. */
1223 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1225 /* The operator-precedence table used by cp_parser_binary_expression.
1226 Transformed into an associative array (binops_by_token) by
1227 cp_parser_new. */
1229 static const cp_parser_binary_operations_map_node binops[] = {
1230 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1231 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1233 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1234 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1235 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1237 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1238 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1240 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1241 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1243 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1244 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1245 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1246 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1248 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1249 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1251 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1253 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1255 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1257 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1259 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1262 /* The same as binops, but initialized by cp_parser_new so that
1263 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1264 for speed. */
1265 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1267 /* Constructors and destructors. */
1269 /* Construct a new context. The context below this one on the stack
1270 is given by NEXT. */
1272 static cp_parser_context *
1273 cp_parser_context_new (cp_parser_context* next)
1275 cp_parser_context *context;
1277 /* Allocate the storage. */
1278 if (cp_parser_context_free_list != NULL)
1280 /* Pull the first entry from the free list. */
1281 context = cp_parser_context_free_list;
1282 cp_parser_context_free_list = context->next;
1283 memset (context, 0, sizeof (*context));
1285 else
1286 context = GGC_CNEW (cp_parser_context);
1288 /* No errors have occurred yet in this context. */
1289 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1290 /* If this is not the bottomost context, copy information that we
1291 need from the previous context. */
1292 if (next)
1294 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1295 expression, then we are parsing one in this context, too. */
1296 context->object_type = next->object_type;
1297 /* Thread the stack. */
1298 context->next = next;
1301 return context;
1304 /* The cp_parser structure represents the C++ parser. */
1306 typedef struct cp_parser GTY(())
1308 /* The lexer from which we are obtaining tokens. */
1309 cp_lexer *lexer;
1311 /* The scope in which names should be looked up. If NULL_TREE, then
1312 we look up names in the scope that is currently open in the
1313 source program. If non-NULL, this is either a TYPE or
1314 NAMESPACE_DECL for the scope in which we should look. It can
1315 also be ERROR_MARK, when we've parsed a bogus scope.
1317 This value is not cleared automatically after a name is looked
1318 up, so we must be careful to clear it before starting a new look
1319 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1320 will look up `Z' in the scope of `X', rather than the current
1321 scope.) Unfortunately, it is difficult to tell when name lookup
1322 is complete, because we sometimes peek at a token, look it up,
1323 and then decide not to consume it. */
1324 tree scope;
1326 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1327 last lookup took place. OBJECT_SCOPE is used if an expression
1328 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1329 respectively. QUALIFYING_SCOPE is used for an expression of the
1330 form "X::Y"; it refers to X. */
1331 tree object_scope;
1332 tree qualifying_scope;
1334 /* A stack of parsing contexts. All but the bottom entry on the
1335 stack will be tentative contexts.
1337 We parse tentatively in order to determine which construct is in
1338 use in some situations. For example, in order to determine
1339 whether a statement is an expression-statement or a
1340 declaration-statement we parse it tentatively as a
1341 declaration-statement. If that fails, we then reparse the same
1342 token stream as an expression-statement. */
1343 cp_parser_context *context;
1345 /* True if we are parsing GNU C++. If this flag is not set, then
1346 GNU extensions are not recognized. */
1347 bool allow_gnu_extensions_p;
1349 /* TRUE if the `>' token should be interpreted as the greater-than
1350 operator. FALSE if it is the end of a template-id or
1351 template-parameter-list. */
1352 bool greater_than_is_operator_p;
1354 /* TRUE if default arguments are allowed within a parameter list
1355 that starts at this point. FALSE if only a gnu extension makes
1356 them permissible. */
1357 bool default_arg_ok_p;
1359 /* TRUE if we are parsing an integral constant-expression. See
1360 [expr.const] for a precise definition. */
1361 bool integral_constant_expression_p;
1363 /* TRUE if we are parsing an integral constant-expression -- but a
1364 non-constant expression should be permitted as well. This flag
1365 is used when parsing an array bound so that GNU variable-length
1366 arrays are tolerated. */
1367 bool allow_non_integral_constant_expression_p;
1369 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1370 been seen that makes the expression non-constant. */
1371 bool non_integral_constant_expression_p;
1373 /* TRUE if local variable names and `this' are forbidden in the
1374 current context. */
1375 bool local_variables_forbidden_p;
1377 /* TRUE if the declaration we are parsing is part of a
1378 linkage-specification of the form `extern string-literal
1379 declaration'. */
1380 bool in_unbraced_linkage_specification_p;
1382 /* TRUE if we are presently parsing a declarator, after the
1383 direct-declarator. */
1384 bool in_declarator_p;
1386 /* TRUE if we are presently parsing a template-argument-list. */
1387 bool in_template_argument_list_p;
1389 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1390 to IN_OMP_BLOCK if parsing OpenMP structured block and
1391 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1392 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1393 iteration-statement, OpenMP block or loop within that switch. */
1394 #define IN_SWITCH_STMT 1
1395 #define IN_ITERATION_STMT 2
1396 #define IN_OMP_BLOCK 4
1397 #define IN_OMP_FOR 8
1398 unsigned char in_statement;
1400 /* TRUE if we are presently parsing the body of a switch statement.
1401 Note that this doesn't quite overlap with in_statement above.
1402 The difference relates to giving the right sets of error messages:
1403 "case not in switch" vs "break statement used with OpenMP...". */
1404 bool in_switch_statement_p;
1406 /* TRUE if we are parsing a type-id in an expression context. In
1407 such a situation, both "type (expr)" and "type (type)" are valid
1408 alternatives. */
1409 bool in_type_id_in_expr_p;
1411 /* TRUE if we are currently in a header file where declarations are
1412 implicitly extern "C". */
1413 bool implicit_extern_c;
1415 /* TRUE if strings in expressions should be translated to the execution
1416 character set. */
1417 bool translate_strings_p;
1419 /* TRUE if we are presently parsing the body of a function, but not
1420 a local class. */
1421 bool in_function_body;
1423 /* If non-NULL, then we are parsing a construct where new type
1424 definitions are not permitted. The string stored here will be
1425 issued as an error message if a type is defined. */
1426 const char *type_definition_forbidden_message;
1428 /* A list of lists. The outer list is a stack, used for member
1429 functions of local classes. At each level there are two sub-list,
1430 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1431 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1432 TREE_VALUE's. The functions are chained in reverse declaration
1433 order.
1435 The TREE_PURPOSE sublist contains those functions with default
1436 arguments that need post processing, and the TREE_VALUE sublist
1437 contains those functions with definitions that need post
1438 processing.
1440 These lists can only be processed once the outermost class being
1441 defined is complete. */
1442 tree unparsed_functions_queues;
1444 /* The number of classes whose definitions are currently in
1445 progress. */
1446 unsigned num_classes_being_defined;
1448 /* The number of template parameter lists that apply directly to the
1449 current declaration. */
1450 unsigned num_template_parameter_lists;
1451 } cp_parser;
1453 /* Prototypes. */
1455 /* Constructors and destructors. */
1457 static cp_parser *cp_parser_new
1458 (void);
1460 /* Routines to parse various constructs.
1462 Those that return `tree' will return the error_mark_node (rather
1463 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1464 Sometimes, they will return an ordinary node if error-recovery was
1465 attempted, even though a parse error occurred. So, to check
1466 whether or not a parse error occurred, you should always use
1467 cp_parser_error_occurred. If the construct is optional (indicated
1468 either by an `_opt' in the name of the function that does the
1469 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1470 the construct is not present. */
1472 /* Lexical conventions [gram.lex] */
1474 static tree cp_parser_identifier
1475 (cp_parser *);
1476 static tree cp_parser_string_literal
1477 (cp_parser *, bool, bool);
1479 /* Basic concepts [gram.basic] */
1481 static bool cp_parser_translation_unit
1482 (cp_parser *);
1484 /* Expressions [gram.expr] */
1486 static tree cp_parser_primary_expression
1487 (cp_parser *, bool, bool, bool, cp_id_kind *);
1488 static tree cp_parser_id_expression
1489 (cp_parser *, bool, bool, bool *, bool, bool);
1490 static tree cp_parser_unqualified_id
1491 (cp_parser *, bool, bool, bool, bool);
1492 static tree cp_parser_nested_name_specifier_opt
1493 (cp_parser *, bool, bool, bool, bool);
1494 static tree cp_parser_nested_name_specifier
1495 (cp_parser *, bool, bool, bool, bool);
1496 static tree cp_parser_class_or_namespace_name
1497 (cp_parser *, bool, bool, bool, bool, bool);
1498 static tree cp_parser_postfix_expression
1499 (cp_parser *, bool, bool);
1500 static tree cp_parser_postfix_open_square_expression
1501 (cp_parser *, tree, bool);
1502 static tree cp_parser_postfix_dot_deref_expression
1503 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1504 static tree cp_parser_parenthesized_expression_list
1505 (cp_parser *, bool, bool, bool *);
1506 static void cp_parser_pseudo_destructor_name
1507 (cp_parser *, tree *, tree *);
1508 static tree cp_parser_unary_expression
1509 (cp_parser *, bool, bool);
1510 static enum tree_code cp_parser_unary_operator
1511 (cp_token *);
1512 static tree cp_parser_new_expression
1513 (cp_parser *);
1514 static tree cp_parser_new_placement
1515 (cp_parser *);
1516 static tree cp_parser_new_type_id
1517 (cp_parser *, tree *);
1518 static cp_declarator *cp_parser_new_declarator_opt
1519 (cp_parser *);
1520 static cp_declarator *cp_parser_direct_new_declarator
1521 (cp_parser *);
1522 static tree cp_parser_new_initializer
1523 (cp_parser *);
1524 static tree cp_parser_delete_expression
1525 (cp_parser *);
1526 static tree cp_parser_cast_expression
1527 (cp_parser *, bool, bool);
1528 static tree cp_parser_binary_expression
1529 (cp_parser *, bool);
1530 static tree cp_parser_question_colon_clause
1531 (cp_parser *, tree);
1532 static tree cp_parser_assignment_expression
1533 (cp_parser *, bool);
1534 static enum tree_code cp_parser_assignment_operator_opt
1535 (cp_parser *);
1536 static tree cp_parser_expression
1537 (cp_parser *, bool);
1538 static tree cp_parser_constant_expression
1539 (cp_parser *, bool, bool *);
1540 static tree cp_parser_builtin_offsetof
1541 (cp_parser *);
1543 /* Statements [gram.stmt.stmt] */
1545 static void cp_parser_statement
1546 (cp_parser *, tree, bool, bool *);
1547 static void cp_parser_label_for_labeled_statement
1548 (cp_parser *);
1549 static tree cp_parser_expression_statement
1550 (cp_parser *, tree);
1551 static tree cp_parser_compound_statement
1552 (cp_parser *, tree, bool);
1553 static void cp_parser_statement_seq_opt
1554 (cp_parser *, tree);
1555 static tree cp_parser_selection_statement
1556 (cp_parser *, bool *);
1557 static tree cp_parser_condition
1558 (cp_parser *);
1559 static tree cp_parser_iteration_statement
1560 (cp_parser *);
1561 static void cp_parser_for_init_statement
1562 (cp_parser *);
1563 static tree cp_parser_jump_statement
1564 (cp_parser *);
1565 static void cp_parser_declaration_statement
1566 (cp_parser *);
1568 static tree cp_parser_implicitly_scoped_statement
1569 (cp_parser *, bool *);
1570 static void cp_parser_already_scoped_statement
1571 (cp_parser *);
1573 /* Declarations [gram.dcl.dcl] */
1575 static void cp_parser_declaration_seq_opt
1576 (cp_parser *);
1577 static void cp_parser_declaration
1578 (cp_parser *);
1579 static void cp_parser_block_declaration
1580 (cp_parser *, bool);
1581 static void cp_parser_simple_declaration
1582 (cp_parser *, bool);
1583 static void cp_parser_decl_specifier_seq
1584 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1585 static tree cp_parser_storage_class_specifier_opt
1586 (cp_parser *);
1587 static tree cp_parser_function_specifier_opt
1588 (cp_parser *, cp_decl_specifier_seq *);
1589 static tree cp_parser_type_specifier
1590 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1591 int *, bool *);
1592 static tree cp_parser_simple_type_specifier
1593 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1594 static tree cp_parser_type_name
1595 (cp_parser *);
1596 static tree cp_parser_elaborated_type_specifier
1597 (cp_parser *, bool, bool);
1598 static tree cp_parser_enum_specifier
1599 (cp_parser *);
1600 static void cp_parser_enumerator_list
1601 (cp_parser *, tree);
1602 static void cp_parser_enumerator_definition
1603 (cp_parser *, tree);
1604 static tree cp_parser_namespace_name
1605 (cp_parser *);
1606 static void cp_parser_namespace_definition
1607 (cp_parser *);
1608 static void cp_parser_namespace_body
1609 (cp_parser *);
1610 static tree cp_parser_qualified_namespace_specifier
1611 (cp_parser *);
1612 static void cp_parser_namespace_alias_definition
1613 (cp_parser *);
1614 static bool cp_parser_using_declaration
1615 (cp_parser *, bool);
1616 static void cp_parser_using_directive
1617 (cp_parser *);
1618 static void cp_parser_asm_definition
1619 (cp_parser *);
1620 static void cp_parser_linkage_specification
1621 (cp_parser *);
1622 static void cp_parser_static_assert
1623 (cp_parser *, bool);
1625 /* Declarators [gram.dcl.decl] */
1627 static tree cp_parser_init_declarator
1628 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1629 static cp_declarator *cp_parser_declarator
1630 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1631 static cp_declarator *cp_parser_direct_declarator
1632 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1633 static enum tree_code cp_parser_ptr_operator
1634 (cp_parser *, tree *, cp_cv_quals *);
1635 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1636 (cp_parser *);
1637 static tree cp_parser_declarator_id
1638 (cp_parser *, bool);
1639 static tree cp_parser_type_id
1640 (cp_parser *);
1641 static void cp_parser_type_specifier_seq
1642 (cp_parser *, bool, cp_decl_specifier_seq *);
1643 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1644 (cp_parser *);
1645 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1646 (cp_parser *, bool *);
1647 static cp_parameter_declarator *cp_parser_parameter_declaration
1648 (cp_parser *, bool, bool *);
1649 static void cp_parser_function_body
1650 (cp_parser *);
1651 static tree cp_parser_initializer
1652 (cp_parser *, bool *, bool *);
1653 static tree cp_parser_initializer_clause
1654 (cp_parser *, bool *);
1655 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1656 (cp_parser *, bool *);
1658 static bool cp_parser_ctor_initializer_opt_and_function_body
1659 (cp_parser *);
1661 /* Classes [gram.class] */
1663 static tree cp_parser_class_name
1664 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1665 static tree cp_parser_class_specifier
1666 (cp_parser *);
1667 static tree cp_parser_class_head
1668 (cp_parser *, bool *, tree *, tree *);
1669 static enum tag_types cp_parser_class_key
1670 (cp_parser *);
1671 static void cp_parser_member_specification_opt
1672 (cp_parser *);
1673 static void cp_parser_member_declaration
1674 (cp_parser *);
1675 static tree cp_parser_pure_specifier
1676 (cp_parser *);
1677 static tree cp_parser_constant_initializer
1678 (cp_parser *);
1680 /* Derived classes [gram.class.derived] */
1682 static tree cp_parser_base_clause
1683 (cp_parser *);
1684 static tree cp_parser_base_specifier
1685 (cp_parser *);
1687 /* Special member functions [gram.special] */
1689 static tree cp_parser_conversion_function_id
1690 (cp_parser *);
1691 static tree cp_parser_conversion_type_id
1692 (cp_parser *);
1693 static cp_declarator *cp_parser_conversion_declarator_opt
1694 (cp_parser *);
1695 static bool cp_parser_ctor_initializer_opt
1696 (cp_parser *);
1697 static void cp_parser_mem_initializer_list
1698 (cp_parser *);
1699 static tree cp_parser_mem_initializer
1700 (cp_parser *);
1701 static tree cp_parser_mem_initializer_id
1702 (cp_parser *);
1704 /* Overloading [gram.over] */
1706 static tree cp_parser_operator_function_id
1707 (cp_parser *);
1708 static tree cp_parser_operator
1709 (cp_parser *);
1711 /* Templates [gram.temp] */
1713 static void cp_parser_template_declaration
1714 (cp_parser *, bool);
1715 static tree cp_parser_template_parameter_list
1716 (cp_parser *);
1717 static tree cp_parser_template_parameter
1718 (cp_parser *, bool *);
1719 static tree cp_parser_type_parameter
1720 (cp_parser *);
1721 static tree cp_parser_template_id
1722 (cp_parser *, bool, bool, bool);
1723 static tree cp_parser_template_name
1724 (cp_parser *, bool, bool, bool, bool *);
1725 static tree cp_parser_template_argument_list
1726 (cp_parser *);
1727 static tree cp_parser_template_argument
1728 (cp_parser *);
1729 static void cp_parser_explicit_instantiation
1730 (cp_parser *);
1731 static void cp_parser_explicit_specialization
1732 (cp_parser *);
1734 /* Exception handling [gram.exception] */
1736 static tree cp_parser_try_block
1737 (cp_parser *);
1738 static bool cp_parser_function_try_block
1739 (cp_parser *);
1740 static void cp_parser_handler_seq
1741 (cp_parser *);
1742 static void cp_parser_handler
1743 (cp_parser *);
1744 static tree cp_parser_exception_declaration
1745 (cp_parser *);
1746 static tree cp_parser_throw_expression
1747 (cp_parser *);
1748 static tree cp_parser_exception_specification_opt
1749 (cp_parser *);
1750 static tree cp_parser_type_id_list
1751 (cp_parser *);
1753 /* GNU Extensions */
1755 static tree cp_parser_asm_specification_opt
1756 (cp_parser *);
1757 static tree cp_parser_asm_operand_list
1758 (cp_parser *);
1759 static tree cp_parser_asm_clobber_list
1760 (cp_parser *);
1761 static tree cp_parser_attributes_opt
1762 (cp_parser *);
1763 static tree cp_parser_attribute_list
1764 (cp_parser *);
1765 static bool cp_parser_extension_opt
1766 (cp_parser *, int *);
1767 static void cp_parser_label_declaration
1768 (cp_parser *);
1770 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1771 static bool cp_parser_pragma
1772 (cp_parser *, enum pragma_context);
1774 /* Objective-C++ Productions */
1776 static tree cp_parser_objc_message_receiver
1777 (cp_parser *);
1778 static tree cp_parser_objc_message_args
1779 (cp_parser *);
1780 static tree cp_parser_objc_message_expression
1781 (cp_parser *);
1782 static tree cp_parser_objc_encode_expression
1783 (cp_parser *);
1784 static tree cp_parser_objc_defs_expression
1785 (cp_parser *);
1786 static tree cp_parser_objc_protocol_expression
1787 (cp_parser *);
1788 static tree cp_parser_objc_selector_expression
1789 (cp_parser *);
1790 static tree cp_parser_objc_expression
1791 (cp_parser *);
1792 static bool cp_parser_objc_selector_p
1793 (enum cpp_ttype);
1794 static tree cp_parser_objc_selector
1795 (cp_parser *);
1796 static tree cp_parser_objc_protocol_refs_opt
1797 (cp_parser *);
1798 static void cp_parser_objc_declaration
1799 (cp_parser *);
1800 static tree cp_parser_objc_statement
1801 (cp_parser *);
1803 /* Utility Routines */
1805 static tree cp_parser_lookup_name
1806 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1807 static tree cp_parser_lookup_name_simple
1808 (cp_parser *, tree);
1809 static tree cp_parser_maybe_treat_template_as_class
1810 (tree, bool);
1811 static bool cp_parser_check_declarator_template_parameters
1812 (cp_parser *, cp_declarator *);
1813 static bool cp_parser_check_template_parameters
1814 (cp_parser *, unsigned);
1815 static tree cp_parser_simple_cast_expression
1816 (cp_parser *);
1817 static tree cp_parser_global_scope_opt
1818 (cp_parser *, bool);
1819 static bool cp_parser_constructor_declarator_p
1820 (cp_parser *, bool);
1821 static tree cp_parser_function_definition_from_specifiers_and_declarator
1822 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1823 static tree cp_parser_function_definition_after_declarator
1824 (cp_parser *, bool);
1825 static void cp_parser_template_declaration_after_export
1826 (cp_parser *, bool);
1827 static void cp_parser_perform_template_parameter_access_checks
1828 (VEC (deferred_access_check,gc)*);
1829 static tree cp_parser_single_declaration
1830 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1831 static tree cp_parser_functional_cast
1832 (cp_parser *, tree);
1833 static tree cp_parser_save_member_function_body
1834 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1835 static tree cp_parser_enclosed_template_argument_list
1836 (cp_parser *);
1837 static void cp_parser_save_default_args
1838 (cp_parser *, tree);
1839 static void cp_parser_late_parsing_for_member
1840 (cp_parser *, tree);
1841 static void cp_parser_late_parsing_default_args
1842 (cp_parser *, tree);
1843 static tree cp_parser_sizeof_operand
1844 (cp_parser *, enum rid);
1845 static bool cp_parser_declares_only_class_p
1846 (cp_parser *);
1847 static void cp_parser_set_storage_class
1848 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1849 static void cp_parser_set_decl_spec_type
1850 (cp_decl_specifier_seq *, tree, bool);
1851 static bool cp_parser_friend_p
1852 (const cp_decl_specifier_seq *);
1853 static cp_token *cp_parser_require
1854 (cp_parser *, enum cpp_ttype, const char *);
1855 static cp_token *cp_parser_require_keyword
1856 (cp_parser *, enum rid, const char *);
1857 static bool cp_parser_token_starts_function_definition_p
1858 (cp_token *);
1859 static bool cp_parser_next_token_starts_class_definition_p
1860 (cp_parser *);
1861 static bool cp_parser_next_token_ends_template_argument_p
1862 (cp_parser *);
1863 static bool cp_parser_nth_token_starts_template_argument_list_p
1864 (cp_parser *, size_t);
1865 static enum tag_types cp_parser_token_is_class_key
1866 (cp_token *);
1867 static void cp_parser_check_class_key
1868 (enum tag_types, tree type);
1869 static void cp_parser_check_access_in_redeclaration
1870 (tree type);
1871 static bool cp_parser_optional_template_keyword
1872 (cp_parser *);
1873 static void cp_parser_pre_parsed_nested_name_specifier
1874 (cp_parser *);
1875 static void cp_parser_cache_group
1876 (cp_parser *, enum cpp_ttype, unsigned);
1877 static void cp_parser_parse_tentatively
1878 (cp_parser *);
1879 static void cp_parser_commit_to_tentative_parse
1880 (cp_parser *);
1881 static void cp_parser_abort_tentative_parse
1882 (cp_parser *);
1883 static bool cp_parser_parse_definitely
1884 (cp_parser *);
1885 static inline bool cp_parser_parsing_tentatively
1886 (cp_parser *);
1887 static bool cp_parser_uncommitted_to_tentative_parse_p
1888 (cp_parser *);
1889 static void cp_parser_error
1890 (cp_parser *, const char *);
1891 static void cp_parser_name_lookup_error
1892 (cp_parser *, tree, tree, const char *);
1893 static bool cp_parser_simulate_error
1894 (cp_parser *);
1895 static bool cp_parser_check_type_definition
1896 (cp_parser *);
1897 static void cp_parser_check_for_definition_in_return_type
1898 (cp_declarator *, tree);
1899 static void cp_parser_check_for_invalid_template_id
1900 (cp_parser *, tree);
1901 static bool cp_parser_non_integral_constant_expression
1902 (cp_parser *, const char *);
1903 static void cp_parser_diagnose_invalid_type_name
1904 (cp_parser *, tree, tree);
1905 static bool cp_parser_parse_and_diagnose_invalid_type_name
1906 (cp_parser *);
1907 static int cp_parser_skip_to_closing_parenthesis
1908 (cp_parser *, bool, bool, bool);
1909 static void cp_parser_skip_to_end_of_statement
1910 (cp_parser *);
1911 static void cp_parser_consume_semicolon_at_end_of_statement
1912 (cp_parser *);
1913 static void cp_parser_skip_to_end_of_block_or_statement
1914 (cp_parser *);
1915 static void cp_parser_skip_to_closing_brace
1916 (cp_parser *);
1917 static void cp_parser_skip_to_end_of_template_parameter_list
1918 (cp_parser *);
1919 static void cp_parser_skip_to_pragma_eol
1920 (cp_parser*, cp_token *);
1921 static bool cp_parser_error_occurred
1922 (cp_parser *);
1923 static bool cp_parser_allow_gnu_extensions_p
1924 (cp_parser *);
1925 static bool cp_parser_is_string_literal
1926 (cp_token *);
1927 static bool cp_parser_is_keyword
1928 (cp_token *, enum rid);
1929 static tree cp_parser_make_typename_type
1930 (cp_parser *, tree, tree);
1932 /* Returns nonzero if we are parsing tentatively. */
1934 static inline bool
1935 cp_parser_parsing_tentatively (cp_parser* parser)
1937 return parser->context->next != NULL;
1940 /* Returns nonzero if TOKEN is a string literal. */
1942 static bool
1943 cp_parser_is_string_literal (cp_token* token)
1945 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1948 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1950 static bool
1951 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1953 return token->keyword == keyword;
1956 /* If not parsing tentatively, issue a diagnostic of the form
1957 FILE:LINE: MESSAGE before TOKEN
1958 where TOKEN is the next token in the input stream. MESSAGE
1959 (specified by the caller) is usually of the form "expected
1960 OTHER-TOKEN". */
1962 static void
1963 cp_parser_error (cp_parser* parser, const char* message)
1965 if (!cp_parser_simulate_error (parser))
1967 cp_token *token = cp_lexer_peek_token (parser->lexer);
1968 /* This diagnostic makes more sense if it is tagged to the line
1969 of the token we just peeked at. */
1970 cp_lexer_set_source_position_from_token (token);
1972 if (token->type == CPP_PRAGMA)
1974 error ("%<#pragma%> is not allowed here");
1975 cp_parser_skip_to_pragma_eol (parser, token);
1976 return;
1979 c_parse_error (message,
1980 /* Because c_parser_error does not understand
1981 CPP_KEYWORD, keywords are treated like
1982 identifiers. */
1983 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1984 token->u.value);
1988 /* Issue an error about name-lookup failing. NAME is the
1989 IDENTIFIER_NODE DECL is the result of
1990 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1991 the thing that we hoped to find. */
1993 static void
1994 cp_parser_name_lookup_error (cp_parser* parser,
1995 tree name,
1996 tree decl,
1997 const char* desired)
1999 /* If name lookup completely failed, tell the user that NAME was not
2000 declared. */
2001 if (decl == error_mark_node)
2003 if (parser->scope && parser->scope != global_namespace)
2004 error ("%<%D::%D%> has not been declared",
2005 parser->scope, name);
2006 else if (parser->scope == global_namespace)
2007 error ("%<::%D%> has not been declared", name);
2008 else if (parser->object_scope
2009 && !CLASS_TYPE_P (parser->object_scope))
2010 error ("request for member %qD in non-class type %qT",
2011 name, parser->object_scope);
2012 else if (parser->object_scope)
2013 error ("%<%T::%D%> has not been declared",
2014 parser->object_scope, name);
2015 else
2016 error ("%qD has not been declared", name);
2018 else if (parser->scope && parser->scope != global_namespace)
2019 error ("%<%D::%D%> %s", parser->scope, name, desired);
2020 else if (parser->scope == global_namespace)
2021 error ("%<::%D%> %s", name, desired);
2022 else
2023 error ("%qD %s", name, desired);
2026 /* If we are parsing tentatively, remember that an error has occurred
2027 during this tentative parse. Returns true if the error was
2028 simulated; false if a message should be issued by the caller. */
2030 static bool
2031 cp_parser_simulate_error (cp_parser* parser)
2033 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2035 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2036 return true;
2038 return false;
2041 /* Check for repeated decl-specifiers. */
2043 static void
2044 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2046 cp_decl_spec ds;
2048 for (ds = ds_first; ds != ds_last; ++ds)
2050 unsigned count = decl_specs->specs[(int)ds];
2051 if (count < 2)
2052 continue;
2053 /* The "long" specifier is a special case because of "long long". */
2054 if (ds == ds_long)
2056 if (count > 2)
2057 error ("%<long long long%> is too long for GCC");
2058 else if (pedantic && !in_system_header && warn_long_long)
2059 pedwarn ("ISO C++ does not support %<long long%>");
2061 else if (count > 1)
2063 static const char *const decl_spec_names[] = {
2064 "signed",
2065 "unsigned",
2066 "short",
2067 "long",
2068 "const",
2069 "volatile",
2070 "restrict",
2071 "inline",
2072 "virtual",
2073 "explicit",
2074 "friend",
2075 "typedef",
2076 "__complex",
2077 "__thread"
2079 error ("duplicate %qs", decl_spec_names[(int)ds]);
2084 /* This function is called when a type is defined. If type
2085 definitions are forbidden at this point, an error message is
2086 issued. */
2088 static bool
2089 cp_parser_check_type_definition (cp_parser* parser)
2091 /* If types are forbidden here, issue a message. */
2092 if (parser->type_definition_forbidden_message)
2094 /* Use `%s' to print the string in case there are any escape
2095 characters in the message. */
2096 error ("%s", parser->type_definition_forbidden_message);
2097 return false;
2099 return true;
2102 /* This function is called when the DECLARATOR is processed. The TYPE
2103 was a type defined in the decl-specifiers. If it is invalid to
2104 define a type in the decl-specifiers for DECLARATOR, an error is
2105 issued. */
2107 static void
2108 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2109 tree type)
2111 /* [dcl.fct] forbids type definitions in return types.
2112 Unfortunately, it's not easy to know whether or not we are
2113 processing a return type until after the fact. */
2114 while (declarator
2115 && (declarator->kind == cdk_pointer
2116 || declarator->kind == cdk_reference
2117 || declarator->kind == cdk_ptrmem))
2118 declarator = declarator->declarator;
2119 if (declarator
2120 && declarator->kind == cdk_function)
2122 error ("new types may not be defined in a return type");
2123 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2124 type);
2128 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2129 "<" in any valid C++ program. If the next token is indeed "<",
2130 issue a message warning the user about what appears to be an
2131 invalid attempt to form a template-id. */
2133 static void
2134 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2135 tree type)
2137 cp_token_position start = 0;
2139 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2141 if (TYPE_P (type))
2142 error ("%qT is not a template", type);
2143 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2144 error ("%qE is not a template", type);
2145 else
2146 error ("invalid template-id");
2147 /* Remember the location of the invalid "<". */
2148 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2149 start = cp_lexer_token_position (parser->lexer, true);
2150 /* Consume the "<". */
2151 cp_lexer_consume_token (parser->lexer);
2152 /* Parse the template arguments. */
2153 cp_parser_enclosed_template_argument_list (parser);
2154 /* Permanently remove the invalid template arguments so that
2155 this error message is not issued again. */
2156 if (start)
2157 cp_lexer_purge_tokens_after (parser->lexer, start);
2161 /* If parsing an integral constant-expression, issue an error message
2162 about the fact that THING appeared and return true. Otherwise,
2163 return false. In either case, set
2164 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2166 static bool
2167 cp_parser_non_integral_constant_expression (cp_parser *parser,
2168 const char *thing)
2170 parser->non_integral_constant_expression_p = true;
2171 if (parser->integral_constant_expression_p)
2173 if (!parser->allow_non_integral_constant_expression_p)
2175 error ("%s cannot appear in a constant-expression", thing);
2176 return true;
2179 return false;
2182 /* Emit a diagnostic for an invalid type name. SCOPE is the
2183 qualifying scope (or NULL, if none) for ID. This function commits
2184 to the current active tentative parse, if any. (Otherwise, the
2185 problematic construct might be encountered again later, resulting
2186 in duplicate error messages.) */
2188 static void
2189 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2191 tree decl, old_scope;
2192 /* Try to lookup the identifier. */
2193 old_scope = parser->scope;
2194 parser->scope = scope;
2195 decl = cp_parser_lookup_name_simple (parser, id);
2196 parser->scope = old_scope;
2197 /* If the lookup found a template-name, it means that the user forgot
2198 to specify an argument list. Emit a useful error message. */
2199 if (TREE_CODE (decl) == TEMPLATE_DECL)
2200 error ("invalid use of template-name %qE without an argument list", decl);
2201 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2202 error ("invalid use of destructor %qD as a type", id);
2203 else if (TREE_CODE (decl) == TYPE_DECL)
2204 /* Something like 'unsigned A a;' */
2205 error ("invalid combination of multiple type-specifiers");
2206 else if (!parser->scope)
2208 /* Issue an error message. */
2209 error ("%qE does not name a type", id);
2210 /* If we're in a template class, it's possible that the user was
2211 referring to a type from a base class. For example:
2213 template <typename T> struct A { typedef T X; };
2214 template <typename T> struct B : public A<T> { X x; };
2216 The user should have said "typename A<T>::X". */
2217 if (processing_template_decl && current_class_type
2218 && TYPE_BINFO (current_class_type))
2220 tree b;
2222 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2224 b = TREE_CHAIN (b))
2226 tree base_type = BINFO_TYPE (b);
2227 if (CLASS_TYPE_P (base_type)
2228 && dependent_type_p (base_type))
2230 tree field;
2231 /* Go from a particular instantiation of the
2232 template (which will have an empty TYPE_FIELDs),
2233 to the main version. */
2234 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2235 for (field = TYPE_FIELDS (base_type);
2236 field;
2237 field = TREE_CHAIN (field))
2238 if (TREE_CODE (field) == TYPE_DECL
2239 && DECL_NAME (field) == id)
2241 inform ("(perhaps %<typename %T::%E%> was intended)",
2242 BINFO_TYPE (b), id);
2243 break;
2245 if (field)
2246 break;
2251 /* Here we diagnose qualified-ids where the scope is actually correct,
2252 but the identifier does not resolve to a valid type name. */
2253 else if (parser->scope != error_mark_node)
2255 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2256 error ("%qE in namespace %qE does not name a type",
2257 id, parser->scope);
2258 else if (TYPE_P (parser->scope))
2259 error ("%qE in class %qT does not name a type", id, parser->scope);
2260 else
2261 gcc_unreachable ();
2263 cp_parser_commit_to_tentative_parse (parser);
2266 /* Check for a common situation where a type-name should be present,
2267 but is not, and issue a sensible error message. Returns true if an
2268 invalid type-name was detected.
2270 The situation handled by this function are variable declarations of the
2271 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2272 Usually, `ID' should name a type, but if we got here it means that it
2273 does not. We try to emit the best possible error message depending on
2274 how exactly the id-expression looks like. */
2276 static bool
2277 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2279 tree id;
2281 cp_parser_parse_tentatively (parser);
2282 id = cp_parser_id_expression (parser,
2283 /*template_keyword_p=*/false,
2284 /*check_dependency_p=*/true,
2285 /*template_p=*/NULL,
2286 /*declarator_p=*/true,
2287 /*optional_p=*/false);
2288 /* After the id-expression, there should be a plain identifier,
2289 otherwise this is not a simple variable declaration. Also, if
2290 the scope is dependent, we cannot do much. */
2291 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2292 || (parser->scope && TYPE_P (parser->scope)
2293 && dependent_type_p (parser->scope)))
2295 cp_parser_abort_tentative_parse (parser);
2296 return false;
2298 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2299 return false;
2301 /* Emit a diagnostic for the invalid type. */
2302 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2303 /* Skip to the end of the declaration; there's no point in
2304 trying to process it. */
2305 cp_parser_skip_to_end_of_block_or_statement (parser);
2306 return true;
2309 /* Consume tokens up to, and including, the next non-nested closing `)'.
2310 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2311 are doing error recovery. Returns -1 if OR_COMMA is true and we
2312 found an unnested comma. */
2314 static int
2315 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2316 bool recovering,
2317 bool or_comma,
2318 bool consume_paren)
2320 unsigned paren_depth = 0;
2321 unsigned brace_depth = 0;
2323 if (recovering && !or_comma
2324 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2325 return 0;
2327 while (true)
2329 cp_token * token = cp_lexer_peek_token (parser->lexer);
2331 switch (token->type)
2333 case CPP_EOF:
2334 case CPP_PRAGMA_EOL:
2335 /* If we've run out of tokens, then there is no closing `)'. */
2336 return 0;
2338 case CPP_SEMICOLON:
2339 /* This matches the processing in skip_to_end_of_statement. */
2340 if (!brace_depth)
2341 return 0;
2342 break;
2344 case CPP_OPEN_BRACE:
2345 ++brace_depth;
2346 break;
2347 case CPP_CLOSE_BRACE:
2348 if (!brace_depth--)
2349 return 0;
2350 break;
2352 case CPP_COMMA:
2353 if (recovering && or_comma && !brace_depth && !paren_depth)
2354 return -1;
2355 break;
2357 case CPP_OPEN_PAREN:
2358 if (!brace_depth)
2359 ++paren_depth;
2360 break;
2362 case CPP_CLOSE_PAREN:
2363 if (!brace_depth && !paren_depth--)
2365 if (consume_paren)
2366 cp_lexer_consume_token (parser->lexer);
2367 return 1;
2369 break;
2371 default:
2372 break;
2375 /* Consume the token. */
2376 cp_lexer_consume_token (parser->lexer);
2380 /* Consume tokens until we reach the end of the current statement.
2381 Normally, that will be just before consuming a `;'. However, if a
2382 non-nested `}' comes first, then we stop before consuming that. */
2384 static void
2385 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2387 unsigned nesting_depth = 0;
2389 while (true)
2391 cp_token *token = cp_lexer_peek_token (parser->lexer);
2393 switch (token->type)
2395 case CPP_EOF:
2396 case CPP_PRAGMA_EOL:
2397 /* If we've run out of tokens, stop. */
2398 return;
2400 case CPP_SEMICOLON:
2401 /* If the next token is a `;', we have reached the end of the
2402 statement. */
2403 if (!nesting_depth)
2404 return;
2405 break;
2407 case CPP_CLOSE_BRACE:
2408 /* If this is a non-nested '}', stop before consuming it.
2409 That way, when confronted with something like:
2411 { 3 + }
2413 we stop before consuming the closing '}', even though we
2414 have not yet reached a `;'. */
2415 if (nesting_depth == 0)
2416 return;
2418 /* If it is the closing '}' for a block that we have
2419 scanned, stop -- but only after consuming the token.
2420 That way given:
2422 void f g () { ... }
2423 typedef int I;
2425 we will stop after the body of the erroneously declared
2426 function, but before consuming the following `typedef'
2427 declaration. */
2428 if (--nesting_depth == 0)
2430 cp_lexer_consume_token (parser->lexer);
2431 return;
2434 case CPP_OPEN_BRACE:
2435 ++nesting_depth;
2436 break;
2438 default:
2439 break;
2442 /* Consume the token. */
2443 cp_lexer_consume_token (parser->lexer);
2447 /* This function is called at the end of a statement or declaration.
2448 If the next token is a semicolon, it is consumed; otherwise, error
2449 recovery is attempted. */
2451 static void
2452 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2454 /* Look for the trailing `;'. */
2455 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2457 /* If there is additional (erroneous) input, skip to the end of
2458 the statement. */
2459 cp_parser_skip_to_end_of_statement (parser);
2460 /* If the next token is now a `;', consume it. */
2461 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2462 cp_lexer_consume_token (parser->lexer);
2466 /* Skip tokens until we have consumed an entire block, or until we
2467 have consumed a non-nested `;'. */
2469 static void
2470 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2472 int nesting_depth = 0;
2474 while (nesting_depth >= 0)
2476 cp_token *token = cp_lexer_peek_token (parser->lexer);
2478 switch (token->type)
2480 case CPP_EOF:
2481 case CPP_PRAGMA_EOL:
2482 /* If we've run out of tokens, stop. */
2483 return;
2485 case CPP_SEMICOLON:
2486 /* Stop if this is an unnested ';'. */
2487 if (!nesting_depth)
2488 nesting_depth = -1;
2489 break;
2491 case CPP_CLOSE_BRACE:
2492 /* Stop if this is an unnested '}', or closes the outermost
2493 nesting level. */
2494 nesting_depth--;
2495 if (!nesting_depth)
2496 nesting_depth = -1;
2497 break;
2499 case CPP_OPEN_BRACE:
2500 /* Nest. */
2501 nesting_depth++;
2502 break;
2504 default:
2505 break;
2508 /* Consume the token. */
2509 cp_lexer_consume_token (parser->lexer);
2513 /* Skip tokens until a non-nested closing curly brace is the next
2514 token. */
2516 static void
2517 cp_parser_skip_to_closing_brace (cp_parser *parser)
2519 unsigned nesting_depth = 0;
2521 while (true)
2523 cp_token *token = cp_lexer_peek_token (parser->lexer);
2525 switch (token->type)
2527 case CPP_EOF:
2528 case CPP_PRAGMA_EOL:
2529 /* If we've run out of tokens, stop. */
2530 return;
2532 case CPP_CLOSE_BRACE:
2533 /* If the next token is a non-nested `}', then we have reached
2534 the end of the current block. */
2535 if (nesting_depth-- == 0)
2536 return;
2537 break;
2539 case CPP_OPEN_BRACE:
2540 /* If it the next token is a `{', then we are entering a new
2541 block. Consume the entire block. */
2542 ++nesting_depth;
2543 break;
2545 default:
2546 break;
2549 /* Consume the token. */
2550 cp_lexer_consume_token (parser->lexer);
2554 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2555 parameter is the PRAGMA token, allowing us to purge the entire pragma
2556 sequence. */
2558 static void
2559 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2561 cp_token *token;
2563 parser->lexer->in_pragma = false;
2566 token = cp_lexer_consume_token (parser->lexer);
2567 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2569 /* Ensure that the pragma is not parsed again. */
2570 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2573 /* Require pragma end of line, resyncing with it as necessary. The
2574 arguments are as for cp_parser_skip_to_pragma_eol. */
2576 static void
2577 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2579 parser->lexer->in_pragma = false;
2580 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2581 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2584 /* This is a simple wrapper around make_typename_type. When the id is
2585 an unresolved identifier node, we can provide a superior diagnostic
2586 using cp_parser_diagnose_invalid_type_name. */
2588 static tree
2589 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2591 tree result;
2592 if (TREE_CODE (id) == IDENTIFIER_NODE)
2594 result = make_typename_type (scope, id, typename_type,
2595 /*complain=*/tf_none);
2596 if (result == error_mark_node)
2597 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2598 return result;
2600 return make_typename_type (scope, id, typename_type, tf_error);
2604 /* Create a new C++ parser. */
2606 static cp_parser *
2607 cp_parser_new (void)
2609 cp_parser *parser;
2610 cp_lexer *lexer;
2611 unsigned i;
2613 /* cp_lexer_new_main is called before calling ggc_alloc because
2614 cp_lexer_new_main might load a PCH file. */
2615 lexer = cp_lexer_new_main ();
2617 /* Initialize the binops_by_token so that we can get the tree
2618 directly from the token. */
2619 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2620 binops_by_token[binops[i].token_type] = binops[i];
2622 parser = GGC_CNEW (cp_parser);
2623 parser->lexer = lexer;
2624 parser->context = cp_parser_context_new (NULL);
2626 /* For now, we always accept GNU extensions. */
2627 parser->allow_gnu_extensions_p = 1;
2629 /* The `>' token is a greater-than operator, not the end of a
2630 template-id. */
2631 parser->greater_than_is_operator_p = true;
2633 parser->default_arg_ok_p = true;
2635 /* We are not parsing a constant-expression. */
2636 parser->integral_constant_expression_p = false;
2637 parser->allow_non_integral_constant_expression_p = false;
2638 parser->non_integral_constant_expression_p = false;
2640 /* Local variable names are not forbidden. */
2641 parser->local_variables_forbidden_p = false;
2643 /* We are not processing an `extern "C"' declaration. */
2644 parser->in_unbraced_linkage_specification_p = false;
2646 /* We are not processing a declarator. */
2647 parser->in_declarator_p = false;
2649 /* We are not processing a template-argument-list. */
2650 parser->in_template_argument_list_p = false;
2652 /* We are not in an iteration statement. */
2653 parser->in_statement = 0;
2655 /* We are not in a switch statement. */
2656 parser->in_switch_statement_p = false;
2658 /* We are not parsing a type-id inside an expression. */
2659 parser->in_type_id_in_expr_p = false;
2661 /* Declarations aren't implicitly extern "C". */
2662 parser->implicit_extern_c = false;
2664 /* String literals should be translated to the execution character set. */
2665 parser->translate_strings_p = true;
2667 /* We are not parsing a function body. */
2668 parser->in_function_body = false;
2670 /* The unparsed function queue is empty. */
2671 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2673 /* There are no classes being defined. */
2674 parser->num_classes_being_defined = 0;
2676 /* No template parameters apply. */
2677 parser->num_template_parameter_lists = 0;
2679 return parser;
2682 /* Create a cp_lexer structure which will emit the tokens in CACHE
2683 and push it onto the parser's lexer stack. This is used for delayed
2684 parsing of in-class method bodies and default arguments, and should
2685 not be confused with tentative parsing. */
2686 static void
2687 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2689 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2690 lexer->next = parser->lexer;
2691 parser->lexer = lexer;
2693 /* Move the current source position to that of the first token in the
2694 new lexer. */
2695 cp_lexer_set_source_position_from_token (lexer->next_token);
2698 /* Pop the top lexer off the parser stack. This is never used for the
2699 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2700 static void
2701 cp_parser_pop_lexer (cp_parser *parser)
2703 cp_lexer *lexer = parser->lexer;
2704 parser->lexer = lexer->next;
2705 cp_lexer_destroy (lexer);
2707 /* Put the current source position back where it was before this
2708 lexer was pushed. */
2709 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2712 /* Lexical conventions [gram.lex] */
2714 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2715 identifier. */
2717 static tree
2718 cp_parser_identifier (cp_parser* parser)
2720 cp_token *token;
2722 /* Look for the identifier. */
2723 token = cp_parser_require (parser, CPP_NAME, "identifier");
2724 /* Return the value. */
2725 return token ? token->u.value : error_mark_node;
2728 /* Parse a sequence of adjacent string constants. Returns a
2729 TREE_STRING representing the combined, nul-terminated string
2730 constant. If TRANSLATE is true, translate the string to the
2731 execution character set. If WIDE_OK is true, a wide string is
2732 invalid here.
2734 C++98 [lex.string] says that if a narrow string literal token is
2735 adjacent to a wide string literal token, the behavior is undefined.
2736 However, C99 6.4.5p4 says that this results in a wide string literal.
2737 We follow C99 here, for consistency with the C front end.
2739 This code is largely lifted from lex_string() in c-lex.c.
2741 FUTURE: ObjC++ will need to handle @-strings here. */
2742 static tree
2743 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2745 tree value;
2746 bool wide = false;
2747 size_t count;
2748 struct obstack str_ob;
2749 cpp_string str, istr, *strs;
2750 cp_token *tok;
2752 tok = cp_lexer_peek_token (parser->lexer);
2753 if (!cp_parser_is_string_literal (tok))
2755 cp_parser_error (parser, "expected string-literal");
2756 return error_mark_node;
2759 /* Try to avoid the overhead of creating and destroying an obstack
2760 for the common case of just one string. */
2761 if (!cp_parser_is_string_literal
2762 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2764 cp_lexer_consume_token (parser->lexer);
2766 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2767 str.len = TREE_STRING_LENGTH (tok->u.value);
2768 count = 1;
2769 if (tok->type == CPP_WSTRING)
2770 wide = true;
2772 strs = &str;
2774 else
2776 gcc_obstack_init (&str_ob);
2777 count = 0;
2781 cp_lexer_consume_token (parser->lexer);
2782 count++;
2783 str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2784 str.len = TREE_STRING_LENGTH (tok->u.value);
2785 if (tok->type == CPP_WSTRING)
2786 wide = true;
2788 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2790 tok = cp_lexer_peek_token (parser->lexer);
2792 while (cp_parser_is_string_literal (tok));
2794 strs = (cpp_string *) obstack_finish (&str_ob);
2797 if (wide && !wide_ok)
2799 cp_parser_error (parser, "a wide string is invalid in this context");
2800 wide = false;
2803 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2804 (parse_in, strs, count, &istr, wide))
2806 value = build_string (istr.len, (char *)istr.text);
2807 free ((void *)istr.text);
2809 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2810 value = fix_string_type (value);
2812 else
2813 /* cpp_interpret_string has issued an error. */
2814 value = error_mark_node;
2816 if (count > 1)
2817 obstack_free (&str_ob, 0);
2819 return value;
2823 /* Basic concepts [gram.basic] */
2825 /* Parse a translation-unit.
2827 translation-unit:
2828 declaration-seq [opt]
2830 Returns TRUE if all went well. */
2832 static bool
2833 cp_parser_translation_unit (cp_parser* parser)
2835 /* The address of the first non-permanent object on the declarator
2836 obstack. */
2837 static void *declarator_obstack_base;
2839 bool success;
2841 /* Create the declarator obstack, if necessary. */
2842 if (!cp_error_declarator)
2844 gcc_obstack_init (&declarator_obstack);
2845 /* Create the error declarator. */
2846 cp_error_declarator = make_declarator (cdk_error);
2847 /* Create the empty parameter list. */
2848 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2849 /* Remember where the base of the declarator obstack lies. */
2850 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2853 cp_parser_declaration_seq_opt (parser);
2855 /* If there are no tokens left then all went well. */
2856 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2858 /* Get rid of the token array; we don't need it any more. */
2859 cp_lexer_destroy (parser->lexer);
2860 parser->lexer = NULL;
2862 /* This file might have been a context that's implicitly extern
2863 "C". If so, pop the lang context. (Only relevant for PCH.) */
2864 if (parser->implicit_extern_c)
2866 pop_lang_context ();
2867 parser->implicit_extern_c = false;
2870 /* Finish up. */
2871 finish_translation_unit ();
2873 success = true;
2875 else
2877 cp_parser_error (parser, "expected declaration");
2878 success = false;
2881 /* Make sure the declarator obstack was fully cleaned up. */
2882 gcc_assert (obstack_next_free (&declarator_obstack)
2883 == declarator_obstack_base);
2885 /* All went well. */
2886 return success;
2889 /* Expressions [gram.expr] */
2891 /* Parse a primary-expression.
2893 primary-expression:
2894 literal
2895 this
2896 ( expression )
2897 id-expression
2899 GNU Extensions:
2901 primary-expression:
2902 ( compound-statement )
2903 __builtin_va_arg ( assignment-expression , type-id )
2904 __builtin_offsetof ( type-id , offsetof-expression )
2906 Objective-C++ Extension:
2908 primary-expression:
2909 objc-expression
2911 literal:
2912 __null
2914 ADDRESS_P is true iff this expression was immediately preceded by
2915 "&" and therefore might denote a pointer-to-member. CAST_P is true
2916 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2917 true iff this expression is a template argument.
2919 Returns a representation of the expression. Upon return, *IDK
2920 indicates what kind of id-expression (if any) was present. */
2922 static tree
2923 cp_parser_primary_expression (cp_parser *parser,
2924 bool address_p,
2925 bool cast_p,
2926 bool template_arg_p,
2927 cp_id_kind *idk)
2929 cp_token *token;
2931 /* Assume the primary expression is not an id-expression. */
2932 *idk = CP_ID_KIND_NONE;
2934 /* Peek at the next token. */
2935 token = cp_lexer_peek_token (parser->lexer);
2936 switch (token->type)
2938 /* literal:
2939 integer-literal
2940 character-literal
2941 floating-literal
2942 string-literal
2943 boolean-literal */
2944 case CPP_CHAR:
2945 case CPP_WCHAR:
2946 case CPP_NUMBER:
2947 token = cp_lexer_consume_token (parser->lexer);
2948 /* Floating-point literals are only allowed in an integral
2949 constant expression if they are cast to an integral or
2950 enumeration type. */
2951 if (TREE_CODE (token->u.value) == REAL_CST
2952 && parser->integral_constant_expression_p
2953 && pedantic)
2955 /* CAST_P will be set even in invalid code like "int(2.7 +
2956 ...)". Therefore, we have to check that the next token
2957 is sure to end the cast. */
2958 if (cast_p)
2960 cp_token *next_token;
2962 next_token = cp_lexer_peek_token (parser->lexer);
2963 if (/* The comma at the end of an
2964 enumerator-definition. */
2965 next_token->type != CPP_COMMA
2966 /* The curly brace at the end of an enum-specifier. */
2967 && next_token->type != CPP_CLOSE_BRACE
2968 /* The end of a statement. */
2969 && next_token->type != CPP_SEMICOLON
2970 /* The end of the cast-expression. */
2971 && next_token->type != CPP_CLOSE_PAREN
2972 /* The end of an array bound. */
2973 && next_token->type != CPP_CLOSE_SQUARE
2974 /* The closing ">" in a template-argument-list. */
2975 && (next_token->type != CPP_GREATER
2976 || parser->greater_than_is_operator_p))
2977 cast_p = false;
2980 /* If we are within a cast, then the constraint that the
2981 cast is to an integral or enumeration type will be
2982 checked at that point. If we are not within a cast, then
2983 this code is invalid. */
2984 if (!cast_p)
2985 cp_parser_non_integral_constant_expression
2986 (parser, "floating-point literal");
2988 return token->u.value;
2990 case CPP_STRING:
2991 case CPP_WSTRING:
2992 /* ??? Should wide strings be allowed when parser->translate_strings_p
2993 is false (i.e. in attributes)? If not, we can kill the third
2994 argument to cp_parser_string_literal. */
2995 return cp_parser_string_literal (parser,
2996 parser->translate_strings_p,
2997 true);
2999 case CPP_OPEN_PAREN:
3001 tree expr;
3002 bool saved_greater_than_is_operator_p;
3004 /* Consume the `('. */
3005 cp_lexer_consume_token (parser->lexer);
3006 /* Within a parenthesized expression, a `>' token is always
3007 the greater-than operator. */
3008 saved_greater_than_is_operator_p
3009 = parser->greater_than_is_operator_p;
3010 parser->greater_than_is_operator_p = true;
3011 /* If we see `( { ' then we are looking at the beginning of
3012 a GNU statement-expression. */
3013 if (cp_parser_allow_gnu_extensions_p (parser)
3014 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3016 /* Statement-expressions are not allowed by the standard. */
3017 if (pedantic)
3018 pedwarn ("ISO C++ forbids braced-groups within expressions");
3020 /* And they're not allowed outside of a function-body; you
3021 cannot, for example, write:
3023 int i = ({ int j = 3; j + 1; });
3025 at class or namespace scope. */
3026 if (!parser->in_function_body)
3028 error ("statement-expressions are allowed only inside functions");
3029 cp_parser_skip_to_end_of_block_or_statement (parser);
3030 expr = error_mark_node;
3032 else
3034 /* Start the statement-expression. */
3035 expr = begin_stmt_expr ();
3036 /* Parse the compound-statement. */
3037 cp_parser_compound_statement (parser, expr, false);
3038 /* Finish up. */
3039 expr = finish_stmt_expr (expr, false);
3042 else
3044 /* Parse the parenthesized expression. */
3045 expr = cp_parser_expression (parser, cast_p);
3046 /* Let the front end know that this expression was
3047 enclosed in parentheses. This matters in case, for
3048 example, the expression is of the form `A::B', since
3049 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3050 not. */
3051 finish_parenthesized_expr (expr);
3053 /* The `>' token might be the end of a template-id or
3054 template-parameter-list now. */
3055 parser->greater_than_is_operator_p
3056 = saved_greater_than_is_operator_p;
3057 /* Consume the `)'. */
3058 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3059 cp_parser_skip_to_end_of_statement (parser);
3061 return expr;
3064 case CPP_KEYWORD:
3065 switch (token->keyword)
3067 /* These two are the boolean literals. */
3068 case RID_TRUE:
3069 cp_lexer_consume_token (parser->lexer);
3070 return boolean_true_node;
3071 case RID_FALSE:
3072 cp_lexer_consume_token (parser->lexer);
3073 return boolean_false_node;
3075 /* The `__null' literal. */
3076 case RID_NULL:
3077 cp_lexer_consume_token (parser->lexer);
3078 return null_node;
3080 /* Recognize the `this' keyword. */
3081 case RID_THIS:
3082 cp_lexer_consume_token (parser->lexer);
3083 if (parser->local_variables_forbidden_p)
3085 error ("%<this%> may not be used in this context");
3086 return error_mark_node;
3088 /* Pointers cannot appear in constant-expressions. */
3089 if (cp_parser_non_integral_constant_expression (parser,
3090 "`this'"))
3091 return error_mark_node;
3092 return finish_this_expr ();
3094 /* The `operator' keyword can be the beginning of an
3095 id-expression. */
3096 case RID_OPERATOR:
3097 goto id_expression;
3099 case RID_FUNCTION_NAME:
3100 case RID_PRETTY_FUNCTION_NAME:
3101 case RID_C99_FUNCTION_NAME:
3102 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3103 __func__ are the names of variables -- but they are
3104 treated specially. Therefore, they are handled here,
3105 rather than relying on the generic id-expression logic
3106 below. Grammatically, these names are id-expressions.
3108 Consume the token. */
3109 token = cp_lexer_consume_token (parser->lexer);
3110 /* Look up the name. */
3111 return finish_fname (token->u.value);
3113 case RID_VA_ARG:
3115 tree expression;
3116 tree type;
3118 /* The `__builtin_va_arg' construct is used to handle
3119 `va_arg'. Consume the `__builtin_va_arg' token. */
3120 cp_lexer_consume_token (parser->lexer);
3121 /* Look for the opening `('. */
3122 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3123 /* Now, parse the assignment-expression. */
3124 expression = cp_parser_assignment_expression (parser,
3125 /*cast_p=*/false);
3126 /* Look for the `,'. */
3127 cp_parser_require (parser, CPP_COMMA, "`,'");
3128 /* Parse the type-id. */
3129 type = cp_parser_type_id (parser);
3130 /* Look for the closing `)'. */
3131 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3132 /* Using `va_arg' in a constant-expression is not
3133 allowed. */
3134 if (cp_parser_non_integral_constant_expression (parser,
3135 "`va_arg'"))
3136 return error_mark_node;
3137 return build_x_va_arg (expression, type);
3140 case RID_OFFSETOF:
3141 return cp_parser_builtin_offsetof (parser);
3143 /* Objective-C++ expressions. */
3144 case RID_AT_ENCODE:
3145 case RID_AT_PROTOCOL:
3146 case RID_AT_SELECTOR:
3147 return cp_parser_objc_expression (parser);
3149 default:
3150 cp_parser_error (parser, "expected primary-expression");
3151 return error_mark_node;
3154 /* An id-expression can start with either an identifier, a
3155 `::' as the beginning of a qualified-id, or the "operator"
3156 keyword. */
3157 case CPP_NAME:
3158 case CPP_SCOPE:
3159 case CPP_TEMPLATE_ID:
3160 case CPP_NESTED_NAME_SPECIFIER:
3162 tree id_expression;
3163 tree decl;
3164 const char *error_msg;
3165 bool template_p;
3166 bool done;
3168 id_expression:
3169 /* Parse the id-expression. */
3170 id_expression
3171 = cp_parser_id_expression (parser,
3172 /*template_keyword_p=*/false,
3173 /*check_dependency_p=*/true,
3174 &template_p,
3175 /*declarator_p=*/false,
3176 /*optional_p=*/false);
3177 if (id_expression == error_mark_node)
3178 return error_mark_node;
3179 token = cp_lexer_peek_token (parser->lexer);
3180 done = (token->type != CPP_OPEN_SQUARE
3181 && token->type != CPP_OPEN_PAREN
3182 && token->type != CPP_DOT
3183 && token->type != CPP_DEREF
3184 && token->type != CPP_PLUS_PLUS
3185 && token->type != CPP_MINUS_MINUS);
3186 /* If we have a template-id, then no further lookup is
3187 required. If the template-id was for a template-class, we
3188 will sometimes have a TYPE_DECL at this point. */
3189 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3190 || TREE_CODE (id_expression) == TYPE_DECL)
3191 decl = id_expression;
3192 /* Look up the name. */
3193 else
3195 tree ambiguous_decls;
3197 decl = cp_parser_lookup_name (parser, id_expression,
3198 none_type,
3199 template_p,
3200 /*is_namespace=*/false,
3201 /*check_dependency=*/true,
3202 &ambiguous_decls);
3203 /* If the lookup was ambiguous, an error will already have
3204 been issued. */
3205 if (ambiguous_decls)
3206 return error_mark_node;
3208 /* In Objective-C++, an instance variable (ivar) may be preferred
3209 to whatever cp_parser_lookup_name() found. */
3210 decl = objc_lookup_ivar (decl, id_expression);
3212 /* If name lookup gives us a SCOPE_REF, then the
3213 qualifying scope was dependent. */
3214 if (TREE_CODE (decl) == SCOPE_REF)
3215 return decl;
3216 /* Check to see if DECL is a local variable in a context
3217 where that is forbidden. */
3218 if (parser->local_variables_forbidden_p
3219 && local_variable_p (decl))
3221 /* It might be that we only found DECL because we are
3222 trying to be generous with pre-ISO scoping rules.
3223 For example, consider:
3225 int i;
3226 void g() {
3227 for (int i = 0; i < 10; ++i) {}
3228 extern void f(int j = i);
3231 Here, name look up will originally find the out
3232 of scope `i'. We need to issue a warning message,
3233 but then use the global `i'. */
3234 decl = check_for_out_of_scope_variable (decl);
3235 if (local_variable_p (decl))
3237 error ("local variable %qD may not appear in this context",
3238 decl);
3239 return error_mark_node;
3244 decl = (finish_id_expression
3245 (id_expression, decl, parser->scope,
3246 idk,
3247 parser->integral_constant_expression_p,
3248 parser->allow_non_integral_constant_expression_p,
3249 &parser->non_integral_constant_expression_p,
3250 template_p, done, address_p,
3251 template_arg_p,
3252 &error_msg));
3253 if (error_msg)
3254 cp_parser_error (parser, error_msg);
3255 return decl;
3258 /* Anything else is an error. */
3259 default:
3260 /* ...unless we have an Objective-C++ message or string literal, that is. */
3261 if (c_dialect_objc ()
3262 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3263 return cp_parser_objc_expression (parser);
3265 cp_parser_error (parser, "expected primary-expression");
3266 return error_mark_node;
3270 /* Parse an id-expression.
3272 id-expression:
3273 unqualified-id
3274 qualified-id
3276 qualified-id:
3277 :: [opt] nested-name-specifier template [opt] unqualified-id
3278 :: identifier
3279 :: operator-function-id
3280 :: template-id
3282 Return a representation of the unqualified portion of the
3283 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3284 a `::' or nested-name-specifier.
3286 Often, if the id-expression was a qualified-id, the caller will
3287 want to make a SCOPE_REF to represent the qualified-id. This
3288 function does not do this in order to avoid wastefully creating
3289 SCOPE_REFs when they are not required.
3291 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3292 `template' keyword.
3294 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3295 uninstantiated templates.
3297 If *TEMPLATE_P is non-NULL, it is set to true iff the
3298 `template' keyword is used to explicitly indicate that the entity
3299 named is a template.
3301 If DECLARATOR_P is true, the id-expression is appearing as part of
3302 a declarator, rather than as part of an expression. */
3304 static tree
3305 cp_parser_id_expression (cp_parser *parser,
3306 bool template_keyword_p,
3307 bool check_dependency_p,
3308 bool *template_p,
3309 bool declarator_p,
3310 bool optional_p)
3312 bool global_scope_p;
3313 bool nested_name_specifier_p;
3315 /* Assume the `template' keyword was not used. */
3316 if (template_p)
3317 *template_p = template_keyword_p;
3319 /* Look for the optional `::' operator. */
3320 global_scope_p
3321 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3322 != NULL_TREE);
3323 /* Look for the optional nested-name-specifier. */
3324 nested_name_specifier_p
3325 = (cp_parser_nested_name_specifier_opt (parser,
3326 /*typename_keyword_p=*/false,
3327 check_dependency_p,
3328 /*type_p=*/false,
3329 declarator_p)
3330 != NULL_TREE);
3331 /* If there is a nested-name-specifier, then we are looking at
3332 the first qualified-id production. */
3333 if (nested_name_specifier_p)
3335 tree saved_scope;
3336 tree saved_object_scope;
3337 tree saved_qualifying_scope;
3338 tree unqualified_id;
3339 bool is_template;
3341 /* See if the next token is the `template' keyword. */
3342 if (!template_p)
3343 template_p = &is_template;
3344 *template_p = cp_parser_optional_template_keyword (parser);
3345 /* Name lookup we do during the processing of the
3346 unqualified-id might obliterate SCOPE. */
3347 saved_scope = parser->scope;
3348 saved_object_scope = parser->object_scope;
3349 saved_qualifying_scope = parser->qualifying_scope;
3350 /* Process the final unqualified-id. */
3351 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3352 check_dependency_p,
3353 declarator_p,
3354 /*optional_p=*/false);
3355 /* Restore the SAVED_SCOPE for our caller. */
3356 parser->scope = saved_scope;
3357 parser->object_scope = saved_object_scope;
3358 parser->qualifying_scope = saved_qualifying_scope;
3360 return unqualified_id;
3362 /* Otherwise, if we are in global scope, then we are looking at one
3363 of the other qualified-id productions. */
3364 else if (global_scope_p)
3366 cp_token *token;
3367 tree id;
3369 /* Peek at the next token. */
3370 token = cp_lexer_peek_token (parser->lexer);
3372 /* If it's an identifier, and the next token is not a "<", then
3373 we can avoid the template-id case. This is an optimization
3374 for this common case. */
3375 if (token->type == CPP_NAME
3376 && !cp_parser_nth_token_starts_template_argument_list_p
3377 (parser, 2))
3378 return cp_parser_identifier (parser);
3380 cp_parser_parse_tentatively (parser);
3381 /* Try a template-id. */
3382 id = cp_parser_template_id (parser,
3383 /*template_keyword_p=*/false,
3384 /*check_dependency_p=*/true,
3385 declarator_p);
3386 /* If that worked, we're done. */
3387 if (cp_parser_parse_definitely (parser))
3388 return id;
3390 /* Peek at the next token. (Changes in the token buffer may
3391 have invalidated the pointer obtained above.) */
3392 token = cp_lexer_peek_token (parser->lexer);
3394 switch (token->type)
3396 case CPP_NAME:
3397 return cp_parser_identifier (parser);
3399 case CPP_KEYWORD:
3400 if (token->keyword == RID_OPERATOR)
3401 return cp_parser_operator_function_id (parser);
3402 /* Fall through. */
3404 default:
3405 cp_parser_error (parser, "expected id-expression");
3406 return error_mark_node;
3409 else
3410 return cp_parser_unqualified_id (parser, template_keyword_p,
3411 /*check_dependency_p=*/true,
3412 declarator_p,
3413 optional_p);
3416 /* Parse an unqualified-id.
3418 unqualified-id:
3419 identifier
3420 operator-function-id
3421 conversion-function-id
3422 ~ class-name
3423 template-id
3425 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3426 keyword, in a construct like `A::template ...'.
3428 Returns a representation of unqualified-id. For the `identifier'
3429 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3430 production a BIT_NOT_EXPR is returned; the operand of the
3431 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3432 other productions, see the documentation accompanying the
3433 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3434 names are looked up in uninstantiated templates. If DECLARATOR_P
3435 is true, the unqualified-id is appearing as part of a declarator,
3436 rather than as part of an expression. */
3438 static tree
3439 cp_parser_unqualified_id (cp_parser* parser,
3440 bool template_keyword_p,
3441 bool check_dependency_p,
3442 bool declarator_p,
3443 bool optional_p)
3445 cp_token *token;
3447 /* Peek at the next token. */
3448 token = cp_lexer_peek_token (parser->lexer);
3450 switch (token->type)
3452 case CPP_NAME:
3454 tree id;
3456 /* We don't know yet whether or not this will be a
3457 template-id. */
3458 cp_parser_parse_tentatively (parser);
3459 /* Try a template-id. */
3460 id = cp_parser_template_id (parser, template_keyword_p,
3461 check_dependency_p,
3462 declarator_p);
3463 /* If it worked, we're done. */
3464 if (cp_parser_parse_definitely (parser))
3465 return id;
3466 /* Otherwise, it's an ordinary identifier. */
3467 return cp_parser_identifier (parser);
3470 case CPP_TEMPLATE_ID:
3471 return cp_parser_template_id (parser, template_keyword_p,
3472 check_dependency_p,
3473 declarator_p);
3475 case CPP_COMPL:
3477 tree type_decl;
3478 tree qualifying_scope;
3479 tree object_scope;
3480 tree scope;
3481 bool done;
3483 /* Consume the `~' token. */
3484 cp_lexer_consume_token (parser->lexer);
3485 /* Parse the class-name. The standard, as written, seems to
3486 say that:
3488 template <typename T> struct S { ~S (); };
3489 template <typename T> S<T>::~S() {}
3491 is invalid, since `~' must be followed by a class-name, but
3492 `S<T>' is dependent, and so not known to be a class.
3493 That's not right; we need to look in uninstantiated
3494 templates. A further complication arises from:
3496 template <typename T> void f(T t) {
3497 t.T::~T();
3500 Here, it is not possible to look up `T' in the scope of `T'
3501 itself. We must look in both the current scope, and the
3502 scope of the containing complete expression.
3504 Yet another issue is:
3506 struct S {
3507 int S;
3508 ~S();
3511 S::~S() {}
3513 The standard does not seem to say that the `S' in `~S'
3514 should refer to the type `S' and not the data member
3515 `S::S'. */
3517 /* DR 244 says that we look up the name after the "~" in the
3518 same scope as we looked up the qualifying name. That idea
3519 isn't fully worked out; it's more complicated than that. */
3520 scope = parser->scope;
3521 object_scope = parser->object_scope;
3522 qualifying_scope = parser->qualifying_scope;
3524 /* Check for invalid scopes. */
3525 if (scope == error_mark_node)
3527 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3528 cp_lexer_consume_token (parser->lexer);
3529 return error_mark_node;
3531 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3533 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3534 error ("scope %qT before %<~%> is not a class-name", scope);
3535 cp_parser_simulate_error (parser);
3536 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3537 cp_lexer_consume_token (parser->lexer);
3538 return error_mark_node;
3540 gcc_assert (!scope || TYPE_P (scope));
3542 /* If the name is of the form "X::~X" it's OK. */
3543 token = cp_lexer_peek_token (parser->lexer);
3544 if (scope
3545 && token->type == CPP_NAME
3546 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3547 == CPP_OPEN_PAREN)
3548 && constructor_name_p (token->u.value, scope))
3550 cp_lexer_consume_token (parser->lexer);
3551 return build_nt (BIT_NOT_EXPR, scope);
3554 /* If there was an explicit qualification (S::~T), first look
3555 in the scope given by the qualification (i.e., S). */
3556 done = false;
3557 type_decl = NULL_TREE;
3558 if (scope)
3560 cp_parser_parse_tentatively (parser);
3561 type_decl = cp_parser_class_name (parser,
3562 /*typename_keyword_p=*/false,
3563 /*template_keyword_p=*/false,
3564 none_type,
3565 /*check_dependency=*/false,
3566 /*class_head_p=*/false,
3567 declarator_p);
3568 if (cp_parser_parse_definitely (parser))
3569 done = true;
3571 /* In "N::S::~S", look in "N" as well. */
3572 if (!done && scope && qualifying_scope)
3574 cp_parser_parse_tentatively (parser);
3575 parser->scope = qualifying_scope;
3576 parser->object_scope = NULL_TREE;
3577 parser->qualifying_scope = NULL_TREE;
3578 type_decl
3579 = cp_parser_class_name (parser,
3580 /*typename_keyword_p=*/false,
3581 /*template_keyword_p=*/false,
3582 none_type,
3583 /*check_dependency=*/false,
3584 /*class_head_p=*/false,
3585 declarator_p);
3586 if (cp_parser_parse_definitely (parser))
3587 done = true;
3589 /* In "p->S::~T", look in the scope given by "*p" as well. */
3590 else if (!done && object_scope)
3592 cp_parser_parse_tentatively (parser);
3593 parser->scope = object_scope;
3594 parser->object_scope = NULL_TREE;
3595 parser->qualifying_scope = NULL_TREE;
3596 type_decl
3597 = cp_parser_class_name (parser,
3598 /*typename_keyword_p=*/false,
3599 /*template_keyword_p=*/false,
3600 none_type,
3601 /*check_dependency=*/false,
3602 /*class_head_p=*/false,
3603 declarator_p);
3604 if (cp_parser_parse_definitely (parser))
3605 done = true;
3607 /* Look in the surrounding context. */
3608 if (!done)
3610 parser->scope = NULL_TREE;
3611 parser->object_scope = NULL_TREE;
3612 parser->qualifying_scope = NULL_TREE;
3613 type_decl
3614 = cp_parser_class_name (parser,
3615 /*typename_keyword_p=*/false,
3616 /*template_keyword_p=*/false,
3617 none_type,
3618 /*check_dependency=*/false,
3619 /*class_head_p=*/false,
3620 declarator_p);
3622 /* If an error occurred, assume that the name of the
3623 destructor is the same as the name of the qualifying
3624 class. That allows us to keep parsing after running
3625 into ill-formed destructor names. */
3626 if (type_decl == error_mark_node && scope)
3627 return build_nt (BIT_NOT_EXPR, scope);
3628 else if (type_decl == error_mark_node)
3629 return error_mark_node;
3631 /* Check that destructor name and scope match. */
3632 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3634 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3635 error ("declaration of %<~%T%> as member of %qT",
3636 type_decl, scope);
3637 cp_parser_simulate_error (parser);
3638 return error_mark_node;
3641 /* [class.dtor]
3643 A typedef-name that names a class shall not be used as the
3644 identifier in the declarator for a destructor declaration. */
3645 if (declarator_p
3646 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3647 && !DECL_SELF_REFERENCE_P (type_decl)
3648 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3649 error ("typedef-name %qD used as destructor declarator",
3650 type_decl);
3652 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3655 case CPP_KEYWORD:
3656 if (token->keyword == RID_OPERATOR)
3658 tree id;
3660 /* This could be a template-id, so we try that first. */
3661 cp_parser_parse_tentatively (parser);
3662 /* Try a template-id. */
3663 id = cp_parser_template_id (parser, template_keyword_p,
3664 /*check_dependency_p=*/true,
3665 declarator_p);
3666 /* If that worked, we're done. */
3667 if (cp_parser_parse_definitely (parser))
3668 return id;
3669 /* We still don't know whether we're looking at an
3670 operator-function-id or a conversion-function-id. */
3671 cp_parser_parse_tentatively (parser);
3672 /* Try an operator-function-id. */
3673 id = cp_parser_operator_function_id (parser);
3674 /* If that didn't work, try a conversion-function-id. */
3675 if (!cp_parser_parse_definitely (parser))
3676 id = cp_parser_conversion_function_id (parser);
3678 return id;
3680 /* Fall through. */
3682 default:
3683 if (optional_p)
3684 return NULL_TREE;
3685 cp_parser_error (parser, "expected unqualified-id");
3686 return error_mark_node;
3690 /* Parse an (optional) nested-name-specifier.
3692 nested-name-specifier:
3693 class-or-namespace-name :: nested-name-specifier [opt]
3694 class-or-namespace-name :: template nested-name-specifier [opt]
3696 PARSER->SCOPE should be set appropriately before this function is
3697 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3698 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3699 in name lookups.
3701 Sets PARSER->SCOPE to the class (TYPE) or namespace
3702 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3703 it unchanged if there is no nested-name-specifier. Returns the new
3704 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3706 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3707 part of a declaration and/or decl-specifier. */
3709 static tree
3710 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3711 bool typename_keyword_p,
3712 bool check_dependency_p,
3713 bool type_p,
3714 bool is_declaration)
3716 bool success = false;
3717 cp_token_position start = 0;
3718 cp_token *token;
3720 /* Remember where the nested-name-specifier starts. */
3721 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3723 start = cp_lexer_token_position (parser->lexer, false);
3724 push_deferring_access_checks (dk_deferred);
3727 while (true)
3729 tree new_scope;
3730 tree old_scope;
3731 tree saved_qualifying_scope;
3732 bool template_keyword_p;
3734 /* Spot cases that cannot be the beginning of a
3735 nested-name-specifier. */
3736 token = cp_lexer_peek_token (parser->lexer);
3738 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3739 the already parsed nested-name-specifier. */
3740 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3742 /* Grab the nested-name-specifier and continue the loop. */
3743 cp_parser_pre_parsed_nested_name_specifier (parser);
3744 /* If we originally encountered this nested-name-specifier
3745 with IS_DECLARATION set to false, we will not have
3746 resolved TYPENAME_TYPEs, so we must do so here. */
3747 if (is_declaration
3748 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3750 new_scope = resolve_typename_type (parser->scope,
3751 /*only_current_p=*/false);
3752 if (new_scope != error_mark_node)
3753 parser->scope = new_scope;
3755 success = true;
3756 continue;
3759 /* Spot cases that cannot be the beginning of a
3760 nested-name-specifier. On the second and subsequent times
3761 through the loop, we look for the `template' keyword. */
3762 if (success && token->keyword == RID_TEMPLATE)
3764 /* A template-id can start a nested-name-specifier. */
3765 else if (token->type == CPP_TEMPLATE_ID)
3767 else
3769 /* If the next token is not an identifier, then it is
3770 definitely not a class-or-namespace-name. */
3771 if (token->type != CPP_NAME)
3772 break;
3773 /* If the following token is neither a `<' (to begin a
3774 template-id), nor a `::', then we are not looking at a
3775 nested-name-specifier. */
3776 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3777 if (token->type != CPP_SCOPE
3778 && !cp_parser_nth_token_starts_template_argument_list_p
3779 (parser, 2))
3780 break;
3783 /* The nested-name-specifier is optional, so we parse
3784 tentatively. */
3785 cp_parser_parse_tentatively (parser);
3787 /* Look for the optional `template' keyword, if this isn't the
3788 first time through the loop. */
3789 if (success)
3790 template_keyword_p = cp_parser_optional_template_keyword (parser);
3791 else
3792 template_keyword_p = false;
3794 /* Save the old scope since the name lookup we are about to do
3795 might destroy it. */
3796 old_scope = parser->scope;
3797 saved_qualifying_scope = parser->qualifying_scope;
3798 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3799 look up names in "X<T>::I" in order to determine that "Y" is
3800 a template. So, if we have a typename at this point, we make
3801 an effort to look through it. */
3802 if (is_declaration
3803 && !typename_keyword_p
3804 && parser->scope
3805 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3806 parser->scope = resolve_typename_type (parser->scope,
3807 /*only_current_p=*/false);
3808 /* Parse the qualifying entity. */
3809 new_scope
3810 = cp_parser_class_or_namespace_name (parser,
3811 typename_keyword_p,
3812 template_keyword_p,
3813 check_dependency_p,
3814 type_p,
3815 is_declaration);
3816 /* Look for the `::' token. */
3817 cp_parser_require (parser, CPP_SCOPE, "`::'");
3819 /* If we found what we wanted, we keep going; otherwise, we're
3820 done. */
3821 if (!cp_parser_parse_definitely (parser))
3823 bool error_p = false;
3825 /* Restore the OLD_SCOPE since it was valid before the
3826 failed attempt at finding the last
3827 class-or-namespace-name. */
3828 parser->scope = old_scope;
3829 parser->qualifying_scope = saved_qualifying_scope;
3830 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3831 break;
3832 /* If the next token is an identifier, and the one after
3833 that is a `::', then any valid interpretation would have
3834 found a class-or-namespace-name. */
3835 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3836 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3837 == CPP_SCOPE)
3838 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3839 != CPP_COMPL))
3841 token = cp_lexer_consume_token (parser->lexer);
3842 if (!error_p)
3844 if (!token->ambiguous_p)
3846 tree decl;
3847 tree ambiguous_decls;
3849 decl = cp_parser_lookup_name (parser, token->u.value,
3850 none_type,
3851 /*is_template=*/false,
3852 /*is_namespace=*/false,
3853 /*check_dependency=*/true,
3854 &ambiguous_decls);
3855 if (TREE_CODE (decl) == TEMPLATE_DECL)
3856 error ("%qD used without template parameters", decl);
3857 else if (ambiguous_decls)
3859 error ("reference to %qD is ambiguous",
3860 token->u.value);
3861 print_candidates (ambiguous_decls);
3862 decl = error_mark_node;
3864 else
3865 cp_parser_name_lookup_error
3866 (parser, token->u.value, decl,
3867 "is not a class or namespace");
3869 parser->scope = error_mark_node;
3870 error_p = true;
3871 /* Treat this as a successful nested-name-specifier
3872 due to:
3874 [basic.lookup.qual]
3876 If the name found is not a class-name (clause
3877 _class_) or namespace-name (_namespace.def_), the
3878 program is ill-formed. */
3879 success = true;
3881 cp_lexer_consume_token (parser->lexer);
3883 break;
3885 /* We've found one valid nested-name-specifier. */
3886 success = true;
3887 /* Name lookup always gives us a DECL. */
3888 if (TREE_CODE (new_scope) == TYPE_DECL)
3889 new_scope = TREE_TYPE (new_scope);
3890 /* Uses of "template" must be followed by actual templates. */
3891 if (template_keyword_p
3892 && !(CLASS_TYPE_P (new_scope)
3893 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3894 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3895 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3896 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3897 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3898 == TEMPLATE_ID_EXPR)))
3899 pedwarn (TYPE_P (new_scope)
3900 ? "%qT is not a template"
3901 : "%qD is not a template",
3902 new_scope);
3903 /* If it is a class scope, try to complete it; we are about to
3904 be looking up names inside the class. */
3905 if (TYPE_P (new_scope)
3906 /* Since checking types for dependency can be expensive,
3907 avoid doing it if the type is already complete. */
3908 && !COMPLETE_TYPE_P (new_scope)
3909 /* Do not try to complete dependent types. */
3910 && !dependent_type_p (new_scope))
3911 new_scope = complete_type (new_scope);
3912 /* Make sure we look in the right scope the next time through
3913 the loop. */
3914 parser->scope = new_scope;
3917 /* If parsing tentatively, replace the sequence of tokens that makes
3918 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3919 token. That way, should we re-parse the token stream, we will
3920 not have to repeat the effort required to do the parse, nor will
3921 we issue duplicate error messages. */
3922 if (success && start)
3924 cp_token *token;
3926 token = cp_lexer_token_at (parser->lexer, start);
3927 /* Reset the contents of the START token. */
3928 token->type = CPP_NESTED_NAME_SPECIFIER;
3929 /* Retrieve any deferred checks. Do not pop this access checks yet
3930 so the memory will not be reclaimed during token replacing below. */
3931 token->u.tree_check_value = GGC_CNEW (struct tree_check);
3932 token->u.tree_check_value->value = parser->scope;
3933 token->u.tree_check_value->checks = get_deferred_access_checks ();
3934 token->u.tree_check_value->qualifying_scope =
3935 parser->qualifying_scope;
3936 token->keyword = RID_MAX;
3938 /* Purge all subsequent tokens. */
3939 cp_lexer_purge_tokens_after (parser->lexer, start);
3942 if (start)
3943 pop_to_parent_deferring_access_checks ();
3945 return success ? parser->scope : NULL_TREE;
3948 /* Parse a nested-name-specifier. See
3949 cp_parser_nested_name_specifier_opt for details. This function
3950 behaves identically, except that it will an issue an error if no
3951 nested-name-specifier is present. */
3953 static tree
3954 cp_parser_nested_name_specifier (cp_parser *parser,
3955 bool typename_keyword_p,
3956 bool check_dependency_p,
3957 bool type_p,
3958 bool is_declaration)
3960 tree scope;
3962 /* Look for the nested-name-specifier. */
3963 scope = cp_parser_nested_name_specifier_opt (parser,
3964 typename_keyword_p,
3965 check_dependency_p,
3966 type_p,
3967 is_declaration);
3968 /* If it was not present, issue an error message. */
3969 if (!scope)
3971 cp_parser_error (parser, "expected nested-name-specifier");
3972 parser->scope = NULL_TREE;
3975 return scope;
3978 /* Parse a class-or-namespace-name.
3980 class-or-namespace-name:
3981 class-name
3982 namespace-name
3984 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3985 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3986 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3987 TYPE_P is TRUE iff the next name should be taken as a class-name,
3988 even the same name is declared to be another entity in the same
3989 scope.
3991 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3992 specified by the class-or-namespace-name. If neither is found the
3993 ERROR_MARK_NODE is returned. */
3995 static tree
3996 cp_parser_class_or_namespace_name (cp_parser *parser,
3997 bool typename_keyword_p,
3998 bool template_keyword_p,
3999 bool check_dependency_p,
4000 bool type_p,
4001 bool is_declaration)
4003 tree saved_scope;
4004 tree saved_qualifying_scope;
4005 tree saved_object_scope;
4006 tree scope;
4007 bool only_class_p;
4009 /* Before we try to parse the class-name, we must save away the
4010 current PARSER->SCOPE since cp_parser_class_name will destroy
4011 it. */
4012 saved_scope = parser->scope;
4013 saved_qualifying_scope = parser->qualifying_scope;
4014 saved_object_scope = parser->object_scope;
4015 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4016 there is no need to look for a namespace-name. */
4017 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4018 if (!only_class_p)
4019 cp_parser_parse_tentatively (parser);
4020 scope = cp_parser_class_name (parser,
4021 typename_keyword_p,
4022 template_keyword_p,
4023 type_p ? class_type : none_type,
4024 check_dependency_p,
4025 /*class_head_p=*/false,
4026 is_declaration);
4027 /* If that didn't work, try for a namespace-name. */
4028 if (!only_class_p && !cp_parser_parse_definitely (parser))
4030 /* Restore the saved scope. */
4031 parser->scope = saved_scope;
4032 parser->qualifying_scope = saved_qualifying_scope;
4033 parser->object_scope = saved_object_scope;
4034 /* If we are not looking at an identifier followed by the scope
4035 resolution operator, then this is not part of a
4036 nested-name-specifier. (Note that this function is only used
4037 to parse the components of a nested-name-specifier.) */
4038 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4039 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4040 return error_mark_node;
4041 scope = cp_parser_namespace_name (parser);
4044 return scope;
4047 /* Parse a postfix-expression.
4049 postfix-expression:
4050 primary-expression
4051 postfix-expression [ expression ]
4052 postfix-expression ( expression-list [opt] )
4053 simple-type-specifier ( expression-list [opt] )
4054 typename :: [opt] nested-name-specifier identifier
4055 ( expression-list [opt] )
4056 typename :: [opt] nested-name-specifier template [opt] template-id
4057 ( expression-list [opt] )
4058 postfix-expression . template [opt] id-expression
4059 postfix-expression -> template [opt] id-expression
4060 postfix-expression . pseudo-destructor-name
4061 postfix-expression -> pseudo-destructor-name
4062 postfix-expression ++
4063 postfix-expression --
4064 dynamic_cast < type-id > ( expression )
4065 static_cast < type-id > ( expression )
4066 reinterpret_cast < type-id > ( expression )
4067 const_cast < type-id > ( expression )
4068 typeid ( expression )
4069 typeid ( type-id )
4071 GNU Extension:
4073 postfix-expression:
4074 ( type-id ) { initializer-list , [opt] }
4076 This extension is a GNU version of the C99 compound-literal
4077 construct. (The C99 grammar uses `type-name' instead of `type-id',
4078 but they are essentially the same concept.)
4080 If ADDRESS_P is true, the postfix expression is the operand of the
4081 `&' operator. CAST_P is true if this expression is the target of a
4082 cast.
4084 Returns a representation of the expression. */
4086 static tree
4087 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4089 cp_token *token;
4090 enum rid keyword;
4091 cp_id_kind idk = CP_ID_KIND_NONE;
4092 tree postfix_expression = NULL_TREE;
4094 /* Peek at the next token. */
4095 token = cp_lexer_peek_token (parser->lexer);
4096 /* Some of the productions are determined by keywords. */
4097 keyword = token->keyword;
4098 switch (keyword)
4100 case RID_DYNCAST:
4101 case RID_STATCAST:
4102 case RID_REINTCAST:
4103 case RID_CONSTCAST:
4105 tree type;
4106 tree expression;
4107 const char *saved_message;
4109 /* All of these can be handled in the same way from the point
4110 of view of parsing. Begin by consuming the token
4111 identifying the cast. */
4112 cp_lexer_consume_token (parser->lexer);
4114 /* New types cannot be defined in the cast. */
4115 saved_message = parser->type_definition_forbidden_message;
4116 parser->type_definition_forbidden_message
4117 = "types may not be defined in casts";
4119 /* Look for the opening `<'. */
4120 cp_parser_require (parser, CPP_LESS, "`<'");
4121 /* Parse the type to which we are casting. */
4122 type = cp_parser_type_id (parser);
4123 /* Look for the closing `>'. */
4124 cp_parser_require (parser, CPP_GREATER, "`>'");
4125 /* Restore the old message. */
4126 parser->type_definition_forbidden_message = saved_message;
4128 /* And the expression which is being cast. */
4129 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4130 expression = cp_parser_expression (parser, /*cast_p=*/true);
4131 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4133 /* Only type conversions to integral or enumeration types
4134 can be used in constant-expressions. */
4135 if (!cast_valid_in_integral_constant_expression_p (type)
4136 && (cp_parser_non_integral_constant_expression
4137 (parser,
4138 "a cast to a type other than an integral or "
4139 "enumeration type")))
4140 return error_mark_node;
4142 switch (keyword)
4144 case RID_DYNCAST:
4145 postfix_expression
4146 = build_dynamic_cast (type, expression);
4147 break;
4148 case RID_STATCAST:
4149 postfix_expression
4150 = build_static_cast (type, expression);
4151 break;
4152 case RID_REINTCAST:
4153 postfix_expression
4154 = build_reinterpret_cast (type, expression);
4155 break;
4156 case RID_CONSTCAST:
4157 postfix_expression
4158 = build_const_cast (type, expression);
4159 break;
4160 default:
4161 gcc_unreachable ();
4164 break;
4166 case RID_TYPEID:
4168 tree type;
4169 const char *saved_message;
4170 bool saved_in_type_id_in_expr_p;
4172 /* Consume the `typeid' token. */
4173 cp_lexer_consume_token (parser->lexer);
4174 /* Look for the `(' token. */
4175 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4176 /* Types cannot be defined in a `typeid' expression. */
4177 saved_message = parser->type_definition_forbidden_message;
4178 parser->type_definition_forbidden_message
4179 = "types may not be defined in a `typeid\' expression";
4180 /* We can't be sure yet whether we're looking at a type-id or an
4181 expression. */
4182 cp_parser_parse_tentatively (parser);
4183 /* Try a type-id first. */
4184 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4185 parser->in_type_id_in_expr_p = true;
4186 type = cp_parser_type_id (parser);
4187 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4188 /* Look for the `)' token. Otherwise, we can't be sure that
4189 we're not looking at an expression: consider `typeid (int
4190 (3))', for example. */
4191 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4192 /* If all went well, simply lookup the type-id. */
4193 if (cp_parser_parse_definitely (parser))
4194 postfix_expression = get_typeid (type);
4195 /* Otherwise, fall back to the expression variant. */
4196 else
4198 tree expression;
4200 /* Look for an expression. */
4201 expression = cp_parser_expression (parser, /*cast_p=*/false);
4202 /* Compute its typeid. */
4203 postfix_expression = build_typeid (expression);
4204 /* Look for the `)' token. */
4205 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4207 /* Restore the saved message. */
4208 parser->type_definition_forbidden_message = saved_message;
4209 /* `typeid' may not appear in an integral constant expression. */
4210 if (cp_parser_non_integral_constant_expression(parser,
4211 "`typeid' operator"))
4212 return error_mark_node;
4214 break;
4216 case RID_TYPENAME:
4218 tree type;
4219 /* The syntax permitted here is the same permitted for an
4220 elaborated-type-specifier. */
4221 type = cp_parser_elaborated_type_specifier (parser,
4222 /*is_friend=*/false,
4223 /*is_declaration=*/false);
4224 postfix_expression = cp_parser_functional_cast (parser, type);
4226 break;
4228 default:
4230 tree type;
4232 /* If the next thing is a simple-type-specifier, we may be
4233 looking at a functional cast. We could also be looking at
4234 an id-expression. So, we try the functional cast, and if
4235 that doesn't work we fall back to the primary-expression. */
4236 cp_parser_parse_tentatively (parser);
4237 /* Look for the simple-type-specifier. */
4238 type = cp_parser_simple_type_specifier (parser,
4239 /*decl_specs=*/NULL,
4240 CP_PARSER_FLAGS_NONE);
4241 /* Parse the cast itself. */
4242 if (!cp_parser_error_occurred (parser))
4243 postfix_expression
4244 = cp_parser_functional_cast (parser, type);
4245 /* If that worked, we're done. */
4246 if (cp_parser_parse_definitely (parser))
4247 break;
4249 /* If the functional-cast didn't work out, try a
4250 compound-literal. */
4251 if (cp_parser_allow_gnu_extensions_p (parser)
4252 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4254 VEC(constructor_elt,gc) *initializer_list = NULL;
4255 bool saved_in_type_id_in_expr_p;
4257 cp_parser_parse_tentatively (parser);
4258 /* Consume the `('. */
4259 cp_lexer_consume_token (parser->lexer);
4260 /* Parse the type. */
4261 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4262 parser->in_type_id_in_expr_p = true;
4263 type = cp_parser_type_id (parser);
4264 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4265 /* Look for the `)'. */
4266 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4267 /* Look for the `{'. */
4268 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4269 /* If things aren't going well, there's no need to
4270 keep going. */
4271 if (!cp_parser_error_occurred (parser))
4273 bool non_constant_p;
4274 /* Parse the initializer-list. */
4275 initializer_list
4276 = cp_parser_initializer_list (parser, &non_constant_p);
4277 /* Allow a trailing `,'. */
4278 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4279 cp_lexer_consume_token (parser->lexer);
4280 /* Look for the final `}'. */
4281 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4283 /* If that worked, we're definitely looking at a
4284 compound-literal expression. */
4285 if (cp_parser_parse_definitely (parser))
4287 /* Warn the user that a compound literal is not
4288 allowed in standard C++. */
4289 if (pedantic)
4290 pedwarn ("ISO C++ forbids compound-literals");
4291 /* Form the representation of the compound-literal. */
4292 postfix_expression
4293 = finish_compound_literal (type, initializer_list);
4294 break;
4298 /* It must be a primary-expression. */
4299 postfix_expression
4300 = cp_parser_primary_expression (parser, address_p, cast_p,
4301 /*template_arg_p=*/false,
4302 &idk);
4304 break;
4307 /* Keep looping until the postfix-expression is complete. */
4308 while (true)
4310 if (idk == CP_ID_KIND_UNQUALIFIED
4311 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4312 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4313 /* It is not a Koenig lookup function call. */
4314 postfix_expression
4315 = unqualified_name_lookup_error (postfix_expression);
4317 /* Peek at the next token. */
4318 token = cp_lexer_peek_token (parser->lexer);
4320 switch (token->type)
4322 case CPP_OPEN_SQUARE:
4323 postfix_expression
4324 = cp_parser_postfix_open_square_expression (parser,
4325 postfix_expression,
4326 false);
4327 idk = CP_ID_KIND_NONE;
4328 break;
4330 case CPP_OPEN_PAREN:
4331 /* postfix-expression ( expression-list [opt] ) */
4333 bool koenig_p;
4334 bool is_builtin_constant_p;
4335 bool saved_integral_constant_expression_p = false;
4336 bool saved_non_integral_constant_expression_p = false;
4337 tree args;
4339 is_builtin_constant_p
4340 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4341 if (is_builtin_constant_p)
4343 /* The whole point of __builtin_constant_p is to allow
4344 non-constant expressions to appear as arguments. */
4345 saved_integral_constant_expression_p
4346 = parser->integral_constant_expression_p;
4347 saved_non_integral_constant_expression_p
4348 = parser->non_integral_constant_expression_p;
4349 parser->integral_constant_expression_p = false;
4351 args = (cp_parser_parenthesized_expression_list
4352 (parser, /*is_attribute_list=*/false,
4353 /*cast_p=*/false,
4354 /*non_constant_p=*/NULL));
4355 if (is_builtin_constant_p)
4357 parser->integral_constant_expression_p
4358 = saved_integral_constant_expression_p;
4359 parser->non_integral_constant_expression_p
4360 = saved_non_integral_constant_expression_p;
4363 if (args == error_mark_node)
4365 postfix_expression = error_mark_node;
4366 break;
4369 /* Function calls are not permitted in
4370 constant-expressions. */
4371 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4372 && cp_parser_non_integral_constant_expression (parser,
4373 "a function call"))
4375 postfix_expression = error_mark_node;
4376 break;
4379 koenig_p = false;
4380 if (idk == CP_ID_KIND_UNQUALIFIED)
4382 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4384 if (args)
4386 koenig_p = true;
4387 postfix_expression
4388 = perform_koenig_lookup (postfix_expression, args);
4390 else
4391 postfix_expression
4392 = unqualified_fn_lookup_error (postfix_expression);
4394 /* We do not perform argument-dependent lookup if
4395 normal lookup finds a non-function, in accordance
4396 with the expected resolution of DR 218. */
4397 else if (args && is_overloaded_fn (postfix_expression))
4399 tree fn = get_first_fn (postfix_expression);
4401 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4402 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4404 /* Only do argument dependent lookup if regular
4405 lookup does not find a set of member functions.
4406 [basic.lookup.koenig]/2a */
4407 if (!DECL_FUNCTION_MEMBER_P (fn))
4409 koenig_p = true;
4410 postfix_expression
4411 = perform_koenig_lookup (postfix_expression, args);
4416 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4418 tree instance = TREE_OPERAND (postfix_expression, 0);
4419 tree fn = TREE_OPERAND (postfix_expression, 1);
4421 if (processing_template_decl
4422 && (type_dependent_expression_p (instance)
4423 || (!BASELINK_P (fn)
4424 && TREE_CODE (fn) != FIELD_DECL)
4425 || type_dependent_expression_p (fn)
4426 || any_type_dependent_arguments_p (args)))
4428 postfix_expression
4429 = build_min_nt (CALL_EXPR, postfix_expression,
4430 args, NULL_TREE);
4431 break;
4434 if (BASELINK_P (fn))
4435 postfix_expression
4436 = (build_new_method_call
4437 (instance, fn, args, NULL_TREE,
4438 (idk == CP_ID_KIND_QUALIFIED
4439 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4440 /*fn_p=*/NULL));
4441 else
4442 postfix_expression
4443 = finish_call_expr (postfix_expression, args,
4444 /*disallow_virtual=*/false,
4445 /*koenig_p=*/false);
4447 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4448 || TREE_CODE (postfix_expression) == MEMBER_REF
4449 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4450 postfix_expression = (build_offset_ref_call_from_tree
4451 (postfix_expression, args));
4452 else if (idk == CP_ID_KIND_QUALIFIED)
4453 /* A call to a static class member, or a namespace-scope
4454 function. */
4455 postfix_expression
4456 = finish_call_expr (postfix_expression, args,
4457 /*disallow_virtual=*/true,
4458 koenig_p);
4459 else
4460 /* All other function calls. */
4461 postfix_expression
4462 = finish_call_expr (postfix_expression, args,
4463 /*disallow_virtual=*/false,
4464 koenig_p);
4466 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4467 idk = CP_ID_KIND_NONE;
4469 break;
4471 case CPP_DOT:
4472 case CPP_DEREF:
4473 /* postfix-expression . template [opt] id-expression
4474 postfix-expression . pseudo-destructor-name
4475 postfix-expression -> template [opt] id-expression
4476 postfix-expression -> pseudo-destructor-name */
4478 /* Consume the `.' or `->' operator. */
4479 cp_lexer_consume_token (parser->lexer);
4481 postfix_expression
4482 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4483 postfix_expression,
4484 false, &idk);
4485 break;
4487 case CPP_PLUS_PLUS:
4488 /* postfix-expression ++ */
4489 /* Consume the `++' token. */
4490 cp_lexer_consume_token (parser->lexer);
4491 /* Generate a representation for the complete expression. */
4492 postfix_expression
4493 = finish_increment_expr (postfix_expression,
4494 POSTINCREMENT_EXPR);
4495 /* Increments may not appear in constant-expressions. */
4496 if (cp_parser_non_integral_constant_expression (parser,
4497 "an increment"))
4498 postfix_expression = error_mark_node;
4499 idk = CP_ID_KIND_NONE;
4500 break;
4502 case CPP_MINUS_MINUS:
4503 /* postfix-expression -- */
4504 /* Consume the `--' token. */
4505 cp_lexer_consume_token (parser->lexer);
4506 /* Generate a representation for the complete expression. */
4507 postfix_expression
4508 = finish_increment_expr (postfix_expression,
4509 POSTDECREMENT_EXPR);
4510 /* Decrements may not appear in constant-expressions. */
4511 if (cp_parser_non_integral_constant_expression (parser,
4512 "a decrement"))
4513 postfix_expression = error_mark_node;
4514 idk = CP_ID_KIND_NONE;
4515 break;
4517 default:
4518 return postfix_expression;
4522 /* We should never get here. */
4523 gcc_unreachable ();
4524 return error_mark_node;
4527 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4528 by cp_parser_builtin_offsetof. We're looking for
4530 postfix-expression [ expression ]
4532 FOR_OFFSETOF is set if we're being called in that context, which
4533 changes how we deal with integer constant expressions. */
4535 static tree
4536 cp_parser_postfix_open_square_expression (cp_parser *parser,
4537 tree postfix_expression,
4538 bool for_offsetof)
4540 tree index;
4542 /* Consume the `[' token. */
4543 cp_lexer_consume_token (parser->lexer);
4545 /* Parse the index expression. */
4546 /* ??? For offsetof, there is a question of what to allow here. If
4547 offsetof is not being used in an integral constant expression context,
4548 then we *could* get the right answer by computing the value at runtime.
4549 If we are in an integral constant expression context, then we might
4550 could accept any constant expression; hard to say without analysis.
4551 Rather than open the barn door too wide right away, allow only integer
4552 constant expressions here. */
4553 if (for_offsetof)
4554 index = cp_parser_constant_expression (parser, false, NULL);
4555 else
4556 index = cp_parser_expression (parser, /*cast_p=*/false);
4558 /* Look for the closing `]'. */
4559 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4561 /* Build the ARRAY_REF. */
4562 postfix_expression = grok_array_decl (postfix_expression, index);
4564 /* When not doing offsetof, array references are not permitted in
4565 constant-expressions. */
4566 if (!for_offsetof
4567 && (cp_parser_non_integral_constant_expression
4568 (parser, "an array reference")))
4569 postfix_expression = error_mark_node;
4571 return postfix_expression;
4574 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4575 by cp_parser_builtin_offsetof. We're looking for
4577 postfix-expression . template [opt] id-expression
4578 postfix-expression . pseudo-destructor-name
4579 postfix-expression -> template [opt] id-expression
4580 postfix-expression -> pseudo-destructor-name
4582 FOR_OFFSETOF is set if we're being called in that context. That sorta
4583 limits what of the above we'll actually accept, but nevermind.
4584 TOKEN_TYPE is the "." or "->" token, which will already have been
4585 removed from the stream. */
4587 static tree
4588 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4589 enum cpp_ttype token_type,
4590 tree postfix_expression,
4591 bool for_offsetof, cp_id_kind *idk)
4593 tree name;
4594 bool dependent_p;
4595 bool pseudo_destructor_p;
4596 tree scope = NULL_TREE;
4598 /* If this is a `->' operator, dereference the pointer. */
4599 if (token_type == CPP_DEREF)
4600 postfix_expression = build_x_arrow (postfix_expression);
4601 /* Check to see whether or not the expression is type-dependent. */
4602 dependent_p = type_dependent_expression_p (postfix_expression);
4603 /* The identifier following the `->' or `.' is not qualified. */
4604 parser->scope = NULL_TREE;
4605 parser->qualifying_scope = NULL_TREE;
4606 parser->object_scope = NULL_TREE;
4607 *idk = CP_ID_KIND_NONE;
4608 /* Enter the scope corresponding to the type of the object
4609 given by the POSTFIX_EXPRESSION. */
4610 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4612 scope = TREE_TYPE (postfix_expression);
4613 /* According to the standard, no expression should ever have
4614 reference type. Unfortunately, we do not currently match
4615 the standard in this respect in that our internal representation
4616 of an expression may have reference type even when the standard
4617 says it does not. Therefore, we have to manually obtain the
4618 underlying type here. */
4619 scope = non_reference (scope);
4620 /* The type of the POSTFIX_EXPRESSION must be complete. */
4621 if (scope == unknown_type_node)
4623 error ("%qE does not have class type", postfix_expression);
4624 scope = NULL_TREE;
4626 else
4627 scope = complete_type_or_else (scope, NULL_TREE);
4628 /* Let the name lookup machinery know that we are processing a
4629 class member access expression. */
4630 parser->context->object_type = scope;
4631 /* If something went wrong, we want to be able to discern that case,
4632 as opposed to the case where there was no SCOPE due to the type
4633 of expression being dependent. */
4634 if (!scope)
4635 scope = error_mark_node;
4636 /* If the SCOPE was erroneous, make the various semantic analysis
4637 functions exit quickly -- and without issuing additional error
4638 messages. */
4639 if (scope == error_mark_node)
4640 postfix_expression = error_mark_node;
4643 /* Assume this expression is not a pseudo-destructor access. */
4644 pseudo_destructor_p = false;
4646 /* If the SCOPE is a scalar type, then, if this is a valid program,
4647 we must be looking at a pseudo-destructor-name. */
4648 if (scope && SCALAR_TYPE_P (scope))
4650 tree s;
4651 tree type;
4653 cp_parser_parse_tentatively (parser);
4654 /* Parse the pseudo-destructor-name. */
4655 s = NULL_TREE;
4656 cp_parser_pseudo_destructor_name (parser, &s, &type);
4657 if (cp_parser_parse_definitely (parser))
4659 pseudo_destructor_p = true;
4660 postfix_expression
4661 = finish_pseudo_destructor_expr (postfix_expression,
4662 s, TREE_TYPE (type));
4666 if (!pseudo_destructor_p)
4668 /* If the SCOPE is not a scalar type, we are looking at an
4669 ordinary class member access expression, rather than a
4670 pseudo-destructor-name. */
4671 bool template_p;
4672 /* Parse the id-expression. */
4673 name = (cp_parser_id_expression
4674 (parser,
4675 cp_parser_optional_template_keyword (parser),
4676 /*check_dependency_p=*/true,
4677 &template_p,
4678 /*declarator_p=*/false,
4679 /*optional_p=*/false));
4680 /* In general, build a SCOPE_REF if the member name is qualified.
4681 However, if the name was not dependent and has already been
4682 resolved; there is no need to build the SCOPE_REF. For example;
4684 struct X { void f(); };
4685 template <typename T> void f(T* t) { t->X::f(); }
4687 Even though "t" is dependent, "X::f" is not and has been resolved
4688 to a BASELINK; there is no need to include scope information. */
4690 /* But we do need to remember that there was an explicit scope for
4691 virtual function calls. */
4692 if (parser->scope)
4693 *idk = CP_ID_KIND_QUALIFIED;
4695 /* If the name is a template-id that names a type, we will get a
4696 TYPE_DECL here. That is invalid code. */
4697 if (TREE_CODE (name) == TYPE_DECL)
4699 error ("invalid use of %qD", name);
4700 postfix_expression = error_mark_node;
4702 else
4704 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4706 name = build_qualified_name (/*type=*/NULL_TREE,
4707 parser->scope,
4708 name,
4709 template_p);
4710 parser->scope = NULL_TREE;
4711 parser->qualifying_scope = NULL_TREE;
4712 parser->object_scope = NULL_TREE;
4714 if (scope && name && BASELINK_P (name))
4715 adjust_result_of_qualified_name_lookup
4716 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4717 postfix_expression
4718 = finish_class_member_access_expr (postfix_expression, name,
4719 template_p);
4723 /* We no longer need to look up names in the scope of the object on
4724 the left-hand side of the `.' or `->' operator. */
4725 parser->context->object_type = NULL_TREE;
4727 /* Outside of offsetof, these operators may not appear in
4728 constant-expressions. */
4729 if (!for_offsetof
4730 && (cp_parser_non_integral_constant_expression
4731 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4732 postfix_expression = error_mark_node;
4734 return postfix_expression;
4737 /* Parse a parenthesized expression-list.
4739 expression-list:
4740 assignment-expression
4741 expression-list, assignment-expression
4743 attribute-list:
4744 expression-list
4745 identifier
4746 identifier, expression-list
4748 CAST_P is true if this expression is the target of a cast.
4750 Returns a TREE_LIST. The TREE_VALUE of each node is a
4751 representation of an assignment-expression. Note that a TREE_LIST
4752 is returned even if there is only a single expression in the list.
4753 error_mark_node is returned if the ( and or ) are
4754 missing. NULL_TREE is returned on no expressions. The parentheses
4755 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4756 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4757 indicates whether or not all of the expressions in the list were
4758 constant. */
4760 static tree
4761 cp_parser_parenthesized_expression_list (cp_parser* parser,
4762 bool is_attribute_list,
4763 bool cast_p,
4764 bool *non_constant_p)
4766 tree expression_list = NULL_TREE;
4767 bool fold_expr_p = is_attribute_list;
4768 tree identifier = NULL_TREE;
4770 /* Assume all the expressions will be constant. */
4771 if (non_constant_p)
4772 *non_constant_p = false;
4774 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4775 return error_mark_node;
4777 /* Consume expressions until there are no more. */
4778 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4779 while (true)
4781 tree expr;
4783 /* At the beginning of attribute lists, check to see if the
4784 next token is an identifier. */
4785 if (is_attribute_list
4786 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4788 cp_token *token;
4790 /* Consume the identifier. */
4791 token = cp_lexer_consume_token (parser->lexer);
4792 /* Save the identifier. */
4793 identifier = token->u.value;
4795 else
4797 /* Parse the next assignment-expression. */
4798 if (non_constant_p)
4800 bool expr_non_constant_p;
4801 expr = (cp_parser_constant_expression
4802 (parser, /*allow_non_constant_p=*/true,
4803 &expr_non_constant_p));
4804 if (expr_non_constant_p)
4805 *non_constant_p = true;
4807 else
4808 expr = cp_parser_assignment_expression (parser, cast_p);
4810 if (fold_expr_p)
4811 expr = fold_non_dependent_expr (expr);
4813 /* Add it to the list. We add error_mark_node
4814 expressions to the list, so that we can still tell if
4815 the correct form for a parenthesized expression-list
4816 is found. That gives better errors. */
4817 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4819 if (expr == error_mark_node)
4820 goto skip_comma;
4823 /* After the first item, attribute lists look the same as
4824 expression lists. */
4825 is_attribute_list = false;
4827 get_comma:;
4828 /* If the next token isn't a `,', then we are done. */
4829 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4830 break;
4832 /* Otherwise, consume the `,' and keep going. */
4833 cp_lexer_consume_token (parser->lexer);
4836 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4838 int ending;
4840 skip_comma:;
4841 /* We try and resync to an unnested comma, as that will give the
4842 user better diagnostics. */
4843 ending = cp_parser_skip_to_closing_parenthesis (parser,
4844 /*recovering=*/true,
4845 /*or_comma=*/true,
4846 /*consume_paren=*/true);
4847 if (ending < 0)
4848 goto get_comma;
4849 if (!ending)
4850 return error_mark_node;
4853 /* We built up the list in reverse order so we must reverse it now. */
4854 expression_list = nreverse (expression_list);
4855 if (identifier)
4856 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4858 return expression_list;
4861 /* Parse a pseudo-destructor-name.
4863 pseudo-destructor-name:
4864 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4865 :: [opt] nested-name-specifier template template-id :: ~ type-name
4866 :: [opt] nested-name-specifier [opt] ~ type-name
4868 If either of the first two productions is used, sets *SCOPE to the
4869 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4870 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4871 or ERROR_MARK_NODE if the parse fails. */
4873 static void
4874 cp_parser_pseudo_destructor_name (cp_parser* parser,
4875 tree* scope,
4876 tree* type)
4878 bool nested_name_specifier_p;
4880 /* Assume that things will not work out. */
4881 *type = error_mark_node;
4883 /* Look for the optional `::' operator. */
4884 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4885 /* Look for the optional nested-name-specifier. */
4886 nested_name_specifier_p
4887 = (cp_parser_nested_name_specifier_opt (parser,
4888 /*typename_keyword_p=*/false,
4889 /*check_dependency_p=*/true,
4890 /*type_p=*/false,
4891 /*is_declaration=*/true)
4892 != NULL_TREE);
4893 /* Now, if we saw a nested-name-specifier, we might be doing the
4894 second production. */
4895 if (nested_name_specifier_p
4896 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4898 /* Consume the `template' keyword. */
4899 cp_lexer_consume_token (parser->lexer);
4900 /* Parse the template-id. */
4901 cp_parser_template_id (parser,
4902 /*template_keyword_p=*/true,
4903 /*check_dependency_p=*/false,
4904 /*is_declaration=*/true);
4905 /* Look for the `::' token. */
4906 cp_parser_require (parser, CPP_SCOPE, "`::'");
4908 /* If the next token is not a `~', then there might be some
4909 additional qualification. */
4910 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4912 /* Look for the type-name. */
4913 *scope = TREE_TYPE (cp_parser_type_name (parser));
4915 if (*scope == error_mark_node)
4916 return;
4918 /* If we don't have ::~, then something has gone wrong. Since
4919 the only caller of this function is looking for something
4920 after `.' or `->' after a scalar type, most likely the
4921 program is trying to get a member of a non-aggregate
4922 type. */
4923 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4924 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4926 cp_parser_error (parser, "request for member of non-aggregate type");
4927 return;
4930 /* Look for the `::' token. */
4931 cp_parser_require (parser, CPP_SCOPE, "`::'");
4933 else
4934 *scope = NULL_TREE;
4936 /* Look for the `~'. */
4937 cp_parser_require (parser, CPP_COMPL, "`~'");
4938 /* Look for the type-name again. We are not responsible for
4939 checking that it matches the first type-name. */
4940 *type = cp_parser_type_name (parser);
4943 /* Parse a unary-expression.
4945 unary-expression:
4946 postfix-expression
4947 ++ cast-expression
4948 -- cast-expression
4949 unary-operator cast-expression
4950 sizeof unary-expression
4951 sizeof ( type-id )
4952 new-expression
4953 delete-expression
4955 GNU Extensions:
4957 unary-expression:
4958 __extension__ cast-expression
4959 __alignof__ unary-expression
4960 __alignof__ ( type-id )
4961 __real__ cast-expression
4962 __imag__ cast-expression
4963 && identifier
4965 ADDRESS_P is true iff the unary-expression is appearing as the
4966 operand of the `&' operator. CAST_P is true if this expression is
4967 the target of a cast.
4969 Returns a representation of the expression. */
4971 static tree
4972 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4974 cp_token *token;
4975 enum tree_code unary_operator;
4977 /* Peek at the next token. */
4978 token = cp_lexer_peek_token (parser->lexer);
4979 /* Some keywords give away the kind of expression. */
4980 if (token->type == CPP_KEYWORD)
4982 enum rid keyword = token->keyword;
4984 switch (keyword)
4986 case RID_ALIGNOF:
4987 case RID_SIZEOF:
4989 tree operand;
4990 enum tree_code op;
4992 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4993 /* Consume the token. */
4994 cp_lexer_consume_token (parser->lexer);
4995 /* Parse the operand. */
4996 operand = cp_parser_sizeof_operand (parser, keyword);
4998 if (TYPE_P (operand))
4999 return cxx_sizeof_or_alignof_type (operand, op, true);
5000 else
5001 return cxx_sizeof_or_alignof_expr (operand, op);
5004 case RID_NEW:
5005 return cp_parser_new_expression (parser);
5007 case RID_DELETE:
5008 return cp_parser_delete_expression (parser);
5010 case RID_EXTENSION:
5012 /* The saved value of the PEDANTIC flag. */
5013 int saved_pedantic;
5014 tree expr;
5016 /* Save away the PEDANTIC flag. */
5017 cp_parser_extension_opt (parser, &saved_pedantic);
5018 /* Parse the cast-expression. */
5019 expr = cp_parser_simple_cast_expression (parser);
5020 /* Restore the PEDANTIC flag. */
5021 pedantic = saved_pedantic;
5023 return expr;
5026 case RID_REALPART:
5027 case RID_IMAGPART:
5029 tree expression;
5031 /* Consume the `__real__' or `__imag__' token. */
5032 cp_lexer_consume_token (parser->lexer);
5033 /* Parse the cast-expression. */
5034 expression = cp_parser_simple_cast_expression (parser);
5035 /* Create the complete representation. */
5036 return build_x_unary_op ((keyword == RID_REALPART
5037 ? REALPART_EXPR : IMAGPART_EXPR),
5038 expression);
5040 break;
5042 default:
5043 break;
5047 /* Look for the `:: new' and `:: delete', which also signal the
5048 beginning of a new-expression, or delete-expression,
5049 respectively. If the next token is `::', then it might be one of
5050 these. */
5051 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5053 enum rid keyword;
5055 /* See if the token after the `::' is one of the keywords in
5056 which we're interested. */
5057 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5058 /* If it's `new', we have a new-expression. */
5059 if (keyword == RID_NEW)
5060 return cp_parser_new_expression (parser);
5061 /* Similarly, for `delete'. */
5062 else if (keyword == RID_DELETE)
5063 return cp_parser_delete_expression (parser);
5066 /* Look for a unary operator. */
5067 unary_operator = cp_parser_unary_operator (token);
5068 /* The `++' and `--' operators can be handled similarly, even though
5069 they are not technically unary-operators in the grammar. */
5070 if (unary_operator == ERROR_MARK)
5072 if (token->type == CPP_PLUS_PLUS)
5073 unary_operator = PREINCREMENT_EXPR;
5074 else if (token->type == CPP_MINUS_MINUS)
5075 unary_operator = PREDECREMENT_EXPR;
5076 /* Handle the GNU address-of-label extension. */
5077 else if (cp_parser_allow_gnu_extensions_p (parser)
5078 && token->type == CPP_AND_AND)
5080 tree identifier;
5082 /* Consume the '&&' token. */
5083 cp_lexer_consume_token (parser->lexer);
5084 /* Look for the identifier. */
5085 identifier = cp_parser_identifier (parser);
5086 /* Create an expression representing the address. */
5087 return finish_label_address_expr (identifier);
5090 if (unary_operator != ERROR_MARK)
5092 tree cast_expression;
5093 tree expression = error_mark_node;
5094 const char *non_constant_p = NULL;
5096 /* Consume the operator token. */
5097 token = cp_lexer_consume_token (parser->lexer);
5098 /* Parse the cast-expression. */
5099 cast_expression
5100 = cp_parser_cast_expression (parser,
5101 unary_operator == ADDR_EXPR,
5102 /*cast_p=*/false);
5103 /* Now, build an appropriate representation. */
5104 switch (unary_operator)
5106 case INDIRECT_REF:
5107 non_constant_p = "`*'";
5108 expression = build_x_indirect_ref (cast_expression, "unary *");
5109 break;
5111 case ADDR_EXPR:
5112 non_constant_p = "`&'";
5113 /* Fall through. */
5114 case BIT_NOT_EXPR:
5115 expression = build_x_unary_op (unary_operator, cast_expression);
5116 break;
5118 case PREINCREMENT_EXPR:
5119 case PREDECREMENT_EXPR:
5120 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5121 ? "`++'" : "`--'");
5122 /* Fall through. */
5123 case UNARY_PLUS_EXPR:
5124 case NEGATE_EXPR:
5125 case TRUTH_NOT_EXPR:
5126 expression = finish_unary_op_expr (unary_operator, cast_expression);
5127 break;
5129 default:
5130 gcc_unreachable ();
5133 if (non_constant_p
5134 && cp_parser_non_integral_constant_expression (parser,
5135 non_constant_p))
5136 expression = error_mark_node;
5138 return expression;
5141 return cp_parser_postfix_expression (parser, address_p, cast_p);
5144 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5145 unary-operator, the corresponding tree code is returned. */
5147 static enum tree_code
5148 cp_parser_unary_operator (cp_token* token)
5150 switch (token->type)
5152 case CPP_MULT:
5153 return INDIRECT_REF;
5155 case CPP_AND:
5156 return ADDR_EXPR;
5158 case CPP_PLUS:
5159 return UNARY_PLUS_EXPR;
5161 case CPP_MINUS:
5162 return NEGATE_EXPR;
5164 case CPP_NOT:
5165 return TRUTH_NOT_EXPR;
5167 case CPP_COMPL:
5168 return BIT_NOT_EXPR;
5170 default:
5171 return ERROR_MARK;
5175 /* Parse a new-expression.
5177 new-expression:
5178 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5179 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5181 Returns a representation of the expression. */
5183 static tree
5184 cp_parser_new_expression (cp_parser* parser)
5186 bool global_scope_p;
5187 tree placement;
5188 tree type;
5189 tree initializer;
5190 tree nelts;
5192 /* Look for the optional `::' operator. */
5193 global_scope_p
5194 = (cp_parser_global_scope_opt (parser,
5195 /*current_scope_valid_p=*/false)
5196 != NULL_TREE);
5197 /* Look for the `new' operator. */
5198 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5199 /* There's no easy way to tell a new-placement from the
5200 `( type-id )' construct. */
5201 cp_parser_parse_tentatively (parser);
5202 /* Look for a new-placement. */
5203 placement = cp_parser_new_placement (parser);
5204 /* If that didn't work out, there's no new-placement. */
5205 if (!cp_parser_parse_definitely (parser))
5206 placement = NULL_TREE;
5208 /* If the next token is a `(', then we have a parenthesized
5209 type-id. */
5210 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5212 /* Consume the `('. */
5213 cp_lexer_consume_token (parser->lexer);
5214 /* Parse the type-id. */
5215 type = cp_parser_type_id (parser);
5216 /* Look for the closing `)'. */
5217 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5218 /* There should not be a direct-new-declarator in this production,
5219 but GCC used to allowed this, so we check and emit a sensible error
5220 message for this case. */
5221 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5223 error ("array bound forbidden after parenthesized type-id");
5224 inform ("try removing the parentheses around the type-id");
5225 cp_parser_direct_new_declarator (parser);
5227 nelts = NULL_TREE;
5229 /* Otherwise, there must be a new-type-id. */
5230 else
5231 type = cp_parser_new_type_id (parser, &nelts);
5233 /* If the next token is a `(', then we have a new-initializer. */
5234 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5235 initializer = cp_parser_new_initializer (parser);
5236 else
5237 initializer = NULL_TREE;
5239 /* A new-expression may not appear in an integral constant
5240 expression. */
5241 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5242 return error_mark_node;
5244 /* Create a representation of the new-expression. */
5245 return build_new (placement, type, nelts, initializer, global_scope_p);
5248 /* Parse a new-placement.
5250 new-placement:
5251 ( expression-list )
5253 Returns the same representation as for an expression-list. */
5255 static tree
5256 cp_parser_new_placement (cp_parser* parser)
5258 tree expression_list;
5260 /* Parse the expression-list. */
5261 expression_list = (cp_parser_parenthesized_expression_list
5262 (parser, false, /*cast_p=*/false,
5263 /*non_constant_p=*/NULL));
5265 return expression_list;
5268 /* Parse a new-type-id.
5270 new-type-id:
5271 type-specifier-seq new-declarator [opt]
5273 Returns the TYPE allocated. If the new-type-id indicates an array
5274 type, *NELTS is set to the number of elements in the last array
5275 bound; the TYPE will not include the last array bound. */
5277 static tree
5278 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5280 cp_decl_specifier_seq type_specifier_seq;
5281 cp_declarator *new_declarator;
5282 cp_declarator *declarator;
5283 cp_declarator *outer_declarator;
5284 const char *saved_message;
5285 tree type;
5287 /* The type-specifier sequence must not contain type definitions.
5288 (It cannot contain declarations of new types either, but if they
5289 are not definitions we will catch that because they are not
5290 complete.) */
5291 saved_message = parser->type_definition_forbidden_message;
5292 parser->type_definition_forbidden_message
5293 = "types may not be defined in a new-type-id";
5294 /* Parse the type-specifier-seq. */
5295 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5296 &type_specifier_seq);
5297 /* Restore the old message. */
5298 parser->type_definition_forbidden_message = saved_message;
5299 /* Parse the new-declarator. */
5300 new_declarator = cp_parser_new_declarator_opt (parser);
5302 /* Determine the number of elements in the last array dimension, if
5303 any. */
5304 *nelts = NULL_TREE;
5305 /* Skip down to the last array dimension. */
5306 declarator = new_declarator;
5307 outer_declarator = NULL;
5308 while (declarator && (declarator->kind == cdk_pointer
5309 || declarator->kind == cdk_ptrmem))
5311 outer_declarator = declarator;
5312 declarator = declarator->declarator;
5314 while (declarator
5315 && declarator->kind == cdk_array
5316 && declarator->declarator
5317 && declarator->declarator->kind == cdk_array)
5319 outer_declarator = declarator;
5320 declarator = declarator->declarator;
5323 if (declarator && declarator->kind == cdk_array)
5325 *nelts = declarator->u.array.bounds;
5326 if (*nelts == error_mark_node)
5327 *nelts = integer_one_node;
5329 if (outer_declarator)
5330 outer_declarator->declarator = declarator->declarator;
5331 else
5332 new_declarator = NULL;
5335 type = groktypename (&type_specifier_seq, new_declarator);
5336 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5338 *nelts = array_type_nelts_top (type);
5339 type = TREE_TYPE (type);
5341 return type;
5344 /* Parse an (optional) new-declarator.
5346 new-declarator:
5347 ptr-operator new-declarator [opt]
5348 direct-new-declarator
5350 Returns the declarator. */
5352 static cp_declarator *
5353 cp_parser_new_declarator_opt (cp_parser* parser)
5355 enum tree_code code;
5356 tree type;
5357 cp_cv_quals cv_quals;
5359 /* We don't know if there's a ptr-operator next, or not. */
5360 cp_parser_parse_tentatively (parser);
5361 /* Look for a ptr-operator. */
5362 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5363 /* If that worked, look for more new-declarators. */
5364 if (cp_parser_parse_definitely (parser))
5366 cp_declarator *declarator;
5368 /* Parse another optional declarator. */
5369 declarator = cp_parser_new_declarator_opt (parser);
5371 /* Create the representation of the declarator. */
5372 if (type)
5373 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5374 else if (code == INDIRECT_REF)
5375 declarator = make_pointer_declarator (cv_quals, declarator);
5376 else
5377 declarator = make_reference_declarator (cv_quals, declarator);
5379 return declarator;
5382 /* If the next token is a `[', there is a direct-new-declarator. */
5383 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5384 return cp_parser_direct_new_declarator (parser);
5386 return NULL;
5389 /* Parse a direct-new-declarator.
5391 direct-new-declarator:
5392 [ expression ]
5393 direct-new-declarator [constant-expression]
5397 static cp_declarator *
5398 cp_parser_direct_new_declarator (cp_parser* parser)
5400 cp_declarator *declarator = NULL;
5402 while (true)
5404 tree expression;
5406 /* Look for the opening `['. */
5407 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5408 /* The first expression is not required to be constant. */
5409 if (!declarator)
5411 expression = cp_parser_expression (parser, /*cast_p=*/false);
5412 /* The standard requires that the expression have integral
5413 type. DR 74 adds enumeration types. We believe that the
5414 real intent is that these expressions be handled like the
5415 expression in a `switch' condition, which also allows
5416 classes with a single conversion to integral or
5417 enumeration type. */
5418 if (!processing_template_decl)
5420 expression
5421 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5422 expression,
5423 /*complain=*/true);
5424 if (!expression)
5426 error ("expression in new-declarator must have integral "
5427 "or enumeration type");
5428 expression = error_mark_node;
5432 /* But all the other expressions must be. */
5433 else
5434 expression
5435 = cp_parser_constant_expression (parser,
5436 /*allow_non_constant=*/false,
5437 NULL);
5438 /* Look for the closing `]'. */
5439 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5441 /* Add this bound to the declarator. */
5442 declarator = make_array_declarator (declarator, expression);
5444 /* If the next token is not a `[', then there are no more
5445 bounds. */
5446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5447 break;
5450 return declarator;
5453 /* Parse a new-initializer.
5455 new-initializer:
5456 ( expression-list [opt] )
5458 Returns a representation of the expression-list. If there is no
5459 expression-list, VOID_ZERO_NODE is returned. */
5461 static tree
5462 cp_parser_new_initializer (cp_parser* parser)
5464 tree expression_list;
5466 expression_list = (cp_parser_parenthesized_expression_list
5467 (parser, false, /*cast_p=*/false,
5468 /*non_constant_p=*/NULL));
5469 if (!expression_list)
5470 expression_list = void_zero_node;
5472 return expression_list;
5475 /* Parse a delete-expression.
5477 delete-expression:
5478 :: [opt] delete cast-expression
5479 :: [opt] delete [ ] cast-expression
5481 Returns a representation of the expression. */
5483 static tree
5484 cp_parser_delete_expression (cp_parser* parser)
5486 bool global_scope_p;
5487 bool array_p;
5488 tree expression;
5490 /* Look for the optional `::' operator. */
5491 global_scope_p
5492 = (cp_parser_global_scope_opt (parser,
5493 /*current_scope_valid_p=*/false)
5494 != NULL_TREE);
5495 /* Look for the `delete' keyword. */
5496 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5497 /* See if the array syntax is in use. */
5498 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5500 /* Consume the `[' token. */
5501 cp_lexer_consume_token (parser->lexer);
5502 /* Look for the `]' token. */
5503 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5504 /* Remember that this is the `[]' construct. */
5505 array_p = true;
5507 else
5508 array_p = false;
5510 /* Parse the cast-expression. */
5511 expression = cp_parser_simple_cast_expression (parser);
5513 /* A delete-expression may not appear in an integral constant
5514 expression. */
5515 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5516 return error_mark_node;
5518 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5521 /* Parse a cast-expression.
5523 cast-expression:
5524 unary-expression
5525 ( type-id ) cast-expression
5527 ADDRESS_P is true iff the unary-expression is appearing as the
5528 operand of the `&' operator. CAST_P is true if this expression is
5529 the target of a cast.
5531 Returns a representation of the expression. */
5533 static tree
5534 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5536 /* If it's a `(', then we might be looking at a cast. */
5537 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5539 tree type = NULL_TREE;
5540 tree expr = NULL_TREE;
5541 bool compound_literal_p;
5542 const char *saved_message;
5544 /* There's no way to know yet whether or not this is a cast.
5545 For example, `(int (3))' is a unary-expression, while `(int)
5546 3' is a cast. So, we resort to parsing tentatively. */
5547 cp_parser_parse_tentatively (parser);
5548 /* Types may not be defined in a cast. */
5549 saved_message = parser->type_definition_forbidden_message;
5550 parser->type_definition_forbidden_message
5551 = "types may not be defined in casts";
5552 /* Consume the `('. */
5553 cp_lexer_consume_token (parser->lexer);
5554 /* A very tricky bit is that `(struct S) { 3 }' is a
5555 compound-literal (which we permit in C++ as an extension).
5556 But, that construct is not a cast-expression -- it is a
5557 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5558 is legal; if the compound-literal were a cast-expression,
5559 you'd need an extra set of parentheses.) But, if we parse
5560 the type-id, and it happens to be a class-specifier, then we
5561 will commit to the parse at that point, because we cannot
5562 undo the action that is done when creating a new class. So,
5563 then we cannot back up and do a postfix-expression.
5565 Therefore, we scan ahead to the closing `)', and check to see
5566 if the token after the `)' is a `{'. If so, we are not
5567 looking at a cast-expression.
5569 Save tokens so that we can put them back. */
5570 cp_lexer_save_tokens (parser->lexer);
5571 /* Skip tokens until the next token is a closing parenthesis.
5572 If we find the closing `)', and the next token is a `{', then
5573 we are looking at a compound-literal. */
5574 compound_literal_p
5575 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5576 /*consume_paren=*/true)
5577 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5578 /* Roll back the tokens we skipped. */
5579 cp_lexer_rollback_tokens (parser->lexer);
5580 /* If we were looking at a compound-literal, simulate an error
5581 so that the call to cp_parser_parse_definitely below will
5582 fail. */
5583 if (compound_literal_p)
5584 cp_parser_simulate_error (parser);
5585 else
5587 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5588 parser->in_type_id_in_expr_p = true;
5589 /* Look for the type-id. */
5590 type = cp_parser_type_id (parser);
5591 /* Look for the closing `)'. */
5592 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5593 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5596 /* Restore the saved message. */
5597 parser->type_definition_forbidden_message = saved_message;
5599 /* If ok so far, parse the dependent expression. We cannot be
5600 sure it is a cast. Consider `(T ())'. It is a parenthesized
5601 ctor of T, but looks like a cast to function returning T
5602 without a dependent expression. */
5603 if (!cp_parser_error_occurred (parser))
5604 expr = cp_parser_cast_expression (parser,
5605 /*address_p=*/false,
5606 /*cast_p=*/true);
5608 if (cp_parser_parse_definitely (parser))
5610 /* Warn about old-style casts, if so requested. */
5611 if (warn_old_style_cast
5612 && !in_system_header
5613 && !VOID_TYPE_P (type)
5614 && current_lang_name != lang_name_c)
5615 warning (OPT_Wold_style_cast, "use of old-style cast");
5617 /* Only type conversions to integral or enumeration types
5618 can be used in constant-expressions. */
5619 if (!cast_valid_in_integral_constant_expression_p (type)
5620 && (cp_parser_non_integral_constant_expression
5621 (parser,
5622 "a cast to a type other than an integral or "
5623 "enumeration type")))
5624 return error_mark_node;
5626 /* Perform the cast. */
5627 expr = build_c_cast (type, expr);
5628 return expr;
5632 /* If we get here, then it's not a cast, so it must be a
5633 unary-expression. */
5634 return cp_parser_unary_expression (parser, address_p, cast_p);
5637 /* Parse a binary expression of the general form:
5639 pm-expression:
5640 cast-expression
5641 pm-expression .* cast-expression
5642 pm-expression ->* cast-expression
5644 multiplicative-expression:
5645 pm-expression
5646 multiplicative-expression * pm-expression
5647 multiplicative-expression / pm-expression
5648 multiplicative-expression % pm-expression
5650 additive-expression:
5651 multiplicative-expression
5652 additive-expression + multiplicative-expression
5653 additive-expression - multiplicative-expression
5655 shift-expression:
5656 additive-expression
5657 shift-expression << additive-expression
5658 shift-expression >> additive-expression
5660 relational-expression:
5661 shift-expression
5662 relational-expression < shift-expression
5663 relational-expression > shift-expression
5664 relational-expression <= shift-expression
5665 relational-expression >= shift-expression
5667 GNU Extension:
5669 relational-expression:
5670 relational-expression <? shift-expression
5671 relational-expression >? shift-expression
5673 equality-expression:
5674 relational-expression
5675 equality-expression == relational-expression
5676 equality-expression != relational-expression
5678 and-expression:
5679 equality-expression
5680 and-expression & equality-expression
5682 exclusive-or-expression:
5683 and-expression
5684 exclusive-or-expression ^ and-expression
5686 inclusive-or-expression:
5687 exclusive-or-expression
5688 inclusive-or-expression | exclusive-or-expression
5690 logical-and-expression:
5691 inclusive-or-expression
5692 logical-and-expression && inclusive-or-expression
5694 logical-or-expression:
5695 logical-and-expression
5696 logical-or-expression || logical-and-expression
5698 All these are implemented with a single function like:
5700 binary-expression:
5701 simple-cast-expression
5702 binary-expression <token> binary-expression
5704 CAST_P is true if this expression is the target of a cast.
5706 The binops_by_token map is used to get the tree codes for each <token> type.
5707 binary-expressions are associated according to a precedence table. */
5709 #define TOKEN_PRECEDENCE(token) \
5710 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5711 ? PREC_NOT_OPERATOR \
5712 : binops_by_token[token->type].prec)
5714 static tree
5715 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5717 cp_parser_expression_stack stack;
5718 cp_parser_expression_stack_entry *sp = &stack[0];
5719 tree lhs, rhs;
5720 cp_token *token;
5721 enum tree_code tree_type, lhs_type, rhs_type;
5722 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5723 bool overloaded_p;
5725 /* Parse the first expression. */
5726 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5727 lhs_type = ERROR_MARK;
5729 for (;;)
5731 /* Get an operator token. */
5732 token = cp_lexer_peek_token (parser->lexer);
5734 new_prec = TOKEN_PRECEDENCE (token);
5736 /* Popping an entry off the stack means we completed a subexpression:
5737 - either we found a token which is not an operator (`>' where it is not
5738 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5739 will happen repeatedly;
5740 - or, we found an operator which has lower priority. This is the case
5741 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5742 parsing `3 * 4'. */
5743 if (new_prec <= prec)
5745 if (sp == stack)
5746 break;
5747 else
5748 goto pop;
5751 get_rhs:
5752 tree_type = binops_by_token[token->type].tree_type;
5754 /* We used the operator token. */
5755 cp_lexer_consume_token (parser->lexer);
5757 /* Extract another operand. It may be the RHS of this expression
5758 or the LHS of a new, higher priority expression. */
5759 rhs = cp_parser_simple_cast_expression (parser);
5760 rhs_type = ERROR_MARK;
5762 /* Get another operator token. Look up its precedence to avoid
5763 building a useless (immediately popped) stack entry for common
5764 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5765 token = cp_lexer_peek_token (parser->lexer);
5766 lookahead_prec = TOKEN_PRECEDENCE (token);
5767 if (lookahead_prec > new_prec)
5769 /* ... and prepare to parse the RHS of the new, higher priority
5770 expression. Since precedence levels on the stack are
5771 monotonically increasing, we do not have to care about
5772 stack overflows. */
5773 sp->prec = prec;
5774 sp->tree_type = tree_type;
5775 sp->lhs = lhs;
5776 sp->lhs_type = lhs_type;
5777 sp++;
5778 lhs = rhs;
5779 lhs_type = rhs_type;
5780 prec = new_prec;
5781 new_prec = lookahead_prec;
5782 goto get_rhs;
5784 pop:
5785 /* If the stack is not empty, we have parsed into LHS the right side
5786 (`4' in the example above) of an expression we had suspended.
5787 We can use the information on the stack to recover the LHS (`3')
5788 from the stack together with the tree code (`MULT_EXPR'), and
5789 the precedence of the higher level subexpression
5790 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5791 which will be used to actually build the additive expression. */
5792 --sp;
5793 prec = sp->prec;
5794 tree_type = sp->tree_type;
5795 rhs = lhs;
5796 rhs_type = lhs_type;
5797 lhs = sp->lhs;
5798 lhs_type = sp->lhs_type;
5801 overloaded_p = false;
5802 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5803 &overloaded_p);
5804 lhs_type = tree_type;
5806 /* If the binary operator required the use of an overloaded operator,
5807 then this expression cannot be an integral constant-expression.
5808 An overloaded operator can be used even if both operands are
5809 otherwise permissible in an integral constant-expression if at
5810 least one of the operands is of enumeration type. */
5812 if (overloaded_p
5813 && (cp_parser_non_integral_constant_expression
5814 (parser, "calls to overloaded operators")))
5815 return error_mark_node;
5818 return lhs;
5822 /* Parse the `? expression : assignment-expression' part of a
5823 conditional-expression. The LOGICAL_OR_EXPR is the
5824 logical-or-expression that started the conditional-expression.
5825 Returns a representation of the entire conditional-expression.
5827 This routine is used by cp_parser_assignment_expression.
5829 ? expression : assignment-expression
5831 GNU Extensions:
5833 ? : assignment-expression */
5835 static tree
5836 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5838 tree expr;
5839 tree assignment_expr;
5841 /* Consume the `?' token. */
5842 cp_lexer_consume_token (parser->lexer);
5843 if (cp_parser_allow_gnu_extensions_p (parser)
5844 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5845 /* Implicit true clause. */
5846 expr = NULL_TREE;
5847 else
5848 /* Parse the expression. */
5849 expr = cp_parser_expression (parser, /*cast_p=*/false);
5851 /* The next token should be a `:'. */
5852 cp_parser_require (parser, CPP_COLON, "`:'");
5853 /* Parse the assignment-expression. */
5854 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5856 /* Build the conditional-expression. */
5857 return build_x_conditional_expr (logical_or_expr,
5858 expr,
5859 assignment_expr);
5862 /* Parse an assignment-expression.
5864 assignment-expression:
5865 conditional-expression
5866 logical-or-expression assignment-operator assignment_expression
5867 throw-expression
5869 CAST_P is true if this expression is the target of a cast.
5871 Returns a representation for the expression. */
5873 static tree
5874 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5876 tree expr;
5878 /* If the next token is the `throw' keyword, then we're looking at
5879 a throw-expression. */
5880 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5881 expr = cp_parser_throw_expression (parser);
5882 /* Otherwise, it must be that we are looking at a
5883 logical-or-expression. */
5884 else
5886 /* Parse the binary expressions (logical-or-expression). */
5887 expr = cp_parser_binary_expression (parser, cast_p);
5888 /* If the next token is a `?' then we're actually looking at a
5889 conditional-expression. */
5890 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5891 return cp_parser_question_colon_clause (parser, expr);
5892 else
5894 enum tree_code assignment_operator;
5896 /* If it's an assignment-operator, we're using the second
5897 production. */
5898 assignment_operator
5899 = cp_parser_assignment_operator_opt (parser);
5900 if (assignment_operator != ERROR_MARK)
5902 tree rhs;
5904 /* Parse the right-hand side of the assignment. */
5905 rhs = cp_parser_assignment_expression (parser, cast_p);
5906 /* An assignment may not appear in a
5907 constant-expression. */
5908 if (cp_parser_non_integral_constant_expression (parser,
5909 "an assignment"))
5910 return error_mark_node;
5911 /* Build the assignment expression. */
5912 expr = build_x_modify_expr (expr,
5913 assignment_operator,
5914 rhs);
5919 return expr;
5922 /* Parse an (optional) assignment-operator.
5924 assignment-operator: one of
5925 = *= /= %= += -= >>= <<= &= ^= |=
5927 GNU Extension:
5929 assignment-operator: one of
5930 <?= >?=
5932 If the next token is an assignment operator, the corresponding tree
5933 code is returned, and the token is consumed. For example, for
5934 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5935 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5936 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5937 operator, ERROR_MARK is returned. */
5939 static enum tree_code
5940 cp_parser_assignment_operator_opt (cp_parser* parser)
5942 enum tree_code op;
5943 cp_token *token;
5945 /* Peek at the next toen. */
5946 token = cp_lexer_peek_token (parser->lexer);
5948 switch (token->type)
5950 case CPP_EQ:
5951 op = NOP_EXPR;
5952 break;
5954 case CPP_MULT_EQ:
5955 op = MULT_EXPR;
5956 break;
5958 case CPP_DIV_EQ:
5959 op = TRUNC_DIV_EXPR;
5960 break;
5962 case CPP_MOD_EQ:
5963 op = TRUNC_MOD_EXPR;
5964 break;
5966 case CPP_PLUS_EQ:
5967 op = PLUS_EXPR;
5968 break;
5970 case CPP_MINUS_EQ:
5971 op = MINUS_EXPR;
5972 break;
5974 case CPP_RSHIFT_EQ:
5975 op = RSHIFT_EXPR;
5976 break;
5978 case CPP_LSHIFT_EQ:
5979 op = LSHIFT_EXPR;
5980 break;
5982 case CPP_AND_EQ:
5983 op = BIT_AND_EXPR;
5984 break;
5986 case CPP_XOR_EQ:
5987 op = BIT_XOR_EXPR;
5988 break;
5990 case CPP_OR_EQ:
5991 op = BIT_IOR_EXPR;
5992 break;
5994 default:
5995 /* Nothing else is an assignment operator. */
5996 op = ERROR_MARK;
5999 /* If it was an assignment operator, consume it. */
6000 if (op != ERROR_MARK)
6001 cp_lexer_consume_token (parser->lexer);
6003 return op;
6006 /* Parse an expression.
6008 expression:
6009 assignment-expression
6010 expression , assignment-expression
6012 CAST_P is true if this expression is the target of a cast.
6014 Returns a representation of the expression. */
6016 static tree
6017 cp_parser_expression (cp_parser* parser, bool cast_p)
6019 tree expression = NULL_TREE;
6021 while (true)
6023 tree assignment_expression;
6025 /* Parse the next assignment-expression. */
6026 assignment_expression
6027 = cp_parser_assignment_expression (parser, cast_p);
6028 /* If this is the first assignment-expression, we can just
6029 save it away. */
6030 if (!expression)
6031 expression = assignment_expression;
6032 else
6033 expression = build_x_compound_expr (expression,
6034 assignment_expression);
6035 /* If the next token is not a comma, then we are done with the
6036 expression. */
6037 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6038 break;
6039 /* Consume the `,'. */
6040 cp_lexer_consume_token (parser->lexer);
6041 /* A comma operator cannot appear in a constant-expression. */
6042 if (cp_parser_non_integral_constant_expression (parser,
6043 "a comma operator"))
6044 expression = error_mark_node;
6047 return expression;
6050 /* Parse a constant-expression.
6052 constant-expression:
6053 conditional-expression
6055 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6056 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6057 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6058 is false, NON_CONSTANT_P should be NULL. */
6060 static tree
6061 cp_parser_constant_expression (cp_parser* parser,
6062 bool allow_non_constant_p,
6063 bool *non_constant_p)
6065 bool saved_integral_constant_expression_p;
6066 bool saved_allow_non_integral_constant_expression_p;
6067 bool saved_non_integral_constant_expression_p;
6068 tree expression;
6070 /* It might seem that we could simply parse the
6071 conditional-expression, and then check to see if it were
6072 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6073 one that the compiler can figure out is constant, possibly after
6074 doing some simplifications or optimizations. The standard has a
6075 precise definition of constant-expression, and we must honor
6076 that, even though it is somewhat more restrictive.
6078 For example:
6080 int i[(2, 3)];
6082 is not a legal declaration, because `(2, 3)' is not a
6083 constant-expression. The `,' operator is forbidden in a
6084 constant-expression. However, GCC's constant-folding machinery
6085 will fold this operation to an INTEGER_CST for `3'. */
6087 /* Save the old settings. */
6088 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6089 saved_allow_non_integral_constant_expression_p
6090 = parser->allow_non_integral_constant_expression_p;
6091 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6092 /* We are now parsing a constant-expression. */
6093 parser->integral_constant_expression_p = true;
6094 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6095 parser->non_integral_constant_expression_p = false;
6096 /* Although the grammar says "conditional-expression", we parse an
6097 "assignment-expression", which also permits "throw-expression"
6098 and the use of assignment operators. In the case that
6099 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6100 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6101 actually essential that we look for an assignment-expression.
6102 For example, cp_parser_initializer_clauses uses this function to
6103 determine whether a particular assignment-expression is in fact
6104 constant. */
6105 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6106 /* Restore the old settings. */
6107 parser->integral_constant_expression_p
6108 = saved_integral_constant_expression_p;
6109 parser->allow_non_integral_constant_expression_p
6110 = saved_allow_non_integral_constant_expression_p;
6111 if (allow_non_constant_p)
6112 *non_constant_p = parser->non_integral_constant_expression_p;
6113 else if (parser->non_integral_constant_expression_p)
6114 expression = error_mark_node;
6115 parser->non_integral_constant_expression_p
6116 = saved_non_integral_constant_expression_p;
6118 return expression;
6121 /* Parse __builtin_offsetof.
6123 offsetof-expression:
6124 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6126 offsetof-member-designator:
6127 id-expression
6128 | offsetof-member-designator "." id-expression
6129 | offsetof-member-designator "[" expression "]" */
6131 static tree
6132 cp_parser_builtin_offsetof (cp_parser *parser)
6134 int save_ice_p, save_non_ice_p;
6135 tree type, expr;
6136 cp_id_kind dummy;
6138 /* We're about to accept non-integral-constant things, but will
6139 definitely yield an integral constant expression. Save and
6140 restore these values around our local parsing. */
6141 save_ice_p = parser->integral_constant_expression_p;
6142 save_non_ice_p = parser->non_integral_constant_expression_p;
6144 /* Consume the "__builtin_offsetof" token. */
6145 cp_lexer_consume_token (parser->lexer);
6146 /* Consume the opening `('. */
6147 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6148 /* Parse the type-id. */
6149 type = cp_parser_type_id (parser);
6150 /* Look for the `,'. */
6151 cp_parser_require (parser, CPP_COMMA, "`,'");
6153 /* Build the (type *)null that begins the traditional offsetof macro. */
6154 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6156 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6157 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6158 true, &dummy);
6159 while (true)
6161 cp_token *token = cp_lexer_peek_token (parser->lexer);
6162 switch (token->type)
6164 case CPP_OPEN_SQUARE:
6165 /* offsetof-member-designator "[" expression "]" */
6166 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6167 break;
6169 case CPP_DOT:
6170 /* offsetof-member-designator "." identifier */
6171 cp_lexer_consume_token (parser->lexer);
6172 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6173 true, &dummy);
6174 break;
6176 case CPP_CLOSE_PAREN:
6177 /* Consume the ")" token. */
6178 cp_lexer_consume_token (parser->lexer);
6179 goto success;
6181 default:
6182 /* Error. We know the following require will fail, but
6183 that gives the proper error message. */
6184 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6185 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6186 expr = error_mark_node;
6187 goto failure;
6191 success:
6192 /* If we're processing a template, we can't finish the semantics yet.
6193 Otherwise we can fold the entire expression now. */
6194 if (processing_template_decl)
6195 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6196 else
6197 expr = finish_offsetof (expr);
6199 failure:
6200 parser->integral_constant_expression_p = save_ice_p;
6201 parser->non_integral_constant_expression_p = save_non_ice_p;
6203 return expr;
6206 /* Statements [gram.stmt.stmt] */
6208 /* Parse a statement.
6210 statement:
6211 labeled-statement
6212 expression-statement
6213 compound-statement
6214 selection-statement
6215 iteration-statement
6216 jump-statement
6217 declaration-statement
6218 try-block
6220 IN_COMPOUND is true when the statement is nested inside a
6221 cp_parser_compound_statement; this matters for certain pragmas.
6223 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6224 is a (possibly labeled) if statement which is not enclosed in braces
6225 and has an else clause. This is used to implement -Wparentheses. */
6227 static void
6228 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6229 bool in_compound, bool *if_p)
6231 tree statement;
6232 cp_token *token;
6233 location_t statement_location;
6235 restart:
6236 if (if_p != NULL)
6237 *if_p = false;
6238 /* There is no statement yet. */
6239 statement = NULL_TREE;
6240 /* Peek at the next token. */
6241 token = cp_lexer_peek_token (parser->lexer);
6242 /* Remember the location of the first token in the statement. */
6243 statement_location = token->location;
6244 /* If this is a keyword, then that will often determine what kind of
6245 statement we have. */
6246 if (token->type == CPP_KEYWORD)
6248 enum rid keyword = token->keyword;
6250 switch (keyword)
6252 case RID_CASE:
6253 case RID_DEFAULT:
6254 /* Looks like a labeled-statement with a case label.
6255 Parse the label, and then use tail recursion to parse
6256 the statement. */
6257 cp_parser_label_for_labeled_statement (parser);
6258 goto restart;
6260 case RID_IF:
6261 case RID_SWITCH:
6262 statement = cp_parser_selection_statement (parser, if_p);
6263 break;
6265 case RID_WHILE:
6266 case RID_DO:
6267 case RID_FOR:
6268 statement = cp_parser_iteration_statement (parser);
6269 break;
6271 case RID_BREAK:
6272 case RID_CONTINUE:
6273 case RID_RETURN:
6274 case RID_GOTO:
6275 statement = cp_parser_jump_statement (parser);
6276 break;
6278 /* Objective-C++ exception-handling constructs. */
6279 case RID_AT_TRY:
6280 case RID_AT_CATCH:
6281 case RID_AT_FINALLY:
6282 case RID_AT_SYNCHRONIZED:
6283 case RID_AT_THROW:
6284 statement = cp_parser_objc_statement (parser);
6285 break;
6287 case RID_TRY:
6288 statement = cp_parser_try_block (parser);
6289 break;
6291 default:
6292 /* It might be a keyword like `int' that can start a
6293 declaration-statement. */
6294 break;
6297 else if (token->type == CPP_NAME)
6299 /* If the next token is a `:', then we are looking at a
6300 labeled-statement. */
6301 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6302 if (token->type == CPP_COLON)
6304 /* Looks like a labeled-statement with an ordinary label.
6305 Parse the label, and then use tail recursion to parse
6306 the statement. */
6307 cp_parser_label_for_labeled_statement (parser);
6308 goto restart;
6311 /* Anything that starts with a `{' must be a compound-statement. */
6312 else if (token->type == CPP_OPEN_BRACE)
6313 statement = cp_parser_compound_statement (parser, NULL, false);
6314 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6315 a statement all its own. */
6316 else if (token->type == CPP_PRAGMA)
6318 /* Only certain OpenMP pragmas are attached to statements, and thus
6319 are considered statements themselves. All others are not. In
6320 the context of a compound, accept the pragma as a "statement" and
6321 return so that we can check for a close brace. Otherwise we
6322 require a real statement and must go back and read one. */
6323 if (in_compound)
6324 cp_parser_pragma (parser, pragma_compound);
6325 else if (!cp_parser_pragma (parser, pragma_stmt))
6326 goto restart;
6327 return;
6329 else if (token->type == CPP_EOF)
6331 cp_parser_error (parser, "expected statement");
6332 return;
6335 /* Everything else must be a declaration-statement or an
6336 expression-statement. Try for the declaration-statement
6337 first, unless we are looking at a `;', in which case we know that
6338 we have an expression-statement. */
6339 if (!statement)
6341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6343 cp_parser_parse_tentatively (parser);
6344 /* Try to parse the declaration-statement. */
6345 cp_parser_declaration_statement (parser);
6346 /* If that worked, we're done. */
6347 if (cp_parser_parse_definitely (parser))
6348 return;
6350 /* Look for an expression-statement instead. */
6351 statement = cp_parser_expression_statement (parser, in_statement_expr);
6354 /* Set the line number for the statement. */
6355 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6356 SET_EXPR_LOCATION (statement, statement_location);
6359 /* Parse the label for a labeled-statement, i.e.
6361 identifier :
6362 case constant-expression :
6363 default :
6365 GNU Extension:
6366 case constant-expression ... constant-expression : statement
6368 When a label is parsed without errors, the label is added to the
6369 parse tree by the finish_* functions, so this function doesn't
6370 have to return the label. */
6372 static void
6373 cp_parser_label_for_labeled_statement (cp_parser* parser)
6375 cp_token *token;
6377 /* The next token should be an identifier. */
6378 token = cp_lexer_peek_token (parser->lexer);
6379 if (token->type != CPP_NAME
6380 && token->type != CPP_KEYWORD)
6382 cp_parser_error (parser, "expected labeled-statement");
6383 return;
6386 switch (token->keyword)
6388 case RID_CASE:
6390 tree expr, expr_hi;
6391 cp_token *ellipsis;
6393 /* Consume the `case' token. */
6394 cp_lexer_consume_token (parser->lexer);
6395 /* Parse the constant-expression. */
6396 expr = cp_parser_constant_expression (parser,
6397 /*allow_non_constant_p=*/false,
6398 NULL);
6400 ellipsis = cp_lexer_peek_token (parser->lexer);
6401 if (ellipsis->type == CPP_ELLIPSIS)
6403 /* Consume the `...' token. */
6404 cp_lexer_consume_token (parser->lexer);
6405 expr_hi =
6406 cp_parser_constant_expression (parser,
6407 /*allow_non_constant_p=*/false,
6408 NULL);
6409 /* We don't need to emit warnings here, as the common code
6410 will do this for us. */
6412 else
6413 expr_hi = NULL_TREE;
6415 if (parser->in_switch_statement_p)
6416 finish_case_label (expr, expr_hi);
6417 else
6418 error ("case label %qE not within a switch statement", expr);
6420 break;
6422 case RID_DEFAULT:
6423 /* Consume the `default' token. */
6424 cp_lexer_consume_token (parser->lexer);
6426 if (parser->in_switch_statement_p)
6427 finish_case_label (NULL_TREE, NULL_TREE);
6428 else
6429 error ("case label not within a switch statement");
6430 break;
6432 default:
6433 /* Anything else must be an ordinary label. */
6434 finish_label_stmt (cp_parser_identifier (parser));
6435 break;
6438 /* Require the `:' token. */
6439 cp_parser_require (parser, CPP_COLON, "`:'");
6442 /* Parse an expression-statement.
6444 expression-statement:
6445 expression [opt] ;
6447 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6448 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6449 indicates whether this expression-statement is part of an
6450 expression statement. */
6452 static tree
6453 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6455 tree statement = NULL_TREE;
6457 /* If the next token is a ';', then there is no expression
6458 statement. */
6459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6460 statement = cp_parser_expression (parser, /*cast_p=*/false);
6462 /* Consume the final `;'. */
6463 cp_parser_consume_semicolon_at_end_of_statement (parser);
6465 if (in_statement_expr
6466 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6467 /* This is the final expression statement of a statement
6468 expression. */
6469 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6470 else if (statement)
6471 statement = finish_expr_stmt (statement);
6472 else
6473 finish_stmt ();
6475 return statement;
6478 /* Parse a compound-statement.
6480 compound-statement:
6481 { statement-seq [opt] }
6483 Returns a tree representing the statement. */
6485 static tree
6486 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6487 bool in_try)
6489 tree compound_stmt;
6491 /* Consume the `{'. */
6492 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6493 return error_mark_node;
6494 /* Begin the compound-statement. */
6495 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6496 /* Parse an (optional) statement-seq. */
6497 cp_parser_statement_seq_opt (parser, in_statement_expr);
6498 /* Finish the compound-statement. */
6499 finish_compound_stmt (compound_stmt);
6500 /* Consume the `}'. */
6501 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6503 return compound_stmt;
6506 /* Parse an (optional) statement-seq.
6508 statement-seq:
6509 statement
6510 statement-seq [opt] statement */
6512 static void
6513 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6515 /* Scan statements until there aren't any more. */
6516 while (true)
6518 cp_token *token = cp_lexer_peek_token (parser->lexer);
6520 /* If we're looking at a `}', then we've run out of statements. */
6521 if (token->type == CPP_CLOSE_BRACE
6522 || token->type == CPP_EOF
6523 || token->type == CPP_PRAGMA_EOL)
6524 break;
6526 /* Parse the statement. */
6527 cp_parser_statement (parser, in_statement_expr, true, NULL);
6531 /* Parse a selection-statement.
6533 selection-statement:
6534 if ( condition ) statement
6535 if ( condition ) statement else statement
6536 switch ( condition ) statement
6538 Returns the new IF_STMT or SWITCH_STMT.
6540 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6541 is a (possibly labeled) if statement which is not enclosed in
6542 braces and has an else clause. This is used to implement
6543 -Wparentheses. */
6545 static tree
6546 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6548 cp_token *token;
6549 enum rid keyword;
6551 if (if_p != NULL)
6552 *if_p = false;
6554 /* Peek at the next token. */
6555 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6557 /* See what kind of keyword it is. */
6558 keyword = token->keyword;
6559 switch (keyword)
6561 case RID_IF:
6562 case RID_SWITCH:
6564 tree statement;
6565 tree condition;
6567 /* Look for the `('. */
6568 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6570 cp_parser_skip_to_end_of_statement (parser);
6571 return error_mark_node;
6574 /* Begin the selection-statement. */
6575 if (keyword == RID_IF)
6576 statement = begin_if_stmt ();
6577 else
6578 statement = begin_switch_stmt ();
6580 /* Parse the condition. */
6581 condition = cp_parser_condition (parser);
6582 /* Look for the `)'. */
6583 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6584 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6585 /*consume_paren=*/true);
6587 if (keyword == RID_IF)
6589 bool nested_if;
6591 /* Add the condition. */
6592 finish_if_stmt_cond (condition, statement);
6594 /* Parse the then-clause. */
6595 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6596 finish_then_clause (statement);
6598 /* If the next token is `else', parse the else-clause. */
6599 if (cp_lexer_next_token_is_keyword (parser->lexer,
6600 RID_ELSE))
6602 /* Consume the `else' keyword. */
6603 cp_lexer_consume_token (parser->lexer);
6604 begin_else_clause (statement);
6605 /* Parse the else-clause. */
6606 cp_parser_implicitly_scoped_statement (parser, NULL);
6607 finish_else_clause (statement);
6609 /* If we are currently parsing a then-clause, then
6610 IF_P will not be NULL. We set it to true to
6611 indicate that this if statement has an else clause.
6612 This may trigger the Wparentheses warning below
6613 when we get back up to the parent if statement. */
6614 if (if_p != NULL)
6615 *if_p = true;
6617 else
6619 /* This if statement does not have an else clause. If
6620 NESTED_IF is true, then the then-clause is an if
6621 statement which does have an else clause. We warn
6622 about the potential ambiguity. */
6623 if (nested_if)
6624 warning (OPT_Wparentheses,
6625 ("%Hsuggest explicit braces "
6626 "to avoid ambiguous %<else%>"),
6627 EXPR_LOCUS (statement));
6630 /* Now we're all done with the if-statement. */
6631 finish_if_stmt (statement);
6633 else
6635 bool in_switch_statement_p;
6636 unsigned char in_statement;
6638 /* Add the condition. */
6639 finish_switch_cond (condition, statement);
6641 /* Parse the body of the switch-statement. */
6642 in_switch_statement_p = parser->in_switch_statement_p;
6643 in_statement = parser->in_statement;
6644 parser->in_switch_statement_p = true;
6645 parser->in_statement |= IN_SWITCH_STMT;
6646 cp_parser_implicitly_scoped_statement (parser, NULL);
6647 parser->in_switch_statement_p = in_switch_statement_p;
6648 parser->in_statement = in_statement;
6650 /* Now we're all done with the switch-statement. */
6651 finish_switch_stmt (statement);
6654 return statement;
6656 break;
6658 default:
6659 cp_parser_error (parser, "expected selection-statement");
6660 return error_mark_node;
6664 /* Parse a condition.
6666 condition:
6667 expression
6668 type-specifier-seq declarator = assignment-expression
6670 GNU Extension:
6672 condition:
6673 type-specifier-seq declarator asm-specification [opt]
6674 attributes [opt] = assignment-expression
6676 Returns the expression that should be tested. */
6678 static tree
6679 cp_parser_condition (cp_parser* parser)
6681 cp_decl_specifier_seq type_specifiers;
6682 const char *saved_message;
6684 /* Try the declaration first. */
6685 cp_parser_parse_tentatively (parser);
6686 /* New types are not allowed in the type-specifier-seq for a
6687 condition. */
6688 saved_message = parser->type_definition_forbidden_message;
6689 parser->type_definition_forbidden_message
6690 = "types may not be defined in conditions";
6691 /* Parse the type-specifier-seq. */
6692 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6693 &type_specifiers);
6694 /* Restore the saved message. */
6695 parser->type_definition_forbidden_message = saved_message;
6696 /* If all is well, we might be looking at a declaration. */
6697 if (!cp_parser_error_occurred (parser))
6699 tree decl;
6700 tree asm_specification;
6701 tree attributes;
6702 cp_declarator *declarator;
6703 tree initializer = NULL_TREE;
6705 /* Parse the declarator. */
6706 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6707 /*ctor_dtor_or_conv_p=*/NULL,
6708 /*parenthesized_p=*/NULL,
6709 /*member_p=*/false);
6710 /* Parse the attributes. */
6711 attributes = cp_parser_attributes_opt (parser);
6712 /* Parse the asm-specification. */
6713 asm_specification = cp_parser_asm_specification_opt (parser);
6714 /* If the next token is not an `=', then we might still be
6715 looking at an expression. For example:
6717 if (A(a).x)
6719 looks like a decl-specifier-seq and a declarator -- but then
6720 there is no `=', so this is an expression. */
6721 cp_parser_require (parser, CPP_EQ, "`='");
6722 /* If we did see an `=', then we are looking at a declaration
6723 for sure. */
6724 if (cp_parser_parse_definitely (parser))
6726 tree pushed_scope;
6727 bool non_constant_p;
6729 /* Create the declaration. */
6730 decl = start_decl (declarator, &type_specifiers,
6731 /*initialized_p=*/true,
6732 attributes, /*prefix_attributes=*/NULL_TREE,
6733 &pushed_scope);
6734 /* Parse the assignment-expression. */
6735 initializer
6736 = cp_parser_constant_expression (parser,
6737 /*allow_non_constant_p=*/true,
6738 &non_constant_p);
6739 if (!non_constant_p)
6740 initializer = fold_non_dependent_expr (initializer);
6742 /* Process the initializer. */
6743 cp_finish_decl (decl,
6744 initializer, !non_constant_p,
6745 asm_specification,
6746 LOOKUP_ONLYCONVERTING);
6748 if (pushed_scope)
6749 pop_scope (pushed_scope);
6751 return convert_from_reference (decl);
6754 /* If we didn't even get past the declarator successfully, we are
6755 definitely not looking at a declaration. */
6756 else
6757 cp_parser_abort_tentative_parse (parser);
6759 /* Otherwise, we are looking at an expression. */
6760 return cp_parser_expression (parser, /*cast_p=*/false);
6763 /* Parse an iteration-statement.
6765 iteration-statement:
6766 while ( condition ) statement
6767 do statement while ( expression ) ;
6768 for ( for-init-statement condition [opt] ; expression [opt] )
6769 statement
6771 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6773 static tree
6774 cp_parser_iteration_statement (cp_parser* parser)
6776 cp_token *token;
6777 enum rid keyword;
6778 tree statement;
6779 unsigned char in_statement;
6781 /* Peek at the next token. */
6782 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6783 if (!token)
6784 return error_mark_node;
6786 /* Remember whether or not we are already within an iteration
6787 statement. */
6788 in_statement = parser->in_statement;
6790 /* See what kind of keyword it is. */
6791 keyword = token->keyword;
6792 switch (keyword)
6794 case RID_WHILE:
6796 tree condition;
6798 /* Begin the while-statement. */
6799 statement = begin_while_stmt ();
6800 /* Look for the `('. */
6801 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6802 /* Parse the condition. */
6803 condition = cp_parser_condition (parser);
6804 finish_while_stmt_cond (condition, statement);
6805 /* Look for the `)'. */
6806 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6807 /* Parse the dependent statement. */
6808 parser->in_statement = IN_ITERATION_STMT;
6809 cp_parser_already_scoped_statement (parser);
6810 parser->in_statement = in_statement;
6811 /* We're done with the while-statement. */
6812 finish_while_stmt (statement);
6814 break;
6816 case RID_DO:
6818 tree expression;
6820 /* Begin the do-statement. */
6821 statement = begin_do_stmt ();
6822 /* Parse the body of the do-statement. */
6823 parser->in_statement = IN_ITERATION_STMT;
6824 cp_parser_implicitly_scoped_statement (parser, NULL);
6825 parser->in_statement = in_statement;
6826 finish_do_body (statement);
6827 /* Look for the `while' keyword. */
6828 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6829 /* Look for the `('. */
6830 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6831 /* Parse the expression. */
6832 expression = cp_parser_expression (parser, /*cast_p=*/false);
6833 /* We're done with the do-statement. */
6834 finish_do_stmt (expression, statement);
6835 /* Look for the `)'. */
6836 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6837 /* Look for the `;'. */
6838 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6840 break;
6842 case RID_FOR:
6844 tree condition = NULL_TREE;
6845 tree expression = NULL_TREE;
6847 /* Begin the for-statement. */
6848 statement = begin_for_stmt ();
6849 /* Look for the `('. */
6850 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6851 /* Parse the initialization. */
6852 cp_parser_for_init_statement (parser);
6853 finish_for_init_stmt (statement);
6855 /* If there's a condition, process it. */
6856 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6857 condition = cp_parser_condition (parser);
6858 finish_for_cond (condition, statement);
6859 /* Look for the `;'. */
6860 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6862 /* If there's an expression, process it. */
6863 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6864 expression = cp_parser_expression (parser, /*cast_p=*/false);
6865 finish_for_expr (expression, statement);
6866 /* Look for the `)'. */
6867 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6869 /* Parse the body of the for-statement. */
6870 parser->in_statement = IN_ITERATION_STMT;
6871 cp_parser_already_scoped_statement (parser);
6872 parser->in_statement = in_statement;
6874 /* We're done with the for-statement. */
6875 finish_for_stmt (statement);
6877 break;
6879 default:
6880 cp_parser_error (parser, "expected iteration-statement");
6881 statement = error_mark_node;
6882 break;
6885 return statement;
6888 /* Parse a for-init-statement.
6890 for-init-statement:
6891 expression-statement
6892 simple-declaration */
6894 static void
6895 cp_parser_for_init_statement (cp_parser* parser)
6897 /* If the next token is a `;', then we have an empty
6898 expression-statement. Grammatically, this is also a
6899 simple-declaration, but an invalid one, because it does not
6900 declare anything. Therefore, if we did not handle this case
6901 specially, we would issue an error message about an invalid
6902 declaration. */
6903 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6905 /* We're going to speculatively look for a declaration, falling back
6906 to an expression, if necessary. */
6907 cp_parser_parse_tentatively (parser);
6908 /* Parse the declaration. */
6909 cp_parser_simple_declaration (parser,
6910 /*function_definition_allowed_p=*/false);
6911 /* If the tentative parse failed, then we shall need to look for an
6912 expression-statement. */
6913 if (cp_parser_parse_definitely (parser))
6914 return;
6917 cp_parser_expression_statement (parser, false);
6920 /* Parse a jump-statement.
6922 jump-statement:
6923 break ;
6924 continue ;
6925 return expression [opt] ;
6926 goto identifier ;
6928 GNU extension:
6930 jump-statement:
6931 goto * expression ;
6933 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6935 static tree
6936 cp_parser_jump_statement (cp_parser* parser)
6938 tree statement = error_mark_node;
6939 cp_token *token;
6940 enum rid keyword;
6942 /* Peek at the next token. */
6943 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6944 if (!token)
6945 return error_mark_node;
6947 /* See what kind of keyword it is. */
6948 keyword = token->keyword;
6949 switch (keyword)
6951 case RID_BREAK:
6952 switch (parser->in_statement)
6954 case 0:
6955 error ("break statement not within loop or switch");
6956 break;
6957 default:
6958 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6959 || parser->in_statement == IN_ITERATION_STMT);
6960 statement = finish_break_stmt ();
6961 break;
6962 case IN_OMP_BLOCK:
6963 error ("invalid exit from OpenMP structured block");
6964 break;
6965 case IN_OMP_FOR:
6966 error ("break statement used with OpenMP for loop");
6967 break;
6969 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6970 break;
6972 case RID_CONTINUE:
6973 switch (parser->in_statement & ~IN_SWITCH_STMT)
6975 case 0:
6976 error ("continue statement not within a loop");
6977 break;
6978 case IN_ITERATION_STMT:
6979 case IN_OMP_FOR:
6980 statement = finish_continue_stmt ();
6981 break;
6982 case IN_OMP_BLOCK:
6983 error ("invalid exit from OpenMP structured block");
6984 break;
6985 default:
6986 gcc_unreachable ();
6988 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6989 break;
6991 case RID_RETURN:
6993 tree expr;
6995 /* If the next token is a `;', then there is no
6996 expression. */
6997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6998 expr = cp_parser_expression (parser, /*cast_p=*/false);
6999 else
7000 expr = NULL_TREE;
7001 /* Build the return-statement. */
7002 statement = finish_return_stmt (expr);
7003 /* Look for the final `;'. */
7004 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7006 break;
7008 case RID_GOTO:
7009 /* Create the goto-statement. */
7010 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7012 /* Issue a warning about this use of a GNU extension. */
7013 if (pedantic)
7014 pedwarn ("ISO C++ forbids computed gotos");
7015 /* Consume the '*' token. */
7016 cp_lexer_consume_token (parser->lexer);
7017 /* Parse the dependent expression. */
7018 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7020 else
7021 finish_goto_stmt (cp_parser_identifier (parser));
7022 /* Look for the final `;'. */
7023 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7024 break;
7026 default:
7027 cp_parser_error (parser, "expected jump-statement");
7028 break;
7031 return statement;
7034 /* Parse a declaration-statement.
7036 declaration-statement:
7037 block-declaration */
7039 static void
7040 cp_parser_declaration_statement (cp_parser* parser)
7042 void *p;
7044 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7045 p = obstack_alloc (&declarator_obstack, 0);
7047 /* Parse the block-declaration. */
7048 cp_parser_block_declaration (parser, /*statement_p=*/true);
7050 /* Free any declarators allocated. */
7051 obstack_free (&declarator_obstack, p);
7053 /* Finish off the statement. */
7054 finish_stmt ();
7057 /* Some dependent statements (like `if (cond) statement'), are
7058 implicitly in their own scope. In other words, if the statement is
7059 a single statement (as opposed to a compound-statement), it is
7060 none-the-less treated as if it were enclosed in braces. Any
7061 declarations appearing in the dependent statement are out of scope
7062 after control passes that point. This function parses a statement,
7063 but ensures that is in its own scope, even if it is not a
7064 compound-statement.
7066 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7067 is a (possibly labeled) if statement which is not enclosed in
7068 braces and has an else clause. This is used to implement
7069 -Wparentheses.
7071 Returns the new statement. */
7073 static tree
7074 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7076 tree statement;
7078 if (if_p != NULL)
7079 *if_p = false;
7081 /* Mark if () ; with a special NOP_EXPR. */
7082 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7084 cp_lexer_consume_token (parser->lexer);
7085 statement = add_stmt (build_empty_stmt ());
7087 /* if a compound is opened, we simply parse the statement directly. */
7088 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7089 statement = cp_parser_compound_statement (parser, NULL, false);
7090 /* If the token is not a `{', then we must take special action. */
7091 else
7093 /* Create a compound-statement. */
7094 statement = begin_compound_stmt (0);
7095 /* Parse the dependent-statement. */
7096 cp_parser_statement (parser, NULL_TREE, false, if_p);
7097 /* Finish the dummy compound-statement. */
7098 finish_compound_stmt (statement);
7101 /* Return the statement. */
7102 return statement;
7105 /* For some dependent statements (like `while (cond) statement'), we
7106 have already created a scope. Therefore, even if the dependent
7107 statement is a compound-statement, we do not want to create another
7108 scope. */
7110 static void
7111 cp_parser_already_scoped_statement (cp_parser* parser)
7113 /* If the token is a `{', then we must take special action. */
7114 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7115 cp_parser_statement (parser, NULL_TREE, false, NULL);
7116 else
7118 /* Avoid calling cp_parser_compound_statement, so that we
7119 don't create a new scope. Do everything else by hand. */
7120 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7121 cp_parser_statement_seq_opt (parser, NULL_TREE);
7122 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7126 /* Declarations [gram.dcl.dcl] */
7128 /* Parse an optional declaration-sequence.
7130 declaration-seq:
7131 declaration
7132 declaration-seq declaration */
7134 static void
7135 cp_parser_declaration_seq_opt (cp_parser* parser)
7137 while (true)
7139 cp_token *token;
7141 token = cp_lexer_peek_token (parser->lexer);
7143 if (token->type == CPP_CLOSE_BRACE
7144 || token->type == CPP_EOF
7145 || token->type == CPP_PRAGMA_EOL)
7146 break;
7148 if (token->type == CPP_SEMICOLON)
7150 /* A declaration consisting of a single semicolon is
7151 invalid. Allow it unless we're being pedantic. */
7152 cp_lexer_consume_token (parser->lexer);
7153 if (pedantic && !in_system_header)
7154 pedwarn ("extra %<;%>");
7155 continue;
7158 /* If we're entering or exiting a region that's implicitly
7159 extern "C", modify the lang context appropriately. */
7160 if (!parser->implicit_extern_c && token->implicit_extern_c)
7162 push_lang_context (lang_name_c);
7163 parser->implicit_extern_c = true;
7165 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7167 pop_lang_context ();
7168 parser->implicit_extern_c = false;
7171 if (token->type == CPP_PRAGMA)
7173 /* A top-level declaration can consist solely of a #pragma.
7174 A nested declaration cannot, so this is done here and not
7175 in cp_parser_declaration. (A #pragma at block scope is
7176 handled in cp_parser_statement.) */
7177 cp_parser_pragma (parser, pragma_external);
7178 continue;
7181 /* Parse the declaration itself. */
7182 cp_parser_declaration (parser);
7186 /* Parse a declaration.
7188 declaration:
7189 block-declaration
7190 function-definition
7191 template-declaration
7192 explicit-instantiation
7193 explicit-specialization
7194 linkage-specification
7195 namespace-definition
7197 GNU extension:
7199 declaration:
7200 __extension__ declaration */
7202 static void
7203 cp_parser_declaration (cp_parser* parser)
7205 cp_token token1;
7206 cp_token token2;
7207 int saved_pedantic;
7208 void *p;
7210 /* Check for the `__extension__' keyword. */
7211 if (cp_parser_extension_opt (parser, &saved_pedantic))
7213 /* Parse the qualified declaration. */
7214 cp_parser_declaration (parser);
7215 /* Restore the PEDANTIC flag. */
7216 pedantic = saved_pedantic;
7218 return;
7221 /* Try to figure out what kind of declaration is present. */
7222 token1 = *cp_lexer_peek_token (parser->lexer);
7224 if (token1.type != CPP_EOF)
7225 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7226 else
7228 token2.type = CPP_EOF;
7229 token2.keyword = RID_MAX;
7232 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7233 p = obstack_alloc (&declarator_obstack, 0);
7235 /* If the next token is `extern' and the following token is a string
7236 literal, then we have a linkage specification. */
7237 if (token1.keyword == RID_EXTERN
7238 && cp_parser_is_string_literal (&token2))
7239 cp_parser_linkage_specification (parser);
7240 /* If the next token is `template', then we have either a template
7241 declaration, an explicit instantiation, or an explicit
7242 specialization. */
7243 else if (token1.keyword == RID_TEMPLATE)
7245 /* `template <>' indicates a template specialization. */
7246 if (token2.type == CPP_LESS
7247 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7248 cp_parser_explicit_specialization (parser);
7249 /* `template <' indicates a template declaration. */
7250 else if (token2.type == CPP_LESS)
7251 cp_parser_template_declaration (parser, /*member_p=*/false);
7252 /* Anything else must be an explicit instantiation. */
7253 else
7254 cp_parser_explicit_instantiation (parser);
7256 /* If the next token is `export', then we have a template
7257 declaration. */
7258 else if (token1.keyword == RID_EXPORT)
7259 cp_parser_template_declaration (parser, /*member_p=*/false);
7260 /* If the next token is `extern', 'static' or 'inline' and the one
7261 after that is `template', we have a GNU extended explicit
7262 instantiation directive. */
7263 else if (cp_parser_allow_gnu_extensions_p (parser)
7264 && (token1.keyword == RID_EXTERN
7265 || token1.keyword == RID_STATIC
7266 || token1.keyword == RID_INLINE)
7267 && token2.keyword == RID_TEMPLATE)
7268 cp_parser_explicit_instantiation (parser);
7269 /* If the next token is `namespace', check for a named or unnamed
7270 namespace definition. */
7271 else if (token1.keyword == RID_NAMESPACE
7272 && (/* A named namespace definition. */
7273 (token2.type == CPP_NAME
7274 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7275 != CPP_EQ))
7276 /* An unnamed namespace definition. */
7277 || token2.type == CPP_OPEN_BRACE
7278 || token2.keyword == RID_ATTRIBUTE))
7279 cp_parser_namespace_definition (parser);
7280 /* Objective-C++ declaration/definition. */
7281 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7282 cp_parser_objc_declaration (parser);
7283 /* We must have either a block declaration or a function
7284 definition. */
7285 else
7286 /* Try to parse a block-declaration, or a function-definition. */
7287 cp_parser_block_declaration (parser, /*statement_p=*/false);
7289 /* Free any declarators allocated. */
7290 obstack_free (&declarator_obstack, p);
7293 /* Parse a block-declaration.
7295 block-declaration:
7296 simple-declaration
7297 asm-definition
7298 namespace-alias-definition
7299 using-declaration
7300 using-directive
7302 GNU Extension:
7304 block-declaration:
7305 __extension__ block-declaration
7306 label-declaration
7308 C++0x Extension:
7310 block-declaration:
7311 static_assert-declaration
7313 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7314 part of a declaration-statement. */
7316 static void
7317 cp_parser_block_declaration (cp_parser *parser,
7318 bool statement_p)
7320 cp_token *token1;
7321 int saved_pedantic;
7323 /* Check for the `__extension__' keyword. */
7324 if (cp_parser_extension_opt (parser, &saved_pedantic))
7326 /* Parse the qualified declaration. */
7327 cp_parser_block_declaration (parser, statement_p);
7328 /* Restore the PEDANTIC flag. */
7329 pedantic = saved_pedantic;
7331 return;
7334 /* Peek at the next token to figure out which kind of declaration is
7335 present. */
7336 token1 = cp_lexer_peek_token (parser->lexer);
7338 /* If the next keyword is `asm', we have an asm-definition. */
7339 if (token1->keyword == RID_ASM)
7341 if (statement_p)
7342 cp_parser_commit_to_tentative_parse (parser);
7343 cp_parser_asm_definition (parser);
7345 /* If the next keyword is `namespace', we have a
7346 namespace-alias-definition. */
7347 else if (token1->keyword == RID_NAMESPACE)
7348 cp_parser_namespace_alias_definition (parser);
7349 /* If the next keyword is `using', we have either a
7350 using-declaration or a using-directive. */
7351 else if (token1->keyword == RID_USING)
7353 cp_token *token2;
7355 if (statement_p)
7356 cp_parser_commit_to_tentative_parse (parser);
7357 /* If the token after `using' is `namespace', then we have a
7358 using-directive. */
7359 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7360 if (token2->keyword == RID_NAMESPACE)
7361 cp_parser_using_directive (parser);
7362 /* Otherwise, it's a using-declaration. */
7363 else
7364 cp_parser_using_declaration (parser,
7365 /*access_declaration_p=*/false);
7367 /* If the next keyword is `__label__' we have a label declaration. */
7368 else if (token1->keyword == RID_LABEL)
7370 if (statement_p)
7371 cp_parser_commit_to_tentative_parse (parser);
7372 cp_parser_label_declaration (parser);
7374 /* If the next token is `static_assert' we have a static assertion. */
7375 else if (token1->keyword == RID_STATIC_ASSERT)
7376 cp_parser_static_assert (parser, /*member_p=*/false);
7377 /* Anything else must be a simple-declaration. */
7378 else
7379 cp_parser_simple_declaration (parser, !statement_p);
7382 /* Parse a simple-declaration.
7384 simple-declaration:
7385 decl-specifier-seq [opt] init-declarator-list [opt] ;
7387 init-declarator-list:
7388 init-declarator
7389 init-declarator-list , init-declarator
7391 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7392 function-definition as a simple-declaration. */
7394 static void
7395 cp_parser_simple_declaration (cp_parser* parser,
7396 bool function_definition_allowed_p)
7398 cp_decl_specifier_seq decl_specifiers;
7399 int declares_class_or_enum;
7400 bool saw_declarator;
7402 /* Defer access checks until we know what is being declared; the
7403 checks for names appearing in the decl-specifier-seq should be
7404 done as if we were in the scope of the thing being declared. */
7405 push_deferring_access_checks (dk_deferred);
7407 /* Parse the decl-specifier-seq. We have to keep track of whether
7408 or not the decl-specifier-seq declares a named class or
7409 enumeration type, since that is the only case in which the
7410 init-declarator-list is allowed to be empty.
7412 [dcl.dcl]
7414 In a simple-declaration, the optional init-declarator-list can be
7415 omitted only when declaring a class or enumeration, that is when
7416 the decl-specifier-seq contains either a class-specifier, an
7417 elaborated-type-specifier, or an enum-specifier. */
7418 cp_parser_decl_specifier_seq (parser,
7419 CP_PARSER_FLAGS_OPTIONAL,
7420 &decl_specifiers,
7421 &declares_class_or_enum);
7422 /* We no longer need to defer access checks. */
7423 stop_deferring_access_checks ();
7425 /* In a block scope, a valid declaration must always have a
7426 decl-specifier-seq. By not trying to parse declarators, we can
7427 resolve the declaration/expression ambiguity more quickly. */
7428 if (!function_definition_allowed_p
7429 && !decl_specifiers.any_specifiers_p)
7431 cp_parser_error (parser, "expected declaration");
7432 goto done;
7435 /* If the next two tokens are both identifiers, the code is
7436 erroneous. The usual cause of this situation is code like:
7438 T t;
7440 where "T" should name a type -- but does not. */
7441 if (!decl_specifiers.type
7442 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7444 /* If parsing tentatively, we should commit; we really are
7445 looking at a declaration. */
7446 cp_parser_commit_to_tentative_parse (parser);
7447 /* Give up. */
7448 goto done;
7451 /* If we have seen at least one decl-specifier, and the next token
7452 is not a parenthesis, then we must be looking at a declaration.
7453 (After "int (" we might be looking at a functional cast.) */
7454 if (decl_specifiers.any_specifiers_p
7455 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7456 cp_parser_commit_to_tentative_parse (parser);
7458 /* Keep going until we hit the `;' at the end of the simple
7459 declaration. */
7460 saw_declarator = false;
7461 while (cp_lexer_next_token_is_not (parser->lexer,
7462 CPP_SEMICOLON))
7464 cp_token *token;
7465 bool function_definition_p;
7466 tree decl;
7468 if (saw_declarator)
7470 /* If we are processing next declarator, coma is expected */
7471 token = cp_lexer_peek_token (parser->lexer);
7472 gcc_assert (token->type == CPP_COMMA);
7473 cp_lexer_consume_token (parser->lexer);
7475 else
7476 saw_declarator = true;
7478 /* Parse the init-declarator. */
7479 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7480 /*checks=*/NULL,
7481 function_definition_allowed_p,
7482 /*member_p=*/false,
7483 declares_class_or_enum,
7484 &function_definition_p);
7485 /* If an error occurred while parsing tentatively, exit quickly.
7486 (That usually happens when in the body of a function; each
7487 statement is treated as a declaration-statement until proven
7488 otherwise.) */
7489 if (cp_parser_error_occurred (parser))
7490 goto done;
7491 /* Handle function definitions specially. */
7492 if (function_definition_p)
7494 /* If the next token is a `,', then we are probably
7495 processing something like:
7497 void f() {}, *p;
7499 which is erroneous. */
7500 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7501 error ("mixing declarations and function-definitions is forbidden");
7502 /* Otherwise, we're done with the list of declarators. */
7503 else
7505 pop_deferring_access_checks ();
7506 return;
7509 /* The next token should be either a `,' or a `;'. */
7510 token = cp_lexer_peek_token (parser->lexer);
7511 /* If it's a `,', there are more declarators to come. */
7512 if (token->type == CPP_COMMA)
7513 /* will be consumed next time around */;
7514 /* If it's a `;', we are done. */
7515 else if (token->type == CPP_SEMICOLON)
7516 break;
7517 /* Anything else is an error. */
7518 else
7520 /* If we have already issued an error message we don't need
7521 to issue another one. */
7522 if (decl != error_mark_node
7523 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7524 cp_parser_error (parser, "expected %<,%> or %<;%>");
7525 /* Skip tokens until we reach the end of the statement. */
7526 cp_parser_skip_to_end_of_statement (parser);
7527 /* If the next token is now a `;', consume it. */
7528 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7529 cp_lexer_consume_token (parser->lexer);
7530 goto done;
7532 /* After the first time around, a function-definition is not
7533 allowed -- even if it was OK at first. For example:
7535 int i, f() {}
7537 is not valid. */
7538 function_definition_allowed_p = false;
7541 /* Issue an error message if no declarators are present, and the
7542 decl-specifier-seq does not itself declare a class or
7543 enumeration. */
7544 if (!saw_declarator)
7546 if (cp_parser_declares_only_class_p (parser))
7547 shadow_tag (&decl_specifiers);
7548 /* Perform any deferred access checks. */
7549 perform_deferred_access_checks ();
7552 /* Consume the `;'. */
7553 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7555 done:
7556 pop_deferring_access_checks ();
7559 /* Parse a decl-specifier-seq.
7561 decl-specifier-seq:
7562 decl-specifier-seq [opt] decl-specifier
7564 decl-specifier:
7565 storage-class-specifier
7566 type-specifier
7567 function-specifier
7568 friend
7569 typedef
7571 GNU Extension:
7573 decl-specifier:
7574 attributes
7576 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7578 The parser flags FLAGS is used to control type-specifier parsing.
7580 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7581 flags:
7583 1: one of the decl-specifiers is an elaborated-type-specifier
7584 (i.e., a type declaration)
7585 2: one of the decl-specifiers is an enum-specifier or a
7586 class-specifier (i.e., a type definition)
7590 static void
7591 cp_parser_decl_specifier_seq (cp_parser* parser,
7592 cp_parser_flags flags,
7593 cp_decl_specifier_seq *decl_specs,
7594 int* declares_class_or_enum)
7596 bool constructor_possible_p = !parser->in_declarator_p;
7598 /* Clear DECL_SPECS. */
7599 clear_decl_specs (decl_specs);
7601 /* Assume no class or enumeration type is declared. */
7602 *declares_class_or_enum = 0;
7604 /* Keep reading specifiers until there are no more to read. */
7605 while (true)
7607 bool constructor_p;
7608 bool found_decl_spec;
7609 cp_token *token;
7611 /* Peek at the next token. */
7612 token = cp_lexer_peek_token (parser->lexer);
7613 /* Handle attributes. */
7614 if (token->keyword == RID_ATTRIBUTE)
7616 /* Parse the attributes. */
7617 decl_specs->attributes
7618 = chainon (decl_specs->attributes,
7619 cp_parser_attributes_opt (parser));
7620 continue;
7622 /* Assume we will find a decl-specifier keyword. */
7623 found_decl_spec = true;
7624 /* If the next token is an appropriate keyword, we can simply
7625 add it to the list. */
7626 switch (token->keyword)
7628 /* decl-specifier:
7629 friend */
7630 case RID_FRIEND:
7631 if (!at_class_scope_p ())
7633 error ("%<friend%> used outside of class");
7634 cp_lexer_purge_token (parser->lexer);
7636 else
7638 ++decl_specs->specs[(int) ds_friend];
7639 /* Consume the token. */
7640 cp_lexer_consume_token (parser->lexer);
7642 break;
7644 /* function-specifier:
7645 inline
7646 virtual
7647 explicit */
7648 case RID_INLINE:
7649 case RID_VIRTUAL:
7650 case RID_EXPLICIT:
7651 cp_parser_function_specifier_opt (parser, decl_specs);
7652 break;
7654 /* decl-specifier:
7655 typedef */
7656 case RID_TYPEDEF:
7657 ++decl_specs->specs[(int) ds_typedef];
7658 /* Consume the token. */
7659 cp_lexer_consume_token (parser->lexer);
7660 /* A constructor declarator cannot appear in a typedef. */
7661 constructor_possible_p = false;
7662 /* The "typedef" keyword can only occur in a declaration; we
7663 may as well commit at this point. */
7664 cp_parser_commit_to_tentative_parse (parser);
7666 if (decl_specs->storage_class != sc_none)
7667 decl_specs->conflicting_specifiers_p = true;
7668 break;
7670 /* storage-class-specifier:
7671 auto
7672 register
7673 static
7674 extern
7675 mutable
7677 GNU Extension:
7678 thread */
7679 case RID_AUTO:
7680 case RID_REGISTER:
7681 case RID_STATIC:
7682 case RID_EXTERN:
7683 case RID_MUTABLE:
7684 /* Consume the token. */
7685 cp_lexer_consume_token (parser->lexer);
7686 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7687 break;
7688 case RID_THREAD:
7689 /* Consume the token. */
7690 cp_lexer_consume_token (parser->lexer);
7691 ++decl_specs->specs[(int) ds_thread];
7692 break;
7694 default:
7695 /* We did not yet find a decl-specifier yet. */
7696 found_decl_spec = false;
7697 break;
7700 /* Constructors are a special case. The `S' in `S()' is not a
7701 decl-specifier; it is the beginning of the declarator. */
7702 constructor_p
7703 = (!found_decl_spec
7704 && constructor_possible_p
7705 && (cp_parser_constructor_declarator_p
7706 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7708 /* If we don't have a DECL_SPEC yet, then we must be looking at
7709 a type-specifier. */
7710 if (!found_decl_spec && !constructor_p)
7712 int decl_spec_declares_class_or_enum;
7713 bool is_cv_qualifier;
7714 tree type_spec;
7716 type_spec
7717 = cp_parser_type_specifier (parser, flags,
7718 decl_specs,
7719 /*is_declaration=*/true,
7720 &decl_spec_declares_class_or_enum,
7721 &is_cv_qualifier);
7723 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7725 /* If this type-specifier referenced a user-defined type
7726 (a typedef, class-name, etc.), then we can't allow any
7727 more such type-specifiers henceforth.
7729 [dcl.spec]
7731 The longest sequence of decl-specifiers that could
7732 possibly be a type name is taken as the
7733 decl-specifier-seq of a declaration. The sequence shall
7734 be self-consistent as described below.
7736 [dcl.type]
7738 As a general rule, at most one type-specifier is allowed
7739 in the complete decl-specifier-seq of a declaration. The
7740 only exceptions are the following:
7742 -- const or volatile can be combined with any other
7743 type-specifier.
7745 -- signed or unsigned can be combined with char, long,
7746 short, or int.
7748 -- ..
7750 Example:
7752 typedef char* Pc;
7753 void g (const int Pc);
7755 Here, Pc is *not* part of the decl-specifier seq; it's
7756 the declarator. Therefore, once we see a type-specifier
7757 (other than a cv-qualifier), we forbid any additional
7758 user-defined types. We *do* still allow things like `int
7759 int' to be considered a decl-specifier-seq, and issue the
7760 error message later. */
7761 if (type_spec && !is_cv_qualifier)
7762 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7763 /* A constructor declarator cannot follow a type-specifier. */
7764 if (type_spec)
7766 constructor_possible_p = false;
7767 found_decl_spec = true;
7771 /* If we still do not have a DECL_SPEC, then there are no more
7772 decl-specifiers. */
7773 if (!found_decl_spec)
7774 break;
7776 decl_specs->any_specifiers_p = true;
7777 /* After we see one decl-specifier, further decl-specifiers are
7778 always optional. */
7779 flags |= CP_PARSER_FLAGS_OPTIONAL;
7782 cp_parser_check_decl_spec (decl_specs);
7784 /* Don't allow a friend specifier with a class definition. */
7785 if (decl_specs->specs[(int) ds_friend] != 0
7786 && (*declares_class_or_enum & 2))
7787 error ("class definition may not be declared a friend");
7790 /* Parse an (optional) storage-class-specifier.
7792 storage-class-specifier:
7793 auto
7794 register
7795 static
7796 extern
7797 mutable
7799 GNU Extension:
7801 storage-class-specifier:
7802 thread
7804 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7806 static tree
7807 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7809 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7811 case RID_AUTO:
7812 case RID_REGISTER:
7813 case RID_STATIC:
7814 case RID_EXTERN:
7815 case RID_MUTABLE:
7816 case RID_THREAD:
7817 /* Consume the token. */
7818 return cp_lexer_consume_token (parser->lexer)->u.value;
7820 default:
7821 return NULL_TREE;
7825 /* Parse an (optional) function-specifier.
7827 function-specifier:
7828 inline
7829 virtual
7830 explicit
7832 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7833 Updates DECL_SPECS, if it is non-NULL. */
7835 static tree
7836 cp_parser_function_specifier_opt (cp_parser* parser,
7837 cp_decl_specifier_seq *decl_specs)
7839 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7841 case RID_INLINE:
7842 if (decl_specs)
7843 ++decl_specs->specs[(int) ds_inline];
7844 break;
7846 case RID_VIRTUAL:
7847 /* 14.5.2.3 [temp.mem]
7849 A member function template shall not be virtual. */
7850 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7851 error ("templates may not be %<virtual%>");
7852 else if (decl_specs)
7853 ++decl_specs->specs[(int) ds_virtual];
7854 break;
7856 case RID_EXPLICIT:
7857 if (decl_specs)
7858 ++decl_specs->specs[(int) ds_explicit];
7859 break;
7861 default:
7862 return NULL_TREE;
7865 /* Consume the token. */
7866 return cp_lexer_consume_token (parser->lexer)->u.value;
7869 /* Parse a linkage-specification.
7871 linkage-specification:
7872 extern string-literal { declaration-seq [opt] }
7873 extern string-literal declaration */
7875 static void
7876 cp_parser_linkage_specification (cp_parser* parser)
7878 tree linkage;
7880 /* Look for the `extern' keyword. */
7881 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7883 /* Look for the string-literal. */
7884 linkage = cp_parser_string_literal (parser, false, false);
7886 /* Transform the literal into an identifier. If the literal is a
7887 wide-character string, or contains embedded NULs, then we can't
7888 handle it as the user wants. */
7889 if (strlen (TREE_STRING_POINTER (linkage))
7890 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7892 cp_parser_error (parser, "invalid linkage-specification");
7893 /* Assume C++ linkage. */
7894 linkage = lang_name_cplusplus;
7896 else
7897 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7899 /* We're now using the new linkage. */
7900 push_lang_context (linkage);
7902 /* If the next token is a `{', then we're using the first
7903 production. */
7904 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7906 /* Consume the `{' token. */
7907 cp_lexer_consume_token (parser->lexer);
7908 /* Parse the declarations. */
7909 cp_parser_declaration_seq_opt (parser);
7910 /* Look for the closing `}'. */
7911 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7913 /* Otherwise, there's just one declaration. */
7914 else
7916 bool saved_in_unbraced_linkage_specification_p;
7918 saved_in_unbraced_linkage_specification_p
7919 = parser->in_unbraced_linkage_specification_p;
7920 parser->in_unbraced_linkage_specification_p = true;
7921 cp_parser_declaration (parser);
7922 parser->in_unbraced_linkage_specification_p
7923 = saved_in_unbraced_linkage_specification_p;
7926 /* We're done with the linkage-specification. */
7927 pop_lang_context ();
7930 /* Parse a static_assert-declaration.
7932 static_assert-declaration:
7933 static_assert ( constant-expression , string-literal ) ;
7935 If MEMBER_P, this static_assert is a class member. */
7937 static void
7938 cp_parser_static_assert(cp_parser *parser, bool member_p)
7940 tree condition;
7941 tree message;
7942 cp_token *token;
7943 location_t saved_loc;
7945 /* Peek at the `static_assert' token so we can keep track of exactly
7946 where the static assertion started. */
7947 token = cp_lexer_peek_token (parser->lexer);
7948 saved_loc = token->location;
7950 /* Look for the `static_assert' keyword. */
7951 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
7952 "`static_assert'"))
7953 return;
7955 /* We know we are in a static assertion; commit to any tentative
7956 parse. */
7957 if (cp_parser_parsing_tentatively (parser))
7958 cp_parser_commit_to_tentative_parse (parser);
7960 /* Parse the `(' starting the static assertion condition. */
7961 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7963 /* Parse the constant-expression. */
7964 condition =
7965 cp_parser_constant_expression (parser,
7966 /*allow_non_constant_p=*/false,
7967 /*non_constant_p=*/NULL);
7969 /* Parse the separating `,'. */
7970 cp_parser_require (parser, CPP_COMMA, "`,'");
7972 /* Parse the string-literal message. */
7973 message = cp_parser_string_literal (parser,
7974 /*translate=*/false,
7975 /*wide_ok=*/true);
7977 /* A `)' completes the static assertion. */
7978 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7979 cp_parser_skip_to_closing_parenthesis (parser,
7980 /*recovering=*/true,
7981 /*or_comma=*/false,
7982 /*consume_paren=*/true);
7984 /* A semicolon terminates the declaration. */
7985 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7987 /* Complete the static assertion, which may mean either processing
7988 the static assert now or saving it for template instantiation. */
7989 finish_static_assert (condition, message, saved_loc, member_p);
7992 /* Special member functions [gram.special] */
7994 /* Parse a conversion-function-id.
7996 conversion-function-id:
7997 operator conversion-type-id
7999 Returns an IDENTIFIER_NODE representing the operator. */
8001 static tree
8002 cp_parser_conversion_function_id (cp_parser* parser)
8004 tree type;
8005 tree saved_scope;
8006 tree saved_qualifying_scope;
8007 tree saved_object_scope;
8008 tree pushed_scope = NULL_TREE;
8010 /* Look for the `operator' token. */
8011 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8012 return error_mark_node;
8013 /* When we parse the conversion-type-id, the current scope will be
8014 reset. However, we need that information in able to look up the
8015 conversion function later, so we save it here. */
8016 saved_scope = parser->scope;
8017 saved_qualifying_scope = parser->qualifying_scope;
8018 saved_object_scope = parser->object_scope;
8019 /* We must enter the scope of the class so that the names of
8020 entities declared within the class are available in the
8021 conversion-type-id. For example, consider:
8023 struct S {
8024 typedef int I;
8025 operator I();
8028 S::operator I() { ... }
8030 In order to see that `I' is a type-name in the definition, we
8031 must be in the scope of `S'. */
8032 if (saved_scope)
8033 pushed_scope = push_scope (saved_scope);
8034 /* Parse the conversion-type-id. */
8035 type = cp_parser_conversion_type_id (parser);
8036 /* Leave the scope of the class, if any. */
8037 if (pushed_scope)
8038 pop_scope (pushed_scope);
8039 /* Restore the saved scope. */
8040 parser->scope = saved_scope;
8041 parser->qualifying_scope = saved_qualifying_scope;
8042 parser->object_scope = saved_object_scope;
8043 /* If the TYPE is invalid, indicate failure. */
8044 if (type == error_mark_node)
8045 return error_mark_node;
8046 return mangle_conv_op_name_for_type (type);
8049 /* Parse a conversion-type-id:
8051 conversion-type-id:
8052 type-specifier-seq conversion-declarator [opt]
8054 Returns the TYPE specified. */
8056 static tree
8057 cp_parser_conversion_type_id (cp_parser* parser)
8059 tree attributes;
8060 cp_decl_specifier_seq type_specifiers;
8061 cp_declarator *declarator;
8062 tree type_specified;
8064 /* Parse the attributes. */
8065 attributes = cp_parser_attributes_opt (parser);
8066 /* Parse the type-specifiers. */
8067 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8068 &type_specifiers);
8069 /* If that didn't work, stop. */
8070 if (type_specifiers.type == error_mark_node)
8071 return error_mark_node;
8072 /* Parse the conversion-declarator. */
8073 declarator = cp_parser_conversion_declarator_opt (parser);
8075 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8076 /*initialized=*/0, &attributes);
8077 if (attributes)
8078 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8079 return type_specified;
8082 /* Parse an (optional) conversion-declarator.
8084 conversion-declarator:
8085 ptr-operator conversion-declarator [opt]
8089 static cp_declarator *
8090 cp_parser_conversion_declarator_opt (cp_parser* parser)
8092 enum tree_code code;
8093 tree class_type;
8094 cp_cv_quals cv_quals;
8096 /* We don't know if there's a ptr-operator next, or not. */
8097 cp_parser_parse_tentatively (parser);
8098 /* Try the ptr-operator. */
8099 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8100 /* If it worked, look for more conversion-declarators. */
8101 if (cp_parser_parse_definitely (parser))
8103 cp_declarator *declarator;
8105 /* Parse another optional declarator. */
8106 declarator = cp_parser_conversion_declarator_opt (parser);
8108 /* Create the representation of the declarator. */
8109 if (class_type)
8110 declarator = make_ptrmem_declarator (cv_quals, class_type,
8111 declarator);
8112 else if (code == INDIRECT_REF)
8113 declarator = make_pointer_declarator (cv_quals, declarator);
8114 else
8115 declarator = make_reference_declarator (cv_quals, declarator);
8117 return declarator;
8120 return NULL;
8123 /* Parse an (optional) ctor-initializer.
8125 ctor-initializer:
8126 : mem-initializer-list
8128 Returns TRUE iff the ctor-initializer was actually present. */
8130 static bool
8131 cp_parser_ctor_initializer_opt (cp_parser* parser)
8133 /* If the next token is not a `:', then there is no
8134 ctor-initializer. */
8135 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8137 /* Do default initialization of any bases and members. */
8138 if (DECL_CONSTRUCTOR_P (current_function_decl))
8139 finish_mem_initializers (NULL_TREE);
8141 return false;
8144 /* Consume the `:' token. */
8145 cp_lexer_consume_token (parser->lexer);
8146 /* And the mem-initializer-list. */
8147 cp_parser_mem_initializer_list (parser);
8149 return true;
8152 /* Parse a mem-initializer-list.
8154 mem-initializer-list:
8155 mem-initializer
8156 mem-initializer , mem-initializer-list */
8158 static void
8159 cp_parser_mem_initializer_list (cp_parser* parser)
8161 tree mem_initializer_list = NULL_TREE;
8163 /* Let the semantic analysis code know that we are starting the
8164 mem-initializer-list. */
8165 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8166 error ("only constructors take base initializers");
8168 /* Loop through the list. */
8169 while (true)
8171 tree mem_initializer;
8173 /* Parse the mem-initializer. */
8174 mem_initializer = cp_parser_mem_initializer (parser);
8175 /* Add it to the list, unless it was erroneous. */
8176 if (mem_initializer != error_mark_node)
8178 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8179 mem_initializer_list = mem_initializer;
8181 /* If the next token is not a `,', we're done. */
8182 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8183 break;
8184 /* Consume the `,' token. */
8185 cp_lexer_consume_token (parser->lexer);
8188 /* Perform semantic analysis. */
8189 if (DECL_CONSTRUCTOR_P (current_function_decl))
8190 finish_mem_initializers (mem_initializer_list);
8193 /* Parse a mem-initializer.
8195 mem-initializer:
8196 mem-initializer-id ( expression-list [opt] )
8198 GNU extension:
8200 mem-initializer:
8201 ( expression-list [opt] )
8203 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8204 class) or FIELD_DECL (for a non-static data member) to initialize;
8205 the TREE_VALUE is the expression-list. An empty initialization
8206 list is represented by void_list_node. */
8208 static tree
8209 cp_parser_mem_initializer (cp_parser* parser)
8211 tree mem_initializer_id;
8212 tree expression_list;
8213 tree member;
8215 /* Find out what is being initialized. */
8216 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8218 pedwarn ("anachronistic old-style base class initializer");
8219 mem_initializer_id = NULL_TREE;
8221 else
8222 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8223 member = expand_member_init (mem_initializer_id);
8224 if (member && !DECL_P (member))
8225 in_base_initializer = 1;
8227 expression_list
8228 = cp_parser_parenthesized_expression_list (parser, false,
8229 /*cast_p=*/false,
8230 /*non_constant_p=*/NULL);
8231 if (expression_list == error_mark_node)
8232 return error_mark_node;
8233 if (!expression_list)
8234 expression_list = void_type_node;
8236 in_base_initializer = 0;
8238 return member ? build_tree_list (member, expression_list) : error_mark_node;
8241 /* Parse a mem-initializer-id.
8243 mem-initializer-id:
8244 :: [opt] nested-name-specifier [opt] class-name
8245 identifier
8247 Returns a TYPE indicating the class to be initializer for the first
8248 production. Returns an IDENTIFIER_NODE indicating the data member
8249 to be initialized for the second production. */
8251 static tree
8252 cp_parser_mem_initializer_id (cp_parser* parser)
8254 bool global_scope_p;
8255 bool nested_name_specifier_p;
8256 bool template_p = false;
8257 tree id;
8259 /* `typename' is not allowed in this context ([temp.res]). */
8260 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8262 error ("keyword %<typename%> not allowed in this context (a qualified "
8263 "member initializer is implicitly a type)");
8264 cp_lexer_consume_token (parser->lexer);
8266 /* Look for the optional `::' operator. */
8267 global_scope_p
8268 = (cp_parser_global_scope_opt (parser,
8269 /*current_scope_valid_p=*/false)
8270 != NULL_TREE);
8271 /* Look for the optional nested-name-specifier. The simplest way to
8272 implement:
8274 [temp.res]
8276 The keyword `typename' is not permitted in a base-specifier or
8277 mem-initializer; in these contexts a qualified name that
8278 depends on a template-parameter is implicitly assumed to be a
8279 type name.
8281 is to assume that we have seen the `typename' keyword at this
8282 point. */
8283 nested_name_specifier_p
8284 = (cp_parser_nested_name_specifier_opt (parser,
8285 /*typename_keyword_p=*/true,
8286 /*check_dependency_p=*/true,
8287 /*type_p=*/true,
8288 /*is_declaration=*/true)
8289 != NULL_TREE);
8290 if (nested_name_specifier_p)
8291 template_p = cp_parser_optional_template_keyword (parser);
8292 /* If there is a `::' operator or a nested-name-specifier, then we
8293 are definitely looking for a class-name. */
8294 if (global_scope_p || nested_name_specifier_p)
8295 return cp_parser_class_name (parser,
8296 /*typename_keyword_p=*/true,
8297 /*template_keyword_p=*/template_p,
8298 none_type,
8299 /*check_dependency_p=*/true,
8300 /*class_head_p=*/false,
8301 /*is_declaration=*/true);
8302 /* Otherwise, we could also be looking for an ordinary identifier. */
8303 cp_parser_parse_tentatively (parser);
8304 /* Try a class-name. */
8305 id = cp_parser_class_name (parser,
8306 /*typename_keyword_p=*/true,
8307 /*template_keyword_p=*/false,
8308 none_type,
8309 /*check_dependency_p=*/true,
8310 /*class_head_p=*/false,
8311 /*is_declaration=*/true);
8312 /* If we found one, we're done. */
8313 if (cp_parser_parse_definitely (parser))
8314 return id;
8315 /* Otherwise, look for an ordinary identifier. */
8316 return cp_parser_identifier (parser);
8319 /* Overloading [gram.over] */
8321 /* Parse an operator-function-id.
8323 operator-function-id:
8324 operator operator
8326 Returns an IDENTIFIER_NODE for the operator which is a
8327 human-readable spelling of the identifier, e.g., `operator +'. */
8329 static tree
8330 cp_parser_operator_function_id (cp_parser* parser)
8332 /* Look for the `operator' keyword. */
8333 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8334 return error_mark_node;
8335 /* And then the name of the operator itself. */
8336 return cp_parser_operator (parser);
8339 /* Parse an operator.
8341 operator:
8342 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8343 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8344 || ++ -- , ->* -> () []
8346 GNU Extensions:
8348 operator:
8349 <? >? <?= >?=
8351 Returns an IDENTIFIER_NODE for the operator which is a
8352 human-readable spelling of the identifier, e.g., `operator +'. */
8354 static tree
8355 cp_parser_operator (cp_parser* parser)
8357 tree id = NULL_TREE;
8358 cp_token *token;
8360 /* Peek at the next token. */
8361 token = cp_lexer_peek_token (parser->lexer);
8362 /* Figure out which operator we have. */
8363 switch (token->type)
8365 case CPP_KEYWORD:
8367 enum tree_code op;
8369 /* The keyword should be either `new' or `delete'. */
8370 if (token->keyword == RID_NEW)
8371 op = NEW_EXPR;
8372 else if (token->keyword == RID_DELETE)
8373 op = DELETE_EXPR;
8374 else
8375 break;
8377 /* Consume the `new' or `delete' token. */
8378 cp_lexer_consume_token (parser->lexer);
8380 /* Peek at the next token. */
8381 token = cp_lexer_peek_token (parser->lexer);
8382 /* If it's a `[' token then this is the array variant of the
8383 operator. */
8384 if (token->type == CPP_OPEN_SQUARE)
8386 /* Consume the `[' token. */
8387 cp_lexer_consume_token (parser->lexer);
8388 /* Look for the `]' token. */
8389 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8390 id = ansi_opname (op == NEW_EXPR
8391 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8393 /* Otherwise, we have the non-array variant. */
8394 else
8395 id = ansi_opname (op);
8397 return id;
8400 case CPP_PLUS:
8401 id = ansi_opname (PLUS_EXPR);
8402 break;
8404 case CPP_MINUS:
8405 id = ansi_opname (MINUS_EXPR);
8406 break;
8408 case CPP_MULT:
8409 id = ansi_opname (MULT_EXPR);
8410 break;
8412 case CPP_DIV:
8413 id = ansi_opname (TRUNC_DIV_EXPR);
8414 break;
8416 case CPP_MOD:
8417 id = ansi_opname (TRUNC_MOD_EXPR);
8418 break;
8420 case CPP_XOR:
8421 id = ansi_opname (BIT_XOR_EXPR);
8422 break;
8424 case CPP_AND:
8425 id = ansi_opname (BIT_AND_EXPR);
8426 break;
8428 case CPP_OR:
8429 id = ansi_opname (BIT_IOR_EXPR);
8430 break;
8432 case CPP_COMPL:
8433 id = ansi_opname (BIT_NOT_EXPR);
8434 break;
8436 case CPP_NOT:
8437 id = ansi_opname (TRUTH_NOT_EXPR);
8438 break;
8440 case CPP_EQ:
8441 id = ansi_assopname (NOP_EXPR);
8442 break;
8444 case CPP_LESS:
8445 id = ansi_opname (LT_EXPR);
8446 break;
8448 case CPP_GREATER:
8449 id = ansi_opname (GT_EXPR);
8450 break;
8452 case CPP_PLUS_EQ:
8453 id = ansi_assopname (PLUS_EXPR);
8454 break;
8456 case CPP_MINUS_EQ:
8457 id = ansi_assopname (MINUS_EXPR);
8458 break;
8460 case CPP_MULT_EQ:
8461 id = ansi_assopname (MULT_EXPR);
8462 break;
8464 case CPP_DIV_EQ:
8465 id = ansi_assopname (TRUNC_DIV_EXPR);
8466 break;
8468 case CPP_MOD_EQ:
8469 id = ansi_assopname (TRUNC_MOD_EXPR);
8470 break;
8472 case CPP_XOR_EQ:
8473 id = ansi_assopname (BIT_XOR_EXPR);
8474 break;
8476 case CPP_AND_EQ:
8477 id = ansi_assopname (BIT_AND_EXPR);
8478 break;
8480 case CPP_OR_EQ:
8481 id = ansi_assopname (BIT_IOR_EXPR);
8482 break;
8484 case CPP_LSHIFT:
8485 id = ansi_opname (LSHIFT_EXPR);
8486 break;
8488 case CPP_RSHIFT:
8489 id = ansi_opname (RSHIFT_EXPR);
8490 break;
8492 case CPP_LSHIFT_EQ:
8493 id = ansi_assopname (LSHIFT_EXPR);
8494 break;
8496 case CPP_RSHIFT_EQ:
8497 id = ansi_assopname (RSHIFT_EXPR);
8498 break;
8500 case CPP_EQ_EQ:
8501 id = ansi_opname (EQ_EXPR);
8502 break;
8504 case CPP_NOT_EQ:
8505 id = ansi_opname (NE_EXPR);
8506 break;
8508 case CPP_LESS_EQ:
8509 id = ansi_opname (LE_EXPR);
8510 break;
8512 case CPP_GREATER_EQ:
8513 id = ansi_opname (GE_EXPR);
8514 break;
8516 case CPP_AND_AND:
8517 id = ansi_opname (TRUTH_ANDIF_EXPR);
8518 break;
8520 case CPP_OR_OR:
8521 id = ansi_opname (TRUTH_ORIF_EXPR);
8522 break;
8524 case CPP_PLUS_PLUS:
8525 id = ansi_opname (POSTINCREMENT_EXPR);
8526 break;
8528 case CPP_MINUS_MINUS:
8529 id = ansi_opname (PREDECREMENT_EXPR);
8530 break;
8532 case CPP_COMMA:
8533 id = ansi_opname (COMPOUND_EXPR);
8534 break;
8536 case CPP_DEREF_STAR:
8537 id = ansi_opname (MEMBER_REF);
8538 break;
8540 case CPP_DEREF:
8541 id = ansi_opname (COMPONENT_REF);
8542 break;
8544 case CPP_OPEN_PAREN:
8545 /* Consume the `('. */
8546 cp_lexer_consume_token (parser->lexer);
8547 /* Look for the matching `)'. */
8548 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8549 return ansi_opname (CALL_EXPR);
8551 case CPP_OPEN_SQUARE:
8552 /* Consume the `['. */
8553 cp_lexer_consume_token (parser->lexer);
8554 /* Look for the matching `]'. */
8555 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8556 return ansi_opname (ARRAY_REF);
8558 default:
8559 /* Anything else is an error. */
8560 break;
8563 /* If we have selected an identifier, we need to consume the
8564 operator token. */
8565 if (id)
8566 cp_lexer_consume_token (parser->lexer);
8567 /* Otherwise, no valid operator name was present. */
8568 else
8570 cp_parser_error (parser, "expected operator");
8571 id = error_mark_node;
8574 return id;
8577 /* Parse a template-declaration.
8579 template-declaration:
8580 export [opt] template < template-parameter-list > declaration
8582 If MEMBER_P is TRUE, this template-declaration occurs within a
8583 class-specifier.
8585 The grammar rule given by the standard isn't correct. What
8586 is really meant is:
8588 template-declaration:
8589 export [opt] template-parameter-list-seq
8590 decl-specifier-seq [opt] init-declarator [opt] ;
8591 export [opt] template-parameter-list-seq
8592 function-definition
8594 template-parameter-list-seq:
8595 template-parameter-list-seq [opt]
8596 template < template-parameter-list > */
8598 static void
8599 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8601 /* Check for `export'. */
8602 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8604 /* Consume the `export' token. */
8605 cp_lexer_consume_token (parser->lexer);
8606 /* Warn that we do not support `export'. */
8607 warning (0, "keyword %<export%> not implemented, and will be ignored");
8610 cp_parser_template_declaration_after_export (parser, member_p);
8613 /* Parse a template-parameter-list.
8615 template-parameter-list:
8616 template-parameter
8617 template-parameter-list , template-parameter
8619 Returns a TREE_LIST. Each node represents a template parameter.
8620 The nodes are connected via their TREE_CHAINs. */
8622 static tree
8623 cp_parser_template_parameter_list (cp_parser* parser)
8625 tree parameter_list = NULL_TREE;
8627 begin_template_parm_list ();
8628 while (true)
8630 tree parameter;
8631 cp_token *token;
8632 bool is_non_type;
8634 /* Parse the template-parameter. */
8635 parameter = cp_parser_template_parameter (parser, &is_non_type);
8636 /* Add it to the list. */
8637 if (parameter != error_mark_node)
8638 parameter_list = process_template_parm (parameter_list,
8639 parameter,
8640 is_non_type);
8641 else
8643 tree err_parm = build_tree_list (parameter, parameter);
8644 TREE_VALUE (err_parm) = error_mark_node;
8645 parameter_list = chainon (parameter_list, err_parm);
8648 /* Peek at the next token. */
8649 token = cp_lexer_peek_token (parser->lexer);
8650 /* If it's not a `,', we're done. */
8651 if (token->type != CPP_COMMA)
8652 break;
8653 /* Otherwise, consume the `,' token. */
8654 cp_lexer_consume_token (parser->lexer);
8657 return end_template_parm_list (parameter_list);
8660 /* Parse a template-parameter.
8662 template-parameter:
8663 type-parameter
8664 parameter-declaration
8666 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8667 the parameter. The TREE_PURPOSE is the default value, if any.
8668 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8669 iff this parameter is a non-type parameter. */
8671 static tree
8672 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8674 cp_token *token;
8675 cp_parameter_declarator *parameter_declarator;
8676 tree parm;
8678 /* Assume it is a type parameter or a template parameter. */
8679 *is_non_type = false;
8680 /* Peek at the next token. */
8681 token = cp_lexer_peek_token (parser->lexer);
8682 /* If it is `class' or `template', we have a type-parameter. */
8683 if (token->keyword == RID_TEMPLATE)
8684 return cp_parser_type_parameter (parser);
8685 /* If it is `class' or `typename' we do not know yet whether it is a
8686 type parameter or a non-type parameter. Consider:
8688 template <typename T, typename T::X X> ...
8692 template <class C, class D*> ...
8694 Here, the first parameter is a type parameter, and the second is
8695 a non-type parameter. We can tell by looking at the token after
8696 the identifier -- if it is a `,', `=', or `>' then we have a type
8697 parameter. */
8698 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8700 /* Peek at the token after `class' or `typename'. */
8701 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8702 /* If it's an identifier, skip it. */
8703 if (token->type == CPP_NAME)
8704 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8705 /* Now, see if the token looks like the end of a template
8706 parameter. */
8707 if (token->type == CPP_COMMA
8708 || token->type == CPP_EQ
8709 || token->type == CPP_GREATER)
8710 return cp_parser_type_parameter (parser);
8713 /* Otherwise, it is a non-type parameter.
8715 [temp.param]
8717 When parsing a default template-argument for a non-type
8718 template-parameter, the first non-nested `>' is taken as the end
8719 of the template parameter-list rather than a greater-than
8720 operator. */
8721 *is_non_type = true;
8722 parameter_declarator
8723 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8724 /*parenthesized_p=*/NULL);
8725 parm = grokdeclarator (parameter_declarator->declarator,
8726 &parameter_declarator->decl_specifiers,
8727 PARM, /*initialized=*/0,
8728 /*attrlist=*/NULL);
8729 if (parm == error_mark_node)
8730 return error_mark_node;
8731 return build_tree_list (parameter_declarator->default_argument, parm);
8734 /* Parse a type-parameter.
8736 type-parameter:
8737 class identifier [opt]
8738 class identifier [opt] = type-id
8739 typename identifier [opt]
8740 typename identifier [opt] = type-id
8741 template < template-parameter-list > class identifier [opt]
8742 template < template-parameter-list > class identifier [opt]
8743 = id-expression
8745 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8746 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8747 the declaration of the parameter. */
8749 static tree
8750 cp_parser_type_parameter (cp_parser* parser)
8752 cp_token *token;
8753 tree parameter;
8755 /* Look for a keyword to tell us what kind of parameter this is. */
8756 token = cp_parser_require (parser, CPP_KEYWORD,
8757 "`class', `typename', or `template'");
8758 if (!token)
8759 return error_mark_node;
8761 switch (token->keyword)
8763 case RID_CLASS:
8764 case RID_TYPENAME:
8766 tree identifier;
8767 tree default_argument;
8769 /* If the next token is an identifier, then it names the
8770 parameter. */
8771 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8772 identifier = cp_parser_identifier (parser);
8773 else
8774 identifier = NULL_TREE;
8776 /* Create the parameter. */
8777 parameter = finish_template_type_parm (class_type_node, identifier);
8779 /* If the next token is an `=', we have a default argument. */
8780 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8782 /* Consume the `=' token. */
8783 cp_lexer_consume_token (parser->lexer);
8784 /* Parse the default-argument. */
8785 push_deferring_access_checks (dk_no_deferred);
8786 default_argument = cp_parser_type_id (parser);
8787 pop_deferring_access_checks ();
8789 else
8790 default_argument = NULL_TREE;
8792 /* Create the combined representation of the parameter and the
8793 default argument. */
8794 parameter = build_tree_list (default_argument, parameter);
8796 break;
8798 case RID_TEMPLATE:
8800 tree parameter_list;
8801 tree identifier;
8802 tree default_argument;
8804 /* Look for the `<'. */
8805 cp_parser_require (parser, CPP_LESS, "`<'");
8806 /* Parse the template-parameter-list. */
8807 parameter_list = cp_parser_template_parameter_list (parser);
8808 /* Look for the `>'. */
8809 cp_parser_require (parser, CPP_GREATER, "`>'");
8810 /* Look for the `class' keyword. */
8811 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8812 /* If the next token is an `=', then there is a
8813 default-argument. If the next token is a `>', we are at
8814 the end of the parameter-list. If the next token is a `,',
8815 then we are at the end of this parameter. */
8816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8817 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8818 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8820 identifier = cp_parser_identifier (parser);
8821 /* Treat invalid names as if the parameter were nameless. */
8822 if (identifier == error_mark_node)
8823 identifier = NULL_TREE;
8825 else
8826 identifier = NULL_TREE;
8828 /* Create the template parameter. */
8829 parameter = finish_template_template_parm (class_type_node,
8830 identifier);
8832 /* If the next token is an `=', then there is a
8833 default-argument. */
8834 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8836 bool is_template;
8838 /* Consume the `='. */
8839 cp_lexer_consume_token (parser->lexer);
8840 /* Parse the id-expression. */
8841 push_deferring_access_checks (dk_no_deferred);
8842 default_argument
8843 = cp_parser_id_expression (parser,
8844 /*template_keyword_p=*/false,
8845 /*check_dependency_p=*/true,
8846 /*template_p=*/&is_template,
8847 /*declarator_p=*/false,
8848 /*optional_p=*/false);
8849 if (TREE_CODE (default_argument) == TYPE_DECL)
8850 /* If the id-expression was a template-id that refers to
8851 a template-class, we already have the declaration here,
8852 so no further lookup is needed. */
8854 else
8855 /* Look up the name. */
8856 default_argument
8857 = cp_parser_lookup_name (parser, default_argument,
8858 none_type,
8859 /*is_template=*/is_template,
8860 /*is_namespace=*/false,
8861 /*check_dependency=*/true,
8862 /*ambiguous_decls=*/NULL);
8863 /* See if the default argument is valid. */
8864 default_argument
8865 = check_template_template_default_arg (default_argument);
8866 pop_deferring_access_checks ();
8868 else
8869 default_argument = NULL_TREE;
8871 /* Create the combined representation of the parameter and the
8872 default argument. */
8873 parameter = build_tree_list (default_argument, parameter);
8875 break;
8877 default:
8878 gcc_unreachable ();
8879 break;
8882 return parameter;
8885 /* Parse a template-id.
8887 template-id:
8888 template-name < template-argument-list [opt] >
8890 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8891 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8892 returned. Otherwise, if the template-name names a function, or set
8893 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8894 names a class, returns a TYPE_DECL for the specialization.
8896 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8897 uninstantiated templates. */
8899 static tree
8900 cp_parser_template_id (cp_parser *parser,
8901 bool template_keyword_p,
8902 bool check_dependency_p,
8903 bool is_declaration)
8905 int i;
8906 tree template;
8907 tree arguments;
8908 tree template_id;
8909 cp_token_position start_of_id = 0;
8910 deferred_access_check *chk;
8911 VEC (deferred_access_check,gc) *access_check;
8912 cp_token *next_token, *next_token_2;
8913 bool is_identifier;
8915 /* If the next token corresponds to a template-id, there is no need
8916 to reparse it. */
8917 next_token = cp_lexer_peek_token (parser->lexer);
8918 if (next_token->type == CPP_TEMPLATE_ID)
8920 struct tree_check *check_value;
8922 /* Get the stored value. */
8923 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
8924 /* Perform any access checks that were deferred. */
8925 access_check = check_value->checks;
8926 if (access_check)
8928 for (i = 0 ;
8929 VEC_iterate (deferred_access_check, access_check, i, chk) ;
8930 ++i)
8932 perform_or_defer_access_check (chk->binfo,
8933 chk->decl,
8934 chk->diag_decl);
8937 /* Return the stored value. */
8938 return check_value->value;
8941 /* Avoid performing name lookup if there is no possibility of
8942 finding a template-id. */
8943 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8944 || (next_token->type == CPP_NAME
8945 && !cp_parser_nth_token_starts_template_argument_list_p
8946 (parser, 2)))
8948 cp_parser_error (parser, "expected template-id");
8949 return error_mark_node;
8952 /* Remember where the template-id starts. */
8953 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8954 start_of_id = cp_lexer_token_position (parser->lexer, false);
8956 push_deferring_access_checks (dk_deferred);
8958 /* Parse the template-name. */
8959 is_identifier = false;
8960 template = cp_parser_template_name (parser, template_keyword_p,
8961 check_dependency_p,
8962 is_declaration,
8963 &is_identifier);
8964 if (template == error_mark_node || is_identifier)
8966 pop_deferring_access_checks ();
8967 return template;
8970 /* If we find the sequence `[:' after a template-name, it's probably
8971 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8972 parse correctly the argument list. */
8973 next_token = cp_lexer_peek_token (parser->lexer);
8974 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8975 if (next_token->type == CPP_OPEN_SQUARE
8976 && next_token->flags & DIGRAPH
8977 && next_token_2->type == CPP_COLON
8978 && !(next_token_2->flags & PREV_WHITE))
8980 cp_parser_parse_tentatively (parser);
8981 /* Change `:' into `::'. */
8982 next_token_2->type = CPP_SCOPE;
8983 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8984 CPP_LESS. */
8985 cp_lexer_consume_token (parser->lexer);
8986 /* Parse the arguments. */
8987 arguments = cp_parser_enclosed_template_argument_list (parser);
8988 if (!cp_parser_parse_definitely (parser))
8990 /* If we couldn't parse an argument list, then we revert our changes
8991 and return simply an error. Maybe this is not a template-id
8992 after all. */
8993 next_token_2->type = CPP_COLON;
8994 cp_parser_error (parser, "expected %<<%>");
8995 pop_deferring_access_checks ();
8996 return error_mark_node;
8998 /* Otherwise, emit an error about the invalid digraph, but continue
8999 parsing because we got our argument list. */
9000 pedwarn ("%<<::%> cannot begin a template-argument list");
9001 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9002 "between %<<%> and %<::%>");
9003 if (!flag_permissive)
9005 static bool hint;
9006 if (!hint)
9008 inform ("(if you use -fpermissive G++ will accept your code)");
9009 hint = true;
9013 else
9015 /* Look for the `<' that starts the template-argument-list. */
9016 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9018 pop_deferring_access_checks ();
9019 return error_mark_node;
9021 /* Parse the arguments. */
9022 arguments = cp_parser_enclosed_template_argument_list (parser);
9025 /* Build a representation of the specialization. */
9026 if (TREE_CODE (template) == IDENTIFIER_NODE)
9027 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9028 else if (DECL_CLASS_TEMPLATE_P (template)
9029 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9031 bool entering_scope;
9032 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9033 template (rather than some instantiation thereof) only if
9034 is not nested within some other construct. For example, in
9035 "template <typename T> void f(T) { A<T>::", A<T> is just an
9036 instantiation of A. */
9037 entering_scope = (template_parm_scope_p ()
9038 && cp_lexer_next_token_is (parser->lexer,
9039 CPP_SCOPE));
9040 template_id
9041 = finish_template_type (template, arguments, entering_scope);
9043 else
9045 /* If it's not a class-template or a template-template, it should be
9046 a function-template. */
9047 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9048 || TREE_CODE (template) == OVERLOAD
9049 || BASELINK_P (template)));
9051 template_id = lookup_template_function (template, arguments);
9054 /* If parsing tentatively, replace the sequence of tokens that makes
9055 up the template-id with a CPP_TEMPLATE_ID token. That way,
9056 should we re-parse the token stream, we will not have to repeat
9057 the effort required to do the parse, nor will we issue duplicate
9058 error messages about problems during instantiation of the
9059 template. */
9060 if (start_of_id)
9062 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9064 /* Reset the contents of the START_OF_ID token. */
9065 token->type = CPP_TEMPLATE_ID;
9066 /* Retrieve any deferred checks. Do not pop this access checks yet
9067 so the memory will not be reclaimed during token replacing below. */
9068 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9069 token->u.tree_check_value->value = template_id;
9070 token->u.tree_check_value->checks = get_deferred_access_checks ();
9071 token->keyword = RID_MAX;
9073 /* Purge all subsequent tokens. */
9074 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9076 /* ??? Can we actually assume that, if template_id ==
9077 error_mark_node, we will have issued a diagnostic to the
9078 user, as opposed to simply marking the tentative parse as
9079 failed? */
9080 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9081 error ("parse error in template argument list");
9084 pop_deferring_access_checks ();
9085 return template_id;
9088 /* Parse a template-name.
9090 template-name:
9091 identifier
9093 The standard should actually say:
9095 template-name:
9096 identifier
9097 operator-function-id
9099 A defect report has been filed about this issue.
9101 A conversion-function-id cannot be a template name because they cannot
9102 be part of a template-id. In fact, looking at this code:
9104 a.operator K<int>()
9106 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9107 It is impossible to call a templated conversion-function-id with an
9108 explicit argument list, since the only allowed template parameter is
9109 the type to which it is converting.
9111 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9112 `template' keyword, in a construction like:
9114 T::template f<3>()
9116 In that case `f' is taken to be a template-name, even though there
9117 is no way of knowing for sure.
9119 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9120 name refers to a set of overloaded functions, at least one of which
9121 is a template, or an IDENTIFIER_NODE with the name of the template,
9122 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9123 names are looked up inside uninstantiated templates. */
9125 static tree
9126 cp_parser_template_name (cp_parser* parser,
9127 bool template_keyword_p,
9128 bool check_dependency_p,
9129 bool is_declaration,
9130 bool *is_identifier)
9132 tree identifier;
9133 tree decl;
9134 tree fns;
9136 /* If the next token is `operator', then we have either an
9137 operator-function-id or a conversion-function-id. */
9138 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9140 /* We don't know whether we're looking at an
9141 operator-function-id or a conversion-function-id. */
9142 cp_parser_parse_tentatively (parser);
9143 /* Try an operator-function-id. */
9144 identifier = cp_parser_operator_function_id (parser);
9145 /* If that didn't work, try a conversion-function-id. */
9146 if (!cp_parser_parse_definitely (parser))
9148 cp_parser_error (parser, "expected template-name");
9149 return error_mark_node;
9152 /* Look for the identifier. */
9153 else
9154 identifier = cp_parser_identifier (parser);
9156 /* If we didn't find an identifier, we don't have a template-id. */
9157 if (identifier == error_mark_node)
9158 return error_mark_node;
9160 /* If the name immediately followed the `template' keyword, then it
9161 is a template-name. However, if the next token is not `<', then
9162 we do not treat it as a template-name, since it is not being used
9163 as part of a template-id. This enables us to handle constructs
9164 like:
9166 template <typename T> struct S { S(); };
9167 template <typename T> S<T>::S();
9169 correctly. We would treat `S' as a template -- if it were `S<T>'
9170 -- but we do not if there is no `<'. */
9172 if (processing_template_decl
9173 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9175 /* In a declaration, in a dependent context, we pretend that the
9176 "template" keyword was present in order to improve error
9177 recovery. For example, given:
9179 template <typename T> void f(T::X<int>);
9181 we want to treat "X<int>" as a template-id. */
9182 if (is_declaration
9183 && !template_keyword_p
9184 && parser->scope && TYPE_P (parser->scope)
9185 && check_dependency_p
9186 && dependent_type_p (parser->scope)
9187 /* Do not do this for dtors (or ctors), since they never
9188 need the template keyword before their name. */
9189 && !constructor_name_p (identifier, parser->scope))
9191 cp_token_position start = 0;
9193 /* Explain what went wrong. */
9194 error ("non-template %qD used as template", identifier);
9195 inform ("use %<%T::template %D%> to indicate that it is a template",
9196 parser->scope, identifier);
9197 /* If parsing tentatively, find the location of the "<" token. */
9198 if (cp_parser_simulate_error (parser))
9199 start = cp_lexer_token_position (parser->lexer, true);
9200 /* Parse the template arguments so that we can issue error
9201 messages about them. */
9202 cp_lexer_consume_token (parser->lexer);
9203 cp_parser_enclosed_template_argument_list (parser);
9204 /* Skip tokens until we find a good place from which to
9205 continue parsing. */
9206 cp_parser_skip_to_closing_parenthesis (parser,
9207 /*recovering=*/true,
9208 /*or_comma=*/true,
9209 /*consume_paren=*/false);
9210 /* If parsing tentatively, permanently remove the
9211 template argument list. That will prevent duplicate
9212 error messages from being issued about the missing
9213 "template" keyword. */
9214 if (start)
9215 cp_lexer_purge_tokens_after (parser->lexer, start);
9216 if (is_identifier)
9217 *is_identifier = true;
9218 return identifier;
9221 /* If the "template" keyword is present, then there is generally
9222 no point in doing name-lookup, so we just return IDENTIFIER.
9223 But, if the qualifying scope is non-dependent then we can
9224 (and must) do name-lookup normally. */
9225 if (template_keyword_p
9226 && (!parser->scope
9227 || (TYPE_P (parser->scope)
9228 && dependent_type_p (parser->scope))))
9229 return identifier;
9232 /* Look up the name. */
9233 decl = cp_parser_lookup_name (parser, identifier,
9234 none_type,
9235 /*is_template=*/false,
9236 /*is_namespace=*/false,
9237 check_dependency_p,
9238 /*ambiguous_decls=*/NULL);
9239 decl = maybe_get_template_decl_from_type_decl (decl);
9241 /* If DECL is a template, then the name was a template-name. */
9242 if (TREE_CODE (decl) == TEMPLATE_DECL)
9244 else
9246 tree fn = NULL_TREE;
9248 /* The standard does not explicitly indicate whether a name that
9249 names a set of overloaded declarations, some of which are
9250 templates, is a template-name. However, such a name should
9251 be a template-name; otherwise, there is no way to form a
9252 template-id for the overloaded templates. */
9253 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9254 if (TREE_CODE (fns) == OVERLOAD)
9255 for (fn = fns; fn; fn = OVL_NEXT (fn))
9256 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9257 break;
9259 if (!fn)
9261 /* The name does not name a template. */
9262 cp_parser_error (parser, "expected template-name");
9263 return error_mark_node;
9267 /* If DECL is dependent, and refers to a function, then just return
9268 its name; we will look it up again during template instantiation. */
9269 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9271 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9272 if (TYPE_P (scope) && dependent_type_p (scope))
9273 return identifier;
9276 return decl;
9279 /* Parse a template-argument-list.
9281 template-argument-list:
9282 template-argument
9283 template-argument-list , template-argument
9285 Returns a TREE_VEC containing the arguments. */
9287 static tree
9288 cp_parser_template_argument_list (cp_parser* parser)
9290 tree fixed_args[10];
9291 unsigned n_args = 0;
9292 unsigned alloced = 10;
9293 tree *arg_ary = fixed_args;
9294 tree vec;
9295 bool saved_in_template_argument_list_p;
9296 bool saved_ice_p;
9297 bool saved_non_ice_p;
9299 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9300 parser->in_template_argument_list_p = true;
9301 /* Even if the template-id appears in an integral
9302 constant-expression, the contents of the argument list do
9303 not. */
9304 saved_ice_p = parser->integral_constant_expression_p;
9305 parser->integral_constant_expression_p = false;
9306 saved_non_ice_p = parser->non_integral_constant_expression_p;
9307 parser->non_integral_constant_expression_p = false;
9308 /* Parse the arguments. */
9311 tree argument;
9313 if (n_args)
9314 /* Consume the comma. */
9315 cp_lexer_consume_token (parser->lexer);
9317 /* Parse the template-argument. */
9318 argument = cp_parser_template_argument (parser);
9319 if (n_args == alloced)
9321 alloced *= 2;
9323 if (arg_ary == fixed_args)
9325 arg_ary = XNEWVEC (tree, alloced);
9326 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9328 else
9329 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9331 arg_ary[n_args++] = argument;
9333 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9335 vec = make_tree_vec (n_args);
9337 while (n_args--)
9338 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9340 if (arg_ary != fixed_args)
9341 free (arg_ary);
9342 parser->non_integral_constant_expression_p = saved_non_ice_p;
9343 parser->integral_constant_expression_p = saved_ice_p;
9344 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9345 return vec;
9348 /* Parse a template-argument.
9350 template-argument:
9351 assignment-expression
9352 type-id
9353 id-expression
9355 The representation is that of an assignment-expression, type-id, or
9356 id-expression -- except that the qualified id-expression is
9357 evaluated, so that the value returned is either a DECL or an
9358 OVERLOAD.
9360 Although the standard says "assignment-expression", it forbids
9361 throw-expressions or assignments in the template argument.
9362 Therefore, we use "conditional-expression" instead. */
9364 static tree
9365 cp_parser_template_argument (cp_parser* parser)
9367 tree argument;
9368 bool template_p;
9369 bool address_p;
9370 bool maybe_type_id = false;
9371 cp_token *token;
9372 cp_id_kind idk;
9374 /* There's really no way to know what we're looking at, so we just
9375 try each alternative in order.
9377 [temp.arg]
9379 In a template-argument, an ambiguity between a type-id and an
9380 expression is resolved to a type-id, regardless of the form of
9381 the corresponding template-parameter.
9383 Therefore, we try a type-id first. */
9384 cp_parser_parse_tentatively (parser);
9385 argument = cp_parser_type_id (parser);
9386 /* If there was no error parsing the type-id but the next token is a '>>',
9387 we probably found a typo for '> >'. But there are type-id which are
9388 also valid expressions. For instance:
9390 struct X { int operator >> (int); };
9391 template <int V> struct Foo {};
9392 Foo<X () >> 5> r;
9394 Here 'X()' is a valid type-id of a function type, but the user just
9395 wanted to write the expression "X() >> 5". Thus, we remember that we
9396 found a valid type-id, but we still try to parse the argument as an
9397 expression to see what happens. */
9398 if (!cp_parser_error_occurred (parser)
9399 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9401 maybe_type_id = true;
9402 cp_parser_abort_tentative_parse (parser);
9404 else
9406 /* If the next token isn't a `,' or a `>', then this argument wasn't
9407 really finished. This means that the argument is not a valid
9408 type-id. */
9409 if (!cp_parser_next_token_ends_template_argument_p (parser))
9410 cp_parser_error (parser, "expected template-argument");
9411 /* If that worked, we're done. */
9412 if (cp_parser_parse_definitely (parser))
9413 return argument;
9415 /* We're still not sure what the argument will be. */
9416 cp_parser_parse_tentatively (parser);
9417 /* Try a template. */
9418 argument = cp_parser_id_expression (parser,
9419 /*template_keyword_p=*/false,
9420 /*check_dependency_p=*/true,
9421 &template_p,
9422 /*declarator_p=*/false,
9423 /*optional_p=*/false);
9424 /* If the next token isn't a `,' or a `>', then this argument wasn't
9425 really finished. */
9426 if (!cp_parser_next_token_ends_template_argument_p (parser))
9427 cp_parser_error (parser, "expected template-argument");
9428 if (!cp_parser_error_occurred (parser))
9430 /* Figure out what is being referred to. If the id-expression
9431 was for a class template specialization, then we will have a
9432 TYPE_DECL at this point. There is no need to do name lookup
9433 at this point in that case. */
9434 if (TREE_CODE (argument) != TYPE_DECL)
9435 argument = cp_parser_lookup_name (parser, argument,
9436 none_type,
9437 /*is_template=*/template_p,
9438 /*is_namespace=*/false,
9439 /*check_dependency=*/true,
9440 /*ambiguous_decls=*/NULL);
9441 if (TREE_CODE (argument) != TEMPLATE_DECL
9442 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9443 cp_parser_error (parser, "expected template-name");
9445 if (cp_parser_parse_definitely (parser))
9446 return argument;
9447 /* It must be a non-type argument. There permitted cases are given
9448 in [temp.arg.nontype]:
9450 -- an integral constant-expression of integral or enumeration
9451 type; or
9453 -- the name of a non-type template-parameter; or
9455 -- the name of an object or function with external linkage...
9457 -- the address of an object or function with external linkage...
9459 -- a pointer to member... */
9460 /* Look for a non-type template parameter. */
9461 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9463 cp_parser_parse_tentatively (parser);
9464 argument = cp_parser_primary_expression (parser,
9465 /*adress_p=*/false,
9466 /*cast_p=*/false,
9467 /*template_arg_p=*/true,
9468 &idk);
9469 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9470 || !cp_parser_next_token_ends_template_argument_p (parser))
9471 cp_parser_simulate_error (parser);
9472 if (cp_parser_parse_definitely (parser))
9473 return argument;
9476 /* If the next token is "&", the argument must be the address of an
9477 object or function with external linkage. */
9478 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9479 if (address_p)
9480 cp_lexer_consume_token (parser->lexer);
9481 /* See if we might have an id-expression. */
9482 token = cp_lexer_peek_token (parser->lexer);
9483 if (token->type == CPP_NAME
9484 || token->keyword == RID_OPERATOR
9485 || token->type == CPP_SCOPE
9486 || token->type == CPP_TEMPLATE_ID
9487 || token->type == CPP_NESTED_NAME_SPECIFIER)
9489 cp_parser_parse_tentatively (parser);
9490 argument = cp_parser_primary_expression (parser,
9491 address_p,
9492 /*cast_p=*/false,
9493 /*template_arg_p=*/true,
9494 &idk);
9495 if (cp_parser_error_occurred (parser)
9496 || !cp_parser_next_token_ends_template_argument_p (parser))
9497 cp_parser_abort_tentative_parse (parser);
9498 else
9500 if (TREE_CODE (argument) == INDIRECT_REF)
9502 gcc_assert (REFERENCE_REF_P (argument));
9503 argument = TREE_OPERAND (argument, 0);
9506 if (TREE_CODE (argument) == VAR_DECL)
9508 /* A variable without external linkage might still be a
9509 valid constant-expression, so no error is issued here
9510 if the external-linkage check fails. */
9511 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9512 cp_parser_simulate_error (parser);
9514 else if (is_overloaded_fn (argument))
9515 /* All overloaded functions are allowed; if the external
9516 linkage test does not pass, an error will be issued
9517 later. */
9519 else if (address_p
9520 && (TREE_CODE (argument) == OFFSET_REF
9521 || TREE_CODE (argument) == SCOPE_REF))
9522 /* A pointer-to-member. */
9524 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9526 else
9527 cp_parser_simulate_error (parser);
9529 if (cp_parser_parse_definitely (parser))
9531 if (address_p)
9532 argument = build_x_unary_op (ADDR_EXPR, argument);
9533 return argument;
9537 /* If the argument started with "&", there are no other valid
9538 alternatives at this point. */
9539 if (address_p)
9541 cp_parser_error (parser, "invalid non-type template argument");
9542 return error_mark_node;
9545 /* If the argument wasn't successfully parsed as a type-id followed
9546 by '>>', the argument can only be a constant expression now.
9547 Otherwise, we try parsing the constant-expression tentatively,
9548 because the argument could really be a type-id. */
9549 if (maybe_type_id)
9550 cp_parser_parse_tentatively (parser);
9551 argument = cp_parser_constant_expression (parser,
9552 /*allow_non_constant_p=*/false,
9553 /*non_constant_p=*/NULL);
9554 argument = fold_non_dependent_expr (argument);
9555 if (!maybe_type_id)
9556 return argument;
9557 if (!cp_parser_next_token_ends_template_argument_p (parser))
9558 cp_parser_error (parser, "expected template-argument");
9559 if (cp_parser_parse_definitely (parser))
9560 return argument;
9561 /* We did our best to parse the argument as a non type-id, but that
9562 was the only alternative that matched (albeit with a '>' after
9563 it). We can assume it's just a typo from the user, and a
9564 diagnostic will then be issued. */
9565 return cp_parser_type_id (parser);
9568 /* Parse an explicit-instantiation.
9570 explicit-instantiation:
9571 template declaration
9573 Although the standard says `declaration', what it really means is:
9575 explicit-instantiation:
9576 template decl-specifier-seq [opt] declarator [opt] ;
9578 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9579 supposed to be allowed. A defect report has been filed about this
9580 issue.
9582 GNU Extension:
9584 explicit-instantiation:
9585 storage-class-specifier template
9586 decl-specifier-seq [opt] declarator [opt] ;
9587 function-specifier template
9588 decl-specifier-seq [opt] declarator [opt] ; */
9590 static void
9591 cp_parser_explicit_instantiation (cp_parser* parser)
9593 int declares_class_or_enum;
9594 cp_decl_specifier_seq decl_specifiers;
9595 tree extension_specifier = NULL_TREE;
9597 /* Look for an (optional) storage-class-specifier or
9598 function-specifier. */
9599 if (cp_parser_allow_gnu_extensions_p (parser))
9601 extension_specifier
9602 = cp_parser_storage_class_specifier_opt (parser);
9603 if (!extension_specifier)
9604 extension_specifier
9605 = cp_parser_function_specifier_opt (parser,
9606 /*decl_specs=*/NULL);
9609 /* Look for the `template' keyword. */
9610 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9611 /* Let the front end know that we are processing an explicit
9612 instantiation. */
9613 begin_explicit_instantiation ();
9614 /* [temp.explicit] says that we are supposed to ignore access
9615 control while processing explicit instantiation directives. */
9616 push_deferring_access_checks (dk_no_check);
9617 /* Parse a decl-specifier-seq. */
9618 cp_parser_decl_specifier_seq (parser,
9619 CP_PARSER_FLAGS_OPTIONAL,
9620 &decl_specifiers,
9621 &declares_class_or_enum);
9622 /* If there was exactly one decl-specifier, and it declared a class,
9623 and there's no declarator, then we have an explicit type
9624 instantiation. */
9625 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9627 tree type;
9629 type = check_tag_decl (&decl_specifiers);
9630 /* Turn access control back on for names used during
9631 template instantiation. */
9632 pop_deferring_access_checks ();
9633 if (type)
9634 do_type_instantiation (type, extension_specifier,
9635 /*complain=*/tf_error);
9637 else
9639 cp_declarator *declarator;
9640 tree decl;
9642 /* Parse the declarator. */
9643 declarator
9644 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9645 /*ctor_dtor_or_conv_p=*/NULL,
9646 /*parenthesized_p=*/NULL,
9647 /*member_p=*/false);
9648 if (declares_class_or_enum & 2)
9649 cp_parser_check_for_definition_in_return_type (declarator,
9650 decl_specifiers.type);
9651 if (declarator != cp_error_declarator)
9653 decl = grokdeclarator (declarator, &decl_specifiers,
9654 NORMAL, 0, &decl_specifiers.attributes);
9655 /* Turn access control back on for names used during
9656 template instantiation. */
9657 pop_deferring_access_checks ();
9658 /* Do the explicit instantiation. */
9659 do_decl_instantiation (decl, extension_specifier);
9661 else
9663 pop_deferring_access_checks ();
9664 /* Skip the body of the explicit instantiation. */
9665 cp_parser_skip_to_end_of_statement (parser);
9668 /* We're done with the instantiation. */
9669 end_explicit_instantiation ();
9671 cp_parser_consume_semicolon_at_end_of_statement (parser);
9674 /* Parse an explicit-specialization.
9676 explicit-specialization:
9677 template < > declaration
9679 Although the standard says `declaration', what it really means is:
9681 explicit-specialization:
9682 template <> decl-specifier [opt] init-declarator [opt] ;
9683 template <> function-definition
9684 template <> explicit-specialization
9685 template <> template-declaration */
9687 static void
9688 cp_parser_explicit_specialization (cp_parser* parser)
9690 bool need_lang_pop;
9691 /* Look for the `template' keyword. */
9692 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9693 /* Look for the `<'. */
9694 cp_parser_require (parser, CPP_LESS, "`<'");
9695 /* Look for the `>'. */
9696 cp_parser_require (parser, CPP_GREATER, "`>'");
9697 /* We have processed another parameter list. */
9698 ++parser->num_template_parameter_lists;
9699 /* [temp]
9701 A template ... explicit specialization ... shall not have C
9702 linkage. */
9703 if (current_lang_name == lang_name_c)
9705 error ("template specialization with C linkage");
9706 /* Give it C++ linkage to avoid confusing other parts of the
9707 front end. */
9708 push_lang_context (lang_name_cplusplus);
9709 need_lang_pop = true;
9711 else
9712 need_lang_pop = false;
9713 /* Let the front end know that we are beginning a specialization. */
9714 if (!begin_specialization ())
9716 end_specialization ();
9717 cp_parser_skip_to_end_of_block_or_statement (parser);
9718 return;
9721 /* If the next keyword is `template', we need to figure out whether
9722 or not we're looking a template-declaration. */
9723 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9725 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9726 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9727 cp_parser_template_declaration_after_export (parser,
9728 /*member_p=*/false);
9729 else
9730 cp_parser_explicit_specialization (parser);
9732 else
9733 /* Parse the dependent declaration. */
9734 cp_parser_single_declaration (parser,
9735 /*checks=*/NULL,
9736 /*member_p=*/false,
9737 /*friend_p=*/NULL);
9738 /* We're done with the specialization. */
9739 end_specialization ();
9740 /* For the erroneous case of a template with C linkage, we pushed an
9741 implicit C++ linkage scope; exit that scope now. */
9742 if (need_lang_pop)
9743 pop_lang_context ();
9744 /* We're done with this parameter list. */
9745 --parser->num_template_parameter_lists;
9748 /* Parse a type-specifier.
9750 type-specifier:
9751 simple-type-specifier
9752 class-specifier
9753 enum-specifier
9754 elaborated-type-specifier
9755 cv-qualifier
9757 GNU Extension:
9759 type-specifier:
9760 __complex__
9762 Returns a representation of the type-specifier. For a
9763 class-specifier, enum-specifier, or elaborated-type-specifier, a
9764 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9766 The parser flags FLAGS is used to control type-specifier parsing.
9768 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9769 in a decl-specifier-seq.
9771 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9772 class-specifier, enum-specifier, or elaborated-type-specifier, then
9773 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9774 if a type is declared; 2 if it is defined. Otherwise, it is set to
9775 zero.
9777 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9778 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9779 is set to FALSE. */
9781 static tree
9782 cp_parser_type_specifier (cp_parser* parser,
9783 cp_parser_flags flags,
9784 cp_decl_specifier_seq *decl_specs,
9785 bool is_declaration,
9786 int* declares_class_or_enum,
9787 bool* is_cv_qualifier)
9789 tree type_spec = NULL_TREE;
9790 cp_token *token;
9791 enum rid keyword;
9792 cp_decl_spec ds = ds_last;
9794 /* Assume this type-specifier does not declare a new type. */
9795 if (declares_class_or_enum)
9796 *declares_class_or_enum = 0;
9797 /* And that it does not specify a cv-qualifier. */
9798 if (is_cv_qualifier)
9799 *is_cv_qualifier = false;
9800 /* Peek at the next token. */
9801 token = cp_lexer_peek_token (parser->lexer);
9803 /* If we're looking at a keyword, we can use that to guide the
9804 production we choose. */
9805 keyword = token->keyword;
9806 switch (keyword)
9808 case RID_ENUM:
9809 /* Look for the enum-specifier. */
9810 type_spec = cp_parser_enum_specifier (parser);
9811 /* If that worked, we're done. */
9812 if (type_spec)
9814 if (declares_class_or_enum)
9815 *declares_class_or_enum = 2;
9816 if (decl_specs)
9817 cp_parser_set_decl_spec_type (decl_specs,
9818 type_spec,
9819 /*user_defined_p=*/true);
9820 return type_spec;
9822 else
9823 goto elaborated_type_specifier;
9825 /* Any of these indicate either a class-specifier, or an
9826 elaborated-type-specifier. */
9827 case RID_CLASS:
9828 case RID_STRUCT:
9829 case RID_UNION:
9830 /* Parse tentatively so that we can back up if we don't find a
9831 class-specifier. */
9832 cp_parser_parse_tentatively (parser);
9833 /* Look for the class-specifier. */
9834 type_spec = cp_parser_class_specifier (parser);
9835 /* If that worked, we're done. */
9836 if (cp_parser_parse_definitely (parser))
9838 if (declares_class_or_enum)
9839 *declares_class_or_enum = 2;
9840 if (decl_specs)
9841 cp_parser_set_decl_spec_type (decl_specs,
9842 type_spec,
9843 /*user_defined_p=*/true);
9844 return type_spec;
9847 /* Fall through. */
9848 elaborated_type_specifier:
9849 /* We're declaring (not defining) a class or enum. */
9850 if (declares_class_or_enum)
9851 *declares_class_or_enum = 1;
9853 /* Fall through. */
9854 case RID_TYPENAME:
9855 /* Look for an elaborated-type-specifier. */
9856 type_spec
9857 = (cp_parser_elaborated_type_specifier
9858 (parser,
9859 decl_specs && decl_specs->specs[(int) ds_friend],
9860 is_declaration));
9861 if (decl_specs)
9862 cp_parser_set_decl_spec_type (decl_specs,
9863 type_spec,
9864 /*user_defined_p=*/true);
9865 return type_spec;
9867 case RID_CONST:
9868 ds = ds_const;
9869 if (is_cv_qualifier)
9870 *is_cv_qualifier = true;
9871 break;
9873 case RID_VOLATILE:
9874 ds = ds_volatile;
9875 if (is_cv_qualifier)
9876 *is_cv_qualifier = true;
9877 break;
9879 case RID_RESTRICT:
9880 ds = ds_restrict;
9881 if (is_cv_qualifier)
9882 *is_cv_qualifier = true;
9883 break;
9885 case RID_COMPLEX:
9886 /* The `__complex__' keyword is a GNU extension. */
9887 ds = ds_complex;
9888 break;
9890 default:
9891 break;
9894 /* Handle simple keywords. */
9895 if (ds != ds_last)
9897 if (decl_specs)
9899 ++decl_specs->specs[(int)ds];
9900 decl_specs->any_specifiers_p = true;
9902 return cp_lexer_consume_token (parser->lexer)->u.value;
9905 /* If we do not already have a type-specifier, assume we are looking
9906 at a simple-type-specifier. */
9907 type_spec = cp_parser_simple_type_specifier (parser,
9908 decl_specs,
9909 flags);
9911 /* If we didn't find a type-specifier, and a type-specifier was not
9912 optional in this context, issue an error message. */
9913 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9915 cp_parser_error (parser, "expected type specifier");
9916 return error_mark_node;
9919 return type_spec;
9922 /* Parse a simple-type-specifier.
9924 simple-type-specifier:
9925 :: [opt] nested-name-specifier [opt] type-name
9926 :: [opt] nested-name-specifier template template-id
9927 char
9928 wchar_t
9929 bool
9930 short
9932 long
9933 signed
9934 unsigned
9935 float
9936 double
9937 void
9939 GNU Extension:
9941 simple-type-specifier:
9942 __typeof__ unary-expression
9943 __typeof__ ( type-id )
9945 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9946 appropriately updated. */
9948 static tree
9949 cp_parser_simple_type_specifier (cp_parser* parser,
9950 cp_decl_specifier_seq *decl_specs,
9951 cp_parser_flags flags)
9953 tree type = NULL_TREE;
9954 cp_token *token;
9956 /* Peek at the next token. */
9957 token = cp_lexer_peek_token (parser->lexer);
9959 /* If we're looking at a keyword, things are easy. */
9960 switch (token->keyword)
9962 case RID_CHAR:
9963 if (decl_specs)
9964 decl_specs->explicit_char_p = true;
9965 type = char_type_node;
9966 break;
9967 case RID_WCHAR:
9968 type = wchar_type_node;
9969 break;
9970 case RID_BOOL:
9971 type = boolean_type_node;
9972 break;
9973 case RID_SHORT:
9974 if (decl_specs)
9975 ++decl_specs->specs[(int) ds_short];
9976 type = short_integer_type_node;
9977 break;
9978 case RID_INT:
9979 if (decl_specs)
9980 decl_specs->explicit_int_p = true;
9981 type = integer_type_node;
9982 break;
9983 case RID_LONG:
9984 if (decl_specs)
9985 ++decl_specs->specs[(int) ds_long];
9986 type = long_integer_type_node;
9987 break;
9988 case RID_SIGNED:
9989 if (decl_specs)
9990 ++decl_specs->specs[(int) ds_signed];
9991 type = integer_type_node;
9992 break;
9993 case RID_UNSIGNED:
9994 if (decl_specs)
9995 ++decl_specs->specs[(int) ds_unsigned];
9996 type = unsigned_type_node;
9997 break;
9998 case RID_FLOAT:
9999 type = float_type_node;
10000 break;
10001 case RID_DOUBLE:
10002 type = double_type_node;
10003 break;
10004 case RID_VOID:
10005 type = void_type_node;
10006 break;
10008 case RID_TYPEOF:
10009 /* Consume the `typeof' token. */
10010 cp_lexer_consume_token (parser->lexer);
10011 /* Parse the operand to `typeof'. */
10012 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10013 /* If it is not already a TYPE, take its type. */
10014 if (!TYPE_P (type))
10015 type = finish_typeof (type);
10017 if (decl_specs)
10018 cp_parser_set_decl_spec_type (decl_specs, type,
10019 /*user_defined_p=*/true);
10021 return type;
10023 default:
10024 break;
10027 /* If the type-specifier was for a built-in type, we're done. */
10028 if (type)
10030 tree id;
10032 /* Record the type. */
10033 if (decl_specs
10034 && (token->keyword != RID_SIGNED
10035 && token->keyword != RID_UNSIGNED
10036 && token->keyword != RID_SHORT
10037 && token->keyword != RID_LONG))
10038 cp_parser_set_decl_spec_type (decl_specs,
10039 type,
10040 /*user_defined=*/false);
10041 if (decl_specs)
10042 decl_specs->any_specifiers_p = true;
10044 /* Consume the token. */
10045 id = cp_lexer_consume_token (parser->lexer)->u.value;
10047 /* There is no valid C++ program where a non-template type is
10048 followed by a "<". That usually indicates that the user thought
10049 that the type was a template. */
10050 cp_parser_check_for_invalid_template_id (parser, type);
10052 return TYPE_NAME (type);
10055 /* The type-specifier must be a user-defined type. */
10056 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10058 bool qualified_p;
10059 bool global_p;
10061 /* Don't gobble tokens or issue error messages if this is an
10062 optional type-specifier. */
10063 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10064 cp_parser_parse_tentatively (parser);
10066 /* Look for the optional `::' operator. */
10067 global_p
10068 = (cp_parser_global_scope_opt (parser,
10069 /*current_scope_valid_p=*/false)
10070 != NULL_TREE);
10071 /* Look for the nested-name specifier. */
10072 qualified_p
10073 = (cp_parser_nested_name_specifier_opt (parser,
10074 /*typename_keyword_p=*/false,
10075 /*check_dependency_p=*/true,
10076 /*type_p=*/false,
10077 /*is_declaration=*/false)
10078 != NULL_TREE);
10079 /* If we have seen a nested-name-specifier, and the next token
10080 is `template', then we are using the template-id production. */
10081 if (parser->scope
10082 && cp_parser_optional_template_keyword (parser))
10084 /* Look for the template-id. */
10085 type = cp_parser_template_id (parser,
10086 /*template_keyword_p=*/true,
10087 /*check_dependency_p=*/true,
10088 /*is_declaration=*/false);
10089 /* If the template-id did not name a type, we are out of
10090 luck. */
10091 if (TREE_CODE (type) != TYPE_DECL)
10093 cp_parser_error (parser, "expected template-id for type");
10094 type = NULL_TREE;
10097 /* Otherwise, look for a type-name. */
10098 else
10099 type = cp_parser_type_name (parser);
10100 /* Keep track of all name-lookups performed in class scopes. */
10101 if (type
10102 && !global_p
10103 && !qualified_p
10104 && TREE_CODE (type) == TYPE_DECL
10105 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10106 maybe_note_name_used_in_class (DECL_NAME (type), type);
10107 /* If it didn't work out, we don't have a TYPE. */
10108 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10109 && !cp_parser_parse_definitely (parser))
10110 type = NULL_TREE;
10111 if (type && decl_specs)
10112 cp_parser_set_decl_spec_type (decl_specs, type,
10113 /*user_defined=*/true);
10116 /* If we didn't get a type-name, issue an error message. */
10117 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10119 cp_parser_error (parser, "expected type-name");
10120 return error_mark_node;
10123 /* There is no valid C++ program where a non-template type is
10124 followed by a "<". That usually indicates that the user thought
10125 that the type was a template. */
10126 if (type && type != error_mark_node)
10128 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10129 If it is, then the '<'...'>' enclose protocol names rather than
10130 template arguments, and so everything is fine. */
10131 if (c_dialect_objc ()
10132 && (objc_is_id (type) || objc_is_class_name (type)))
10134 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10135 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10137 /* Clobber the "unqualified" type previously entered into
10138 DECL_SPECS with the new, improved protocol-qualified version. */
10139 if (decl_specs)
10140 decl_specs->type = qual_type;
10142 return qual_type;
10145 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10148 return type;
10151 /* Parse a type-name.
10153 type-name:
10154 class-name
10155 enum-name
10156 typedef-name
10158 enum-name:
10159 identifier
10161 typedef-name:
10162 identifier
10164 Returns a TYPE_DECL for the type. */
10166 static tree
10167 cp_parser_type_name (cp_parser* parser)
10169 tree type_decl;
10170 tree identifier;
10172 /* We can't know yet whether it is a class-name or not. */
10173 cp_parser_parse_tentatively (parser);
10174 /* Try a class-name. */
10175 type_decl = cp_parser_class_name (parser,
10176 /*typename_keyword_p=*/false,
10177 /*template_keyword_p=*/false,
10178 none_type,
10179 /*check_dependency_p=*/true,
10180 /*class_head_p=*/false,
10181 /*is_declaration=*/false);
10182 /* If it's not a class-name, keep looking. */
10183 if (!cp_parser_parse_definitely (parser))
10185 /* It must be a typedef-name or an enum-name. */
10186 identifier = cp_parser_identifier (parser);
10187 if (identifier == error_mark_node)
10188 return error_mark_node;
10190 /* Look up the type-name. */
10191 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10193 if (TREE_CODE (type_decl) != TYPE_DECL
10194 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10196 /* See if this is an Objective-C type. */
10197 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10198 tree type = objc_get_protocol_qualified_type (identifier, protos);
10199 if (type)
10200 type_decl = TYPE_NAME (type);
10203 /* Issue an error if we did not find a type-name. */
10204 if (TREE_CODE (type_decl) != TYPE_DECL)
10206 if (!cp_parser_simulate_error (parser))
10207 cp_parser_name_lookup_error (parser, identifier, type_decl,
10208 "is not a type");
10209 type_decl = error_mark_node;
10211 /* Remember that the name was used in the definition of the
10212 current class so that we can check later to see if the
10213 meaning would have been different after the class was
10214 entirely defined. */
10215 else if (type_decl != error_mark_node
10216 && !parser->scope)
10217 maybe_note_name_used_in_class (identifier, type_decl);
10220 return type_decl;
10224 /* Parse an elaborated-type-specifier. Note that the grammar given
10225 here incorporates the resolution to DR68.
10227 elaborated-type-specifier:
10228 class-key :: [opt] nested-name-specifier [opt] identifier
10229 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10230 enum :: [opt] nested-name-specifier [opt] identifier
10231 typename :: [opt] nested-name-specifier identifier
10232 typename :: [opt] nested-name-specifier template [opt]
10233 template-id
10235 GNU extension:
10237 elaborated-type-specifier:
10238 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10239 class-key attributes :: [opt] nested-name-specifier [opt]
10240 template [opt] template-id
10241 enum attributes :: [opt] nested-name-specifier [opt] identifier
10243 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10244 declared `friend'. If IS_DECLARATION is TRUE, then this
10245 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10246 something is being declared.
10248 Returns the TYPE specified. */
10250 static tree
10251 cp_parser_elaborated_type_specifier (cp_parser* parser,
10252 bool is_friend,
10253 bool is_declaration)
10255 enum tag_types tag_type;
10256 tree identifier;
10257 tree type = NULL_TREE;
10258 tree attributes = NULL_TREE;
10260 /* See if we're looking at the `enum' keyword. */
10261 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10263 /* Consume the `enum' token. */
10264 cp_lexer_consume_token (parser->lexer);
10265 /* Remember that it's an enumeration type. */
10266 tag_type = enum_type;
10267 /* Parse the attributes. */
10268 attributes = cp_parser_attributes_opt (parser);
10270 /* Or, it might be `typename'. */
10271 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10272 RID_TYPENAME))
10274 /* Consume the `typename' token. */
10275 cp_lexer_consume_token (parser->lexer);
10276 /* Remember that it's a `typename' type. */
10277 tag_type = typename_type;
10278 /* The `typename' keyword is only allowed in templates. */
10279 if (!processing_template_decl)
10280 pedwarn ("using %<typename%> outside of template");
10282 /* Otherwise it must be a class-key. */
10283 else
10285 tag_type = cp_parser_class_key (parser);
10286 if (tag_type == none_type)
10287 return error_mark_node;
10288 /* Parse the attributes. */
10289 attributes = cp_parser_attributes_opt (parser);
10292 /* Look for the `::' operator. */
10293 cp_parser_global_scope_opt (parser,
10294 /*current_scope_valid_p=*/false);
10295 /* Look for the nested-name-specifier. */
10296 if (tag_type == typename_type)
10298 if (!cp_parser_nested_name_specifier (parser,
10299 /*typename_keyword_p=*/true,
10300 /*check_dependency_p=*/true,
10301 /*type_p=*/true,
10302 is_declaration))
10303 return error_mark_node;
10305 else
10306 /* Even though `typename' is not present, the proposed resolution
10307 to Core Issue 180 says that in `class A<T>::B', `B' should be
10308 considered a type-name, even if `A<T>' is dependent. */
10309 cp_parser_nested_name_specifier_opt (parser,
10310 /*typename_keyword_p=*/true,
10311 /*check_dependency_p=*/true,
10312 /*type_p=*/true,
10313 is_declaration);
10314 /* For everything but enumeration types, consider a template-id. */
10315 /* For an enumeration type, consider only a plain identifier. */
10316 if (tag_type != enum_type)
10318 bool template_p = false;
10319 tree decl;
10321 /* Allow the `template' keyword. */
10322 template_p = cp_parser_optional_template_keyword (parser);
10323 /* If we didn't see `template', we don't know if there's a
10324 template-id or not. */
10325 if (!template_p)
10326 cp_parser_parse_tentatively (parser);
10327 /* Parse the template-id. */
10328 decl = cp_parser_template_id (parser, template_p,
10329 /*check_dependency_p=*/true,
10330 is_declaration);
10331 /* If we didn't find a template-id, look for an ordinary
10332 identifier. */
10333 if (!template_p && !cp_parser_parse_definitely (parser))
10335 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10336 in effect, then we must assume that, upon instantiation, the
10337 template will correspond to a class. */
10338 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10339 && tag_type == typename_type)
10340 type = make_typename_type (parser->scope, decl,
10341 typename_type,
10342 /*complain=*/tf_error);
10343 else
10344 type = TREE_TYPE (decl);
10347 if (!type)
10349 identifier = cp_parser_identifier (parser);
10351 if (identifier == error_mark_node)
10353 parser->scope = NULL_TREE;
10354 return error_mark_node;
10357 /* For a `typename', we needn't call xref_tag. */
10358 if (tag_type == typename_type
10359 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10360 return cp_parser_make_typename_type (parser, parser->scope,
10361 identifier);
10362 /* Look up a qualified name in the usual way. */
10363 if (parser->scope)
10365 tree decl;
10367 decl = cp_parser_lookup_name (parser, identifier,
10368 tag_type,
10369 /*is_template=*/false,
10370 /*is_namespace=*/false,
10371 /*check_dependency=*/true,
10372 /*ambiguous_decls=*/NULL);
10374 /* If we are parsing friend declaration, DECL may be a
10375 TEMPLATE_DECL tree node here. However, we need to check
10376 whether this TEMPLATE_DECL results in valid code. Consider
10377 the following example:
10379 namespace N {
10380 template <class T> class C {};
10382 class X {
10383 template <class T> friend class N::C; // #1, valid code
10385 template <class T> class Y {
10386 friend class N::C; // #2, invalid code
10389 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10390 name lookup of `N::C'. We see that friend declaration must
10391 be template for the code to be valid. Note that
10392 processing_template_decl does not work here since it is
10393 always 1 for the above two cases. */
10395 decl = (cp_parser_maybe_treat_template_as_class
10396 (decl, /*tag_name_p=*/is_friend
10397 && parser->num_template_parameter_lists));
10399 if (TREE_CODE (decl) != TYPE_DECL)
10401 cp_parser_diagnose_invalid_type_name (parser,
10402 parser->scope,
10403 identifier);
10404 return error_mark_node;
10407 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10409 bool allow_template = (parser->num_template_parameter_lists
10410 || DECL_SELF_REFERENCE_P (decl));
10411 type = check_elaborated_type_specifier (tag_type, decl,
10412 allow_template);
10414 if (type == error_mark_node)
10415 return error_mark_node;
10418 type = TREE_TYPE (decl);
10420 else
10422 /* An elaborated-type-specifier sometimes introduces a new type and
10423 sometimes names an existing type. Normally, the rule is that it
10424 introduces a new type only if there is not an existing type of
10425 the same name already in scope. For example, given:
10427 struct S {};
10428 void f() { struct S s; }
10430 the `struct S' in the body of `f' is the same `struct S' as in
10431 the global scope; the existing definition is used. However, if
10432 there were no global declaration, this would introduce a new
10433 local class named `S'.
10435 An exception to this rule applies to the following code:
10437 namespace N { struct S; }
10439 Here, the elaborated-type-specifier names a new type
10440 unconditionally; even if there is already an `S' in the
10441 containing scope this declaration names a new type.
10442 This exception only applies if the elaborated-type-specifier
10443 forms the complete declaration:
10445 [class.name]
10447 A declaration consisting solely of `class-key identifier ;' is
10448 either a redeclaration of the name in the current scope or a
10449 forward declaration of the identifier as a class name. It
10450 introduces the name into the current scope.
10452 We are in this situation precisely when the next token is a `;'.
10454 An exception to the exception is that a `friend' declaration does
10455 *not* name a new type; i.e., given:
10457 struct S { friend struct T; };
10459 `T' is not a new type in the scope of `S'.
10461 Also, `new struct S' or `sizeof (struct S)' never results in the
10462 definition of a new type; a new type can only be declared in a
10463 declaration context. */
10465 tag_scope ts;
10466 bool template_p;
10468 if (is_friend)
10469 /* Friends have special name lookup rules. */
10470 ts = ts_within_enclosing_non_class;
10471 else if (is_declaration
10472 && cp_lexer_next_token_is (parser->lexer,
10473 CPP_SEMICOLON))
10474 /* This is a `class-key identifier ;' */
10475 ts = ts_current;
10476 else
10477 ts = ts_global;
10479 template_p =
10480 (parser->num_template_parameter_lists
10481 && (cp_parser_next_token_starts_class_definition_p (parser)
10482 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10483 /* An unqualified name was used to reference this type, so
10484 there were no qualifying templates. */
10485 if (!cp_parser_check_template_parameters (parser,
10486 /*num_templates=*/0))
10487 return error_mark_node;
10488 type = xref_tag (tag_type, identifier, ts, template_p);
10492 if (type == error_mark_node)
10493 return error_mark_node;
10495 /* Allow attributes on forward declarations of classes. */
10496 if (attributes)
10498 if (TREE_CODE (type) == TYPENAME_TYPE)
10499 warning (OPT_Wattributes,
10500 "attributes ignored on uninstantiated type");
10501 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10502 && ! processing_explicit_instantiation)
10503 warning (OPT_Wattributes,
10504 "attributes ignored on template instantiation");
10505 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10506 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10507 else
10508 warning (OPT_Wattributes,
10509 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10512 if (tag_type != enum_type)
10513 cp_parser_check_class_key (tag_type, type);
10515 /* A "<" cannot follow an elaborated type specifier. If that
10516 happens, the user was probably trying to form a template-id. */
10517 cp_parser_check_for_invalid_template_id (parser, type);
10519 return type;
10522 /* Parse an enum-specifier.
10524 enum-specifier:
10525 enum identifier [opt] { enumerator-list [opt] }
10527 GNU Extensions:
10528 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10529 attributes[opt]
10531 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10532 if the token stream isn't an enum-specifier after all. */
10534 static tree
10535 cp_parser_enum_specifier (cp_parser* parser)
10537 tree identifier;
10538 tree type;
10539 tree attributes;
10541 /* Parse tentatively so that we can back up if we don't find a
10542 enum-specifier. */
10543 cp_parser_parse_tentatively (parser);
10545 /* Caller guarantees that the current token is 'enum', an identifier
10546 possibly follows, and the token after that is an opening brace.
10547 If we don't have an identifier, fabricate an anonymous name for
10548 the enumeration being defined. */
10549 cp_lexer_consume_token (parser->lexer);
10551 attributes = cp_parser_attributes_opt (parser);
10553 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10554 identifier = cp_parser_identifier (parser);
10555 else
10556 identifier = make_anon_name ();
10558 /* Look for the `{' but don't consume it yet. */
10559 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10560 cp_parser_simulate_error (parser);
10562 if (!cp_parser_parse_definitely (parser))
10563 return NULL_TREE;
10565 /* Issue an error message if type-definitions are forbidden here. */
10566 if (!cp_parser_check_type_definition (parser))
10567 type = error_mark_node;
10568 else
10569 /* Create the new type. We do this before consuming the opening
10570 brace so the enum will be recorded as being on the line of its
10571 tag (or the 'enum' keyword, if there is no tag). */
10572 type = start_enum (identifier);
10574 /* Consume the opening brace. */
10575 cp_lexer_consume_token (parser->lexer);
10577 if (type == error_mark_node)
10579 cp_parser_skip_to_end_of_block_or_statement (parser);
10580 return error_mark_node;
10583 /* If the next token is not '}', then there are some enumerators. */
10584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10585 cp_parser_enumerator_list (parser, type);
10587 /* Consume the final '}'. */
10588 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10590 /* Look for trailing attributes to apply to this enumeration, and
10591 apply them if appropriate. */
10592 if (cp_parser_allow_gnu_extensions_p (parser))
10594 tree trailing_attr = cp_parser_attributes_opt (parser);
10595 cplus_decl_attributes (&type,
10596 trailing_attr,
10597 (int) ATTR_FLAG_TYPE_IN_PLACE);
10600 /* Finish up the enumeration. */
10601 finish_enum (type);
10603 return type;
10606 /* Parse an enumerator-list. The enumerators all have the indicated
10607 TYPE.
10609 enumerator-list:
10610 enumerator-definition
10611 enumerator-list , enumerator-definition */
10613 static void
10614 cp_parser_enumerator_list (cp_parser* parser, tree type)
10616 while (true)
10618 /* Parse an enumerator-definition. */
10619 cp_parser_enumerator_definition (parser, type);
10621 /* If the next token is not a ',', we've reached the end of
10622 the list. */
10623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10624 break;
10625 /* Otherwise, consume the `,' and keep going. */
10626 cp_lexer_consume_token (parser->lexer);
10627 /* If the next token is a `}', there is a trailing comma. */
10628 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10630 if (pedantic && !in_system_header)
10631 pedwarn ("comma at end of enumerator list");
10632 break;
10637 /* Parse an enumerator-definition. The enumerator has the indicated
10638 TYPE.
10640 enumerator-definition:
10641 enumerator
10642 enumerator = constant-expression
10644 enumerator:
10645 identifier */
10647 static void
10648 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10650 tree identifier;
10651 tree value;
10653 /* Look for the identifier. */
10654 identifier = cp_parser_identifier (parser);
10655 if (identifier == error_mark_node)
10656 return;
10658 /* If the next token is an '=', then there is an explicit value. */
10659 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10661 /* Consume the `=' token. */
10662 cp_lexer_consume_token (parser->lexer);
10663 /* Parse the value. */
10664 value = cp_parser_constant_expression (parser,
10665 /*allow_non_constant_p=*/false,
10666 NULL);
10668 else
10669 value = NULL_TREE;
10671 /* Create the enumerator. */
10672 build_enumerator (identifier, value, type);
10675 /* Parse a namespace-name.
10677 namespace-name:
10678 original-namespace-name
10679 namespace-alias
10681 Returns the NAMESPACE_DECL for the namespace. */
10683 static tree
10684 cp_parser_namespace_name (cp_parser* parser)
10686 tree identifier;
10687 tree namespace_decl;
10689 /* Get the name of the namespace. */
10690 identifier = cp_parser_identifier (parser);
10691 if (identifier == error_mark_node)
10692 return error_mark_node;
10694 /* Look up the identifier in the currently active scope. Look only
10695 for namespaces, due to:
10697 [basic.lookup.udir]
10699 When looking up a namespace-name in a using-directive or alias
10700 definition, only namespace names are considered.
10702 And:
10704 [basic.lookup.qual]
10706 During the lookup of a name preceding the :: scope resolution
10707 operator, object, function, and enumerator names are ignored.
10709 (Note that cp_parser_class_or_namespace_name only calls this
10710 function if the token after the name is the scope resolution
10711 operator.) */
10712 namespace_decl = cp_parser_lookup_name (parser, identifier,
10713 none_type,
10714 /*is_template=*/false,
10715 /*is_namespace=*/true,
10716 /*check_dependency=*/true,
10717 /*ambiguous_decls=*/NULL);
10718 /* If it's not a namespace, issue an error. */
10719 if (namespace_decl == error_mark_node
10720 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10722 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10723 error ("%qD is not a namespace-name", identifier);
10724 cp_parser_error (parser, "expected namespace-name");
10725 namespace_decl = error_mark_node;
10728 return namespace_decl;
10731 /* Parse a namespace-definition.
10733 namespace-definition:
10734 named-namespace-definition
10735 unnamed-namespace-definition
10737 named-namespace-definition:
10738 original-namespace-definition
10739 extension-namespace-definition
10741 original-namespace-definition:
10742 namespace identifier { namespace-body }
10744 extension-namespace-definition:
10745 namespace original-namespace-name { namespace-body }
10747 unnamed-namespace-definition:
10748 namespace { namespace-body } */
10750 static void
10751 cp_parser_namespace_definition (cp_parser* parser)
10753 tree identifier, attribs;
10755 /* Look for the `namespace' keyword. */
10756 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10758 /* Get the name of the namespace. We do not attempt to distinguish
10759 between an original-namespace-definition and an
10760 extension-namespace-definition at this point. The semantic
10761 analysis routines are responsible for that. */
10762 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10763 identifier = cp_parser_identifier (parser);
10764 else
10765 identifier = NULL_TREE;
10767 /* Parse any specified attributes. */
10768 attribs = cp_parser_attributes_opt (parser);
10770 /* Look for the `{' to start the namespace. */
10771 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10772 /* Start the namespace. */
10773 push_namespace_with_attribs (identifier, attribs);
10774 /* Parse the body of the namespace. */
10775 cp_parser_namespace_body (parser);
10776 /* Finish the namespace. */
10777 pop_namespace ();
10778 /* Look for the final `}'. */
10779 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10782 /* Parse a namespace-body.
10784 namespace-body:
10785 declaration-seq [opt] */
10787 static void
10788 cp_parser_namespace_body (cp_parser* parser)
10790 cp_parser_declaration_seq_opt (parser);
10793 /* Parse a namespace-alias-definition.
10795 namespace-alias-definition:
10796 namespace identifier = qualified-namespace-specifier ; */
10798 static void
10799 cp_parser_namespace_alias_definition (cp_parser* parser)
10801 tree identifier;
10802 tree namespace_specifier;
10804 /* Look for the `namespace' keyword. */
10805 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10806 /* Look for the identifier. */
10807 identifier = cp_parser_identifier (parser);
10808 if (identifier == error_mark_node)
10809 return;
10810 /* Look for the `=' token. */
10811 cp_parser_require (parser, CPP_EQ, "`='");
10812 /* Look for the qualified-namespace-specifier. */
10813 namespace_specifier
10814 = cp_parser_qualified_namespace_specifier (parser);
10815 /* Look for the `;' token. */
10816 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10818 /* Register the alias in the symbol table. */
10819 do_namespace_alias (identifier, namespace_specifier);
10822 /* Parse a qualified-namespace-specifier.
10824 qualified-namespace-specifier:
10825 :: [opt] nested-name-specifier [opt] namespace-name
10827 Returns a NAMESPACE_DECL corresponding to the specified
10828 namespace. */
10830 static tree
10831 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10833 /* Look for the optional `::'. */
10834 cp_parser_global_scope_opt (parser,
10835 /*current_scope_valid_p=*/false);
10837 /* Look for the optional nested-name-specifier. */
10838 cp_parser_nested_name_specifier_opt (parser,
10839 /*typename_keyword_p=*/false,
10840 /*check_dependency_p=*/true,
10841 /*type_p=*/false,
10842 /*is_declaration=*/true);
10844 return cp_parser_namespace_name (parser);
10847 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10848 access declaration.
10850 using-declaration:
10851 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10852 using :: unqualified-id ;
10854 access-declaration:
10855 qualified-id ;
10859 static bool
10860 cp_parser_using_declaration (cp_parser* parser,
10861 bool access_declaration_p)
10863 cp_token *token;
10864 bool typename_p = false;
10865 bool global_scope_p;
10866 tree decl;
10867 tree identifier;
10868 tree qscope;
10870 if (access_declaration_p)
10871 cp_parser_parse_tentatively (parser);
10872 else
10874 /* Look for the `using' keyword. */
10875 cp_parser_require_keyword (parser, RID_USING, "`using'");
10877 /* Peek at the next token. */
10878 token = cp_lexer_peek_token (parser->lexer);
10879 /* See if it's `typename'. */
10880 if (token->keyword == RID_TYPENAME)
10882 /* Remember that we've seen it. */
10883 typename_p = true;
10884 /* Consume the `typename' token. */
10885 cp_lexer_consume_token (parser->lexer);
10889 /* Look for the optional global scope qualification. */
10890 global_scope_p
10891 = (cp_parser_global_scope_opt (parser,
10892 /*current_scope_valid_p=*/false)
10893 != NULL_TREE);
10895 /* If we saw `typename', or didn't see `::', then there must be a
10896 nested-name-specifier present. */
10897 if (typename_p || !global_scope_p)
10898 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10899 /*check_dependency_p=*/true,
10900 /*type_p=*/false,
10901 /*is_declaration=*/true);
10902 /* Otherwise, we could be in either of the two productions. In that
10903 case, treat the nested-name-specifier as optional. */
10904 else
10905 qscope = cp_parser_nested_name_specifier_opt (parser,
10906 /*typename_keyword_p=*/false,
10907 /*check_dependency_p=*/true,
10908 /*type_p=*/false,
10909 /*is_declaration=*/true);
10910 if (!qscope)
10911 qscope = global_namespace;
10913 if (access_declaration_p && cp_parser_error_occurred (parser))
10914 /* Something has already gone wrong; there's no need to parse
10915 further. Since an error has occurred, the return value of
10916 cp_parser_parse_definitely will be false, as required. */
10917 return cp_parser_parse_definitely (parser);
10919 /* Parse the unqualified-id. */
10920 identifier = cp_parser_unqualified_id (parser,
10921 /*template_keyword_p=*/false,
10922 /*check_dependency_p=*/true,
10923 /*declarator_p=*/true,
10924 /*optional_p=*/false);
10926 if (access_declaration_p)
10928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10929 cp_parser_simulate_error (parser);
10930 if (!cp_parser_parse_definitely (parser))
10931 return false;
10934 /* The function we call to handle a using-declaration is different
10935 depending on what scope we are in. */
10936 if (qscope == error_mark_node || identifier == error_mark_node)
10938 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10939 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10940 /* [namespace.udecl]
10942 A using declaration shall not name a template-id. */
10943 error ("a template-id may not appear in a using-declaration");
10944 else
10946 if (at_class_scope_p ())
10948 /* Create the USING_DECL. */
10949 decl = do_class_using_decl (parser->scope, identifier);
10950 /* Add it to the list of members in this class. */
10951 finish_member_declaration (decl);
10953 else
10955 decl = cp_parser_lookup_name_simple (parser, identifier);
10956 if (decl == error_mark_node)
10957 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10958 else if (!at_namespace_scope_p ())
10959 do_local_using_decl (decl, qscope, identifier);
10960 else
10961 do_toplevel_using_decl (decl, qscope, identifier);
10965 /* Look for the final `;'. */
10966 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10968 return true;
10971 /* Parse a using-directive.
10973 using-directive:
10974 using namespace :: [opt] nested-name-specifier [opt]
10975 namespace-name ; */
10977 static void
10978 cp_parser_using_directive (cp_parser* parser)
10980 tree namespace_decl;
10981 tree attribs;
10983 /* Look for the `using' keyword. */
10984 cp_parser_require_keyword (parser, RID_USING, "`using'");
10985 /* And the `namespace' keyword. */
10986 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10987 /* Look for the optional `::' operator. */
10988 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10989 /* And the optional nested-name-specifier. */
10990 cp_parser_nested_name_specifier_opt (parser,
10991 /*typename_keyword_p=*/false,
10992 /*check_dependency_p=*/true,
10993 /*type_p=*/false,
10994 /*is_declaration=*/true);
10995 /* Get the namespace being used. */
10996 namespace_decl = cp_parser_namespace_name (parser);
10997 /* And any specified attributes. */
10998 attribs = cp_parser_attributes_opt (parser);
10999 /* Update the symbol table. */
11000 parse_using_directive (namespace_decl, attribs);
11001 /* Look for the final `;'. */
11002 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11005 /* Parse an asm-definition.
11007 asm-definition:
11008 asm ( string-literal ) ;
11010 GNU Extension:
11012 asm-definition:
11013 asm volatile [opt] ( string-literal ) ;
11014 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11015 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11016 : asm-operand-list [opt] ) ;
11017 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11018 : asm-operand-list [opt]
11019 : asm-operand-list [opt] ) ; */
11021 static void
11022 cp_parser_asm_definition (cp_parser* parser)
11024 tree string;
11025 tree outputs = NULL_TREE;
11026 tree inputs = NULL_TREE;
11027 tree clobbers = NULL_TREE;
11028 tree asm_stmt;
11029 bool volatile_p = false;
11030 bool extended_p = false;
11032 /* Look for the `asm' keyword. */
11033 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11034 /* See if the next token is `volatile'. */
11035 if (cp_parser_allow_gnu_extensions_p (parser)
11036 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11038 /* Remember that we saw the `volatile' keyword. */
11039 volatile_p = true;
11040 /* Consume the token. */
11041 cp_lexer_consume_token (parser->lexer);
11043 /* Look for the opening `('. */
11044 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11045 return;
11046 /* Look for the string. */
11047 string = cp_parser_string_literal (parser, false, false);
11048 if (string == error_mark_node)
11050 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11051 /*consume_paren=*/true);
11052 return;
11055 /* If we're allowing GNU extensions, check for the extended assembly
11056 syntax. Unfortunately, the `:' tokens need not be separated by
11057 a space in C, and so, for compatibility, we tolerate that here
11058 too. Doing that means that we have to treat the `::' operator as
11059 two `:' tokens. */
11060 if (cp_parser_allow_gnu_extensions_p (parser)
11061 && parser->in_function_body
11062 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11063 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11065 bool inputs_p = false;
11066 bool clobbers_p = false;
11068 /* The extended syntax was used. */
11069 extended_p = true;
11071 /* Look for outputs. */
11072 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11074 /* Consume the `:'. */
11075 cp_lexer_consume_token (parser->lexer);
11076 /* Parse the output-operands. */
11077 if (cp_lexer_next_token_is_not (parser->lexer,
11078 CPP_COLON)
11079 && cp_lexer_next_token_is_not (parser->lexer,
11080 CPP_SCOPE)
11081 && cp_lexer_next_token_is_not (parser->lexer,
11082 CPP_CLOSE_PAREN))
11083 outputs = cp_parser_asm_operand_list (parser);
11085 /* If the next token is `::', there are no outputs, and the
11086 next token is the beginning of the inputs. */
11087 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11088 /* The inputs are coming next. */
11089 inputs_p = true;
11091 /* Look for inputs. */
11092 if (inputs_p
11093 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11095 /* Consume the `:' or `::'. */
11096 cp_lexer_consume_token (parser->lexer);
11097 /* Parse the output-operands. */
11098 if (cp_lexer_next_token_is_not (parser->lexer,
11099 CPP_COLON)
11100 && cp_lexer_next_token_is_not (parser->lexer,
11101 CPP_CLOSE_PAREN))
11102 inputs = cp_parser_asm_operand_list (parser);
11104 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11105 /* The clobbers are coming next. */
11106 clobbers_p = true;
11108 /* Look for clobbers. */
11109 if (clobbers_p
11110 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11112 /* Consume the `:' or `::'. */
11113 cp_lexer_consume_token (parser->lexer);
11114 /* Parse the clobbers. */
11115 if (cp_lexer_next_token_is_not (parser->lexer,
11116 CPP_CLOSE_PAREN))
11117 clobbers = cp_parser_asm_clobber_list (parser);
11120 /* Look for the closing `)'. */
11121 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11122 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11123 /*consume_paren=*/true);
11124 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11126 /* Create the ASM_EXPR. */
11127 if (parser->in_function_body)
11129 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11130 inputs, clobbers);
11131 /* If the extended syntax was not used, mark the ASM_EXPR. */
11132 if (!extended_p)
11134 tree temp = asm_stmt;
11135 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11136 temp = TREE_OPERAND (temp, 0);
11138 ASM_INPUT_P (temp) = 1;
11141 else
11142 cgraph_add_asm_node (string);
11145 /* Declarators [gram.dcl.decl] */
11147 /* Parse an init-declarator.
11149 init-declarator:
11150 declarator initializer [opt]
11152 GNU Extension:
11154 init-declarator:
11155 declarator asm-specification [opt] attributes [opt] initializer [opt]
11157 function-definition:
11158 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11159 function-body
11160 decl-specifier-seq [opt] declarator function-try-block
11162 GNU Extension:
11164 function-definition:
11165 __extension__ function-definition
11167 The DECL_SPECIFIERS apply to this declarator. Returns a
11168 representation of the entity declared. If MEMBER_P is TRUE, then
11169 this declarator appears in a class scope. The new DECL created by
11170 this declarator is returned.
11172 The CHECKS are access checks that should be performed once we know
11173 what entity is being declared (and, therefore, what classes have
11174 befriended it).
11176 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11177 for a function-definition here as well. If the declarator is a
11178 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11179 be TRUE upon return. By that point, the function-definition will
11180 have been completely parsed.
11182 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11183 is FALSE. */
11185 static tree
11186 cp_parser_init_declarator (cp_parser* parser,
11187 cp_decl_specifier_seq *decl_specifiers,
11188 VEC (deferred_access_check,gc)* checks,
11189 bool function_definition_allowed_p,
11190 bool member_p,
11191 int declares_class_or_enum,
11192 bool* function_definition_p)
11194 cp_token *token;
11195 cp_declarator *declarator;
11196 tree prefix_attributes;
11197 tree attributes;
11198 tree asm_specification;
11199 tree initializer;
11200 tree decl = NULL_TREE;
11201 tree scope;
11202 bool is_initialized;
11203 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11204 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11205 "(...)". */
11206 enum cpp_ttype initialization_kind;
11207 bool is_parenthesized_init = false;
11208 bool is_non_constant_init;
11209 int ctor_dtor_or_conv_p;
11210 bool friend_p;
11211 tree pushed_scope = NULL;
11213 /* Gather the attributes that were provided with the
11214 decl-specifiers. */
11215 prefix_attributes = decl_specifiers->attributes;
11217 /* Assume that this is not the declarator for a function
11218 definition. */
11219 if (function_definition_p)
11220 *function_definition_p = false;
11222 /* Defer access checks while parsing the declarator; we cannot know
11223 what names are accessible until we know what is being
11224 declared. */
11225 resume_deferring_access_checks ();
11227 /* Parse the declarator. */
11228 declarator
11229 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11230 &ctor_dtor_or_conv_p,
11231 /*parenthesized_p=*/NULL,
11232 /*member_p=*/false);
11233 /* Gather up the deferred checks. */
11234 stop_deferring_access_checks ();
11236 /* If the DECLARATOR was erroneous, there's no need to go
11237 further. */
11238 if (declarator == cp_error_declarator)
11239 return error_mark_node;
11241 /* Check that the number of template-parameter-lists is OK. */
11242 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11243 return error_mark_node;
11245 if (declares_class_or_enum & 2)
11246 cp_parser_check_for_definition_in_return_type (declarator,
11247 decl_specifiers->type);
11249 /* Figure out what scope the entity declared by the DECLARATOR is
11250 located in. `grokdeclarator' sometimes changes the scope, so
11251 we compute it now. */
11252 scope = get_scope_of_declarator (declarator);
11254 /* If we're allowing GNU extensions, look for an asm-specification
11255 and attributes. */
11256 if (cp_parser_allow_gnu_extensions_p (parser))
11258 /* Look for an asm-specification. */
11259 asm_specification = cp_parser_asm_specification_opt (parser);
11260 /* And attributes. */
11261 attributes = cp_parser_attributes_opt (parser);
11263 else
11265 asm_specification = NULL_TREE;
11266 attributes = NULL_TREE;
11269 /* Peek at the next token. */
11270 token = cp_lexer_peek_token (parser->lexer);
11271 /* Check to see if the token indicates the start of a
11272 function-definition. */
11273 if (cp_parser_token_starts_function_definition_p (token))
11275 if (!function_definition_allowed_p)
11277 /* If a function-definition should not appear here, issue an
11278 error message. */
11279 cp_parser_error (parser,
11280 "a function-definition is not allowed here");
11281 return error_mark_node;
11283 else
11285 /* Neither attributes nor an asm-specification are allowed
11286 on a function-definition. */
11287 if (asm_specification)
11288 error ("an asm-specification is not allowed on a function-definition");
11289 if (attributes)
11290 error ("attributes are not allowed on a function-definition");
11291 /* This is a function-definition. */
11292 *function_definition_p = true;
11294 /* Parse the function definition. */
11295 if (member_p)
11296 decl = cp_parser_save_member_function_body (parser,
11297 decl_specifiers,
11298 declarator,
11299 prefix_attributes);
11300 else
11301 decl
11302 = (cp_parser_function_definition_from_specifiers_and_declarator
11303 (parser, decl_specifiers, prefix_attributes, declarator));
11305 return decl;
11309 /* [dcl.dcl]
11311 Only in function declarations for constructors, destructors, and
11312 type conversions can the decl-specifier-seq be omitted.
11314 We explicitly postpone this check past the point where we handle
11315 function-definitions because we tolerate function-definitions
11316 that are missing their return types in some modes. */
11317 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11319 cp_parser_error (parser,
11320 "expected constructor, destructor, or type conversion");
11321 return error_mark_node;
11324 /* An `=' or an `(' indicates an initializer. */
11325 if (token->type == CPP_EQ
11326 || token->type == CPP_OPEN_PAREN)
11328 is_initialized = true;
11329 initialization_kind = token->type;
11331 else
11333 /* If the init-declarator isn't initialized and isn't followed by a
11334 `,' or `;', it's not a valid init-declarator. */
11335 if (token->type != CPP_COMMA
11336 && token->type != CPP_SEMICOLON)
11338 cp_parser_error (parser, "expected initializer");
11339 return error_mark_node;
11341 is_initialized = false;
11342 initialization_kind = CPP_EOF;
11345 /* Because start_decl has side-effects, we should only call it if we
11346 know we're going ahead. By this point, we know that we cannot
11347 possibly be looking at any other construct. */
11348 cp_parser_commit_to_tentative_parse (parser);
11350 /* If the decl specifiers were bad, issue an error now that we're
11351 sure this was intended to be a declarator. Then continue
11352 declaring the variable(s), as int, to try to cut down on further
11353 errors. */
11354 if (decl_specifiers->any_specifiers_p
11355 && decl_specifiers->type == error_mark_node)
11357 cp_parser_error (parser, "invalid type in declaration");
11358 decl_specifiers->type = integer_type_node;
11361 /* Check to see whether or not this declaration is a friend. */
11362 friend_p = cp_parser_friend_p (decl_specifiers);
11364 /* Enter the newly declared entry in the symbol table. If we're
11365 processing a declaration in a class-specifier, we wait until
11366 after processing the initializer. */
11367 if (!member_p)
11369 if (parser->in_unbraced_linkage_specification_p)
11370 decl_specifiers->storage_class = sc_extern;
11371 decl = start_decl (declarator, decl_specifiers,
11372 is_initialized, attributes, prefix_attributes,
11373 &pushed_scope);
11375 else if (scope)
11376 /* Enter the SCOPE. That way unqualified names appearing in the
11377 initializer will be looked up in SCOPE. */
11378 pushed_scope = push_scope (scope);
11380 /* Perform deferred access control checks, now that we know in which
11381 SCOPE the declared entity resides. */
11382 if (!member_p && decl)
11384 tree saved_current_function_decl = NULL_TREE;
11386 /* If the entity being declared is a function, pretend that we
11387 are in its scope. If it is a `friend', it may have access to
11388 things that would not otherwise be accessible. */
11389 if (TREE_CODE (decl) == FUNCTION_DECL)
11391 saved_current_function_decl = current_function_decl;
11392 current_function_decl = decl;
11395 /* Perform access checks for template parameters. */
11396 cp_parser_perform_template_parameter_access_checks (checks);
11398 /* Perform the access control checks for the declarator and the
11399 the decl-specifiers. */
11400 perform_deferred_access_checks ();
11402 /* Restore the saved value. */
11403 if (TREE_CODE (decl) == FUNCTION_DECL)
11404 current_function_decl = saved_current_function_decl;
11407 /* Parse the initializer. */
11408 initializer = NULL_TREE;
11409 is_parenthesized_init = false;
11410 is_non_constant_init = true;
11411 if (is_initialized)
11413 if (function_declarator_p (declarator))
11415 if (initialization_kind == CPP_EQ)
11416 initializer = cp_parser_pure_specifier (parser);
11417 else
11419 /* If the declaration was erroneous, we don't really
11420 know what the user intended, so just silently
11421 consume the initializer. */
11422 if (decl != error_mark_node)
11423 error ("initializer provided for function");
11424 cp_parser_skip_to_closing_parenthesis (parser,
11425 /*recovering=*/true,
11426 /*or_comma=*/false,
11427 /*consume_paren=*/true);
11430 else
11431 initializer = cp_parser_initializer (parser,
11432 &is_parenthesized_init,
11433 &is_non_constant_init);
11436 /* The old parser allows attributes to appear after a parenthesized
11437 initializer. Mark Mitchell proposed removing this functionality
11438 on the GCC mailing lists on 2002-08-13. This parser accepts the
11439 attributes -- but ignores them. */
11440 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11441 if (cp_parser_attributes_opt (parser))
11442 warning (OPT_Wattributes,
11443 "attributes after parenthesized initializer ignored");
11445 /* For an in-class declaration, use `grokfield' to create the
11446 declaration. */
11447 if (member_p)
11449 if (pushed_scope)
11451 pop_scope (pushed_scope);
11452 pushed_scope = false;
11454 decl = grokfield (declarator, decl_specifiers,
11455 initializer, !is_non_constant_init,
11456 /*asmspec=*/NULL_TREE,
11457 prefix_attributes);
11458 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11459 cp_parser_save_default_args (parser, decl);
11462 /* Finish processing the declaration. But, skip friend
11463 declarations. */
11464 if (!friend_p && decl && decl != error_mark_node)
11466 cp_finish_decl (decl,
11467 initializer, !is_non_constant_init,
11468 asm_specification,
11469 /* If the initializer is in parentheses, then this is
11470 a direct-initialization, which means that an
11471 `explicit' constructor is OK. Otherwise, an
11472 `explicit' constructor cannot be used. */
11473 ((is_parenthesized_init || !is_initialized)
11474 ? 0 : LOOKUP_ONLYCONVERTING));
11476 if (!friend_p && pushed_scope)
11477 pop_scope (pushed_scope);
11479 return decl;
11482 /* Parse a declarator.
11484 declarator:
11485 direct-declarator
11486 ptr-operator declarator
11488 abstract-declarator:
11489 ptr-operator abstract-declarator [opt]
11490 direct-abstract-declarator
11492 GNU Extensions:
11494 declarator:
11495 attributes [opt] direct-declarator
11496 attributes [opt] ptr-operator declarator
11498 abstract-declarator:
11499 attributes [opt] ptr-operator abstract-declarator [opt]
11500 attributes [opt] direct-abstract-declarator
11502 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11503 detect constructor, destructor or conversion operators. It is set
11504 to -1 if the declarator is a name, and +1 if it is a
11505 function. Otherwise it is set to zero. Usually you just want to
11506 test for >0, but internally the negative value is used.
11508 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11509 a decl-specifier-seq unless it declares a constructor, destructor,
11510 or conversion. It might seem that we could check this condition in
11511 semantic analysis, rather than parsing, but that makes it difficult
11512 to handle something like `f()'. We want to notice that there are
11513 no decl-specifiers, and therefore realize that this is an
11514 expression, not a declaration.)
11516 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11517 the declarator is a direct-declarator of the form "(...)".
11519 MEMBER_P is true iff this declarator is a member-declarator. */
11521 static cp_declarator *
11522 cp_parser_declarator (cp_parser* parser,
11523 cp_parser_declarator_kind dcl_kind,
11524 int* ctor_dtor_or_conv_p,
11525 bool* parenthesized_p,
11526 bool member_p)
11528 cp_token *token;
11529 cp_declarator *declarator;
11530 enum tree_code code;
11531 cp_cv_quals cv_quals;
11532 tree class_type;
11533 tree attributes = NULL_TREE;
11535 /* Assume this is not a constructor, destructor, or type-conversion
11536 operator. */
11537 if (ctor_dtor_or_conv_p)
11538 *ctor_dtor_or_conv_p = 0;
11540 if (cp_parser_allow_gnu_extensions_p (parser))
11541 attributes = cp_parser_attributes_opt (parser);
11543 /* Peek at the next token. */
11544 token = cp_lexer_peek_token (parser->lexer);
11546 /* Check for the ptr-operator production. */
11547 cp_parser_parse_tentatively (parser);
11548 /* Parse the ptr-operator. */
11549 code = cp_parser_ptr_operator (parser,
11550 &class_type,
11551 &cv_quals);
11552 /* If that worked, then we have a ptr-operator. */
11553 if (cp_parser_parse_definitely (parser))
11555 /* If a ptr-operator was found, then this declarator was not
11556 parenthesized. */
11557 if (parenthesized_p)
11558 *parenthesized_p = true;
11559 /* The dependent declarator is optional if we are parsing an
11560 abstract-declarator. */
11561 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11562 cp_parser_parse_tentatively (parser);
11564 /* Parse the dependent declarator. */
11565 declarator = cp_parser_declarator (parser, dcl_kind,
11566 /*ctor_dtor_or_conv_p=*/NULL,
11567 /*parenthesized_p=*/NULL,
11568 /*member_p=*/false);
11570 /* If we are parsing an abstract-declarator, we must handle the
11571 case where the dependent declarator is absent. */
11572 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11573 && !cp_parser_parse_definitely (parser))
11574 declarator = NULL;
11576 /* Build the representation of the ptr-operator. */
11577 if (class_type)
11578 declarator = make_ptrmem_declarator (cv_quals,
11579 class_type,
11580 declarator);
11581 else if (code == INDIRECT_REF)
11582 declarator = make_pointer_declarator (cv_quals, declarator);
11583 else
11584 declarator = make_reference_declarator (cv_quals, declarator);
11586 /* Everything else is a direct-declarator. */
11587 else
11589 if (parenthesized_p)
11590 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11591 CPP_OPEN_PAREN);
11592 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11593 ctor_dtor_or_conv_p,
11594 member_p);
11597 if (attributes && declarator && declarator != cp_error_declarator)
11598 declarator->attributes = attributes;
11600 return declarator;
11603 /* Parse a direct-declarator or direct-abstract-declarator.
11605 direct-declarator:
11606 declarator-id
11607 direct-declarator ( parameter-declaration-clause )
11608 cv-qualifier-seq [opt]
11609 exception-specification [opt]
11610 direct-declarator [ constant-expression [opt] ]
11611 ( declarator )
11613 direct-abstract-declarator:
11614 direct-abstract-declarator [opt]
11615 ( parameter-declaration-clause )
11616 cv-qualifier-seq [opt]
11617 exception-specification [opt]
11618 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11619 ( abstract-declarator )
11621 Returns a representation of the declarator. DCL_KIND is
11622 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11623 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11624 we are parsing a direct-declarator. It is
11625 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11626 of ambiguity we prefer an abstract declarator, as per
11627 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11628 cp_parser_declarator. */
11630 static cp_declarator *
11631 cp_parser_direct_declarator (cp_parser* parser,
11632 cp_parser_declarator_kind dcl_kind,
11633 int* ctor_dtor_or_conv_p,
11634 bool member_p)
11636 cp_token *token;
11637 cp_declarator *declarator = NULL;
11638 tree scope = NULL_TREE;
11639 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11640 bool saved_in_declarator_p = parser->in_declarator_p;
11641 bool first = true;
11642 tree pushed_scope = NULL_TREE;
11644 while (true)
11646 /* Peek at the next token. */
11647 token = cp_lexer_peek_token (parser->lexer);
11648 if (token->type == CPP_OPEN_PAREN)
11650 /* This is either a parameter-declaration-clause, or a
11651 parenthesized declarator. When we know we are parsing a
11652 named declarator, it must be a parenthesized declarator
11653 if FIRST is true. For instance, `(int)' is a
11654 parameter-declaration-clause, with an omitted
11655 direct-abstract-declarator. But `((*))', is a
11656 parenthesized abstract declarator. Finally, when T is a
11657 template parameter `(T)' is a
11658 parameter-declaration-clause, and not a parenthesized
11659 named declarator.
11661 We first try and parse a parameter-declaration-clause,
11662 and then try a nested declarator (if FIRST is true).
11664 It is not an error for it not to be a
11665 parameter-declaration-clause, even when FIRST is
11666 false. Consider,
11668 int i (int);
11669 int i (3);
11671 The first is the declaration of a function while the
11672 second is a the definition of a variable, including its
11673 initializer.
11675 Having seen only the parenthesis, we cannot know which of
11676 these two alternatives should be selected. Even more
11677 complex are examples like:
11679 int i (int (a));
11680 int i (int (3));
11682 The former is a function-declaration; the latter is a
11683 variable initialization.
11685 Thus again, we try a parameter-declaration-clause, and if
11686 that fails, we back out and return. */
11688 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11690 cp_parameter_declarator *params;
11691 unsigned saved_num_template_parameter_lists;
11693 /* In a member-declarator, the only valid interpretation
11694 of a parenthesis is the start of a
11695 parameter-declaration-clause. (It is invalid to
11696 initialize a static data member with a parenthesized
11697 initializer; only the "=" form of initialization is
11698 permitted.) */
11699 if (!member_p)
11700 cp_parser_parse_tentatively (parser);
11702 /* Consume the `('. */
11703 cp_lexer_consume_token (parser->lexer);
11704 if (first)
11706 /* If this is going to be an abstract declarator, we're
11707 in a declarator and we can't have default args. */
11708 parser->default_arg_ok_p = false;
11709 parser->in_declarator_p = true;
11712 /* Inside the function parameter list, surrounding
11713 template-parameter-lists do not apply. */
11714 saved_num_template_parameter_lists
11715 = parser->num_template_parameter_lists;
11716 parser->num_template_parameter_lists = 0;
11718 /* Parse the parameter-declaration-clause. */
11719 params = cp_parser_parameter_declaration_clause (parser);
11721 parser->num_template_parameter_lists
11722 = saved_num_template_parameter_lists;
11724 /* If all went well, parse the cv-qualifier-seq and the
11725 exception-specification. */
11726 if (member_p || cp_parser_parse_definitely (parser))
11728 cp_cv_quals cv_quals;
11729 tree exception_specification;
11731 if (ctor_dtor_or_conv_p)
11732 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11733 first = false;
11734 /* Consume the `)'. */
11735 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11737 /* Parse the cv-qualifier-seq. */
11738 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11739 /* And the exception-specification. */
11740 exception_specification
11741 = cp_parser_exception_specification_opt (parser);
11743 /* Create the function-declarator. */
11744 declarator = make_call_declarator (declarator,
11745 params,
11746 cv_quals,
11747 exception_specification);
11748 /* Any subsequent parameter lists are to do with
11749 return type, so are not those of the declared
11750 function. */
11751 parser->default_arg_ok_p = false;
11753 /* Repeat the main loop. */
11754 continue;
11758 /* If this is the first, we can try a parenthesized
11759 declarator. */
11760 if (first)
11762 bool saved_in_type_id_in_expr_p;
11764 parser->default_arg_ok_p = saved_default_arg_ok_p;
11765 parser->in_declarator_p = saved_in_declarator_p;
11767 /* Consume the `('. */
11768 cp_lexer_consume_token (parser->lexer);
11769 /* Parse the nested declarator. */
11770 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11771 parser->in_type_id_in_expr_p = true;
11772 declarator
11773 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11774 /*parenthesized_p=*/NULL,
11775 member_p);
11776 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11777 first = false;
11778 /* Expect a `)'. */
11779 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11780 declarator = cp_error_declarator;
11781 if (declarator == cp_error_declarator)
11782 break;
11784 goto handle_declarator;
11786 /* Otherwise, we must be done. */
11787 else
11788 break;
11790 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11791 && token->type == CPP_OPEN_SQUARE)
11793 /* Parse an array-declarator. */
11794 tree bounds;
11796 if (ctor_dtor_or_conv_p)
11797 *ctor_dtor_or_conv_p = 0;
11799 first = false;
11800 parser->default_arg_ok_p = false;
11801 parser->in_declarator_p = true;
11802 /* Consume the `['. */
11803 cp_lexer_consume_token (parser->lexer);
11804 /* Peek at the next token. */
11805 token = cp_lexer_peek_token (parser->lexer);
11806 /* If the next token is `]', then there is no
11807 constant-expression. */
11808 if (token->type != CPP_CLOSE_SQUARE)
11810 bool non_constant_p;
11812 bounds
11813 = cp_parser_constant_expression (parser,
11814 /*allow_non_constant=*/true,
11815 &non_constant_p);
11816 if (!non_constant_p)
11817 bounds = fold_non_dependent_expr (bounds);
11818 /* Normally, the array bound must be an integral constant
11819 expression. However, as an extension, we allow VLAs
11820 in function scopes. */
11821 else if (!parser->in_function_body)
11823 error ("array bound is not an integer constant");
11824 bounds = error_mark_node;
11827 else
11828 bounds = NULL_TREE;
11829 /* Look for the closing `]'. */
11830 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11832 declarator = cp_error_declarator;
11833 break;
11836 declarator = make_array_declarator (declarator, bounds);
11838 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11840 tree qualifying_scope;
11841 tree unqualified_name;
11842 special_function_kind sfk;
11843 bool abstract_ok;
11845 /* Parse a declarator-id */
11846 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11847 if (abstract_ok)
11848 cp_parser_parse_tentatively (parser);
11849 unqualified_name
11850 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11851 qualifying_scope = parser->scope;
11852 if (abstract_ok)
11854 if (!cp_parser_parse_definitely (parser))
11855 unqualified_name = error_mark_node;
11856 else if (unqualified_name
11857 && (qualifying_scope
11858 || (TREE_CODE (unqualified_name)
11859 != IDENTIFIER_NODE)))
11861 cp_parser_error (parser, "expected unqualified-id");
11862 unqualified_name = error_mark_node;
11866 if (!unqualified_name)
11867 return NULL;
11868 if (unqualified_name == error_mark_node)
11870 declarator = cp_error_declarator;
11871 break;
11874 if (qualifying_scope && at_namespace_scope_p ()
11875 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11877 /* In the declaration of a member of a template class
11878 outside of the class itself, the SCOPE will sometimes
11879 be a TYPENAME_TYPE. For example, given:
11881 template <typename T>
11882 int S<T>::R::i = 3;
11884 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11885 this context, we must resolve S<T>::R to an ordinary
11886 type, rather than a typename type.
11888 The reason we normally avoid resolving TYPENAME_TYPEs
11889 is that a specialization of `S' might render
11890 `S<T>::R' not a type. However, if `S' is
11891 specialized, then this `i' will not be used, so there
11892 is no harm in resolving the types here. */
11893 tree type;
11895 /* Resolve the TYPENAME_TYPE. */
11896 type = resolve_typename_type (qualifying_scope,
11897 /*only_current_p=*/false);
11898 /* If that failed, the declarator is invalid. */
11899 if (type == error_mark_node)
11900 error ("%<%T::%D%> is not a type",
11901 TYPE_CONTEXT (qualifying_scope),
11902 TYPE_IDENTIFIER (qualifying_scope));
11903 qualifying_scope = type;
11906 sfk = sfk_none;
11907 if (unqualified_name)
11909 tree class_type;
11911 if (qualifying_scope
11912 && CLASS_TYPE_P (qualifying_scope))
11913 class_type = qualifying_scope;
11914 else
11915 class_type = current_class_type;
11917 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11919 tree name_type = TREE_TYPE (unqualified_name);
11920 if (class_type && same_type_p (name_type, class_type))
11922 if (qualifying_scope
11923 && CLASSTYPE_USE_TEMPLATE (name_type))
11925 error ("invalid use of constructor as a template");
11926 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11927 "name the constructor in a qualified name",
11928 class_type,
11929 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11930 class_type, name_type);
11931 declarator = cp_error_declarator;
11932 break;
11934 else
11935 unqualified_name = constructor_name (class_type);
11937 else
11939 /* We do not attempt to print the declarator
11940 here because we do not have enough
11941 information about its original syntactic
11942 form. */
11943 cp_parser_error (parser, "invalid declarator");
11944 declarator = cp_error_declarator;
11945 break;
11949 if (class_type)
11951 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11952 sfk = sfk_destructor;
11953 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11954 sfk = sfk_conversion;
11955 else if (/* There's no way to declare a constructor
11956 for an anonymous type, even if the type
11957 got a name for linkage purposes. */
11958 !TYPE_WAS_ANONYMOUS (class_type)
11959 && constructor_name_p (unqualified_name,
11960 class_type))
11962 unqualified_name = constructor_name (class_type);
11963 sfk = sfk_constructor;
11966 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11967 *ctor_dtor_or_conv_p = -1;
11970 declarator = make_id_declarator (qualifying_scope,
11971 unqualified_name,
11972 sfk);
11973 declarator->id_loc = token->location;
11975 handle_declarator:;
11976 scope = get_scope_of_declarator (declarator);
11977 if (scope)
11978 /* Any names that appear after the declarator-id for a
11979 member are looked up in the containing scope. */
11980 pushed_scope = push_scope (scope);
11981 parser->in_declarator_p = true;
11982 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11983 || (declarator && declarator->kind == cdk_id))
11984 /* Default args are only allowed on function
11985 declarations. */
11986 parser->default_arg_ok_p = saved_default_arg_ok_p;
11987 else
11988 parser->default_arg_ok_p = false;
11990 first = false;
11992 /* We're done. */
11993 else
11994 break;
11997 /* For an abstract declarator, we might wind up with nothing at this
11998 point. That's an error; the declarator is not optional. */
11999 if (!declarator)
12000 cp_parser_error (parser, "expected declarator");
12002 /* If we entered a scope, we must exit it now. */
12003 if (pushed_scope)
12004 pop_scope (pushed_scope);
12006 parser->default_arg_ok_p = saved_default_arg_ok_p;
12007 parser->in_declarator_p = saved_in_declarator_p;
12009 return declarator;
12012 /* Parse a ptr-operator.
12014 ptr-operator:
12015 * cv-qualifier-seq [opt]
12017 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12019 GNU Extension:
12021 ptr-operator:
12022 & cv-qualifier-seq [opt]
12024 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12025 Returns ADDR_EXPR if a reference was used. In the case of a
12026 pointer-to-member, *TYPE is filled in with the TYPE containing the
12027 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
12028 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
12029 ERROR_MARK if an error occurred. */
12031 static enum tree_code
12032 cp_parser_ptr_operator (cp_parser* parser,
12033 tree* type,
12034 cp_cv_quals *cv_quals)
12036 enum tree_code code = ERROR_MARK;
12037 cp_token *token;
12039 /* Assume that it's not a pointer-to-member. */
12040 *type = NULL_TREE;
12041 /* And that there are no cv-qualifiers. */
12042 *cv_quals = TYPE_UNQUALIFIED;
12044 /* Peek at the next token. */
12045 token = cp_lexer_peek_token (parser->lexer);
12046 /* If it's a `*' or `&' we have a pointer or reference. */
12047 if (token->type == CPP_MULT || token->type == CPP_AND)
12049 /* Remember which ptr-operator we were processing. */
12050 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12052 /* Consume the `*' or `&'. */
12053 cp_lexer_consume_token (parser->lexer);
12055 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12056 `&', if we are allowing GNU extensions. (The only qualifier
12057 that can legally appear after `&' is `restrict', but that is
12058 enforced during semantic analysis. */
12059 if (code == INDIRECT_REF
12060 || cp_parser_allow_gnu_extensions_p (parser))
12061 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12063 else
12065 /* Try the pointer-to-member case. */
12066 cp_parser_parse_tentatively (parser);
12067 /* Look for the optional `::' operator. */
12068 cp_parser_global_scope_opt (parser,
12069 /*current_scope_valid_p=*/false);
12070 /* Look for the nested-name specifier. */
12071 cp_parser_nested_name_specifier (parser,
12072 /*typename_keyword_p=*/false,
12073 /*check_dependency_p=*/true,
12074 /*type_p=*/false,
12075 /*is_declaration=*/false);
12076 /* If we found it, and the next token is a `*', then we are
12077 indeed looking at a pointer-to-member operator. */
12078 if (!cp_parser_error_occurred (parser)
12079 && cp_parser_require (parser, CPP_MULT, "`*'"))
12081 /* Indicate that the `*' operator was used. */
12082 code = INDIRECT_REF;
12084 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12085 error ("%qD is a namespace", parser->scope);
12086 else
12088 /* The type of which the member is a member is given by the
12089 current SCOPE. */
12090 *type = parser->scope;
12091 /* The next name will not be qualified. */
12092 parser->scope = NULL_TREE;
12093 parser->qualifying_scope = NULL_TREE;
12094 parser->object_scope = NULL_TREE;
12095 /* Look for the optional cv-qualifier-seq. */
12096 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12099 /* If that didn't work we don't have a ptr-operator. */
12100 if (!cp_parser_parse_definitely (parser))
12101 cp_parser_error (parser, "expected ptr-operator");
12104 return code;
12107 /* Parse an (optional) cv-qualifier-seq.
12109 cv-qualifier-seq:
12110 cv-qualifier cv-qualifier-seq [opt]
12112 cv-qualifier:
12113 const
12114 volatile
12116 GNU Extension:
12118 cv-qualifier:
12119 __restrict__
12121 Returns a bitmask representing the cv-qualifiers. */
12123 static cp_cv_quals
12124 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12126 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12128 while (true)
12130 cp_token *token;
12131 cp_cv_quals cv_qualifier;
12133 /* Peek at the next token. */
12134 token = cp_lexer_peek_token (parser->lexer);
12135 /* See if it's a cv-qualifier. */
12136 switch (token->keyword)
12138 case RID_CONST:
12139 cv_qualifier = TYPE_QUAL_CONST;
12140 break;
12142 case RID_VOLATILE:
12143 cv_qualifier = TYPE_QUAL_VOLATILE;
12144 break;
12146 case RID_RESTRICT:
12147 cv_qualifier = TYPE_QUAL_RESTRICT;
12148 break;
12150 default:
12151 cv_qualifier = TYPE_UNQUALIFIED;
12152 break;
12155 if (!cv_qualifier)
12156 break;
12158 if (cv_quals & cv_qualifier)
12160 error ("duplicate cv-qualifier");
12161 cp_lexer_purge_token (parser->lexer);
12163 else
12165 cp_lexer_consume_token (parser->lexer);
12166 cv_quals |= cv_qualifier;
12170 return cv_quals;
12173 /* Parse a declarator-id.
12175 declarator-id:
12176 id-expression
12177 :: [opt] nested-name-specifier [opt] type-name
12179 In the `id-expression' case, the value returned is as for
12180 cp_parser_id_expression if the id-expression was an unqualified-id.
12181 If the id-expression was a qualified-id, then a SCOPE_REF is
12182 returned. The first operand is the scope (either a NAMESPACE_DECL
12183 or TREE_TYPE), but the second is still just a representation of an
12184 unqualified-id. */
12186 static tree
12187 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12189 tree id;
12190 /* The expression must be an id-expression. Assume that qualified
12191 names are the names of types so that:
12193 template <class T>
12194 int S<T>::R::i = 3;
12196 will work; we must treat `S<T>::R' as the name of a type.
12197 Similarly, assume that qualified names are templates, where
12198 required, so that:
12200 template <class T>
12201 int S<T>::R<T>::i = 3;
12203 will work, too. */
12204 id = cp_parser_id_expression (parser,
12205 /*template_keyword_p=*/false,
12206 /*check_dependency_p=*/false,
12207 /*template_p=*/NULL,
12208 /*declarator_p=*/true,
12209 optional_p);
12210 if (id && BASELINK_P (id))
12211 id = BASELINK_FUNCTIONS (id);
12212 return id;
12215 /* Parse a type-id.
12217 type-id:
12218 type-specifier-seq abstract-declarator [opt]
12220 Returns the TYPE specified. */
12222 static tree
12223 cp_parser_type_id (cp_parser* parser)
12225 cp_decl_specifier_seq type_specifier_seq;
12226 cp_declarator *abstract_declarator;
12228 /* Parse the type-specifier-seq. */
12229 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12230 &type_specifier_seq);
12231 if (type_specifier_seq.type == error_mark_node)
12232 return error_mark_node;
12234 /* There might or might not be an abstract declarator. */
12235 cp_parser_parse_tentatively (parser);
12236 /* Look for the declarator. */
12237 abstract_declarator
12238 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12239 /*parenthesized_p=*/NULL,
12240 /*member_p=*/false);
12241 /* Check to see if there really was a declarator. */
12242 if (!cp_parser_parse_definitely (parser))
12243 abstract_declarator = NULL;
12245 return groktypename (&type_specifier_seq, abstract_declarator);
12248 /* Parse a type-specifier-seq.
12250 type-specifier-seq:
12251 type-specifier type-specifier-seq [opt]
12253 GNU extension:
12255 type-specifier-seq:
12256 attributes type-specifier-seq [opt]
12258 If IS_CONDITION is true, we are at the start of a "condition",
12259 e.g., we've just seen "if (".
12261 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12263 static void
12264 cp_parser_type_specifier_seq (cp_parser* parser,
12265 bool is_condition,
12266 cp_decl_specifier_seq *type_specifier_seq)
12268 bool seen_type_specifier = false;
12269 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12271 /* Clear the TYPE_SPECIFIER_SEQ. */
12272 clear_decl_specs (type_specifier_seq);
12274 /* Parse the type-specifiers and attributes. */
12275 while (true)
12277 tree type_specifier;
12278 bool is_cv_qualifier;
12280 /* Check for attributes first. */
12281 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12283 type_specifier_seq->attributes =
12284 chainon (type_specifier_seq->attributes,
12285 cp_parser_attributes_opt (parser));
12286 continue;
12289 /* Look for the type-specifier. */
12290 type_specifier = cp_parser_type_specifier (parser,
12291 flags,
12292 type_specifier_seq,
12293 /*is_declaration=*/false,
12294 NULL,
12295 &is_cv_qualifier);
12296 if (!type_specifier)
12298 /* If the first type-specifier could not be found, this is not a
12299 type-specifier-seq at all. */
12300 if (!seen_type_specifier)
12302 cp_parser_error (parser, "expected type-specifier");
12303 type_specifier_seq->type = error_mark_node;
12304 return;
12306 /* If subsequent type-specifiers could not be found, the
12307 type-specifier-seq is complete. */
12308 break;
12311 seen_type_specifier = true;
12312 /* The standard says that a condition can be:
12314 type-specifier-seq declarator = assignment-expression
12316 However, given:
12318 struct S {};
12319 if (int S = ...)
12321 we should treat the "S" as a declarator, not as a
12322 type-specifier. The standard doesn't say that explicitly for
12323 type-specifier-seq, but it does say that for
12324 decl-specifier-seq in an ordinary declaration. Perhaps it
12325 would be clearer just to allow a decl-specifier-seq here, and
12326 then add a semantic restriction that if any decl-specifiers
12327 that are not type-specifiers appear, the program is invalid. */
12328 if (is_condition && !is_cv_qualifier)
12329 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12332 cp_parser_check_decl_spec (type_specifier_seq);
12335 /* Parse a parameter-declaration-clause.
12337 parameter-declaration-clause:
12338 parameter-declaration-list [opt] ... [opt]
12339 parameter-declaration-list , ...
12341 Returns a representation for the parameter declarations. A return
12342 value of NULL indicates a parameter-declaration-clause consisting
12343 only of an ellipsis. */
12345 static cp_parameter_declarator *
12346 cp_parser_parameter_declaration_clause (cp_parser* parser)
12348 cp_parameter_declarator *parameters;
12349 cp_token *token;
12350 bool ellipsis_p;
12351 bool is_error;
12353 /* Peek at the next token. */
12354 token = cp_lexer_peek_token (parser->lexer);
12355 /* Check for trivial parameter-declaration-clauses. */
12356 if (token->type == CPP_ELLIPSIS)
12358 /* Consume the `...' token. */
12359 cp_lexer_consume_token (parser->lexer);
12360 return NULL;
12362 else if (token->type == CPP_CLOSE_PAREN)
12363 /* There are no parameters. */
12365 #ifndef NO_IMPLICIT_EXTERN_C
12366 if (in_system_header && current_class_type == NULL
12367 && current_lang_name == lang_name_c)
12368 return NULL;
12369 else
12370 #endif
12371 return no_parameters;
12373 /* Check for `(void)', too, which is a special case. */
12374 else if (token->keyword == RID_VOID
12375 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12376 == CPP_CLOSE_PAREN))
12378 /* Consume the `void' token. */
12379 cp_lexer_consume_token (parser->lexer);
12380 /* There are no parameters. */
12381 return no_parameters;
12384 /* Parse the parameter-declaration-list. */
12385 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12386 /* If a parse error occurred while parsing the
12387 parameter-declaration-list, then the entire
12388 parameter-declaration-clause is erroneous. */
12389 if (is_error)
12390 return NULL;
12392 /* Peek at the next token. */
12393 token = cp_lexer_peek_token (parser->lexer);
12394 /* If it's a `,', the clause should terminate with an ellipsis. */
12395 if (token->type == CPP_COMMA)
12397 /* Consume the `,'. */
12398 cp_lexer_consume_token (parser->lexer);
12399 /* Expect an ellipsis. */
12400 ellipsis_p
12401 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12403 /* It might also be `...' if the optional trailing `,' was
12404 omitted. */
12405 else if (token->type == CPP_ELLIPSIS)
12407 /* Consume the `...' token. */
12408 cp_lexer_consume_token (parser->lexer);
12409 /* And remember that we saw it. */
12410 ellipsis_p = true;
12412 else
12413 ellipsis_p = false;
12415 /* Finish the parameter list. */
12416 if (parameters && ellipsis_p)
12417 parameters->ellipsis_p = true;
12419 return parameters;
12422 /* Parse a parameter-declaration-list.
12424 parameter-declaration-list:
12425 parameter-declaration
12426 parameter-declaration-list , parameter-declaration
12428 Returns a representation of the parameter-declaration-list, as for
12429 cp_parser_parameter_declaration_clause. However, the
12430 `void_list_node' is never appended to the list. Upon return,
12431 *IS_ERROR will be true iff an error occurred. */
12433 static cp_parameter_declarator *
12434 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12436 cp_parameter_declarator *parameters = NULL;
12437 cp_parameter_declarator **tail = &parameters;
12438 bool saved_in_unbraced_linkage_specification_p;
12440 /* Assume all will go well. */
12441 *is_error = false;
12442 /* The special considerations that apply to a function within an
12443 unbraced linkage specifications do not apply to the parameters
12444 to the function. */
12445 saved_in_unbraced_linkage_specification_p
12446 = parser->in_unbraced_linkage_specification_p;
12447 parser->in_unbraced_linkage_specification_p = false;
12449 /* Look for more parameters. */
12450 while (true)
12452 cp_parameter_declarator *parameter;
12453 bool parenthesized_p;
12454 /* Parse the parameter. */
12455 parameter
12456 = cp_parser_parameter_declaration (parser,
12457 /*template_parm_p=*/false,
12458 &parenthesized_p);
12460 /* If a parse error occurred parsing the parameter declaration,
12461 then the entire parameter-declaration-list is erroneous. */
12462 if (!parameter)
12464 *is_error = true;
12465 parameters = NULL;
12466 break;
12468 /* Add the new parameter to the list. */
12469 *tail = parameter;
12470 tail = &parameter->next;
12472 /* Peek at the next token. */
12473 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12474 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12475 /* These are for Objective-C++ */
12476 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12477 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12478 /* The parameter-declaration-list is complete. */
12479 break;
12480 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12482 cp_token *token;
12484 /* Peek at the next token. */
12485 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12486 /* If it's an ellipsis, then the list is complete. */
12487 if (token->type == CPP_ELLIPSIS)
12488 break;
12489 /* Otherwise, there must be more parameters. Consume the
12490 `,'. */
12491 cp_lexer_consume_token (parser->lexer);
12492 /* When parsing something like:
12494 int i(float f, double d)
12496 we can tell after seeing the declaration for "f" that we
12497 are not looking at an initialization of a variable "i",
12498 but rather at the declaration of a function "i".
12500 Due to the fact that the parsing of template arguments
12501 (as specified to a template-id) requires backtracking we
12502 cannot use this technique when inside a template argument
12503 list. */
12504 if (!parser->in_template_argument_list_p
12505 && !parser->in_type_id_in_expr_p
12506 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12507 /* However, a parameter-declaration of the form
12508 "foat(f)" (which is a valid declaration of a
12509 parameter "f") can also be interpreted as an
12510 expression (the conversion of "f" to "float"). */
12511 && !parenthesized_p)
12512 cp_parser_commit_to_tentative_parse (parser);
12514 else
12516 cp_parser_error (parser, "expected %<,%> or %<...%>");
12517 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12518 cp_parser_skip_to_closing_parenthesis (parser,
12519 /*recovering=*/true,
12520 /*or_comma=*/false,
12521 /*consume_paren=*/false);
12522 break;
12526 parser->in_unbraced_linkage_specification_p
12527 = saved_in_unbraced_linkage_specification_p;
12529 return parameters;
12532 /* Parse a parameter declaration.
12534 parameter-declaration:
12535 decl-specifier-seq declarator
12536 decl-specifier-seq declarator = assignment-expression
12537 decl-specifier-seq abstract-declarator [opt]
12538 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12540 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12541 declares a template parameter. (In that case, a non-nested `>'
12542 token encountered during the parsing of the assignment-expression
12543 is not interpreted as a greater-than operator.)
12545 Returns a representation of the parameter, or NULL if an error
12546 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12547 true iff the declarator is of the form "(p)". */
12549 static cp_parameter_declarator *
12550 cp_parser_parameter_declaration (cp_parser *parser,
12551 bool template_parm_p,
12552 bool *parenthesized_p)
12554 int declares_class_or_enum;
12555 bool greater_than_is_operator_p;
12556 cp_decl_specifier_seq decl_specifiers;
12557 cp_declarator *declarator;
12558 tree default_argument;
12559 cp_token *token;
12560 const char *saved_message;
12562 /* In a template parameter, `>' is not an operator.
12564 [temp.param]
12566 When parsing a default template-argument for a non-type
12567 template-parameter, the first non-nested `>' is taken as the end
12568 of the template parameter-list rather than a greater-than
12569 operator. */
12570 greater_than_is_operator_p = !template_parm_p;
12572 /* Type definitions may not appear in parameter types. */
12573 saved_message = parser->type_definition_forbidden_message;
12574 parser->type_definition_forbidden_message
12575 = "types may not be defined in parameter types";
12577 /* Parse the declaration-specifiers. */
12578 cp_parser_decl_specifier_seq (parser,
12579 CP_PARSER_FLAGS_NONE,
12580 &decl_specifiers,
12581 &declares_class_or_enum);
12582 /* If an error occurred, there's no reason to attempt to parse the
12583 rest of the declaration. */
12584 if (cp_parser_error_occurred (parser))
12586 parser->type_definition_forbidden_message = saved_message;
12587 return NULL;
12590 /* Peek at the next token. */
12591 token = cp_lexer_peek_token (parser->lexer);
12592 /* If the next token is a `)', `,', `=', `>', or `...', then there
12593 is no declarator. */
12594 if (token->type == CPP_CLOSE_PAREN
12595 || token->type == CPP_COMMA
12596 || token->type == CPP_EQ
12597 || token->type == CPP_ELLIPSIS
12598 || token->type == CPP_GREATER)
12600 declarator = NULL;
12601 if (parenthesized_p)
12602 *parenthesized_p = false;
12604 /* Otherwise, there should be a declarator. */
12605 else
12607 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12608 parser->default_arg_ok_p = false;
12610 /* After seeing a decl-specifier-seq, if the next token is not a
12611 "(", there is no possibility that the code is a valid
12612 expression. Therefore, if parsing tentatively, we commit at
12613 this point. */
12614 if (!parser->in_template_argument_list_p
12615 /* In an expression context, having seen:
12617 (int((char ...
12619 we cannot be sure whether we are looking at a
12620 function-type (taking a "char" as a parameter) or a cast
12621 of some object of type "char" to "int". */
12622 && !parser->in_type_id_in_expr_p
12623 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12624 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12625 cp_parser_commit_to_tentative_parse (parser);
12626 /* Parse the declarator. */
12627 declarator = cp_parser_declarator (parser,
12628 CP_PARSER_DECLARATOR_EITHER,
12629 /*ctor_dtor_or_conv_p=*/NULL,
12630 parenthesized_p,
12631 /*member_p=*/false);
12632 parser->default_arg_ok_p = saved_default_arg_ok_p;
12633 /* After the declarator, allow more attributes. */
12634 decl_specifiers.attributes
12635 = chainon (decl_specifiers.attributes,
12636 cp_parser_attributes_opt (parser));
12639 /* The restriction on defining new types applies only to the type
12640 of the parameter, not to the default argument. */
12641 parser->type_definition_forbidden_message = saved_message;
12643 /* If the next token is `=', then process a default argument. */
12644 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12646 bool saved_greater_than_is_operator_p;
12647 /* Consume the `='. */
12648 cp_lexer_consume_token (parser->lexer);
12650 /* If we are defining a class, then the tokens that make up the
12651 default argument must be saved and processed later. */
12652 if (!template_parm_p && at_class_scope_p ()
12653 && TYPE_BEING_DEFINED (current_class_type))
12655 unsigned depth = 0;
12656 cp_token *first_token;
12657 cp_token *token;
12659 /* Add tokens until we have processed the entire default
12660 argument. We add the range [first_token, token). */
12661 first_token = cp_lexer_peek_token (parser->lexer);
12662 while (true)
12664 bool done = false;
12666 /* Peek at the next token. */
12667 token = cp_lexer_peek_token (parser->lexer);
12668 /* What we do depends on what token we have. */
12669 switch (token->type)
12671 /* In valid code, a default argument must be
12672 immediately followed by a `,' `)', or `...'. */
12673 case CPP_COMMA:
12674 case CPP_CLOSE_PAREN:
12675 case CPP_ELLIPSIS:
12676 /* If we run into a non-nested `;', `}', or `]',
12677 then the code is invalid -- but the default
12678 argument is certainly over. */
12679 case CPP_SEMICOLON:
12680 case CPP_CLOSE_BRACE:
12681 case CPP_CLOSE_SQUARE:
12682 if (depth == 0)
12683 done = true;
12684 /* Update DEPTH, if necessary. */
12685 else if (token->type == CPP_CLOSE_PAREN
12686 || token->type == CPP_CLOSE_BRACE
12687 || token->type == CPP_CLOSE_SQUARE)
12688 --depth;
12689 break;
12691 case CPP_OPEN_PAREN:
12692 case CPP_OPEN_SQUARE:
12693 case CPP_OPEN_BRACE:
12694 ++depth;
12695 break;
12697 case CPP_GREATER:
12698 /* If we see a non-nested `>', and `>' is not an
12699 operator, then it marks the end of the default
12700 argument. */
12701 if (!depth && !greater_than_is_operator_p)
12702 done = true;
12703 break;
12705 /* If we run out of tokens, issue an error message. */
12706 case CPP_EOF:
12707 case CPP_PRAGMA_EOL:
12708 error ("file ends in default argument");
12709 done = true;
12710 break;
12712 case CPP_NAME:
12713 case CPP_SCOPE:
12714 /* In these cases, we should look for template-ids.
12715 For example, if the default argument is
12716 `X<int, double>()', we need to do name lookup to
12717 figure out whether or not `X' is a template; if
12718 so, the `,' does not end the default argument.
12720 That is not yet done. */
12721 break;
12723 default:
12724 break;
12727 /* If we've reached the end, stop. */
12728 if (done)
12729 break;
12731 /* Add the token to the token block. */
12732 token = cp_lexer_consume_token (parser->lexer);
12735 /* Create a DEFAULT_ARG to represented the unparsed default
12736 argument. */
12737 default_argument = make_node (DEFAULT_ARG);
12738 DEFARG_TOKENS (default_argument)
12739 = cp_token_cache_new (first_token, token);
12740 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12742 /* Outside of a class definition, we can just parse the
12743 assignment-expression. */
12744 else
12746 bool saved_local_variables_forbidden_p;
12748 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12749 set correctly. */
12750 saved_greater_than_is_operator_p
12751 = parser->greater_than_is_operator_p;
12752 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12753 /* Local variable names (and the `this' keyword) may not
12754 appear in a default argument. */
12755 saved_local_variables_forbidden_p
12756 = parser->local_variables_forbidden_p;
12757 parser->local_variables_forbidden_p = true;
12758 /* The default argument expression may cause implicitly
12759 defined member functions to be synthesized, which will
12760 result in garbage collection. We must treat this
12761 situation as if we were within the body of function so as
12762 to avoid collecting live data on the stack. */
12763 ++function_depth;
12764 /* Parse the assignment-expression. */
12765 if (template_parm_p)
12766 push_deferring_access_checks (dk_no_deferred);
12767 default_argument
12768 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12769 if (template_parm_p)
12770 pop_deferring_access_checks ();
12771 /* Restore saved state. */
12772 --function_depth;
12773 parser->greater_than_is_operator_p
12774 = saved_greater_than_is_operator_p;
12775 parser->local_variables_forbidden_p
12776 = saved_local_variables_forbidden_p;
12778 if (!parser->default_arg_ok_p)
12780 if (!flag_pedantic_errors)
12781 warning (0, "deprecated use of default argument for parameter of non-function");
12782 else
12784 error ("default arguments are only permitted for function parameters");
12785 default_argument = NULL_TREE;
12789 else
12790 default_argument = NULL_TREE;
12792 return make_parameter_declarator (&decl_specifiers,
12793 declarator,
12794 default_argument);
12797 /* Parse a function-body.
12799 function-body:
12800 compound_statement */
12802 static void
12803 cp_parser_function_body (cp_parser *parser)
12805 cp_parser_compound_statement (parser, NULL, false);
12808 /* Parse a ctor-initializer-opt followed by a function-body. Return
12809 true if a ctor-initializer was present. */
12811 static bool
12812 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12814 tree body;
12815 bool ctor_initializer_p;
12817 /* Begin the function body. */
12818 body = begin_function_body ();
12819 /* Parse the optional ctor-initializer. */
12820 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12821 /* Parse the function-body. */
12822 cp_parser_function_body (parser);
12823 /* Finish the function body. */
12824 finish_function_body (body);
12826 return ctor_initializer_p;
12829 /* Parse an initializer.
12831 initializer:
12832 = initializer-clause
12833 ( expression-list )
12835 Returns an expression representing the initializer. If no
12836 initializer is present, NULL_TREE is returned.
12838 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12839 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12840 set to FALSE if there is no initializer present. If there is an
12841 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12842 is set to true; otherwise it is set to false. */
12844 static tree
12845 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12846 bool* non_constant_p)
12848 cp_token *token;
12849 tree init;
12851 /* Peek at the next token. */
12852 token = cp_lexer_peek_token (parser->lexer);
12854 /* Let our caller know whether or not this initializer was
12855 parenthesized. */
12856 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12857 /* Assume that the initializer is constant. */
12858 *non_constant_p = false;
12860 if (token->type == CPP_EQ)
12862 /* Consume the `='. */
12863 cp_lexer_consume_token (parser->lexer);
12864 /* Parse the initializer-clause. */
12865 init = cp_parser_initializer_clause (parser, non_constant_p);
12867 else if (token->type == CPP_OPEN_PAREN)
12868 init = cp_parser_parenthesized_expression_list (parser, false,
12869 /*cast_p=*/false,
12870 non_constant_p);
12871 else
12873 /* Anything else is an error. */
12874 cp_parser_error (parser, "expected initializer");
12875 init = error_mark_node;
12878 return init;
12881 /* Parse an initializer-clause.
12883 initializer-clause:
12884 assignment-expression
12885 { initializer-list , [opt] }
12888 Returns an expression representing the initializer.
12890 If the `assignment-expression' production is used the value
12891 returned is simply a representation for the expression.
12893 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12894 the elements of the initializer-list (or NULL, if the last
12895 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12896 NULL_TREE. There is no way to detect whether or not the optional
12897 trailing `,' was provided. NON_CONSTANT_P is as for
12898 cp_parser_initializer. */
12900 static tree
12901 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12903 tree initializer;
12905 /* Assume the expression is constant. */
12906 *non_constant_p = false;
12908 /* If it is not a `{', then we are looking at an
12909 assignment-expression. */
12910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12912 initializer
12913 = cp_parser_constant_expression (parser,
12914 /*allow_non_constant_p=*/true,
12915 non_constant_p);
12916 if (!*non_constant_p)
12917 initializer = fold_non_dependent_expr (initializer);
12919 else
12921 /* Consume the `{' token. */
12922 cp_lexer_consume_token (parser->lexer);
12923 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12924 initializer = make_node (CONSTRUCTOR);
12925 /* If it's not a `}', then there is a non-trivial initializer. */
12926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12928 /* Parse the initializer list. */
12929 CONSTRUCTOR_ELTS (initializer)
12930 = cp_parser_initializer_list (parser, non_constant_p);
12931 /* A trailing `,' token is allowed. */
12932 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12933 cp_lexer_consume_token (parser->lexer);
12935 /* Now, there should be a trailing `}'. */
12936 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12939 return initializer;
12942 /* Parse an initializer-list.
12944 initializer-list:
12945 initializer-clause
12946 initializer-list , initializer-clause
12948 GNU Extension:
12950 initializer-list:
12951 identifier : initializer-clause
12952 initializer-list, identifier : initializer-clause
12954 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12955 for the initializer. If the INDEX of the elt is non-NULL, it is the
12956 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12957 as for cp_parser_initializer. */
12959 static VEC(constructor_elt,gc) *
12960 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12962 VEC(constructor_elt,gc) *v = NULL;
12964 /* Assume all of the expressions are constant. */
12965 *non_constant_p = false;
12967 /* Parse the rest of the list. */
12968 while (true)
12970 cp_token *token;
12971 tree identifier;
12972 tree initializer;
12973 bool clause_non_constant_p;
12975 /* If the next token is an identifier and the following one is a
12976 colon, we are looking at the GNU designated-initializer
12977 syntax. */
12978 if (cp_parser_allow_gnu_extensions_p (parser)
12979 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12980 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12982 /* Warn the user that they are using an extension. */
12983 if (pedantic)
12984 pedwarn ("ISO C++ does not allow designated initializers");
12985 /* Consume the identifier. */
12986 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
12987 /* Consume the `:'. */
12988 cp_lexer_consume_token (parser->lexer);
12990 else
12991 identifier = NULL_TREE;
12993 /* Parse the initializer. */
12994 initializer = cp_parser_initializer_clause (parser,
12995 &clause_non_constant_p);
12996 /* If any clause is non-constant, so is the entire initializer. */
12997 if (clause_non_constant_p)
12998 *non_constant_p = true;
13000 /* Add it to the vector. */
13001 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13003 /* If the next token is not a comma, we have reached the end of
13004 the list. */
13005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13006 break;
13008 /* Peek at the next token. */
13009 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13010 /* If the next token is a `}', then we're still done. An
13011 initializer-clause can have a trailing `,' after the
13012 initializer-list and before the closing `}'. */
13013 if (token->type == CPP_CLOSE_BRACE)
13014 break;
13016 /* Consume the `,' token. */
13017 cp_lexer_consume_token (parser->lexer);
13020 return v;
13023 /* Classes [gram.class] */
13025 /* Parse a class-name.
13027 class-name:
13028 identifier
13029 template-id
13031 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13032 to indicate that names looked up in dependent types should be
13033 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13034 keyword has been used to indicate that the name that appears next
13035 is a template. TAG_TYPE indicates the explicit tag given before
13036 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13037 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13038 is the class being defined in a class-head.
13040 Returns the TYPE_DECL representing the class. */
13042 static tree
13043 cp_parser_class_name (cp_parser *parser,
13044 bool typename_keyword_p,
13045 bool template_keyword_p,
13046 enum tag_types tag_type,
13047 bool check_dependency_p,
13048 bool class_head_p,
13049 bool is_declaration)
13051 tree decl;
13052 tree scope;
13053 bool typename_p;
13054 cp_token *token;
13056 /* All class-names start with an identifier. */
13057 token = cp_lexer_peek_token (parser->lexer);
13058 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13060 cp_parser_error (parser, "expected class-name");
13061 return error_mark_node;
13064 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13065 to a template-id, so we save it here. */
13066 scope = parser->scope;
13067 if (scope == error_mark_node)
13068 return error_mark_node;
13070 /* Any name names a type if we're following the `typename' keyword
13071 in a qualified name where the enclosing scope is type-dependent. */
13072 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13073 && dependent_type_p (scope));
13074 /* Handle the common case (an identifier, but not a template-id)
13075 efficiently. */
13076 if (token->type == CPP_NAME
13077 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13079 cp_token *identifier_token;
13080 tree identifier;
13081 bool ambiguous_p;
13083 /* Look for the identifier. */
13084 identifier_token = cp_lexer_peek_token (parser->lexer);
13085 ambiguous_p = identifier_token->ambiguous_p;
13086 identifier = cp_parser_identifier (parser);
13087 /* If the next token isn't an identifier, we are certainly not
13088 looking at a class-name. */
13089 if (identifier == error_mark_node)
13090 decl = error_mark_node;
13091 /* If we know this is a type-name, there's no need to look it
13092 up. */
13093 else if (typename_p)
13094 decl = identifier;
13095 else
13097 tree ambiguous_decls;
13098 /* If we already know that this lookup is ambiguous, then
13099 we've already issued an error message; there's no reason
13100 to check again. */
13101 if (ambiguous_p)
13103 cp_parser_simulate_error (parser);
13104 return error_mark_node;
13106 /* If the next token is a `::', then the name must be a type
13107 name.
13109 [basic.lookup.qual]
13111 During the lookup for a name preceding the :: scope
13112 resolution operator, object, function, and enumerator
13113 names are ignored. */
13114 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13115 tag_type = typename_type;
13116 /* Look up the name. */
13117 decl = cp_parser_lookup_name (parser, identifier,
13118 tag_type,
13119 /*is_template=*/false,
13120 /*is_namespace=*/false,
13121 check_dependency_p,
13122 &ambiguous_decls);
13123 if (ambiguous_decls)
13125 error ("reference to %qD is ambiguous", identifier);
13126 print_candidates (ambiguous_decls);
13127 if (cp_parser_parsing_tentatively (parser))
13129 identifier_token->ambiguous_p = true;
13130 cp_parser_simulate_error (parser);
13132 return error_mark_node;
13136 else
13138 /* Try a template-id. */
13139 decl = cp_parser_template_id (parser, template_keyword_p,
13140 check_dependency_p,
13141 is_declaration);
13142 if (decl == error_mark_node)
13143 return error_mark_node;
13146 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13148 /* If this is a typename, create a TYPENAME_TYPE. */
13149 if (typename_p && decl != error_mark_node)
13151 decl = make_typename_type (scope, decl, typename_type,
13152 /*complain=*/tf_error);
13153 if (decl != error_mark_node)
13154 decl = TYPE_NAME (decl);
13157 /* Check to see that it is really the name of a class. */
13158 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13159 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13160 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13161 /* Situations like this:
13163 template <typename T> struct A {
13164 typename T::template X<int>::I i;
13167 are problematic. Is `T::template X<int>' a class-name? The
13168 standard does not seem to be definitive, but there is no other
13169 valid interpretation of the following `::'. Therefore, those
13170 names are considered class-names. */
13172 decl = make_typename_type (scope, decl, tag_type, tf_error);
13173 if (decl != error_mark_node)
13174 decl = TYPE_NAME (decl);
13176 else if (TREE_CODE (decl) != TYPE_DECL
13177 || TREE_TYPE (decl) == error_mark_node
13178 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13179 decl = error_mark_node;
13181 if (decl == error_mark_node)
13182 cp_parser_error (parser, "expected class-name");
13184 return decl;
13187 /* Parse a class-specifier.
13189 class-specifier:
13190 class-head { member-specification [opt] }
13192 Returns the TREE_TYPE representing the class. */
13194 static tree
13195 cp_parser_class_specifier (cp_parser* parser)
13197 cp_token *token;
13198 tree type;
13199 tree attributes = NULL_TREE;
13200 int has_trailing_semicolon;
13201 bool nested_name_specifier_p;
13202 unsigned saved_num_template_parameter_lists;
13203 bool saved_in_function_body;
13204 tree old_scope = NULL_TREE;
13205 tree scope = NULL_TREE;
13206 tree bases;
13208 push_deferring_access_checks (dk_no_deferred);
13210 /* Parse the class-head. */
13211 type = cp_parser_class_head (parser,
13212 &nested_name_specifier_p,
13213 &attributes,
13214 &bases);
13215 /* If the class-head was a semantic disaster, skip the entire body
13216 of the class. */
13217 if (!type)
13219 cp_parser_skip_to_end_of_block_or_statement (parser);
13220 pop_deferring_access_checks ();
13221 return error_mark_node;
13224 /* Look for the `{'. */
13225 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13227 pop_deferring_access_checks ();
13228 return error_mark_node;
13231 /* Process the base classes. If they're invalid, skip the
13232 entire class body. */
13233 if (!xref_basetypes (type, bases))
13235 cp_parser_skip_to_closing_brace (parser);
13237 /* Consuming the closing brace yields better error messages
13238 later on. */
13239 cp_lexer_consume_token (parser->lexer);
13240 pop_deferring_access_checks ();
13241 return error_mark_node;
13244 /* Issue an error message if type-definitions are forbidden here. */
13245 cp_parser_check_type_definition (parser);
13246 /* Remember that we are defining one more class. */
13247 ++parser->num_classes_being_defined;
13248 /* Inside the class, surrounding template-parameter-lists do not
13249 apply. */
13250 saved_num_template_parameter_lists
13251 = parser->num_template_parameter_lists;
13252 parser->num_template_parameter_lists = 0;
13253 /* We are not in a function body. */
13254 saved_in_function_body = parser->in_function_body;
13255 parser->in_function_body = false;
13257 /* Start the class. */
13258 if (nested_name_specifier_p)
13260 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13261 old_scope = push_inner_scope (scope);
13263 type = begin_class_definition (type, attributes);
13265 if (type == error_mark_node)
13266 /* If the type is erroneous, skip the entire body of the class. */
13267 cp_parser_skip_to_closing_brace (parser);
13268 else
13269 /* Parse the member-specification. */
13270 cp_parser_member_specification_opt (parser);
13272 /* Look for the trailing `}'. */
13273 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13274 /* We get better error messages by noticing a common problem: a
13275 missing trailing `;'. */
13276 token = cp_lexer_peek_token (parser->lexer);
13277 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13278 /* Look for trailing attributes to apply to this class. */
13279 if (cp_parser_allow_gnu_extensions_p (parser))
13280 attributes = cp_parser_attributes_opt (parser);
13281 if (type != error_mark_node)
13282 type = finish_struct (type, attributes);
13283 if (nested_name_specifier_p)
13284 pop_inner_scope (old_scope, scope);
13285 /* If this class is not itself within the scope of another class,
13286 then we need to parse the bodies of all of the queued function
13287 definitions. Note that the queued functions defined in a class
13288 are not always processed immediately following the
13289 class-specifier for that class. Consider:
13291 struct A {
13292 struct B { void f() { sizeof (A); } };
13295 If `f' were processed before the processing of `A' were
13296 completed, there would be no way to compute the size of `A'.
13297 Note that the nesting we are interested in here is lexical --
13298 not the semantic nesting given by TYPE_CONTEXT. In particular,
13299 for:
13301 struct A { struct B; };
13302 struct A::B { void f() { } };
13304 there is no need to delay the parsing of `A::B::f'. */
13305 if (--parser->num_classes_being_defined == 0)
13307 tree queue_entry;
13308 tree fn;
13309 tree class_type = NULL_TREE;
13310 tree pushed_scope = NULL_TREE;
13312 /* In a first pass, parse default arguments to the functions.
13313 Then, in a second pass, parse the bodies of the functions.
13314 This two-phased approach handles cases like:
13316 struct S {
13317 void f() { g(); }
13318 void g(int i = 3);
13322 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13323 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13324 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13325 TREE_PURPOSE (parser->unparsed_functions_queues)
13326 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13328 fn = TREE_VALUE (queue_entry);
13329 /* If there are default arguments that have not yet been processed,
13330 take care of them now. */
13331 if (class_type != TREE_PURPOSE (queue_entry))
13333 if (pushed_scope)
13334 pop_scope (pushed_scope);
13335 class_type = TREE_PURPOSE (queue_entry);
13336 pushed_scope = push_scope (class_type);
13338 /* Make sure that any template parameters are in scope. */
13339 maybe_begin_member_template_processing (fn);
13340 /* Parse the default argument expressions. */
13341 cp_parser_late_parsing_default_args (parser, fn);
13342 /* Remove any template parameters from the symbol table. */
13343 maybe_end_member_template_processing ();
13345 if (pushed_scope)
13346 pop_scope (pushed_scope);
13347 /* Now parse the body of the functions. */
13348 for (TREE_VALUE (parser->unparsed_functions_queues)
13349 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13350 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13351 TREE_VALUE (parser->unparsed_functions_queues)
13352 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13354 /* Figure out which function we need to process. */
13355 fn = TREE_VALUE (queue_entry);
13356 /* Parse the function. */
13357 cp_parser_late_parsing_for_member (parser, fn);
13361 /* Put back any saved access checks. */
13362 pop_deferring_access_checks ();
13364 /* Restore saved state. */
13365 parser->in_function_body = saved_in_function_body;
13366 parser->num_template_parameter_lists
13367 = saved_num_template_parameter_lists;
13369 return type;
13372 /* Parse a class-head.
13374 class-head:
13375 class-key identifier [opt] base-clause [opt]
13376 class-key nested-name-specifier identifier base-clause [opt]
13377 class-key nested-name-specifier [opt] template-id
13378 base-clause [opt]
13380 GNU Extensions:
13381 class-key attributes identifier [opt] base-clause [opt]
13382 class-key attributes nested-name-specifier identifier base-clause [opt]
13383 class-key attributes nested-name-specifier [opt] template-id
13384 base-clause [opt]
13386 Returns the TYPE of the indicated class. Sets
13387 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13388 involving a nested-name-specifier was used, and FALSE otherwise.
13390 Returns error_mark_node if this is not a class-head.
13392 Returns NULL_TREE if the class-head is syntactically valid, but
13393 semantically invalid in a way that means we should skip the entire
13394 body of the class. */
13396 static tree
13397 cp_parser_class_head (cp_parser* parser,
13398 bool* nested_name_specifier_p,
13399 tree *attributes_p,
13400 tree *bases)
13402 tree nested_name_specifier;
13403 enum tag_types class_key;
13404 tree id = NULL_TREE;
13405 tree type = NULL_TREE;
13406 tree attributes;
13407 bool template_id_p = false;
13408 bool qualified_p = false;
13409 bool invalid_nested_name_p = false;
13410 bool invalid_explicit_specialization_p = false;
13411 tree pushed_scope = NULL_TREE;
13412 unsigned num_templates;
13414 /* Assume no nested-name-specifier will be present. */
13415 *nested_name_specifier_p = false;
13416 /* Assume no template parameter lists will be used in defining the
13417 type. */
13418 num_templates = 0;
13420 /* Look for the class-key. */
13421 class_key = cp_parser_class_key (parser);
13422 if (class_key == none_type)
13423 return error_mark_node;
13425 /* Parse the attributes. */
13426 attributes = cp_parser_attributes_opt (parser);
13428 /* If the next token is `::', that is invalid -- but sometimes
13429 people do try to write:
13431 struct ::S {};
13433 Handle this gracefully by accepting the extra qualifier, and then
13434 issuing an error about it later if this really is a
13435 class-head. If it turns out just to be an elaborated type
13436 specifier, remain silent. */
13437 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13438 qualified_p = true;
13440 push_deferring_access_checks (dk_no_check);
13442 /* Determine the name of the class. Begin by looking for an
13443 optional nested-name-specifier. */
13444 nested_name_specifier
13445 = cp_parser_nested_name_specifier_opt (parser,
13446 /*typename_keyword_p=*/false,
13447 /*check_dependency_p=*/false,
13448 /*type_p=*/false,
13449 /*is_declaration=*/false);
13450 /* If there was a nested-name-specifier, then there *must* be an
13451 identifier. */
13452 if (nested_name_specifier)
13454 /* Although the grammar says `identifier', it really means
13455 `class-name' or `template-name'. You are only allowed to
13456 define a class that has already been declared with this
13457 syntax.
13459 The proposed resolution for Core Issue 180 says that wherever
13460 you see `class T::X' you should treat `X' as a type-name.
13462 It is OK to define an inaccessible class; for example:
13464 class A { class B; };
13465 class A::B {};
13467 We do not know if we will see a class-name, or a
13468 template-name. We look for a class-name first, in case the
13469 class-name is a template-id; if we looked for the
13470 template-name first we would stop after the template-name. */
13471 cp_parser_parse_tentatively (parser);
13472 type = cp_parser_class_name (parser,
13473 /*typename_keyword_p=*/false,
13474 /*template_keyword_p=*/false,
13475 class_type,
13476 /*check_dependency_p=*/false,
13477 /*class_head_p=*/true,
13478 /*is_declaration=*/false);
13479 /* If that didn't work, ignore the nested-name-specifier. */
13480 if (!cp_parser_parse_definitely (parser))
13482 invalid_nested_name_p = true;
13483 id = cp_parser_identifier (parser);
13484 if (id == error_mark_node)
13485 id = NULL_TREE;
13487 /* If we could not find a corresponding TYPE, treat this
13488 declaration like an unqualified declaration. */
13489 if (type == error_mark_node)
13490 nested_name_specifier = NULL_TREE;
13491 /* Otherwise, count the number of templates used in TYPE and its
13492 containing scopes. */
13493 else
13495 tree scope;
13497 for (scope = TREE_TYPE (type);
13498 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13499 scope = (TYPE_P (scope)
13500 ? TYPE_CONTEXT (scope)
13501 : DECL_CONTEXT (scope)))
13502 if (TYPE_P (scope)
13503 && CLASS_TYPE_P (scope)
13504 && CLASSTYPE_TEMPLATE_INFO (scope)
13505 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13506 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13507 ++num_templates;
13510 /* Otherwise, the identifier is optional. */
13511 else
13513 /* We don't know whether what comes next is a template-id,
13514 an identifier, or nothing at all. */
13515 cp_parser_parse_tentatively (parser);
13516 /* Check for a template-id. */
13517 id = cp_parser_template_id (parser,
13518 /*template_keyword_p=*/false,
13519 /*check_dependency_p=*/true,
13520 /*is_declaration=*/true);
13521 /* If that didn't work, it could still be an identifier. */
13522 if (!cp_parser_parse_definitely (parser))
13524 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13525 id = cp_parser_identifier (parser);
13526 else
13527 id = NULL_TREE;
13529 else
13531 template_id_p = true;
13532 ++num_templates;
13536 pop_deferring_access_checks ();
13538 if (id)
13539 cp_parser_check_for_invalid_template_id (parser, id);
13541 /* If it's not a `:' or a `{' then we can't really be looking at a
13542 class-head, since a class-head only appears as part of a
13543 class-specifier. We have to detect this situation before calling
13544 xref_tag, since that has irreversible side-effects. */
13545 if (!cp_parser_next_token_starts_class_definition_p (parser))
13547 cp_parser_error (parser, "expected %<{%> or %<:%>");
13548 return error_mark_node;
13551 /* At this point, we're going ahead with the class-specifier, even
13552 if some other problem occurs. */
13553 cp_parser_commit_to_tentative_parse (parser);
13554 /* Issue the error about the overly-qualified name now. */
13555 if (qualified_p)
13556 cp_parser_error (parser,
13557 "global qualification of class name is invalid");
13558 else if (invalid_nested_name_p)
13559 cp_parser_error (parser,
13560 "qualified name does not name a class");
13561 else if (nested_name_specifier)
13563 tree scope;
13565 /* Reject typedef-names in class heads. */
13566 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13568 error ("invalid class name in declaration of %qD", type);
13569 type = NULL_TREE;
13570 goto done;
13573 /* Figure out in what scope the declaration is being placed. */
13574 scope = current_scope ();
13575 /* If that scope does not contain the scope in which the
13576 class was originally declared, the program is invalid. */
13577 if (scope && !is_ancestor (scope, nested_name_specifier))
13579 error ("declaration of %qD in %qD which does not enclose %qD",
13580 type, scope, nested_name_specifier);
13581 type = NULL_TREE;
13582 goto done;
13584 /* [dcl.meaning]
13586 A declarator-id shall not be qualified exception of the
13587 definition of a ... nested class outside of its class
13588 ... [or] a the definition or explicit instantiation of a
13589 class member of a namespace outside of its namespace. */
13590 if (scope == nested_name_specifier)
13592 pedwarn ("extra qualification ignored");
13593 nested_name_specifier = NULL_TREE;
13594 num_templates = 0;
13597 /* An explicit-specialization must be preceded by "template <>". If
13598 it is not, try to recover gracefully. */
13599 if (at_namespace_scope_p ()
13600 && parser->num_template_parameter_lists == 0
13601 && template_id_p)
13603 error ("an explicit specialization must be preceded by %<template <>%>");
13604 invalid_explicit_specialization_p = true;
13605 /* Take the same action that would have been taken by
13606 cp_parser_explicit_specialization. */
13607 ++parser->num_template_parameter_lists;
13608 begin_specialization ();
13610 /* There must be no "return" statements between this point and the
13611 end of this function; set "type "to the correct return value and
13612 use "goto done;" to return. */
13613 /* Make sure that the right number of template parameters were
13614 present. */
13615 if (!cp_parser_check_template_parameters (parser, num_templates))
13617 /* If something went wrong, there is no point in even trying to
13618 process the class-definition. */
13619 type = NULL_TREE;
13620 goto done;
13623 /* Look up the type. */
13624 if (template_id_p)
13626 type = TREE_TYPE (id);
13627 type = maybe_process_partial_specialization (type);
13628 if (nested_name_specifier)
13629 pushed_scope = push_scope (nested_name_specifier);
13631 else if (nested_name_specifier)
13633 tree class_type;
13635 /* Given:
13637 template <typename T> struct S { struct T };
13638 template <typename T> struct S<T>::T { };
13640 we will get a TYPENAME_TYPE when processing the definition of
13641 `S::T'. We need to resolve it to the actual type before we
13642 try to define it. */
13643 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13645 class_type = resolve_typename_type (TREE_TYPE (type),
13646 /*only_current_p=*/false);
13647 if (class_type != error_mark_node)
13648 type = TYPE_NAME (class_type);
13649 else
13651 cp_parser_error (parser, "could not resolve typename type");
13652 type = error_mark_node;
13656 maybe_process_partial_specialization (TREE_TYPE (type));
13657 class_type = current_class_type;
13658 /* Enter the scope indicated by the nested-name-specifier. */
13659 pushed_scope = push_scope (nested_name_specifier);
13660 /* Get the canonical version of this type. */
13661 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13662 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13663 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13665 type = push_template_decl (type);
13666 if (type == error_mark_node)
13668 type = NULL_TREE;
13669 goto done;
13673 type = TREE_TYPE (type);
13674 *nested_name_specifier_p = true;
13676 else /* The name is not a nested name. */
13678 /* If the class was unnamed, create a dummy name. */
13679 if (!id)
13680 id = make_anon_name ();
13681 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13682 parser->num_template_parameter_lists);
13685 /* Indicate whether this class was declared as a `class' or as a
13686 `struct'. */
13687 if (TREE_CODE (type) == RECORD_TYPE)
13688 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13689 cp_parser_check_class_key (class_key, type);
13691 /* If this type was already complete, and we see another definition,
13692 that's an error. */
13693 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13695 error ("redefinition of %q#T", type);
13696 error ("previous definition of %q+#T", type);
13697 type = NULL_TREE;
13698 goto done;
13700 else if (type == error_mark_node)
13701 type = NULL_TREE;
13703 /* We will have entered the scope containing the class; the names of
13704 base classes should be looked up in that context. For example:
13706 struct A { struct B {}; struct C; };
13707 struct A::C : B {};
13709 is valid. */
13710 *bases = NULL_TREE;
13712 /* Get the list of base-classes, if there is one. */
13713 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13714 *bases = cp_parser_base_clause (parser);
13716 done:
13717 /* Leave the scope given by the nested-name-specifier. We will
13718 enter the class scope itself while processing the members. */
13719 if (pushed_scope)
13720 pop_scope (pushed_scope);
13722 if (invalid_explicit_specialization_p)
13724 end_specialization ();
13725 --parser->num_template_parameter_lists;
13727 *attributes_p = attributes;
13728 return type;
13731 /* Parse a class-key.
13733 class-key:
13734 class
13735 struct
13736 union
13738 Returns the kind of class-key specified, or none_type to indicate
13739 error. */
13741 static enum tag_types
13742 cp_parser_class_key (cp_parser* parser)
13744 cp_token *token;
13745 enum tag_types tag_type;
13747 /* Look for the class-key. */
13748 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13749 if (!token)
13750 return none_type;
13752 /* Check to see if the TOKEN is a class-key. */
13753 tag_type = cp_parser_token_is_class_key (token);
13754 if (!tag_type)
13755 cp_parser_error (parser, "expected class-key");
13756 return tag_type;
13759 /* Parse an (optional) member-specification.
13761 member-specification:
13762 member-declaration member-specification [opt]
13763 access-specifier : member-specification [opt] */
13765 static void
13766 cp_parser_member_specification_opt (cp_parser* parser)
13768 while (true)
13770 cp_token *token;
13771 enum rid keyword;
13773 /* Peek at the next token. */
13774 token = cp_lexer_peek_token (parser->lexer);
13775 /* If it's a `}', or EOF then we've seen all the members. */
13776 if (token->type == CPP_CLOSE_BRACE
13777 || token->type == CPP_EOF
13778 || token->type == CPP_PRAGMA_EOL)
13779 break;
13781 /* See if this token is a keyword. */
13782 keyword = token->keyword;
13783 switch (keyword)
13785 case RID_PUBLIC:
13786 case RID_PROTECTED:
13787 case RID_PRIVATE:
13788 /* Consume the access-specifier. */
13789 cp_lexer_consume_token (parser->lexer);
13790 /* Remember which access-specifier is active. */
13791 current_access_specifier = token->u.value;
13792 /* Look for the `:'. */
13793 cp_parser_require (parser, CPP_COLON, "`:'");
13794 break;
13796 default:
13797 /* Accept #pragmas at class scope. */
13798 if (token->type == CPP_PRAGMA)
13800 cp_parser_pragma (parser, pragma_external);
13801 break;
13804 /* Otherwise, the next construction must be a
13805 member-declaration. */
13806 cp_parser_member_declaration (parser);
13811 /* Parse a member-declaration.
13813 member-declaration:
13814 decl-specifier-seq [opt] member-declarator-list [opt] ;
13815 function-definition ; [opt]
13816 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13817 using-declaration
13818 template-declaration
13820 member-declarator-list:
13821 member-declarator
13822 member-declarator-list , member-declarator
13824 member-declarator:
13825 declarator pure-specifier [opt]
13826 declarator constant-initializer [opt]
13827 identifier [opt] : constant-expression
13829 GNU Extensions:
13831 member-declaration:
13832 __extension__ member-declaration
13834 member-declarator:
13835 declarator attributes [opt] pure-specifier [opt]
13836 declarator attributes [opt] constant-initializer [opt]
13837 identifier [opt] attributes [opt] : constant-expression
13839 C++0x Extensions:
13841 member-declaration:
13842 static_assert-declaration */
13844 static void
13845 cp_parser_member_declaration (cp_parser* parser)
13847 cp_decl_specifier_seq decl_specifiers;
13848 tree prefix_attributes;
13849 tree decl;
13850 int declares_class_or_enum;
13851 bool friend_p;
13852 cp_token *token;
13853 int saved_pedantic;
13855 /* Check for the `__extension__' keyword. */
13856 if (cp_parser_extension_opt (parser, &saved_pedantic))
13858 /* Recurse. */
13859 cp_parser_member_declaration (parser);
13860 /* Restore the old value of the PEDANTIC flag. */
13861 pedantic = saved_pedantic;
13863 return;
13866 /* Check for a template-declaration. */
13867 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13869 /* An explicit specialization here is an error condition, and we
13870 expect the specialization handler to detect and report this. */
13871 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13872 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13873 cp_parser_explicit_specialization (parser);
13874 else
13875 cp_parser_template_declaration (parser, /*member_p=*/true);
13877 return;
13880 /* Check for a using-declaration. */
13881 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13883 /* Parse the using-declaration. */
13884 cp_parser_using_declaration (parser,
13885 /*access_declaration_p=*/false);
13886 return;
13889 /* Check for @defs. */
13890 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13892 tree ivar, member;
13893 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13894 ivar = ivar_chains;
13895 while (ivar)
13897 member = ivar;
13898 ivar = TREE_CHAIN (member);
13899 TREE_CHAIN (member) = NULL_TREE;
13900 finish_member_declaration (member);
13902 return;
13905 /* If the next token is `static_assert' we have a static assertion. */
13906 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
13908 cp_parser_static_assert (parser, /*member_p=*/true);
13909 return;
13912 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13913 return;
13915 /* Parse the decl-specifier-seq. */
13916 cp_parser_decl_specifier_seq (parser,
13917 CP_PARSER_FLAGS_OPTIONAL,
13918 &decl_specifiers,
13919 &declares_class_or_enum);
13920 prefix_attributes = decl_specifiers.attributes;
13921 decl_specifiers.attributes = NULL_TREE;
13922 /* Check for an invalid type-name. */
13923 if (!decl_specifiers.type
13924 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13925 return;
13926 /* If there is no declarator, then the decl-specifier-seq should
13927 specify a type. */
13928 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13930 /* If there was no decl-specifier-seq, and the next token is a
13931 `;', then we have something like:
13933 struct S { ; };
13935 [class.mem]
13937 Each member-declaration shall declare at least one member
13938 name of the class. */
13939 if (!decl_specifiers.any_specifiers_p)
13941 cp_token *token = cp_lexer_peek_token (parser->lexer);
13942 if (pedantic && !token->in_system_header)
13943 pedwarn ("%Hextra %<;%>", &token->location);
13945 else
13947 tree type;
13949 /* See if this declaration is a friend. */
13950 friend_p = cp_parser_friend_p (&decl_specifiers);
13951 /* If there were decl-specifiers, check to see if there was
13952 a class-declaration. */
13953 type = check_tag_decl (&decl_specifiers);
13954 /* Nested classes have already been added to the class, but
13955 a `friend' needs to be explicitly registered. */
13956 if (friend_p)
13958 /* If the `friend' keyword was present, the friend must
13959 be introduced with a class-key. */
13960 if (!declares_class_or_enum)
13961 error ("a class-key must be used when declaring a friend");
13962 /* In this case:
13964 template <typename T> struct A {
13965 friend struct A<T>::B;
13968 A<T>::B will be represented by a TYPENAME_TYPE, and
13969 therefore not recognized by check_tag_decl. */
13970 if (!type
13971 && decl_specifiers.type
13972 && TYPE_P (decl_specifiers.type))
13973 type = decl_specifiers.type;
13974 if (!type || !TYPE_P (type))
13975 error ("friend declaration does not name a class or "
13976 "function");
13977 else
13978 make_friend_class (current_class_type, type,
13979 /*complain=*/true);
13981 /* If there is no TYPE, an error message will already have
13982 been issued. */
13983 else if (!type || type == error_mark_node)
13985 /* An anonymous aggregate has to be handled specially; such
13986 a declaration really declares a data member (with a
13987 particular type), as opposed to a nested class. */
13988 else if (ANON_AGGR_TYPE_P (type))
13990 /* Remove constructors and such from TYPE, now that we
13991 know it is an anonymous aggregate. */
13992 fixup_anonymous_aggr (type);
13993 /* And make the corresponding data member. */
13994 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13995 /* Add it to the class. */
13996 finish_member_declaration (decl);
13998 else
13999 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14002 else
14004 /* See if these declarations will be friends. */
14005 friend_p = cp_parser_friend_p (&decl_specifiers);
14007 /* Keep going until we hit the `;' at the end of the
14008 declaration. */
14009 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14011 tree attributes = NULL_TREE;
14012 tree first_attribute;
14014 /* Peek at the next token. */
14015 token = cp_lexer_peek_token (parser->lexer);
14017 /* Check for a bitfield declaration. */
14018 if (token->type == CPP_COLON
14019 || (token->type == CPP_NAME
14020 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14021 == CPP_COLON))
14023 tree identifier;
14024 tree width;
14026 /* Get the name of the bitfield. Note that we cannot just
14027 check TOKEN here because it may have been invalidated by
14028 the call to cp_lexer_peek_nth_token above. */
14029 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14030 identifier = cp_parser_identifier (parser);
14031 else
14032 identifier = NULL_TREE;
14034 /* Consume the `:' token. */
14035 cp_lexer_consume_token (parser->lexer);
14036 /* Get the width of the bitfield. */
14037 width
14038 = cp_parser_constant_expression (parser,
14039 /*allow_non_constant=*/false,
14040 NULL);
14042 /* Look for attributes that apply to the bitfield. */
14043 attributes = cp_parser_attributes_opt (parser);
14044 /* Remember which attributes are prefix attributes and
14045 which are not. */
14046 first_attribute = attributes;
14047 /* Combine the attributes. */
14048 attributes = chainon (prefix_attributes, attributes);
14050 /* Create the bitfield declaration. */
14051 decl = grokbitfield (identifier
14052 ? make_id_declarator (NULL_TREE,
14053 identifier,
14054 sfk_none)
14055 : NULL,
14056 &decl_specifiers,
14057 width);
14058 /* Apply the attributes. */
14059 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14061 else
14063 cp_declarator *declarator;
14064 tree initializer;
14065 tree asm_specification;
14066 int ctor_dtor_or_conv_p;
14068 /* Parse the declarator. */
14069 declarator
14070 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14071 &ctor_dtor_or_conv_p,
14072 /*parenthesized_p=*/NULL,
14073 /*member_p=*/true);
14075 /* If something went wrong parsing the declarator, make sure
14076 that we at least consume some tokens. */
14077 if (declarator == cp_error_declarator)
14079 /* Skip to the end of the statement. */
14080 cp_parser_skip_to_end_of_statement (parser);
14081 /* If the next token is not a semicolon, that is
14082 probably because we just skipped over the body of
14083 a function. So, we consume a semicolon if
14084 present, but do not issue an error message if it
14085 is not present. */
14086 if (cp_lexer_next_token_is (parser->lexer,
14087 CPP_SEMICOLON))
14088 cp_lexer_consume_token (parser->lexer);
14089 return;
14092 if (declares_class_or_enum & 2)
14093 cp_parser_check_for_definition_in_return_type
14094 (declarator, decl_specifiers.type);
14096 /* Look for an asm-specification. */
14097 asm_specification = cp_parser_asm_specification_opt (parser);
14098 /* Look for attributes that apply to the declaration. */
14099 attributes = cp_parser_attributes_opt (parser);
14100 /* Remember which attributes are prefix attributes and
14101 which are not. */
14102 first_attribute = attributes;
14103 /* Combine the attributes. */
14104 attributes = chainon (prefix_attributes, attributes);
14106 /* If it's an `=', then we have a constant-initializer or a
14107 pure-specifier. It is not correct to parse the
14108 initializer before registering the member declaration
14109 since the member declaration should be in scope while
14110 its initializer is processed. However, the rest of the
14111 front end does not yet provide an interface that allows
14112 us to handle this correctly. */
14113 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14115 /* In [class.mem]:
14117 A pure-specifier shall be used only in the declaration of
14118 a virtual function.
14120 A member-declarator can contain a constant-initializer
14121 only if it declares a static member of integral or
14122 enumeration type.
14124 Therefore, if the DECLARATOR is for a function, we look
14125 for a pure-specifier; otherwise, we look for a
14126 constant-initializer. When we call `grokfield', it will
14127 perform more stringent semantics checks. */
14128 if (function_declarator_p (declarator))
14129 initializer = cp_parser_pure_specifier (parser);
14130 else
14131 /* Parse the initializer. */
14132 initializer = cp_parser_constant_initializer (parser);
14134 /* Otherwise, there is no initializer. */
14135 else
14136 initializer = NULL_TREE;
14138 /* See if we are probably looking at a function
14139 definition. We are certainly not looking at a
14140 member-declarator. Calling `grokfield' has
14141 side-effects, so we must not do it unless we are sure
14142 that we are looking at a member-declarator. */
14143 if (cp_parser_token_starts_function_definition_p
14144 (cp_lexer_peek_token (parser->lexer)))
14146 /* The grammar does not allow a pure-specifier to be
14147 used when a member function is defined. (It is
14148 possible that this fact is an oversight in the
14149 standard, since a pure function may be defined
14150 outside of the class-specifier. */
14151 if (initializer)
14152 error ("pure-specifier on function-definition");
14153 decl = cp_parser_save_member_function_body (parser,
14154 &decl_specifiers,
14155 declarator,
14156 attributes);
14157 /* If the member was not a friend, declare it here. */
14158 if (!friend_p)
14159 finish_member_declaration (decl);
14160 /* Peek at the next token. */
14161 token = cp_lexer_peek_token (parser->lexer);
14162 /* If the next token is a semicolon, consume it. */
14163 if (token->type == CPP_SEMICOLON)
14164 cp_lexer_consume_token (parser->lexer);
14165 return;
14167 else
14168 /* Create the declaration. */
14169 decl = grokfield (declarator, &decl_specifiers,
14170 initializer, /*init_const_expr_p=*/true,
14171 asm_specification,
14172 attributes);
14175 /* Reset PREFIX_ATTRIBUTES. */
14176 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14177 attributes = TREE_CHAIN (attributes);
14178 if (attributes)
14179 TREE_CHAIN (attributes) = NULL_TREE;
14181 /* If there is any qualification still in effect, clear it
14182 now; we will be starting fresh with the next declarator. */
14183 parser->scope = NULL_TREE;
14184 parser->qualifying_scope = NULL_TREE;
14185 parser->object_scope = NULL_TREE;
14186 /* If it's a `,', then there are more declarators. */
14187 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14188 cp_lexer_consume_token (parser->lexer);
14189 /* If the next token isn't a `;', then we have a parse error. */
14190 else if (cp_lexer_next_token_is_not (parser->lexer,
14191 CPP_SEMICOLON))
14193 cp_parser_error (parser, "expected %<;%>");
14194 /* Skip tokens until we find a `;'. */
14195 cp_parser_skip_to_end_of_statement (parser);
14197 break;
14200 if (decl)
14202 /* Add DECL to the list of members. */
14203 if (!friend_p)
14204 finish_member_declaration (decl);
14206 if (TREE_CODE (decl) == FUNCTION_DECL)
14207 cp_parser_save_default_args (parser, decl);
14212 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14215 /* Parse a pure-specifier.
14217 pure-specifier:
14220 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14221 Otherwise, ERROR_MARK_NODE is returned. */
14223 static tree
14224 cp_parser_pure_specifier (cp_parser* parser)
14226 cp_token *token;
14228 /* Look for the `=' token. */
14229 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14230 return error_mark_node;
14231 /* Look for the `0' token. */
14232 token = cp_lexer_consume_token (parser->lexer);
14233 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14234 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14236 cp_parser_error (parser,
14237 "invalid pure specifier (only `= 0' is allowed)");
14238 cp_parser_skip_to_end_of_statement (parser);
14239 return error_mark_node;
14241 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14243 error ("templates may not be %<virtual%>");
14244 return error_mark_node;
14247 return integer_zero_node;
14250 /* Parse a constant-initializer.
14252 constant-initializer:
14253 = constant-expression
14255 Returns a representation of the constant-expression. */
14257 static tree
14258 cp_parser_constant_initializer (cp_parser* parser)
14260 /* Look for the `=' token. */
14261 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14262 return error_mark_node;
14264 /* It is invalid to write:
14266 struct S { static const int i = { 7 }; };
14269 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14271 cp_parser_error (parser,
14272 "a brace-enclosed initializer is not allowed here");
14273 /* Consume the opening brace. */
14274 cp_lexer_consume_token (parser->lexer);
14275 /* Skip the initializer. */
14276 cp_parser_skip_to_closing_brace (parser);
14277 /* Look for the trailing `}'. */
14278 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14280 return error_mark_node;
14283 return cp_parser_constant_expression (parser,
14284 /*allow_non_constant=*/false,
14285 NULL);
14288 /* Derived classes [gram.class.derived] */
14290 /* Parse a base-clause.
14292 base-clause:
14293 : base-specifier-list
14295 base-specifier-list:
14296 base-specifier
14297 base-specifier-list , base-specifier
14299 Returns a TREE_LIST representing the base-classes, in the order in
14300 which they were declared. The representation of each node is as
14301 described by cp_parser_base_specifier.
14303 In the case that no bases are specified, this function will return
14304 NULL_TREE, not ERROR_MARK_NODE. */
14306 static tree
14307 cp_parser_base_clause (cp_parser* parser)
14309 tree bases = NULL_TREE;
14311 /* Look for the `:' that begins the list. */
14312 cp_parser_require (parser, CPP_COLON, "`:'");
14314 /* Scan the base-specifier-list. */
14315 while (true)
14317 cp_token *token;
14318 tree base;
14320 /* Look for the base-specifier. */
14321 base = cp_parser_base_specifier (parser);
14322 /* Add BASE to the front of the list. */
14323 if (base != error_mark_node)
14325 TREE_CHAIN (base) = bases;
14326 bases = base;
14328 /* Peek at the next token. */
14329 token = cp_lexer_peek_token (parser->lexer);
14330 /* If it's not a comma, then the list is complete. */
14331 if (token->type != CPP_COMMA)
14332 break;
14333 /* Consume the `,'. */
14334 cp_lexer_consume_token (parser->lexer);
14337 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14338 base class had a qualified name. However, the next name that
14339 appears is certainly not qualified. */
14340 parser->scope = NULL_TREE;
14341 parser->qualifying_scope = NULL_TREE;
14342 parser->object_scope = NULL_TREE;
14344 return nreverse (bases);
14347 /* Parse a base-specifier.
14349 base-specifier:
14350 :: [opt] nested-name-specifier [opt] class-name
14351 virtual access-specifier [opt] :: [opt] nested-name-specifier
14352 [opt] class-name
14353 access-specifier virtual [opt] :: [opt] nested-name-specifier
14354 [opt] class-name
14356 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14357 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14358 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14359 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14361 static tree
14362 cp_parser_base_specifier (cp_parser* parser)
14364 cp_token *token;
14365 bool done = false;
14366 bool virtual_p = false;
14367 bool duplicate_virtual_error_issued_p = false;
14368 bool duplicate_access_error_issued_p = false;
14369 bool class_scope_p, template_p;
14370 tree access = access_default_node;
14371 tree type;
14373 /* Process the optional `virtual' and `access-specifier'. */
14374 while (!done)
14376 /* Peek at the next token. */
14377 token = cp_lexer_peek_token (parser->lexer);
14378 /* Process `virtual'. */
14379 switch (token->keyword)
14381 case RID_VIRTUAL:
14382 /* If `virtual' appears more than once, issue an error. */
14383 if (virtual_p && !duplicate_virtual_error_issued_p)
14385 cp_parser_error (parser,
14386 "%<virtual%> specified more than once in base-specified");
14387 duplicate_virtual_error_issued_p = true;
14390 virtual_p = true;
14392 /* Consume the `virtual' token. */
14393 cp_lexer_consume_token (parser->lexer);
14395 break;
14397 case RID_PUBLIC:
14398 case RID_PROTECTED:
14399 case RID_PRIVATE:
14400 /* If more than one access specifier appears, issue an
14401 error. */
14402 if (access != access_default_node
14403 && !duplicate_access_error_issued_p)
14405 cp_parser_error (parser,
14406 "more than one access specifier in base-specified");
14407 duplicate_access_error_issued_p = true;
14410 access = ridpointers[(int) token->keyword];
14412 /* Consume the access-specifier. */
14413 cp_lexer_consume_token (parser->lexer);
14415 break;
14417 default:
14418 done = true;
14419 break;
14422 /* It is not uncommon to see programs mechanically, erroneously, use
14423 the 'typename' keyword to denote (dependent) qualified types
14424 as base classes. */
14425 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14427 if (!processing_template_decl)
14428 error ("keyword %<typename%> not allowed outside of templates");
14429 else
14430 error ("keyword %<typename%> not allowed in this context "
14431 "(the base class is implicitly a type)");
14432 cp_lexer_consume_token (parser->lexer);
14435 /* Look for the optional `::' operator. */
14436 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14437 /* Look for the nested-name-specifier. The simplest way to
14438 implement:
14440 [temp.res]
14442 The keyword `typename' is not permitted in a base-specifier or
14443 mem-initializer; in these contexts a qualified name that
14444 depends on a template-parameter is implicitly assumed to be a
14445 type name.
14447 is to pretend that we have seen the `typename' keyword at this
14448 point. */
14449 cp_parser_nested_name_specifier_opt (parser,
14450 /*typename_keyword_p=*/true,
14451 /*check_dependency_p=*/true,
14452 typename_type,
14453 /*is_declaration=*/true);
14454 /* If the base class is given by a qualified name, assume that names
14455 we see are type names or templates, as appropriate. */
14456 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14457 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14459 /* Finally, look for the class-name. */
14460 type = cp_parser_class_name (parser,
14461 class_scope_p,
14462 template_p,
14463 typename_type,
14464 /*check_dependency_p=*/true,
14465 /*class_head_p=*/false,
14466 /*is_declaration=*/true);
14468 if (type == error_mark_node)
14469 return error_mark_node;
14471 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14474 /* Exception handling [gram.exception] */
14476 /* Parse an (optional) exception-specification.
14478 exception-specification:
14479 throw ( type-id-list [opt] )
14481 Returns a TREE_LIST representing the exception-specification. The
14482 TREE_VALUE of each node is a type. */
14484 static tree
14485 cp_parser_exception_specification_opt (cp_parser* parser)
14487 cp_token *token;
14488 tree type_id_list;
14490 /* Peek at the next token. */
14491 token = cp_lexer_peek_token (parser->lexer);
14492 /* If it's not `throw', then there's no exception-specification. */
14493 if (!cp_parser_is_keyword (token, RID_THROW))
14494 return NULL_TREE;
14496 /* Consume the `throw'. */
14497 cp_lexer_consume_token (parser->lexer);
14499 /* Look for the `('. */
14500 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14502 /* Peek at the next token. */
14503 token = cp_lexer_peek_token (parser->lexer);
14504 /* If it's not a `)', then there is a type-id-list. */
14505 if (token->type != CPP_CLOSE_PAREN)
14507 const char *saved_message;
14509 /* Types may not be defined in an exception-specification. */
14510 saved_message = parser->type_definition_forbidden_message;
14511 parser->type_definition_forbidden_message
14512 = "types may not be defined in an exception-specification";
14513 /* Parse the type-id-list. */
14514 type_id_list = cp_parser_type_id_list (parser);
14515 /* Restore the saved message. */
14516 parser->type_definition_forbidden_message = saved_message;
14518 else
14519 type_id_list = empty_except_spec;
14521 /* Look for the `)'. */
14522 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14524 return type_id_list;
14527 /* Parse an (optional) type-id-list.
14529 type-id-list:
14530 type-id
14531 type-id-list , type-id
14533 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14534 in the order that the types were presented. */
14536 static tree
14537 cp_parser_type_id_list (cp_parser* parser)
14539 tree types = NULL_TREE;
14541 while (true)
14543 cp_token *token;
14544 tree type;
14546 /* Get the next type-id. */
14547 type = cp_parser_type_id (parser);
14548 /* Add it to the list. */
14549 types = add_exception_specifier (types, type, /*complain=*/1);
14550 /* Peek at the next token. */
14551 token = cp_lexer_peek_token (parser->lexer);
14552 /* If it is not a `,', we are done. */
14553 if (token->type != CPP_COMMA)
14554 break;
14555 /* Consume the `,'. */
14556 cp_lexer_consume_token (parser->lexer);
14559 return nreverse (types);
14562 /* Parse a try-block.
14564 try-block:
14565 try compound-statement handler-seq */
14567 static tree
14568 cp_parser_try_block (cp_parser* parser)
14570 tree try_block;
14572 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14573 try_block = begin_try_block ();
14574 cp_parser_compound_statement (parser, NULL, true);
14575 finish_try_block (try_block);
14576 cp_parser_handler_seq (parser);
14577 finish_handler_sequence (try_block);
14579 return try_block;
14582 /* Parse a function-try-block.
14584 function-try-block:
14585 try ctor-initializer [opt] function-body handler-seq */
14587 static bool
14588 cp_parser_function_try_block (cp_parser* parser)
14590 tree compound_stmt;
14591 tree try_block;
14592 bool ctor_initializer_p;
14594 /* Look for the `try' keyword. */
14595 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14596 return false;
14597 /* Let the rest of the front-end know where we are. */
14598 try_block = begin_function_try_block (&compound_stmt);
14599 /* Parse the function-body. */
14600 ctor_initializer_p
14601 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14602 /* We're done with the `try' part. */
14603 finish_function_try_block (try_block);
14604 /* Parse the handlers. */
14605 cp_parser_handler_seq (parser);
14606 /* We're done with the handlers. */
14607 finish_function_handler_sequence (try_block, compound_stmt);
14609 return ctor_initializer_p;
14612 /* Parse a handler-seq.
14614 handler-seq:
14615 handler handler-seq [opt] */
14617 static void
14618 cp_parser_handler_seq (cp_parser* parser)
14620 while (true)
14622 cp_token *token;
14624 /* Parse the handler. */
14625 cp_parser_handler (parser);
14626 /* Peek at the next token. */
14627 token = cp_lexer_peek_token (parser->lexer);
14628 /* If it's not `catch' then there are no more handlers. */
14629 if (!cp_parser_is_keyword (token, RID_CATCH))
14630 break;
14634 /* Parse a handler.
14636 handler:
14637 catch ( exception-declaration ) compound-statement */
14639 static void
14640 cp_parser_handler (cp_parser* parser)
14642 tree handler;
14643 tree declaration;
14645 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14646 handler = begin_handler ();
14647 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14648 declaration = cp_parser_exception_declaration (parser);
14649 finish_handler_parms (declaration, handler);
14650 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14651 cp_parser_compound_statement (parser, NULL, false);
14652 finish_handler (handler);
14655 /* Parse an exception-declaration.
14657 exception-declaration:
14658 type-specifier-seq declarator
14659 type-specifier-seq abstract-declarator
14660 type-specifier-seq
14663 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14664 ellipsis variant is used. */
14666 static tree
14667 cp_parser_exception_declaration (cp_parser* parser)
14669 cp_decl_specifier_seq type_specifiers;
14670 cp_declarator *declarator;
14671 const char *saved_message;
14673 /* If it's an ellipsis, it's easy to handle. */
14674 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14676 /* Consume the `...' token. */
14677 cp_lexer_consume_token (parser->lexer);
14678 return NULL_TREE;
14681 /* Types may not be defined in exception-declarations. */
14682 saved_message = parser->type_definition_forbidden_message;
14683 parser->type_definition_forbidden_message
14684 = "types may not be defined in exception-declarations";
14686 /* Parse the type-specifier-seq. */
14687 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14688 &type_specifiers);
14689 /* If it's a `)', then there is no declarator. */
14690 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14691 declarator = NULL;
14692 else
14693 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14694 /*ctor_dtor_or_conv_p=*/NULL,
14695 /*parenthesized_p=*/NULL,
14696 /*member_p=*/false);
14698 /* Restore the saved message. */
14699 parser->type_definition_forbidden_message = saved_message;
14701 if (!type_specifiers.any_specifiers_p)
14702 return error_mark_node;
14704 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14707 /* Parse a throw-expression.
14709 throw-expression:
14710 throw assignment-expression [opt]
14712 Returns a THROW_EXPR representing the throw-expression. */
14714 static tree
14715 cp_parser_throw_expression (cp_parser* parser)
14717 tree expression;
14718 cp_token* token;
14720 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14721 token = cp_lexer_peek_token (parser->lexer);
14722 /* Figure out whether or not there is an assignment-expression
14723 following the "throw" keyword. */
14724 if (token->type == CPP_COMMA
14725 || token->type == CPP_SEMICOLON
14726 || token->type == CPP_CLOSE_PAREN
14727 || token->type == CPP_CLOSE_SQUARE
14728 || token->type == CPP_CLOSE_BRACE
14729 || token->type == CPP_COLON)
14730 expression = NULL_TREE;
14731 else
14732 expression = cp_parser_assignment_expression (parser,
14733 /*cast_p=*/false);
14735 return build_throw (expression);
14738 /* GNU Extensions */
14740 /* Parse an (optional) asm-specification.
14742 asm-specification:
14743 asm ( string-literal )
14745 If the asm-specification is present, returns a STRING_CST
14746 corresponding to the string-literal. Otherwise, returns
14747 NULL_TREE. */
14749 static tree
14750 cp_parser_asm_specification_opt (cp_parser* parser)
14752 cp_token *token;
14753 tree asm_specification;
14755 /* Peek at the next token. */
14756 token = cp_lexer_peek_token (parser->lexer);
14757 /* If the next token isn't the `asm' keyword, then there's no
14758 asm-specification. */
14759 if (!cp_parser_is_keyword (token, RID_ASM))
14760 return NULL_TREE;
14762 /* Consume the `asm' token. */
14763 cp_lexer_consume_token (parser->lexer);
14764 /* Look for the `('. */
14765 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14767 /* Look for the string-literal. */
14768 asm_specification = cp_parser_string_literal (parser, false, false);
14770 /* Look for the `)'. */
14771 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14773 return asm_specification;
14776 /* Parse an asm-operand-list.
14778 asm-operand-list:
14779 asm-operand
14780 asm-operand-list , asm-operand
14782 asm-operand:
14783 string-literal ( expression )
14784 [ string-literal ] string-literal ( expression )
14786 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14787 each node is the expression. The TREE_PURPOSE is itself a
14788 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14789 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14790 is a STRING_CST for the string literal before the parenthesis. */
14792 static tree
14793 cp_parser_asm_operand_list (cp_parser* parser)
14795 tree asm_operands = NULL_TREE;
14797 while (true)
14799 tree string_literal;
14800 tree expression;
14801 tree name;
14803 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14805 /* Consume the `[' token. */
14806 cp_lexer_consume_token (parser->lexer);
14807 /* Read the operand name. */
14808 name = cp_parser_identifier (parser);
14809 if (name != error_mark_node)
14810 name = build_string (IDENTIFIER_LENGTH (name),
14811 IDENTIFIER_POINTER (name));
14812 /* Look for the closing `]'. */
14813 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14815 else
14816 name = NULL_TREE;
14817 /* Look for the string-literal. */
14818 string_literal = cp_parser_string_literal (parser, false, false);
14820 /* Look for the `('. */
14821 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14822 /* Parse the expression. */
14823 expression = cp_parser_expression (parser, /*cast_p=*/false);
14824 /* Look for the `)'. */
14825 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14827 /* Add this operand to the list. */
14828 asm_operands = tree_cons (build_tree_list (name, string_literal),
14829 expression,
14830 asm_operands);
14831 /* If the next token is not a `,', there are no more
14832 operands. */
14833 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14834 break;
14835 /* Consume the `,'. */
14836 cp_lexer_consume_token (parser->lexer);
14839 return nreverse (asm_operands);
14842 /* Parse an asm-clobber-list.
14844 asm-clobber-list:
14845 string-literal
14846 asm-clobber-list , string-literal
14848 Returns a TREE_LIST, indicating the clobbers in the order that they
14849 appeared. The TREE_VALUE of each node is a STRING_CST. */
14851 static tree
14852 cp_parser_asm_clobber_list (cp_parser* parser)
14854 tree clobbers = NULL_TREE;
14856 while (true)
14858 tree string_literal;
14860 /* Look for the string literal. */
14861 string_literal = cp_parser_string_literal (parser, false, false);
14862 /* Add it to the list. */
14863 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14864 /* If the next token is not a `,', then the list is
14865 complete. */
14866 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14867 break;
14868 /* Consume the `,' token. */
14869 cp_lexer_consume_token (parser->lexer);
14872 return clobbers;
14875 /* Parse an (optional) series of attributes.
14877 attributes:
14878 attributes attribute
14880 attribute:
14881 __attribute__ (( attribute-list [opt] ))
14883 The return value is as for cp_parser_attribute_list. */
14885 static tree
14886 cp_parser_attributes_opt (cp_parser* parser)
14888 tree attributes = NULL_TREE;
14890 while (true)
14892 cp_token *token;
14893 tree attribute_list;
14895 /* Peek at the next token. */
14896 token = cp_lexer_peek_token (parser->lexer);
14897 /* If it's not `__attribute__', then we're done. */
14898 if (token->keyword != RID_ATTRIBUTE)
14899 break;
14901 /* Consume the `__attribute__' keyword. */
14902 cp_lexer_consume_token (parser->lexer);
14903 /* Look for the two `(' tokens. */
14904 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14905 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14907 /* Peek at the next token. */
14908 token = cp_lexer_peek_token (parser->lexer);
14909 if (token->type != CPP_CLOSE_PAREN)
14910 /* Parse the attribute-list. */
14911 attribute_list = cp_parser_attribute_list (parser);
14912 else
14913 /* If the next token is a `)', then there is no attribute
14914 list. */
14915 attribute_list = NULL;
14917 /* Look for the two `)' tokens. */
14918 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14919 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14921 /* Add these new attributes to the list. */
14922 attributes = chainon (attributes, attribute_list);
14925 return attributes;
14928 /* Parse an attribute-list.
14930 attribute-list:
14931 attribute
14932 attribute-list , attribute
14934 attribute:
14935 identifier
14936 identifier ( identifier )
14937 identifier ( identifier , expression-list )
14938 identifier ( expression-list )
14940 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14941 to an attribute. The TREE_PURPOSE of each node is the identifier
14942 indicating which attribute is in use. The TREE_VALUE represents
14943 the arguments, if any. */
14945 static tree
14946 cp_parser_attribute_list (cp_parser* parser)
14948 tree attribute_list = NULL_TREE;
14949 bool save_translate_strings_p = parser->translate_strings_p;
14951 parser->translate_strings_p = false;
14952 while (true)
14954 cp_token *token;
14955 tree identifier;
14956 tree attribute;
14958 /* Look for the identifier. We also allow keywords here; for
14959 example `__attribute__ ((const))' is legal. */
14960 token = cp_lexer_peek_token (parser->lexer);
14961 if (token->type == CPP_NAME
14962 || token->type == CPP_KEYWORD)
14964 tree arguments = NULL_TREE;
14966 /* Consume the token. */
14967 token = cp_lexer_consume_token (parser->lexer);
14969 /* Save away the identifier that indicates which attribute
14970 this is. */
14971 identifier = token->u.value;
14972 attribute = build_tree_list (identifier, NULL_TREE);
14974 /* Peek at the next token. */
14975 token = cp_lexer_peek_token (parser->lexer);
14976 /* If it's an `(', then parse the attribute arguments. */
14977 if (token->type == CPP_OPEN_PAREN)
14979 arguments = cp_parser_parenthesized_expression_list
14980 (parser, true, /*cast_p=*/false,
14981 /*non_constant_p=*/NULL);
14982 /* Save the arguments away. */
14983 TREE_VALUE (attribute) = arguments;
14986 if (arguments != error_mark_node)
14988 /* Add this attribute to the list. */
14989 TREE_CHAIN (attribute) = attribute_list;
14990 attribute_list = attribute;
14993 token = cp_lexer_peek_token (parser->lexer);
14995 /* Now, look for more attributes. If the next token isn't a
14996 `,', we're done. */
14997 if (token->type != CPP_COMMA)
14998 break;
15000 /* Consume the comma and keep going. */
15001 cp_lexer_consume_token (parser->lexer);
15003 parser->translate_strings_p = save_translate_strings_p;
15005 /* We built up the list in reverse order. */
15006 return nreverse (attribute_list);
15009 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15010 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15011 current value of the PEDANTIC flag, regardless of whether or not
15012 the `__extension__' keyword is present. The caller is responsible
15013 for restoring the value of the PEDANTIC flag. */
15015 static bool
15016 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15018 /* Save the old value of the PEDANTIC flag. */
15019 *saved_pedantic = pedantic;
15021 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15023 /* Consume the `__extension__' token. */
15024 cp_lexer_consume_token (parser->lexer);
15025 /* We're not being pedantic while the `__extension__' keyword is
15026 in effect. */
15027 pedantic = 0;
15029 return true;
15032 return false;
15035 /* Parse a label declaration.
15037 label-declaration:
15038 __label__ label-declarator-seq ;
15040 label-declarator-seq:
15041 identifier , label-declarator-seq
15042 identifier */
15044 static void
15045 cp_parser_label_declaration (cp_parser* parser)
15047 /* Look for the `__label__' keyword. */
15048 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15050 while (true)
15052 tree identifier;
15054 /* Look for an identifier. */
15055 identifier = cp_parser_identifier (parser);
15056 /* If we failed, stop. */
15057 if (identifier == error_mark_node)
15058 break;
15059 /* Declare it as a label. */
15060 finish_label_decl (identifier);
15061 /* If the next token is a `;', stop. */
15062 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15063 break;
15064 /* Look for the `,' separating the label declarations. */
15065 cp_parser_require (parser, CPP_COMMA, "`,'");
15068 /* Look for the final `;'. */
15069 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15072 /* Support Functions */
15074 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15075 NAME should have one of the representations used for an
15076 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15077 is returned. If PARSER->SCOPE is a dependent type, then a
15078 SCOPE_REF is returned.
15080 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15081 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15082 was formed. Abstractly, such entities should not be passed to this
15083 function, because they do not need to be looked up, but it is
15084 simpler to check for this special case here, rather than at the
15085 call-sites.
15087 In cases not explicitly covered above, this function returns a
15088 DECL, OVERLOAD, or baselink representing the result of the lookup.
15089 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15090 is returned.
15092 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15093 (e.g., "struct") that was used. In that case bindings that do not
15094 refer to types are ignored.
15096 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15097 ignored.
15099 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15100 are ignored.
15102 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15103 types.
15105 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15106 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15107 NULL_TREE otherwise. */
15109 static tree
15110 cp_parser_lookup_name (cp_parser *parser, tree name,
15111 enum tag_types tag_type,
15112 bool is_template,
15113 bool is_namespace,
15114 bool check_dependency,
15115 tree *ambiguous_decls)
15117 int flags = 0;
15118 tree decl;
15119 tree object_type = parser->context->object_type;
15121 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15122 flags |= LOOKUP_COMPLAIN;
15124 /* Assume that the lookup will be unambiguous. */
15125 if (ambiguous_decls)
15126 *ambiguous_decls = NULL_TREE;
15128 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15129 no longer valid. Note that if we are parsing tentatively, and
15130 the parse fails, OBJECT_TYPE will be automatically restored. */
15131 parser->context->object_type = NULL_TREE;
15133 if (name == error_mark_node)
15134 return error_mark_node;
15136 /* A template-id has already been resolved; there is no lookup to
15137 do. */
15138 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15139 return name;
15140 if (BASELINK_P (name))
15142 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15143 == TEMPLATE_ID_EXPR);
15144 return name;
15147 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15148 it should already have been checked to make sure that the name
15149 used matches the type being destroyed. */
15150 if (TREE_CODE (name) == BIT_NOT_EXPR)
15152 tree type;
15154 /* Figure out to which type this destructor applies. */
15155 if (parser->scope)
15156 type = parser->scope;
15157 else if (object_type)
15158 type = object_type;
15159 else
15160 type = current_class_type;
15161 /* If that's not a class type, there is no destructor. */
15162 if (!type || !CLASS_TYPE_P (type))
15163 return error_mark_node;
15164 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15165 lazily_declare_fn (sfk_destructor, type);
15166 if (!CLASSTYPE_DESTRUCTORS (type))
15167 return error_mark_node;
15168 /* If it was a class type, return the destructor. */
15169 return CLASSTYPE_DESTRUCTORS (type);
15172 /* By this point, the NAME should be an ordinary identifier. If
15173 the id-expression was a qualified name, the qualifying scope is
15174 stored in PARSER->SCOPE at this point. */
15175 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15177 /* Perform the lookup. */
15178 if (parser->scope)
15180 bool dependent_p;
15182 if (parser->scope == error_mark_node)
15183 return error_mark_node;
15185 /* If the SCOPE is dependent, the lookup must be deferred until
15186 the template is instantiated -- unless we are explicitly
15187 looking up names in uninstantiated templates. Even then, we
15188 cannot look up the name if the scope is not a class type; it
15189 might, for example, be a template type parameter. */
15190 dependent_p = (TYPE_P (parser->scope)
15191 && !(parser->in_declarator_p
15192 && currently_open_class (parser->scope))
15193 && dependent_type_p (parser->scope));
15194 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15195 && dependent_p)
15197 if (tag_type)
15199 tree type;
15201 /* The resolution to Core Issue 180 says that `struct
15202 A::B' should be considered a type-name, even if `A'
15203 is dependent. */
15204 type = make_typename_type (parser->scope, name, tag_type,
15205 /*complain=*/tf_error);
15206 decl = TYPE_NAME (type);
15208 else if (is_template
15209 && (cp_parser_next_token_ends_template_argument_p (parser)
15210 || cp_lexer_next_token_is (parser->lexer,
15211 CPP_CLOSE_PAREN)))
15212 decl = make_unbound_class_template (parser->scope,
15213 name, NULL_TREE,
15214 /*complain=*/tf_error);
15215 else
15216 decl = build_qualified_name (/*type=*/NULL_TREE,
15217 parser->scope, name,
15218 is_template);
15220 else
15222 tree pushed_scope = NULL_TREE;
15224 /* If PARSER->SCOPE is a dependent type, then it must be a
15225 class type, and we must not be checking dependencies;
15226 otherwise, we would have processed this lookup above. So
15227 that PARSER->SCOPE is not considered a dependent base by
15228 lookup_member, we must enter the scope here. */
15229 if (dependent_p)
15230 pushed_scope = push_scope (parser->scope);
15231 /* If the PARSER->SCOPE is a template specialization, it
15232 may be instantiated during name lookup. In that case,
15233 errors may be issued. Even if we rollback the current
15234 tentative parse, those errors are valid. */
15235 decl = lookup_qualified_name (parser->scope, name,
15236 tag_type != none_type,
15237 /*complain=*/true);
15238 if (pushed_scope)
15239 pop_scope (pushed_scope);
15241 parser->qualifying_scope = parser->scope;
15242 parser->object_scope = NULL_TREE;
15244 else if (object_type)
15246 tree object_decl = NULL_TREE;
15247 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15248 OBJECT_TYPE is not a class. */
15249 if (CLASS_TYPE_P (object_type))
15250 /* If the OBJECT_TYPE is a template specialization, it may
15251 be instantiated during name lookup. In that case, errors
15252 may be issued. Even if we rollback the current tentative
15253 parse, those errors are valid. */
15254 object_decl = lookup_member (object_type,
15255 name,
15256 /*protect=*/0,
15257 tag_type != none_type);
15258 /* Look it up in the enclosing context, too. */
15259 decl = lookup_name_real (name, tag_type != none_type,
15260 /*nonclass=*/0,
15261 /*block_p=*/true, is_namespace, flags);
15262 parser->object_scope = object_type;
15263 parser->qualifying_scope = NULL_TREE;
15264 if (object_decl)
15265 decl = object_decl;
15267 else
15269 decl = lookup_name_real (name, tag_type != none_type,
15270 /*nonclass=*/0,
15271 /*block_p=*/true, is_namespace, flags);
15272 parser->qualifying_scope = NULL_TREE;
15273 parser->object_scope = NULL_TREE;
15276 /* If the lookup failed, let our caller know. */
15277 if (!decl || decl == error_mark_node)
15278 return error_mark_node;
15280 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15281 if (TREE_CODE (decl) == TREE_LIST)
15283 if (ambiguous_decls)
15284 *ambiguous_decls = decl;
15285 /* The error message we have to print is too complicated for
15286 cp_parser_error, so we incorporate its actions directly. */
15287 if (!cp_parser_simulate_error (parser))
15289 error ("reference to %qD is ambiguous", name);
15290 print_candidates (decl);
15292 return error_mark_node;
15295 gcc_assert (DECL_P (decl)
15296 || TREE_CODE (decl) == OVERLOAD
15297 || TREE_CODE (decl) == SCOPE_REF
15298 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15299 || BASELINK_P (decl));
15301 /* If we have resolved the name of a member declaration, check to
15302 see if the declaration is accessible. When the name resolves to
15303 set of overloaded functions, accessibility is checked when
15304 overload resolution is done.
15306 During an explicit instantiation, access is not checked at all,
15307 as per [temp.explicit]. */
15308 if (DECL_P (decl))
15309 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15311 return decl;
15314 /* Like cp_parser_lookup_name, but for use in the typical case where
15315 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15316 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15318 static tree
15319 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15321 return cp_parser_lookup_name (parser, name,
15322 none_type,
15323 /*is_template=*/false,
15324 /*is_namespace=*/false,
15325 /*check_dependency=*/true,
15326 /*ambiguous_decls=*/NULL);
15329 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15330 the current context, return the TYPE_DECL. If TAG_NAME_P is
15331 true, the DECL indicates the class being defined in a class-head,
15332 or declared in an elaborated-type-specifier.
15334 Otherwise, return DECL. */
15336 static tree
15337 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15339 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15340 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15342 struct A {
15343 template <typename T> struct B;
15346 template <typename T> struct A::B {};
15348 Similarly, in an elaborated-type-specifier:
15350 namespace N { struct X{}; }
15352 struct A {
15353 template <typename T> friend struct N::X;
15356 However, if the DECL refers to a class type, and we are in
15357 the scope of the class, then the name lookup automatically
15358 finds the TYPE_DECL created by build_self_reference rather
15359 than a TEMPLATE_DECL. For example, in:
15361 template <class T> struct S {
15362 S s;
15365 there is no need to handle such case. */
15367 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15368 return DECL_TEMPLATE_RESULT (decl);
15370 return decl;
15373 /* If too many, or too few, template-parameter lists apply to the
15374 declarator, issue an error message. Returns TRUE if all went well,
15375 and FALSE otherwise. */
15377 static bool
15378 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15379 cp_declarator *declarator)
15381 unsigned num_templates;
15383 /* We haven't seen any classes that involve template parameters yet. */
15384 num_templates = 0;
15386 switch (declarator->kind)
15388 case cdk_id:
15389 if (declarator->u.id.qualifying_scope)
15391 tree scope;
15392 tree member;
15394 scope = declarator->u.id.qualifying_scope;
15395 member = declarator->u.id.unqualified_name;
15397 while (scope && CLASS_TYPE_P (scope))
15399 /* You're supposed to have one `template <...>'
15400 for every template class, but you don't need one
15401 for a full specialization. For example:
15403 template <class T> struct S{};
15404 template <> struct S<int> { void f(); };
15405 void S<int>::f () {}
15407 is correct; there shouldn't be a `template <>' for
15408 the definition of `S<int>::f'. */
15409 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15410 /* If SCOPE does not have template information of any
15411 kind, then it is not a template, nor is it nested
15412 within a template. */
15413 break;
15414 if (explicit_class_specialization_p (scope))
15415 break;
15416 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15417 ++num_templates;
15419 scope = TYPE_CONTEXT (scope);
15422 else if (TREE_CODE (declarator->u.id.unqualified_name)
15423 == TEMPLATE_ID_EXPR)
15424 /* If the DECLARATOR has the form `X<y>' then it uses one
15425 additional level of template parameters. */
15426 ++num_templates;
15428 return cp_parser_check_template_parameters (parser,
15429 num_templates);
15431 case cdk_function:
15432 case cdk_array:
15433 case cdk_pointer:
15434 case cdk_reference:
15435 case cdk_ptrmem:
15436 return (cp_parser_check_declarator_template_parameters
15437 (parser, declarator->declarator));
15439 case cdk_error:
15440 return true;
15442 default:
15443 gcc_unreachable ();
15445 return false;
15448 /* NUM_TEMPLATES were used in the current declaration. If that is
15449 invalid, return FALSE and issue an error messages. Otherwise,
15450 return TRUE. */
15452 static bool
15453 cp_parser_check_template_parameters (cp_parser* parser,
15454 unsigned num_templates)
15456 /* If there are more template classes than parameter lists, we have
15457 something like:
15459 template <class T> void S<T>::R<T>::f (); */
15460 if (parser->num_template_parameter_lists < num_templates)
15462 error ("too few template-parameter-lists");
15463 return false;
15465 /* If there are the same number of template classes and parameter
15466 lists, that's OK. */
15467 if (parser->num_template_parameter_lists == num_templates)
15468 return true;
15469 /* If there are more, but only one more, then we are referring to a
15470 member template. That's OK too. */
15471 if (parser->num_template_parameter_lists == num_templates + 1)
15472 return true;
15473 /* Otherwise, there are too many template parameter lists. We have
15474 something like:
15476 template <class T> template <class U> void S::f(); */
15477 error ("too many template-parameter-lists");
15478 return false;
15481 /* Parse an optional `::' token indicating that the following name is
15482 from the global namespace. If so, PARSER->SCOPE is set to the
15483 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15484 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15485 Returns the new value of PARSER->SCOPE, if the `::' token is
15486 present, and NULL_TREE otherwise. */
15488 static tree
15489 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15491 cp_token *token;
15493 /* Peek at the next token. */
15494 token = cp_lexer_peek_token (parser->lexer);
15495 /* If we're looking at a `::' token then we're starting from the
15496 global namespace, not our current location. */
15497 if (token->type == CPP_SCOPE)
15499 /* Consume the `::' token. */
15500 cp_lexer_consume_token (parser->lexer);
15501 /* Set the SCOPE so that we know where to start the lookup. */
15502 parser->scope = global_namespace;
15503 parser->qualifying_scope = global_namespace;
15504 parser->object_scope = NULL_TREE;
15506 return parser->scope;
15508 else if (!current_scope_valid_p)
15510 parser->scope = NULL_TREE;
15511 parser->qualifying_scope = NULL_TREE;
15512 parser->object_scope = NULL_TREE;
15515 return NULL_TREE;
15518 /* Returns TRUE if the upcoming token sequence is the start of a
15519 constructor declarator. If FRIEND_P is true, the declarator is
15520 preceded by the `friend' specifier. */
15522 static bool
15523 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15525 bool constructor_p;
15526 tree type_decl = NULL_TREE;
15527 bool nested_name_p;
15528 cp_token *next_token;
15530 /* The common case is that this is not a constructor declarator, so
15531 try to avoid doing lots of work if at all possible. It's not
15532 valid declare a constructor at function scope. */
15533 if (parser->in_function_body)
15534 return false;
15535 /* And only certain tokens can begin a constructor declarator. */
15536 next_token = cp_lexer_peek_token (parser->lexer);
15537 if (next_token->type != CPP_NAME
15538 && next_token->type != CPP_SCOPE
15539 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15540 && next_token->type != CPP_TEMPLATE_ID)
15541 return false;
15543 /* Parse tentatively; we are going to roll back all of the tokens
15544 consumed here. */
15545 cp_parser_parse_tentatively (parser);
15546 /* Assume that we are looking at a constructor declarator. */
15547 constructor_p = true;
15549 /* Look for the optional `::' operator. */
15550 cp_parser_global_scope_opt (parser,
15551 /*current_scope_valid_p=*/false);
15552 /* Look for the nested-name-specifier. */
15553 nested_name_p
15554 = (cp_parser_nested_name_specifier_opt (parser,
15555 /*typename_keyword_p=*/false,
15556 /*check_dependency_p=*/false,
15557 /*type_p=*/false,
15558 /*is_declaration=*/false)
15559 != NULL_TREE);
15560 /* Outside of a class-specifier, there must be a
15561 nested-name-specifier. */
15562 if (!nested_name_p &&
15563 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15564 || friend_p))
15565 constructor_p = false;
15566 /* If we still think that this might be a constructor-declarator,
15567 look for a class-name. */
15568 if (constructor_p)
15570 /* If we have:
15572 template <typename T> struct S { S(); };
15573 template <typename T> S<T>::S ();
15575 we must recognize that the nested `S' names a class.
15576 Similarly, for:
15578 template <typename T> S<T>::S<T> ();
15580 we must recognize that the nested `S' names a template. */
15581 type_decl = cp_parser_class_name (parser,
15582 /*typename_keyword_p=*/false,
15583 /*template_keyword_p=*/false,
15584 none_type,
15585 /*check_dependency_p=*/false,
15586 /*class_head_p=*/false,
15587 /*is_declaration=*/false);
15588 /* If there was no class-name, then this is not a constructor. */
15589 constructor_p = !cp_parser_error_occurred (parser);
15592 /* If we're still considering a constructor, we have to see a `(',
15593 to begin the parameter-declaration-clause, followed by either a
15594 `)', an `...', or a decl-specifier. We need to check for a
15595 type-specifier to avoid being fooled into thinking that:
15597 S::S (f) (int);
15599 is a constructor. (It is actually a function named `f' that
15600 takes one parameter (of type `int') and returns a value of type
15601 `S::S'. */
15602 if (constructor_p
15603 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15606 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15607 /* A parameter declaration begins with a decl-specifier,
15608 which is either the "attribute" keyword, a storage class
15609 specifier, or (usually) a type-specifier. */
15610 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15612 tree type;
15613 tree pushed_scope = NULL_TREE;
15614 unsigned saved_num_template_parameter_lists;
15616 /* Names appearing in the type-specifier should be looked up
15617 in the scope of the class. */
15618 if (current_class_type)
15619 type = NULL_TREE;
15620 else
15622 type = TREE_TYPE (type_decl);
15623 if (TREE_CODE (type) == TYPENAME_TYPE)
15625 type = resolve_typename_type (type,
15626 /*only_current_p=*/false);
15627 if (type == error_mark_node)
15629 cp_parser_abort_tentative_parse (parser);
15630 return false;
15633 pushed_scope = push_scope (type);
15636 /* Inside the constructor parameter list, surrounding
15637 template-parameter-lists do not apply. */
15638 saved_num_template_parameter_lists
15639 = parser->num_template_parameter_lists;
15640 parser->num_template_parameter_lists = 0;
15642 /* Look for the type-specifier. */
15643 cp_parser_type_specifier (parser,
15644 CP_PARSER_FLAGS_NONE,
15645 /*decl_specs=*/NULL,
15646 /*is_declarator=*/true,
15647 /*declares_class_or_enum=*/NULL,
15648 /*is_cv_qualifier=*/NULL);
15650 parser->num_template_parameter_lists
15651 = saved_num_template_parameter_lists;
15653 /* Leave the scope of the class. */
15654 if (pushed_scope)
15655 pop_scope (pushed_scope);
15657 constructor_p = !cp_parser_error_occurred (parser);
15660 else
15661 constructor_p = false;
15662 /* We did not really want to consume any tokens. */
15663 cp_parser_abort_tentative_parse (parser);
15665 return constructor_p;
15668 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15669 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15670 they must be performed once we are in the scope of the function.
15672 Returns the function defined. */
15674 static tree
15675 cp_parser_function_definition_from_specifiers_and_declarator
15676 (cp_parser* parser,
15677 cp_decl_specifier_seq *decl_specifiers,
15678 tree attributes,
15679 const cp_declarator *declarator)
15681 tree fn;
15682 bool success_p;
15684 /* Begin the function-definition. */
15685 success_p = start_function (decl_specifiers, declarator, attributes);
15687 /* The things we're about to see are not directly qualified by any
15688 template headers we've seen thus far. */
15689 reset_specialization ();
15691 /* If there were names looked up in the decl-specifier-seq that we
15692 did not check, check them now. We must wait until we are in the
15693 scope of the function to perform the checks, since the function
15694 might be a friend. */
15695 perform_deferred_access_checks ();
15697 if (!success_p)
15699 /* Skip the entire function. */
15700 cp_parser_skip_to_end_of_block_or_statement (parser);
15701 fn = error_mark_node;
15703 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
15705 /* Seen already, skip it. An error message has already been output. */
15706 cp_parser_skip_to_end_of_block_or_statement (parser);
15707 fn = current_function_decl;
15708 current_function_decl = NULL_TREE;
15709 /* If this is a function from a class, pop the nested class. */
15710 if (current_class_name)
15711 pop_nested_class ();
15713 else
15714 fn = cp_parser_function_definition_after_declarator (parser,
15715 /*inline_p=*/false);
15717 return fn;
15720 /* Parse the part of a function-definition that follows the
15721 declarator. INLINE_P is TRUE iff this function is an inline
15722 function defined with a class-specifier.
15724 Returns the function defined. */
15726 static tree
15727 cp_parser_function_definition_after_declarator (cp_parser* parser,
15728 bool inline_p)
15730 tree fn;
15731 bool ctor_initializer_p = false;
15732 bool saved_in_unbraced_linkage_specification_p;
15733 bool saved_in_function_body;
15734 unsigned saved_num_template_parameter_lists;
15736 saved_in_function_body = parser->in_function_body;
15737 parser->in_function_body = true;
15738 /* If the next token is `return', then the code may be trying to
15739 make use of the "named return value" extension that G++ used to
15740 support. */
15741 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15743 /* Consume the `return' keyword. */
15744 cp_lexer_consume_token (parser->lexer);
15745 /* Look for the identifier that indicates what value is to be
15746 returned. */
15747 cp_parser_identifier (parser);
15748 /* Issue an error message. */
15749 error ("named return values are no longer supported");
15750 /* Skip tokens until we reach the start of the function body. */
15751 while (true)
15753 cp_token *token = cp_lexer_peek_token (parser->lexer);
15754 if (token->type == CPP_OPEN_BRACE
15755 || token->type == CPP_EOF
15756 || token->type == CPP_PRAGMA_EOL)
15757 break;
15758 cp_lexer_consume_token (parser->lexer);
15761 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15762 anything declared inside `f'. */
15763 saved_in_unbraced_linkage_specification_p
15764 = parser->in_unbraced_linkage_specification_p;
15765 parser->in_unbraced_linkage_specification_p = false;
15766 /* Inside the function, surrounding template-parameter-lists do not
15767 apply. */
15768 saved_num_template_parameter_lists
15769 = parser->num_template_parameter_lists;
15770 parser->num_template_parameter_lists = 0;
15771 /* If the next token is `try', then we are looking at a
15772 function-try-block. */
15773 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15774 ctor_initializer_p = cp_parser_function_try_block (parser);
15775 /* A function-try-block includes the function-body, so we only do
15776 this next part if we're not processing a function-try-block. */
15777 else
15778 ctor_initializer_p
15779 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15781 /* Finish the function. */
15782 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15783 (inline_p ? 2 : 0));
15784 /* Generate code for it, if necessary. */
15785 expand_or_defer_fn (fn);
15786 /* Restore the saved values. */
15787 parser->in_unbraced_linkage_specification_p
15788 = saved_in_unbraced_linkage_specification_p;
15789 parser->num_template_parameter_lists
15790 = saved_num_template_parameter_lists;
15791 parser->in_function_body = saved_in_function_body;
15793 return fn;
15796 /* Parse a template-declaration, assuming that the `export' (and
15797 `extern') keywords, if present, has already been scanned. MEMBER_P
15798 is as for cp_parser_template_declaration. */
15800 static void
15801 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15803 tree decl = NULL_TREE;
15804 VEC (deferred_access_check,gc) *checks;
15805 tree parameter_list;
15806 bool friend_p = false;
15807 bool need_lang_pop;
15809 /* Look for the `template' keyword. */
15810 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15811 return;
15813 /* And the `<'. */
15814 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15815 return;
15816 if (at_class_scope_p () && current_function_decl)
15818 /* 14.5.2.2 [temp.mem]
15820 A local class shall not have member templates. */
15821 error ("invalid declaration of member template in local class");
15822 cp_parser_skip_to_end_of_block_or_statement (parser);
15823 return;
15825 /* [temp]
15827 A template ... shall not have C linkage. */
15828 if (current_lang_name == lang_name_c)
15830 error ("template with C linkage");
15831 /* Give it C++ linkage to avoid confusing other parts of the
15832 front end. */
15833 push_lang_context (lang_name_cplusplus);
15834 need_lang_pop = true;
15836 else
15837 need_lang_pop = false;
15839 /* We cannot perform access checks on the template parameter
15840 declarations until we know what is being declared, just as we
15841 cannot check the decl-specifier list. */
15842 push_deferring_access_checks (dk_deferred);
15844 /* If the next token is `>', then we have an invalid
15845 specialization. Rather than complain about an invalid template
15846 parameter, issue an error message here. */
15847 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15849 cp_parser_error (parser, "invalid explicit specialization");
15850 begin_specialization ();
15851 parameter_list = NULL_TREE;
15853 else
15854 /* Parse the template parameters. */
15855 parameter_list = cp_parser_template_parameter_list (parser);
15857 /* Get the deferred access checks from the parameter list. These
15858 will be checked once we know what is being declared, as for a
15859 member template the checks must be performed in the scope of the
15860 class containing the member. */
15861 checks = get_deferred_access_checks ();
15863 /* Look for the `>'. */
15864 cp_parser_skip_to_end_of_template_parameter_list (parser);
15865 /* We just processed one more parameter list. */
15866 ++parser->num_template_parameter_lists;
15867 /* If the next token is `template', there are more template
15868 parameters. */
15869 if (cp_lexer_next_token_is_keyword (parser->lexer,
15870 RID_TEMPLATE))
15871 cp_parser_template_declaration_after_export (parser, member_p);
15872 else
15874 /* There are no access checks when parsing a template, as we do not
15875 know if a specialization will be a friend. */
15876 push_deferring_access_checks (dk_no_check);
15877 decl = cp_parser_single_declaration (parser,
15878 checks,
15879 member_p,
15880 &friend_p);
15881 pop_deferring_access_checks ();
15883 /* If this is a member template declaration, let the front
15884 end know. */
15885 if (member_p && !friend_p && decl)
15887 if (TREE_CODE (decl) == TYPE_DECL)
15888 cp_parser_check_access_in_redeclaration (decl);
15890 decl = finish_member_template_decl (decl);
15892 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15893 make_friend_class (current_class_type, TREE_TYPE (decl),
15894 /*complain=*/true);
15896 /* We are done with the current parameter list. */
15897 --parser->num_template_parameter_lists;
15899 pop_deferring_access_checks ();
15901 /* Finish up. */
15902 finish_template_decl (parameter_list);
15904 /* Register member declarations. */
15905 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15906 finish_member_declaration (decl);
15907 /* For the erroneous case of a template with C linkage, we pushed an
15908 implicit C++ linkage scope; exit that scope now. */
15909 if (need_lang_pop)
15910 pop_lang_context ();
15911 /* If DECL is a function template, we must return to parse it later.
15912 (Even though there is no definition, there might be default
15913 arguments that need handling.) */
15914 if (member_p && decl
15915 && (TREE_CODE (decl) == FUNCTION_DECL
15916 || DECL_FUNCTION_TEMPLATE_P (decl)))
15917 TREE_VALUE (parser->unparsed_functions_queues)
15918 = tree_cons (NULL_TREE, decl,
15919 TREE_VALUE (parser->unparsed_functions_queues));
15922 /* Perform the deferred access checks from a template-parameter-list.
15923 CHECKS is a TREE_LIST of access checks, as returned by
15924 get_deferred_access_checks. */
15926 static void
15927 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
15929 ++processing_template_parmlist;
15930 perform_access_checks (checks);
15931 --processing_template_parmlist;
15934 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15935 `function-definition' sequence. MEMBER_P is true, this declaration
15936 appears in a class scope.
15938 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15939 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15941 static tree
15942 cp_parser_single_declaration (cp_parser* parser,
15943 VEC (deferred_access_check,gc)* checks,
15944 bool member_p,
15945 bool* friend_p)
15947 int declares_class_or_enum;
15948 tree decl = NULL_TREE;
15949 cp_decl_specifier_seq decl_specifiers;
15950 bool function_definition_p = false;
15952 /* This function is only used when processing a template
15953 declaration. */
15954 gcc_assert (innermost_scope_kind () == sk_template_parms
15955 || innermost_scope_kind () == sk_template_spec);
15957 /* Defer access checks until we know what is being declared. */
15958 push_deferring_access_checks (dk_deferred);
15960 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15961 alternative. */
15962 cp_parser_decl_specifier_seq (parser,
15963 CP_PARSER_FLAGS_OPTIONAL,
15964 &decl_specifiers,
15965 &declares_class_or_enum);
15966 if (friend_p)
15967 *friend_p = cp_parser_friend_p (&decl_specifiers);
15969 /* There are no template typedefs. */
15970 if (decl_specifiers.specs[(int) ds_typedef])
15972 error ("template declaration of %qs", "typedef");
15973 decl = error_mark_node;
15976 /* Gather up the access checks that occurred the
15977 decl-specifier-seq. */
15978 stop_deferring_access_checks ();
15980 /* Check for the declaration of a template class. */
15981 if (declares_class_or_enum)
15983 if (cp_parser_declares_only_class_p (parser))
15985 decl = shadow_tag (&decl_specifiers);
15987 /* In this case:
15989 struct C {
15990 friend template <typename T> struct A<T>::B;
15993 A<T>::B will be represented by a TYPENAME_TYPE, and
15994 therefore not recognized by shadow_tag. */
15995 if (friend_p && *friend_p
15996 && !decl
15997 && decl_specifiers.type
15998 && TYPE_P (decl_specifiers.type))
15999 decl = decl_specifiers.type;
16001 if (decl && decl != error_mark_node)
16002 decl = TYPE_NAME (decl);
16003 else
16004 decl = error_mark_node;
16006 /* Perform access checks for template parameters. */
16007 cp_parser_perform_template_parameter_access_checks (checks);
16010 /* If it's not a template class, try for a template function. If
16011 the next token is a `;', then this declaration does not declare
16012 anything. But, if there were errors in the decl-specifiers, then
16013 the error might well have come from an attempted class-specifier.
16014 In that case, there's no need to warn about a missing declarator. */
16015 if (!decl
16016 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16017 || decl_specifiers.type != error_mark_node))
16018 decl = cp_parser_init_declarator (parser,
16019 &decl_specifiers,
16020 checks,
16021 /*function_definition_allowed_p=*/true,
16022 member_p,
16023 declares_class_or_enum,
16024 &function_definition_p);
16026 pop_deferring_access_checks ();
16028 /* Clear any current qualification; whatever comes next is the start
16029 of something new. */
16030 parser->scope = NULL_TREE;
16031 parser->qualifying_scope = NULL_TREE;
16032 parser->object_scope = NULL_TREE;
16033 /* Look for a trailing `;' after the declaration. */
16034 if (!function_definition_p
16035 && (decl == error_mark_node
16036 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16037 cp_parser_skip_to_end_of_block_or_statement (parser);
16039 return decl;
16042 /* Parse a cast-expression that is not the operand of a unary "&". */
16044 static tree
16045 cp_parser_simple_cast_expression (cp_parser *parser)
16047 return cp_parser_cast_expression (parser, /*address_p=*/false,
16048 /*cast_p=*/false);
16051 /* Parse a functional cast to TYPE. Returns an expression
16052 representing the cast. */
16054 static tree
16055 cp_parser_functional_cast (cp_parser* parser, tree type)
16057 tree expression_list;
16058 tree cast;
16060 expression_list
16061 = cp_parser_parenthesized_expression_list (parser, false,
16062 /*cast_p=*/true,
16063 /*non_constant_p=*/NULL);
16065 cast = build_functional_cast (type, expression_list);
16066 /* [expr.const]/1: In an integral constant expression "only type
16067 conversions to integral or enumeration type can be used". */
16068 if (TREE_CODE (type) == TYPE_DECL)
16069 type = TREE_TYPE (type);
16070 if (cast != error_mark_node
16071 && !cast_valid_in_integral_constant_expression_p (type)
16072 && (cp_parser_non_integral_constant_expression
16073 (parser, "a call to a constructor")))
16074 return error_mark_node;
16075 return cast;
16078 /* Save the tokens that make up the body of a member function defined
16079 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16080 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16081 specifiers applied to the declaration. Returns the FUNCTION_DECL
16082 for the member function. */
16084 static tree
16085 cp_parser_save_member_function_body (cp_parser* parser,
16086 cp_decl_specifier_seq *decl_specifiers,
16087 cp_declarator *declarator,
16088 tree attributes)
16090 cp_token *first;
16091 cp_token *last;
16092 tree fn;
16094 /* Create the function-declaration. */
16095 fn = start_method (decl_specifiers, declarator, attributes);
16096 /* If something went badly wrong, bail out now. */
16097 if (fn == error_mark_node)
16099 /* If there's a function-body, skip it. */
16100 if (cp_parser_token_starts_function_definition_p
16101 (cp_lexer_peek_token (parser->lexer)))
16102 cp_parser_skip_to_end_of_block_or_statement (parser);
16103 return error_mark_node;
16106 /* Remember it, if there default args to post process. */
16107 cp_parser_save_default_args (parser, fn);
16109 /* Save away the tokens that make up the body of the
16110 function. */
16111 first = parser->lexer->next_token;
16112 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16113 /* Handle function try blocks. */
16114 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16115 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16116 last = parser->lexer->next_token;
16118 /* Save away the inline definition; we will process it when the
16119 class is complete. */
16120 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16121 DECL_PENDING_INLINE_P (fn) = 1;
16123 /* We need to know that this was defined in the class, so that
16124 friend templates are handled correctly. */
16125 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16127 /* We're done with the inline definition. */
16128 finish_method (fn);
16130 /* Add FN to the queue of functions to be parsed later. */
16131 TREE_VALUE (parser->unparsed_functions_queues)
16132 = tree_cons (NULL_TREE, fn,
16133 TREE_VALUE (parser->unparsed_functions_queues));
16135 return fn;
16138 /* Parse a template-argument-list, as well as the trailing ">" (but
16139 not the opening ">"). See cp_parser_template_argument_list for the
16140 return value. */
16142 static tree
16143 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16145 tree arguments;
16146 tree saved_scope;
16147 tree saved_qualifying_scope;
16148 tree saved_object_scope;
16149 bool saved_greater_than_is_operator_p;
16150 bool saved_skip_evaluation;
16152 /* [temp.names]
16154 When parsing a template-id, the first non-nested `>' is taken as
16155 the end of the template-argument-list rather than a greater-than
16156 operator. */
16157 saved_greater_than_is_operator_p
16158 = parser->greater_than_is_operator_p;
16159 parser->greater_than_is_operator_p = false;
16160 /* Parsing the argument list may modify SCOPE, so we save it
16161 here. */
16162 saved_scope = parser->scope;
16163 saved_qualifying_scope = parser->qualifying_scope;
16164 saved_object_scope = parser->object_scope;
16165 /* We need to evaluate the template arguments, even though this
16166 template-id may be nested within a "sizeof". */
16167 saved_skip_evaluation = skip_evaluation;
16168 skip_evaluation = false;
16169 /* Parse the template-argument-list itself. */
16170 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16171 arguments = NULL_TREE;
16172 else
16173 arguments = cp_parser_template_argument_list (parser);
16174 /* Look for the `>' that ends the template-argument-list. If we find
16175 a '>>' instead, it's probably just a typo. */
16176 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16178 if (!saved_greater_than_is_operator_p)
16180 /* If we're in a nested template argument list, the '>>' has
16181 to be a typo for '> >'. We emit the error message, but we
16182 continue parsing and we push a '>' as next token, so that
16183 the argument list will be parsed correctly. Note that the
16184 global source location is still on the token before the
16185 '>>', so we need to say explicitly where we want it. */
16186 cp_token *token = cp_lexer_peek_token (parser->lexer);
16187 error ("%H%<>>%> should be %<> >%> "
16188 "within a nested template argument list",
16189 &token->location);
16191 /* ??? Proper recovery should terminate two levels of
16192 template argument list here. */
16193 token->type = CPP_GREATER;
16195 else
16197 /* If this is not a nested template argument list, the '>>'
16198 is a typo for '>'. Emit an error message and continue.
16199 Same deal about the token location, but here we can get it
16200 right by consuming the '>>' before issuing the diagnostic. */
16201 cp_lexer_consume_token (parser->lexer);
16202 error ("spurious %<>>%>, use %<>%> to terminate "
16203 "a template argument list");
16206 else
16207 cp_parser_skip_to_end_of_template_parameter_list (parser);
16208 /* The `>' token might be a greater-than operator again now. */
16209 parser->greater_than_is_operator_p
16210 = saved_greater_than_is_operator_p;
16211 /* Restore the SAVED_SCOPE. */
16212 parser->scope = saved_scope;
16213 parser->qualifying_scope = saved_qualifying_scope;
16214 parser->object_scope = saved_object_scope;
16215 skip_evaluation = saved_skip_evaluation;
16217 return arguments;
16220 /* MEMBER_FUNCTION is a member function, or a friend. If default
16221 arguments, or the body of the function have not yet been parsed,
16222 parse them now. */
16224 static void
16225 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16227 /* If this member is a template, get the underlying
16228 FUNCTION_DECL. */
16229 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16230 member_function = DECL_TEMPLATE_RESULT (member_function);
16232 /* There should not be any class definitions in progress at this
16233 point; the bodies of members are only parsed outside of all class
16234 definitions. */
16235 gcc_assert (parser->num_classes_being_defined == 0);
16236 /* While we're parsing the member functions we might encounter more
16237 classes. We want to handle them right away, but we don't want
16238 them getting mixed up with functions that are currently in the
16239 queue. */
16240 parser->unparsed_functions_queues
16241 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16243 /* Make sure that any template parameters are in scope. */
16244 maybe_begin_member_template_processing (member_function);
16246 /* If the body of the function has not yet been parsed, parse it
16247 now. */
16248 if (DECL_PENDING_INLINE_P (member_function))
16250 tree function_scope;
16251 cp_token_cache *tokens;
16253 /* The function is no longer pending; we are processing it. */
16254 tokens = DECL_PENDING_INLINE_INFO (member_function);
16255 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16256 DECL_PENDING_INLINE_P (member_function) = 0;
16258 /* If this is a local class, enter the scope of the containing
16259 function. */
16260 function_scope = current_function_decl;
16261 if (function_scope)
16262 push_function_context_to (function_scope);
16265 /* Push the body of the function onto the lexer stack. */
16266 cp_parser_push_lexer_for_tokens (parser, tokens);
16268 /* Let the front end know that we going to be defining this
16269 function. */
16270 start_preparsed_function (member_function, NULL_TREE,
16271 SF_PRE_PARSED | SF_INCLASS_INLINE);
16273 /* Don't do access checking if it is a templated function. */
16274 if (processing_template_decl)
16275 push_deferring_access_checks (dk_no_check);
16277 /* Now, parse the body of the function. */
16278 cp_parser_function_definition_after_declarator (parser,
16279 /*inline_p=*/true);
16281 if (processing_template_decl)
16282 pop_deferring_access_checks ();
16284 /* Leave the scope of the containing function. */
16285 if (function_scope)
16286 pop_function_context_from (function_scope);
16287 cp_parser_pop_lexer (parser);
16290 /* Remove any template parameters from the symbol table. */
16291 maybe_end_member_template_processing ();
16293 /* Restore the queue. */
16294 parser->unparsed_functions_queues
16295 = TREE_CHAIN (parser->unparsed_functions_queues);
16298 /* If DECL contains any default args, remember it on the unparsed
16299 functions queue. */
16301 static void
16302 cp_parser_save_default_args (cp_parser* parser, tree decl)
16304 tree probe;
16306 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16307 probe;
16308 probe = TREE_CHAIN (probe))
16309 if (TREE_PURPOSE (probe))
16311 TREE_PURPOSE (parser->unparsed_functions_queues)
16312 = tree_cons (current_class_type, decl,
16313 TREE_PURPOSE (parser->unparsed_functions_queues));
16314 break;
16318 /* FN is a FUNCTION_DECL which may contains a parameter with an
16319 unparsed DEFAULT_ARG. Parse the default args now. This function
16320 assumes that the current scope is the scope in which the default
16321 argument should be processed. */
16323 static void
16324 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16326 bool saved_local_variables_forbidden_p;
16327 tree parm;
16329 /* While we're parsing the default args, we might (due to the
16330 statement expression extension) encounter more classes. We want
16331 to handle them right away, but we don't want them getting mixed
16332 up with default args that are currently in the queue. */
16333 parser->unparsed_functions_queues
16334 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16336 /* Local variable names (and the `this' keyword) may not appear
16337 in a default argument. */
16338 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16339 parser->local_variables_forbidden_p = true;
16341 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16342 parm;
16343 parm = TREE_CHAIN (parm))
16345 cp_token_cache *tokens;
16346 tree default_arg = TREE_PURPOSE (parm);
16347 tree parsed_arg;
16348 VEC(tree,gc) *insts;
16349 tree copy;
16350 unsigned ix;
16352 if (!default_arg)
16353 continue;
16355 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16356 /* This can happen for a friend declaration for a function
16357 already declared with default arguments. */
16358 continue;
16360 /* Push the saved tokens for the default argument onto the parser's
16361 lexer stack. */
16362 tokens = DEFARG_TOKENS (default_arg);
16363 cp_parser_push_lexer_for_tokens (parser, tokens);
16365 /* Parse the assignment-expression. */
16366 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16368 if (!processing_template_decl)
16369 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16371 TREE_PURPOSE (parm) = parsed_arg;
16373 /* Update any instantiations we've already created. */
16374 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16375 VEC_iterate (tree, insts, ix, copy); ix++)
16376 TREE_PURPOSE (copy) = parsed_arg;
16378 /* If the token stream has not been completely used up, then
16379 there was extra junk after the end of the default
16380 argument. */
16381 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16382 cp_parser_error (parser, "expected %<,%>");
16384 /* Revert to the main lexer. */
16385 cp_parser_pop_lexer (parser);
16388 /* Make sure no default arg is missing. */
16389 check_default_args (fn);
16391 /* Restore the state of local_variables_forbidden_p. */
16392 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16394 /* Restore the queue. */
16395 parser->unparsed_functions_queues
16396 = TREE_CHAIN (parser->unparsed_functions_queues);
16399 /* Parse the operand of `sizeof' (or a similar operator). Returns
16400 either a TYPE or an expression, depending on the form of the
16401 input. The KEYWORD indicates which kind of expression we have
16402 encountered. */
16404 static tree
16405 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16407 static const char *format;
16408 tree expr = NULL_TREE;
16409 const char *saved_message;
16410 bool saved_integral_constant_expression_p;
16411 bool saved_non_integral_constant_expression_p;
16413 /* Initialize FORMAT the first time we get here. */
16414 if (!format)
16415 format = "types may not be defined in '%s' expressions";
16417 /* Types cannot be defined in a `sizeof' expression. Save away the
16418 old message. */
16419 saved_message = parser->type_definition_forbidden_message;
16420 /* And create the new one. */
16421 parser->type_definition_forbidden_message
16422 = XNEWVEC (const char, strlen (format)
16423 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16424 + 1 /* `\0' */);
16425 sprintf ((char *) parser->type_definition_forbidden_message,
16426 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16428 /* The restrictions on constant-expressions do not apply inside
16429 sizeof expressions. */
16430 saved_integral_constant_expression_p
16431 = parser->integral_constant_expression_p;
16432 saved_non_integral_constant_expression_p
16433 = parser->non_integral_constant_expression_p;
16434 parser->integral_constant_expression_p = false;
16436 /* Do not actually evaluate the expression. */
16437 ++skip_evaluation;
16438 /* If it's a `(', then we might be looking at the type-id
16439 construction. */
16440 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16442 tree type;
16443 bool saved_in_type_id_in_expr_p;
16445 /* We can't be sure yet whether we're looking at a type-id or an
16446 expression. */
16447 cp_parser_parse_tentatively (parser);
16448 /* Consume the `('. */
16449 cp_lexer_consume_token (parser->lexer);
16450 /* Parse the type-id. */
16451 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16452 parser->in_type_id_in_expr_p = true;
16453 type = cp_parser_type_id (parser);
16454 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16455 /* Now, look for the trailing `)'. */
16456 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16457 /* If all went well, then we're done. */
16458 if (cp_parser_parse_definitely (parser))
16460 cp_decl_specifier_seq decl_specs;
16462 /* Build a trivial decl-specifier-seq. */
16463 clear_decl_specs (&decl_specs);
16464 decl_specs.type = type;
16466 /* Call grokdeclarator to figure out what type this is. */
16467 expr = grokdeclarator (NULL,
16468 &decl_specs,
16469 TYPENAME,
16470 /*initialized=*/0,
16471 /*attrlist=*/NULL);
16475 /* If the type-id production did not work out, then we must be
16476 looking at the unary-expression production. */
16477 if (!expr)
16478 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16479 /*cast_p=*/false);
16480 /* Go back to evaluating expressions. */
16481 --skip_evaluation;
16483 /* Free the message we created. */
16484 free ((char *) parser->type_definition_forbidden_message);
16485 /* And restore the old one. */
16486 parser->type_definition_forbidden_message = saved_message;
16487 parser->integral_constant_expression_p
16488 = saved_integral_constant_expression_p;
16489 parser->non_integral_constant_expression_p
16490 = saved_non_integral_constant_expression_p;
16492 return expr;
16495 /* If the current declaration has no declarator, return true. */
16497 static bool
16498 cp_parser_declares_only_class_p (cp_parser *parser)
16500 /* If the next token is a `;' or a `,' then there is no
16501 declarator. */
16502 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16503 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16506 /* Update the DECL_SPECS to reflect the storage class indicated by
16507 KEYWORD. */
16509 static void
16510 cp_parser_set_storage_class (cp_parser *parser,
16511 cp_decl_specifier_seq *decl_specs,
16512 enum rid keyword)
16514 cp_storage_class storage_class;
16516 if (parser->in_unbraced_linkage_specification_p)
16518 error ("invalid use of %qD in linkage specification",
16519 ridpointers[keyword]);
16520 return;
16522 else if (decl_specs->storage_class != sc_none)
16524 decl_specs->conflicting_specifiers_p = true;
16525 return;
16528 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16529 && decl_specs->specs[(int) ds_thread])
16531 error ("%<__thread%> before %qD", ridpointers[keyword]);
16532 decl_specs->specs[(int) ds_thread] = 0;
16535 switch (keyword)
16537 case RID_AUTO:
16538 storage_class = sc_auto;
16539 break;
16540 case RID_REGISTER:
16541 storage_class = sc_register;
16542 break;
16543 case RID_STATIC:
16544 storage_class = sc_static;
16545 break;
16546 case RID_EXTERN:
16547 storage_class = sc_extern;
16548 break;
16549 case RID_MUTABLE:
16550 storage_class = sc_mutable;
16551 break;
16552 default:
16553 gcc_unreachable ();
16555 decl_specs->storage_class = storage_class;
16557 /* A storage class specifier cannot be applied alongside a typedef
16558 specifier. If there is a typedef specifier present then set
16559 conflicting_specifiers_p which will trigger an error later
16560 on in grokdeclarator. */
16561 if (decl_specs->specs[(int)ds_typedef])
16562 decl_specs->conflicting_specifiers_p = true;
16565 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16566 is true, the type is a user-defined type; otherwise it is a
16567 built-in type specified by a keyword. */
16569 static void
16570 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16571 tree type_spec,
16572 bool user_defined_p)
16574 decl_specs->any_specifiers_p = true;
16576 /* If the user tries to redeclare bool or wchar_t (with, for
16577 example, in "typedef int wchar_t;") we remember that this is what
16578 happened. In system headers, we ignore these declarations so
16579 that G++ can work with system headers that are not C++-safe. */
16580 if (decl_specs->specs[(int) ds_typedef]
16581 && !user_defined_p
16582 && (type_spec == boolean_type_node
16583 || type_spec == wchar_type_node)
16584 && (decl_specs->type
16585 || decl_specs->specs[(int) ds_long]
16586 || decl_specs->specs[(int) ds_short]
16587 || decl_specs->specs[(int) ds_unsigned]
16588 || decl_specs->specs[(int) ds_signed]))
16590 decl_specs->redefined_builtin_type = type_spec;
16591 if (!decl_specs->type)
16593 decl_specs->type = type_spec;
16594 decl_specs->user_defined_type_p = false;
16597 else if (decl_specs->type)
16598 decl_specs->multiple_types_p = true;
16599 else
16601 decl_specs->type = type_spec;
16602 decl_specs->user_defined_type_p = user_defined_p;
16603 decl_specs->redefined_builtin_type = NULL_TREE;
16607 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16608 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16610 static bool
16611 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16613 return decl_specifiers->specs[(int) ds_friend] != 0;
16616 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16617 issue an error message indicating that TOKEN_DESC was expected.
16619 Returns the token consumed, if the token had the appropriate type.
16620 Otherwise, returns NULL. */
16622 static cp_token *
16623 cp_parser_require (cp_parser* parser,
16624 enum cpp_ttype type,
16625 const char* token_desc)
16627 if (cp_lexer_next_token_is (parser->lexer, type))
16628 return cp_lexer_consume_token (parser->lexer);
16629 else
16631 /* Output the MESSAGE -- unless we're parsing tentatively. */
16632 if (!cp_parser_simulate_error (parser))
16634 char *message = concat ("expected ", token_desc, NULL);
16635 cp_parser_error (parser, message);
16636 free (message);
16638 return NULL;
16642 /* An error message is produced if the next token is not '>'.
16643 All further tokens are skipped until the desired token is
16644 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16646 static void
16647 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16649 /* Current level of '< ... >'. */
16650 unsigned level = 0;
16651 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16652 unsigned nesting_depth = 0;
16654 /* Are we ready, yet? If not, issue error message. */
16655 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16656 return;
16658 /* Skip tokens until the desired token is found. */
16659 while (true)
16661 /* Peek at the next token. */
16662 switch (cp_lexer_peek_token (parser->lexer)->type)
16664 case CPP_LESS:
16665 if (!nesting_depth)
16666 ++level;
16667 break;
16669 case CPP_GREATER:
16670 if (!nesting_depth && level-- == 0)
16672 /* We've reached the token we want, consume it and stop. */
16673 cp_lexer_consume_token (parser->lexer);
16674 return;
16676 break;
16678 case CPP_OPEN_PAREN:
16679 case CPP_OPEN_SQUARE:
16680 ++nesting_depth;
16681 break;
16683 case CPP_CLOSE_PAREN:
16684 case CPP_CLOSE_SQUARE:
16685 if (nesting_depth-- == 0)
16686 return;
16687 break;
16689 case CPP_EOF:
16690 case CPP_PRAGMA_EOL:
16691 case CPP_SEMICOLON:
16692 case CPP_OPEN_BRACE:
16693 case CPP_CLOSE_BRACE:
16694 /* The '>' was probably forgotten, don't look further. */
16695 return;
16697 default:
16698 break;
16701 /* Consume this token. */
16702 cp_lexer_consume_token (parser->lexer);
16706 /* If the next token is the indicated keyword, consume it. Otherwise,
16707 issue an error message indicating that TOKEN_DESC was expected.
16709 Returns the token consumed, if the token had the appropriate type.
16710 Otherwise, returns NULL. */
16712 static cp_token *
16713 cp_parser_require_keyword (cp_parser* parser,
16714 enum rid keyword,
16715 const char* token_desc)
16717 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16719 if (token && token->keyword != keyword)
16721 dyn_string_t error_msg;
16723 /* Format the error message. */
16724 error_msg = dyn_string_new (0);
16725 dyn_string_append_cstr (error_msg, "expected ");
16726 dyn_string_append_cstr (error_msg, token_desc);
16727 cp_parser_error (parser, error_msg->s);
16728 dyn_string_delete (error_msg);
16729 return NULL;
16732 return token;
16735 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16736 function-definition. */
16738 static bool
16739 cp_parser_token_starts_function_definition_p (cp_token* token)
16741 return (/* An ordinary function-body begins with an `{'. */
16742 token->type == CPP_OPEN_BRACE
16743 /* A ctor-initializer begins with a `:'. */
16744 || token->type == CPP_COLON
16745 /* A function-try-block begins with `try'. */
16746 || token->keyword == RID_TRY
16747 /* The named return value extension begins with `return'. */
16748 || token->keyword == RID_RETURN);
16751 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16752 definition. */
16754 static bool
16755 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16757 cp_token *token;
16759 token = cp_lexer_peek_token (parser->lexer);
16760 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16763 /* Returns TRUE iff the next token is the "," or ">" ending a
16764 template-argument. */
16766 static bool
16767 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16769 cp_token *token;
16771 token = cp_lexer_peek_token (parser->lexer);
16772 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16775 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16776 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16778 static bool
16779 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16780 size_t n)
16782 cp_token *token;
16784 token = cp_lexer_peek_nth_token (parser->lexer, n);
16785 if (token->type == CPP_LESS)
16786 return true;
16787 /* Check for the sequence `<::' in the original code. It would be lexed as
16788 `[:', where `[' is a digraph, and there is no whitespace before
16789 `:'. */
16790 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16792 cp_token *token2;
16793 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16794 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16795 return true;
16797 return false;
16800 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16801 or none_type otherwise. */
16803 static enum tag_types
16804 cp_parser_token_is_class_key (cp_token* token)
16806 switch (token->keyword)
16808 case RID_CLASS:
16809 return class_type;
16810 case RID_STRUCT:
16811 return record_type;
16812 case RID_UNION:
16813 return union_type;
16815 default:
16816 return none_type;
16820 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16822 static void
16823 cp_parser_check_class_key (enum tag_types class_key, tree type)
16825 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16826 pedwarn ("%qs tag used in naming %q#T",
16827 class_key == union_type ? "union"
16828 : class_key == record_type ? "struct" : "class",
16829 type);
16832 /* Issue an error message if DECL is redeclared with different
16833 access than its original declaration [class.access.spec/3].
16834 This applies to nested classes and nested class templates.
16835 [class.mem/1]. */
16837 static void
16838 cp_parser_check_access_in_redeclaration (tree decl)
16840 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16841 return;
16843 if ((TREE_PRIVATE (decl)
16844 != (current_access_specifier == access_private_node))
16845 || (TREE_PROTECTED (decl)
16846 != (current_access_specifier == access_protected_node)))
16847 error ("%qD redeclared with different access", decl);
16850 /* Look for the `template' keyword, as a syntactic disambiguator.
16851 Return TRUE iff it is present, in which case it will be
16852 consumed. */
16854 static bool
16855 cp_parser_optional_template_keyword (cp_parser *parser)
16857 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16859 /* The `template' keyword can only be used within templates;
16860 outside templates the parser can always figure out what is a
16861 template and what is not. */
16862 if (!processing_template_decl)
16864 error ("%<template%> (as a disambiguator) is only allowed "
16865 "within templates");
16866 /* If this part of the token stream is rescanned, the same
16867 error message would be generated. So, we purge the token
16868 from the stream. */
16869 cp_lexer_purge_token (parser->lexer);
16870 return false;
16872 else
16874 /* Consume the `template' keyword. */
16875 cp_lexer_consume_token (parser->lexer);
16876 return true;
16880 return false;
16883 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16884 set PARSER->SCOPE, and perform other related actions. */
16886 static void
16887 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16889 int i;
16890 struct tree_check *check_value;
16891 deferred_access_check *chk;
16892 VEC (deferred_access_check,gc) *checks;
16894 /* Get the stored value. */
16895 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
16896 /* Perform any access checks that were deferred. */
16897 checks = check_value->checks;
16898 if (checks)
16900 for (i = 0 ;
16901 VEC_iterate (deferred_access_check, checks, i, chk) ;
16902 ++i)
16904 perform_or_defer_access_check (chk->binfo,
16905 chk->decl,
16906 chk->diag_decl);
16909 /* Set the scope from the stored value. */
16910 parser->scope = check_value->value;
16911 parser->qualifying_scope = check_value->qualifying_scope;
16912 parser->object_scope = NULL_TREE;
16915 /* Consume tokens up through a non-nested END token. */
16917 static void
16918 cp_parser_cache_group (cp_parser *parser,
16919 enum cpp_ttype end,
16920 unsigned depth)
16922 while (true)
16924 cp_token *token;
16926 /* Abort a parenthesized expression if we encounter a brace. */
16927 if ((end == CPP_CLOSE_PAREN || depth == 0)
16928 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16929 return;
16930 /* If we've reached the end of the file, stop. */
16931 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16932 || (end != CPP_PRAGMA_EOL
16933 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16934 return;
16935 /* Consume the next token. */
16936 token = cp_lexer_consume_token (parser->lexer);
16937 /* See if it starts a new group. */
16938 if (token->type == CPP_OPEN_BRACE)
16940 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16941 if (depth == 0)
16942 return;
16944 else if (token->type == CPP_OPEN_PAREN)
16945 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16946 else if (token->type == CPP_PRAGMA)
16947 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16948 else if (token->type == end)
16949 return;
16953 /* Begin parsing tentatively. We always save tokens while parsing
16954 tentatively so that if the tentative parsing fails we can restore the
16955 tokens. */
16957 static void
16958 cp_parser_parse_tentatively (cp_parser* parser)
16960 /* Enter a new parsing context. */
16961 parser->context = cp_parser_context_new (parser->context);
16962 /* Begin saving tokens. */
16963 cp_lexer_save_tokens (parser->lexer);
16964 /* In order to avoid repetitive access control error messages,
16965 access checks are queued up until we are no longer parsing
16966 tentatively. */
16967 push_deferring_access_checks (dk_deferred);
16970 /* Commit to the currently active tentative parse. */
16972 static void
16973 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16975 cp_parser_context *context;
16976 cp_lexer *lexer;
16978 /* Mark all of the levels as committed. */
16979 lexer = parser->lexer;
16980 for (context = parser->context; context->next; context = context->next)
16982 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16983 break;
16984 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16985 while (!cp_lexer_saving_tokens (lexer))
16986 lexer = lexer->next;
16987 cp_lexer_commit_tokens (lexer);
16991 /* Abort the currently active tentative parse. All consumed tokens
16992 will be rolled back, and no diagnostics will be issued. */
16994 static void
16995 cp_parser_abort_tentative_parse (cp_parser* parser)
16997 cp_parser_simulate_error (parser);
16998 /* Now, pretend that we want to see if the construct was
16999 successfully parsed. */
17000 cp_parser_parse_definitely (parser);
17003 /* Stop parsing tentatively. If a parse error has occurred, restore the
17004 token stream. Otherwise, commit to the tokens we have consumed.
17005 Returns true if no error occurred; false otherwise. */
17007 static bool
17008 cp_parser_parse_definitely (cp_parser* parser)
17010 bool error_occurred;
17011 cp_parser_context *context;
17013 /* Remember whether or not an error occurred, since we are about to
17014 destroy that information. */
17015 error_occurred = cp_parser_error_occurred (parser);
17016 /* Remove the topmost context from the stack. */
17017 context = parser->context;
17018 parser->context = context->next;
17019 /* If no parse errors occurred, commit to the tentative parse. */
17020 if (!error_occurred)
17022 /* Commit to the tokens read tentatively, unless that was
17023 already done. */
17024 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17025 cp_lexer_commit_tokens (parser->lexer);
17027 pop_to_parent_deferring_access_checks ();
17029 /* Otherwise, if errors occurred, roll back our state so that things
17030 are just as they were before we began the tentative parse. */
17031 else
17033 cp_lexer_rollback_tokens (parser->lexer);
17034 pop_deferring_access_checks ();
17036 /* Add the context to the front of the free list. */
17037 context->next = cp_parser_context_free_list;
17038 cp_parser_context_free_list = context;
17040 return !error_occurred;
17043 /* Returns true if we are parsing tentatively and are not committed to
17044 this tentative parse. */
17046 static bool
17047 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17049 return (cp_parser_parsing_tentatively (parser)
17050 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17053 /* Returns nonzero iff an error has occurred during the most recent
17054 tentative parse. */
17056 static bool
17057 cp_parser_error_occurred (cp_parser* parser)
17059 return (cp_parser_parsing_tentatively (parser)
17060 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17063 /* Returns nonzero if GNU extensions are allowed. */
17065 static bool
17066 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17068 return parser->allow_gnu_extensions_p;
17071 /* Objective-C++ Productions */
17074 /* Parse an Objective-C expression, which feeds into a primary-expression
17075 above.
17077 objc-expression:
17078 objc-message-expression
17079 objc-string-literal
17080 objc-encode-expression
17081 objc-protocol-expression
17082 objc-selector-expression
17084 Returns a tree representation of the expression. */
17086 static tree
17087 cp_parser_objc_expression (cp_parser* parser)
17089 /* Try to figure out what kind of declaration is present. */
17090 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17092 switch (kwd->type)
17094 case CPP_OPEN_SQUARE:
17095 return cp_parser_objc_message_expression (parser);
17097 case CPP_OBJC_STRING:
17098 kwd = cp_lexer_consume_token (parser->lexer);
17099 return objc_build_string_object (kwd->u.value);
17101 case CPP_KEYWORD:
17102 switch (kwd->keyword)
17104 case RID_AT_ENCODE:
17105 return cp_parser_objc_encode_expression (parser);
17107 case RID_AT_PROTOCOL:
17108 return cp_parser_objc_protocol_expression (parser);
17110 case RID_AT_SELECTOR:
17111 return cp_parser_objc_selector_expression (parser);
17113 default:
17114 break;
17116 default:
17117 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17118 cp_parser_skip_to_end_of_block_or_statement (parser);
17121 return error_mark_node;
17124 /* Parse an Objective-C message expression.
17126 objc-message-expression:
17127 [ objc-message-receiver objc-message-args ]
17129 Returns a representation of an Objective-C message. */
17131 static tree
17132 cp_parser_objc_message_expression (cp_parser* parser)
17134 tree receiver, messageargs;
17136 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17137 receiver = cp_parser_objc_message_receiver (parser);
17138 messageargs = cp_parser_objc_message_args (parser);
17139 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17141 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17144 /* Parse an objc-message-receiver.
17146 objc-message-receiver:
17147 expression
17148 simple-type-specifier
17150 Returns a representation of the type or expression. */
17152 static tree
17153 cp_parser_objc_message_receiver (cp_parser* parser)
17155 tree rcv;
17157 /* An Objective-C message receiver may be either (1) a type
17158 or (2) an expression. */
17159 cp_parser_parse_tentatively (parser);
17160 rcv = cp_parser_expression (parser, false);
17162 if (cp_parser_parse_definitely (parser))
17163 return rcv;
17165 rcv = cp_parser_simple_type_specifier (parser,
17166 /*decl_specs=*/NULL,
17167 CP_PARSER_FLAGS_NONE);
17169 return objc_get_class_reference (rcv);
17172 /* Parse the arguments and selectors comprising an Objective-C message.
17174 objc-message-args:
17175 objc-selector
17176 objc-selector-args
17177 objc-selector-args , objc-comma-args
17179 objc-selector-args:
17180 objc-selector [opt] : assignment-expression
17181 objc-selector-args objc-selector [opt] : assignment-expression
17183 objc-comma-args:
17184 assignment-expression
17185 objc-comma-args , assignment-expression
17187 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17188 selector arguments and TREE_VALUE containing a list of comma
17189 arguments. */
17191 static tree
17192 cp_parser_objc_message_args (cp_parser* parser)
17194 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17195 bool maybe_unary_selector_p = true;
17196 cp_token *token = cp_lexer_peek_token (parser->lexer);
17198 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17200 tree selector = NULL_TREE, arg;
17202 if (token->type != CPP_COLON)
17203 selector = cp_parser_objc_selector (parser);
17205 /* Detect if we have a unary selector. */
17206 if (maybe_unary_selector_p
17207 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17208 return build_tree_list (selector, NULL_TREE);
17210 maybe_unary_selector_p = false;
17211 cp_parser_require (parser, CPP_COLON, "`:'");
17212 arg = cp_parser_assignment_expression (parser, false);
17214 sel_args
17215 = chainon (sel_args,
17216 build_tree_list (selector, arg));
17218 token = cp_lexer_peek_token (parser->lexer);
17221 /* Handle non-selector arguments, if any. */
17222 while (token->type == CPP_COMMA)
17224 tree arg;
17226 cp_lexer_consume_token (parser->lexer);
17227 arg = cp_parser_assignment_expression (parser, false);
17229 addl_args
17230 = chainon (addl_args,
17231 build_tree_list (NULL_TREE, arg));
17233 token = cp_lexer_peek_token (parser->lexer);
17236 return build_tree_list (sel_args, addl_args);
17239 /* Parse an Objective-C encode expression.
17241 objc-encode-expression:
17242 @encode objc-typename
17244 Returns an encoded representation of the type argument. */
17246 static tree
17247 cp_parser_objc_encode_expression (cp_parser* parser)
17249 tree type;
17251 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17252 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17253 type = complete_type (cp_parser_type_id (parser));
17254 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17256 if (!type)
17258 error ("%<@encode%> must specify a type as an argument");
17259 return error_mark_node;
17262 return objc_build_encode_expr (type);
17265 /* Parse an Objective-C @defs expression. */
17267 static tree
17268 cp_parser_objc_defs_expression (cp_parser *parser)
17270 tree name;
17272 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17273 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17274 name = cp_parser_identifier (parser);
17275 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17277 return objc_get_class_ivars (name);
17280 /* Parse an Objective-C protocol expression.
17282 objc-protocol-expression:
17283 @protocol ( identifier )
17285 Returns a representation of the protocol expression. */
17287 static tree
17288 cp_parser_objc_protocol_expression (cp_parser* parser)
17290 tree proto;
17292 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17293 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17294 proto = cp_parser_identifier (parser);
17295 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17297 return objc_build_protocol_expr (proto);
17300 /* Parse an Objective-C selector expression.
17302 objc-selector-expression:
17303 @selector ( objc-method-signature )
17305 objc-method-signature:
17306 objc-selector
17307 objc-selector-seq
17309 objc-selector-seq:
17310 objc-selector :
17311 objc-selector-seq objc-selector :
17313 Returns a representation of the method selector. */
17315 static tree
17316 cp_parser_objc_selector_expression (cp_parser* parser)
17318 tree sel_seq = NULL_TREE;
17319 bool maybe_unary_selector_p = true;
17320 cp_token *token;
17322 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17323 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17324 token = cp_lexer_peek_token (parser->lexer);
17326 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17327 || token->type == CPP_SCOPE)
17329 tree selector = NULL_TREE;
17331 if (token->type != CPP_COLON
17332 || token->type == CPP_SCOPE)
17333 selector = cp_parser_objc_selector (parser);
17335 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17336 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17338 /* Detect if we have a unary selector. */
17339 if (maybe_unary_selector_p)
17341 sel_seq = selector;
17342 goto finish_selector;
17344 else
17346 cp_parser_error (parser, "expected %<:%>");
17349 maybe_unary_selector_p = false;
17350 token = cp_lexer_consume_token (parser->lexer);
17352 if (token->type == CPP_SCOPE)
17354 sel_seq
17355 = chainon (sel_seq,
17356 build_tree_list (selector, NULL_TREE));
17357 sel_seq
17358 = chainon (sel_seq,
17359 build_tree_list (NULL_TREE, NULL_TREE));
17361 else
17362 sel_seq
17363 = chainon (sel_seq,
17364 build_tree_list (selector, NULL_TREE));
17366 token = cp_lexer_peek_token (parser->lexer);
17369 finish_selector:
17370 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17372 return objc_build_selector_expr (sel_seq);
17375 /* Parse a list of identifiers.
17377 objc-identifier-list:
17378 identifier
17379 objc-identifier-list , identifier
17381 Returns a TREE_LIST of identifier nodes. */
17383 static tree
17384 cp_parser_objc_identifier_list (cp_parser* parser)
17386 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17387 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17389 while (sep->type == CPP_COMMA)
17391 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17392 list = chainon (list,
17393 build_tree_list (NULL_TREE,
17394 cp_parser_identifier (parser)));
17395 sep = cp_lexer_peek_token (parser->lexer);
17398 return list;
17401 /* Parse an Objective-C alias declaration.
17403 objc-alias-declaration:
17404 @compatibility_alias identifier identifier ;
17406 This function registers the alias mapping with the Objective-C front-end.
17407 It returns nothing. */
17409 static void
17410 cp_parser_objc_alias_declaration (cp_parser* parser)
17412 tree alias, orig;
17414 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17415 alias = cp_parser_identifier (parser);
17416 orig = cp_parser_identifier (parser);
17417 objc_declare_alias (alias, orig);
17418 cp_parser_consume_semicolon_at_end_of_statement (parser);
17421 /* Parse an Objective-C class forward-declaration.
17423 objc-class-declaration:
17424 @class objc-identifier-list ;
17426 The function registers the forward declarations with the Objective-C
17427 front-end. It returns nothing. */
17429 static void
17430 cp_parser_objc_class_declaration (cp_parser* parser)
17432 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17433 objc_declare_class (cp_parser_objc_identifier_list (parser));
17434 cp_parser_consume_semicolon_at_end_of_statement (parser);
17437 /* Parse a list of Objective-C protocol references.
17439 objc-protocol-refs-opt:
17440 objc-protocol-refs [opt]
17442 objc-protocol-refs:
17443 < objc-identifier-list >
17445 Returns a TREE_LIST of identifiers, if any. */
17447 static tree
17448 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17450 tree protorefs = NULL_TREE;
17452 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17454 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17455 protorefs = cp_parser_objc_identifier_list (parser);
17456 cp_parser_require (parser, CPP_GREATER, "`>'");
17459 return protorefs;
17462 /* Parse a Objective-C visibility specification. */
17464 static void
17465 cp_parser_objc_visibility_spec (cp_parser* parser)
17467 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17469 switch (vis->keyword)
17471 case RID_AT_PRIVATE:
17472 objc_set_visibility (2);
17473 break;
17474 case RID_AT_PROTECTED:
17475 objc_set_visibility (0);
17476 break;
17477 case RID_AT_PUBLIC:
17478 objc_set_visibility (1);
17479 break;
17480 default:
17481 return;
17484 /* Eat '@private'/'@protected'/'@public'. */
17485 cp_lexer_consume_token (parser->lexer);
17488 /* Parse an Objective-C method type. */
17490 static void
17491 cp_parser_objc_method_type (cp_parser* parser)
17493 objc_set_method_type
17494 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17495 ? PLUS_EXPR
17496 : MINUS_EXPR);
17499 /* Parse an Objective-C protocol qualifier. */
17501 static tree
17502 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17504 tree quals = NULL_TREE, node;
17505 cp_token *token = cp_lexer_peek_token (parser->lexer);
17507 node = token->u.value;
17509 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17510 && (node == ridpointers [(int) RID_IN]
17511 || node == ridpointers [(int) RID_OUT]
17512 || node == ridpointers [(int) RID_INOUT]
17513 || node == ridpointers [(int) RID_BYCOPY]
17514 || node == ridpointers [(int) RID_BYREF]
17515 || node == ridpointers [(int) RID_ONEWAY]))
17517 quals = tree_cons (NULL_TREE, node, quals);
17518 cp_lexer_consume_token (parser->lexer);
17519 token = cp_lexer_peek_token (parser->lexer);
17520 node = token->u.value;
17523 return quals;
17526 /* Parse an Objective-C typename. */
17528 static tree
17529 cp_parser_objc_typename (cp_parser* parser)
17531 tree typename = NULL_TREE;
17533 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17535 tree proto_quals, cp_type = NULL_TREE;
17537 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17538 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17540 /* An ObjC type name may consist of just protocol qualifiers, in which
17541 case the type shall default to 'id'. */
17542 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17543 cp_type = cp_parser_type_id (parser);
17545 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17546 typename = build_tree_list (proto_quals, cp_type);
17549 return typename;
17552 /* Check to see if TYPE refers to an Objective-C selector name. */
17554 static bool
17555 cp_parser_objc_selector_p (enum cpp_ttype type)
17557 return (type == CPP_NAME || type == CPP_KEYWORD
17558 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17559 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17560 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17561 || type == CPP_XOR || type == CPP_XOR_EQ);
17564 /* Parse an Objective-C selector. */
17566 static tree
17567 cp_parser_objc_selector (cp_parser* parser)
17569 cp_token *token = cp_lexer_consume_token (parser->lexer);
17571 if (!cp_parser_objc_selector_p (token->type))
17573 error ("invalid Objective-C++ selector name");
17574 return error_mark_node;
17577 /* C++ operator names are allowed to appear in ObjC selectors. */
17578 switch (token->type)
17580 case CPP_AND_AND: return get_identifier ("and");
17581 case CPP_AND_EQ: return get_identifier ("and_eq");
17582 case CPP_AND: return get_identifier ("bitand");
17583 case CPP_OR: return get_identifier ("bitor");
17584 case CPP_COMPL: return get_identifier ("compl");
17585 case CPP_NOT: return get_identifier ("not");
17586 case CPP_NOT_EQ: return get_identifier ("not_eq");
17587 case CPP_OR_OR: return get_identifier ("or");
17588 case CPP_OR_EQ: return get_identifier ("or_eq");
17589 case CPP_XOR: return get_identifier ("xor");
17590 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17591 default: return token->u.value;
17595 /* Parse an Objective-C params list. */
17597 static tree
17598 cp_parser_objc_method_keyword_params (cp_parser* parser)
17600 tree params = NULL_TREE;
17601 bool maybe_unary_selector_p = true;
17602 cp_token *token = cp_lexer_peek_token (parser->lexer);
17604 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17606 tree selector = NULL_TREE, typename, identifier;
17608 if (token->type != CPP_COLON)
17609 selector = cp_parser_objc_selector (parser);
17611 /* Detect if we have a unary selector. */
17612 if (maybe_unary_selector_p
17613 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17614 return selector;
17616 maybe_unary_selector_p = false;
17617 cp_parser_require (parser, CPP_COLON, "`:'");
17618 typename = cp_parser_objc_typename (parser);
17619 identifier = cp_parser_identifier (parser);
17621 params
17622 = chainon (params,
17623 objc_build_keyword_decl (selector,
17624 typename,
17625 identifier));
17627 token = cp_lexer_peek_token (parser->lexer);
17630 return params;
17633 /* Parse the non-keyword Objective-C params. */
17635 static tree
17636 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17638 tree params = make_node (TREE_LIST);
17639 cp_token *token = cp_lexer_peek_token (parser->lexer);
17640 *ellipsisp = false; /* Initially, assume no ellipsis. */
17642 while (token->type == CPP_COMMA)
17644 cp_parameter_declarator *parmdecl;
17645 tree parm;
17647 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17648 token = cp_lexer_peek_token (parser->lexer);
17650 if (token->type == CPP_ELLIPSIS)
17652 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17653 *ellipsisp = true;
17654 break;
17657 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17658 parm = grokdeclarator (parmdecl->declarator,
17659 &parmdecl->decl_specifiers,
17660 PARM, /*initialized=*/0,
17661 /*attrlist=*/NULL);
17663 chainon (params, build_tree_list (NULL_TREE, parm));
17664 token = cp_lexer_peek_token (parser->lexer);
17667 return params;
17670 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17672 static void
17673 cp_parser_objc_interstitial_code (cp_parser* parser)
17675 cp_token *token = cp_lexer_peek_token (parser->lexer);
17677 /* If the next token is `extern' and the following token is a string
17678 literal, then we have a linkage specification. */
17679 if (token->keyword == RID_EXTERN
17680 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17681 cp_parser_linkage_specification (parser);
17682 /* Handle #pragma, if any. */
17683 else if (token->type == CPP_PRAGMA)
17684 cp_parser_pragma (parser, pragma_external);
17685 /* Allow stray semicolons. */
17686 else if (token->type == CPP_SEMICOLON)
17687 cp_lexer_consume_token (parser->lexer);
17688 /* Finally, try to parse a block-declaration, or a function-definition. */
17689 else
17690 cp_parser_block_declaration (parser, /*statement_p=*/false);
17693 /* Parse a method signature. */
17695 static tree
17696 cp_parser_objc_method_signature (cp_parser* parser)
17698 tree rettype, kwdparms, optparms;
17699 bool ellipsis = false;
17701 cp_parser_objc_method_type (parser);
17702 rettype = cp_parser_objc_typename (parser);
17703 kwdparms = cp_parser_objc_method_keyword_params (parser);
17704 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17706 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17709 /* Pars an Objective-C method prototype list. */
17711 static void
17712 cp_parser_objc_method_prototype_list (cp_parser* parser)
17714 cp_token *token = cp_lexer_peek_token (parser->lexer);
17716 while (token->keyword != RID_AT_END)
17718 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17720 objc_add_method_declaration
17721 (cp_parser_objc_method_signature (parser));
17722 cp_parser_consume_semicolon_at_end_of_statement (parser);
17724 else
17725 /* Allow for interspersed non-ObjC++ code. */
17726 cp_parser_objc_interstitial_code (parser);
17728 token = cp_lexer_peek_token (parser->lexer);
17731 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17732 objc_finish_interface ();
17735 /* Parse an Objective-C method definition list. */
17737 static void
17738 cp_parser_objc_method_definition_list (cp_parser* parser)
17740 cp_token *token = cp_lexer_peek_token (parser->lexer);
17742 while (token->keyword != RID_AT_END)
17744 tree meth;
17746 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17748 push_deferring_access_checks (dk_deferred);
17749 objc_start_method_definition
17750 (cp_parser_objc_method_signature (parser));
17752 /* For historical reasons, we accept an optional semicolon. */
17753 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17754 cp_lexer_consume_token (parser->lexer);
17756 perform_deferred_access_checks ();
17757 stop_deferring_access_checks ();
17758 meth = cp_parser_function_definition_after_declarator (parser,
17759 false);
17760 pop_deferring_access_checks ();
17761 objc_finish_method_definition (meth);
17763 else
17764 /* Allow for interspersed non-ObjC++ code. */
17765 cp_parser_objc_interstitial_code (parser);
17767 token = cp_lexer_peek_token (parser->lexer);
17770 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17771 objc_finish_implementation ();
17774 /* Parse Objective-C ivars. */
17776 static void
17777 cp_parser_objc_class_ivars (cp_parser* parser)
17779 cp_token *token = cp_lexer_peek_token (parser->lexer);
17781 if (token->type != CPP_OPEN_BRACE)
17782 return; /* No ivars specified. */
17784 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17785 token = cp_lexer_peek_token (parser->lexer);
17787 while (token->type != CPP_CLOSE_BRACE)
17789 cp_decl_specifier_seq declspecs;
17790 int decl_class_or_enum_p;
17791 tree prefix_attributes;
17793 cp_parser_objc_visibility_spec (parser);
17795 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17796 break;
17798 cp_parser_decl_specifier_seq (parser,
17799 CP_PARSER_FLAGS_OPTIONAL,
17800 &declspecs,
17801 &decl_class_or_enum_p);
17802 prefix_attributes = declspecs.attributes;
17803 declspecs.attributes = NULL_TREE;
17805 /* Keep going until we hit the `;' at the end of the
17806 declaration. */
17807 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17809 tree width = NULL_TREE, attributes, first_attribute, decl;
17810 cp_declarator *declarator = NULL;
17811 int ctor_dtor_or_conv_p;
17813 /* Check for a (possibly unnamed) bitfield declaration. */
17814 token = cp_lexer_peek_token (parser->lexer);
17815 if (token->type == CPP_COLON)
17816 goto eat_colon;
17818 if (token->type == CPP_NAME
17819 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17820 == CPP_COLON))
17822 /* Get the name of the bitfield. */
17823 declarator = make_id_declarator (NULL_TREE,
17824 cp_parser_identifier (parser),
17825 sfk_none);
17827 eat_colon:
17828 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17829 /* Get the width of the bitfield. */
17830 width
17831 = cp_parser_constant_expression (parser,
17832 /*allow_non_constant=*/false,
17833 NULL);
17835 else
17837 /* Parse the declarator. */
17838 declarator
17839 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17840 &ctor_dtor_or_conv_p,
17841 /*parenthesized_p=*/NULL,
17842 /*member_p=*/false);
17845 /* Look for attributes that apply to the ivar. */
17846 attributes = cp_parser_attributes_opt (parser);
17847 /* Remember which attributes are prefix attributes and
17848 which are not. */
17849 first_attribute = attributes;
17850 /* Combine the attributes. */
17851 attributes = chainon (prefix_attributes, attributes);
17853 if (width)
17855 /* Create the bitfield declaration. */
17856 decl = grokbitfield (declarator, &declspecs, width);
17857 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17859 else
17860 decl = grokfield (declarator, &declspecs,
17861 NULL_TREE, /*init_const_expr_p=*/false,
17862 NULL_TREE, attributes);
17864 /* Add the instance variable. */
17865 objc_add_instance_variable (decl);
17867 /* Reset PREFIX_ATTRIBUTES. */
17868 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17869 attributes = TREE_CHAIN (attributes);
17870 if (attributes)
17871 TREE_CHAIN (attributes) = NULL_TREE;
17873 token = cp_lexer_peek_token (parser->lexer);
17875 if (token->type == CPP_COMMA)
17877 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17878 continue;
17880 break;
17883 cp_parser_consume_semicolon_at_end_of_statement (parser);
17884 token = cp_lexer_peek_token (parser->lexer);
17887 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17888 /* For historical reasons, we accept an optional semicolon. */
17889 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17890 cp_lexer_consume_token (parser->lexer);
17893 /* Parse an Objective-C protocol declaration. */
17895 static void
17896 cp_parser_objc_protocol_declaration (cp_parser* parser)
17898 tree proto, protorefs;
17899 cp_token *tok;
17901 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17902 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17904 error ("identifier expected after %<@protocol%>");
17905 goto finish;
17908 /* See if we have a forward declaration or a definition. */
17909 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17911 /* Try a forward declaration first. */
17912 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17914 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17915 finish:
17916 cp_parser_consume_semicolon_at_end_of_statement (parser);
17919 /* Ok, we got a full-fledged definition (or at least should). */
17920 else
17922 proto = cp_parser_identifier (parser);
17923 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17924 objc_start_protocol (proto, protorefs);
17925 cp_parser_objc_method_prototype_list (parser);
17929 /* Parse an Objective-C superclass or category. */
17931 static void
17932 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17933 tree *categ)
17935 cp_token *next = cp_lexer_peek_token (parser->lexer);
17937 *super = *categ = NULL_TREE;
17938 if (next->type == CPP_COLON)
17940 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17941 *super = cp_parser_identifier (parser);
17943 else if (next->type == CPP_OPEN_PAREN)
17945 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17946 *categ = cp_parser_identifier (parser);
17947 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17951 /* Parse an Objective-C class interface. */
17953 static void
17954 cp_parser_objc_class_interface (cp_parser* parser)
17956 tree name, super, categ, protos;
17958 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17959 name = cp_parser_identifier (parser);
17960 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17961 protos = cp_parser_objc_protocol_refs_opt (parser);
17963 /* We have either a class or a category on our hands. */
17964 if (categ)
17965 objc_start_category_interface (name, categ, protos);
17966 else
17968 objc_start_class_interface (name, super, protos);
17969 /* Handle instance variable declarations, if any. */
17970 cp_parser_objc_class_ivars (parser);
17971 objc_continue_interface ();
17974 cp_parser_objc_method_prototype_list (parser);
17977 /* Parse an Objective-C class implementation. */
17979 static void
17980 cp_parser_objc_class_implementation (cp_parser* parser)
17982 tree name, super, categ;
17984 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17985 name = cp_parser_identifier (parser);
17986 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17988 /* We have either a class or a category on our hands. */
17989 if (categ)
17990 objc_start_category_implementation (name, categ);
17991 else
17993 objc_start_class_implementation (name, super);
17994 /* Handle instance variable declarations, if any. */
17995 cp_parser_objc_class_ivars (parser);
17996 objc_continue_implementation ();
17999 cp_parser_objc_method_definition_list (parser);
18002 /* Consume the @end token and finish off the implementation. */
18004 static void
18005 cp_parser_objc_end_implementation (cp_parser* parser)
18007 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18008 objc_finish_implementation ();
18011 /* Parse an Objective-C declaration. */
18013 static void
18014 cp_parser_objc_declaration (cp_parser* parser)
18016 /* Try to figure out what kind of declaration is present. */
18017 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18019 switch (kwd->keyword)
18021 case RID_AT_ALIAS:
18022 cp_parser_objc_alias_declaration (parser);
18023 break;
18024 case RID_AT_CLASS:
18025 cp_parser_objc_class_declaration (parser);
18026 break;
18027 case RID_AT_PROTOCOL:
18028 cp_parser_objc_protocol_declaration (parser);
18029 break;
18030 case RID_AT_INTERFACE:
18031 cp_parser_objc_class_interface (parser);
18032 break;
18033 case RID_AT_IMPLEMENTATION:
18034 cp_parser_objc_class_implementation (parser);
18035 break;
18036 case RID_AT_END:
18037 cp_parser_objc_end_implementation (parser);
18038 break;
18039 default:
18040 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18041 cp_parser_skip_to_end_of_block_or_statement (parser);
18045 /* Parse an Objective-C try-catch-finally statement.
18047 objc-try-catch-finally-stmt:
18048 @try compound-statement objc-catch-clause-seq [opt]
18049 objc-finally-clause [opt]
18051 objc-catch-clause-seq:
18052 objc-catch-clause objc-catch-clause-seq [opt]
18054 objc-catch-clause:
18055 @catch ( exception-declaration ) compound-statement
18057 objc-finally-clause
18058 @finally compound-statement
18060 Returns NULL_TREE. */
18062 static tree
18063 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18064 location_t location;
18065 tree stmt;
18067 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18068 location = cp_lexer_peek_token (parser->lexer)->location;
18069 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18070 node, lest it get absorbed into the surrounding block. */
18071 stmt = push_stmt_list ();
18072 cp_parser_compound_statement (parser, NULL, false);
18073 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18075 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18077 cp_parameter_declarator *parmdecl;
18078 tree parm;
18080 cp_lexer_consume_token (parser->lexer);
18081 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18082 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18083 parm = grokdeclarator (parmdecl->declarator,
18084 &parmdecl->decl_specifiers,
18085 PARM, /*initialized=*/0,
18086 /*attrlist=*/NULL);
18087 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18088 objc_begin_catch_clause (parm);
18089 cp_parser_compound_statement (parser, NULL, false);
18090 objc_finish_catch_clause ();
18093 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18095 cp_lexer_consume_token (parser->lexer);
18096 location = cp_lexer_peek_token (parser->lexer)->location;
18097 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18098 node, lest it get absorbed into the surrounding block. */
18099 stmt = push_stmt_list ();
18100 cp_parser_compound_statement (parser, NULL, false);
18101 objc_build_finally_clause (location, pop_stmt_list (stmt));
18104 return objc_finish_try_stmt ();
18107 /* Parse an Objective-C synchronized statement.
18109 objc-synchronized-stmt:
18110 @synchronized ( expression ) compound-statement
18112 Returns NULL_TREE. */
18114 static tree
18115 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18116 location_t location;
18117 tree lock, stmt;
18119 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18121 location = cp_lexer_peek_token (parser->lexer)->location;
18122 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18123 lock = cp_parser_expression (parser, false);
18124 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18126 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18127 node, lest it get absorbed into the surrounding block. */
18128 stmt = push_stmt_list ();
18129 cp_parser_compound_statement (parser, NULL, false);
18131 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18134 /* Parse an Objective-C throw statement.
18136 objc-throw-stmt:
18137 @throw assignment-expression [opt] ;
18139 Returns a constructed '@throw' statement. */
18141 static tree
18142 cp_parser_objc_throw_statement (cp_parser *parser) {
18143 tree expr = NULL_TREE;
18145 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18147 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18148 expr = cp_parser_assignment_expression (parser, false);
18150 cp_parser_consume_semicolon_at_end_of_statement (parser);
18152 return objc_build_throw_stmt (expr);
18155 /* Parse an Objective-C statement. */
18157 static tree
18158 cp_parser_objc_statement (cp_parser * parser) {
18159 /* Try to figure out what kind of declaration is present. */
18160 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18162 switch (kwd->keyword)
18164 case RID_AT_TRY:
18165 return cp_parser_objc_try_catch_finally_statement (parser);
18166 case RID_AT_SYNCHRONIZED:
18167 return cp_parser_objc_synchronized_statement (parser);
18168 case RID_AT_THROW:
18169 return cp_parser_objc_throw_statement (parser);
18170 default:
18171 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18172 cp_parser_skip_to_end_of_block_or_statement (parser);
18175 return error_mark_node;
18178 /* OpenMP 2.5 parsing routines. */
18180 /* Returns name of the next clause.
18181 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18182 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18183 returned and the token is consumed. */
18185 static pragma_omp_clause
18186 cp_parser_omp_clause_name (cp_parser *parser)
18188 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18190 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18191 result = PRAGMA_OMP_CLAUSE_IF;
18192 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18193 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18194 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18195 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18196 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18198 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18199 const char *p = IDENTIFIER_POINTER (id);
18201 switch (p[0])
18203 case 'c':
18204 if (!strcmp ("copyin", p))
18205 result = PRAGMA_OMP_CLAUSE_COPYIN;
18206 else if (!strcmp ("copyprivate", p))
18207 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18208 break;
18209 case 'f':
18210 if (!strcmp ("firstprivate", p))
18211 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18212 break;
18213 case 'l':
18214 if (!strcmp ("lastprivate", p))
18215 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18216 break;
18217 case 'n':
18218 if (!strcmp ("nowait", p))
18219 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18220 else if (!strcmp ("num_threads", p))
18221 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18222 break;
18223 case 'o':
18224 if (!strcmp ("ordered", p))
18225 result = PRAGMA_OMP_CLAUSE_ORDERED;
18226 break;
18227 case 'r':
18228 if (!strcmp ("reduction", p))
18229 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18230 break;
18231 case 's':
18232 if (!strcmp ("schedule", p))
18233 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18234 else if (!strcmp ("shared", p))
18235 result = PRAGMA_OMP_CLAUSE_SHARED;
18236 break;
18240 if (result != PRAGMA_OMP_CLAUSE_NONE)
18241 cp_lexer_consume_token (parser->lexer);
18243 return result;
18246 /* Validate that a clause of the given type does not already exist. */
18248 static void
18249 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18251 tree c;
18253 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18254 if (OMP_CLAUSE_CODE (c) == code)
18256 error ("too many %qs clauses", name);
18257 break;
18261 /* OpenMP 2.5:
18262 variable-list:
18263 identifier
18264 variable-list , identifier
18266 In addition, we match a closing parenthesis. An opening parenthesis
18267 will have been consumed by the caller.
18269 If KIND is nonzero, create the appropriate node and install the decl
18270 in OMP_CLAUSE_DECL and add the node to the head of the list.
18272 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18273 return the list created. */
18275 static tree
18276 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18277 tree list)
18279 while (1)
18281 tree name, decl;
18283 name = cp_parser_id_expression (parser, /*template_p=*/false,
18284 /*check_dependency_p=*/true,
18285 /*template_p=*/NULL,
18286 /*declarator_p=*/false,
18287 /*optional_p=*/false);
18288 if (name == error_mark_node)
18289 goto skip_comma;
18291 decl = cp_parser_lookup_name_simple (parser, name);
18292 if (decl == error_mark_node)
18293 cp_parser_name_lookup_error (parser, name, decl, NULL);
18294 else if (kind != 0)
18296 tree u = build_omp_clause (kind);
18297 OMP_CLAUSE_DECL (u) = decl;
18298 OMP_CLAUSE_CHAIN (u) = list;
18299 list = u;
18301 else
18302 list = tree_cons (decl, NULL_TREE, list);
18304 get_comma:
18305 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18306 break;
18307 cp_lexer_consume_token (parser->lexer);
18310 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18312 int ending;
18314 /* Try to resync to an unnested comma. Copied from
18315 cp_parser_parenthesized_expression_list. */
18316 skip_comma:
18317 ending = cp_parser_skip_to_closing_parenthesis (parser,
18318 /*recovering=*/true,
18319 /*or_comma=*/true,
18320 /*consume_paren=*/true);
18321 if (ending < 0)
18322 goto get_comma;
18325 return list;
18328 /* Similarly, but expect leading and trailing parenthesis. This is a very
18329 common case for omp clauses. */
18331 static tree
18332 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18334 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18335 return cp_parser_omp_var_list_no_open (parser, kind, list);
18336 return list;
18339 /* OpenMP 2.5:
18340 default ( shared | none ) */
18342 static tree
18343 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18345 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18346 tree c;
18348 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18349 return list;
18350 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18352 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18353 const char *p = IDENTIFIER_POINTER (id);
18355 switch (p[0])
18357 case 'n':
18358 if (strcmp ("none", p) != 0)
18359 goto invalid_kind;
18360 kind = OMP_CLAUSE_DEFAULT_NONE;
18361 break;
18363 case 's':
18364 if (strcmp ("shared", p) != 0)
18365 goto invalid_kind;
18366 kind = OMP_CLAUSE_DEFAULT_SHARED;
18367 break;
18369 default:
18370 goto invalid_kind;
18373 cp_lexer_consume_token (parser->lexer);
18375 else
18377 invalid_kind:
18378 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18381 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18382 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18383 /*or_comma=*/false,
18384 /*consume_paren=*/true);
18386 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18387 return list;
18389 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18390 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18391 OMP_CLAUSE_CHAIN (c) = list;
18392 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18394 return c;
18397 /* OpenMP 2.5:
18398 if ( expression ) */
18400 static tree
18401 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18403 tree t, c;
18405 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18406 return list;
18408 t = cp_parser_condition (parser);
18410 if (t == error_mark_node
18411 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18412 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18413 /*or_comma=*/false,
18414 /*consume_paren=*/true);
18416 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18418 c = build_omp_clause (OMP_CLAUSE_IF);
18419 OMP_CLAUSE_IF_EXPR (c) = t;
18420 OMP_CLAUSE_CHAIN (c) = list;
18422 return c;
18425 /* OpenMP 2.5:
18426 nowait */
18428 static tree
18429 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18431 tree c;
18433 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18435 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18436 OMP_CLAUSE_CHAIN (c) = list;
18437 return c;
18440 /* OpenMP 2.5:
18441 num_threads ( expression ) */
18443 static tree
18444 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18446 tree t, c;
18448 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18449 return list;
18451 t = cp_parser_expression (parser, false);
18453 if (t == error_mark_node
18454 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18455 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18456 /*or_comma=*/false,
18457 /*consume_paren=*/true);
18459 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18461 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18462 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18463 OMP_CLAUSE_CHAIN (c) = list;
18465 return c;
18468 /* OpenMP 2.5:
18469 ordered */
18471 static tree
18472 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18474 tree c;
18476 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18478 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18479 OMP_CLAUSE_CHAIN (c) = list;
18480 return c;
18483 /* OpenMP 2.5:
18484 reduction ( reduction-operator : variable-list )
18486 reduction-operator:
18487 One of: + * - & ^ | && || */
18489 static tree
18490 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18492 enum tree_code code;
18493 tree nlist, c;
18495 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18496 return list;
18498 switch (cp_lexer_peek_token (parser->lexer)->type)
18500 case CPP_PLUS:
18501 code = PLUS_EXPR;
18502 break;
18503 case CPP_MULT:
18504 code = MULT_EXPR;
18505 break;
18506 case CPP_MINUS:
18507 code = MINUS_EXPR;
18508 break;
18509 case CPP_AND:
18510 code = BIT_AND_EXPR;
18511 break;
18512 case CPP_XOR:
18513 code = BIT_XOR_EXPR;
18514 break;
18515 case CPP_OR:
18516 code = BIT_IOR_EXPR;
18517 break;
18518 case CPP_AND_AND:
18519 code = TRUTH_ANDIF_EXPR;
18520 break;
18521 case CPP_OR_OR:
18522 code = TRUTH_ORIF_EXPR;
18523 break;
18524 default:
18525 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18526 resync_fail:
18527 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18528 /*or_comma=*/false,
18529 /*consume_paren=*/true);
18530 return list;
18532 cp_lexer_consume_token (parser->lexer);
18534 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18535 goto resync_fail;
18537 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18538 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18539 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18541 return nlist;
18544 /* OpenMP 2.5:
18545 schedule ( schedule-kind )
18546 schedule ( schedule-kind , expression )
18548 schedule-kind:
18549 static | dynamic | guided | runtime */
18551 static tree
18552 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18554 tree c, t;
18556 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18557 return list;
18559 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18561 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18563 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18564 const char *p = IDENTIFIER_POINTER (id);
18566 switch (p[0])
18568 case 'd':
18569 if (strcmp ("dynamic", p) != 0)
18570 goto invalid_kind;
18571 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18572 break;
18574 case 'g':
18575 if (strcmp ("guided", p) != 0)
18576 goto invalid_kind;
18577 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18578 break;
18580 case 'r':
18581 if (strcmp ("runtime", p) != 0)
18582 goto invalid_kind;
18583 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18584 break;
18586 default:
18587 goto invalid_kind;
18590 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18591 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18592 else
18593 goto invalid_kind;
18594 cp_lexer_consume_token (parser->lexer);
18596 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18598 cp_lexer_consume_token (parser->lexer);
18600 t = cp_parser_assignment_expression (parser, false);
18602 if (t == error_mark_node)
18603 goto resync_fail;
18604 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18605 error ("schedule %<runtime%> does not take "
18606 "a %<chunk_size%> parameter");
18607 else
18608 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18610 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18611 goto resync_fail;
18613 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18614 goto resync_fail;
18616 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18617 OMP_CLAUSE_CHAIN (c) = list;
18618 return c;
18620 invalid_kind:
18621 cp_parser_error (parser, "invalid schedule kind");
18622 resync_fail:
18623 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18624 /*or_comma=*/false,
18625 /*consume_paren=*/true);
18626 return list;
18629 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18630 is a bitmask in MASK. Return the list of clauses found; the result
18631 of clause default goes in *pdefault. */
18633 static tree
18634 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18635 const char *where, cp_token *pragma_tok)
18637 tree clauses = NULL;
18639 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18641 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18642 const char *c_name;
18643 tree prev = clauses;
18645 switch (c_kind)
18647 case PRAGMA_OMP_CLAUSE_COPYIN:
18648 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18649 c_name = "copyin";
18650 break;
18651 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18652 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18653 clauses);
18654 c_name = "copyprivate";
18655 break;
18656 case PRAGMA_OMP_CLAUSE_DEFAULT:
18657 clauses = cp_parser_omp_clause_default (parser, clauses);
18658 c_name = "default";
18659 break;
18660 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18661 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18662 clauses);
18663 c_name = "firstprivate";
18664 break;
18665 case PRAGMA_OMP_CLAUSE_IF:
18666 clauses = cp_parser_omp_clause_if (parser, clauses);
18667 c_name = "if";
18668 break;
18669 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18670 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18671 clauses);
18672 c_name = "lastprivate";
18673 break;
18674 case PRAGMA_OMP_CLAUSE_NOWAIT:
18675 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18676 c_name = "nowait";
18677 break;
18678 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18679 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18680 c_name = "num_threads";
18681 break;
18682 case PRAGMA_OMP_CLAUSE_ORDERED:
18683 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18684 c_name = "ordered";
18685 break;
18686 case PRAGMA_OMP_CLAUSE_PRIVATE:
18687 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18688 clauses);
18689 c_name = "private";
18690 break;
18691 case PRAGMA_OMP_CLAUSE_REDUCTION:
18692 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18693 c_name = "reduction";
18694 break;
18695 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18696 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18697 c_name = "schedule";
18698 break;
18699 case PRAGMA_OMP_CLAUSE_SHARED:
18700 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18701 clauses);
18702 c_name = "shared";
18703 break;
18704 default:
18705 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18706 goto saw_error;
18709 if (((mask >> c_kind) & 1) == 0)
18711 /* Remove the invalid clause(s) from the list to avoid
18712 confusing the rest of the compiler. */
18713 clauses = prev;
18714 error ("%qs is not valid for %qs", c_name, where);
18717 saw_error:
18718 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18719 return finish_omp_clauses (clauses);
18722 /* OpenMP 2.5:
18723 structured-block:
18724 statement
18726 In practice, we're also interested in adding the statement to an
18727 outer node. So it is convenient if we work around the fact that
18728 cp_parser_statement calls add_stmt. */
18730 static unsigned
18731 cp_parser_begin_omp_structured_block (cp_parser *parser)
18733 unsigned save = parser->in_statement;
18735 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18736 This preserves the "not within loop or switch" style error messages
18737 for nonsense cases like
18738 void foo() {
18739 #pragma omp single
18740 break;
18743 if (parser->in_statement)
18744 parser->in_statement = IN_OMP_BLOCK;
18746 return save;
18749 static void
18750 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18752 parser->in_statement = save;
18755 static tree
18756 cp_parser_omp_structured_block (cp_parser *parser)
18758 tree stmt = begin_omp_structured_block ();
18759 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18761 cp_parser_statement (parser, NULL_TREE, false, NULL);
18763 cp_parser_end_omp_structured_block (parser, save);
18764 return finish_omp_structured_block (stmt);
18767 /* OpenMP 2.5:
18768 # pragma omp atomic new-line
18769 expression-stmt
18771 expression-stmt:
18772 x binop= expr | x++ | ++x | x-- | --x
18773 binop:
18774 +, *, -, /, &, ^, |, <<, >>
18776 where x is an lvalue expression with scalar type. */
18778 static void
18779 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18781 tree lhs, rhs;
18782 enum tree_code code;
18784 cp_parser_require_pragma_eol (parser, pragma_tok);
18786 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18787 /*cast_p=*/false);
18788 switch (TREE_CODE (lhs))
18790 case ERROR_MARK:
18791 goto saw_error;
18793 case PREINCREMENT_EXPR:
18794 case POSTINCREMENT_EXPR:
18795 lhs = TREE_OPERAND (lhs, 0);
18796 code = PLUS_EXPR;
18797 rhs = integer_one_node;
18798 break;
18800 case PREDECREMENT_EXPR:
18801 case POSTDECREMENT_EXPR:
18802 lhs = TREE_OPERAND (lhs, 0);
18803 code = MINUS_EXPR;
18804 rhs = integer_one_node;
18805 break;
18807 default:
18808 switch (cp_lexer_peek_token (parser->lexer)->type)
18810 case CPP_MULT_EQ:
18811 code = MULT_EXPR;
18812 break;
18813 case CPP_DIV_EQ:
18814 code = TRUNC_DIV_EXPR;
18815 break;
18816 case CPP_PLUS_EQ:
18817 code = PLUS_EXPR;
18818 break;
18819 case CPP_MINUS_EQ:
18820 code = MINUS_EXPR;
18821 break;
18822 case CPP_LSHIFT_EQ:
18823 code = LSHIFT_EXPR;
18824 break;
18825 case CPP_RSHIFT_EQ:
18826 code = RSHIFT_EXPR;
18827 break;
18828 case CPP_AND_EQ:
18829 code = BIT_AND_EXPR;
18830 break;
18831 case CPP_OR_EQ:
18832 code = BIT_IOR_EXPR;
18833 break;
18834 case CPP_XOR_EQ:
18835 code = BIT_XOR_EXPR;
18836 break;
18837 default:
18838 cp_parser_error (parser,
18839 "invalid operator for %<#pragma omp atomic%>");
18840 goto saw_error;
18842 cp_lexer_consume_token (parser->lexer);
18844 rhs = cp_parser_expression (parser, false);
18845 if (rhs == error_mark_node)
18846 goto saw_error;
18847 break;
18849 finish_omp_atomic (code, lhs, rhs);
18850 cp_parser_consume_semicolon_at_end_of_statement (parser);
18851 return;
18853 saw_error:
18854 cp_parser_skip_to_end_of_block_or_statement (parser);
18858 /* OpenMP 2.5:
18859 # pragma omp barrier new-line */
18861 static void
18862 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18864 cp_parser_require_pragma_eol (parser, pragma_tok);
18865 finish_omp_barrier ();
18868 /* OpenMP 2.5:
18869 # pragma omp critical [(name)] new-line
18870 structured-block */
18872 static tree
18873 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18875 tree stmt, name = NULL;
18877 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18879 cp_lexer_consume_token (parser->lexer);
18881 name = cp_parser_identifier (parser);
18883 if (name == error_mark_node
18884 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18885 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18886 /*or_comma=*/false,
18887 /*consume_paren=*/true);
18888 if (name == error_mark_node)
18889 name = NULL;
18891 cp_parser_require_pragma_eol (parser, pragma_tok);
18893 stmt = cp_parser_omp_structured_block (parser);
18894 return c_finish_omp_critical (stmt, name);
18897 /* OpenMP 2.5:
18898 # pragma omp flush flush-vars[opt] new-line
18900 flush-vars:
18901 ( variable-list ) */
18903 static void
18904 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18906 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18907 (void) cp_parser_omp_var_list (parser, 0, NULL);
18908 cp_parser_require_pragma_eol (parser, pragma_tok);
18910 finish_omp_flush ();
18913 /* Parse the restricted form of the for statment allowed by OpenMP. */
18915 static tree
18916 cp_parser_omp_for_loop (cp_parser *parser)
18918 tree init, cond, incr, body, decl, pre_body;
18919 location_t loc;
18921 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18923 cp_parser_error (parser, "for statement expected");
18924 return NULL;
18926 loc = cp_lexer_consume_token (parser->lexer)->location;
18927 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18928 return NULL;
18930 init = decl = NULL;
18931 pre_body = push_stmt_list ();
18932 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18934 cp_decl_specifier_seq type_specifiers;
18936 /* First, try to parse as an initialized declaration. See
18937 cp_parser_condition, from whence the bulk of this is copied. */
18939 cp_parser_parse_tentatively (parser);
18940 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18941 &type_specifiers);
18942 if (!cp_parser_error_occurred (parser))
18944 tree asm_specification, attributes;
18945 cp_declarator *declarator;
18947 declarator = cp_parser_declarator (parser,
18948 CP_PARSER_DECLARATOR_NAMED,
18949 /*ctor_dtor_or_conv_p=*/NULL,
18950 /*parenthesized_p=*/NULL,
18951 /*member_p=*/false);
18952 attributes = cp_parser_attributes_opt (parser);
18953 asm_specification = cp_parser_asm_specification_opt (parser);
18955 cp_parser_require (parser, CPP_EQ, "`='");
18956 if (cp_parser_parse_definitely (parser))
18958 tree pushed_scope;
18960 decl = start_decl (declarator, &type_specifiers,
18961 /*initialized_p=*/false, attributes,
18962 /*prefix_attributes=*/NULL_TREE,
18963 &pushed_scope);
18965 init = cp_parser_assignment_expression (parser, false);
18967 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18968 asm_specification, LOOKUP_ONLYCONVERTING);
18970 if (pushed_scope)
18971 pop_scope (pushed_scope);
18974 else
18975 cp_parser_abort_tentative_parse (parser);
18977 /* If parsing as an initialized declaration failed, try again as
18978 a simple expression. */
18979 if (decl == NULL)
18980 init = cp_parser_expression (parser, false);
18982 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18983 pre_body = pop_stmt_list (pre_body);
18985 cond = NULL;
18986 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18987 cond = cp_parser_condition (parser);
18988 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18990 incr = NULL;
18991 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18992 incr = cp_parser_expression (parser, false);
18994 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18995 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18996 /*or_comma=*/false,
18997 /*consume_paren=*/true);
18999 /* Note that we saved the original contents of this flag when we entered
19000 the structured block, and so we don't need to re-save it here. */
19001 parser->in_statement = IN_OMP_FOR;
19003 /* Note that the grammar doesn't call for a structured block here,
19004 though the loop as a whole is a structured block. */
19005 body = push_stmt_list ();
19006 cp_parser_statement (parser, NULL_TREE, false, NULL);
19007 body = pop_stmt_list (body);
19009 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19012 /* OpenMP 2.5:
19013 #pragma omp for for-clause[optseq] new-line
19014 for-loop */
19016 #define OMP_FOR_CLAUSE_MASK \
19017 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19018 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19019 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19020 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19021 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19022 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19023 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19025 static tree
19026 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19028 tree clauses, sb, ret;
19029 unsigned int save;
19031 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19032 "#pragma omp for", pragma_tok);
19034 sb = begin_omp_structured_block ();
19035 save = cp_parser_begin_omp_structured_block (parser);
19037 ret = cp_parser_omp_for_loop (parser);
19038 if (ret)
19039 OMP_FOR_CLAUSES (ret) = clauses;
19041 cp_parser_end_omp_structured_block (parser, save);
19042 add_stmt (finish_omp_structured_block (sb));
19044 return ret;
19047 /* OpenMP 2.5:
19048 # pragma omp master new-line
19049 structured-block */
19051 static tree
19052 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19054 cp_parser_require_pragma_eol (parser, pragma_tok);
19055 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19058 /* OpenMP 2.5:
19059 # pragma omp ordered new-line
19060 structured-block */
19062 static tree
19063 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19065 cp_parser_require_pragma_eol (parser, pragma_tok);
19066 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19069 /* OpenMP 2.5:
19071 section-scope:
19072 { section-sequence }
19074 section-sequence:
19075 section-directive[opt] structured-block
19076 section-sequence section-directive structured-block */
19078 static tree
19079 cp_parser_omp_sections_scope (cp_parser *parser)
19081 tree stmt, substmt;
19082 bool error_suppress = false;
19083 cp_token *tok;
19085 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19086 return NULL_TREE;
19088 stmt = push_stmt_list ();
19090 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19092 unsigned save;
19094 substmt = begin_omp_structured_block ();
19095 save = cp_parser_begin_omp_structured_block (parser);
19097 while (1)
19099 cp_parser_statement (parser, NULL_TREE, false, NULL);
19101 tok = cp_lexer_peek_token (parser->lexer);
19102 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19103 break;
19104 if (tok->type == CPP_CLOSE_BRACE)
19105 break;
19106 if (tok->type == CPP_EOF)
19107 break;
19110 cp_parser_end_omp_structured_block (parser, save);
19111 substmt = finish_omp_structured_block (substmt);
19112 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19113 add_stmt (substmt);
19116 while (1)
19118 tok = cp_lexer_peek_token (parser->lexer);
19119 if (tok->type == CPP_CLOSE_BRACE)
19120 break;
19121 if (tok->type == CPP_EOF)
19122 break;
19124 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19126 cp_lexer_consume_token (parser->lexer);
19127 cp_parser_require_pragma_eol (parser, tok);
19128 error_suppress = false;
19130 else if (!error_suppress)
19132 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19133 error_suppress = true;
19136 substmt = cp_parser_omp_structured_block (parser);
19137 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19138 add_stmt (substmt);
19140 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19142 substmt = pop_stmt_list (stmt);
19144 stmt = make_node (OMP_SECTIONS);
19145 TREE_TYPE (stmt) = void_type_node;
19146 OMP_SECTIONS_BODY (stmt) = substmt;
19148 add_stmt (stmt);
19149 return stmt;
19152 /* OpenMP 2.5:
19153 # pragma omp sections sections-clause[optseq] newline
19154 sections-scope */
19156 #define OMP_SECTIONS_CLAUSE_MASK \
19157 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19158 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19159 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19160 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19161 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19163 static tree
19164 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19166 tree clauses, ret;
19168 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19169 "#pragma omp sections", pragma_tok);
19171 ret = cp_parser_omp_sections_scope (parser);
19172 if (ret)
19173 OMP_SECTIONS_CLAUSES (ret) = clauses;
19175 return ret;
19178 /* OpenMP 2.5:
19179 # pragma parallel parallel-clause new-line
19180 # pragma parallel for parallel-for-clause new-line
19181 # pragma parallel sections parallel-sections-clause new-line */
19183 #define OMP_PARALLEL_CLAUSE_MASK \
19184 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19185 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19186 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19187 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19188 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19189 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19190 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19191 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19193 static tree
19194 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19196 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19197 const char *p_name = "#pragma omp parallel";
19198 tree stmt, clauses, par_clause, ws_clause, block;
19199 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19200 unsigned int save;
19202 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19204 cp_lexer_consume_token (parser->lexer);
19205 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19206 p_name = "#pragma omp parallel for";
19207 mask |= OMP_FOR_CLAUSE_MASK;
19208 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19210 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19212 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19213 const char *p = IDENTIFIER_POINTER (id);
19214 if (strcmp (p, "sections") == 0)
19216 cp_lexer_consume_token (parser->lexer);
19217 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19218 p_name = "#pragma omp parallel sections";
19219 mask |= OMP_SECTIONS_CLAUSE_MASK;
19220 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19224 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19225 block = begin_omp_parallel ();
19226 save = cp_parser_begin_omp_structured_block (parser);
19228 switch (p_kind)
19230 case PRAGMA_OMP_PARALLEL:
19231 cp_parser_already_scoped_statement (parser);
19232 par_clause = clauses;
19233 break;
19235 case PRAGMA_OMP_PARALLEL_FOR:
19236 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19237 stmt = cp_parser_omp_for_loop (parser);
19238 if (stmt)
19239 OMP_FOR_CLAUSES (stmt) = ws_clause;
19240 break;
19242 case PRAGMA_OMP_PARALLEL_SECTIONS:
19243 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19244 stmt = cp_parser_omp_sections_scope (parser);
19245 if (stmt)
19246 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19247 break;
19249 default:
19250 gcc_unreachable ();
19253 cp_parser_end_omp_structured_block (parser, save);
19254 stmt = finish_omp_parallel (par_clause, block);
19255 if (p_kind != PRAGMA_OMP_PARALLEL)
19256 OMP_PARALLEL_COMBINED (stmt) = 1;
19257 return stmt;
19260 /* OpenMP 2.5:
19261 # pragma omp single single-clause[optseq] new-line
19262 structured-block */
19264 #define OMP_SINGLE_CLAUSE_MASK \
19265 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19266 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19267 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19268 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19270 static tree
19271 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19273 tree stmt = make_node (OMP_SINGLE);
19274 TREE_TYPE (stmt) = void_type_node;
19276 OMP_SINGLE_CLAUSES (stmt)
19277 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19278 "#pragma omp single", pragma_tok);
19279 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19281 return add_stmt (stmt);
19284 /* OpenMP 2.5:
19285 # pragma omp threadprivate (variable-list) */
19287 static void
19288 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19290 tree vars;
19292 vars = cp_parser_omp_var_list (parser, 0, NULL);
19293 cp_parser_require_pragma_eol (parser, pragma_tok);
19295 if (!targetm.have_tls)
19296 sorry ("threadprivate variables not supported in this target");
19298 finish_omp_threadprivate (vars);
19301 /* Main entry point to OpenMP statement pragmas. */
19303 static void
19304 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19306 tree stmt;
19308 switch (pragma_tok->pragma_kind)
19310 case PRAGMA_OMP_ATOMIC:
19311 cp_parser_omp_atomic (parser, pragma_tok);
19312 return;
19313 case PRAGMA_OMP_CRITICAL:
19314 stmt = cp_parser_omp_critical (parser, pragma_tok);
19315 break;
19316 case PRAGMA_OMP_FOR:
19317 stmt = cp_parser_omp_for (parser, pragma_tok);
19318 break;
19319 case PRAGMA_OMP_MASTER:
19320 stmt = cp_parser_omp_master (parser, pragma_tok);
19321 break;
19322 case PRAGMA_OMP_ORDERED:
19323 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19324 break;
19325 case PRAGMA_OMP_PARALLEL:
19326 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19327 break;
19328 case PRAGMA_OMP_SECTIONS:
19329 stmt = cp_parser_omp_sections (parser, pragma_tok);
19330 break;
19331 case PRAGMA_OMP_SINGLE:
19332 stmt = cp_parser_omp_single (parser, pragma_tok);
19333 break;
19334 default:
19335 gcc_unreachable ();
19338 if (stmt)
19339 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19342 /* The parser. */
19344 static GTY (()) cp_parser *the_parser;
19347 /* Special handling for the first token or line in the file. The first
19348 thing in the file might be #pragma GCC pch_preprocess, which loads a
19349 PCH file, which is a GC collection point. So we need to handle this
19350 first pragma without benefit of an existing lexer structure.
19352 Always returns one token to the caller in *FIRST_TOKEN. This is
19353 either the true first token of the file, or the first token after
19354 the initial pragma. */
19356 static void
19357 cp_parser_initial_pragma (cp_token *first_token)
19359 tree name = NULL;
19361 cp_lexer_get_preprocessor_token (NULL, first_token);
19362 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19363 return;
19365 cp_lexer_get_preprocessor_token (NULL, first_token);
19366 if (first_token->type == CPP_STRING)
19368 name = first_token->u.value;
19370 cp_lexer_get_preprocessor_token (NULL, first_token);
19371 if (first_token->type != CPP_PRAGMA_EOL)
19372 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19374 else
19375 error ("expected string literal");
19377 /* Skip to the end of the pragma. */
19378 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19379 cp_lexer_get_preprocessor_token (NULL, first_token);
19381 /* Now actually load the PCH file. */
19382 if (name)
19383 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19385 /* Read one more token to return to our caller. We have to do this
19386 after reading the PCH file in, since its pointers have to be
19387 live. */
19388 cp_lexer_get_preprocessor_token (NULL, first_token);
19391 /* Normal parsing of a pragma token. Here we can (and must) use the
19392 regular lexer. */
19394 static bool
19395 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19397 cp_token *pragma_tok;
19398 unsigned int id;
19400 pragma_tok = cp_lexer_consume_token (parser->lexer);
19401 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19402 parser->lexer->in_pragma = true;
19404 id = pragma_tok->pragma_kind;
19405 switch (id)
19407 case PRAGMA_GCC_PCH_PREPROCESS:
19408 error ("%<#pragma GCC pch_preprocess%> must be first");
19409 break;
19411 case PRAGMA_OMP_BARRIER:
19412 switch (context)
19414 case pragma_compound:
19415 cp_parser_omp_barrier (parser, pragma_tok);
19416 return false;
19417 case pragma_stmt:
19418 error ("%<#pragma omp barrier%> may only be "
19419 "used in compound statements");
19420 break;
19421 default:
19422 goto bad_stmt;
19424 break;
19426 case PRAGMA_OMP_FLUSH:
19427 switch (context)
19429 case pragma_compound:
19430 cp_parser_omp_flush (parser, pragma_tok);
19431 return false;
19432 case pragma_stmt:
19433 error ("%<#pragma omp flush%> may only be "
19434 "used in compound statements");
19435 break;
19436 default:
19437 goto bad_stmt;
19439 break;
19441 case PRAGMA_OMP_THREADPRIVATE:
19442 cp_parser_omp_threadprivate (parser, pragma_tok);
19443 return false;
19445 case PRAGMA_OMP_ATOMIC:
19446 case PRAGMA_OMP_CRITICAL:
19447 case PRAGMA_OMP_FOR:
19448 case PRAGMA_OMP_MASTER:
19449 case PRAGMA_OMP_ORDERED:
19450 case PRAGMA_OMP_PARALLEL:
19451 case PRAGMA_OMP_SECTIONS:
19452 case PRAGMA_OMP_SINGLE:
19453 if (context == pragma_external)
19454 goto bad_stmt;
19455 cp_parser_omp_construct (parser, pragma_tok);
19456 return true;
19458 case PRAGMA_OMP_SECTION:
19459 error ("%<#pragma omp section%> may only be used in "
19460 "%<#pragma omp sections%> construct");
19461 break;
19463 default:
19464 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19465 c_invoke_pragma_handler (id);
19466 break;
19468 bad_stmt:
19469 cp_parser_error (parser, "expected declaration specifiers");
19470 break;
19473 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19474 return false;
19477 /* The interface the pragma parsers have to the lexer. */
19479 enum cpp_ttype
19480 pragma_lex (tree *value)
19482 cp_token *tok;
19483 enum cpp_ttype ret;
19485 tok = cp_lexer_peek_token (the_parser->lexer);
19487 ret = tok->type;
19488 *value = tok->u.value;
19490 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19491 ret = CPP_EOF;
19492 else if (ret == CPP_STRING)
19493 *value = cp_parser_string_literal (the_parser, false, false);
19494 else
19496 cp_lexer_consume_token (the_parser->lexer);
19497 if (ret == CPP_KEYWORD)
19498 ret = CPP_NAME;
19501 return ret;
19505 /* External interface. */
19507 /* Parse one entire translation unit. */
19509 void
19510 c_parse_file (void)
19512 bool error_occurred;
19513 static bool already_called = false;
19515 if (already_called)
19517 sorry ("inter-module optimizations not implemented for C++");
19518 return;
19520 already_called = true;
19522 the_parser = cp_parser_new ();
19523 push_deferring_access_checks (flag_access_control
19524 ? dk_no_deferred : dk_no_check);
19525 error_occurred = cp_parser_translation_unit (the_parser);
19526 the_parser = NULL;
19529 #include "gt-cp-parser.h"